summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorKenny Root <kroot@google.com>2013-09-10 14:46:43 -0700
committerKenny Root <kroot@google.com>2013-09-11 12:56:37 -0700
commite884f65168ea49f85d15a4d7d810904a33a1a22e (patch)
tree6195bb85e54f1c396a5d8e0c46535d2078ad4e0e /crypto
parent1b2c7f9fb9a7fa8d3243bf6a767cefdc354d410a (diff)
downloadlibcore-e884f65168ea49f85d15a4d7d810904a33a1a22e.zip
libcore-e884f65168ea49f85d15a4d7d810904a33a1a22e.tar.gz
libcore-e884f65168ea49f85d15a4d7d810904a33a1a22e.tar.bz2
Return IvParameters in OpenSSLCipher#getParameters
The getParameters() call was unimplemented in the OpenSSLCipher as an oversight. Add it so code relying on it will continue to work. Additionally add tests for getIV() and getParameters() to make sure they work correctly. (cherry picked from commit 8d59a14a150738b8b3a2a8c31d1a48b8ae0a3d0c) Bug: 10423926 Change-Id: I6bc7fc540509242dff9e5411f66f82be54691cb4
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/main/java/org/conscrypt/OpenSSLCipher.java49
1 files changed, 44 insertions, 5 deletions
diff --git a/crypto/src/main/java/org/conscrypt/OpenSSLCipher.java b/crypto/src/main/java/org/conscrypt/OpenSSLCipher.java
index 632f9e2..7acccc7 100644
--- a/crypto/src/main/java/org/conscrypt/OpenSSLCipher.java
+++ b/crypto/src/main/java/org/conscrypt/OpenSSLCipher.java
@@ -16,6 +16,7 @@
package org.conscrypt;
+import java.io.IOException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
@@ -118,6 +119,11 @@ public abstract class OpenSSLCipher extends CipherSpi {
}
/**
+ * Returns the standard name for the particular algorithm.
+ */
+ protected abstract String getBaseCipherName();
+
+ /**
* Returns the OpenSSL cipher name for the particular {@code keySize} and
* cipher {@code mode}.
*/
@@ -214,10 +220,22 @@ public abstract class OpenSSLCipher extends CipherSpi {
@Override
protected AlgorithmParameters engineGetParameters() {
+ if (iv != null && iv.length > 0) {
+ try {
+ AlgorithmParameters params = AlgorithmParameters.getInstance(getBaseCipherName());
+ params.init(iv);
+ return params;
+ } catch (NoSuchAlgorithmException e) {
+ return null;
+ } catch (IOException e) {
+ return null;
+ }
+ }
return null;
}
- private void engineInitInternal(int opmode, Key key, byte[] iv) throws InvalidKeyException, InvalidAlgorithmParameterException {
+ private void engineInitInternal(int opmode, Key key, byte[] iv, SecureRandom random)
+ throws InvalidKeyException, InvalidAlgorithmParameterException {
if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE) {
encrypting = true;
} else if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.UNWRAP_MODE) {
@@ -245,9 +263,15 @@ public abstract class OpenSSLCipher extends CipherSpi {
}
final int ivLength = NativeCrypto.EVP_CIPHER_iv_length(cipherType);
- if (iv == null) {
+ if (iv == null && ivLength != 0) {
iv = new byte[ivLength];
- } else if (iv.length != ivLength) {
+ if (encrypting) {
+ if (random == null) {
+ random = new SecureRandom();
+ }
+ random.nextBytes(iv);
+ }
+ } else if (iv != null && iv.length != ivLength) {
throw new InvalidAlgorithmParameterException("expected IV length of " + ivLength);
}
@@ -273,7 +297,7 @@ public abstract class OpenSSLCipher extends CipherSpi {
@Override
protected void engineInit(int opmode, Key key, SecureRandom random) throws InvalidKeyException {
try {
- engineInitInternal(opmode, key, null);
+ engineInitInternal(opmode, key, null, random);
} catch (InvalidAlgorithmParameterException e) {
throw new RuntimeException(e);
}
@@ -290,7 +314,7 @@ public abstract class OpenSSLCipher extends CipherSpi {
iv = null;
}
- engineInitInternal(opmode, key, iv);
+ engineInitInternal(opmode, key, iv, random);
}
@Override
@@ -631,6 +655,11 @@ public abstract class OpenSSLCipher extends CipherSpi {
}
@Override
+ protected String getBaseCipherName() {
+ return "AES";
+ }
+
+ @Override
protected String getCipherName(int keyLength, Mode mode) {
return "aes-" + (keyLength * 8) + "-" + mode.toString().toLowerCase(Locale.US);
}
@@ -721,6 +750,11 @@ public abstract class OpenSSLCipher extends CipherSpi {
}
@Override
+ protected String getBaseCipherName() {
+ return "DESede";
+ }
+
+ @Override
protected String getCipherName(int keySize, Mode mode) {
final String baseCipherName;
if (keySize == 16) {
@@ -780,6 +814,11 @@ public abstract class OpenSSLCipher extends CipherSpi {
}
@Override
+ protected String getBaseCipherName() {
+ return "ARCFOUR";
+ }
+
+ @Override
protected String getCipherName(int keySize, Mode mode) {
return "rc4";
}