try.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * crypto_sign/try.c version 20111119
  3. * D. J. Bernstein
  4. * Public domain.
  5. */
  6. #include <stdlib.h>
  7. #include "randombytes.h"
  8. #include "crypto_sign.h"
  9. #define MAXTEST_BYTES 10000
  10. #define TUNE_BYTES 1536
  11. extern unsigned char *alignedcalloc(unsigned long long);
  12. const char *primitiveimplementation = crypto_sign_IMPLEMENTATION;
  13. static unsigned char *pk; unsigned long long pklen; static unsigned char *pk2;
  14. static unsigned char *sk; unsigned long long sklen; static unsigned char *sk2;
  15. static unsigned char *m; unsigned long long mlen; static unsigned char *m2;
  16. static unsigned char *sm; unsigned long long smlen; static unsigned char *sm2;
  17. static unsigned char *t; unsigned long long tlen; static unsigned char *t2;
  18. void preallocate(void)
  19. {
  20. #ifdef RAND_R_PRNG_NOT_SEEDED
  21. RAND_status();
  22. #endif
  23. }
  24. void allocate(void)
  25. {
  26. pk = alignedcalloc(pklen = crypto_sign_PUBLICKEYBYTES);
  27. sk = alignedcalloc(sklen = crypto_sign_SECRETKEYBYTES);
  28. m = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
  29. sm = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
  30. t = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
  31. pk2 = alignedcalloc(pklen);
  32. sk2 = alignedcalloc(sklen);
  33. m2 = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
  34. sm2 = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
  35. t2 = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
  36. }
  37. void predoit(void)
  38. {
  39. crypto_sign_keypair(pk,sk);
  40. mlen = TUNE_BYTES;
  41. smlen = 0;
  42. }
  43. void doit(void)
  44. {
  45. randombytes(m,mlen);
  46. crypto_sign(sm,&smlen,m,mlen,sk);
  47. crypto_sign_open(t,&tlen,sm,smlen,pk);
  48. }
  49. static unsigned char chain[37]; long long chainlen = 37;
  50. char checksum[37 * 2 + 1];
  51. const char *checksum_compute(void)
  52. {
  53. long long mlen;
  54. long long i;
  55. long long j;
  56. long long loops;
  57. for (loops = 0;loops < 10;++loops) {
  58. for (j = -16;j < 0;++j) sk2[j] = sk[j] = random();
  59. for (j = 0;j < sklen + 16;++j) sk2[j] = sk[j] = random();
  60. for (j = -16;j < 0;++j) pk2[j] = pk[j] = random();
  61. for (j = 0;j < pklen + 16;++j) pk2[j] = pk[j] = random();
  62. if (crypto_sign_keypair(pk,sk) != 0) return "crypto_sign_keypair returns nonzero";
  63. for (j = -16;j < 0;++j) if (pk[j] != pk2[j]) return "crypto_sign_keypair writes before pk";
  64. for (j = pklen;j < pklen + 16;++j) if (pk[j] != pk2[j]) return "crypto_sign_keypair writes after pk";
  65. for (j = -16;j < 0;++j) if (sk[j] != sk2[j]) return "crypto_sign_keypair writes before sk";
  66. for (j = sklen;j < sklen + 16;++j) if (sk[j] != sk2[j]) return "crypto_sign_keypair writes after sk";
  67. for (mlen = 0;mlen < MAXTEST_BYTES;mlen += 1 + (mlen / 16)) {
  68. for (j = -16;j < 0;++j) m2[j] = m[j] = random();
  69. for (j = mlen;j < mlen + 16;++j) m2[j] = m[j] = random();
  70. randombytes(m,mlen);
  71. if (mlen > 0)
  72. for (j = 0;j < chainlen;++j) m[j % mlen] ^= chain[j];
  73. for (j = 0;j < mlen;++j) m2[j] = m[j];
  74. for (j = -16;j < 0;++j) pk2[j] = pk[j];
  75. for (j = 0;j < pklen + 16;++j) pk2[j] = pk[j];
  76. for (j = -16;j < 0;++j) sk2[j] = sk[j];
  77. for (j = 0;j < sklen + 16;++j) sk2[j] = sk[j];
  78. for (j = -16;j < 0;++j) sm2[j] = sm[j] = random();
  79. for (j = 0;j < mlen + crypto_sign_BYTES + 16;++j) sm2[j] = sm[j] = random();
  80. if (crypto_sign(sm,&smlen,m,mlen,sk) != 0) return "crypto_sign returns nonzero";
  81. if (smlen > mlen + crypto_sign_BYTES) return "crypto_sign returns more than crypto_sign_BYTES extra bytes";
  82. if (smlen == 0) return "crypto_sign returns empty message";
  83. for (j = -16;j < 0;++j) if (pk[j] != pk2[j]) return "crypto_sign overwrites pk";
  84. for (j = 0;j < pklen + 16;++j) if (pk[j] != pk2[j]) return "crypto_sign overwrites pk";
  85. for (j = -16;j < 0;++j) if (sk[j] != sk2[j]) return "crypto_sign overwrites sk";
  86. for (j = 0;j < sklen + 16;++j) if (sk[j] != sk2[j]) return "crypto_sign overwrites sk";
  87. for (j = -16;j < 0;++j) if (m[j] != m2[j]) return "crypto_sign overwrites m";
  88. for (j = 0;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_sign overwrites m";
  89. for (j = -16;j < 0;++j) if (sm[j] != sm2[j]) return "crypto_sign writes before sm";
  90. for (j = smlen;j < smlen + 16;++j) if (sm[j] != sm2[j]) return "crypto_sign writes after sm";
  91. for (j = 0;j < smlen;++j) chain[j % chainlen] ^= sm[j];
  92. for (j = -16;j < 0;++j) sm2[j] = sm[j];
  93. for (j = 0;j < smlen + 16;++j) sm2[j] = sm[j];
  94. for (j = -16;j < 0;++j) t2[j] = t[j] = random();
  95. for (j = 0;j < smlen + 16;++j) t2[j] = t[j] = random();
  96. if (crypto_sign_open(t,&tlen,sm,smlen,pk) != 0) return "crypto_sign_open returns nonzero";
  97. if (tlen != mlen) return "crypto_sign_open does not match length";
  98. for (i = 0;i < tlen;++i)
  99. if (t[i] != m[i])
  100. return "crypto_sign_open does not match contents";
  101. for (j = -16;j < 0;++j) if (pk[j] != pk2[j]) return "crypto_sign_open overwrites pk";
  102. for (j = 0;j < pklen + 16;++j) if (pk[j] != pk2[j]) return "crypto_sign_open overwrites pk";
  103. for (j = -16;j < 0;++j) if (sk[j] != sk2[j]) return "crypto_sign_open overwrites sk";
  104. for (j = 0;j < sklen + 16;++j) if (sk[j] != sk2[j]) return "crypto_sign_open overwrites sk";
  105. for (j = -16;j < 0;++j) if (sm[j] != sm2[j]) return "crypto_sign_open overwrites sm";
  106. for (j = 0;j < smlen + 16;++j) if (sm[j] != sm2[j]) return "crypto_sign_open overwrites sm";
  107. for (j = -16;j < 0;++j) if (t[j] != t2[j]) return "crypto_sign_open writes before t";
  108. for (j = smlen;j < smlen + 16;++j) if (t[j] != t2[j]) return "crypto_sign_open writes after t";
  109. j = random() % smlen;
  110. sm[j] ^= 1;
  111. for (j = -16;j < 0;++j) sm2[j] = sm[j];
  112. for (j = 0;j < smlen + 16;++j) sm2[j] = sm[j];
  113. for (j = -16;j < 0;++j) t2[j] = t[j] = random();
  114. for (j = 0;j < smlen + 16;++j) t2[j] = t[j] = random();
  115. if (crypto_sign_open(t,&tlen,sm,smlen,pk) == 0) {
  116. if (tlen != mlen) return "crypto_sign_open allows trivial forgery of length";
  117. for (i = 0;i < tlen;++i)
  118. if (t[i] != m[i])
  119. return "crypto_sign_open allows trivial forgery of contents";
  120. }
  121. for (j = -16;j < 0;++j) if (pk[j] != pk2[j]) return "crypto_sign_open overwrites pk";
  122. for (j = 0;j < pklen + 16;++j) if (pk[j] != pk2[j]) return "crypto_sign_open overwrites pk";
  123. for (j = -16;j < 0;++j) if (sk[j] != sk2[j]) return "crypto_sign_open overwrites sk";
  124. for (j = 0;j < sklen + 16;++j) if (sk[j] != sk2[j]) return "crypto_sign_open overwrites sk";
  125. for (j = -16;j < 0;++j) if (sm[j] != sm2[j]) return "crypto_sign_open overwrites sm";
  126. for (j = 0;j < smlen + 16;++j) if (sm[j] != sm2[j]) return "crypto_sign_open overwrites sm";
  127. for (j = -16;j < 0;++j) if (t[j] != t2[j]) return "crypto_sign_open writes before t";
  128. for (j = smlen;j < smlen + 16;++j) if (t[j] != t2[j]) return "crypto_sign_open writes after t";
  129. sm[j] ^= 1;
  130. }
  131. }
  132. for (i = 0;i < chainlen;++i) {
  133. checksum[2 * i] = "0123456789abcdef"[15 & (chain[i] >> 4)];
  134. checksum[2 * i + 1] = "0123456789abcdef"[15 & chain[i]];
  135. }
  136. checksum[2 * i] = 0;
  137. return 0;
  138. }