lms.c 27 KB

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