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/falcon_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/falcon_sign_key_manager.h"
27 #include "tink/experimental/pqcrypto/signature/falcon_verify_key_manager.h"
28 #include "tink/experimental/pqcrypto/signature/subtle/falcon_subtle_utils.h"
29 #include "tink/util/test_matchers.h"
30 #include "proto/tink.pb.h"
31
32 namespace crypto {
33 namespace tink {
34 namespace {
35
36 using ::crypto::tink::test::IsOk;
37 using ::google::crypto::tink::FalconKeyFormat;
38 using ::google::crypto::tink::FalconPrivateKey;
39 using google::crypto::tink::KeyTemplate;
40 using google::crypto::tink::OutputPrefixType;
41
42 struct FalconTestCase {
43 std::string test_name;
44 int32_t private_key_size;
45 KeyTemplate key_template;
46 };
47
48 using FalconKeyTemplateTest = testing::TestWithParam<FalconTestCase>;
49
TEST_P(FalconKeyTemplateTest,CheckKeyTemplateValid)50 TEST_P(FalconKeyTemplateTest, CheckKeyTemplateValid) {
51 std::string type_url =
52 "type.googleapis.com/google.crypto.tink.FalconPrivateKey";
53
54 const FalconTestCase& test_case = GetParam();
55 EXPECT_EQ(type_url, test_case.key_template.type_url());
56 EXPECT_EQ(OutputPrefixType::TINK,
57 test_case.key_template.output_prefix_type());
58
59 FalconKeyFormat key_format;
60 EXPECT_TRUE(key_format.ParseFromString(test_case.key_template.value()));
61 EXPECT_EQ(test_case.private_key_size, key_format.key_size());
62 }
63
TEST_P(FalconKeyTemplateTest,SameReference)64 TEST_P(FalconKeyTemplateTest, SameReference) {
65 const KeyTemplate& key_template = GetParam().key_template;
66 const KeyTemplate& key_template_2 = GetParam().key_template;
67
68 EXPECT_EQ(&key_template, &key_template_2);
69 }
70
TEST_P(FalconKeyTemplateTest,KeyManagerCompatibility)71 TEST_P(FalconKeyTemplateTest, KeyManagerCompatibility) {
72 FalconSignKeyManager sign_key_manager;
73 FalconVerifyKeyManager verify_key_manager;
74 std::unique_ptr<KeyManager<PublicKeySign>> key_manager =
75 internal::MakePrivateKeyManager<PublicKeySign>(&sign_key_manager,
76 &verify_key_manager);
77 FalconKeyFormat key_format;
78 const FalconTestCase& test_case = GetParam();
79 key_format.set_key_size(test_case.private_key_size);
80
81 util::StatusOr<std::unique_ptr<portable_proto::MessageLite>> new_key_result =
82 key_manager->get_key_factory().NewKey(key_format);
83 EXPECT_THAT(new_key_result, IsOk());
84 }
85
86 INSTANTIATE_TEST_SUITE_P(
87 FalconKeyTemplateTests, FalconKeyTemplateTest,
88 testing::ValuesIn<FalconTestCase>(
89 {{"Falcon512", subtle::kFalcon512PrivateKeySize,
90 Falcon512KeyTemplate()},
91 {"Falcon1024", subtle::kFalcon1024PrivateKeySize,
92 Falcon1024KeyTemplate()}}),
__anon48e585b70202(const testing::TestParamInfo<FalconKeyTemplateTest::ParamType>& info) 93 [](const testing::TestParamInfo<FalconKeyTemplateTest::ParamType>& info) {
94 return info.param.test_name;
95 });
96
97 } // namespace
98
99 } // namespace tink
100 } // namespace crypto
101