try.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * crypto_hashblocks/try.c version 20090118
  3. * D. J. Bernstein
  4. * Public domain.
  5. */
  6. #include <stdlib.h>
  7. #include "crypto_hashblocks.h"
  8. extern unsigned char *alignedcalloc(unsigned long long);
  9. const char *primitiveimplementation = crypto_hashblocks_IMPLEMENTATION;
  10. #define MAXTEST_BYTES (10000 + crypto_hashblocks_STATEBYTES)
  11. #define CHECKSUM_BYTES 4096
  12. #define TUNE_BYTES 1536
  13. static unsigned char *h;
  14. static unsigned char *h2;
  15. static unsigned char *m;
  16. static unsigned char *m2;
  17. void preallocate(void)
  18. {
  19. }
  20. void allocate(void)
  21. {
  22. h = alignedcalloc(crypto_hashblocks_STATEBYTES);
  23. h2 = alignedcalloc(crypto_hashblocks_STATEBYTES);
  24. m = alignedcalloc(MAXTEST_BYTES);
  25. m2 = alignedcalloc(MAXTEST_BYTES);
  26. }
  27. void predoit(void)
  28. {
  29. }
  30. void doit(void)
  31. {
  32. crypto_hashblocks(h,m,TUNE_BYTES);
  33. }
  34. char checksum[crypto_hashblocks_STATEBYTES * 2 + 1];
  35. const char *checksum_compute(void)
  36. {
  37. long long i;
  38. long long j;
  39. for (i = 0;i < CHECKSUM_BYTES;++i) {
  40. long long hlen = crypto_hashblocks_STATEBYTES;
  41. long long mlen = i;
  42. for (j = -16;j < 0;++j) h[j] = random();
  43. for (j = hlen;j < hlen + 16;++j) h[j] = random();
  44. for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
  45. for (j = -16;j < 0;++j) m[j] = random();
  46. for (j = mlen;j < mlen + 16;++j) m[j] = random();
  47. for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
  48. if (crypto_hashblocks(h,m,mlen) != 0) return "crypto_hashblocks returns nonzero";
  49. for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_hashblocks writes to input";
  50. for (j = -16;j < 0;++j) if (h2[j] != h[j]) return "crypto_hashblocks writes before output";
  51. for (j = hlen;j < hlen + 16;++j) if (h2[j] != h[j]) return "crypto_hashblocks writes after output";
  52. for (j = 0;j < hlen;++j) m2[j] = h2[j];
  53. if (crypto_hashblocks(h2,m2,mlen) != 0) return "crypto_hashblocks returns nonzero";
  54. if (crypto_hashblocks(m2,m2,mlen) != 0) return "crypto_hashblocks returns nonzero";
  55. for (j = 0;j < hlen;++j) if (m2[j] != h2[j]) return "crypto_hashblocks does not handle overlap";
  56. for (j = 0;j < mlen;++j) m[j] ^= h[j % hlen];
  57. m[mlen] = h[0];
  58. }
  59. if (crypto_hashblocks(h,m,CHECKSUM_BYTES) != 0) return "crypto_hashblocks returns nonzero";
  60. for (i = 0;i < crypto_hashblocks_STATEBYTES;++i) {
  61. checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)];
  62. checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]];
  63. }
  64. checksum[2 * i] = 0;
  65. return 0;
  66. }