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/daead" 23 pb "github.com/google/tink/testing/go/protos/testing_api_go_grpc" 24) 25 26// DeterministicAEADService implements the DeterministicAead testing service. 27type DeterministicAEADService struct { 28 pb.DeterministicAeadServer 29} 30 31func (s *DeterministicAEADService) 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 = daead.New(handle) 37 if err != nil { 38 return &pb.CreationResponse{Err: err.Error()}, nil 39 } 40 return &pb.CreationResponse{}, nil 41} 42 43func (s *DeterministicAEADService) EncryptDeterministically(ctx context.Context, req *pb.DeterministicAeadEncryptRequest) (*pb.DeterministicAeadEncryptResponse, error) { 44 handle, err := toKeysetHandle(req.GetAnnotatedKeyset()) 45 if err != nil { 46 return nil, err 47 } 48 cipher, err := daead.New(handle) 49 if err != nil { 50 return nil, err 51 } 52 ciphertext, err := cipher.EncryptDeterministically(req.Plaintext, req.AssociatedData) 53 if err != nil { 54 return &pb.DeterministicAeadEncryptResponse{ 55 Result: &pb.DeterministicAeadEncryptResponse_Err{err.Error()}}, nil 56 } 57 return &pb.DeterministicAeadEncryptResponse{ 58 Result: &pb.DeterministicAeadEncryptResponse_Ciphertext{ciphertext}}, nil 59} 60 61func (s *DeterministicAEADService) DecryptDeterministically(ctx context.Context, req *pb.DeterministicAeadDecryptRequest) (*pb.DeterministicAeadDecryptResponse, error) { 62 handle, err := toKeysetHandle(req.GetAnnotatedKeyset()) 63 if err != nil { 64 return nil, err 65 } 66 cipher, err := daead.New(handle) 67 if err != nil { 68 return nil, err 69 } 70 plaintext, err := cipher.DecryptDeterministically(req.Ciphertext, req.AssociatedData) 71 if err != nil { 72 return &pb.DeterministicAeadDecryptResponse{ 73 Result: &pb.DeterministicAeadDecryptResponse_Err{err.Error()}}, nil 74 } 75 return &pb.DeterministicAeadDecryptResponse{ 76 Result: &pb.DeterministicAeadDecryptResponse_Plaintext{plaintext}}, nil 77} 78