1// Copyright 2022 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 17// Package stubkeymanager defines key managers for testing primitives. 18package stubkeymanager 19 20import ( 21 "io" 22 23 "google.golang.org/protobuf/proto" 24 "github.com/google/tink/go/core/registry" 25 "github.com/google/tink/go/internal/internalregistry" 26 tinkpb "github.com/google/tink/go/proto/tink_go_proto" 27) 28 29// StubKeyManager is a key manager for testing. 30type StubKeyManager struct { 31 URL string 32 Prim interface{} 33 Key proto.Message 34 KeyData *tinkpb.KeyData 35} 36 37var _ (registry.KeyManager) = (*StubKeyManager)(nil) 38 39// Primitive returns the stub primitive. 40func (km *StubKeyManager) Primitive(serializedKey []byte) (interface{}, error) { 41 return km.Prim, nil 42} 43 44// NewKey returns the stub Key. 45func (km *StubKeyManager) NewKey(serializedKeyFormat []byte) (proto.Message, error) { 46 return km.Key, nil 47} 48 49// NewKeyData returns the stub KeyData. 50func (km *StubKeyManager) NewKeyData(serializedKeyFormat []byte) (*tinkpb.KeyData, error) { 51 return km.KeyData, nil 52} 53 54// DoesSupport returns true if this KeyManager supports key type identified by typeURL. 55func (km *StubKeyManager) DoesSupport(typeURL string) bool { 56 return typeURL == km.URL 57} 58 59// TypeURL returns the stub type url. 60func (km *StubKeyManager) TypeURL() string { 61 return km.URL 62} 63 64// StubPrivateKeyManager is a private key manager for testing. 65type StubPrivateKeyManager struct { 66 StubKeyManager 67 PubKeyData *tinkpb.KeyData 68} 69 70var _ (registry.PrivateKeyManager) = (*StubPrivateKeyManager)(nil) 71 72// PublicKeyData returns the stub public key data. 73func (skm *StubPrivateKeyManager) PublicKeyData(serializedKey []byte) (*tinkpb.KeyData, error) { 74 return skm.PubKeyData, nil 75} 76 77// StubDerivableKeyManager is a derivable key manager for testing. 78type StubDerivableKeyManager struct { 79 StubKeyManager 80 KeyMatType tinkpb.KeyData_KeyMaterialType 81 DerKey proto.Message 82 DerErr error 83} 84 85var _ (internalregistry.DerivableKeyManager) = (*StubDerivableKeyManager)(nil) 86 87// KeyMaterialType returns the stub key material type. 88func (dkm *StubDerivableKeyManager) KeyMaterialType() tinkpb.KeyData_KeyMaterialType { 89 return dkm.KeyMatType 90} 91 92// DeriveKey returns the stub derived key and error. 93func (dkm *StubDerivableKeyManager) DeriveKey(serializedKeyFormat []byte, pseudorandomness io.Reader) (proto.Message, error) { 94 return dkm.DerKey, dkm.DerErr 95} 96