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 19, 2023
1 parent b929aca commit 289f541
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
29 changes: 14 additions & 15 deletions src/kms/kms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,12 +698,12 @@ export class KmsClient {
public async coverCryptEncrypt(
uniqueIdentifier: string,
accessPolicy: string,
data: Uint8Array | Uint8Array[],
data: Uint8Array | [],
options: {
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,15 @@ 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]))

console.log(`Enc chunks: ${encryptedChunks.toString()}`)
}

return encryptedChunks
Expand All @@ -788,11 +787,14 @@ export class KmsClient {
*/
public async coverCryptDecrypt(
uniqueIdentifier: string,
data: Uint8Array | Uint8Array[],
data: Uint8Array | [],
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 +826,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
9 changes: 4 additions & 5 deletions tests/KMS.test.ts
Original file line number Diff line number Diff line change
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

0 comments on commit 289f541

Please sign in to comment.