xref: /aosp_15_r20/external/tink/go/prf/hmac_prf_key_manager.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 prf
18
19import (
20	"errors"
21	"fmt"
22	"io"
23
24	"google.golang.org/protobuf/proto"
25	"github.com/google/tink/go/keyset"
26	"github.com/google/tink/go/prf/subtle"
27	"github.com/google/tink/go/subtle/random"
28	commonpb "github.com/google/tink/go/proto/common_go_proto"
29	hmacpb "github.com/google/tink/go/proto/hmac_prf_go_proto"
30	tinkpb "github.com/google/tink/go/proto/tink_go_proto"
31)
32
33const (
34	hmacprfKeyVersion = 0
35	hmacprfTypeURL    = "type.googleapis.com/google.crypto.tink.HmacPrfKey"
36)
37
38var errInvalidHMACPRFKey = errors.New("hmac_prf_key_manager: invalid key")
39var errInvalidHMACPRFKeyFormat = errors.New("hmac_prf_key_manager: invalid key format")
40
41// hmacprfKeyManager generates new HMAC PRF keys and produces new instances of HMAC.
42type hmacprfKeyManager struct{}
43
44// Primitive constructs a HMAC instance for the given serialized HMACKey.
45func (km *hmacprfKeyManager) Primitive(serializedKey []byte) (interface{}, error) {
46	if len(serializedKey) == 0 {
47		return nil, errInvalidHMACPRFKey
48	}
49	key := new(hmacpb.HmacPrfKey)
50	if err := proto.Unmarshal(serializedKey, key); err != nil {
51		return nil, errInvalidHMACPRFKey
52	}
53	if err := km.validateKey(key); err != nil {
54		return nil, err
55	}
56	hash := commonpb.HashType_name[int32(key.Params.Hash)]
57	hmac, err := subtle.NewHMACPRF(hash, key.KeyValue)
58	if err != nil {
59		return nil, err
60	}
61	return hmac, nil
62}
63
64// NewKey generates a new HMACPRFKey according to specification in the given HMACPRFKeyFormat.
65func (km *hmacprfKeyManager) NewKey(serializedKeyFormat []byte) (proto.Message, error) {
66	if len(serializedKeyFormat) == 0 {
67		return nil, errInvalidHMACPRFKeyFormat
68	}
69	keyFormat := new(hmacpb.HmacPrfKeyFormat)
70	if err := proto.Unmarshal(serializedKeyFormat, keyFormat); err != nil {
71		return nil, errInvalidHMACPRFKeyFormat
72	}
73	if err := km.validateKeyFormat(keyFormat); err != nil {
74		return nil, fmt.Errorf("hmac_prf_key_manager: invalid key format: %s", err)
75	}
76	return &hmacpb.HmacPrfKey{
77		Version:  hmacprfKeyVersion,
78		Params:   keyFormat.Params,
79		KeyValue: random.GetRandomBytes(keyFormat.KeySize),
80	}, nil
81}
82
83// NewKeyData generates a new KeyData according to specification in the given
84// serialized HMACPRFKeyFormat. This should be used solely by the key management API.
85func (km *hmacprfKeyManager) NewKeyData(serializedKeyFormat []byte) (*tinkpb.KeyData, error) {
86	key, err := km.NewKey(serializedKeyFormat)
87	if err != nil {
88		return nil, err
89	}
90	serializedKey, err := proto.Marshal(key)
91	if err != nil {
92		return nil, errInvalidHMACPRFKeyFormat
93	}
94
95	return &tinkpb.KeyData{
96		TypeUrl:         hmacprfTypeURL,
97		Value:           serializedKey,
98		KeyMaterialType: km.KeyMaterialType(),
99	}, nil
100}
101
102// DoesSupport checks whether this KeyManager supports the given key type.
103func (km *hmacprfKeyManager) DoesSupport(typeURL string) bool {
104	return typeURL == hmacprfTypeURL
105}
106
107// TypeURL returns the type URL of keys managed by this KeyManager.
108func (km *hmacprfKeyManager) TypeURL() string {
109	return hmacprfTypeURL
110}
111
112// validateKey validates the given HMACPRFKey. It only validates the version of the
113// key because other parameters will be validated in primitive construction.
114func (km *hmacprfKeyManager) validateKey(key *hmacpb.HmacPrfKey) error {
115	err := keyset.ValidateKeyVersion(key.Version, hmacprfKeyVersion)
116	if err != nil {
117		return fmt.Errorf("hmac_prf_key_manager: invalid version: %s", err)
118	}
119	keySize := uint32(len(key.KeyValue))
120	hash := commonpb.HashType_name[int32(key.Params.Hash)]
121	return subtle.ValidateHMACPRFParams(hash, keySize)
122}
123
124// KeyMaterialType returns the key material type of this key manager.
125func (km *hmacprfKeyManager) KeyMaterialType() tinkpb.KeyData_KeyMaterialType {
126	return tinkpb.KeyData_SYMMETRIC
127}
128
129// DeriveKey derives a new key from serializedKeyFormat and pseudorandomness.
130func (km *hmacprfKeyManager) DeriveKey(serializedKeyFormat []byte, pseudorandomness io.Reader) (proto.Message, error) {
131	if len(serializedKeyFormat) == 0 {
132		return nil, errInvalidHMACPRFKeyFormat
133	}
134	keyFormat := new(hmacpb.HmacPrfKeyFormat)
135	if err := proto.Unmarshal(serializedKeyFormat, keyFormat); err != nil {
136		return nil, errInvalidHMACPRFKeyFormat
137	}
138	if err := km.validateKeyFormat(keyFormat); err != nil {
139		return nil, fmt.Errorf("hmac_key_manager: invalid key format: %v", err)
140	}
141	if err := keyset.ValidateKeyVersion(keyFormat.GetVersion(), hmacprfKeyVersion); err != nil {
142		return nil, fmt.Errorf("hmac_key_manager: invalid key version: %s", err)
143	}
144
145	keyValue := make([]byte, keyFormat.GetKeySize())
146	if _, err := io.ReadFull(pseudorandomness, keyValue); err != nil {
147		return nil, fmt.Errorf("hmac_key_manager: not enough pseudorandomness given")
148	}
149	return &hmacpb.HmacPrfKey{
150		Version:  hmacprfKeyVersion,
151		Params:   keyFormat.GetParams(),
152		KeyValue: keyValue,
153	}, nil
154}
155
156// validateKeyFormat validates the given HMACKeyFormat
157func (km *hmacprfKeyManager) validateKeyFormat(format *hmacpb.HmacPrfKeyFormat) error {
158	if format.Params == nil {
159		return fmt.Errorf("null HMAC params")
160	}
161	hash := commonpb.HashType_name[int32(format.Params.Hash)]
162	return subtle.ValidateHMACPRFParams(hash, format.KeySize)
163}
164