lms.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * The LMS stateful-hash public-key signature scheme
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * The following sources were referenced in the design of this implementation
  9. * of the LMS algorithm:
  10. *
  11. * [1] IETF RFC8554
  12. * D. McGrew, M. Curcio, S.Fluhrer
  13. * https://datatracker.ietf.org/doc/html/rfc8554
  14. *
  15. * [2] NIST Special Publication 800-208
  16. * David A. Cooper et. al.
  17. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_LMS_C)
  21. #include <string.h>
  22. #include "lmots.h"
  23. #include "psa/crypto.h"
  24. #include "psa_util_internal.h"
  25. #include "mbedtls/lms.h"
  26. #include "mbedtls/error.h"
  27. #include "mbedtls/platform_util.h"
  28. #include "mbedtls/platform.h"
  29. /* Define a local translating function to save code size by not using too many
  30. * arguments in each translating place. */
  31. static int local_err_translation(psa_status_t status)
  32. {
  33. return psa_status_to_mbedtls(status, psa_to_lms_errors,
  34. ARRAY_LENGTH(psa_to_lms_errors),
  35. psa_generic_status_to_mbedtls);
  36. }
  37. #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
  38. #define SIG_Q_LEAF_ID_OFFSET (0)
  39. #define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
  40. MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
  41. #define SIG_TYPE_OFFSET(otstype) (SIG_OTS_SIG_OFFSET + \
  42. MBEDTLS_LMOTS_SIG_LEN(otstype))
  43. #define SIG_PATH_OFFSET(otstype) (SIG_TYPE_OFFSET(otstype) + \
  44. MBEDTLS_LMS_TYPE_LEN)
  45. #define PUBLIC_KEY_TYPE_OFFSET (0)
  46. #define PUBLIC_KEY_OTSTYPE_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
  47. MBEDTLS_LMS_TYPE_LEN)
  48. #define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_OTSTYPE_OFFSET + \
  49. MBEDTLS_LMOTS_TYPE_LEN)
  50. #define PUBLIC_KEY_ROOT_NODE_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
  51. MBEDTLS_LMOTS_I_KEY_ID_LEN)
  52. /* Currently only support H=10 */
  53. #define H_TREE_HEIGHT_MAX 10
  54. #define MERKLE_TREE_NODE_AM(type) ((size_t) 1 << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
  55. #define MERKLE_TREE_LEAF_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
  56. #define MERKLE_TREE_INTERNAL_NODE_AM(type) ((unsigned int) \
  57. (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type)))
  58. #define D_CONST_LEN (2)
  59. static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = { 0x82, 0x82 };
  60. static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = { 0x83, 0x83 };
  61. /* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
  62. * public key and some other parameters like the leaf index). This function
  63. * implements RFC8554 section 5.3, in the case where r >= 2^h.
  64. *
  65. * params The LMS parameter set, the underlying LMOTS
  66. * parameter set, and I value which describe the key
  67. * being used.
  68. *
  69. * pub_key The public key of the private whose index
  70. * corresponds to the index of this leaf node. This
  71. * is a hash output.
  72. *
  73. * r_node_idx The index of this node in the Merkle tree. Note
  74. * that the root node of the Merkle tree is
  75. * 1-indexed.
  76. *
  77. * out The output node value, which is a hash output.
  78. */
  79. static int create_merkle_leaf_value(const mbedtls_lms_parameters_t *params,
  80. unsigned char *pub_key,
  81. unsigned int r_node_idx,
  82. unsigned char *out)
  83. {
  84. psa_hash_operation_t op;
  85. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  86. size_t output_hash_len;
  87. unsigned char r_node_idx_bytes[4];
  88. op = psa_hash_operation_init();
  89. status = psa_hash_setup(&op, PSA_ALG_SHA_256);
  90. if (status != PSA_SUCCESS) {
  91. goto exit;
  92. }
  93. status = psa_hash_update(&op, params->I_key_identifier,
  94. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  95. if (status != PSA_SUCCESS) {
  96. goto exit;
  97. }
  98. MBEDTLS_PUT_UINT32_BE(r_node_idx, r_node_idx_bytes, 0);
  99. status = psa_hash_update(&op, r_node_idx_bytes, 4);
  100. if (status != PSA_SUCCESS) {
  101. goto exit;
  102. }
  103. status = psa_hash_update(&op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN);
  104. if (status != PSA_SUCCESS) {
  105. goto exit;
  106. }
  107. status = psa_hash_update(&op, pub_key,
  108. MBEDTLS_LMOTS_N_HASH_LEN(params->otstype));
  109. if (status != PSA_SUCCESS) {
  110. goto exit;
  111. }
  112. status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
  113. &output_hash_len);
  114. if (status != PSA_SUCCESS) {
  115. goto exit;
  116. }
  117. exit:
  118. psa_hash_abort(&op);
  119. return PSA_TO_MBEDTLS_ERR(status);
  120. }
  121. /* Calculate the value of an internal node of the Merkle tree (which is a hash
  122. * of a public key and some other parameters like the node index). This function
  123. * implements RFC8554 section 5.3, in the case where r < 2^h.
  124. *
  125. * params The LMS parameter set, the underlying LMOTS
  126. * parameter set, and I value which describe the key
  127. * being used.
  128. *
  129. * left_node The value of the child of this node which is on
  130. * the left-hand side. As with all nodes on the
  131. * Merkle tree, this is a hash output.
  132. *
  133. * right_node The value of the child of this node which is on
  134. * the right-hand side. As with all nodes on the
  135. * Merkle tree, this is a hash output.
  136. *
  137. * r_node_idx The index of this node in the Merkle tree. Note
  138. * that the root node of the Merkle tree is
  139. * 1-indexed.
  140. *
  141. * out The output node value, which is a hash output.
  142. */
  143. static int create_merkle_internal_value(const mbedtls_lms_parameters_t *params,
  144. const unsigned char *left_node,
  145. const unsigned char *right_node,
  146. unsigned int r_node_idx,
  147. unsigned char *out)
  148. {
  149. psa_hash_operation_t op;
  150. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  151. size_t output_hash_len;
  152. unsigned char r_node_idx_bytes[4];
  153. op = psa_hash_operation_init();
  154. status = psa_hash_setup(&op, PSA_ALG_SHA_256);
  155. if (status != PSA_SUCCESS) {
  156. goto exit;
  157. }
  158. status = psa_hash_update(&op, params->I_key_identifier,
  159. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  160. if (status != PSA_SUCCESS) {
  161. goto exit;
  162. }
  163. MBEDTLS_PUT_UINT32_BE(r_node_idx, r_node_idx_bytes, 0);
  164. status = psa_hash_update(&op, r_node_idx_bytes, 4);
  165. if (status != PSA_SUCCESS) {
  166. goto exit;
  167. }
  168. status = psa_hash_update(&op, D_INTR_CONSTANT_BYTES, D_CONST_LEN);
  169. if (status != PSA_SUCCESS) {
  170. goto exit;
  171. }
  172. status = psa_hash_update(&op, left_node,
  173. MBEDTLS_LMS_M_NODE_BYTES(params->type));
  174. if (status != PSA_SUCCESS) {
  175. goto exit;
  176. }
  177. status = psa_hash_update(&op, right_node,
  178. MBEDTLS_LMS_M_NODE_BYTES(params->type));
  179. if (status != PSA_SUCCESS) {
  180. goto exit;
  181. }
  182. status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
  183. &output_hash_len);
  184. if (status != PSA_SUCCESS) {
  185. goto exit;
  186. }
  187. exit:
  188. psa_hash_abort(&op);
  189. return PSA_TO_MBEDTLS_ERR(status);
  190. }
  191. void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)
  192. {
  193. memset(ctx, 0, sizeof(*ctx));
  194. }
  195. void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx)
  196. {
  197. if (ctx == NULL) {
  198. return;
  199. }
  200. mbedtls_platform_zeroize(ctx, sizeof(*ctx));
  201. }
  202. int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx,
  203. const unsigned char *key, size_t key_size)
  204. {
  205. mbedtls_lms_algorithm_type_t type;
  206. mbedtls_lmots_algorithm_type_t otstype;
  207. type = (mbedtls_lms_algorithm_type_t) MBEDTLS_GET_UINT32_BE(key, PUBLIC_KEY_TYPE_OFFSET);
  208. if (type != MBEDTLS_LMS_SHA256_M32_H10) {
  209. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  210. }
  211. ctx->params.type = type;
  212. if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
  213. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  214. }
  215. otstype = (mbedtls_lmots_algorithm_type_t)
  216. MBEDTLS_GET_UINT32_BE(key, PUBLIC_KEY_OTSTYPE_OFFSET);
  217. if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
  218. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  219. }
  220. ctx->params.otstype = otstype;
  221. memcpy(ctx->params.I_key_identifier,
  222. key + PUBLIC_KEY_I_KEY_ID_OFFSET,
  223. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  224. memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
  225. MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
  226. ctx->have_public_key = 1;
  227. return 0;
  228. }
  229. int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
  230. unsigned char *key,
  231. size_t key_size, size_t *key_len)
  232. {
  233. if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
  234. return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
  235. }
  236. if (!ctx->have_public_key) {
  237. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  238. }
  239. MBEDTLS_PUT_UINT32_BE(ctx->params.type, key, PUBLIC_KEY_TYPE_OFFSET);
  240. MBEDTLS_PUT_UINT32_BE(ctx->params.otstype, key, PUBLIC_KEY_OTSTYPE_OFFSET);
  241. memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
  242. ctx->params.I_key_identifier,
  243. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  244. memcpy(key +PUBLIC_KEY_ROOT_NODE_OFFSET,
  245. ctx->T_1_pub_key,
  246. MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
  247. if (key_len != NULL) {
  248. *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
  249. }
  250. return 0;
  251. }
  252. int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx,
  253. const unsigned char *msg, size_t msg_size,
  254. const unsigned char *sig, size_t sig_size)
  255. {
  256. unsigned int q_leaf_identifier;
  257. unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
  258. unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
  259. unsigned int height;
  260. unsigned int curr_node_id;
  261. unsigned int parent_node_id;
  262. const unsigned char *left_node;
  263. const unsigned char *right_node;
  264. mbedtls_lmots_parameters_t ots_params;
  265. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  266. if (!ctx->have_public_key) {
  267. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  268. }
  269. if (ctx->params.type
  270. != MBEDTLS_LMS_SHA256_M32_H10) {
  271. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  272. }
  273. if (ctx->params.otstype
  274. != MBEDTLS_LMOTS_SHA256_N32_W8) {
  275. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  276. }
  277. if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
  278. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  279. }
  280. if (sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
  281. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  282. }
  283. if (MBEDTLS_GET_UINT32_BE(sig, SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
  284. != MBEDTLS_LMOTS_SHA256_N32_W8) {
  285. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  286. }
  287. if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
  288. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  289. }
  290. if (MBEDTLS_GET_UINT32_BE(sig, SIG_TYPE_OFFSET(ctx->params.otstype))
  291. != MBEDTLS_LMS_SHA256_M32_H10) {
  292. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  293. }
  294. q_leaf_identifier = MBEDTLS_GET_UINT32_BE(sig, SIG_Q_LEAF_ID_OFFSET);
  295. if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
  296. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  297. }
  298. memcpy(ots_params.I_key_identifier,
  299. ctx->params.I_key_identifier,
  300. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  301. MBEDTLS_PUT_UINT32_BE(q_leaf_identifier, ots_params.q_leaf_identifier, 0);
  302. ots_params.type = ctx->params.otstype;
  303. ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
  304. msg,
  305. msg_size,
  306. sig + SIG_OTS_SIG_OFFSET,
  307. MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
  308. Kc_candidate_ots_pub_key,
  309. sizeof(Kc_candidate_ots_pub_key),
  310. NULL);
  311. if (ret != 0) {
  312. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  313. }
  314. create_merkle_leaf_value(
  315. &ctx->params,
  316. Kc_candidate_ots_pub_key,
  317. MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
  318. Tc_candidate_root_node);
  319. curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
  320. q_leaf_identifier;
  321. for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
  322. height++) {
  323. parent_node_id = curr_node_id / 2;
  324. /* Left/right node ordering matters for the hash */
  325. if (curr_node_id & 1) {
  326. left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
  327. height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
  328. right_node = Tc_candidate_root_node;
  329. } else {
  330. left_node = Tc_candidate_root_node;
  331. right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
  332. height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
  333. }
  334. create_merkle_internal_value(&ctx->params, left_node, right_node,
  335. parent_node_id, Tc_candidate_root_node);
  336. curr_node_id /= 2;
  337. }
  338. if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
  339. MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
  340. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  341. }
  342. return 0;
  343. }
  344. #if defined(MBEDTLS_LMS_PRIVATE)
  345. /* Calculate a full Merkle tree based on a private key. This function
  346. * implements RFC8554 section 5.3, and is used to generate a public key (as the
  347. * public key is the root node of the Merkle tree).
  348. *
  349. * ctx The LMS private context, containing a parameter
  350. * set and private key material consisting of both
  351. * public and private OTS.
  352. *
  353. * tree The output tree, which is 2^(H + 1) hash outputs.
  354. * In the case of H=10 we have 2048 tree nodes (of
  355. * which 1024 of them are leaf nodes). Note that
  356. * because the Merkle tree root is 1-indexed, the 0
  357. * index tree node is never used.
  358. */
  359. static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
  360. unsigned char *tree)
  361. {
  362. unsigned int priv_key_idx;
  363. unsigned int r_node_idx;
  364. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  365. /* First create the leaf nodes, in ascending order */
  366. for (priv_key_idx = 0;
  367. priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
  368. priv_key_idx++) {
  369. r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
  370. ret = create_merkle_leaf_value(&ctx->params,
  371. ctx->ots_public_keys[priv_key_idx].public_key,
  372. r_node_idx,
  373. &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(
  374. ctx->params.type)]);
  375. if (ret != 0) {
  376. return ret;
  377. }
  378. }
  379. /* Then the internal nodes, in reverse order so that we can guarantee the
  380. * parent has been created */
  381. for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
  382. r_node_idx > 0;
  383. r_node_idx--) {
  384. ret = create_merkle_internal_value(&ctx->params,
  385. &tree[(r_node_idx * 2) *
  386. MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
  387. &tree[(r_node_idx * 2 + 1) *
  388. MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
  389. r_node_idx,
  390. &tree[r_node_idx *
  391. MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]);
  392. if (ret != 0) {
  393. return ret;
  394. }
  395. }
  396. return 0;
  397. }
  398. /* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
  399. * and return the full path. This function implements RFC8554 section 5.4.1, as
  400. * the Merkle path is the main component of an LMS signature.
  401. *
  402. * ctx The LMS private context, containing a parameter
  403. * set and private key material consisting of both
  404. * public and private OTS.
  405. *
  406. * leaf_node_id Which leaf node to calculate the path from.
  407. *
  408. * path The output path, which is H hash outputs.
  409. */
  410. static int get_merkle_path(mbedtls_lms_private_t *ctx,
  411. unsigned int leaf_node_id,
  412. unsigned char *path)
  413. {
  414. const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
  415. unsigned int curr_node_id = leaf_node_id;
  416. unsigned int adjacent_node_id;
  417. unsigned char *tree = NULL;
  418. unsigned int height;
  419. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  420. tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(ctx->params.type),
  421. node_bytes);
  422. if (tree == NULL) {
  423. return MBEDTLS_ERR_LMS_ALLOC_FAILED;
  424. }
  425. ret = calculate_merkle_tree(ctx, tree);
  426. if (ret != 0) {
  427. goto exit;
  428. }
  429. for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
  430. height++) {
  431. adjacent_node_id = curr_node_id ^ 1;
  432. memcpy(&path[height * node_bytes],
  433. &tree[adjacent_node_id * node_bytes], node_bytes);
  434. curr_node_id >>= 1;
  435. }
  436. ret = 0;
  437. exit:
  438. mbedtls_zeroize_and_free(tree, node_bytes *
  439. (size_t) MERKLE_TREE_NODE_AM(ctx->params.type));
  440. return ret;
  441. }
  442. void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
  443. {
  444. memset(ctx, 0, sizeof(*ctx));
  445. }
  446. void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
  447. {
  448. if (ctx == NULL) {
  449. return;
  450. }
  451. unsigned int idx;
  452. if (ctx->have_private_key) {
  453. if (ctx->ots_private_keys != NULL) {
  454. for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
  455. mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
  456. }
  457. }
  458. if (ctx->ots_public_keys != NULL) {
  459. for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
  460. mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
  461. }
  462. }
  463. mbedtls_free(ctx->ots_private_keys);
  464. mbedtls_free(ctx->ots_public_keys);
  465. }
  466. mbedtls_platform_zeroize(ctx, sizeof(*ctx));
  467. }
  468. int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
  469. mbedtls_lms_algorithm_type_t type,
  470. mbedtls_lmots_algorithm_type_t otstype,
  471. int (*f_rng)(void *, unsigned char *, size_t),
  472. void *p_rng, const unsigned char *seed,
  473. size_t seed_size)
  474. {
  475. unsigned int idx = 0;
  476. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  477. if (type != MBEDTLS_LMS_SHA256_M32_H10) {
  478. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  479. }
  480. if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
  481. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  482. }
  483. if (ctx->have_private_key) {
  484. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  485. }
  486. ctx->params.type = type;
  487. ctx->params.otstype = otstype;
  488. ctx->have_private_key = 1;
  489. ret = f_rng(p_rng,
  490. ctx->params.I_key_identifier,
  491. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  492. if (ret != 0) {
  493. goto exit;
  494. }
  495. /* Requires a cast to size_t to avoid an implicit cast warning on certain
  496. * platforms (particularly Windows) */
  497. ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
  498. sizeof(*ctx->ots_private_keys));
  499. if (ctx->ots_private_keys == NULL) {
  500. ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
  501. goto exit;
  502. }
  503. /* Requires a cast to size_t to avoid an implicit cast warning on certain
  504. * platforms (particularly Windows) */
  505. ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
  506. sizeof(*ctx->ots_public_keys));
  507. if (ctx->ots_public_keys == NULL) {
  508. ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
  509. goto exit;
  510. }
  511. for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
  512. mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
  513. mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]);
  514. }
  515. for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
  516. ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx],
  517. otstype,
  518. ctx->params.I_key_identifier,
  519. idx, seed, seed_size);
  520. if (ret != 0) {
  521. goto exit;
  522. }
  523. ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
  524. &ctx->ots_private_keys[idx]);
  525. if (ret != 0) {
  526. goto exit;
  527. }
  528. }
  529. ctx->q_next_usable_key = 0;
  530. exit:
  531. if (ret != 0) {
  532. mbedtls_lms_private_free(ctx);
  533. }
  534. return ret;
  535. }
  536. int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
  537. const mbedtls_lms_private_t *priv_ctx)
  538. {
  539. const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
  540. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  541. unsigned char *tree = NULL;
  542. if (!priv_ctx->have_private_key) {
  543. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  544. }
  545. if (priv_ctx->params.type
  546. != MBEDTLS_LMS_SHA256_M32_H10) {
  547. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  548. }
  549. if (priv_ctx->params.otstype
  550. != MBEDTLS_LMOTS_SHA256_N32_W8) {
  551. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  552. }
  553. tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(priv_ctx->params.type),
  554. node_bytes);
  555. if (tree == NULL) {
  556. return MBEDTLS_ERR_LMS_ALLOC_FAILED;
  557. }
  558. memcpy(&ctx->params, &priv_ctx->params,
  559. sizeof(mbedtls_lmots_parameters_t));
  560. ret = calculate_merkle_tree(priv_ctx, tree);
  561. if (ret != 0) {
  562. goto exit;
  563. }
  564. /* Root node is always at position 1, due to 1-based indexing */
  565. memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
  566. ctx->have_public_key = 1;
  567. ret = 0;
  568. exit:
  569. mbedtls_zeroize_and_free(tree, node_bytes *
  570. (size_t) MERKLE_TREE_NODE_AM(priv_ctx->params.type));
  571. return ret;
  572. }
  573. int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
  574. int (*f_rng)(void *, unsigned char *, size_t),
  575. void *p_rng, const unsigned char *msg,
  576. unsigned int msg_size, unsigned char *sig, size_t sig_size,
  577. size_t *sig_len)
  578. {
  579. uint32_t q_leaf_identifier;
  580. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  581. if (!ctx->have_private_key) {
  582. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  583. }
  584. if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
  585. return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
  586. }
  587. if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
  588. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  589. }
  590. if (ctx->params.otstype
  591. != MBEDTLS_LMOTS_SHA256_N32_W8) {
  592. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  593. }
  594. if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
  595. return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
  596. }
  597. q_leaf_identifier = ctx->q_next_usable_key;
  598. /* This new value must _always_ be written back to the disk before the
  599. * signature is returned.
  600. */
  601. ctx->q_next_usable_key += 1;
  602. if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
  603. < SIG_OTS_SIG_OFFSET) {
  604. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  605. }
  606. ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
  607. f_rng,
  608. p_rng,
  609. msg,
  610. msg_size,
  611. sig + SIG_OTS_SIG_OFFSET,
  612. MBEDTLS_LMS_SIG_LEN(ctx->params.type,
  613. ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
  614. NULL);
  615. if (ret != 0) {
  616. return ret;
  617. }
  618. MBEDTLS_PUT_UINT32_BE(ctx->params.type, sig, SIG_TYPE_OFFSET(ctx->params.otstype));
  619. MBEDTLS_PUT_UINT32_BE(q_leaf_identifier, sig, SIG_Q_LEAF_ID_OFFSET);
  620. ret = get_merkle_path(ctx,
  621. MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
  622. sig + SIG_PATH_OFFSET(ctx->params.otstype));
  623. if (ret != 0) {
  624. return ret;
  625. }
  626. if (sig_len != NULL) {
  627. *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
  628. }
  629. return 0;
  630. }
  631. #endif /* defined(MBEDTLS_LMS_PRIVATE) */
  632. #endif /* defined(MBEDTLS_LMS_C) */