Search in sources :

Example 1 with HmacKey

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

the class RegistryTest method testGetPrimitive_Hmac_shouldWork.

@Test
public void testGetPrimitive_Hmac_shouldWork() throws Exception {
    KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
    HmacKey hmacKey = (HmacKey) Registry.newKey(template);
    KeyData hmacKeyData = Registry.newKeyData(template);
    Mac mac = Registry.getPrimitive(hmacKeyData);
    assertThat(hmacKey.getKeyValue().size()).isEqualTo(32);
    assertThat(hmacKey.getParams().getTagSize()).isEqualTo(16);
    assertThat(hmacKey.getParams().getHash()).isEqualTo(HashType.SHA256);
    assertThat(hmacKeyData.getTypeUrl()).isEqualTo(MacConfig.HMAC_TYPE_URL);
    // This might break when we add native implementations.
    assertThat(mac.getClass()).isEqualTo(MacJce.class);
}
Also used : HmacKey(com.google.crypto.tink.proto.HmacKey) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 2 with HmacKey

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

the class TestUtil method assertHmacKey.

/**
 * Asserts that {@code key} is generated from {@code keyTemplate}.
 */
public static void assertHmacKey(KeyTemplate keyTemplate, Keyset.Key key) throws Exception {
    assertThat(key.getKeyId()).isGreaterThan(0);
    assertThat(key.getStatus()).isEqualTo(KeyStatusType.ENABLED);
    assertThat(key.getOutputPrefixType()).isEqualTo(OutputPrefixType.TINK);
    assertThat(key.hasKeyData()).isTrue();
    assertThat(key.getKeyData().getTypeUrl()).isEqualTo(keyTemplate.getTypeUrl());
    HmacKeyFormat hmacKeyFormat = HmacKeyFormat.parseFrom(keyTemplate.getValue());
    HmacKey hmacKey = HmacKey.parseFrom(key.getKeyData().getValue());
    assertThat(hmacKey.getParams()).isEqualTo(hmacKeyFormat.getParams());
    assertThat(hmacKey.getKeyValue().size()).isEqualTo(hmacKeyFormat.getKeySize());
}
Also used : HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat) HmacKey(com.google.crypto.tink.proto.HmacKey)

Example 3 with HmacKey

use of com.google.crypto.tink.proto.HmacKey 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)

Example 4 with HmacKey

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

the class AesCtrHmacAeadKeyManager method newKey.

/**
 * @param keyFormat  {@code AesCtrHmacAeadKeyFormat} proto
 * @return new {@code AesCtrHmacAeadKey} proto
 */
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
    if (!(keyFormat instanceof AesCtrHmacAeadKeyFormat)) {
        throw new GeneralSecurityException("expected AesCtrHmacAeadKeyFormat proto");
    }
    AesCtrHmacAeadKeyFormat format = (AesCtrHmacAeadKeyFormat) keyFormat;
    AesCtrKey aesCtrKey = (AesCtrKey) Registry.newKey(AesCtrKeyManager.TYPE_URL, format.getAesCtrKeyFormat());
    HmacKey hmacKey = (HmacKey) Registry.newKey(MacConfig.HMAC_TYPE_URL, format.getHmacKeyFormat());
    return AesCtrHmacAeadKey.newBuilder().setAesCtrKey(aesCtrKey).setHmacKey(hmacKey).setVersion(VERSION).build();
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) GeneralSecurityException(java.security.GeneralSecurityException) AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat) HmacKey(com.google.crypto.tink.proto.HmacKey)

Example 5 with HmacKey

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

the class TestUtil method createAesCtrHmacAeadKeyData.

/**
 * @return a {@code KeyData} containing a {@code AesCtrHmacAeadKey}.
 */
public static KeyData createAesCtrHmacAeadKeyData(byte[] aesCtrKeyValue, int ivSize, byte[] hmacKeyValue, int tagSize) throws Exception {
    AesCtrKey aesCtrKey = createAesCtrKey(aesCtrKeyValue, ivSize);
    HmacKey hmacKey = createHmacKey(hmacKeyValue, tagSize);
    AesCtrHmacAeadKey keyProto = AesCtrHmacAeadKey.newBuilder().setAesCtrKey(aesCtrKey).setHmacKey(hmacKey).build();
    return createKeyData(keyProto, AeadConfig.AES_CTR_HMAC_AEAD_TYPE_URL, KeyData.KeyMaterialType.SYMMETRIC);
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) HmacKey(com.google.crypto.tink.proto.HmacKey) AesCtrHmacAeadKey(com.google.crypto.tink.proto.AesCtrHmacAeadKey)

Aggregations

HmacKey (com.google.crypto.tink.proto.HmacKey)7 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)3 Test (org.junit.Test)3 AesCtrKey (com.google.crypto.tink.proto.AesCtrKey)2 HmacKeyFormat (com.google.crypto.tink.proto.HmacKeyFormat)2 GeneralSecurityException (java.security.GeneralSecurityException)2 DummyAead (com.google.crypto.tink.TestUtil.DummyAead)1 AesCtrHmacAeadKey (com.google.crypto.tink.proto.AesCtrHmacAeadKey)1 AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)1 HashType (com.google.crypto.tink.proto.HashType)1 KeyData (com.google.crypto.tink.proto.KeyData)1 MacJce (com.google.crypto.tink.subtle.MacJce)1 ByteString (com.google.protobuf.ByteString)1 TreeSet (java.util.TreeSet)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1