summaryrefslogtreecommitdiffstats
path: root/dom/crypto/test/test_WebCrypto_Import_Multiple_Identical_Keys.html
blob: 6913bb78e34bf13f1ef27767d56298d1eb58830a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html>

<head>
<title>WebCrypto Test Suite</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="./test_WebCrypto.css"/>
<script src="/tests/SimpleTest/SimpleTest.js"></script>

<!-- General testing framework -->
<script src="./test-array.js"></script>

<script>/*<![CDATA[*/
"use strict";

// -----------------------------------------------------------------------------
TestArray.addTest(
  "Import the same ECDSA key multiple times and ensure that it can be used.",
  function () {
    var alg = { name: "ECDSA", namedCurve: "P-256", hash: "SHA-256" };
    crypto.subtle.generateKey(alg, true, ["sign", "verify"])
    .then(function(keyPair) {
      return crypto.subtle.exportKey("jwk", keyPair.privateKey);
    })
    .then(function(exportedKey) {
      let keyImportPromises = [];
      for (let i = 0; i < 20; i++) {
        keyImportPromises.push(
          crypto.subtle.importKey("jwk", exportedKey, alg, false, ["sign"]));
      }
      return Promise.all(keyImportPromises);
    })
    .then(function(importedKeys) {
      let signPromises = [];
      let data = crypto.getRandomValues(new Uint8Array(32));
      for (let key of importedKeys) {
        signPromises.push(crypto.subtle.sign(alg, key, data));
      }
      return Promise.all(signPromises);
    })
    .then(complete(this, function(signatures) {
      return signatures.length == 20;
    }), error(this));
  }
);

// -----------------------------------------------------------------------------
// This is the same test, but with an RSA key. This test framework stringifies
// each test so it can be sent to and ran in a worker, which unfortunately
// means we can't factor out common code here.
TestArray.addTest(
  "Import the same RSA key multiple times and ensure that it can be used.",
  function () {
    var alg = {
      name: "RSASSA-PKCS1-v1_5",
      modulusLength: 2048,
      publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
      hash: "SHA-256"
    };
    crypto.subtle.generateKey(alg, true, ["sign", "verify"])
    .then(function(keyPair) {
      return crypto.subtle.exportKey("jwk", keyPair.privateKey);
    })
    .then(function(exportedKey) {
      let keyImportPromises = [];
      for (let i = 0; i < 20; i++) {
        keyImportPromises.push(
          crypto.subtle.importKey("jwk", exportedKey, alg, false, ["sign"]));
      }
      return Promise.all(keyImportPromises);
    })
    .then(function(importedKeys) {
      let signPromises = [];
      let data = crypto.getRandomValues(new Uint8Array(32));
      for (let key of importedKeys) {
        signPromises.push(crypto.subtle.sign(alg, key, data));
      }
      return Promise.all(signPromises);
    })
    .then(complete(this, function(signatures) {
      return signatures.length == 20;
    }), error(this));
  }
);
/*]]>*/</script>
</head>

<body>

<div id="content">
	<div id="head">
		<b>Web</b>Crypto<br>
	</div>

    <div id="start" onclick="start();">RUN ALL</div>

    <div id="resultDiv" class="content">
    Summary:
    <span class="pass"><span id="passN">0</span> passed, </span>
    <span class="fail"><span id="failN">0</span> failed, </span>
    <span class="pending"><span id="pendingN">0</span> pending.</span>
    <br/>
    <br/>

    <table id="results">
        <tr>
            <th>Test</th>
            <th>Result</th>
            <th>Time</th>
        </tr>
    </table>

    </div>

    <div id="foot"></div>
</div>

</body>
</html>