4 <script src=
"../../../resources/js-test.js"></script>
5 <script src=
"../resources/common.js"></script>
8 <p id=
"description"></p>
9 <div id=
"console"></div>
12 description("Tests encrypt/decrypt for AES-CBC");
16 // -------------------------------------------------
17 // Successful encryption/decryption
18 // -------------------------------------------------
20 // Test vectors marked with [1] were copied from:
21 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
23 // The NIST tests do not have a padding block. To match the WebCrypto
24 // expectations, a PKCS#5 padding block has been added.
26 var kAesCbcSuccessVectors
= [
27 // 128-bit key with plaintext that is an exact multiple of block size.
28 // Derived from [1] F.2.1 (CBC-AES128.Encrypt), by adding padding block.
30 key
: "2b7e151628aed2a6abf7158809cf4f3c",
31 iv
: "000102030405060708090a0b0c0d0e0f",
32 plainText
: "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
33 cipherText
: "7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7" +
35 "8cb82807230e1321d3fae00d18cc2012"
38 // 256-bit key, where final block of plaintext has to pad by 3.
39 // Derived from [1] F.2.6 CBC-AES256.Decrypt, by stripping 3 bytes off
40 // plaintext and adding padding block.
42 key
: "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
43 iv
: "000102030405060708090a0b0c0d0e0f",
44 plainText
: "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be6",
45 cipherText
: "f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461c9aaf02a6a54e9e242ccbf48c59daca6"
48 // 128-bit key, with empty plaintext.
49 // Derived from Chromium's EncryptorTest.EmptyEncrypt() (encryptor_unittest.cc)
51 key
: "3132383d5369787465656e4279746573",
52 iv
: "5377656574205369787465656e204956",
54 cipherText
: "8518b8878d34e7185e300d0fcc426396"
58 function runAesCbcSuccessTestCase(testCase
)
60 var algorithm
= {name
: 'aes-cbc', iv
: hexStringToUint8Array(testCase
.iv
)};
63 var keyData
= hexStringToUint8Array(testCase
.key
);
64 var usages
= ['encrypt', 'decrypt'];
65 var extractable
= false;
68 return crypto
.subtle
.importKey('raw', keyData
, algorithm
, extractable
, usages
).then(function(result
) {
71 // shouldBe() can only resolve variables in global context.
73 shouldEvaluateAs("tmpKey.type", "secret");
74 shouldEvaluateAs("tmpKey.extractable", false);
75 shouldEvaluateAs("tmpKey.algorithm.name", "AES-CBC");
76 shouldEvaluateAs("tmpKey.algorithm.length", keyData
.byteLength
* 8);
77 shouldEvaluateAs("tmpKey.usages.join(',')", "encrypt,decrypt");
80 return crypto
.subtle
.encrypt(algorithm
, key
, hexStringToUint8Array(testCase
.plainText
));
81 }).then(function(result
) {
82 bytesShouldMatchHexString("Encryption", testCase
.cipherText
, result
);
85 return crypto
.subtle
.decrypt(algorithm
, key
, hexStringToUint8Array(testCase
.cipherText
));
86 }).then(function(result
) {
87 bytesShouldMatchHexString("Decryption", testCase
.plainText
, result
);
91 var lastPromise
= Promise
.resolve(null);
93 kAesCbcSuccessVectors
.forEach(function(test
) {
94 lastPromise
= lastPromise
.then(runAesCbcSuccessTestCase
.bind(null, test
));
97 lastPromise
.then(finishJSTest
, failAndFinishJSTest
);