1
0

try.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * crypto_hash/try.c version 20090118
  3. * D. J. Bernstein
  4. * Public domain.
  5. */
  6. #include <stdlib.h>
  7. #include "crypto_hash.h"
  8. extern unsigned char *alignedcalloc(unsigned long long);
  9. const char *primitiveimplementation = crypto_hash_IMPLEMENTATION;
  10. #define MAXTEST_BYTES (10000 + crypto_hash_BYTES)
  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_hash_BYTES);
  23. h2 = alignedcalloc(crypto_hash_BYTES);
  24. m = alignedcalloc(MAXTEST_BYTES);
  25. m2 = alignedcalloc(MAXTEST_BYTES);
  26. }
  27. void predoit(void)
  28. {
  29. }
  30. void doit(void)
  31. {
  32. crypto_hash(h,m,TUNE_BYTES);
  33. }
  34. char checksum[crypto_hash_BYTES * 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_hash_BYTES;
  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_hash(h,m,mlen) != 0) return "crypto_hash returns nonzero";
  49. for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_hash writes to input";
  50. for (j = -16;j < 0;++j) if (h2[j] != h[j]) return "crypto_hash writes before output";
  51. for (j = hlen;j < hlen + 16;++j) if (h2[j] != h[j]) return "crypto_hash writes after output";
  52. if (crypto_hash(m2,m2,mlen) != 0) return "crypto_hash returns nonzero";
  53. for (j = 0;j < hlen;++j) if (m2[j] != h[j]) return "crypto_hash does not handle overlap";
  54. for (j = 0;j < mlen;++j) m[j] ^= h[j % hlen];
  55. m[mlen] = h[0];
  56. }
  57. if (crypto_hash(h,m,CHECKSUM_BYTES) != 0) return "crypto_hash returns nonzero";
  58. for (i = 0;i < crypto_hash_BYTES;++i) {
  59. checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)];
  60. checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]];
  61. }
  62. checksum[2 * i] = 0;
  63. return 0;
  64. }