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 23 "google.golang.org/protobuf/proto" 24 "github.com/google/tink/go/core/registry" 25 "github.com/google/tink/go/hybrid/subtle" 26 "github.com/google/tink/go/keyset" 27 commonpb "github.com/google/tink/go/proto/common_go_proto" 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 eciesAEADHKDFPrivateKeyKeyVersion = 0 34 eciesAEADHKDFPrivateKeyTypeURL = "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey" 35) 36 37// common errors 38var errInvalidECIESAEADHKDFPrivateKeyKey = fmt.Errorf("ecies_aead_hkdf_private_key_manager: invalid key") 39var errInvalidECIESAEADHKDFPrivateKeyKeyFormat = fmt.Errorf("ecies_aead_hkdf_private_key_manager: invalid key format") 40 41// eciesAEADHKDFPrivateKeyKeyManager is an implementation of PrivateKeyManager interface. 42// It generates new ECIESAEADHKDFPrivateKeyKey keys and produces new instances of ECIESAEADHKDFPrivateKey subtle. 43type eciesAEADHKDFPrivateKeyKeyManager struct{} 44 45// Assert that eciesAEADHKDFPrivateKeyKeyManager implements the PrivateKeyManager interface. 46var _ registry.PrivateKeyManager = (*eciesAEADHKDFPrivateKeyKeyManager)(nil) 47 48// Primitive creates an ECIESAEADHKDFPrivateKey subtle for the given serialized ECIESAEADHKDFPrivateKey proto. 49func (km *eciesAEADHKDFPrivateKeyKeyManager) Primitive(serializedKey []byte) (interface{}, error) { 50 if len(serializedKey) == 0 { 51 return nil, errInvalidECIESAEADHKDFPrivateKeyKey 52 } 53 key := new(eahpb.EciesAeadHkdfPrivateKey) 54 if err := proto.Unmarshal(serializedKey, key); err != nil { 55 return nil, errInvalidECIESAEADHKDFPrivateKeyKey 56 } 57 if err := km.validateKey(key); err != nil { 58 return nil, errInvalidECIESAEADHKDFPrivateKeyKey 59 } 60 curve, err := subtle.GetCurve(key.PublicKey.Params.KemParams.CurveType.String()) 61 if err != nil { 62 return nil, err 63 } 64 pvt := subtle.GetECPrivateKey(curve, key.KeyValue) 65 rDem, err := newRegisterECIESAEADHKDFDemHelper(key.PublicKey.Params.DemParams.AeadDem) 66 if err != nil { 67 return nil, err 68 } 69 salt := key.PublicKey.Params.KemParams.HkdfSalt 70 hash := key.PublicKey.Params.KemParams.HkdfHashType.String() 71 ptFormat := key.PublicKey.Params.EcPointFormat.String() 72 return subtle.NewECIESAEADHKDFHybridDecrypt(pvt, salt, hash, ptFormat, rDem) 73} 74 75// NewKey creates a new key according to specification the given serialized ECIESAEADHKDFPrivateKeyKeyFormat. 76func (km *eciesAEADHKDFPrivateKeyKeyManager) NewKey(serializedKeyFormat []byte) (proto.Message, error) { 77 if len(serializedKeyFormat) == 0 { 78 return nil, errInvalidECIESAEADHKDFPrivateKeyKeyFormat 79 } 80 keyFormat := new(eahpb.EciesAeadHkdfKeyFormat) 81 if err := proto.Unmarshal(serializedKeyFormat, keyFormat); err != nil { 82 return nil, errInvalidECIESAEADHKDFPrivateKeyKeyFormat 83 } 84 if err := km.validateKeyFormat(keyFormat); err != nil { 85 return nil, errInvalidECIESAEADHKDFPrivateKeyKeyFormat 86 } 87 curve, err := subtle.GetCurve(keyFormat.Params.KemParams.CurveType.String()) 88 if err != nil { 89 return nil, err 90 } 91 pvt, err := subtle.GenerateECDHKeyPair(curve) 92 if err != nil { 93 return nil, err 94 } 95 96 return &eahpb.EciesAeadHkdfPrivateKey{ 97 Version: eciesAEADHKDFPrivateKeyKeyVersion, 98 KeyValue: pvt.D.Bytes(), 99 PublicKey: &eahpb.EciesAeadHkdfPublicKey{ 100 Version: eciesAEADHKDFPrivateKeyKeyVersion, 101 Params: keyFormat.Params, 102 X: pvt.PublicKey.Point.X.Bytes(), 103 Y: pvt.PublicKey.Point.Y.Bytes(), 104 }, 105 }, nil 106} 107 108// NewKeyData creates a new KeyData according to specification in the given serialized 109// ECIESAEADHKDFPrivateKeyKeyFormat. 110// It should be used solely by the key management API. 111func (km *eciesAEADHKDFPrivateKeyKeyManager) NewKeyData(serializedKeyFormat []byte) (*tinkpb.KeyData, error) { 112 key, err := km.NewKey(serializedKeyFormat) 113 if err != nil { 114 return nil, err 115 } 116 serializedKey, err := proto.Marshal(key) 117 if err != nil { 118 return nil, err 119 } 120 return &tinkpb.KeyData{ 121 TypeUrl: eciesAEADHKDFPrivateKeyTypeURL, 122 Value: serializedKey, 123 KeyMaterialType: tinkpb.KeyData_ASYMMETRIC_PRIVATE, 124 }, nil 125} 126 127func (km *eciesAEADHKDFPrivateKeyKeyManager) PublicKeyData(serializedPrivKey []byte) (*tinkpb.KeyData, error) { 128 privKey := new(eahpb.EciesAeadHkdfPrivateKey) 129 if err := proto.Unmarshal(serializedPrivKey, privKey); err != nil { 130 return nil, errInvalidECIESAEADHKDFPrivateKeyKey 131 } 132 serializedPubKey, err := proto.Marshal(privKey.PublicKey) 133 if err != nil { 134 return nil, errInvalidECIESAEADHKDFPrivateKeyKey 135 } 136 return &tinkpb.KeyData{ 137 TypeUrl: eciesAEADHKDFPublicKeyTypeURL, 138 Value: serializedPubKey, 139 KeyMaterialType: tinkpb.KeyData_ASYMMETRIC_PUBLIC, 140 }, nil 141} 142 143// DoesSupport indicates if this key manager supports the given key type. 144func (km *eciesAEADHKDFPrivateKeyKeyManager) DoesSupport(typeURL string) bool { 145 return typeURL == eciesAEADHKDFPrivateKeyTypeURL 146} 147 148// TypeURL returns the key type of keys managed by this key manager. 149func (km *eciesAEADHKDFPrivateKeyKeyManager) TypeURL() string { 150 return eciesAEADHKDFPrivateKeyTypeURL 151} 152 153// validateKey validates the given ECDSAPrivateKey. 154func (km *eciesAEADHKDFPrivateKeyKeyManager) validateKey(key *eahpb.EciesAeadHkdfPrivateKey) error { 155 if err := keyset.ValidateKeyVersion(key.Version, eciesAEADHKDFPrivateKeyKeyVersion); err != nil { 156 return fmt.Errorf("ecies_aead_hkdf_private_key_manager: invalid key: %s", err) 157 } 158 return checkECIESAEADHKDFParams(key.PublicKey.Params) 159} 160 161// validateKeyFormat validates the given ECDSAKeyFormat. 162func (km *eciesAEADHKDFPrivateKeyKeyManager) validateKeyFormat(format *eahpb.EciesAeadHkdfKeyFormat) error { 163 return checkECIESAEADHKDFParams(format.Params) 164} 165 166func checkECIESAEADHKDFParams(params *eahpb.EciesAeadHkdfParams) error { 167 _, err := subtle.GetCurve(params.KemParams.CurveType.String()) 168 if err != nil { 169 return err 170 } 171 if params.KemParams.HkdfHashType == commonpb.HashType_UNKNOWN_HASH { 172 return errors.New("hash unsupported for HMAC") 173 } 174 175 if params.EcPointFormat == commonpb.EcPointFormat_UNKNOWN_FORMAT { 176 return errors.New("unknown EC point format") 177 } 178 km, err := registry.GetKeyManager(params.DemParams.AeadDem.TypeUrl) 179 if err != nil { 180 return err 181 } 182 _, err = km.NewKeyData(params.DemParams.AeadDem.Value) 183 if err != nil { 184 return err 185 } 186 return nil 187} 188