try.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * crypto_box/try.c version 20090118
  3. * D. J. Bernstein
  4. * Public domain.
  5. */
  6. #include "crypto_box.h"
  7. extern unsigned char *alignedcalloc(unsigned long long);
  8. const char *primitiveimplementation = crypto_box_IMPLEMENTATION;
  9. #define MAXTEST_BYTES 10000
  10. #define CHECKSUM_BYTES 4096
  11. #define TUNE_BYTES 1536
  12. static unsigned char *ska;
  13. static unsigned char *pka;
  14. static unsigned char *skb;
  15. static unsigned char *pkb;
  16. static unsigned char *s;
  17. static unsigned char *n;
  18. static unsigned char *m;
  19. static unsigned char *c;
  20. static unsigned char *t;
  21. static unsigned char *ska2;
  22. static unsigned char *pka2;
  23. static unsigned char *skb2;
  24. static unsigned char *pkb2;
  25. static unsigned char *s2;
  26. static unsigned char *n2;
  27. static unsigned char *m2;
  28. static unsigned char *c2;
  29. static unsigned char *t2;
  30. #define sklen crypto_box_SECRETKEYBYTES
  31. #define pklen crypto_box_PUBLICKEYBYTES
  32. #define nlen crypto_box_NONCEBYTES
  33. #define slen crypto_box_BEFORENMBYTES
  34. void preallocate(void)
  35. {
  36. }
  37. void allocate(void)
  38. {
  39. ska = alignedcalloc(sklen);
  40. pka = alignedcalloc(pklen);
  41. skb = alignedcalloc(sklen);
  42. pkb = alignedcalloc(pklen);
  43. n = alignedcalloc(nlen);
  44. m = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
  45. c = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
  46. t = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
  47. s = alignedcalloc(slen);
  48. ska2 = alignedcalloc(sklen);
  49. pka2 = alignedcalloc(pklen);
  50. skb2 = alignedcalloc(sklen);
  51. pkb2 = alignedcalloc(pklen);
  52. n2 = alignedcalloc(nlen);
  53. m2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
  54. c2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
  55. t2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
  56. s2 = alignedcalloc(slen);
  57. }
  58. void predoit(void)
  59. {
  60. }
  61. void doit(void)
  62. {
  63. crypto_box(c,m,TUNE_BYTES + crypto_box_ZEROBYTES,n,pka,skb);
  64. crypto_box_open(t,c,TUNE_BYTES + crypto_box_ZEROBYTES,n,pkb,ska);
  65. }
  66. char checksum[nlen * 2 + 1];
  67. const char *checksum_compute(void)
  68. {
  69. long long i;
  70. long long j;
  71. if (crypto_box_keypair(pka,ska) != 0) return "crypto_box_keypair returns nonzero";
  72. if (crypto_box_keypair(pkb,skb) != 0) return "crypto_box_keypair returns nonzero";
  73. for (j = 0;j < crypto_box_ZEROBYTES;++j) m[j] = 0;
  74. for (i = 0;i < CHECKSUM_BYTES;++i) {
  75. long long mlen = i + crypto_box_ZEROBYTES;
  76. long long tlen = i + crypto_box_ZEROBYTES;
  77. long long clen = i + crypto_box_ZEROBYTES;
  78. for (j = -16;j < 0;++j) ska[j] = random();
  79. for (j = -16;j < 0;++j) skb[j] = random();
  80. for (j = -16;j < 0;++j) pka[j] = random();
  81. for (j = -16;j < 0;++j) pkb[j] = random();
  82. for (j = -16;j < 0;++j) m[j] = random();
  83. for (j = -16;j < 0;++j) n[j] = random();
  84. for (j = sklen;j < sklen + 16;++j) ska[j] = random();
  85. for (j = sklen;j < sklen + 16;++j) skb[j] = random();
  86. for (j = pklen;j < pklen + 16;++j) pka[j] = random();
  87. for (j = pklen;j < pklen + 16;++j) pkb[j] = random();
  88. for (j = mlen;j < mlen + 16;++j) m[j] = random();
  89. for (j = nlen;j < nlen + 16;++j) n[j] = random();
  90. for (j = -16;j < sklen + 16;++j) ska2[j] = ska[j];
  91. for (j = -16;j < sklen + 16;++j) skb2[j] = skb[j];
  92. for (j = -16;j < pklen + 16;++j) pka2[j] = pka[j];
  93. for (j = -16;j < pklen + 16;++j) pkb2[j] = pkb[j];
  94. for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
  95. for (j = -16;j < nlen + 16;++j) n2[j] = n[j];
  96. for (j = -16;j < clen + 16;++j) c2[j] = c[j] = random();
  97. if (crypto_box(c,m,mlen,n,pkb,ska) != 0) return "crypto_box returns nonzero";
  98. for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box overwrites m";
  99. for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box overwrites n";
  100. for (j = -16;j < 0;++j) if (c2[j] != c[j]) return "crypto_box writes before output";
  101. for (j = clen;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box writes after output";
  102. for (j = 0;j < crypto_box_BOXZEROBYTES;++j)
  103. if (c[j] != 0) return "crypto_box does not clear extra bytes";
  104. for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box overwrites ska";
  105. for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box overwrites skb";
  106. for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box overwrites pka";
  107. for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box overwrites pkb";
  108. for (j = -16;j < 0;++j) c[j] = random();
  109. for (j = clen;j < clen + 16;++j) c[j] = random();
  110. for (j = -16;j < clen + 16;++j) c2[j] = c[j];
  111. for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random();
  112. if (crypto_box_open(t,c,clen,n,pka,skb) != 0) return "crypto_box_open returns nonzero";
  113. for (j = -16;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box_open overwrites c";
  114. for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open overwrites n";
  115. for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open writes before output";
  116. for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open writes after output";
  117. for (j = 0;j < crypto_box_ZEROBYTES;++j)
  118. if (t[j] != 0) return "crypto_box_open does not clear extra bytes";
  119. for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box_open overwrites ska";
  120. for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites skb";
  121. for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pka";
  122. for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box_open overwrites pkb";
  123. for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "plaintext does not match";
  124. for (j = -16;j < slen + 16;++j) s2[j] = s[j] = random();
  125. if (crypto_box_beforenm(s,pkb,ska) != 0) return "crypto_box_beforenm returns nonzero";
  126. for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pk";
  127. for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites sk";
  128. for (j = -16;j < 0;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes before output";
  129. for (j = slen;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes after output";
  130. for (j = -16;j < slen + 16;++j) s2[j] = s[j];
  131. for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random();
  132. if (crypto_box_afternm(t,m,mlen,n,s) != 0) return "crypto_box_afternm returns nonzero";
  133. for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_afternm overwrites s";
  134. for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_afternm overwrites m";
  135. for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_afternm overwrites n";
  136. for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes before output";
  137. for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes after output";
  138. for (j = 0;j < crypto_box_BOXZEROBYTES;++j)
  139. if (t[j] != 0) return "crypto_box_afternm does not clear extra bytes";
  140. for (j = 0;j < mlen;++j) if (t[j] != c[j]) return "crypto_box_afternm does not match crypto_box";
  141. if (crypto_box_beforenm(s,pka,skb) != 0) return "crypto_box_beforenm returns nonzero";
  142. for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random();
  143. if (crypto_box_open_afternm(t,c,clen,n,s) != 0) return "crypto_box_open_afternm returns nonzero";
  144. for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_open_afternm overwrites s";
  145. for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_open_afternm overwrites m";
  146. for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open_afternm overwrites n";
  147. for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes before output";
  148. for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes after output";
  149. for (j = 0;j < crypto_box_ZEROBYTES;++j)
  150. if (t[j] != 0) return "crypto_box_open_afternm does not clear extra bytes";
  151. for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "crypto_box_open_afternm does not match crypto_box_open";
  152. for (j = 0;j < i;++j) n[j % nlen] ^= c[j + crypto_box_BOXZEROBYTES];
  153. if (i == 0) m[crypto_box_ZEROBYTES] = 0;
  154. m[i + crypto_box_ZEROBYTES] = m[crypto_box_ZEROBYTES];
  155. for (j = 0;j < i;++j) m[j + crypto_box_ZEROBYTES] ^= c[j + crypto_box_BOXZEROBYTES];
  156. }
  157. for (i = 0;i < nlen;++i) {
  158. checksum[2 * i] = "0123456789abcdef"[15 & (n[i] >> 4)];
  159. checksum[2 * i + 1] = "0123456789abcdef"[15 & n[i]];
  160. }
  161. checksum[2 * i] = 0;
  162. return 0;
  163. }