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/aead/aead_key_templates.h"
18
19 #include <string>
20
21 #include "absl/strings/string_view.h"
22 #include "proto/aes_ctr.pb.h"
23 #include "proto/aes_ctr_hmac_aead.pb.h"
24 #include "proto/aes_eax.pb.h"
25 #include "proto/aes_gcm.pb.h"
26 #include "proto/aes_gcm_siv.pb.h"
27 #include "proto/common.pb.h"
28 #include "proto/hmac.pb.h"
29 #include "proto/kms_envelope.pb.h"
30 #include "proto/tink.pb.h"
31
32 using google::crypto::tink::AesCtrHmacAeadKeyFormat;
33 using google::crypto::tink::AesEaxKeyFormat;
34 using google::crypto::tink::AesGcmKeyFormat;
35 using google::crypto::tink::AesGcmSivKeyFormat;
36 using google::crypto::tink::HashType;
37 using google::crypto::tink::KeyTemplate;
38 using google::crypto::tink::KmsEnvelopeAeadKeyFormat;
39 using google::crypto::tink::OutputPrefixType;
40
41 namespace crypto {
42 namespace tink {
43
44 namespace {
45
NewAesEaxKeyTemplate(int key_size_in_bytes,int iv_size_in_bytes)46 KeyTemplate* NewAesEaxKeyTemplate(int key_size_in_bytes, int iv_size_in_bytes) {
47 KeyTemplate* key_template = new KeyTemplate;
48 key_template->set_type_url(
49 "type.googleapis.com/google.crypto.tink.AesEaxKey");
50 key_template->set_output_prefix_type(OutputPrefixType::TINK);
51 AesEaxKeyFormat key_format;
52 key_format.set_key_size(key_size_in_bytes);
53 key_format.mutable_params()->set_iv_size(iv_size_in_bytes);
54 key_format.SerializeToString(key_template->mutable_value());
55 return key_template;
56 }
57
NewAesGcmKeyTemplate(int key_size_in_bytes,OutputPrefixType output_prefix_type)58 KeyTemplate* NewAesGcmKeyTemplate(int key_size_in_bytes,
59 OutputPrefixType output_prefix_type) {
60 KeyTemplate* key_template = new KeyTemplate;
61 key_template->set_type_url(
62 "type.googleapis.com/google.crypto.tink.AesGcmKey");
63 key_template->set_output_prefix_type(output_prefix_type);
64 AesGcmKeyFormat key_format;
65 key_format.set_key_size(key_size_in_bytes);
66 key_format.SerializeToString(key_template->mutable_value());
67 return key_template;
68 }
69
NewAesGcmSivKeyTemplate(int key_size_in_bytes)70 KeyTemplate* NewAesGcmSivKeyTemplate(int key_size_in_bytes) {
71 KeyTemplate* key_template = new KeyTemplate;
72 key_template->set_type_url(
73 "type.googleapis.com/google.crypto.tink.AesGcmSivKey");
74 key_template->set_output_prefix_type(OutputPrefixType::TINK);
75 AesGcmSivKeyFormat key_format;
76 key_format.set_key_size(key_size_in_bytes);
77 key_format.SerializeToString(key_template->mutable_value());
78 return key_template;
79 }
80
NewAesCtrHmacAeadKeyTemplate(int aes_key_size_in_bytes,int iv_size_in_bytes,int hmac_key_size_in_bytes,int tag_size_in_bytes,HashType hash_type)81 KeyTemplate* NewAesCtrHmacAeadKeyTemplate(int aes_key_size_in_bytes,
82 int iv_size_in_bytes,
83 int hmac_key_size_in_bytes,
84 int tag_size_in_bytes,
85 HashType hash_type) {
86 KeyTemplate* key_template = new KeyTemplate;
87 key_template->set_type_url(
88 "type.googleapis.com/google.crypto.tink.AesCtrHmacAeadKey");
89 key_template->set_output_prefix_type(OutputPrefixType::TINK);
90 AesCtrHmacAeadKeyFormat key_format;
91 auto aes_ctr_key_format = key_format.mutable_aes_ctr_key_format();
92 aes_ctr_key_format->set_key_size(aes_key_size_in_bytes);
93 aes_ctr_key_format->mutable_params()->set_iv_size(iv_size_in_bytes);
94 auto hmac_key_format = key_format.mutable_hmac_key_format();
95 hmac_key_format->set_key_size(hmac_key_size_in_bytes);
96 hmac_key_format->mutable_params()->set_hash(hash_type);
97 hmac_key_format->mutable_params()->set_tag_size(tag_size_in_bytes);
98 key_format.SerializeToString(key_template->mutable_value());
99 return key_template;
100 }
101
NewXChaCha20Poly1305KeyTemplate()102 KeyTemplate* NewXChaCha20Poly1305KeyTemplate() {
103 KeyTemplate* key_template = new KeyTemplate;
104 key_template->set_type_url(
105 "type.googleapis.com/google.crypto.tink.XChaCha20Poly1305Key");
106 key_template->set_output_prefix_type(OutputPrefixType::TINK);
107 return key_template;
108 }
109
110 } // anonymous namespace
111
112 // static
Aes128Eax()113 const KeyTemplate& AeadKeyTemplates::Aes128Eax() {
114 static const KeyTemplate* key_template =
115 NewAesEaxKeyTemplate(/* key_size_in_bytes= */ 16,
116 /* iv_size_in_bytes= */ 16);
117 return *key_template;
118 }
119
120 // static
Aes256Eax()121 const KeyTemplate& AeadKeyTemplates::Aes256Eax() {
122 static const KeyTemplate* key_template =
123 NewAesEaxKeyTemplate(/* key_size_in_bytes= */ 32,
124 /* iv_size_in_bytes= */ 16);
125 return *key_template;
126 }
127
128 // static
Aes128Gcm()129 const KeyTemplate& AeadKeyTemplates::Aes128Gcm() {
130 static const KeyTemplate* key_template =
131 NewAesGcmKeyTemplate(/* key_size_in_bytes= */ 16, OutputPrefixType::TINK);
132 return *key_template;
133 }
134
135 // static
Aes128GcmNoPrefix()136 const KeyTemplate& AeadKeyTemplates::Aes128GcmNoPrefix() {
137 static const KeyTemplate* key_template =
138 NewAesGcmKeyTemplate(/* key_size_in_bytes= */ 16, OutputPrefixType::RAW);
139 return *key_template;
140 }
141
142 // static
Aes256Gcm()143 const KeyTemplate& AeadKeyTemplates::Aes256Gcm() {
144 static const KeyTemplate* key_template =
145 NewAesGcmKeyTemplate(/* key_size_in_bytes= */ 32, OutputPrefixType::TINK);
146 return *key_template;
147 }
148
149 // static
Aes256GcmNoPrefix()150 const KeyTemplate& AeadKeyTemplates::Aes256GcmNoPrefix() {
151 static const KeyTemplate* key_template =
152 NewAesGcmKeyTemplate(/* key_size_in_bytes= */ 32, OutputPrefixType::RAW);
153 return *key_template;
154 }
155
156 // static
Aes128GcmSiv()157 const KeyTemplate& AeadKeyTemplates::Aes128GcmSiv() {
158 static const KeyTemplate* key_template =
159 NewAesGcmSivKeyTemplate(/* key_size_in_bytes= */ 16);
160 return *key_template;
161 }
162
163 // static
Aes256GcmSiv()164 const KeyTemplate& AeadKeyTemplates::Aes256GcmSiv() {
165 static const KeyTemplate* key_template =
166 NewAesGcmSivKeyTemplate(/* key_size_in_bytes= */ 32);
167 return *key_template;
168 }
169
170 // static
Aes128CtrHmacSha256()171 const KeyTemplate& AeadKeyTemplates::Aes128CtrHmacSha256() {
172 static const KeyTemplate* key_template = NewAesCtrHmacAeadKeyTemplate(
173 /* aes_key_size_in_bytes= */ 16,
174 /* iv_size_in_bytes= */ 16,
175 /* hmac_key_size_in_bytes= */ 32,
176 /* tag_size_in_bytes= */ 16, HashType::SHA256);
177 return *key_template;
178 }
179
180 // static
Aes256CtrHmacSha256()181 const KeyTemplate& AeadKeyTemplates::Aes256CtrHmacSha256() {
182 static const KeyTemplate* key_template = NewAesCtrHmacAeadKeyTemplate(
183 /* aes_key_size_in_bytes= */ 32,
184 /* iv_size_in_bytes= */ 16,
185 /* hmac_key_size_in_bytes= */ 32,
186 /* tag_size_in_bytes= */ 32, HashType::SHA256);
187 return *key_template;
188 }
189
190 // static
XChaCha20Poly1305()191 const KeyTemplate& AeadKeyTemplates::XChaCha20Poly1305() {
192 static const KeyTemplate* key_template = NewXChaCha20Poly1305KeyTemplate();
193 return *key_template;
194 }
195
196 // static
KmsEnvelopeAead(absl::string_view kek_uri,const KeyTemplate & dek_template)197 KeyTemplate AeadKeyTemplates::KmsEnvelopeAead(absl::string_view kek_uri,
198 const KeyTemplate& dek_template) {
199 KeyTemplate key_template;
200 key_template.set_type_url(
201 "type.googleapis.com/google.crypto.tink.KmsEnvelopeAeadKey");
202 key_template.set_output_prefix_type(OutputPrefixType::RAW);
203 KmsEnvelopeAeadKeyFormat key_format;
204 key_format.set_kek_uri(std::string(kek_uri));
205 key_format.mutable_dek_template()->MergeFrom(dek_template);
206 key_format.SerializeToString(key_template.mutable_value());
207 return key_template;
208 }
209
210 } // namespace tink
211 } // namespace crypto
212