Search in sources :

Example 6 with HmacKey

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

the class RegistryTest method testGetKeyManager_wrongType_shouldThrowException.

@Test
public void testGetKeyManager_wrongType_shouldThrowException() throws Exception {
    // TODO(thaidn): make this assignment throw some exception.
    KeyManager<Aead> wrongType = Registry.getKeyManager(MacConfig.HMAC_TYPE_URL);
    KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
    HmacKey hmacKey = (HmacKey) Registry.newKey(template);
    try {
        Aead unused = wrongType.getPrimitive(hmacKey);
        fail("Expected ClassCastException");
    } catch (ClassCastException e) {
        assertExceptionContains(e, "MacJce cannot be cast to com.google.crypto.tink.Aead");
    }
}
Also used : DummyAead(com.google.crypto.tink.TestUtil.DummyAead) HmacKey(com.google.crypto.tink.proto.HmacKey) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 7 with HmacKey

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

the class HmacKeyManager method getPrimitive.

/**
 * @param key {@code HmacKey} proto
 */
@Override
public Mac getPrimitive(MessageLite key) throws GeneralSecurityException {
    if (!(key instanceof HmacKey)) {
        throw new GeneralSecurityException("expected HmacKey proto");
    }
    HmacKey keyProto = (HmacKey) key;
    validate(keyProto);
    HashType hash = keyProto.getParams().getHash();
    byte[] keyValue = keyProto.getKeyValue().toByteArray();
    SecretKeySpec keySpec = new SecretKeySpec(keyValue, "HMAC");
    int tagSize = keyProto.getParams().getTagSize();
    switch(hash) {
        case SHA1:
            return new MacJce("HMACSHA1", keySpec, tagSize);
        case SHA256:
            return new MacJce("HMACSHA256", keySpec, tagSize);
        case SHA512:
            return new MacJce("HMACSHA512", keySpec, tagSize);
        default:
            throw new GeneralSecurityException("unknown hash");
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) HashType(com.google.crypto.tink.proto.HashType) MacJce(com.google.crypto.tink.subtle.MacJce) HmacKey(com.google.crypto.tink.proto.HmacKey)

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