Search in sources :

Example 41 with KeyTemplate

use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.

the class MacKeyTemplatesTest method testHMAC_SHA256_256BITTAG.

@Test
public void testHMAC_SHA256_256BITTAG() throws Exception {
    KeyTemplate template = MacKeyTemplates.HMAC_SHA256_256BITTAG;
    assertEquals(HmacKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    HmacKeyFormat format = HmacKeyFormat.parseFrom(template.getValue());
    assertEquals(32, format.getKeySize());
    assertEquals(32, format.getParams().getTagSize());
    assertEquals(HashType.SHA256, format.getParams().getHash());
}
Also used : HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 42 with KeyTemplate

use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.

the class RegistryEciesAeadHkdfDemHelperTest method testGetAead.

@Test
public void testGetAead() throws Exception {
    byte[] plaintext = "some plaintext string".getBytes(UTF_8);
    byte[] associatedData = "some associated data".getBytes(UTF_8);
    int count = 0;
    for (KeyTemplate template : keyTemplates) {
        RegistryEciesAeadHkdfDemHelper helper = new RegistryEciesAeadHkdfDemHelper(template);
        byte[] symmetricKey = Random.randBytes(helper.getSymmetricKeySizeInBytes());
        Aead aead = helper.getAead(symmetricKey);
        byte[] ciphertext = aead.encrypt(plaintext, associatedData);
        byte[] decrypted = aead.decrypt(ciphertext, associatedData);
        assertArrayEquals(plaintext, decrypted);
        // Try using a symmetric key that is too short.
        symmetricKey = Random.randBytes(helper.getSymmetricKeySizeInBytes() - 1);
        try {
            aead = helper.getAead(symmetricKey);
            fail("Symmetric key too short, should have thrown exception:\n" + template.toString());
        } catch (GeneralSecurityException e) {
            // Expected.
            assertExceptionContains(e, "incorrect length");
        }
        // Try using a symmetric key that is too long.
        symmetricKey = Random.randBytes(helper.getSymmetricKeySizeInBytes() + 1);
        try {
            aead = helper.getAead(symmetricKey);
            fail("Symmetric key too long, should have thrown exception:\n" + template.toString());
        } catch (GeneralSecurityException e) {
            // Expected.
            assertExceptionContains(e, "incorrect length");
        }
        count++;
    }
    assertEquals(keyTemplates.length, count);
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Aead(com.google.crypto.tink.Aead) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 43 with KeyTemplate

use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.

the class RegistryEciesAeadHkdfDemHelperTest method testConstructorWithUnsupportedTemplates.

@Test
public void testConstructorWithUnsupportedTemplates() throws Exception {
    RegistryEciesAeadHkdfDemHelper unusedHelper;
    // Unsupported templates.
    int templateCount = 4;
    KeyTemplate[] templates = new KeyTemplate[templateCount];
    templates[0] = AeadKeyTemplates.AES128_EAX;
    templates[1] = AeadKeyTemplates.AES256_EAX;
    templates[2] = AeadKeyTemplates.CHACHA20_POLY1305;
    templates[3] = SignatureKeyTemplates.ECDSA_P256;
    int count = 0;
    for (KeyTemplate template : templates) {
        try {
            unusedHelper = new RegistryEciesAeadHkdfDemHelper(template);
            fail("DEM type not supported, should have thrown exception:\n" + template.toString());
        } catch (GeneralSecurityException e) {
            // Expected.
            assertExceptionContains(e, "unsupported AEAD DEM key type");
            assertExceptionContains(e, template.getTypeUrl());
        }
        count++;
    }
    assertEquals(templateCount, count);
    // An inconsistent template.
    KeyTemplate template = KeyTemplate.newBuilder().setTypeUrl(AeadKeyTemplates.AES128_CTR_HMAC_SHA256.getTypeUrl()).setValue(SignatureKeyTemplates.ECDSA_P256.getValue()).build();
    try {
        unusedHelper = new RegistryEciesAeadHkdfDemHelper(template);
        fail("Inconsistent template, should have thrown exception:\n" + template.toString());
    } catch (GeneralSecurityException e) {
    // Expected.
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 44 with KeyTemplate

use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.

the class HybridEncryptFactoryTest method testBasicEncryption.

@Test
public void testBasicEncryption() throws Exception {
    EllipticCurveType curve = EllipticCurveType.NIST_P384;
    HashType hashType = HashType.SHA256;
    EcPointFormat primaryPointFormat = EcPointFormat.UNCOMPRESSED;
    EcPointFormat rawPointFormat = EcPointFormat.COMPRESSED;
    KeyTemplate primaryDemKeyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
    KeyTemplate rawDemKeyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
    byte[] primarySalt = "some salt".getBytes("UTF-8");
    byte[] rawSalt = "other salt".getBytes("UTF-8");
    EciesAeadHkdfPrivateKey primaryPrivProto = TestUtil.generateEciesAeadHkdfPrivKey(curve, hashType, primaryPointFormat, primaryDemKeyTemplate, primarySalt);
    Key primaryPriv = TestUtil.createKey(TestUtil.createKeyData(primaryPrivProto, EciesAeadHkdfPrivateKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 8, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    Key primaryPub = TestUtil.createKey(TestUtil.createKeyData(primaryPrivProto.getPublicKey(), EciesAeadHkdfPublicKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    EciesAeadHkdfPrivateKey rawPrivProto = TestUtil.generateEciesAeadHkdfPrivKey(curve, hashType, rawPointFormat, rawDemKeyTemplate, rawSalt);
    Key rawPriv = TestUtil.createKey(TestUtil.createKeyData(rawPrivProto, EciesAeadHkdfPrivateKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 11, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    Key rawPub = TestUtil.createKey(TestUtil.createKeyData(rawPrivProto.getPublicKey(), EciesAeadHkdfPublicKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    KeysetHandle keysetHandlePub = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPub, rawPub));
    KeysetHandle keysetHandlePriv = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPriv, rawPriv));
    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) 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 45 with KeyTemplate

use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.

the class HybridKeyTemplatesTest method testECIES_P256_HKDF_HMAC_SHA256_AES128_GCM.

@Test
public void testECIES_P256_HKDF_HMAC_SHA256_AES128_GCM() throws Exception {
    KeyTemplate template = HybridKeyTemplates.ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM;
    assertEquals(EciesAeadHkdfPrivateKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    EciesAeadHkdfKeyFormat format = EciesAeadHkdfKeyFormat.parseFrom(template.getValue());
    assertTrue(format.hasParams());
    assertTrue(format.getParams().hasKemParams());
    assertTrue(format.getParams().hasDemParams());
    assertTrue(format.getParams().getDemParams().hasAeadDem());
    assertEquals(EcPointFormat.UNCOMPRESSED, format.getParams().getEcPointFormat());
    EciesHkdfKemParams kemParams = format.getParams().getKemParams();
    assertEquals(EllipticCurveType.NIST_P256, kemParams.getCurveType());
    assertEquals(HashType.SHA256, kemParams.getHkdfHashType());
    assertTrue(kemParams.getHkdfSalt().isEmpty());
    assertEquals(AeadKeyTemplates.AES128_GCM.toString(), format.getParams().getDemParams().getAeadDem().toString());
}
Also used : EciesAeadHkdfKeyFormat(com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat) EciesHkdfKemParams(com.google.crypto.tink.proto.EciesHkdfKemParams) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Aggregations

KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)82 Test (org.junit.Test)79 GeneralSecurityException (java.security.GeneralSecurityException)18 ByteString (com.google.protobuf.ByteString)12 KeyData (com.google.crypto.tink.proto.KeyData)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 HashType (com.google.crypto.tink.proto.HashType)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 TreeSet (java.util.TreeSet)7 Keyset (com.google.crypto.tink.proto.Keyset)6 KeysetHandle (com.google.crypto.tink.KeysetHandle)5 DummyAead (com.google.crypto.tink.TestUtil.DummyAead)5 AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)4 AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)4 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)4 EcdsaKeyFormat (com.google.crypto.tink.proto.EcdsaKeyFormat)4 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)4 HmacKeyFormat (com.google.crypto.tink.proto.HmacKeyFormat)4 AesCtrHmacStreamingKeyFormat (com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat)3 AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)3