blob: 41d34bfe16a58ed9401a7aae764bc8c175a5db35 [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/*
2 * The LMS stateful-hash public-key signature scheme
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Raef Coles8ff6df52021-07-21 12:42:15 +01006 */
7
8/*
9 * The following sources were referenced in the design of this implementation
10 * of the LMS algorithm:
11 *
12 * [1] IETF RFC8554
13 * D. McGrew, M. Curcio, S.Fluhrer
14 * https://datatracker.ietf.org/doc/html/rfc8554
15 *
16 * [2] NIST Special Publication 800-208
17 * David A. Cooper et. al.
18 * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
19 */
20
21#include "common.h"
22
Raef Coles5127e852022-10-07 10:35:56 +010023#if defined(MBEDTLS_LMS_C)
Raef Coles8ff6df52021-07-21 12:42:15 +010024
25#include <string.h>
26
Raef Coles7dce69a2022-08-24 14:07:06 +010027#include "lmots.h"
28
Raef Colesc8f96042022-08-25 13:49:54 +010029#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020030#include "psa_util_internal.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010031#include "mbedtls/lms.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010032#include "mbedtls/error.h"
33#include "mbedtls/platform_util.h"
34
Raef Coles8ff6df52021-07-21 12:42:15 +010035#include "mbedtls/platform.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010036
Andrzej Kurek00644842023-05-30 05:45:00 -040037/* Define a local translating function to save code size by not using too many
38 * arguments in each translating place. */
39static int local_err_translation(psa_status_t status)
40{
41 return psa_status_to_mbedtls(status, psa_to_lms_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040042 ARRAY_LENGTH(psa_to_lms_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040043 psa_generic_status_to_mbedtls);
44}
45#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050046
Raef Coles57d53282022-09-27 11:30:51 +010047#define SIG_Q_LEAF_ID_OFFSET (0)
48#define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
49 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
50#define SIG_TYPE_OFFSET(otstype) (SIG_OTS_SIG_OFFSET + \
51 MBEDTLS_LMOTS_SIG_LEN(otstype))
52#define SIG_PATH_OFFSET(otstype) (SIG_TYPE_OFFSET(otstype) + \
53 MBEDTLS_LMS_TYPE_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010054
Raef Coles57d53282022-09-27 11:30:51 +010055#define PUBLIC_KEY_TYPE_OFFSET (0)
56#define PUBLIC_KEY_OTSTYPE_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
57 MBEDTLS_LMS_TYPE_LEN)
58#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_OTSTYPE_OFFSET + \
59 MBEDTLS_LMOTS_TYPE_LEN)
60#define PUBLIC_KEY_ROOT_NODE_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
61 MBEDTLS_LMOTS_I_KEY_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010062
63
Raef Colese9479a02022-09-01 16:06:35 +010064/* Currently only support H=10 */
Raef Coles57d53282022-09-27 11:30:51 +010065#define H_TREE_HEIGHT_MAX 10
Moritz Fischera6a94ad2022-11-12 08:28:04 -080066#define MERKLE_TREE_NODE_AM(type) ((size_t) 1 << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
67#define MERKLE_TREE_LEAF_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
Dave Rodgmanb4cb8be2023-11-24 17:08:54 +000068#define MERKLE_TREE_INTERNAL_NODE_AM(type) ((unsigned int) \
69 (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type)))
Raef Coles8ff6df52021-07-21 12:42:15 +010070
71#define D_CONST_LEN (2)
Gilles Peskine449bd832023-01-11 14:50:10 +010072static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = { 0x82, 0x82 };
73static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = { 0x83, 0x83 };
Raef Coles8ff6df52021-07-21 12:42:15 +010074
Raef Coles0a967cc2022-09-02 17:46:15 +010075
Raef Coles285d44b2022-10-10 15:44:17 +010076/* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
Raef Coles0a967cc2022-09-02 17:46:15 +010077 * public key and some other parameters like the leaf index). This function
78 * implements RFC8554 section 5.3, in the case where r >= 2^h.
79 *
Raef Coles98d6e222022-09-23 09:04:04 +010080 * params The LMS parameter set, the underlying LMOTS
81 * parameter set, and I value which describe the key
82 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +010083 *
Raef Coles98d6e222022-09-23 09:04:04 +010084 * pub_key The public key of the private whose index
85 * corresponds to the index of this leaf node. This
86 * is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010087 *
Raef Coles285d44b2022-10-10 15:44:17 +010088 * r_node_idx The index of this node in the Merkle tree. Note
89 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +010090 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +010091 *
Raef Coles98d6e222022-09-23 09:04:04 +010092 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010093 */
Gilles Peskine449bd832023-01-11 14:50:10 +010094static int create_merkle_leaf_value(const mbedtls_lms_parameters_t *params,
95 unsigned char *pub_key,
96 unsigned int r_node_idx,
97 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +010098{
Raef Colesc8f96042022-08-25 13:49:54 +010099 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100101 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100102 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100103
Minos Galanakisdf2f0aa2025-06-06 14:34:54 +0100104 /* Always zeroize the output buffer because it may contain data from the previous invocation */
Minos Galanakis9b3051f2025-06-02 14:36:25 +0100105 memset(out, 0, MBEDTLS_LMS_M_NODE_BYTES(params->type));
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 op = psa_hash_operation_init();
108 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
109 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 status = psa_hash_update(&op, params->I_key_identifier,
114 MBEDTLS_LMOTS_I_KEY_ID_LEN);
115 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100118
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000119 MBEDTLS_PUT_UINT32_BE(r_node_idx, r_node_idx_bytes, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 status = psa_hash_update(&op, r_node_idx_bytes, 4);
121 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100122 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 status = psa_hash_update(&op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN);
126 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100127 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 status = psa_hash_update(&op, pub_key,
131 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype));
132 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
137 &output_hash_len);
138 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100141
Raef Coles01c71a12022-08-31 15:55:00 +0100142exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100144
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500145 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100146}
147
Raef Coles285d44b2022-10-10 15:44:17 +0100148/* Calculate the value of an internal node of the Merkle tree (which is a hash
Raef Coles0a967cc2022-09-02 17:46:15 +0100149 * of a public key and some other parameters like the node index). This function
150 * implements RFC8554 section 5.3, in the case where r < 2^h.
151 *
Raef Coles98d6e222022-09-23 09:04:04 +0100152 * params The LMS parameter set, the underlying LMOTS
153 * parameter set, and I value which describe the key
154 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100155 *
Raef Coles98d6e222022-09-23 09:04:04 +0100156 * left_node The value of the child of this node which is on
157 * the left-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100158 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100159 *
Raef Coles98d6e222022-09-23 09:04:04 +0100160 * right_node The value of the child of this node which is on
161 * the right-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100162 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100163 *
Raef Coles285d44b2022-10-10 15:44:17 +0100164 * r_node_idx The index of this node in the Merkle tree. Note
165 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100166 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100167 *
Raef Coles98d6e222022-09-23 09:04:04 +0100168 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100169 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100170static int create_merkle_internal_value(const mbedtls_lms_parameters_t *params,
171 const unsigned char *left_node,
172 const unsigned char *right_node,
173 unsigned int r_node_idx,
174 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100175{
Raef Colesc8f96042022-08-25 13:49:54 +0100176 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100178 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100179 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 op = psa_hash_operation_init();
182 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
183 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 status = psa_hash_update(&op, params->I_key_identifier,
188 MBEDTLS_LMOTS_I_KEY_ID_LEN);
189 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100192
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000193 MBEDTLS_PUT_UINT32_BE(r_node_idx, r_node_idx_bytes, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 status = psa_hash_update(&op, r_node_idx_bytes, 4);
195 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 status = psa_hash_update(&op, D_INTR_CONSTANT_BYTES, D_CONST_LEN);
200 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100201 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 status = psa_hash_update(&op, left_node,
205 MBEDTLS_LMS_M_NODE_BYTES(params->type));
206 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100207 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 status = psa_hash_update(&op, right_node,
211 MBEDTLS_LMS_M_NODE_BYTES(params->type));
212 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
217 &output_hash_len);
218 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100221
Raef Coles01c71a12022-08-31 15:55:00 +0100222exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100224
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500225 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100226}
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100229{
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 memset(ctx, 0, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100231}
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100234{
Troy-Butler9ac3e232024-03-22 14:46:04 -0400235 if (ctx == NULL) {
236 return;
237 }
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100240}
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx,
243 const unsigned char *key, size_t key_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100244{
Minos Galanakis548e2db2025-06-02 14:17:38 +0100245 if (key_size < 4) {
246 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
247 }
248
Minos Galanakiscaaffc12025-06-02 14:28:31 +0100249 uint32_t type = MBEDTLS_GET_UINT32_BE(key, PUBLIC_KEY_TYPE_OFFSET);
250 if (type != (uint32_t) MBEDTLS_LMS_SHA256_M32_H10) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100252 }
Minos Galanakiscaaffc12025-06-02 14:28:31 +0100253 ctx->params.type = (mbedtls_lms_algorithm_type_t) type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
256 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese89488d2022-10-07 16:06:35 +0100257 }
258
Minos Galanakiscaaffc12025-06-02 14:28:31 +0100259 uint32_t otstype = MBEDTLS_GET_UINT32_BE(key, PUBLIC_KEY_OTSTYPE_OFFSET);
260 if (otstype != (uint32_t) MBEDTLS_LMOTS_SHA256_N32_W8) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100262 }
Minos Galanakiscaaffc12025-06-02 14:28:31 +0100263 ctx->params.otstype = (mbedtls_lmots_algorithm_type_t) otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 memcpy(ctx->params.I_key_identifier,
266 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
267 MBEDTLS_LMOTS_I_KEY_ID_LEN);
268 memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
269 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100270
Raef Colesf5632d32022-09-01 09:56:52 +0100271 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100274}
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
277 unsigned char *key,
278 size_t key_size, size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100279{
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
281 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100282 }
283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (!ctx->have_public_key) {
285 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100286 }
287
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000288 MBEDTLS_PUT_UINT32_BE(ctx->params.type, key, PUBLIC_KEY_TYPE_OFFSET);
289 MBEDTLS_PUT_UINT32_BE(ctx->params.otstype, key, PUBLIC_KEY_OTSTYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
291 ctx->params.I_key_identifier,
292 MBEDTLS_LMOTS_I_KEY_ID_LEN);
293 memcpy(key +PUBLIC_KEY_ROOT_NODE_OFFSET,
294 ctx->T_1_pub_key,
295 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100298 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
299 }
300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100302}
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx,
305 const unsigned char *msg, size_t msg_size,
306 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100307{
308 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100309 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
310 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100311 unsigned int height;
312 unsigned int curr_node_id;
313 unsigned int parent_node_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 const unsigned char *left_node;
315 const unsigned char *right_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100316 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if (!ctx->have_public_key) {
320 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100321 }
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (ctx->params.type
324 != MBEDTLS_LMS_SHA256_M32_H10) {
325 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100326 }
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (ctx->params.otstype
329 != MBEDTLS_LMOTS_SHA256_N32_W8) {
330 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100331 }
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
334 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100335 }
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
338 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100339 }
340
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000341 if (MBEDTLS_GET_UINT32_BE(sig, SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 != MBEDTLS_LMOTS_SHA256_N32_W8) {
343 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100344 }
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
347 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100348 }
349
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000350 if (MBEDTLS_GET_UINT32_BE(sig, SIG_TYPE_OFFSET(ctx->params.otstype))
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 != MBEDTLS_LMS_SHA256_M32_H10) {
352 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100353 }
354
355
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000356 q_leaf_identifier = MBEDTLS_GET_UINT32_BE(sig, SIG_Q_LEAF_ID_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
359 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100360 }
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 memcpy(ots_params.I_key_identifier,
363 ctx->params.I_key_identifier,
364 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000365 MBEDTLS_PUT_UINT32_BE(q_leaf_identifier, ots_params.q_leaf_identifier, 0);
Raef Colesf5632d32022-09-01 09:56:52 +0100366 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
369 msg,
370 msg_size,
371 sig + SIG_OTS_SIG_OFFSET,
372 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
373 Kc_candidate_ots_pub_key,
374 sizeof(Kc_candidate_ots_pub_key),
375 NULL);
376 if (ret != 0) {
377 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100378 }
379
Minos Galanakisae449bf2025-06-02 14:33:47 +0100380 ret = create_merkle_leaf_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 &ctx->params,
382 Kc_candidate_ots_pub_key,
383 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
384 Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100385
Minos Galanakisae449bf2025-06-02 14:33:47 +0100386 if (ret != 0) {
387 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
388 }
389
Raef Coles366d67d2022-09-01 17:23:12 +0100390 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
391 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
394 height++) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100395 parent_node_id = curr_node_id / 2;
396
397 /* Left/right node ordering matters for the hash */
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (curr_node_id & 1) {
Raef Coles57d53282022-09-27 11:30:51 +0100399 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100400 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100401 right_node = Tc_candidate_root_node;
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 } else {
Raef Coles8ff6df52021-07-21 12:42:15 +0100403 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100404 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100405 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100406 }
407
Minos Galanakisae449bf2025-06-02 14:33:47 +0100408 ret = create_merkle_internal_value(&ctx->params, left_node, right_node,
409 parent_node_id, Tc_candidate_root_node);
410 if (ret != 0) {
411 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
412 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100413 curr_node_id /= 2;
414 }
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
417 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
418 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100419 }
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100422}
423
Raef Coles5127e852022-10-07 10:35:56 +0100424#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100425
Raef Coles285d44b2022-10-10 15:44:17 +0100426/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100427 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100428 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100429 *
Raef Coles98d6e222022-09-23 09:04:04 +0100430 * ctx The LMS private context, containing a parameter
431 * set and private key material consisting of both
432 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100433 *
Raef Coles98d6e222022-09-23 09:04:04 +0100434 * tree The output tree, which is 2^(H + 1) hash outputs.
435 * In the case of H=10 we have 2048 tree nodes (of
436 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100437 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100438 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100439 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
441 unsigned char *tree)
Raef Colesab4f8742022-09-01 12:24:31 +0100442{
443 unsigned int priv_key_idx;
444 unsigned int r_node_idx;
445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
446
447 /* First create the leaf nodes, in ascending order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 for (priv_key_idx = 0;
Raef Colese9479a02022-09-01 16:06:35 +0100449 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 priv_key_idx++) {
Raef Colese9479a02022-09-01 16:06:35 +0100451 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 ret = create_merkle_leaf_value(&ctx->params,
454 ctx->ots_public_keys[priv_key_idx].public_key,
455 r_node_idx,
456 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(
457 ctx->params.type)]);
458 if (ret != 0) {
459 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100460 }
461 }
462
463 /* Then the internal nodes, in reverse order so that we can guarantee the
464 * parent has been created */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
Raef Colese9479a02022-09-01 16:06:35 +0100466 r_node_idx > 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 r_node_idx--) {
468 ret = create_merkle_internal_value(&ctx->params,
469 &tree[(r_node_idx * 2) *
470 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
471 &tree[(r_node_idx * 2 + 1) *
472 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
473 r_node_idx,
474 &tree[r_node_idx *
475 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]);
476 if (ret != 0) {
477 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100478 }
479 }
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100482}
483
Raef Coles285d44b2022-10-10 15:44:17 +0100484/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100485 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100486 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100487 *
Raef Coles98d6e222022-09-23 09:04:04 +0100488 * ctx The LMS private context, containing a parameter
489 * set and private key material consisting of both
490 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100491 *
Raef Coles98d6e222022-09-23 09:04:04 +0100492 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100493 *
Raef Coles75b4c772022-10-10 13:58:28 +0100494 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100495 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100496static int get_merkle_path(mbedtls_lms_private_t *ctx,
497 unsigned int leaf_node_id,
498 unsigned char *path)
Raef Colesab4f8742022-09-01 12:24:31 +0100499{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800500 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100501 unsigned int curr_node_id = leaf_node_id;
502 unsigned int adjacent_node_id;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800503 unsigned char *tree = NULL;
Raef Colesab4f8742022-09-01 12:24:31 +0100504 unsigned int height;
505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
506
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000507 tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(ctx->params.type),
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 node_bytes);
509 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800510 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
511 }
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 ret = calculate_merkle_tree(ctx, tree);
514 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100515 goto exit;
Raef Colesab4f8742022-09-01 12:24:31 +0100516 }
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
519 height++) {
Raef Colesab4f8742022-09-01 12:24:31 +0100520 adjacent_node_id = curr_node_id ^ 1;
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 memcpy(&path[height * node_bytes],
523 &tree[adjacent_node_id * node_bytes], node_bytes);
Raef Colesab4f8742022-09-01 12:24:31 +0100524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 curr_node_id >>= 1;
Raef Colesab4f8742022-09-01 12:24:31 +0100526 }
527
Raef Coles142e5772022-10-12 10:47:27 +0100528 ret = 0;
529
530exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100531 mbedtls_zeroize_and_free(tree, node_bytes *
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000532 (size_t) MERKLE_TREE_NODE_AM(ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100535}
536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100538{
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100540}
Raef Coles8ff6df52021-07-21 12:42:15 +0100541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100543{
Troy-Butler9ac3e232024-03-22 14:46:04 -0400544 if (ctx == NULL) {
545 return;
546 }
547
Raef Coles01c71a12022-08-31 15:55:00 +0100548 unsigned int idx;
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (ctx->have_private_key) {
551 if (ctx->ots_private_keys != NULL) {
552 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
553 mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100554 }
Raef Coles01c71a12022-08-31 15:55:00 +0100555 }
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (ctx->ots_public_keys != NULL) {
558 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
559 mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100560 }
561 }
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 mbedtls_free(ctx->ots_private_keys);
564 mbedtls_free(ctx->ots_public_keys);
Raef Coles8ff6df52021-07-21 12:42:15 +0100565 }
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100568}
Raef Coles8ff6df52021-07-21 12:42:15 +0100569
Raef Coles01c71a12022-08-31 15:55:00 +0100570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
572 mbedtls_lms_algorithm_type_t type,
573 mbedtls_lmots_algorithm_type_t otstype,
574 int (*f_rng)(void *, unsigned char *, size_t),
575 void *p_rng, const unsigned char *seed,
576 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100577{
578 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
582 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100583 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
586 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100587 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (ctx->have_private_key) {
590 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100591 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100592
Raef Colesf5632d32022-09-01 09:56:52 +0100593 ctx->params.type = type;
594 ctx->params.otstype = otstype;
Raef Colescbd02ad2022-10-13 14:11:49 +0100595 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 ret = f_rng(p_rng,
598 ctx->params.I_key_identifier,
599 MBEDTLS_LMOTS_I_KEY_ID_LEN);
600 if (ret != 0) {
Raef Coles3f6cdd72022-10-07 14:07:59 +0100601 goto exit;
602 }
Raef Coles01c71a12022-08-31 15:55:00 +0100603
Raef Coles45c4ff92022-10-12 15:22:48 +0100604 /* Requires a cast to size_t to avoid an implicit cast warning on certain
605 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
607 sizeof(*ctx->ots_private_keys));
608 if (ctx->ots_private_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100609 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
610 goto exit;
611 }
612
Raef Coles45c4ff92022-10-12 15:22:48 +0100613 /* Requires a cast to size_t to avoid an implicit cast warning on certain
614 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
616 sizeof(*ctx->ots_public_keys));
617 if (ctx->ots_public_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100618 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
619 goto exit;
620 }
621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
623 mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
624 mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]);
Raef Coles01c71a12022-08-31 15:55:00 +0100625 }
626
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
629 ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx],
630 otstype,
631 ctx->params.I_key_identifier,
632 idx, seed, seed_size);
633 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100634 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 }
Raef Coles01c71a12022-08-31 15:55:00 +0100636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
638 &ctx->ots_private_keys[idx]);
639 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100640 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 }
Raef Coles01c71a12022-08-31 15:55:00 +0100642 }
643
Raef Colesf5632d32022-09-01 09:56:52 +0100644 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100645
646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (ret != 0) {
Raef Colesdc8fb792022-10-07 13:27:54 +0100648 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100649 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100652}
653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
655 const mbedtls_lms_private_t *priv_ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100656{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800657 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800659 unsigned char *tree = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +0100660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (!priv_ctx->have_private_key) {
662 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100663 }
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (priv_ctx->params.type
666 != MBEDTLS_LMS_SHA256_M32_H10) {
667 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100668 }
669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (priv_ctx->params.otstype
671 != MBEDTLS_LMOTS_SHA256_N32_W8) {
672 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100673 }
674
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000675 tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(priv_ctx->params.type),
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 node_bytes);
677 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800678 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
679 }
680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 memcpy(&ctx->params, &priv_ctx->params,
682 sizeof(mbedtls_lmots_parameters_t));
Raef Coles8ff6df52021-07-21 12:42:15 +0100683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 ret = calculate_merkle_tree(priv_ctx, tree);
685 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100686 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100687 }
688
689 /* Root node is always at position 1, due to 1-based indexing */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100691
Raef Colesf5632d32022-09-01 09:56:52 +0100692 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100693
Raef Coles142e5772022-10-12 10:47:27 +0100694 ret = 0;
695
696exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100697 mbedtls_zeroize_and_free(tree, node_bytes *
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000698 (size_t) MERKLE_TREE_NODE_AM(priv_ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100701}
702
Raef Coles01c71a12022-08-31 15:55:00 +0100703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
705 int (*f_rng)(void *, unsigned char *, size_t),
706 void *p_rng, const unsigned char *msg,
707 unsigned int msg_size, unsigned char *sig, size_t sig_size,
708 size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100709{
710 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (!ctx->have_private_key) {
714 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100715 }
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
718 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100719 }
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
722 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100723 }
724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (ctx->params.otstype
726 != MBEDTLS_LMOTS_SHA256_N32_W8) {
727 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100728 }
729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
731 return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
Raef Coles8ff6df52021-07-21 12:42:15 +0100732 }
733
734
Raef Colesf5632d32022-09-01 09:56:52 +0100735 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100736 /* This new value must _always_ be written back to the disk before the
737 * signature is returned.
738 */
Raef Colesf5632d32022-09-01 09:56:52 +0100739 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
742 < SIG_OTS_SIG_OFFSET) {
743 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles1fb2f322022-10-10 11:23:07 +0100744 }
745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
747 f_rng,
748 p_rng,
749 msg,
750 msg_size,
751 sig + SIG_OTS_SIG_OFFSET,
752 MBEDTLS_LMS_SIG_LEN(ctx->params.type,
753 ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
754 NULL);
755 if (ret != 0) {
756 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100757 }
758
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000759 MBEDTLS_PUT_UINT32_BE(ctx->params.type, sig, SIG_TYPE_OFFSET(ctx->params.otstype));
760 MBEDTLS_PUT_UINT32_BE(q_leaf_identifier, sig, SIG_Q_LEAF_ID_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 ret = get_merkle_path(ctx,
763 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
764 sig + SIG_PATH_OFFSET(ctx->params.otstype));
765 if (ret != 0) {
766 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100767 }
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100770 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100771 }
772
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100775}
776
Raef Coles5127e852022-10-07 10:35:56 +0100777#endif /* defined(MBEDTLS_LMS_PRIVATE) */
778#endif /* defined(MBEDTLS_LMS_C) */