try.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * crypto_verify/try.c version 20090118
  3. * D. J. Bernstein
  4. * Public domain.
  5. */
  6. #include <stdlib.h>
  7. #include "crypto_verify.h"
  8. extern unsigned char *alignedcalloc(unsigned long long);
  9. const char *primitiveimplementation = crypto_verify_IMPLEMENTATION;
  10. static unsigned char *x;
  11. static unsigned char *y;
  12. void preallocate(void)
  13. {
  14. }
  15. void allocate(void)
  16. {
  17. x = alignedcalloc(crypto_verify_BYTES);
  18. y = alignedcalloc(crypto_verify_BYTES);
  19. }
  20. void predoit(void)
  21. {
  22. }
  23. void doit(void)
  24. {
  25. crypto_verify(x,y);
  26. }
  27. static const char *check(void)
  28. {
  29. int r = crypto_verify(x,y);
  30. if (r == 0) {
  31. if (memcmp(x,y,crypto_verify_BYTES)) return "different strings pass verify";
  32. } else if (r == -1) {
  33. if (!memcmp(x,y,crypto_verify_BYTES)) return "equal strings fail verify";
  34. } else {
  35. return "weird return value from verify";
  36. }
  37. return 0;
  38. }
  39. char checksum[2];
  40. const char *checksum_compute(void)
  41. {
  42. long long tests;
  43. long long i;
  44. long long j;
  45. const char *c;
  46. for (tests = 0;tests < 100000;++tests) {
  47. for (i = 0;i < crypto_verify_BYTES;++i) x[i] = random();
  48. for (i = 0;i < crypto_verify_BYTES;++i) y[i] = random();
  49. c = check(); if (c) return c;
  50. for (i = 0;i < crypto_verify_BYTES;++i) y[i] = x[i];
  51. c = check(); if (c) return c;
  52. y[random() % crypto_verify_BYTES] = random();
  53. c = check(); if (c) return c;
  54. y[random() % crypto_verify_BYTES] = random();
  55. c = check(); if (c) return c;
  56. y[random() % crypto_verify_BYTES] = random();
  57. c = check(); if (c) return c;
  58. }
  59. checksum[0] = '0';
  60. checksum[1] = 0;
  61. return 0;
  62. }