use of com.google.crypto.tink.proto.testing.VerifyMacResponse in project tink by google.
the class TestingServicesTest method verifyMac_failsOnBadKeyset.
@Test
public void verifyMac_failsOnBadKeyset() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(HmacKeyManager.hmacSha256HalfDigestTemplate());
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
ComputeMacResponse compResponse = computeMac(macStub, keyset, data);
assertThat(compResponse.getErr()).isEmpty();
byte[] macValue = compResponse.getMacValue().toByteArray();
byte[] badKeyset = "bad keyset".getBytes(UTF_8);
VerifyMacResponse verifyResponse = verifyMac(macStub, badKeyset, macValue, data);
assertThat(verifyResponse.getErr()).isNotEmpty();
}
use of com.google.crypto.tink.proto.testing.VerifyMacResponse in project tink by google.
the class TestingServicesTest method computeVerifyMac_success.
@Test
public void computeVerifyMac_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(HmacKeyManager.hmacSha256HalfDigestTemplate());
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
ComputeMacResponse compResponse = computeMac(macStub, keyset, data);
assertThat(compResponse.getErr()).isEmpty();
byte[] macValue = compResponse.getMacValue().toByteArray();
VerifyMacResponse verifyResponse = verifyMac(macStub, keyset, macValue, data);
assertThat(verifyResponse.getErr()).isEmpty();
}
use of com.google.crypto.tink.proto.testing.VerifyMacResponse 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.proto.testing.VerifyMacResponse in project tink by google.
the class TestingServicesTest method verifyMac_failsOnBadMacValue.
@Test
public void verifyMac_failsOnBadMacValue() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(HmacKeyManager.hmacSha256HalfDigestTemplate());
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
VerifyMacResponse verifyResponse = verifyMac(macStub, keyset, "bad mac_value".getBytes(UTF_8), data);
assertThat(verifyResponse.getErr()).isNotEmpty();
}
Aggregations