xref: /aosp_15_r20/external/tink/cc/experimental/pqcrypto/signature/dilithium_key_template.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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/signature/dilithium_key_template.h"
18 
19 #include "tink/util/constants.h"
20 #include "proto/experimental/pqcrypto/dilithium.pb.h"
21 #include "proto/tink.pb.h"
22 
23 extern "C" {
24 #include "third_party/pqclean/crypto_sign/dilithium2/api.h"
25 #include "third_party/pqclean/crypto_sign/dilithium2aes/api.h"
26 #include "third_party/pqclean/crypto_sign/dilithium3/api.h"
27 #include "third_party/pqclean/crypto_sign/dilithium3aes/api.h"
28 #include "third_party/pqclean/crypto_sign/dilithium5/api.h"
29 #include "third_party/pqclean/crypto_sign/dilithium5aes/api.h"
30 }
31 
32 namespace crypto {
33 namespace tink {
34 namespace {
35 
36 using google::crypto::tink::DilithiumKeyFormat;
37 using google::crypto::tink::DilithiumParams;
38 using google::crypto::tink::DilithiumPrivateKey;
39 using google::crypto::tink::DilithiumSeedExpansion;
40 using google::crypto::tink::KeyTemplate;
41 using google::crypto::tink::OutputPrefixType;
42 
NewDilithiumKeyTemplate(int32_t key_size,DilithiumSeedExpansion seed_expansion)43 KeyTemplate* NewDilithiumKeyTemplate(int32_t key_size,
44                                      DilithiumSeedExpansion seed_expansion) {
45   KeyTemplate* key_template = new KeyTemplate;
46   key_template->set_type_url(
47       absl::StrCat(kTypeGoogleapisCom, DilithiumPrivateKey().GetTypeName()));
48   key_template->set_output_prefix_type(OutputPrefixType::TINK);
49 
50   DilithiumKeyFormat key_format;
51   DilithiumParams* params = key_format.mutable_params();
52   params->set_key_size(key_size);
53   params->set_seed_expansion(seed_expansion);
54   key_format.SerializeToString(key_template->mutable_value());
55 
56   return key_template;
57 }
58 
59 }  // anonymous namespace
60 
Dilithium2KeyTemplate()61 const google::crypto::tink::KeyTemplate& Dilithium2KeyTemplate() {
62   static const KeyTemplate* key_template =
63       NewDilithiumKeyTemplate(PQCLEAN_DILITHIUM2_CRYPTO_SECRETKEYBYTES,
64                               DilithiumSeedExpansion::SEED_EXPANSION_SHAKE);
65   return *key_template;
66 }
67 
Dilithium3KeyTemplate()68 const google::crypto::tink::KeyTemplate& Dilithium3KeyTemplate() {
69   static const KeyTemplate* key_template =
70       NewDilithiumKeyTemplate(PQCLEAN_DILITHIUM3_CRYPTO_SECRETKEYBYTES,
71                               DilithiumSeedExpansion::SEED_EXPANSION_SHAKE);
72   return *key_template;
73 }
74 
Dilithium5KeyTemplate()75 const google::crypto::tink::KeyTemplate& Dilithium5KeyTemplate() {
76   static const KeyTemplate* key_template =
77       NewDilithiumKeyTemplate(PQCLEAN_DILITHIUM5_CRYPTO_SECRETKEYBYTES,
78                               DilithiumSeedExpansion::SEED_EXPANSION_SHAKE);
79   return *key_template;
80 }
81 
Dilithium2AesKeyTemplate()82 const google::crypto::tink::KeyTemplate& Dilithium2AesKeyTemplate() {
83   static const KeyTemplate* key_template =
84       NewDilithiumKeyTemplate(PQCLEAN_DILITHIUM2AES_CRYPTO_SECRETKEYBYTES,
85                               DilithiumSeedExpansion::SEED_EXPANSION_AES);
86   return *key_template;
87 }
88 
Dilithium3AesKeyTemplate()89 const google::crypto::tink::KeyTemplate& Dilithium3AesKeyTemplate() {
90   static const KeyTemplate* key_template =
91       NewDilithiumKeyTemplate(PQCLEAN_DILITHIUM3AES_CRYPTO_SECRETKEYBYTES,
92                               DilithiumSeedExpansion::SEED_EXPANSION_AES);
93   return *key_template;
94 }
95 
Dilithium5AesKeyTemplate()96 const google::crypto::tink::KeyTemplate& Dilithium5AesKeyTemplate() {
97   static const KeyTemplate* key_template =
98       NewDilithiumKeyTemplate(PQCLEAN_DILITHIUM5AES_CRYPTO_SECRETKEYBYTES,
99                               DilithiumSeedExpansion::SEED_EXPANSION_AES);
100   return *key_template;
101 }
102 
103 }  // namespace tink
104 }  // namespace crypto
105