try.c 4.6 KB

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