1 /* Copyright (c) 2023, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <vector>
16
17 #include <string.h>
18
19 #include <gtest/gtest.h>
20
21 #include <openssl/bytestring.h>
22 #include <openssl/ctrdrbg.h>
23 #include <openssl/experimental/kyber.h>
24
25 #include "../test/file_test.h"
26 #include "../test/test_util.h"
27 #include "../keccak/internal.h"
28 #include "./internal.h"
29
30
31 template <typename T>
Marshal(int (* marshal_func)(CBB *,const T *),const T * t)32 static std::vector<uint8_t> Marshal(int (*marshal_func)(CBB *, const T *),
33 const T *t) {
34 bssl::ScopedCBB cbb;
35 uint8_t *encoded;
36 size_t encoded_len;
37 if (!CBB_init(cbb.get(), 1) || //
38 !marshal_func(cbb.get(), t) || //
39 !CBB_finish(cbb.get(), &encoded, &encoded_len)) {
40 abort();
41 }
42
43 std::vector<uint8_t> ret(encoded, encoded + encoded_len);
44 OPENSSL_free(encoded);
45 return ret;
46 }
47
TEST(KyberTest,Basic)48 TEST(KyberTest, Basic) {
49 // This function makes several Kyber keys, which runs up against stack limits.
50 // Heap-allocate them instead.
51
52 uint8_t encoded_public_key[KYBER_PUBLIC_KEY_BYTES];
53 auto priv = std::make_unique<KYBER_private_key>();
54 KYBER_generate_key(encoded_public_key, priv.get());
55
56 uint8_t first_two_bytes[2];
57 OPENSSL_memcpy(first_two_bytes, encoded_public_key, sizeof(first_two_bytes));
58 OPENSSL_memset(encoded_public_key, 0xff, sizeof(first_two_bytes));
59 CBS encoded_public_key_cbs;
60 CBS_init(&encoded_public_key_cbs, encoded_public_key,
61 sizeof(encoded_public_key));
62 auto pub = std::make_unique<KYBER_public_key>();
63 // Parsing should fail because the first coefficient is >= kPrime;
64 ASSERT_FALSE(KYBER_parse_public_key(pub.get(), &encoded_public_key_cbs));
65
66 OPENSSL_memcpy(encoded_public_key, first_two_bytes, sizeof(first_two_bytes));
67 CBS_init(&encoded_public_key_cbs, encoded_public_key,
68 sizeof(encoded_public_key));
69 ASSERT_TRUE(KYBER_parse_public_key(pub.get(), &encoded_public_key_cbs));
70 EXPECT_EQ(CBS_len(&encoded_public_key_cbs), 0u);
71
72 EXPECT_EQ(Bytes(encoded_public_key),
73 Bytes(Marshal(KYBER_marshal_public_key, pub.get())));
74
75 auto pub2 = std::make_unique<KYBER_public_key>();
76 KYBER_public_from_private(pub2.get(), priv.get());
77 EXPECT_EQ(Bytes(encoded_public_key),
78 Bytes(Marshal(KYBER_marshal_public_key, pub2.get())));
79
80 std::vector<uint8_t> encoded_private_key(
81 Marshal(KYBER_marshal_private_key, priv.get()));
82 EXPECT_EQ(encoded_private_key.size(), size_t{KYBER_PRIVATE_KEY_BYTES});
83
84 OPENSSL_memcpy(first_two_bytes, encoded_private_key.data(),
85 sizeof(first_two_bytes));
86 OPENSSL_memset(encoded_private_key.data(), 0xff, sizeof(first_two_bytes));
87 CBS cbs;
88 CBS_init(&cbs, encoded_private_key.data(), encoded_private_key.size());
89 auto priv2 = std::make_unique<KYBER_private_key>();
90 // Parsing should fail because the first coefficient is >= kPrime.
91 ASSERT_FALSE(KYBER_parse_private_key(priv2.get(), &cbs));
92
93 OPENSSL_memcpy(encoded_private_key.data(), first_two_bytes,
94 sizeof(first_two_bytes));
95 CBS_init(&cbs, encoded_private_key.data(), encoded_private_key.size());
96 ASSERT_TRUE(KYBER_parse_private_key(priv2.get(), &cbs));
97 EXPECT_EQ(Bytes(encoded_private_key),
98 Bytes(Marshal(KYBER_marshal_private_key, priv2.get())));
99
100 uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES];
101 uint8_t shared_secret1[KYBER_SHARED_SECRET_BYTES];
102 uint8_t shared_secret2[KYBER_SHARED_SECRET_BYTES];
103 KYBER_encap(ciphertext, shared_secret1, pub.get());
104 KYBER_decap(shared_secret2, ciphertext, priv.get());
105 EXPECT_EQ(Bytes(shared_secret1), Bytes(shared_secret2));
106 KYBER_decap(shared_secret2, ciphertext, priv2.get());
107 EXPECT_EQ(Bytes(shared_secret1), Bytes(shared_secret2));
108 }
109
KyberFileTest(FileTest * t)110 static void KyberFileTest(FileTest *t) {
111 std::vector<uint8_t> seed, public_key_expected, private_key_expected,
112 ciphertext_expected, shared_secret_expected, given_generate_entropy,
113 given_encap_entropy_pre_hash;
114 t->IgnoreAttribute("count");
115 ASSERT_TRUE(t->GetBytes(&seed, "seed"));
116 ASSERT_TRUE(t->GetBytes(&public_key_expected, "pk"));
117 ASSERT_TRUE(t->GetBytes(&private_key_expected, "sk"));
118 ASSERT_TRUE(t->GetBytes(&ciphertext_expected, "ct"));
119 ASSERT_TRUE(t->GetBytes(&shared_secret_expected, "ss"));
120 ASSERT_TRUE(t->GetBytes(&given_generate_entropy, "generateEntropy"));
121 ASSERT_TRUE(
122 t->GetBytes(&given_encap_entropy_pre_hash, "encapEntropyPreHash"));
123
124 KYBER_private_key priv;
125 uint8_t encoded_private_key[KYBER_PRIVATE_KEY_BYTES];
126 KYBER_public_key pub;
127 uint8_t encoded_public_key[KYBER_PUBLIC_KEY_BYTES];
128 uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES];
129 uint8_t gen_key_entropy[KYBER_GENERATE_KEY_ENTROPY];
130 uint8_t encap_entropy[KYBER_ENCAP_ENTROPY];
131 uint8_t encapsulated_key[KYBER_SHARED_SECRET_BYTES];
132 uint8_t decapsulated_key[KYBER_SHARED_SECRET_BYTES];
133 // The test vectors provide a CTR-DRBG seed which is used to generate the
134 // input entropy.
135 ASSERT_EQ(seed.size(), size_t{CTR_DRBG_ENTROPY_LEN});
136 {
137 bssl::UniquePtr<CTR_DRBG_STATE> state(
138 CTR_DRBG_new(seed.data(), nullptr, 0));
139 ASSERT_TRUE(state);
140 ASSERT_TRUE(
141 CTR_DRBG_generate(state.get(), gen_key_entropy, 32, nullptr, 0));
142 ASSERT_TRUE(
143 CTR_DRBG_generate(state.get(), gen_key_entropy + 32, 32, nullptr, 0));
144 ASSERT_TRUE(CTR_DRBG_generate(state.get(), encap_entropy,
145 KYBER_ENCAP_ENTROPY, nullptr, 0));
146 }
147
148 EXPECT_EQ(Bytes(gen_key_entropy), Bytes(given_generate_entropy));
149 EXPECT_EQ(Bytes(encap_entropy), Bytes(given_encap_entropy_pre_hash));
150
151 BORINGSSL_keccak(encap_entropy, sizeof(encap_entropy), encap_entropy,
152 sizeof(encap_entropy), boringssl_sha3_256);
153
154 KYBER_generate_key_external_entropy(encoded_public_key, &priv,
155 gen_key_entropy);
156 CBB cbb;
157 CBB_init_fixed(&cbb, encoded_private_key, sizeof(encoded_private_key));
158 ASSERT_TRUE(KYBER_marshal_private_key(&cbb, &priv));
159 CBS encoded_public_key_cbs;
160 CBS_init(&encoded_public_key_cbs, encoded_public_key,
161 sizeof(encoded_public_key));
162 ASSERT_TRUE(KYBER_parse_public_key(&pub, &encoded_public_key_cbs));
163 KYBER_encap_external_entropy(ciphertext, encapsulated_key, &pub,
164 encap_entropy);
165 KYBER_decap(decapsulated_key, ciphertext, &priv);
166
167 EXPECT_EQ(Bytes(encapsulated_key), Bytes(decapsulated_key));
168 EXPECT_EQ(Bytes(private_key_expected), Bytes(encoded_private_key));
169 EXPECT_EQ(Bytes(public_key_expected), Bytes(encoded_public_key));
170 EXPECT_EQ(Bytes(ciphertext_expected), Bytes(ciphertext));
171 EXPECT_EQ(Bytes(shared_secret_expected), Bytes(encapsulated_key));
172
173 uint8_t corrupted_ciphertext[KYBER_CIPHERTEXT_BYTES];
174 OPENSSL_memcpy(corrupted_ciphertext, ciphertext, KYBER_CIPHERTEXT_BYTES);
175 corrupted_ciphertext[3] ^= 0x40;
176 uint8_t corrupted_decapsulated_key[KYBER_SHARED_SECRET_BYTES];
177 KYBER_decap(corrupted_decapsulated_key, corrupted_ciphertext, &priv);
178 // It would be nice to have actual test vectors for the failure case, but the
179 // NIST submission currently does not include those, so we are just testing
180 // for inequality.
181 EXPECT_NE(Bytes(encapsulated_key), Bytes(corrupted_decapsulated_key));
182 }
183
TEST(KyberTest,TestVectors)184 TEST(KyberTest, TestVectors) {
185 FileTestGTest("crypto/kyber/kyber_tests.txt", KyberFileTest);
186 }
187