mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Merge branch '914-simpler-secure-random' into 'master'
Remove Fortuna generator, fix Android SecureRandom bug Closes #914 See merge request !500
This commit is contained in:
@@ -45,7 +45,7 @@ public class EllipticCurveMultiplicationTest extends BrambleTestCase {
|
||||
byte[] seed = new byte[32];
|
||||
new SecureRandom().nextBytes(seed);
|
||||
// Montgomery ladder multiplier
|
||||
SecureRandom random = new FortunaSecureRandom(seed);
|
||||
SecureRandom random = new PseudoSecureRandom(seed);
|
||||
ECKeyGenerationParameters montgomeryGeneratorParams =
|
||||
new ECKeyGenerationParameters(PARAMETERS, random);
|
||||
ECKeyPairGenerator montgomeryGenerator = new ECKeyPairGenerator();
|
||||
@@ -63,7 +63,7 @@ public class EllipticCurveMultiplicationTest extends BrambleTestCase {
|
||||
ECPublicKeyParameters montgomeryPublic2 =
|
||||
(ECPublicKeyParameters) montgomeryKeyPair2.getPublic();
|
||||
// Default multiplier
|
||||
random = new FortunaSecureRandom(seed);
|
||||
random = new PseudoSecureRandom(seed);
|
||||
ECKeyGenerationParameters defaultGeneratorParams =
|
||||
new ECKeyGenerationParameters(defaultParameters, random);
|
||||
ECKeyPairGenerator defaultGenerator = new ECKeyPairGenerator();
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.junit.Test;
|
||||
import org.spongycastle.crypto.BlockCipher;
|
||||
import org.spongycastle.crypto.engines.AESLightEngine;
|
||||
import org.spongycastle.crypto.params.KeyParameter;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class FortunaGeneratorTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testCounterInitialisedToOne() {
|
||||
FortunaGenerator f = new FortunaGenerator(new byte[32]);
|
||||
// The counter is little-endian
|
||||
byte[] expected = new byte[16];
|
||||
expected[0] = 1;
|
||||
assertArrayEquals(expected, f.getCounter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncrementCounter() {
|
||||
FortunaGenerator f = new FortunaGenerator(new byte[32]);
|
||||
// Increment the counter until it reaches 255
|
||||
for (int i = 1; i < 255; i++) f.incrementCounter();
|
||||
byte[] expected = new byte[16];
|
||||
expected[0] = (byte) 255;
|
||||
assertArrayEquals(expected, f.getCounter());
|
||||
// Increment the counter again - it should carry into the next byte
|
||||
f.incrementCounter();
|
||||
expected[0] = 0;
|
||||
expected[1] = 1;
|
||||
assertArrayEquals(expected, f.getCounter());
|
||||
// Increment the counter until it carries into the next byte
|
||||
for (int i = 256; i < 65536; i++) f.incrementCounter();
|
||||
expected[0] = 0;
|
||||
expected[1] = 0;
|
||||
expected[2] = 1;
|
||||
assertArrayEquals(expected, f.getCounter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextBytes() {
|
||||
// Generate several outputs with the same seed - they should all match
|
||||
byte[] seed = new byte[32];
|
||||
byte[] out1 = new byte[48];
|
||||
new FortunaGenerator(seed).nextBytes(out1, 0, 48);
|
||||
// One byte longer than a block, with an offset of one
|
||||
byte[] out2 = new byte[49];
|
||||
new FortunaGenerator(seed).nextBytes(out2, 1, 48);
|
||||
for (int i = 0; i < 48; i++) assertEquals(out1[i], out2[i + 1]);
|
||||
// One byte shorter than a block
|
||||
byte[] out3 = new byte[47];
|
||||
new FortunaGenerator(seed).nextBytes(out3, 0, 47);
|
||||
for (int i = 0; i < 47; i++) assertEquals(out1[i], out3[i]);
|
||||
// Less than a block, with an offset greater than a block
|
||||
byte[] out4 = new byte[32];
|
||||
new FortunaGenerator(seed).nextBytes(out4, 17, 15);
|
||||
for (int i = 0; i < 15; i++) assertEquals(out1[i], out4[i + 17]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRekeying() {
|
||||
byte[] seed = new byte[32];
|
||||
FortunaGenerator f = new FortunaGenerator(seed);
|
||||
// Generate three blocks of output
|
||||
byte[] out1 = new byte[48];
|
||||
f.nextBytes(out1, 0, 48);
|
||||
// Create another generator with the same seed and generate one block
|
||||
f = new FortunaGenerator(seed);
|
||||
byte[] out2 = new byte[16];
|
||||
f.nextBytes(out2, 0, 16);
|
||||
// The generator should have rekeyed with the 2nd and 3rd blocks
|
||||
byte[] expectedKey = new byte[32];
|
||||
System.arraycopy(out1, 16, expectedKey, 0, 32);
|
||||
// The generator's counter should have been incremented 3 times
|
||||
byte[] expectedCounter = new byte[16];
|
||||
expectedCounter[0] = 4;
|
||||
// The next expected output block is the counter encrypted with the key
|
||||
byte[] expectedOutput = new byte[16];
|
||||
BlockCipher c = new AESLightEngine();
|
||||
c.init(true, new KeyParameter(expectedKey));
|
||||
c.processBlock(expectedCounter, 0, expectedOutput, 0);
|
||||
// Check that the generator produces the expected output block
|
||||
byte[] out3 = new byte[16];
|
||||
f.nextBytes(out3, 0, 16);
|
||||
assertArrayEquals(expectedOutput, out3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaximumRequestLength() {
|
||||
int expectedMax = 1024 * 1024;
|
||||
byte[] output = new byte[expectedMax + 123];
|
||||
FortunaGenerator f = new FortunaGenerator(new byte[32]);
|
||||
assertEquals(expectedMax, f.nextBytes(output, 0, output.length));
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.junit.Test;
|
||||
import org.spongycastle.crypto.BlockCipher;
|
||||
import org.spongycastle.crypto.digests.SHA256Digest;
|
||||
import org.spongycastle.crypto.engines.AESLightEngine;
|
||||
import org.spongycastle.crypto.params.KeyParameter;
|
||||
|
||||
import static org.briarproject.bramble.crypto.FortunaSecureRandom.SELF_TEST_VECTOR_1;
|
||||
import static org.briarproject.bramble.crypto.FortunaSecureRandom.SELF_TEST_VECTOR_2;
|
||||
import static org.briarproject.bramble.crypto.FortunaSecureRandom.SELF_TEST_VECTOR_3;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class FortunaSecureRandomTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testClassPassesSelfTest() {
|
||||
assertTrue(FortunaSecureRandom.selfTest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelfTestVectorsAreReproducible() {
|
||||
byte[] key = new byte[32], seed = new byte[32];
|
||||
byte[] counter = new byte[16], output = new byte[16];
|
||||
byte[] newKey = new byte[32];
|
||||
// Calculate the initial key
|
||||
DoubleDigest digest = new DoubleDigest(new SHA256Digest());
|
||||
digest.update(key);
|
||||
digest.update(seed);
|
||||
digest.digest(key, 0, 32);
|
||||
// Calculate the first output block and the new key
|
||||
BlockCipher c = new AESLightEngine();
|
||||
c.init(true, new KeyParameter(key));
|
||||
counter[0] = 1;
|
||||
c.processBlock(counter, 0, output, 0);
|
||||
counter[0] = 2;
|
||||
c.processBlock(counter, 0, newKey, 0);
|
||||
counter[0] = 3;
|
||||
c.processBlock(counter, 0, newKey, 16);
|
||||
System.arraycopy(newKey, 0, key, 0, 32);
|
||||
// The first self-test vector should match the first output block
|
||||
assertArrayEquals(SELF_TEST_VECTOR_1, output);
|
||||
// Calculate the second output block and the new key before reseeding
|
||||
c.init(true, new KeyParameter(key));
|
||||
counter[0] = 4;
|
||||
c.processBlock(counter, 0, output, 0);
|
||||
counter[0] = 5;
|
||||
c.processBlock(counter, 0, newKey, 0);
|
||||
counter[0] = 6;
|
||||
c.processBlock(counter, 0, newKey, 16);
|
||||
System.arraycopy(newKey, 0, key, 0, 32);
|
||||
// The second self-test vector should match the second output block
|
||||
assertArrayEquals(SELF_TEST_VECTOR_2, output);
|
||||
// Calculate the new key after reseeding
|
||||
digest.update(key);
|
||||
digest.update(seed);
|
||||
digest.digest(key, 0, 32);
|
||||
// Calculate the third output block
|
||||
c.init(true, new KeyParameter(key));
|
||||
counter[0] = 8;
|
||||
c.processBlock(counter, 0, output, 0);
|
||||
// The third self-test vector should match the third output block
|
||||
assertArrayEquals(SELF_TEST_VECTOR_3, output);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSeedProvider;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -21,7 +21,7 @@ public class HashTest extends BrambleTestCase {
|
||||
private final byte[] inputBytes2 = new byte[0];
|
||||
|
||||
public HashTest() {
|
||||
crypto = new CryptoComponentImpl(new TestSeedProvider());
|
||||
crypto = new CryptoComponentImpl(new TestSecureRandomProvider());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -3,9 +3,9 @@ package org.briarproject.bramble.crypto;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.system.SeedProvider;
|
||||
import org.briarproject.bramble.api.system.SecureRandomProvider;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSeedProvider;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
@@ -14,8 +14,9 @@ public class KeyAgreementTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testDeriveMasterSecret() throws Exception {
|
||||
SeedProvider seedProvider = new TestSeedProvider();
|
||||
CryptoComponent crypto = new CryptoComponentImpl(seedProvider);
|
||||
SecureRandomProvider
|
||||
secureRandomProvider = new TestSecureRandomProvider();
|
||||
CryptoComponent crypto = new CryptoComponentImpl(secureRandomProvider);
|
||||
KeyPair aPair = crypto.generateAgreementKeyPair();
|
||||
byte[] aPub = aPair.getPublic().getEncoded();
|
||||
KeyPair bPair = crypto.generateAgreementKeyPair();
|
||||
@@ -27,8 +28,9 @@ public class KeyAgreementTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testDeriveSharedSecret() throws Exception {
|
||||
SeedProvider seedProvider = new TestSeedProvider();
|
||||
CryptoComponent crypto = new CryptoComponentImpl(seedProvider);
|
||||
SecureRandomProvider
|
||||
secureRandomProvider = new TestSecureRandomProvider();
|
||||
CryptoComponent crypto = new CryptoComponentImpl(secureRandomProvider);
|
||||
KeyPair aPair = crypto.generateAgreementKeyPair();
|
||||
byte[] aPub = aPair.getPublic().getEncoded();
|
||||
KeyPair bPair = crypto.generateAgreementKeyPair();
|
||||
|
||||
@@ -1,20 +1,24 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.transport.TransportKeys;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSeedProvider;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class KeyDerivationTest extends BrambleTestCase {
|
||||
|
||||
@@ -23,7 +27,7 @@ public class KeyDerivationTest extends BrambleTestCase {
|
||||
private final SecretKey master;
|
||||
|
||||
public KeyDerivationTest() {
|
||||
crypto = new CryptoComponentImpl(new TestSeedProvider());
|
||||
crypto = new CryptoComponentImpl(new TestSecureRandomProvider());
|
||||
master = TestUtils.getSecretKey();
|
||||
}
|
||||
|
||||
@@ -156,11 +160,7 @@ public class KeyDerivationTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
private void assertAllDifferent(List<SecretKey> keys) {
|
||||
for (SecretKey ki : keys) {
|
||||
for (SecretKey kj : keys) {
|
||||
if (ki == kj) assertArrayEquals(ki.getBytes(), kj.getBytes());
|
||||
else assertFalse(Arrays.equals(ki.getBytes(), kj.getBytes()));
|
||||
}
|
||||
}
|
||||
Set<Bytes> set = new HashSet<Bytes>();
|
||||
for (SecretKey k : keys) assertTrue(set.add(new Bytes(k.getBytes())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.briarproject.bramble.api.crypto.KeyParser;
|
||||
import org.briarproject.bramble.api.crypto.PrivateKey;
|
||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSeedProvider;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -19,7 +19,7 @@ import static org.junit.Assert.assertTrue;
|
||||
public class KeyEncodingAndParsingTest extends BrambleTestCase {
|
||||
|
||||
private final CryptoComponentImpl crypto =
|
||||
new CryptoComponentImpl(new TestSeedProvider());
|
||||
new CryptoComponentImpl(new TestSecureRandomProvider());
|
||||
|
||||
@Test
|
||||
public void testAgreementPublicKeyLength() throws Exception {
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.briarproject.bramble.crypto;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSeedProvider;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -22,7 +22,7 @@ public class MacTest extends BrambleTestCase {
|
||||
private final byte[] inputBytes2 = new byte[0];
|
||||
|
||||
public MacTest() {
|
||||
crypto = new CryptoComponentImpl(new TestSeedProvider());
|
||||
crypto = new CryptoComponentImpl(new TestSecureRandomProvider());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSeedProvider;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -15,7 +15,7 @@ import static org.junit.Assert.assertTrue;
|
||||
public class PasswordBasedKdfTest extends BrambleTestCase {
|
||||
|
||||
private final CryptoComponentImpl crypto =
|
||||
new CryptoComponentImpl(new TestSeedProvider());
|
||||
new CryptoComponentImpl(new TestSecureRandomProvider());
|
||||
|
||||
@Test
|
||||
public void testEncryptionAndDecryption() {
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.PseudoRandom;
|
||||
|
||||
import java.security.Provider;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.SecureRandomSpi;
|
||||
|
||||
class PseudoSecureRandom extends SecureRandom {
|
||||
|
||||
private static final Provider PROVIDER = new PseudoSecureRandomProvider();
|
||||
|
||||
PseudoSecureRandom(byte[] seed) {
|
||||
super(new PseudoSecureRandomSpi(seed), PROVIDER);
|
||||
}
|
||||
|
||||
private static class PseudoSecureRandomSpi extends SecureRandomSpi {
|
||||
|
||||
private final PseudoRandom pseudoRandom;
|
||||
|
||||
private PseudoSecureRandomSpi(byte[] seed) {
|
||||
pseudoRandom = new PseudoRandomImpl(seed);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte[] engineGenerateSeed(int length) {
|
||||
return pseudoRandom.nextBytes(length);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void engineNextBytes(byte[] b) {
|
||||
byte[] random = pseudoRandom.nextBytes(b.length);
|
||||
System.arraycopy(random, 0, b, 0, b.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void engineSetSeed(byte[] seed) {
|
||||
// Thank you for your input
|
||||
}
|
||||
}
|
||||
|
||||
private static class PseudoSecureRandomProvider extends Provider {
|
||||
|
||||
private PseudoSecureRandomProvider() {
|
||||
super("PseudoSecureRandom", 1.0, "Only for testing");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package org.briarproject.bramble.crypto;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSeedProvider;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -22,7 +22,7 @@ public class SignatureTest extends BrambleTestCase {
|
||||
private final byte[] inputBytes = TestUtils.getRandomBytes(123);
|
||||
|
||||
public SignatureTest() {
|
||||
crypto = new CryptoComponentImpl(new TestSeedProvider());
|
||||
crypto = new CryptoComponentImpl(new TestSecureRandomProvider());
|
||||
KeyPair k = crypto.generateSignatureKeyPair();
|
||||
publicKey = k.getPublic().getEncoded();
|
||||
privateKey = k.getPrivate().getEncoded();
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.bramble.util.OsUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.security.Provider;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class LinuxSecureRandomProviderTest extends BrambleTestCase {
|
||||
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
testDir.mkdirs();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProviderWritesToRandomDeviceOnFirstCall()
|
||||
throws Exception {
|
||||
if (!(OsUtils.isLinux())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
// Redirect the provider's output to a file
|
||||
File urandom = new File(testDir, "urandom");
|
||||
urandom.delete();
|
||||
assertTrue(urandom.createNewFile());
|
||||
assertEquals(0, urandom.length());
|
||||
LinuxSecureRandomProvider p = new LinuxSecureRandomProvider(urandom);
|
||||
// Getting a provider should write entropy to the file
|
||||
Provider provider = p.getProvider();
|
||||
assertNotNull(provider);
|
||||
assertEquals("LinuxPRNG", provider.getName());
|
||||
// There should be at least 16 bytes from the clock, 8 from the runtime
|
||||
long length = urandom.length();
|
||||
assertTrue(length >= 24);
|
||||
// Getting another provider should not write to the file again
|
||||
provider = p.getProvider();
|
||||
assertNotNull(provider);
|
||||
assertEquals("LinuxPRNG", provider.getName());
|
||||
assertEquals(length, urandom.length());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
TestUtils.deleteTestDirectory(testDir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
import org.briarproject.bramble.util.OsUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class LinuxSecureRandomSpiTest extends BrambleTestCase {
|
||||
|
||||
private static final File RANDOM_DEVICE = new File("/dev/urandom");
|
||||
private static final int SEED_BYTES = 32;
|
||||
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
testDir.mkdirs();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeedsAreDistinct() {
|
||||
if (!(OsUtils.isLinux())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
Set<Bytes> seeds = new HashSet<Bytes>();
|
||||
LinuxSecureRandomSpi engine = new LinuxSecureRandomSpi();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
byte[] seed = engine.engineGenerateSeed(SEED_BYTES);
|
||||
assertEquals(SEED_BYTES, seed.length);
|
||||
assertTrue(seeds.add(new Bytes(seed)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEngineSetSeedWritesToRandomDevice() throws Exception {
|
||||
if (!(OsUtils.isLinux())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
// Redirect the engine's output to a file
|
||||
File urandom = new File(testDir, "urandom");
|
||||
urandom.delete();
|
||||
assertTrue(urandom.createNewFile());
|
||||
assertEquals(0, urandom.length());
|
||||
// Generate a seed
|
||||
byte[] seed = TestUtils.getRandomBytes(SEED_BYTES);
|
||||
// Check that the engine writes the seed to the file
|
||||
LinuxSecureRandomSpi engine = new LinuxSecureRandomSpi(RANDOM_DEVICE,
|
||||
urandom);
|
||||
engine.engineSetSeed(seed);
|
||||
assertEquals(SEED_BYTES, urandom.length());
|
||||
byte[] written = new byte[SEED_BYTES];
|
||||
FileInputStream in = new FileInputStream(urandom);
|
||||
IoUtils.read(in, written);
|
||||
in.close();
|
||||
assertArrayEquals(seed, written);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEngineNextBytesReadsFromRandomDevice() throws Exception {
|
||||
if (!(OsUtils.isLinux())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
// Generate some entropy
|
||||
byte[] entropy = TestUtils.getRandomBytes(SEED_BYTES);
|
||||
// Write the entropy to a file
|
||||
File urandom = new File(testDir, "urandom");
|
||||
urandom.delete();
|
||||
FileOutputStream out = new FileOutputStream(urandom);
|
||||
out.write(entropy);
|
||||
out.flush();
|
||||
out.close();
|
||||
assertTrue(urandom.exists());
|
||||
assertEquals(SEED_BYTES, urandom.length());
|
||||
// Check that the engine reads from the file
|
||||
LinuxSecureRandomSpi engine = new LinuxSecureRandomSpi(urandom,
|
||||
RANDOM_DEVICE);
|
||||
byte[] b = new byte[SEED_BYTES];
|
||||
engine.engineNextBytes(b);
|
||||
assertArrayEquals(entropy, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEngineGenerateSeedReadsFromRandomDevice() throws Exception {
|
||||
if (!(OsUtils.isLinux())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
// Generate some entropy
|
||||
byte[] entropy = TestUtils.getRandomBytes(SEED_BYTES);
|
||||
// Write the entropy to a file
|
||||
File urandom = new File(testDir, "urandom");
|
||||
urandom.delete();
|
||||
FileOutputStream out = new FileOutputStream(urandom);
|
||||
out.write(entropy);
|
||||
out.flush();
|
||||
out.close();
|
||||
assertTrue(urandom.exists());
|
||||
assertEquals(SEED_BYTES, urandom.length());
|
||||
// Check that the engine reads from the file
|
||||
LinuxSecureRandomSpi engine = new LinuxSecureRandomSpi(urandom,
|
||||
RANDOM_DEVICE);
|
||||
byte[] b = engine.engineGenerateSeed(SEED_BYTES);
|
||||
assertArrayEquals(entropy, b);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
TestUtils.deleteTestDirectory(testDir);
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import org.briarproject.bramble.api.Bytes;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.briarproject.bramble.util.OsUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.briarproject.bramble.api.system.SeedProvider.SEED_BYTES;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class LinuxSeedProviderTest extends BrambleTestCase {
|
||||
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
testDir.mkdirs();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeedAppearsSane() {
|
||||
if (!(OsUtils.isLinux())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
Set<Bytes> seeds = new HashSet<Bytes>();
|
||||
LinuxSeedProvider p = new LinuxSeedProvider();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
byte[] seed = p.getSeed();
|
||||
assertEquals(SEED_BYTES, seed.length);
|
||||
assertTrue(seeds.add(new Bytes(seed)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntropyIsWrittenToPool() throws Exception {
|
||||
if (!(OsUtils.isLinux())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
// Redirect the provider's entropy to a file
|
||||
File urandom = new File(testDir, "urandom");
|
||||
urandom.delete();
|
||||
assertTrue(urandom.createNewFile());
|
||||
assertEquals(0, urandom.length());
|
||||
String path = urandom.getAbsolutePath();
|
||||
LinuxSeedProvider p = new LinuxSeedProvider(path, "/dev/urandom");
|
||||
p.getSeed();
|
||||
// There should be 16 bytes from the clock, plus network interfaces
|
||||
assertTrue(urandom.length() > 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSeedIsReadFromPool() throws Exception {
|
||||
if (!(OsUtils.isLinux())) {
|
||||
System.err.println("WARNING: Skipping test, can't run on this OS");
|
||||
return;
|
||||
}
|
||||
// Generate a seed
|
||||
byte[] seed = TestUtils.getRandomBytes(SEED_BYTES);
|
||||
// Write the seed to a file
|
||||
File urandom = new File(testDir, "urandom");
|
||||
urandom.delete();
|
||||
FileOutputStream out = new FileOutputStream(urandom);
|
||||
out.write(seed);
|
||||
out.flush();
|
||||
out.close();
|
||||
assertTrue(urandom.exists());
|
||||
assertEquals(SEED_BYTES, urandom.length());
|
||||
// Check that the provider reads the seed from the file
|
||||
String path = urandom.getAbsolutePath();
|
||||
LinuxSeedProvider p = new LinuxSeedProvider("/dev/urandom", path);
|
||||
assertArrayEquals(seed, p.getSeed());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
TestUtils.deleteTestDirectory(testDir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.SecureRandomProvider;
|
||||
|
||||
import java.security.Provider;
|
||||
|
||||
@NotNullByDefault
|
||||
public class TestSecureRandomProvider implements SecureRandomProvider {
|
||||
|
||||
@Override
|
||||
public Provider getProvider() {
|
||||
// Use the default provider
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.SeedProvider;
|
||||
|
||||
@NotNullByDefault
|
||||
public class TestSeedProvider implements SeedProvider {
|
||||
|
||||
@Override
|
||||
public byte[] getSeed() {
|
||||
return TestUtils.getRandomBytes(32);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.system.SeedProvider;
|
||||
import org.briarproject.bramble.api.system.SecureRandomProvider;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@@ -12,7 +12,7 @@ public class TestSeedProviderModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
SeedProvider provideSeedProvider() {
|
||||
return new TestSeedProvider();
|
||||
SecureRandomProvider provideSeedProvider() {
|
||||
return new TestSecureRandomProvider();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user