Bug 681864 - Get rid of unused Utils.sha* helpers. r=rnewman
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Fri, 26 Aug 2011 10:27:29 -0700
changeset 77613 52ee9fe9c3e9b290878c817dffe437435c1ccf78
parent 77612 5c5fa8d8009e64f3b41d178f27b6864bc29466e9
child 77614 459aa245e7f052196e60e935edf35c23b98b6b87
push id78
push userclegnitto@mozilla.com
push dateFri, 16 Dec 2011 17:32:24 +0000
treeherdermozilla-release@79d24e644fdd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs681864
milestone9.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 681864 - Get rid of unused Utils.sha* helpers. r=rnewman
services/sync/modules/util.js
services/sync/tests/unit/head_helpers.js
services/sync/tests/unit/test_keys.js
services/sync/tests/unit/test_utils_hkdfExpand.js
services/sync/tests/unit/test_utils_sha1hmac.js
services/sync/tests/unit/test_utils_sha256HMAC.js
services/sync/tests/unit/xpcshell.ini
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -405,50 +405,31 @@ let Utils = {
   bytesAsHex: function bytesAsHex(bytes) {
     let hex = "";
     for (let i = 0; i < bytes.length; i++) {
       hex += ("0" + bytes[i].charCodeAt().toString(16)).slice(-2);
     }
     return hex;
   },
 
-  _sha256: function _sha256(message) {
-    let hasher = Cc["@mozilla.org/security/hash;1"].
-      createInstance(Ci.nsICryptoHash);
-    hasher.init(hasher.SHA256);
-    return Utils.digestUTF8(message, hasher);
-  },
-
-  sha256: function sha256(message) {
-    return Utils.bytesAsHex(Utils._sha256(message));
-  },
-
-  sha256Base64: function (message) {
-    return btoa(Utils._sha256(message));
-  },
-
   _sha1: function _sha1(message) {
     let hasher = Cc["@mozilla.org/security/hash;1"].
       createInstance(Ci.nsICryptoHash);
     hasher.init(hasher.SHA1);
     return Utils.digestUTF8(message, hasher);
   },
 
   sha1: function sha1(message) {
     return Utils.bytesAsHex(Utils._sha1(message));
   },
 
   sha1Base32: function sha1Base32(message) {
     return Utils.encodeBase32(Utils._sha1(message));
   },
   
-  sha1Base64: function (message) {
-    return btoa(Utils._sha1(message));
-  },
-
   /**
    * Produce an HMAC key object from a key string.
    */
   makeHMACKey: function makeHMACKey(str) {
     return Svc.KeyFactory.keyFromString(Ci.nsIKeyObject.HMAC, str);
   },
     
   /**
@@ -457,36 +438,16 @@ let Utils = {
   makeHMACHasher: function makeHMACHasher(type, key) {
     let hasher = Cc["@mozilla.org/security/hmac;1"]
                    .createInstance(Ci.nsICryptoHMAC);
     hasher.init(type, key);
     return hasher;
   },
 
   /**
-   * Some HMAC convenience functions for tests and backwards compatibility:
-   * 
-   *   sha1HMACBytes: hashes byte string, returns bytes string
-   *   sha256HMAC: hashes UTF-8 encoded string, returns hex string
-   *   sha256HMACBytes: hashes byte string, returns bytes string
-   */
-  sha1HMACBytes: function sha1HMACBytes(message, key) {
-    let h = Utils.makeHMACHasher(Ci.nsICryptoHMAC.SHA1, key);
-    return Utils.digestBytes(message, h);
-  },
-  sha256HMAC: function sha256HMAC(message, key) {
-    let h = Utils.makeHMACHasher(Ci.nsICryptoHMAC.SHA256, key);
-    return Utils.bytesAsHex(Utils.digestUTF8(message, h));
-  },
-  sha256HMACBytes: function sha256HMACBytes(message, key) {
-    let h = Utils.makeHMACHasher(Ci.nsICryptoHMAC.SHA256, key);
-    return Utils.digestBytes(message, h);
-  },
-
-  /**
    * HMAC-based Key Derivation Step 2 according to RFC 5869.
    */
   hkdfExpand: function hkdfExpand(prk, info, len) {
     const BLOCKSIZE = 256 / 8;
     let h = Utils.makeHMACHasher(Ci.nsICryptoHMAC.SHA256,
                                  Utils.makeHMACKey(prk));
     let T = "";
     let Tn = "";
--- a/services/sync/tests/unit/head_helpers.js
+++ b/services/sync/tests/unit/head_helpers.js
@@ -77,43 +77,40 @@ function FakeGUIDService() {
   let latestGUID = 0;
 
   Utils.makeGUID = function fake_makeGUID() {
     return "fake-guid-" + latestGUID++;
   };
 }
 
 
+function fakeSHA256HMAC(message) {
+   message = message.substr(0, 64);
+   while (message.length < 64) {
+     message += " ";
+   }
+   return message;
+}
+
 /*
  * Mock implementation of WeaveCrypto. It does not encrypt or
  * decrypt, merely returning the input verbatim.
  */
 function FakeCryptoService() {
   this.counter = 0;
 
   delete Svc.Crypto;  // get rid of the getter first
   Svc.Crypto = this;
-  Utils.sha256HMAC = this.sha256HMAC;
 
-  CryptoWrapper.prototype.ciphertextHMAC = this.ciphertextHMAC;
+  CryptoWrapper.prototype.ciphertextHMAC = function ciphertextHMAC(keyBundle) {
+    return fakeSHA256HMAC(this.ciphertext);
+  };
 }
 FakeCryptoService.prototype = {
 
-  sha256HMAC: function Utils_sha256HMAC(message, hasher) {
-     message = message.substr(0, 64);
-     while (message.length < 64) {
-       message += " ";
-     }
-     return message;
-  },
-
-  ciphertextHMAC: function CryptoWrapper_ciphertextHMAC(keyBundle) {
-    return Utils.sha256HMAC(this.ciphertext);
-  },
-
   encrypt: function(aClearText, aSymmetricKey, aIV) {
     return aClearText;
   },
 
   decrypt: function(aCipherText, aSymmetricKey, aIV) {
     return aCipherText;
   },
 
@@ -182,25 +179,25 @@ let _ = function(some, debug, text, to) 
 _("Setting the identity for passphrase");
 Cu.import("resource://services-sync/identity.js");
 
 
 /*
  * Test setup helpers.
  */
 
-// Turn WBO cleartext into "encrypted" payload as it goes over the wire
+// Turn WBO cleartext into fake "encrypted" payload as it goes over the wire.
 function encryptPayload(cleartext) {
   if (typeof cleartext == "object") {
     cleartext = JSON.stringify(cleartext);
   }
 
   return {ciphertext: cleartext, // ciphertext == cleartext with fake crypto
           IV: "irrelevant",
-          hmac: Utils.sha256HMAC(cleartext, Utils.makeHMACKey(""))};
+          hmac: fakeSHA256HMAC(cleartext, Utils.makeHMACKey(""))};
 }
 
 function generateNewKeys(collections) {
   let wbo = CollectionKeys.generateNewKeysWBO(collections);
   let modified = new_timestamp();
   CollectionKeys.setContents(wbo.cleartext, modified);
 }
 
--- a/services/sync/tests/unit/test_keys.js
+++ b/services/sync/tests/unit/test_keys.js
@@ -1,35 +1,40 @@
 var btoa;
 
 Cu.import("resource://services-sync/record.js");
 Cu.import("resource://services-sync/constants.js");
 btoa = Cu.import("resource://services-sync/util.js").btoa;
 
+function sha256HMAC(message, key) {
+  let h = Utils.makeHMACHasher(Ci.nsICryptoHMAC.SHA256, key);
+  return Utils.digestBytes(message, h);
+}
+
 function test_time_keyFromString(iterations) {
   let k;
   let o;
   let b = new BulkKeyBundle();
   let d = Utils.decodeKeyBase32("ababcdefabcdefabcdefabcdef");
   b.generateRandom();
   
-  _("Running " + iterations + " iterations of hmacKeyObject + sha256HMACBytes.");
+  _("Running " + iterations + " iterations of hmacKeyObject + sha256HMAC.");
   for (let i = 0; i < iterations; ++i) {
     let k = b.hmacKeyObject;
-    o = Utils.sha256HMACBytes(d, k);
+    o = sha256HMAC(d, k);
   }
   do_check_true(!!o);
   _("Done.");
 }
 
 function test_repeated_hmac() {
   let testKey = "ababcdefabcdefabcdefabcdef";
   let k = Utils.makeHMACKey("foo");
-  let one = Utils.sha256HMACBytes(Utils.decodeKeyBase32(testKey), k);
-  let two = Utils.sha256HMACBytes(Utils.decodeKeyBase32(testKey), k);
+  let one = sha256HMAC(Utils.decodeKeyBase32(testKey), k);
+  let two = sha256HMAC(Utils.decodeKeyBase32(testKey), k);
   do_check_eq(one, two);
 }
 
 function do_check_array_eq(a1, a2) {
   do_check_eq(a1.length, a2.length);
   for (let i = 0; i < a1.length; ++i) {
     do_check_eq(a1[i], a2[i]);
   }
@@ -40,20 +45,20 @@ function test_keymanager() {
   
   let username = "john@example.com";
   
   // Decode the key here to mirror what generateEntry will do,
   // but pass it encoded into the KeyBundle call below.
   
   let sha256inputE = "" + HMAC_INPUT + username + "\x01";
   let key = Utils.makeHMACKey(Utils.decodeKeyBase32(testKey));
-  let encryptKey = Utils.sha256HMACBytes(sha256inputE, key);
+  let encryptKey = sha256HMAC(sha256inputE, key);
   
   let sha256inputH = encryptKey + HMAC_INPUT + username + "\x02";
-  let hmacKey = Utils.sha256HMACBytes(sha256inputH, key);
+  let hmacKey = sha256HMAC(sha256inputH, key);
   
   // Encryption key is stored in base64 for WeaveCrypto convenience.
   do_check_eq(btoa(encryptKey), new SyncKeyBundle(null, username, testKey).encryptionKey);
   do_check_eq(hmacKey,          new SyncKeyBundle(null, username, testKey).hmacKey);
   
   // Test with the same KeyBundle for both.
   let obj = new SyncKeyBundle(null, username, testKey);
   do_check_eq(hmacKey, obj.hmacKey);
--- a/services/sync/tests/unit/test_utils_hkdfExpand.js
+++ b/services/sync/tests/unit/test_utils_hkdfExpand.js
@@ -54,34 +54,38 @@ let tc3 = {
    L:    42,
    PRK:  "19ef24a32c717b167f33a91d6f648bdf" +
          "96596776afdb6377ac434c1c293ccb04",
    OKM:  "8da4e775a563c18f715f802a063c5a31" +
          "b8a11f5c5ee1879ec3454e5f3c738d2d" +
          "9d201395faa4b61a96c8"
 };
 
+function sha256HMAC(message, key) {
+  let h = Utils.makeHMACHasher(Ci.nsICryptoHMAC.SHA256, key);
+  return Utils.digestBytes(message, h);
+}
+
 function _hexToString(hex) {
   let ret = "";
   if (hex.length % 2 != 0) {
     return false;
   }
 
   for (let i = 0; i < hex.length; i += 2) {
     let cur = hex[i] + hex[i + 1];
     ret += String.fromCharCode(parseInt(cur, 16));
   }
   return ret;
 }
 
 function extract_hex(salt, ikm) {
   salt = _hexToString(salt);
   ikm = _hexToString(ikm);
-  return Utils.bytesAsHex(
-    Utils.sha256HMACBytes(ikm, Utils.makeHMACKey(salt)));
+  return Utils.bytesAsHex(sha256HMAC(ikm, Utils.makeHMACKey(salt)));
 }
 
 function expand_hex(prk, info, len) {
   prk = _hexToString(prk);
   info = _hexToString(info);
   return Utils.bytesAsHex(Utils.hkdfExpand(prk, info, len));
 }
 
deleted file mode 100644
--- a/services/sync/tests/unit/test_utils_sha1hmac.js
+++ /dev/null
@@ -1,61 +0,0 @@
-Cu.import("resource://services-sync/util.js");
-
-function _hexToString(hex) {
-  var ret = '';
-  if (hex.length % 2 != 0) {
-    return false;
-  }
-
-  for (var i = 0; i < hex.length; i += 2) {
-    var cur = hex[i] + hex[i + 1];
-    ret += String.fromCharCode(parseInt(cur, 16));
-  }
-  return ret;
-}
-
-function run_test() {
-  let test_data = 
-   [{test_case:     1,
-     key:           _hexToString("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
-     key_len:       20,
-     data:          "Hi There",
-     data_len:      8,
-     digest:        "b617318655057264e28bc0b6fb378c8ef146be00"},
-
-    {test_case:     2,
-     key:           "Jefe",
-     key_len:       4,
-     data:          "what do ya want for nothing?",
-     data_len:      28,
-     digest:        "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"}];
-  
-  let d;
-  // Testing repeated hashing.
-  d = test_data[0];
-  do_check_eq(Utils.bytesAsHex(Utils.sha1HMACBytes(d.data, Utils.makeHMACKey(d.key))), d.digest);
-  d = test_data[1];
-  do_check_eq(Utils.bytesAsHex(Utils.sha1HMACBytes(d.data, Utils.makeHMACKey(d.key))), d.digest);
-  d = test_data[0];
-  do_check_eq(Utils.bytesAsHex(Utils.sha1HMACBytes(d.data, Utils.makeHMACKey(d.key))), d.digest);
-  d = test_data[1];
-  do_check_eq(Utils.bytesAsHex(Utils.sha1HMACBytes(d.data, Utils.makeHMACKey(d.key))), d.digest);
-  let kk = Utils.makeHMACKey(d.key);
-  do_check_eq(
-      Utils.bytesAsHex(
-        Utils.sha1HMACBytes(
-          Utils.sha1HMACBytes(
-            Utils.sha1HMACBytes(d.data, kk),
-            kk),
-          kk)),
-      Utils.bytesAsHex(
-        Utils.sha1HMACBytes(
-          Utils.sha1HMACBytes(
-            Utils.sha1HMACBytes(d.data, kk),
-            kk),
-          kk)));
-  
-  d = test_data[0];
-  kk = Utils.makeHMACKey(d.key);
-  do_check_eq(Utils.bytesAsHex(Utils.sha1HMACBytes(d.data, kk)), d.digest);
-  do_check_eq(Utils.bytesAsHex(Utils.sha1HMACBytes(d.data, kk)), d.digest);
-}
deleted file mode 100644
--- a/services/sync/tests/unit/test_utils_sha256HMAC.js
+++ /dev/null
@@ -1,52 +0,0 @@
-_("Make sure sha256 hmac works with various messages and keys");
-Cu.import("resource://services-sync/util.js");
-
-function run_test() {
-  let key1 = Utils.makeHMACKey("key1");
-  let key2 = Utils.makeHMACKey("key2");
-
-  let mes1 = "message 1";
-  let mes2 = "message 2";
-
-  _("Make sure right sha256 hmacs are generated");
-  let hmac11 = Utils.sha256HMAC(mes1, key1);
-  do_check_eq(hmac11, "54f035bfbd6b44445d771c7c430e0753f1c00823974108fb4723703782552296");
-  let hmac12 = Utils.sha256HMAC(mes1, key2);
-  do_check_eq(hmac12, "1dbeae48de1b12f69517d828fb32969c74c8adc0715babc41b8f50254a980e70");
-  let hmac21 = Utils.sha256HMAC(mes2, key1);
-  do_check_eq(hmac21, "e00e91db4e86973868de8b3e818f4c968894d4135a3209bfea7b9e699484f07a");
-  let hmac22 = Utils.sha256HMAC(mes2, key2);
-  do_check_eq(hmac22, "4624312da31ada485b87beeecef0e5f0311cd5de60ea12291ce34cab158e0cc7");
-
-  _("Repeated hmacs shouldn't change the digest");
-  do_check_eq(Utils.sha256HMAC(mes1, key1), hmac11);
-  do_check_eq(Utils.sha256HMAC(mes1, key2), hmac12);
-  do_check_eq(Utils.sha256HMAC(mes2, key1), hmac21);
-  do_check_eq(Utils.sha256HMAC(mes2, key2), hmac22);
-  
-  // RFC tests for SHA256-HMAC.
-  let k1 = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
-  let d1 = "\x48\x69\x20\x54\x68\x65\x72\x65";
-  let o1 = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7";
-  
-  // Sample data for Marcus Wohlschon.
-  let k2 = "78xR6gnAnBja7nYl6vkECo5bTlax5iHelP/jenj+dhQ=";
-  let d2 = "4F5T+hK0VFBx880sYlMtfkBD2ZGe337tgbbQRTFxndFEgCC1fRrJkvzZ6Wytr+DySw3rxJ05O4Lqfn9F8Kxlvc4pcnAX//TK6MvRLs1NmcZr6HTo3NPurNB1VRTnJCE6";
-  let o2 = "3dd17eb5091e0f2400a733f9e2cf8264d59206b6351078c2ce88499f1971f9b0";
-    
-  do_check_eq(Utils.sha256HMACBytes(d1, Utils.makeHMACKey(k1)), o1);
-  do_check_eq(Utils.sha256HMAC(d2, Utils.makeHMACKey(k2)), o2);
-  
-  // Checking HMAC exceptions.
-  let ex;
-  try {
-    Utils.throwHMACMismatch("aaa", "bbb");
-  }
-  catch (e) {
-    ex = e;
-  }
-  do_check_true(Utils.isHMACMismatch(ex));
-  do_check_false(!!Utils.isHMACMismatch(new Error()));
-  do_check_false(!!Utils.isHMACMismatch(null));
-  do_check_false(!!Utils.isHMACMismatch("Error"));
-}
--- a/services/sync/tests/unit/xpcshell.ini
+++ b/services/sync/tests/unit/xpcshell.ini
@@ -117,12 +117,10 @@ skip-if = (os == "mac" && debug) || os =
 [test_utils_lock.js]
 [test_utils_makeGUID.js]
 [test_utils_makeURI.js]
 [test_utils_namedTimer.js]
 [test_utils_notify.js]
 [test_utils_passphrase.js]
 [test_utils_pbkdf2.js]
 [test_utils_sha1.js]
-[test_utils_sha1hmac.js]
-[test_utils_sha256HMAC.js]
 [test_utils_stackTrace.js]
 [test_utils_utf8.js]