blob: 4bdfd434adb023851bb1ea1d59d3725da2f27e46 [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
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 op = psa_hash_operation_init();
105 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
106 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 status = psa_hash_update(&op, params->I_key_identifier,
111 MBEDTLS_LMOTS_I_KEY_ID_LEN);
112 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100115
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000116 MBEDTLS_PUT_UINT32_BE(r_node_idx, r_node_idx_bytes, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 status = psa_hash_update(&op, r_node_idx_bytes, 4);
118 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 status = psa_hash_update(&op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN);
123 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 status = psa_hash_update(&op, pub_key,
128 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype));
129 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
134 &output_hash_len);
135 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100136 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100138
Raef Coles01c71a12022-08-31 15:55:00 +0100139exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100141
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500142 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100143}
144
Raef Coles285d44b2022-10-10 15:44:17 +0100145/* Calculate the value of an internal node of the Merkle tree (which is a hash
Raef Coles0a967cc2022-09-02 17:46:15 +0100146 * of a public key and some other parameters like the node index). This function
147 * implements RFC8554 section 5.3, in the case where r < 2^h.
148 *
Raef Coles98d6e222022-09-23 09:04:04 +0100149 * params The LMS parameter set, the underlying LMOTS
150 * parameter set, and I value which describe the key
151 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100152 *
Raef Coles98d6e222022-09-23 09:04:04 +0100153 * left_node The value of the child of this node which is on
154 * the left-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100155 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100156 *
Raef Coles98d6e222022-09-23 09:04:04 +0100157 * right_node The value of the child of this node which is on
158 * the right-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100159 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100160 *
Raef Coles285d44b2022-10-10 15:44:17 +0100161 * r_node_idx The index of this node in the Merkle tree. Note
162 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100163 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100164 *
Raef Coles98d6e222022-09-23 09:04:04 +0100165 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167static int create_merkle_internal_value(const mbedtls_lms_parameters_t *params,
168 const unsigned char *left_node,
169 const unsigned char *right_node,
170 unsigned int r_node_idx,
171 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100172{
Raef Colesc8f96042022-08-25 13:49:54 +0100173 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100175 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100176 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 op = psa_hash_operation_init();
179 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
180 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 status = psa_hash_update(&op, params->I_key_identifier,
185 MBEDTLS_LMOTS_I_KEY_ID_LEN);
186 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100189
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000190 MBEDTLS_PUT_UINT32_BE(r_node_idx, r_node_idx_bytes, 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 status = psa_hash_update(&op, r_node_idx_bytes, 4);
192 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100193 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 status = psa_hash_update(&op, D_INTR_CONSTANT_BYTES, D_CONST_LEN);
197 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 status = psa_hash_update(&op, left_node,
202 MBEDTLS_LMS_M_NODE_BYTES(params->type));
203 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100204 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 status = psa_hash_update(&op, right_node,
208 MBEDTLS_LMS_M_NODE_BYTES(params->type));
209 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100210 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
214 &output_hash_len);
215 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100218
Raef Coles01c71a12022-08-31 15:55:00 +0100219exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100221
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500222 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100223}
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100226{
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 memset(ctx, 0, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100228}
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100231{
Troy-Butler9ac3e232024-03-22 14:46:04 -0400232 if (ctx == NULL) {
233 return;
234 }
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100237}
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx,
240 const unsigned char *key, size_t key_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100241{
Raef Coles01c71a12022-08-31 15:55:00 +0100242 mbedtls_lms_algorithm_type_t type;
243 mbedtls_lmots_algorithm_type_t otstype;
244
Minos Galanakis548e2db2025-06-02 14:17:38 +0100245 if (key_size < 4) {
246 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
247 }
248
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000249 type = (mbedtls_lms_algorithm_type_t) MBEDTLS_GET_UINT32_BE(key, PUBLIC_KEY_TYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
251 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100252 }
Raef Colesf5632d32022-09-01 09:56:52 +0100253 ctx->params.type = 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
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000259 otstype = (mbedtls_lmots_algorithm_type_t)
260 MBEDTLS_GET_UINT32_BE(key, PUBLIC_KEY_OTSTYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
262 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100263 }
Raef Colesf5632d32022-09-01 09:56:52 +0100264 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 memcpy(ctx->params.I_key_identifier,
267 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
268 MBEDTLS_LMOTS_I_KEY_ID_LEN);
269 memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
270 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100271
Raef Colesf5632d32022-09-01 09:56:52 +0100272 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100275}
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
278 unsigned char *key,
279 size_t key_size, size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100280{
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
282 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100283 }
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (!ctx->have_public_key) {
286 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100287 }
288
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000289 MBEDTLS_PUT_UINT32_BE(ctx->params.type, key, PUBLIC_KEY_TYPE_OFFSET);
290 MBEDTLS_PUT_UINT32_BE(ctx->params.otstype, key, PUBLIC_KEY_OTSTYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
292 ctx->params.I_key_identifier,
293 MBEDTLS_LMOTS_I_KEY_ID_LEN);
294 memcpy(key +PUBLIC_KEY_ROOT_NODE_OFFSET,
295 ctx->T_1_pub_key,
296 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100299 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
300 }
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100303}
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx,
306 const unsigned char *msg, size_t msg_size,
307 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100308{
309 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100310 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
311 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100312 unsigned int height;
313 unsigned int curr_node_id;
314 unsigned int parent_node_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 const unsigned char *left_node;
316 const unsigned char *right_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100317 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (!ctx->have_public_key) {
321 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100322 }
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (ctx->params.type
325 != MBEDTLS_LMS_SHA256_M32_H10) {
326 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100327 }
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (ctx->params.otstype
330 != MBEDTLS_LMOTS_SHA256_N32_W8) {
331 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100332 }
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
335 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100336 }
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
339 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100340 }
341
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000342 if (MBEDTLS_GET_UINT32_BE(sig, SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 != MBEDTLS_LMOTS_SHA256_N32_W8) {
344 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100345 }
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
348 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100349 }
350
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000351 if (MBEDTLS_GET_UINT32_BE(sig, SIG_TYPE_OFFSET(ctx->params.otstype))
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 != MBEDTLS_LMS_SHA256_M32_H10) {
353 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100354 }
355
356
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000357 q_leaf_identifier = MBEDTLS_GET_UINT32_BE(sig, SIG_Q_LEAF_ID_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
360 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100361 }
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 memcpy(ots_params.I_key_identifier,
364 ctx->params.I_key_identifier,
365 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000366 MBEDTLS_PUT_UINT32_BE(q_leaf_identifier, ots_params.q_leaf_identifier, 0);
Raef Colesf5632d32022-09-01 09:56:52 +0100367 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
370 msg,
371 msg_size,
372 sig + SIG_OTS_SIG_OFFSET,
373 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
374 Kc_candidate_ots_pub_key,
375 sizeof(Kc_candidate_ots_pub_key),
376 NULL);
377 if (ret != 0) {
378 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100379 }
380
Raef Colesebd35b52022-09-01 11:52:17 +0100381 create_merkle_leaf_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 &ctx->params,
383 Kc_candidate_ots_pub_key,
384 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
385 Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100386
Raef Coles366d67d2022-09-01 17:23:12 +0100387 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
388 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
391 height++) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100392 parent_node_id = curr_node_id / 2;
393
394 /* Left/right node ordering matters for the hash */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (curr_node_id & 1) {
Raef Coles57d53282022-09-27 11:30:51 +0100396 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100397 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100398 right_node = Tc_candidate_root_node;
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 } else {
Raef Coles8ff6df52021-07-21 12:42:15 +0100400 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100401 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100402 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100403 }
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 create_merkle_internal_value(&ctx->params, left_node, right_node,
406 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100407
408 curr_node_id /= 2;
409 }
410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
412 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
413 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100414 }
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100417}
418
Raef Coles5127e852022-10-07 10:35:56 +0100419#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100420
Raef Coles285d44b2022-10-10 15:44:17 +0100421/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100422 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100423 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100424 *
Raef Coles98d6e222022-09-23 09:04:04 +0100425 * ctx The LMS private context, containing a parameter
426 * set and private key material consisting of both
427 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100428 *
Raef Coles98d6e222022-09-23 09:04:04 +0100429 * tree The output tree, which is 2^(H + 1) hash outputs.
430 * In the case of H=10 we have 2048 tree nodes (of
431 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100432 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100433 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100434 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
436 unsigned char *tree)
Raef Colesab4f8742022-09-01 12:24:31 +0100437{
438 unsigned int priv_key_idx;
439 unsigned int r_node_idx;
440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
441
442 /* First create the leaf nodes, in ascending order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 for (priv_key_idx = 0;
Raef Colese9479a02022-09-01 16:06:35 +0100444 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 priv_key_idx++) {
Raef Colese9479a02022-09-01 16:06:35 +0100446 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 ret = create_merkle_leaf_value(&ctx->params,
449 ctx->ots_public_keys[priv_key_idx].public_key,
450 r_node_idx,
451 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(
452 ctx->params.type)]);
453 if (ret != 0) {
454 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100455 }
456 }
457
458 /* Then the internal nodes, in reverse order so that we can guarantee the
459 * parent has been created */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
Raef Colese9479a02022-09-01 16:06:35 +0100461 r_node_idx > 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 r_node_idx--) {
463 ret = create_merkle_internal_value(&ctx->params,
464 &tree[(r_node_idx * 2) *
465 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
466 &tree[(r_node_idx * 2 + 1) *
467 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
468 r_node_idx,
469 &tree[r_node_idx *
470 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]);
471 if (ret != 0) {
472 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100473 }
474 }
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 return 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100477}
478
Raef Coles285d44b2022-10-10 15:44:17 +0100479/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100480 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100481 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100482 *
Raef Coles98d6e222022-09-23 09:04:04 +0100483 * ctx The LMS private context, containing a parameter
484 * set and private key material consisting of both
485 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100486 *
Raef Coles98d6e222022-09-23 09:04:04 +0100487 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100488 *
Raef Coles75b4c772022-10-10 13:58:28 +0100489 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100490 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491static int get_merkle_path(mbedtls_lms_private_t *ctx,
492 unsigned int leaf_node_id,
493 unsigned char *path)
Raef Colesab4f8742022-09-01 12:24:31 +0100494{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800495 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100496 unsigned int curr_node_id = leaf_node_id;
497 unsigned int adjacent_node_id;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800498 unsigned char *tree = NULL;
Raef Colesab4f8742022-09-01 12:24:31 +0100499 unsigned int height;
500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
501
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000502 tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(ctx->params.type),
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 node_bytes);
504 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800505 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
506 }
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 ret = calculate_merkle_tree(ctx, tree);
509 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100510 goto exit;
Raef Colesab4f8742022-09-01 12:24:31 +0100511 }
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
514 height++) {
Raef Colesab4f8742022-09-01 12:24:31 +0100515 adjacent_node_id = curr_node_id ^ 1;
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 memcpy(&path[height * node_bytes],
518 &tree[adjacent_node_id * node_bytes], node_bytes);
Raef Colesab4f8742022-09-01 12:24:31 +0100519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 curr_node_id >>= 1;
Raef Colesab4f8742022-09-01 12:24:31 +0100521 }
522
Raef Coles142e5772022-10-12 10:47:27 +0100523 ret = 0;
524
525exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100526 mbedtls_zeroize_and_free(tree, node_bytes *
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000527 (size_t) MERKLE_TREE_NODE_AM(ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100530}
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100533{
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100535}
Raef Coles8ff6df52021-07-21 12:42:15 +0100536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100538{
Troy-Butler9ac3e232024-03-22 14:46:04 -0400539 if (ctx == NULL) {
540 return;
541 }
542
Raef Coles01c71a12022-08-31 15:55:00 +0100543 unsigned int idx;
544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (ctx->have_private_key) {
546 if (ctx->ots_private_keys != NULL) {
547 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
548 mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100549 }
Raef Coles01c71a12022-08-31 15:55:00 +0100550 }
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (ctx->ots_public_keys != NULL) {
553 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
554 mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100555 }
556 }
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 mbedtls_free(ctx->ots_private_keys);
559 mbedtls_free(ctx->ots_public_keys);
Raef Coles8ff6df52021-07-21 12:42:15 +0100560 }
561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100563}
Raef Coles8ff6df52021-07-21 12:42:15 +0100564
Raef Coles01c71a12022-08-31 15:55:00 +0100565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
567 mbedtls_lms_algorithm_type_t type,
568 mbedtls_lmots_algorithm_type_t otstype,
569 int (*f_rng)(void *, unsigned char *, size_t),
570 void *p_rng, const unsigned char *seed,
571 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100572{
573 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
577 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100578 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
581 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100582 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (ctx->have_private_key) {
585 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100586 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100587
Raef Colesf5632d32022-09-01 09:56:52 +0100588 ctx->params.type = type;
589 ctx->params.otstype = otstype;
Raef Colescbd02ad2022-10-13 14:11:49 +0100590 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 ret = f_rng(p_rng,
593 ctx->params.I_key_identifier,
594 MBEDTLS_LMOTS_I_KEY_ID_LEN);
595 if (ret != 0) {
Raef Coles3f6cdd72022-10-07 14:07:59 +0100596 goto exit;
597 }
Raef Coles01c71a12022-08-31 15:55:00 +0100598
Raef Coles45c4ff92022-10-12 15:22:48 +0100599 /* Requires a cast to size_t to avoid an implicit cast warning on certain
600 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
602 sizeof(*ctx->ots_private_keys));
603 if (ctx->ots_private_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100604 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
605 goto exit;
606 }
607
Raef Coles45c4ff92022-10-12 15:22:48 +0100608 /* Requires a cast to size_t to avoid an implicit cast warning on certain
609 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
611 sizeof(*ctx->ots_public_keys));
612 if (ctx->ots_public_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100613 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
614 goto exit;
615 }
616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
618 mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
619 mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]);
Raef Coles01c71a12022-08-31 15:55:00 +0100620 }
621
622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
624 ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx],
625 otstype,
626 ctx->params.I_key_identifier,
627 idx, seed, seed_size);
628 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 }
Raef Coles01c71a12022-08-31 15:55:00 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
633 &ctx->ots_private_keys[idx]);
634 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100635 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 }
Raef Coles01c71a12022-08-31 15:55:00 +0100637 }
638
Raef Colesf5632d32022-09-01 09:56:52 +0100639 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100640
641exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (ret != 0) {
Raef Colesdc8fb792022-10-07 13:27:54 +0100643 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100644 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100647}
648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
650 const mbedtls_lms_private_t *priv_ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100651{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800652 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800654 unsigned char *tree = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +0100655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (!priv_ctx->have_private_key) {
657 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100658 }
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (priv_ctx->params.type
661 != MBEDTLS_LMS_SHA256_M32_H10) {
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.otstype
666 != MBEDTLS_LMOTS_SHA256_N32_W8) {
667 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100668 }
669
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000670 tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(priv_ctx->params.type),
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 node_bytes);
672 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800673 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
674 }
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 memcpy(&ctx->params, &priv_ctx->params,
677 sizeof(mbedtls_lmots_parameters_t));
Raef Coles8ff6df52021-07-21 12:42:15 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 ret = calculate_merkle_tree(priv_ctx, tree);
680 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100681 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100682 }
683
684 /* Root node is always at position 1, due to 1-based indexing */
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100686
Raef Colesf5632d32022-09-01 09:56:52 +0100687 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100688
Raef Coles142e5772022-10-12 10:47:27 +0100689 ret = 0;
690
691exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100692 mbedtls_zeroize_and_free(tree, node_bytes *
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000693 (size_t) MERKLE_TREE_NODE_AM(priv_ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100696}
697
Raef Coles01c71a12022-08-31 15:55:00 +0100698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
700 int (*f_rng)(void *, unsigned char *, size_t),
701 void *p_rng, const unsigned char *msg,
702 unsigned int msg_size, unsigned char *sig, size_t sig_size,
703 size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100704{
705 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (!ctx->have_private_key) {
709 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100710 }
711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
713 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100714 }
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
717 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100718 }
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (ctx->params.otstype
721 != MBEDTLS_LMOTS_SHA256_N32_W8) {
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->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
726 return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
Raef Coles8ff6df52021-07-21 12:42:15 +0100727 }
728
729
Raef Colesf5632d32022-09-01 09:56:52 +0100730 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100731 /* This new value must _always_ be written back to the disk before the
732 * signature is returned.
733 */
Raef Colesf5632d32022-09-01 09:56:52 +0100734 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
737 < SIG_OTS_SIG_OFFSET) {
738 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles1fb2f322022-10-10 11:23:07 +0100739 }
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
742 f_rng,
743 p_rng,
744 msg,
745 msg_size,
746 sig + SIG_OTS_SIG_OFFSET,
747 MBEDTLS_LMS_SIG_LEN(ctx->params.type,
748 ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
749 NULL);
750 if (ret != 0) {
751 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100752 }
753
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000754 MBEDTLS_PUT_UINT32_BE(ctx->params.type, sig, SIG_TYPE_OFFSET(ctx->params.otstype));
755 MBEDTLS_PUT_UINT32_BE(q_leaf_identifier, sig, SIG_Q_LEAF_ID_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 ret = get_merkle_path(ctx,
758 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
759 sig + SIG_PATH_OFFSET(ctx->params.otstype));
760 if (ret != 0) {
761 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100762 }
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100765 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100766 }
767
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100770}
771
Raef Coles5127e852022-10-07 10:35:56 +0100772#endif /* defined(MBEDTLS_LMS_PRIVATE) */
773#endif /* defined(MBEDTLS_LMS_C) */