xref: /aosp_15_r20/external/tink/testing/go/prf_set_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/prf"
23	pb "github.com/google/tink/testing/go/protos/testing_api_go_grpc"
24)
25
26// PrfSetService implements the PrfSet testing service.
27type PrfSetService struct {
28	pb.PrfSetServer
29}
30
31func (s *PrfSetService) 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 = prf.NewPRFSet(handle)
37	if err != nil {
38		return &pb.CreationResponse{Err: err.Error()}, nil
39	}
40	return &pb.CreationResponse{}, nil
41}
42
43func (s *PrfSetService) KeyIds(ctx context.Context, req *pb.PrfSetKeyIdsRequest) (*pb.PrfSetKeyIdsResponse, error) {
44	handle, err := toKeysetHandle(req.GetAnnotatedKeyset())
45	if err != nil {
46		return &pb.PrfSetKeyIdsResponse{
47			Result: &pb.PrfSetKeyIdsResponse_Err{err.Error()}}, nil
48	}
49	primitive, err := prf.NewPRFSet(handle)
50	if err != nil {
51		return &pb.PrfSetKeyIdsResponse{
52			Result: &pb.PrfSetKeyIdsResponse_Err{err.Error()}}, nil
53	}
54	output := &pb.PrfSetKeyIdsResponse_Output{}
55	output.PrimaryKeyId = primitive.PrimaryID
56	for keyID := range primitive.PRFs {
57		output.KeyId = append(output.KeyId, keyID)
58	}
59	return &pb.PrfSetKeyIdsResponse{
60		Result: &pb.PrfSetKeyIdsResponse_Output_{output}}, nil
61}
62
63func (s *PrfSetService) Compute(ctx context.Context, req *pb.PrfSetComputeRequest) (*pb.PrfSetComputeResponse, error) {
64	handle, err := toKeysetHandle(req.GetAnnotatedKeyset())
65	if err != nil {
66		return &pb.PrfSetComputeResponse{
67			Result: &pb.PrfSetComputeResponse_Err{err.Error()}}, nil
68	}
69	primitive, err := prf.NewPRFSet(handle)
70	if err != nil {
71		return &pb.PrfSetComputeResponse{
72			Result: &pb.PrfSetComputeResponse_Err{err.Error()}}, nil
73	}
74	output, err := primitive.PRFs[req.KeyId].ComputePRF(req.InputData, uint32(req.OutputLength))
75	if err != nil {
76		return &pb.PrfSetComputeResponse{
77			Result: &pb.PrfSetComputeResponse_Err{err.Error()}}, nil
78	}
79	return &pb.PrfSetComputeResponse{
80		Result: &pb.PrfSetComputeResponse_Output{output}}, nil
81}
82