aboutsummaryrefslogtreecommitdiff
path: root/keystore-cts/java/com/google/security/wycheproof/testcases/CipherOutputStreamTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'keystore-cts/java/com/google/security/wycheproof/testcases/CipherOutputStreamTest.java')
-rw-r--r--keystore-cts/java/com/google/security/wycheproof/testcases/CipherOutputStreamTest.java56
1 files changed, 36 insertions, 20 deletions
diff --git a/keystore-cts/java/com/google/security/wycheproof/testcases/CipherOutputStreamTest.java b/keystore-cts/java/com/google/security/wycheproof/testcases/CipherOutputStreamTest.java
index d016941..bf863fe 100644
--- a/keystore-cts/java/com/google/security/wycheproof/testcases/CipherOutputStreamTest.java
+++ b/keystore-cts/java/com/google/security/wycheproof/testcases/CipherOutputStreamTest.java
@@ -24,12 +24,17 @@ import java.security.spec.AlgorithmParameterSpec;
import java.util.ArrayList;
import java.util.Arrays;
import javax.crypto.Cipher;
+import javax.crypto.SecretKey;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
+import org.junit.After;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
+import org.junit.Ignore;
+import android.security.keystore.KeyProtection;
+import android.security.keystore.KeyProperties;
+import java.security.KeyStore;
+import android.keystore.cts.util.KeyStoreUtil;
/**
* CipherOutputStream tests
@@ -51,18 +56,31 @@ import org.junit.runners.JUnit4;
* behaviour, so that the tests are able to catch additional problems.
*/
-@RunWith(JUnit4.class)
public class CipherOutputStreamTest {
+ private static final String EXPECTED_PROVIDER_NAME = TestUtil.EXPECTED_CRYPTO_OP_PROVIDER_NAME;
static final SecureRandom rand = new SecureRandom();
+ @After
+ public void tearDown() throws Exception {
+ KeyStoreUtil.cleanUpKeyStore();
+ }
+
static byte[] randomBytes(int size) {
byte[] bytes = new byte[size];
rand.nextBytes(bytes);
return bytes;
}
- static SecretKeySpec randomKey(String algorithm, int keySizeInBytes) {
- return new SecretKeySpec(randomBytes(keySizeInBytes), "AES");
+ static SecretKey randomKey(String algorithm, String alias, int keySizeInBytes) throws Exception{
+ SecretKeySpec keySpec = new SecretKeySpec(randomBytes(keySizeInBytes), "AES");
+ KeyStore keyStore = KeyStoreUtil.saveSecretKeyToKeystore(alias, keySpec,
+ new KeyProtection.Builder(KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
+ .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
+ .setRandomizedEncryptionRequired(false)
+ .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
+ .build());
+ // Key imported, obtain a reference to it.
+ return (SecretKey) keyStore.getKey(alias, null);
}
static AlgorithmParameterSpec randomParameters(
@@ -77,21 +95,21 @@ public class CipherOutputStreamTest {
@SuppressWarnings("InsecureCryptoUsage")
public static class TestVector {
public String algorithm;
- public SecretKeySpec key;
+ public SecretKey key;
public AlgorithmParameterSpec params;
public byte[] pt;
public byte[] aad;
public byte[] ct;
public TestVector(
- String algorithm, int keySize, int ivSize, int tagSize, int ptSize, int aadSize)
- throws Exception {
+ String algorithm, String alias, int keySize,
+ int ivSize, int tagSize, int ptSize, int aadSize) throws Exception {
this.algorithm = algorithm;
- this.key = randomKey(algorithm, keySize);
+ this.key = randomKey(algorithm, alias, keySize);
this.params = randomParameters(algorithm, ivSize, tagSize);
this.pt = randomBytes(ptSize);
this.aad = randomBytes(aadSize);
- Cipher cipher = Cipher.getInstance(algorithm);
+ Cipher cipher = Cipher.getInstance(algorithm, EXPECTED_PROVIDER_NAME);
cipher.init(Cipher.ENCRYPT_MODE, this.key, this.params);
cipher.updateAAD(aad);
this.ct = cipher.doFinal(pt);
@@ -106,13 +124,16 @@ public class CipherOutputStreamTest {
int[] ptSizes,
int[] aadSizes)
throws Exception {
+ int counter = 0;
ArrayList<TestVector> result = new ArrayList<TestVector>();
for (int keySize : keySizes) {
for (int ivSize : ivSizes) {
for (int tagSize : tagSizes) {
for (int ptSize : ptSizes) {
for (int aadSize : aadSizes) {
- result.add(new TestVector(algorithm, keySize, ivSize, tagSize, ptSize, aadSize));
+ String keyAlias = "Key" + counter++;
+ result.add(new TestVector(algorithm, keyAlias, keySize,
+ ivSize, tagSize, ptSize, aadSize));
}
}
}
@@ -124,7 +145,7 @@ public class CipherOutputStreamTest {
@SuppressWarnings("InsecureCryptoUsage")
public void testEncrypt(Iterable<TestVector> tests) throws Exception {
for (TestVector t : tests) {
- Cipher cipher = Cipher.getInstance(t.algorithm);
+ Cipher cipher = Cipher.getInstance(t.algorithm, EXPECTED_PROVIDER_NAME);
cipher.init(Cipher.ENCRYPT_MODE, t.key, t.params);
cipher.updateAAD(t.aad);
ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -138,7 +159,7 @@ public class CipherOutputStreamTest {
@SuppressWarnings("InsecureCryptoUsage")
public void testDecrypt(Iterable<TestVector> tests) throws Exception {
for (TestVector t : tests) {
- Cipher cipher = Cipher.getInstance(t.algorithm);
+ Cipher cipher = Cipher.getInstance(t.algorithm, EXPECTED_PROVIDER_NAME);
cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
cipher.updateAAD(t.aad);
ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -160,7 +181,7 @@ public class CipherOutputStreamTest {
public void testCorruptDecrypt(Iterable<TestVector> tests, boolean acceptEmptyPlaintext)
throws Exception {
for (TestVector t : tests) {
- Cipher cipher = Cipher.getInstance(t.algorithm);
+ Cipher cipher = Cipher.getInstance(t.algorithm, EXPECTED_PROVIDER_NAME);
cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
cipher.updateAAD(t.aad);
byte[] ct = Arrays.copyOf(t.ct, t.ct.length);
@@ -228,6 +249,7 @@ public class CipherOutputStreamTest {
/** Tests CipherOutputStream with AES-EAX if AES-EAS is supported by the provider. */
@SuppressWarnings("InsecureCryptoUsage")
@Test
+ @Ignore // Ignored due to AES/EAX algorithm is not supported in AndroidKeyStore
public void testAesEax() throws Exception {
final String algorithm = "AES/EAX/NoPadding";
final int[] keySizes = {16, 32};
@@ -235,12 +257,6 @@ public class CipherOutputStreamTest {
final int[] tagSizes = {12, 16};
final int[] ptSizes = {8, 16, 65, 8100};
final int[] aadSizes = {0, 8, 24};
- try {
- Cipher.getInstance(algorithm);
- } catch (NoSuchAlgorithmException ex) {
- System.out.println("Skipping testAesEax");
- return;
- }
Iterable<TestVector> v =
getTestVectors(algorithm, keySizes, ivSizes, tagSizes, ptSizes, aadSizes);
testEncrypt(v);