zandobersek@gmail.com | 91e697a | 2017-05-02 06:37:07 +0000 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <script src="../../resources/js-test-pre.js"></script> |
| 5 | <script src="../resources/common.js"></script> |
| 6 | </head> |
| 7 | <body> |
| 8 | <p id="description"></p> |
| 9 | <div id="console"></div> |
| 10 | |
| 11 | <script type="text/javascript"> |
| 12 | description("Test HKDF deriveBits operation for corner-case length values"); |
| 13 | |
| 14 | jsTestIsAsync = true; |
| 15 | |
| 16 | var nonExtractable = false; |
| 17 | var rawKey = asciiToUint8Array("jnOw99oOZFLIEPMr"); |
| 18 | var info = asciiToUint8Array("jnOw99oO"); |
| 19 | var salt = asciiToUint8Array("jnOw99oO"); |
| 20 | |
| 21 | crypto.subtle.importKey("raw", rawKey, "HKDF", nonExtractable, ["deriveBits"]).then(function(result) { |
| 22 | baseKey = result; |
| 23 | |
| 24 | deriveBits = function(hash, length) { |
| 25 | return crypto.subtle.deriveBits({name: "HKDF", info: info, salt: salt, hash: hash}, baseKey, length); |
| 26 | }; |
| 27 | |
| 28 | // For each SHA algorithm and the corresponding HashLen length, we check that: |
| 29 | // - deriving with zero length rejects, |
| 30 | // - deriving with 8, HashLen * 8 and 255 * HashLen * 8 resolves, |
| 31 | // - deriving with 256 * HashLen * 8 rejects. |
| 32 | |
| 33 | return Promise.resolve().then(function(result) { |
| 34 | // SHA-1, hash output length is 20 bytes |
| 35 | return shouldReject('deriveBits("sha-1", 0)').then(function(result) { |
| 36 | return Promise.all([ |
| 37 | deriveBits("sha-1", 8), |
| 38 | deriveBits("sha-1", 20 * 8), |
| 39 | deriveBits("sha-1", 255 * 20 * 8) |
| 40 | ]).then(function(result) { |
| 41 | testPassed("Bit derivations for SHA-1 with minimum, maximum and HashLen lengths all passed"); |
| 42 | return shouldReject('deriveBits("sha-1", 256 * 20 * 8)'); |
| 43 | }); |
| 44 | }); |
| 45 | }).then(function(result) { |
| 46 | // SHA-224, hash output length is 28 bytes |
| 47 | return shouldReject('deriveBits("sha-224", 0)').then(function(result) { |
| 48 | return Promise.all([ |
| 49 | deriveBits("sha-224", 8), |
| 50 | deriveBits("sha-224", 28 * 8), |
| 51 | deriveBits("sha-224", 255 * 28 * 8) |
| 52 | ]).then(function(result) { |
| 53 | testPassed("Bit derivations for SHA-224 with minimum, maximum and HashLen lengths all passed"); |
| 54 | return shouldReject('deriveBits("sha-224", 256 * 28 * 8)'); |
| 55 | }); |
| 56 | }); |
| 57 | }).then(function(result) { |
| 58 | // SHA-256, hash output length is 32 bytes |
| 59 | return shouldReject('deriveBits("sha-256", 0)').then(function(result) { |
| 60 | return Promise.all([ |
| 61 | deriveBits("sha-256", 8), |
| 62 | deriveBits("sha-256", 32 * 8), |
| 63 | deriveBits("sha-256", 255 * 32 * 8) |
| 64 | ]).then(function(result) { |
| 65 | testPassed("Bit derivations for SHA-256 with minimum, maximum and HashLen lengths all passed"); |
| 66 | return shouldReject('deriveBits("sha-256", 256 * 32 * 8)'); |
| 67 | }); |
| 68 | }); |
| 69 | }).then(function(result) { |
| 70 | // SHA-384, hash output length is 48 bytes |
| 71 | return shouldReject('deriveBits("sha-384", 0)').then(function(result) { |
| 72 | return Promise.all([ |
| 73 | deriveBits("sha-384", 8), |
| 74 | deriveBits("sha-384", 48 * 8), |
| 75 | deriveBits("sha-384", 255 * 48 * 8) |
| 76 | ]).then(function(result) { |
| 77 | testPassed("Bit derivations for SHA-384 with minimum, maximum and HashLen lengths all passed"); |
| 78 | return shouldReject('deriveBits("sha-384", 256 * 48 * 8)'); |
| 79 | }); |
| 80 | }); |
| 81 | }).then(function(result) { |
| 82 | // SHA-512, hash output length is 64 bytes |
| 83 | return shouldReject('deriveBits("sha-512", 0)').then(function(result) { |
| 84 | return Promise.all([ |
| 85 | deriveBits("sha-512", 8), |
| 86 | deriveBits("sha-512", 64 * 8), |
| 87 | deriveBits("sha-512", 255 * 64 * 8) |
| 88 | ]).then(function(result) { |
| 89 | testPassed("Bit derivations for SHA-512 with minimum, maximum and HashLen lengths all passed"); |
| 90 | return shouldReject('deriveBits("sha-512", 256 * 64 * 8)'); |
| 91 | }); |
| 92 | }); |
| 93 | }); |
| 94 | }).then(finishJSTest, finishJSTest); |
| 95 | |
| 96 | </script> |
| 97 | |
| 98 | <script src="../../resources/js-test-post.js"></script> |
| 99 | </body> |
| 100 | </html> |