try.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * crypto_scalarmult/try.c version 20090118
  3. * D. J. Bernstein
  4. * Public domain.
  5. */
  6. #include <stdlib.h>
  7. #include "crypto_scalarmult.h"
  8. extern unsigned char *alignedcalloc(unsigned long long);
  9. const char *primitiveimplementation = crypto_scalarmult_IMPLEMENTATION;
  10. #define mlen crypto_scalarmult_SCALARBYTES
  11. #define nlen crypto_scalarmult_SCALARBYTES
  12. #define plen crypto_scalarmult_BYTES
  13. #define qlen crypto_scalarmult_BYTES
  14. #define rlen crypto_scalarmult_BYTES
  15. static unsigned char *m;
  16. static unsigned char *n;
  17. static unsigned char *p;
  18. static unsigned char *q;
  19. static unsigned char *r;
  20. static unsigned char *m2;
  21. static unsigned char *n2;
  22. static unsigned char *p2;
  23. static unsigned char *q2;
  24. static unsigned char *r2;
  25. void preallocate(void)
  26. {
  27. }
  28. void allocate(void)
  29. {
  30. m = alignedcalloc(mlen);
  31. n = alignedcalloc(nlen);
  32. p = alignedcalloc(plen);
  33. q = alignedcalloc(qlen);
  34. r = alignedcalloc(rlen);
  35. m2 = alignedcalloc(mlen + crypto_scalarmult_BYTES);
  36. n2 = alignedcalloc(nlen + crypto_scalarmult_BYTES);
  37. p2 = alignedcalloc(plen + crypto_scalarmult_BYTES);
  38. q2 = alignedcalloc(qlen + crypto_scalarmult_BYTES);
  39. r2 = alignedcalloc(rlen + crypto_scalarmult_BYTES);
  40. }
  41. void predoit(void)
  42. {
  43. }
  44. void doit(void)
  45. {
  46. crypto_scalarmult(q,n,p);
  47. crypto_scalarmult_base(r,n);
  48. }
  49. char checksum[crypto_scalarmult_BYTES * 2 + 1];
  50. const char *checksum_compute(void)
  51. {
  52. long long i;
  53. long long j;
  54. long long tests;
  55. for (i = 0;i < mlen;++i) m[i] = i;
  56. for (i = 0;i < nlen;++i) n[i] = i + 1;
  57. for (i = 0;i < plen;++i) p[i] = i + 2;
  58. for (i = 0;i < qlen;++i) q[i] = i + 3;
  59. for (i = 0;i < rlen;++i) r[i] = i + 4;
  60. for (i = -16;i < 0;++i) p[i] = random();
  61. for (i = -16;i < 0;++i) n[i] = random();
  62. for (i = plen;i < plen + 16;++i) p[i] = random();
  63. for (i = nlen;i < nlen + 16;++i) n[i] = random();
  64. for (i = -16;i < plen + 16;++i) p2[i] = p[i];
  65. for (i = -16;i < nlen + 16;++i) n2[i] = n[i];
  66. if (crypto_scalarmult_base(p,n) != 0) return "crypto_scalarmult_base returns nonzero";
  67. for (i = -16;i < nlen + 16;++i) if (n2[i] != n[i]) return "crypto_scalarmult_base overwrites input";
  68. for (i = -16;i < 0;++i) if (p2[i] != p[i]) return "crypto_scalarmult_base writes before output";
  69. for (i = plen;i < plen + 16;++i) if (p2[i] != p[i]) return "crypto_scalarmult_base writes after output";
  70. for (tests = 0;tests < 100;++tests) {
  71. for (i = -16;i < 0;++i) q[i] = random();
  72. for (i = -16;i < 0;++i) p[i] = random();
  73. for (i = -16;i < 0;++i) m[i] = random();
  74. for (i = qlen;i < qlen + 16;++i) q[i] = random();
  75. for (i = plen;i < plen + 16;++i) p[i] = random();
  76. for (i = mlen;i < mlen + 16;++i) m[i] = random();
  77. for (i = -16;i < qlen + 16;++i) q2[i] = q[i];
  78. for (i = -16;i < plen + 16;++i) p2[i] = p[i];
  79. for (i = -16;i < mlen + 16;++i) m2[i] = m[i];
  80. if (crypto_scalarmult(q,m,p) != 0) return "crypto_scalarmult returns nonzero";
  81. for (i = -16;i < mlen + 16;++i) if (m2[i] != m[i]) return "crypto_scalarmult overwrites n input";
  82. for (i = -16;i < plen + 16;++i) if (p2[i] != p[i]) return "crypto_scalarmult overwrites p input";
  83. for (i = -16;i < 0;++i) if (q2[i] != q[i]) return "crypto_scalarmult writes before output";
  84. for (i = qlen;i < qlen + 16;++i) if (q2[i] != q[i]) return "crypto_scalarmult writes after output";
  85. if (crypto_scalarmult(m2,m2,p) != 0) return "crypto_scalarmult returns nonzero";
  86. for (i = 0;i < qlen;++i) if (q[i] != m2[i]) return "crypto_scalarmult does not handle n overlap";
  87. for (i = 0;i < qlen;++i) m2[i] = m[i];
  88. if (crypto_scalarmult(p2,m2,p2) != 0) return "crypto_scalarmult returns nonzero";
  89. for (i = 0;i < qlen;++i) if (q[i] != p2[i]) return "crypto_scalarmult does not handle p overlap";
  90. if (crypto_scalarmult(r,n,q) != 0) return "crypto_scalarmult returns nonzero";
  91. if (crypto_scalarmult(q,n,p) != 0) return "crypto_scalarmult returns nonzero";
  92. if (crypto_scalarmult(p,m,q) != 0) return "crypto_scalarmult returns nonzero";
  93. for (j = 0;j < plen;++j) if (p[j] != r[j]) return "crypto_scalarmult not associative";
  94. for (j = 0;j < mlen;++j) m[j] ^= q[j % qlen];
  95. for (j = 0;j < nlen;++j) n[j] ^= p[j % plen];
  96. }
  97. for (i = 0;i < crypto_scalarmult_BYTES;++i) {
  98. checksum[2 * i] = "0123456789abcdef"[15 & (p[i] >> 4)];
  99. checksum[2 * i + 1] = "0123456789abcdef"[15 & p[i]];
  100. }
  101. checksum[2 * i] = 0;
  102. return 0;
  103. }