Skip to content
Snippets Groups Projects
Commit 337fd980 authored by tholenst's avatar tholenst Committed by Copybara-Service
Browse files

Migrate the AesEaxKeyManagerTest to directly test on the KeyTypeManager interface.

PiperOrigin-RevId: 266357537
parent 04035827
No related branches found
No related tags found
No related merge requests found
...@@ -16,99 +16,140 @@ ...@@ -16,99 +16,140 @@
package com.google.crypto.tink.aead; package com.google.crypto.tink.aead;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import com.google.crypto.tink.Aead; import com.google.crypto.tink.Aead;
import com.google.crypto.tink.CryptoFormat;
import com.google.crypto.tink.KeyManager;
import com.google.crypto.tink.KeyManagerImpl;
import com.google.crypto.tink.KeysetHandle;
import com.google.crypto.tink.TestUtil; import com.google.crypto.tink.TestUtil;
import com.google.crypto.tink.proto.AesEaxKey; import com.google.crypto.tink.proto.AesEaxKey;
import com.google.crypto.tink.proto.AesEaxKeyFormat; import com.google.crypto.tink.proto.AesEaxKeyFormat;
import com.google.crypto.tink.proto.AesEaxParams; import com.google.crypto.tink.proto.AesEaxParams;
import com.google.crypto.tink.proto.KeyData; import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
import com.google.crypto.tink.proto.KeyStatusType; import com.google.crypto.tink.subtle.AesEaxJce;
import com.google.crypto.tink.proto.KeyTemplate;
import com.google.crypto.tink.proto.OutputPrefixType;
import com.google.crypto.tink.subtle.Bytes; import com.google.crypto.tink.subtle.Bytes;
import com.google.crypto.tink.subtle.Random; import com.google.crypto.tink.subtle.Random;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
/** /** Test for AesEaxJce and its key manager. */
* Test for AesEaxJce and its key manager.
*/
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class AesEaxKeyManagerTest { public class AesEaxKeyManagerTest {
@BeforeClass private final AesEaxKeyManager manager = new AesEaxKeyManager();
public static void setUp() throws GeneralSecurityException { private final AesEaxKeyManager.KeyFactory<AesEaxKeyFormat, AesEaxKey> factory =
AeadConfig.register(); manager.keyFactory();
}
@Test @Test
public void testNewKeyMultipleTimes() throws Exception { public void basics() throws Exception {
AesEaxKeyFormat eaxKeyFormat = AesEaxKeyFormat.newBuilder() assertThat(manager.getKeyType()).isEqualTo("type.googleapis.com/google.crypto.tink.AesEaxKey");
.setParams(AesEaxParams.newBuilder().setIvSize(16).build()) assertThat(manager.getVersion()).isEqualTo(0);
.setKeySize(16) assertThat(manager.keyMaterialType()).isEqualTo(KeyMaterialType.SYMMETRIC);
.build(); }
ByteString serialized = ByteString.copyFrom(eaxKeyFormat.toByteArray());
KeyTemplate keyTemplate = KeyTemplate.newBuilder() private static AesEaxKeyFormat createKeyFormat(int keySize, int ivSize) {
.setTypeUrl(new AesEaxKeyManager().getKeyType()) return AesEaxKeyFormat.newBuilder()
.setValue(serialized) .setParams(AesEaxParams.newBuilder().setIvSize(ivSize))
.setKeySize(keySize)
.build(); .build();
KeyManager<Aead> keyManager = new KeyManagerImpl<>(new AesEaxKeyManager(), Aead.class); }
Set<String> keys = new TreeSet<String>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 27;
for (int i = 0; i < numTests / 3; i++) {
AesEaxKey key = (AesEaxKey) keyManager.newKey(eaxKeyFormat);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
key = (AesEaxKey) keyManager.newKey(serialized); @Test
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray())); public void validateKeyFormat_empty() throws Exception {
assertEquals(16, key.getKeyValue().toByteArray().length); try {
factory.validateKeyFormat(AesEaxKeyFormat.getDefaultInstance());
fail();
} catch (GeneralSecurityException e) {
// expected.
}
}
KeyData keyData = keyManager.newKeyData(keyTemplate.getValue()); @Test
key = AesEaxKey.parseFrom(keyData.getValue()); public void validateKeyFormat_valid() throws Exception {
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray())); factory.validateKeyFormat(createKeyFormat(16, 12));
assertEquals(16, key.getKeyValue().toByteArray().length); factory.validateKeyFormat(createKeyFormat(16, 16));
factory.validateKeyFormat(createKeyFormat(32, 12));
factory.validateKeyFormat(createKeyFormat(32, 16));
}
@Test
public void validateKeyFormat_keySize() throws Exception {
for (int len = 0; len < 200; ++len) {
AesEaxKeyFormat format = createKeyFormat(len, 16);
if (len == 16 || len == 32) {
factory.validateKeyFormat(format);
} else {
try {
factory.validateKeyFormat(format);
fail();
} catch (GeneralSecurityException e) {
// expected
}
}
} }
assertEquals(numTests, keys.size());
} }
@Test @Test
public void testNewKeyWithCorruptedFormat() throws Exception { public void validateKeyFormat_ivSize() throws Exception {
ByteString serialized = ByteString.copyFrom(new byte[128]); for (int ivSize = 0; ivSize < 200; ++ivSize) {
KeyTemplate keyTemplate = KeyTemplate.newBuilder() AesEaxKeyFormat format = createKeyFormat(32, ivSize);
.setTypeUrl(new AesEaxKeyManager().getKeyType()) if (ivSize == 12 || ivSize == 16) {
.setValue(serialized) factory.validateKeyFormat(format);
.build(); } else {
KeyManager<Aead> keyManager = new KeyManagerImpl<>(new AesEaxKeyManager(), Aead.class); try {
try { factory.validateKeyFormat(format);
keyManager.newKey(serialized); fail();
fail("Corrupted format, should have thrown exception"); } catch (GeneralSecurityException e) {
} catch (GeneralSecurityException expected) { // expected
// Expected }
}
} }
try { }
keyManager.newKeyData(keyTemplate.getValue());
fail("Corrupted format, should have thrown exception"); @Test
} catch (GeneralSecurityException expected) { public void createKey_checkValues() throws Exception {
// Expected AesEaxKeyFormat format = createKeyFormat(32, 16);
AesEaxKey key = factory.createKey(format);
assertThat(key.getKeyValue()).hasSize(format.getKeySize());
assertThat(key.getParams()).isEqualTo(format.getParams());
}
@Test
public void createKey_checkValues_variant2() throws Exception {
AesEaxKeyFormat format = createKeyFormat(16, 12);
AesEaxKey key = factory.createKey(format);
assertThat(key.getKeyValue()).hasSize(format.getKeySize());
assertThat(key.getParams()).isEqualTo(format.getParams());
}
@Test
public void createKey_multipleTimes() throws Exception {
AesEaxKeyFormat format = createKeyFormat(32, 16);
Set<String> keys = new TreeSet<>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 50;
for (int i = 0; i < numTests; i++) {
keys.add(TestUtil.hexEncode(factory.createKey(format).getKeyValue().toByteArray()));
} }
assertThat(keys).hasSize(numTests);
} }
private static final int AES_KEY_SIZE = 16; @Test
public void getPrimitive() throws Exception {
AesEaxKey key = factory.createKey(createKeyFormat(32, 16));
Aead managerAead = manager.getPrimitive(key, Aead.class);
Aead directAead = new AesEaxJce(key.getKeyValue().toByteArray(), key.getParams().getIvSize());
byte[] plaintext = Random.randBytes(20);
byte[] associatedData = Random.randBytes(20);
assertThat(directAead.decrypt(managerAead.encrypt(plaintext, associatedData), associatedData))
.isEqualTo(plaintext);
}
private static class PublicTestVector { private static class PublicTestVector {
String name; String name;
...@@ -118,8 +159,15 @@ public class AesEaxKeyManagerTest { ...@@ -118,8 +159,15 @@ public class AesEaxKeyManagerTest {
public byte[] iv; public byte[] iv;
public byte[] ciphertext; public byte[] ciphertext;
public byte[] tag; public byte[] tag;
public PublicTestVector(String name, String keyValue, String plaintext, String aad,
String iv, String ciphertext, String tag) { public PublicTestVector(
String name,
String keyValue,
String plaintext,
String aad,
String iv,
String ciphertext,
String tag) {
try { try {
this.name = name; this.name = name;
this.keyValue = TestUtil.hexDecode(keyValue); this.keyValue = TestUtil.hexDecode(keyValue);
...@@ -137,86 +185,86 @@ public class AesEaxKeyManagerTest { ...@@ -137,86 +185,86 @@ public class AesEaxKeyManagerTest {
// Test vectors from // Test vectors from
// http://web.cs.ucdavis.edu/~rogaway/papers/eax.pdf. // http://web.cs.ucdavis.edu/~rogaway/papers/eax.pdf.
PublicTestVector[] publicTestVectors = { PublicTestVector[] publicTestVectors = {
new PublicTestVector( new PublicTestVector(
"Test Case 1", "Test Case 1",
"233952dee4d5ed5f9b9c6d6ff80ff478", "233952dee4d5ed5f9b9c6d6ff80ff478",
"", "",
"6bfb914fd07eae6b", "6bfb914fd07eae6b",
"62ec67f9c3a4a407fcb2a8c49031a8b3", "62ec67f9c3a4a407fcb2a8c49031a8b3",
"", "",
"e037830e8389f27b025a2d6527e79d01"), "e037830e8389f27b025a2d6527e79d01"),
new PublicTestVector( new PublicTestVector(
"Test Case 2", "Test Case 2",
"91945d3f4dcbee0bf45ef52255f095a4", "91945d3f4dcbee0bf45ef52255f095a4",
"f7fb", "f7fb",
"fa3bfd4806eb53fa", "fa3bfd4806eb53fa",
"becaf043b0a23d843194ba972c66debd", "becaf043b0a23d843194ba972c66debd",
"19dd", "19dd",
"5c4c9331049d0bdab0277408f67967e5"), "5c4c9331049d0bdab0277408f67967e5"),
new PublicTestVector( new PublicTestVector(
"Test Case 3", "Test Case 3",
"01f74ad64077f2e704c0f60ada3dd523", "01f74ad64077f2e704c0f60ada3dd523",
"1a47cb4933", "1a47cb4933",
"234a3463c1264ac6", "234a3463c1264ac6",
"70c3db4f0d26368400a10ed05d2bff5e", "70c3db4f0d26368400a10ed05d2bff5e",
"d851d5bae0", "d851d5bae0",
"3a59f238a23e39199dc9266626c40f80"), "3a59f238a23e39199dc9266626c40f80"),
new PublicTestVector( new PublicTestVector(
"Test Case 4", "Test Case 4",
"d07cf6cbb7f313bdde66b727afd3c5e8", "d07cf6cbb7f313bdde66b727afd3c5e8",
"481c9e39b1", "481c9e39b1",
"33cce2eabff5a79d", "33cce2eabff5a79d",
"8408dfff3c1a2b1292dc199e46b7d617", "8408dfff3c1a2b1292dc199e46b7d617",
"632a9d131a", "632a9d131a",
"d4c168a4225d8e1ff755939974a7bede"), "d4c168a4225d8e1ff755939974a7bede"),
new PublicTestVector( new PublicTestVector(
"Test Case 5", "Test Case 5",
"35b6d0580005bbc12b0587124557d2c2", "35b6d0580005bbc12b0587124557d2c2",
"40d0c07da5e4", "40d0c07da5e4",
"aeb96eaebe2970e9", "aeb96eaebe2970e9",
"fdb6b06676eedc5c61d74276e1f8e816", "fdb6b06676eedc5c61d74276e1f8e816",
"071dfe16c675", "071dfe16c675",
"cb0677e536f73afe6a14b74ee49844dd"), "cb0677e536f73afe6a14b74ee49844dd"),
new PublicTestVector( new PublicTestVector(
"Test Case 6", "Test Case 6",
"bd8e6e11475e60b268784c38c62feb22", "bd8e6e11475e60b268784c38c62feb22",
"4de3b35c3fc039245bd1fb7d", "4de3b35c3fc039245bd1fb7d",
"d4482d1ca78dce0f", "d4482d1ca78dce0f",
"6eac5c93072d8e8513f750935e46da1b", "6eac5c93072d8e8513f750935e46da1b",
"835bb4f15d743e350e728414", "835bb4f15d743e350e728414",
"abb8644fd6ccb86947c5e10590210a4f"), "abb8644fd6ccb86947c5e10590210a4f"),
new PublicTestVector( new PublicTestVector(
"Test Case 7", "Test Case 7",
"7c77d6e813bed5ac98baa417477a2e7d", "7c77d6e813bed5ac98baa417477a2e7d",
"8b0a79306c9ce7ed99dae4f87f8dd61636", "8b0a79306c9ce7ed99dae4f87f8dd61636",
"65d2017990d62528", "65d2017990d62528",
"1a8c98dcd73d38393b2bf1569deefc19", "1a8c98dcd73d38393b2bf1569deefc19",
"02083e3979da014812f59f11d52630da30", "02083e3979da014812f59f11d52630da30",
"137327d10649b0aa6e1c181db617d7f2"), "137327d10649b0aa6e1c181db617d7f2"),
new PublicTestVector( new PublicTestVector(
"Test Case 8", "Test Case 8",
"5fff20cafab119ca2fc73549e20f5b0d", "5fff20cafab119ca2fc73549e20f5b0d",
"1bda122bce8a8dbaf1877d962b8592dd2d56", "1bda122bce8a8dbaf1877d962b8592dd2d56",
"54b9f04e6a09189a", "54b9f04e6a09189a",
"dde59b97d722156d4d9aff2bc7559826", "dde59b97d722156d4d9aff2bc7559826",
"2ec47b2c4954a489afc7ba4897edcdae8cc3", "2ec47b2c4954a489afc7ba4897edcdae8cc3",
"3b60450599bd02c96382902aef7f832a"), "3b60450599bd02c96382902aef7f832a"),
new PublicTestVector( new PublicTestVector(
"Test Case 9", "Test Case 9",
"a4a4782bcffd3ec5e7ef6d8c34a56123", "a4a4782bcffd3ec5e7ef6d8c34a56123",
"6cf36720872b8513f6eab1a8a44438d5ef11", "6cf36720872b8513f6eab1a8a44438d5ef11",
"899a175897561d7e", "899a175897561d7e",
"b781fcf2f75fa5a8de97a9ca48e522ec", "b781fcf2f75fa5a8de97a9ca48e522ec",
"0de18fd0fdd91e7af19f1d8ee8733938b1e8", "0de18fd0fdd91e7af19f1d8ee8733938b1e8",
"e7f6d2231618102fdb7fe55ff1991700"), "e7f6d2231618102fdb7fe55ff1991700"),
new PublicTestVector( new PublicTestVector(
"Test Case 10", "Test Case 10",
"8395fcf1e95bebd697bd010bc766aac3", "8395fcf1e95bebd697bd010bc766aac3",
"ca40d7446e545ffaed3bd12a740a659ffbbb3ceab7", "ca40d7446e545ffaed3bd12a740a659ffbbb3ceab7",
"126735fcc320d25a", "126735fcc320d25a",
"22e7add93cfc6393c57ec0b3c17d6b44", "22e7add93cfc6393c57ec0b3c17d6b44",
"cb8920f87a6c75cff39627b56e3ed197c552d295a7", "cb8920f87a6c75cff39627b56e3ed197c552d295a7",
"cfc46afc253b4652b1af3795b124ab6e"), "cfc46afc253b4652b1af3795b124ab6e"),
}; };
@Test @Test
...@@ -225,7 +273,12 @@ public class AesEaxKeyManagerTest { ...@@ -225,7 +273,12 @@ public class AesEaxKeyManagerTest {
if (TestUtil.shouldSkipTestWithAesKeySize(t.keyValue.length)) { if (TestUtil.shouldSkipTestWithAesKeySize(t.keyValue.length)) {
continue; continue;
} }
Aead aead = getRawAesEax(t.keyValue, t.iv.length); AesEaxKey key =
AesEaxKey.newBuilder()
.setKeyValue(ByteString.copyFrom(t.keyValue))
.setParams(AesEaxParams.newBuilder().setIvSize(t.iv.length))
.build();
Aead aead = manager.getPrimitive(key, Aead.class);
try { try {
byte[] ciphertext = Bytes.concat(t.iv, t.ciphertext, t.tag); byte[] ciphertext = Bytes.concat(t.iv, t.ciphertext, t.tag);
byte[] plaintext = aead.decrypt(ciphertext, t.aad); byte[] plaintext = aead.decrypt(ciphertext, t.aad);
...@@ -236,46 +289,13 @@ public class AesEaxKeyManagerTest { ...@@ -236,46 +289,13 @@ public class AesEaxKeyManagerTest {
} }
} }
private Aead getRawAesEax(byte[] keyValue, int ivSizeInBytes) throws Exception {
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(
TestUtil.createKeyset(
TestUtil.createKey(
TestUtil.createAesEaxKeyData(keyValue, ivSizeInBytes),
42,
KeyStatusType.ENABLED,
OutputPrefixType.RAW)));
return keysetHandle.getPrimitive(Aead.class);
}
@Test
public void testBasic() throws Exception {
byte[] keyValue = Random.randBytes(AES_KEY_SIZE);
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(
TestUtil.createKeyset(
TestUtil.createKey(
TestUtil.createAesEaxKeyData(keyValue, 12),
42,
KeyStatusType.ENABLED,
OutputPrefixType.TINK)));
TestUtil.runBasicAeadTests(keysetHandle.getPrimitive(Aead.class));
}
@Test @Test
public void testCiphertextSize() throws Exception { public void testCiphertextSize() throws Exception {
byte[] keyValue = Random.randBytes(AES_KEY_SIZE); AesEaxKey key = factory.createKey(createKeyFormat(32, 16));
KeysetHandle keysetHandle = TestUtil.createKeysetHandle( Aead aead = manager.getPrimitive(key, Aead.class);
TestUtil.createKeyset(
TestUtil.createKey(
TestUtil.createAesEaxKeyData(keyValue, 16),
42,
KeyStatusType.ENABLED,
OutputPrefixType.TINK)));
Aead aead = keysetHandle.getPrimitive(Aead.class);
byte[] plaintext = "plaintext".getBytes("UTF-8"); byte[] plaintext = "plaintext".getBytes("UTF-8");
byte[] associatedData = "associatedData".getBytes("UTF-8"); byte[] associatedData = "associatedData".getBytes("UTF-8");
byte[] ciphertext = aead.encrypt(plaintext, associatedData); byte[] ciphertext = aead.encrypt(plaintext, associatedData);
assertEquals( assertEquals(16 /* IV_SIZE */ + plaintext.length + 16 /* TAG_SIZE */, ciphertext.length);
CryptoFormat.NON_RAW_PREFIX_SIZE + 16 /* IV_SIZE */ + plaintext.length + 16 /* TAG_SIZE */,
ciphertext.length);
} }
} }
...@@ -18,13 +18,13 @@ package com.google.crypto.tink.aead; ...@@ -18,13 +18,13 @@ package com.google.crypto.tink.aead;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import com.google.crypto.tink.Aead; import com.google.crypto.tink.Aead;
import com.google.crypto.tink.TestUtil; import com.google.crypto.tink.TestUtil;
import com.google.crypto.tink.proto.AesGcmKey; import com.google.crypto.tink.proto.AesGcmKey;
import com.google.crypto.tink.proto.AesGcmKeyFormat; import com.google.crypto.tink.proto.AesGcmKeyFormat;
import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
import com.google.crypto.tink.subtle.AesGcmJce; import com.google.crypto.tink.subtle.AesGcmJce;
import com.google.crypto.tink.subtle.Bytes; import com.google.crypto.tink.subtle.Bytes;
import com.google.crypto.tink.subtle.Random; import com.google.crypto.tink.subtle.Random;
...@@ -43,6 +43,13 @@ public class AesGcmKeyManagerTest { ...@@ -43,6 +43,13 @@ public class AesGcmKeyManagerTest {
private final AesGcmKeyManager.KeyFactory<AesGcmKeyFormat, AesGcmKey> factory = private final AesGcmKeyManager.KeyFactory<AesGcmKeyFormat, AesGcmKey> factory =
manager.keyFactory(); manager.keyFactory();
@Test
public void basics() throws Exception {
assertThat(manager.getKeyType()).isEqualTo("type.googleapis.com/google.crypto.tink.AesGcmKey");
assertThat(manager.getVersion()).isEqualTo(0);
assertThat(manager.keyMaterialType()).isEqualTo(KeyMaterialType.SYMMETRIC);
}
@Test @Test
public void validateKeyFormat_empty() throws Exception { public void validateKeyFormat_empty() throws Exception {
try { try {
...@@ -120,7 +127,7 @@ public class AesGcmKeyManagerTest { ...@@ -120,7 +127,7 @@ public class AesGcmKeyManagerTest {
for (int i = 0; i < numTests; i++) { for (int i = 0; i < numTests; i++) {
keys.add(TestUtil.hexEncode(factory.createKey(format).getKeyValue().toByteArray())); keys.add(TestUtil.hexEncode(factory.createKey(format).getKeyValue().toByteArray()));
} }
assertEquals(numTests, keys.size()); assertThat(keys).hasSize(numTests);
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment