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.

290 lines
10 KiB

  1. /*
  2. * Generate intermediate test vectors useful to test implementations.
  3. */
  4. // Number of sample vectors to be generated
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "../fips202.h"
  9. #include "../params.h"
  10. #include "../randombytes.h"
  11. #include "../utils.h"
  12. #include "../wots.h"
  13. #include "../xmss_commons.h"
  14. #include "../xmss_core.h"
  15. #include <sys/random.h>
  16. #include <sys/stat.h>
  17. #include <json.h>
  18. struct param_t {
  19. uint8_t oid;
  20. const char *hash;
  21. unsigned height;
  22. unsigned n_samples;
  23. };
  24. void sprint_hex(char *sbuf, unsigned char *buf, int len) {
  25. for (int i = 0; i < len; i++) {
  26. sprintf(&sbuf[2*i], "%02X", buf[i]);
  27. }
  28. sbuf[2*len] = '\0';
  29. }
  30. void vectors_keygen(uint32_t oid, json_object *jreq, json_object *jres, uint32_t n_samples) {
  31. xmss_params params;
  32. struct json_object *tc_req, *tcs_req, *tc_res, *tcs_res;
  33. xmss_parse_oid(&params, oid);
  34. unsigned char seed[params.n * 3];
  35. unsigned char pk[params.pk_bytes + XMSS_OID_LEN];
  36. unsigned char sk[params.sk_bytes + XMSS_OID_LEN];
  37. char *sbuf;
  38. unsigned i;
  39. tcs_req = json_object_new_array();
  40. tcs_res = json_object_new_array();
  41. for (i = 0; i < XMSS_OID_LEN; i++) {
  42. pk[XMSS_OID_LEN - i - 1] = (oid >> (8 * i)) & 0xFF;
  43. sk[XMSS_OID_LEN - i - 1] = (oid >> (8 * i)) & 0xFF;
  44. }
  45. for (size_t i=0; i<n_samples; i++) {
  46. tc_req = json_object_new_object();
  47. tc_res = json_object_new_object();
  48. getentropy(seed, 3*params.n);
  49. xmssmt_core_seed_keypair(&params, &pk[XMSS_OID_LEN], &sk[XMSS_OID_LEN], seed);
  50. json_object_object_add(tc_req, "tcId", json_object_new_int(i+1));
  51. sbuf = malloc(2*params.n + 1);
  52. sprint_hex(sbuf, seed, params.n);
  53. json_object_object_add(tc_req, "S_XMSS", json_object_new_string(sbuf));
  54. sprint_hex(sbuf, &seed[params.n], params.n);
  55. json_object_object_add(tc_req, "SK_PRF", json_object_new_string(sbuf));
  56. sprint_hex(sbuf, &seed[2*params.n], params.n);
  57. json_object_object_add(tc_req, "I", json_object_new_string(sbuf));
  58. free(sbuf);
  59. json_object_object_add(tc_res, "tcId", json_object_new_int(i+1));
  60. sbuf = malloc(2*params.pk_bytes + 1);
  61. sprint_hex(sbuf, pk, params.pk_bytes + XMSS_OID_LEN);
  62. json_object_object_add(tc_res, "publicKey", json_object_new_string(sbuf));
  63. free(sbuf);
  64. sbuf = malloc(2*params.sk_bytes + 1);
  65. sprint_hex(sbuf, sk, params.sk_bytes + XMSS_OID_LEN);
  66. json_object_object_add(tc_res, "secretKey", json_object_new_string(sbuf));
  67. free(sbuf);
  68. json_object_array_add(tcs_req,tc_req);
  69. json_object_array_add(tcs_res,tc_res);
  70. }
  71. json_object_object_add(jreq, "tests", tcs_req);
  72. json_object_object_add(jres, "tests", tcs_res);
  73. }
  74. void json_header_keygen(json_object *jobj) {
  75. json_object_object_add(jobj, "vsId", json_object_new_int(0));
  76. json_object_object_add(jobj, "algorithm", json_object_new_string("XMSS"));
  77. json_object_object_add(jobj, "mode", json_object_new_string("keyGen"));
  78. json_object_object_add(jobj, "revision", json_object_new_string("1.0"));
  79. json_object_object_add(jobj, "isSample", json_object_new_boolean(1));
  80. }
  81. void keygen_KAT(const struct param_t *h) {
  82. struct json_object *jres, *jreq, *tgs_req, *tgs_res, *tg_req, *tg_res;
  83. char buf[256];
  84. jreq = json_object_new_object();
  85. jres = json_object_new_object();
  86. json_header_keygen(jreq);
  87. json_header_keygen(jres);
  88. tgs_req = json_object_new_array();
  89. tgs_res = json_object_new_array();
  90. tg_req = json_object_new_object();
  91. tg_res = json_object_new_object();
  92. // Request file
  93. json_object_object_add(tg_req, "tgId", json_object_new_int(1));
  94. json_object_object_add(tg_req, "testType", json_object_new_string("AFT"));
  95. json_object_object_add(tg_req, "OID", json_object_new_int(h->oid));
  96. // Response file
  97. json_object_object_add(tg_res, "tgId", json_object_new_int(1));
  98. json_object_object_add(tg_req, "OID", json_object_new_int(h->oid));
  99. vectors_keygen(h->oid, tg_req, tg_res, h->n_samples);
  100. json_object_array_add(tgs_req, tg_req);
  101. json_object_array_add(tgs_res, tg_res);
  102. json_object_object_add(jreq, "testGroups", tgs_req);
  103. json_object_object_add(jres, "testGroups", tgs_res);
  104. sprintf(buf, "XMSS-%s-%s-H%u", "keyGen", h->hash, h->height);
  105. mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  106. sprintf(buf, "XMSS-%s-%s-H%u/%s", "keyGen", h->hash, h->height, "prompt.json");
  107. json_object_to_file_ext(buf, jreq, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
  108. sprintf(buf, "XMSS-%s-%s-H%u/%s", "keyGen", h->hash, h->height, "expectedResults.json");
  109. json_object_to_file_ext(buf, jres, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
  110. json_object_put(jres);
  111. json_object_put(jreq);
  112. }
  113. void json_header_siggen(json_object *jobj) {
  114. json_object_object_add(jobj, "vsId", json_object_new_int(0));
  115. json_object_object_add(jobj, "algorithm", json_object_new_string("XMSS"));
  116. json_object_object_add(jobj, "mode", json_object_new_string("sigGen"));
  117. json_object_object_add(jobj, "revision", json_object_new_string("1.0"));
  118. json_object_object_add(jobj, "isSample", json_object_new_boolean(1));
  119. }
  120. void vectors_siggen(uint32_t oid, unsigned char *sk, json_object *jreq, json_object *jres, uint32_t n_samples) {
  121. xmss_params params;
  122. struct json_object *tc_req, *tcs_req, *tc_res, *tcs_res;
  123. xmss_parse_oid(&params, oid);
  124. unsigned char sm[params.sig_bytes + 128];
  125. unsigned long long smlen = 0;
  126. unsigned char msg[128];
  127. unsigned q;
  128. unsigned height = 1u << (params.full_height);
  129. char *sbuf;
  130. tcs_req = json_object_new_array();
  131. tcs_res = json_object_new_array();
  132. for (size_t i=0; i<n_samples; i++) {
  133. tc_req = json_object_new_object();
  134. tc_res = json_object_new_object();
  135. getentropy(msg, 128);
  136. getentropy(&q, sizeof(q));
  137. q = q % height;
  138. smlen = 0;
  139. ull_to_bytes(sk, params.index_bytes, q);
  140. xmss_core_sign(&params, sk, sm, &smlen, msg, 128);
  141. json_object_object_add(tc_res, "tcId", json_object_new_int(i+1));
  142. json_object_object_add(tc_req, "tcId", json_object_new_int(i+1));
  143. sbuf = malloc(2*128 + 1);
  144. sprint_hex(sbuf, msg, 128);
  145. json_object_object_add(tc_req, "message", json_object_new_string(sbuf));
  146. free(sbuf);
  147. sbuf = malloc(2*smlen + 1);
  148. sprint_hex(sbuf, sm, smlen);
  149. json_object_object_add(tc_res, "signature", json_object_new_string(sbuf));
  150. free(sbuf);
  151. json_object_object_add(tc_req, "q", json_object_new_int(q));
  152. json_object_array_add(tcs_req, tc_req);
  153. json_object_array_add(tcs_res, tc_res);
  154. }
  155. json_object_object_add(jreq, "tests", tcs_req);
  156. json_object_object_add(jres, "tests", tcs_res);
  157. }
  158. void siggen_KAT(const struct param_t *h) {
  159. xmss_params params;
  160. struct json_object *jres, *jreq, *tgs_req, *tgs_res, *tg_req, *tg_res;
  161. xmss_parse_oid(&params, h->oid);
  162. char buf[256], *sbuf;
  163. unsigned char seed[params.n * 3];
  164. unsigned char pk[params.pk_bytes + XMSS_OID_LEN];
  165. unsigned char sk[params.sk_bytes + XMSS_OID_LEN];
  166. jreq = json_object_new_object();
  167. jres = json_object_new_object();
  168. json_header_siggen(jreq);
  169. json_header_siggen(jres);
  170. tgs_req = json_object_new_array();
  171. tgs_res = json_object_new_array();
  172. tg_req = json_object_new_object();
  173. tg_res = json_object_new_object();
  174. // Request file
  175. json_object_object_add(tg_req, "tgId", json_object_new_int(1));
  176. json_object_object_add(tg_req, "testType", json_object_new_string("AFT"));
  177. json_object_object_add(tg_req, "OID", json_object_new_int(h->oid));
  178. // Response file
  179. json_object_object_add(tg_res, "tgId", json_object_new_int(1));
  180. json_object_object_add(tg_req, "OID", json_object_new_int(h->oid));
  181. getentropy(seed, 3*params.n);
  182. xmssmt_core_seed_keypair(&params, pk + XMSS_OID_LEN, sk + XMSS_OID_LEN, seed);
  183. sbuf = malloc(2*params.n + 1);
  184. sprint_hex(sbuf, seed, params.n);
  185. json_object_object_add(tg_req, "S_XMSS", json_object_new_string(sbuf));
  186. sprint_hex(sbuf, &seed[params.n], params.n);
  187. json_object_object_add(tg_req, "SK_PRF", json_object_new_string(sbuf));
  188. sprint_hex(sbuf, &seed[2*params.n], params.n);
  189. json_object_object_add(tg_req, "I", json_object_new_string(sbuf));
  190. free(sbuf);
  191. sbuf = malloc(2*params.pk_bytes + 1);
  192. sprint_hex(sbuf, pk, params.pk_bytes + XMSS_OID_LEN);
  193. json_object_object_add(tg_res, "publicKey", json_object_new_string(sbuf));
  194. free(sbuf);
  195. vectors_siggen(h->oid, sk + XMSS_OID_LEN, tg_req, tg_res, h->n_samples);
  196. json_object_array_add(tgs_req, tg_req);
  197. json_object_array_add(tgs_res, tg_res);
  198. json_object_object_add(jreq, "testGroups", tgs_req);
  199. json_object_object_add(jres, "testGroups", tgs_res);
  200. sprintf(buf, "XMSS-%s-%s-H%u", "sigGen", h->hash, h->height);
  201. mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  202. sprintf(buf, "XMSS-%s-%s-H%u/%s", "sigGen", h->hash, h->height, "prompt.json");
  203. json_object_to_file_ext(buf, jreq, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
  204. sprintf(buf, "XMSS-%s-%s-H%u/%s", "sigGen", h->hash, h->height, "expectedResults.json");
  205. json_object_to_file_ext(buf, jres, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
  206. json_object_put(jres);
  207. json_object_put(jreq);
  208. }
  209. int main() {
  210. const struct param_t OIDs[] = {
  211. {0x01, "SHA256-N32", 10, 10}, // H10
  212. {0x0D, "SHA256-N24", 10, 10}, // H10
  213. {0x10, "SHAKE256-N32", 10, 10}, // H10
  214. {0x13, "SHAKE256-N24", 10, 10}, // H10
  215. {0x02, "SHA256-N32", 16, 5}, // H16
  216. {0x0E, "SHA256-N24", 16, 5}, // H16
  217. {0x11, "SHAKE256-N32", 16, 5}, // H16
  218. {0x14, "SHAKE256-N24", 16, 5}, // H16
  219. {0x03, "SHA256-N32", 20, 3}, // H20
  220. {0x0F, "SHA256-N24", 20, 3}, // H20
  221. {0x12, "SHAKE256-N32", 20, 3}, // H20
  222. {0x15, "SHAKE256-N24", 20, 3} // H20
  223. };
  224. for (size_t i=0; i<12; i++) {
  225. keygen_KAT(&OIDs[i]);
  226. siggen_KAT(&OIDs[i]);
  227. }
  228. }