Skip to content

Commit

Permalink
test add param
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibsG committed Sep 20, 2023
1 parent b929aca commit 249e4f4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
23 changes: 10 additions & 13 deletions src/kms/kms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ export class KmsClient {
headerMetadata?: Uint8Array
authenticationData?: Uint8Array
} = {},
): Promise<Uint8Array> {
): Promise<Uint8Array | Uint8Array[]> {
const accessPolicyBytes = new TextEncoder().encode(accessPolicy)
const accessPolicySize = encode(accessPolicyBytes.length)

Expand Down Expand Up @@ -764,16 +764,13 @@ export class KmsClient {

let { result: nbChunks, tail: tailPlaintext } = decode(encryptedData)

let encryptedChunks = Uint8Array.from([])
const encryptedChunks = []
for (let i = 0; i < nbChunks; i++) {
const { result: chunkSize, tail } = decode(tailPlaintext)
const chunk = tail.slice(0, chunkSize)
tailPlaintext = tail.slice(chunkSize)

encryptedChunks = Uint8Array.from([
...encryptedChunks,
...Uint8Array.from([...chunk]),
])
encryptedChunks.push(new Uint8Array([...chunk]))
}

return encryptedChunks
Expand All @@ -792,7 +789,10 @@ export class KmsClient {
options: {
authenticationData?: Uint8Array
} = {},
): Promise<{ headerMetadata: Uint8Array; plaintext: Uint8Array }> {
): Promise<{
headerMetadata: Uint8Array
plaintext: Uint8Array | Uint8Array[]
}> {
const cryptographicParameters = new CryptographicParameters()

if (data instanceof Uint8Array) {
Expand Down Expand Up @@ -824,23 +824,20 @@ export class KmsClient {

const { result: headerMetadataLength, tail } = decode(response.data)
const headerMetadata = tail.slice(0, headerMetadataLength)
let plaintext = tail.slice(headerMetadataLength)
let plaintext: Uint8Array | Uint8Array[] = tail.slice(headerMetadataLength)

if (
cryptographicParameters.cryptographicAlgorithm ===
CryptographicAlgorithm.CoverCryptBulk
) {
let { result: nbChunks, tail: tailPlaintext } = decode(plaintext)
let decryptedChunks = Uint8Array.from([])
const decryptedChunks = []
for (let i = 0; i < nbChunks; i++) {
const { result: chunkSize, tail } = decode(tailPlaintext)
const chunk = tail.slice(0, chunkSize)
tailPlaintext = tail.slice(chunkSize)

decryptedChunks = Uint8Array.from([
...decryptedChunks,
...Uint8Array.from([...chunk]),
])
decryptedChunks.push(new Uint8Array([...chunk]))

plaintext = decryptedChunks
}
Expand Down
21 changes: 10 additions & 11 deletions tests/KMS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,11 @@ test(

// encryption
const plaintext2 = new TextEncoder().encode("abcdefgh")
const ciphertext2 = await client.coverCryptEncrypt(
const ciphertext2 = (await client.coverCryptEncrypt(
mpkID,
"Department::FIN && Security Level::Confidential",
plaintext2,
)
)) as Uint8Array

// decryption
{
Expand Down Expand Up @@ -758,11 +758,10 @@ test(
expect(AccessPolicy.fromKey(udk).booleanAccessPolicy).toEqual(apb)

// encryption
const plaintext = Uint8Array.from([
...new TextEncoder().encode("abcdefgh"),
...new TextEncoder().encode("azertyui"),
...new TextEncoder().encode("qsdfghjk"),
])
const plaintext = []
plaintext.push(new TextEncoder().encode("abcdefgh"))
plaintext.push(new TextEncoder().encode("azertyui"))
plaintext.push(new TextEncoder().encode("qsdfghjk"))

const ciphertext = await client.coverCryptEncrypt(
mpkID,
Expand Down Expand Up @@ -907,11 +906,11 @@ test(
)

const oldPlaintext = Uint8Array.from([1, 2, 3])
const oldKmsCiphertext = await client.coverCryptEncrypt(
const oldKmsCiphertext = (await client.coverCryptEncrypt(
mpkID,
"Security::Simple",
oldPlaintext,
)
)) as Uint8Array
const oldLocalCiphertext = oldLocalEncryption.encrypt(
"Security::Simple",
oldPlaintext,
Expand Down Expand Up @@ -952,11 +951,11 @@ test(
expect(newPublicKey.bytes()).not.toEqual(oldPublicKey.bytes())

const newPlaintext = Uint8Array.from([4, 5, 6])
const newKmsCiphertext = await client.coverCryptEncrypt(
const newKmsCiphertext = (await client.coverCryptEncrypt(
mpkID,
"Security::Simple",
newPlaintext,
)
)) as Uint8Array
const newLocalCiphertext = newLocalEncryption.encrypt(
"Security::Simple",
newPlaintext,
Expand Down
8 changes: 4 additions & 4 deletions tests/cover_crypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ test("Demo using KMS", async () => {
//
// a protected marketing message
const protectedMkgData = new TextEncoder().encode("protected_mkg_message")
const protectedMkgCiphertext = await client.coverCryptEncrypt(
const protectedMkgCiphertext = (await client.coverCryptEncrypt(
masterPublicKeyUID,
"Department::MKG && Security Level::Protected",
protectedMkgData,
)
)) as Uint8Array

// a top-secret marketing message
const topSecretMkgData = new TextEncoder().encode("top_secret_mkg_message")
Expand Down Expand Up @@ -327,11 +327,11 @@ test("Demo using KMS", async () => {
const confidentialMkgData = new TextEncoder().encode(
"confidential_mkg_message",
)
const newConfidentialMkgCiphertext = await client.coverCryptEncrypt(
const newConfidentialMkgCiphertext = (await client.coverCryptEncrypt(
masterPublicKeyUID,
"Department::MKG && Security Level::Confidential",
confidentialMkgData,
)
)) as Uint8Array

// The automatically rekeyed confidential marketing user key can still decrypt
// the "old" `protected marketing` message, as well as the new `confidential marketing` message.
Expand Down

0 comments on commit 249e4f4

Please sign in to comment.