tholenst | 9a1e975 | 2021-04-13 23:13:20 -0700 | [diff] [blame] | 1 | // Copyright 2020 Google LLC |
| 2 | // |
juerg | 72d1814 | 2020-06-23 10:00:08 -0700 | [diff] [blame] | 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 services |
| 18 | |
| 19 | import ( |
juerg | 72d1814 | 2020-06-23 10:00:08 -0700 | [diff] [blame] | 20 | "context" |
| 21 | |
| 22 | "github.com/google/tink/go/hybrid" |
juerg | c7c1d53 | 2023-04-13 09:43:21 -0700 | [diff] [blame] | 23 | pb "github.com/google/tink/testing/go/protos/testing_api_go_grpc" |
juerg | 72d1814 | 2020-06-23 10:00:08 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // HybridService implements the Hybrid encryption and decryption testing service. |
| 27 | type HybridService struct { |
juerg | 3c90b1a | 2021-12-16 04:21:47 -0800 | [diff] [blame] | 28 | pb.HybridServer |
juerg | 72d1814 | 2020-06-23 10:00:08 -0700 | [diff] [blame] | 29 | } |
| 30 | |
tholenst | 53ee0e5 | 2022-08-22 07:59:30 -0700 | [diff] [blame] | 31 | func (s *HybridService) CreateHybridEncrypt(ctx context.Context, req *pb.CreationRequest) (*pb.CreationResponse, error) { |
felobato | 912db1c | 2022-11-16 17:02:44 -0800 | [diff] [blame] | 32 | handle, err := toKeysetHandle(req.GetAnnotatedKeyset()) |
tholenst | 53ee0e5 | 2022-08-22 07:59:30 -0700 | [diff] [blame] | 33 | if err != nil { |
| 34 | return &pb.CreationResponse{Err: err.Error()}, nil |
| 35 | } |
| 36 | _, err = hybrid.NewHybridEncrypt(handle) |
| 37 | if err != nil { |
| 38 | return &pb.CreationResponse{Err: err.Error()}, nil |
| 39 | } |
| 40 | return &pb.CreationResponse{}, nil |
| 41 | } |
| 42 | |
| 43 | func (s *HybridService) CreateHybridDecrypt(ctx context.Context, req *pb.CreationRequest) (*pb.CreationResponse, error) { |
felobato | 912db1c | 2022-11-16 17:02:44 -0800 | [diff] [blame] | 44 | handle, err := toKeysetHandle(req.GetAnnotatedKeyset()) |
tholenst | 53ee0e5 | 2022-08-22 07:59:30 -0700 | [diff] [blame] | 45 | if err != nil { |
| 46 | return &pb.CreationResponse{Err: err.Error()}, nil |
| 47 | } |
| 48 | _, err = hybrid.NewHybridDecrypt(handle) |
| 49 | if err != nil { |
| 50 | return &pb.CreationResponse{Err: err.Error()}, nil |
| 51 | } |
| 52 | return &pb.CreationResponse{}, nil |
| 53 | } |
| 54 | |
juerg | 72d1814 | 2020-06-23 10:00:08 -0700 | [diff] [blame] | 55 | func (s *HybridService) Encrypt(ctx context.Context, req *pb.HybridEncryptRequest) (*pb.HybridEncryptResponse, error) { |
felobato | 912db1c | 2022-11-16 17:02:44 -0800 | [diff] [blame] | 56 | handle, err := toKeysetHandle(req.GetPublicAnnotatedKeyset()) |
juerg | 72d1814 | 2020-06-23 10:00:08 -0700 | [diff] [blame] | 57 | if err != nil { |
| 58 | return &pb.HybridEncryptResponse{ |
| 59 | Result: &pb.HybridEncryptResponse_Err{err.Error()}}, nil |
| 60 | } |
| 61 | cipher, err := hybrid.NewHybridEncrypt(handle) |
| 62 | if err != nil { |
| 63 | return &pb.HybridEncryptResponse{ |
| 64 | Result: &pb.HybridEncryptResponse_Err{err.Error()}}, nil |
| 65 | } |
| 66 | ciphertext, err := cipher.Encrypt(req.Plaintext, req.ContextInfo) |
| 67 | if err != nil { |
| 68 | return &pb.HybridEncryptResponse{ |
| 69 | Result: &pb.HybridEncryptResponse_Err{err.Error()}}, nil |
| 70 | } |
| 71 | return &pb.HybridEncryptResponse{ |
| 72 | Result: &pb.HybridEncryptResponse_Ciphertext{ciphertext}}, nil |
| 73 | } |
| 74 | |
| 75 | func (s *HybridService) Decrypt(ctx context.Context, req *pb.HybridDecryptRequest) (*pb.HybridDecryptResponse, error) { |
felobato | 912db1c | 2022-11-16 17:02:44 -0800 | [diff] [blame] | 76 | handle, err := toKeysetHandle(req.GetPrivateAnnotatedKeyset()) |
juerg | 72d1814 | 2020-06-23 10:00:08 -0700 | [diff] [blame] | 77 | if err != nil { |
| 78 | return &pb.HybridDecryptResponse{ |
| 79 | Result: &pb.HybridDecryptResponse_Err{err.Error()}}, nil |
| 80 | } |
| 81 | cipher, err := hybrid.NewHybridDecrypt(handle) |
| 82 | if err != nil { |
| 83 | return &pb.HybridDecryptResponse{ |
| 84 | Result: &pb.HybridDecryptResponse_Err{err.Error()}}, nil |
| 85 | } |
| 86 | plaintext, err := cipher.Decrypt(req.Ciphertext, req.ContextInfo) |
| 87 | if err != nil { |
| 88 | return &pb.HybridDecryptResponse{ |
| 89 | Result: &pb.HybridDecryptResponse_Err{err.Error()}}, nil |
| 90 | } |
| 91 | return &pb.HybridDecryptResponse{ |
| 92 | Result: &pb.HybridDecryptResponse_Plaintext{plaintext}}, nil |
| 93 | } |