tor-browser

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

test_WebCrypto_ECDSA.html (7714B)


      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  "Generate an ECDSA key for named curve P-256",
     28  function() {
     29    var that = this;
     30    var alg = { name: "ECDSA", namedCurve: "P-256" };
     31    crypto.subtle.generateKey(alg, false, ["sign", "verify"]).then(
     32      complete(that, function(x) {
     33        return exists(x.publicKey) &&
     34               (x.publicKey.algorithm.name == alg.name) &&
     35               (x.publicKey.algorithm.namedCurve == alg.namedCurve) &&
     36               (x.publicKey.type == "public") &&
     37               x.publicKey.extractable &&
     38               (x.publicKey.usages.length == 1) &&
     39               (x.publicKey.usages[0] == "verify") &&
     40               exists(x.privateKey) &&
     41               (x.privateKey.algorithm.name == alg.name) &&
     42               (x.privateKey.algorithm.namedCurve == alg.namedCurve) &&
     43               (x.privateKey.type == "private") &&
     44               !x.privateKey.extractable &&
     45               (x.privateKey.usages.length == 1) &&
     46               (x.privateKey.usages[0] == "sign");
     47      }),
     48      error(that)
     49    );
     50  }
     51 );
     52 
     53 // -----------------------------------------------------------------------------
     54 TestArray.addTest(
     55  "ECDSA JWK import and verify a known-good signature",
     56  function() {
     57    var that = this;
     58    var alg = { name: "ECDSA", namedCurve: "P-521", hash: "SHA-512" };
     59 
     60    function doVerify(x) {
     61      return crypto.subtle.verify(alg, x, tv.ecdsa_verify.sig, tv.ecdsa_verify.data);
     62    }
     63 
     64    crypto.subtle.importKey("jwk", tv.ecdsa_verify.pub_jwk, alg, true, ["verify"])
     65      .then(doVerify)
     66      .then(complete(that, x => x), error(that));
     67  }
     68 );
     69 
     70 // -----------------------------------------------------------------------------
     71 TestArray.addTest(
     72  "ECDSA key generation with public key export",
     73  function() {
     74    var that = this;
     75    var alg = { name: "ECDSA", namedCurve: "P-256", hash: "SHA-256" };
     76    var msg = Uint8Array.from([1]);
     77 
     78    crypto.subtle.generateKey(alg, false, ["sign", "verify"])
     79      .then(pair => Promise.all([
     80          crypto.subtle.sign(alg, pair.privateKey, msg),
     81          crypto.subtle.exportKey("spki", pair.publicKey)
     82            .then(spki => crypto.subtle.importKey("spki", spki, alg, false, ["verify"])),
     83      ]))
     84      .then(sigAndKey => crypto.subtle.verify(alg, sigAndKey[1], sigAndKey[0], msg))
     85      .then(complete(that, x => x), error(that));
     86  }
     87 );
     88 
     89 // -----------------------------------------------------------------------------
     90 TestArray.addTest(
     91  "ECDSA JWK import and reject a known-bad signature",
     92  function() {
     93    var that = this;
     94    var alg = { name: "ECDSA", namedCurve: "P-521", hash: "SHA-512" };
     95 
     96    function doVerify(x) {
     97      return crypto.subtle.verify(alg, x, tv.ecdsa_verify.sig_tampered,
     98                                          tv.ecdsa_verify.data);
     99    }
    100 
    101    crypto.subtle.importKey("jwk", tv.ecdsa_verify.pub_jwk, alg, true, ["verify"])
    102      .then(doVerify)
    103      .then(complete(that, x => !x), error(that));
    104  }
    105 );
    106 
    107 // -----------------------------------------------------------------------------
    108 TestArray.addTest(
    109  "ECDSA sign/verify round-trip",
    110  function() {
    111    var that = this;
    112    var alg = { name: "ECDSA", namedCurve: "P-521", hash: "SHA-512" };
    113    var pubKey;
    114 
    115 
    116    function doSign(keyPair) {
    117      pubKey = keyPair.publicKey;
    118      return crypto.subtle.sign(alg, keyPair.privateKey, tv.ecdsa_verify.data);
    119    }
    120    function doVerify(sig) {
    121      return crypto.subtle.verify(alg, pubKey, sig, tv.ecdsa_verify.data);
    122    }
    123 
    124    crypto.subtle.generateKey(alg, true, ["sign", "verify"])
    125      .then(doSign)
    126      .then(doVerify)
    127      .then(complete(that, x => x), error(that));
    128  }
    129 );
    130 
    131 
    132 // -----------------------------------------------------------------------------
    133 TestArray.addTest(
    134  "Verify that ECDSA import fails with a key with a mismatched 'alg' field",
    135  function() {
    136    var that = this;
    137    var alg = { name: "ECDSA", namedCurve: "P-521", hash: "SHA-512" };
    138 
    139    crypto.subtle.importKey("jwk", tv.ecdsa_jwk_alg_mismatch.pub_jwk, alg, true, ["verify"])
    140      .then(error(that), complete(that));
    141  }
    142 );
    143 
    144 // -----------------------------------------------------------------------------
    145 TestArray.addTest(
    146  "Verify that ECDSA import fails with a key with a mismatched 'crv' field",
    147  function() {
    148    var that = this;
    149    var alg = { name: "ECDSA", namedCurve: "P-521", hash: "SHA-512" };
    150 
    151    crypto.subtle.importKey("jwk", tv.ecdsa_jwk_crv_mismatch.pub_jwk, alg, true, ["verify"])
    152      .then(error(that), complete(that));
    153  }
    154 );
    155 
    156 // -----------------------------------------------------------------------------
    157 TestArray.addTest(
    158  "Verify that ECDSA import fails with a known-bad public key",
    159  function() {
    160    var that = this;
    161    var alg = { name: "ECDSA", namedCurve: "P-521", hash: "SHA-512" };
    162 
    163    crypto.subtle.importKey("jwk", tv.ecdsa_bad.pub_jwk, alg, true, ["verify"])
    164      .then(error(that), complete(that));
    165  }
    166 );
    167 
    168 // -----------------------------------------------------------------------------
    169 TestArray.addTest(
    170  "Raw import/export of a public ECDSA key (P-521)",
    171  function() {
    172    var that = this;
    173    var alg = { name: "ECDSA", namedCurve: "P-521", hash: "SHA-512" };
    174 
    175    function doExport(x) {
    176      return crypto.subtle.exportKey("raw", x);
    177    }
    178 
    179    crypto.subtle.importKey("raw", tv.ecdsa_verify.raw, alg, true, ["verify"])
    180      .then(doExport)
    181      .then(memcmp_complete(that, tv.ecdsa_verify.raw), error(that));
    182  }
    183 );
    184 
    185 // -----------------------------------------------------------------------------
    186 TestArray.addTest(
    187  "ECDSA raw import and verify a known-good signature",
    188  function() {
    189    var that = this;
    190    var alg = { name: "ECDSA", namedCurve: "P-521", hash: "SHA-512" };
    191 
    192    function doVerify(x) {
    193      return crypto.subtle.verify(alg, x, tv.ecdsa_verify.sig, tv.ecdsa_verify.data);
    194    }
    195 
    196    crypto.subtle.importKey("raw", tv.ecdsa_verify.raw, alg, true, ["verify"])
    197      .then(doVerify)
    198      .then(complete(that, x => x), error(that));
    199  }
    200 );
    201 
    202 // -----------------------------------------------------------------------------
    203 TestArray.addTest(
    204  "Importing an RSA key as an ECDSA key should fail",
    205  function() {
    206    var that = this;
    207    var alg = { name: "ECDSA", namedCurve: "P-256" };
    208 
    209    // tv.spki is the SPKI for an RSA key, not an ECDSA key
    210    crypto.subtle.importKey("spki", tv.spki, alg, false, ["verify"])
    211      .then(error(that), complete(that));
    212  }
    213 );
    214 
    215 /* ]]>*/</script>
    216 </head>
    217 
    218 <body>
    219 
    220 <div id="content">
    221        <div id="head">
    222                <b>Web</b>Crypto<br>
    223        </div>
    224 
    225    <div id="start" onclick="start();">RUN ALL</div>
    226 
    227    <div id="resultDiv" class="content">
    228    Summary:
    229    <span class="pass"><span id="passN">0</span> passed, </span>
    230    <span class="fail"><span id="failN">0</span> failed, </span>
    231    <span class="pending"><span id="pendingN">0</span> pending.</span>
    232    <br/>
    233    <br/>
    234 
    235    <table id="results">
    236        <tr>
    237            <th>Test</th>
    238            <th>Result</th>
    239            <th>Time</th>
    240        </tr>
    241    </table>
    242 
    243    </div>
    244 
    245    <div id="foot"></div>
    246 </div>
    247 
    248 </body>
    249 </html>