Search in sources :

Example 1 with AesCmacKey

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

the class AesCmacKeyManagerTest method validateKey_notValid_throws.

@Test
public void validateKey_notValid_throws() throws Exception {
    AesCmacKeyManager manager = new AesCmacKeyManager();
    AesCmacKey validKey = manager.keyFactory().createKey(makeAesCmacKeyFormat(32, 16));
    assertThrows(GeneralSecurityException.class, () -> manager.validateKey(AesCmacKey.newBuilder(validKey).setKeyValue(ByteString.copyFrom(Random.randBytes(16))).build()));
    assertThrows(GeneralSecurityException.class, () -> manager.validateKey(AesCmacKey.newBuilder(validKey).setKeyValue(ByteString.copyFrom(Random.randBytes(64))).build()));
    assertThrows(GeneralSecurityException.class, () -> manager.validateKey(AesCmacKey.newBuilder(validKey).setParams(AesCmacParams.newBuilder(validKey.getParams()).setTagSize(0).build()).build()));
    assertThrows(GeneralSecurityException.class, () -> manager.validateKey(AesCmacKey.newBuilder(validKey).setParams(AesCmacParams.newBuilder(validKey.getParams()).setTagSize(9).build()).build()));
    assertThrows(GeneralSecurityException.class, () -> manager.validateKey(AesCmacKey.newBuilder(validKey).setParams(AesCmacParams.newBuilder(validKey.getParams()).setTagSize(17).build()).build()));
    assertThrows(GeneralSecurityException.class, () -> manager.validateKey(AesCmacKey.newBuilder(validKey).setParams(AesCmacParams.newBuilder(validKey.getParams()).setTagSize(32).build()).build()));
}
Also used : AesCmacKey(com.google.crypto.tink.proto.AesCmacKey) Test(org.junit.Test)

Example 2 with AesCmacKey

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

the class AesCmacKeyManagerTest method createKey_checkValues.

@Test
public void createKey_checkValues() throws Exception {
    AesCmacKeyFormat keyFormat = makeAesCmacKeyFormat(32, 16);
    AesCmacKey key = new AesCmacKeyManager().keyFactory().createKey(keyFormat);
    assertThat(key.getKeyValue()).hasSize(keyFormat.getKeySize());
    assertThat(key.getParams().getTagSize()).isEqualTo(keyFormat.getParams().getTagSize());
}
Also used : AesCmacKey(com.google.crypto.tink.proto.AesCmacKey) AesCmacKeyFormat(com.google.crypto.tink.proto.AesCmacKeyFormat) Test(org.junit.Test)

Example 3 with AesCmacKey

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

the class AesCmacKeyManagerTest method getPrimitive_works.

@Test
public void getPrimitive_works() throws Exception {
    AesCmacKeyManager manager = new AesCmacKeyManager();
    AesCmacKey validKey = manager.keyFactory().createKey(makeAesCmacKeyFormat(32, 16));
    Mac managerMac = manager.getPrimitive(validKey, Mac.class);
    Mac directMac = new PrfMac(new PrfAesCmac(validKey.getKeyValue().toByteArray()), validKey.getParams().getTagSize());
    byte[] message = Random.randBytes(50);
    managerMac.verifyMac(directMac.computeMac(message), message);
}
Also used : PrfAesCmac(com.google.crypto.tink.subtle.PrfAesCmac) PrfMac(com.google.crypto.tink.subtle.PrfMac) AesCmacKey(com.google.crypto.tink.proto.AesCmacKey) Mac(com.google.crypto.tink.Mac) PrfMac(com.google.crypto.tink.subtle.PrfMac) Test(org.junit.Test)

Example 4 with AesCmacKey

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

the class AesCmacKeyManagerTest method validateKey_wrongVersion_throws.

@Test
public void validateKey_wrongVersion_throws() throws Exception {
    AesCmacKeyManager manager = new AesCmacKeyManager();
    AesCmacKey validKey = manager.keyFactory().createKey(makeAesCmacKeyFormat(32, 16));
    assertThrows(GeneralSecurityException.class, () -> manager.validateKey(AesCmacKey.newBuilder(validKey).setVersion(1).build()));
}
Also used : AesCmacKey(com.google.crypto.tink.proto.AesCmacKey) Test(org.junit.Test)

Aggregations

AesCmacKey (com.google.crypto.tink.proto.AesCmacKey)4 Test (org.junit.Test)4 Mac (com.google.crypto.tink.Mac)1 AesCmacKeyFormat (com.google.crypto.tink.proto.AesCmacKeyFormat)1 PrfAesCmac (com.google.crypto.tink.subtle.PrfAesCmac)1 PrfMac (com.google.crypto.tink.subtle.PrfMac)1