blob: 8d3cae0524931b38628ab2d761485b0ab0961f22 [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{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100233}
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx,
236 const unsigned char *key, size_t key_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100237{
Raef Coles01c71a12022-08-31 15:55:00 +0100238 mbedtls_lms_algorithm_type_t type;
239 mbedtls_lmots_algorithm_type_t otstype;
240
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000241 type = (mbedtls_lms_algorithm_type_t) MBEDTLS_GET_UINT32_BE(key, PUBLIC_KEY_TYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
243 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100244 }
Raef Colesf5632d32022-09-01 09:56:52 +0100245 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
248 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese89488d2022-10-07 16:06:35 +0100249 }
250
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000251 otstype = (mbedtls_lmots_algorithm_type_t)
252 MBEDTLS_GET_UINT32_BE(key, PUBLIC_KEY_OTSTYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
254 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100255 }
Raef Colesf5632d32022-09-01 09:56:52 +0100256 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 memcpy(ctx->params.I_key_identifier,
259 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
260 MBEDTLS_LMOTS_I_KEY_ID_LEN);
261 memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
262 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100263
Raef Colesf5632d32022-09-01 09:56:52 +0100264 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100267}
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
270 unsigned char *key,
271 size_t key_size, size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100272{
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
274 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100275 }
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (!ctx->have_public_key) {
278 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100279 }
280
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000281 MBEDTLS_PUT_UINT32_BE(ctx->params.type, key, PUBLIC_KEY_TYPE_OFFSET);
282 MBEDTLS_PUT_UINT32_BE(ctx->params.otstype, key, PUBLIC_KEY_OTSTYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
284 ctx->params.I_key_identifier,
285 MBEDTLS_LMOTS_I_KEY_ID_LEN);
286 memcpy(key +PUBLIC_KEY_ROOT_NODE_OFFSET,
287 ctx->T_1_pub_key,
288 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100291 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
292 }
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100295}
296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx,
298 const unsigned char *msg, size_t msg_size,
299 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100300{
301 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100302 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
303 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100304 unsigned int height;
305 unsigned int curr_node_id;
306 unsigned int parent_node_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 const unsigned char *left_node;
308 const unsigned char *right_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100309 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (!ctx->have_public_key) {
313 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100314 }
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if (ctx->params.type
317 != MBEDTLS_LMS_SHA256_M32_H10) {
318 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100319 }
320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if (ctx->params.otstype
322 != MBEDTLS_LMOTS_SHA256_N32_W8) {
323 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100324 }
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
327 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100328 }
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
331 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100332 }
333
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000334 if (MBEDTLS_GET_UINT32_BE(sig, SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 != MBEDTLS_LMOTS_SHA256_N32_W8) {
336 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100337 }
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
340 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100341 }
342
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000343 if (MBEDTLS_GET_UINT32_BE(sig, SIG_TYPE_OFFSET(ctx->params.otstype))
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 != MBEDTLS_LMS_SHA256_M32_H10) {
345 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100346 }
347
348
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000349 q_leaf_identifier = MBEDTLS_GET_UINT32_BE(sig, SIG_Q_LEAF_ID_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
352 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100353 }
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 memcpy(ots_params.I_key_identifier,
356 ctx->params.I_key_identifier,
357 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000358 MBEDTLS_PUT_UINT32_BE(q_leaf_identifier, ots_params.q_leaf_identifier, 0);
Raef Colesf5632d32022-09-01 09:56:52 +0100359 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
362 msg,
363 msg_size,
364 sig + SIG_OTS_SIG_OFFSET,
365 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
366 Kc_candidate_ots_pub_key,
367 sizeof(Kc_candidate_ots_pub_key),
368 NULL);
369 if (ret != 0) {
370 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100371 }
372
Raef Colesebd35b52022-09-01 11:52:17 +0100373 create_merkle_leaf_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 &ctx->params,
375 Kc_candidate_ots_pub_key,
376 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
377 Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100378
Raef Coles366d67d2022-09-01 17:23:12 +0100379 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
380 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
383 height++) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100384 parent_node_id = curr_node_id / 2;
385
386 /* Left/right node ordering matters for the hash */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (curr_node_id & 1) {
Raef Coles57d53282022-09-27 11:30:51 +0100388 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100389 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100390 right_node = Tc_candidate_root_node;
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 } else {
Raef Coles8ff6df52021-07-21 12:42:15 +0100392 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100393 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100394 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100395 }
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 create_merkle_internal_value(&ctx->params, left_node, right_node,
398 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100399
400 curr_node_id /= 2;
401 }
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
404 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
405 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100406 }
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100409}
410
Raef Coles5127e852022-10-07 10:35:56 +0100411#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100412
Raef Coles285d44b2022-10-10 15:44:17 +0100413/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100414 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100415 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100416 *
Raef Coles98d6e222022-09-23 09:04:04 +0100417 * ctx The LMS private context, containing a parameter
418 * set and private key material consisting of both
419 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100420 *
Raef Coles98d6e222022-09-23 09:04:04 +0100421 * tree The output tree, which is 2^(H + 1) hash outputs.
422 * In the case of H=10 we have 2048 tree nodes (of
423 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100424 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100425 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100426 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
428 unsigned char *tree)
Raef Colesab4f8742022-09-01 12:24:31 +0100429{
430 unsigned int priv_key_idx;
431 unsigned int r_node_idx;
432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
433
434 /* First create the leaf nodes, in ascending order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 for (priv_key_idx = 0;
Raef Colese9479a02022-09-01 16:06:35 +0100436 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 priv_key_idx++) {
Raef Colese9479a02022-09-01 16:06:35 +0100438 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 ret = create_merkle_leaf_value(&ctx->params,
441 ctx->ots_public_keys[priv_key_idx].public_key,
442 r_node_idx,
443 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(
444 ctx->params.type)]);
445 if (ret != 0) {
446 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100447 }
448 }
449
450 /* Then the internal nodes, in reverse order so that we can guarantee the
451 * parent has been created */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
Raef Colese9479a02022-09-01 16:06:35 +0100453 r_node_idx > 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 r_node_idx--) {
455 ret = create_merkle_internal_value(&ctx->params,
456 &tree[(r_node_idx * 2) *
457 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
458 &tree[(r_node_idx * 2 + 1) *
459 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
460 r_node_idx,
461 &tree[r_node_idx *
462 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]);
463 if (ret != 0) {
464 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100465 }
466 }
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100469}
470
Raef Coles285d44b2022-10-10 15:44:17 +0100471/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100472 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100473 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100474 *
Raef Coles98d6e222022-09-23 09:04:04 +0100475 * ctx The LMS private context, containing a parameter
476 * set and private key material consisting of both
477 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100478 *
Raef Coles98d6e222022-09-23 09:04:04 +0100479 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100480 *
Raef Coles75b4c772022-10-10 13:58:28 +0100481 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100482 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483static int get_merkle_path(mbedtls_lms_private_t *ctx,
484 unsigned int leaf_node_id,
485 unsigned char *path)
Raef Colesab4f8742022-09-01 12:24:31 +0100486{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800487 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100488 unsigned int curr_node_id = leaf_node_id;
489 unsigned int adjacent_node_id;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800490 unsigned char *tree = NULL;
Raef Colesab4f8742022-09-01 12:24:31 +0100491 unsigned int height;
492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
493
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000494 tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(ctx->params.type),
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 node_bytes);
496 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800497 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
498 }
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 ret = calculate_merkle_tree(ctx, tree);
501 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100502 goto exit;
Raef Colesab4f8742022-09-01 12:24:31 +0100503 }
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
506 height++) {
Raef Colesab4f8742022-09-01 12:24:31 +0100507 adjacent_node_id = curr_node_id ^ 1;
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 memcpy(&path[height * node_bytes],
510 &tree[adjacent_node_id * node_bytes], node_bytes);
Raef Colesab4f8742022-09-01 12:24:31 +0100511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 curr_node_id >>= 1;
Raef Colesab4f8742022-09-01 12:24:31 +0100513 }
514
Raef Coles142e5772022-10-12 10:47:27 +0100515 ret = 0;
516
517exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100518 mbedtls_zeroize_and_free(tree, node_bytes *
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000519 (size_t) MERKLE_TREE_NODE_AM(ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100522}
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100525{
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100527}
Raef Coles8ff6df52021-07-21 12:42:15 +0100528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100530{
531 unsigned int idx;
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (ctx->have_private_key) {
534 if (ctx->ots_private_keys != NULL) {
535 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
536 mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100537 }
Raef Coles01c71a12022-08-31 15:55:00 +0100538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (ctx->ots_public_keys != NULL) {
541 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
542 mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100543 }
544 }
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 mbedtls_free(ctx->ots_private_keys);
547 mbedtls_free(ctx->ots_public_keys);
Raef Coles8ff6df52021-07-21 12:42:15 +0100548 }
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100551}
Raef Coles8ff6df52021-07-21 12:42:15 +0100552
Raef Coles01c71a12022-08-31 15:55:00 +0100553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
555 mbedtls_lms_algorithm_type_t type,
556 mbedtls_lmots_algorithm_type_t otstype,
557 int (*f_rng)(void *, unsigned char *, size_t),
558 void *p_rng, const unsigned char *seed,
559 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100560{
561 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100562 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
565 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100566 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
569 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100570 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (ctx->have_private_key) {
573 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100574 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100575
Raef Colesf5632d32022-09-01 09:56:52 +0100576 ctx->params.type = type;
577 ctx->params.otstype = otstype;
Raef Colescbd02ad2022-10-13 14:11:49 +0100578 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 ret = f_rng(p_rng,
581 ctx->params.I_key_identifier,
582 MBEDTLS_LMOTS_I_KEY_ID_LEN);
583 if (ret != 0) {
Raef Coles3f6cdd72022-10-07 14:07:59 +0100584 goto exit;
585 }
Raef Coles01c71a12022-08-31 15:55:00 +0100586
Raef Coles45c4ff92022-10-12 15:22:48 +0100587 /* Requires a cast to size_t to avoid an implicit cast warning on certain
588 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
590 sizeof(*ctx->ots_private_keys));
591 if (ctx->ots_private_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100592 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
593 goto exit;
594 }
595
Raef Coles45c4ff92022-10-12 15:22:48 +0100596 /* Requires a cast to size_t to avoid an implicit cast warning on certain
597 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
599 sizeof(*ctx->ots_public_keys));
600 if (ctx->ots_public_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100601 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
602 goto exit;
603 }
604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
606 mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
607 mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]);
Raef Coles01c71a12022-08-31 15:55:00 +0100608 }
609
610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
612 ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx],
613 otstype,
614 ctx->params.I_key_identifier,
615 idx, seed, seed_size);
616 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100617 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 }
Raef Coles01c71a12022-08-31 15:55:00 +0100619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
621 &ctx->ots_private_keys[idx]);
622 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 }
Raef Coles01c71a12022-08-31 15:55:00 +0100625 }
626
Raef Colesf5632d32022-09-01 09:56:52 +0100627 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100628
629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (ret != 0) {
Raef Colesdc8fb792022-10-07 13:27:54 +0100631 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100632 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100635}
636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
638 const mbedtls_lms_private_t *priv_ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100639{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800640 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800642 unsigned char *tree = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (!priv_ctx->have_private_key) {
645 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100646 }
647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (priv_ctx->params.type
649 != MBEDTLS_LMS_SHA256_M32_H10) {
650 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100651 }
652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (priv_ctx->params.otstype
654 != MBEDTLS_LMOTS_SHA256_N32_W8) {
655 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100656 }
657
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000658 tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(priv_ctx->params.type),
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 node_bytes);
660 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800661 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
662 }
663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 memcpy(&ctx->params, &priv_ctx->params,
665 sizeof(mbedtls_lmots_parameters_t));
Raef Coles8ff6df52021-07-21 12:42:15 +0100666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 ret = calculate_merkle_tree(priv_ctx, tree);
668 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100669 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100670 }
671
672 /* Root node is always at position 1, due to 1-based indexing */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100674
Raef Colesf5632d32022-09-01 09:56:52 +0100675 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100676
Raef Coles142e5772022-10-12 10:47:27 +0100677 ret = 0;
678
679exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100680 mbedtls_zeroize_and_free(tree, node_bytes *
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000681 (size_t) MERKLE_TREE_NODE_AM(priv_ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100684}
685
Raef Coles01c71a12022-08-31 15:55:00 +0100686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
688 int (*f_rng)(void *, unsigned char *, size_t),
689 void *p_rng, const unsigned char *msg,
690 unsigned int msg_size, unsigned char *sig, size_t sig_size,
691 size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100692{
693 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (!ctx->have_private_key) {
697 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100698 }
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
701 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100702 }
703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
705 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100706 }
707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (ctx->params.otstype
709 != MBEDTLS_LMOTS_SHA256_N32_W8) {
710 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100711 }
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
714 return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
Raef Coles8ff6df52021-07-21 12:42:15 +0100715 }
716
717
Raef Colesf5632d32022-09-01 09:56:52 +0100718 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100719 /* This new value must _always_ be written back to the disk before the
720 * signature is returned.
721 */
Raef Colesf5632d32022-09-01 09:56:52 +0100722 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
725 < SIG_OTS_SIG_OFFSET) {
726 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles1fb2f322022-10-10 11:23:07 +0100727 }
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
730 f_rng,
731 p_rng,
732 msg,
733 msg_size,
734 sig + SIG_OTS_SIG_OFFSET,
735 MBEDTLS_LMS_SIG_LEN(ctx->params.type,
736 ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
737 NULL);
738 if (ret != 0) {
739 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100740 }
741
Dave Rodgmandf4d4212023-11-09 14:01:39 +0000742 MBEDTLS_PUT_UINT32_BE(ctx->params.type, sig, SIG_TYPE_OFFSET(ctx->params.otstype));
743 MBEDTLS_PUT_UINT32_BE(q_leaf_identifier, sig, SIG_Q_LEAF_ID_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 ret = get_merkle_path(ctx,
746 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
747 sig + SIG_PATH_OFFSET(ctx->params.otstype));
748 if (ret != 0) {
749 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100750 }
751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100753 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100754 }
755
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100758}
759
Raef Coles5127e852022-10-07 10:35:56 +0100760#endif /* defined(MBEDTLS_LMS_PRIVATE) */
761#endif /* defined(MBEDTLS_LMS_C) */