Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / crypto / test / test_WebCrypto_RSA_OAEP.html
blobaaa12cc95ad9b9b9154dd8c1c26baa376eeca95c
1 <!DOCTYPE html>
2 <html>
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>
10 <!-- Utilities for manipulating ABVs -->
11 <script src="util.js"></script>
13 <!-- A simple wrapper around IndexedDB -->
14 <script src="simpledb.js"></script>
16 <!-- Test vectors drawn from the literature -->
17 <script src="./test-vectors.js"></script>
19 <!-- General testing framework -->
20 <script src="./test-array.js"></script>
22 <script>/* <![CDATA[*/
23 "use strict";
25 // Generating 2048-bit keys takes some time.
26 SimpleTest.requestLongerTimeout(2);
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"};
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);
41 function doDecrypt(x) {
42 return crypto.subtle.decrypt(alg, privKey, x);
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)
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]),
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);
76 function doDecrypt(x) {
77 return crypto.subtle.decrypt(alg, privKey, x);
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)
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"};
98 function doDecrypt(x) {
99 return crypto.subtle.decrypt(alg, x, tv.rsaoaep.result);
101 function fail() { error(that); }
103 crypto.subtle.importKey("pkcs8", tv.rsaoaep.pkcs8, alg, false, ["decrypt"])
104 .then( doDecrypt, fail )
105 .then( memcmp_complete(that, tv.rsaoaep.data), fail );
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]),
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));
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));
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"};
145 crypto.subtle.importKey("pkcs8", tv.rsaoaep.pkcs8, alg, false, ["decrypt"])
146 .then(error(that), complete(that));
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]),
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);
167 function doDecrypt(x) {
168 return crypto.subtle.decrypt("RSA-OAEP", privKey, x);
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));
178 /* ]]>*/</script>
179 </head>
181 <body>
183 <div id="content">
184 <div id="head">
185 <b>Web</b>Crypto<br>
186 </div>
188 <div id="start" onclick="start();">RUN ALL</div>
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/>
198 <table id="results">
199 <tr>
200 <th>Test</th>
201 <th>Result</th>
202 <th>Time</th>
203 </tr>
204 </table>
206 </div>
208 <div id="foot"></div>
209 </div>
211 </body>
212 </html>