use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class PublicKeySignIntegrationTest method testMultipleKeys.
@Test
public void testMultipleKeys() throws Exception {
EcdsaPrivateKey tinkPrivateKey = TestUtil.generateEcdsaPrivKey(EllipticCurveType.NIST_P521, HashType.SHA512, EcdsaSignatureEncoding.DER);
Key tink = TestUtil.createKey(TestUtil.createKeyData(tinkPrivateKey, new EcdsaSignKeyManager().getKeyType(), KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 1, KeyStatusType.ENABLED, OutputPrefixType.TINK);
EcdsaPrivateKey legacyPrivateKey = TestUtil.generateEcdsaPrivKey(EllipticCurveType.NIST_P256, HashType.SHA256, EcdsaSignatureEncoding.DER);
Key legacy = TestUtil.createKey(TestUtil.createKeyData(legacyPrivateKey, new EcdsaSignKeyManager().getKeyType(), KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 2, KeyStatusType.ENABLED, OutputPrefixType.LEGACY);
EcdsaPrivateKey rawPrivateKey = TestUtil.generateEcdsaPrivKey(EllipticCurveType.NIST_P384, HashType.SHA512, EcdsaSignatureEncoding.DER);
Key raw = TestUtil.createKey(TestUtil.createKeyData(rawPrivateKey, new EcdsaSignKeyManager().getKeyType(), KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 3, KeyStatusType.ENABLED, OutputPrefixType.RAW);
EcdsaPrivateKey crunchyPrivateKey = TestUtil.generateEcdsaPrivKey(EllipticCurveType.NIST_P384, HashType.SHA512, EcdsaSignatureEncoding.DER);
Key crunchy = TestUtil.createKey(TestUtil.createKeyData(crunchyPrivateKey, new EcdsaSignKeyManager().getKeyType(), KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 4, KeyStatusType.ENABLED, OutputPrefixType.CRUNCHY);
Key[] keys = new Key[] { tink, legacy, raw, crunchy };
EcdsaPrivateKey[] privateKeys = new EcdsaPrivateKey[] { tinkPrivateKey, legacyPrivateKey, rawPrivateKey, crunchyPrivateKey };
int j = keys.length;
for (int i = 0; i < j; i++) {
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(keys[i], keys[(i + 1) % j], keys[(i + 2) % j], keys[(i + 3) % j]));
// Signs with the primary private key.
PublicKeySign signer = keysetHandle.getPrimitive(PublicKeySign.class);
byte[] plaintext = Random.randBytes(1211);
byte[] sig = signer.sign(plaintext);
if (keys[i].getOutputPrefixType() != OutputPrefixType.RAW) {
byte[] prefix = Arrays.copyOfRange(sig, 0, CryptoFormat.NON_RAW_PREFIX_SIZE);
assertArrayEquals(prefix, CryptoFormat.getOutputPrefix(keys[i]));
}
// Verifying with the primary public key should work.
PublicKeyVerify verifier = TestUtil.createKeysetHandle(TestUtil.createKeyset(TestUtil.createKey(TestUtil.createKeyData(privateKeys[i].getPublicKey(), new EcdsaVerifyKeyManager().getKeyType(), KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), keys[i].getKeyId(), KeyStatusType.ENABLED, keys[i].getOutputPrefixType()))).getPrimitive(PublicKeyVerify.class);
try {
verifier.verify(sig, plaintext);
} catch (GeneralSecurityException ex) {
fail("Valid signature, should not throw exception");
}
// Verifying with a random public key should fail.
EcdsaPrivateKey randomPrivKey = TestUtil.generateEcdsaPrivKey(EllipticCurveType.NIST_P521, HashType.SHA512, EcdsaSignatureEncoding.DER);
final PublicKeyVerify verifier2 = TestUtil.createKeysetHandle(TestUtil.createKeyset(TestUtil.createKey(TestUtil.createKeyData(randomPrivKey.getPublicKey(), new EcdsaVerifyKeyManager().getKeyType(), KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), keys[i].getKeyId(), KeyStatusType.ENABLED, keys[i].getOutputPrefixType()))).getPrimitive(PublicKeyVerify.class);
assertThrows(GeneralSecurityException.class, () -> verifier2.verify(sig, plaintext));
}
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class StreamingAeadFactoryTest method testMultipleKeys.
@Test
public void testMultipleKeys() throws Exception {
byte[] primaryKeyValue = Random.randBytes(KDF_KEY_SIZE);
byte[] otherKeyValue = Random.randBytes(KDF_KEY_SIZE);
byte[] anotherKeyValue = Random.randBytes(KDF_KEY_SIZE);
int derivedKeySize = AES_KEY_SIZE;
Key primaryKey = TestUtil.createKey(TestUtil.createAesGcmHkdfStreamingKeyData(primaryKeyValue, derivedKeySize, 512), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
// Another key with a smaller segment size than the primary key
Key otherKey = TestUtil.createKey(TestUtil.createAesCtrHmacStreamingKeyData(otherKeyValue, derivedKeySize, 256), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
// Another key with a larger segment size than the primary key
Key anotherKey = TestUtil.createKey(TestUtil.createAesGcmHkdfStreamingKeyData(anotherKeyValue, derivedKeySize, 1024), 72, KeyStatusType.ENABLED, OutputPrefixType.RAW);
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryKey, otherKey, anotherKey));
StreamingAead streamingAead = StreamingAeadFactory.getPrimitive(keysetHandle);
StreamingAead primaryAead = StreamingAeadFactory.getPrimitive(TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryKey)));
StreamingAead otherAead = StreamingAeadFactory.getPrimitive(TestUtil.createKeysetHandle(TestUtil.createKeyset(otherKey)));
StreamingAead anotherAead = StreamingAeadFactory.getPrimitive(TestUtil.createKeysetHandle(TestUtil.createKeyset(anotherKey)));
StreamingTestUtil.testEncryptionAndDecryption(streamingAead, streamingAead);
StreamingTestUtil.testEncryptionAndDecryption(streamingAead, primaryAead);
StreamingTestUtil.testEncryptionAndDecryption(primaryAead, streamingAead);
StreamingTestUtil.testEncryptionAndDecryption(otherAead, streamingAead);
StreamingTestUtil.testEncryptionAndDecryption(anotherAead, streamingAead);
StreamingTestUtil.testEncryptionAndDecryption(primaryAead, primaryAead);
StreamingTestUtil.testEncryptionAndDecryption(otherAead, otherAead);
StreamingTestUtil.testEncryptionAndDecryption(anotherAead, anotherAead);
IOException expected = assertThrows(IOException.class, () -> StreamingTestUtil.testEncryptionAndDecryption(otherAead, primaryAead));
assertExceptionContains(expected, "No matching key");
IOException expected2 = assertThrows(IOException.class, () -> StreamingTestUtil.testEncryptionAndDecryption(anotherAead, primaryAead));
assertExceptionContains(expected2, "No matching key");
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class DeterministicAeadWrapperTest 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);
PrimitiveSet<DeterministicAead> primitives = TestUtil.createPrimitiveSet(TestUtil.createKeyset(primary, raw, legacy, tink), DeterministicAead.class);
DeterministicAead daead = new DeterministicAeadWrapper().wrap(primitives);
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
PrimitiveSet<DeterministicAead> primitives2 = TestUtil.createPrimitiveSet(TestUtil.createKeyset(raw, legacy, tink), DeterministicAead.class);
DeterministicAead daead2 = new DeterministicAeadWrapper().wrap(primitives2);
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);
primitives2 = TestUtil.createPrimitiveSet(TestUtil.createKeyset(random), DeterministicAead.class);
daead2 = new DeterministicAeadWrapper().wrap(primitives2);
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 DeterministicAeadWrapperTest 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);
PrimitiveSet<DeterministicAead> primitives = TestUtil.createPrimitiveSet(TestUtil.createKeyset(primary, raw, legacy), DeterministicAead.class);
DeterministicAead daead = new DeterministicAeadWrapper().wrap(primitives);
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 DeterministicAeadWrapperTest method testSmallPlaintextWithRawKey.
private static void testSmallPlaintextWithRawKey(int keySize) throws Exception {
Key primary = TestUtil.createKey(TestUtil.createAesSivKeyData(keySize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
PrimitiveSet<DeterministicAead> primitives = TestUtil.createPrimitiveSet(TestUtil.createKeyset(primary), DeterministicAead.class);
DeterministicAead daead = new DeterministicAeadWrapper().wrap(primitives);
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);
}
Aggregations