Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

497 строки
12 KiB

  1. #include <stdint.h>
  2. #include <string.h>
  3. #include "params.h"
  4. #include "xmss_core.h"
  5. int xmss_str_to_oid(uint32_t *oid, const char *s)
  6. {
  7. if (!strcmp(s, "XMSS-SHA2_10_256")) {
  8. *oid = 0x00000001;
  9. }
  10. else if (!strcmp(s, "XMSS-SHA2_16_256")) {
  11. *oid = 0x00000002;
  12. }
  13. else if (!strcmp(s, "XMSS-SHA2_20_256")) {
  14. *oid = 0x00000003;
  15. }
  16. else if (!strcmp(s, "XMSS-SHA2_10_512")) {
  17. *oid = 0x00000004;
  18. }
  19. else if (!strcmp(s, "XMSS-SHA2_16_512")) {
  20. *oid = 0x00000005;
  21. }
  22. else if (!strcmp(s, "XMSS-SHA2_20_512")) {
  23. *oid = 0x00000006;
  24. }
  25. else if (!strcmp(s, "XMSS-SHAKE_10_256")) {
  26. *oid = 0x00000007;
  27. }
  28. else if (!strcmp(s, "XMSS-SHAKE_16_256")) {
  29. *oid = 0x00000008;
  30. }
  31. else if (!strcmp(s, "XMSS-SHAKE_20_256")) {
  32. *oid = 0x00000009;
  33. }
  34. else if (!strcmp(s, "XMSS-SHAKE_10_512")) {
  35. *oid = 0x0000000a;
  36. }
  37. else if (!strcmp(s, "XMSS-SHAKE_16_512")) {
  38. *oid = 0x0000000b;
  39. }
  40. else if (!strcmp(s, "XMSS-SHAKE_20_512")) {
  41. *oid = 0x0000000c;
  42. }
  43. else {
  44. return -1;
  45. }
  46. return 0;
  47. }
  48. int xmssmt_str_to_oid(uint32_t *oid, const char *s)
  49. {
  50. if (!strcmp(s, "XMSSMT-SHA2_20/2_256")) {
  51. *oid = 0x00000001;
  52. }
  53. else if (!strcmp(s, "XMSSMT-SHA2_20/4_256")) {
  54. *oid = 0x00000002;
  55. }
  56. else if (!strcmp(s, "XMSSMT-SHA2_40/2_256")) {
  57. *oid = 0x00000003;
  58. }
  59. else if (!strcmp(s, "XMSSMT-SHA2_40/4_256")) {
  60. *oid = 0x00000004;
  61. }
  62. else if (!strcmp(s, "XMSSMT-SHA2_40/8_256")) {
  63. *oid = 0x00000005;
  64. }
  65. else if (!strcmp(s, "XMSSMT-SHA2_60/3_256")) {
  66. *oid = 0x00000006;
  67. }
  68. else if (!strcmp(s, "XMSSMT-SHA2_60/6_256")) {
  69. *oid = 0x00000007;
  70. }
  71. else if (!strcmp(s, "XMSSMT-SHA2_60/12_256")) {
  72. *oid = 0x00000008;
  73. }
  74. else if (!strcmp(s, "XMSSMT-SHA2_20/2_512")) {
  75. *oid = 0x00000009;
  76. }
  77. else if (!strcmp(s, "XMSSMT-SHA2_20/4_512")) {
  78. *oid = 0x0000000a;
  79. }
  80. else if (!strcmp(s, "XMSSMT-SHA2_40/2_512")) {
  81. *oid = 0x0000000b;
  82. }
  83. else if (!strcmp(s, "XMSSMT-SHA2_40/4_512")) {
  84. *oid = 0x0000000c;
  85. }
  86. else if (!strcmp(s, "XMSSMT-SHA2_40/8_512")) {
  87. *oid = 0x0000000d;
  88. }
  89. else if (!strcmp(s, "XMSSMT-SHA2_60/3_512")) {
  90. *oid = 0x0000000e;
  91. }
  92. else if (!strcmp(s, "XMSSMT-SHA2_60/6_512")) {
  93. *oid = 0x0000000f;
  94. }
  95. else if (!strcmp(s, "XMSSMT-SHA2_60/12_512")) {
  96. *oid = 0x00000010;
  97. }
  98. else if (!strcmp(s, "XMSSMT-SHAKE_20/2_256")) {
  99. *oid = 0x00000011;
  100. }
  101. else if (!strcmp(s, "XMSSMT-SHAKE_20/4_256")) {
  102. *oid = 0x00000012;
  103. }
  104. else if (!strcmp(s, "XMSSMT-SHAKE_40/2_256")) {
  105. *oid = 0x00000013;
  106. }
  107. else if (!strcmp(s, "XMSSMT-SHAKE_40/4_256")) {
  108. *oid = 0x00000014;
  109. }
  110. else if (!strcmp(s, "XMSSMT-SHAKE_40/8_256")) {
  111. *oid = 0x00000015;
  112. }
  113. else if (!strcmp(s, "XMSSMT-SHAKE_60/3_256")) {
  114. *oid = 0x00000016;
  115. }
  116. else if (!strcmp(s, "XMSSMT-SHAKE_60/6_256")) {
  117. *oid = 0x00000017;
  118. }
  119. else if (!strcmp(s, "XMSSMT-SHAKE_60/12_256")) {
  120. *oid = 0x00000018;
  121. }
  122. else if (!strcmp(s, "XMSSMT-SHAKE_20/2_512")) {
  123. *oid = 0x00000019;
  124. }
  125. else if (!strcmp(s, "XMSSMT-SHAKE_20/4_512")) {
  126. *oid = 0x0000001a;
  127. }
  128. else if (!strcmp(s, "XMSSMT-SHAKE_40/2_512")) {
  129. *oid = 0x0000001b;
  130. }
  131. else if (!strcmp(s, "XMSSMT-SHAKE_40/4_512")) {
  132. *oid = 0x0000001c;
  133. }
  134. else if (!strcmp(s, "XMSSMT-SHAKE_40/8_512")) {
  135. *oid = 0x0000001d;
  136. }
  137. else if (!strcmp(s, "XMSSMT-SHAKE_60/3_512")) {
  138. *oid = 0x0000001e;
  139. }
  140. else if (!strcmp(s, "XMSSMT-SHAKE_60/6_512")) {
  141. *oid = 0x0000001f;
  142. }
  143. else if (!strcmp(s, "XMSSMT-SHAKE_60/12_512")) {
  144. *oid = 0x00000020;
  145. }
  146. else {
  147. return -1;
  148. }
  149. return 0;
  150. }
  151. int xmss_parse_oid(xmss_params *params, const uint32_t oid)
  152. {
  153. switch (oid) {
  154. case 0x00000001:
  155. case 0x00000002:
  156. case 0x00000003:
  157. case 0x00000004:
  158. case 0x00000005:
  159. case 0x00000006:
  160. params->func = XMSS_SHA2;
  161. break;
  162. case 0x00000007:
  163. case 0x00000008:
  164. case 0x00000009:
  165. case 0x0000000a:
  166. case 0x0000000b:
  167. case 0x0000000c:
  168. params->func = XMSS_SHAKE;
  169. break;
  170. default:
  171. return -1;
  172. }
  173. switch (oid) {
  174. case 0x00000001:
  175. case 0x00000002:
  176. case 0x00000003:
  177. case 0x00000007:
  178. case 0x00000008:
  179. case 0x00000009:
  180. params->n = 32;
  181. break;
  182. case 0x00000004:
  183. case 0x00000005:
  184. case 0x00000006:
  185. case 0x0000000a:
  186. case 0x0000000b:
  187. case 0x0000000c:
  188. params->n = 64;
  189. break;
  190. default:
  191. return -1;
  192. }
  193. switch (oid) {
  194. case 0x00000001:
  195. case 0x00000004:
  196. case 0x00000007:
  197. case 0x0000000a:
  198. params->full_height = 10;
  199. break;
  200. case 0x00000002:
  201. case 0x00000005:
  202. case 0x00000008:
  203. case 0x0000000b:
  204. params->full_height = 16;
  205. break;
  206. case 0x00000003:
  207. case 0x00000006:
  208. case 0x00000009:
  209. case 0x0000000c:
  210. params->full_height = 20;
  211. break;
  212. default:
  213. return -1;
  214. }
  215. params->d = 1;
  216. params->wots_w = 16;
  217. // TODO figure out sensible and legal values for this based on the above
  218. params->bds_k = 0;
  219. return xmss_xmssmt_initialize_params(params);
  220. }
  221. int xmssmt_parse_oid(xmss_params *params, const uint32_t oid)
  222. {
  223. switch (oid) {
  224. case 0x00000001:
  225. case 0x00000002:
  226. case 0x00000003:
  227. case 0x00000004:
  228. case 0x00000005:
  229. case 0x00000006:
  230. case 0x00000007:
  231. case 0x00000008:
  232. case 0x00000009:
  233. case 0x0000000a:
  234. case 0x0000000b:
  235. case 0x0000000c:
  236. case 0x0000000d:
  237. case 0x0000000e:
  238. case 0x0000000f:
  239. case 0x00000010:
  240. params->func = XMSS_SHA2;
  241. break;
  242. case 0x00000011:
  243. case 0x00000012:
  244. case 0x00000013:
  245. case 0x00000014:
  246. case 0x00000015:
  247. case 0x00000016:
  248. case 0x00000017:
  249. case 0x00000018:
  250. case 0x00000019:
  251. case 0x0000001a:
  252. case 0x0000001b:
  253. case 0x0000001c:
  254. case 0x0000001e:
  255. case 0x0000001d:
  256. case 0x0000001f:
  257. case 0x00000020:
  258. params->func = XMSS_SHAKE;
  259. break;
  260. default:
  261. return -1;
  262. }
  263. switch (oid) {
  264. case 0x00000001:
  265. case 0x00000002:
  266. case 0x00000003:
  267. case 0x00000004:
  268. case 0x00000005:
  269. case 0x00000006:
  270. case 0x00000007:
  271. case 0x00000008:
  272. case 0x00000011:
  273. case 0x00000012:
  274. case 0x00000013:
  275. case 0x00000014:
  276. case 0x00000015:
  277. case 0x00000016:
  278. case 0x00000017:
  279. case 0x00000018:
  280. params->n = 32;
  281. break;
  282. case 0x00000009:
  283. case 0x0000000a:
  284. case 0x0000000b:
  285. case 0x0000000c:
  286. case 0x0000000d:
  287. case 0x0000000e:
  288. case 0x0000000f:
  289. case 0x00000010:
  290. case 0x00000019:
  291. case 0x0000001a:
  292. case 0x0000001b:
  293. case 0x0000001c:
  294. case 0x0000001d:
  295. case 0x0000001e:
  296. case 0x0000001f:
  297. case 0x00000020:
  298. params->n = 64;
  299. break;
  300. default:
  301. return -1;
  302. }
  303. switch (oid) {
  304. case 0x00000001:
  305. case 0x00000002:
  306. case 0x00000009:
  307. case 0x0000000a:
  308. case 0x00000011:
  309. case 0x00000012:
  310. case 0x00000019:
  311. case 0x0000001a:
  312. params->full_height = 20;
  313. break;
  314. case 0x00000003:
  315. case 0x00000004:
  316. case 0x00000005:
  317. case 0x0000000b:
  318. case 0x0000000c:
  319. case 0x0000000d:
  320. case 0x00000013:
  321. case 0x00000014:
  322. case 0x00000015:
  323. case 0x0000001b:
  324. case 0x0000001c:
  325. case 0x0000001d:
  326. params->full_height = 40;
  327. break;
  328. case 0x00000006:
  329. case 0x00000007:
  330. case 0x00000008:
  331. case 0x0000000e:
  332. case 0x0000000f:
  333. case 0x00000010:
  334. case 0x00000016:
  335. case 0x00000017:
  336. case 0x00000018:
  337. case 0x0000001e:
  338. case 0x0000001f:
  339. case 0x00000020:
  340. params->full_height = 60;
  341. break;
  342. default:
  343. return -1;
  344. }
  345. switch (oid) {
  346. case 0x00000001:
  347. case 0x00000003:
  348. case 0x00000009:
  349. case 0x0000000b:
  350. case 0x00000011:
  351. case 0x00000013:
  352. case 0x00000019:
  353. case 0x0000001b:
  354. params->d = 2;
  355. break;
  356. case 0x00000002:
  357. case 0x00000004:
  358. case 0x0000000a:
  359. case 0x0000000c:
  360. case 0x00000012:
  361. case 0x00000014:
  362. case 0x0000001a:
  363. case 0x0000001c:
  364. params->d = 4;
  365. break;
  366. case 0x00000005:
  367. case 0x0000000d:
  368. case 0x00000015:
  369. case 0x0000001d:
  370. params->d = 8;
  371. break;
  372. case 0x00000006:
  373. case 0x0000000e:
  374. case 0x00000016:
  375. case 0x0000001e:
  376. params->d = 3;
  377. break;
  378. case 0x00000007:
  379. case 0x0000000f:
  380. case 0x00000017:
  381. case 0x0000001f:
  382. params->d = 6;
  383. break;
  384. case 0x00000008:
  385. case 0x00000010:
  386. case 0x00000018:
  387. case 0x00000020:
  388. params->d = 12;
  389. break;
  390. default:
  391. return -1;
  392. }
  393. params->wots_w = 16;
  394. // TODO figure out sensible and legal values for this based on the above
  395. params->bds_k = 0;
  396. return xmss_xmssmt_initialize_params(params);
  397. }
  398. /**
  399. * Given a params struct where the following properties have been initialized;
  400. * - full_height; the height of the complete (hyper)tree
  401. * - n; the number of bytes of hash function output
  402. * - d; the number of layers (d > 1 implies XMSSMT)
  403. * - func; one of {XMSS_SHA2, XMSS_SHAKE}
  404. * - wots_w; the Winternitz parameter
  405. * - optionally, bds_k; the BDS traversal trade-off parameter,
  406. * this function initializes the remainder of the params structure.
  407. */
  408. int xmss_xmssmt_initialize_params(xmss_params *params)
  409. {
  410. params->tree_height = params->full_height / params->d;
  411. if (params->wots_w == 4) {
  412. params->wots_log_w = 2;
  413. params->wots_len1 = 8 * params->n / params->wots_log_w;
  414. /* len_2 = floor(log(len_1 * (w - 1)) / log(w)) + 1 */
  415. params->wots_len2 = 5;
  416. }
  417. else if (params->wots_w == 16) {
  418. params->wots_log_w = 4;
  419. params->wots_len1 = 8 * params->n / params->wots_log_w;
  420. /* len_2 = floor(log(len_1 * (w - 1)) / log(w)) + 1 */
  421. params->wots_len2 = 3;
  422. }
  423. else if (params->wots_w == 256) {
  424. params->wots_log_w = 8;
  425. params->wots_len1 = 8 * params->n / params->wots_log_w;
  426. /* len_2 = floor(log(len_1 * (w - 1)) / log(w)) + 1 */
  427. params->wots_len2 = 2;
  428. }
  429. else {
  430. return -1;
  431. }
  432. params->wots_len = params->wots_len1 + params->wots_len2;
  433. params->wots_sig_bytes = params->wots_len * params->n;
  434. if (params->d == 1) { // Assume this is XMSS, not XMSS^MT
  435. /* In XMSS, always use fixed 4 bytes for index_bytes */
  436. params->index_bytes = 4;
  437. }
  438. else {
  439. /* In XMSS^MT, round index_bytes up to nearest byte. */
  440. params->index_bytes = (params->full_height + 7) / 8;
  441. }
  442. params->sig_bytes = (params->index_bytes + params->n
  443. + params->d * params->wots_sig_bytes
  444. + params->full_height * params->n);
  445. params->pk_bytes = 2 * params->n;
  446. params->sk_bytes = xmss_xmssmt_core_sk_bytes(params);
  447. return 0;
  448. }