blob: 7cf5f72f62f96e8b8b71ac1796c4332a6253be19 [file] [log] [blame]
tholenst9a1e9752021-04-13 23:13:20 -07001// Copyright 2020 Google LLC
2//
juerg72d18142020-06-23 10:00:08 -07003// 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 (
juerg72d18142020-06-23 10:00:08 -070020 "context"
21
22 "github.com/google/tink/go/hybrid"
juergc7c1d532023-04-13 09:43:21 -070023 pb "github.com/google/tink/testing/go/protos/testing_api_go_grpc"
juerg72d18142020-06-23 10:00:08 -070024)
25
26// HybridService implements the Hybrid encryption and decryption testing service.
27type HybridService struct {
juerg3c90b1a2021-12-16 04:21:47 -080028 pb.HybridServer
juerg72d18142020-06-23 10:00:08 -070029}
30
tholenst53ee0e52022-08-22 07:59:30 -070031func (s *HybridService) CreateHybridEncrypt(ctx context.Context, req *pb.CreationRequest) (*pb.CreationResponse, error) {
felobato912db1c2022-11-16 17:02:44 -080032 handle, err := toKeysetHandle(req.GetAnnotatedKeyset())
tholenst53ee0e52022-08-22 07:59:30 -070033 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
43func (s *HybridService) CreateHybridDecrypt(ctx context.Context, req *pb.CreationRequest) (*pb.CreationResponse, error) {
felobato912db1c2022-11-16 17:02:44 -080044 handle, err := toKeysetHandle(req.GetAnnotatedKeyset())
tholenst53ee0e52022-08-22 07:59:30 -070045 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
juerg72d18142020-06-23 10:00:08 -070055func (s *HybridService) Encrypt(ctx context.Context, req *pb.HybridEncryptRequest) (*pb.HybridEncryptResponse, error) {
felobato912db1c2022-11-16 17:02:44 -080056 handle, err := toKeysetHandle(req.GetPublicAnnotatedKeyset())
juerg72d18142020-06-23 10:00:08 -070057 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
75func (s *HybridService) Decrypt(ctx context.Context, req *pb.HybridDecryptRequest) (*pb.HybridDecryptResponse, error) {
felobato912db1c2022-11-16 17:02:44 -080076 handle, err := toKeysetHandle(req.GetPrivateAnnotatedKeyset())
juerg72d18142020-06-23 10:00:08 -070077 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}
OSZAR »