lmots.c 29 KB

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