diff options
Diffstat (limited to '')
| -rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c | 8 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_tests.c | 11 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c | 80 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h | 32 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_unittest.c | 44 |
5 files changed, 77 insertions, 98 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c b/src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c index 5a61248090..a8495f55e3 100644 --- a/src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c +++ b/src/regress/lib/libcrypto/mlkem/mlkem_iteration_tests.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: mlkem_iteration_tests.c,v 1.2 2024/12/26 07:26:45 tb Exp $ */ | 1 | /* $OpenBSD: mlkem_iteration_tests.c,v 1.3 2025/05/19 06:47:40 beck Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2024 Google Inc. | 3 | * Copyright (c) 2024 Google Inc. |
| 4 | * Copyright (c) 2024 Bob Beck <beck@obtuse.com> | 4 | * Copyright (c) 2024 Bob Beck <beck@obtuse.com> |
| @@ -116,8 +116,10 @@ MlkemIterativeTest(struct iteration_ctx *ctx) | |||
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | /* generate ek as encoded_public_key */ | 118 | /* generate ek as encoded_public_key */ |
| 119 | ctx->generate_key_external_entropy(ctx->encoded_public_key, | 119 | if (!ctx->generate_key_external_entropy(ctx->encoded_public_key, |
| 120 | ctx->priv, seed); | 120 | ctx->priv, seed)) { |
| 121 | errx(1, "generate_key_external_entropy"); | ||
| 122 | } | ||
| 121 | ctx->public_from_private(ctx->pub, ctx->priv); | 123 | ctx->public_from_private(ctx->pub, ctx->priv); |
| 122 | 124 | ||
| 123 | /* hash in ek */ | 125 | /* hash in ek */ |
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests.c b/src/regress/lib/libcrypto/mlkem/mlkem_tests.c index e9ae417887..a4e7208c76 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.3 2025/05/03 08:34:07 tb Exp $ */ | 1 | /* $OpenBSD: mlkem_tests.c,v 1.4 2025/05/19 06:47:40 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> |
| @@ -112,7 +112,8 @@ MlkemDecapFileTest(struct decap_ctx *decap) | |||
| 112 | parse_get_cbs(p, DECAP_PRIVATE_KEY, &private_key); | 112 | parse_get_cbs(p, DECAP_PRIVATE_KEY, &private_key); |
| 113 | parse_get_int(p, DECAP_RESULT, &should_fail); | 113 | parse_get_int(p, DECAP_RESULT, &should_fail); |
| 114 | 114 | ||
| 115 | if (!decap->parse_private_key(decap->private_key, &private_key)) { | 115 | if (!decap->parse_private_key(decap->private_key, |
| 116 | CBS_data(&private_key), CBS_len(&private_key))) { | ||
| 116 | if ((failed = !should_fail)) | 117 | if ((failed = !should_fail)) |
| 117 | parse_info(p, "parse private key"); | 118 | parse_info(p, "parse private key"); |
| 118 | goto err; | 119 | goto err; |
| @@ -207,7 +208,8 @@ MlkemNistDecapFileTest(struct decap_ctx *decap) | |||
| 207 | MLKEM_SHARED_SECRET_BYTES, CBS_len(&k))) | 208 | MLKEM_SHARED_SECRET_BYTES, CBS_len(&k))) |
| 208 | goto err; | 209 | goto err; |
| 209 | 210 | ||
| 210 | if (!decap->parse_private_key(decap->private_key, &dk)) { | 211 | if (!decap->parse_private_key(decap->private_key, CBS_data(&dk), |
| 212 | CBS_len(&dk))) { | ||
| 211 | parse_info(p, "parse private key"); | 213 | parse_info(p, "parse private key"); |
| 212 | goto err; | 214 | goto err; |
| 213 | } | 215 | } |
| @@ -360,7 +362,8 @@ MlkemEncapFileTest(struct encap_ctx *encap) | |||
| 360 | parse_get_cbs(p, ENCAP_SHARED_SECRET, &shared_secret); | 362 | parse_get_cbs(p, ENCAP_SHARED_SECRET, &shared_secret); |
| 361 | parse_get_int(p, ENCAP_RESULT, &should_fail); | 363 | parse_get_int(p, ENCAP_RESULT, &should_fail); |
| 362 | 364 | ||
| 363 | if (!encap->parse_public_key(encap->public_key, &public_key)) { | 365 | if (!encap->parse_public_key(encap->public_key, CBS_data(&public_key), |
| 366 | CBS_len(&public_key))) { | ||
| 364 | if ((failed = !should_fail)) | 367 | if ((failed = !should_fail)) |
| 365 | parse_info(p, "parse public key"); | 368 | parse_info(p, "parse public key"); |
| 366 | goto err; | 369 | goto err; |
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c index 1bb2ed3a8b..8677713c8e 100644 --- a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c +++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: mlkem_tests_util.c,v 1.5 2024/12/26 00:04:24 tb Exp $ */ | 1 | /* $OpenBSD: mlkem_tests_util.c,v 1.6 2025/05/19 06:47:40 beck Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2024 Google Inc. | 3 | * Copyright (c) 2024 Google Inc. |
| 4 | * Copyright (c) 2024 Bob Beck <beck@obtuse.com> | 4 | * Copyright (c) 2024 Bob Beck <beck@obtuse.com> |
| @@ -83,25 +83,10 @@ mlkem768_encode_private_key(const void *private_key, uint8_t **out_buf, | |||
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | int | 85 | int |
| 86 | mlkem768_encode_public_key(const void *public_key, uint8_t **out_buf, | 86 | mlkem768_marshal_public_key(const void *public_key, uint8_t **out_buf, |
| 87 | size_t *out_len) | 87 | size_t *out_len) |
| 88 | { | 88 | { |
| 89 | CBB cbb; | 89 | return MLKEM768_marshal_public_key(out_buf, out_len, public_key); |
| 90 | int ret = 0; | ||
| 91 | |||
| 92 | if (!CBB_init(&cbb, MLKEM768_PUBLIC_KEY_BYTES)) | ||
| 93 | goto err; | ||
| 94 | if (!MLKEM768_marshal_public_key(&cbb, public_key)) | ||
| 95 | goto err; | ||
| 96 | if (!CBB_finish(&cbb, out_buf, out_len)) | ||
| 97 | goto err; | ||
| 98 | |||
| 99 | ret = 1; | ||
| 100 | |||
| 101 | err: | ||
| 102 | CBB_cleanup(&cbb); | ||
| 103 | |||
| 104 | return ret; | ||
| 105 | } | 90 | } |
| 106 | 91 | ||
| 107 | int | 92 | int |
| @@ -127,25 +112,10 @@ mlkem1024_encode_private_key(const void *private_key, uint8_t **out_buf, | |||
| 127 | } | 112 | } |
| 128 | 113 | ||
| 129 | int | 114 | int |
| 130 | mlkem1024_encode_public_key(const void *public_key, uint8_t **out_buf, | 115 | mlkem1024_marshal_public_key(const void *public_key, uint8_t **out_buf, |
| 131 | size_t *out_len) | 116 | size_t *out_len) |
| 132 | { | 117 | { |
| 133 | CBB cbb; | 118 | return MLKEM1024_marshal_public_key(out_buf, out_len, public_key); |
| 134 | int ret = 0; | ||
| 135 | |||
| 136 | if (!CBB_init(&cbb, MLKEM1024_PUBLIC_KEY_BYTES)) | ||
| 137 | goto err; | ||
| 138 | if (!MLKEM1024_marshal_public_key(&cbb, public_key)) | ||
| 139 | goto err; | ||
| 140 | if (!CBB_finish(&cbb, out_buf, out_len)) | ||
| 141 | goto err; | ||
| 142 | |||
| 143 | ret = 1; | ||
| 144 | |||
| 145 | err: | ||
| 146 | CBB_cleanup(&cbb); | ||
| 147 | |||
| 148 | return ret; | ||
| 149 | } | 119 | } |
| 150 | 120 | ||
| 151 | int | 121 | int |
| @@ -173,32 +143,36 @@ mlkem768_encap_external_entropy(uint8_t *out_ciphertext, | |||
| 173 | public_key, entropy); | 143 | public_key, entropy); |
| 174 | } | 144 | } |
| 175 | 145 | ||
| 176 | void | 146 | int |
| 177 | mlkem768_generate_key(uint8_t *out_encoded_public_key, | 147 | mlkem768_generate_key(uint8_t *out_encoded_public_key, |
| 178 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key) | 148 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key) |
| 179 | { | 149 | { |
| 180 | MLKEM768_generate_key(out_encoded_public_key, optional_out_seed, | 150 | return MLKEM768_generate_key(out_encoded_public_key, optional_out_seed, |
| 181 | out_private_key); | 151 | out_private_key); |
| 182 | } | 152 | } |
| 183 | 153 | ||
| 184 | void | 154 | int |
| 185 | mlkem768_generate_key_external_entropy(uint8_t *out_encoded_public_key, | 155 | mlkem768_generate_key_external_entropy(uint8_t *out_encoded_public_key, |
| 186 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]) | 156 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]) |
| 187 | { | 157 | { |
| 188 | MLKEM768_generate_key_external_entropy(out_encoded_public_key, | 158 | return MLKEM768_generate_key_external_entropy(out_encoded_public_key, |
| 189 | out_private_key, entropy); | 159 | out_private_key, entropy); |
| 190 | } | 160 | } |
| 191 | 161 | ||
| 192 | int | 162 | int |
| 193 | mlkem768_parse_private_key(void *out_private_key, CBS *private_key_cbs) | 163 | mlkem768_parse_private_key(void *out_private_key, const uint8_t *private_key, |
| 164 | size_t private_key_len) | ||
| 194 | { | 165 | { |
| 195 | return MLKEM768_parse_private_key(out_private_key, private_key_cbs); | 166 | return MLKEM768_parse_private_key(out_private_key, private_key, |
| 167 | private_key_len); | ||
| 196 | } | 168 | } |
| 197 | 169 | ||
| 198 | int | 170 | int |
| 199 | mlkem768_parse_public_key(void *out_public_key, CBS *public_key_cbs) | 171 | mlkem768_parse_public_key(void *out_public_key, const uint8_t *public_key, |
| 172 | size_t public_key_len) | ||
| 200 | { | 173 | { |
| 201 | return MLKEM768_parse_public_key(out_public_key, public_key_cbs); | 174 | return MLKEM768_parse_public_key(out_public_key, public_key, |
| 175 | public_key_len); | ||
| 202 | } | 176 | } |
| 203 | 177 | ||
| 204 | void | 178 | void |
| @@ -232,26 +206,28 @@ mlkem1024_encap_external_entropy(uint8_t *out_ciphertext, | |||
| 232 | public_key, entropy); | 206 | public_key, entropy); |
| 233 | } | 207 | } |
| 234 | 208 | ||
| 235 | void | 209 | int |
| 236 | mlkem1024_generate_key(uint8_t *out_encoded_public_key, | 210 | mlkem1024_generate_key(uint8_t *out_encoded_public_key, |
| 237 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key) | 211 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key) |
| 238 | { | 212 | { |
| 239 | MLKEM1024_generate_key(out_encoded_public_key, optional_out_seed, | 213 | return MLKEM1024_generate_key(out_encoded_public_key, optional_out_seed, |
| 240 | out_private_key); | 214 | out_private_key); |
| 241 | } | 215 | } |
| 242 | 216 | ||
| 243 | void | 217 | int |
| 244 | mlkem1024_generate_key_external_entropy(uint8_t *out_encoded_public_key, | 218 | mlkem1024_generate_key_external_entropy(uint8_t *out_encoded_public_key, |
| 245 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]) | 219 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]) |
| 246 | { | 220 | { |
| 247 | MLKEM1024_generate_key_external_entropy(out_encoded_public_key, | 221 | return MLKEM1024_generate_key_external_entropy(out_encoded_public_key, |
| 248 | out_private_key, entropy); | 222 | out_private_key, entropy); |
| 249 | } | 223 | } |
| 250 | 224 | ||
| 251 | int | 225 | int |
| 252 | mlkem1024_parse_private_key(void *out_private_key, CBS *private_key_cbs) | 226 | mlkem1024_parse_private_key(void *out_private_key, const uint8_t *private_key, |
| 227 | size_t private_key_len) | ||
| 253 | { | 228 | { |
| 254 | return MLKEM1024_parse_private_key(out_private_key, private_key_cbs); | 229 | return MLKEM1024_parse_private_key(out_private_key, private_key, |
| 230 | private_key_len); | ||
| 255 | } | 231 | } |
| 256 | 232 | ||
| 257 | void | 233 | void |
| @@ -261,7 +237,9 @@ mlkem1024_public_from_private(void *out_public_key, const void *private_key) | |||
| 261 | } | 237 | } |
| 262 | 238 | ||
| 263 | int | 239 | int |
| 264 | mlkem1024_parse_public_key(void *out_public_key, CBS *public_key_cbs) | 240 | mlkem1024_parse_public_key(void *out_public_key, const uint8_t *public_key, |
| 241 | size_t public_key_len) | ||
| 265 | { | 242 | { |
| 266 | return MLKEM1024_parse_public_key(out_public_key, public_key_cbs); | 243 | return MLKEM1024_parse_public_key(out_public_key, public_key, |
| 244 | public_key_len); | ||
| 267 | } | 245 | } |
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h index 7fbe6f76a9..a3b255082f 100644 --- a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h +++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: mlkem_tests_util.h,v 1.4 2024/12/26 00:04:24 tb Exp $ */ | 1 | /* $OpenBSD: mlkem_tests_util.h,v 1.5 2025/05/19 06:47:40 beck Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2024 Bob Beck <beck@obtuse.com> | 3 | * Copyright (c) 2024 Bob Beck <beck@obtuse.com> |
| 4 | * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> |
| @@ -32,11 +32,11 @@ int compare_data(const uint8_t *want, const uint8_t *got, size_t len, | |||
| 32 | 32 | ||
| 33 | int mlkem768_encode_private_key(const void *priv, uint8_t **out_buf, | 33 | int mlkem768_encode_private_key(const void *priv, uint8_t **out_buf, |
| 34 | size_t *out_len); | 34 | size_t *out_len); |
| 35 | int mlkem768_encode_public_key(const void *pub, uint8_t **out_buf, | 35 | int mlkem768_marshal_public_key(const void *pub, uint8_t **out_buf, |
| 36 | size_t *out_len); | 36 | size_t *out_len); |
| 37 | int mlkem1024_encode_private_key(const void *priv, uint8_t **out_buf, | 37 | int mlkem1024_encode_private_key(const void *priv, uint8_t **out_buf, |
| 38 | size_t *out_len); | 38 | size_t *out_len); |
| 39 | int mlkem1024_encode_public_key(const void *pub, uint8_t **out_buf, | 39 | int mlkem1024_marshal_public_key(const void *pub, uint8_t **out_buf, |
| 40 | size_t *out_len); | 40 | size_t *out_len); |
| 41 | 41 | ||
| 42 | int mlkem768_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], | 42 | int mlkem768_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
| @@ -46,12 +46,12 @@ void mlkem768_encap(uint8_t *out_ciphertext, | |||
| 46 | void mlkem768_encap_external_entropy(uint8_t *out_ciphertext, | 46 | void mlkem768_encap_external_entropy(uint8_t *out_ciphertext, |
| 47 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const void *pub, | 47 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const void *pub, |
| 48 | const uint8_t entropy[MLKEM_ENCAP_ENTROPY]); | 48 | const uint8_t entropy[MLKEM_ENCAP_ENTROPY]); |
| 49 | void mlkem768_generate_key(uint8_t *out_encoded_public_key, | 49 | int mlkem768_generate_key(uint8_t *out_encoded_public_key, |
| 50 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key); | 50 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key); |
| 51 | void mlkem768_generate_key_external_entropy(uint8_t *out_encoded_public_key, | 51 | int mlkem768_generate_key_external_entropy(uint8_t *out_encoded_public_key, |
| 52 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]); | 52 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]); |
| 53 | int mlkem768_parse_private_key(void *priv, CBS *private_key_cbs); | 53 | int mlkem768_parse_private_key(void *priv, const uint8_t *in, size_t in_len); |
| 54 | int mlkem768_parse_public_key(void *pub, CBS *in); | 54 | int mlkem768_parse_public_key(void *pub, const uint8_t *in, size_t in_len); |
| 55 | void mlkem768_public_from_private(void *out_public_key, const void *private_key); | 55 | void mlkem768_public_from_private(void *out_public_key, const void *private_key); |
| 56 | 56 | ||
| 57 | int mlkem1024_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], | 57 | int mlkem1024_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
| @@ -61,16 +61,16 @@ void mlkem1024_encap(uint8_t *out_ciphertext, | |||
| 61 | void mlkem1024_encap_external_entropy(uint8_t *out_ciphertext, | 61 | void mlkem1024_encap_external_entropy(uint8_t *out_ciphertext, |
| 62 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const void *pub, | 62 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const void *pub, |
| 63 | const uint8_t entropy[MLKEM_ENCAP_ENTROPY]); | 63 | const uint8_t entropy[MLKEM_ENCAP_ENTROPY]); |
| 64 | void mlkem1024_generate_key(uint8_t *out_encoded_public_key, | 64 | int mlkem1024_generate_key(uint8_t *out_encoded_public_key, |
| 65 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key); | 65 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key); |
| 66 | void mlkem1024_generate_key_external_entropy(uint8_t *out_encoded_public_key, | 66 | int mlkem1024_generate_key_external_entropy(uint8_t *out_encoded_public_key, |
| 67 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]); | 67 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]); |
| 68 | int mlkem1024_parse_private_key(void *priv, CBS *private_key_cbs); | 68 | int mlkem1024_parse_private_key(void *priv, const uint8_t *in, size_t in_len); |
| 69 | int mlkem1024_parse_public_key(void *pub, CBS *in); | 69 | int mlkem1024_parse_public_key(void *pub, const uint8_t *in, size_t in_len); |
| 70 | void mlkem1024_public_from_private(void *out_public_key, const void *private_key); | 70 | void mlkem1024_public_from_private(void *out_public_key, const void *private_key); |
| 71 | 71 | ||
| 72 | typedef int (*mlkem_encode_private_key_fn)(const void *, uint8_t **, size_t *); | 72 | typedef int (*mlkem_encode_private_key_fn)(const void *, uint8_t **, size_t *); |
| 73 | typedef int (*mlkem_encode_public_key_fn)(const void *, uint8_t **, size_t *); | 73 | typedef int (*mlkem_marshal_public_key_fn)(const void *, uint8_t **, size_t *); |
| 74 | typedef int (*mlkem_decap_fn)(uint8_t [MLKEM_SHARED_SECRET_BYTES], | 74 | typedef int (*mlkem_decap_fn)(uint8_t [MLKEM_SHARED_SECRET_BYTES], |
| 75 | const uint8_t *, size_t, const void *); | 75 | const uint8_t *, size_t, const void *); |
| 76 | typedef void (*mlkem_encap_fn)(uint8_t *, uint8_t [MLKEM_SHARED_SECRET_BYTES], | 76 | typedef void (*mlkem_encap_fn)(uint8_t *, uint8_t [MLKEM_SHARED_SECRET_BYTES], |
| @@ -78,11 +78,11 @@ typedef void (*mlkem_encap_fn)(uint8_t *, uint8_t [MLKEM_SHARED_SECRET_BYTES], | |||
| 78 | typedef void (*mlkem_encap_external_entropy_fn)(uint8_t *, | 78 | typedef void (*mlkem_encap_external_entropy_fn)(uint8_t *, |
| 79 | uint8_t [MLKEM_SHARED_SECRET_BYTES], const void *, | 79 | uint8_t [MLKEM_SHARED_SECRET_BYTES], const void *, |
| 80 | const uint8_t [MLKEM_ENCAP_ENTROPY]); | 80 | const uint8_t [MLKEM_ENCAP_ENTROPY]); |
| 81 | typedef void (*mlkem_generate_key_fn)(uint8_t *, uint8_t *, void *); | 81 | typedef int (*mlkem_generate_key_fn)(uint8_t *, uint8_t *, void *); |
| 82 | typedef void (*mlkem_generate_key_external_entropy_fn)(uint8_t *, void *, | 82 | typedef int (*mlkem_generate_key_external_entropy_fn)(uint8_t *, void *, |
| 83 | const uint8_t [MLKEM_SEED_BYTES]); | 83 | const uint8_t [MLKEM_SEED_BYTES]); |
| 84 | typedef int (*mlkem_parse_private_key_fn)(void *, CBS *); | 84 | typedef int (*mlkem_parse_private_key_fn)(void *, const uint8_t *, size_t); |
| 85 | typedef int (*mlkem_parse_public_key_fn)(void *, CBS *); | 85 | typedef int (*mlkem_parse_public_key_fn)(void *, const uint8_t *, size_t); |
| 86 | typedef void (*mlkem_public_from_private_fn)(void *out_public_key, | 86 | typedef void (*mlkem_public_from_private_fn)(void *out_public_key, |
| 87 | const void *private_key); | 87 | const void *private_key); |
| 88 | 88 | ||
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c b/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c index 597297b8cc..a1adc88569 100644 --- a/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c +++ b/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: mlkem_unittest.c,v 1.7 2025/05/03 08:34:55 tb Exp $ */ | 1 | /* $OpenBSD: mlkem_unittest.c,v 1.8 2025/05/19 06:47:40 beck Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2024 Google Inc. | 3 | * Copyright (c) 2024 Google Inc. |
| 4 | * Copyright (c) 2024 Bob Beck <beck@obtuse.com> | 4 | * Copyright (c) 2024 Bob Beck <beck@obtuse.com> |
| @@ -42,7 +42,7 @@ struct unittest_ctx { | |||
| 42 | mlkem_parse_private_key_fn parse_private_key; | 42 | mlkem_parse_private_key_fn parse_private_key; |
| 43 | mlkem_parse_public_key_fn parse_public_key; | 43 | mlkem_parse_public_key_fn parse_public_key; |
| 44 | mlkem_encode_private_key_fn encode_private_key; | 44 | mlkem_encode_private_key_fn encode_private_key; |
| 45 | mlkem_encode_public_key_fn encode_public_key; | 45 | mlkem_marshal_public_key_fn marshal_public_key; |
| 46 | mlkem_public_from_private_fn public_from_private; | 46 | mlkem_public_from_private_fn public_from_private; |
| 47 | }; | 47 | }; |
| 48 | 48 | ||
| @@ -54,36 +54,32 @@ MlKemUnitTest(struct unittest_ctx *ctx) | |||
| 54 | uint8_t first_two_bytes[2]; | 54 | uint8_t first_two_bytes[2]; |
| 55 | uint8_t *encoded_private_key = NULL, *tmp_buf = NULL; | 55 | uint8_t *encoded_private_key = NULL, *tmp_buf = NULL; |
| 56 | size_t encoded_private_key_len, tmp_buf_len; | 56 | size_t encoded_private_key_len, tmp_buf_len; |
| 57 | CBS cbs; | ||
| 58 | int failed = 0; | 57 | int failed = 0; |
| 59 | 58 | ||
| 60 | ctx->generate_key(ctx->encoded_public_key, NULL, ctx->priv); | 59 | if (!ctx->generate_key(ctx->encoded_public_key, NULL, ctx->priv)) { |
| 60 | warnx("generate_key failed"); | ||
| 61 | failed |= 1; | ||
| 62 | } | ||
| 61 | 63 | ||
| 62 | memcpy(first_two_bytes, ctx->encoded_public_key, sizeof(first_two_bytes)); | 64 | memcpy(first_two_bytes, ctx->encoded_public_key, sizeof(first_two_bytes)); |
| 63 | memset(ctx->encoded_public_key, 0xff, sizeof(first_two_bytes)); | 65 | memset(ctx->encoded_public_key, 0xff, sizeof(first_two_bytes)); |
| 64 | 66 | ||
| 65 | CBS_init(&cbs, ctx->encoded_public_key, ctx->encoded_public_key_len); | ||
| 66 | |||
| 67 | /* Parsing should fail because the first coefficient is >= kPrime. */ | 67 | /* Parsing should fail because the first coefficient is >= kPrime. */ |
| 68 | if (ctx->parse_public_key(ctx->pub, &cbs)) { | 68 | if (ctx->parse_public_key(ctx->pub, ctx->encoded_public_key, |
| 69 | ctx->encoded_public_key_len)) { | ||
| 69 | warnx("parse_public_key should have failed"); | 70 | warnx("parse_public_key should have failed"); |
| 70 | failed |= 1; | 71 | failed |= 1; |
| 71 | } | 72 | } |
| 72 | 73 | ||
| 73 | memcpy(ctx->encoded_public_key, first_two_bytes, sizeof(first_two_bytes)); | 74 | memcpy(ctx->encoded_public_key, first_two_bytes, sizeof(first_two_bytes)); |
| 74 | CBS_init(&cbs, ctx->encoded_public_key, ctx->encoded_public_key_len); | 75 | if (!ctx->parse_public_key(ctx->pub, ctx->encoded_public_key, |
| 75 | if (!ctx->parse_public_key(ctx->pub, &cbs)) { | 76 | ctx->encoded_public_key_len)) { |
| 76 | warnx("MLKEM768_parse_public_key"); | 77 | warnx("MLKEM768_parse_public_key"); |
| 77 | failed |= 1; | 78 | failed |= 1; |
| 78 | } | 79 | } |
| 79 | 80 | ||
| 80 | if (CBS_len(&cbs) != 0u) { | 81 | if (!ctx->marshal_public_key(ctx->pub, &tmp_buf, &tmp_buf_len)) { |
| 81 | warnx("CBS_len must be 0"); | 82 | warnx("marshal_public_key"); |
| 82 | failed |= 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | if (!ctx->encode_public_key(ctx->pub, &tmp_buf, &tmp_buf_len)) { | ||
| 86 | warnx("encode_public_key"); | ||
| 87 | failed |= 1; | 83 | failed |= 1; |
| 88 | } | 84 | } |
| 89 | if (ctx->encoded_public_key_len != tmp_buf_len) { | 85 | if (ctx->encoded_public_key_len != tmp_buf_len) { |
| @@ -100,8 +96,8 @@ MlKemUnitTest(struct unittest_ctx *ctx) | |||
| 100 | tmp_buf = NULL; | 96 | tmp_buf = NULL; |
| 101 | 97 | ||
| 102 | ctx->public_from_private(ctx->pub2, ctx->priv); | 98 | ctx->public_from_private(ctx->pub2, ctx->priv); |
| 103 | if (!ctx->encode_public_key(ctx->pub2, &tmp_buf, &tmp_buf_len)) { | 99 | if (!ctx->marshal_public_key(ctx->pub2, &tmp_buf, &tmp_buf_len)) { |
| 104 | warnx("encode_public_key"); | 100 | warnx("marshal_public_key"); |
| 105 | failed |= 1; | 101 | failed |= 1; |
| 106 | } | 102 | } |
| 107 | if (ctx->encoded_public_key_len != tmp_buf_len) { | 103 | if (ctx->encoded_public_key_len != tmp_buf_len) { |
| @@ -125,18 +121,18 @@ MlKemUnitTest(struct unittest_ctx *ctx) | |||
| 125 | 121 | ||
| 126 | memcpy(first_two_bytes, encoded_private_key, sizeof(first_two_bytes)); | 122 | memcpy(first_two_bytes, encoded_private_key, sizeof(first_two_bytes)); |
| 127 | memset(encoded_private_key, 0xff, sizeof(first_two_bytes)); | 123 | memset(encoded_private_key, 0xff, sizeof(first_two_bytes)); |
| 128 | CBS_init(&cbs, encoded_private_key, encoded_private_key_len); | ||
| 129 | 124 | ||
| 130 | /* Parsing should fail because the first coefficient is >= kPrime. */ | 125 | /* Parsing should fail because the first coefficient is >= kPrime. */ |
| 131 | if (ctx->parse_private_key(ctx->priv2, &cbs)) { | 126 | if (ctx->parse_private_key(ctx->priv2, encoded_private_key, |
| 127 | encoded_private_key_len)) { | ||
| 132 | warnx("MLKEM768_parse_private_key should have failed"); | 128 | warnx("MLKEM768_parse_private_key should have failed"); |
| 133 | failed |= 1; | 129 | failed |= 1; |
| 134 | } | 130 | } |
| 135 | 131 | ||
| 136 | memcpy(encoded_private_key, first_two_bytes, sizeof(first_two_bytes)); | 132 | memcpy(encoded_private_key, first_two_bytes, sizeof(first_two_bytes)); |
| 137 | CBS_init(&cbs, encoded_private_key, encoded_private_key_len); | ||
| 138 | 133 | ||
| 139 | if (!ctx->parse_private_key(ctx->priv2, &cbs)) { | 134 | if (!ctx->parse_private_key(ctx->priv2, encoded_private_key, |
| 135 | encoded_private_key_len)) { | ||
| 140 | warnx("MLKEM768_parse_private_key"); | 136 | warnx("MLKEM768_parse_private_key"); |
| 141 | failed |= 1; | 137 | failed |= 1; |
| 142 | } | 138 | } |
| @@ -210,7 +206,7 @@ mlkem768_unittest(void) | |||
| 210 | .parse_private_key = mlkem768_parse_private_key, | 206 | .parse_private_key = mlkem768_parse_private_key, |
| 211 | .parse_public_key = mlkem768_parse_public_key, | 207 | .parse_public_key = mlkem768_parse_public_key, |
| 212 | .encode_private_key = mlkem768_encode_private_key, | 208 | .encode_private_key = mlkem768_encode_private_key, |
| 213 | .encode_public_key = mlkem768_encode_public_key, | 209 | .marshal_public_key = mlkem768_marshal_public_key, |
| 214 | .public_from_private = mlkem768_public_from_private, | 210 | .public_from_private = mlkem768_public_from_private, |
| 215 | }; | 211 | }; |
| 216 | 212 | ||
| @@ -239,7 +235,7 @@ mlkem1024_unittest(void) | |||
| 239 | .parse_private_key = mlkem1024_parse_private_key, | 235 | .parse_private_key = mlkem1024_parse_private_key, |
| 240 | .parse_public_key = mlkem1024_parse_public_key, | 236 | .parse_public_key = mlkem1024_parse_public_key, |
| 241 | .encode_private_key = mlkem1024_encode_private_key, | 237 | .encode_private_key = mlkem1024_encode_private_key, |
| 242 | .encode_public_key = mlkem1024_encode_public_key, | 238 | .marshal_public_key = mlkem1024_marshal_public_key, |
| 243 | .public_from_private = mlkem1024_public_from_private, | 239 | .public_from_private = mlkem1024_public_from_private, |
| 244 | }; | 240 | }; |
| 245 | 241 | ||
