tor-browser

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

test_WebCrypto_RSA_OAEP.html (5855B)


      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 // Generating 2048-bit keys takes some time.
     26 SimpleTest.requestLongerTimeout(2);
     27 
     28 // -----------------------------------------------------------------------------
     29 TestArray.addTest(
     30  "RSA-OAEP encrypt/decrypt round-trip",
     31  function() {
     32    var that = this;
     33    var privKey, pubKey;
     34    var alg = {name: "RSA-OAEP", hash: "SHA-1"};
     35 
     36    function setPriv(x) { privKey = x; }
     37    function setPub(x) { pubKey = x; }
     38    function doEncrypt() {
     39      return crypto.subtle.encrypt(alg, pubKey, tv.rsaoaep.data);
     40    }
     41    function doDecrypt(x) {
     42      return crypto.subtle.decrypt(alg, privKey, x);
     43    }
     44 
     45    Promise.all([
     46      crypto.subtle.importKey("pkcs8", tv.rsaoaep.pkcs8, alg, false, ["decrypt"])
     47          .then(setPriv, error(that)),
     48      crypto.subtle.importKey("spki", tv.rsaoaep.spki, alg, false, ["encrypt"])
     49          .then(setPub, error(that)),
     50    ]).then(doEncrypt, error(that))
     51      .then(doDecrypt, error(that))
     52      .then(
     53        memcmp_complete(that, tv.rsaoaep.data),
     54        error(that)
     55      );
     56  }
     57 );
     58 
     59 // -----------------------------------------------------------------------------
     60 TestArray.addTest(
     61  "RSA-OAEP key generation and encrypt/decrypt round-trip (SHA-256)",
     62  function() {
     63    var that = this;
     64    var alg = {
     65      name: "RSA-OAEP",
     66      hash: "SHA-256",
     67      modulusLength: 2048,
     68      publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
     69    };
     70 
     71    var privKey, pubKey, data = crypto.getRandomValues(new Uint8Array(128));
     72    function setKey(x) { pubKey = x.publicKey; privKey = x.privateKey; }
     73    function doEncrypt() {
     74      return crypto.subtle.encrypt(alg, pubKey, data);
     75    }
     76    function doDecrypt(x) {
     77      return crypto.subtle.decrypt(alg, privKey, x);
     78    }
     79 
     80    crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"])
     81      .then(setKey, error(that))
     82      .then(doEncrypt, error(that))
     83      .then(doDecrypt, error(that))
     84      .then(
     85        memcmp_complete(that, data),
     86        error(that)
     87      );
     88  }
     89 );
     90 
     91 // -----------------------------------------------------------------------------
     92 TestArray.addTest(
     93  "RSA-OAEP decryption known answer",
     94  function() {
     95    var that = this;
     96    var alg = {name: "RSA-OAEP", hash: "SHA-1"};
     97 
     98    function doDecrypt(x) {
     99      return crypto.subtle.decrypt(alg, x, tv.rsaoaep.result);
    100    }
    101    function fail() { error(that); }
    102 
    103    crypto.subtle.importKey("pkcs8", tv.rsaoaep.pkcs8, alg, false, ["decrypt"])
    104      .then( doDecrypt, fail )
    105      .then( memcmp_complete(that, tv.rsaoaep.data), fail );
    106  }
    107 );
    108 
    109 // -----------------------------------------------------------------------------
    110 TestArray.addTest(
    111  "RSA-OAEP input data length checks (2048-bit key)",
    112  function() {
    113    var that = this;
    114    var pubKey;
    115    var alg = {
    116      name: "RSA-OAEP",
    117      hash: "SHA-1",
    118      modulusLength: 2048,
    119      publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
    120    };
    121 
    122    function setKey(x) { pubKey = x.publicKey; }
    123    function doEncrypt(n) {
    124      console.log("entered encrypt(" + n + ")");
    125      return function() {
    126        return crypto.subtle.encrypt(alg, pubKey, new Uint8Array(n));
    127      };
    128    }
    129 
    130    crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"])
    131      .then(setKey, error(that))
    132      .then(doEncrypt(214), error(that))
    133      .then(doEncrypt(215), error(that))
    134      .then(error(that), complete(that));
    135  }
    136 );
    137 
    138 // -----------------------------------------------------------------------------
    139 TestArray.addTest(
    140  "RSA-OAEP key import with invalid hash",
    141  function() {
    142    var that = this;
    143    var alg = {name: "RSA-OAEP", hash: "SHA-123"};
    144 
    145    crypto.subtle.importKey("pkcs8", tv.rsaoaep.pkcs8, alg, false, ["decrypt"])
    146      .then(error(that), complete(that));
    147  }
    148 );
    149 
    150 // -----------------------------------------------------------------------------
    151 TestArray.addTest(
    152  "Test that RSA-OAEP encrypt/decrypt accepts strings as AlgorithmIdentifiers",
    153  function() {
    154    var that = this;
    155    var alg = {
    156      name: "RSA-OAEP",
    157      hash: "SHA-256",
    158      modulusLength: 2048,
    159      publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
    160    };
    161 
    162    var privKey, pubKey, data = crypto.getRandomValues(new Uint8Array(128));
    163    function setKey(x) { pubKey = x.publicKey; privKey = x.privateKey; }
    164    function doEncrypt() {
    165      return crypto.subtle.encrypt("RSA-OAEP", pubKey, data);
    166    }
    167    function doDecrypt(x) {
    168      return crypto.subtle.decrypt("RSA-OAEP", privKey, x);
    169    }
    170 
    171    crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"])
    172      .then(setKey)
    173      .then(doEncrypt)
    174      .then(doDecrypt)
    175      .then(memcmp_complete(that, data), error(that));
    176  }
    177 );
    178 /* ]]>*/</script>
    179 </head>
    180 
    181 <body>
    182 
    183 <div id="content">
    184 <div id="head">
    185 	<b>Web</b>Crypto<br>
    186 </div>
    187 
    188    <div id="start" onclick="start();">RUN ALL</div>
    189 
    190    <div id="resultDiv" class="content">
    191    Summary:
    192    <span class="pass"><span id="passN">0</span> passed, </span>
    193    <span class="fail"><span id="failN">0</span> failed, </span>
    194    <span class="pending"><span id="pendingN">0</span> pending.</span>
    195    <br/>
    196    <br/>
    197 
    198    <table id="results">
    199        <tr>
    200            <th>Test</th>
    201            <th>Result</th>
    202            <th>Time</th>
    203        </tr>
    204    </table>
    205 
    206    </div>
    207 
    208    <div id="foot"></div>
    209 </div>
    210 
    211 </body>
    212 </html>