25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

469 lines
11 KiB

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