xref: /aosp_15_r20/external/tink/go/hybrid/ecies_aead_hkdf_public_key_manager.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2019 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 hybrid
18
19import (
20	"errors"
21	"fmt"
22	"math/big"
23
24	"google.golang.org/protobuf/proto"
25	"github.com/google/tink/go/core/registry"
26	"github.com/google/tink/go/hybrid/subtle"
27	"github.com/google/tink/go/keyset"
28	eahpb "github.com/google/tink/go/proto/ecies_aead_hkdf_go_proto"
29	tinkpb "github.com/google/tink/go/proto/tink_go_proto"
30)
31
32const (
33	eciesAEADHKDFPublicKeyKeyVersion = 0
34
35	eciesAEADHKDFPublicKeyTypeURL = "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPublicKey"
36)
37
38// common errors
39var errInvalidECIESAEADHKDFPublicKeyKey = fmt.Errorf("ecies_aead_hkdf_public_key_manager: invalid key")
40var errInvalidECIESAEADHKDFPublicKeyKeyFormat = fmt.Errorf("ecies_aead_hkdf_public_key_manager: invalid key format")
41
42// eciesAEADHKDFPublicKeyKeyManager is an implementation of KeyManager interface.
43// It generates new ECIESAEADHKDFPublicKeyKey keys and produces new instances of ECIESAEADHKDFPublicKey subtle.
44type eciesAEADHKDFPublicKeyKeyManager struct{}
45
46// Assert that eciesAEADHKDFPublicKeyKeyManager implements the KeyManager interface.
47var _ registry.KeyManager = (*eciesAEADHKDFPublicKeyKeyManager)(nil)
48
49// Primitive creates an ECIESAEADHKDFPublicKey subtle for the given serialized ECIESAEADHKDFPublicKey proto.
50func (km *eciesAEADHKDFPublicKeyKeyManager) Primitive(serializedKey []byte) (interface{}, error) {
51	if len(serializedKey) == 0 {
52		return nil, errInvalidECIESAEADHKDFPublicKeyKey
53	}
54	key := new(eahpb.EciesAeadHkdfPublicKey)
55	if err := proto.Unmarshal(serializedKey, key); err != nil {
56		return nil, errInvalidECIESAEADHKDFPublicKeyKey
57	}
58	if err := km.validateKey(key); err != nil {
59		return nil, errInvalidECIESAEADHKDFPublicKeyKey
60	}
61	curve, err := subtle.GetCurve(key.Params.KemParams.CurveType.String())
62	if err != nil {
63		return nil, err
64	}
65	pub := subtle.ECPublicKey{
66		Curve: curve,
67		Point: subtle.ECPoint{
68			X: new(big.Int).SetBytes(key.X),
69			Y: new(big.Int).SetBytes(key.Y),
70		},
71	}
72	rDem, err := newRegisterECIESAEADHKDFDemHelper(key.Params.DemParams.AeadDem)
73	if err != nil {
74		return nil, err
75	}
76	salt := key.Params.KemParams.HkdfSalt
77	hash := key.Params.KemParams.HkdfHashType.String()
78	ptFormat := key.Params.EcPointFormat.String()
79
80	return subtle.NewECIESAEADHKDFHybridEncrypt(&pub, salt, hash, ptFormat, rDem)
81}
82
83// DoesSupport indicates if this key manager supports the given key type.
84func (km *eciesAEADHKDFPublicKeyKeyManager) DoesSupport(typeURL string) bool {
85	return typeURL == eciesAEADHKDFPublicKeyTypeURL
86}
87
88// TypeURL returns the key type of keys managed by this key manager.
89func (km *eciesAEADHKDFPublicKeyKeyManager) TypeURL() string {
90	return eciesAEADHKDFPublicKeyTypeURL
91}
92
93// validateKey validates the given ECDSAPrivateKey.
94func (km *eciesAEADHKDFPublicKeyKeyManager) validateKey(key *eahpb.EciesAeadHkdfPublicKey) error {
95	if err := keyset.ValidateKeyVersion(key.Version, eciesAEADHKDFPublicKeyKeyVersion); err != nil {
96		return fmt.Errorf("ecies_aead_hkdf_public_key_manager: invalid key: %s", err)
97	}
98	return checkECIESAEADHKDFParams(key.Params)
99}
100
101// NewKey is not implemented for public key manager.
102func (km *eciesAEADHKDFPublicKeyKeyManager) NewKey(serializedKeyFormat []byte) (proto.Message, error) {
103	return nil, errors.New("public key manager does not implement NewKey")
104}
105
106// NewKeyData is not implemented for public key manager.
107func (km *eciesAEADHKDFPublicKeyKeyManager) NewKeyData(serializedKeyFormat []byte) (*tinkpb.KeyData, error) {
108	return nil, errors.New("public key manager does not implement NewKeyData")
109}
110