blob: 0c470a0c34338bac72609247500607bee33f492c [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))
68#define MERKLE_TREE_INTERNAL_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010069
70#define D_CONST_LEN (2)
Gilles Peskine449bd832023-01-11 14:50:10 +010071static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = { 0x82, 0x82 };
72static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = { 0x83, 0x83 };
Raef Coles8ff6df52021-07-21 12:42:15 +010073
Raef Coles0a967cc2022-09-02 17:46:15 +010074
Raef Coles285d44b2022-10-10 15:44:17 +010075/* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
Raef Coles0a967cc2022-09-02 17:46:15 +010076 * public key and some other parameters like the leaf index). This function
77 * implements RFC8554 section 5.3, in the case where r >= 2^h.
78 *
Raef Coles98d6e222022-09-23 09:04:04 +010079 * params The LMS parameter set, the underlying LMOTS
80 * parameter set, and I value which describe the key
81 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +010082 *
Raef Coles98d6e222022-09-23 09:04:04 +010083 * pub_key The public key of the private whose index
84 * corresponds to the index of this leaf node. This
85 * is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010086 *
Raef Coles285d44b2022-10-10 15:44:17 +010087 * r_node_idx The index of this node in the Merkle tree. Note
88 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +010089 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +010090 *
Raef Coles98d6e222022-09-23 09:04:04 +010091 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010092 */
Gilles Peskine449bd832023-01-11 14:50:10 +010093static int create_merkle_leaf_value(const mbedtls_lms_parameters_t *params,
94 unsigned char *pub_key,
95 unsigned int r_node_idx,
96 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +010097{
Raef Colesc8f96042022-08-25 13:49:54 +010098 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +010099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100100 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100101 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 op = psa_hash_operation_init();
104 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
105 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 status = psa_hash_update(&op, params->I_key_identifier,
110 MBEDTLS_LMOTS_I_KEY_ID_LEN);
111 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
116 status = psa_hash_update(&op, r_node_idx_bytes, 4);
117 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 status = psa_hash_update(&op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN);
122 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 status = psa_hash_update(&op, pub_key,
127 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype));
128 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
133 &output_hash_len);
134 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100137
Raef Coles01c71a12022-08-31 15:55:00 +0100138exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100140
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500141 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100142}
143
Raef Coles285d44b2022-10-10 15:44:17 +0100144/* Calculate the value of an internal node of the Merkle tree (which is a hash
Raef Coles0a967cc2022-09-02 17:46:15 +0100145 * of a public key and some other parameters like the node index). This function
146 * implements RFC8554 section 5.3, in the case where r < 2^h.
147 *
Raef Coles98d6e222022-09-23 09:04:04 +0100148 * params The LMS parameter set, the underlying LMOTS
149 * parameter set, and I value which describe the key
150 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100151 *
Raef Coles98d6e222022-09-23 09:04:04 +0100152 * left_node The value of the child of this node which is on
153 * the left-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100154 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100155 *
Raef Coles98d6e222022-09-23 09:04:04 +0100156 * right_node The value of the child of this node which is on
157 * the right-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 Coles285d44b2022-10-10 15:44:17 +0100160 * r_node_idx The index of this node in the Merkle tree. Note
161 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100162 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100163 *
Raef Coles98d6e222022-09-23 09:04:04 +0100164 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100165 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166static int create_merkle_internal_value(const mbedtls_lms_parameters_t *params,
167 const unsigned char *left_node,
168 const unsigned char *right_node,
169 unsigned int r_node_idx,
170 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100171{
Raef Colesc8f96042022-08-25 13:49:54 +0100172 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100174 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100175 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 op = psa_hash_operation_init();
178 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
179 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 status = psa_hash_update(&op, params->I_key_identifier,
184 MBEDTLS_LMOTS_I_KEY_ID_LEN);
185 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100186 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
190 status = psa_hash_update(&op, r_node_idx_bytes, 4);
191 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 status = psa_hash_update(&op, D_INTR_CONSTANT_BYTES, D_CONST_LEN);
196 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100197 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 status = psa_hash_update(&op, left_node,
201 MBEDTLS_LMS_M_NODE_BYTES(params->type));
202 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100203 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 status = psa_hash_update(&op, right_node,
207 MBEDTLS_LMS_M_NODE_BYTES(params->type));
208 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100209 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
213 &output_hash_len);
214 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100217
Raef Coles01c71a12022-08-31 15:55:00 +0100218exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100220
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500221 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100222}
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100225{
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 memset(ctx, 0, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100227}
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100230{
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100232}
233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx,
235 const unsigned char *key, size_t key_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100236{
Raef Coles01c71a12022-08-31 15:55:00 +0100237 mbedtls_lms_algorithm_type_t type;
238 mbedtls_lmots_algorithm_type_t otstype;
239
Agathiyan Bragadeesh10b67752023-07-12 11:19:17 +0100240 type = (mbedtls_lms_algorithm_type_t) mbedtls_lms_network_bytes_to_unsigned_int(
241 MBEDTLS_LMS_TYPE_LEN,
242 key +
243 PUBLIC_KEY_TYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
245 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100246 }
Raef Colesf5632d32022-09-01 09:56:52 +0100247 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
250 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese89488d2022-10-07 16:06:35 +0100251 }
252
Agathiyan Bragadeesh10b67752023-07-12 11:19:17 +0100253 otstype = (mbedtls_lmots_algorithm_type_t) mbedtls_lms_network_bytes_to_unsigned_int(
254 MBEDTLS_LMOTS_TYPE_LEN,
255 key +
256 PUBLIC_KEY_OTSTYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
258 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100259 }
Raef Colesf5632d32022-09-01 09:56:52 +0100260 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 memcpy(ctx->params.I_key_identifier,
263 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
264 MBEDTLS_LMOTS_I_KEY_ID_LEN);
265 memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
266 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100267
Raef Colesf5632d32022-09-01 09:56:52 +0100268 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100271}
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
274 unsigned char *key,
275 size_t key_size, size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100276{
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
278 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100279 }
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (!ctx->have_public_key) {
282 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100283 }
284
285 mbedtls_lms_unsigned_int_to_network_bytes(
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 ctx->params.type,
287 MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET);
288 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.otstype,
289 MBEDTLS_LMOTS_TYPE_LEN,
290 key + PUBLIC_KEY_OTSTYPE_OFFSET);
291 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
343 sig + SIG_OTS_SIG_OFFSET +
344 MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
345 != MBEDTLS_LMOTS_SHA256_N32_W8) {
346 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100347 }
348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
350 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
354 sig + SIG_TYPE_OFFSET(ctx->params.otstype))
355 != MBEDTLS_LMS_SHA256_M32_H10) {
356 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100357 }
358
359
Raef Colesad054252022-09-27 10:59:16 +0100360 q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
364 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100365 }
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 memcpy(ots_params.I_key_identifier,
368 ctx->params.I_key_identifier,
369 MBEDTLS_LMOTS_I_KEY_ID_LEN);
370 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
Raef Colesad054252022-09-27 10:59:16 +0100371 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 ots_params.q_leaf_identifier);
Raef Colesf5632d32022-09-01 09:56:52 +0100373 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
376 msg,
377 msg_size,
378 sig + SIG_OTS_SIG_OFFSET,
379 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
380 Kc_candidate_ots_pub_key,
381 sizeof(Kc_candidate_ots_pub_key),
382 NULL);
383 if (ret != 0) {
384 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100385 }
386
Raef Colesebd35b52022-09-01 11:52:17 +0100387 create_merkle_leaf_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 &ctx->params,
389 Kc_candidate_ots_pub_key,
390 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
391 Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100392
Raef Coles366d67d2022-09-01 17:23:12 +0100393 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
394 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
397 height++) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100398 parent_node_id = curr_node_id / 2;
399
400 /* Left/right node ordering matters for the hash */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (curr_node_id & 1) {
Raef Coles57d53282022-09-27 11:30:51 +0100402 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100403 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100404 right_node = Tc_candidate_root_node;
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 } else {
Raef Coles8ff6df52021-07-21 12:42:15 +0100406 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100407 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100408 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100409 }
410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 create_merkle_internal_value(&ctx->params, left_node, right_node,
412 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100413
414 curr_node_id /= 2;
415 }
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
418 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
419 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100420 }
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100423}
424
Raef Coles5127e852022-10-07 10:35:56 +0100425#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100426
Raef Coles285d44b2022-10-10 15:44:17 +0100427/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100428 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100429 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100430 *
Raef Coles98d6e222022-09-23 09:04:04 +0100431 * ctx The LMS private context, containing a parameter
432 * set and private key material consisting of both
433 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100434 *
Raef Coles98d6e222022-09-23 09:04:04 +0100435 * tree The output tree, which is 2^(H + 1) hash outputs.
436 * In the case of H=10 we have 2048 tree nodes (of
437 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100438 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100439 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100440 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
442 unsigned char *tree)
Raef Colesab4f8742022-09-01 12:24:31 +0100443{
444 unsigned int priv_key_idx;
445 unsigned int r_node_idx;
446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
447
448 /* First create the leaf nodes, in ascending order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 for (priv_key_idx = 0;
Raef Colese9479a02022-09-01 16:06:35 +0100450 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 priv_key_idx++) {
Raef Colese9479a02022-09-01 16:06:35 +0100452 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 ret = create_merkle_leaf_value(&ctx->params,
455 ctx->ots_public_keys[priv_key_idx].public_key,
456 r_node_idx,
457 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(
458 ctx->params.type)]);
459 if (ret != 0) {
460 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100461 }
462 }
463
464 /* Then the internal nodes, in reverse order so that we can guarantee the
465 * parent has been created */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
Raef Colese9479a02022-09-01 16:06:35 +0100467 r_node_idx > 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 r_node_idx--) {
469 ret = create_merkle_internal_value(&ctx->params,
470 &tree[(r_node_idx * 2) *
471 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
472 &tree[(r_node_idx * 2 + 1) *
473 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
474 r_node_idx,
475 &tree[r_node_idx *
476 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]);
477 if (ret != 0) {
478 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100479 }
480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100483}
484
Raef Coles285d44b2022-10-10 15:44:17 +0100485/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100486 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100487 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100488 *
Raef Coles98d6e222022-09-23 09:04:04 +0100489 * ctx The LMS private context, containing a parameter
490 * set and private key material consisting of both
491 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100492 *
Raef Coles98d6e222022-09-23 09:04:04 +0100493 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100494 *
Raef Coles75b4c772022-10-10 13:58:28 +0100495 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100496 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497static int get_merkle_path(mbedtls_lms_private_t *ctx,
498 unsigned int leaf_node_id,
499 unsigned char *path)
Raef Colesab4f8742022-09-01 12:24:31 +0100500{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800501 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100502 unsigned int curr_node_id = leaf_node_id;
503 unsigned int adjacent_node_id;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800504 unsigned char *tree = NULL;
Raef Colesab4f8742022-09-01 12:24:31 +0100505 unsigned int height;
506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(ctx->params.type),
509 node_bytes);
510 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800511 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
512 }
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 ret = calculate_merkle_tree(ctx, tree);
515 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100516 goto exit;
Raef Colesab4f8742022-09-01 12:24:31 +0100517 }
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
520 height++) {
Raef Colesab4f8742022-09-01 12:24:31 +0100521 adjacent_node_id = curr_node_id ^ 1;
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 memcpy(&path[height * node_bytes],
524 &tree[adjacent_node_id * node_bytes], node_bytes);
Raef Colesab4f8742022-09-01 12:24:31 +0100525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 curr_node_id >>= 1;
Raef Colesab4f8742022-09-01 12:24:31 +0100527 }
528
Raef Coles142e5772022-10-12 10:47:27 +0100529 ret = 0;
530
531exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100532 mbedtls_zeroize_and_free(tree, node_bytes *
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MERKLE_TREE_NODE_AM(ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100536}
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100539{
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100541}
Raef Coles8ff6df52021-07-21 12:42:15 +0100542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100544{
545 unsigned int idx;
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (ctx->have_private_key) {
548 if (ctx->ots_private_keys != NULL) {
549 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
550 mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100551 }
Raef Coles01c71a12022-08-31 15:55:00 +0100552 }
553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (ctx->ots_public_keys != NULL) {
555 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
556 mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100557 }
558 }
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 mbedtls_free(ctx->ots_private_keys);
561 mbedtls_free(ctx->ots_public_keys);
Raef Coles8ff6df52021-07-21 12:42:15 +0100562 }
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100565}
Raef Coles8ff6df52021-07-21 12:42:15 +0100566
Raef Coles01c71a12022-08-31 15:55:00 +0100567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
569 mbedtls_lms_algorithm_type_t type,
570 mbedtls_lmots_algorithm_type_t otstype,
571 int (*f_rng)(void *, unsigned char *, size_t),
572 void *p_rng, const unsigned char *seed,
573 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100574{
575 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
579 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100580 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
583 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100584 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ctx->have_private_key) {
587 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100588 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100589
Raef Colesf5632d32022-09-01 09:56:52 +0100590 ctx->params.type = type;
591 ctx->params.otstype = otstype;
Raef Colescbd02ad2022-10-13 14:11:49 +0100592 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 ret = f_rng(p_rng,
595 ctx->params.I_key_identifier,
596 MBEDTLS_LMOTS_I_KEY_ID_LEN);
597 if (ret != 0) {
Raef Coles3f6cdd72022-10-07 14:07:59 +0100598 goto exit;
599 }
Raef Coles01c71a12022-08-31 15:55:00 +0100600
Raef Coles45c4ff92022-10-12 15:22:48 +0100601 /* Requires a cast to size_t to avoid an implicit cast warning on certain
602 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
604 sizeof(*ctx->ots_private_keys));
605 if (ctx->ots_private_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100606 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
607 goto exit;
608 }
609
Raef Coles45c4ff92022-10-12 15:22:48 +0100610 /* Requires a cast to size_t to avoid an implicit cast warning on certain
611 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
613 sizeof(*ctx->ots_public_keys));
614 if (ctx->ots_public_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100615 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
616 goto exit;
617 }
618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
620 mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
621 mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]);
Raef Coles01c71a12022-08-31 15:55:00 +0100622 }
623
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
626 ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx],
627 otstype,
628 ctx->params.I_key_identifier,
629 idx, seed, seed_size);
630 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100631 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 }
Raef Coles01c71a12022-08-31 15:55:00 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
635 &ctx->ots_private_keys[idx]);
636 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100637 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 }
Raef Coles01c71a12022-08-31 15:55:00 +0100639 }
640
Raef Colesf5632d32022-09-01 09:56:52 +0100641 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100642
643exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (ret != 0) {
Raef Colesdc8fb792022-10-07 13:27:54 +0100645 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100646 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100649}
650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
652 const mbedtls_lms_private_t *priv_ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100653{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800654 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800656 unsigned char *tree = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +0100657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (!priv_ctx->have_private_key) {
659 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100660 }
661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (priv_ctx->params.type
663 != MBEDTLS_LMS_SHA256_M32_H10) {
664 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100665 }
666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (priv_ctx->params.otstype
668 != MBEDTLS_LMOTS_SHA256_N32_W8) {
669 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(priv_ctx->params.type),
673 node_bytes);
674 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800675 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
676 }
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 memcpy(&ctx->params, &priv_ctx->params,
679 sizeof(mbedtls_lmots_parameters_t));
Raef Coles8ff6df52021-07-21 12:42:15 +0100680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 ret = calculate_merkle_tree(priv_ctx, tree);
682 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100683 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100684 }
685
686 /* Root node is always at position 1, due to 1-based indexing */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100688
Raef Colesf5632d32022-09-01 09:56:52 +0100689 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100690
Raef Coles142e5772022-10-12 10:47:27 +0100691 ret = 0;
692
693exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100694 mbedtls_zeroize_and_free(tree, node_bytes *
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 MERKLE_TREE_NODE_AM(priv_ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100698}
699
Raef Coles01c71a12022-08-31 15:55:00 +0100700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
702 int (*f_rng)(void *, unsigned char *, size_t),
703 void *p_rng, const unsigned char *msg,
704 unsigned int msg_size, unsigned char *sig, size_t sig_size,
705 size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100706{
707 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (!ctx->have_private_key) {
711 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100712 }
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
715 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100716 }
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
719 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100720 }
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if (ctx->params.otstype
723 != MBEDTLS_LMOTS_SHA256_N32_W8) {
724 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100725 }
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
728 return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
Raef Coles8ff6df52021-07-21 12:42:15 +0100729 }
730
731
Raef Colesf5632d32022-09-01 09:56:52 +0100732 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100733 /* This new value must _always_ be written back to the disk before the
734 * signature is returned.
735 */
Raef Colesf5632d32022-09-01 09:56:52 +0100736 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
739 < SIG_OTS_SIG_OFFSET) {
740 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles1fb2f322022-10-10 11:23:07 +0100741 }
742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
744 f_rng,
745 p_rng,
746 msg,
747 msg_size,
748 sig + SIG_OTS_SIG_OFFSET,
749 MBEDTLS_LMS_SIG_LEN(ctx->params.type,
750 ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
751 NULL);
752 if (ret != 0) {
753 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100754 }
755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
757 MBEDTLS_LMS_TYPE_LEN,
758 sig + SIG_TYPE_OFFSET(ctx->params.otstype));
759 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
760 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
761 sig + SIG_Q_LEAF_ID_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 ret = get_merkle_path(ctx,
764 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
765 sig + SIG_PATH_OFFSET(ctx->params.otstype));
766 if (ret != 0) {
767 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100768 }
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100771 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100772 }
773
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100776}
777
Raef Coles5127e852022-10-07 10:35:56 +0100778#endif /* defined(MBEDTLS_LMS_PRIVATE) */
779#endif /* defined(MBEDTLS_LMS_C) */