PageRenderTime 35ms CodeModel.GetById 12ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

/services/sync/tests/unit/test_utils_hkdfExpand.js

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