Skip to content

Commit

Permalink
refactor(node): skip concat on counter mode de/encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Nov 3, 2021
1 parent 0c6d3f3 commit c373148
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/runtime/node/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ async function cbcDecrypt(

let plaintext!: Uint8Array
try {
const cipher = createDecipheriv(algorithm, encKey, iv)
plaintext = concat(cipher.update(ciphertext), cipher.final())
const decipher = createDecipheriv(algorithm, encKey, iv)
plaintext = concat(decipher.update(ciphertext), decipher.final())
} catch {
//
}
Expand All @@ -77,13 +77,15 @@ async function gcmDecrypt(
throw new JOSENotSupported(`alg ${enc} is not supported by your javascript runtime`)
}
try {
const cipher = createDecipheriv(algorithm, cek, iv, { authTagLength: 16 })
cipher.setAuthTag(tag)
const decipher = createDecipheriv(algorithm, cek, iv, { authTagLength: 16 })
decipher.setAuthTag(tag)
if (aad.byteLength) {
cipher.setAAD(aad, { plaintextLength: ciphertext.length })
decipher.setAAD(aad, { plaintextLength: ciphertext.length })
}

return concat(cipher.update(ciphertext), cipher.final())
const plaintext = decipher.update(ciphertext)
decipher.final()
return plaintext
} catch {
throw new JWEDecryptionFailed()
}
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/node/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ async function gcmEncrypt(
cipher.setAAD(aad, { plaintextLength: plaintext.length })
}

const ciphertext = concat(cipher.update(plaintext), cipher.final())
const ciphertext = cipher.update(plaintext)
cipher.final()
const tag = cipher.getAuthTag()

return { ciphertext, tag }
Expand Down

0 comments on commit c373148

Please sign in to comment.