PacketWriter is implemented by two classes: PacketWriterImpl and

PacketEncrypter. The separation allows authentication and encryption
to be tested separately.
This commit is contained in:
akwizgran
2011-08-09 17:50:54 +01:00
parent e9d0021f56
commit f3f0c223c4
9 changed files with 274 additions and 13 deletions

View File

@@ -8,8 +8,10 @@ import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.Signature;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import net.sf.briar.api.crypto.CryptoComponent;
@@ -24,9 +26,11 @@ class CryptoComponentImpl implements CryptoComponent {
private static final String KEY_PAIR_ALGO = "ECDSA";
private static final int KEY_PAIR_KEYSIZE = 256; // Bits
private static final String MAC_ALGO = "HMacSHA256";
private static final String PACKET_CIPHER_ALGO = "AES/CTR/NoPadding";
private static final String SECRET_KEY_ALGO = "AES";
private static final int SECRET_KEY_KEYSIZE = 256; // Bits
private static final String SIGNATURE_ALGO = "ECDSA";
private static final String TAG_CIPHER_ALGO = "AES/ECB/NoPadding";
private final KeyParser keyParser;
private final KeyPairGenerator keyPairGenerator;
@@ -81,6 +85,18 @@ class CryptoComponentImpl implements CryptoComponent {
}
}
public Cipher getPacketCipher() {
try {
return Cipher.getInstance(PACKET_CIPHER_ALGO, PROVIDER);
} catch(NoSuchAlgorithmException impossible) {
throw new RuntimeException(impossible);
} catch(NoSuchPaddingException impossible) {
throw new RuntimeException(impossible);
} catch(NoSuchProviderException impossible) {
throw new RuntimeException(impossible);
}
}
public Signature getSignature() {
try {
return Signature.getInstance(SIGNATURE_ALGO, PROVIDER);
@@ -90,4 +106,16 @@ class CryptoComponentImpl implements CryptoComponent {
throw new RuntimeException(impossible);
}
}
public Cipher getTagCipher() {
try {
return Cipher.getInstance(TAG_CIPHER_ALGO, PROVIDER);
} catch(NoSuchAlgorithmException impossible) {
throw new RuntimeException(impossible);
} catch(NoSuchPaddingException impossible) {
throw new RuntimeException(impossible);
} catch(NoSuchProviderException impossible) {
throw new RuntimeException(impossible);
}
}
}