OLD | NEW |
| (Empty) |
1 if (self.importScripts) { | |
2 importScripts('../../resources/js-test.js'); | |
3 importScripts('common.js'); | |
4 } | |
5 | |
6 function shouldEvaluateAsSilent(expressionToEval, expectedResult) | |
7 { | |
8 var result = eval(expressionToEval); | |
9 if (result !== expectedResult) { | |
10 testFailed(expressionToEval + " evaluated to " + result + " instead of "
+ expectedResult); | |
11 } | |
12 } | |
13 | |
14 function doPostMessage(data) | |
15 { | |
16 if (isWorker()) | |
17 self.postMessage(data); | |
18 else | |
19 self.postMessage(data, '*'); | |
20 } | |
21 | |
22 function notifySuccess() | |
23 { | |
24 doPostMessage("TEST_FINISHED"); | |
25 } | |
26 | |
27 function notifyFailure(details) | |
28 { | |
29 doPostMessage("FAIL:" + details); | |
30 } | |
31 | |
32 function testGenerateRsaKey() | |
33 { | |
34 var extractable = false; | |
35 var usages = ['sign', 'verify']; | |
36 // Note that the modulus length is unusually small in order to speed up | |
37 // the test (1024 or 2048 would be more typical). | |
38 var algorithm = {name: "RSASSA-PKCS1-v1_5", modulusLength: 256, publicExpone
nt: hexStringToUint8Array("010001"), hash: {name: "SHA-256"}}; | |
39 | |
40 return crypto.subtle.generateKey(algorithm, extractable, usages).then(functi
on(result) { | |
41 publicKey = result.publicKey; | |
42 privateKey = result.privateKey; | |
43 | |
44 shouldEvaluateAsSilent("publicKey.type", "public"); | |
45 shouldEvaluateAsSilent("publicKey.extractable", true); | |
46 shouldEvaluateAsSilent("publicKey.algorithm.name", algorithm.name); | |
47 shouldEvaluateAsSilent("publicKey.algorithm.modulusLength", algorithm.mo
dulusLength); | |
48 shouldEvaluateAsSilent("bytesToHexString(publicKey.algorithm.publicExpon
ent)", "010001"); | |
49 | |
50 shouldEvaluateAsSilent("privateKey.type", "private"); | |
51 shouldEvaluateAsSilent("privateKey.extractable", false); | |
52 shouldEvaluateAsSilent("privateKey.algorithm.name", algorithm.name); | |
53 shouldEvaluateAsSilent("privateKey.algorithm.modulusLength", algorithm.m
odulusLength); | |
54 shouldEvaluateAsSilent("bytesToHexString(privateKey.algorithm.publicExpo
nent)", "010001"); | |
55 }); | |
56 } | |
57 | |
58 // Very similar to "hmac-sign-verify.html". | |
59 function testHmac() | |
60 { | |
61 var importAlgorithm = {name: 'HMAC', hash: {name: "SHA-256"}}; | |
62 var algorithm = {name: 'HMAC'}; | |
63 | |
64 var key = null; | |
65 | |
66 var testCase = { | |
67 hash: "SHA-256", | |
68 key: "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a
8f58ffefa176", | |
69 message: "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc28
8baf4a92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92d1b0
ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f22a003b8ab8de5
4f6ded0e3ab9245fa79568451dfa258e", | |
70 mac: "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b" | |
71 }; | |
72 | |
73 var keyData = hexStringToUint8Array(testCase.key); | |
74 var usages = ['sign', 'verify']; | |
75 var extractable = true; | |
76 | |
77 // (1) Import the key | |
78 return crypto.subtle.importKey('raw', keyData, importAlgorithm, extractable,
usages).then(function(result) { | |
79 key = result; | |
80 | |
81 // shouldBe() can only resolve variables in global context. | |
82 tmpKey = key; | |
83 shouldEvaluateAsSilent("tmpKey.type", "secret"); | |
84 shouldEvaluateAsSilent("tmpKey.extractable", true); | |
85 shouldEvaluateAsSilent("tmpKey.algorithm.name", "HMAC"); | |
86 shouldEvaluateAsSilent("tmpKey.algorithm.hash.name", testCase.hash); | |
87 shouldEvaluateAsSilent("tmpKey.algorithm.length", keyData.length * 8); | |
88 shouldEvaluateAsSilent("tmpKey.usages.join(',')", "sign,verify"); | |
89 | |
90 // (2) Sign. | |
91 var signPromise = crypto.subtle.sign(algorithm, key, hexStringToUint8Arr
ay(testCase.message)); | |
92 | |
93 // (3) Verify | |
94 var verifyPromise = crypto.subtle.verify(algorithm, key, hexStringToUint
8Array(testCase.mac), hexStringToUint8Array(testCase.message)); | |
95 | |
96 // (4) Verify truncated mac (by stripping 1 byte off of it). | |
97 var expectedMac = hexStringToUint8Array(testCase.mac); | |
98 var verifyTruncatedPromise = crypto.subtle.verify(algorithm, key, expect
edMac.subarray(0, expectedMac.byteLength - 1), hexStringToUint8Array(testCase.me
ssage)); | |
99 | |
100 var exportKeyPromise = crypto.subtle.exportKey('raw', key); | |
101 | |
102 return Promise.all([signPromise, verifyPromise, verifyTruncatedPromise,
exportKeyPromise]); | |
103 }).then(function(result) { | |
104 // signPromise | |
105 mac = result[0]; | |
106 shouldEvaluateAsSilent("bytesToHexString(mac)", testCase.mac); | |
107 | |
108 // verifyPromise | |
109 verifyResult = result[1]; | |
110 shouldEvaluateAsSilent("verifyResult", true); | |
111 | |
112 // verifyTruncatedPromise | |
113 verifyResult = result[2]; | |
114 shouldEvaluateAsSilent("verifyResult", false); | |
115 | |
116 // exportKeyPromise | |
117 exportedKeyData = result[3]; | |
118 shouldEvaluateAsSilent("bytesToHexString(exportedKeyData)", testCase.key
); | |
119 }); | |
120 } | |
121 | |
122 // Very similar to aes-gcm-encrypt-decrypt.hml | |
123 function testAesGcm() | |
124 { | |
125 var testCase = { | |
126 "key": "e03548984a7ec8eaf0870637df0ac6bc17f7159315d0ae26a764fd224e483810", | |
127 "iv": "f4feb26b846be4cd224dbc5133a5ae13814ebe19d3032acdd3a006463fdb71e83a9
d5d96679f26cc1719dd6b4feb3bab5b4b7993d0c0681f36d105ad3002fb66b201538e2b7479838ab
83402b0d816cd6e0fe5857e6f4adf92de8ee72b122ba1ac81795024943b7d0151bbf84ce87c8911f
512c397d14112296da7ecdd0da52a", | |
128 "cipherText": "fda718aa1ec163487e21afc34f5a3a34795a9ee71dd3e7ee9a18fdb2418
1dc982b29c6ec723294a130ca2234952bb0ef68c0f3", | |
129 "additionalData": "aab26eb3e7acd09a034a9e2651636ab3868e51281590ecc948355e4
57da42b7ad1391c7be0d9e82895e506173a81857c3226829fbd6dfb3f9657a71a2934445d7c05fa9
401cddd5109016ba32c3856afaadc48de80b8a01b57cb", | |
130 "authenticationTag": "4795fbe0", | |
131 "plainText": "69fd0c9da10b56ec6786333f8d76d4b74f8a434195f2f241f088b2520fb5
fa29455df9893164fb1638abe6617915d9497a8fe2" | |
132 } | |
133 | |
134 var key = null; | |
135 var keyData = hexStringToUint8Array(testCase.key); | |
136 var iv = hexStringToUint8Array(testCase.iv); | |
137 var additionalData = hexStringToUint8Array(testCase.additionalData); | |
138 var tag = hexStringToUint8Array(testCase.authenticationTag); | |
139 var usages = ['encrypt', 'decrypt']; | |
140 var extractable = false; | |
141 | |
142 var tagLengthBits = tag.byteLength * 8; | |
143 | |
144 var algorithm = {name: 'aes-gcm', iv: iv, additionalData: additionalData, ta
gLength: tagLengthBits}; | |
145 | |
146 // (1) Import the key | |
147 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage
s).then(function(result) { | |
148 key = result; | |
149 | |
150 // shouldBe() can only resolve variables in global context. | |
151 tmpKey = key; | |
152 shouldEvaluateAsSilent("tmpKey.type", "secret"); | |
153 shouldEvaluateAsSilent("tmpKey.extractable", false); | |
154 shouldEvaluateAsSilent("tmpKey.algorithm.name", "AES-GCM"); | |
155 shouldEvaluateAsSilent("tmpKey.usages.join(',')", "encrypt,decrypt"); | |
156 | |
157 // (2) Encrypt | |
158 var encryptPromise1 = crypto.subtle.encrypt(algorithm, key, hexStringToU
int8Array(testCase.plainText)); | |
159 var encryptPromise2 = crypto.subtle.encrypt(algorithm, key, hexStringToU
int8Array(testCase.plainText)); | |
160 | |
161 // (3) Decrypt | |
162 var decryptPromise1 = crypto.subtle.decrypt(algorithm, key, hexStringToU
int8Array(testCase.cipherText + testCase.authenticationTag)); | |
163 var decryptPromise2 = crypto.subtle.decrypt(algorithm, key, hexStringToU
int8Array(testCase.cipherText + testCase.authenticationTag)); | |
164 | |
165 return Promise.all([encryptPromise1, encryptPromise2, decryptPromise1, d
ecryptPromise2]); | |
166 }).then(function(result) { | |
167 // encryptPromise1, encryptPromise2 | |
168 for (var i = 0; i < 2; ++i) { | |
169 cipherText = result[i]; | |
170 shouldEvaluateAsSilent("bytesToHexString(cipherText)", testCase.ciph
erText + testCase.authenticationTag); | |
171 } | |
172 | |
173 // decryptPromise1, decryptPromise2 | |
174 for (var i = 0; i < 2; ++i) { | |
175 plainText = result[2 + i]; | |
176 shouldEvaluateAsSilent("bytesToHexString(plainText)", testCase.plain
Text); | |
177 } | |
178 }); | |
179 } | |
180 | |
181 Promise.all([ | |
182 testHmac(), | |
183 testGenerateRsaKey(), | |
184 testAesGcm(), | |
185 testHmac(), | |
186 testAesGcm(), | |
187 ]).then(notifySuccess, notifyFailure); | |
OLD | NEW |