Renamed frame cipher -> segment cipher, frame key -> segment key.

This commit is contained in:
akwizgran
2012-01-17 21:44:28 +00:00
parent 6085b70b85
commit 249c82d30e
14 changed files with 170 additions and 169 deletions

View File

@@ -9,7 +9,7 @@ import javax.crypto.Mac;
public interface CryptoComponent { public interface CryptoComponent {
ErasableKey deriveFrameKey(byte[] secret, boolean initiator); ErasableKey deriveSegmentKey(byte[] secret, boolean initiator);
ErasableKey deriveTagKey(byte[] secret, boolean initiator); ErasableKey deriveTagKey(byte[] secret, boolean initiator);
@@ -19,19 +19,19 @@ public interface CryptoComponent {
KeyPair generateKeyPair(); KeyPair generateKeyPair();
ErasableKey generateTestKey();
Cipher getFrameCipher();
KeyParser getKeyParser(); KeyParser getKeyParser();
Mac getMac(); ErasableKey generateTestKey();
MessageDigest getMessageDigest(); MessageDigest getMessageDigest();
SecureRandom getSecureRandom(); SecureRandom getSecureRandom();
Cipher getSegmentCipher();
Signature getSignature(); Signature getSignature();
Cipher getTagCipher(); Cipher getTagCipher();
Mac getMac();
} }

View File

@@ -24,20 +24,21 @@ import com.google.inject.Inject;
class CryptoComponentImpl implements CryptoComponent { class CryptoComponentImpl implements CryptoComponent {
private static final String PROVIDER = "BC"; 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 String KEY_PAIR_ALGO = "ECDSA";
private static final int KEY_PAIR_BITS = 256; 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 String SECRET_KEY_ALGO = "AES";
private static final int SECRET_KEY_BYTES = 32; // 256 bits private static final int SECRET_KEY_BYTES = 32; // 256 bits
private static final int KEY_DERIVATION_IV_BYTES = 16; // 128 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 SIGNATURE_ALGO = "ECDSA";
private static final String TAG_CIPHER_ALGO = "AES/ECB/NoPadding"; 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 // 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[] 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[] MAC = { 'M', 'A', 'C', 0 };
private static final byte[] NEXT = { 'N', 'E', 'X', 'T', 0 }; private static final byte[] NEXT = { 'N', 'E', 'X', 'T', 0 };
// Context strings for key derivation // Context strings for key derivation
@@ -65,16 +66,16 @@ class CryptoComponentImpl implements CryptoComponent {
secureRandom = new SecureRandom(); 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) { public ErasableKey deriveTagKey(byte[] secret, boolean initiator) {
if(initiator) return deriveKey(secret, TAG, INITIATOR); if(initiator) return deriveKey(secret, TAG, INITIATOR);
else return deriveKey(secret, TAG, RESPONDER); 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) { public ErasableKey deriveMacKey(byte[] secret, boolean initiator) {
if(initiator) return deriveKey(secret, MAC, INITIATOR); if(initiator) return deriveKey(secret, MAC, INITIATOR);
else return deriveKey(secret, MAC, RESPONDER); else return deriveKey(secret, MAC, RESPONDER);
@@ -103,7 +104,7 @@ class CryptoComponentImpl implements CryptoComponent {
assert ivBytes[ivBytes.length - 1] == 0; assert ivBytes[ivBytes.length - 1] == 0;
IvParameterSpec iv = new IvParameterSpec(ivBytes); IvParameterSpec iv = new IvParameterSpec(ivBytes);
try { try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGO, PROVIDER); Cipher cipher = Cipher.getInstance(KEY_DERIVATION_ALGO, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, key, iv); cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] output = cipher.doFinal(KEY_DERIVATION_INPUT); byte[] output = cipher.doFinal(KEY_DERIVATION_INPUT);
assert output.length == SECRET_KEY_BYTES; assert output.length == SECRET_KEY_BYTES;
@@ -128,30 +129,14 @@ class CryptoComponentImpl implements CryptoComponent {
return keyPairGenerator.generateKeyPair(); 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() { public KeyParser getKeyParser() {
return keyParser; return keyParser;
} }
public Mac getMac() { public ErasableKey generateTestKey() {
try { byte[] b = new byte[SECRET_KEY_BYTES];
return Mac.getInstance(MAC_ALGO, PROVIDER); getSecureRandom().nextBytes(b);
} catch(GeneralSecurityException e) { return new ErasableKeyImpl(b, SECRET_KEY_ALGO);
throw new RuntimeException(e);
}
} }
public MessageDigest getMessageDigest() { public MessageDigest getMessageDigest() {
@@ -182,4 +167,20 @@ class CryptoComponentImpl implements CryptoComponent {
throw new RuntimeException(e); 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);
}
}
} }

View File

@@ -37,20 +37,20 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
private ConnectionReader createConnectionReader(InputStream in, private ConnectionReader createConnectionReader(InputStream in,
byte[] secret, byte[] tag, boolean initiator) { byte[] secret, byte[] tag, boolean initiator) {
// Derive the keys and erase the secret // 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 tagKey = crypto.deriveTagKey(secret, initiator);
ErasableKey segKey = crypto.deriveSegmentKey(secret, initiator);
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
ByteUtils.erase(secret); ByteUtils.erase(secret);
// Create the decrypter // Create the decrypter
Cipher tagCipher = crypto.getTagCipher(); Cipher tagCipher = crypto.getTagCipher();
Cipher frameCipher = crypto.getFrameCipher(); Cipher segCipher = crypto.getSegmentCipher();
Mac mac = crypto.getMac();
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in, IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
tagCipher, frameCipher, tagKey, frameKey, false, tag); tagCipher, segCipher, tagKey, segKey, false, tag);
// No error correction // No error correction
IncomingErrorCorrectionLayer correcter = IncomingErrorCorrectionLayer correcter =
new NullIncomingErrorCorrectionLayer(decrypter); new NullIncomingErrorCorrectionLayer(decrypter);
// Create the reader // Create the reader
Mac mac = crypto.getMac();
return new ConnectionReaderImpl(correcter, mac, macKey); return new ConnectionReaderImpl(correcter, mac, macKey);
} }
@@ -67,21 +67,21 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
private ConnectionReader createConnectionReader(SegmentSource in, private ConnectionReader createConnectionReader(SegmentSource in,
byte[] secret, Segment buffered, boolean initiator) { byte[] secret, Segment buffered, boolean initiator) {
// Derive the keys and erase the secret // 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 tagKey = crypto.deriveTagKey(secret, initiator);
ErasableKey segKey = crypto.deriveSegmentKey(secret, initiator);
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
ByteUtils.erase(secret); ByteUtils.erase(secret);
// Create the decrypter // Create the decrypter
Cipher tagCipher = crypto.getTagCipher(); Cipher tagCipher = crypto.getTagCipher();
Cipher frameCipher = crypto.getFrameCipher(); Cipher segCipher = crypto.getSegmentCipher();
Mac mac = crypto.getMac();
IncomingEncryptionLayer decrypter = IncomingEncryptionLayer decrypter =
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher, new IncomingSegmentedEncryptionLayer(in, tagCipher, segCipher,
tagKey, frameKey, false, buffered); tagKey, segKey, false, buffered);
// No error correction // No error correction
IncomingErrorCorrectionLayer correcter = IncomingErrorCorrectionLayer correcter =
new NullIncomingErrorCorrectionLayer(decrypter); new NullIncomingErrorCorrectionLayer(decrypter);
// Create the reader // Create the reader
Mac mac = crypto.getMac();
return new ConnectionReaderImpl(correcter, mac, macKey); return new ConnectionReaderImpl(correcter, mac, macKey);
} }
} }

View File

@@ -27,14 +27,14 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
long capacity, byte[] secret, boolean initiator) { long capacity, byte[] secret, boolean initiator) {
// Derive the keys and erase the secret // Derive the keys and erase the secret
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator); ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator); ErasableKey segKey = crypto.deriveSegmentKey(secret, initiator);
ErasableKey macKey = crypto.deriveMacKey(secret, initiator); ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
ByteUtils.erase(secret); ByteUtils.erase(secret);
// Create the encrypter // Create the encrypter
Cipher tagCipher = crypto.getTagCipher(); Cipher tagCipher = crypto.getTagCipher();
Cipher frameCipher = crypto.getFrameCipher(); Cipher segCipher = crypto.getSegmentCipher();
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out, OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
capacity, tagCipher, frameCipher, tagKey, frameKey, false); capacity, tagCipher, segCipher, tagKey, segKey, false);
// No error correction // No error correction
OutgoingErrorCorrectionLayer correcter = OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter); new NullOutgoingErrorCorrectionLayer(encrypter);
@@ -47,15 +47,15 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
long capacity, byte[] secret, boolean initiator) { long capacity, byte[] secret, boolean initiator) {
// Derive the keys and erase the secret // Derive the keys and erase the secret
ErasableKey tagKey = crypto.deriveTagKey(secret, initiator); ErasableKey tagKey = crypto.deriveTagKey(secret, initiator);
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator); ErasableKey segKey = crypto.deriveSegmentKey(secret, initiator);
ErasableKey macKey = crypto.deriveMacKey(secret, initiator); ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
ByteUtils.erase(secret); ByteUtils.erase(secret);
// Create the encrypter // Create the encrypter
Cipher tagCipher = crypto.getTagCipher(); Cipher tagCipher = crypto.getTagCipher();
Cipher frameCipher = crypto.getFrameCipher(); Cipher segCipher = crypto.getSegmentCipher();
OutgoingEncryptionLayer encrypter = OutgoingEncryptionLayer encrypter =
new OutgoingSegmentedEncryptionLayer(out, capacity, tagCipher, new OutgoingSegmentedEncryptionLayer(out, capacity, tagCipher,
frameCipher, tagKey, frameKey, false); segCipher, tagKey, segKey, false);
// No error correction // No error correction
OutgoingErrorCorrectionLayer correcter = OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter); new NullOutgoingErrorCorrectionLayer(encrypter);

View File

@@ -21,8 +21,8 @@ import net.sf.briar.api.transport.Segment;
class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer { class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
private final InputStream in; private final InputStream in;
private final Cipher tagCipher, frameCipher; private final Cipher tagCipher, segCipher;
private final ErasableKey tagKey, frameKey; private final ErasableKey tagKey, segKey;
private final int blockSize; private final int blockSize;
private final byte[] iv, ciphertext; private final byte[] iv, ciphertext;
private final boolean tagEverySegment; private final boolean tagEverySegment;
@@ -32,16 +32,16 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
private long segmentNumber = 0L; private long segmentNumber = 0L;
IncomingEncryptionLayerImpl(InputStream in, Cipher tagCipher, IncomingEncryptionLayerImpl(InputStream in, Cipher tagCipher,
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey, Cipher segCipher, ErasableKey tagKey, ErasableKey segKey,
boolean tagEverySegment, byte[] bufferedTag) { boolean tagEverySegment, byte[] bufferedTag) {
this.in = in; this.in = in;
this.tagCipher = tagCipher; this.tagCipher = tagCipher;
this.frameCipher = frameCipher; this.segCipher = segCipher;
this.tagKey = tagKey; this.tagKey = tagKey;
this.frameKey = frameKey; this.segKey = segKey;
this.tagEverySegment = tagEverySegment; this.tagEverySegment = tagEverySegment;
this.bufferedTag = bufferedTag; this.bufferedTag = bufferedTag;
blockSize = frameCipher.getBlockSize(); blockSize = segCipher.getBlockSize();
if(blockSize < FRAME_HEADER_LENGTH) if(blockSize < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
iv = IvEncoder.encodeIv(0L, blockSize); iv = IvEncoder.encodeIv(0L, blockSize);
@@ -93,8 +93,8 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
try { try {
IvEncoder.updateIv(iv, segmentNumber); IvEncoder.updateIv(iv, segmentNumber);
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.DECRYPT_MODE, segKey, ivSpec);
int decrypted = frameCipher.update(ciphertext, 0, blockSize, int decrypted = segCipher.update(ciphertext, 0, blockSize,
plaintext); plaintext);
if(decrypted != blockSize) throw new RuntimeException(); if(decrypted != blockSize) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) { } catch(GeneralSecurityException badCipher) {
@@ -113,7 +113,7 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
} }
// Decrypt the remainder of the frame/segment // Decrypt the remainder of the frame/segment
try { try {
int decrypted = frameCipher.doFinal(ciphertext, blockSize, int decrypted = segCipher.doFinal(ciphertext, blockSize,
length - blockSize, plaintext, blockSize); length - blockSize, plaintext, blockSize);
if(decrypted != length - blockSize) if(decrypted != length - blockSize)
throw new RuntimeException(); throw new RuntimeException();
@@ -124,7 +124,7 @@ class IncomingEncryptionLayerImpl implements IncomingEncryptionLayer {
s.setSegmentNumber(segmentNumber++); s.setSegmentNumber(segmentNumber++);
return true; return true;
} catch(IOException e) { } catch(IOException e) {
frameKey.erase(); segKey.erase();
tagKey.erase(); tagKey.erase();
throw e; throw e;
} }

View File

@@ -19,8 +19,8 @@ import net.sf.briar.api.transport.Segment;
class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer { class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
private final SegmentSource in; private final SegmentSource in;
private final Cipher tagCipher, frameCipher; private final Cipher tagCipher, segCipher;
private final ErasableKey tagKey, frameKey; private final ErasableKey tagKey, segKey;
private final int blockSize; private final int blockSize;
private final byte[] iv; private final byte[] iv;
private final boolean tagEverySegment; private final boolean tagEverySegment;
@@ -31,15 +31,15 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
private long segmentNumber = 0L; private long segmentNumber = 0L;
IncomingSegmentedEncryptionLayer(SegmentSource in, Cipher tagCipher, IncomingSegmentedEncryptionLayer(SegmentSource in, Cipher tagCipher,
Cipher frameCipher, ErasableKey tagKey, ErasableKey frameKey, Cipher segCipher, ErasableKey tagKey, ErasableKey segKey,
boolean tagEverySegment, Segment s) { boolean tagEverySegment, Segment s) {
this.in = in; this.in = in;
this.tagCipher = tagCipher; this.tagCipher = tagCipher;
this.frameCipher = frameCipher; this.segCipher = segCipher;
this.tagKey = tagKey; this.tagKey = tagKey;
this.frameKey = frameKey; this.segKey = segKey;
this.tagEverySegment = tagEverySegment; this.tagEverySegment = tagEverySegment;
blockSize = frameCipher.getBlockSize(); blockSize = segCipher.getBlockSize();
if(blockSize < FRAME_HEADER_LENGTH) if(blockSize < FRAME_HEADER_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
iv = IvEncoder.encodeIv(0L, blockSize); iv = IvEncoder.encodeIv(0L, blockSize);
@@ -76,8 +76,8 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
try { try {
IvEncoder.updateIv(iv, segmentNumber); IvEncoder.updateIv(iv, segmentNumber);
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.DECRYPT_MODE, segKey, ivSpec);
int decrypted = frameCipher.doFinal(ciphertext, offset, int decrypted = segCipher.doFinal(ciphertext, offset,
length - offset, s.getBuffer()); length - offset, s.getBuffer());
if(decrypted != length - offset) throw new RuntimeException(); if(decrypted != length - offset) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) { } catch(GeneralSecurityException badCipher) {
@@ -87,7 +87,7 @@ class IncomingSegmentedEncryptionLayer implements IncomingEncryptionLayer {
s.setSegmentNumber(segmentNumber++); s.setSegmentNumber(segmentNumber++);
return true; return true;
} catch(IOException e) { } catch(IOException e) {
frameKey.erase(); segKey.erase();
tagKey.erase(); tagKey.erase();
throw e; throw e;
} }

View File

@@ -16,24 +16,24 @@ import net.sf.briar.api.transport.Segment;
class OutgoingEncryptionLayerImpl implements OutgoingEncryptionLayer { class OutgoingEncryptionLayerImpl implements OutgoingEncryptionLayer {
private final OutputStream out; private final OutputStream out;
private final Cipher tagCipher, frameCipher; private final Cipher tagCipher, segCipher;
private final ErasableKey tagKey, frameKey; private final ErasableKey tagKey, segKey;
private final boolean tagEverySegment; private final boolean tagEverySegment;
private final byte[] iv, ciphertext; private final byte[] iv, ciphertext;
private long capacity; private long capacity;
OutgoingEncryptionLayerImpl(OutputStream out, long capacity, OutgoingEncryptionLayerImpl(OutputStream out, long capacity,
Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey, Cipher tagCipher, Cipher segCipher, ErasableKey tagKey,
ErasableKey frameKey, boolean tagEverySegment) { ErasableKey segKey, boolean tagEverySegment) {
this.out = out; this.out = out;
this.capacity = capacity; this.capacity = capacity;
this.tagCipher = tagCipher; this.tagCipher = tagCipher;
this.frameCipher = frameCipher; this.segCipher = segCipher;
this.tagKey = tagKey; this.tagKey = tagKey;
this.frameKey = frameKey; this.segKey = segKey;
this.tagEverySegment = tagEverySegment; this.tagEverySegment = tagEverySegment;
iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize()); iv = IvEncoder.encodeIv(0L, segCipher.getBlockSize());
ciphertext = new byte[MAX_SEGMENT_LENGTH]; ciphertext = new byte[MAX_SEGMENT_LENGTH];
} }
@@ -49,8 +49,8 @@ class OutgoingEncryptionLayerImpl implements OutgoingEncryptionLayer {
IvEncoder.updateIv(iv, segmentNumber); IvEncoder.updateIv(iv, segmentNumber);
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
try { try {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
int encrypted = frameCipher.doFinal(plaintext, 0, length, int encrypted = segCipher.doFinal(plaintext, 0, length,
ciphertext, offset); ciphertext, offset);
if(encrypted != length) throw new RuntimeException(); if(encrypted != length) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) { } catch(GeneralSecurityException badCipher) {
@@ -59,7 +59,7 @@ class OutgoingEncryptionLayerImpl implements OutgoingEncryptionLayer {
try { try {
out.write(ciphertext, 0, offset + length); out.write(ciphertext, 0, offset + length);
} catch(IOException e) { } catch(IOException e) {
frameKey.erase(); segKey.erase();
tagKey.erase(); tagKey.erase();
throw e; throw e;
} }

View File

@@ -15,8 +15,8 @@ import net.sf.briar.api.transport.Segment;
class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer { class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer {
private final SegmentSink out; private final SegmentSink out;
private final Cipher tagCipher, frameCipher; private final Cipher tagCipher, segCipher;
private final ErasableKey tagKey, frameKey; private final ErasableKey tagKey, segKey;
private final boolean tagEverySegment; private final boolean tagEverySegment;
private final byte[] iv; private final byte[] iv;
private final Segment segment; private final Segment segment;
@@ -24,16 +24,16 @@ class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer {
private long capacity; private long capacity;
OutgoingSegmentedEncryptionLayer(SegmentSink out, long capacity, OutgoingSegmentedEncryptionLayer(SegmentSink out, long capacity,
Cipher tagCipher, Cipher frameCipher, ErasableKey tagKey, Cipher tagCipher, Cipher segCipher, ErasableKey tagKey,
ErasableKey frameKey, boolean tagEverySegment) { ErasableKey segKey, boolean tagEverySegment) {
this.out = out; this.out = out;
this.capacity = capacity; this.capacity = capacity;
this.tagCipher = tagCipher; this.tagCipher = tagCipher;
this.frameCipher = frameCipher; this.segCipher = segCipher;
this.tagKey = tagKey; this.tagKey = tagKey;
this.frameKey = frameKey; this.segKey = segKey;
this.tagEverySegment = tagEverySegment; this.tagEverySegment = tagEverySegment;
iv = IvEncoder.encodeIv(0L, frameCipher.getBlockSize()); iv = IvEncoder.encodeIv(0L, segCipher.getBlockSize());
segment = new SegmentImpl(); segment = new SegmentImpl();
} }
@@ -49,8 +49,8 @@ class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer {
IvEncoder.updateIv(iv, segmentNumber); IvEncoder.updateIv(iv, segmentNumber);
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
try { try {
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
int encrypted = frameCipher.doFinal(plaintext, 0, length, int encrypted = segCipher.doFinal(plaintext, 0, length,
ciphertext, offset); ciphertext, offset);
if(encrypted != length) throw new RuntimeException(); if(encrypted != length) throw new RuntimeException();
} catch(GeneralSecurityException badCipher) { } catch(GeneralSecurityException badCipher) {
@@ -60,7 +60,7 @@ class OutgoingSegmentedEncryptionLayer implements OutgoingEncryptionLayer {
try { try {
out.writeSegment(segment); out.writeSegment(segment);
} catch(IOException e) { } catch(IOException e) {
frameKey.erase(); segKey.erase();
tagKey.erase(); tagKey.erase();
throw e; throw e;
} }

View File

@@ -27,8 +27,8 @@ public class KeyDerivationTest extends BriarTestCase {
@Test @Test
public void testSixKeysAreDistinct() { public void testSixKeysAreDistinct() {
List<ErasableKey> keys = new ArrayList<ErasableKey>(); List<ErasableKey> keys = new ArrayList<ErasableKey>();
keys.add(crypto.deriveFrameKey(secret, true)); keys.add(crypto.deriveSegmentKey(secret, true));
keys.add(crypto.deriveFrameKey(secret, false)); keys.add(crypto.deriveSegmentKey(secret, false));
keys.add(crypto.deriveTagKey(secret, true)); keys.add(crypto.deriveTagKey(secret, true));
keys.add(crypto.deriveTagKey(secret, false)); keys.add(crypto.deriveTagKey(secret, false));
keys.add(crypto.deriveMacKey(secret, true)); keys.add(crypto.deriveMacKey(secret, true));

View File

@@ -27,26 +27,26 @@ import com.google.inject.Injector;
public class FrameReadWriteTest extends BriarTestCase { public class FrameReadWriteTest extends BriarTestCase {
private final CryptoComponent crypto; private final CryptoComponent crypto;
private final Cipher tagCipher, frameCipher; private final Cipher tagCipher, segCipher;
private final Mac mac;
private final Random random; private final Random random;
private final byte[] outSecret; private final byte[] outSecret;
private final ErasableKey tagKey, frameKey, macKey; private final ErasableKey tagKey, segKey, macKey;
private final Mac mac;
public FrameReadWriteTest() { public FrameReadWriteTest() {
super(); super();
Injector i = Guice.createInjector(new CryptoModule()); Injector i = Guice.createInjector(new CryptoModule());
crypto = i.getInstance(CryptoComponent.class); crypto = i.getInstance(CryptoComponent.class);
tagCipher = crypto.getTagCipher(); tagCipher = crypto.getTagCipher();
frameCipher = crypto.getFrameCipher(); segCipher = crypto.getSegmentCipher();
mac = crypto.getMac();
random = new Random(); random = new Random();
// Since we're sending frames to ourselves, we only need outgoing keys // Since we're sending frames to ourselves, we only need outgoing keys
outSecret = new byte[32]; outSecret = new byte[32];
random.nextBytes(outSecret); random.nextBytes(outSecret);
tagKey = crypto.deriveTagKey(outSecret, true); tagKey = crypto.deriveTagKey(outSecret, true);
frameKey = crypto.deriveFrameKey(outSecret, true); segKey = crypto.deriveSegmentKey(outSecret, true);
macKey = crypto.deriveMacKey(outSecret, true); macKey = crypto.deriveMacKey(outSecret, true);
mac = crypto.getMac();
} }
@Test @Test
@@ -69,13 +69,13 @@ public class FrameReadWriteTest extends BriarTestCase {
byte[] frame1 = new byte[321]; byte[] frame1 = new byte[321];
random.nextBytes(frame1); random.nextBytes(frame1);
// Copy the keys - the copies will be erased // Copy the keys - the copies will be erased
ErasableKey frameCopy = frameKey.copy();
ErasableKey tagCopy = tagKey.copy(); ErasableKey tagCopy = tagKey.copy();
ErasableKey segCopy = segKey.copy();
ErasableKey macCopy = macKey.copy(); ErasableKey macCopy = macKey.copy();
// Write the frames // Write the frames
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out, OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, tagCopy, frameCopy, Long.MAX_VALUE, tagCipher, segCipher, tagCopy, segCopy,
false); false);
OutgoingErrorCorrectionLayer correcter = OutgoingErrorCorrectionLayer correcter =
new NullOutgoingErrorCorrectionLayer(encrypter); new NullOutgoingErrorCorrectionLayer(encrypter);
@@ -94,7 +94,7 @@ public class FrameReadWriteTest extends BriarTestCase {
assertEquals(0L, TagEncoder.decodeTag(tag, tagCipher, tagKey)); assertEquals(0L, TagEncoder.decodeTag(tag, tagCipher, tagKey));
// Read the frames back // Read the frames back
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in, IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
tagCipher, frameCipher, tagKey, frameKey, false, recoveredTag); tagCipher, segCipher, tagKey, segKey, false, recoveredTag);
IncomingErrorCorrectionLayer correcter1 = IncomingErrorCorrectionLayer correcter1 =
new NullIncomingErrorCorrectionLayer(decrypter); new NullIncomingErrorCorrectionLayer(decrypter);
ConnectionReader reader = new ConnectionReaderImpl(correcter1, mac, ConnectionReader reader = new ConnectionReaderImpl(correcter1, mac,

View File

@@ -23,17 +23,17 @@ import com.google.inject.Injector;
public class IncomingEncryptionLayerImplTest extends BriarTestCase { public class IncomingEncryptionLayerImplTest extends BriarTestCase {
private final Cipher tagCipher, frameCipher; private final Cipher tagCipher, segCipher;
private final ErasableKey tagKey, frameKey; private final ErasableKey tagKey, segKey;
public IncomingEncryptionLayerImplTest() { public IncomingEncryptionLayerImplTest() {
super(); super();
Injector i = Guice.createInjector(new CryptoModule()); Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class); CryptoComponent crypto = i.getInstance(CryptoComponent.class);
tagCipher = crypto.getTagCipher(); tagCipher = crypto.getTagCipher();
frameCipher = crypto.getFrameCipher(); segCipher = crypto.getSegmentCipher();
tagKey = crypto.generateTestKey(); tagKey = crypto.generateTestKey();
frameKey = crypto.generateTestKey(); segKey = crypto.generateTestKey();
} }
@Test @Test
@@ -44,17 +44,17 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
// Calculate the ciphertext for the first segment // Calculate the ciphertext for the first segment
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH]; byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0); 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); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length); byte[] ciphertext = segCipher.doFinal(plaintext, 0, plaintext.length);
// Calculate the ciphertext for the second segment // Calculate the ciphertext for the second segment
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH]; byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0); HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
IvEncoder.updateIv(iv, 1L); IvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv); ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0, byte[] ciphertext1 = segCipher.doFinal(plaintext1, 0,
plaintext1.length); plaintext1.length);
// Concatenate the ciphertexts, excluding the first tag // Concatenate the ciphertexts, excluding the first tag
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -63,7 +63,7 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Use the encryption layer to decrypt the ciphertext // Use the encryption layer to decrypt the ciphertext
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in, IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
tagCipher, frameCipher, tagKey, frameKey, false, tag); tagCipher, segCipher, tagKey, segKey, false, tag);
// First segment // First segment
Segment s = new SegmentImpl(); Segment s = new SegmentImpl();
assertTrue(decrypter.readSegment(s)); assertTrue(decrypter.readSegment(s));
@@ -91,10 +91,10 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
// Calculate the ciphertext for the first segment // Calculate the ciphertext for the first segment
byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH]; byte[] plaintext = new byte[FRAME_HEADER_LENGTH + 123 + MAC_LENGTH];
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0); 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); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext = frameCipher.doFinal(plaintext, 0, plaintext.length); byte[] ciphertext = segCipher.doFinal(plaintext, 0, plaintext.length);
// Calculate the tag for the second segment // Calculate the tag for the second segment
byte[] tag1 = new byte[TAG_LENGTH]; byte[] tag1 = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey); TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey);
@@ -103,8 +103,8 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0); HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
IvEncoder.updateIv(iv, 1L); IvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv); ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0, byte[] ciphertext1 = segCipher.doFinal(plaintext1, 0,
plaintext1.length); plaintext1.length);
// Concatenate the ciphertexts, excluding the first tag // Concatenate the ciphertexts, excluding the first tag
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -114,7 +114,7 @@ public class IncomingEncryptionLayerImplTest extends BriarTestCase {
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
// Use the encryption layer to decrypt the ciphertext // Use the encryption layer to decrypt the ciphertext
IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in, IncomingEncryptionLayer decrypter = new IncomingEncryptionLayerImpl(in,
tagCipher, frameCipher, tagKey, frameKey, true, tag); tagCipher, segCipher, tagKey, segKey, true, tag);
// First segment // First segment
Segment s = new SegmentImpl(); Segment s = new SegmentImpl();
assertTrue(decrypter.readSegment(s)); assertTrue(decrypter.readSegment(s));

View File

@@ -23,17 +23,17 @@ import com.google.inject.Injector;
public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase { public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
private final Cipher tagCipher, frameCipher; private final Cipher tagCipher, segCipher;
private final ErasableKey tagKey, frameKey; private final ErasableKey tagKey, segKey;
public IncomingSegmentedEncryptionLayerTest() { public IncomingSegmentedEncryptionLayerTest() {
super(); super();
Injector i = Guice.createInjector(new CryptoModule()); Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class); CryptoComponent crypto = i.getInstance(CryptoComponent.class);
tagCipher = crypto.getTagCipher(); tagCipher = crypto.getTagCipher();
frameCipher = crypto.getFrameCipher(); segCipher = crypto.getSegmentCipher();
tagKey = crypto.generateTestKey(); tagKey = crypto.generateTestKey();
frameKey = crypto.generateTestKey(); segKey = crypto.generateTestKey();
} }
@Test @Test
@@ -43,18 +43,18 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0); HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
byte[] ciphertext = new byte[TAG_LENGTH + plaintext.length]; byte[] ciphertext = new byte[TAG_LENGTH + plaintext.length];
TagEncoder.encodeTag(ciphertext, 0L, tagCipher, tagKey); 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); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, segCipher.doFinal(plaintext, 0, plaintext.length, ciphertext,
TAG_LENGTH); TAG_LENGTH);
// Calculate the ciphertext for the second segment // Calculate the ciphertext for the second segment
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH]; byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH];
HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0); HeaderEncoder.encodeHeader(plaintext1, 1L, 1234, 0);
IvEncoder.updateIv(iv, 1L); IvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv); ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext1 = frameCipher.doFinal(plaintext1, 0, byte[] ciphertext1 = segCipher.doFinal(plaintext1, 0,
plaintext1.length); plaintext1.length);
// Buffer the first segment and create a source for the second // Buffer the first segment and create a source for the second
Segment buffered = new SegmentImpl(); Segment buffered = new SegmentImpl();
@@ -64,8 +64,8 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
SegmentSource in = new ByteArraySegmentSource(ciphertext1); SegmentSource in = new ByteArraySegmentSource(ciphertext1);
// Use the encryption layer to decrypt the ciphertext // Use the encryption layer to decrypt the ciphertext
IncomingEncryptionLayer decrypter = IncomingEncryptionLayer decrypter =
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher, new IncomingSegmentedEncryptionLayer(in, tagCipher, segCipher,
tagKey, frameKey, false, buffered); tagKey, segKey, false, buffered);
// First segment // First segment
Segment s = new SegmentImpl(); Segment s = new SegmentImpl();
assertTrue(decrypter.readSegment(s)); assertTrue(decrypter.readSegment(s));
@@ -92,10 +92,10 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0); HeaderEncoder.encodeHeader(plaintext, 0L, 123, 0);
byte[] ciphertext = new byte[TAG_LENGTH + plaintext.length]; byte[] ciphertext = new byte[TAG_LENGTH + plaintext.length];
TagEncoder.encodeTag(ciphertext, 0L, tagCipher, tagKey); 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); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, segCipher.doFinal(plaintext, 0, plaintext.length, ciphertext,
TAG_LENGTH); TAG_LENGTH);
// Calculate the ciphertext for the second frame, including its tag // Calculate the ciphertext for the second frame, including its tag
byte[] plaintext1 = new byte[FRAME_HEADER_LENGTH + 1234 + MAC_LENGTH]; 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); TagEncoder.encodeTag(ciphertext1, 1L, tagCipher, tagKey);
IvEncoder.updateIv(iv, 1L); IvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv); ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
frameCipher.doFinal(plaintext1, 0, plaintext1.length, ciphertext1, segCipher.doFinal(plaintext1, 0, plaintext1.length, ciphertext1,
TAG_LENGTH); TAG_LENGTH);
// Buffer the first segment and create a source for the second // Buffer the first segment and create a source for the second
Segment buffered = new SegmentImpl(); Segment buffered = new SegmentImpl();
@@ -115,8 +115,8 @@ public class IncomingSegmentedEncryptionLayerTest extends BriarTestCase {
SegmentSource in = new ByteArraySegmentSource(ciphertext1); SegmentSource in = new ByteArraySegmentSource(ciphertext1);
// Use the encryption layer to decrypt the ciphertext // Use the encryption layer to decrypt the ciphertext
IncomingEncryptionLayer decrypter = IncomingEncryptionLayer decrypter =
new IncomingSegmentedEncryptionLayer(in, tagCipher, frameCipher, new IncomingSegmentedEncryptionLayer(in, tagCipher, segCipher,
tagKey, frameKey, true, buffered); tagKey, segKey, true, buffered);
// First segment // First segment
Segment s = new SegmentImpl(); Segment s = new SegmentImpl();
assertTrue(decrypter.readSegment(s)); assertTrue(decrypter.readSegment(s));

View File

@@ -23,17 +23,17 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
private static final int MAC_LENGTH = 32; private static final int MAC_LENGTH = 32;
private final Cipher tagCipher, frameCipher; private final Cipher tagCipher, segCipher;
private final ErasableKey tagKey, frameKey; private final ErasableKey tagKey, segKey;
public OutgoingEncryptionLayerImplTest() { public OutgoingEncryptionLayerImplTest() {
super(); super();
Injector i = Guice.createInjector(new CryptoModule()); Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class); CryptoComponent crypto = i.getInstance(CryptoComponent.class);
tagCipher = crypto.getTagCipher(); tagCipher = crypto.getTagCipher();
frameCipher = crypto.getFrameCipher(); segCipher = crypto.getSegmentCipher();
tagKey = crypto.generateTestKey(); tagKey = crypto.generateTestKey();
frameKey = crypto.generateTestKey(); segKey = crypto.generateTestKey();
} }
@Test @Test
@@ -42,17 +42,17 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
byte[] tag = new byte[TAG_LENGTH]; byte[] tag = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey); TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
// Calculate the expected ciphertext for the first segment // 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]; byte[] plaintext = new byte[123 + MAC_LENGTH];
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext = frameCipher.doFinal(plaintext); byte[] ciphertext = segCipher.doFinal(plaintext);
// Calculate the expected ciphertext for the second segment // Calculate the expected ciphertext for the second segment
byte[] plaintext1 = new byte[1234 + MAC_LENGTH]; byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
IvEncoder.updateIv(iv, 1L); IvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv); ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext1 = frameCipher.doFinal(plaintext1); byte[] ciphertext1 = segCipher.doFinal(plaintext1);
// Concatenate the ciphertexts // Concatenate the ciphertexts
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(tag); out.write(tag);
@@ -62,7 +62,7 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
// Use the encryption layer to encrypt the plaintext // Use the encryption layer to encrypt the plaintext
out.reset(); out.reset();
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out, OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out,
Long.MAX_VALUE, tagCipher, frameCipher, tagKey, frameKey, Long.MAX_VALUE, tagCipher, segCipher, tagKey, segKey,
false); false);
Segment s = new SegmentImpl(); Segment s = new SegmentImpl();
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length); System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
@@ -86,11 +86,11 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
byte[] tag = new byte[TAG_LENGTH]; byte[] tag = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey); TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
// Calculate the expected ciphertext for the first segment // 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]; byte[] plaintext = new byte[123 + MAC_LENGTH];
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext = frameCipher.doFinal(plaintext); byte[] ciphertext = segCipher.doFinal(plaintext);
// Calculate the expected tag for the second segment // Calculate the expected tag for the second segment
byte[] tag1 = new byte[TAG_LENGTH]; byte[] tag1 = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey); TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey);
@@ -98,8 +98,8 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
byte[] plaintext1 = new byte[1234 + MAC_LENGTH]; byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
IvEncoder.updateIv(iv, 1L); IvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv); ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext1 = frameCipher.doFinal(plaintext1); byte[] ciphertext1 = segCipher.doFinal(plaintext1);
// Concatenate the ciphertexts // Concatenate the ciphertexts
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(tag); out.write(tag);
@@ -110,7 +110,7 @@ public class OutgoingEncryptionLayerImplTest extends BriarTestCase {
// Use the encryption layer to encrypt the plaintext // Use the encryption layer to encrypt the plaintext
out.reset(); out.reset();
OutgoingEncryptionLayer encrypter = new OutgoingEncryptionLayerImpl(out, 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(); Segment s = new SegmentImpl();
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length); System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
s.setLength(plaintext.length); s.setLength(plaintext.length);

View File

@@ -25,17 +25,17 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
private static final int MAC_LENGTH = 32; private static final int MAC_LENGTH = 32;
private final Cipher tagCipher, frameCipher; private final Cipher tagCipher, segCipher;
private final ErasableKey tagKey, frameKey; private final ErasableKey tagKey, segKey;
public OutgoingSegmentedEncryptionLayerTest() { public OutgoingSegmentedEncryptionLayerTest() {
super(); super();
Injector i = Guice.createInjector(new CryptoModule()); Injector i = Guice.createInjector(new CryptoModule());
CryptoComponent crypto = i.getInstance(CryptoComponent.class); CryptoComponent crypto = i.getInstance(CryptoComponent.class);
tagCipher = crypto.getTagCipher(); tagCipher = crypto.getTagCipher();
frameCipher = crypto.getFrameCipher(); segCipher = crypto.getSegmentCipher();
tagKey = crypto.generateTestKey(); tagKey = crypto.generateTestKey();
frameKey = crypto.generateTestKey(); segKey = crypto.generateTestKey();
} }
@Test @Test
@@ -44,17 +44,17 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
byte[] tag = new byte[TAG_LENGTH]; byte[] tag = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey); TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
// Calculate the expected ciphertext for the first segment // 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]; byte[] plaintext = new byte[123 + MAC_LENGTH];
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext = frameCipher.doFinal(plaintext); byte[] ciphertext = segCipher.doFinal(plaintext);
// Calculate the expected ciphertext for the second segment // Calculate the expected ciphertext for the second segment
byte[] plaintext1 = new byte[1234 + MAC_LENGTH]; byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
IvEncoder.updateIv(iv, 1L); IvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv); ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext1 = frameCipher.doFinal(plaintext1); byte[] ciphertext1 = segCipher.doFinal(plaintext1);
// Concatenate the ciphertexts // Concatenate the ciphertexts
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(tag); out.write(tag);
@@ -65,7 +65,7 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
ByteArraySegmentSink sink = new ByteArraySegmentSink(); ByteArraySegmentSink sink = new ByteArraySegmentSink();
OutgoingEncryptionLayer encrypter = OutgoingEncryptionLayer encrypter =
new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE, new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE,
tagCipher, frameCipher, tagKey, frameKey, false); tagCipher, segCipher, tagKey, segKey, false);
Segment s = new SegmentImpl(); Segment s = new SegmentImpl();
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length); System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
s.setLength(plaintext.length); s.setLength(plaintext.length);
@@ -88,11 +88,11 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
byte[] tag = new byte[TAG_LENGTH]; byte[] tag = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey); TagEncoder.encodeTag(tag, 0L, tagCipher, tagKey);
// Calculate the expected ciphertext for the first segment // 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]; byte[] plaintext = new byte[123 + MAC_LENGTH];
IvParameterSpec ivSpec = new IvParameterSpec(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext = frameCipher.doFinal(plaintext); byte[] ciphertext = segCipher.doFinal(plaintext);
// Calculate the expected tag for the second segment // Calculate the expected tag for the second segment
byte[] tag1 = new byte[TAG_LENGTH]; byte[] tag1 = new byte[TAG_LENGTH];
TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey); TagEncoder.encodeTag(tag1, 1L, tagCipher, tagKey);
@@ -100,8 +100,8 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
byte[] plaintext1 = new byte[1234 + MAC_LENGTH]; byte[] plaintext1 = new byte[1234 + MAC_LENGTH];
IvEncoder.updateIv(iv, 1L); IvEncoder.updateIv(iv, 1L);
ivSpec = new IvParameterSpec(iv); ivSpec = new IvParameterSpec(iv);
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec); segCipher.init(Cipher.ENCRYPT_MODE, segKey, ivSpec);
byte[] ciphertext1 = frameCipher.doFinal(plaintext1); byte[] ciphertext1 = segCipher.doFinal(plaintext1);
// Concatenate the ciphertexts // Concatenate the ciphertexts
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(tag); out.write(tag);
@@ -113,7 +113,7 @@ public class OutgoingSegmentedEncryptionLayerTest extends BriarTestCase {
SegmentSink sink = new ByteArraySegmentSink(); SegmentSink sink = new ByteArraySegmentSink();
OutgoingEncryptionLayer encrypter = OutgoingEncryptionLayer encrypter =
new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE, new OutgoingSegmentedEncryptionLayer(sink, Long.MAX_VALUE,
tagCipher, frameCipher, tagKey, frameKey, true); tagCipher, segCipher, tagKey, segKey, true);
Segment s = new SegmentImpl(); Segment s = new SegmentImpl();
System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length); System.arraycopy(plaintext, 0, s.getBuffer(), 0, plaintext.length);
s.setLength(plaintext.length); s.setLength(plaintext.length);