diff options
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem_unittest.c')
-rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_unittest.c | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c b/src/regress/lib/libcrypto/mlkem/mlkem_unittest.c index 23b3d8b261..adb1c47d8e 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.6 2024/12/26 12:35:25 tb Exp $ */ | 1 | /* $OpenBSD: mlkem_unittest.c,v 1.11 2025/05/21 03:46:20 tb 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> |
@@ -41,8 +41,8 @@ struct unittest_ctx { | |||
41 | mlkem_generate_key_fn generate_key; | 41 | mlkem_generate_key_fn generate_key; |
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_marshal_private_key_fn marshal_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) { |
@@ -117,7 +113,7 @@ MlKemUnitTest(struct unittest_ctx *ctx) | |||
117 | free(tmp_buf); | 113 | free(tmp_buf); |
118 | tmp_buf = NULL; | 114 | tmp_buf = NULL; |
119 | 115 | ||
120 | if (!ctx->encode_private_key(ctx->priv, &encoded_private_key, | 116 | if (!ctx->marshal_private_key(ctx->priv, &encoded_private_key, |
121 | &encoded_private_key_len)) { | 117 | &encoded_private_key_len)) { |
122 | warnx("mlkem768_encode_private_key"); | 118 | warnx("mlkem768_encode_private_key"); |
123 | failed |= 1; | 119 | failed |= 1; |
@@ -125,23 +121,23 @@ 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 | } |
143 | 139 | ||
144 | if (!ctx->encode_private_key(ctx->priv2, &tmp_buf, &tmp_buf_len)) { | 140 | if (!ctx->marshal_private_key(ctx->priv2, &tmp_buf, &tmp_buf_len)) { |
145 | warnx("encode_private_key"); | 141 | warnx("encode_private_key"); |
146 | failed |= 1; | 142 | failed |= 1; |
147 | } | 143 | } |
@@ -161,16 +157,22 @@ MlKemUnitTest(struct unittest_ctx *ctx) | |||
161 | tmp_buf = NULL; | 157 | tmp_buf = NULL; |
162 | 158 | ||
163 | ctx->encap(ctx->ciphertext, shared_secret1, ctx->pub); | 159 | ctx->encap(ctx->ciphertext, shared_secret1, ctx->pub); |
164 | ctx->decap(shared_secret2, ctx->ciphertext, ctx->ciphertext_len, | 160 | if (!ctx->decap(shared_secret2, ctx->ciphertext, ctx->ciphertext_len, |
165 | ctx->priv); | 161 | ctx->priv)) { |
162 | warnx("decap() failed using priv"); | ||
163 | failed |= 1; | ||
164 | } | ||
166 | if (compare_data(shared_secret1, shared_secret2, MLKEM_SHARED_SECRET_BYTES, | 165 | if (compare_data(shared_secret1, shared_secret2, MLKEM_SHARED_SECRET_BYTES, |
167 | "shared secrets with priv") != 0) { | 166 | "shared secrets with priv") != 0) { |
168 | warnx("compare_data"); | 167 | warnx("compare_data"); |
169 | failed |= 1; | 168 | failed |= 1; |
170 | } | 169 | } |
171 | 170 | ||
172 | ctx->decap(shared_secret2, ctx->ciphertext, ctx->ciphertext_len, | 171 | if (!ctx->decap(shared_secret2, ctx->ciphertext, ctx->ciphertext_len, |
173 | ctx->priv2); | 172 | ctx->priv2)) { |
173 | warnx("decap() failed using priv2"); | ||
174 | failed |= 1; | ||
175 | } | ||
174 | if (compare_data(shared_secret1, shared_secret2, MLKEM_SHARED_SECRET_BYTES, | 176 | if (compare_data(shared_secret1, shared_secret2, MLKEM_SHARED_SECRET_BYTES, |
175 | "shared secrets with priv2") != 0) { | 177 | "shared secrets with priv2") != 0) { |
176 | warnx("compare_data"); | 178 | warnx("compare_data"); |
@@ -203,8 +205,8 @@ mlkem768_unittest(void) | |||
203 | .generate_key = mlkem768_generate_key, | 205 | .generate_key = mlkem768_generate_key, |
204 | .parse_private_key = mlkem768_parse_private_key, | 206 | .parse_private_key = mlkem768_parse_private_key, |
205 | .parse_public_key = mlkem768_parse_public_key, | 207 | .parse_public_key = mlkem768_parse_public_key, |
206 | .encode_private_key = mlkem768_encode_private_key, | 208 | .marshal_private_key = mlkem768_marshal_private_key, |
207 | .encode_public_key = mlkem768_encode_public_key, | 209 | .marshal_public_key = mlkem768_marshal_public_key, |
208 | .public_from_private = mlkem768_public_from_private, | 210 | .public_from_private = mlkem768_public_from_private, |
209 | }; | 211 | }; |
210 | 212 | ||
@@ -232,8 +234,8 @@ mlkem1024_unittest(void) | |||
232 | .generate_key = mlkem1024_generate_key, | 234 | .generate_key = mlkem1024_generate_key, |
233 | .parse_private_key = mlkem1024_parse_private_key, | 235 | .parse_private_key = mlkem1024_parse_private_key, |
234 | .parse_public_key = mlkem1024_parse_public_key, | 236 | .parse_public_key = mlkem1024_parse_public_key, |
235 | .encode_private_key = mlkem1024_encode_private_key, | 237 | .marshal_private_key = mlkem1024_marshal_private_key, |
236 | .encode_public_key = mlkem1024_encode_public_key, | 238 | .marshal_public_key = mlkem1024_marshal_public_key, |
237 | .public_from_private = mlkem1024_public_from_private, | 239 | .public_from_private = mlkem1024_public_from_private, |
238 | }; | 240 | }; |
239 | 241 | ||
@@ -248,7 +250,7 @@ main(void) | |||
248 | /* | 250 | /* |
249 | * XXX - this is split into two helper functions since having a few | 251 | * XXX - this is split into two helper functions since having a few |
250 | * ML-KEM key blobs on the stack makes Emscripten's stack explode, | 252 | * ML-KEM key blobs on the stack makes Emscripten's stack explode, |
251 | * leading to inscrutable silent failures unles ASAN is enabled. | 253 | * leading to inscrutable silent failures unless ASAN is enabled. |
252 | * Go figure. | 254 | * Go figure. |
253 | */ | 255 | */ |
254 | 256 | ||