lmots.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * The LM-OTS one-time 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 LM-OTS 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 "mbedtls/lms.h"
  24. #include "mbedtls/platform_util.h"
  25. #include "mbedtls/error.h"
  26. #include "psa_util_internal.h"
  27. #include "psa/crypto.h"
  28. /* Define a local translating function to save code size by not using too many
  29. * arguments in each translating place. */
  30. static int local_err_translation(psa_status_t status)
  31. {
  32. return psa_status_to_mbedtls(status, psa_to_lms_errors,
  33. ARRAY_LENGTH(psa_to_lms_errors),
  34. psa_generic_status_to_mbedtls);
  35. }
  36. #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
  37. #define PUBLIC_KEY_TYPE_OFFSET (0)
  38. #define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
  39. MBEDTLS_LMOTS_TYPE_LEN)
  40. #define PUBLIC_KEY_Q_LEAF_ID_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
  41. MBEDTLS_LMOTS_I_KEY_ID_LEN)
  42. #define PUBLIC_KEY_KEY_HASH_OFFSET (PUBLIC_KEY_Q_LEAF_ID_OFFSET + \
  43. MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
  44. /* We only support parameter sets that use 8-bit digits, as it does not require
  45. * translation logic between digits and bytes */
  46. #define W_WINTERNITZ_PARAMETER (8u)
  47. #define CHECKSUM_LEN (2)
  48. #define I_DIGIT_IDX_LEN (2)
  49. #define J_HASH_IDX_LEN (1)
  50. #define D_CONST_LEN (2)
  51. #define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
  52. #define D_CONST_LEN (2)
  53. static const unsigned char D_PUBLIC_CONSTANT_BYTES[D_CONST_LEN] = { 0x80, 0x80 };
  54. static const unsigned char D_MESSAGE_CONSTANT_BYTES[D_CONST_LEN] = { 0x81, 0x81 };
  55. #if defined(MBEDTLS_TEST_HOOKS)
  56. int (*mbedtls_lmots_sign_private_key_invalidated_hook)(unsigned char *) = NULL;
  57. #endif /* defined(MBEDTLS_TEST_HOOKS) */
  58. /* Calculate the checksum digits that are appended to the end of the LMOTS digit
  59. * string. See NIST SP800-208 section 3.1 or RFC8554 Algorithm 2 for details of
  60. * the checksum algorithm.
  61. *
  62. * params The LMOTS parameter set, I and q values which
  63. * describe the key being used.
  64. *
  65. * digest The digit string to create the digest from. As
  66. * this does not contain a checksum, it is the same
  67. * size as a hash output.
  68. */
  69. static unsigned short lmots_checksum_calculate(const mbedtls_lmots_parameters_t *params,
  70. const unsigned char *digest)
  71. {
  72. size_t idx;
  73. unsigned sum = 0;
  74. for (idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++) {
  75. sum += DIGIT_MAX_VALUE - digest[idx];
  76. }
  77. return sum;
  78. }
  79. /* Create the string of digest digits (in the base determined by the Winternitz
  80. * parameter with the checksum appended to the end (Q || cksm(Q)). See NIST
  81. * SP800-208 section 3.1 or RFC8554 Algorithm 3 step 5 (also used in Algorithm
  82. * 4b step 3) for details.
  83. *
  84. * params The LMOTS parameter set, I and q values which
  85. * describe the key being used.
  86. *
  87. * msg The message that will be hashed to create the
  88. * digest.
  89. *
  90. * msg_size The size of the message.
  91. *
  92. * C_random_value The random value that will be combined with the
  93. * message digest. This is always the same size as a
  94. * hash output for whichever hash algorithm is
  95. * determined by the parameter set.
  96. *
  97. * output An output containing the digit string (+
  98. * checksum) of length P digits (in the case of
  99. * MBEDTLS_LMOTS_SHA256_N32_W8, this means it is of
  100. * size P bytes).
  101. */
  102. static int create_digit_array_with_checksum(const mbedtls_lmots_parameters_t *params,
  103. const unsigned char *msg,
  104. size_t msg_len,
  105. const unsigned char *C_random_value,
  106. unsigned char *out)
  107. {
  108. psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
  109. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  110. size_t output_hash_len;
  111. unsigned short checksum;
  112. status = psa_hash_setup(&op, PSA_ALG_SHA_256);
  113. if (status != PSA_SUCCESS) {
  114. goto exit;
  115. }
  116. status = psa_hash_update(&op, params->I_key_identifier,
  117. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  118. if (status != PSA_SUCCESS) {
  119. goto exit;
  120. }
  121. status = psa_hash_update(&op, params->q_leaf_identifier,
  122. MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
  123. if (status != PSA_SUCCESS) {
  124. goto exit;
  125. }
  126. status = psa_hash_update(&op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN);
  127. if (status != PSA_SUCCESS) {
  128. goto exit;
  129. }
  130. status = psa_hash_update(&op, C_random_value,
  131. MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type));
  132. if (status != PSA_SUCCESS) {
  133. goto exit;
  134. }
  135. status = psa_hash_update(&op, msg, msg_len);
  136. if (status != PSA_SUCCESS) {
  137. goto exit;
  138. }
  139. status = psa_hash_finish(&op, out,
  140. MBEDTLS_LMOTS_N_HASH_LEN(params->type),
  141. &output_hash_len);
  142. if (status != PSA_SUCCESS) {
  143. goto exit;
  144. }
  145. checksum = lmots_checksum_calculate(params, out);
  146. MBEDTLS_PUT_UINT16_BE(checksum, out, MBEDTLS_LMOTS_N_HASH_LEN(params->type));
  147. exit:
  148. psa_hash_abort(&op);
  149. return PSA_TO_MBEDTLS_ERR(status);
  150. }
  151. /* Hash each element of the string of digits (+ checksum), producing a hash
  152. * output for each element. This is used in several places (by varying the
  153. * hash_idx_min/max_values) in order to calculate a public key from a private
  154. * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554
  155. * Algorithm 3 step 5), and to calculate a public key candidate from a
  156. * signature and message (RFC8554 Algorithm 4b step 3).
  157. *
  158. * params The LMOTS parameter set, I and q values which
  159. * describe the key being used.
  160. *
  161. * x_digit_array The array of digits (of size P, 34 in the case of
  162. * MBEDTLS_LMOTS_SHA256_N32_W8).
  163. *
  164. * hash_idx_min_values An array of the starting values of the j iterator
  165. * for each of the members of the digit array. If
  166. * this value in NULL, then all iterators will start
  167. * at 0.
  168. *
  169. * hash_idx_max_values An array of the upper bound values of the j
  170. * iterator for each of the members of the digit
  171. * array. If this value in NULL, then iterator is
  172. * bounded to be less than 2^w - 1 (255 in the case
  173. * of MBEDTLS_LMOTS_SHA256_N32_W8)
  174. *
  175. * output An array containing a hash output for each member
  176. * of the digit string P. In the case of
  177. * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 *
  178. * 34.
  179. */
  180. static int hash_digit_array(const mbedtls_lmots_parameters_t *params,
  181. const unsigned char *x_digit_array,
  182. const unsigned char *hash_idx_min_values,
  183. const unsigned char *hash_idx_max_values,
  184. unsigned char *output)
  185. {
  186. unsigned int i_digit_idx;
  187. unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
  188. unsigned int j_hash_idx;
  189. unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
  190. unsigned int j_hash_idx_min;
  191. unsigned int j_hash_idx_max;
  192. psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
  193. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  194. size_t output_hash_len;
  195. unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
  196. for (i_digit_idx = 0;
  197. i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
  198. i_digit_idx++) {
  199. memcpy(tmp_hash,
  200. &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
  201. MBEDTLS_LMOTS_N_HASH_LEN(params->type));
  202. j_hash_idx_min = hash_idx_min_values != NULL ?
  203. hash_idx_min_values[i_digit_idx] : 0;
  204. j_hash_idx_max = hash_idx_max_values != NULL ?
  205. hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
  206. for (j_hash_idx = j_hash_idx_min;
  207. j_hash_idx < j_hash_idx_max;
  208. j_hash_idx++) {
  209. status = psa_hash_setup(&op, PSA_ALG_SHA_256);
  210. if (status != PSA_SUCCESS) {
  211. goto exit;
  212. }
  213. status = psa_hash_update(&op,
  214. params->I_key_identifier,
  215. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  216. if (status != PSA_SUCCESS) {
  217. goto exit;
  218. }
  219. status = psa_hash_update(&op,
  220. params->q_leaf_identifier,
  221. MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
  222. if (status != PSA_SUCCESS) {
  223. goto exit;
  224. }
  225. MBEDTLS_PUT_UINT16_BE(i_digit_idx, i_digit_idx_bytes, 0);
  226. status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN);
  227. if (status != PSA_SUCCESS) {
  228. goto exit;
  229. }
  230. j_hash_idx_bytes[0] = (uint8_t) j_hash_idx;
  231. status = psa_hash_update(&op, j_hash_idx_bytes, J_HASH_IDX_LEN);
  232. if (status != PSA_SUCCESS) {
  233. goto exit;
  234. }
  235. status = psa_hash_update(&op, tmp_hash,
  236. MBEDTLS_LMOTS_N_HASH_LEN(params->type));
  237. if (status != PSA_SUCCESS) {
  238. goto exit;
  239. }
  240. status = psa_hash_finish(&op, tmp_hash, sizeof(tmp_hash),
  241. &output_hash_len);
  242. if (status != PSA_SUCCESS) {
  243. goto exit;
  244. }
  245. psa_hash_abort(&op);
  246. }
  247. memcpy(&output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
  248. tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type));
  249. }
  250. exit:
  251. psa_hash_abort(&op);
  252. mbedtls_platform_zeroize(tmp_hash, sizeof(tmp_hash));
  253. return PSA_TO_MBEDTLS_ERR(status);
  254. }
  255. /* Combine the hashes of the digit array into a public key. This is used in
  256. * in order to calculate a public key from a private key (RFC8554 Algorithm 1
  257. * step 4), and to calculate a public key candidate from a signature and message
  258. * (RFC8554 Algorithm 4b step 3).
  259. *
  260. * params The LMOTS parameter set, I and q values which describe
  261. * the key being used.
  262. * y_hashed_digits The array of hashes, one hash for each digit of the
  263. * symbol array (which is of size P, 34 in the case of
  264. * MBEDTLS_LMOTS_SHA256_N32_W8)
  265. *
  266. * pub_key The output public key (or candidate public key in
  267. * case this is being run as part of signature
  268. * verification), in the form of a hash output.
  269. */
  270. static int public_key_from_hashed_digit_array(const mbedtls_lmots_parameters_t *params,
  271. const unsigned char *y_hashed_digits,
  272. unsigned char *pub_key)
  273. {
  274. psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
  275. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  276. size_t output_hash_len;
  277. status = psa_hash_setup(&op, PSA_ALG_SHA_256);
  278. if (status != PSA_SUCCESS) {
  279. goto exit;
  280. }
  281. status = psa_hash_update(&op,
  282. params->I_key_identifier,
  283. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  284. if (status != PSA_SUCCESS) {
  285. goto exit;
  286. }
  287. status = psa_hash_update(&op, params->q_leaf_identifier,
  288. MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
  289. if (status != PSA_SUCCESS) {
  290. goto exit;
  291. }
  292. status = psa_hash_update(&op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN);
  293. if (status != PSA_SUCCESS) {
  294. goto exit;
  295. }
  296. status = psa_hash_update(&op, y_hashed_digits,
  297. MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
  298. MBEDTLS_LMOTS_N_HASH_LEN(params->type));
  299. if (status != PSA_SUCCESS) {
  300. goto exit;
  301. }
  302. status = psa_hash_finish(&op, pub_key,
  303. MBEDTLS_LMOTS_N_HASH_LEN(params->type),
  304. &output_hash_len);
  305. if (status != PSA_SUCCESS) {
  306. exit:
  307. psa_hash_abort(&op);
  308. }
  309. return PSA_TO_MBEDTLS_ERR(status);
  310. }
  311. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  312. int mbedtls_lms_error_from_psa(psa_status_t status)
  313. {
  314. switch (status) {
  315. case PSA_SUCCESS:
  316. return 0;
  317. case PSA_ERROR_HARDWARE_FAILURE:
  318. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  319. case PSA_ERROR_NOT_SUPPORTED:
  320. return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
  321. case PSA_ERROR_BUFFER_TOO_SMALL:
  322. return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
  323. case PSA_ERROR_INVALID_ARGUMENT:
  324. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  325. default:
  326. return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
  327. }
  328. }
  329. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  330. void mbedtls_lmots_public_init(mbedtls_lmots_public_t *ctx)
  331. {
  332. memset(ctx, 0, sizeof(*ctx));
  333. }
  334. void mbedtls_lmots_public_free(mbedtls_lmots_public_t *ctx)
  335. {
  336. if (ctx == NULL) {
  337. return;
  338. }
  339. mbedtls_platform_zeroize(ctx, sizeof(*ctx));
  340. }
  341. int mbedtls_lmots_import_public_key(mbedtls_lmots_public_t *ctx,
  342. const unsigned char *key, size_t key_len)
  343. {
  344. if (key_len < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
  345. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  346. }
  347. ctx->params.type = (mbedtls_lmots_algorithm_type_t)
  348. MBEDTLS_GET_UINT32_BE(key, MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
  349. if (key_len != MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
  350. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  351. }
  352. memcpy(ctx->params.I_key_identifier,
  353. key + PUBLIC_KEY_I_KEY_ID_OFFSET,
  354. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  355. memcpy(ctx->params.q_leaf_identifier,
  356. key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
  357. MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
  358. memcpy(ctx->public_key,
  359. key + PUBLIC_KEY_KEY_HASH_OFFSET,
  360. MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
  361. ctx->have_public_key = 1;
  362. return 0;
  363. }
  364. int mbedtls_lmots_export_public_key(const mbedtls_lmots_public_t *ctx,
  365. unsigned char *key, size_t key_size,
  366. size_t *key_len)
  367. {
  368. if (key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
  369. return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
  370. }
  371. if (!ctx->have_public_key) {
  372. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  373. }
  374. MBEDTLS_PUT_UINT32_BE(ctx->params.type, key, MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
  375. memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
  376. ctx->params.I_key_identifier,
  377. MBEDTLS_LMOTS_I_KEY_ID_LEN);
  378. memcpy(key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
  379. ctx->params.q_leaf_identifier,
  380. MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
  381. memcpy(key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
  382. MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
  383. if (key_len != NULL) {
  384. *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
  385. }
  386. return 0;
  387. }
  388. int mbedtls_lmots_calculate_public_key_candidate(const mbedtls_lmots_parameters_t *params,
  389. const unsigned char *msg,
  390. size_t msg_size,
  391. const unsigned char *sig,
  392. size_t sig_size,
  393. unsigned char *out,
  394. size_t out_size,
  395. size_t *out_len)
  396. {
  397. unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
  398. unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
  399. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  400. if (msg == NULL && msg_size != 0) {
  401. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  402. }
  403. if (sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
  404. out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type)) {
  405. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  406. }
  407. ret = create_digit_array_with_checksum(params, msg, msg_size,
  408. sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
  409. tmp_digit_array);
  410. if (ret) {
  411. return ret;
  412. }
  413. ret = hash_digit_array(params,
  414. sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
  415. tmp_digit_array, NULL, (unsigned char *) y_hashed_digits);
  416. if (ret) {
  417. return ret;
  418. }
  419. ret = public_key_from_hashed_digit_array(params,
  420. (unsigned char *) y_hashed_digits,
  421. out);
  422. if (ret) {
  423. return ret;
  424. }
  425. if (out_len != NULL) {
  426. *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
  427. }
  428. return 0;
  429. }
  430. int mbedtls_lmots_verify(const mbedtls_lmots_public_t *ctx,
  431. const unsigned char *msg, size_t msg_size,
  432. const unsigned char *sig, size_t sig_size)
  433. {
  434. unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
  435. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  436. if (msg == NULL && msg_size != 0) {
  437. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  438. }
  439. if (!ctx->have_public_key) {
  440. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  441. }
  442. if (ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8) {
  443. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  444. }
  445. if (sig_size < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
  446. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  447. }
  448. if (MBEDTLS_GET_UINT32_BE(sig, MBEDTLS_LMOTS_SIG_TYPE_OFFSET) != MBEDTLS_LMOTS_SHA256_N32_W8) {
  449. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  450. }
  451. ret = mbedtls_lmots_calculate_public_key_candidate(&ctx->params,
  452. msg, msg_size, sig, sig_size,
  453. Kc_public_key_candidate,
  454. MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
  455. NULL);
  456. if (ret) {
  457. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  458. }
  459. if (memcmp(&Kc_public_key_candidate, ctx->public_key,
  460. sizeof(ctx->public_key))) {
  461. return MBEDTLS_ERR_LMS_VERIFY_FAILED;
  462. }
  463. return 0;
  464. }
  465. #if defined(MBEDTLS_LMS_PRIVATE)
  466. void mbedtls_lmots_private_init(mbedtls_lmots_private_t *ctx)
  467. {
  468. memset(ctx, 0, sizeof(*ctx));
  469. }
  470. void mbedtls_lmots_private_free(mbedtls_lmots_private_t *ctx)
  471. {
  472. if (ctx == NULL) {
  473. return;
  474. }
  475. mbedtls_platform_zeroize(ctx,
  476. sizeof(*ctx));
  477. }
  478. int mbedtls_lmots_generate_private_key(mbedtls_lmots_private_t *ctx,
  479. mbedtls_lmots_algorithm_type_t type,
  480. const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
  481. uint32_t q_leaf_identifier,
  482. const unsigned char *seed,
  483. size_t seed_size)
  484. {
  485. psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
  486. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  487. size_t output_hash_len;
  488. unsigned int i_digit_idx;
  489. unsigned char i_digit_idx_bytes[2];
  490. unsigned char const_bytes[1] = { 0xFF };
  491. if (ctx->have_private_key) {
  492. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  493. }
  494. if (type != MBEDTLS_LMOTS_SHA256_N32_W8) {
  495. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  496. }
  497. ctx->params.type = type;
  498. memcpy(ctx->params.I_key_identifier,
  499. I_key_identifier,
  500. sizeof(ctx->params.I_key_identifier));
  501. MBEDTLS_PUT_UINT32_BE(q_leaf_identifier, ctx->params.q_leaf_identifier, 0);
  502. for (i_digit_idx = 0;
  503. i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
  504. i_digit_idx++) {
  505. status = psa_hash_setup(&op, PSA_ALG_SHA_256);
  506. if (status != PSA_SUCCESS) {
  507. goto exit;
  508. }
  509. status = psa_hash_update(&op,
  510. ctx->params.I_key_identifier,
  511. sizeof(ctx->params.I_key_identifier));
  512. if (status != PSA_SUCCESS) {
  513. goto exit;
  514. }
  515. status = psa_hash_update(&op,
  516. ctx->params.q_leaf_identifier,
  517. MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
  518. if (status != PSA_SUCCESS) {
  519. goto exit;
  520. }
  521. MBEDTLS_PUT_UINT16_BE(i_digit_idx, i_digit_idx_bytes, 0);
  522. status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN);
  523. if (status != PSA_SUCCESS) {
  524. goto exit;
  525. }
  526. status = psa_hash_update(&op, const_bytes, sizeof(const_bytes));
  527. if (status != PSA_SUCCESS) {
  528. goto exit;
  529. }
  530. status = psa_hash_update(&op, seed, seed_size);
  531. if (status != PSA_SUCCESS) {
  532. goto exit;
  533. }
  534. status = psa_hash_finish(&op,
  535. ctx->private_key[i_digit_idx],
  536. MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
  537. &output_hash_len);
  538. if (status != PSA_SUCCESS) {
  539. goto exit;
  540. }
  541. psa_hash_abort(&op);
  542. }
  543. ctx->have_private_key = 1;
  544. exit:
  545. psa_hash_abort(&op);
  546. return PSA_TO_MBEDTLS_ERR(status);
  547. }
  548. int mbedtls_lmots_calculate_public_key(mbedtls_lmots_public_t *ctx,
  549. const mbedtls_lmots_private_t *priv_ctx)
  550. {
  551. unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
  552. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  553. /* Check that a private key is loaded */
  554. if (!priv_ctx->have_private_key) {
  555. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  556. }
  557. ret = hash_digit_array(&priv_ctx->params,
  558. (unsigned char *) priv_ctx->private_key, NULL,
  559. NULL, (unsigned char *) y_hashed_digits);
  560. if (ret) {
  561. goto exit;
  562. }
  563. ret = public_key_from_hashed_digit_array(&priv_ctx->params,
  564. (unsigned char *) y_hashed_digits,
  565. ctx->public_key);
  566. if (ret) {
  567. goto exit;
  568. }
  569. memcpy(&ctx->params, &priv_ctx->params,
  570. sizeof(ctx->params));
  571. ctx->have_public_key = 1;
  572. exit:
  573. mbedtls_platform_zeroize(y_hashed_digits, sizeof(y_hashed_digits));
  574. return ret;
  575. }
  576. int mbedtls_lmots_sign(mbedtls_lmots_private_t *ctx,
  577. int (*f_rng)(void *, unsigned char *, size_t),
  578. void *p_rng, const unsigned char *msg, size_t msg_size,
  579. unsigned char *sig, size_t sig_size, size_t *sig_len)
  580. {
  581. unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
  582. /* Create a temporary buffer to prepare the signature in. This allows us to
  583. * finish creating a signature (ensuring the process doesn't fail), and then
  584. * erase the private key **before** writing any data into the sig parameter
  585. * buffer. If data were directly written into the sig buffer, it might leak
  586. * a partial signature on failure, which effectively compromises the private
  587. * key.
  588. */
  589. unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
  590. unsigned char tmp_c_random[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
  591. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  592. if (msg == NULL && msg_size != 0) {
  593. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  594. }
  595. if (sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type)) {
  596. return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
  597. }
  598. /* Check that a private key is loaded */
  599. if (!ctx->have_private_key) {
  600. return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
  601. }
  602. ret = f_rng(p_rng, tmp_c_random,
  603. MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
  604. if (ret) {
  605. return ret;
  606. }
  607. ret = create_digit_array_with_checksum(&ctx->params,
  608. msg, msg_size,
  609. tmp_c_random,
  610. tmp_digit_array);
  611. if (ret) {
  612. goto exit;
  613. }
  614. ret = hash_digit_array(&ctx->params, (unsigned char *) ctx->private_key,
  615. NULL, tmp_digit_array, (unsigned char *) tmp_sig);
  616. if (ret) {
  617. goto exit;
  618. }
  619. MBEDTLS_PUT_UINT32_BE(ctx->params.type, sig, MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
  620. /* Test hook to check if sig is being written to before we invalidate the
  621. * private key.
  622. */
  623. #if defined(MBEDTLS_TEST_HOOKS)
  624. if (mbedtls_lmots_sign_private_key_invalidated_hook != NULL) {
  625. ret = (*mbedtls_lmots_sign_private_key_invalidated_hook)(sig);
  626. if (ret != 0) {
  627. return ret;
  628. }
  629. }
  630. #endif /* defined(MBEDTLS_TEST_HOOKS) */
  631. /* We've got a valid signature now, so it's time to make sure the private
  632. * key can't be reused.
  633. */
  634. ctx->have_private_key = 0;
  635. mbedtls_platform_zeroize(ctx->private_key,
  636. sizeof(ctx->private_key));
  637. memcpy(sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
  638. MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type));
  639. memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
  640. MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
  641. * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
  642. if (sig_len != NULL) {
  643. *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
  644. }
  645. ret = 0;
  646. exit:
  647. mbedtls_platform_zeroize(tmp_digit_array, sizeof(tmp_digit_array));
  648. mbedtls_platform_zeroize(tmp_sig, sizeof(tmp_sig));
  649. return ret;
  650. }
  651. #endif /* defined(MBEDTLS_LMS_PRIVATE) */
  652. #endif /* defined(MBEDTLS_LMS_C) */