test_WebCrypto_Structured_Cloning.html (9414B)
1 <!DOCTYPE html> 2 <html> 3 4 <head> 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> 9 10 <!-- Utilities for manipulating ABVs --> 11 <script src="util.js"></script> 12 13 <!-- A simple wrapper around IndexedDB --> 14 <script src="simpledb.js"></script> 15 16 <!-- Test vectors drawn from the literature --> 17 <script src="./test-vectors.js"></script> 18 19 <!-- General testing framework --> 20 <script src="./test-array.js"></script> 21 22 <script>/* <![CDATA[*/ 23 "use strict"; 24 25 // ----------------------------------------------------------------------------- 26 TestArray.addTest( 27 "Structured Cloning: AES-CTR", 28 function() { 29 var that = this; 30 var data = crypto.getRandomValues(new Uint8Array(128)); 31 var iv = crypto.getRandomValues(new Uint8Array(16)); 32 var alg = {name: "AES-CTR", length: 128, iv}; 33 34 var counter = new Uint8Array(16); 35 var algEncrypt = {name: "AES-CTR", length: 128, counter}; 36 37 crypto.subtle.generateKey(alg, true, ["encrypt"]) 38 .then(util.cloneExportCompareKeys) 39 .then(x => crypto.subtle.encrypt(algEncrypt, x, data)) 40 .then(complete(that), error(that)); 41 } 42 ); 43 44 // ----------------------------------------------------------------------------- 45 TestArray.addTest( 46 "Structured Cloning: AES-CBC", 47 function() { 48 var that = this; 49 var data = crypto.getRandomValues(new Uint8Array(128)); 50 var iv = crypto.getRandomValues(new Uint8Array(16)); 51 var alg = {name: "AES-CBC", length: 128, iv}; 52 53 crypto.subtle.generateKey(alg, true, ["encrypt"]) 54 .then(util.cloneExportCompareKeys) 55 .then(x => crypto.subtle.encrypt(alg, x, data)) 56 .then(complete(that), error(that)); 57 } 58 ); 59 60 // ----------------------------------------------------------------------------- 61 TestArray.addTest( 62 "Structured Cloning: AES-GCM", 63 function() { 64 var that = this; 65 var data = crypto.getRandomValues(new Uint8Array(128)); 66 var iv = crypto.getRandomValues(new Uint8Array(16)); 67 var alg = {name: "AES-GCM", length: 128, iv}; 68 69 crypto.subtle.generateKey(alg, true, ["encrypt"]) 70 .then(util.cloneExportCompareKeys) 71 .then(x => crypto.subtle.encrypt(alg, x, data)) 72 .then(complete(that), error(that)); 73 } 74 ); 75 76 // ----------------------------------------------------------------------------- 77 TestArray.addTest( 78 "Structured Cloning: AES-KW", 79 function() { 80 var that = this; 81 var alg = {name: "AES-KW", length: 128}; 82 83 crypto.subtle.generateKey(alg, true, ["wrapKey"]) 84 .then(util.cloneExportCompareKeys) 85 .then(x => crypto.subtle.wrapKey("raw", x, x, "AES-KW")) 86 .then(complete(that), error(that)); 87 } 88 ); 89 90 // ----------------------------------------------------------------------------- 91 TestArray.addTest( 92 "Structured Cloning: HMAC", 93 function() { 94 var that = this; 95 var data = crypto.getRandomValues(new Uint8Array(128)); 96 var alg = {name: "HMAC", length: 256, hash: "SHA-256"}; 97 98 crypto.subtle.generateKey(alg, true, ["sign", "verify"]) 99 .then(util.cloneExportCompareKeys) 100 .then(x => crypto.subtle.sign("HMAC", x, data)) 101 .then(complete(that), error(that)); 102 } 103 ); 104 105 // ----------------------------------------------------------------------------- 106 TestArray.addTest( 107 "Structured Cloning: PBKDF2", 108 function() { 109 var that = this; 110 var key = new TextEncoder().encode("password"); 111 112 var alg = { 113 name: "PBKDF2", 114 hash: "SHA-1", 115 salt: crypto.getRandomValues(new Uint8Array(8)), 116 iterations: 4096, 117 }; 118 119 crypto.subtle.importKey("raw", key, "PBKDF2", true, ["deriveBits"]) 120 .then(util.cloneExportCompareKeys) 121 .then(x => crypto.subtle.deriveBits(alg, x, 160)) 122 .then(complete(that), error(that)); 123 } 124 ); 125 126 // ----------------------------------------------------------------------------- 127 TestArray.addTest( 128 "Structured Cloning: HKDF", 129 function() { 130 var that = this; 131 var key = new TextEncoder().encode("password"); 132 133 var alg = { 134 name: "HKDF", 135 hash: "SHA-256", 136 salt: new Uint8Array(), 137 info: new Uint8Array(), 138 }; 139 140 crypto.subtle.importKey("raw", key, "HKDF", false, ["deriveBits"]) 141 .then(util.clone) 142 .then(x => crypto.subtle.deriveBits(alg, x, 16)) 143 .then(complete(that), error(that)); 144 } 145 ); 146 147 // ----------------------------------------------------------------------------- 148 TestArray.addTest( 149 "Structured Cloning: RSA-OAEP", 150 function() { 151 var that = this; 152 var data = crypto.getRandomValues(new Uint8Array(128)); 153 154 var alg = { 155 name: "RSA-OAEP", 156 hash: "SHA-256", 157 modulusLength: 2048, 158 publicExponent: new Uint8Array([1, 0, 1]), 159 }; 160 161 crypto.subtle.generateKey(alg, true, ["encrypt", "decrypt"]) 162 .then(util.cloneExportCompareKeys) 163 .then(x => { 164 return crypto.subtle.encrypt(alg, x.publicKey, data) 165 .then(ct => crypto.subtle.decrypt(alg, x.privateKey, ct)); 166 }) 167 .then(complete(that), error(that)); 168 } 169 ); 170 171 // ----------------------------------------------------------------------------- 172 TestArray.addTest( 173 "Structured Cloning: RSASSA-PKCS1-v1_5", 174 function() { 175 var that = this; 176 var data = crypto.getRandomValues(new Uint8Array(128)); 177 178 var alg = { 179 name: "RSASSA-PKCS1-v1_5", 180 hash: "SHA-256", 181 modulusLength: 2048, 182 publicExponent: new Uint8Array([1, 0, 1]), 183 }; 184 185 crypto.subtle.generateKey(alg, true, ["sign", "verify"]) 186 .then(util.cloneExportCompareKeys) 187 .then(x => { 188 return crypto.subtle.sign(alg, x.privateKey, data) 189 .then(sig => crypto.subtle.verify(alg, x.publicKey, sig, data)); 190 }) 191 .then(complete(that, x => x), error(that)); 192 } 193 ); 194 195 // ----------------------------------------------------------------------------- 196 TestArray.addTest( 197 "Structured Cloning: RSA-PSS", 198 function() { 199 var that = this; 200 var data = crypto.getRandomValues(new Uint8Array(128)); 201 202 var alg = { 203 name: "RSA-PSS", 204 hash: "SHA-256", 205 modulusLength: 2048, 206 publicExponent: new Uint8Array([1, 0, 1]), 207 saltLength: 20, 208 }; 209 210 crypto.subtle.generateKey(alg, true, ["sign", "verify"]) 211 .then(util.cloneExportCompareKeys) 212 .then(x => { 213 return crypto.subtle.sign(alg, x.privateKey, data) 214 .then(sig => crypto.subtle.verify(alg, x.publicKey, sig, data)); 215 }) 216 .then(complete(that, x => x), error(that)); 217 } 218 ); 219 220 // ----------------------------------------------------------------------------- 221 TestArray.addTest( 222 "Structured Cloning: Ed25519", 223 function() { 224 var that = this; 225 var data = crypto.getRandomValues(new Uint8Array(128)); 226 227 var alg = { 228 name: 'Ed25519' 229 }; 230 231 crypto.subtle.generateKey(alg, true, ['sign', 'verify']) 232 .then(util.cloneExportCompareKeys) 233 .then(x => { 234 return crypto.subtle.sign(alg, x.privateKey, data) 235 .then(sig => crypto.subtle.verify(alg, x.publicKey, sig, data)); 236 }) 237 .then(complete(that, x => x), error(that)); 238 } 239 ); 240 241 242 // ----------------------------------------------------------------------------- 243 /* TestArray.addTest( 244 "Structured Cloning: DH", 245 function() { 246 var that = this; 247 var alg = {name: "DH", prime: tv.dh.prime, generator: new Uint8Array([2])}; 248 249 crypto.subtle.generateKey(alg, true, ["deriveBits"]) 250 .then(util.cloneExportCompareKeys) 251 .then(x => { 252 var alg = {name: "DH", public: x.publicKey}; 253 return crypto.subtle.deriveBits(alg, x.privateKey, 16); 254 }) 255 .then(complete(that), error(that)); 256 } 257 );*/ 258 259 // ----------------------------------------------------------------------------- 260 /* TestArray.addTest( 261 "Structured Cloning: ECDH", 262 function() { 263 var that = this; 264 var alg = {name: "ECDH", namedCurve: "P-256"}; 265 266 crypto.subtle.generateKey(alg, true, ["deriveBits"]) 267 .then(util.cloneExportCompareKeys) 268 .then(x => { 269 var alg = {name: "ECDH", public: x.publicKey}; 270 return crypto.subtle.deriveBits(alg, x.privateKey, 16); 271 }) 272 .then(complete(that), error(that)); 273 } 274 );*/ 275 276 // ----------------------------------------------------------------------------- 277 /* TestArray.addTest( 278 "Structured Cloning: ECDSA", 279 function() { 280 var that = this; 281 var data = crypto.getRandomValues(new Uint8Array(128)); 282 var alg = {name: "ECDSA", namedCurve: "P-256", hash: "SHA-256"}; 283 284 crypto.subtle.generateKey(alg, true, ["sign", "verify"]) 285 .then(util.cloneExportCompareKeys) 286 .then(x => { 287 return crypto.subtle.sign(alg, x.privateKey, data) 288 .then(sig => crypto.subtle.verify(alg, x.publicKey, sig, data)); 289 }) 290 .then(complete(that), error(that)); 291 } 292 );*/ 293 /* ]]>*/</script> 294 </head> 295 296 <body> 297 298 <div id="content"> 299 <div id="head"> 300 <b>Web</b>Crypto<br> 301 </div> 302 303 <div id="start" onclick="start();">RUN ALL</div> 304 305 <div id="resultDiv" class="content"> 306 Summary: 307 <span class="pass"><span id="passN">0</span> passed, </span> 308 <span class="fail"><span id="failN">0</span> failed, </span> 309 <span class="pending"><span id="pendingN">0</span> pending.</span> 310 <br/> 311 <br/> 312 313 <table id="results"> 314 <tr> 315 <th>Test</th> 316 <th>Result</th> 317 <th>Time</th> 318 </tr> 319 </table> 320 321 </div> 322 323 <div id="foot"></div> 324 </div> 325 326 </body> 327 </html>