mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Use a constant for the tag size.
This commit is contained in:
@@ -14,22 +14,22 @@ public class SharedSecretTest extends TestCase {
|
||||
Random random = new Random();
|
||||
byte[] secret = new byte[40];
|
||||
random.nextBytes(secret);
|
||||
secret[16] = (byte) 0;
|
||||
secret[SharedSecret.IV_BYTES] = (byte) 0;
|
||||
SharedSecret s = new SharedSecret(secret);
|
||||
assertTrue(Arrays.equals(secret, s.getBytes()));
|
||||
secret[16] = (byte) 1;
|
||||
secret[SharedSecret.IV_BYTES] = (byte) 1;
|
||||
s = new SharedSecret(secret);
|
||||
assertTrue(Arrays.equals(secret, s.getBytes()));
|
||||
// The Alice flag must be either 0 or 1
|
||||
secret[16] = (byte) 2;
|
||||
secret[SharedSecret.IV_BYTES] = (byte) 2;
|
||||
try {
|
||||
s = new SharedSecret(secret);
|
||||
fail();
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
// The secret must be at least 18 bytes long
|
||||
secret = new byte[17];
|
||||
// The ciphertext must be at least 1 byte long
|
||||
secret = new byte[SharedSecret.IV_BYTES + 1];
|
||||
random.nextBytes(secret);
|
||||
secret[16] = (byte) 0;
|
||||
secret[SharedSecret.IV_BYTES] = (byte) 0;
|
||||
try {
|
||||
s = new SharedSecret(secret);
|
||||
fail();
|
||||
|
||||
@@ -53,7 +53,7 @@ public class ConnectionRecogniserImplTest extends TestCase {
|
||||
}});
|
||||
final ConnectionRecogniserImpl c =
|
||||
new ConnectionRecogniserImpl(transportId, crypto, db);
|
||||
assertNull(c.acceptConnection(new byte[16]));
|
||||
assertNull(c.acceptConnection(new byte[Constants.TAG_BYTES]));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class PacketEncrypterImplTest extends TestCase {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
PacketEncrypter p = new PacketEncrypterImpl(out, tagCipher,
|
||||
packetCipher, tagKey, packetKey);
|
||||
p.writeTag(new byte[16]);
|
||||
p.writeTag(new byte[Constants.TAG_BYTES]);
|
||||
p.getOutputStream().write((byte) 0);
|
||||
p.finishPacket();
|
||||
assertEquals(17, out.toByteArray().length);
|
||||
@@ -44,7 +44,7 @@ public class PacketEncrypterImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testEncryption() throws Exception {
|
||||
byte[] tag = new byte[16];
|
||||
byte[] tag = new byte[Constants.TAG_BYTES];
|
||||
byte[] packet = new byte[123];
|
||||
// Calculate the expected encrypted tag
|
||||
tagCipher.init(Cipher.ENCRYPT_MODE, tagKey);
|
||||
@@ -63,14 +63,15 @@ public class PacketEncrypterImplTest extends TestCase {
|
||||
p.getOutputStream().write(packet);
|
||||
p.finishPacket();
|
||||
byte[] ciphertext = out.toByteArray();
|
||||
assertEquals(16 + packet.length, ciphertext.length);
|
||||
assertEquals(Constants.TAG_BYTES + packet.length, ciphertext.length);
|
||||
// Check the tag
|
||||
byte[] actualTag = new byte[16];
|
||||
System.arraycopy(ciphertext, 0, actualTag, 0, 16);
|
||||
byte[] actualTag = new byte[Constants.TAG_BYTES];
|
||||
System.arraycopy(ciphertext, 0, actualTag, 0, Constants.TAG_BYTES);
|
||||
assertTrue(Arrays.equals(expectedTag, actualTag));
|
||||
// Check the packet
|
||||
byte[] actualPacket = new byte[packet.length];
|
||||
System.arraycopy(ciphertext, 16, actualPacket, 0, actualPacket.length);
|
||||
System.arraycopy(ciphertext, Constants.TAG_BYTES, actualPacket, 0,
|
||||
actualPacket.length);
|
||||
assertTrue(Arrays.equals(expectedPacket, actualPacket));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,8 +36,9 @@ public class PacketWriterImplTest extends TestCase {
|
||||
PacketEncrypter e = new NullPacketEncrypter(out);
|
||||
PacketWriter p = new PacketWriterImpl(e, mac, 0, 0L);
|
||||
p.getOutputStream().write(0);
|
||||
// There should be 16 zero bytes for the tag, 1 for the byte written
|
||||
assertTrue(Arrays.equals(new byte[17], out.toByteArray()));
|
||||
// There should be TAG_BYTES bytes for the tag, 1 byte for the write
|
||||
assertTrue(Arrays.equals(new byte[Constants.TAG_BYTES + 1],
|
||||
out.toByteArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -93,6 +94,7 @@ public class PacketWriterImplTest extends TestCase {
|
||||
+ "00000000" // 32 bits for the packet number
|
||||
+ "00000000" // 32 bits for the block number
|
||||
);
|
||||
assertEquals(Constants.TAG_BYTES, expectedTag.length);
|
||||
byte[] expectedTag1 = StringUtils.fromHexString(
|
||||
"0000" // 16 bits reserved
|
||||
+ "F00D" // 16 bits for the transport ID
|
||||
@@ -100,6 +102,7 @@ public class PacketWriterImplTest extends TestCase {
|
||||
+ "00000001" // 32 bits for the packet number
|
||||
+ "00000000" // 32 bits for the block number
|
||||
);
|
||||
assertEquals(Constants.TAG_BYTES, expectedTag1.length);
|
||||
// Calculate what the MAC on the first packet should be
|
||||
mac.update(expectedTag);
|
||||
mac.update((byte) 0);
|
||||
@@ -119,24 +122,27 @@ public class PacketWriterImplTest extends TestCase {
|
||||
p.getOutputStream().write(0);
|
||||
p.nextPacket();
|
||||
byte[] written = out.toByteArray();
|
||||
assertEquals(17 + expectedMac.length + 17 + expectedMac1.length,
|
||||
assertEquals(Constants.TAG_BYTES + 1 + expectedMac.length
|
||||
+ Constants.TAG_BYTES + 1 + expectedMac1.length,
|
||||
written.length);
|
||||
// Check the first packet's tag
|
||||
byte[] actualTag = new byte[16];
|
||||
System.arraycopy(written, 0, actualTag, 0, 16);
|
||||
byte[] actualTag = new byte[Constants.TAG_BYTES];
|
||||
System.arraycopy(written, 0, actualTag, 0, Constants.TAG_BYTES);
|
||||
assertTrue(Arrays.equals(expectedTag, actualTag));
|
||||
// Check the first packet's MAC
|
||||
byte[] actualMac = new byte[expectedMac.length];
|
||||
System.arraycopy(written, 17, actualMac, 0, actualMac.length);
|
||||
System.arraycopy(written, Constants.TAG_BYTES + 1, actualMac, 0,
|
||||
actualMac.length);
|
||||
assertTrue(Arrays.equals(expectedMac, actualMac));
|
||||
// Check the second packet's tag
|
||||
byte[] actualTag1 = new byte[16];
|
||||
System.arraycopy(written, 17 + expectedMac.length, actualTag1, 0, 16);
|
||||
byte[] actualTag1 = new byte[Constants.TAG_BYTES];
|
||||
System.arraycopy(written, Constants.TAG_BYTES + 1 + expectedMac.length,
|
||||
actualTag1, 0, Constants.TAG_BYTES);
|
||||
assertTrue(Arrays.equals(expectedTag1, actualTag1));
|
||||
// Check the second packet's MAC
|
||||
byte[] actualMac1 = new byte[expectedMac1.length];
|
||||
System.arraycopy(written, 17 + expectedMac.length + 17, actualMac1, 0,
|
||||
actualMac1.length);
|
||||
System.arraycopy(written, Constants.TAG_BYTES + 1 + expectedMac.length
|
||||
+ Constants.TAG_BYTES + 1, actualMac1, 0, actualMac1.length);
|
||||
assertTrue(Arrays.equals(expectedMac1, actualMac1));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user