shorthash.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <rtthread.h>
  2. #define TEST_NAME "shorthash"
  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. #define MAXLEN 64
  12. static char results[] = "\
  13. 310e0edd47db6f72\n\
  14. fd67dc93c539f874\n\
  15. 5a4fa9d909806c0d\n\
  16. 2d7efbd796666785\n\
  17. b7877127e09427cf\n\
  18. 8da699cd64557618\n\
  19. cee3fe586e46c9cb\n\
  20. 37d1018bf50002ab\n\
  21. 6224939a79f5f593\n\
  22. b0e4a90bdf82009e\n\
  23. f3b9dd94c5bb5d7a\n\
  24. a7ad6b22462fb3f4\n\
  25. fbe50e86bc8f1e75\n\
  26. 903d84c02756ea14\n\
  27. eef27a8e90ca23f7\n\
  28. e545be4961ca29a1\n\
  29. db9bc2577fcc2a3f\n\
  30. 9447be2cf5e99a69\n\
  31. 9cd38d96f0b3c14b\n\
  32. bd6179a71dc96dbb\n\
  33. 98eea21af25cd6be\n\
  34. c7673b2eb0cbf2d0\n\
  35. 883ea3e395675393\n\
  36. c8ce5ccd8c030ca8\n\
  37. 94af49f6c650adb8\n\
  38. eab8858ade92e1bc\n\
  39. f315bb5bb835d817\n\
  40. adcf6b0763612e2f\n\
  41. a5c91da7acaa4dde\n\
  42. 716595876650a2a6\n\
  43. 28ef495c53a387ad\n\
  44. 42c341d8fa92d832\n\
  45. ce7cf2722f512771\n\
  46. e37859f94623f3a7\n\
  47. 381205bb1ab0e012\n\
  48. ae97a10fd434e015\n\
  49. b4a31508beff4d31\n\
  50. 81396229f0907902\n\
  51. 4d0cf49ee5d4dcca\n\
  52. 5c73336a76d8bf9a\n\
  53. d0a704536ba93e0e\n\
  54. 925958fcd6420cad\n\
  55. a915c29bc8067318\n\
  56. 952b79f3bc0aa6d4\n\
  57. f21df2e41d4535f9\n\
  58. 87577519048f53a9\n\
  59. 10a56cf5dfcd9adb\n\
  60. eb75095ccd986cd0\n\
  61. 51a9cb9ecba312e6\n\
  62. 96afadfc2ce666c7\n\
  63. 72fe52975a4364ee\n\
  64. 5a1645b276d592a1\n\
  65. b274cb8ebf87870a\n\
  66. 6f9bb4203de7b381\n\
  67. eaecb2a30b22a87f\n\
  68. 9924a43cc1315724\n\
  69. bd838d3aafbf8db7\n\
  70. 0b1a2a3265d51aea\n\
  71. 135079a3231ce660\n\
  72. 932b2846e4d70666\n\
  73. e1915f5cb1eca46c\n\
  74. f325965ca16d629f\n\
  75. 575ff28e60381be5\n\
  76. 724506eb4c328a95\n\
  77. ";
  78. int
  79. libsodium_short_hash_test(void)
  80. {
  81. char* output = (char*) malloc(sizeof(char) * 1089);
  82. if(output == NULL)
  83. {
  84. printf("Failed to malloc memory\n");
  85. return -1;
  86. }
  87. unsigned char in[MAXLEN];
  88. unsigned char out[crypto_shorthash_BYTES];
  89. unsigned char k[crypto_shorthash_KEYBYTES];
  90. size_t i;
  91. size_t j;
  92. for (i = 0; i < crypto_shorthash_KEYBYTES; ++i) {
  93. k[i] = (unsigned char) i;
  94. }
  95. for (i = 0; i < MAXLEN; ++i) {
  96. in[i] = (unsigned char) i;
  97. crypto_shorthash(out, in, (unsigned long long) i, k);
  98. for (j = 0; j < crypto_shorthash_BYTES; ++j) {
  99. sprintf(output + strlen(output), "%02x", (unsigned int) out[j]);
  100. }
  101. sprintf(output + strlen(output), "\n");
  102. }
  103. printf("%s", output);
  104. assert(strcmp(output, results) == 0);
  105. assert(crypto_shorthash_bytes() > 0);
  106. assert(crypto_shorthash_keybytes() > 0);
  107. assert(strcmp(crypto_shorthash_primitive(), "siphash24") == 0);
  108. assert(crypto_shorthash_bytes() == crypto_shorthash_siphash24_bytes());
  109. assert(crypto_shorthash_keybytes() ==
  110. crypto_shorthash_siphash24_keybytes());
  111. printf("\n====== SHORT HASH TEST PASS ======\n");
  112. return 0;
  113. }
  114. MSH_CMD_EXPORT(libsodium_short_hash_test, libsodium short hash test)