| 6 | // |
| 7 | // https://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 | #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 | #define OPENSSL_UNSTABLE_EXPERIMENTAL_KYBER |
| 24 | #include <openssl/experimental/kyber.h> |
| 25 |
| 26 | #include "../fipsmodule/keccak/internal.h" |
| 27 | #include "../test/file_test.h" |
| 28 | #include "../test/test_util.h" |
| 29 | #include "./internal.h" |
| 30 |
| 31 |
| 32 | template <typename T> |
| 33 | static std::vector<uint8_t> Marshal(int (*marshal_func)(CBB *, const T *), |
| 34 | const T *t) { |
| 35 | bssl::ScopedCBB cbb; |
| 36 | uint8_t *encoded; |
| 37 | size_t encoded_len; |
| 38 | if (!CBB_init(cbb.get(), 1) || // |
| 39 | !marshal_func(cbb.get(), t) || // |
| 40 | !CBB_finish(cbb.get(), &encoded, &encoded_len)) { |
| 41 | abort(); |
| 42 | } |
| 43 |
| 44 | std::vector<uint8_t> ret(encoded, encoded + encoded_len); |
| 45 | OPENSSL_free(encoded); |
| 46 | return ret; |
| 47 | } |
| 48 |
| 49 | TEST(KyberTest, Basic) { |
| 50 | // This function makes several Kyber keys, which runs up against stack limits. |
| 51 | // Heap-allocate them instead. |
| 52 |
| 53 | uint8_t encoded_public_key[KYBER_PUBLIC_KEY_BYTES]; |
| 54 | auto priv = std::make_unique<KYBER_private_key>(); |
| 55 | KYBER_generate_key(encoded_public_key, priv.get()); |
| 56 |
| 57 | uint8_t first_two_bytes[2]; |
| 58 | OPENSSL_memcpy(first_two_bytes, encoded_public_key, sizeof(first_two_bytes)); |
| 59 | OPENSSL_memset(encoded_public_key, 0xff, sizeof(first_two_bytes)); |
| 60 | CBS encoded_public_key_cbs; |
| 61 | CBS_init(&encoded_public_key_cbs, encoded_public_key, |
| 62 | sizeof(encoded_public_key)); |
| 63 | auto pub = std::make_unique<KYBER_public_key>(); |
| 64 | // Parsing should fail because the first coefficient is >= kPrime; |
| 65 | ASSERT_FALSE(KYBER_parse_public_key(pub.get(), &encoded_public_key_cbs)); |
| 66 |
| 67 | OPENSSL_memcpy(encoded_public_key, first_two_bytes, sizeof(first_two_bytes)); |
| 68 | CBS_init(&encoded_public_key_cbs, encoded_public_key, |
| 69 | sizeof(encoded_public_key)); |
| 70 | ASSERT_TRUE(KYBER_parse_public_key(pub.get(), &encoded_public_key_cbs)); |
| 71 | EXPECT_EQ(CBS_len(&encoded_public_key_cbs), 0u); |
| 72 |
| 73 | EXPECT_EQ(Bytes(encoded_public_key), |
| 74 | Bytes(Marshal(KYBER_marshal_public_key, pub.get()))); |
| 75 |
| 76 | auto pub2 = std::make_unique<KYBER_public_key>(); |
| 77 | KYBER_public_from_private(pub2.get(), priv.get()); |
| 78 | EXPECT_EQ(Bytes(encoded_public_key), |
| 79 | Bytes(Marshal(KYBER_marshal_public_key, pub2.get()))); |
| 80 |
| 81 | std::vector<uint8_t> encoded_private_key( |
| 82 | Marshal(KYBER_marshal_private_key, priv.get())); |
| 83 | EXPECT_EQ(encoded_private_key.size(), size_t{KYBER_PRIVATE_KEY_BYTES}); |
| 84 |
| 85 | OPENSSL_memcpy(first_two_bytes, encoded_private_key.data(), |
| 86 | sizeof(first_two_bytes)); |
| 87 | OPENSSL_memset(encoded_private_key.data(), 0xff, sizeof(first_two_bytes)); |
| 88 | CBS cbs; |
| 89 | CBS_init(&cbs, encoded_private_key.data(), encoded_private_key.size()); |
| 90 | auto priv2 = std::make_unique<KYBER_private_key>(); |
| 91 | // Parsing should fail because the first coefficient is >= kPrime. |
| 92 | ASSERT_FALSE(KYBER_parse_private_key(priv2.get(), &cbs)); |
| 93 |
| 94 | OPENSSL_memcpy(encoded_private_key.data(), first_two_bytes, |
| 95 | sizeof(first_two_bytes)); |
| 96 | CBS_init(&cbs, encoded_private_key.data(), encoded_private_key.size()); |
| 97 | ASSERT_TRUE(KYBER_parse_private_key(priv2.get(), &cbs)); |
| 98 | EXPECT_EQ( |
| 99 | Bytes(Declassified(encoded_private_key)), |
| 100 | Bytes(Declassified((Marshal(KYBER_marshal_private_key, priv2.get()))))); |
| 101 |
| 102 | uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES]; |
| 103 | uint8_t shared_secret1[KYBER_SHARED_SECRET_BYTES]; |
| 104 | uint8_t shared_secret2[KYBER_SHARED_SECRET_BYTES]; |
| 105 | KYBER_encap(ciphertext, shared_secret1, pub.get()); |
| 106 | KYBER_decap(shared_secret2, ciphertext, priv.get()); |
| 107 | EXPECT_EQ(Bytes(Declassified(shared_secret1)), |
| 108 | Bytes(Declassified(shared_secret2))); |
| 109 | KYBER_decap(shared_secret2, ciphertext, priv2.get()); |
| 110 | EXPECT_EQ(Bytes(Declassified(shared_secret1)), |
| 111 | Bytes(Declassified(shared_secret2))); |
| 112 | } |
| 113 |
| 114 | static void KyberFileTest(FileTest *t) { |
| 115 | std::vector<uint8_t> seed, public_key_expected, private_key_expected, |
| 116 | ciphertext_expected, shared_secret_expected, given_generate_entropy, |
| 117 | given_encap_entropy_pre_hash; |
| 118 | t->IgnoreAttribute("count"); |
| 119 | ASSERT_TRUE(t->GetBytes(&seed, "seed")); |
| 120 | ASSERT_TRUE(t->GetBytes(&public_key_expected, "pk")); |
| 121 | ASSERT_TRUE(t->GetBytes(&private_key_expected, "sk")); |
| 122 | ASSERT_TRUE(t->GetBytes(&ciphertext_expected, "ct")); |
| 123 | ASSERT_TRUE(t->GetBytes(&shared_secret_expected, "ss")); |
| 124 | ASSERT_TRUE(t->GetBytes(&given_generate_entropy, "generateEntropy")); |
| 125 | ASSERT_TRUE( |
| 126 | t->GetBytes(&given_encap_entropy_pre_hash, "encapEntropyPreHash")); |
| 127 |
| 128 | KYBER_private_key priv; |
| 129 | uint8_t encoded_private_key[KYBER_PRIVATE_KEY_BYTES]; |
| 130 | KYBER_public_key pub; |
| 131 | uint8_t encoded_public_key[KYBER_PUBLIC_KEY_BYTES]; |
| 132 | uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES]; |
| 133 | uint8_t gen_key_entropy[KYBER_GENERATE_KEY_ENTROPY]; |
| 134 | uint8_t encap_entropy[KYBER_ENCAP_ENTROPY]; |
| 135 | uint8_t encapsulated_key[KYBER_SHARED_SECRET_BYTES]; |
| 136 | uint8_t decapsulated_key[KYBER_SHARED_SECRET_BYTES]; |
| 137 | // The test vectors provide a CTR-DRBG seed which is used to generate the |
| 138 | // input entropy. |
| 139 | ASSERT_EQ(seed.size(), size_t{CTR_DRBG_ENTROPY_LEN}); |
| 140 | CONSTTIME_SECRET(seed.data(), seed.size()); |
| 141 | { |
| 142 | bssl::UniquePtr<CTR_DRBG_STATE> state( |
| 143 | CTR_DRBG_new(seed.data(), nullptr, 0)); |
| 144 | ASSERT_TRUE(state); |
| 145 | ASSERT_TRUE( |
| 146 | CTR_DRBG_generate(state.get(), gen_key_entropy, 32, nullptr, 0)); |
| 147 | ASSERT_TRUE( |
| 148 | CTR_DRBG_generate(state.get(), gen_key_entropy + 32, 32, nullptr, 0)); |
| 149 | ASSERT_TRUE(CTR_DRBG_generate(state.get(), encap_entropy, |
| 150 | KYBER_ENCAP_ENTROPY, nullptr, 0)); |
| 151 | } |
| 152 |
| 153 | EXPECT_EQ(Bytes(Declassified(gen_key_entropy)), |
| 154 | Bytes(given_generate_entropy)); |
| 155 | EXPECT_EQ(Bytes(Declassified(encap_entropy)), |
| 156 | Bytes(given_encap_entropy_pre_hash)); |
| 157 |
| 158 | BORINGSSL_keccak(encap_entropy, sizeof(encap_entropy), encap_entropy, |
| 159 | sizeof(encap_entropy), boringssl_sha3_256); |
| 160 |
| 161 | KYBER_generate_key_external_entropy(encoded_public_key, &priv, |
| 162 | gen_key_entropy); |
| 163 | CBB cbb; |
| 164 | CBB_init_fixed(&cbb, encoded_private_key, sizeof(encoded_private_key)); |
| 165 | ASSERT_TRUE(KYBER_marshal_private_key(&cbb, &priv)); |
| 166 | CBS encoded_public_key_cbs; |
| 167 | CBS_init(&encoded_public_key_cbs, encoded_public_key, |
| 168 | sizeof(encoded_public_key)); |
| 169 | ASSERT_TRUE(KYBER_parse_public_key(&pub, &encoded_public_key_cbs)); |
| 170 | KYBER_encap_external_entropy(ciphertext, encapsulated_key, &pub, |
| 171 | encap_entropy); |
| 172 | KYBER_decap(decapsulated_key, ciphertext, &priv); |
| 173 |
| 174 | EXPECT_EQ(Bytes(Declassified(encapsulated_key)), |
| 175 | Bytes(Declassified(decapsulated_key))); |
| 176 | EXPECT_EQ(Bytes(private_key_expected), |
| 177 | Bytes(Declassified(encoded_private_key))); |
| 178 | EXPECT_EQ(Bytes(public_key_expected), Bytes(encoded_public_key)); |
| 179 | EXPECT_EQ(Bytes(ciphertext_expected), Bytes(ciphertext)); |
| 180 | EXPECT_EQ(Bytes(shared_secret_expected), |
| 181 | Bytes(Declassified(encapsulated_key))); |
| 182 |
| 183 | uint8_t corrupted_ciphertext[KYBER_CIPHERTEXT_BYTES]; |
| 184 | OPENSSL_memcpy(corrupted_ciphertext, ciphertext, KYBER_CIPHERTEXT_BYTES); |
| 185 | corrupted_ciphertext[3] ^= 0x40; |
| 186 | uint8_t corrupted_decapsulated_key[KYBER_SHARED_SECRET_BYTES]; |
| 187 | KYBER_decap(corrupted_decapsulated_key, corrupted_ciphertext, &priv); |
| 188 | // It would be nice to have actual test vectors for the failure case, but the |
| 189 | // NIST submission currently does not include those, so we are just testing |
| 190 | // for inequality. |
| 191 | EXPECT_NE(Bytes(Declassified(encapsulated_key)), |
| 192 | Bytes(Declassified(corrupted_decapsulated_key))); |
| 193 | } |
| 194 |
| 195 | TEST(KyberTest, TestVectors) { |
| 196 | FileTestGTest("crypto/kyber/kyber_tests.txt", KyberFileTest); |
| 197 | } |
| 198 |