您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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