Search in sources :

Example 1 with PrfSetComputeResponse

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

the class PrfSetServiceImpl method compute.

/**
 * Computes the output of one PRF.
 */
@Override
public void compute(PrfSetComputeRequest request, StreamObserver<PrfSetComputeResponse> responseObserver) {
    PrfSetComputeResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        PrfSet prfSet = keysetHandle.getPrimitive(PrfSet.class);
        Map<Integer, Prf> prfs = prfSet.getPrfs();
        if (!prfs.containsKey(request.getKeyId())) {
            response = PrfSetComputeResponse.newBuilder().setErr("Unknown Key ID.").build();
        } else {
            byte[] output = prfs.get(request.getKeyId()).compute(request.getInputData().toByteArray(), request.getOutputLength());
            response = PrfSetComputeResponse.newBuilder().setOutput(ByteString.copyFrom(output)).build();
        }
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = PrfSetComputeResponse.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) Prf(com.google.crypto.tink.prf.Prf) PrfSet(com.google.crypto.tink.prf.PrfSet) GeneralSecurityException(java.security.GeneralSecurityException) PrfSetComputeResponse(com.google.crypto.tink.proto.testing.PrfSetComputeResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException)

Example 2 with PrfSetComputeResponse

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

the class TestingServicesTest method computePrf_success.

@Test
public void computePrf_success() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(HmacPrfKeyManager.hmacSha256Template());
    byte[] inputData = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    int outputLength = 15;
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    PrfSetKeyIdsResponse keyIdsResponse = keyIds(prfSetStub, keyset);
    assertThat(keyIdsResponse.getErr()).isEmpty();
    int primaryKeyId = keyIdsResponse.getOutput().getPrimaryKeyId();
    PrfSetComputeResponse computeResponse = computePrf(prfSetStub, keyset, primaryKeyId, inputData, outputLength);
    assertThat(computeResponse.getErr()).isEmpty();
    assertThat(computeResponse.getOutput().size()).isEqualTo(outputLength);
}
Also used : PrfSetComputeResponse(com.google.crypto.tink.proto.testing.PrfSetComputeResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) PrfSetKeyIdsResponse(com.google.crypto.tink.proto.testing.PrfSetKeyIdsResponse) Test(org.junit.Test)

Example 3 with PrfSetComputeResponse

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

the class TestingServicesTest method computePrf_failsOnBadOutputLength.

@Test
public void computePrf_failsOnBadOutputLength() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(HmacPrfKeyManager.hmacSha256Template());
    byte[] inputData = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    int outputLength = 12345;
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    PrfSetKeyIdsResponse keyIdsResponse = keyIds(prfSetStub, keyset);
    assertThat(keyIdsResponse.getErr()).isEmpty();
    int primaryKeyId = keyIdsResponse.getOutput().getPrimaryKeyId();
    PrfSetComputeResponse computeResponse = computePrf(prfSetStub, keyset, primaryKeyId, inputData, outputLength);
    assertThat(computeResponse.getErr()).isNotEmpty();
}
Also used : PrfSetComputeResponse(com.google.crypto.tink.proto.testing.PrfSetComputeResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) PrfSetKeyIdsResponse(com.google.crypto.tink.proto.testing.PrfSetKeyIdsResponse) Test(org.junit.Test)

Example 4 with PrfSetComputeResponse

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

the class TestingServicesTest method computePrf_failsOnUnknownKeyId.

@Test
public void computePrf_failsOnUnknownKeyId() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(HmacPrfKeyManager.hmacSha256Template());
    byte[] inputData = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    int outputLength = 15;
    int badKeyId = 123456789;
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    PrfSetComputeResponse computeResponse = computePrf(prfSetStub, keyset, badKeyId, inputData, outputLength);
    assertThat(computeResponse.getErr()).isNotEmpty();
}
Also used : PrfSetComputeResponse(com.google.crypto.tink.proto.testing.PrfSetComputeResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Aggregations

PrfSetComputeResponse (com.google.crypto.tink.proto.testing.PrfSetComputeResponse)4 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)3 Test (org.junit.Test)3 PrfSetKeyIdsResponse (com.google.crypto.tink.proto.testing.PrfSetKeyIdsResponse)2 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 Prf (com.google.crypto.tink.prf.Prf)1 PrfSet (com.google.crypto.tink.prf.PrfSet)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1