Skip to content

Commit e87deca

Browse files
jpowersdevpanva
authored andcommitted
WebCryptoAPI: test AES-CBC decryption with invalid ciphertext lengths
Check that 0-, 1-, 15-, 17-, 31-, and 33-byte ciphertexts reject with OperationError for 128-, 192-, and 256-bit AES-CBC keys. Also check that empty plaintext encrypts to one padding block and decrypts successfully. Run in secure window and dedicated worker contexts. Upstream the regression coverage from: WebKit/WebKit#77307 https://bugs.webkit.org/show_bug.cgi?id=327745 Developed and adapted with assistance from OpenAI Codex.
1 parent 2fbf9d3 commit e87deca

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// META: title=WebCryptoAPI: AES-CBC decryption ciphertext lengths
2+
// META: global=window,dedicatedworker
3+
4+
/*
5+
* Copyright (C) 2026 Jonathan Powers. All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND ITS CONTRIBUTORS ``AS IS'' AND ANY
17+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ITS CONTRIBUTORS BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
// https://w3c.github.io/webcrypto/#aes-cbc-operations
29+
// Decryption requires a nonempty ciphertext whose length is a multiple of 16 bytes.
30+
for (const keyLength of [128, 192, 256]) {
31+
for (const cipherTextLength of [0, 1, 15, 17, 31, 33]) {
32+
promise_test(async t => {
33+
const key = await crypto.subtle.importKey("raw", new Uint8Array(keyLength / 8), "AES-CBC", false, ["decrypt"]);
34+
const parameters = { name: "AES-CBC", iv: new Uint8Array(16) };
35+
await promise_rejects_dom(t, "OperationError", crypto.subtle.decrypt(parameters, key, new Uint8Array(cipherTextLength)));
36+
}, `AES-CBC with a ${keyLength}-bit key rejects ${cipherTextLength}-byte ciphertext with OperationError`);
37+
}
38+
39+
promise_test(async () => {
40+
const key = await crypto.subtle.importKey("raw", new Uint8Array(keyLength / 8), "AES-CBC", false, ["encrypt", "decrypt"]);
41+
const parameters = { name: "AES-CBC", iv: new Uint8Array(16) };
42+
const cipherText = await crypto.subtle.encrypt(parameters, key, new Uint8Array(0));
43+
assert_equals(cipherText.byteLength, 16, "Encrypting empty plaintext produces a full padding block");
44+
const plainText = await crypto.subtle.decrypt(parameters, key, cipherText);
45+
assert_equals(plainText.byteLength, 0, "Decrypting a full padding block produces empty plaintext");
46+
}, `AES-CBC with a ${keyLength}-bit key decrypts a padded empty message`);
47+
}

0 commit comments

Comments
 (0)