Bug 1539578 - Add telemetry for DH use in WebCrypto API r=keeler
☠☠ backed out by bd03a5a96bde ☠ ☠
authorJ.C. Jones <jjones@mozilla.com>
Fri, 29 Mar 2019 15:55:54 +0000
changeset 466794 e356ebea641d36d5285b058b53b4ecc4e6eb3eff
parent 466793 8cde0ca01548efbce3d08620f0831acd6af26938
child 466795 7d8d318ea0627102c14b211ab487042563b122b8
push id35780
push useropoprus@mozilla.com
push dateFri, 29 Mar 2019 21:53:01 +0000
treeherdermozilla-central@414f37afbe07 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskeeler
bugs1539578
milestone68.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 1539578 - Add telemetry for DH use in WebCrypto API r=keeler Our WebCrypto implementation supports using DH as an algorithm in generateKey, which is not one of the recognized algorithms in the published specification [0]. We should seek to remove it from Firefox, but before we do, it'd be good to gather some telemetry on whether it's used at all, even in its' non-standard form. [0] https://www.w3.org/TR/WebCryptoAPI/#algorithm-overview Differential Revision: https://phabricator.services.mozilla.com/D25291
dom/crypto/WebCryptoTask.cpp
dom/crypto/moz.build
dom/crypto/test/browser/browser.ini
dom/crypto/test/browser/browser_WebCrypto_telemetry.js
dom/crypto/test/browser/head.js
--- a/dom/crypto/WebCryptoTask.cpp
+++ b/dom/crypto/WebCryptoTask.cpp
@@ -80,16 +80,17 @@ enum TelemetryAlgorithm {
   TA_SHA_384 = 17,
   TA_SHA_512 = 18,
   // Later additions
   TA_AES_KW = 19,
   TA_ECDH = 20,
   TA_PBKDF2 = 21,
   TA_ECDSA = 22,
   TA_HKDF = 23,
+  TA_DH = 24,
 };
 
 // Convenience functions for extracting / converting information
 
 // OOM-safe CryptoBuffer initialization, suitable for constructors
 #define ATTEMPT_BUFFER_INIT(dst, src)    \
   if (!dst.Assign(src)) {                \
     mEarlyRv = NS_ERROR_DOM_UNKNOWN_ERR; \
@@ -2878,16 +2879,17 @@ class DeriveDhBitsTask : public ReturnAr
       : mPrivKey(aKey.GetPrivateKey()) {
     mEarlyRv = GetKeyLengthForAlgorithm(aCx, aTargetAlgorithm, mLength);
     if (NS_SUCCEEDED(mEarlyRv)) {
       Init(aCx, aAlgorithm, aKey);
     }
   }
 
   void Init(JSContext* aCx, const ObjectOrString& aAlgorithm, CryptoKey& aKey) {
+    Telemetry::Accumulate(Telemetry::WEBCRYPTO_ALG, TA_DH);
     CHECK_KEY_ALGORITHM(aKey.Algorithm(), WEBCRYPTO_ALG_DH);
 
     // Check that we have a private key.
     if (!mPrivKey) {
       mEarlyRv = NS_ERROR_DOM_INVALID_ACCESS_ERR;
       return;
     }
 
--- a/dom/crypto/moz.build
+++ b/dom/crypto/moz.build
@@ -29,8 +29,9 @@ include('/ipc/chromium/chromium-config.m
 FINAL_LIBRARY = 'xul'
 
 LOCAL_INCLUDES += [
     '/security/manager/ssl',
     '/xpcom/build',
 ]
 
 MOCHITEST_MANIFESTS += ['test/mochitest.ini']
+BROWSER_CHROME_MANIFESTS += ['test/browser/browser.ini']
new file mode 100644
--- /dev/null
+++ b/dom/crypto/test/browser/browser.ini
@@ -0,0 +1,7 @@
+[DEFAULT]
+support-files =
+  head.js
+  ../test-vectors.js
+  ../util.js
+
+[browser_WebCrypto_telemetry.js]
new file mode 100644
--- /dev/null
+++ b/dom/crypto/test/browser/browser_WebCrypto_telemetry.js
@@ -0,0 +1,60 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/* global tv */
+
+const WEBCRYPTO_ALG_PROBE = "WEBCRYPTO_ALG";
+
+ChromeUtils.defineModuleGetter(this, "TelemetryTestUtils",
+  "resource://testing-common/TelemetryTestUtils.jsm");
+
+function validateHistogramEntryCount(aHistogramName, aExpectedTotalCount, aBucketCounts) {
+  let hist = Services.telemetry.getHistogramById(aHistogramName);
+  let resultIndexes = hist.snapshot();
+
+  let entriesSeen = Object.values(resultIndexes.values).reduce((a, b) => a + b, 0);
+
+  is(entriesSeen, aExpectedTotalCount, `Expecting ${aExpectedTotalCount} histogram entries in ` +
+     aHistogramName);
+
+  for (let bucket in aBucketCounts) {
+    is(resultIndexes.values[bucket], aBucketCounts[bucket], `Expecting bucket ${bucket} to be ` +
+      aBucketCounts[bucket]);
+  }
+}
+
+function cleanupTelemetry() {
+  Services.telemetry.clearScalars();
+  Services.telemetry.clearEvents();
+  Services.telemetry.getHistogramById(WEBCRYPTO_ALG_PROBE).clear();
+}
+
+add_task(async function ecdh_key() {
+  cleanupTelemetry();
+
+  var alg = { name: "ECDH", namedCurve: "P-256" };
+
+  let x = await crypto.subtle.generateKey(alg, false, ["deriveKey", "deriveBits"]);
+  await crypto.subtle.deriveBits({ name: "ECDH", public: x.publicKey }, x.privateKey, 128);
+
+  validateHistogramEntryCount(WEBCRYPTO_ALG_PROBE, 1, {20: 1});
+});
+
+
+add_task(async function dh_key() {
+  cleanupTelemetry();
+
+  var alg = {
+    name: "DH",
+    prime: tv.dh.prime,
+    generator: new Uint8Array([0x02]),
+  };
+
+  let x = await crypto.subtle.generateKey(alg, false, ["deriveKey", "deriveBits"]);
+  await crypto.subtle.deriveBits({ name: "DH", public: x.publicKey }, x.privateKey, 128);
+
+  validateHistogramEntryCount(WEBCRYPTO_ALG_PROBE, 1, {24: 1});
+});
new file mode 100644
--- /dev/null
+++ b/dom/crypto/test/browser/head.js
@@ -0,0 +1,18 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+let exports = this;
+
+const scripts = [
+  "util.js",
+  "test-vectors.js",
+];
+
+for (let script of scripts) {
+  Services.scriptloader.loadSubScript(
+    `chrome://mochitests/content/browser/dom/crypto/test/browser/${script}`,
+    this);
+}