Search in sources :

Example 1 with ComputeMacResponse

use of com.google.crypto.tink.proto.testing.ComputeMacResponse 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 ComputeMacResponse

use of com.google.crypto.tink.proto.testing.ComputeMacResponse 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 ComputeMacResponse

use of com.google.crypto.tink.proto.testing.ComputeMacResponse 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 4 with ComputeMacResponse

use of com.google.crypto.tink.proto.testing.ComputeMacResponse in project tink by google.

the class TestingServicesTest method computeMac_failsOnBadKeyset.

@Test
public void computeMac_failsOnBadKeyset() throws Exception {
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    byte[] data = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    ComputeMacResponse compResponse = computeMac(macStub, badKeyset, data);
    assertThat(compResponse.getErr()).isNotEmpty();
}
Also used : ComputeMacResponse(com.google.crypto.tink.proto.testing.ComputeMacResponse) Test(org.junit.Test)

Aggregations

ComputeMacResponse (com.google.crypto.tink.proto.testing.ComputeMacResponse)4 Test (org.junit.Test)3 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)2 VerifyMacResponse (com.google.crypto.tink.proto.testing.VerifyMacResponse)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