diff options
author | tb <> | 2024-12-20 00:07:12 +0000 |
---|---|---|
committer | tb <> | 2024-12-20 00:07:12 +0000 |
commit | b1838090ad769162b2ea08f33254c67d084bb22f (patch) | |
tree | f04653a882e3c8a90b00b8e6f900e4a225736db9 /src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c | |
parent | 86a35b733e7f43fa01b1c83b3c582855e18ef432 (diff) | |
download | openbsd-b1838090ad769162b2ea08f33254c67d084bb22f.tar.gz openbsd-b1838090ad769162b2ea08f33254c67d084bb22f.tar.bz2 openbsd-b1838090ad769162b2ea08f33254c67d084bb22f.zip |
Rework and fix the mlkem tests
Make proper use of CBB and CBS. If a CBS ever owns data, you're holding
it wrong. Ditch gross macros, sscanf, and globals. The use of fgets is
annoying here, so replace it with getline, which be provided by portable
if needed.
Most importantly, make the tests actually signal failure rather than
only printing an error. Fix the state machines in a few of them. Some
tests didn't parse the .txt file at all. Others mostly did but didn't
actually test what they were supposed to be testing. Such failures
were hidden by the way the tests were written.
This basically needed a complete revamp. It still isn't pretty and much
of it could be deduplicated, but I only have so much time alotted on this
blue planet.
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c')
-rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c | 194 |
1 files changed, 169 insertions, 25 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c index c4c13f0e7a..50a93027e2 100644 --- a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c +++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c | |||
@@ -1,7 +1,8 @@ | |||
1 | /* $OpenBSD: mlkem_tests_util.c,v 1.2 2024/12/14 19:16:24 tb Exp $ */ | 1 | /* $OpenBSD: mlkem_tests_util.c,v 1.3 2024/12/20 00:07:12 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> |
5 | * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> | ||
5 | * | 6 | * |
6 | * Permission to use, copy, modify, and/or distribute this software for any | 7 | * Permission to use, copy, modify, and/or distribute this software for any |
7 | * purpose with or without fee is hereby granted, provided that the above | 8 | * purpose with or without fee is hereby granted, provided that the above |
@@ -19,15 +20,19 @@ | |||
19 | #include <err.h> | 20 | #include <err.h> |
20 | #include <stdint.h> | 21 | #include <stdint.h> |
21 | #include <stdio.h> | 22 | #include <stdio.h> |
22 | #include <stdlib.h> | ||
23 | #include <string.h> | 23 | #include <string.h> |
24 | 24 | ||
25 | #include "bytestring.h" | ||
26 | #include "mlkem.h" | ||
27 | |||
28 | #include "mlkem_internal.h" | ||
29 | |||
25 | #include "mlkem_tests_util.h" | 30 | #include "mlkem_tests_util.h" |
26 | 31 | ||
27 | int failure; | 32 | int failure; |
28 | int test_number; | 33 | int test_number; |
29 | 34 | ||
30 | void | 35 | static void |
31 | hexdump(const uint8_t *buf, size_t len, const uint8_t *compare) | 36 | hexdump(const uint8_t *buf, size_t len, const uint8_t *compare) |
32 | { | 37 | { |
33 | const char *mark = ""; | 38 | const char *mark = ""; |
@@ -43,32 +48,171 @@ hexdump(const uint8_t *buf, size_t len, const uint8_t *compare) | |||
43 | } | 48 | } |
44 | 49 | ||
45 | int | 50 | int |
46 | hex_decode(char *buf, size_t len, uint8_t **out_buf, size_t *out_len) | 51 | compare_data(const uint8_t *want, const uint8_t *got, size_t len, size_t line, |
52 | const char *msg) | ||
47 | { | 53 | { |
48 | size_t i; | 54 | if (memcmp(want, got, len) == 0) |
49 | if (*out_buf != NULL) | 55 | return 0; |
50 | abort(); /* Du hast einin rotweinflarsche... */ | ||
51 | 56 | ||
52 | MALLOC(*out_buf, len); | 57 | warnx("FAIL: #%zu - %s differs", line, msg); |
53 | *out_len = 0; | 58 | fprintf(stderr, "want:\n"); |
59 | hexdump(want, len, got); | ||
60 | fprintf(stderr, "got:\n"); | ||
61 | hexdump(got, len, want); | ||
62 | fprintf(stderr, "\n"); | ||
54 | 63 | ||
55 | for (i = 0; i < len; i += 2) { | ||
56 | if (sscanf(buf + i, "%2hhx", *out_buf + *out_len) != 1) | ||
57 | err(1, "FAIL- hex decode failed for %d\n", | ||
58 | (int)*out_len); | ||
59 | (*out_len)++; | ||
60 | } | ||
61 | return 1; | 64 | return 1; |
62 | } | 65 | } |
63 | 66 | ||
67 | int | ||
68 | compare_length(size_t want, size_t got, size_t line, const char *msg) | ||
69 | { | ||
70 | if (want == got) | ||
71 | return 1; | ||
72 | |||
73 | warnx("#%zu: %s: want %zu, got %zu", line, msg, want, got); | ||
74 | return 0; | ||
75 | } | ||
76 | |||
77 | static int | ||
78 | hex_get_nibble_cbs(CBS *cbs, uint8_t *out_nibble) | ||
79 | { | ||
80 | uint8_t c; | ||
81 | |||
82 | if (!CBS_get_u8(cbs, &c)) | ||
83 | return 0; | ||
84 | |||
85 | if (c >= '0' && c <= '9') { | ||
86 | *out_nibble = c - '0'; | ||
87 | return 1; | ||
88 | } | ||
89 | if (c >= 'a' && c <= 'f') { | ||
90 | *out_nibble = c - 'a' + 10; | ||
91 | return 1; | ||
92 | } | ||
93 | if (c >= 'A' && c <= 'F') { | ||
94 | *out_nibble = c - 'A' + 10; | ||
95 | return 1; | ||
96 | } | ||
97 | |||
98 | return 0; | ||
99 | } | ||
100 | |||
64 | void | 101 | void |
65 | grab_data(CBS *cbs, char *buf, size_t offset) | 102 | hex_decode_cbs(CBS *cbs, CBB *cbb, size_t line, const char *msg) |
103 | { | ||
104 | if (!CBB_init(cbb, 0)) | ||
105 | errx(1, "#%zu %s: %s CBB_init", line, msg, __func__); | ||
106 | |||
107 | while (CBS_len(cbs) > 0) { | ||
108 | uint8_t hi, lo; | ||
109 | |||
110 | if (!hex_get_nibble_cbs(cbs, &hi)) | ||
111 | errx(1, "#%zu %s: %s nibble", line, msg, __func__); | ||
112 | if (!hex_get_nibble_cbs(cbs, &lo)) | ||
113 | errx(1, "#%zu %s: %s nibble", line, msg, __func__); | ||
114 | |||
115 | if (!CBB_add_u8(cbb, hi << 4 | lo)) | ||
116 | errx(1, "#%zu %s: %s CBB_add_u8", line, msg, __func__); | ||
117 | } | ||
118 | } | ||
119 | |||
120 | int | ||
121 | get_string_cbs(CBS *cbs_in, const char *str, size_t line, const char *msg) | ||
122 | { | ||
123 | CBS cbs; | ||
124 | size_t len = strlen(str); | ||
125 | |||
126 | if (!CBS_get_bytes(cbs_in, &cbs, len)) | ||
127 | errx(1, "#%zu %s: %s CBB_get_bytes", line, msg, __func__); | ||
128 | |||
129 | return CBS_mem_equal(&cbs, str, len); | ||
130 | } | ||
131 | |||
132 | int | ||
133 | mlkem768_encode_private_key(const struct MLKEM768_private_key *priv, | ||
134 | uint8_t **out_buf, size_t *out_len) | ||
135 | { | ||
136 | CBB cbb; | ||
137 | int ret = 0; | ||
138 | |||
139 | if (!CBB_init(&cbb, MLKEM768_PUBLIC_KEY_BYTES)) | ||
140 | goto err; | ||
141 | if (!MLKEM768_marshal_private_key(&cbb, priv)) | ||
142 | goto err; | ||
143 | if (!CBB_finish(&cbb, out_buf, out_len)) | ||
144 | goto err; | ||
145 | |||
146 | ret = 1; | ||
147 | |||
148 | err: | ||
149 | CBB_cleanup(&cbb); | ||
150 | |||
151 | return ret; | ||
152 | } | ||
153 | |||
154 | int | ||
155 | mlkem768_encode_public_key(const struct MLKEM768_public_key *pub, | ||
156 | uint8_t **out_buf, size_t *out_len) | ||
66 | { | 157 | { |
67 | char *start = buf + offset; | 158 | CBB cbb; |
68 | size_t len = strlen(start); | 159 | int ret = 0; |
69 | uint8_t *new = NULL; | 160 | |
70 | size_t new_len = 0; | 161 | if (!CBB_init(&cbb, MLKEM768_PUBLIC_KEY_BYTES)) |
71 | /* This is hex encoded - decode it. */ | 162 | goto err; |
72 | TEST(!hex_decode(start, len - 1, &new, &new_len), "hex decode failed"); | 163 | if (!MLKEM768_marshal_public_key(&cbb, pub)) |
73 | CBS_init(cbs, new, new_len); | 164 | goto err; |
165 | if (!CBB_finish(&cbb, out_buf, out_len)) | ||
166 | goto err; | ||
167 | |||
168 | ret = 1; | ||
169 | |||
170 | err: | ||
171 | CBB_cleanup(&cbb); | ||
172 | |||
173 | return ret; | ||
174 | } | ||
175 | |||
176 | int | ||
177 | mlkem1024_encode_private_key(const struct MLKEM1024_private_key *priv, | ||
178 | uint8_t **out_buf, size_t *out_len) | ||
179 | { | ||
180 | CBB cbb; | ||
181 | int ret = 0; | ||
182 | |||
183 | if (!CBB_init(&cbb, MLKEM1024_PUBLIC_KEY_BYTES)) | ||
184 | goto err; | ||
185 | if (!MLKEM1024_marshal_private_key(&cbb, priv)) | ||
186 | goto err; | ||
187 | if (!CBB_finish(&cbb, out_buf, out_len)) | ||
188 | goto err; | ||
189 | |||
190 | ret = 1; | ||
191 | |||
192 | err: | ||
193 | CBB_cleanup(&cbb); | ||
194 | |||
195 | return ret; | ||
196 | } | ||
197 | |||
198 | int | ||
199 | mlkem1024_encode_public_key(const struct MLKEM1024_public_key *pub, | ||
200 | uint8_t **out_buf, size_t *out_len) | ||
201 | { | ||
202 | CBB cbb; | ||
203 | int ret = 0; | ||
204 | |||
205 | if (!CBB_init(&cbb, MLKEM1024_PUBLIC_KEY_BYTES)) | ||
206 | goto err; | ||
207 | if (!MLKEM1024_marshal_public_key(&cbb, pub)) | ||
208 | goto err; | ||
209 | if (!CBB_finish(&cbb, out_buf, out_len)) | ||
210 | goto err; | ||
211 | |||
212 | ret = 1; | ||
213 | |||
214 | err: | ||
215 | CBB_cleanup(&cbb); | ||
216 | |||
217 | return ret; | ||
74 | } | 218 | } |