Search in sources :

Example 36 with KeyTemplate

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

the class AeadKeyTemplatesTest method testAES128_CTR_HMAC_SHA256.

@Test
public void testAES128_CTR_HMAC_SHA256() throws Exception {
    KeyTemplate template = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
    assertEquals(AesCtrHmacAeadKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.parseFrom(template.getValue());
    assertTrue(format.hasAesCtrKeyFormat());
    assertTrue(format.getAesCtrKeyFormat().hasParams());
    assertEquals(16, format.getAesCtrKeyFormat().getKeySize());
    assertEquals(16, format.getAesCtrKeyFormat().getParams().getIvSize());
    assertTrue(format.hasHmacKeyFormat());
    assertTrue(format.getHmacKeyFormat().hasParams());
    assertEquals(32, format.getHmacKeyFormat().getKeySize());
    assertEquals(16, format.getHmacKeyFormat().getParams().getTagSize());
    assertEquals(HashType.SHA256, format.getHmacKeyFormat().getParams().getHash());
}
Also used : AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 37 with KeyTemplate

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

the class AeadKeyTemplatesTest method testCreateAesGcmKeyTemplate.

@Test
public void testCreateAesGcmKeyTemplate() throws Exception {
    // Intentionally using "weird" or invalid values for parameters,
    // to test that the function correctly puts them in the resulting template.
    int keySize = 42;
    KeyTemplate template = AeadKeyTemplates.createAesGcmKeyTemplate(keySize);
    assertEquals(AesGcmKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    AesGcmKeyFormat format = AesGcmKeyFormat.parseFrom(template.getValue());
    assertEquals(keySize, format.getKeySize());
}
Also used : AesGcmKeyFormat(com.google.crypto.tink.proto.AesGcmKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 38 with KeyTemplate

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

the class AeadKeyTemplatesTest method testCreateAesCtrHmacAeadKeyTemplate.

@Test
public void testCreateAesCtrHmacAeadKeyTemplate() throws Exception {
    // Intentionally using "weird" or invalid values for parameters,
    // to test that the function correctly puts them in the resulting template.
    int aesKeySize = 42;
    int ivSize = 72;
    int hmacKeySize = 24;
    int tagSize = 27;
    HashType hashType = HashType.SHA224;
    KeyTemplate template = AeadKeyTemplates.createAesCtrHmacAeadKeyTemplate(aesKeySize, ivSize, hmacKeySize, tagSize, hashType);
    assertEquals(AesCtrHmacAeadKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
    AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.parseFrom(template.getValue());
    assertTrue(format.hasAesCtrKeyFormat());
    assertTrue(format.getAesCtrKeyFormat().hasParams());
    assertEquals(aesKeySize, format.getAesCtrKeyFormat().getKeySize());
    assertEquals(ivSize, format.getAesCtrKeyFormat().getParams().getIvSize());
    assertTrue(format.hasHmacKeyFormat());
    assertTrue(format.getHmacKeyFormat().hasParams());
    assertEquals(hmacKeySize, format.getHmacKeyFormat().getKeySize());
    assertEquals(tagSize, format.getHmacKeyFormat().getParams().getTagSize());
    assertEquals(hashType, format.getHmacKeyFormat().getParams().getHash());
}
Also used : HashType(com.google.crypto.tink.proto.HashType) AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 39 with KeyTemplate

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

the class Ed25519PrivateKeyManagerTest method testBasic.

@Test
public void testBasic() throws Exception {
    Ed25519PrivateKeyManager manager = new Ed25519PrivateKeyManager();
    KeyTemplate template = SignatureKeyTemplates.ED25519;
    MessageLite key = manager.newKey(template);
    assertTrue(key instanceof Ed25519PrivateKey);
    Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
    assertEquals(32, keyProto.getKeyValue().size());
    PublicKeySign signer = manager.getPrimitive(key);
    assertTrue(signer instanceof Ed25519Sign);
    byte[] message = Random.randBytes(20);
    byte[] signature = signer.sign(message);
    assertEquals(64, signature.length);
    Ed25519PublicKeyManager publicKeyManager = new Ed25519PublicKeyManager();
    PublicKeyVerify verifier = publicKeyManager.getPrimitive(keyProto.getPublicKey());
    assertTrue(verifier instanceof Ed25519Verify);
    try {
        verifier.verify(signature, message);
    } catch (GeneralSecurityException e) {
        fail("Do not expect GeneralSecurityException: " + e);
    }
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519Verify(com.google.crypto.tink.subtle.Ed25519Verify) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) Ed25519Sign(com.google.crypto.tink.subtle.Ed25519Sign) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) MessageLite(com.google.protobuf.MessageLite) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test)

Example 40 with KeyTemplate

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

the class HmacKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    HmacKeyManager keyManager = new HmacKeyManager();
    HmacKeyFormat hmacKeyFormat = HmacKeyFormat.newBuilder().setParams(HmacParams.newBuilder().setHash(HashType.SHA256).setTagSize(16).build()).setKeySize(32).build();
    ByteString serialized = ByteString.copyFrom(hmacKeyFormat.toByteArray());
    KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(HmacKeyManager.TYPE_URL).setValue(serialized).build();
    // Calls newKey multiple times and make sure that we get different HmacKey each time.
    Set<String> keys = new TreeSet<String>();
    int numTests = 27;
    for (int i = 0; i < numTests / 3; i++) {
        HmacKey key = (HmacKey) keyManager.newKey(hmacKeyFormat);
        assertEquals(32, key.getKeyValue().toByteArray().length);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        key = (HmacKey) keyManager.newKey(serialized);
        assertEquals(32, key.getKeyValue().toByteArray().length);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        key = HmacKey.parseFrom(keyManager.newKeyData(keyTemplate.getValue()).getValue());
        assertEquals(32, key.getKeyValue().toByteArray().length);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
    }
    assertEquals(numTests, keys.size());
}
Also used : ByteString(com.google.protobuf.ByteString) TreeSet(java.util.TreeSet) HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat) ByteString(com.google.protobuf.ByteString) HmacKey(com.google.crypto.tink.proto.HmacKey) 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