Bug 1022343 - Use the hash function's block size as the default length when generating keys r=rbarnes
authorTim Taubert <ttaubert@mozilla.com>
Sun, 08 Jun 2014 13:46:54 +0200
changeset 190007 b33e1b8f211254050fb2575edd2fc51e4741d48e
parent 190006 bcea87ef8d6cf195b9bda84f90991fac6cba3457
child 190008 8b3b7b0e9f812d834cd445bd750e89ee077c401b
push id8288
push userryanvm@gmail.com
push dateMon, 23 Jun 2014 14:59:00 +0000
treeherderb2g-inbound@c65bf5a0595c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrbarnes
bugs1022343
milestone33.0a1
Bug 1022343 - Use the hash function's block size as the default length when generating keys r=rbarnes
dom/crypto/WebCryptoTask.cpp
dom/crypto/test/tests.js
--- a/dom/crypto/WebCryptoTask.cpp
+++ b/dom/crypto/WebCryptoTask.cpp
@@ -1164,21 +1164,27 @@ public:
         hashName.Assign(hashAlg.mName.Value());
       }
 
       if (params.mLength.WasPassed()) {
         mLength = params.mLength.Value();
       } else {
         KeyAlgorithm hashAlg(global, hashName);
         switch (hashAlg.Mechanism()) {
-          case CKM_SHA_1: mLength = 128; break;
-          case CKM_SHA256: mLength = 256; break;
-          case CKM_SHA384: mLength = 384; break;
-          case CKM_SHA512: mLength = 512; break;
-          default: mLength = 0; break;
+          case CKM_SHA_1:
+          case CKM_SHA256:
+            mLength = 512;
+            break;
+          case CKM_SHA384:
+          case CKM_SHA512:
+            mLength = 1024;
+            break;
+          default:
+            mLength = 0;
+            break;
         }
       }
 
       if (mLength == 0) {
         mEarlyRv = NS_ERROR_DOM_DATA_ERR;
         return;
       }
 
--- a/dom/crypto/test/tests.js
+++ b/dom/crypto/test/tests.js
@@ -327,17 +327,32 @@ TestArray.addTest(
 // -----------------------------------------------------------------------------
 TestArray.addTest(
   "Generate a 256-bit HMAC-SHA-256 key without specifying a key length",
   function() {
     var that = this;
     var alg = { name: "HMAC", hash: {name: "SHA-256"} };
     crypto.subtle.generateKey(alg, true, ["sign", "verify"]).then(
       complete(that, function(x) {
-        return hasKeyFields(x) && x.algorithm.length == 256;
+        return hasKeyFields(x) && x.algorithm.length == 512;
+      }),
+      error(that)
+    );
+  }
+);
+
+// -----------------------------------------------------------------------------
+TestArray.addTest(
+  "Generate a 256-bit HMAC-SHA-512 key without specifying a key length",
+  function() {
+    var that = this;
+    var alg = { name: "HMAC", hash: {name: "SHA-512"} };
+    crypto.subtle.generateKey(alg, true, ["sign", "verify"]).then(
+      complete(that, function(x) {
+        return hasKeyFields(x) && x.algorithm.length == 1024;
       }),
       error(that)
     );
   }
 );
 
 // -----------------------------------------------------------------------------
 TestArray.addTest(