1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/crypto/tests/unit/test_utils_hkdfExpand.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,120 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://services-common/utils.js"); 1.8 +Cu.import("resource://services-crypto/utils.js"); 1.9 + 1.10 +// Test vectors from RFC 5869 1.11 + 1.12 +// Test case 1 1.13 + 1.14 +let tc1 = { 1.15 + IKM: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", 1.16 + salt: "000102030405060708090a0b0c", 1.17 + info: "f0f1f2f3f4f5f6f7f8f9", 1.18 + L: 42, 1.19 + PRK: "077709362c2e32df0ddc3f0dc47bba63" + 1.20 + "90b6c73bb50f9c3122ec844ad7c2b3e5", 1.21 + OKM: "3cb25f25faacd57a90434f64d0362f2a" + 1.22 + "2d2d0a90cf1a5a4c5db02d56ecc4c5bf" + 1.23 + "34007208d5b887185865" 1.24 +}; 1.25 + 1.26 +// Test case 2 1.27 + 1.28 +let tc2 = { 1.29 + IKM: "000102030405060708090a0b0c0d0e0f" + 1.30 + "101112131415161718191a1b1c1d1e1f" + 1.31 + "202122232425262728292a2b2c2d2e2f" + 1.32 + "303132333435363738393a3b3c3d3e3f" + 1.33 + "404142434445464748494a4b4c4d4e4f", 1.34 + salt: "606162636465666768696a6b6c6d6e6f" + 1.35 + "707172737475767778797a7b7c7d7e7f" + 1.36 + "808182838485868788898a8b8c8d8e8f" + 1.37 + "909192939495969798999a9b9c9d9e9f" + 1.38 + "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf", 1.39 + info: "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" + 1.40 + "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" + 1.41 + "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" + 1.42 + "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" + 1.43 + "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", 1.44 + L: 82, 1.45 + PRK: "06a6b88c5853361a06104c9ceb35b45c" + 1.46 + "ef760014904671014a193f40c15fc244", 1.47 + OKM: "b11e398dc80327a1c8e7f78c596a4934" + 1.48 + "4f012eda2d4efad8a050cc4c19afa97c" + 1.49 + "59045a99cac7827271cb41c65e590e09" + 1.50 + "da3275600c2f09b8367793a9aca3db71" + 1.51 + "cc30c58179ec3e87c14c01d5c1f3434f" + 1.52 + "1d87" 1.53 +}; 1.54 + 1.55 +// Test case 3 1.56 + 1.57 +let tc3 = { 1.58 + IKM: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", 1.59 + salt: "", 1.60 + info: "", 1.61 + L: 42, 1.62 + PRK: "19ef24a32c717b167f33a91d6f648bdf" + 1.63 + "96596776afdb6377ac434c1c293ccb04", 1.64 + OKM: "8da4e775a563c18f715f802a063c5a31" + 1.65 + "b8a11f5c5ee1879ec3454e5f3c738d2d" + 1.66 + "9d201395faa4b61a96c8" 1.67 +}; 1.68 + 1.69 +function sha256HMAC(message, key) { 1.70 + let h = CryptoUtils.makeHMACHasher(Ci.nsICryptoHMAC.SHA256, key); 1.71 + return CryptoUtils.digestBytes(message, h); 1.72 +} 1.73 + 1.74 +function _hexToString(hex) { 1.75 + let ret = ""; 1.76 + if (hex.length % 2 != 0) { 1.77 + return false; 1.78 + } 1.79 + 1.80 + for (let i = 0; i < hex.length; i += 2) { 1.81 + let cur = hex[i] + hex[i + 1]; 1.82 + ret += String.fromCharCode(parseInt(cur, 16)); 1.83 + } 1.84 + return ret; 1.85 +} 1.86 + 1.87 +function extract_hex(salt, ikm) { 1.88 + salt = _hexToString(salt); 1.89 + ikm = _hexToString(ikm); 1.90 + return CommonUtils.bytesAsHex(sha256HMAC(ikm, CryptoUtils.makeHMACKey(salt))); 1.91 +} 1.92 + 1.93 +function expand_hex(prk, info, len) { 1.94 + prk = _hexToString(prk); 1.95 + info = _hexToString(info); 1.96 + return CommonUtils.bytesAsHex(CryptoUtils.hkdfExpand(prk, info, len)); 1.97 +} 1.98 + 1.99 +function hkdf_hex(ikm, salt, info, len) { 1.100 + ikm = _hexToString(ikm); 1.101 + if (salt) 1.102 + salt = _hexToString(salt); 1.103 + info = _hexToString(info); 1.104 + return CommonUtils.bytesAsHex(CryptoUtils.hkdf(ikm, salt, info, len)); 1.105 +} 1.106 + 1.107 +function run_test() { 1.108 + _("Verifying Test Case 1"); 1.109 + do_check_eq(extract_hex(tc1.salt, tc1.IKM), tc1.PRK); 1.110 + do_check_eq(expand_hex(tc1.PRK, tc1.info, tc1.L), tc1.OKM); 1.111 + do_check_eq(hkdf_hex(tc1.IKM, tc1.salt, tc1.info, tc1.L), tc1.OKM); 1.112 + 1.113 + _("Verifying Test Case 2"); 1.114 + do_check_eq(extract_hex(tc2.salt, tc2.IKM), tc2.PRK); 1.115 + do_check_eq(expand_hex(tc2.PRK, tc2.info, tc2.L), tc2.OKM); 1.116 + do_check_eq(hkdf_hex(tc2.IKM, tc2.salt, tc2.info, tc2.L), tc2.OKM); 1.117 + 1.118 + _("Verifying Test Case 3"); 1.119 + do_check_eq(extract_hex(tc3.salt, tc3.IKM), tc3.PRK); 1.120 + do_check_eq(expand_hex(tc3.PRK, tc3.info, tc3.L), tc3.OKM); 1.121 + do_check_eq(hkdf_hex(tc3.IKM, tc3.salt, tc3.info, tc3.L), tc3.OKM); 1.122 + do_check_eq(hkdf_hex(tc3.IKM, undefined, tc3.info, tc3.L), tc3.OKM); 1.123 +}