Search in sources :

Example 11 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class HmacKeyManagerTest method getPrimitive_worksForSha512.

@Test
public void getPrimitive_worksForSha512() throws Exception {
    HmacKey validKey = factory.createKey(makeHmacKeyFormat(16, 33, HashType.SHA512));
    Mac managerMac = manager.getPrimitive(validKey, Mac.class);
    Mac directMac = new PrfMac(new PrfHmacJce("HMACSHA512", new SecretKeySpec(validKey.getKeyValue().toByteArray(), "HMAC")), 33);
    byte[] message = Random.randBytes(50);
    managerMac.verifyMac(directMac.computeMac(message), message);
}
Also used : PrfMac(com.google.crypto.tink.subtle.PrfMac) SecretKeySpec(javax.crypto.spec.SecretKeySpec) PrfHmacJce(com.google.crypto.tink.subtle.PrfHmacJce) HmacKey(com.google.crypto.tink.proto.HmacKey) Mac(com.google.crypto.tink.Mac) PrfMac(com.google.crypto.tink.subtle.PrfMac) Test(org.junit.Test)

Example 12 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class MacServiceImpl method computeMac.

/**
 * Encrypts a message.
 */
@Override
public void computeMac(ComputeMacRequest request, StreamObserver<ComputeMacResponse> responseObserver) {
    ComputeMacResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        Mac mac = keysetHandle.getPrimitive(Mac.class);
        byte[] macValue = mac.computeMac(request.getData().toByteArray());
        response = ComputeMacResponse.newBuilder().setMacValue(ByteString.copyFrom(macValue)).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = ComputeMacResponse.newBuilder().setErr(e.toString()).build();
    } catch (IOException e) {
        responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
        return;
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) ComputeMacResponse(com.google.crypto.tink.proto.testing.ComputeMacResponse) Mac(com.google.crypto.tink.Mac)

Example 13 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class MacServiceImpl method verifyMac.

/**
 * Decrypts a message.
 */
@Override
public void verifyMac(VerifyMacRequest request, StreamObserver<VerifyMacResponse> responseObserver) {
    VerifyMacResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        Mac mac = keysetHandle.getPrimitive(Mac.class);
        mac.verifyMac(request.getMacValue().toByteArray(), request.getData().toByteArray());
        response = VerifyMacResponse.getDefaultInstance();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = VerifyMacResponse.newBuilder().setErr(e.toString()).build();
    } catch (IOException e) {
        responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
        return;
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) VerifyMacResponse(com.google.crypto.tink.proto.testing.VerifyMacResponse) Mac(com.google.crypto.tink.Mac)

Example 14 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class PrfHmacJceTest method testMacTestVectors.

@Test
public void testMacTestVectors() throws Exception {
    Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
    for (MacTestVector t : HMAC_TEST_VECTORS) {
        Mac mac = new PrfMac(new PrfHmacJce(t.algName, new SecretKeySpec(t.key, "HMAC")), t.tag.length);
        assertArrayEquals(t.tag, mac.computeMac(t.message));
        try {
            mac.verifyMac(t.tag, t.message);
        } catch (GeneralSecurityException e) {
            throw new AssertionError("Valid MAC, should not throw exception", e);
        }
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Example 15 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class PrfHmacJceTest method testPrfPrefixOfMac.

@Test
public void testPrfPrefixOfMac() throws Exception {
    Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
    for (MacTestVector t : HMAC_TEST_VECTORS) {
        Prf prf = new PrfHmacJce(t.algName, new SecretKeySpec(t.key, "HMAC"));
        Mac mac = new PrfMac(prf, t.tag.length);
        byte[] prBytes = prf.compute(t.message, t.tag.length - 1);
        byte[] tag = mac.computeMac(t.message);
        assertEquals(prBytes.length, t.tag.length - 1);
        assertArrayEquals(prBytes, Arrays.copyOf(tag, prBytes.length));
    }
}
Also used : Prf(com.google.crypto.tink.prf.Prf) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Aggregations

Mac (com.google.crypto.tink.Mac)34 Test (org.junit.Test)29 GeneralSecurityException (java.security.GeneralSecurityException)19 SecretKeySpec (javax.crypto.spec.SecretKeySpec)15 KeysetHandle (com.google.crypto.tink.KeysetHandle)7 Key (com.google.crypto.tink.proto.Keyset.Key)6 PrfMac (com.google.crypto.tink.subtle.PrfMac)4 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)3 HmacKey (com.google.crypto.tink.proto.HmacKey)3 PrfHmacJce (com.google.crypto.tink.subtle.PrfHmacJce)3 IOException (java.io.IOException)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 Aead (com.google.crypto.tink.Aead)1 KeyManager (com.google.crypto.tink.KeyManager)1 Prf (com.google.crypto.tink.prf.Prf)1 AesCmacKey (com.google.crypto.tink.proto.AesCmacKey)1 KeyTypeEntry (com.google.crypto.tink.proto.KeyTypeEntry)1 RegistryConfig (com.google.crypto.tink.proto.RegistryConfig)1 ComputeMacResponse (com.google.crypto.tink.proto.testing.ComputeMacResponse)1 VerifyMacResponse (com.google.crypto.tink.proto.testing.VerifyMacResponse)1