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 "Send a CryptoKey to a Worker and use it to encrypt data",
29 var worker
= new Worker(`data:text/plain,
30 onmessage = ({data: {key, data, nonce}}) => {
31 var alg = { name: "AES-GCM", iv: nonce };
32 crypto.subtle.encrypt(alg, key, data).then(postMessage);
36 var data
= crypto
.getRandomValues(new Uint8Array(128));
37 var nonce
= crypto
.getRandomValues(new Uint8Array(16));
38 var alg
= { name
: "AES-GCM", length
: 128 };
41 // Generate a new AES key.
42 crypto
.subtle
.generateKey(alg
, false, ["encrypt", "decrypt"]).then(key
=> {
43 // Wait for ciphertext, check and decrypt.
44 worker
.addEventListener("message", ({data
: ciphertext
}) => {
45 crypto
.subtle
.decrypt({ name
: "AES-GCM", iv
: nonce
}, key
, ciphertext
)
46 .then(memcmp_complete(that
, data
), error(that
));
49 // Send it to the worker.
50 worker
.postMessage({key
, data
, nonce
});
55 // -----------------------------------------------------------------------------
57 "Get a CryptoKey from a Worker and encrypt/decrypt data",
59 var worker
= new Worker(`data:text/plain,
60 var alg = { name: "AES-GCM", length: 128 };
61 crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"])
65 var data
= crypto
.getRandomValues(new Uint8Array(128));
66 var nonce
= crypto
.getRandomValues(new Uint8Array(16));
67 var alg
= { name
: "AES-GCM", iv
: nonce
};
70 // Wait for the key from the worker.
71 worker
.addEventListener("message", ({data
: key
}) => {
72 // Encrypt some data with the key.
73 crypto
.subtle
.encrypt(alg
, key
, data
).then(ciphertext
=> {
74 // Verify and decrypt.
75 crypto
.subtle
.decrypt(alg
, key
, ciphertext
)
76 .then(memcmp_complete(that
, data
), error(that
));
82 // -----------------------------------------------------------------------------
84 "Web crypto in terminating Worker",
86 var worker
= new Worker(`data:text/plain,
87 function infiniteEncrypt(key, data, nonce) {
88 var alg = { name: "AES-GCM", iv: nonce };
89 return crypto.subtle.encrypt(alg, key, data).then(_ => {
90 infiniteEncrypt(key, data, nonce);
93 onmessage = ({data: {key, data, nonce}}) => {
94 infiniteEncrypt(key, data, nonce);
95 postMessage("started");
99 var data
= crypto
.getRandomValues(new Uint8Array(128));
100 var nonce
= crypto
.getRandomValues(new Uint8Array(16));
101 var alg
= { name
: "AES-GCM", length
: 128 };
104 // Generate a new AES key.
105 crypto
.subtle
.generateKey(alg
, false, ["encrypt", "decrypt"]).then(key
=> {
106 worker
.addEventListener("message", ({data
: msg
}) => {
107 if (msg
=== "started") {
108 // Terminate the worker while its busy doing crypto work
112 // Just end the test immediate since we can't receive any
113 // more messages from the worker after calling terminate().
114 // If we haven't crashed, then the test is a success.
119 // Send it to the worker.
120 worker
.postMessage({key
, data
, nonce
});
134 <div id=
"start" onclick=
"start();">RUN ALL
</div>
136 <div id=
"resultDiv" class=
"content">
138 <span class=
"pass"><span id=
"passN">0</span> passed,
</span>
139 <span class=
"fail"><span id=
"failN">0</span> failed,
</span>
140 <span class=
"pending"><span id=
"pendingN">0</span> pending.
</span>
154 <div id=
"foot"></div>