• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2function define_tests() {
3    // May want to test prefixed implementations.
4    var subtle = self.crypto.subtle;
5
6    var pkcs8 = {
7        "P-521": new Uint8Array([48, 129, 238, 2, 1, 0, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0, 35, 4, 129, 214, 48, 129, 211, 2, 1, 1, 4, 66, 1, 166, 126, 211, 33, 145, 90, 100, 170, 53, 155, 125, 100, 141, 220, 38, 24, 250, 142, 141, 24, 103, 232, 247, 24, 48, 177, 13, 37, 237, 40, 145, 250, 241, 47, 60, 126, 117, 66, 26, 46, 162, 100, 249, 169, 21, 50, 13, 39, 79, 225, 71, 7, 66, 185, 132, 233, 107, 152, 145, 32, 129, 250, 205, 71, 141, 161, 129, 137, 3, 129, 134, 0, 4, 0, 32, 157, 72, 63, 40, 102, 104, 129, 198, 100, 31, 58, 18, 111, 64, 15, 81, 228, 101, 17, 112, 254, 103, 140, 117, 232, 87, 18, 226, 134, 138, 220, 133, 8, 36, 153, 123, 235, 240, 188, 130, 180, 48, 40, 166, 210, 236, 23, 119, 202, 69, 39, 159, 114, 6, 163, 234, 139, 92, 210, 7, 63, 73, 62, 69, 0, 12, 181, 76, 58, 90, 202, 162, 104, 197, 103, 16, 66, 136, 120, 217, 139, 138, 251, 246, 138, 97, 33, 83, 99, 40, 70, 216, 7, 233, 38, 114, 105, 143, 27, 156, 97, 29, 231, 211, 142, 52, 205, 108, 115, 136, 144, 146, 197, 110, 82, 214, 128, 241, 223, 208, 146, 184, 122, 200, 239, 159, 243, 200, 251, 72]),
8        "P-256": new Uint8Array([48, 129, 135, 2, 1, 0, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 4, 109, 48, 107, 2, 1, 1, 4, 32, 15, 247, 79, 232, 241, 202, 175, 97, 92, 206, 241, 29, 217, 53, 114, 87, 98, 217, 216, 65, 236, 186, 185, 94, 170, 38, 68, 123, 52, 100, 245, 113, 161, 68, 3, 66, 0, 4, 140, 96, 11, 44, 102, 25, 45, 97, 158, 39, 210, 37, 107, 59, 151, 118, 178, 141, 30, 5, 246, 13, 234, 189, 98, 174, 123, 154, 211, 157, 224, 217, 59, 4, 102, 109, 199, 119, 14, 126, 207, 13, 211, 203, 203, 211, 110, 221, 107, 94, 220, 153, 81, 7, 55, 161, 237, 104, 46, 205, 112, 244, 10, 47]),
9        "P-384": new Uint8Array([48, 129, 182, 2, 1, 0, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0, 34, 4, 129, 158, 48, 129, 155, 2, 1, 1, 4, 48, 248, 113, 165, 102, 101, 137, 193, 74, 87, 71, 38, 62, 248, 91, 49, 156, 192, 35, 219, 110, 53, 103, 108, 61, 120, 30, 239, 139, 5, 95, 207, 190, 134, 250, 13, 6, 208, 86, 181, 25, 95, 177, 50, 58, 248, 222, 37, 179, 161, 100, 3, 98, 0, 4, 241, 25, 101, 223, 125, 212, 89, 77, 4, 25, 197, 8, 100, 130, 163, 184, 38, 185, 121, 127, 155, 224, 189, 13, 16, 156, 158, 30, 153, 137, 193, 185, 169, 43, 143, 38, 159, 152, 225, 122, 209, 132, 186, 115, 193, 247, 151, 98, 175, 69, 175, 129, 65, 96, 38, 66, 218, 39, 26, 107, 176, 255, 235, 12, 180, 71, 143, 207, 112, 126, 102, 26, 166, 214, 205, 245, 21, 73, 200, 140, 63, 19, 11, 233, 232, 32, 31, 111, 106, 9, 244, 24, 90, 175, 149, 196])
10    };
11
12    var spki = {
13        "P-521": new Uint8Array([48, 129, 155, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0, 35, 3, 129, 134, 0, 4, 0, 238, 105, 249, 71, 21, 215, 1, 233, 226, 1, 19, 51, 212, 244, 249, 108, 186, 125, 145, 248, 139, 17, 43, 175, 117, 207, 9, 204, 31, 138, 202, 151, 97, 141, 169, 56, 152, 34, 210, 155, 111, 233, 153, 106, 97, 32, 62, 247, 82, 183, 113, 232, 149, 143, 196, 103, 123, 179, 119, 133, 101, 171, 96, 214, 237, 0, 222, 171, 103, 97, 137, 91, 147, 94, 58, 211, 37, 251, 133, 73, 229, 111, 19, 120, 106, 167, 63, 136, 162, 236, 254, 64, 147, 52, 115, 216, 174, 242, 64, 196, 223, 215, 213, 6, 242, 44, 221, 14, 85, 85, 143, 63, 191, 5, 235, 247, 239, 239, 122, 114, 215, 143, 70, 70, 155, 132, 72, 242, 110, 39, 18]),
14        "P-256": new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 154, 116, 32, 120, 126, 95, 77, 105, 211, 232, 34, 114, 115, 1, 109, 56, 224, 71, 129, 133, 223, 127, 238, 156, 142, 103, 60, 202, 211, 79, 126, 128, 254, 49, 141, 182, 221, 107, 119, 218, 99, 32, 165, 246, 151, 89, 9, 68, 23, 177, 52, 239, 138, 139, 116, 193, 101, 4, 57, 198, 115, 0, 90, 61]),
15        "P-384": new Uint8Array([48, 118, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0, 34, 3, 98, 0, 4, 145, 130, 45, 194, 175, 89, 193, 143, 91, 103, 248, 13, 246, 26, 38, 3, 194, 168, 240, 179, 192, 175, 130, 45, 99, 194, 121, 112, 26, 130, 69, 96, 64, 68, 1, 221, 233, 165, 110, 229, 39, 87, 234, 139, 199, 72, 212, 200, 43, 83, 55, 180, 141, 123, 101, 88, 58, 61, 87, 36, 56, 136, 0, 54, 186, 198, 115, 15, 66, 202, 82, 120, 150, 107, 213, 242, 30, 134, 226, 29, 48, 197, 166, 208, 70, 62, 197, 19, 221, 80, 159, 252, 220, 175, 31, 245])
16    };
17
18    var sizes = {
19        "P-521": 66,
20        "P-256": 32,
21        "P-384": 48
22    };
23
24    var derivations = {
25        "P-521": new Uint8Array([0, 156, 43, 206, 87, 190, 128, 173, 171, 59, 7, 56, 91, 142, 89, 144, 235, 125, 111, 222, 189, 176, 27, 243, 83, 113, 164, 246, 7, 94, 157, 40, 138, 193, 42, 109, 254, 3, 170, 87, 67, 188, 129, 112, 157, 73, 168, 34, 148, 2, 25, 182, 75, 118, 138, 205, 82, 15, 161, 54, 142, 160, 175, 141, 71, 93]),
26        "P-256": new Uint8Array([14, 143, 60, 77, 177, 178, 162, 131, 115, 90, 0, 220, 87, 31, 26, 232, 151, 28, 227, 35, 250, 17, 131, 137, 203, 95, 65, 196, 59, 61, 181, 161]),
27        "P-384": new Uint8Array([224, 189, 107, 206, 10, 239, 140, 164, 136, 56, 166, 226, 252, 197, 126, 103, 185, 197, 232, 134, 12, 95, 11, 233, 218, 190, 197, 62, 69, 78, 24, 160, 161, 116, 196, 136, 136, 162, 100, 136, 17, 91, 45, 201, 241, 223, 165, 45])
28    };
29
30    return importKeys(pkcs8, spki, sizes)
31    .then(function(results) {
32        publicKeys = results.publicKeys;
33        privateKeys = results.privateKeys;
34        ecdsaKeyPairs = results.ecdsaKeyPairs;
35        noDeriveBitsKeys = results.noDeriveBitsKeys;
36
37        Object.keys(sizes).forEach(function(namedCurve) {
38            // Basic success case
39            promise_test(function(test) {
40                return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, privateKeys[namedCurve], 8 * sizes[namedCurve])
41                .then(function(derivation) {
42                    assert_true(equalBuffers(derivation, derivations[namedCurve]), "Derived correct bits");
43                }, function(err) {
44                    assert_unreached("deriveBits failed with error " + err.name + ": " + err.message);
45                });
46            }, namedCurve + " good parameters");
47
48            // Case insensitivity check
49            promise_test(function(test) {
50                return subtle.deriveBits({name: "EcDh", public: publicKeys[namedCurve]}, privateKeys[namedCurve], 8 * sizes[namedCurve])
51                .then(function(derivation) {
52                    assert_true(equalBuffers(derivation, derivations[namedCurve]), "Derived correct bits");
53                }, function(err) {
54                    assert_unreached("deriveBits failed with error " + err.name + ": " + err.message);
55                });
56            }, namedCurve + " mixed case parameters");
57
58            // Null length
59            promise_test(function(test) {
60                return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, privateKeys[namedCurve], null)
61                .then(function(derivation) {
62                    assert_true(equalBuffers(derivation, derivations[namedCurve]), "Derived correct bits");
63                }, function(err) {
64                    assert_unreached("deriveBits failed with error " + err.name + ": " + err.message);
65                });
66            }, namedCurve + " with null length");
67
68            // Shorter than entire derivation per algorithm
69            promise_test(function(test) {
70                return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, privateKeys[namedCurve], 8 * sizes[namedCurve] - 32)
71                .then(function(derivation) {
72                    assert_true(equalBuffers(derivation, derivations[namedCurve], 8 * sizes[namedCurve] - 32), "Derived correct bits");
73                }, function(err) {
74                    assert_unreached("deriveBits failed with error " + err.name + ": " + err.message);
75                });
76            }, namedCurve + " short result");
77
78            // Non-multiple of 8
79            promise_test(function(test) {
80                return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, privateKeys[namedCurve], 8 * sizes[namedCurve] - 11)
81                .then(function(derivation) {
82                    assert_true(equalBuffers(derivation, derivations[namedCurve], 8 * sizes[namedCurve] - 11), "Derived correct bits");
83                }, function(err) {
84                    assert_unreached("deriveBits failed with error " + err.name + ": " + err.message);
85                });
86            }, namedCurve + " non-multiple of 8 bits");
87
88            // Errors to test:
89
90            // - missing public property TypeError
91            promise_test(function(test) {
92                return subtle.deriveBits({name: "ECDH"}, privateKeys[namedCurve], 8 * sizes[namedCurve])
93                .then(function(derivation) {
94                    assert_unreached("deriveBits succeeded but should have failed with TypeError");
95                }, function(err) {
96                    assert_equals(err.name, "TypeError", "Should throw correct error, not " + err.name + ": " + err.message);
97                });
98            }, namedCurve + " missing public curve");
99
100            // - Non CryptoKey public property TypeError
101            promise_test(function(test) {
102                return subtle.deriveBits({name: "ECDH", public: {message: "Not a CryptoKey"}}, privateKeys[namedCurve], 8 * sizes[namedCurve])
103                .then(function(derivation) {
104                    assert_unreached("deriveBits succeeded but should have failed with TypeError");
105                }, function(err) {
106                    assert_equals(err.name, "TypeError", "Should throw correct error, not " + err.name + ": " + err.message);
107                });
108            }, namedCurve + " public property of algorithm is not a CryptoKey");
109
110            // - wrong named curve
111            promise_test(function(test) {
112                publicKey = publicKeys["P-256"];
113                if (namedCurve === "P-256") {
114                    publicKey = publicKeys["P-384"];
115                }
116                return subtle.deriveBits({name: "ECDH", public: publicKey}, privateKeys[namedCurve], 8 * sizes[namedCurve])
117                .then(function(derivation) {
118                    assert_unreached("deriveBits succeeded but should have failed with InvalidAccessError");
119                }, function(err) {
120                    assert_equals(err.name, "InvalidAccessError", "Should throw correct error, not " + err.name + ": " + err.message);
121                });
122            }, namedCurve + " mismatched curves");
123
124            // - not ECDH public property InvalidAccessError
125            promise_test(function(test) {
126                return subtle.deriveBits({name: "ECDH", public: ecdsaKeyPairs[namedCurve].publicKey}, privateKeys[namedCurve], 8 * sizes[namedCurve])
127                .then(function(derivation) {
128                    assert_unreached("deriveBits succeeded but should have failed with InvalidAccessError");
129                }, function(err) {
130                    assert_equals(err.name, "InvalidAccessError", "Should throw correct error, not " + err.name + ": " + err.message);
131                });
132            }, namedCurve + " public property of algorithm is not an ECDSA public key");
133
134            // - No deriveBits usage in baseKey InvalidAccessError
135            promise_test(function(test) {
136                return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, noDeriveBitsKeys[namedCurve], 8 * sizes[namedCurve])
137                .then(function(derivation) {
138                    assert_unreached("deriveBits succeeded but should have failed with InvalidAccessError");
139                }, function(err) {
140                    assert_equals(err.name, "InvalidAccessError", "Should throw correct error, not " + err.name + ": " + err.message);
141                });
142            }, namedCurve + " no deriveBits usage for base key");
143
144            // - Use public key for baseKey InvalidAccessError
145            promise_test(function(test) {
146                return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, publicKeys[namedCurve], 8 * sizes[namedCurve])
147                .then(function(derivation) {
148                    assert_unreached("deriveBits succeeded but should have failed with InvalidAccessError");
149                }, function(err) {
150                    assert_equals(err.name, "InvalidAccessError", "Should throw correct error, not " + err.name + ": " + err.message);
151                });
152            }, namedCurve + " base key is not a private key");
153
154            // - Use private key for public property InvalidAccessError
155            promise_test(function(test) {
156                return subtle.deriveBits({name: "ECDH", public: privateKeys[namedCurve]}, privateKeys[namedCurve], 8 * sizes[namedCurve])
157                .then(function(derivation) {
158                    assert_unreached("deriveBits succeeded but should have failed with InvalidAccessError");
159                }, function(err) {
160                    assert_equals(err.name, "InvalidAccessError", "Should throw correct error, not " + err.name + ": " + err.message);
161                });
162            }, namedCurve + " public property value is a private key");
163
164            // - Use secret key for public property InvalidAccessError
165            promise_test(function(test) {
166                return subtle.generateKey({name: "AES-CBC", length: 128}, true, ["encrypt", "decrypt"])
167                .then(function(secretKey) {
168                    return subtle.deriveBits({name: "ECDH", public: secretKey}, privateKeys[namedCurve], 8 * sizes[namedCurve])
169                    .then(function(derivation) {
170                        assert_unreached("deriveBits succeeded but should have failed with InvalidAccessError");
171                    }, function(err) {
172                        assert_equals(err.name, "InvalidAccessError", "Should throw correct error, not " + err.name + ": " + err.message);
173                    });
174                });
175            }, namedCurve + " public property value is a secret key");
176
177            // - Length greater than 256, 384, 521 for particular curves OperationError
178            promise_test(function(test) {
179                return subtle.deriveBits({name: "ECDH", public: publicKeys[namedCurve]}, privateKeys[namedCurve], 8 * sizes[namedCurve] + 8)
180                .then(function(derivation) {
181                    assert_unreached("deriveBits succeeded but should have failed with OperationError");
182                }, function(err) {
183                    assert_equals(err.name, "OperationError", "Should throw correct error, not " + err.name + ": " + err.message);
184                });
185            }, namedCurve + " asking for too many bits");
186        });
187    });
188
189    function importKeys(pkcs8, spki, sizes) {
190        var privateKeys = {};
191        var publicKeys = {};
192        var ecdsaKeyPairs = {};
193        var noDeriveBitsKeys = {};
194
195        var promises = [];
196        Object.keys(pkcs8).forEach(function(namedCurve) {
197            var operation = subtle.importKey("pkcs8", pkcs8[namedCurve],
198                                            {name: "ECDH", namedCurve: namedCurve},
199                                            false, ["deriveBits", "deriveKey"])
200                            .then(function(key) {
201                                privateKeys[namedCurve] = key;
202                            }, function (err) {
203                                privateKeys[namedCurve] = null;
204                            });
205            promises.push(operation);
206        });
207        Object.keys(pkcs8).forEach(function(namedCurve) {
208            var operation = subtle.importKey("pkcs8", pkcs8[namedCurve],
209                                            {name: "ECDH", namedCurve: namedCurve},
210                                            false, ["deriveKey"])
211                            .then(function(key) {
212                                noDeriveBitsKeys[namedCurve] = key;
213                            }, function (err) {
214                                noDeriveBitsKeys[namedCurve] = null;
215                            });
216            promises.push(operation);
217        });
218        Object.keys(spki).forEach(function(namedCurve) {
219            var operation = subtle.importKey("spki", spki[namedCurve],
220                                            {name: "ECDH", namedCurve: namedCurve},
221                                            false, [])
222                            .then(function(key) {
223                                publicKeys[namedCurve] = key;
224                            }, function (err) {
225                                publicKeys[namedCurve] = null;
226                            });
227            promises.push(operation);
228        });
229        Object.keys(sizes).forEach(function(namedCurve) {
230            var operation = subtle.generateKey({name: "ECDSA", namedCurve: namedCurve}, false, ["sign", "verify"])
231                            .then(function(keyPair) {
232                                ecdsaKeyPairs[namedCurve] = keyPair;
233                            }, function (err) {
234                                ecdsaKeyPairs[namedCurve] = null;
235                            });
236            promises.push(operation);
237        });
238
239        return Promise.all(promises)
240               .then(function(results) {return {privateKeys: privateKeys, publicKeys: publicKeys, ecdsaKeyPairs: ecdsaKeyPairs, noDeriveBitsKeys: noDeriveBitsKeys}});
241    }
242
243    // Compares two ArrayBuffer or ArrayBufferView objects. If bitCount is
244    // omitted, the two values must be the same length and have the same contents
245    // in every byte. If bitCount is included, only that leading number of bits
246    // have to match.
247    function equalBuffers(a, b, bitCount) {
248        var remainder;
249
250        if (typeof bitCount === "undefined" && a.byteLength !== b.byteLength) {
251            return false;
252        }
253
254        var aBytes = new Uint8Array(a);
255        var bBytes = new Uint8Array(b);
256
257        var length = a.byteLength;
258        if (typeof bitCount !== "undefined") {
259            length = Math.floor(bitCount / 8);
260        }
261
262        for (var i=0; i<length; i++) {
263            if (aBytes[i] !== bBytes[i]) {
264                return false;
265            }
266        }
267
268        if (typeof bitCount !== "undefined") {
269            remainder = bitCount % 8;
270            return aBytes[length] >> (8 - remainder) === bBytes[length] >> (8 - remainder);
271        }
272
273        return true;
274    }
275
276}
277