tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_WebCrypto_Workers.html (4707B)


      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  "Send a CryptoKey to a Worker and use it to encrypt data",
     28  function() {
     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);
     33      };
     34    `);
     35 
     36    var data = crypto.getRandomValues(new Uint8Array(128));
     37    var nonce = crypto.getRandomValues(new Uint8Array(16));
     38    var alg = { name: "AES-GCM", length: 128 };
     39    var that = this;
     40 
     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));
     47      });
     48 
     49      // Send it to the worker.
     50      worker.postMessage({key, data, nonce});
     51    });
     52  }
     53 );
     54 
     55 // -----------------------------------------------------------------------------
     56 TestArray.addTest(
     57  "Get a CryptoKey from a Worker and encrypt/decrypt data",
     58  function() {
     59    var worker = new Worker(`data:text/plain,
     60      var alg = { name: "AES-GCM", length: 128 };
     61      crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"])
     62        .then(postMessage);
     63    `);
     64 
     65    var data = crypto.getRandomValues(new Uint8Array(128));
     66    var nonce = crypto.getRandomValues(new Uint8Array(16));
     67    var alg = { name: "AES-GCM", iv: nonce };
     68    var that = this;
     69 
     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));
     77      });
     78    });
     79  }
     80 );
     81 
     82 // -----------------------------------------------------------------------------
     83 TestArray.addTest(
     84  "Web crypto in terminating Worker",
     85  function() {
     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);
     91        });
     92      }
     93      onmessage = ({data: {key, data, nonce}}) => {
     94        infiniteEncrypt(key, data, nonce);
     95        postMessage("started");
     96      };
     97    `);
     98 
     99    var data = crypto.getRandomValues(new Uint8Array(128));
    100    var nonce = crypto.getRandomValues(new Uint8Array(16));
    101    var alg = { name: "AES-GCM", length: 128 };
    102    var that = this;
    103 
    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
    109            worker.terminate();
    110            worker = null;
    111 
    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.
    115            that.complete(true);
    116          }
    117      });
    118 
    119      // Send it to the worker.
    120      worker.postMessage({key, data, nonce});
    121    });
    122  }
    123 );
    124 /* ]]>*/</script>
    125 </head>
    126 
    127 <body>
    128 
    129 <div id="content">
    130 <div id="head">
    131 	<b>Web</b>Crypto<br>
    132 </div>
    133 
    134    <div id="start" onclick="start();">RUN ALL</div>
    135 
    136    <div id="resultDiv" class="content">
    137    Summary:
    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>
    141    <br/>
    142    <br/>
    143 
    144    <table id="results">
    145        <tr>
    146            <th>Test</th>
    147            <th>Result</th>
    148            <th>Time</th>
    149        </tr>
    150    </table>
    151 
    152    </div>
    153 
    154    <div id="foot"></div>
    155 </div>
    156 
    157 </body>
    158 </html>