xref: /aosp_15_r20/external/tink/go/prf/prf_key_templates.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	"fmt"
21
22	"google.golang.org/protobuf/proto"
23	"github.com/google/tink/go/internal/tinkerror"
24	cmacpb "github.com/google/tink/go/proto/aes_cmac_prf_go_proto"
25	commonpb "github.com/google/tink/go/proto/common_go_proto"
26	hkdfpb "github.com/google/tink/go/proto/hkdf_prf_go_proto"
27	hmacpb "github.com/google/tink/go/proto/hmac_prf_go_proto"
28	tinkpb "github.com/google/tink/go/proto/tink_go_proto"
29)
30
31// This file contains pre-generated KeyTemplate for PRF.
32
33// HMACSHA256PRFKeyTemplate is a KeyTemplate that generates an HMAC key with the following parameters:
34//   - Key size: 32 bytes
35//   - Hash function: SHA256
36func HMACSHA256PRFKeyTemplate() *tinkpb.KeyTemplate {
37	return createHMACPRFKeyTemplate(32, commonpb.HashType_SHA256)
38}
39
40// HMACSHA512PRFKeyTemplate is a KeyTemplate that generates an HMAC key with the following parameters:
41//   - Key size: 64 bytes
42//   - Hash function: SHA512
43func HMACSHA512PRFKeyTemplate() *tinkpb.KeyTemplate {
44	return createHMACPRFKeyTemplate(64, commonpb.HashType_SHA512)
45}
46
47// HKDFSHA256PRFKeyTemplate is a KeyTemplate that generates an HKDF key with the following parameters:
48//   - Key size: 32 bytes
49//   - Salt: empty
50//   - Hash function: SHA256
51func HKDFSHA256PRFKeyTemplate() *tinkpb.KeyTemplate {
52	return createHKDFPRFKeyTemplate(32, commonpb.HashType_SHA256, make([]byte, 0))
53}
54
55// AESCMACPRFKeyTemplate is a KeyTemplate that generates a AES-CMAC key with the following parameters:
56//   - Key size: 32 bytes
57func AESCMACPRFKeyTemplate() *tinkpb.KeyTemplate {
58	return createAESCMACPRFKeyTemplate(32)
59}
60
61// createHMACPRFKeyTemplate creates a new KeyTemplate for HMAC using the given parameters.
62func createHMACPRFKeyTemplate(keySize uint32, hashType commonpb.HashType) *tinkpb.KeyTemplate {
63	params := hmacpb.HmacPrfParams{
64		Hash: hashType,
65	}
66	format := hmacpb.HmacPrfKeyFormat{
67		Params:  &params,
68		KeySize: keySize,
69	}
70	serializedFormat, err := proto.Marshal(&format)
71	if err != nil {
72		tinkerror.Fail(fmt.Sprintf("failed to marshal key format: %s", err))
73	}
74	return &tinkpb.KeyTemplate{
75		TypeUrl:          hmacprfTypeURL,
76		OutputPrefixType: tinkpb.OutputPrefixType_RAW,
77		Value:            serializedFormat,
78	}
79}
80
81// createHKDFPRFKeyTemplate creates a new KeyTemplate for HKDF using the given parameters.
82func createHKDFPRFKeyTemplate(keySize uint32, hashType commonpb.HashType, salt []byte) *tinkpb.KeyTemplate {
83	params := hkdfpb.HkdfPrfParams{
84		Hash: hashType,
85		Salt: salt,
86	}
87	format := hkdfpb.HkdfPrfKeyFormat{
88		Params:  &params,
89		KeySize: keySize,
90	}
91	serializedFormat, err := proto.Marshal(&format)
92	if err != nil {
93		tinkerror.Fail(fmt.Sprintf("failed to marshal key format: %s", err))
94	}
95	return &tinkpb.KeyTemplate{
96		TypeUrl:          hkdfprfTypeURL,
97		OutputPrefixType: tinkpb.OutputPrefixType_RAW,
98		Value:            serializedFormat,
99	}
100}
101
102// createAESCMACPRFKeyTemplate creates a new KeyTemplate for AES-CMAC using the given parameters.
103func createAESCMACPRFKeyTemplate(keySize uint32) *tinkpb.KeyTemplate {
104	format := cmacpb.AesCmacPrfKeyFormat{
105		KeySize: keySize,
106	}
107	serializedFormat, err := proto.Marshal(&format)
108	if err != nil {
109		tinkerror.Fail(fmt.Sprintf("failed to marshal key format: %s", err))
110	}
111	return &tinkpb.KeyTemplate{
112		TypeUrl:          aescmacprfTypeURL,
113		OutputPrefixType: tinkpb.OutputPrefixType_RAW,
114		Value:            serializedFormat,
115	}
116}
117