-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If available, use Conscrypt generate randomness.
Also, we want to provide an additional internal function "validateUsesConscrypt" that lets us enforce to only use Conscrypt to generate randomness. For this, we move the "Random" class to internal, and add this function. The subtle class forwards calls to the internal class. PiperOrigin-RevId: 545581282 Change-Id: I7af7112154e4d9db204446568dd75880f921ec8c
- Loading branch information
1 parent
49f2aed
commit 6409bba
Showing
10 changed files
with
379 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.google.crypto.tink.internal; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.SecureRandom; | ||
|
||
/** Provides secure randomness using {@link SecureRandom}. */ | ||
public final class Random { | ||
private static final ThreadLocal<SecureRandom> localRandom = | ||
new ThreadLocal<SecureRandom>() { | ||
@Override | ||
protected SecureRandom initialValue() { | ||
return newDefaultSecureRandom(); | ||
} | ||
}; | ||
|
||
private static SecureRandom create() { | ||
// Use Conscrypt if possible. Conscrypt may have three different provider names. | ||
// For legacy compatibility reasons it uses the algorithm name "SHA1PRNG". | ||
try { | ||
return SecureRandom.getInstance("SHA1PRNG", "GmsCore_OpenSSL"); | ||
} catch (GeneralSecurityException e) { | ||
// ignore | ||
} | ||
try { | ||
return SecureRandom.getInstance("SHA1PRNG", "AndroidOpenSSL"); | ||
} catch (GeneralSecurityException e) { | ||
// ignore | ||
} | ||
try { | ||
return SecureRandom.getInstance("SHA1PRNG", "Conscrypt"); | ||
} catch (GeneralSecurityException e) { | ||
// ignore | ||
} | ||
return new SecureRandom(); | ||
} | ||
|
||
private static SecureRandom newDefaultSecureRandom() { | ||
SecureRandom retval = create(); | ||
retval.nextLong(); // force seeding | ||
return retval; | ||
} | ||
|
||
/** Returns a random byte array of size {@code size}. */ | ||
public static byte[] randBytes(int size) { | ||
byte[] rand = new byte[size]; | ||
localRandom.get().nextBytes(rand); | ||
return rand; | ||
} | ||
|
||
public static final int randInt(int max) { | ||
return localRandom.get().nextInt(max); | ||
} | ||
|
||
public static final int randInt() { | ||
return localRandom.get().nextInt(); | ||
} | ||
|
||
/** Throws a GeneralSecurityException if the provider is not Conscrypt. */ | ||
public static final void validateUsesConscrypt() throws GeneralSecurityException { | ||
String providerName = localRandom.get().getProvider().getName(); | ||
if (!providerName.equals("GmsCore_OpenSSL") | ||
&& !providerName.equals("AndroidOpenSSL") | ||
&& !providerName.equals("Conscrypt")) { | ||
throw new GeneralSecurityException( | ||
"Requires GmsCore_OpenSSL, AndroidOpenSSL or Conscrypt to generate randomness, but got " | ||
+ providerName); | ||
} | ||
} | ||
|
||
private Random() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/test/java/com/google/crypto/tink/internal/RandomTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.google.crypto.tink.internal; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.crypto.tink.testing.TestUtil; | ||
import java.security.Security; | ||
import java.util.ArrayList; | ||
import org.conscrypt.Conscrypt; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public final class RandomTest { | ||
|
||
static boolean conscryptProviderAdded; | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
try { | ||
Conscrypt.checkAvailability(); | ||
Security.addProvider(Conscrypt.newProvider()); | ||
conscryptProviderAdded = true; | ||
} catch (Exception | UnsatisfiedLinkError e) { | ||
conscryptProviderAdded = false; | ||
} | ||
} | ||
|
||
@Test | ||
public void validateUsesConscrypt_doesNotThrowIfConscryptProviderIsAdded() throws Exception { | ||
if (TestUtil.isAndroid()) { | ||
// Android uses Conscrypt by default, but trying to add it manually fails. | ||
assertThat(conscryptProviderAdded).isFalse(); | ||
} else { | ||
assertThat(conscryptProviderAdded).isTrue(); | ||
} | ||
Random.validateUsesConscrypt(); | ||
} | ||
|
||
@Test | ||
public void randBytes_works() throws Exception { | ||
assertThat(Random.randBytes(10)).hasLength(10); | ||
} | ||
|
||
@Test | ||
public void randIntWithMax_works() throws Exception { | ||
assertThat(Random.randInt(5)).isLessThan(5); | ||
} | ||
|
||
@Test | ||
public void randInt_works() throws Exception { | ||
int unused = Random.randInt(); | ||
} | ||
|
||
@Test | ||
public void randBytes_areDifferent() throws Exception { | ||
assertThat(Random.randBytes(32)).isNotEqualTo(Random.randBytes(32)); | ||
} | ||
|
||
@Test | ||
public void randomBytesInDifferentThreads_areDifferent() throws Exception { | ||
ArrayList<Thread> threads = new ArrayList<>(); | ||
final byte[] b0 = new byte[10]; | ||
final byte[] b1 = new byte[10]; | ||
threads.add( | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
System.arraycopy(Random.randBytes(10), 0, b0, 0, 10); | ||
} | ||
}); | ||
threads.add( | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
System.arraycopy(Random.randBytes(10), 0, b1, 0, 10); | ||
} | ||
}); | ||
for (Thread thread : threads) { | ||
thread.start(); | ||
} | ||
for (Thread thread : threads) { | ||
thread.join(); | ||
} | ||
assertThat(b0).isNotEqualTo(b1); | ||
} | ||
} |
Oops, something went wrong.