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 // -----------------------------------------------------------------------------
27 "Test for presence of WebCrypto API methods",
30 exists(window
.crypto
.subtle
) &&
31 exists(window
.crypto
.subtle
.encrypt
) &&
32 exists(window
.crypto
.subtle
.decrypt
) &&
33 exists(window
.crypto
.subtle
.sign
) &&
34 exists(window
.crypto
.subtle
.verify
) &&
35 exists(window
.crypto
.subtle
.digest
) &&
36 exists(window
.crypto
.subtle
.importKey
) &&
37 exists(window
.crypto
.subtle
.exportKey
) &&
38 exists(window
.crypto
.subtle
.generateKey
) &&
39 exists(window
.crypto
.subtle
.deriveKey
) &&
40 exists(window
.crypto
.subtle
.deriveBits
)
45 // -----------------------------------------------------------------------------
47 "Clean failure on a mal-formed algorithm",
52 throw new Error("Oh no, no name!");
56 crypto
.subtle
.importKey("raw", tv
.raw
, alg
, true, ["encrypt"])
59 complete(that
, function(x
) { return true; })
64 // -----------------------------------------------------------------------------
66 "Import / export round-trip with 'raw'",
71 function doExport(x
) {
72 if (!hasKeyFields(x
)) {
73 throw new Error("Invalid key; missing field(s)");
74 } else if ((x
.algorithm
.name
!= alg
) ||
75 (x
.algorithm
.length
!= 8 * tv
.raw
.length
) ||
76 (x
.type
!= "secret") ||
78 (x
.usages
.length
!= 1) ||
79 (x
.usages
[0] != "encrypt")) {
80 throw new Error("Invalid key: incorrect key data");
82 return crypto
.subtle
.exportKey("raw", x
);
85 crypto
.subtle
.importKey("raw", tv
.raw
, alg
, true, ["encrypt"])
88 memcmp_complete(that
, tv
.raw
),
94 // -----------------------------------------------------------------------------
96 "Import failure with format 'raw'",
101 crypto
.subtle
.importKey("raw", tv
.negative_raw
, alg
, true, ["encrypt"])
102 .then(error(that
), complete(that
));
106 // -----------------------------------------------------------------------------
108 "Proper handling of an ABV representing part of a buffer",
113 var u8
= new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
114 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
115 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
116 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f]);
117 var u32
= new Uint32Array(u8
.buffer
, 8, 4);
118 var out
= u8
.subarray(8, 24);
120 function doExport(x
) {
121 return crypto
.subtle
.exportKey("raw", x
);
124 crypto
.subtle
.importKey("raw", u32
, alg
, true, ["encrypt"])
125 .then(doExport
, error(that
))
126 .then(memcmp_complete(that
, out
), error(that
));
130 // -----------------------------------------------------------------------------
132 "Import / export round-trip with 'pkcs8'",
135 var alg
= { name
: "RSASSA-PKCS1-v1_5", hash
: "SHA-1" };
137 function doExport(x
) {
138 if (!hasKeyFields(x
)) {
139 throw new Error("Invalid key; missing field(s)");
140 } else if ((x
.algorithm
.name
!= alg
.name
) ||
141 (x
.algorithm
.hash
.name
!= alg
.hash
) ||
142 (x
.algorithm
.modulusLength
!= 512) ||
143 (x
.algorithm
.publicExponent
.byteLength
!= 3) ||
144 (x
.type
!= "private") ||
146 (x
.usages
.length
!= 1) ||
147 (x
.usages
[0] != "sign")) {
148 throw new Error("Invalid key: incorrect key data");
150 return crypto
.subtle
.exportKey("pkcs8", x
);
153 crypto
.subtle
.importKey("pkcs8", tv
.pkcs8
, alg
, true, ["sign"])
156 memcmp_complete(that
, tv
.pkcs8
),
162 // -----------------------------------------------------------------------------
164 "Import failure with format 'pkcs8'",
167 var alg
= { name
: "RSASSA-PKCS1-v1_5", hash
: "SHA-1" };
169 crypto
.subtle
.importKey("pkcs8", tv
.negative_pkcs8
, alg
, true, ["encrypt"])
170 .then(error(that
), complete(that
));
174 // -----------------------------------------------------------------------------
176 "Import / export round-trip with 'spki'",
180 name
: "RSASSA-PKCS1-v1_5",
184 function doExport(x
) {
185 if (!hasKeyFields(x
)) {
186 throw new Error("Invalid key; missing field(s)");
187 } else if ((x
.algorithm
.name
!= alg
.name
) ||
188 (x
.algorithm
.modulusLength
!= 1024) ||
189 (x
.algorithm
.publicExponent
.byteLength
!= 3) ||
190 (x
.type
!= "public") ||
192 (x
.usages
.length
!= 1) ||
193 (x
.usages
[0] != "verify")) {
194 throw new Error("Invalid key: incorrect key data");
196 return crypto
.subtle
.exportKey("spki", x
);
199 crypto
.subtle
.importKey("spki", tv
.spki
, alg
, true, ["verify"])
200 .then(doExport
, error(that
))
202 memcmp_complete(that
, tv
.spki
),
208 // -----------------------------------------------------------------------------
210 "Import failure with format 'spki'",
214 name
: "RSASSA-PKCS1-v1_5",
218 crypto
.subtle
.importKey("spki", tv
.negative_spki
, alg
, true, ["encrypt"])
219 .then(error(that
), complete(that
));
223 // -----------------------------------------------------------------------------
225 "Importing an ECDSA key as an RSA key should fail",
229 name
: "RSASSA-PKCS1-v1_5",
233 crypto
.subtle
.importKey("spki", tv
.ecdh_p256
.spki
, alg
, true, ["verify"])
234 .then(error(that
), complete(that
));
238 // -----------------------------------------------------------------------------
240 "Refuse to export non-extractable key",
245 function doExport(x
) {
246 return crypto
.subtle
.exportKey("raw", x
);
249 crypto
.subtle
.importKey("raw", tv
.raw
, alg
, false, ["encrypt"])
250 .then(doExport
, error(that
))
258 // -----------------------------------------------------------------------------
260 "IndexedDB store / retrieve round-trip",
265 var dbname
= "keyDB";
266 var dbstore
= "keystore";
271 function doIndexedDB(x
) {
273 var req
= indexedDB
.deleteDatabase(dbname
);
274 req
.onerror
= error(that
);
275 req
.onsuccess
= doCreateDB
;
278 function doCreateDB() {
279 var req
= indexedDB
.open(dbname
, dbversion
);
280 req
.onerror
= error(that
);
281 req
.onupgradeneeded = function(e
) {
282 db
= e
.target
.result
;
283 db
.createObjectStore(dbstore
, {keyPath
: "id"});
286 req
.onsuccess
= doPut
;
290 var req
= db
.transaction([dbstore
], "readwrite")
291 .objectStore(dbstore
)
292 .add({id
: dbkey
, val
: importedKey
});
293 req
.onerror
= error(that
);
294 req
.onsuccess
= doGet
;
298 var req
= db
.transaction([dbstore
], "readwrite")
299 .objectStore(dbstore
)
301 req
.onerror
= error(that
);
302 req
.onsuccess
= complete(that
, function(e
) {
304 return hasKeyFields(e
.target
.result
.val
);
308 crypto
.subtle
.importKey("raw", tv
.raw
, alg
, false, ["encrypt"])
309 .then(doIndexedDB
, error(that
));
313 // -----------------------------------------------------------------------------
315 "Generate a 256-bit HMAC-SHA-256 key",
318 var alg
= { name
: "HMAC", length
: 256, hash
: {name
: "SHA-256"} };
319 crypto
.subtle
.generateKey(alg
, true, ["sign", "verify"]).then(
320 complete(that
, function(x
) {
321 return hasKeyFields(x
) && x
.algorithm
.length
== 256;
328 // -----------------------------------------------------------------------------
330 "Generate a 256-bit HMAC-SHA-256 key without specifying a key length",
333 var alg
= { name
: "HMAC", hash
: {name
: "SHA-256"} };
334 crypto
.subtle
.generateKey(alg
, true, ["sign", "verify"]).then(
335 complete(that
, function(x
) {
336 return hasKeyFields(x
) && x
.algorithm
.length
== 512;
343 // -----------------------------------------------------------------------------
345 "Generate a 256-bit HMAC-SHA-512 key without specifying a key length",
348 var alg
= { name
: "HMAC", hash
: {name
: "SHA-512"} };
349 crypto
.subtle
.generateKey(alg
, true, ["sign", "verify"]).then(
350 complete(that
, function(x
) {
351 return hasKeyFields(x
) && x
.algorithm
.length
== 1024;
358 // -----------------------------------------------------------------------------
360 "Fail generating an HMAC key when specifying an invalid hash algorithm",
363 var alg
= { name
: "HMAC", hash
: {name
: "SHA-123"} };
364 crypto
.subtle
.generateKey(alg
, true, ["sign", "verify"]).then(
366 complete(that
, function() { return true; })
371 // -----------------------------------------------------------------------------
373 "Fail generating an HMAC key when specifying a zero length",
376 var alg
= { name
: "HMAC", hash
: {name
: "SHA-256"}, length
: 0 };
377 crypto
.subtle
.generateKey(alg
, true, ["sign", "verify"]).then(
379 complete(that
, function() { return true; })
384 // -----------------------------------------------------------------------------
386 "Generate a 192-bit AES key",
389 var alg
= { name
: "AES-GCM", length
: 192 };
390 crypto
.subtle
.generateKey(alg
, true, ["encrypt"]).then(
391 complete(that
, function(x
) {
392 return hasKeyFields(x
);
399 // -----------------------------------------------------------------------------
401 "Fail generating an AES key of wrong length",
404 var alg
= { name
: "AES-CBC", length
: 64 };
405 crypto
.subtle
.generateKey(alg
, false, ["encrypt"]).then(
407 complete(that
, function(e
) {
408 return e
.name
== "OperationError";
414 // -----------------------------------------------------------------------------
416 "Fail generating a key with bad algorithm argument",
419 var alg
= { name
: "AES", length
: 128 };
420 crypto
.subtle
.generateKey(alg
, false, ["encrypt"]).then(
422 complete(that
, function(e
) {
423 return e
.name
== "NotSupportedError";
429 // -----------------------------------------------------------------------------
430 TestArray
.addTest( "Generate a 1024-bit RSA key",
434 name
: "RSASSA-PKCS1-v1_5",
437 publicExponent
: new Uint8Array([0x01, 0x00, 0x01]),
439 crypto
.subtle
.generateKey(alg
, false, ["sign", "verify"]).then(
440 complete(that
, function(x
) {
441 return exists(x
.publicKey
) &&
442 (x
.publicKey
.algorithm
.name
== alg
.name
) &&
443 (x
.publicKey
.algorithm
.modulusLength
== alg
.modulusLength
) &&
444 (x
.publicKey
.type
== "public") &&
445 x
.publicKey
.extractable
&&
446 (x
.publicKey
.usages
.length
== 1) &&
447 (x
.publicKey
.usages
[0] == "verify") &&
448 exists(x
.privateKey
) &&
449 (x
.privateKey
.algorithm
.name
== alg
.name
) &&
450 (x
.privateKey
.algorithm
.modulusLength
== alg
.modulusLength
) &&
451 (x
.privateKey
.type
== "private") &&
452 !x
.privateKey
.extractable
&&
453 (x
.privateKey
.usages
.length
== 1) &&
454 (x
.privateKey
.usages
[0] == "sign");
461 // -----------------------------------------------------------------------------
463 "Fail cleanly when NSS refuses to generate a key pair",
467 name
: "RSASSA-PKCS1-v1_5",
469 modulusLength
: 2299, // NSS does not like this key length
470 publicExponent
: new Uint8Array([0x01, 0x00, 0x01]),
473 crypto
.subtle
.generateKey(alg
, false, ["sign"])
474 .then( error(that
), complete(that
) );
478 // -----------------------------------------------------------------------------
483 crypto
.subtle
.digest("SHA-256", tv
.sha256
.data
).then(
484 memcmp_complete(that
, tv
.sha256
.result
),
490 // -----------------------------------------------------------------------------
492 "Fail cleanly on unknown hash algorithm",
495 crypto
.subtle
.digest("GOST-34_311-95", tv
.sha256
.data
).then(
497 complete(that
, function() { return true; })
502 // -----------------------------------------------------------------------------
508 function doEncrypt(x
) {
509 return crypto
.subtle
.encrypt(
510 { name
: "AES-CBC", iv
: tv
.aes_cbc_enc
.iv
},
511 x
, tv
.aes_cbc_enc
.data
);
514 crypto
.subtle
.importKey("raw", tv
.aes_cbc_enc
.key
, "AES-CBC", false, ["encrypt"])
517 memcmp_complete(that
, tv
.aes_cbc_enc
.result
),
523 // -----------------------------------------------------------------------------
525 "AES-CBC encrypt with wrong IV size",
529 function encrypt(x
, iv
) {
530 return crypto
.subtle
.encrypt(
531 { name
: "AES-CBC", iv
},
532 x
, tv
.aes_cbc_enc
.data
);
535 function checkPromises(promises
) {
536 for (var promise
of promises
) {
537 if (promise
.status
!= "rejected") {
540 if (promise
.reason
.name
!= "OperationError") {
548 crypto
.subtle
.importKey("raw", tv
.aes_cbc_enc
.key
, "AES-CBC", false, ["encrypt"])
549 .then(function(key
) {
550 var p1
= encrypt(key
, new Uint8Array(15));
551 var p2
= encrypt(key
, new Uint8Array(17));
553 Promise
.allSettled([p1
, p2
])
554 .then(complete(that
, checkPromises
));
559 // -----------------------------------------------------------------------------
565 function doDecrypt(x
) {
566 return crypto
.subtle
.decrypt(
567 { name
: "AES-CBC", iv
: tv
.aes_cbc_dec
.iv
},
568 x
, tv
.aes_cbc_dec
.data
);
571 crypto
.subtle
.importKey("raw", tv
.aes_cbc_dec
.key
, "AES-CBC", false, ["decrypt"])
574 memcmp_complete(that
, tv
.aes_cbc_dec
.result
),
580 // -----------------------------------------------------------------------------
582 "AES-CBC decrypt with wrong IV size",
586 function decrypt(x
, iv
) {
587 return crypto
.subtle
.decrypt(
588 { name
: "AES-CBC", iv
},
589 x
, tv
.aes_cbc_dec
.data
);
592 function checkPromises(promises
) {
593 for (var promise
of promises
) {
594 if (promise
.status
!= "rejected") {
597 if (promise
.reason
.name
!= "OperationError") {
605 crypto
.subtle
.importKey("raw", tv
.aes_cbc_dec
.key
, "AES-CBC", false, ["decrypt"])
606 .then(function(key
) {
607 var p1
= decrypt(key
, new Uint8Array(15));
608 var p2
= decrypt(key
, new Uint8Array(17));
610 Promise
.allSettled([p1
, p2
])
611 .then(complete(that
, checkPromises
));
616 // -----------------------------------------------------------------------------
618 "AES-CTR encryption",
622 function doEncrypt(x
) {
623 return crypto
.subtle
.encrypt(
624 { name
: "AES-CTR", counter
: tv
.aes_ctr_enc
.iv
, length
: 32 },
625 x
, tv
.aes_ctr_enc
.data
);
628 crypto
.subtle
.importKey("raw", tv
.aes_ctr_enc
.key
, "AES-CTR", false, ["encrypt"])
631 memcmp_complete(that
, tv
.aes_ctr_enc
.result
),
637 // -----------------------------------------------------------------------------
639 "AES-CTR encryption with wrong IV size",
643 function encrypt(x
, iv
) {
644 return crypto
.subtle
.encrypt(
645 { name
: "AES-CTR", counter
: iv
, length
: 32 },
646 x
, tv
.aes_ctr_enc
.data
);
649 function checkPromises(promises
) {
650 for (var promise
of promises
) {
651 if (promise
.status
!= "rejected") {
654 if (promise
.reason
.name
!= "OperationError") {
662 crypto
.subtle
.importKey("raw", tv
.aes_ctr_enc
.key
, "AES-CTR", false, ["encrypt"])
663 .then(function(key
) {
664 var p1
= encrypt(key
, new Uint8Array(15));
665 var p2
= encrypt(key
, new Uint8Array(17));
667 Promise
.allSettled([p1
, p2
])
668 .then(complete(that
, checkPromises
));
673 // -----------------------------------------------------------------------------
675 "AES-CTR decryption",
679 function doDecrypt(x
) {
680 return crypto
.subtle
.decrypt(
681 { name
: "AES-CTR", counter
: tv
.aes_ctr_dec
.iv
, length
: 32 },
682 x
, tv
.aes_ctr_dec
.data
);
685 crypto
.subtle
.importKey("raw", tv
.aes_ctr_dec
.key
, "AES-CTR", false, ["decrypt"])
688 memcmp_complete(that
, tv
.aes_ctr_dec
.result
),
694 // -----------------------------------------------------------------------------
696 "AES-CTR decryption with wrong IV size",
700 function decrypt(x
, iv
) {
701 return crypto
.subtle
.decrypt(
702 { name
: "AES-CTR", counter
: iv
, length
: 32 },
703 x
, tv
.aes_ctr_dec
.data
);
706 function checkPromises(promises
) {
707 for (var promise
of promises
) {
708 if (promise
.status
!= "rejected") {
711 if (promise
.reason
.name
!= "OperationError") {
719 crypto
.subtle
.importKey("raw", tv
.aes_ctr_dec
.key
, "AES-CTR", false, ["decrypt"])
720 .then(function(key
) {
721 var p1
= decrypt(key
, new Uint8Array(15));
722 var p2
= decrypt(key
, new Uint8Array(17));
724 Promise
.allSettled([p1
, p2
])
725 .then(complete(that
, checkPromises
));
730 // -----------------------------------------------------------------------------
732 "AES-GCM encryption",
736 function doEncrypt(x
) {
737 return crypto
.subtle
.encrypt(
740 iv
: tv
.aes_gcm_enc
.iv
,
741 additionalData
: tv
.aes_gcm_enc
.adata
,
744 x
, tv
.aes_gcm_enc
.data
);
747 crypto
.subtle
.importKey("raw", tv
.aes_gcm_enc
.key
, "AES-GCM", false, ["encrypt"])
750 memcmp_complete(that
, tv
.aes_gcm_enc
.result
),
756 // -----------------------------------------------------------------------------
758 "AES-GCM decryption",
762 function doDecrypt(x
) {
763 return crypto
.subtle
.decrypt(
766 iv
: tv
.aes_gcm_dec
.iv
,
767 additionalData
: tv
.aes_gcm_dec
.adata
,
770 x
, tv
.aes_gcm_dec
.data
);
773 crypto
.subtle
.importKey("raw", tv
.aes_gcm_dec
.key
, "AES-GCM", false, ["decrypt"])
776 memcmp_complete(that
, tv
.aes_gcm_dec
.result
),
782 // -----------------------------------------------------------------------------
784 "AES-GCM decryption, failing authentication check",
788 function doDecrypt(x
) {
789 return crypto
.subtle
.decrypt(
792 iv
: tv
.aes_gcm_dec_fail
.iv
,
793 additionalData
: tv
.aes_gcm_dec_fail
.adata
,
796 x
, tv
.aes_gcm_dec_fail
.data
);
799 crypto
.subtle
.importKey("raw", tv
.aes_gcm_dec_fail
.key
, "AES-GCM", false, ["decrypt"])
808 // -----------------------------------------------------------------------------
810 "AES-GCM encryption, fail with a zero-length IV",
815 iv
: new Uint8Array(),
816 additionalData
: tv
.aes_gcm_enc
.adata
,
820 function doEncrypt(x
) {
821 return crypto
.subtle
.encrypt(alg
, x
, tv
.aes_gcm_enc
.data
);
824 crypto
.subtle
.importKey("raw", tv
.aes_gcm_enc
.key
, "AES-GCM", false, ["encrypt"])
826 .then(error(that
), complete(that
));
830 // -----------------------------------------------------------------------------
832 "AES-GCM encryption, accept an all-zero IV (1 byte)",
837 iv
: new Uint8Array(1),
838 additionalData
: tv
.aes_gcm_enc
.adata
,
842 function doEncrypt(x
) {
843 return crypto
.subtle
.encrypt(alg
, x
, tv
.aes_gcm_enc
.data
);
846 crypto
.subtle
.importKey("raw", tv
.aes_gcm_enc
.key
, "AES-GCM", false, ["encrypt"])
848 .then(complete(that
), error(that
));
852 // -----------------------------------------------------------------------------
854 "AES-GCM encryption, accept an all-zero IV (12 bytes)",
859 iv
: new Uint8Array(12),
860 additionalData
: tv
.aes_gcm_enc
.adata
,
864 function doEncrypt(x
) {
865 return crypto
.subtle
.encrypt(alg
, x
, tv
.aes_gcm_enc
.data
);
868 crypto
.subtle
.importKey("raw", tv
.aes_gcm_enc
.key
, "AES-GCM", false, ["encrypt"])
870 .then(complete(that
), error(that
));
874 // -----------------------------------------------------------------------------
876 "AES-GCM encryption, accept an all-zero IV (16 bytes)",
881 iv
: new Uint8Array(16),
882 additionalData
: tv
.aes_gcm_enc
.adata
,
886 function doEncrypt(x
) {
887 return crypto
.subtle
.encrypt(alg
, x
, tv
.aes_gcm_enc
.data
);
890 crypto
.subtle
.importKey("raw", tv
.aes_gcm_enc
.key
, "AES-GCM", false, ["encrypt"])
892 .then(complete(that
), error(that
));
896 // -----------------------------------------------------------------------------
907 return crypto
.subtle
.sign("HMAC", x
, tv
.hmac_sign
.data
);
910 crypto
.subtle
.importKey("raw", tv
.hmac_sign
.key
, alg
, false, ["sign"])
913 memcmp_complete(that
, tv
.hmac_sign
.result
),
919 // -----------------------------------------------------------------------------
921 "HMAC SHA-256 verify",
929 function doVerify(x
) {
930 return crypto
.subtle
.verify("HMAC", x
, tv
.hmac_verify
.sig
, tv
.hmac_verify
.data
);
933 crypto
.subtle
.importKey("raw", tv
.hmac_verify
.key
, alg
, false, ["verify"])
936 complete(that
, function(x
) { return !!x
; }),
942 // -----------------------------------------------------------------------------
944 "HMAC SHA-256, failing verification due to bad signature",
952 function doVerify(x
) {
953 return crypto
.subtle
.verify("HMAC", x
, tv
.hmac_verify
.sig_fail
,
954 tv
.hmac_verify
.data
);
957 crypto
.subtle
.importKey("raw", tv
.hmac_verify
.key
, alg
, false, ["verify"])
960 complete(that
, function(x
) { return !x
; }),
966 // -----------------------------------------------------------------------------
968 "HMAC SHA-256, failing verification due to key usage restriction",
976 function doVerify(x
) {
977 return crypto
.subtle
.verify("HMAC", x
, tv
.hmac_verify
.sig
,
978 tv
.hmac_verify
.data
);
981 crypto
.subtle
.importKey("raw", tv
.hmac_verify
.key
, alg
, false, ["encrypt"])
985 complete(that
, function(x
) { return true; })
990 // -----------------------------------------------------------------------------
992 "RSASSA/SHA-1 signature",
995 var alg
= { name
: "RSASSA-PKCS1-v1_5", hash
: "SHA-1" };
998 return crypto
.subtle
.sign(alg
.name
, x
, tv
.rsassa
.data
);
1001 crypto
.subtle
.importKey("pkcs8", tv
.rsassa
.pkcs8
, alg
, false, ["sign"])
1003 .then( memcmp_complete(that
, tv
.rsassa
.sig1
), error(that
) );
1007 // -----------------------------------------------------------------------------
1009 "RSASSA verification (SHA-1)",
1012 var alg
= { name
: "RSASSA-PKCS1-v1_5", hash
: "SHA-1" };
1014 function doVerify(x
) {
1015 return crypto
.subtle
.verify(alg
.name
, x
, tv
.rsassa
.sig1
, tv
.rsassa
.data
);
1018 crypto
.subtle
.importKey("spki", tv
.rsassa
.spki
, alg
, false, ["verify"])
1021 complete(that
, function(x
) { return x
; }),
1027 // -----------------------------------------------------------------------------
1029 "RSASSA verification (SHA-1), failing verification",
1032 var alg
= { name
: "RSASSA-PKCS1-v1_5", hash
: "SHA-1" };
1034 function doVerify(x
) {
1035 return crypto
.subtle
.verify(alg
.name
, x
, tv
.rsassa
.sig_fail
, tv
.rsassa
.data
);
1038 crypto
.subtle
.importKey("spki", tv
.rsassa
.spki
, alg
, false, ["verify"])
1041 complete(that
, function(x
) { return !x
; }),
1047 // -----------------------------------------------------------------------------
1049 "RSASSA/SHA-256 signature",
1052 var alg
= { name
: "RSASSA-PKCS1-v1_5", hash
: "SHA-256" };
1054 function doSign(x
) {
1055 return crypto
.subtle
.sign(alg
.name
, x
, tv
.rsassa
.data
);
1058 crypto
.subtle
.importKey("pkcs8", tv
.rsassa
.pkcs8
, alg
, false, ["sign"])
1060 .then( memcmp_complete(that
, tv
.rsassa
.sig256
), error(that
) );
1064 // -----------------------------------------------------------------------------
1066 "RSASSA verification (SHA-256)",
1069 var alg
= { name
: "RSASSA-PKCS1-v1_5", hash
: "SHA-256" };
1071 function doVerify(x
) {
1072 return crypto
.subtle
.verify(alg
.name
, x
, tv
.rsassa
.sig256
, tv
.rsassa
.data
);
1075 crypto
.subtle
.importKey("spki", tv
.rsassa
.spki
, alg
, false, ["verify"])
1078 complete(that
, function(x
) { return x
; }),
1084 // -----------------------------------------------------------------------------
1086 "RSASSA verification (SHA-256), failing verification",
1089 var alg
= { name
: "RSASSA-PKCS1-v1_5", hash
: "SHA-256" };
1091 function doVerify(x
) {
1092 return crypto
.subtle
.verify(alg
.name
, x
, tv
.rsassa
.sig_fail
, tv
.rsassa
.data
);
1095 crypto
.subtle
.importKey("spki", tv
.rsassa
.spki
, alg
, false, ["verify"])
1098 complete(that
, function(x
) { return !x
; }),
1104 // -----------------------------------------------------------------------------
1106 "Test that we return ArrayBuffers not ArrayBufferViews",
1110 crypto
.subtle
.digest("SHA-256", tv
.sha256
.data
)
1111 .then(complete(that
, function(x
) {
1112 return x
instanceof ArrayBuffer
;
1117 // -----------------------------------------------------------------------------
1119 "Ensure that importing an invalid key doesn't crash",
1122 var alg
= {name
: "RSA-OAEP", hash
: "SHA-1"};
1124 crypto
.subtle
.importKey("pkcs8", tv
.broken_pkcs8
.rsa
, alg
, false, ["decrypt"])
1125 .then(error(that
), complete(that
));
1129 // -----------------------------------------------------------------------------
1131 "Test that we check keys before using them for encryption/signatures",
1135 function doCheckRSASSA() {
1136 var alg
= {name
: "HMAC", hash
: {name
: "SHA-1"}};
1138 function doSign(x
) {
1139 return crypto
.subtle
.sign("RSASSA-PKCS1-v1_5", x
, new Uint8Array());
1142 return crypto
.subtle
.generateKey(alg
, false, ["sign"]).then(doSign
);
1145 doCheckRSASSA().then(error(that
), complete(that
));
1149 // -----------------------------------------------------------------------------
1151 "Test that we're using the right globals when creating objects",
1153 // This test isn't supported in workers.
1154 if (window
.importScripts
) {
1155 return this.complete(true);
1159 var data
= crypto
.getRandomValues(new Uint8Array(10));
1160 var hmacAlg
= {name
: "HMAC", length
: 256, hash
: "SHA-1"};
1165 modulusLength
: 1024,
1166 publicExponent
: new Uint8Array([0x01, 0x00, 0x01]),
1169 function checkPrototypes(obj
, type
) {
1170 return obj
.__proto__
!= window
[type
].prototype &&
1171 obj
.__proto__
== frames
[0][type
].prototype;
1174 var p1
= crypto
.subtle
.importKey
.call(
1175 frames
[0].crypto
.subtle
, "raw", data
, hmacAlg
, false, ["sign", "verify"]);
1176 var p2
= crypto
.subtle
.generateKey
.call(
1177 frames
[0].crypto
.subtle
, hmacAlg
, false, ["sign", "verify"]);
1178 var p3
= crypto
.subtle
.generateKey
.call(
1179 frames
[0].crypto
.subtle
, rsaAlg
, false, ["sign", "verify"]);
1181 if (!checkPrototypes(p1
, "Promise") ||
1182 !checkPrototypes(p2
, "Promise") ||
1183 !checkPrototypes(p3
, "Promise")) {
1187 return Promise
.all([p1
, p2
, p3
]).then(complete(that
, keys
=> {
1188 return keys
.every(key
=> {
1189 if (key
instanceof frames
[0].CryptoKey
) {
1190 return checkPrototypes(key
, "CryptoKey");
1193 return checkPrototypes(key
.publicKey
, "CryptoKey") &&
1194 checkPrototypes(key
.privateKey
, "CryptoKey");
1206 <b>Web
</b>Crypto
<br>
1209 <iframe style=
"display: none;"></iframe>
1210 <div id=
"start" onclick=
"start();">RUN ALL
</div>
1212 <div id=
"resultDiv" class=
"content">
1214 <span class=
"pass"><span id=
"passN">0</span> passed,
</span>
1215 <span class=
"fail"><span id=
"failN">0</span> failed,
</span>
1216 <span class=
"pending"><span id=
"pendingN">0</span> pending.
</span>
1220 <table id=
"results">
1230 <div id=
"foot"></div>