xref: /aosp_15_r20/external/tink/go/mac/mac_key_templates.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2018 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 mac
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_go_proto"
25	commonpb "github.com/google/tink/go/proto/common_go_proto"
26	hmacpb "github.com/google/tink/go/proto/hmac_go_proto"
27	tinkpb "github.com/google/tink/go/proto/tink_go_proto"
28)
29
30// This file contains pre-generated KeyTemplate for MAC.
31
32// HMACSHA256Tag128KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:
33//   - Key size: 32 bytes
34//   - Tag size: 16 bytes
35//   - Hash function: SHA256
36func HMACSHA256Tag128KeyTemplate() *tinkpb.KeyTemplate {
37	return createHMACKeyTemplate(32, 16, commonpb.HashType_SHA256)
38}
39
40// HMACSHA256Tag256KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:
41//   - Key size: 32 bytes
42//   - Tag size: 32 bytes
43//   - Hash function: SHA256
44func HMACSHA256Tag256KeyTemplate() *tinkpb.KeyTemplate {
45	return createHMACKeyTemplate(32, 32, commonpb.HashType_SHA256)
46}
47
48// HMACSHA512Tag256KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:
49//   - Key size: 64 bytes
50//   - Tag size: 32 bytes
51//   - Hash function: SHA512
52func HMACSHA512Tag256KeyTemplate() *tinkpb.KeyTemplate {
53	return createHMACKeyTemplate(64, 32, commonpb.HashType_SHA512)
54}
55
56// HMACSHA512Tag512KeyTemplate is a KeyTemplate that generates a HMAC key with the following parameters:
57//   - Key size: 64 bytes
58//   - Tag size: 64 bytes
59//   - Hash function: SHA512
60func HMACSHA512Tag512KeyTemplate() *tinkpb.KeyTemplate {
61	return createHMACKeyTemplate(64, 64, commonpb.HashType_SHA512)
62}
63
64// AESCMACTag128KeyTemplate is a KeyTemplate that generates a AES-CMAC key with the following parameters:
65//   - Key size: 32 bytes
66//   - Tag size: 16 bytes
67func AESCMACTag128KeyTemplate() *tinkpb.KeyTemplate {
68	return createCMACKeyTemplate(32, 16)
69}
70
71// createHMACKeyTemplate creates a new KeyTemplate for HMAC using the given parameters.
72func createHMACKeyTemplate(keySize, tagSize uint32, hashType commonpb.HashType) *tinkpb.KeyTemplate {
73	params := hmacpb.HmacParams{
74		Hash:    hashType,
75		TagSize: tagSize,
76	}
77	format := hmacpb.HmacKeyFormat{
78		Params:  &params,
79		KeySize: keySize,
80	}
81	serializedFormat, err := proto.Marshal(&format)
82	if err != nil {
83		tinkerror.Fail(fmt.Sprintf("failed to marshal key format: %s", err))
84	}
85	return &tinkpb.KeyTemplate{
86		TypeUrl:          hmacTypeURL,
87		Value:            serializedFormat,
88		OutputPrefixType: tinkpb.OutputPrefixType_TINK,
89	}
90}
91
92// createCMACKeyTemplate creates a new KeyTemplate for CMAC using the given parameters.
93func createCMACKeyTemplate(keySize uint32, tagSize uint32) *tinkpb.KeyTemplate {
94	params := cmacpb.AesCmacParams{
95		TagSize: tagSize,
96	}
97	format := cmacpb.AesCmacKeyFormat{
98		Params:  &params,
99		KeySize: keySize,
100	}
101	serializedFormat, err := proto.Marshal(&format)
102	if err != nil {
103		tinkerror.Fail(fmt.Sprintf("failed to marshal key format: %s", err))
104	}
105	return &tinkpb.KeyTemplate{
106		TypeUrl:          cmacTypeURL,
107		Value:            serializedFormat,
108		OutputPrefixType: tinkpb.OutputPrefixType_TINK,
109	}
110}
111