Skip to content

Commit d318f95

Browse files
committed
fix: WebCrypto API passes the tag with encrypted data.
See: aws#237 Since the WebCrypto decrypt API expects the AES-GCM tag with the encrypted data, zero bytes of encrypted data is not zero bytes of data.
1 parent 4b06f62 commit d318f95

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

modules/material-management-browser/src/material_helpers.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,16 @@ export function getSubtleFunction<T extends WebCryptoMaterial<T>> (
175175
const { nonZeroByteSubtle, zeroByteSubtle } = backend
176176
const { nonZeroByteCryptoKey, zeroByteCryptoKey } = deriveKey
177177
const algorithm = { name: cipherName, iv, additionalData, tagLength }
178-
if (data.byteLength) {
179-
return nonZeroByteSubtle[subtleFunction](algorithm, nonZeroByteCryptoKey, data)
180-
} else {
178+
/* Precondition: The WebCrypto AES-GCM decrypt API expects the data *and* tag together.
179+
* This means that on decrypt any amount of data less than tagLength is invalid.
180+
* This also means that zero encrypted data will be equal to tagLength.
181+
*/
182+
const dataByteLength = subtleFunction === 'decrypt' ? data.byteLength - tagLength / 8 : data.byteLength
183+
needs(dataByteLength >= 0, 'Invalid data length.')
184+
if (dataByteLength === 0) {
181185
return zeroByteSubtle[subtleFunction](algorithm, zeroByteCryptoKey, data)
186+
} else {
187+
return nonZeroByteSubtle[subtleFunction](algorithm, nonZeroByteCryptoKey, data)
182188
}
183189
}
184190
// This should be impossible

0 commit comments

Comments
 (0)