try.c 4.0 KB

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