Bug 1425462 Implement a very naive form of jitter (and not in the js engine either) draft
authorTom Ritter <tom@mozilla.com>
Fri, 26 Jan 2018 14:46:06 -0600
changeset 747818 bd2e965f95bffd3d8997823951482cf02ce52194
parent 747817 ad3a09e5553bc1daa18d007c1acb0bcf95917f26
child 747819 e1941f8cd39a62a6c8d9bae6e6763b9c0a607795
push id97017
push userbmo:tom@mozilla.com
push dateFri, 26 Jan 2018 22:16:13 +0000
bugs1425462
milestone60.0a1
Bug 1425462 Implement a very naive form of jitter (and not in the js engine either) MozReview-Commit-ID: CAdV3OktL9p
toolkit/components/resistfingerprinting/nsRFPService.cpp
--- a/toolkit/components/resistfingerprinting/nsRFPService.cpp
+++ b/toolkit/components/resistfingerprinting/nsRFPService.cpp
@@ -167,24 +167,38 @@ nsRFPService::ReduceTimePrecisionImpl(do
   // We think this is okay, and we codify it in some tests.
   double timeScaled = aTime * (1000000 / aTimeScale);
   //Cut off anything less than a microsecond.
   long long timeAsInt = llround(timeScaled);
   //Cast the resolution (in microseconds) to an int.
   long long resolutionAsInt = aResolutionUSec;
   // Perform the clamping.
   long long rounded = (timeAsInt / resolutionAsInt) * resolutionAsInt;
+
+  long jitterToAdd = 0;
+  double randomMultiplier = 0;
+  if (jitter && sJitterUSec > 0) {
+    // Get a random value between 0.0 and 1.0
+    if(!NS_FAILED(RandomDouble(&randomMultiplier))) {
+      // Add or Subtract up to the jitter value
+      jitterToAdd = (randomMultiplier * 2 * sJitterUSec) - sJitterUSec;
+    }
+  }
+
   // Cast it back to a double and reduce it to the correct units.
-  double ret = double(rounded) / (1000000.0 / aTimeScale);
+  double ret = double(rounded + jitterToAdd) / (1000000.0 / aTimeScale);
 
 #if defined(DEBUG)
-    MOZ_LOG(gResistFingerprintingLog, LogLevel::Verbose,
-      ("Given: (%.*f, Scaled: %.*f, Converted: %lli), Rounding with (%lli, Originally %.*f), Intermediate: (%lli), Got: (%lli Converted: %.*f)",
-      DBL_DIG-1, aTime, DBL_DIG-1, timeScaled, timeAsInt, resolutionAsInt, DBL_DIG-1, aResolutionUSec,
-      (timeAsInt / resolutionAsInt), rounded, DBL_DIG-1, ret));
+  uint32_t jitterLocal = sJitterUSec;
+  MOZ_LOG(gResistFingerprintingLog, LogLevel::Verbose,
+    ("Given: (%.*f, Scaled: %.*f, Converted: %lli), Rounding with (%lli, Originally %.*f), "
+      "Intermediate: (%lli), Got: (%lli) Jitter: (%s Max: %u Multipler: %f Value: %li) Final: (%lli Converted: %.*f)",
+    DBL_DIG-1, aTime, DBL_DIG-1, timeScaled, timeAsInt, resolutionAsInt, DBL_DIG-1, aResolutionUSec,
+    (timeAsInt / resolutionAsInt), rounded, (jitter && jitterLocal > 0 ? "On" : "Off"), jitterLocal, randomMultiplier,
+    jitterToAdd, (rounded + jitterToAdd), DBL_DIG-1, ret));
 #endif
 
   return ret;
 }
 
 /* static */
 double
 nsRFPService::ReduceTimePrecisionAsUSecs(double aTime, bool jitter /* = false */)