Search in sources :

Example 1 with VerifyMacResponse

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();
}
Also used : KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) VerifyMacResponse(com.google.crypto.tink.proto.testing.VerifyMacResponse) ComputeMacResponse(com.google.crypto.tink.proto.testing.ComputeMacResponse) Test(org.junit.Test)

Example 2 with VerifyMacResponse

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();
}
Also used : KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) VerifyMacResponse(com.google.crypto.tink.proto.testing.VerifyMacResponse) ComputeMacResponse(com.google.crypto.tink.proto.testing.ComputeMacResponse) Test(org.junit.Test)

Example 3 with VerifyMacResponse

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();
}
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 4 with VerifyMacResponse

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();
}
Also used : KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) VerifyMacResponse(com.google.crypto.tink.proto.testing.VerifyMacResponse) Test(org.junit.Test)

Aggregations

VerifyMacResponse (com.google.crypto.tink.proto.testing.VerifyMacResponse)4 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)3 Test (org.junit.Test)3 ComputeMacResponse (com.google.crypto.tink.proto.testing.ComputeMacResponse)2 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 Mac (com.google.crypto.tink.Mac)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1