Search in sources :

Example 1 with PrfSet

use of com.google.crypto.tink.prf.PrfSet 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 PrfSet

use of com.google.crypto.tink.prf.PrfSet in project tink by google.

the class PrfSetServiceImpl method keyIds.

/**
 * Returns the key IDs of the keyset.
 */
@Override
public void keyIds(PrfSetKeyIdsRequest request, StreamObserver<PrfSetKeyIdsResponse> responseObserver) {
    PrfSetKeyIdsResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        PrfSet prfSet = keysetHandle.getPrimitive(PrfSet.class);
        PrfSetKeyIdsResponse.Output output = PrfSetKeyIdsResponse.Output.newBuilder().setPrimaryKeyId(prfSet.getPrimaryId()).addAllKeyId(prfSet.getPrfs().keySet()).build();
        response = PrfSetKeyIdsResponse.newBuilder().setOutput(output).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = PrfSetKeyIdsResponse.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) PrfSet(com.google.crypto.tink.prf.PrfSet) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) PrfSetKeyIdsResponse(com.google.crypto.tink.proto.testing.PrfSetKeyIdsResponse)

Aggregations

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