Delete SecureRandomInitializer
See https://android-developers.googleblog.com/2013/08/some-securerandom-thoughts.html Android JellyBean (4.3) required a workaround for SecureRandom initialization, which is no longer required as it was fixed in KitKat (4.4) and we no longer support JellyBean. Given we were previously initializing SecureRandom on a background thread, I did some brief investigation into performance. On my Pixel 3a: Creating SecureRandom takes 0.2-1ms Getting the first random number (which sets the seed) takes 0.03-0.1ms Further random numbers take ~7us Change-Id: I2a93d2607e86b488227feead5fdaaffa73ebe417 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2852318 Reviewed-by: Andrew Grieve <agrieve@chromium.org> Reviewed-by: Robert Sesek <rsesek@chromium.org> Reviewed-by: Joe Downing <joedow@chromium.org> Commit-Queue: Michael Thiessen <mthiesse@chromium.org> Cr-Commit-Position: refs/heads/master@{#876694}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
b824c0ba33
commit
aedbb74f94
base
chrome
android
java
src
org
chromium
chrome
browser
browser
android
crypto
java
src
org
chromium
chrome
browser
crypto
remoting/android/java/src/org/chromium/chromoting
@ -3849,7 +3849,6 @@ if (is_android) {
|
||||
"android/java/src/org/chromium/base/PowerMonitor.java",
|
||||
"android/java/src/org/chromium/base/Promise.java",
|
||||
"android/java/src/org/chromium/base/RadioUtils.java",
|
||||
"android/java/src/org/chromium/base/SecureRandomInitializer.java",
|
||||
"android/java/src/org/chromium/base/StreamUtil.java",
|
||||
"android/java/src/org/chromium/base/StrictModeContext.java",
|
||||
"android/java/src/org/chromium/base/SysUtils.java",
|
||||
|
@ -1,35 +0,0 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package org.chromium.base;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* This class contains code to initialize a SecureRandom generator securely on Android platforms
|
||||
* <= 4.3. See
|
||||
* {@link http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html}.
|
||||
*/
|
||||
// TODO(crbug.com/635567): Fix this properly.
|
||||
@SuppressLint("SecureRandom")
|
||||
public class SecureRandomInitializer {
|
||||
private static final int NUM_RANDOM_BYTES = 16;
|
||||
|
||||
/**
|
||||
* Safely initializes the random number generator, by seeding it with data from /dev/urandom.
|
||||
*/
|
||||
public static void initialize(SecureRandom generator) throws IOException {
|
||||
try (FileInputStream fis = new FileInputStream("/dev/urandom")) {
|
||||
byte[] seedBytes = new byte[NUM_RANDOM_BYTES];
|
||||
if (fis.read(seedBytes) != seedBytes.length) {
|
||||
throw new IOException("Failed to get enough random data.");
|
||||
}
|
||||
generator.setSeed(seedBytes);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,23 +4,16 @@
|
||||
|
||||
package org.chromium.chrome.browser.externalnav;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.chromium.base.IntentUtils;
|
||||
import org.chromium.base.Log;
|
||||
import org.chromium.base.SecureRandomInitializer;
|
||||
import org.chromium.base.task.AsyncTask;
|
||||
import org.chromium.base.task.BackgroundOnlyAsyncTask;
|
||||
import org.chromium.chrome.browser.IntentHandler;
|
||||
import org.chromium.url.Origin;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* This class generates a token for the most recently launched external intent that has
|
||||
@ -42,8 +35,7 @@ public class IntentWithRequestMetadataHandler {
|
||||
|
||||
private static final Object INSTANCE_LOCK = new Object();
|
||||
private static IntentWithRequestMetadataHandler sIntentWithRequestMetadataHandler;
|
||||
private SecureRandom mSecureRandom;
|
||||
private AsyncTask<SecureRandom> mSecureRandomInitializer;
|
||||
private SecureRandom mSecureRandom = new SecureRandom();
|
||||
private RequestMetadata mRequestMetadata;
|
||||
private byte[] mIntentToken;
|
||||
private String mUri;
|
||||
@ -89,25 +81,6 @@ public class IntentWithRequestMetadataHandler {
|
||||
return sIntentWithRequestMetadataHandler;
|
||||
}
|
||||
|
||||
private IntentWithRequestMetadataHandler() {
|
||||
mSecureRandomInitializer = new BackgroundOnlyAsyncTask<SecureRandom>() {
|
||||
// SecureRandomInitializer addresses the bug in SecureRandom that "TrulyRandom"
|
||||
// warns about, so this lint warning can safely be suppressed.
|
||||
@SuppressLint("TrulyRandom")
|
||||
@Override
|
||||
protected SecureRandom doInBackground() {
|
||||
SecureRandom secureRandom = null;
|
||||
try {
|
||||
secureRandom = new SecureRandom();
|
||||
SecureRandomInitializer.initialize(secureRandom);
|
||||
} catch (IOException ioe) {
|
||||
Log.e(TAG, "Cannot initialize SecureRandom", ioe);
|
||||
}
|
||||
return secureRandom;
|
||||
}
|
||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new token for the intent and put the token and request metadata in the
|
||||
* intent extra. This will invalidate the token on the previously launched intent with request
|
||||
@ -117,15 +90,6 @@ public class IntentWithRequestMetadataHandler {
|
||||
* @param metadata Request metadata to be put into the intent extra.
|
||||
*/
|
||||
public void onNewIntentWithRequestMetadata(Intent intent, RequestMetadata metadata) {
|
||||
if (mSecureRandomInitializer != null) {
|
||||
try {
|
||||
mSecureRandom = mSecureRandomInitializer.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
Log.e(TAG, "Error fetching SecureRandom", e);
|
||||
}
|
||||
mSecureRandomInitializer = null;
|
||||
}
|
||||
if (mSecureRandom == null) return;
|
||||
mIntentToken = new byte[32];
|
||||
mSecureRandom.nextBytes(mIntentToken);
|
||||
intent.putExtra(EXTRA_REQUEST_METADATA_TOKEN, mIntentToken);
|
||||
|
@ -4,13 +4,11 @@
|
||||
|
||||
package org.chromium.chrome.browser.webapps;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import org.chromium.base.ApiCompatibilityUtils;
|
||||
import org.chromium.base.ContextUtils;
|
||||
import org.chromium.base.SecureRandomInitializer;
|
||||
import org.chromium.base.StrictModeContext;
|
||||
|
||||
import java.io.File;
|
||||
@ -178,10 +176,7 @@ public class WebappAuthenticator {
|
||||
|
||||
/**
|
||||
* Generates the authentication encryption key in a background thread (if necessary).
|
||||
* SecureRandomInitializer addresses the bug in SecureRandom that "TrulyRandom" warns about, so
|
||||
* this lint warning can safely be suppressed.
|
||||
*/
|
||||
@SuppressLint("TrulyRandom")
|
||||
private static SecretKey generateMacKey() {
|
||||
if (sKey != null) {
|
||||
return sKey;
|
||||
@ -189,10 +184,9 @@ public class WebappAuthenticator {
|
||||
try {
|
||||
KeyGenerator generator = KeyGenerator.getInstance(MAC_ALGORITHM_NAME);
|
||||
SecureRandom random = new SecureRandom();
|
||||
SecureRandomInitializer.initialize(random);
|
||||
generator.init(MAC_KEY_BYTE_COUNT * 8, random);
|
||||
return generator.generateKey();
|
||||
} catch (NoSuchAlgorithmException | IOException e) {
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
package org.chromium.chrome.browser.crypto;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.AnyThread;
|
||||
@ -13,7 +12,6 @@ import androidx.annotation.VisibleForTesting;
|
||||
import org.chromium.base.ByteArrayGenerator;
|
||||
import org.chromium.base.Log;
|
||||
import org.chromium.base.ObserverList;
|
||||
import org.chromium.base.SecureRandomInitializer;
|
||||
import org.chromium.base.task.AsyncTask;
|
||||
import org.chromium.base.task.PostTask;
|
||||
import org.chromium.content_public.browser.UiThreadTaskTraits;
|
||||
@ -193,9 +191,6 @@ public class CipherFactory {
|
||||
*/
|
||||
private Callable<CipherData> createGeneratorCallable() {
|
||||
return new Callable<CipherData>() {
|
||||
// SecureRandomInitializer addresses the bug in SecureRandom that "TrulyRandom"
|
||||
// warns about, so this lint warning can safely be suppressed.
|
||||
@SuppressLint("TrulyRandom")
|
||||
@Override
|
||||
public CipherData call() {
|
||||
// Poll random data to generate initialization parameters for the Cipher.
|
||||
@ -212,14 +207,10 @@ public class CipherFactory {
|
||||
|
||||
try {
|
||||
SecureRandom random = new SecureRandom();
|
||||
SecureRandomInitializer.initialize(random);
|
||||
|
||||
KeyGenerator generator = KeyGenerator.getInstance("AES");
|
||||
generator.init(128, random);
|
||||
return new CipherData(generator.generateKey(), iv);
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Couldn't get generator data.");
|
||||
return null;
|
||||
} catch (GeneralSecurityException e) {
|
||||
Log.e(TAG, "Couldn't get generator instances.");
|
||||
return null;
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
package org.chromium.chromoting;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.ComponentName;
|
||||
@ -16,9 +15,7 @@ import android.util.Base64;
|
||||
|
||||
import org.chromium.base.ContextUtils;
|
||||
import org.chromium.base.Log;
|
||||
import org.chromium.base.SecureRandomInitializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -46,18 +43,12 @@ public class ThirdPartyTokenFetcher {
|
||||
private static final String RESPONSE_TYPE = "code token";
|
||||
|
||||
/** This is used to securely generate an opaque 128 bit for the |mState| variable. */
|
||||
@SuppressLint("TrulyRandom")
|
||||
private static SecureRandom sSecureRandom;
|
||||
|
||||
// TODO(lambroslambrou): Refactor this class to only initialize a PRNG when ThirdPartyAuth is
|
||||
// actually used.
|
||||
static {
|
||||
sSecureRandom = new SecureRandom();
|
||||
try {
|
||||
SecureRandomInitializer.initialize(sSecureRandom);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to initialize PRNG: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
/** This is used to launch the third party login page in the browser. */
|
||||
|
Reference in New Issue
Block a user