Use a constant for the tag size.

This commit is contained in:
akwizgran
2011-08-12 14:26:56 +02:00
parent f0cf825ca9
commit 68b4760dfa
11 changed files with 43 additions and 33 deletions

View File

@@ -16,7 +16,7 @@ import javax.crypto.spec.IvParameterSpec;
*/
class SharedSecret {
private static final int IV_BYTES = 16;
static final int IV_BYTES = 16;
private final IvParameterSpec iv;
private final boolean alice;

View File

@@ -92,7 +92,8 @@ DatabaseListener {
public synchronized ContactId acceptConnection(byte[] tag)
throws DbException {
if(tag.length != 16) throw new IllegalArgumentException();
if(tag.length != Constants.TAG_BYTES)
throw new IllegalArgumentException();
if(!initialised) initialise();
Bytes b = new Bytes(tag);
ContactId contactId = tagToContact.remove(b);

View File

@@ -2,7 +2,7 @@ package net.sf.briar.transport;
interface Constants {
static final int TAG_BYTES = 16;
static final int MAX_16_BIT_UNSIGNED = 65535; // 2^16 - 1
static final long MAX_32_BIT_UNSIGNED = 4294967295L; // 2^32 - 1
}

View File

@@ -5,9 +5,12 @@ import java.io.OutputStream;
interface PacketEncrypter {
/** Returns the output stream to which packets should be written. */
OutputStream getOutputStream();
/** Encrypts the given tag and writes it to the underlying output stream. */
void writeTag(byte[] tag) throws IOException;
/** Finishes writing the current packet. */
void finishPacket() throws IOException;
}

View File

@@ -15,14 +15,12 @@ import javax.crypto.spec.IvParameterSpec;
class PacketEncrypterImpl extends FilterOutputStream
implements PacketEncrypter {
private final OutputStream out;
private final Cipher tagCipher, packetCipher;
private final SecretKey packetKey;
PacketEncrypterImpl(OutputStream out, Cipher tagCipher,
Cipher packetCipher, SecretKey tagKey, SecretKey packetKey) {
super(out);
this.out = out;
this.tagCipher = tagCipher;
this.packetCipher = packetCipher;
this.packetKey = packetKey;
@@ -31,7 +29,7 @@ implements PacketEncrypter {
} catch(InvalidKeyException e) {
throw new IllegalArgumentException(e);
}
if(tagCipher.getOutputSize(16) != 16)
if(tagCipher.getOutputSize(Constants.TAG_BYTES) != Constants.TAG_BYTES)
throw new IllegalArgumentException();
}
@@ -40,7 +38,8 @@ implements PacketEncrypter {
}
public void writeTag(byte[] tag) throws IOException {
if(tag.length != 16) throw new IllegalArgumentException();
if(tag.length != Constants.TAG_BYTES)
throw new IllegalArgumentException();
IvParameterSpec iv = new IvParameterSpec(tag);
try {
out.write(tagCipher.doFinal(tag));

View File

@@ -4,7 +4,7 @@ public class TagEncoder {
static byte[] encodeTag(int transportIdentifier, long connectionNumber,
long packetNumber) {
byte[] tag = new byte[16];
byte[] tag = new byte[Constants.TAG_BYTES];
// Encode the transport identifier as an unsigned 16-bit integer
writeUint16(transportIdentifier, tag, 2);
// Encode the connection number as an unsigned 32-bit integer