hash.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <rtthread.h>
  2. #define TEST_NAME "hash"
  3. #include <assert.h>
  4. #include <errno.h>
  5. #include <limits.h>
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "sodium.h"
  11. #include "quirks.h"
  12. static unsigned char x[] = "testing\n";
  13. static unsigned char x2[] =
  14. "The Conscience of a Hacker is a small essay written January 8, 1986 by a "
  15. "computer security hacker who went by the handle of The Mentor, who "
  16. "belonged to the 2nd generation of Legion of Doom.";
  17. static unsigned char h[crypto_hash_BYTES];
  18. // Ground Truth
  19. static unsigned int h_x_sha512[crypto_hash_BYTES] = {0x24, 0xf9, 0x50, 0xaa, 0xc7, 0xb9, 0xea, 0x9b, 0x3c, 0xb7, 0x28, 0x22, 0x8a, 0x0c, 0x82, 0xb6, 0x7c, 0x39, 0xe9, 0x6b, 0x4b, 0x34, 0x47, 0x98, 0x87, 0x0d, 0x5d, 0xae, 0xe9, 0x3e, 0x3a, 0xe5, 0x93, 0x1b, 0xaa, 0xe8, 0xc7, 0xca, 0xcf, 0xea, 0x4b, 0x62, 0x94, 0x52, 0xc3, 0x80, 0x26, 0xa8, 0x1d, 0x13, 0x8b, 0xc7, 0xaa, 0xd1, 0xaf, 0x3e, 0xf7, 0xbf, 0xd5, 0xec, 0x64, 0x6d, 0x6c, 0x28};
  20. static unsigned int h_x2_sha512[crypto_hash_BYTES] = {0xa7, 0x7a, 0xbe, 0x1c, 0xcf, 0x8f, 0x54, 0x97, 0xe2, 0x28, 0xfb, 0xc0, 0xac, 0xd7, 0x3a, 0x52, 0x1e, 0xde, 0xdb, 0x21, 0xb8, 0x97, 0x26, 0x68, 0x4a, 0x6e, 0xbb, 0xc3, 0xba, 0xa3, 0x23, 0x61, 0xac, 0xa5, 0xa2, 0x44, 0xda, 0xa8, 0x4f, 0x24, 0xbf, 0x19, 0xc6, 0x8b, 0xaf, 0x78, 0xe6, 0x90, 0x76, 0x25, 0xa6, 0x59, 0xb1, 0x54, 0x79, 0xeb, 0x7b, 0xd4, 0x26, 0xfc, 0x62, 0xaa, 0xfa, 0x73};
  21. static unsigned int h_x_sha256[crypto_hash_sha256_BYTES] = {0x12, 0xa6, 0x1f, 0x4e, 0x17, 0x3f, 0xb3, 0xa1, 0x1c, 0x05, 0xd6, 0x47, 0x1f, 0x74, 0x72, 0x8f, 0x76, 0x23, 0x1b, 0x4a, 0x5f, 0xcd, 0x96, 0x67, 0xce, 0xf3, 0xaf, 0x87, 0xa3, 0xae, 0x4d, 0xc2};
  22. static unsigned int h_x2_sha256[crypto_hash_sha256_BYTES] = {0x71, 0xcc, 0x81, 0x23, 0xfe, 0xf8, 0xc2, 0x36, 0xe4, 0x51, 0xd3, 0xc3, 0xdd, 0xf1, 0xad, 0xae, 0x9a, 0xa6, 0xcd, 0x95, 0x21, 0xe7, 0x04, 0x17, 0x69, 0xd7, 0x37, 0x02, 0x49, 0x00, 0xa0, 0x3a};
  23. int libsodium_hash_test(void)
  24. {
  25. size_t i;
  26. // SHA512
  27. crypto_hash(h, x, sizeof x - 1U);
  28. printf("\nsha512(\"testing\\n\") = ");
  29. for (i = 0; i < crypto_hash_BYTES; ++i) {
  30. printf("%02x", (unsigned int) h[i]);
  31. assert((unsigned int)h[i] == h_x_sha512[i]);
  32. }
  33. printf("\n");
  34. crypto_hash(h, x2, sizeof(x2) - 1U);
  35. printf("\nsha512(\"%s\") = ", x2);
  36. for (i = 0; i < crypto_hash_BYTES; ++i) {
  37. printf("%02x", (unsigned int) h[i]);
  38. assert((unsigned int)h[i] == h_x2_sha512[i]);
  39. }
  40. printf("\n");
  41. // SHA256
  42. crypto_hash_sha256(h, x, sizeof(x) - 1U);
  43. printf("\nsha256(\"testing\\n\") = ");
  44. for (i = 0; i < crypto_hash_sha256_BYTES; ++i) {
  45. printf("%02x", (unsigned int) h[i]);
  46. assert((unsigned int)h[i] == h_x_sha256[i]);
  47. }
  48. printf("\n");
  49. crypto_hash_sha256(h, x2, sizeof(x2) - 1U);
  50. printf("\nsha256(\"%s\") = ", x2);
  51. for (i = 0; i < crypto_hash_sha256_BYTES; ++i) {
  52. printf("%02x", (unsigned int) h[i]);
  53. assert((unsigned int)h[i] == h_x2_sha256[i]);
  54. }
  55. printf("\n");
  56. assert(crypto_hash_bytes() > 0U);
  57. assert(strcmp(crypto_hash_primitive(), "sha512") == 0);
  58. assert(crypto_hash_sha256_bytes() > 0U);
  59. assert(crypto_hash_sha512_bytes() >= crypto_hash_sha256_bytes());
  60. assert(crypto_hash_sha512_bytes() == crypto_hash_bytes());
  61. assert(crypto_hash_sha256_statebytes() == sizeof(crypto_hash_sha256_state));
  62. assert(crypto_hash_sha512_statebytes() == sizeof(crypto_hash_sha512_state));
  63. printf("\n====== HASH TEST PASS ======\n");
  64. return 0;
  65. }
  66. MSH_CMD_EXPORT(libsodium_hash_test, libsodium hash test)