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