5 <title>WebCrypto Test Suite
</title>
6 <meta http-equiv=
"Content-Type" content=
"text/html;charset=utf-8" />
7 <link rel=
"stylesheet" href=
"./test_WebCrypto.css"/>
8 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
10 <!-- Utilities for manipulating ABVs -->
11 <script src=
"util.js"></script>
13 <!-- A simple wrapper around IndexedDB -->
14 <script src=
"simpledb.js"></script>
16 <!-- Test vectors drawn from the literature -->
17 <script src=
"./test-vectors.js"></script>
19 <!-- General testing framework -->
20 <script src=
"./test-array.js"></script>
22 <script>/* <![CDATA[*/
25 // Generating 2048-bit keys takes some time.
26 SimpleTest
.requestLongerTimeout(2);
28 // -----------------------------------------------------------------------------
30 "RSA-PSS key generation (SHA-1, 1024-bit)",
37 publicExponent
: new Uint8Array([0x01, 0x00, 0x01]),
40 crypto
.subtle
.generateKey(alg
, false, ["sign", "verify"])
41 .then(complete(that
), error(that
));
45 // -----------------------------------------------------------------------------
47 "RSA-PSS key generation and sign/verify round-trip (SHA-256, 2048-bit)",
54 publicExponent
: new Uint8Array([0x01, 0x00, 0x01]),
58 var data
= crypto
.getRandomValues(new Uint8Array(128));
59 function setKey(x
) { pubKey
= x
.publicKey
; privKey
= x
.privateKey
; }
61 return crypto
.subtle
.sign({name
: "RSA-PSS", saltLength
: 32}, privKey
, data
);
63 function doVerify(x
) {
64 return crypto
.subtle
.verify({name
: "RSA-PSS", saltLength
: 32}, pubKey
, x
, data
);
67 crypto
.subtle
.generateKey(alg
, false, ["sign", "verify"])
68 .then(setKey
, error(that
))
69 .then(doSign
, error(that
))
70 .then(doVerify
, error(that
))
71 .then(complete(that
, x
=> x
), error(that
));
75 // -----------------------------------------------------------------------------
77 "RSA-PSS verify known signature (SHA-1, 1024-bit)",
80 var alg
= {name
: "RSA-PSS", hash
: "SHA-1"};
83 function doVerify(x
) {
84 return crypto
.subtle
.verify({name
: "RSA-PSS", saltLength
: vec
.saltLength
}, x
, vec
.sig
, vec
.data
);
87 crypto
.subtle
.importKey("spki", vec
.spki
, alg
, false, ["verify"])
88 .then(doVerify
, error(that
))
89 .then(complete(that
, x
=> x
), error(that
));
93 // -----------------------------------------------------------------------------
95 "Test invalid RSA-PSS signatures",
98 var alg
= {name
: "RSA-PSS", hash
: "SHA-1"};
101 function doVerify(x
) {
102 var algo
= {name
: "RSA-PSS", saltLength
: vec
.saltLength
};
103 var clone1
= new Uint8Array(vec
.data
);
104 var clone2
= new Uint8Array(vec
.data
);
105 clone1
[clone1
.byteLength
- 1] ^= 1;
109 crypto
.subtle
.verify(algo
, x
, vec
.sig
, clone1
),
110 crypto
.subtle
.verify(algo
, x
, vec
.sig
, clone2
),
111 crypto
.subtle
.verify(algo
, x
, vec
.sig
, vec
.data
.slice(1)),
112 crypto
.subtle
.verify(algo
, x
, vec
.sig
, vec
.data
.slice(0, vec
.data
.byteLength
- 1)),
116 crypto
.subtle
.importKey("spki", vec
.spki
, alg
, false, ["verify"])
117 .then(doVerify
, error(that
))
118 .then(results
=> results
.every(x
=> !x
))
119 .then(complete(that
, x
=> x
), error(that
));
123 // -----------------------------------------------------------------------------
125 "RSA-PSS verify known signature (SHA-1, 1024-bit, JWK)",
128 var alg
= {name
: "RSA-PSS", hash
: "SHA-1"};
130 function doVerify(x
) {
131 return crypto
.subtle
.verify({name
: "RSA-PSS", saltLength
: tv
.rsapss
.saltLength
}, x
, tv
.rsapss
.sig
, tv
.rsapss
.data
);
134 crypto
.subtle
.importKey("jwk", tv
.rsapss
.jwk_pub
, alg
, false, ["verify"])
135 .then(doVerify
, error(that
))
136 .then(complete(that
, x
=> x
), error(that
));
140 // -----------------------------------------------------------------------------
142 "RSA-PSS verify known signatures (SHA-1 to SHA-512, 1024-bit)",
146 function verifyCase(hash
, tv
) {
147 var alg
= {name
: "RSA-PSS", hash
, saltLength
: tv
.saltLength
};
148 return crypto
.subtle
.importKey("spki", tv
.spki
, alg
, false, ["verify"])
149 .then(x
=> crypto
.subtle
.verify(alg
, x
, tv
.sig
, tv
.data
));
153 verifyCase("SHA-1", tv
.rsapss2
),
154 verifyCase("SHA-256", tv
.rsapss3
),
155 verifyCase("SHA-384", tv
.rsapss4
),
156 verifyCase("SHA-512", tv
.rsapss5
),
157 ]).then(complete(that
, x
=> x
.every(y
=> y
)), error(that
));
161 // -----------------------------------------------------------------------------
163 "RSA-PSS import SPKI/PKCS#8 keys and sign/verify (SHA-1, 1024-bit)",
166 var alg
= {name
: "RSA-PSS", hash
: "SHA-1"};
169 function setKeys([pub
, priv
]) { pubKey
= pub
; privKey
= priv
; }
171 return crypto
.subtle
.sign({name
: "RSA-PSS", saltLength
: tv
.rsapss
.saltLength
}, privKey
, tv
.rsapss
.data
);
173 function doVerify(x
) {
174 return crypto
.subtle
.verify({name
: "RSA-PSS", saltLength
: tv
.rsapss
.saltLength
}, pubKey
, x
, tv
.rsapss
.data
);
178 crypto
.subtle
.importKey("spki", tv
.rsapss
.spki
, alg
, false, ["verify"]);
180 crypto
.subtle
.importKey("pkcs8", tv
.rsapss
.pkcs8
, alg
, false, ["sign"]);
182 Promise
.all([spki
, pkcs8
])
183 .then(setKeys
, error(that
))
184 .then(doSign
, error(that
))
185 .then(doVerify
, error(that
))
186 .then(complete(that
, x
=> x
), error(that
));
190 // -----------------------------------------------------------------------------
192 "RSA-PSS import JWK keys and sign/verify (SHA-1, 1024-bit)",
195 var alg
= {name
: "RSA-PSS", hash
: "SHA-1"};
198 function setKeys([pub
, priv
]) { pubKey
= pub
; privKey
= priv
; }
200 return crypto
.subtle
.sign({name
: "RSA-PSS", saltLength
: tv
.rsapss
.saltLength
}, privKey
, tv
.rsapss
.data
);
202 function doVerify(x
) {
203 return crypto
.subtle
.verify({name
: "RSA-PSS", saltLength
: tv
.rsapss
.saltLength
}, pubKey
, x
, tv
.rsapss
.data
);
207 crypto
.subtle
.importKey("jwk", tv
.rsapss
.jwk_pub
, alg
, false, ["verify"]);
209 crypto
.subtle
.importKey("jwk", tv
.rsapss
.jwk_priv
, alg
, false, ["sign"]);
211 Promise
.all([spki
, pkcs8
])
212 .then(setKeys
, error(that
))
213 .then(doSign
, error(that
))
214 .then(doVerify
, error(that
))
215 .then(complete(that
, x
=> x
), error(that
));
219 // -----------------------------------------------------------------------------
221 "RSA-PSS SPKI import/export (SHA-1, 1024-bit)",
224 var alg
= {name
: "RSA-PSS", hash
: "SHA-1"};
226 function doExport(x
) {
227 return crypto
.subtle
.exportKey("spki", x
);
230 crypto
.subtle
.importKey("spki", tv
.rsapss
.spki
, alg
, true, ["verify"])
231 .then(doExport
, error(that
))
232 .then(memcmp_complete(that
, tv
.rsapss
.spki
), error(that
));
236 // -----------------------------------------------------------------------------
238 "RSA-PSS PKCS#8 import/export (SHA-1, 1024-bit)",
241 var alg
= {name
: "RSA-PSS", hash
: "SHA-1"};
243 function doExport(x
) {
244 return crypto
.subtle
.exportKey("pkcs8", x
);
247 crypto
.subtle
.importKey("pkcs8", tv
.rsapss
.pkcs8
, alg
, true, ["sign"])
248 .then(doExport
, error(that
))
249 .then(memcmp_complete(that
, tv
.rsapss
.pkcs8
), error(that
));
253 // -----------------------------------------------------------------------------
255 "RSA-PSS JWK export a public key",
258 var alg
= {name
: "RSA-PSS", hash
: "SHA-1"};
259 var jwk
= tv
.rsapss
.jwk_pub
;
261 function doExport(x
) {
262 return crypto
.subtle
.exportKey("jwk", x
);
265 crypto
.subtle
.importKey("jwk", jwk
, alg
, true, ["verify"])
268 complete(that
, function(x
) {
269 return hasBaseJwkFields(x
) &&
270 hasFields(x
, ["n", "e"]) &&
274 shallowArrayEquals(x
.key_ops
, ["verify"]) &&
283 // -----------------------------------------------------------------------------
285 "RSA-PSS JWK export a private key",
288 var alg
= {name
: "RSA-PSS", hash
: "SHA-1"};
289 var jwk
= tv
.rsapss
.jwk_priv
;
291 function doExport(x
) {
292 return crypto
.subtle
.exportKey("jwk", x
);
295 crypto
.subtle
.importKey("jwk", jwk
, alg
, true, ["sign"])
298 complete(that
, function(x
) {
299 return hasBaseJwkFields(x
) &&
300 hasFields(x
, ["n", "e", "d", "p", "q", "dp", "dq", "qi"]) &&
304 shallowArrayEquals(x
.key_ops
, ["sign"]) &&
319 // -----------------------------------------------------------------------------
321 "Deterministic RSA-PSS signatures with saltLength=0 (SHA-256, 2048-bit)",
328 publicExponent
: new Uint8Array([0x01, 0x00, 0x01]),
332 var data
= crypto
.getRandomValues(new Uint8Array(128));
333 function setKey(x
) { pubKey
= x
.publicKey
; privKey
= x
.privateKey
; }
335 function doSignTwice() {
337 crypto
.subtle
.sign({name
: "RSA-PSS", saltLength
: 0}, privKey
, data
),
338 crypto
.subtle
.sign({name
: "RSA-PSS", saltLength
: 0}, privKey
, data
),
342 function doVerify(x
) {
343 return crypto
.subtle
.verify({name
: "RSA-PSS", saltLength
: 0}, pubKey
, x
, data
);
346 crypto
.subtle
.generateKey(alg
, false, ["sign", "verify"])
347 .then(setKey
, error(that
))
348 .then(doSignTwice
, error(that
))
349 .then(([sig1
, sig2
]) => {
350 if (!util
.memcmp(sig1
, sig2
)) {
351 throw new Error("sig1 must be equal to sig2");
356 .then(doVerify
, error(that
))
357 .then(complete(that
, x
=> x
), error(that
));
370 <div id=
"start" onclick=
"start();">RUN ALL
</div>
372 <div id=
"resultDiv" class=
"content">
374 <span class=
"pass"><span id=
"passN">0</span> passed,
</span>
375 <span class=
"fail"><span id=
"failN">0</span> failed,
</span>
376 <span class=
"pending"><span id=
"pendingN">0</span> pending.
</span>
390 <div id=
"foot"></div>