aboutsummaryrefslogtreecommitdiff
path: root/keystore-cts/java/com/google/security/wycheproof/testcases/MacTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'keystore-cts/java/com/google/security/wycheproof/testcases/MacTest.java')
-rw-r--r--keystore-cts/java/com/google/security/wycheproof/testcases/MacTest.java73
1 files changed, 31 insertions, 42 deletions
diff --git a/keystore-cts/java/com/google/security/wycheproof/testcases/MacTest.java b/keystore-cts/java/com/google/security/wycheproof/testcases/MacTest.java
index 34b6115..171af58 100644
--- a/keystore-cts/java/com/google/security/wycheproof/testcases/MacTest.java
+++ b/keystore-cts/java/com/google/security/wycheproof/testcases/MacTest.java
@@ -15,18 +15,20 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
-import com.google.security.wycheproof.WycheproofRunner.ProviderType;
-import com.google.security.wycheproof.WycheproofRunner.SlowTest;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.security.Key;
+import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Mac;
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 android.keystore.cts.util.KeyStoreUtil;
/**
* Tests for MACs.
@@ -35,8 +37,21 @@ import org.junit.runners.JUnit4;
* with known results are necessary. So far only simple test vectors for long messages are
* available.
*/
-@RunWith(JUnit4.class)
public class MacTest {
+ private static final String EXPECTED_PROVIDER_NAME = TestUtil.EXPECTED_CRYPTO_OP_PROVIDER_NAME;
+ private static final String KEY_ALIAS_1 = "TestKey";
+
+ @After
+ public void tearDown() throws Exception {
+ KeyStoreUtil.cleanUpKeyStore();
+ }
+
+ private static Key getKeyStoreSecretKey(byte[] keyMaterial, String algorithm) throws Exception {
+ KeyStore keyStore = KeyStoreUtil.saveSecretKeyToKeystore(KEY_ALIAS_1,
+ new SecretKeySpec(keyMaterial, algorithm),
+ new KeyProtection.Builder(KeyProperties.PURPOSE_SIGN).build());
+ return keyStore.getKey(KEY_ALIAS_1, null);
+ }
/**
* Computes the maximum of an array with at least one element.
@@ -79,7 +94,7 @@ public class MacTest {
*/
private void testUpdateWithChunks(String algorithm, Key key, byte[] data, int... chunkSizes)
throws Exception {
- Mac mac = Mac.getInstance(algorithm);
+ Mac mac = Mac.getInstance(algorithm, EXPECTED_PROVIDER_NAME);
// First evaluation: compute MAC in one piece.
int totalLength = 0;
@@ -178,15 +193,14 @@ public class MacTest {
public void testMac(String algorithm, int keySize) throws Exception {
try {
- Mac.getInstance(algorithm);
+ Mac.getInstance(algorithm, EXPECTED_PROVIDER_NAME);
} catch (NoSuchAlgorithmException ex) {
- System.out.println("Algorithm " + algorithm + " is not supported. Skipping test.");
- return;
+ fail("Algorithm " + algorithm + " is not supported.");
}
byte[] key = new byte[keySize];
SecureRandom rand = new SecureRandom();
rand.nextBytes(key);
- testUpdate(algorithm, new SecretKeySpec(key, algorithm));
+ testUpdate(algorithm, getKeyStoreSecretKey(key, algorithm));
}
@Test
@@ -215,21 +229,25 @@ public class MacTest {
}
@Test
+ @Ignore // HmacSha3 algorithms are not supported in AndroidKeyStore
public void testHmacSha3_224() throws Exception {
testMac("HMACSHA3-224", 28);
}
@Test
+ @Ignore // HmacSha3 algorithms are not supported in AndroidKeyStore
public void testHmacSha3_256() throws Exception {
testMac("HMACSHA3-256", 32);
}
@Test
+ @Ignore // HmacSha3 algorithms are not supported in AndroidKeyStore
public void testHmacSha3_384() throws Exception {
testMac("HMACSHA3-384", 48);
}
@Test
+ @Ignore // HmacSha3 algorithms are not supported in AndroidKeyStore
public void testHmacSha3_512() throws Exception {
testMac("HMACSHA3-512", 64);
}
@@ -246,7 +264,7 @@ public class MacTest {
*/
public byte[] macRepeatedMessage(String algorithm, Key key, byte[] message, long repetitions)
throws Exception {
- Mac mac = Mac.getInstance(algorithm);
+ Mac mac = Mac.getInstance(algorithm, EXPECTED_PROVIDER_NAME);
mac.init(key);
// If the message is short then it is more efficient to collect multiple copies
// of the message in one chunk and call update with the larger chunk.
@@ -283,26 +301,18 @@ public class MacTest {
String algorithm, String keyhex, String message, long repetitions, String expected)
throws Exception {
- Key key = new SecretKeySpec(TestUtil.hexToBytes(keyhex), algorithm);
+ Key key = getKeyStoreSecretKey(TestUtil.hexToBytes(keyhex), algorithm);
byte[] bytes = message.getBytes(UTF_8);
byte[] mac = null;
try {
mac = macRepeatedMessage(algorithm, key, bytes, repetitions);
} catch (NoSuchAlgorithmException ex) {
- System.out.println("Algorithm " + algorithm + " is not supported. Skipping test.");
- return;
+ fail("Algorithm " + algorithm + " is not supported.");
}
String hexmac = TestUtil.bytesToHex(mac);
assertEquals(expected, hexmac);
}
- @SlowTest(
- providers = {
- ProviderType.OPENJDK,
- ProviderType.BOUNCY_CASTLE,
- ProviderType.SPONGY_CASTLE,
- ProviderType.CONSCRYPT
- })
@Test
public void testLongMacSha1() throws Exception {
testLongMac(
@@ -319,13 +329,6 @@ public class MacTest {
"d7f4c387f2237ea119fcc27cd7520fc5132b6230");
}
- @SlowTest(
- providers = {
- ProviderType.OPENJDK,
- ProviderType.BOUNCY_CASTLE,
- ProviderType.SPONGY_CASTLE,
- ProviderType.CONSCRYPT
- })
@Test
public void testLongMacSha256() throws Exception {
testLongMac(
@@ -342,13 +345,6 @@ public class MacTest {
"59a75754df7093fa4339aa618b64b104f153a5b42cc85394fdb8735b13ea684a");
}
- @SlowTest(
- providers = {
- ProviderType.OPENJDK,
- ProviderType.BOUNCY_CASTLE,
- ProviderType.SPONGY_CASTLE,
- ProviderType.CONSCRYPT
- })
@Test
public void testLongMacSha384() throws Exception {
testLongMac(
@@ -369,13 +365,6 @@ public class MacTest {
+ "a477e6a84d159d8b7a3daaa89c4f2372");
}
- @SlowTest(
- providers = {
- ProviderType.OPENJDK,
- ProviderType.BOUNCY_CASTLE,
- ProviderType.SPONGY_CASTLE,
- ProviderType.CONSCRYPT
- })
@Test
public void testLongMacSha512() throws Exception {
testLongMac(