diff options
-rw-r--r-- | src/regress/lib/libcrypto/md/md_test.c | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/md/md_test.c b/src/regress/lib/libcrypto/md/md_test.c index f2b4eca33a..9854f1b665 100644 --- a/src/regress/lib/libcrypto/md/md_test.c +++ b/src/regress/lib/libcrypto/md/md_test.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: md_test.c,v 1.1.1.1 2022/09/02 13:34:48 tb Exp $ */ | 1 | /* $OpenBSD: md_test.c,v 1.2 2025/01/19 07:40:57 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2022 Joshua Sing <joshua@hypera.dev> | 3 | * Copyright (c) 2022 Joshua Sing <joshua@hypera.dev> |
4 | * | 4 | * |
@@ -200,6 +200,17 @@ md_hash_from_algorithm(int algorithm, const char **out_label, | |||
200 | return 1; | 200 | return 1; |
201 | } | 201 | } |
202 | 202 | ||
203 | static void | ||
204 | hexdump(const unsigned char *buf, size_t len) | ||
205 | { | ||
206 | size_t i; | ||
207 | |||
208 | for (i = 1; i <= len; i++) | ||
209 | fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n"); | ||
210 | |||
211 | fprintf(stderr, "\n"); | ||
212 | } | ||
213 | |||
203 | static int | 214 | static int |
204 | md_test(void) | 215 | md_test(void) |
205 | { | 216 | { |
@@ -290,12 +301,66 @@ md_test(void) | |||
290 | return failed; | 301 | return failed; |
291 | } | 302 | } |
292 | 303 | ||
304 | static int | ||
305 | md5_large_test(void) | ||
306 | { | ||
307 | MD5_CTX ctx; | ||
308 | uint8_t in[1024]; | ||
309 | uint8_t out[EVP_MAX_MD_SIZE]; | ||
310 | unsigned int out_len; | ||
311 | size_t in_len; | ||
312 | size_t i; | ||
313 | const char *label; | ||
314 | uint8_t want[] = { | ||
315 | 0xd8, 0xbc, 0xae, 0x13, 0xb5, 0x5a, 0xb0, 0xfc, | ||
316 | 0x7f, 0x8a, 0xe1, 0x78, 0x27, 0x8d, 0x44, 0x1b, | ||
317 | }; | ||
318 | int failed = 1; | ||
319 | |||
320 | memset(in, 'A', sizeof(in)); | ||
321 | in_len = sizeof(in); | ||
322 | |||
323 | memset(out, 0, sizeof(out)); | ||
324 | out_len = 16; | ||
325 | |||
326 | label = "md5"; | ||
327 | |||
328 | MD5_Init(&ctx); | ||
329 | |||
330 | for (i = 0; i < (2<<28) + 1; i += in_len) { | ||
331 | if (!MD5_Update(&ctx, in, in_len)) { | ||
332 | fprintf(stderr, "FAIL (%s): MD5_Update failed\n", label); | ||
333 | goto failed; | ||
334 | } | ||
335 | } | ||
336 | if (!MD5_Final(out, &ctx)) { | ||
337 | fprintf(stderr, "FAIL (%s): MD5_Final failed\n", label); | ||
338 | goto failed; | ||
339 | } | ||
340 | |||
341 | if (memcmp(out, want, out_len) != 0) { | ||
342 | fprintf(stderr, "FAIL (%s): MD5 mismatch\n", label); | ||
343 | hexdump(out, out_len); | ||
344 | goto failed; | ||
345 | } | ||
346 | if (ctx.Nh != 0x1 || ctx.Nl != 0x2000) { | ||
347 | fprintf(stderr, "FAIL (%s): MD5 incorrect bit length\n", label); | ||
348 | goto failed; | ||
349 | } | ||
350 | |||
351 | failed = 0; | ||
352 | |||
353 | failed: | ||
354 | return failed; | ||
355 | } | ||
356 | |||
293 | int | 357 | int |
294 | main(int argc, char **argv) | 358 | main(int argc, char **argv) |
295 | { | 359 | { |
296 | int failed = 0; | 360 | int failed = 0; |
297 | 361 | ||
298 | failed |= md_test(); | 362 | failed |= md_test(); |
363 | failed |= md5_large_test(); | ||
299 | 364 | ||
300 | return failed; | 365 | return failed; |
301 | } | 366 | } |