Replaced AuthenticatedCipher opmode with a boolean.

This commit is contained in:
akwizgran
2014-01-16 18:56:24 +00:00
parent 4ac85e955f
commit e5353dc6d4
7 changed files with 15 additions and 36 deletions

View File

@@ -9,7 +9,7 @@ public interface AuthenticatedCipher {
* Initializes this cipher with a key, an initialisation vector (IV) and * Initializes this cipher with a key, an initialisation vector (IV) and
* additional authenticated data (AAD). * additional authenticated data (AAD).
*/ */
void init(int opmode, SecretKey key, byte[] iv, byte[] aad) void init(boolean encrypt, SecretKey key, byte[] iv, byte[] aad)
throws GeneralSecurityException; throws GeneralSecurityException;
/** Encrypts or decrypts data in a single-part operation. */ /** Encrypts or decrypts data in a single-part operation. */

View File

@@ -2,11 +2,8 @@ package org.briarproject.crypto;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import javax.crypto.Cipher;
import org.briarproject.api.crypto.AuthenticatedCipher; import org.briarproject.api.crypto.AuthenticatedCipher;
import org.briarproject.api.crypto.SecretKey; import org.briarproject.api.crypto.SecretKey;
import org.spongycastle.crypto.DataLengthException; import org.spongycastle.crypto.DataLengthException;
import org.spongycastle.crypto.InvalidCipherTextException; import org.spongycastle.crypto.InvalidCipherTextException;
import org.spongycastle.crypto.modes.AEADBlockCipher; import org.spongycastle.crypto.modes.AEADBlockCipher;
@@ -39,23 +36,12 @@ class AuthenticatedCipherImpl implements AuthenticatedCipher {
} }
} }
public void init(int opmode, SecretKey key, byte[] iv, byte[] aad) public void init(boolean encrypt, SecretKey key, byte[] iv, byte[] aad)
throws GeneralSecurityException { throws GeneralSecurityException {
KeyParameter k = new KeyParameter(key.getEncoded()); KeyParameter k = new KeyParameter(key.getEncoded());
AEADParameters params = new AEADParameters(k, macLength * 8, iv, aad); AEADParameters params = new AEADParameters(k, macLength * 8, iv, aad);
try { try {
switch(opmode) { cipher.init(encrypt, params);
case Cipher.ENCRYPT_MODE:
case Cipher.WRAP_MODE:
cipher.init(true, params);
break;
case Cipher.DECRYPT_MODE:
case Cipher.UNWRAP_MODE:
cipher.init(false, params);
break;
default:
throw new IllegalArgumentException();
}
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {
throw new GeneralSecurityException(e.getMessage()); throw new GeneralSecurityException(e.getMessage());
} }

View File

@@ -1,8 +1,6 @@
package org.briarproject.crypto; package org.briarproject.crypto;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static javax.crypto.Cipher.DECRYPT_MODE;
import static javax.crypto.Cipher.ENCRYPT_MODE;
import static org.briarproject.api.invitation.InvitationConstants.CODE_BITS; import static org.briarproject.api.invitation.InvitationConstants.CODE_BITS;
import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH; import static org.briarproject.api.transport.TransportConstants.TAG_LENGTH;
import static org.briarproject.crypto.EllipticCurveConstants.P; import static org.briarproject.crypto.EllipticCurveConstants.P;
@@ -238,7 +236,6 @@ class CryptoComponentImpl implements CryptoComponent {
ECPublicKeyParameters ecPub = ((Sec1PublicKey) pub).getKey(); ECPublicKeyParameters ecPub = ((Sec1PublicKey) pub).getKey();
ECDHCBasicAgreement agreement = new ECDHCBasicAgreement(); ECDHCBasicAgreement agreement = new ECDHCBasicAgreement();
agreement.init(ecPriv); agreement.init(ecPriv);
// FIXME: Should we use another format for the shared secret?
return agreement.calculateAgreement(ecPub).toByteArray(); return agreement.calculateAgreement(ecPub).toByteArray();
} }
@@ -305,8 +302,8 @@ class CryptoComponentImpl implements CryptoComponent {
} }
public AuthenticatedCipher getFrameCipher() { public AuthenticatedCipher getFrameCipher() {
AEADBlockCipher cipher = new GCMBlockCipher(new AESLightEngine()); AEADBlockCipher a = new GCMBlockCipher(new AESLightEngine());
return new AuthenticatedCipherImpl(cipher, MAC_BYTES); return new AuthenticatedCipherImpl(a, MAC_BYTES);
} }
public void encodeTag(byte[] tag, SecretKey tagKey, long connection) { public void encodeTag(byte[] tag, SecretKey tagKey, long connection) {
@@ -343,10 +340,10 @@ class CryptoComponentImpl implements CryptoComponent {
System.arraycopy(iv, 0, output, salt.length + 4, iv.length); System.arraycopy(iv, 0, output, salt.length + 4, iv.length);
// Initialise the cipher and encrypt the plaintext // Initialise the cipher and encrypt the plaintext
try { try {
AEADBlockCipher c = new GCMBlockCipher(new AESLightEngine()); AEADBlockCipher a = new GCMBlockCipher(new AESLightEngine());
AuthenticatedCipher cipher = new AuthenticatedCipherImpl(c, AuthenticatedCipher cipher = new AuthenticatedCipherImpl(a,
MAC_BYTES); MAC_BYTES);
cipher.init(ENCRYPT_MODE, key, iv, null); cipher.init(true, key, iv, null);
int outputOff = salt.length + 4 + iv.length; int outputOff = salt.length + 4 + iv.length;
cipher.doFinal(input, 0, input.length, output, outputOff); cipher.doFinal(input, 0, input.length, output, outputOff);
return output; return output;
@@ -374,9 +371,9 @@ class CryptoComponentImpl implements CryptoComponent {
// Initialise the cipher // Initialise the cipher
AuthenticatedCipher cipher; AuthenticatedCipher cipher;
try { try {
AEADBlockCipher c = new GCMBlockCipher(new AESLightEngine()); AEADBlockCipher a = new GCMBlockCipher(new AESLightEngine());
cipher = new AuthenticatedCipherImpl(c, MAC_BYTES); cipher = new AuthenticatedCipherImpl(a, MAC_BYTES);
cipher.init(DECRYPT_MODE, key, iv, null); cipher.init(false, key, iv, null);
} catch(GeneralSecurityException e) { } catch(GeneralSecurityException e) {
key.erase(); key.erase();
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@@ -1,6 +1,5 @@
package org.briarproject.transport; package org.briarproject.transport;
import static javax.crypto.Cipher.DECRYPT_MODE;
import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH; import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH; import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH; import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
@@ -60,7 +59,7 @@ class IncomingEncryptionLayer implements FrameReader {
FrameEncoder.encodeIv(iv, frameNumber); FrameEncoder.encodeIv(iv, frameNumber);
FrameEncoder.encodeAad(aad, frameNumber, plaintextLength); FrameEncoder.encodeAad(aad, frameNumber, plaintextLength);
try { try {
frameCipher.init(DECRYPT_MODE, frameKey, iv, aad); frameCipher.init(false, frameKey, iv, aad);
int decrypted = frameCipher.doFinal(ciphertext, 0, ciphertextLength, int decrypted = frameCipher.doFinal(ciphertext, 0, ciphertextLength,
frame, 0); frame, 0);
if(decrypted != plaintextLength) throw new RuntimeException(); if(decrypted != plaintextLength) throw new RuntimeException();

View File

@@ -1,6 +1,5 @@
package org.briarproject.transport; package org.briarproject.transport;
import static javax.crypto.Cipher.ENCRYPT_MODE;
import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH; import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH; import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH; import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
@@ -97,7 +96,7 @@ class OutgoingEncryptionLayer implements FrameWriter {
FrameEncoder.encodeIv(iv, frameNumber); FrameEncoder.encodeIv(iv, frameNumber);
FrameEncoder.encodeAad(aad, frameNumber, plaintextLength); FrameEncoder.encodeAad(aad, frameNumber, plaintextLength);
try { try {
frameCipher.init(ENCRYPT_MODE, frameKey, iv, aad); frameCipher.init(true, frameKey, iv, aad);
int encrypted = frameCipher.doFinal(frame, 0, plaintextLength, int encrypted = frameCipher.doFinal(frame, 0, plaintextLength,
ciphertext, 0); ciphertext, 0);
if(encrypted != ciphertextLength) throw new RuntimeException(); if(encrypted != ciphertextLength) throw new RuntimeException();

View File

@@ -1,6 +1,5 @@
package org.briarproject.transport; package org.briarproject.transport;
import static javax.crypto.Cipher.ENCRYPT_MODE;
import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH; import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH; import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH; import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
@@ -175,7 +174,7 @@ public class IncomingEncryptionLayerTest extends BriarTestCase {
byte[] ciphertext = new byte[frameLength]; byte[] ciphertext = new byte[frameLength];
FrameEncoder.encodeIv(iv, frameNumber); FrameEncoder.encodeIv(iv, frameNumber);
FrameEncoder.encodeAad(aad, frameNumber, plaintext.length); FrameEncoder.encodeAad(aad, frameNumber, plaintext.length);
frameCipher.init(ENCRYPT_MODE, frameKey, iv, aad); frameCipher.init(true, frameKey, iv, aad);
FrameEncoder.encodeHeader(plaintext, finalFrame, payloadLength); FrameEncoder.encodeHeader(plaintext, finalFrame, payloadLength);
if(badPadding) plaintext[HEADER_LENGTH + payloadLength] = 1; if(badPadding) plaintext[HEADER_LENGTH + payloadLength] = 1;
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0); frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);

View File

@@ -1,6 +1,5 @@
package org.briarproject.transport; package org.briarproject.transport;
import static javax.crypto.Cipher.ENCRYPT_MODE;
import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH; import static org.briarproject.api.transport.TransportConstants.AAD_LENGTH;
import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH; import static org.briarproject.api.transport.TransportConstants.HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.IV_LENGTH; import static org.briarproject.api.transport.TransportConstants.IV_LENGTH;
@@ -51,7 +50,7 @@ public class OutgoingEncryptionLayerTest extends BriarTestCase {
// Calculate the expected ciphertext // Calculate the expected ciphertext
FrameEncoder.encodeIv(iv, 0); FrameEncoder.encodeIv(iv, 0);
FrameEncoder.encodeAad(aad, 0, plaintext.length); FrameEncoder.encodeAad(aad, 0, plaintext.length);
frameCipher.init(ENCRYPT_MODE, frameKey, iv, aad); frameCipher.init(true, frameKey, iv, aad);
FrameEncoder.encodeHeader(plaintext, false, payloadLength); FrameEncoder.encodeHeader(plaintext, false, payloadLength);
frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0); frameCipher.doFinal(plaintext, 0, plaintext.length, ciphertext, 0);
// Check that the actual tag and ciphertext match what's expected // Check that the actual tag and ciphertext match what's expected