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);
}
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();
}
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();
}
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);
}
}
}
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));
}
}
Aggregations