diff options
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem768_keygen_tests.c')
| -rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem768_keygen_tests.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem768_keygen_tests.c b/src/regress/lib/libcrypto/mlkem/mlkem768_keygen_tests.c new file mode 100644 index 0000000000..60c3983dea --- /dev/null +++ b/src/regress/lib/libcrypto/mlkem/mlkem768_keygen_tests.c | |||
| @@ -0,0 +1,129 @@ | |||
| 1 | /* Copyright (c) 2024, Google Inc. | ||
| 2 | * Copyright (c) 2024, Bob Beck <beck@obtuse.com> | ||
| 3 | * | ||
| 4 | * Permission to use, copy, modify, and/or distribute this software for any | ||
| 5 | * purpose with or without fee is hereby granted, provided that the above | ||
| 6 | * copyright notice and this permission notice appear in all copies. | ||
| 7 | * | ||
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
| 11 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION | ||
| 13 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | ||
| 14 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ | ||
| 15 | |||
| 16 | #include <err.h> | ||
| 17 | #include <stdlib.h> | ||
| 18 | #include <stdio.h> | ||
| 19 | #include <string.h> | ||
| 20 | |||
| 21 | #include <openssl/evp.h> | ||
| 22 | |||
| 23 | #include "bytestring.h" | ||
| 24 | #include "mlkem.h" | ||
| 25 | #include "mlkem_internal.h" | ||
| 26 | #include "sha3_internal.h" | ||
| 27 | #include "mlkem_tests_util.h" | ||
| 28 | |||
| 29 | static int | ||
| 30 | encode_private_key(const struct MLKEM768_private_key *priv, uint8_t **out_buf, | ||
| 31 | size_t *out_len) | ||
| 32 | { | ||
| 33 | CBB cbb; | ||
| 34 | if (!CBB_init(&cbb, MLKEM768_PUBLIC_KEY_BYTES)) | ||
| 35 | return 0; | ||
| 36 | if (!MLKEM768_marshal_private_key(&cbb, priv)) | ||
| 37 | return 0; | ||
| 38 | if (!CBB_finish(&cbb, out_buf, out_len)) | ||
| 39 | return 0; | ||
| 40 | CBB_cleanup(&cbb); | ||
| 41 | return 1; | ||
| 42 | } | ||
| 43 | |||
| 44 | static void | ||
| 45 | MlkemKeygenFileTest(CBS *seed, CBS *public_key, CBS *private_key) | ||
| 46 | { | ||
| 47 | struct MLKEM768_private_key priv; | ||
| 48 | uint8_t *encoded_private_key = NULL; | ||
| 49 | uint8_t encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES]; | ||
| 50 | size_t len; | ||
| 51 | |||
| 52 | TEST(CBS_len(seed) != MLKEM_SEED_BYTES, "seed len bogus"); | ||
| 53 | TEST(CBS_len(private_key) != MLKEM768_PRIVATE_KEY_BYTES, | ||
| 54 | "expected private key len bogus"); | ||
| 55 | TEST(CBS_len(public_key) != MLKEM768_PUBLIC_KEY_BYTES, | ||
| 56 | "expected public key len bogus"); | ||
| 57 | MLKEM768_generate_key_external_entropy(encoded_public_key, &priv, | ||
| 58 | CBS_data(seed)); | ||
| 59 | TEST(!encode_private_key(&priv, &encoded_private_key, | ||
| 60 | &len), "encode_private_key"); | ||
| 61 | TEST(len != MLKEM768_PRIVATE_KEY_BYTES, "private key len bogus"); | ||
| 62 | TEST_DATAEQ(encoded_public_key, CBS_data(public_key), | ||
| 63 | MLKEM768_PUBLIC_KEY_BYTES, "public key"); | ||
| 64 | TEST_DATAEQ(encoded_private_key, CBS_data(private_key), | ||
| 65 | MLKEM768_PRIVATE_KEY_BYTES, "private key"); | ||
| 66 | free(encoded_private_key); | ||
| 67 | } | ||
| 68 | |||
| 69 | #define S_START 0 | ||
| 70 | #define S_SEED 1 | ||
| 71 | #define S_PUBLIC_KEY 2 | ||
| 72 | #define S_PRIVATE_KEY 3 | ||
| 73 | |||
| 74 | int | ||
| 75 | main(int argc, char **argv) | ||
| 76 | { | ||
| 77 | CBS seed, public_key, private_key; | ||
| 78 | char *buf; | ||
| 79 | FILE *fp; | ||
| 80 | int state; | ||
| 81 | |||
| 82 | fprintf(stderr, "Testing keygen test vectors in %s\n", argv[1]); | ||
| 83 | TEST((fp = fopen(argv[1], "r")) == NULL, "can't open test file"); | ||
| 84 | MALLOC(buf, 16*1024); | ||
| 85 | state = S_SEED; | ||
| 86 | test_number = 1; | ||
| 87 | while (fgets(buf, 16*1024, fp) != NULL) { | ||
| 88 | switch (state) { | ||
| 89 | case S_START: | ||
| 90 | if (strcmp(buf, "\n") != 0) | ||
| 91 | break; | ||
| 92 | state = S_SEED; | ||
| 93 | break; | ||
| 94 | case S_SEED: | ||
| 95 | if (strncmp(buf, "seed: ", strlen("seed: ")) != 0) { | ||
| 96 | break; | ||
| 97 | } | ||
| 98 | grab_data(&seed, buf, strlen("seed: ")); | ||
| 99 | state = S_PUBLIC_KEY; | ||
| 100 | break; | ||
| 101 | case S_PUBLIC_KEY: | ||
| 102 | if (strncmp(buf, "public_key: ", | ||
| 103 | strlen("public_key: ")) != 0) | ||
| 104 | break; | ||
| 105 | grab_data(&public_key, buf, strlen("public_key: ")); | ||
| 106 | state = S_PRIVATE_KEY; | ||
| 107 | break; | ||
| 108 | case S_PRIVATE_KEY: | ||
| 109 | if (strncmp(buf, "private_key: ", | ||
| 110 | strlen("private_key: ")) != 0) | ||
| 111 | break; | ||
| 112 | grab_data(&private_key, buf, strlen("private_key: ")); | ||
| 113 | state = S_START; | ||
| 114 | break; | ||
| 115 | |||
| 116 | MlkemKeygenFileTest(&seed, &public_key, &private_key); | ||
| 117 | free((void *)CBS_data(&seed)); | ||
| 118 | free((void *)CBS_data(&public_key)); | ||
| 119 | free((void *)CBS_data(&private_key)); | ||
| 120 | |||
| 121 | test_number++; | ||
| 122 | state = S_START; | ||
| 123 | break; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | free(buf); | ||
| 128 | exit(failure); | ||
| 129 | } | ||
