Search in sources :

Example 1 with HmacKeyFormat

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

the class HmacKeyManager method newKey.

/**
 * @param keyFormat {@code HmacKeyFormat} proto
 * @return new {@code HmacKey} proto
 */
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
    if (!(keyFormat instanceof HmacKeyFormat)) {
        throw new GeneralSecurityException("expected HmacKeyFormat proto");
    }
    HmacKeyFormat format = (HmacKeyFormat) keyFormat;
    validate(format);
    return HmacKey.newBuilder().setVersion(VERSION).setParams(format.getParams()).setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))).build();
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat)

Example 2 with HmacKeyFormat

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

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

use of com.google.crypto.tink.proto.HmacKeyFormat 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 5 with HmacKeyFormat

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

the class AeadKeyTemplates method createAesCtrHmacAeadKeyTemplate.

/**
 * @return a {@link KeyTemplate} containing a {@link AesCtrHmacAeadKeyFormat} with some specific
 *     parameters.
 */
public static KeyTemplate createAesCtrHmacAeadKeyTemplate(int aesKeySize, int ivSize, int hmacKeySize, int tagSize, HashType hashType) {
    AesCtrKeyFormat aesCtrKeyFormat = AesCtrKeyFormat.newBuilder().setParams(AesCtrParams.newBuilder().setIvSize(ivSize).build()).setKeySize(aesKeySize).build();
    HmacKeyFormat hmacKeyFormat = HmacKeyFormat.newBuilder().setParams(HmacParams.newBuilder().setHash(hashType).setTagSize(tagSize).build()).setKeySize(hmacKeySize).build();
    AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.newBuilder().setAesCtrKeyFormat(aesCtrKeyFormat).setHmacKeyFormat(hmacKeyFormat).build();
    return KeyTemplate.newBuilder().setValue(format.toByteString()).setTypeUrl(AesCtrHmacAeadKeyManager.TYPE_URL).setOutputPrefixType(OutputPrefixType.TINK).build();
}
Also used : AesCtrKeyFormat(com.google.crypto.tink.proto.AesCtrKeyFormat) HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat) AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)

Aggregations

HmacKeyFormat (com.google.crypto.tink.proto.HmacKeyFormat)8 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)4 Test (org.junit.Test)4 HmacKey (com.google.crypto.tink.proto.HmacKey)2 AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)1 AesCtrKeyFormat (com.google.crypto.tink.proto.AesCtrKeyFormat)1 HashType (com.google.crypto.tink.proto.HashType)1 HmacParams (com.google.crypto.tink.proto.HmacParams)1 ByteString (com.google.protobuf.ByteString)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TreeSet (java.util.TreeSet)1