diff options
author | beck <> | 2025-08-14 15:48:48 +0000 |
---|---|---|
committer | beck <> | 2025-08-14 15:48:48 +0000 |
commit | 6452fa9fc6f33dac80ee572764b9fe29a469f8ce (patch) | |
tree | 0956ae670e4f193442bcf99d2b1fb70a43a6b5b5 /src/regress/lib/libcrypto/mlkem/mlkem_tests.c | |
parent | 9bef27f78e41e8026f1d588e4e36e385061f3deb (diff) | |
download | openbsd-6452fa9fc6f33dac80ee572764b9fe29a469f8ce.tar.gz openbsd-6452fa9fc6f33dac80ee572764b9fe29a469f8ce.tar.bz2 openbsd-6452fa9fc6f33dac80ee572764b9fe29a469f8ce.zip |
Add a reasonable ML-KEM API for public use.
Adapt the tests to use this API.
This does not yet make the symbols public in Symbols.list
which will happen shortly with a bump.
This includes some partial rototilling of the non-public
interfaces which will be shortly continued when the internal
code is deduplicated to not have multiple copies for ML-KEM
768 and ML-KEM 1024 (which is just an artifact of unravelling
the boring C++ code).
ok jsing@, tb@
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem_tests.c')
-rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_tests.c | 292 |
1 files changed, 142 insertions, 150 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests.c b/src/regress/lib/libcrypto/mlkem/mlkem_tests.c index 8e04dc6ad2..3269ba951f 100644 --- a/src/regress/lib/libcrypto/mlkem/mlkem_tests.c +++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: mlkem_tests.c,v 1.6 2025/05/20 00:33:41 beck Exp $ */ | 1 | /* $OpenBSD: mlkem_tests.c,v 1.7 2025/08/14 15:48:48 beck Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2024 Google Inc. | 3 | * Copyright (c) 2024 Google Inc. |
4 | * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> |
@@ -38,12 +38,8 @@ enum test_type { | |||
38 | 38 | ||
39 | struct decap_ctx { | 39 | struct decap_ctx { |
40 | struct parse *parse_ctx; | 40 | struct parse *parse_ctx; |
41 | 41 | int rank; | |
42 | void *private_key; | 42 | MLKEM_private_key *private_key; |
43 | size_t private_key_len; | ||
44 | |||
45 | mlkem_parse_private_key_fn parse_private_key; | ||
46 | mlkem_decap_fn decap; | ||
47 | }; | 43 | }; |
48 | 44 | ||
49 | enum decap_states { | 45 | enum decap_states { |
@@ -89,21 +85,26 @@ decap_init(void *ctx, void *parse_ctx) | |||
89 | 85 | ||
90 | decap->parse_ctx = parse_ctx; | 86 | decap->parse_ctx = parse_ctx; |
91 | 87 | ||
92 | return 1; | 88 | return (decap->private_key = MLKEM_private_key_new(decap->rank)) |
89 | != NULL; | ||
93 | } | 90 | } |
94 | 91 | ||
95 | static void | 92 | static void |
96 | decap_finish(void *ctx) | 93 | decap_finish(void *ctx) |
97 | { | 94 | { |
98 | (void)ctx; | 95 | struct decap_ctx *decap = ctx; |
96 | |||
97 | MLKEM_private_key_free(decap->private_key); | ||
98 | decap->private_key = NULL; | ||
99 | } | 99 | } |
100 | 100 | ||
101 | static int | 101 | static int |
102 | MlkemDecapFileTest(struct decap_ctx *decap) | 102 | MlkemDecapFileTest(struct decap_ctx *decap) |
103 | { | 103 | { |
104 | struct parse *p = decap->parse_ctx; | 104 | struct parse *p = decap->parse_ctx; |
105 | uint8_t shared_secret_buf[MLKEM_SHARED_SECRET_BYTES]; | 105 | uint8_t *shared_secret_buf = NULL; |
106 | CBS ciphertext, shared_secret, private_key; | 106 | CBS ciphertext, shared_secret, private_key; |
107 | size_t s_len = 0; | ||
107 | int should_fail; | 108 | int should_fail; |
108 | int failed = 1; | 109 | int failed = 1; |
109 | 110 | ||
@@ -112,21 +113,28 @@ MlkemDecapFileTest(struct decap_ctx *decap) | |||
112 | parse_get_cbs(p, DECAP_PRIVATE_KEY, &private_key); | 113 | parse_get_cbs(p, DECAP_PRIVATE_KEY, &private_key); |
113 | parse_get_int(p, DECAP_RESULT, &should_fail); | 114 | parse_get_int(p, DECAP_RESULT, &should_fail); |
114 | 115 | ||
115 | if (!decap->parse_private_key(decap->private_key, | 116 | if (!MLKEM_parse_private_key(decap->private_key, |
116 | CBS_data(&private_key), CBS_len(&private_key))) { | 117 | CBS_data(&private_key), CBS_len(&private_key))) { |
117 | if ((failed = !should_fail)) | 118 | if ((failed = !should_fail)) |
118 | parse_info(p, "parse private key"); | 119 | parse_info(p, "parse private key"); |
119 | goto err; | 120 | goto err; |
120 | } | 121 | } |
121 | if (!decap->decap(shared_secret_buf, | 122 | if (!MLKEM_decap(decap->private_key, CBS_data(&ciphertext), |
122 | CBS_data(&ciphertext), CBS_len(&ciphertext), decap->private_key)) { | 123 | CBS_len(&ciphertext), &shared_secret_buf, &s_len)) { |
123 | if ((failed = !should_fail)) | 124 | if ((failed = !should_fail)) |
124 | parse_info(p, "decap"); | 125 | parse_info(p, "decap"); |
125 | goto err; | 126 | goto err; |
126 | } | 127 | } |
127 | 128 | ||
129 | if (s_len != MLKEM_SHARED_SECRET_LENGTH) { | ||
130 | if ((failed = !should_fail)) | ||
131 | parse_info(p, "shared secret length %zu != %d", s_len, | ||
132 | MLKEM_SHARED_SECRET_LENGTH); | ||
133 | goto err; | ||
134 | } | ||
135 | |||
128 | failed = !parse_data_equal(p, "shared_secret", &shared_secret, | 136 | failed = !parse_data_equal(p, "shared_secret", &shared_secret, |
129 | shared_secret_buf, sizeof(shared_secret_buf)); | 137 | shared_secret_buf, s_len); |
130 | 138 | ||
131 | if (should_fail != failed) { | 139 | if (should_fail != failed) { |
132 | parse_info(p, "FAIL: should_fail %d, failed %d", | 140 | parse_info(p, "FAIL: should_fail %d, failed %d", |
@@ -135,6 +143,7 @@ MlkemDecapFileTest(struct decap_ctx *decap) | |||
135 | } | 143 | } |
136 | 144 | ||
137 | err: | 145 | err: |
146 | freezero(shared_secret_buf, s_len); | ||
138 | return failed; | 147 | return failed; |
139 | } | 148 | } |
140 | 149 | ||
@@ -193,8 +202,9 @@ static int | |||
193 | MlkemNistDecapFileTest(struct decap_ctx *decap) | 202 | MlkemNistDecapFileTest(struct decap_ctx *decap) |
194 | { | 203 | { |
195 | struct parse *p = decap->parse_ctx; | 204 | struct parse *p = decap->parse_ctx; |
196 | uint8_t shared_secret[MLKEM_SHARED_SECRET_BYTES]; | 205 | uint8_t *shared_secret = NULL; |
197 | CBS dk, c, k; | 206 | CBS dk, c, k; |
207 | size_t s_len = 0; | ||
198 | int failed = 1; | 208 | int failed = 1; |
199 | 209 | ||
200 | parse_instruction_get_cbs(p, NIST_DECAP_DK, &dk); | 210 | parse_instruction_get_cbs(p, NIST_DECAP_DK, &dk); |
@@ -202,27 +212,34 @@ MlkemNistDecapFileTest(struct decap_ctx *decap) | |||
202 | parse_get_cbs(p, NIST_DECAP_K, &k); | 212 | parse_get_cbs(p, NIST_DECAP_K, &k); |
203 | 213 | ||
204 | if (!parse_length_equal(p, "private key", | 214 | if (!parse_length_equal(p, "private key", |
205 | decap->private_key_len, CBS_len(&dk))) | 215 | MLKEM_private_key_encoded_length(decap->private_key), CBS_len(&dk))) |
206 | goto err; | 216 | goto err; |
207 | if (!parse_length_equal(p, "shared secret", | 217 | if (!parse_length_equal(p, "shared secret", |
208 | MLKEM_SHARED_SECRET_BYTES, CBS_len(&k))) | 218 | MLKEM_SHARED_SECRET_BYTES, CBS_len(&k))) |
209 | goto err; | 219 | goto err; |
210 | 220 | ||
211 | if (!decap->parse_private_key(decap->private_key, CBS_data(&dk), | 221 | if (!MLKEM_parse_private_key(decap->private_key, CBS_data(&dk), |
212 | CBS_len(&dk))) { | 222 | CBS_len(&dk))) { |
213 | parse_info(p, "parse private key"); | 223 | parse_info(p, "parse private key"); |
214 | goto err; | 224 | goto err; |
215 | } | 225 | } |
216 | if (!decap->decap(shared_secret, CBS_data(&c), CBS_len(&c), | 226 | if (!MLKEM_decap(decap->private_key, CBS_data(&c), CBS_len(&c), |
217 | decap->private_key)) { | 227 | &shared_secret, &s_len)) { |
218 | parse_info(p, "decap"); | 228 | parse_info(p, "decap"); |
219 | goto err; | 229 | goto err; |
220 | } | 230 | } |
221 | 231 | ||
232 | if (s_len != MLKEM_SHARED_SECRET_LENGTH) { | ||
233 | parse_info(p, "shared secret length %zu != %d", s_len, | ||
234 | MLKEM_SHARED_SECRET_LENGTH); | ||
235 | goto err; | ||
236 | } | ||
237 | |||
222 | failed = !parse_data_equal(p, "shared secret", &k, | 238 | failed = !parse_data_equal(p, "shared secret", &k, |
223 | shared_secret, MLKEM_SHARED_SECRET_BYTES); | 239 | shared_secret, s_len); |
224 | 240 | ||
225 | err: | 241 | err: |
242 | free(shared_secret); | ||
226 | return failed; | 243 | return failed; |
227 | } | 244 | } |
228 | 245 | ||
@@ -246,46 +263,31 @@ static const struct test_parse nist_decap_parse = { | |||
246 | }; | 263 | }; |
247 | 264 | ||
248 | static int | 265 | static int |
249 | mlkem_decap_tests(const char *fn, size_t size, enum test_type test_type) | 266 | mlkem_decap_tests(const char *fn, int rank, enum test_type test_type) |
250 | { | 267 | { |
251 | struct MLKEM768_private_key private_key768; | 268 | struct decap_ctx decap = { |
252 | struct decap_ctx decap768 = { | 269 | .rank = rank, |
253 | .private_key = &private_key768, | ||
254 | .private_key_len = MLKEM768_PRIVATE_KEY_BYTES, | ||
255 | |||
256 | .parse_private_key = mlkem768_parse_private_key, | ||
257 | .decap = mlkem768_decap, | ||
258 | }; | ||
259 | struct MLKEM1024_private_key private_key1024; | ||
260 | struct decap_ctx decap1024 = { | ||
261 | .private_key = &private_key1024, | ||
262 | .private_key_len = MLKEM1024_PRIVATE_KEY_BYTES, | ||
263 | |||
264 | .parse_private_key = mlkem1024_parse_private_key, | ||
265 | .decap = mlkem1024_decap, | ||
266 | }; | 270 | }; |
271 | int ret = 0; | ||
267 | 272 | ||
268 | if (size == 768 && test_type == TEST_TYPE_NORMAL) | 273 | if (test_type == TEST_TYPE_NORMAL) |
269 | return parse_test_file(fn, &decap_parse, &decap768); | 274 | ret = parse_test_file(fn, &decap_parse, &decap); |
270 | if (size == 768 && test_type == TEST_TYPE_NIST) | 275 | else if (test_type == TEST_TYPE_NIST) |
271 | return parse_test_file(fn, &nist_decap_parse, &decap768); | 276 | ret = parse_test_file(fn, &nist_decap_parse, &decap); |
272 | if (size == 1024 && test_type == TEST_TYPE_NORMAL) | 277 | else |
273 | return parse_test_file(fn, &decap_parse, &decap1024); | 278 | errx(1, "unknown decap test: rank %d, type %d", rank, |
274 | if (size == 1024 && test_type == TEST_TYPE_NIST) | 279 | test_type); |
275 | return parse_test_file(fn, &nist_decap_parse, &decap1024); | ||
276 | 280 | ||
277 | errx(1, "unknown decap test: size %zu, type %d", size, test_type); | 281 | return ret; |
278 | } | 282 | } |
279 | 283 | ||
280 | struct encap_ctx { | 284 | struct encap_ctx { |
281 | struct parse *parse_ctx; | 285 | struct parse *parse_ctx; |
282 | 286 | ||
283 | void *public_key; | 287 | int rank; |
288 | MLKEM_public_key *public_key; | ||
284 | uint8_t *ciphertext; | 289 | uint8_t *ciphertext; |
285 | size_t ciphertext_len; | 290 | size_t ciphertext_len; |
286 | |||
287 | mlkem_parse_public_key_fn parse_public_key; | ||
288 | mlkem_encap_external_entropy_fn encap_external_entropy; | ||
289 | }; | 291 | }; |
290 | 292 | ||
291 | enum encap_states { | 293 | enum encap_states { |
@@ -338,21 +340,30 @@ encap_init(void *ctx, void *parse_ctx) | |||
338 | 340 | ||
339 | encap->parse_ctx = parse_ctx; | 341 | encap->parse_ctx = parse_ctx; |
340 | 342 | ||
341 | return 1; | 343 | encap->ciphertext = NULL; |
344 | return (encap->public_key = MLKEM_public_key_new(encap->rank)) | ||
345 | != NULL; | ||
342 | } | 346 | } |
343 | 347 | ||
344 | static void | 348 | static void |
345 | encap_finish(void *ctx) | 349 | encap_finish(void *ctx) |
346 | { | 350 | { |
347 | (void)ctx; | 351 | struct encap_ctx *encap = ctx; |
352 | |||
353 | freezero(encap->ciphertext, encap->ciphertext_len); | ||
354 | encap->ciphertext = NULL; | ||
355 | MLKEM_public_key_free(encap->public_key); | ||
356 | encap->public_key = NULL; | ||
348 | } | 357 | } |
349 | 358 | ||
350 | static int | 359 | static int |
351 | MlkemEncapFileTest(struct encap_ctx *encap) | 360 | MlkemEncapFileTest(struct encap_ctx *encap) |
352 | { | 361 | { |
353 | struct parse *p = encap->parse_ctx; | ||
354 | uint8_t shared_secret_buf[MLKEM_SHARED_SECRET_BYTES]; | ||
355 | CBS entropy, public_key, ciphertext, shared_secret; | 362 | CBS entropy, public_key, ciphertext, shared_secret; |
363 | struct parse *p = encap->parse_ctx; | ||
364 | uint8_t *shared_secret_buf = NULL; | ||
365 | size_t s_len = 0; | ||
366 | |||
356 | int should_fail; | 367 | int should_fail; |
357 | int failed = 1; | 368 | int failed = 1; |
358 | 369 | ||
@@ -362,20 +373,33 @@ MlkemEncapFileTest(struct encap_ctx *encap) | |||
362 | parse_get_cbs(p, ENCAP_SHARED_SECRET, &shared_secret); | 373 | parse_get_cbs(p, ENCAP_SHARED_SECRET, &shared_secret); |
363 | parse_get_int(p, ENCAP_RESULT, &should_fail); | 374 | parse_get_int(p, ENCAP_RESULT, &should_fail); |
364 | 375 | ||
365 | if (!encap->parse_public_key(encap->public_key, CBS_data(&public_key), | 376 | if (!MLKEM_parse_public_key(encap->public_key, CBS_data(&public_key), |
366 | CBS_len(&public_key))) { | 377 | CBS_len(&public_key))) { |
367 | if ((failed = !should_fail)) | 378 | if ((failed = !should_fail)) |
368 | parse_info(p, "parse public key"); | 379 | parse_info(p, "parse public key"); |
369 | goto err; | 380 | goto err; |
370 | } | 381 | } |
371 | encap->encap_external_entropy(encap->ciphertext, shared_secret_buf, | 382 | if (!MLKEM_encap_external_entropy(encap->public_key, CBS_data(&entropy), |
372 | encap->public_key, CBS_data(&entropy)); | 383 | &encap->ciphertext, &encap->ciphertext_len, &shared_secret_buf, |
384 | &s_len)) { | ||
385 | if ((failed = !should_fail)) | ||
386 | parse_info(p, "encap_external_entropy"); | ||
387 | goto err; | ||
388 | } | ||
389 | |||
390 | if (s_len != MLKEM_SHARED_SECRET_LENGTH) { | ||
391 | if ((failed = !should_fail)) | ||
392 | parse_info(p, "shared secret length %zu != %d", s_len, | ||
393 | MLKEM_SHARED_SECRET_LENGTH); | ||
394 | goto err; | ||
395 | } | ||
373 | 396 | ||
374 | failed = !parse_data_equal(p, "shared_secret", &shared_secret, | 397 | failed = !parse_data_equal(p, "shared_secret", &shared_secret, |
375 | shared_secret_buf, sizeof(shared_secret_buf)); | 398 | shared_secret_buf, s_len); |
376 | failed |= !parse_data_equal(p, "ciphertext", &ciphertext, | 399 | failed |= !parse_data_equal(p, "ciphertext", &ciphertext, |
377 | encap->ciphertext, encap->ciphertext_len); | 400 | encap->ciphertext, encap->ciphertext_len); |
378 | 401 | ||
402 | |||
379 | if (should_fail != failed) { | 403 | if (should_fail != failed) { |
380 | parse_info(p, "FAIL: should_fail %d, failed %d", | 404 | parse_info(p, "FAIL: should_fail %d, failed %d", |
381 | should_fail, failed); | 405 | should_fail, failed); |
@@ -383,6 +407,7 @@ MlkemEncapFileTest(struct encap_ctx *encap) | |||
383 | } | 407 | } |
384 | 408 | ||
385 | err: | 409 | err: |
410 | freezero(shared_secret_buf, s_len); | ||
386 | return failed; | 411 | return failed; |
387 | } | 412 | } |
388 | 413 | ||
@@ -403,48 +428,22 @@ static const struct test_parse encap_parse = { | |||
403 | }; | 428 | }; |
404 | 429 | ||
405 | static int | 430 | static int |
406 | mlkem_encap_tests(const char *fn, size_t size) | 431 | mlkem_encap_tests(const char *fn, int rank) |
407 | { | 432 | { |
408 | struct MLKEM768_public_key public_key768; | 433 | struct encap_ctx encap = { |
409 | uint8_t ciphertext768[MLKEM768_CIPHERTEXT_BYTES]; | 434 | .rank = rank, |
410 | struct encap_ctx encap768 = { | ||
411 | .public_key = &public_key768, | ||
412 | .ciphertext = ciphertext768, | ||
413 | .ciphertext_len = sizeof(ciphertext768), | ||
414 | |||
415 | .parse_public_key = mlkem768_parse_public_key, | ||
416 | .encap_external_entropy = mlkem768_encap_external_entropy, | ||
417 | }; | ||
418 | struct MLKEM1024_public_key public_key1024; | ||
419 | uint8_t ciphertext1024[MLKEM1024_CIPHERTEXT_BYTES]; | ||
420 | struct encap_ctx encap1024 = { | ||
421 | .public_key = &public_key1024, | ||
422 | .ciphertext = ciphertext1024, | ||
423 | .ciphertext_len = sizeof(ciphertext1024), | ||
424 | |||
425 | .parse_public_key = mlkem1024_parse_public_key, | ||
426 | .encap_external_entropy = mlkem1024_encap_external_entropy, | ||
427 | }; | 435 | }; |
428 | 436 | ||
429 | if (size == 768) | 437 | return parse_test_file(fn, &encap_parse, &encap); |
430 | return parse_test_file(fn, &encap_parse, &encap768); | ||
431 | if (size == 1024) | ||
432 | return parse_test_file(fn, &encap_parse, &encap1024); | ||
433 | |||
434 | errx(1, "unknown encap test: size %zu", size); | ||
435 | } | 438 | } |
436 | 439 | ||
437 | struct keygen_ctx { | 440 | struct keygen_ctx { |
438 | struct parse *parse_ctx; | 441 | struct parse *parse_ctx; |
439 | 442 | ||
440 | void *private_key; | 443 | int rank; |
441 | void *encoded_public_key; | 444 | MLKEM_private_key *private_key; |
445 | uint8_t *encoded_public_key; | ||
442 | size_t encoded_public_key_len; | 446 | size_t encoded_public_key_len; |
443 | size_t private_key_len; | ||
444 | size_t public_key_len; | ||
445 | |||
446 | mlkem_generate_key_external_entropy_fn generate_key_external_entropy; | ||
447 | mlkem_marshal_private_key_fn marshal_private_key; | ||
448 | }; | 447 | }; |
449 | 448 | ||
450 | enum keygen_states { | 449 | enum keygen_states { |
@@ -482,13 +481,19 @@ keygen_init(void *ctx, void *parse_ctx) | |||
482 | 481 | ||
483 | keygen->parse_ctx = parse_ctx; | 482 | keygen->parse_ctx = parse_ctx; |
484 | 483 | ||
485 | return 1; | 484 | return (keygen->private_key = MLKEM_private_key_new(keygen->rank)) |
485 | != NULL; | ||
486 | } | 486 | } |
487 | 487 | ||
488 | static void | 488 | static void |
489 | keygen_finish(void *ctx) | 489 | keygen_finish(void *ctx) |
490 | { | 490 | { |
491 | (void)ctx; | 491 | struct keygen_ctx *keygen = ctx; |
492 | |||
493 | freezero(keygen->encoded_public_key, keygen->encoded_public_key_len); | ||
494 | keygen->encoded_public_key = NULL; | ||
495 | MLKEM_private_key_free(keygen->private_key); | ||
496 | keygen->private_key = NULL; | ||
492 | } | 497 | } |
493 | 498 | ||
494 | static int | 499 | static int |
@@ -504,18 +509,25 @@ MlkemKeygenFileTest(struct keygen_ctx *keygen) | |||
504 | parse_get_cbs(p, KEYGEN_PUBLIC_KEY, &public_key); | 509 | parse_get_cbs(p, KEYGEN_PUBLIC_KEY, &public_key); |
505 | parse_get_cbs(p, KEYGEN_PRIVATE_KEY, &private_key); | 510 | parse_get_cbs(p, KEYGEN_PRIVATE_KEY, &private_key); |
506 | 511 | ||
507 | if (!parse_length_equal(p, "seed", MLKEM_SEED_BYTES, CBS_len(&seed))) | 512 | if (!parse_length_equal(p, "seed", MLKEM_SEED_LENGTH, CBS_len(&seed))) |
508 | goto err; | 513 | goto err; |
514 | |||
515 | if (!MLKEM_generate_key_external_entropy(keygen->private_key, | ||
516 | &keygen->encoded_public_key, &keygen->encoded_public_key_len, | ||
517 | CBS_data(&seed))) { | ||
518 | parse_info(p, "generate_key_external_entropy"); | ||
519 | goto err; | ||
520 | } | ||
521 | |||
509 | if (!parse_length_equal(p, "public key", | 522 | if (!parse_length_equal(p, "public key", |
510 | keygen->public_key_len, CBS_len(&public_key))) | 523 | keygen->encoded_public_key_len, CBS_len(&public_key))) |
511 | goto err; | 524 | goto err; |
512 | if (!parse_length_equal(p, "private key", | 525 | if (!parse_length_equal(p, "private key", |
513 | keygen->private_key_len, CBS_len(&private_key))) | 526 | MLKEM_private_key_encoded_length(keygen->private_key), |
527 | CBS_len(&private_key))) | ||
514 | goto err; | 528 | goto err; |
515 | 529 | ||
516 | keygen->generate_key_external_entropy(keygen->encoded_public_key, | 530 | if (!MLKEM_marshal_private_key(keygen->private_key, |
517 | keygen->private_key, CBS_data(&seed)); | ||
518 | if (!keygen->marshal_private_key(keygen->private_key, | ||
519 | &encoded_private_key, &encoded_private_key_len)) { | 531 | &encoded_private_key, &encoded_private_key_len)) { |
520 | parse_info(p, "encode private key"); | 532 | parse_info(p, "encode private key"); |
521 | goto err; | 533 | goto err; |
@@ -589,7 +601,7 @@ MlkemNistKeygenFileTest(struct keygen_ctx *keygen) | |||
589 | struct parse *p = keygen->parse_ctx; | 601 | struct parse *p = keygen->parse_ctx; |
590 | CBB seed_cbb; | 602 | CBB seed_cbb; |
591 | CBS z, d, ek, dk; | 603 | CBS z, d, ek, dk; |
592 | uint8_t seed[MLKEM_SEED_BYTES]; | 604 | uint8_t seed[MLKEM_SEED_LENGTH]; |
593 | size_t seed_len; | 605 | size_t seed_len; |
594 | uint8_t *encoded_private_key = NULL; | 606 | uint8_t *encoded_private_key = NULL; |
595 | size_t encoded_private_key_len = 0; | 607 | size_t encoded_private_key_len = 0; |
@@ -609,12 +621,17 @@ MlkemNistKeygenFileTest(struct keygen_ctx *keygen) | |||
609 | if (!CBB_finish(&seed_cbb, NULL, &seed_len)) | 621 | if (!CBB_finish(&seed_cbb, NULL, &seed_len)) |
610 | parse_errx(p, "CBB_finish"); | 622 | parse_errx(p, "CBB_finish"); |
611 | 623 | ||
612 | if (!parse_length_equal(p, "bogus z or d", MLKEM_SEED_BYTES, seed_len)) | 624 | if (!parse_length_equal(p, "bogus z or d", MLKEM_SEED_LENGTH, seed_len)) |
613 | goto err; | 625 | goto err; |
614 | 626 | ||
615 | keygen->generate_key_external_entropy(keygen->encoded_public_key, | 627 | if (!MLKEM_generate_key_external_entropy(keygen->private_key, |
616 | keygen->private_key, seed); | 628 | &keygen->encoded_public_key, &keygen->encoded_public_key_len, |
617 | if (!keygen->marshal_private_key(keygen->private_key, | 629 | seed)) { |
630 | parse_info(p, "generate_key_external_entropy"); | ||
631 | goto err; | ||
632 | } | ||
633 | |||
634 | if (!MLKEM_marshal_private_key(keygen->private_key, | ||
618 | &encoded_private_key, &encoded_private_key_len)) { | 635 | &encoded_private_key, &encoded_private_key_len)) { |
619 | parse_info(p, "encode private key"); | 636 | parse_info(p, "encode private key"); |
620 | goto err; | 637 | goto err; |
@@ -648,74 +665,49 @@ static const struct test_parse nist_keygen_parse = { | |||
648 | }; | 665 | }; |
649 | 666 | ||
650 | static int | 667 | static int |
651 | mlkem_keygen_tests(const char *fn, size_t size, enum test_type test_type) | 668 | mlkem_keygen_tests(const char *fn, int rank, enum test_type test_type) |
652 | { | 669 | { |
653 | struct MLKEM768_private_key private_key768; | 670 | struct keygen_ctx keygen = { |
654 | uint8_t encoded_public_key768[MLKEM768_PUBLIC_KEY_BYTES]; | 671 | .rank = rank, |
655 | struct keygen_ctx keygen768 = { | ||
656 | .private_key = &private_key768, | ||
657 | .encoded_public_key = encoded_public_key768, | ||
658 | .encoded_public_key_len = sizeof(encoded_public_key768), | ||
659 | .private_key_len = MLKEM768_PRIVATE_KEY_BYTES, | ||
660 | .public_key_len = MLKEM768_PUBLIC_KEY_BYTES, | ||
661 | |||
662 | .generate_key_external_entropy = | ||
663 | mlkem768_generate_key_external_entropy, | ||
664 | .marshal_private_key = | ||
665 | mlkem768_marshal_private_key, | ||
666 | }; | ||
667 | struct MLKEM1024_private_key private_key1024; | ||
668 | uint8_t encoded_public_key1024[MLKEM1024_PUBLIC_KEY_BYTES]; | ||
669 | struct keygen_ctx keygen1024 = { | ||
670 | .private_key = &private_key1024, | ||
671 | .encoded_public_key = encoded_public_key1024, | ||
672 | .encoded_public_key_len = sizeof(encoded_public_key1024), | ||
673 | .private_key_len = MLKEM1024_PRIVATE_KEY_BYTES, | ||
674 | .public_key_len = MLKEM1024_PUBLIC_KEY_BYTES, | ||
675 | |||
676 | .generate_key_external_entropy = | ||
677 | mlkem1024_generate_key_external_entropy, | ||
678 | .marshal_private_key = | ||
679 | mlkem1024_marshal_private_key, | ||
680 | }; | 672 | }; |
673 | int ret = 0; | ||
681 | 674 | ||
682 | if (size == 768 && test_type == TEST_TYPE_NORMAL) | 675 | if (test_type == TEST_TYPE_NORMAL) |
683 | return parse_test_file(fn, &keygen_parse, &keygen768); | 676 | ret = parse_test_file(fn, &keygen_parse, &keygen); |
684 | if (size == 768 && test_type == TEST_TYPE_NIST) | 677 | else if (test_type == TEST_TYPE_NIST) |
685 | return parse_test_file(fn, &nist_keygen_parse, &keygen768); | 678 | ret = parse_test_file(fn, &nist_keygen_parse, &keygen); |
686 | if (size == 1024 && test_type == TEST_TYPE_NORMAL) | 679 | else |
687 | return parse_test_file(fn, &keygen_parse, &keygen1024); | 680 | errx(1, "unknown keygen test: rank %d, type %d", rank, |
688 | if (size == 1024 && test_type == TEST_TYPE_NIST) | 681 | test_type); |
689 | return parse_test_file(fn, &nist_keygen_parse, &keygen1024); | ||
690 | 682 | ||
691 | errx(1, "unknown keygen test: size %zu, type %d", size, test_type); | 683 | return ret; |
692 | } | 684 | } |
693 | 685 | ||
694 | static int | 686 | static int |
695 | run_mlkem_test(const char *test, const char *fn) | 687 | run_mlkem_test(const char *test, const char *fn) |
696 | { | 688 | { |
697 | if (strcmp(test, "mlkem768_decap_tests") == 0) | 689 | if (strcmp(test, "mlkem768_decap_tests") == 0) |
698 | return mlkem_decap_tests(fn, 768, TEST_TYPE_NORMAL); | 690 | return mlkem_decap_tests(fn, RANK768, TEST_TYPE_NORMAL); |
699 | if (strcmp(test, "mlkem768_nist_decap_tests") == 0) | 691 | if (strcmp(test, "mlkem768_nist_decap_tests") == 0) |
700 | return mlkem_decap_tests(fn, 768, TEST_TYPE_NIST); | 692 | return mlkem_decap_tests(fn, RANK768, TEST_TYPE_NIST); |
701 | if (strcmp(test, "mlkem1024_decap_tests") == 0) | 693 | if (strcmp(test, "mlkem1024_decap_tests") == 0) |
702 | return mlkem_decap_tests(fn, 1024, TEST_TYPE_NORMAL); | 694 | return mlkem_decap_tests(fn, RANK1024, TEST_TYPE_NORMAL); |
703 | if (strcmp(test, "mlkem1024_nist_decap_tests") == 0) | 695 | if (strcmp(test, "mlkem1024_nist_decap_tests") == 0) |
704 | return mlkem_decap_tests(fn, 1024, TEST_TYPE_NIST); | 696 | return mlkem_decap_tests(fn, RANK1024, TEST_TYPE_NIST); |
705 | 697 | ||
706 | if (strcmp(test, "mlkem768_encap_tests") == 0) | 698 | if (strcmp(test, "mlkem768_encap_tests") == 0) |
707 | return mlkem_encap_tests(fn, 768); | 699 | return mlkem_encap_tests(fn, RANK768); |
708 | if (strcmp(test, "mlkem1024_encap_tests") == 0) | 700 | if (strcmp(test, "mlkem1024_encap_tests") == 0) |
709 | return mlkem_encap_tests(fn, 1024); | 701 | return mlkem_encap_tests(fn, RANK1024); |
710 | 702 | ||
711 | if (strcmp(test, "mlkem768_keygen_tests") == 0) | 703 | if (strcmp(test, "mlkem768_keygen_tests") == 0) |
712 | return mlkem_keygen_tests(fn, 768, TEST_TYPE_NORMAL); | 704 | return mlkem_keygen_tests(fn, RANK768, TEST_TYPE_NORMAL); |
713 | if (strcmp(test, "mlkem768_nist_keygen_tests") == 0) | 705 | if (strcmp(test, "mlkem768_nist_keygen_tests") == 0) |
714 | return mlkem_keygen_tests(fn, 768, TEST_TYPE_NIST); | 706 | return mlkem_keygen_tests(fn, RANK768, TEST_TYPE_NIST); |
715 | if (strcmp(test, "mlkem1024_keygen_tests") == 0) | 707 | if (strcmp(test, "mlkem1024_keygen_tests") == 0) |
716 | return mlkem_keygen_tests(fn, 1024, TEST_TYPE_NORMAL); | 708 | return mlkem_keygen_tests(fn, RANK1024, TEST_TYPE_NORMAL); |
717 | if (strcmp(test, "mlkem1024_nist_keygen_tests") == 0) | 709 | if (strcmp(test, "mlkem1024_nist_keygen_tests") == 0) |
718 | return mlkem_keygen_tests(fn, 1024, TEST_TYPE_NIST); | 710 | return mlkem_keygen_tests(fn, RANK1024, TEST_TYPE_NIST); |
719 | 711 | ||
720 | errx(1, "unknown test %s (test file %s)", test, fn); | 712 | errx(1, "unknown test %s (test file %s)", test, fn); |
721 | } | 713 | } |