Search in sources :

Example 1 with Key

use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.

the class PrimitiveSetTest method testAddInvalidKey.

@Test
public void testAddInvalidKey() throws Exception {
    PrimitiveSet<Mac> pset = PrimitiveSet.newPrimitiveSet();
    Key key1 = Key.newBuilder().setKeyId(1).setStatus(KeyStatusType.ENABLED).build();
    try {
        pset.addPrimitive(new DummyMac1(), key1);
        fail("Expected GeneralSecurityException.");
    } catch (GeneralSecurityException e) {
        assertExceptionContains(e, "unknown output prefix type");
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Key(com.google.crypto.tink.proto.Keyset.Key) Test(org.junit.Test)

Example 2 with Key

use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.

the class EciesAeadHkdfPrivateKeyManagerTest method testNewKey.

@Test
public void testNewKey() throws Exception {
    EllipticCurveType curve = EllipticCurveType.NIST_P384;
    HashType hashType = HashType.SHA256;
    EcPointFormat pointFormat = EcPointFormat.UNCOMPRESSED;
    KeyTemplate demKeyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
    byte[] salt = "some salt".getBytes("UTF-8");
    EciesAeadHkdfParams params = HybridKeyTemplates.createEciesAeadHkdfParams(curve, hashType, pointFormat, demKeyTemplate, salt);
    EciesAeadHkdfPrivateKeyManager manager = new EciesAeadHkdfPrivateKeyManager();
    EciesAeadHkdfPrivateKey keyProto = (EciesAeadHkdfPrivateKey) manager.newKey(EciesAeadHkdfKeyFormat.newBuilder().setParams(params).build());
    assertEquals(params, keyProto.getPublicKey().getParams());
    Key primaryPriv = TestUtil.createKey(TestUtil.createKeyData(keyProto, EciesAeadHkdfPrivateKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 8, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    Key primaryPub = TestUtil.createKey(TestUtil.createKeyData(keyProto.getPublicKey(), EciesAeadHkdfPublicKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    KeysetHandle keysetHandlePub = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPub));
    KeysetHandle keysetHandlePriv = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPriv));
    HybridEncrypt hybridEncrypt = HybridEncryptFactory.getPrimitive(keysetHandlePub);
    HybridDecrypt hybridDecrypt = HybridDecryptFactory.getPrimitive(keysetHandlePriv);
    byte[] plaintext = Random.randBytes(20);
    byte[] contextInfo = Random.randBytes(20);
    byte[] ciphertext = hybridEncrypt.encrypt(plaintext, contextInfo);
    assertArrayEquals(plaintext, hybridDecrypt.decrypt(ciphertext, contextInfo));
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) EciesAeadHkdfParams(com.google.crypto.tink.proto.EciesAeadHkdfParams) HybridDecrypt(com.google.crypto.tink.HybridDecrypt) HashType(com.google.crypto.tink.proto.HashType) EcPointFormat(com.google.crypto.tink.proto.EcPointFormat) EciesAeadHkdfPrivateKey(com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey) EllipticCurveType(com.google.crypto.tink.proto.EllipticCurveType) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) EciesAeadHkdfPrivateKey(com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey) Key(com.google.crypto.tink.proto.Keyset.Key) Test(org.junit.Test)

Example 3 with Key

use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.

the class TestUtil method createPrimitiveSet.

/**
 * @return a {@code PrimitiveSet} from a {@code KeySet}
 */
public static <P> PrimitiveSet<P> createPrimitiveSet(Keyset keyset, Class<P> inputClass) throws GeneralSecurityException {
    PrimitiveSet<P> primitives = PrimitiveSet.newPrimitiveSet(inputClass);
    for (Keyset.Key key : keyset.getKeyList()) {
        if (key.getStatus() == KeyStatusType.ENABLED) {
            P primitive = Registry.getPrimitive(key.getKeyData(), inputClass);
            PrimitiveSet.Entry<P> entry = primitives.addPrimitive(primitive, key);
            if (key.getKeyId() == keyset.getPrimaryKeyId()) {
                primitives.setPrimary(entry);
            }
        }
    }
    return primitives;
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Key(com.google.crypto.tink.proto.Keyset.Key) PrimitiveSet(com.google.crypto.tink.PrimitiveSet)

Example 4 with Key

use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.

the class TestUtil method createKeyset.

/**
 * @return a keyset from a list of keys. The first key is primary.
 */
public static Keyset createKeyset(Key primary, Key... keys) throws Exception {
    Keyset.Builder builder = Keyset.newBuilder();
    builder.addKey(primary).setPrimaryKeyId(primary.getKeyId());
    for (Key key : keys) {
        builder.addKey(key);
    }
    return builder.build();
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) RsaSsaPkcs1PublicKey(com.google.crypto.tink.proto.RsaSsaPkcs1PublicKey) EcdsaPrivateKey(com.google.crypto.tink.proto.EcdsaPrivateKey) AesSivKey(com.google.crypto.tink.proto.AesSivKey) AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) ECPublicKey(java.security.interfaces.ECPublicKey) EciesAeadHkdfPublicKey(com.google.crypto.tink.proto.EciesAeadHkdfPublicKey) EcdsaPublicKey(com.google.crypto.tink.proto.EcdsaPublicKey) EciesAeadHkdfPrivateKey(com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey) AesCtrHmacAeadKey(com.google.crypto.tink.proto.AesCtrHmacAeadKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) AesCtrHmacStreamingKey(com.google.crypto.tink.proto.AesCtrHmacStreamingKey) AesGcmHkdfStreamingKey(com.google.crypto.tink.proto.AesGcmHkdfStreamingKey) RsaSsaPssPublicKey(com.google.crypto.tink.proto.RsaSsaPssPublicKey) HkdfPrfKey(com.google.crypto.tink.proto.HkdfPrfKey) AesGcmKey(com.google.crypto.tink.proto.AesGcmKey) HmacKey(com.google.crypto.tink.proto.HmacKey) Key(com.google.crypto.tink.proto.Keyset.Key)

Example 5 with Key

use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.

the class DeterministicAeadFactoryTest method testInvalidKeyMaterial.

@Test
public void testInvalidKeyMaterial() throws Exception {
    Key valid = TestUtil.createKey(TestUtil.createAesSivKeyData(64), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK);
    Key invalid = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(Random.randBytes(16), 12, Random.randBytes(16), 16), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(valid, invalid));
    GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> DeterministicAeadFactory.getPrimitive(keysetHandle));
    assertExceptionContains(e, "com.google.crypto.tink.DeterministicAead not supported");
    // invalid as the primary key.
    KeysetHandle keysetHandle2 = TestUtil.createKeysetHandle(TestUtil.createKeyset(invalid, valid));
    GeneralSecurityException e2 = assertThrows(GeneralSecurityException.class, () -> DeterministicAeadFactory.getPrimitive(keysetHandle2));
    assertExceptionContains(e2, "com.google.crypto.tink.DeterministicAead not supported");
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) GeneralSecurityException(java.security.GeneralSecurityException) Key(com.google.crypto.tink.proto.Keyset.Key) Test(org.junit.Test)

Aggregations

Key (com.google.crypto.tink.proto.Keyset.Key)56 Test (org.junit.Test)44 KeysetHandle (com.google.crypto.tink.KeysetHandle)31 GeneralSecurityException (java.security.GeneralSecurityException)27 Aead (com.google.crypto.tink.Aead)11 DeterministicAead (com.google.crypto.tink.DeterministicAead)10 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)8 Keyset (com.google.crypto.tink.proto.Keyset)7 Mac (com.google.crypto.tink.Mac)6 PublicKeySign (com.google.crypto.tink.PublicKeySign)6 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)6 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)6 EciesAeadHkdfPrivateKey (com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey)6 HybridDecrypt (com.google.crypto.tink.HybridDecrypt)4 HybridEncrypt (com.google.crypto.tink.HybridEncrypt)4 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)4 EcPointFormat (com.google.crypto.tink.proto.EcPointFormat)4 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)4 HashType (com.google.crypto.tink.proto.HashType)4 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)4