OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <html> | |
3 <head> | |
4 <script src="../../resources/js-test.js"></script> | |
5 <script src="resources/common.js"></script> | |
6 </head> | |
7 <body> | |
8 <p id="description"></p> | |
9 <div id="console"></div> | |
10 | |
11 <script> | |
12 description("Tests encrypt/decrypt for AES-GCM"); | |
13 | |
14 jsTestIsAsync = true; | |
15 | |
16 // These tests come from the NIST GCM test vectors: | |
17 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/gcmtestvectors.zip | |
18 // | |
19 // Both encryption and decryption are expected to work. | |
20 var kAesGcmSuccessVectors = | |
21 [ | |
22 // [Keylen = 128] | |
23 // [IVlen = 96] | |
24 // [PTlen = 0] | |
25 // [AADlen = 0] | |
26 // [Taglen = 128] | |
27 { | |
28 "key": "cf063a34d4a9a76c2c86787d3f96db71", | |
29 "iv": "113b9785971864c83b01c787", | |
30 "plainText": "", | |
31 "cipherText": "", | |
32 "additionalData": "", | |
33 "authenticationTag": "72ac8493e3a5228b5d130a69d2510e42" | |
34 }, | |
35 | |
36 // [Keylen = 128] | |
37 // [IVlen = 96] | |
38 // [PTlen = 0] | |
39 // [AADlen = 128] | |
40 // [Taglen = 120] | |
41 { | |
42 "key": "6dfa1a07c14f978020ace450ad663d18", | |
43 "iv": "34edfa462a14c6969a680ec1", | |
44 "plainText": "", | |
45 "cipherText": "", | |
46 "additionalData": "2a35c7f5f8578e919a581c60500c04f6", | |
47 "authenticationTag": "751f3098d59cf4ea1d2fb0853bde1c" | |
48 }, | |
49 | |
50 // [Keylen = 128] | |
51 // [IVlen = 96] | |
52 // [PTlen = 128] | |
53 // [AADlen = 128] | |
54 // [Taglen = 112] | |
55 { | |
56 "key": "ed6cd876ceba555706674445c229c12d", | |
57 "iv": "92ecbf74b765bc486383ca2e", | |
58 "plainText": "bfaaaea3880d72d4378561e2597a9b35", | |
59 "cipherText": "bdd2ed6c66fa087dce617d7fd1ff6d93", | |
60 "additionalData": "95bd10d77dbe0e87fb34217f1a2e5efe", | |
61 "authenticationTag": "ba82e49c55a22ed02ca67da4ec6f" | |
62 }, | |
63 | |
64 // [Keylen = 256] | |
65 // [IVlen = 1024] | |
66 // [PTlen = 408] | |
67 // [AADlen = 720] | |
68 // [Taglen = 32] | |
69 { | |
70 "key": "e03548984a7ec8eaf0870637df0ac6bc17f7159315d0ae26a764fd224e483810", | |
71 "iv": "f4feb26b846be4cd224dbc5133a5ae13814ebe19d3032acdd3a006463fdb71e83a9d5
d96679f26cc1719dd6b4feb3bab5b4b7993d0c0681f36d105ad3002fb66b201538e2b7479838ab83
402b0d816cd6e0fe5857e6f4adf92de8ee72b122ba1ac81795024943b7d0151bbf84ce87c8911f51
2c397d14112296da7ecdd0da52a", | |
72 "cipherText": "fda718aa1ec163487e21afc34f5a3a34795a9ee71dd3e7ee9a18fdb24181d
c982b29c6ec723294a130ca2234952bb0ef68c0f3", | |
73 "additionalData": "aab26eb3e7acd09a034a9e2651636ab3868e51281590ecc948355e457
da42b7ad1391c7be0d9e82895e506173a81857c3226829fbd6dfb3f9657a71a2934445d7c05fa940
1cddd5109016ba32c3856afaadc48de80b8a01b57cb", | |
74 "authenticationTag": "4795fbe0", | |
75 "plainText": "69fd0c9da10b56ec6786333f8d76d4b74f8a434195f2f241f088b2520fb5fa
29455df9893164fb1638abe6617915d9497a8fe2" | |
76 } | |
77 ]; | |
78 | |
79 function runAesGcmSuccessTestCase(testCase) | |
80 { | |
81 var key = null; | |
82 var keyData = hexStringToUint8Array(testCase.key); | |
83 var iv = hexStringToUint8Array(testCase.iv); | |
84 var additionalData = hexStringToUint8Array(testCase.additionalData); | |
85 var tag = hexStringToUint8Array(testCase.authenticationTag); | |
86 var usages = ['encrypt', 'decrypt']; | |
87 var extractable = false; | |
88 | |
89 var tagLengthBits = tag.byteLength * 8; | |
90 | |
91 var algorithm = {name: 'aes-gcm', iv: iv, additionalData: additionalData, ta
gLength: tagLengthBits}; | |
92 | |
93 // (1) Import the key | |
94 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage
s).then(function(result) { | |
95 key = result; | |
96 | |
97 // shouldBe() can only resolve variables in global context. | |
98 tmpKey = key; | |
99 shouldEvaluateAs("tmpKey.type", "secret"); | |
100 shouldEvaluateAs("tmpKey.extractable", false); | |
101 shouldEvaluateAs("tmpKey.algorithm.name", "AES-GCM"); | |
102 shouldEvaluateAs("tmpKey.usages.join(',')", "encrypt,decrypt"); | |
103 | |
104 // (2) Encrypt. | |
105 return crypto.subtle.encrypt(algorithm, key, hexStringToUint8Array(testC
ase.plainText)); | |
106 }).then(function(result) { | |
107 bytesShouldMatchHexString("Encryption", testCase.cipherText + testCase.a
uthenticationTag, result); | |
108 | |
109 // (3) Decrypt | |
110 return crypto.subtle.decrypt(algorithm, key, hexStringToUint8Array(testC
ase.cipherText + testCase.authenticationTag)); | |
111 }).then(function(result) { | |
112 bytesShouldMatchHexString("Decryption", testCase.plainText, result); | |
113 }); | |
114 } | |
115 | |
116 var lastPromise = Promise.resolve(null); | |
117 | |
118 kAesGcmSuccessVectors.forEach(function(test) { | |
119 lastPromise = lastPromise.then(runAesGcmSuccessTestCase.bind(null, test)); | |
120 }); | |
121 | |
122 lastPromise.then(finishJSTest, failAndFinishJSTest); | |
123 | |
124 </script> | |
125 | |
126 </body> | |
127 </html> | |
OLD | NEW |