1 // Copyright 2021 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
17 #include "tink/experimental/pqcrypto/kem/cecpq2_hybrid_key_templates.h"
18
19 #include <string>
20
21 #include "absl/strings/string_view.h"
22 #include "tink/aead/aead_key_templates.h"
23 #include "tink/daead/deterministic_aead_key_templates.h"
24 #include "proto/common.pb.h"
25 #include "proto/experimental/pqcrypto/cecpq2_aead_hkdf.pb.h"
26 #include "proto/tink.pb.h"
27
28 namespace crypto {
29 namespace tink {
30 namespace {
31
32 using google::crypto::tink::EcPointFormat;
33 using google::crypto::tink::EllipticCurveType;
34 using google::crypto::tink::HashType;
35 using google::crypto::tink::KeyTemplate;
36 using google::crypto::tink::OutputPrefixType;
37
NewCecpq2AeadHkdfKeyTemplate(EllipticCurveType curve_type,HashType hkdf_hash_type,EcPointFormat ec_point_format,const KeyTemplate & dem_key_template,OutputPrefixType prefix_type,absl::string_view hkdf_salt)38 KeyTemplate* NewCecpq2AeadHkdfKeyTemplate(EllipticCurveType curve_type,
39 HashType hkdf_hash_type,
40 EcPointFormat ec_point_format,
41 const KeyTemplate& dem_key_template,
42 OutputPrefixType prefix_type,
43 absl::string_view hkdf_salt) {
44 KeyTemplate* key_template = new KeyTemplate;
45 key_template->set_type_url(
46 "type.googleapis.com/google.crypto.tink.Cecpq2AeadHkdfPrivateKey");
47 key_template->set_output_prefix_type(prefix_type);
48 google::crypto::tink::Cecpq2AeadHkdfKeyFormat key_format;
49 auto dem_params = key_format.mutable_params()->mutable_dem_params();
50 *(dem_params->mutable_aead_dem()) = dem_key_template;
51 auto kem_params = key_format.mutable_params()->mutable_kem_params();
52 kem_params->set_curve_type(curve_type);
53 kem_params->set_hkdf_hash_type(hkdf_hash_type);
54 std::string hkdf_salt_str(hkdf_salt.data(), hkdf_salt.size());
55 kem_params->set_hkdf_salt(hkdf_salt_str);
56 kem_params->set_ec_point_format(ec_point_format);
57 key_format.SerializeToString(key_template->mutable_value());
58 return key_template;
59 }
60
61 } // anonymous namespace
62
Cecpq2HybridKeyTemplateX25519HkdfHmacSha256Aes256Gcm()63 const KeyTemplate& Cecpq2HybridKeyTemplateX25519HkdfHmacSha256Aes256Gcm() {
64 static const KeyTemplate* key_template = NewCecpq2AeadHkdfKeyTemplate(
65 EllipticCurveType::CURVE25519, HashType::SHA256,
66 EcPointFormat::COMPRESSED, AeadKeyTemplates::Aes256Gcm(),
67 OutputPrefixType::TINK,
68 /* hkdf_salt= */ "");
69 return *key_template;
70 }
71
72 const KeyTemplate&
Cecpq2HybridKeyTemplateX25519HkdfHmacSha256XChaCha20Poly1305()73 Cecpq2HybridKeyTemplateX25519HkdfHmacSha256XChaCha20Poly1305() {
74 static const KeyTemplate* key_template = NewCecpq2AeadHkdfKeyTemplate(
75 EllipticCurveType::CURVE25519, HashType::SHA256,
76 EcPointFormat::COMPRESSED, AeadKeyTemplates::XChaCha20Poly1305(),
77 OutputPrefixType::TINK,
78 /* hkdf_salt= */ "");
79 return *key_template;
80 }
81
82 const google::crypto::tink::KeyTemplate&
Cecpq2HybridKeyTemplateX25519HkdfHmacSha256DeterministicAesSiv()83 Cecpq2HybridKeyTemplateX25519HkdfHmacSha256DeterministicAesSiv() {
84 static const KeyTemplate* key_template = NewCecpq2AeadHkdfKeyTemplate(
85 EllipticCurveType::CURVE25519, HashType::SHA256,
86 EcPointFormat::COMPRESSED, DeterministicAeadKeyTemplates::Aes256Siv(),
87 OutputPrefixType::TINK,
88 /* hkdf_salt= */ "");
89 return *key_template;
90 }
91
92 } // namespace tink
93 } // namespace crypto
94