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.

466 lines
11 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 = 0x01000001;
  9. }
  10. else if (!strcmp(s, "XMSS-SHA2_16_256")) {
  11. *oid = 0x02000002;
  12. }
  13. else if (!strcmp(s, "XMSS-SHA2_20_256")) {
  14. *oid = 0x03000003;
  15. }
  16. else if (!strcmp(s, "XMSS-SHA2_10_512")) {
  17. *oid = 0x04000004;
  18. }
  19. else if (!strcmp(s, "XMSS-SHA2_16_512")) {
  20. *oid = 0x05000005;
  21. }
  22. else if (!strcmp(s, "XMSS-SHA2_20_512")) {
  23. *oid = 0x06000006;
  24. }
  25. else if (!strcmp(s, "XMSS-SHAKE_10_256")) {
  26. *oid = 0x07000007;
  27. }
  28. else if (!strcmp(s, "XMSS-SHAKE_16_256")) {
  29. *oid = 0x08000008;
  30. }
  31. else if (!strcmp(s, "XMSS-SHAKE_20_256")) {
  32. *oid = 0x09000009;
  33. }
  34. else if (!strcmp(s, "XMSS-SHAKE_10_512")) {
  35. *oid = 0x0a00000a;
  36. }
  37. else if (!strcmp(s, "XMSS-SHAKE_16_512")) {
  38. *oid = 0x0b00000b;
  39. }
  40. else if (!strcmp(s, "XMSS-SHAKE_20_512")) {
  41. *oid = 0x0c00000c;
  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 = 0x01000001;
  52. }
  53. else if (!strcmp(s, "XMSSMT-SHA2_20/4_256")) {
  54. *oid = 0x02000002;
  55. }
  56. else if (!strcmp(s, "XMSSMT-SHA2_40/2_256")) {
  57. *oid = 0x03000003;
  58. }
  59. else if (!strcmp(s, "XMSSMT-SHA2_40/4_256")) {
  60. *oid = 0x04000004;
  61. }
  62. else if (!strcmp(s, "XMSSMT-SHA2_40/8_256")) {
  63. *oid = 0x05000005;
  64. }
  65. else if (!strcmp(s, "XMSSMT-SHA2_60/3_256")) {
  66. *oid = 0x06000006;
  67. }
  68. else if (!strcmp(s, "XMSSMT-SHA2_60/6_256")) {
  69. *oid = 0x07000007;
  70. }
  71. else if (!strcmp(s, "XMSSMT-SHA2_60/12_256")) {
  72. *oid = 0x08000008;
  73. }
  74. else if (!strcmp(s, "XMSSMT-SHA2_20/2_512")) {
  75. *oid = 0x09000009;
  76. }
  77. else if (!strcmp(s, "XMSSMT-SHA2_20/4_512")) {
  78. *oid = 0x0a00000a;
  79. }
  80. else if (!strcmp(s, "XMSSMT-SHA2_40/2_512")) {
  81. *oid = 0x0b00000b;
  82. }
  83. else if (!strcmp(s, "XMSSMT-SHA2_40/4_512")) {
  84. *oid = 0x0c00000c;
  85. }
  86. else if (!strcmp(s, "XMSSMT-SHA2_40/8_512")) {
  87. *oid = 0x0d00000d;
  88. }
  89. else if (!strcmp(s, "XMSSMT-SHA2_60/3_512")) {
  90. *oid = 0x0e00000e;
  91. }
  92. else if (!strcmp(s, "XMSSMT-SHA2_60/6_512")) {
  93. *oid = 0x0f00000f;
  94. }
  95. else if (!strcmp(s, "XMSSMT-SHA2_60/12_512")) {
  96. *oid = 0x01010101;
  97. }
  98. else if (!strcmp(s, "XMSSMT-SHAKE_20/2_256")) {
  99. *oid = 0x02010102;
  100. }
  101. else if (!strcmp(s, "XMSSMT-SHAKE_20/4_256")) {
  102. *oid = 0x03010103;
  103. }
  104. else if (!strcmp(s, "XMSSMT-SHAKE_40/2_256")) {
  105. *oid = 0x04010104;
  106. }
  107. else if (!strcmp(s, "XMSSMT-SHAKE_40/4_256")) {
  108. *oid = 0x05010105;
  109. }
  110. else if (!strcmp(s, "XMSSMT-SHAKE_40/8_256")) {
  111. *oid = 0x06010106;
  112. }
  113. else if (!strcmp(s, "XMSSMT-SHAKE_60/3_256")) {
  114. *oid = 0x07010107;
  115. }
  116. else if (!strcmp(s, "XMSSMT-SHAKE_60/6_256")) {
  117. *oid = 0x08010108;
  118. }
  119. else if (!strcmp(s, "XMSSMT-SHAKE_60/12_256")) {
  120. *oid = 0x09010109;
  121. }
  122. else if (!strcmp(s, "XMSSMT-SHAKE_20/2_512")) {
  123. *oid = 0x0a01010a;
  124. }
  125. else if (!strcmp(s, "XMSSMT-SHAKE_20/4_512")) {
  126. *oid = 0x0b01010b;
  127. }
  128. else if (!strcmp(s, "XMSSMT-SHAKE_40/2_512")) {
  129. *oid = 0x0c01010c;
  130. }
  131. else if (!strcmp(s, "XMSSMT-SHAKE_40/4_512")) {
  132. *oid = 0x0d01010d;
  133. }
  134. else if (!strcmp(s, "XMSSMT-SHAKE_40/8_512")) {
  135. *oid = 0x0e01010e;
  136. }
  137. else if (!strcmp(s, "XMSSMT-SHAKE_60/3_512")) {
  138. *oid = 0x0f01010f;
  139. }
  140. else if (!strcmp(s, "XMSSMT-SHAKE_60/6_512")) {
  141. *oid = 0x01020201;
  142. }
  143. else if (!strcmp(s, "XMSSMT-SHAKE_60/12_512")) {
  144. *oid = 0x02020202;
  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 0x01000001:
  155. case 0x02000002:
  156. case 0x03000003:
  157. case 0x04000004:
  158. case 0x05000005:
  159. case 0x06000006:
  160. params->func = XMSS_SHA2;
  161. break;
  162. case 0x07000007:
  163. case 0x08000008:
  164. case 0x09000009:
  165. case 0x0a00000a:
  166. case 0x0b00000b:
  167. case 0x0c00000c:
  168. params->func = XMSS_SHAKE;
  169. break;
  170. default:
  171. return 1;
  172. }
  173. switch (oid) {
  174. case 0x01000001:
  175. case 0x02000002:
  176. case 0x03000003:
  177. case 0x07000007:
  178. case 0x08000008:
  179. case 0x09000009:
  180. params->n = 32;
  181. break;
  182. case 0x04000004:
  183. case 0x05000005:
  184. case 0x06000006:
  185. case 0x0a00000a:
  186. case 0x0b00000b:
  187. case 0x0c00000c:
  188. params->n = 64;
  189. break;
  190. default:
  191. return 1;
  192. }
  193. switch (oid) {
  194. case 0x01000001:
  195. case 0x04000004:
  196. case 0x07000007:
  197. case 0x0a00000a:
  198. params->full_height = 10;
  199. break;
  200. case 0x02000002:
  201. case 0x05000005:
  202. case 0x08000008:
  203. case 0x0b00000b:
  204. params->full_height = 16;
  205. break;
  206. case 0x03000003:
  207. case 0x06000006:
  208. case 0x09000009:
  209. case 0x0c00000c:
  210. params->full_height = 20;
  211. break;
  212. default:
  213. return 1;
  214. }
  215. params->d = 1;
  216. params->tree_height = params->full_height / params->d;
  217. params->wots_w = 16;
  218. params->wots_log_w = 4;
  219. params->wots_len1 = 8 * params->n / params->wots_log_w;
  220. /* len_2 = floor(log(len_1 * (w - 1)) / log(w)) + 1 */
  221. params->wots_len2 = 3;
  222. params->wots_len = params->wots_len1 + params->wots_len2;
  223. params->wots_sig_bytes = params->wots_len * params->n;
  224. params->index_bytes = 4;
  225. params->sig_bytes = (params->index_bytes + params->n
  226. + params->d * params->wots_sig_bytes
  227. + params->full_height * params->n);
  228. params->pk_bytes = 2 * params->n;
  229. params->sk_bytes = xmss_core_sk_bytes(params);
  230. // TODO figure out sensible and legal values for this based on the above
  231. params->bds_k = 0;
  232. return 0;
  233. }
  234. int xmssmt_parse_oid(xmss_params *params, const uint32_t oid)
  235. {
  236. switch (oid) {
  237. case 0x01000001:
  238. case 0x02000002:
  239. case 0x03000003:
  240. case 0x04000004:
  241. case 0x05000005:
  242. case 0x06000006:
  243. case 0x07000007:
  244. case 0x08000008:
  245. case 0x09000009:
  246. case 0x0a00000a:
  247. case 0x0b00000b:
  248. case 0x0c00000c:
  249. case 0x0d00000d:
  250. case 0x0e00000e:
  251. case 0x0f00000f:
  252. case 0x01010101:
  253. params->func = XMSS_SHA2;
  254. break;
  255. case 0x02010102:
  256. case 0x03010103:
  257. case 0x04010104:
  258. case 0x05010105:
  259. case 0x06010106:
  260. case 0x07010107:
  261. case 0x08010108:
  262. case 0x09010109:
  263. case 0x0a01010a:
  264. case 0x0b01010b:
  265. case 0x0c01010c:
  266. case 0x0d01010d:
  267. case 0x0e01010e:
  268. case 0x0f01010f:
  269. case 0x01020201:
  270. case 0x02020202:
  271. params->func = XMSS_SHAKE;
  272. break;
  273. default:
  274. return 1;
  275. }
  276. switch (oid) {
  277. case 0x01000001:
  278. case 0x02000002:
  279. case 0x03000003:
  280. case 0x04000004:
  281. case 0x05000005:
  282. case 0x06000006:
  283. case 0x07000007:
  284. case 0x08000008:
  285. case 0x02010102:
  286. case 0x03010103:
  287. case 0x04010104:
  288. case 0x05010105:
  289. case 0x06010106:
  290. case 0x07010107:
  291. case 0x08010108:
  292. case 0x09010109:
  293. params->n = 32;
  294. break;
  295. case 0x09000009:
  296. case 0x0a00000a:
  297. case 0x0b00000b:
  298. case 0x0c00000c:
  299. case 0x0d00000d:
  300. case 0x0e00000e:
  301. case 0x0f00000f:
  302. case 0x01010101:
  303. case 0x0a01010a:
  304. case 0x0b01010b:
  305. case 0x0c01010c:
  306. case 0x0d01010d:
  307. case 0x0e01010e:
  308. case 0x0f01010f:
  309. case 0x01020201:
  310. case 0x02020202:
  311. params->n = 64;
  312. break;
  313. default:
  314. return 1;
  315. }
  316. switch (oid) {
  317. case 0x01000001:
  318. case 0x02000002:
  319. case 0x09000009:
  320. case 0x0a00000a:
  321. case 0x02010102:
  322. case 0x03010103:
  323. case 0x0a01010a:
  324. case 0x0b01010b:
  325. params->full_height = 20;
  326. break;
  327. case 0x03000003:
  328. case 0x04000004:
  329. case 0x05000005:
  330. case 0x0b00000b:
  331. case 0x0c00000c:
  332. case 0x0d00000d:
  333. case 0x04010104:
  334. case 0x05010105:
  335. case 0x06010106:
  336. case 0x0c01010c:
  337. case 0x0d01010d:
  338. case 0x0e01010e:
  339. params->full_height = 40;
  340. break;
  341. case 0x06000006:
  342. case 0x07000007:
  343. case 0x08000008:
  344. case 0x0e00000e:
  345. case 0x0f00000f:
  346. case 0x01010101:
  347. case 0x07010107:
  348. case 0x08010108:
  349. case 0x09010109:
  350. case 0x0f01010f:
  351. case 0x01020201:
  352. case 0x02020202:
  353. params->full_height = 60;
  354. break;
  355. default:
  356. return 1;
  357. }
  358. switch (oid) {
  359. case 0x01000001:
  360. case 0x03000003:
  361. case 0x09000009:
  362. case 0x0b00000b:
  363. case 0x02010102:
  364. case 0x04010104:
  365. case 0x0a01010a:
  366. case 0x0c01010c:
  367. params->d = 2;
  368. break;
  369. case 0x02000002:
  370. case 0x04000004:
  371. case 0x0a00000a:
  372. case 0x0c00000c:
  373. case 0x03010103:
  374. case 0x05010105:
  375. case 0x0b01010b:
  376. case 0x0d01010d:
  377. params->d = 4;
  378. break;
  379. case 0x05000005:
  380. case 0x0d00000d:
  381. case 0x06010106:
  382. case 0x0e01010e:
  383. params->d = 8;
  384. break;
  385. case 0x06000006:
  386. case 0x0e00000e:
  387. case 0x07010107:
  388. case 0x0f01010f:
  389. params->d = 3;
  390. break;
  391. case 0x07000007:
  392. case 0x0f00000f:
  393. case 0x08010108:
  394. case 0x01020201:
  395. params->d = 6;
  396. break;
  397. case 0x08000008:
  398. case 0x01010101:
  399. case 0x09010109:
  400. case 0x02020202:
  401. params->d = 12;
  402. break;
  403. default:
  404. return 1;
  405. }
  406. params->tree_height = params->full_height / params->d;
  407. params->wots_w = 16;
  408. params->wots_log_w = 4;
  409. params->wots_len1 = 8 * params->n / params->wots_log_w;
  410. /* len_2 = floor(log(len_1 * (w - 1)) / log(w)) + 1 */
  411. params->wots_len2 = 3;
  412. params->wots_len = params->wots_len1 + params->wots_len2;
  413. params->wots_sig_bytes = params->wots_len * params->n;
  414. /* Round index_bytes up to nearest byte. */
  415. params->index_bytes = (params->full_height + 7) / 8;
  416. params->sig_bytes = (params->index_bytes + params->n
  417. + params->d * params->wots_sig_bytes
  418. + params->full_height * params->n);
  419. params->pk_bytes = 2 * params->n;
  420. params->sk_bytes = xmssmt_core_sk_bytes(params);
  421. // TODO figure out sensible and legal values for this based on the above
  422. params->bds_k = 0;
  423. return 0;
  424. }