()
| 1 | |
| 2 | function define_tests() { |
| 3 | // May want to test prefixed implementations. |
| 4 | var subtle = self.crypto.subtle; |
| 5 | |
| 6 | // hkdf2_vectors sets up test data with the correct derivations for each |
| 7 | // test case. |
| 8 | var testData = getTestData(); |
| 9 | var derivedKeys = testData.derivedKeys; |
| 10 | var salts = testData.salts; |
| 11 | var derivations = testData.derivations; |
| 12 | var infos = testData.infos; |
| 13 | |
| 14 | // What kinds of keys can be created with deriveKey? The following: |
| 15 | var derivedKeyTypes = testData.derivedKeyTypes; |
| 16 | |
| 17 | return setUpBaseKeys(derivedKeys) |
| 18 | .then(function(allKeys) { |
| 19 | // We get several kinds of base keys. Normal ones that can be used for |
| 20 | // derivation operations, ones that lack the deriveBits usage, ones |
| 21 | // that lack the deriveKeys usage, and one key that is for the wrong |
| 22 | // algorithm (not HKDF in this case). |
| 23 | var baseKeys = allKeys.baseKeys; |
| 24 | var noBits = allKeys.noBits; |
| 25 | var noKey = allKeys.noKey; |
| 26 | var wrongKey = allKeys.wrongKey; |
| 27 | |
| 28 | // Test each combination of derivedKey size, salt size, hash function, |
| 29 | // and number of iterations. The derivations object is structured in |
| 30 | // that way, so navigate it to run tests and compare with correct results. |
| 31 | Object.keys(derivations).forEach(function(derivedKeySize) { |
| 32 | Object.keys(derivations[derivedKeySize]).forEach(function(saltSize) { |
| 33 | Object.keys(derivations[derivedKeySize][saltSize]).forEach(function(hashName) { |
| 34 | Object.keys(derivations[derivedKeySize][saltSize][hashName]).forEach(function(infoSize) { |
| 35 | var testName = derivedKeySize + " derivedKey, " + saltSize + " salt, " + hashName + ", with " + infoSize + " info"; |
| 36 | var algorithm = {name: "HKDF", salt: salts[saltSize], info: infos[infoSize], hash: hashName}; |
| 37 | |
| 38 | // Check for correct deriveBits result |
| 39 | subsetTest(promise_test, function(test) { |
| 40 | return subtle.deriveBits(algorithm, baseKeys[derivedKeySize], 256) |
| 41 | .then(function(derivation) { |
| 42 | assert_true(equalBuffers(derivation, derivations[derivedKeySize][saltSize][hashName][infoSize]), "Derived correct key"); |
| 43 | }, function(err) { |
| 44 | assert_unreached("deriveBits failed with error " + err.name + ": " + err.message); |
| 45 | }); |
| 46 | }, testName); |
| 47 | |
| 48 | // 0 length |
| 49 | subsetTest(promise_test, function(test) { |
| 50 | return subtle.deriveBits(algorithm, baseKeys[derivedKeySize], 0) |
| 51 | .then(function(derivation) { |
| 52 | assert_equals(derivation.byteLength, 0, "Derived correctly empty key"); |
| 53 | }, function(err) { |
| 54 | assert_unreached("deriveBits failed with error " + err.name + ": " + err.message); |
| 55 | }); |
| 56 | }, testName + " with 0 length"); |
| 57 | |
| 58 | // Check for correct deriveKey results for every kind of |
| 59 | // key that can be created by the deriveKeys operation. |
| 60 | derivedKeyTypes.forEach(function(derivedKeyType) { |
nothing calls this directly
no test coverage detected