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