Reference implementations of PQC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

255 lines
8.1 KiB

  1. #include "api.h"
  2. #include "randombytes.h"
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #ifndef NTESTS
  8. #define NTESTS 5
  9. #endif
  10. #define MLEN 32
  11. const uint8_t canary[8] = {
  12. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
  13. };
  14. /* allocate a bit more for all keys and messages and
  15. * make sure it is not touched by the implementations.
  16. */
  17. static void write_canary(uint8_t *d) {
  18. for (size_t i = 0; i < 8; i++) {
  19. d[i] = canary[i];
  20. }
  21. }
  22. static int check_canary(const uint8_t *d) {
  23. for (size_t i = 0; i < 8; i++) {
  24. if (d[i] != canary[i]) {
  25. return -1;
  26. }
  27. }
  28. return 0;
  29. }
  30. // https://stackoverflow.com/a/1489985/1711232
  31. #define PASTER(x, y) x##_##y
  32. #define EVALUATOR(x, y) PASTER(x, y)
  33. #define NAMESPACE(fun) EVALUATOR(PQCLEAN_NAMESPACE, fun)
  34. #define CRYPTO_PUBLICKEYBYTES NAMESPACE(CRYPTO_PUBLICKEYBYTES)
  35. #define CRYPTO_SECRETKEYBYTES NAMESPACE(CRYPTO_SECRETKEYBYTES)
  36. #define CRYPTO_BYTES NAMESPACE(CRYPTO_BYTES)
  37. #define CRYPTO_ALGNAME NAMESPACE(CRYPTO_ALGNAME)
  38. #define crypto_sign_keypair NAMESPACE(crypto_sign_keypair)
  39. #define crypto_sign NAMESPACE(crypto_sign)
  40. #define crypto_sign_open NAMESPACE(crypto_sign_open)
  41. #define crypto_sign_signature NAMESPACE(crypto_sign_signature)
  42. #define crypto_sign_verify NAMESPACE(crypto_sign_verify)
  43. #define RETURNS_ZERO(f) \
  44. if ((f) != 0) { \
  45. puts("(f) returned non-zero returncode"); \
  46. return -1; \
  47. }
  48. // https://stackoverflow.com/a/55243651/248065
  49. #define MY_TRUTHY_VALUE_X 1
  50. #define CAT(x,y) CAT_(x,y)
  51. #define CAT_(x,y) x##y
  52. #define HAS_NAMESPACE(x) CAT(CAT(MY_TRUTHY_VALUE_,CAT(PQCLEAN_NAMESPACE,CAT(_,x))),X)
  53. #if !HAS_NAMESPACE(API_H)
  54. #error "namespace not properly defined for header guard"
  55. #endif
  56. static int test_sign(void) {
  57. /*
  58. * This is most likely going to be aligned by the compiler.
  59. * 16 extra bytes for canary
  60. * 1 extra byte for unalignment
  61. */
  62. uint8_t pk_aligned[CRYPTO_PUBLICKEYBYTES + 16 + 1];
  63. uint8_t sk_aligned[CRYPTO_SECRETKEYBYTES + 16 + 1];
  64. uint8_t sm_aligned[MLEN + CRYPTO_BYTES + 16 + 1];
  65. uint8_t m_aligned[MLEN + 16 + 1];
  66. /*
  67. * Make sure all pointers are odd.
  68. * This ensures that the implementation does not assume anything about the
  69. * data alignment. For example this would catch if an implementation
  70. * directly uses these pointers to load into vector registers using movdqa.
  71. */
  72. uint8_t *pk = (uint8_t *) ((uintptr_t) pk_aligned|(uintptr_t) 1);
  73. uint8_t *sk = (uint8_t *) ((uintptr_t) sk_aligned|(uintptr_t) 1);
  74. uint8_t *sm = (uint8_t *) ((uintptr_t) sm_aligned|(uintptr_t) 1);
  75. uint8_t *m = (uint8_t *) ((uintptr_t) m_aligned|(uintptr_t) 1);
  76. size_t mlen;
  77. size_t smlen;
  78. int returncode;
  79. int i;
  80. /*
  81. * Write 8 byte canary before and after the actual memory regions.
  82. * This is used to validate that the implementation does not assume
  83. * anything about the placement of data in memory
  84. * (e.g., assuming that the pk is always behind the sk)
  85. */
  86. write_canary(pk);
  87. write_canary(pk + CRYPTO_PUBLICKEYBYTES + 8);
  88. write_canary(sk);
  89. write_canary(sk + CRYPTO_SECRETKEYBYTES + 8);
  90. write_canary(sm);
  91. write_canary(sm + MLEN + CRYPTO_BYTES + 8);
  92. write_canary(m);
  93. write_canary(m + MLEN + 8);
  94. for (i = 0; i < NTESTS; i++) {
  95. RETURNS_ZERO(crypto_sign_keypair(pk + 8, sk + 8));
  96. randombytes(m + 8, MLEN);
  97. RETURNS_ZERO(crypto_sign(sm + 8, &smlen, m + 8, MLEN, sk + 8));
  98. // By relying on m == sm we prevent having to allocate CRYPTO_BYTES
  99. // twice
  100. if ((returncode =
  101. crypto_sign_open(sm + 8, &mlen, sm + 8, smlen, pk + 8)) != 0) {
  102. fprintf(stderr, "ERROR Signature did not verify correctly!\n");
  103. if (returncode > 0) {
  104. fprintf(stderr, "ERROR return code should be < 0 on failure");
  105. }
  106. return 1;
  107. }
  108. // Validate that the implementation did not touch the canary
  109. if (check_canary(pk) || check_canary(pk + CRYPTO_PUBLICKEYBYTES + 8) ||
  110. check_canary(sk) || check_canary(sk + CRYPTO_SECRETKEYBYTES + 8) ||
  111. check_canary(sm) || check_canary(sm + MLEN + CRYPTO_BYTES + 8) ||
  112. check_canary(m) || check_canary(m + MLEN + 8)) {
  113. fprintf(stderr, "ERROR canary overwritten\n");
  114. return 1;
  115. }
  116. }
  117. return 0;
  118. }
  119. static int test_sign_detached(void) {
  120. /*
  121. * This is most likely going to be aligned by the compiler.
  122. * 16 extra bytes for canary
  123. * 1 extra byte for unalignment
  124. */
  125. uint8_t pk_aligned[CRYPTO_PUBLICKEYBYTES + 16 + 1];
  126. uint8_t sk_aligned[CRYPTO_SECRETKEYBYTES + 16 + 1];
  127. uint8_t sig_aligned[CRYPTO_BYTES + 16 + 1];
  128. uint8_t m_aligned[MLEN + 16 + 1];
  129. /*
  130. * Make sure all pointers are odd.
  131. * This ensures that the implementation does not assume anything about the
  132. * data alignment. For example this would catch if an implementation
  133. * directly uses these pointers to load into vector registers using movdqa.
  134. */
  135. uint8_t *pk = (uint8_t *) ((uintptr_t) pk_aligned|(uintptr_t) 1);
  136. uint8_t *sk = (uint8_t *) ((uintptr_t) sk_aligned|(uintptr_t) 1);
  137. uint8_t *sig = (uint8_t *) ((uintptr_t) sig_aligned|(uintptr_t) 1);
  138. uint8_t *m = (uint8_t *) ((uintptr_t) m_aligned|(uintptr_t) 1);
  139. size_t siglen;
  140. int returncode;
  141. int i;
  142. /*
  143. * Write 8 byte canary before and after the actual memory regions.
  144. * This is used to validate that the implementation does not assume
  145. * anything about the placement of data in memory
  146. * (e.g., assuming that the pk is always behind the sk)
  147. */
  148. write_canary(pk);
  149. write_canary(pk + CRYPTO_PUBLICKEYBYTES + 8);
  150. write_canary(sk);
  151. write_canary(sk + CRYPTO_SECRETKEYBYTES + 8);
  152. write_canary(sig);
  153. write_canary(sig + CRYPTO_BYTES + 8);
  154. write_canary(m);
  155. write_canary(m + MLEN + 8);
  156. for (i = 0; i < NTESTS; i++) {
  157. RETURNS_ZERO(crypto_sign_keypair(pk + 8, sk + 8));
  158. randombytes(m + 8, MLEN);
  159. RETURNS_ZERO(crypto_sign_signature(sig + 8, &siglen, m + 8, MLEN, sk + 8));
  160. if ((returncode =
  161. crypto_sign_verify(sig + 8, siglen, m + 8, MLEN, pk + 8)) != 0) {
  162. fprintf(stderr, "ERROR Signature did not verify correctly!\n");
  163. if (returncode > 0) {
  164. fprintf(stderr, "ERROR return code should be < 0 on failure");
  165. }
  166. return 1;
  167. }
  168. // Validate that the implementation did not touch the canary
  169. if (check_canary(pk) || check_canary(pk + CRYPTO_PUBLICKEYBYTES + 8) ||
  170. check_canary(sk) || check_canary(sk + CRYPTO_SECRETKEYBYTES + 8) ||
  171. check_canary(sig) || check_canary(sig + CRYPTO_BYTES + 8) ||
  172. check_canary(m) || check_canary(m + MLEN + 8)) {
  173. fprintf(stderr, "ERROR canary overwritten\n");
  174. return 1;
  175. }
  176. }
  177. return 0;
  178. }
  179. static int test_wrong_pk(void) {
  180. uint8_t pk[CRYPTO_PUBLICKEYBYTES];
  181. uint8_t pk2[CRYPTO_PUBLICKEYBYTES];
  182. uint8_t sk[CRYPTO_SECRETKEYBYTES];
  183. uint8_t sm[MLEN + CRYPTO_BYTES];
  184. uint8_t m[MLEN];
  185. size_t mlen;
  186. size_t smlen;
  187. int returncode;
  188. int i;
  189. for (i = 0; i < NTESTS; i++) {
  190. RETURNS_ZERO(crypto_sign_keypair(pk2, sk));
  191. RETURNS_ZERO(crypto_sign_keypair(pk, sk));
  192. randombytes(m, MLEN);
  193. RETURNS_ZERO(crypto_sign(sm, &smlen, m, MLEN, sk));
  194. // By relying on m == sm we prevent having to allocate CRYPTO_BYTES
  195. // twice
  196. returncode = crypto_sign_open(sm, &mlen, sm, smlen, pk2);
  197. if (!returncode) {
  198. fprintf(stderr, "ERROR Signature did verify correctly under wrong public key!\n");
  199. if (returncode > 0) {
  200. fprintf(stderr, "ERROR return code should be < 0");
  201. }
  202. return 1;
  203. }
  204. }
  205. return 0;
  206. }
  207. int main(void) {
  208. // check if CRYPTO_ALGNAME is printable
  209. puts(CRYPTO_ALGNAME);
  210. int result = 0;
  211. result += test_sign();
  212. result += test_sign_detached();
  213. result += test_wrong_pk();
  214. return result;
  215. }