• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
20 #include <string>
21 
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "tink/core/key_manager_impl.h"
25 #include "tink/core/private_key_manager_impl.h"
26 #include "tink/experimental/pqcrypto/signature/dilithium_sign_key_manager.h"
27 #include "tink/experimental/pqcrypto/signature/dilithium_verify_key_manager.h"
28 #include "tink/util/test_matchers.h"
29 #include "proto/tink.pb.h"
30 
31 extern "C" {
32 #include "third_party/pqclean/crypto_sign/dilithium2/api.h"
33 #include "third_party/pqclean/crypto_sign/dilithium2aes/api.h"
34 #include "third_party/pqclean/crypto_sign/dilithium3/api.h"
35 #include "third_party/pqclean/crypto_sign/dilithium3aes/api.h"
36 #include "third_party/pqclean/crypto_sign/dilithium5/api.h"
37 #include "third_party/pqclean/crypto_sign/dilithium5aes/api.h"
38 }
39 
40 namespace crypto {
41 namespace tink {
42 namespace {
43 
44 using ::crypto::tink::test::IsOk;
45 using ::google::crypto::tink::DilithiumKeyFormat;
46 using ::google::crypto::tink::DilithiumParams;
47 using ::google::crypto::tink::DilithiumPrivateKey;
48 using ::google::crypto::tink::DilithiumSeedExpansion;
49 using ::google::crypto::tink::KeyTemplate;
50 using ::google::crypto::tink::OutputPrefixType;
51 
52 struct DilithiumKeyTemplateTestCase {
53   std::string test_name;
54   int32_t key_size;
55   DilithiumSeedExpansion seed_expansion;
56   KeyTemplate key_template;
57 };
58 
59 using DilithiumKeyTemplateTest =
60     testing::TestWithParam<DilithiumKeyTemplateTestCase>;
61 
TEST_P(DilithiumKeyTemplateTest,CheckDilithiumInitialization)62 TEST_P(DilithiumKeyTemplateTest, CheckDilithiumInitialization) {
63   std::string type_url =
64       "type.googleapis.com/google.crypto.tink.DilithiumPrivateKey";
65   const KeyTemplate& key_template = GetParam().key_template;
66 
67   EXPECT_EQ(type_url, key_template.type_url());
68   EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
69 }
70 
TEST_P(DilithiumKeyTemplateTest,ValidateKeyFormat)71 TEST_P(DilithiumKeyTemplateTest, ValidateKeyFormat) {
72   const DilithiumKeyTemplateTestCase& test_case = GetParam();
73   DilithiumKeyFormat key_format;
74 
75   DilithiumParams* params = key_format.mutable_params();
76   params->set_key_size(test_case.key_size);
77   params->set_seed_expansion(test_case.seed_expansion);
78 
79   EXPECT_THAT(DilithiumSignKeyManager().ValidateKeyFormat(key_format), IsOk());
80   EXPECT_TRUE(key_format.ParseFromString(test_case.key_template.value()));
81 }
82 
TEST_P(DilithiumKeyTemplateTest,SameReference)83 TEST_P(DilithiumKeyTemplateTest, SameReference) {
84   const KeyTemplate& key_template = GetParam().key_template;
85   const KeyTemplate& key_template_2 = GetParam().key_template;
86 
87   EXPECT_EQ(&key_template, &key_template_2);
88 }
89 
TEST_P(DilithiumKeyTemplateTest,KeyManagerCompatibility)90 TEST_P(DilithiumKeyTemplateTest, KeyManagerCompatibility) {
91   DilithiumSignKeyManager sign_key_manager;
92   DilithiumVerifyKeyManager verify_key_manager;
93   std::unique_ptr<KeyManager<PublicKeySign>> key_manager =
94       internal::MakePrivateKeyManager<PublicKeySign>(&sign_key_manager,
95                                                      &verify_key_manager);
96   DilithiumKeyFormat key_format;
97   const DilithiumKeyTemplateTestCase& test_case = GetParam();
98 
99   DilithiumParams* params = key_format.mutable_params();
100   params->set_key_size(test_case.key_size);
101   params->set_seed_expansion(test_case.seed_expansion);
102 
103   util::StatusOr<std::unique_ptr<portable_proto::MessageLite>> new_key_result =
104       key_manager->get_key_factory().NewKey(key_format);
105   EXPECT_THAT(new_key_result, IsOk());
106 }
107 
108 INSTANTIATE_TEST_SUITE_P(
109     DilithiumKeyTemplateTests, DilithiumKeyTemplateTest,
110     testing::ValuesIn<DilithiumKeyTemplateTestCase>(
111         {{"Dilithium2", PQCLEAN_DILITHIUM2_CRYPTO_SECRETKEYBYTES,
112           DilithiumSeedExpansion::SEED_EXPANSION_SHAKE,
113           Dilithium2KeyTemplate()},
114          {"Dilithium3", PQCLEAN_DILITHIUM3_CRYPTO_SECRETKEYBYTES,
115           DilithiumSeedExpansion::SEED_EXPANSION_SHAKE,
116           Dilithium3KeyTemplate()},
117          {"Dilithium5", PQCLEAN_DILITHIUM5_CRYPTO_SECRETKEYBYTES,
118           DilithiumSeedExpansion::SEED_EXPANSION_SHAKE,
119           Dilithium5KeyTemplate()},
120          {"Dilithium2Aes", PQCLEAN_DILITHIUM2AES_CRYPTO_SECRETKEYBYTES,
121           DilithiumSeedExpansion::SEED_EXPANSION_AES,
122           Dilithium2AesKeyTemplate()},
123          {"Dilithium3Aes", PQCLEAN_DILITHIUM3AES_CRYPTO_SECRETKEYBYTES,
124           DilithiumSeedExpansion::SEED_EXPANSION_AES,
125           Dilithium3AesKeyTemplate()},
126          {"Dilithium5Aes", PQCLEAN_DILITHIUM5AES_CRYPTO_SECRETKEYBYTES,
127           DilithiumSeedExpansion::SEED_EXPANSION_AES,
128           Dilithium5AesKeyTemplate()}}),
129     [](const testing::TestParamInfo<DilithiumKeyTemplateTest::ParamType>&
__anonb2f3970d0202(const testing::TestParamInfo<DilithiumKeyTemplateTest::ParamType>& info) 130            info) { return info.param.test_name; });
131 
132 }  // namespace
133 }  // namespace tink
134 }  // namespace crypto
135