use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class DeterministicAeadFactoryTest method testSmallPlaintextWithRawKey.
private static void testSmallPlaintextWithRawKey(int keySize) throws Exception {
Key primary = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary));
DeterministicAead daead = DeterministicAeadFactory.getPrimitive(keysetHandle);
byte[] plaintext = Random.randBytes(1);
byte[] associatedData = Random.randBytes(20);
byte[] ciphertext = daead.encryptDeterministically(plaintext, associatedData);
assertArrayEquals(plaintext, daead.decryptDeterministically(ciphertext, associatedData));
assertEquals(CryptoFormat.RAW_PREFIX_SIZE + plaintext.length + 16, ciphertext.length);
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class DeterministicAeadFactoryTest method testMultipleKeys.
private static void testMultipleKeys(int keySize) throws Exception {
Key primary = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK);
Key raw = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key legacy = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 44, KeyStatusType.ENABLED, OutputPrefixType.LEGACY);
Key tink = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 45, KeyStatusType.ENABLED, OutputPrefixType.TINK);
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary, raw, legacy, tink));
DeterministicAead daead = DeterministicAeadFactory.getPrimitive(keysetHandle);
byte[] plaintext = Random.randBytes(20);
byte[] associatedData = Random.randBytes(20);
byte[] ciphertext = daead.encryptDeterministically(plaintext, associatedData);
byte[] prefix = Arrays.copyOfRange(ciphertext, 0, CryptoFormat.NON_RAW_PREFIX_SIZE);
assertArrayEquals(prefix, CryptoFormat.getOutputPrefix(primary));
assertArrayEquals(plaintext, daead.decryptDeterministically(ciphertext, associatedData));
assertEquals(CryptoFormat.NON_RAW_PREFIX_SIZE + plaintext.length + 16, ciphertext.length);
// encrypt with a non-primary RAW key and decrypt with the keyset
KeysetHandle keysetHandle2 = TestUtil.createKeysetHandle(TestUtil.createKeyset(raw, legacy, tink));
DeterministicAead daead2 = DeterministicAeadFactory.getPrimitive(keysetHandle2);
ciphertext = daead2.encryptDeterministically(plaintext, associatedData);
assertArrayEquals(plaintext, daead.decryptDeterministically(ciphertext, associatedData));
// encrypt with a random key not in the keyset, decrypt with the keyset should fail
Key random = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 44, KeyStatusType.ENABLED, OutputPrefixType.TINK);
keysetHandle2 = TestUtil.createKeysetHandle(TestUtil.createKeyset(random));
daead2 = DeterministicAeadFactory.getPrimitive(keysetHandle2);
ciphertext = daead2.encryptDeterministically(plaintext, associatedData);
try {
daead.decryptDeterministically(ciphertext, associatedData);
fail("Expected GeneralSecurityException");
} catch (GeneralSecurityException e) {
assertExceptionContains(e, "decryption failed");
}
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class DeterministicAeadFactoryTest method testRawKeyAsPrimary.
private static void testRawKeyAsPrimary(int keySize) throws Exception {
Key primary = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key raw = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key legacy = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 44, KeyStatusType.ENABLED, OutputPrefixType.LEGACY);
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary, raw, legacy));
DeterministicAead daead = DeterministicAeadFactory.getPrimitive(keysetHandle);
byte[] plaintext = Random.randBytes(20);
byte[] associatedData = Random.randBytes(20);
byte[] ciphertext = daead.encryptDeterministically(plaintext, associatedData);
assertArrayEquals(plaintext, daead.decryptDeterministically(ciphertext, associatedData));
assertEquals(CryptoFormat.RAW_PREFIX_SIZE + plaintext.length + 16, ciphertext.length);
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class DeterministicAeadIntegrationTest method testRawKeyAsPrimary.
private static void testRawKeyAsPrimary(int keySize) throws Exception {
Key primary = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key raw = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key legacy = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 44, KeyStatusType.ENABLED, OutputPrefixType.LEGACY);
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary, raw, legacy));
DeterministicAead daead = keysetHandle.getPrimitive(DeterministicAead.class);
byte[] plaintext = Random.randBytes(20);
byte[] associatedData = Random.randBytes(20);
byte[] ciphertext = daead.encryptDeterministically(plaintext, associatedData);
assertArrayEquals(plaintext, daead.decryptDeterministically(ciphertext, associatedData));
assertEquals(CryptoFormat.RAW_PREFIX_SIZE + plaintext.length + 16, ciphertext.length);
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class MacFactoryTest method testSmallPlaintextWithRawKey.
@Test
public void testSmallPlaintextWithRawKey() throws Exception {
byte[] keyValue = Random.randBytes(HMAC_KEY_SIZE);
Key primary = TestUtil.createKey(TestUtil.createHmacKeyData(keyValue, 16), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary));
Mac mac = MacFactory.getPrimitive(keysetHandle);
byte[] plaintext = "blah".getBytes("UTF-8");
byte[] tag = mac.computeMac(plaintext);
// no prefix
assertEquals(16, /* TAG */
tag.length);
try {
mac.verifyMac(tag, plaintext);
} catch (GeneralSecurityException e) {
fail("Valid MAC, should not throw exception");
}
}
Aggregations