• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 package com.google.crypto.tink.testing;
18 
19 import com.google.crypto.tink.Mac;
20 import com.google.crypto.tink.testing.proto.ComputeMacRequest;
21 import com.google.crypto.tink.testing.proto.ComputeMacResponse;
22 import com.google.crypto.tink.testing.proto.CreationRequest;
23 import com.google.crypto.tink.testing.proto.CreationResponse;
24 import com.google.crypto.tink.testing.proto.MacGrpc.MacImplBase;
25 import com.google.crypto.tink.testing.proto.VerifyMacRequest;
26 import com.google.crypto.tink.testing.proto.VerifyMacResponse;
27 import com.google.protobuf.ByteString;
28 import io.grpc.stub.StreamObserver;
29 import java.security.GeneralSecurityException;
30 
31 /** Implements a gRPC MAC Testing service. */
32 public final class MacServiceImpl extends MacImplBase {
33 
MacServiceImpl()34   public MacServiceImpl() throws GeneralSecurityException {
35   }
36 
37   @Override
create(CreationRequest request, StreamObserver<CreationResponse> responseObserver)38   public void create(CreationRequest request, StreamObserver<CreationResponse> responseObserver) {
39     Util.createPrimitiveForRpc(request, responseObserver, Mac.class);
40   }
41 
computeMac( ComputeMacRequest request)42   private ComputeMacResponse computeMac(
43       ComputeMacRequest request) throws GeneralSecurityException {
44     try {
45       Mac mac =
46           Util.parseBinaryProtoKeyset(request.getAnnotatedKeyset())
47               .getPrimitive(Mac.class);
48       byte[] macValue = mac.computeMac(request.getData().toByteArray());
49       return ComputeMacResponse.newBuilder().setMacValue(ByteString.copyFrom(macValue)).build();
50     } catch (GeneralSecurityException e)  {
51       return ComputeMacResponse.newBuilder().setErr(e.toString()).build();
52     }
53   }
54 
55   @Override
computeMac( ComputeMacRequest request, StreamObserver<ComputeMacResponse> responseObserver)56   public void computeMac(
57       ComputeMacRequest request,
58       StreamObserver<ComputeMacResponse> responseObserver) {
59     try {
60       responseObserver.onNext(computeMac(request));
61       responseObserver.onCompleted();
62     } catch (GeneralSecurityException e) {
63       responseObserver.onError(e);
64     }
65   }
66 
verifyMac(VerifyMacRequest request)67   private VerifyMacResponse verifyMac(VerifyMacRequest request) throws GeneralSecurityException {
68     try {
69       Mac mac =
70           Util.parseBinaryProtoKeyset(request.getAnnotatedKeyset())
71               .getPrimitive(Mac.class);
72       mac.verifyMac(request.getMacValue().toByteArray(), request.getData().toByteArray());
73       return VerifyMacResponse.getDefaultInstance();
74     } catch (GeneralSecurityException e) {
75       return VerifyMacResponse.newBuilder().setErr(e.toString()).build();
76     }
77   }
78 
79   @Override
verifyMac( VerifyMacRequest request, StreamObserver<VerifyMacResponse> responseObserver)80   public void verifyMac(
81       VerifyMacRequest request,
82       StreamObserver<VerifyMacResponse> responseObserver) {
83     try {
84       responseObserver.onNext(verifyMac(request));
85       responseObserver.onCompleted();
86     } catch (GeneralSecurityException e) {
87       responseObserver.onError(e);
88     }
89   }
90 }
91