| 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 <openssl/mldsa.h> |
| 16 |
| 17 | #include <memory> |
| 18 | #include <vector> |
| 19 |
| 20 | #include <gtest/gtest.h> |
| 21 |
| 22 | #include <openssl/bytestring.h> |
| 23 | #include <openssl/mem.h> |
| 24 | #include <openssl/span.h> |
| 25 |
| 26 | #include "../fipsmodule/bcm_interface.h" |
| 27 | #include "../internal.h" |
| 28 | #include "../test/file_test.h" |
| 29 | #include "../test/test_util.h" |
| 30 |
| 31 |
| 32 | namespace { |
| 33 |
| 34 | template <typename T> |
| 35 | std::vector<uint8_t> Marshal(bcm_status (*marshal_func)(CBB *, const T *), |
| 36 | const T *t) { |
| 37 | bssl::ScopedCBB cbb; |
| 38 | uint8_t *encoded; |
| 39 | size_t encoded_len; |
| 40 | if (!CBB_init(cbb.get(), 1) || // |
| 41 | marshal_func(cbb.get(), t) != bcm_status::approved || // |
| 42 | !CBB_finish(cbb.get(), &encoded, &encoded_len)) { |
| 43 | abort(); |
| 44 | } |
| 45 |
| 46 | std::vector<uint8_t> ret(encoded, encoded + encoded_len); |
| 47 | OPENSSL_free(encoded); |
| 48 | return ret; |
| 49 | } |
| 50 |
| 51 | // This test is very slow, so it is disabled by default. |
| 52 | TEST(MLDSATest, DISABLED_BitFlips) { |
| 53 | std::vector<uint8_t> encoded_public_key(MLDSA65_PUBLIC_KEY_BYTES); |
| 54 | auto priv = std::make_unique<MLDSA65_private_key>(); |
| 55 | uint8_t seed[MLDSA_SEED_BYTES]; |
| 56 | EXPECT_TRUE( |
| 57 | MLDSA65_generate_key(encoded_public_key.data(), seed, priv.get())); |
| 58 |
| 59 | std::vector<uint8_t> encoded_signature(MLDSA65_SIGNATURE_BYTES); |
| 60 | static const uint8_t kMessage[] = {'H', 'e', 'l', 'l', 'o', ' ', |
| 61 | 'w', 'o', 'r', 'l', 'd'}; |
| 62 | EXPECT_TRUE(MLDSA65_sign(encoded_signature.data(), priv.get(), kMessage, |
| 63 | sizeof(kMessage), nullptr, 0)); |
| 64 |
| 65 | auto pub = std::make_unique<MLDSA65_public_key>(); |
| 66 | CBS cbs = CBS(encoded_public_key); |
| 67 | ASSERT_TRUE(MLDSA65_parse_public_key(pub.get(), &cbs)); |
| 68 |
| 69 | EXPECT_EQ(MLDSA65_verify(pub.get(), encoded_signature.data(), |
| 70 | encoded_signature.size(), kMessage, sizeof(kMessage), |
| 71 | nullptr, 0), |
| 72 | 1); |
| 73 |
| 74 | for (size_t i = 0; i < MLDSA65_SIGNATURE_BYTES; i++) { |
| 75 | for (int j = 0; j < 8; j++) { |
| 76 | encoded_signature[i] ^= 1 << j; |
| 77 | EXPECT_EQ(MLDSA65_verify(pub.get(), encoded_signature.data(), |
| 78 | encoded_signature.size(), kMessage, |
| 79 | sizeof(kMessage), nullptr, 0), |
| 80 | 0) |
| 81 | << "Bit flip in signature at byte " << i << " bit " << j |
| 82 | << " didn't cause a verification failure"; |
| 83 | encoded_signature[i] ^= 1 << j; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 |
| 88 | template < |
| 89 | typename PrivateKey, typename PublicKey, size_t PublicKeyBytes, |
| 90 | size_t SignatureBytes, int (*Generate)(uint8_t *, uint8_t *, PrivateKey *), |
| 91 | int (*Sign)(uint8_t *, const PrivateKey *, const uint8_t *, size_t, |
| 92 | const uint8_t *, size_t), |
| 93 | int (*ParsePublicKey)(PublicKey *, CBS *), |
| 94 | int (*Verify)(const PublicKey *, const uint8_t *, size_t, const uint8_t *, |
| 95 | size_t, const uint8_t *, size_t), |
| 96 | int (*PrivateKeyFromSeed)(PrivateKey *, const uint8_t *, size_t), |
| 97 | typename BCMPrivateKey, bcm_status (*ParsePrivate)(BCMPrivateKey *, CBS *), |
| 98 | bcm_status (*MarshalPrivate)(CBB *, const BCMPrivateKey *)> |
| 99 | static void MLDSABasicTest() { |
| 100 | std::vector<uint8_t> encoded_public_key(PublicKeyBytes); |
| 101 | auto priv = std::make_unique<PrivateKey>(); |
| 102 | uint8_t seed[MLDSA_SEED_BYTES]; |
| 103 | EXPECT_TRUE(Generate(encoded_public_key.data(), seed, priv.get())); |
| 104 |
| 105 | const std::vector<uint8_t> encoded_private_key = |
| 106 | Marshal(MarshalPrivate, reinterpret_cast<BCMPrivateKey *>(priv.get())); |
| 107 | CBS cbs = CBS(encoded_private_key); |
| 108 | EXPECT_TRUE(bcm_success( |
| 109 | ParsePrivate(reinterpret_cast<BCMPrivateKey *>(priv.get()), &cbs))); |
| 110 |
| 111 | std::vector<uint8_t> encoded_signature(SignatureBytes); |
| 112 | static const uint8_t kMessage[] = {'H', 'e', 'l', 'l', 'o', ' ', |
| 113 | 'w', 'o', 'r', 'l', 'd'}; |
| 114 | static const uint8_t kContext[] = {'c', 't', 'x'}; |
| 115 | EXPECT_TRUE(Sign(encoded_signature.data(), priv.get(), kMessage, |
| 116 | sizeof(kMessage), kContext, sizeof(kContext))); |
| 117 |
| 118 | auto pub = std::make_unique<PublicKey>(); |
| 119 | cbs = CBS(encoded_public_key); |
| 120 | ASSERT_TRUE(ParsePublicKey(pub.get(), &cbs)); |
| 121 |
| 122 | EXPECT_EQ( |
| 123 | Verify(pub.get(), encoded_signature.data(), encoded_signature.size(), |
| 124 | kMessage, sizeof(kMessage), kContext, sizeof(kContext)), |
| 125 | 1); |
| 126 |
| 127 | auto priv2 = std::make_unique<PrivateKey>(); |
| 128 | EXPECT_TRUE(PrivateKeyFromSeed(priv2.get(), seed, sizeof(seed))); |
| 129 |
| 130 | EXPECT_EQ( |
| 131 | Bytes(Declassified(Marshal( |
| 132 | MarshalPrivate, reinterpret_cast<BCMPrivateKey *>(priv.get())))), |
| 133 | Bytes(Declassified(Marshal( |
| 134 | MarshalPrivate, reinterpret_cast<BCMPrivateKey *>(priv2.get()))))); |
| 135 | } |
| 136 |
| 137 | TEST(MLDSATest, Basic65) { |
| 138 | MLDSABasicTest<MLDSA65_private_key, MLDSA65_public_key, |
| 139 | MLDSA65_PUBLIC_KEY_BYTES, MLDSA65_SIGNATURE_BYTES, |
| 140 | MLDSA65_generate_key, MLDSA65_sign, MLDSA65_parse_public_key, |
| 141 | MLDSA65_verify, MLDSA65_private_key_from_seed, |
| 142 | BCM_mldsa65_private_key, BCM_mldsa65_parse_private_key, |
| 143 | BCM_mldsa65_marshal_private_key>(); |
| 144 | } |
| 145 |
| 146 | // These are the wrapper functions needed for `MLDSABasicTest`. ML-DSA-87 isn't |
| 147 | // publicly exposed yet, so they are included here. It's good to exercise the |
| 148 | // ML-DSA-65 wrapper functions so that they aren't untested (even if they are |
| 149 | // quite trivial) thus `MLDSABasicTest` is done this way around. |
| 150 |
| 151 | struct MLDSA87_private_key { |
| 152 | BCM_mldsa87_private_key priv; |
| 153 | }; |
| 154 |
| 155 | struct MLDSA87_public_key { |
| 156 | BCM_mldsa87_public_key pub; |
| 157 | }; |
| 158 |
| 159 | static int MLDSA87_generate_key( |
| 160 | uint8_t out_encoded_public_key[BCM_MLDSA87_PUBLIC_KEY_BYTES], |
| 161 | uint8_t out_seed[MLDSA_SEED_BYTES], |
| 162 | struct MLDSA87_private_key *out_private_key) { |
| 163 | return bcm_success(BCM_mldsa87_generate_key( |
| 164 | out_encoded_public_key, out_seed, |
| 165 | reinterpret_cast<BCM_mldsa87_private_key *>(out_private_key))); |
| 166 | } |
| 167 |
| 168 | static int MLDSA87_private_key_from_seed( |
| 169 | struct MLDSA87_private_key *out_private_key, const uint8_t *seed, |
| 170 | size_t seed_len) { |
| 171 | if (seed_len != BCM_MLDSA_SEED_BYTES) { |
| 172 | return 0; |
| 173 | } |
| 174 | return bcm_success(BCM_mldsa87_private_key_from_seed( |
| 175 | reinterpret_cast<BCM_mldsa87_private_key *>(out_private_key), seed)); |
| 176 | } |
| 177 |
| 178 | static int MLDSA87_sign( |
| 179 | uint8_t out_encoded_signature[BCM_MLDSA87_SIGNATURE_BYTES], |
| 180 | const struct MLDSA87_private_key *private_key, const uint8_t *msg, |
| 181 | size_t msg_len, const uint8_t *context, size_t context_len) { |
| 182 | return bcm_success(BCM_mldsa87_sign( |
| 183 | out_encoded_signature, |
| 184 | reinterpret_cast<const BCM_mldsa87_private_key *>(private_key), msg, |
| 185 | msg_len, context, context_len)); |
| 186 | } |
| 187 |
| 188 | static int MLDSA87_verify(const struct MLDSA87_public_key *public_key, |
| 189 | const uint8_t *signature, size_t signature_len, |
| 190 | const uint8_t *msg, size_t msg_len, |
| 191 | const uint8_t *context, size_t context_len) { |
| 192 | if (context_len > 255 || signature_len != BCM_MLDSA87_SIGNATURE_BYTES) { |
| 193 | return 0; |
| 194 | } |
| 195 | return bcm_success(BCM_mldsa87_verify( |
| 196 | reinterpret_cast<const BCM_mldsa87_public_key *>(public_key), signature, |
| 197 | msg, msg_len, context, context_len)); |
| 198 | } |
| 199 |
| 200 | static int MLDSA87_parse_public_key(struct MLDSA87_public_key *public_key, |
| 201 | CBS *in) { |
| 202 | return bcm_success(BCM_mldsa87_parse_public_key( |
| 203 | reinterpret_cast<BCM_mldsa87_public_key *>(public_key), in)); |
| 204 | } |
| 205 |
| 206 | TEST(MLDSATest, Basic87) { |
| 207 | MLDSABasicTest<MLDSA87_private_key, MLDSA87_public_key, |
| 208 | BCM_MLDSA87_PUBLIC_KEY_BYTES, BCM_MLDSA87_SIGNATURE_BYTES, |
| 209 | MLDSA87_generate_key, MLDSA87_sign, MLDSA87_parse_public_key, |
| 210 | MLDSA87_verify, MLDSA87_private_key_from_seed, |
| 211 | BCM_mldsa87_private_key, BCM_mldsa87_parse_private_key, |
| 212 | BCM_mldsa87_marshal_private_key>(); |
| 213 | } |
| 214 |
| 215 | TEST(MLDSATest, SignatureIsRandomized) { |
| 216 | std::vector<uint8_t> encoded_public_key(MLDSA65_PUBLIC_KEY_BYTES); |
| 217 | auto priv = std::make_unique<MLDSA65_private_key>(); |
| 218 | uint8_t seed[MLDSA_SEED_BYTES]; |
| 219 | EXPECT_TRUE( |
| 220 | MLDSA65_generate_key(encoded_public_key.data(), seed, priv.get())); |
| 221 |
| 222 | auto pub = std::make_unique<MLDSA65_public_key>(); |
| 223 | CBS cbs = CBS(encoded_public_key); |
| 224 | ASSERT_TRUE(MLDSA65_parse_public_key(pub.get(), &cbs)); |
| 225 |
| 226 | std::vector<uint8_t> encoded_signature1(MLDSA65_SIGNATURE_BYTES); |
| 227 | std::vector<uint8_t> encoded_signature2(MLDSA65_SIGNATURE_BYTES); |
| 228 | static const uint8_t kMessage[] = {'H', 'e', 'l', 'l', 'o', ' ', |
| 229 | 'w', 'o', 'r', 'l', 'd'}; |
| 230 | EXPECT_TRUE(MLDSA65_sign(encoded_signature1.data(), priv.get(), kMessage, |
| 231 | sizeof(kMessage), nullptr, 0)); |
| 232 | EXPECT_TRUE(MLDSA65_sign(encoded_signature2.data(), priv.get(), kMessage, |
| 233 | sizeof(kMessage), nullptr, 0)); |
| 234 |
| 235 | EXPECT_NE(Bytes(encoded_signature1), Bytes(encoded_signature2)); |
| 236 |
| 237 | // Even though the signatures are different, they both verify. |
| 238 | EXPECT_EQ(MLDSA65_verify(pub.get(), encoded_signature1.data(), |
| 239 | encoded_signature1.size(), kMessage, |
| 240 | sizeof(kMessage), nullptr, 0), |
| 241 | 1); |
| 242 | EXPECT_EQ(MLDSA65_verify(pub.get(), encoded_signature2.data(), |
| 243 | encoded_signature2.size(), kMessage, |
| 244 | sizeof(kMessage), nullptr, 0), |
| 245 | 1); |
| 246 | } |
| 247 |
| 248 | TEST(MLDSATest, PublicFromPrivateIsConsistent) { |
| 249 | std::vector<uint8_t> encoded_public_key(MLDSA65_PUBLIC_KEY_BYTES); |
| 250 | auto priv = std::make_unique<MLDSA65_private_key>(); |
| 251 | uint8_t seed[MLDSA_SEED_BYTES]; |
| 252 | EXPECT_TRUE( |
| 253 | MLDSA65_generate_key(encoded_public_key.data(), seed, priv.get())); |
| 254 |
| 255 | auto pub = std::make_unique<MLDSA65_public_key>(); |
| 256 | EXPECT_TRUE(MLDSA65_public_from_private(pub.get(), priv.get())); |
| 257 |
| 258 | std::vector<uint8_t> encoded_public_key2(MLDSA65_PUBLIC_KEY_BYTES); |
| 259 |
| 260 | CBB cbb; |
| 261 | CBB_init_fixed(&cbb, encoded_public_key2.data(), encoded_public_key2.size()); |
| 262 | ASSERT_TRUE(MLDSA65_marshal_public_key(&cbb, pub.get())); |
| 263 |
| 264 | EXPECT_EQ(Bytes(encoded_public_key2), Bytes(encoded_public_key)); |
| 265 | } |
| 266 |
| 267 | TEST(MLDSATest, InvalidPublicKeyEncodingLength) { |
| 268 | // Encode a public key with a trailing 0 at the end. |
| 269 | std::vector<uint8_t> encoded_public_key(MLDSA65_PUBLIC_KEY_BYTES + 1); |
| 270 | auto priv = std::make_unique<MLDSA65_private_key>(); |
| 271 | uint8_t seed[MLDSA_SEED_BYTES]; |
| 272 | EXPECT_TRUE( |
| 273 | MLDSA65_generate_key(encoded_public_key.data(), seed, priv.get())); |
| 274 |
| 275 | // Public key is 1 byte too short. |
| 276 | CBS cbs = |
| 277 | CBS(bssl::Span(encoded_public_key).first(MLDSA65_PUBLIC_KEY_BYTES - 1)); |
| 278 | auto parsed_pub = std::make_unique<MLDSA65_public_key>(); |
| 279 | EXPECT_FALSE(MLDSA65_parse_public_key(parsed_pub.get(), &cbs)); |
| 280 |
| 281 | // Public key has the correct length. |
| 282 | cbs = CBS(bssl::Span(encoded_public_key).first(MLDSA65_PUBLIC_KEY_BYTES)); |
| 283 | EXPECT_TRUE(MLDSA65_parse_public_key(parsed_pub.get(), &cbs)); |
| 284 |
| 285 | // Public key is 1 byte too long. |
| 286 | cbs = CBS(encoded_public_key); |
| 287 | EXPECT_FALSE(MLDSA65_parse_public_key(parsed_pub.get(), &cbs)); |
| 288 | } |
| 289 |
| 290 | TEST(MLDSATest, InvalidPrivateKeyEncodingLength) { |
| 291 | std::vector<uint8_t> encoded_public_key(MLDSA65_PUBLIC_KEY_BYTES); |
| 292 | auto priv = std::make_unique<BCM_mldsa65_private_key>(); |
| 293 | uint8_t seed[MLDSA_SEED_BYTES]; |
| 294 | EXPECT_TRUE(bcm_success( |
| 295 | BCM_mldsa65_generate_key(encoded_public_key.data(), seed, priv.get()))); |
| 296 |
| 297 | CBB cbb; |
| 298 | std::vector<uint8_t> malformed_private_key(MLDSA65_PRIVATE_KEY_BYTES + 1, 0); |
| 299 | CBB_init_fixed(&cbb, malformed_private_key.data(), MLDSA65_PRIVATE_KEY_BYTES); |
| 300 | ASSERT_TRUE(bcm_success(BCM_mldsa65_marshal_private_key( |
| 301 | &cbb, reinterpret_cast<BCM_mldsa65_private_key *>(priv.get())))); |
| 302 |
| 303 | CBS cbs; |
| 304 | auto parsed_priv = std::make_unique<BCM_mldsa65_private_key>(); |
| 305 |
| 306 | // Private key is 1 byte too short. |
| 307 | CBS_init(&cbs, malformed_private_key.data(), MLDSA65_PRIVATE_KEY_BYTES - 1); |
| 308 | EXPECT_FALSE( |
| 309 | bcm_success(BCM_mldsa65_parse_private_key(parsed_priv.get(), &cbs))); |
| 310 |
| 311 | // Private key has the correct length. |
| 312 | CBS_init(&cbs, malformed_private_key.data(), MLDSA65_PRIVATE_KEY_BYTES); |
| 313 | EXPECT_TRUE( |
| 314 | bcm_success(BCM_mldsa65_parse_private_key(parsed_priv.get(), &cbs))); |
| 315 |
| 316 | // Private key is 1 byte too long. |
| 317 | CBS_init(&cbs, malformed_private_key.data(), MLDSA65_PRIVATE_KEY_BYTES + 1); |
| 318 | EXPECT_FALSE( |
| 319 | bcm_success(BCM_mldsa65_parse_private_key(parsed_priv.get(), &cbs))); |
| 320 | } |
| 321 |
| 322 | template <typename PrivateKey, typename PublicKey, size_t SignatureBytes, |
| 323 | bcm_status (*ParsePrivateKey)(PrivateKey *, CBS *), |
| 324 | bcm_status (*SignInternal)(uint8_t *, const PrivateKey *, |
| 325 | const uint8_t *, size_t, const uint8_t *, |
| 326 | size_t, const uint8_t *, size_t, |
| 327 | const uint8_t *), |
| 328 | bcm_status (*PublicFromPrivate)(PublicKey *, const PrivateKey *), |
| 329 | bcm_status (*VerifyInternal)(const PublicKey *, const uint8_t *, |
| 330 | const uint8_t *, size_t, const uint8_t *, |
| 331 | size_t, const uint8_t *, size_t)> |
| 332 | static void MLDSASigGenTest(FileTest *t) { |
| 333 | std::vector<uint8_t> private_key_bytes, msg, expected_signature; |
| 334 | ASSERT_TRUE(t->GetBytes(&private_key_bytes, "sk")); |
| 335 | ASSERT_TRUE(t->GetBytes(&msg, "message")); |
| 336 | ASSERT_TRUE(t->GetBytes(&expected_signature, "signature")); |
| 337 |
| 338 | auto priv = std::make_unique<PrivateKey>(); |
| 339 | CBS cbs; |
| 340 | CBS_init(&cbs, private_key_bytes.data(), private_key_bytes.size()); |
| 341 | EXPECT_TRUE(bcm_success(ParsePrivateKey(priv.get(), &cbs))); |
| 342 |
| 343 | const uint8_t zero_randomizer[BCM_MLDSA_SIGNATURE_RANDOMIZER_BYTES] = {0}; |
| 344 | std::vector<uint8_t> signature(SignatureBytes); |
| 345 | EXPECT_TRUE(bcm_success(SignInternal(signature.data(), priv.get(), msg.data(), |
| 346 | msg.size(), nullptr, 0, nullptr, 0, |
| 347 | zero_randomizer))); |
| 348 |
| 349 | EXPECT_EQ(Bytes(signature), Bytes(expected_signature)); |
| 350 |
| 351 | auto pub = std::make_unique<PublicKey>(); |
| 352 | ASSERT_TRUE(bcm_success(PublicFromPrivate(pub.get(), priv.get()))); |
| 353 | EXPECT_TRUE( |
| 354 | bcm_success(VerifyInternal(pub.get(), signature.data(), msg.data(), |
| 355 | msg.size(), nullptr, 0, nullptr, 0))); |
| 356 | } |
| 357 |
| 358 | TEST(MLDSATest, SigGenTests65) { |
| 359 | FileTestGTest( |
| 360 | "crypto/mldsa/mldsa_nist_siggen_65_tests.txt", |
| 361 | MLDSASigGenTest<BCM_mldsa65_private_key, BCM_mldsa65_public_key, |
| 362 | MLDSA65_SIGNATURE_BYTES, BCM_mldsa65_parse_private_key, |
| 363 | BCM_mldsa65_sign_internal, |
| 364 | BCM_mldsa65_public_from_private, |
| 365 | BCM_mldsa65_verify_internal>); |
| 366 | } |
| 367 |
| 368 | TEST(MLDSATest, SigGenTests87) { |
| 369 | FileTestGTest( |
| 370 | "crypto/mldsa/mldsa_nist_siggen_87_tests.txt", |
| 371 | MLDSASigGenTest<BCM_mldsa87_private_key, BCM_mldsa87_public_key, |
| 372 | BCM_MLDSA87_SIGNATURE_BYTES, |
| 373 | BCM_mldsa87_parse_private_key, BCM_mldsa87_sign_internal, |
| 374 | BCM_mldsa87_public_from_private, |
| 375 | BCM_mldsa87_verify_internal>); |
| 376 | } |
| 377 |
| 378 | template <typename PrivateKey, size_t PublicKeyBytes, |
| 379 | bcm_status (*Generate)(uint8_t *, PrivateKey *, const uint8_t *), |
| 380 | bcm_status (*MarshalPrivate)(CBB *, const PrivateKey *)> |
| 381 | static void MLDSAKeyGenTest(FileTest *t) { |
| 382 | std::vector<uint8_t> seed, expected_public_key, expected_private_key; |
| 383 | ASSERT_TRUE(t->GetBytes(&seed, "seed")); |
| 384 | CONSTTIME_SECRET(seed.data(), seed.size()); |
| 385 | ASSERT_TRUE(t->GetBytes(&expected_public_key, "pub")); |
| 386 | ASSERT_TRUE(t->GetBytes(&expected_private_key, "priv")); |
| 387 |
| 388 | std::vector<uint8_t> encoded_public_key(PublicKeyBytes); |
| 389 | auto priv = std::make_unique<PrivateKey>(); |
| 390 | ASSERT_TRUE(bcm_success( |
| 391 | Generate(encoded_public_key.data(), priv.get(), seed.data()))); |
| 392 |
| 393 | const std::vector<uint8_t> encoded_private_key = |
| 394 | Marshal(MarshalPrivate, priv.get()); |
| 395 |
| 396 | EXPECT_EQ(Bytes(encoded_public_key), Bytes(expected_public_key)); |
| 397 | EXPECT_EQ(Bytes(Declassified(encoded_private_key)), |
| 398 | Bytes(expected_private_key)); |
| 399 | } |
| 400 |
| 401 | TEST(MLDSATest, KeyGenTests65) { |
| 402 | FileTestGTest( |
| 403 | "crypto/mldsa/mldsa_nist_keygen_65_tests.txt", |
| 404 | MLDSAKeyGenTest<BCM_mldsa65_private_key, MLDSA65_PUBLIC_KEY_BYTES, |
| 405 | BCM_mldsa65_generate_key_external_entropy, |
| 406 | BCM_mldsa65_marshal_private_key>); |
| 407 | } |
| 408 |
| 409 | TEST(MLDSATest, KeyGenTests87) { |
| 410 | FileTestGTest( |
| 411 | "crypto/mldsa/mldsa_nist_keygen_87_tests.txt", |
| 412 | MLDSAKeyGenTest<BCM_mldsa87_private_key, BCM_MLDSA87_PUBLIC_KEY_BYTES, |
| 413 | BCM_mldsa87_generate_key_external_entropy, |
| 414 | BCM_mldsa87_marshal_private_key>); |
| 415 | } |
| 416 |
| 417 | template < |
| 418 | typename PrivateKey, bcm_status_t (*ParsePrivateKey)(PrivateKey *, CBS *), |
| 419 | size_t SignatureBytes, |
| 420 | bcm_status_t (*SignInternal)(uint8_t *, const PrivateKey *, const uint8_t *, |
| 421 | size_t, const uint8_t *, size_t, |
| 422 | const uint8_t *, size_t, const uint8_t *)> |
| 423 | static void MLDSAWycheproofSignTest(FileTest *t) { |
| 424 | std::vector<uint8_t> private_key_bytes, msg, expected_signature, context; |
| 425 | ASSERT_TRUE(t->GetInstructionBytes(&private_key_bytes, "privateKey")); |
| 426 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 427 | ASSERT_TRUE(t->GetBytes(&expected_signature, "sig")); |
| 428 | if (t->HasAttribute("ctx")) { |
| 429 | t->GetBytes(&context, "ctx"); |
| 430 | } |
| 431 | std::string result; |
| 432 | ASSERT_TRUE(t->GetAttribute(&result, "result")); |
| 433 | t->IgnoreAttribute("flags"); |
| 434 |
| 435 | CBS cbs; |
| 436 | CBS_init(&cbs, private_key_bytes.data(), private_key_bytes.size()); |
| 437 | auto priv = std::make_unique<PrivateKey>(); |
| 438 | const int priv_ok = bcm_success(ParsePrivateKey(priv.get(), &cbs)); |
| 439 |
| 440 | if (!priv_ok) { |
| 441 | ASSERT_TRUE(result != "valid"); |
| 442 | return; |
| 443 | } |
| 444 |
| 445 | // Unfortunately we need to reimplement the context length check here because |
| 446 | // we are using the internal function in order to pass in an all-zero |
| 447 | // randomizer. |
| 448 | if (context.size() > 255) { |
| 449 | ASSERT_TRUE(result != "valid"); |
| 450 | return; |
| 451 | } |
| 452 |
| 453 | const uint8_t zero_randomizer[BCM_MLDSA_SIGNATURE_RANDOMIZER_BYTES] = {0}; |
| 454 | std::vector<uint8_t> signature(SignatureBytes); |
| 455 | const uint8_t context_prefix[2] = {0, static_cast<uint8_t>(context.size())}; |
| 456 | EXPECT_TRUE(bcm_success(SignInternal(signature.data(), priv.get(), msg.data(), |
| 457 | msg.size(), context_prefix, |
| 458 | sizeof(context_prefix), context.data(), |
| 459 | context.size(), zero_randomizer))); |
| 460 |
| 461 | EXPECT_EQ(Bytes(signature), Bytes(expected_signature)); |
| 462 | } |
| 463 |
| 464 | TEST(MLDSATest, WycheproofSignTests65) { |
| 465 | FileTestGTest( |
| 466 | "third_party/wycheproof_testvectors/mldsa_65_standard_sign_test.txt", |
| 467 | MLDSAWycheproofSignTest< |
| 468 | BCM_mldsa65_private_key, BCM_mldsa65_parse_private_key, |
| 469 | MLDSA65_SIGNATURE_BYTES, BCM_mldsa65_sign_internal>); |
| 470 | } |
| 471 |
| 472 | TEST(MLDSATest, WycheproofSignTests87) { |
| 473 | FileTestGTest( |
| 474 | "third_party/wycheproof_testvectors/mldsa_87_standard_sign_test.txt", |
| 475 | MLDSAWycheproofSignTest< |
| 476 | BCM_mldsa87_private_key, BCM_mldsa87_parse_private_key, |
| 477 | BCM_MLDSA87_SIGNATURE_BYTES, BCM_mldsa87_sign_internal>); |
| 478 | } |
| 479 |
| 480 | template <typename PublicKey, size_t SignatureLength, |
| 481 | bcm_status_t (*ParsePublicKey)(PublicKey *, CBS *), |
| 482 | bcm_status_t (*Verify)(const PublicKey *, const uint8_t *, |
| 483 | const uint8_t *, size_t, const uint8_t *, |
| 484 | size_t)> |
| 485 | static void MLDSAWycheproofVerifyTest(FileTest *t) { |
| 486 | std::vector<uint8_t> public_key_bytes, msg, signature, context; |
| 487 | ASSERT_TRUE(t->GetInstructionBytes(&public_key_bytes, "publicKey")); |
| 488 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 489 | ASSERT_TRUE(t->GetBytes(&signature, "sig")); |
| 490 | if (t->HasAttribute("ctx")) { |
| 491 | t->GetBytes(&context, "ctx"); |
| 492 | } |
| 493 | std::string result, flags; |
| 494 | ASSERT_TRUE(t->GetAttribute(&result, "result")); |
| 495 | ASSERT_TRUE(t->GetAttribute(&flags, "flags")); |
| 496 |
| 497 | CBS cbs; |
| 498 | CBS_init(&cbs, public_key_bytes.data(), public_key_bytes.size()); |
| 499 | auto pub = std::make_unique<PublicKey>(); |
| 500 | const int pub_ok = bcm_success(ParsePublicKey(pub.get(), &cbs)); |
| 501 |
| 502 | if (!pub_ok) { |
| 503 | EXPECT_EQ(flags, "IncorrectPublicKeyLength"); |
| 504 | return; |
| 505 | } |
| 506 |
| 507 | const int sig_ok = |
| 508 | signature.size() == SignatureLength && context.size() <= 255 && |
| 509 | bcm_success(Verify(pub.get(), signature.data(), msg.data(), msg.size(), |
| 510 | context.data(), context.size())); |
| 511 | if (!sig_ok) { |
| 512 | EXPECT_EQ(result, "invalid"); |
| 513 | } else { |
| 514 | EXPECT_EQ(result, "valid"); |
| 515 | } |
| 516 | } |
| 517 |
| 518 | TEST(MLDSATest, WycheproofVerifyTests65) { |
| 519 | FileTestGTest( |
| 520 | "third_party/wycheproof_testvectors/mldsa_65_standard_verify_test.txt", |
| 521 | MLDSAWycheproofVerifyTest< |
| 522 | BCM_mldsa65_public_key, BCM_MLDSA65_SIGNATURE_BYTES, |
| 523 | BCM_mldsa65_parse_public_key, BCM_mldsa65_verify>); |
| 524 | } |
| 525 |
| 526 | TEST(MLDSATest, WycheproofVerifyTests87) { |
| 527 | FileTestGTest( |
| 528 | "third_party/wycheproof_testvectors/mldsa_87_standard_verify_test.txt", |
| 529 | MLDSAWycheproofVerifyTest< |
| 530 | BCM_mldsa87_public_key, BCM_MLDSA87_SIGNATURE_BYTES, |
| 531 | BCM_mldsa87_parse_public_key, BCM_mldsa87_verify>); |
| 532 | } |
| 533 |
| 534 | TEST(MLDSATest, Self) { ASSERT_TRUE(boringssl_self_test_mldsa()); } |
| 535 |
| 536 | TEST(MLDSATest, PWCT) { |
| 537 | uint8_t seed[BCM_MLDSA_SEED_BYTES]; |
| 538 |
| 539 | auto pub65 = std::make_unique<uint8_t[]>(BCM_MLDSA65_PUBLIC_KEY_BYTES); |
| 540 | auto priv65 = std::make_unique<BCM_mldsa65_private_key>(); |
| 541 | ASSERT_EQ(BCM_mldsa65_generate_key_fips(pub65.get(), seed, priv65.get()), |
| 542 | bcm_status::approved); |
| 543 |
| 544 | auto pub87 = std::make_unique<uint8_t[]>(BCM_MLDSA87_PUBLIC_KEY_BYTES); |
| 545 | auto priv87 = std::make_unique<BCM_mldsa87_private_key>(); |
| 546 | ASSERT_EQ(BCM_mldsa87_generate_key_fips(pub87.get(), seed, priv87.get()), |
| 547 | bcm_status::approved); |
| 548 | } |
| 549 |
| 550 | TEST(MLDSATest, NullptrArgumentsToCreate) { |
| 551 | // For FIPS reasons, this should fail rather than crash. |
| 552 | ASSERT_EQ(BCM_mldsa65_generate_key_fips(nullptr, nullptr, nullptr), |
| 553 | bcm_status::failure); |
| 554 | ASSERT_EQ(BCM_mldsa87_generate_key_fips(nullptr, nullptr, nullptr), |
| 555 | bcm_status::failure); |
| 556 | ASSERT_EQ( |
| 557 | BCM_mldsa65_generate_key_external_entropy_fips(nullptr, nullptr, nullptr), |
| 558 | bcm_status::failure); |
| 559 | ASSERT_EQ( |
| 560 | BCM_mldsa87_generate_key_external_entropy_fips(nullptr, nullptr, nullptr), |
| 561 | bcm_status::failure); |
| 562 | } |
| 563 |
| 564 | } // namespace |
| 565 |