• 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/subtle/dilithium_key.h"
18 
19 #include <string>
20 #include <utility>
21 
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "absl/strings/str_cat.h"
25 #include "tink/util/status.h"
26 #include "tink/util/statusor.h"
27 #include "tink/util/test_matchers.h"
28 
29 extern "C" {
30 #include "third_party/pqclean/crypto_sign/dilithium2/api.h"
31 #include "third_party/pqclean/crypto_sign/dilithium2aes/api.h"
32 #include "third_party/pqclean/crypto_sign/dilithium3/api.h"
33 #include "third_party/pqclean/crypto_sign/dilithium3aes/api.h"
34 #include "third_party/pqclean/crypto_sign/dilithium5/api.h"
35 #include "third_party/pqclean/crypto_sign/dilithium5aes/api.h"
36 }
37 
38 namespace crypto {
39 namespace tink {
40 namespace subtle {
41 namespace {
42 
43 using ::crypto::tink::test::IsOk;
44 
45 struct DilithiumTestCase {
46   std::string test_name;
47   int32_t private_key_size;
48   int32_t public_key_size;
49   DilithiumSeedExpansion seed_expansion;
50 };
51 
52 using DilithiumKeyTest = testing::TestWithParam<DilithiumTestCase>;
53 
TEST_P(DilithiumKeyTest,DilithiumKeysLength)54 TEST_P(DilithiumKeyTest, DilithiumKeysLength) {
55   const DilithiumTestCase& test_case = GetParam();
56 
57   // Generate key pair.
58   util::StatusOr<
59       std::pair<DilithiumPrivateKeyPqclean, DilithiumPublicKeyPqclean>>
60       key_pair = DilithiumPrivateKeyPqclean::GenerateKeyPair(
61           test_case.private_key_size, test_case.seed_expansion);
62 
63   ASSERT_THAT(key_pair, IsOk());
64 
65   // Check keys size.
66   EXPECT_EQ((key_pair->first).GetKeyData().size(), test_case.private_key_size);
67   EXPECT_EQ((key_pair->second).GetKeyData().size(), test_case.public_key_size);
68 }
69 
TEST_P(DilithiumKeyTest,DifferentContent)70 TEST_P(DilithiumKeyTest, DifferentContent) {
71   const DilithiumTestCase& test_case = GetParam();
72 
73   // Generate key pair.
74   util::StatusOr<
75       std::pair<DilithiumPrivateKeyPqclean, DilithiumPublicKeyPqclean>>
76       key_pair = DilithiumPrivateKeyPqclean::GenerateKeyPair(
77           test_case.private_key_size, test_case.seed_expansion);
78 
79   ASSERT_THAT(key_pair, IsOk());
80 
81   // Check keys content is different.
82   EXPECT_NE(util::SecretDataAsStringView(key_pair->first.GetKeyData()),
83             key_pair->second.GetKeyData());
84 }
85 
86 INSTANTIATE_TEST_SUITE_P(
87     DilithiumKeyTesta, DilithiumKeyTest,
88     testing::ValuesIn<DilithiumTestCase>({
89         {"Dilithium2", PQCLEAN_DILITHIUM2_CRYPTO_SECRETKEYBYTES,
90          PQCLEAN_DILITHIUM2_CRYPTO_PUBLICKEYBYTES,
91          DilithiumSeedExpansion::SEED_EXPANSION_SHAKE},
92         {"Dilithium3", PQCLEAN_DILITHIUM3_CRYPTO_SECRETKEYBYTES,
93          PQCLEAN_DILITHIUM3_CRYPTO_PUBLICKEYBYTES,
94          DilithiumSeedExpansion::SEED_EXPANSION_SHAKE},
95         {"Dilithium5", PQCLEAN_DILITHIUM5_CRYPTO_SECRETKEYBYTES,
96          PQCLEAN_DILITHIUM5_CRYPTO_PUBLICKEYBYTES,
97          DilithiumSeedExpansion::SEED_EXPANSION_SHAKE},
98         {"Dilithium2Aes", PQCLEAN_DILITHIUM2AES_CRYPTO_SECRETKEYBYTES,
99          PQCLEAN_DILITHIUM2AES_CRYPTO_PUBLICKEYBYTES,
100          DilithiumSeedExpansion::SEED_EXPANSION_AES},
101         {"Dilithium3Aes", PQCLEAN_DILITHIUM3AES_CRYPTO_SECRETKEYBYTES,
102          PQCLEAN_DILITHIUM3AES_CRYPTO_PUBLICKEYBYTES,
103          DilithiumSeedExpansion::SEED_EXPANSION_AES},
104         {"Dilithium5Aes", PQCLEAN_DILITHIUM5AES_CRYPTO_SECRETKEYBYTES,
105          PQCLEAN_DILITHIUM5AES_CRYPTO_PUBLICKEYBYTES,
106          DilithiumSeedExpansion::SEED_EXPANSION_AES},
107     }),
__anon3225eb720202(const testing::TestParamInfo<DilithiumKeyTest::ParamType>& info) 108     [](const testing::TestParamInfo<DilithiumKeyTest::ParamType>& info) {
109       return info.param.test_name;
110     });
111 
112 }  // namespace
113 }  // namespace subtle
114 }  // namespace tink
115 }  // namespace crypto
116