mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Minor refactoring.
This commit is contained in:
@@ -9,6 +9,6 @@ interface ConnectionDecrypter {
|
||||
/** Returns an input stream from which decrypted data can be read. */
|
||||
InputStream getInputStream();
|
||||
|
||||
/** Reads and decrypts the MAC for the current frame. */
|
||||
void readMac(byte[] mac) throws IOException;
|
||||
/** Reads and decrypts the remainder of the current frame. */
|
||||
void readFinal(byte[] b) throws IOException;
|
||||
}
|
||||
|
||||
@@ -6,13 +6,9 @@ import java.io.EOFException;
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.ShortBufferException;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
@@ -41,29 +37,25 @@ implements ConnectionDecrypter {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void readMac(byte[] mac) throws IOException {
|
||||
public void readFinal(byte[] b) throws IOException {
|
||||
try {
|
||||
if(betweenFrames) throw new IllegalStateException();
|
||||
// If we have any plaintext in the buffer, copy it into the MAC
|
||||
System.arraycopy(buf, bufOff, mac, 0, bufLen);
|
||||
// Read the remainder of the MAC
|
||||
// If we have any plaintext in the buffer, copy it into the frame
|
||||
System.arraycopy(buf, bufOff, b, 0, bufLen);
|
||||
// Read the remainder of the frame
|
||||
int offset = bufLen;
|
||||
while(offset < mac.length) {
|
||||
int read = in.read(mac, offset, mac.length - offset);
|
||||
while(offset < b.length) {
|
||||
int read = in.read(b, offset, b.length - offset);
|
||||
if(read == -1) break;
|
||||
offset += read;
|
||||
}
|
||||
if(offset < mac.length) throw new EOFException(); // Unexpected EOF
|
||||
// Decrypt the remainder of the MAC
|
||||
if(offset < b.length) throw new EOFException(); // Unexpected EOF
|
||||
// Decrypt the remainder of the frame
|
||||
try {
|
||||
int length = mac.length - bufLen;
|
||||
int i = frameCipher.doFinal(mac, bufLen, length, mac, bufLen);
|
||||
int length = b.length - bufLen;
|
||||
int i = frameCipher.doFinal(b, bufLen, length, b, bufLen);
|
||||
if(i < length) throw new RuntimeException();
|
||||
} catch(BadPaddingException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
} catch(IllegalBlockSizeException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
} catch(ShortBufferException badCipher) {
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
bufOff = bufLen = 0;
|
||||
@@ -140,7 +132,7 @@ implements ConnectionDecrypter {
|
||||
try {
|
||||
int i = frameCipher.update(buf, 0, offset, buf);
|
||||
if(i < offset) throw new RuntimeException();
|
||||
} catch(ShortBufferException badCipher) {
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
return true;
|
||||
@@ -153,10 +145,8 @@ implements ConnectionDecrypter {
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
try {
|
||||
frameCipher.init(Cipher.DECRYPT_MODE, frameKey, ivSpec);
|
||||
} catch(InvalidAlgorithmParameterException badIv) {
|
||||
throw new RuntimeException(badIv);
|
||||
} catch(InvalidKeyException badKey) {
|
||||
throw new RuntimeException(badKey);
|
||||
} catch(GeneralSecurityException badIvOrKey) {
|
||||
throw new RuntimeException(badIvOrKey);
|
||||
}
|
||||
frame++;
|
||||
betweenFrames = false;
|
||||
|
||||
@@ -9,8 +9,8 @@ interface ConnectionEncrypter {
|
||||
/** Returns an output stream to which unencrypted data can be written. */
|
||||
OutputStream getOutputStream();
|
||||
|
||||
/** Encrypts and writes the MAC for the current frame. */
|
||||
void writeMac(byte[] mac) throws IOException;
|
||||
/** Encrypts and writes the remainder of the current frame. */
|
||||
void writeFinal(byte[] b) throws IOException;
|
||||
|
||||
/** Returns the maximum number of bytes that can be written. */
|
||||
long getRemainingCapacity();
|
||||
|
||||
@@ -6,15 +6,13 @@ import static net.sf.briar.util.ByteUtils.MAX_32_BIT_UNSIGNED;
|
||||
import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
|
||||
import net.sf.briar.api.crypto.ErasableKey;
|
||||
|
||||
class ConnectionEncrypterImpl extends FilterOutputStream
|
||||
implements ConnectionEncrypter {
|
||||
|
||||
@@ -42,17 +40,15 @@ implements ConnectionEncrypter {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void writeMac(byte[] mac) throws IOException {
|
||||
public void writeFinal(byte[] b) throws IOException {
|
||||
try {
|
||||
if(!tagWritten || betweenFrames) throw new IllegalStateException();
|
||||
try {
|
||||
out.write(frameCipher.doFinal(mac));
|
||||
} catch(BadPaddingException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
} catch(IllegalBlockSizeException badCipher) {
|
||||
out.write(frameCipher.doFinal(b));
|
||||
} catch(GeneralSecurityException badCipher) {
|
||||
throw new RuntimeException(badCipher);
|
||||
}
|
||||
capacity -= mac.length;
|
||||
capacity -= b.length;
|
||||
betweenFrames = true;
|
||||
} catch(IOException e) {
|
||||
frameKey.erase();
|
||||
@@ -114,10 +110,8 @@ implements ConnectionEncrypter {
|
||||
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||
try {
|
||||
frameCipher.init(Cipher.ENCRYPT_MODE, frameKey, ivSpec);
|
||||
} catch(InvalidAlgorithmParameterException badIv) {
|
||||
throw new RuntimeException(badIv);
|
||||
} catch(InvalidKeyException badKey) {
|
||||
throw new RuntimeException(badKey);
|
||||
} catch(GeneralSecurityException badIvOrKey) {
|
||||
throw new RuntimeException(badIvOrKey);
|
||||
}
|
||||
frame++;
|
||||
betweenFrames = false;
|
||||
|
||||
@@ -128,7 +128,7 @@ implements ConnectionReader {
|
||||
}
|
||||
// Read the MAC
|
||||
byte[] expectedMac = mac.doFinal();
|
||||
decrypter.readMac(footer);
|
||||
decrypter.readFinal(footer);
|
||||
if(!Arrays.equals(expectedMac, footer)) throw new FormatException();
|
||||
frame++;
|
||||
if(payloadLen > 0) betweenFrames = false;
|
||||
|
||||
@@ -103,7 +103,7 @@ implements ConnectionWriter {
|
||||
mac.update(header);
|
||||
out.write(payload);
|
||||
mac.update(payload);
|
||||
encrypter.writeMac(mac.doFinal());
|
||||
encrypter.writeFinal(mac.doFinal());
|
||||
frame++;
|
||||
buf.reset();
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ class PaddedConnectionWriter extends ConnectionWriterImpl {
|
||||
mac.update(payload);
|
||||
out.write(padding, 0, paddingLength);
|
||||
mac.update(padding, 0, paddingLength);
|
||||
encrypter.writeMac(mac.doFinal());
|
||||
encrypter.writeFinal(mac.doFinal());
|
||||
frame++;
|
||||
buf.reset();
|
||||
}
|
||||
|
||||
@@ -80,12 +80,12 @@ public class ConnectionDecrypterImplTest extends TestCase {
|
||||
byte[] decrypted = new byte[ciphertext.length];
|
||||
TestUtils.readFully(d.getInputStream(), decrypted);
|
||||
byte[] decryptedMac = new byte[MAC_LENGTH];
|
||||
d.readMac(decryptedMac);
|
||||
d.readFinal(decryptedMac);
|
||||
// Second frame
|
||||
byte[] decrypted1 = new byte[ciphertext1.length];
|
||||
TestUtils.readFully(d.getInputStream(), decrypted1);
|
||||
byte[] decryptedMac1 = new byte[MAC_LENGTH];
|
||||
d.readMac(decryptedMac1);
|
||||
d.readFinal(decryptedMac1);
|
||||
// Check that the actual plaintext matches the expected plaintext
|
||||
out.reset();
|
||||
out.write(plaintext);
|
||||
|
||||
@@ -79,9 +79,9 @@ public class ConnectionEncrypterImplTest extends TestCase {
|
||||
ConnectionEncrypter e = new ConnectionEncrypterImpl(out, Long.MAX_VALUE,
|
||||
tagCipher, frameCipher, tagKey, frameKey);
|
||||
e.getOutputStream().write(plaintext);
|
||||
e.writeMac(plaintextMac);
|
||||
e.writeFinal(plaintextMac);
|
||||
e.getOutputStream().write(plaintext1);
|
||||
e.writeMac(plaintextMac);
|
||||
e.writeFinal(plaintextMac);
|
||||
byte[] actual = out.toByteArray();
|
||||
// Check that the actual ciphertext matches the expected ciphertext
|
||||
assertArrayEquals(expected, actual);
|
||||
|
||||
@@ -17,7 +17,7 @@ class NullConnectionDecrypter implements ConnectionDecrypter {
|
||||
return in;
|
||||
}
|
||||
|
||||
public void readMac(byte[] mac) throws IOException {
|
||||
public void readFinal(byte[] mac) throws IOException {
|
||||
int offset = 0;
|
||||
while(offset < mac.length) {
|
||||
int read = in.read(mac, offset, mac.length - offset);
|
||||
|
||||
@@ -23,7 +23,7 @@ implements ConnectionEncrypter {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void writeMac(byte[] mac) throws IOException {
|
||||
public void writeFinal(byte[] mac) throws IOException {
|
||||
out.write(mac);
|
||||
capacity -= mac.length;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user