diff options
Diffstat (limited to 'src/microhttpd/test_md5.c')
-rw-r--r-- | src/microhttpd/test_md5.c | 95 |
1 files changed, 76 insertions, 19 deletions
diff --git a/src/microhttpd/test_md5.c b/src/microhttpd/test_md5.c index 13f9d46e..e6addab3 100644 --- a/src/microhttpd/test_md5.c +++ b/src/microhttpd/test_md5.c | |||
@@ -24,11 +24,16 @@ | |||
24 | */ | 24 | */ |
25 | 25 | ||
26 | #include "mhd_options.h" | 26 | #include "mhd_options.h" |
27 | #include "md5.h" | 27 | #include "mhd_md5_wrap.h" |
28 | #include "test_helpers.h" | 28 | #include "test_helpers.h" |
29 | #include <stdio.h> | 29 | #include <stdio.h> |
30 | #include <stdlib.h> | 30 | #include <stdlib.h> |
31 | 31 | ||
32 | #if defined(MHD_MD5_TLSLIB) && defined(MHD_HTTPS_REQUIRE_GCRYPT) | ||
33 | #define NEED_GCRYP_INIT 1 | ||
34 | #include <gcrypt.h> | ||
35 | #endif /* MHD_MD5_TLSLIB && MHD_HTTPS_REQUIRE_GCRYPT */ | ||
36 | |||
32 | static int verbose = 0; /* verbose level (0-1)*/ | 37 | static int verbose = 0; /* verbose level (0-1)*/ |
33 | 38 | ||
34 | 39 | ||
@@ -293,9 +298,9 @@ check_result (const char *test_name, | |||
293 | { | 298 | { |
294 | char calc_str[MD5_DIGEST_STRING_SIZE]; | 299 | char calc_str[MD5_DIGEST_STRING_SIZE]; |
295 | bin2hex (calculated, MD5_DIGEST_SIZE, calc_str); | 300 | bin2hex (calculated, MD5_DIGEST_SIZE, calc_str); |
296 | printf ( | 301 | printf ("PASSED: %s check %u: calculated digest %s " |
297 | "PASSED: %s check %u: calculated digest %s match expected digest.\n", | 302 | "matches expected digest.\n", |
298 | test_name, check_num, calc_str); | 303 | test_name, check_num, calc_str); |
299 | fflush (stdout); | 304 | fflush (stdout); |
300 | } | 305 | } |
301 | return failed ? 1 : 0; | 306 | return failed ? 1 : 0; |
@@ -312,19 +317,28 @@ test1_str (void) | |||
312 | { | 317 | { |
313 | unsigned int i; | 318 | unsigned int i; |
314 | int num_failed = 0; | 319 | int num_failed = 0; |
315 | struct Md5Ctx ctx; | 320 | struct Md5CtxWr ctx; |
321 | |||
322 | MHD_MD5_init_one_time (&ctx); | ||
316 | 323 | ||
317 | for (i = 0; i < units1_num; i++) | 324 | for (i = 0; i < units1_num; i++) |
318 | { | 325 | { |
319 | uint8_t digest[MD5_DIGEST_SIZE]; | 326 | uint8_t digest[MD5_DIGEST_SIZE]; |
320 | 327 | ||
321 | MHD_MD5_init (&ctx); | ||
322 | MHD_MD5_update (&ctx, (const uint8_t *) data_units1[i].str_l.str, | 328 | MHD_MD5_update (&ctx, (const uint8_t *) data_units1[i].str_l.str, |
323 | data_units1[i].str_l.len); | 329 | data_units1[i].str_l.len); |
324 | MHD_MD5_finish (&ctx, digest); | 330 | MHD_MD5_finish_reset (&ctx, digest); |
331 | #ifdef MHD_MD5_HAS_EXT_ERROR | ||
332 | if (0 != ctx.ext_error) | ||
333 | { | ||
334 | fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); | ||
335 | exit (99); | ||
336 | } | ||
337 | #endif | ||
325 | num_failed += check_result (__FUNCTION__, i, digest, | 338 | num_failed += check_result (__FUNCTION__, i, digest, |
326 | data_units1[i].digest); | 339 | data_units1[i].digest); |
327 | } | 340 | } |
341 | MHD_MD5_deinit (&ctx); | ||
328 | return num_failed; | 342 | return num_failed; |
329 | } | 343 | } |
330 | 344 | ||
@@ -334,18 +348,27 @@ test1_bin (void) | |||
334 | { | 348 | { |
335 | unsigned int i; | 349 | unsigned int i; |
336 | int num_failed = 0; | 350 | int num_failed = 0; |
337 | struct Md5Ctx ctx; | 351 | struct Md5CtxWr ctx; |
352 | |||
353 | MHD_MD5_init_one_time (&ctx); | ||
338 | 354 | ||
339 | for (i = 0; i < units2_num; i++) | 355 | for (i = 0; i < units2_num; i++) |
340 | { | 356 | { |
341 | uint8_t digest[MD5_DIGEST_SIZE]; | 357 | uint8_t digest[MD5_DIGEST_SIZE]; |
342 | 358 | ||
343 | MHD_MD5_init (&ctx); | ||
344 | MHD_MD5_update (&ctx, data_units2[i].bin_l.bin, data_units2[i].bin_l.len); | 359 | MHD_MD5_update (&ctx, data_units2[i].bin_l.bin, data_units2[i].bin_l.len); |
345 | MHD_MD5_finish (&ctx, digest); | 360 | MHD_MD5_finish_reset (&ctx, digest); |
361 | #ifdef MHD_MD5_HAS_EXT_ERROR | ||
362 | if (0 != ctx.ext_error) | ||
363 | { | ||
364 | fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); | ||
365 | exit (99); | ||
366 | } | ||
367 | #endif | ||
346 | num_failed += check_result (__FUNCTION__, i, digest, | 368 | num_failed += check_result (__FUNCTION__, i, digest, |
347 | data_units2[i].digest); | 369 | data_units2[i].digest); |
348 | } | 370 | } |
371 | MHD_MD5_deinit (&ctx); | ||
349 | return num_failed; | 372 | return num_failed; |
350 | } | 373 | } |
351 | 374 | ||
@@ -356,24 +379,33 @@ test2_str (void) | |||
356 | { | 379 | { |
357 | unsigned int i; | 380 | unsigned int i; |
358 | int num_failed = 0; | 381 | int num_failed = 0; |
359 | struct Md5Ctx ctx; | 382 | struct Md5CtxWr ctx; |
383 | |||
384 | MHD_MD5_init_one_time (&ctx); | ||
360 | 385 | ||
361 | for (i = 0; i < units1_num; i++) | 386 | for (i = 0; i < units1_num; i++) |
362 | { | 387 | { |
363 | uint8_t digest[MD5_DIGEST_SIZE]; | 388 | uint8_t digest[MD5_DIGEST_SIZE]; |
364 | size_t part_s = data_units1[i].str_l.len / 4; | 389 | size_t part_s = data_units1[i].str_l.len / 4; |
365 | 390 | ||
366 | MHD_MD5_init (&ctx); | ||
367 | MHD_MD5_update (&ctx, (const uint8_t *) "", 0); | 391 | MHD_MD5_update (&ctx, (const uint8_t *) "", 0); |
368 | MHD_MD5_update (&ctx, (const uint8_t *) data_units1[i].str_l.str, part_s); | 392 | MHD_MD5_update (&ctx, (const uint8_t *) data_units1[i].str_l.str, part_s); |
369 | MHD_MD5_update (&ctx, (const uint8_t *) "", 0); | 393 | MHD_MD5_update (&ctx, (const uint8_t *) "", 0); |
370 | MHD_MD5_update (&ctx, (const uint8_t *) data_units1[i].str_l.str + part_s, | 394 | MHD_MD5_update (&ctx, (const uint8_t *) data_units1[i].str_l.str + part_s, |
371 | data_units1[i].str_l.len - part_s); | 395 | data_units1[i].str_l.len - part_s); |
372 | MHD_MD5_update (&ctx, (const uint8_t *) "", 0); | 396 | MHD_MD5_update (&ctx, (const uint8_t *) "", 0); |
373 | MHD_MD5_finish (&ctx, digest); | 397 | MHD_MD5_finish_reset (&ctx, digest); |
398 | #ifdef MHD_MD5_HAS_EXT_ERROR | ||
399 | if (0 != ctx.ext_error) | ||
400 | { | ||
401 | fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); | ||
402 | exit (99); | ||
403 | } | ||
404 | #endif | ||
374 | num_failed += check_result (__FUNCTION__, i, digest, | 405 | num_failed += check_result (__FUNCTION__, i, digest, |
375 | data_units1[i].digest); | 406 | data_units1[i].digest); |
376 | } | 407 | } |
408 | MHD_MD5_deinit (&ctx); | ||
377 | return num_failed; | 409 | return num_failed; |
378 | } | 410 | } |
379 | 411 | ||
@@ -383,22 +415,31 @@ test2_bin (void) | |||
383 | { | 415 | { |
384 | unsigned int i; | 416 | unsigned int i; |
385 | int num_failed = 0; | 417 | int num_failed = 0; |
386 | struct Md5Ctx ctx; | 418 | struct Md5CtxWr ctx; |
419 | |||
420 | MHD_MD5_init_one_time (&ctx); | ||
387 | 421 | ||
388 | for (i = 0; i < units2_num; i++) | 422 | for (i = 0; i < units2_num; i++) |
389 | { | 423 | { |
390 | uint8_t digest[MD5_DIGEST_SIZE]; | 424 | uint8_t digest[MD5_DIGEST_SIZE]; |
391 | size_t part_s = data_units2[i].bin_l.len * 2 / 3; | 425 | size_t part_s = data_units2[i].bin_l.len * 2 / 3; |
392 | 426 | ||
393 | MHD_MD5_init (&ctx); | ||
394 | MHD_MD5_update (&ctx, data_units2[i].bin_l.bin, part_s); | 427 | MHD_MD5_update (&ctx, data_units2[i].bin_l.bin, part_s); |
395 | MHD_MD5_update (&ctx, (const uint8_t *) "", 0); | 428 | MHD_MD5_update (&ctx, (const uint8_t *) "", 0); |
396 | MHD_MD5_update (&ctx, data_units2[i].bin_l.bin + part_s, | 429 | MHD_MD5_update (&ctx, data_units2[i].bin_l.bin + part_s, |
397 | data_units2[i].bin_l.len - part_s); | 430 | data_units2[i].bin_l.len - part_s); |
398 | MHD_MD5_finish (&ctx, digest); | 431 | MHD_MD5_finish_reset (&ctx, digest); |
432 | #ifdef MHD_MD5_HAS_EXT_ERROR | ||
433 | if (0 != ctx.ext_error) | ||
434 | { | ||
435 | fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); | ||
436 | exit (99); | ||
437 | } | ||
438 | #endif | ||
399 | num_failed += check_result (__FUNCTION__, i, digest, | 439 | num_failed += check_result (__FUNCTION__, i, digest, |
400 | data_units2[i].digest); | 440 | data_units2[i].digest); |
401 | } | 441 | } |
442 | MHD_MD5_deinit (&ctx); | ||
402 | return num_failed; | 443 | return num_failed; |
403 | } | 444 | } |
404 | 445 | ||
@@ -414,7 +455,7 @@ test_unaligned (void) | |||
414 | unsigned int offset; | 455 | unsigned int offset; |
415 | uint8_t *buf; | 456 | uint8_t *buf; |
416 | uint8_t *digest_buf; | 457 | uint8_t *digest_buf; |
417 | struct Md5Ctx ctx; | 458 | struct Md5CtxWr ctx; |
418 | 459 | ||
419 | const struct data_unit2 *const tdata = data_units2 + DATA_POS; | 460 | const struct data_unit2 *const tdata = data_units2 + DATA_POS; |
420 | 461 | ||
@@ -423,6 +464,8 @@ test_unaligned (void) | |||
423 | if ((NULL == buf) || (NULL == digest_buf)) | 464 | if ((NULL == buf) || (NULL == digest_buf)) |
424 | exit (99); | 465 | exit (99); |
425 | 466 | ||
467 | MHD_MD5_init_one_time (&ctx); | ||
468 | |||
426 | for (offset = MAX_OFFSET; offset >= 1; --offset) | 469 | for (offset = MAX_OFFSET; offset >= 1; --offset) |
427 | { | 470 | { |
428 | uint8_t *unaligned_digest; | 471 | uint8_t *unaligned_digest; |
@@ -433,12 +476,19 @@ test_unaligned (void) | |||
433 | unaligned_digest = digest_buf + MAX_OFFSET - offset; | 476 | unaligned_digest = digest_buf + MAX_OFFSET - offset; |
434 | memset (unaligned_digest, 0, MD5_DIGEST_SIZE); | 477 | memset (unaligned_digest, 0, MD5_DIGEST_SIZE); |
435 | 478 | ||
436 | MHD_MD5_init (&ctx); | ||
437 | MHD_MD5_update (&ctx, unaligned_buf, tdata->bin_l.len); | 479 | MHD_MD5_update (&ctx, unaligned_buf, tdata->bin_l.len); |
438 | MHD_MD5_finish (&ctx, unaligned_digest); | 480 | MHD_MD5_finish_reset (&ctx, unaligned_digest); |
481 | #ifdef MHD_MD5_HAS_EXT_ERROR | ||
482 | if (0 != ctx.ext_error) | ||
483 | { | ||
484 | fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); | ||
485 | exit (99); | ||
486 | } | ||
487 | #endif | ||
439 | num_failed += check_result (__FUNCTION__, MAX_OFFSET - offset, | 488 | num_failed += check_result (__FUNCTION__, MAX_OFFSET - offset, |
440 | unaligned_digest, tdata->digest); | 489 | unaligned_digest, tdata->digest); |
441 | } | 490 | } |
491 | MHD_MD5_deinit (&ctx); | ||
442 | free (digest_buf); | 492 | free (digest_buf); |
443 | free (buf); | 493 | free (buf); |
444 | return num_failed; | 494 | return num_failed; |
@@ -453,6 +503,13 @@ main (int argc, char *argv[]) | |||
453 | if (has_param (argc, argv, "-v") || has_param (argc, argv, "--verbose")) | 503 | if (has_param (argc, argv, "-v") || has_param (argc, argv, "--verbose")) |
454 | verbose = 1; | 504 | verbose = 1; |
455 | 505 | ||
506 | #ifdef NEED_GCRYP_INIT | ||
507 | gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0); | ||
508 | #ifdef GCRYCTL_INITIALIZATION_FINISHED | ||
509 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); | ||
510 | #endif /* GCRYCTL_INITIALIZATION_FINISHED */ | ||
511 | #endif /* NEED_GCRYP_INIT */ | ||
512 | |||
456 | num_failed += test1_str (); | 513 | num_failed += test1_str (); |
457 | num_failed += test1_bin (); | 514 | num_failed += test1_bin (); |
458 | 515 | ||