mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Include stream number in stream header nonce.
This commit is contained in:
@@ -22,22 +22,23 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
||||
|
||||
private final AuthenticatedCipher cipher;
|
||||
private final SecretKey streamHeaderKey, frameKey;
|
||||
private final byte[] streamHeaderIv;
|
||||
private final byte[] streamHeaderIv, payload;
|
||||
private final int payloadLength = 123, paddingLength = 234;
|
||||
private final long streamNumber = 1234;
|
||||
|
||||
public StreamDecrypterImplTest() {
|
||||
cipher = new TestAuthenticatedCipher(); // Null cipher
|
||||
streamHeaderKey = TestUtils.getSecretKey();
|
||||
frameKey = TestUtils.getSecretKey();
|
||||
streamHeaderIv = TestUtils.getRandomBytes(STREAM_HEADER_IV_LENGTH);
|
||||
payload = TestUtils.getRandomBytes(payloadLength);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadValidFrames() throws Exception {
|
||||
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
||||
int payloadLength = 123, paddingLength = 234;
|
||||
FrameEncoder.encodeHeader(frameHeader, false, payloadLength,
|
||||
paddingLength);
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
|
||||
byte[] frameHeader1 = new byte[FRAME_HEADER_LENGTH];
|
||||
int payloadLength1 = 345, paddingLength1 = 456;
|
||||
@@ -60,7 +61,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
StreamDecrypterImpl s = new StreamDecrypterImpl(in, cipher,
|
||||
streamHeaderKey);
|
||||
streamNumber, streamHeaderKey);
|
||||
|
||||
// Read the first frame
|
||||
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
||||
@@ -78,10 +79,9 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
||||
@Test(expected = IOException.class)
|
||||
public void testTruncatedFrameThrowsException() throws Exception {
|
||||
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
||||
int payloadLength = 123, paddingLength = 234;
|
||||
FrameEncoder.encodeHeader(frameHeader, false, payloadLength,
|
||||
paddingLength);
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(streamHeaderIv);
|
||||
out.write(frameKey.getBytes());
|
||||
@@ -93,7 +93,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
StreamDecrypterImpl s = new StreamDecrypterImpl(in, cipher,
|
||||
streamHeaderKey);
|
||||
streamNumber, streamHeaderKey);
|
||||
|
||||
// Try to read the truncated frame
|
||||
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
||||
@@ -121,7 +121,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
StreamDecrypterImpl s = new StreamDecrypterImpl(in, cipher,
|
||||
streamHeaderKey);
|
||||
streamNumber, streamHeaderKey);
|
||||
|
||||
// Try to read the invalid frame
|
||||
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
||||
@@ -131,10 +131,8 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
||||
@Test(expected = IOException.class)
|
||||
public void testNonZeroPaddingThrowsException() throws Exception {
|
||||
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
||||
int payloadLength = 123, paddingLength = 234;
|
||||
FrameEncoder.encodeHeader(frameHeader, false, payloadLength,
|
||||
paddingLength);
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
// Set one of the padding bytes non-zero
|
||||
byte[] padding = new byte[paddingLength];
|
||||
padding[paddingLength - 1] = 1;
|
||||
@@ -150,7 +148,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
StreamDecrypterImpl s = new StreamDecrypterImpl(in, cipher,
|
||||
streamHeaderKey);
|
||||
streamNumber, streamHeaderKey);
|
||||
|
||||
// Try to read the invalid frame
|
||||
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
||||
@@ -160,10 +158,8 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testCannotReadBeyondFinalFrame() throws Exception {
|
||||
byte[] frameHeader = new byte[FRAME_HEADER_LENGTH];
|
||||
int payloadLength = 123, paddingLength = 234;
|
||||
FrameEncoder.encodeHeader(frameHeader, true, payloadLength,
|
||||
paddingLength);
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(streamHeaderIv);
|
||||
@@ -178,7 +174,7 @@ public class StreamDecrypterImplTest extends BriarTestCase {
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
StreamDecrypterImpl s = new StreamDecrypterImpl(in, cipher,
|
||||
streamHeaderKey);
|
||||
streamNumber, streamHeaderKey);
|
||||
|
||||
// Read the first frame
|
||||
byte[] buffer = new byte[MAX_PAYLOAD_LENGTH];
|
||||
|
||||
@@ -17,7 +17,9 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
|
||||
private final AuthenticatedCipher cipher;
|
||||
private final SecretKey streamHeaderKey, frameKey;
|
||||
private final byte[] tag, streamHeaderIv;
|
||||
private final byte[] tag, streamHeaderIv, payload;
|
||||
private final long streamNumber = 1234;
|
||||
private final int payloadLength = 123, paddingLength = 234;
|
||||
|
||||
public StreamEncrypterImplTest() {
|
||||
cipher = new TestAuthenticatedCipher(); // Null cipher
|
||||
@@ -25,15 +27,14 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
frameKey = TestUtils.getSecretKey();
|
||||
tag = TestUtils.getRandomBytes(TAG_LENGTH);
|
||||
streamHeaderIv = TestUtils.getRandomBytes(STREAM_HEADER_IV_LENGTH);
|
||||
payload = TestUtils.getRandomBytes(payloadLength);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteUnpaddedNonFinalFrameWithTag() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength = 123;
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, tag, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
s.writeFrame(payload, payloadLength, 0, false);
|
||||
|
||||
@@ -55,10 +56,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testWriteUnpaddedFinalFrameWithTag() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength = 123;
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, tag, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
s.writeFrame(payload, payloadLength, 0, true);
|
||||
|
||||
@@ -80,10 +79,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testWriteUnpaddedNonFinalFrameWithoutTag() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, null,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength = 123;
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, null, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
s.writeFrame(payload, payloadLength, 0, false);
|
||||
|
||||
@@ -104,10 +101,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testWriteUnpaddedFinalFrameWithoutTag() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, null,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength = 123;
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, null, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
s.writeFrame(payload, payloadLength, 0, true);
|
||||
|
||||
@@ -128,10 +123,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testWritePaddedNonFinalFrameWithTag() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength = 123, paddingLength = 234;
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, tag, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
s.writeFrame(payload, payloadLength, paddingLength, false);
|
||||
|
||||
@@ -155,10 +148,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testWritePaddedFinalFrameWithTag() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength = 123, paddingLength = 234;
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, tag, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
s.writeFrame(payload, payloadLength, paddingLength, true);
|
||||
|
||||
@@ -182,10 +173,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testWritePaddedNonFinalFrameWithoutTag() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, null,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength = 123, paddingLength = 234;
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, null, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
s.writeFrame(payload, payloadLength, paddingLength, false);
|
||||
|
||||
@@ -208,10 +197,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testWritePaddedFinalFrameWithoutTag() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, null,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength = 123, paddingLength = 234;
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, null, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
s.writeFrame(payload, payloadLength, paddingLength, true);
|
||||
|
||||
@@ -234,10 +221,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testWriteTwoFramesWithTag() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength = 123, paddingLength = 234;
|
||||
byte[] payload = TestUtils.getRandomBytes(payloadLength);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, tag, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
int payloadLength1 = 345, paddingLength1 = 456;
|
||||
byte[] payload1 = TestUtils.getRandomBytes(payloadLength1);
|
||||
|
||||
@@ -273,8 +258,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
public void testFlushWritesTagAndStreamHeaderIfNotAlreadyWritten()
|
||||
throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, tag, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
// Flush the stream once
|
||||
s.flush();
|
||||
@@ -293,8 +278,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
public void testFlushDoesNotWriteTagOrStreamHeaderIfAlreadyWritten()
|
||||
throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, tag,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, tag, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
// Flush the stream twice
|
||||
s.flush();
|
||||
@@ -313,8 +298,8 @@ public class StreamEncrypterImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testFlushDoesNotWriteTagIfNull() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher, null,
|
||||
streamHeaderIv, streamHeaderKey, frameKey);
|
||||
StreamEncrypterImpl s = new StreamEncrypterImpl(out, cipher,
|
||||
streamNumber, null, streamHeaderIv, streamHeaderKey, frameKey);
|
||||
|
||||
// Flush the stream once
|
||||
s.flush();
|
||||
|
||||
Reference in New Issue
Block a user