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 17package services 18 19import ( 20 "context" 21 22 "github.com/google/tink/go/mac" 23 pb "github.com/google/tink/testing/go/protos/testing_api_go_grpc" 24) 25 26// MacService implements the MAC testing service. 27type MacService struct { 28 pb.MacServer 29} 30 31func (s *MacService) Create(ctx context.Context, req *pb.CreationRequest) (*pb.CreationResponse, error) { 32 handle, err := toKeysetHandle(req.GetAnnotatedKeyset()) 33 if err != nil { 34 return &pb.CreationResponse{Err: err.Error()}, nil 35 } 36 _, err = mac.New(handle) 37 if err != nil { 38 return &pb.CreationResponse{Err: err.Error()}, nil 39 } 40 return &pb.CreationResponse{}, nil 41} 42 43func (s *MacService) ComputeMac(ctx context.Context, req *pb.ComputeMacRequest) (*pb.ComputeMacResponse, error) { 44 45 handle, err := toKeysetHandle(req.GetAnnotatedKeyset()) 46 if err != nil { 47 return &pb.ComputeMacResponse{ 48 Result: &pb.ComputeMacResponse_Err{err.Error()}}, nil 49 } 50 primitive, err := mac.New(handle) 51 if err != nil { 52 return &pb.ComputeMacResponse{ 53 Result: &pb.ComputeMacResponse_Err{err.Error()}}, nil 54 } 55 macValue, err := primitive.ComputeMAC(req.Data) 56 if err != nil { 57 return &pb.ComputeMacResponse{ 58 Result: &pb.ComputeMacResponse_Err{err.Error()}}, nil 59 } 60 return &pb.ComputeMacResponse{ 61 Result: &pb.ComputeMacResponse_MacValue{macValue}}, nil 62} 63 64func (s *MacService) VerifyMac(ctx context.Context, req *pb.VerifyMacRequest) (*pb.VerifyMacResponse, error) { 65 66 handle, err := toKeysetHandle(req.GetAnnotatedKeyset()) 67 if err != nil { 68 return &pb.VerifyMacResponse{Err: err.Error()}, nil 69 } 70 primitive, err := mac.New(handle) 71 if err != nil { 72 return &pb.VerifyMacResponse{Err: err.Error()}, nil 73 } 74 err = primitive.VerifyMAC(req.MacValue, req.Data) 75 if err != nil { 76 return &pb.VerifyMacResponse{Err: err.Error()}, nil 77 } 78 return &pb.VerifyMacResponse{}, nil 79} 80