1 // Copyright 2018 Google Inc.
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
17 #include "tink/mac/mac_key_templates.h"
18
19 #include "proto/aes_cmac.pb.h"
20 #include "proto/common.pb.h"
21 #include "proto/hmac.pb.h"
22 #include "proto/tink.pb.h"
23
24 namespace crypto {
25 namespace tink {
26 namespace {
27
28 using google::crypto::tink::AesCmacKeyFormat;
29 using google::crypto::tink::HashType;
30 using google::crypto::tink::HmacKeyFormat;
31 using google::crypto::tink::KeyTemplate;
32 using google::crypto::tink::OutputPrefixType;
33
NewHmacKeyTemplate(int key_size_in_bytes,int tag_size_in_bytes,HashType hash_type)34 KeyTemplate* NewHmacKeyTemplate(int key_size_in_bytes, int tag_size_in_bytes,
35 HashType hash_type) {
36 KeyTemplate* key_template = new KeyTemplate;
37 key_template->set_type_url("type.googleapis.com/google.crypto.tink.HmacKey");
38 key_template->set_output_prefix_type(OutputPrefixType::TINK);
39 HmacKeyFormat key_format;
40 key_format.set_key_size(key_size_in_bytes);
41 key_format.mutable_params()->set_tag_size(tag_size_in_bytes);
42 key_format.mutable_params()->set_hash(hash_type);
43 key_format.SerializeToString(key_template->mutable_value());
44 return key_template;
45 }
46
NewAesCmacKeyTemplate(int key_size_in_bytes,int tag_size_in_bytes)47 KeyTemplate* NewAesCmacKeyTemplate(int key_size_in_bytes,
48 int tag_size_in_bytes) {
49 KeyTemplate* key_template = new KeyTemplate;
50 key_template->set_type_url(
51 "type.googleapis.com/google.crypto.tink.AesCmacKey");
52 key_template->set_output_prefix_type(OutputPrefixType::TINK);
53 AesCmacKeyFormat key_format;
54 key_format.set_key_size(key_size_in_bytes);
55 key_format.mutable_params()->set_tag_size(tag_size_in_bytes);
56 key_format.SerializeToString(key_template->mutable_value());
57 return key_template;
58 }
59
60 } // anonymous namespace
61
62 // static
HmacSha256HalfSizeTag()63 const KeyTemplate& MacKeyTemplates::HmacSha256HalfSizeTag() {
64 static const KeyTemplate* key_template =
65 NewHmacKeyTemplate(/* key_size_in_bytes= */ 32,
66 /* tag_size_in_bytes= */ 16, HashType::SHA256);
67 return *key_template;
68 }
69
70 // static
HmacSha256()71 const KeyTemplate& MacKeyTemplates::HmacSha256() {
72 static const KeyTemplate* key_template =
73 NewHmacKeyTemplate(/* key_size_in_bytes= */ 32,
74 /* tag_size_in_bytes= */ 32, HashType::SHA256);
75 return *key_template;
76 }
77
78 // static
HmacSha512HalfSizeTag()79 const KeyTemplate& MacKeyTemplates::HmacSha512HalfSizeTag() {
80 static const KeyTemplate* key_template =
81 NewHmacKeyTemplate(/* key_size_in_bytes= */ 64,
82 /* tag_size_in_bytes= */ 32, HashType::SHA512);
83 return *key_template;
84 }
85
86 // static
HmacSha512()87 const KeyTemplate& MacKeyTemplates::HmacSha512() {
88 static const KeyTemplate* key_template =
89 NewHmacKeyTemplate(/* key_size_in_bytes= */ 64,
90 /* tag_size_in_bytes= */ 64, HashType::SHA512);
91 return *key_template;
92 }
93
94 // static
AesCmac()95 const KeyTemplate& MacKeyTemplates::AesCmac() {
96 static const KeyTemplate* key_template = NewAesCmacKeyTemplate(
97 /* key_size_in_bytes= */ 32, /* tag_size_in_bytes= */ 16);
98 return *key_template;
99 }
100
101 } // namespace tink
102 } // namespace crypto
103