mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Renamed frame cipher -> segment cipher, frame key -> segment key.
This commit is contained in:
@@ -9,7 +9,7 @@ import javax.crypto.Mac;
|
||||
|
||||
public interface CryptoComponent {
|
||||
|
||||
ErasableKey deriveFrameKey(byte[] secret, boolean initiator);
|
||||
ErasableKey deriveSegmentKey(byte[] secret, boolean initiator);
|
||||
|
||||
ErasableKey deriveTagKey(byte[] secret, boolean initiator);
|
||||
|
||||
@@ -19,19 +19,19 @@ public interface CryptoComponent {
|
||||
|
||||
KeyPair generateKeyPair();
|
||||
|
||||
ErasableKey generateTestKey();
|
||||
|
||||
Cipher getFrameCipher();
|
||||
|
||||
KeyParser getKeyParser();
|
||||
|
||||
Mac getMac();
|
||||
ErasableKey generateTestKey();
|
||||
|
||||
MessageDigest getMessageDigest();
|
||||
|
||||
SecureRandom getSecureRandom();
|
||||
|
||||
Cipher getSegmentCipher();
|
||||
|
||||
Signature getSignature();
|
||||
|
||||
Cipher getTagCipher();
|
||||
|
||||
Mac getMac();
|
||||
}
|
||||
|
||||
@@ -24,20 +24,21 @@ import com.google.inject.Inject;
|
||||
class CryptoComponentImpl implements CryptoComponent {
|
||||
|
||||
private static final String PROVIDER = "BC";
|
||||
private static final String DIGEST_ALGO = "SHA-256";
|
||||
private static final String KEY_PAIR_ALGO = "ECDSA";
|
||||
private static final int KEY_PAIR_BITS = 256;
|
||||
private static final String CIPHER_ALGO = "AES/CTR/NoPadding";
|
||||
private static final String SECRET_KEY_ALGO = "AES";
|
||||
private static final int SECRET_KEY_BYTES = 32; // 256 bits
|
||||
private static final int KEY_DERIVATION_IV_BYTES = 16; // 128 bits
|
||||
private static final String MAC_ALGO = "HMacSHA256";
|
||||
private static final String KEY_DERIVATION_ALGO = "AES/CTR/NoPadding";
|
||||
private static final String DIGEST_ALGO = "SHA-256";
|
||||
private static final String SIGNATURE_ALGO = "ECDSA";
|
||||
private static final String TAG_CIPHER_ALGO = "AES/ECB/NoPadding";
|
||||
private static final String SEGMENT_CIPHER_ALGO = "AES/CTR/NoPadding";
|
||||
private static final String MAC_ALGO = "HMacSHA256";
|
||||
|
||||
// Labels for key derivation, null-terminated
|
||||
private static final byte[] FRAME = { 'F', 'R', 'A', 'M', 'E', 0 };
|
||||
private static final byte[] TAG = { 'T', 'A', 'G', 0 };
|
||||
private static final byte[] SEGMENT = { 'S', 'E', 'G', 0 };
|
||||
private static final byte[] MAC = { 'M', 'A', 'C', 0 };
|
||||
private static final byte[] NEXT = { 'N', 'E', 'X', 'T', 0 };
|
||||
// Context strings for key derivation
|
||||
@@ -65,16 +66,16 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
secureRandom = new SecureRandom();
|
||||
}
|
||||
|
||||
public ErasableKey deriveFrameKey(byte[] secret, boolean initiator) {
|
||||
if(initiator) return deriveKey(secret, FRAME, INITIATOR);
|
||||
else return deriveKey(secret, FRAME, RESPONDER);
|
||||
}
|
||||
|
||||
public ErasableKey deriveTagKey(byte[] secret, boolean initiator) {
|
||||
if(initiator) return deriveKey(secret, TAG, INITIATOR);
|
||||
else return deriveKey(secret, TAG, RESPONDER);
|
||||
}
|
||||
|
||||
public ErasableKey deriveSegmentKey(byte[] secret, boolean initiator) {
|
||||
if(initiator) return deriveKey(secret, SEGMENT, INITIATOR);
|
||||
else return deriveKey(secret, SEGMENT, RESPONDER);
|
||||
}
|
||||
|
||||
public ErasableKey deriveMacKey(byte[] secret, boolean initiator) {
|
||||
if(initiator) return deriveKey(secret, MAC, INITIATOR);
|
||||
else return deriveKey(secret, MAC, RESPONDER);
|
||||
@@ -103,7 +104,7 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
assert ivBytes[ivBytes.length - 1] == 0;
|
||||
IvParameterSpec iv = new IvParameterSpec(ivBytes);
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance(CIPHER_ALGO, PROVIDER);
|
||||
Cipher cipher = Cipher.getInstance(KEY_DERIVATION_ALGO, PROVIDER);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||
byte[] output = cipher.doFinal(KEY_DERIVATION_INPUT);
|
||||
assert output.length == SECRET_KEY_BYTES;
|
||||
@@ -128,30 +129,14 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
return keyPairGenerator.generateKeyPair();
|
||||
}
|
||||
|
||||
public ErasableKey generateTestKey() {
|
||||
byte[] b = new byte[SECRET_KEY_BYTES];
|
||||
getSecureRandom().nextBytes(b);
|
||||
return new ErasableKeyImpl(b, SECRET_KEY_ALGO);
|
||||
}
|
||||
|
||||
public Cipher getFrameCipher() {
|
||||
try {
|
||||
return Cipher.getInstance(CIPHER_ALGO, PROVIDER);
|
||||
} catch(GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public KeyParser getKeyParser() {
|
||||
return keyParser;
|
||||
}
|
||||
|
||||
public Mac getMac() {
|
||||
try {
|
||||
return Mac.getInstance(MAC_ALGO, PROVIDER);
|
||||
} catch(GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
public ErasableKey generateTestKey() {
|
||||
byte[] b = new byte[SECRET_KEY_BYTES];
|
||||
getSecureRandom().nextBytes(b);
|
||||
return new ErasableKeyImpl(b, SECRET_KEY_ALGO);
|
||||
}
|
||||
|
||||
public MessageDigest getMessageDigest() {
|
||||
@@ -182,4 +167,20 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Cipher getSegmentCipher() {
|
||||
try {
|
||||
return Cipher.getInstance(SEGMENT_CIPHER_ALGO, PROVIDER);
|
||||
} catch(GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Mac getMac() {
|
||||
try {
|
||||
return Mac.getInstance(MAC_ALGO, PROVIDER);
|
||||
} catch(GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,20 +37,20 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
|
||||
private ConnectionReader createConnectionReader(InputStream in,
|
||||
byte[] secret, byte[] tag, boolean initiator) {
|
||||
// Derive the keys and erase the secret
|
||||
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
|
||||
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
|
||||
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
|
||||
ErasableKey segKey = crypto.deriveSegmentKey(secret, initiator);
|
||||
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
|
||||
ByteUtils.erase(secret);
|
||||
// Create the decrypter
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
Cipher frameCipher = crypto.getFrameCipher();
|
||||
Mac mac = crypto.getMac();
|
||||
Cipher segCipher = crypto.getSegmentCipher();
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false, tag);
|
||||
tagCipher, segCipher, tagKey, segKey, false, tag);
|
||||
// No error correction
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
// Create the reader
|
||||
Mac mac = crypto.getMac();
|
||||
return new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
}
|
||||
|
||||
@@ -67,21 +67,21 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
|
||||
private ConnectionReader createConnectionReader(SegmentSource in,
|
||||
byte[] secret, Segment buffered, boolean initiator) {
|
||||
// Derive the keys and erase the secret
|
||||
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
|
||||
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
|
||||
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
|
||||
ErasableKey segKey = crypto.deriveSegmentKey(secret, initiator);
|
||||
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
|
||||
ByteUtils.erase(secret);
|
||||
// Create the decrypter
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
Cipher frameCipher = crypto.getFrameCipher();
|
||||
Mac mac = crypto.getMac();
|
||||
Cipher segCipher = crypto.getSegmentCipher();
|
||||
IncomingEncryptionLayer decrypter =
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
|
||||
tagKey, frameKey, false, buffered);
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, segCipher,
|
||||
tagKey, segKey, false, buffered);
|
||||
// No error correction
|
||||
IncomingErrorCorrectionLayer correcter =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
// Create the reader
|
||||
Mac mac = crypto.getMac();
|
||||
return new ConnectionReaderImpl(correcter, mac, macKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,14 +27,14 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
|
||||
long capacity, byte[] secret, boolean initiator) {
|
||||
// Derive the keys and erase the secret
|
||||
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
|
||||
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
|
||||
ErasableKey segKey = crypto.deriveSegmentKey(secret, initiator);
|
||||
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
|
||||
ByteUtils.erase(secret);
|
||||
// Create the encrypter
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
Cipher frameCipher = crypto.getFrameCipher();
|
||||
Cipher segCipher = crypto.getSegmentCipher();
|
||||
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
|
||||
capacity, tagCipher, frameCipher, tagKey, frameKey, false);
|
||||
capacity, tagCipher, segCipher, tagKey, segKey, false);
|
||||
// No error correction
|
||||
OutgoingErrorCorrectionLayer correcter =
|
||||
new NullOutgoingErrorCorrectionLayer(encrypter);
|
||||
@@ -47,15 +47,15 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
|
||||
long capacity, byte[] secret, boolean initiator) {
|
||||
// Derive the keys and erase the secret
|
||||
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
|
||||
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
|
||||
ErasableKey segKey = crypto.deriveSegmentKey(secret, initiator);
|
||||
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
|
||||
ByteUtils.erase(secret);
|
||||
// Create the encrypter
|
||||
Cipher tagCipher = crypto.getTagCipher();
|
||||
Cipher frameCipher = crypto.getFrameCipher();
|
||||
Cipher segCipher = crypto.getSegmentCipher();
|
||||
OutgoingEncryptionLayer encrypter =
|
||||
new OutgoingSegmentedEncryptionLayer(out, capacity, tagCipher,
|
||||
frameCipher, tagKey, frameKey, false);
|
||||
segCipher, tagKey, segKey, false);
|
||||
// No error correction
|
||||
OutgoingErrorCorrectionLayer correcter =
|
||||
new NullOutgoingErrorCorrectionLayer(encrypter);
|
||||
|
||||
@@ -21,8 +21,8 @@ import net.sf.briar.api.transport.Segment;
|
||||
class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
|
||||
|
||||
private final InputStream in;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final Cipher tagCipher, segCipher;
|
||||
private final ErasableKey tagKey, segKey;
|
||||
private final int blockSize;
|
||||
private final byte[] iv, ciphertext;
|
||||
private final boolean tagEverySegment;
|
||||
@@ -32,16 +32,16 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
|
||||
private long segmentNumber = 0L;
|
||||
|
||||
IncomingEncryptionLayerImpl(InputStream in, Cipher tagCipher,
|
||||
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey,
|
||||
Cipher segCipher, ErasableKey tagKey, ErasableKey segKey,
|
||||
boolean tagEverySegment, byte[] bufferedTag) {
|
||||
this.in = in;
|
||||
this.tagCipher = tagCipher;
|
||||
this.frameCipher = frameCipher;
|
||||
this.segCipher = segCipher;
|
||||
this.tagKey = tagKey;
|
||||
this.frameKey = frameKey;
|
||||
this.segKey = segKey;
|
||||
this.tagEverySegment = tagEverySegment;
|
||||
this.bufferedTag = bufferedTag;
|
||||
blockSize = frameCipher.getBlockSize();
|
||||
blockSize = segCipher.getBlockSize();
|
||||
if(blockSize < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
iv = IvEncoder.encodeIv(0L, blockSize);
|
||||
@@ -93,8 +93,8 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
|
||||
try {
|
||||
IvEncoder.updateIv(iv, segmentNumber);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||
int decrypted = frameCipher.update(ciphertext, 0, blockSize,
|
||||
segCipher.init(Cipher.DECRYPT_MODE, segKey, ivSpec);
|
||||
int decrypted = segCipher.update(ciphertext, 0, blockSize,
|
||||
plaintext);
|
||||
if(decrypted != blockSize) throw new RuntimeException();
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
@@ -113,7 +113,7 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
|
||||
}
|
||||
// Decrypt the remainder of the frame/segment
|
||||
try {
|
||||
int decrypted = frameCipher.doFinal(ciphertext, blockSize,
|
||||
int decrypted = segCipher.doFinal(ciphertext, blockSize,
|
||||
length - blockSize, plaintext, blockSize);
|
||||
if(decrypted != length - blockSize)
|
||||
throw new RuntimeException();
|
||||
@@ -124,7 +124,7 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
|
||||
s.setSegmentNumber(segmentNumber++);
|
||||
return true;
|
||||
} catch(IOException e) {
|
||||
frameKey.erase();
|
||||
segKey.erase();
|
||||
tagKey.erase();
|
||||
throw e;
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ import net.sf.briar.api.transport.Segment;
|
||||
class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
|
||||
|
||||
private final SegmentSource in;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final Cipher tagCipher, segCipher;
|
||||
private final ErasableKey tagKey, segKey;
|
||||
private final int blockSize;
|
||||
private final byte[] iv;
|
||||
private final boolean tagEverySegment;
|
||||
@@ -31,15 +31,15 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
|
||||
private long segmentNumber = 0L;
|
||||
|
||||
IncomingSegmentedEncryptionLayer(SegmentSource in, Cipher tagCipher,
|
||||
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey,
|
||||
Cipher segCipher, ErasableKey tagKey, ErasableKey segKey,
|
||||
boolean tagEverySegment, Segment s) {
|
||||
this.in = in;
|
||||
this.tagCipher = tagCipher;
|
||||
this.frameCipher = frameCipher;
|
||||
this.segCipher = segCipher;
|
||||
this.tagKey = tagKey;
|
||||
this.frameKey = frameKey;
|
||||
this.segKey = segKey;
|
||||
this.tagEverySegment = tagEverySegment;
|
||||
blockSize = frameCipher.getBlockSize();
|
||||
blockSize = segCipher.getBlockSize();
|
||||
if(blockSize < FRAME_HEADER_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
iv = IvEncoder.encodeIv(0L, blockSize);
|
||||
@@ -76,8 +76,8 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
|
||||
try {
|
||||
IvEncoder.updateIv(iv, segmentNumber);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||
int decrypted = frameCipher.doFinal(ciphertext, offset,
|
||||
segCipher.init(Cipher.DECRYPT_MODE, segKey, ivSpec);
|
||||
int decrypted = segCipher.doFinal(ciphertext, offset,
|
||||
length - offset, s.getBuffer());
|
||||
if(decrypted != length - offset) throw new RuntimeException();
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
@@ -87,7 +87,7 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
|
||||
s.setSegmentNumber(segmentNumber++);
|
||||
return true;
|
||||
} catch(IOException e) {
|
||||
frameKey.erase();
|
||||
segKey.erase();
|
||||
tagKey.erase();
|
||||
throw e;
|
||||
}
|
||||
|
||||
@@ -16,24 +16,24 @@ import net.sf.briar.api.transport.Segment;
|
||||
class OutgoingEncryptionLayerImpl implements OutgoingEncryptionLayer {
|
||||
|
||||
private final OutputStream out;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final Cipher tagCipher, segCipher;
|
||||
private final ErasableKey tagKey, segKey;
|
||||
private final boolean tagEverySegment;
|
||||
private final byte[] iv, ciphertext;
|
||||
|
||||
private long capacity;
|
||||
|
||||
OutgoingEncryptionLayerImpl(OutputStream out, long capacity,
|
||||
Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey,
|
||||
ErasableKey frameKey, boolean tagEverySegment) {
|
||||
Cipher tagCipher, Cipher segCipher, ErasableKey tagKey,
|
||||
ErasableKey segKey, boolean tagEverySegment) {
|
||||
this.out = out;
|
||||
this.capacity = capacity;
|
||||
this.tagCipher = tagCipher;
|
||||
this.frameCipher = frameCipher;
|
||||
this.segCipher = segCipher;
|
||||
this.tagKey = tagKey;
|
||||
this.frameKey = frameKey;
|
||||
this.segKey = segKey;
|
||||
this.tagEverySegment = tagEverySegment;
|
||||
iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
iv = IvEncoder.encodeIv(0L, segCipher.getBlockSize());
|
||||
ciphertext = new byte[MAX_SEGMENT_LENGTH];
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ class OutgoingEncryptionLayerImpl implements OutgoingEncryptionLayer {
|
||||
IvEncoder.updateIv(iv, segmentNumber);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
try {
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
int encrypted = frameCipher.doFinal(plaintext, 0, length,
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
int encrypted = segCipher.doFinal(plaintext, 0, length,
|
||||
ciphertext, offset);
|
||||
if(encrypted != length) throw new RuntimeException();
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
@@ -59,7 +59,7 @@ class OutgoingEncryptionLayerImpl implements OutgoingEncryptionLayer {
|
||||
try {
|
||||
out.write(ciphertext, 0, offset + length);
|
||||
} catch(IOException e) {
|
||||
frameKey.erase();
|
||||
segKey.erase();
|
||||
tagKey.erase();
|
||||
throw e;
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ import net.sf.briar.api.transport.Segment;
|
||||
class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer {
|
||||
|
||||
private final SegmentSink out;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final Cipher tagCipher, segCipher;
|
||||
private final ErasableKey tagKey, segKey;
|
||||
private final boolean tagEverySegment;
|
||||
private final byte[] iv;
|
||||
private final Segment segment;
|
||||
@@ -24,16 +24,16 @@ class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer {
|
||||
private long capacity;
|
||||
|
||||
OutgoingSegmentedEncryptionLayer(SegmentSink out, long capacity,
|
||||
Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey,
|
||||
ErasableKey frameKey, boolean tagEverySegment) {
|
||||
Cipher tagCipher, Cipher segCipher, ErasableKey tagKey,
|
||||
ErasableKey segKey, boolean tagEverySegment) {
|
||||
this.out = out;
|
||||
this.capacity = capacity;
|
||||
this.tagCipher = tagCipher;
|
||||
this.frameCipher = frameCipher;
|
||||
this.segCipher = segCipher;
|
||||
this.tagKey = tagKey;
|
||||
this.frameKey = frameKey;
|
||||
this.segKey = segKey;
|
||||
this.tagEverySegment = tagEverySegment;
|
||||
iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
iv = IvEncoder.encodeIv(0L, segCipher.getBlockSize());
|
||||
segment = new SegmentImpl();
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer {
|
||||
IvEncoder.updateIv(iv, segmentNumber);
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
try {
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
int encrypted = frameCipher.doFinal(plaintext, 0, length,
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
int encrypted = segCipher.doFinal(plaintext, 0, length,
|
||||
ciphertext, offset);
|
||||
if(encrypted != length) throw new RuntimeException();
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
@@ -60,7 +60,7 @@ class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer {
|
||||
try {
|
||||
out.writeSegment(segment);
|
||||
} catch(IOException e) {
|
||||
frameKey.erase();
|
||||
segKey.erase();
|
||||
tagKey.erase();
|
||||
throw e;
|
||||
}
|
||||
|
||||
@@ -27,8 +27,8 @@ public class KeyDerivationTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testSixKeysAreDistinct() {
|
||||
List<ErasableKey> keys = new ArrayList<ErasableKey>();
|
||||
keys.add(crypto.deriveFrameKey(secret, true));
|
||||
keys.add(crypto.deriveFrameKey(secret, false));
|
||||
keys.add(crypto.deriveSegmentKey(secret, true));
|
||||
keys.add(crypto.deriveSegmentKey(secret, false));
|
||||
keys.add(crypto.deriveTagKey(secret, true));
|
||||
keys.add(crypto.deriveTagKey(secret, false));
|
||||
keys.add(crypto.deriveMacKey(secret, true));
|
||||
|
||||
@@ -27,26 +27,26 @@ import com.google.inject.Injector;
|
||||
public class FrameReadWriteTest extends BriarTestCase {
|
||||
|
||||
private final CryptoComponent crypto;
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final Cipher tagCipher, segCipher;
|
||||
private final Mac mac;
|
||||
private final Random random;
|
||||
private final byte[] outSecret;
|
||||
private final ErasableKey tagKey, frameKey, macKey;
|
||||
private final Mac mac;
|
||||
private final ErasableKey tagKey, segKey, macKey;
|
||||
|
||||
public FrameReadWriteTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
crypto = i.getInstance(CryptoComponent.class);
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
segCipher = crypto.getSegmentCipher();
|
||||
mac = crypto.getMac();
|
||||
random = new Random();
|
||||
// Since we're sending frames to ourselves, we only need outgoing keys
|
||||
outSecret = new byte[32];
|
||||
random.nextBytes(outSecret);
|
||||
tagKey = crypto.deriveTagKey(outSecret, true);
|
||||
frameKey = crypto.deriveFrameKey(outSecret, true);
|
||||
segKey = crypto.deriveSegmentKey(outSecret, true);
|
||||
macKey = crypto.deriveMacKey(outSecret, true);
|
||||
mac = crypto.getMac();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -69,13 +69,13 @@ public class FrameReadWriteTest extends BriarTestCase {
|
||||
byte[] frame1 = new byte[321];
|
||||
random.nextBytes(frame1);
|
||||
// Copy the keys - the copies will be erased
|
||||
ErasableKey frameCopy = frameKey.copy();
|
||||
ErasableKey tagCopy = tagKey.copy();
|
||||
ErasableKey segCopy = segKey.copy();
|
||||
ErasableKey macCopy = macKey.copy();
|
||||
// Write the frames
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
|
||||
Long.MAX_VALUE, tagCipher, frameCipher, tagCopy, frameCopy,
|
||||
Long.MAX_VALUE, tagCipher, segCipher, tagCopy, segCopy,
|
||||
false);
|
||||
OutgoingErrorCorrectionLayer correcter =
|
||||
new NullOutgoingErrorCorrectionLayer(encrypter);
|
||||
@@ -94,7 +94,7 @@ public class FrameReadWriteTest extends BriarTestCase {
|
||||
assertEquals(0L, TagEncoder.decodeTag(tag, tagCipher, tagKey));
|
||||
// Read the frames back
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false, recoveredTag);
|
||||
tagCipher, segCipher, tagKey, segKey, false, recoveredTag);
|
||||
IncomingErrorCorrectionLayer correcter1 =
|
||||
new NullIncomingErrorCorrectionLayer(decrypter);
|
||||
ConnectionReader reader = new ConnectionReaderImpl(correcter1, mac,
|
||||
|
||||
@@ -23,17 +23,17 @@ import com.google.inject.Injector;
|
||||
|
||||
public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final Cipher tagCipher, segCipher;
|
||||
private final ErasableKey tagKey, segKey;
|
||||
|
||||
public IncomingEncryptionLayerImplTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
segCipher = crypto.getSegmentCipher();
|
||||
tagKey = crypto.generateTestKey();
|
||||
frameKey = crypto.generateTestKey();
|
||||
segKey = crypto.generateTestKey();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -44,17 +44,17 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
// Calculate the ciphertext for the first segment
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
byte[] iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
byte[] iv = IvEncoder.encodeIv(0L, segCipher.getBlockSize());
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext = segCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
// Calculate the ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0,
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext1 = segCipher.doFinal(plaintext1, 0,
|
||||
plaintext1.length);
|
||||
// Concatenate the ciphertexts, excluding the first tag
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@@ -63,7 +63,7 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false, tag);
|
||||
tagCipher, segCipher, tagKey, segKey, false, tag);
|
||||
// First segment
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
@@ -91,10 +91,10 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
// Calculate the ciphertext for the first segment
|
||||
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
byte[] iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
byte[] iv = IvEncoder.encodeIv(0L, segCipher.getBlockSize());
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext = segCipher.doFinal(plaintext, 0, plaintext.length);
|
||||
// Calculate the tag for the second segment
|
||||
byte[] tag1 = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey);
|
||||
@@ -103,8 +103,8 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0,
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext1 = segCipher.doFinal(plaintext1, 0,
|
||||
plaintext1.length);
|
||||
// Concatenate the ciphertexts, excluding the first tag
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@@ -114,7 +114,7 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
|
||||
tagCipher, frameCipher, tagKey, frameKey, true, tag);
|
||||
tagCipher, segCipher, tagKey, segKey, true, tag);
|
||||
// First segment
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
|
||||
@@ -23,17 +23,17 @@ import com.google.inject.Injector;
|
||||
|
||||
public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final Cipher tagCipher, segCipher;
|
||||
private final ErasableKey tagKey, segKey;
|
||||
|
||||
public IncomingSegmentedEncryptionLayerTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
segCipher = crypto.getSegmentCipher();
|
||||
tagKey = crypto.generateTestKey();
|
||||
frameKey = crypto.generateTestKey();
|
||||
segKey = crypto.generateTestKey();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -43,18 +43,18 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
byte[] ciphertext = new byte[TAG_LENGTH + plaintext.length];
|
||||
TagEncoder.encodeTag(ciphertext, 0L, tagCipher, tagKey);
|
||||
byte[] iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
byte[] iv = IvEncoder.encodeIv(0L, segCipher.getBlockSize());
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext,
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
segCipher.doFinal(plaintext, 0, plaintext.length, ciphertext,
|
||||
TAG_LENGTH);
|
||||
// Calculate the ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0,
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext1 = segCipher.doFinal(plaintext1, 0,
|
||||
plaintext1.length);
|
||||
// Buffer the first segment and create a source for the second
|
||||
Segment buffered = new SegmentImpl();
|
||||
@@ -64,8 +64,8 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
SegmentSource in = new ByteArraySegmentSource(ciphertext1);
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
IncomingEncryptionLayer decrypter =
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
|
||||
tagKey, frameKey, false, buffered);
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, segCipher,
|
||||
tagKey, segKey, false, buffered);
|
||||
// First segment
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
@@ -92,10 +92,10 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
|
||||
byte[] ciphertext = new byte[TAG_LENGTH + plaintext.length];
|
||||
TagEncoder.encodeTag(ciphertext, 0L, tagCipher, tagKey);
|
||||
byte[] iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize());
|
||||
byte[] iv = IvEncoder.encodeIv(0L, segCipher.getBlockSize());
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext,
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
segCipher.doFinal(plaintext, 0, plaintext.length, ciphertext,
|
||||
TAG_LENGTH);
|
||||
// Calculate the ciphertext for the second frame, including its tag
|
||||
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
|
||||
@@ -104,8 +104,8 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
TagEncoder.encodeTag(ciphertext1, 1L, tagCipher, tagKey);
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
frameCipher.doFinal(plaintext1, 0, plaintext1.length, ciphertext1,
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
segCipher.doFinal(plaintext1, 0, plaintext1.length, ciphertext1,
|
||||
TAG_LENGTH);
|
||||
// Buffer the first segment and create a source for the second
|
||||
Segment buffered = new SegmentImpl();
|
||||
@@ -115,8 +115,8 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
SegmentSource in = new ByteArraySegmentSource(ciphertext1);
|
||||
// Use the encryption layer to decrypt the ciphertext
|
||||
IncomingEncryptionLayer decrypter =
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher,
|
||||
tagKey, frameKey, true, buffered);
|
||||
new IncomingSegmentedEncryptionLayer(in, tagCipher, segCipher,
|
||||
tagKey, segKey, true, buffered);
|
||||
// First segment
|
||||
Segment s = new SegmentImpl();
|
||||
assertTrue(decrypter.readSegment(s));
|
||||
|
||||
@@ -23,17 +23,17 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
|
||||
private static final int MAC_LENGTH = 32;
|
||||
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final Cipher tagCipher, segCipher;
|
||||
private final ErasableKey tagKey, segKey;
|
||||
|
||||
public OutgoingEncryptionLayerImplTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
segCipher = crypto.getSegmentCipher();
|
||||
tagKey = crypto.generateTestKey();
|
||||
frameKey = crypto.generateTestKey();
|
||||
segKey = crypto.generateTestKey();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -42,17 +42,17 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first segment
|
||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||
byte[] iv = new byte[segCipher.getBlockSize()];
|
||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext = segCipher.doFinal(plaintext);
|
||||
// Calculate the expected ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext1 = segCipher.doFinal(plaintext1);
|
||||
// Concatenate the ciphertexts
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(tag);
|
||||
@@ -62,7 +62,7 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
// Use the encryption layer to encrypt the plaintext
|
||||
out.reset();
|
||||
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
|
||||
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey,
|
||||
Long.MAX_VALUE, tagCipher, segCipher, tagKey, segKey,
|
||||
false);
|
||||
Segment s = new SegmentImpl();
|
||||
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
|
||||
@@ -86,11 +86,11 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first segment
|
||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||
byte[] iv = new byte[segCipher.getBlockSize()];
|
||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext = segCipher.doFinal(plaintext);
|
||||
// Calculate the expected tag for the second segment
|
||||
byte[] tag1 = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey);
|
||||
@@ -98,8 +98,8 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext1 = segCipher.doFinal(plaintext1);
|
||||
// Concatenate the ciphertexts
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(tag);
|
||||
@@ -110,7 +110,7 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
|
||||
// Use the encryption layer to encrypt the plaintext
|
||||
out.reset();
|
||||
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
|
||||
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey, true);
|
||||
Long.MAX_VALUE, tagCipher, segCipher, tagKey, segKey, true);
|
||||
Segment s = new SegmentImpl();
|
||||
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
|
||||
s.setLength(plaintext.length);
|
||||
|
||||
@@ -25,17 +25,17 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
|
||||
private static final int MAC_LENGTH = 32;
|
||||
|
||||
private final Cipher tagCipher, frameCipher;
|
||||
private final ErasableKey tagKey, frameKey;
|
||||
private final Cipher tagCipher, segCipher;
|
||||
private final ErasableKey tagKey, segKey;
|
||||
|
||||
public OutgoingSegmentedEncryptionLayerTest() {
|
||||
super();
|
||||
Injector i = Guice.createInjector(new CryptoModule());
|
||||
CryptoComponent crypto = i.getInstance(CryptoComponent.class);
|
||||
tagCipher = crypto.getTagCipher();
|
||||
frameCipher = crypto.getFrameCipher();
|
||||
segCipher = crypto.getSegmentCipher();
|
||||
tagKey = crypto.generateTestKey();
|
||||
frameKey = crypto.generateTestKey();
|
||||
segKey = crypto.generateTestKey();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -44,17 +44,17 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first segment
|
||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||
byte[] iv = new byte[segCipher.getBlockSize()];
|
||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext = segCipher.doFinal(plaintext);
|
||||
// Calculate the expected ciphertext for the second segment
|
||||
byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext1 = segCipher.doFinal(plaintext1);
|
||||
// Concatenate the ciphertexts
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(tag);
|
||||
@@ -65,7 +65,7 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
ByteArraySegmentSink sink = new ByteArraySegmentSink();
|
||||
OutgoingEncryptionLayer encrypter =
|
||||
new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE,
|
||||
tagCipher, frameCipher, tagKey, frameKey, false);
|
||||
tagCipher, segCipher, tagKey, segKey, false);
|
||||
Segment s = new SegmentImpl();
|
||||
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
|
||||
s.setLength(plaintext.length);
|
||||
@@ -88,11 +88,11 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
byte[] tag = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
|
||||
// Calculate the expected ciphertext for the first segment
|
||||
byte[] iv = new byte[frameCipher.getBlockSize()];
|
||||
byte[] iv = new byte[segCipher.getBlockSize()];
|
||||
byte[] plaintext = new byte[123 + MAC_LENGTH];
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext = frameCipher.doFinal(plaintext);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext = segCipher.doFinal(plaintext);
|
||||
// Calculate the expected tag for the second segment
|
||||
byte[] tag1 = new byte[TAG_LENGTH];
|
||||
TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey);
|
||||
@@ -100,8 +100,8 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
|
||||
IvEncoder.updateIv(iv, 1L);
|
||||
ivSpec = new IvParameterSpec(iv);
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
byte[] ciphertext1 = frameCipher.doFinal(plaintext1);
|
||||
segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
|
||||
byte[] ciphertext1 = segCipher.doFinal(plaintext1);
|
||||
// Concatenate the ciphertexts
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(tag);
|
||||
@@ -113,7 +113,7 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
|
||||
SegmentSink sink = new ByteArraySegmentSink();
|
||||
OutgoingEncryptionLayer encrypter =
|
||||
new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE,
|
||||
tagCipher, frameCipher, tagKey, frameKey, true);
|
||||
tagCipher, segCipher, tagKey, segKey, true);
|
||||
Segment s = new SegmentImpl();
|
||||
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
|
||||
s.setLength(plaintext.length);
|
||||
|
||||
Reference in New Issue
Block a user