diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-11-01 20:47:52 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-11-01 20:47:52 +0000 |
commit | 652e89b59ed2207c2c12172fdabcd6e659995c81 (patch) | |
tree | f054c819d483c1056e18c1099afd4c7fcd2582a0 /src/util/crypto_hash.c | |
parent | 5e4113e83368849500792e57946c3d8dd9e548d8 (diff) | |
download | gnunet-652e89b59ed2207c2c12172fdabcd6e659995c81.tar.gz gnunet-652e89b59ed2207c2c12172fdabcd6e659995c81.zip |
fixing bio testcase and a bug in bio.c, also indenting
Diffstat (limited to 'src/util/crypto_hash.c')
-rw-r--r-- | src/util/crypto_hash.c | 66 |
1 files changed, 31 insertions, 35 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c index 60e0ec5a2..3f5869489 100644 --- a/src/util/crypto_hash.c +++ b/src/util/crypto_hash.c | |||
@@ -359,12 +359,10 @@ sha512_final (struct sha512_ctx *sctx, unsigned char *hash) | |||
359 | * @param ret pointer to where to write the hashcode | 359 | * @param ret pointer to where to write the hashcode |
360 | */ | 360 | */ |
361 | void | 361 | void |
362 | GNUNET_CRYPTO_hash (const void *block, | 362 | GNUNET_CRYPTO_hash (const void *block, size_t size, GNUNET_HashCode * ret) |
363 | size_t size, | ||
364 | GNUNET_HashCode * ret) | ||
365 | { | 363 | { |
366 | struct sha512_ctx ctx; | 364 | struct sha512_ctx ctx; |
367 | 365 | ||
368 | sha512_init (&ctx); | 366 | sha512_init (&ctx); |
369 | sha512_update (&ctx, block, size); | 367 | sha512_update (&ctx, block, size); |
370 | sha512_final (&ctx, (unsigned char *) ret); | 368 | sha512_final (&ctx, (unsigned char *) ret); |
@@ -416,7 +414,7 @@ struct FileHashContext | |||
416 | * Current offset. | 414 | * Current offset. |
417 | */ | 415 | */ |
418 | uint64_t offset; | 416 | uint64_t offset; |
419 | 417 | ||
420 | /** | 418 | /** |
421 | * Run on shutdown? | 419 | * Run on shutdown? |
422 | */ | 420 | */ |
@@ -480,8 +478,7 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
480 | GNUNET_SCHEDULER_add_after (tc->sched, | 478 | GNUNET_SCHEDULER_add_after (tc->sched, |
481 | fhc->run_on_shutdown, | 479 | fhc->run_on_shutdown, |
482 | GNUNET_SCHEDULER_PRIORITY_KEEP, | 480 | GNUNET_SCHEDULER_PRIORITY_KEEP, |
483 | GNUNET_SCHEDULER_NO_TASK, | 481 | GNUNET_SCHEDULER_NO_TASK, &file_hash_task, fhc); |
484 | &file_hash_task, fhc); | ||
485 | } | 482 | } |
486 | 483 | ||
487 | 484 | ||
@@ -523,8 +520,8 @@ GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched, | |||
523 | } | 520 | } |
524 | fhc->run_on_shutdown = run_on_shutdown; | 521 | fhc->run_on_shutdown = run_on_shutdown; |
525 | fhc->fh = GNUNET_DISK_file_open (filename, | 522 | fhc->fh = GNUNET_DISK_file_open (filename, |
526 | GNUNET_DISK_OPEN_READ, | 523 | GNUNET_DISK_OPEN_READ, |
527 | GNUNET_DISK_PERM_NONE); | 524 | GNUNET_DISK_PERM_NONE); |
528 | if (!fhc->fh) | 525 | if (!fhc->fh) |
529 | { | 526 | { |
530 | file_hash_finish (fhc, NULL); | 527 | file_hash_finish (fhc, NULL); |
@@ -533,8 +530,7 @@ GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched, | |||
533 | GNUNET_SCHEDULER_add_after (sched, | 530 | GNUNET_SCHEDULER_add_after (sched, |
534 | run_on_shutdown, | 531 | run_on_shutdown, |
535 | priority, | 532 | priority, |
536 | GNUNET_SCHEDULER_NO_TASK, | 533 | GNUNET_SCHEDULER_NO_TASK, &file_hash_task, fhc); |
537 | &file_hash_task, fhc); | ||
538 | } | 534 | } |
539 | 535 | ||
540 | 536 | ||
@@ -662,18 +658,18 @@ GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a, | |||
662 | } | 658 | } |
663 | 659 | ||
664 | void | 660 | void |
665 | GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, GNUNET_HashCode * result) | 661 | GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode, |
662 | GNUNET_HashCode * result) | ||
666 | { | 663 | { |
667 | int i; | 664 | int i; |
668 | for (i = (sizeof (GNUNET_HashCode) / sizeof (uint32_t)) - 1; i >= 0; | 665 | for (i = (sizeof (GNUNET_HashCode) / sizeof (uint32_t)) - 1; i >= 0; i--) |
669 | i--) | 666 | result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, (uint32_t) - 1); |
670 | result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, (uint32_t)-1); | ||
671 | } | 667 | } |
672 | 668 | ||
673 | void | 669 | void |
674 | GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, | 670 | GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, |
675 | const GNUNET_HashCode * b, | 671 | const GNUNET_HashCode * b, |
676 | GNUNET_HashCode * result) | 672 | GNUNET_HashCode * result) |
677 | { | 673 | { |
678 | int i; | 674 | int i; |
679 | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; | 675 | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; |
@@ -683,8 +679,8 @@ GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a, | |||
683 | 679 | ||
684 | void | 680 | void |
685 | GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, | 681 | GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, |
686 | const GNUNET_HashCode * delta, | 682 | const GNUNET_HashCode * delta, |
687 | GNUNET_HashCode * result) | 683 | GNUNET_HashCode * result) |
688 | { | 684 | { |
689 | int i; | 685 | int i; |
690 | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; | 686 | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; |
@@ -694,7 +690,7 @@ GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a, | |||
694 | 690 | ||
695 | void | 691 | void |
696 | GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, | 692 | GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, |
697 | const GNUNET_HashCode * b, GNUNET_HashCode * result) | 693 | const GNUNET_HashCode * b, GNUNET_HashCode * result) |
698 | { | 694 | { |
699 | int i; | 695 | int i; |
700 | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; | 696 | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; |
@@ -707,18 +703,18 @@ GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, | |||
707 | */ | 703 | */ |
708 | void | 704 | void |
709 | GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc, | 705 | GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc, |
710 | struct GNUNET_CRYPTO_AesSessionKey *skey, | 706 | struct GNUNET_CRYPTO_AesSessionKey *skey, |
711 | struct GNUNET_CRYPTO_AesInitializationVector | 707 | struct GNUNET_CRYPTO_AesInitializationVector |
712 | *iv) | 708 | *iv) |
713 | { | 709 | { |
714 | GNUNET_assert (sizeof (GNUNET_HashCode) >= | 710 | GNUNET_assert (sizeof (GNUNET_HashCode) >= |
715 | GNUNET_CRYPTO_AES_KEY_LENGTH + | 711 | GNUNET_CRYPTO_AES_KEY_LENGTH + |
716 | sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); | 712 | sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); |
717 | memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH); | 713 | memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH); |
718 | skey->crc32 = | 714 | skey->crc32 = |
719 | htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH)); | 715 | htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH)); |
720 | memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH], | 716 | memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH], |
721 | sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); | 717 | sizeof (struct GNUNET_CRYPTO_AesInitializationVector)); |
722 | } | 718 | } |
723 | 719 | ||
724 | /** | 720 | /** |
@@ -741,21 +737,21 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit) | |||
741 | */ | 737 | */ |
742 | int | 738 | int |
743 | GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, | 739 | GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, |
744 | const GNUNET_HashCode * h2) | 740 | const GNUNET_HashCode * h2) |
745 | { | 741 | { |
746 | unsigned int *i1; | 742 | unsigned int *i1; |
747 | unsigned int *i2; | 743 | unsigned int *i2; |
748 | int i; | 744 | int i; |
749 | 745 | ||
750 | i1 = (unsigned int *) h1; | 746 | i1 = (unsigned int *) h1; |
751 | i2 = (unsigned int *) h2; | 747 | i2 = (unsigned int *) h2; |
752 | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; | 748 | for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; |
753 | i--) | 749 | i--) |
754 | { | 750 | { |
755 | if (i1[i] > i2[i]) | 751 | if (i1[i] > i2[i]) |
756 | return 1; | 752 | return 1; |
757 | if (i1[i] < i2[i]) | 753 | if (i1[i] < i2[i]) |
758 | return -1; | 754 | return -1; |
759 | } | 755 | } |
760 | return 0; | 756 | return 0; |
761 | } | 757 | } |
@@ -767,21 +763,21 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, | |||
767 | */ | 763 | */ |
768 | int | 764 | int |
769 | GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1, | 765 | GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1, |
770 | const GNUNET_HashCode * h2, | 766 | const GNUNET_HashCode * h2, |
771 | const GNUNET_HashCode * target) | 767 | const GNUNET_HashCode * target) |
772 | { | 768 | { |
773 | int i; | 769 | int i; |
774 | unsigned int d1; | 770 | unsigned int d1; |
775 | unsigned int d2; | 771 | unsigned int d2; |
776 | 772 | ||
777 | for (i = sizeof (GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--) | 773 | for (i = sizeof (GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--) |
778 | { | 774 | { |
779 | d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i]; | 775 | d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i]; |
780 | d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i]; | 776 | d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i]; |
781 | if (d1 > d2) | 777 | if (d1 > d2) |
782 | return 1; | 778 | return 1; |
783 | else if (d1 < d2) | 779 | else if (d1 < d2) |
784 | return -1; | 780 | return -1; |
785 | } | 781 | } |
786 | return 0; | 782 | return 0; |
787 | } | 783 | } |