xref: /aosp_15_r20/external/tink/testing/go/hybrid_service.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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/hybrid"
23	pb "github.com/google/tink/testing/go/protos/testing_api_go_grpc"
24)
25
26// HybridService implements the Hybrid encryption and decryption testing service.
27type HybridService struct {
28	pb.HybridServer
29}
30
31func (s *HybridService) CreateHybridEncrypt(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 = 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) {
44	handle, err := toKeysetHandle(req.GetAnnotatedKeyset())
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
55func (s *HybridService) Encrypt(ctx context.Context, req *pb.HybridEncryptRequest) (*pb.HybridEncryptResponse, error) {
56	handle, err := toKeysetHandle(req.GetPublicAnnotatedKeyset())
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
75func (s *HybridService) Decrypt(ctx context.Context, req *pb.HybridDecryptRequest) (*pb.HybridDecryptResponse, error) {
76	handle, err := toKeysetHandle(req.GetPrivateAnnotatedKeyset())
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}
94