|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://services-common/utils.js"); |
|
5 Cu.import("resource://services-crypto/utils.js"); |
|
6 |
|
7 // Test vectors from RFC 5869 |
|
8 |
|
9 // Test case 1 |
|
10 |
|
11 let tc1 = { |
|
12 IKM: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", |
|
13 salt: "000102030405060708090a0b0c", |
|
14 info: "f0f1f2f3f4f5f6f7f8f9", |
|
15 L: 42, |
|
16 PRK: "077709362c2e32df0ddc3f0dc47bba63" + |
|
17 "90b6c73bb50f9c3122ec844ad7c2b3e5", |
|
18 OKM: "3cb25f25faacd57a90434f64d0362f2a" + |
|
19 "2d2d0a90cf1a5a4c5db02d56ecc4c5bf" + |
|
20 "34007208d5b887185865" |
|
21 }; |
|
22 |
|
23 // Test case 2 |
|
24 |
|
25 let tc2 = { |
|
26 IKM: "000102030405060708090a0b0c0d0e0f" + |
|
27 "101112131415161718191a1b1c1d1e1f" + |
|
28 "202122232425262728292a2b2c2d2e2f" + |
|
29 "303132333435363738393a3b3c3d3e3f" + |
|
30 "404142434445464748494a4b4c4d4e4f", |
|
31 salt: "606162636465666768696a6b6c6d6e6f" + |
|
32 "707172737475767778797a7b7c7d7e7f" + |
|
33 "808182838485868788898a8b8c8d8e8f" + |
|
34 "909192939495969798999a9b9c9d9e9f" + |
|
35 "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf", |
|
36 info: "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" + |
|
37 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" + |
|
38 "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" + |
|
39 "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" + |
|
40 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", |
|
41 L: 82, |
|
42 PRK: "06a6b88c5853361a06104c9ceb35b45c" + |
|
43 "ef760014904671014a193f40c15fc244", |
|
44 OKM: "b11e398dc80327a1c8e7f78c596a4934" + |
|
45 "4f012eda2d4efad8a050cc4c19afa97c" + |
|
46 "59045a99cac7827271cb41c65e590e09" + |
|
47 "da3275600c2f09b8367793a9aca3db71" + |
|
48 "cc30c58179ec3e87c14c01d5c1f3434f" + |
|
49 "1d87" |
|
50 }; |
|
51 |
|
52 // Test case 3 |
|
53 |
|
54 let tc3 = { |
|
55 IKM: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", |
|
56 salt: "", |
|
57 info: "", |
|
58 L: 42, |
|
59 PRK: "19ef24a32c717b167f33a91d6f648bdf" + |
|
60 "96596776afdb6377ac434c1c293ccb04", |
|
61 OKM: "8da4e775a563c18f715f802a063c5a31" + |
|
62 "b8a11f5c5ee1879ec3454e5f3c738d2d" + |
|
63 "9d201395faa4b61a96c8" |
|
64 }; |
|
65 |
|
66 function sha256HMAC(message, key) { |
|
67 let h = CryptoUtils.makeHMACHasher(Ci.nsICryptoHMAC.SHA256, key); |
|
68 return CryptoUtils.digestBytes(message, h); |
|
69 } |
|
70 |
|
71 function _hexToString(hex) { |
|
72 let ret = ""; |
|
73 if (hex.length % 2 != 0) { |
|
74 return false; |
|
75 } |
|
76 |
|
77 for (let i = 0; i < hex.length; i += 2) { |
|
78 let cur = hex[i] + hex[i + 1]; |
|
79 ret += String.fromCharCode(parseInt(cur, 16)); |
|
80 } |
|
81 return ret; |
|
82 } |
|
83 |
|
84 function extract_hex(salt, ikm) { |
|
85 salt = _hexToString(salt); |
|
86 ikm = _hexToString(ikm); |
|
87 return CommonUtils.bytesAsHex(sha256HMAC(ikm, CryptoUtils.makeHMACKey(salt))); |
|
88 } |
|
89 |
|
90 function expand_hex(prk, info, len) { |
|
91 prk = _hexToString(prk); |
|
92 info = _hexToString(info); |
|
93 return CommonUtils.bytesAsHex(CryptoUtils.hkdfExpand(prk, info, len)); |
|
94 } |
|
95 |
|
96 function hkdf_hex(ikm, salt, info, len) { |
|
97 ikm = _hexToString(ikm); |
|
98 if (salt) |
|
99 salt = _hexToString(salt); |
|
100 info = _hexToString(info); |
|
101 return CommonUtils.bytesAsHex(CryptoUtils.hkdf(ikm, salt, info, len)); |
|
102 } |
|
103 |
|
104 function run_test() { |
|
105 _("Verifying Test Case 1"); |
|
106 do_check_eq(extract_hex(tc1.salt, tc1.IKM), tc1.PRK); |
|
107 do_check_eq(expand_hex(tc1.PRK, tc1.info, tc1.L), tc1.OKM); |
|
108 do_check_eq(hkdf_hex(tc1.IKM, tc1.salt, tc1.info, tc1.L), tc1.OKM); |
|
109 |
|
110 _("Verifying Test Case 2"); |
|
111 do_check_eq(extract_hex(tc2.salt, tc2.IKM), tc2.PRK); |
|
112 do_check_eq(expand_hex(tc2.PRK, tc2.info, tc2.L), tc2.OKM); |
|
113 do_check_eq(hkdf_hex(tc2.IKM, tc2.salt, tc2.info, tc2.L), tc2.OKM); |
|
114 |
|
115 _("Verifying Test Case 3"); |
|
116 do_check_eq(extract_hex(tc3.salt, tc3.IKM), tc3.PRK); |
|
117 do_check_eq(expand_hex(tc3.PRK, tc3.info, tc3.L), tc3.OKM); |
|
118 do_check_eq(hkdf_hex(tc3.IKM, tc3.salt, tc3.info, tc3.L), tc3.OKM); |
|
119 do_check_eq(hkdf_hex(tc3.IKM, undefined, tc3.info, tc3.L), tc3.OKM); |
|
120 } |