blob: acc35233145807667a7d2620f1a22b35b618aad6 [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
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/*
21 * The following sources were referenced in the design of this implementation
22 * of the LMS algorithm:
23 *
24 * [1] IETF RFC8554
25 * D. McGrew, M. Curcio, S.Fluhrer
26 * https://datatracker.ietf.org/doc/html/rfc8554
27 *
28 * [2] NIST Special Publication 800-208
29 * David A. Cooper et. al.
30 * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
31 */
32
33#include "common.h"
34
Raef Coles5127e852022-10-07 10:35:56 +010035#if defined(MBEDTLS_LMS_C)
Raef Coles8ff6df52021-07-21 12:42:15 +010036
37#include <string.h>
38
Raef Coles7dce69a2022-08-24 14:07:06 +010039#include "lmots.h"
40
Raef Colesc8f96042022-08-25 13:49:54 +010041#include "psa/crypto.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050042#include "mbedtls/psa_util.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010043#include "mbedtls/lms.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010044#include "mbedtls/error.h"
45#include "mbedtls/platform_util.h"
46
Raef Coles8ff6df52021-07-21 12:42:15 +010047#include "mbedtls/platform.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010048
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050049#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
50 psa_to_lms_errors, \
51 psa_generic_status_to_mbedtls)
52
Raef Coles57d53282022-09-27 11:30:51 +010053#define SIG_Q_LEAF_ID_OFFSET (0)
54#define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
55 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
56#define SIG_TYPE_OFFSET(otstype) (SIG_OTS_SIG_OFFSET + \
57 MBEDTLS_LMOTS_SIG_LEN(otstype))
58#define SIG_PATH_OFFSET(otstype) (SIG_TYPE_OFFSET(otstype) + \
59 MBEDTLS_LMS_TYPE_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010060
Raef Coles57d53282022-09-27 11:30:51 +010061#define PUBLIC_KEY_TYPE_OFFSET (0)
62#define PUBLIC_KEY_OTSTYPE_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
63 MBEDTLS_LMS_TYPE_LEN)
64#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_OTSTYPE_OFFSET + \
65 MBEDTLS_LMOTS_TYPE_LEN)
66#define PUBLIC_KEY_ROOT_NODE_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
67 MBEDTLS_LMOTS_I_KEY_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010068
69
Raef Colese9479a02022-09-01 16:06:35 +010070/* Currently only support H=10 */
Raef Coles57d53282022-09-27 11:30:51 +010071#define H_TREE_HEIGHT_MAX 10
Moritz Fischera6a94ad2022-11-12 08:28:04 -080072#define MERKLE_TREE_NODE_AM(type) ((size_t) 1 << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
73#define MERKLE_TREE_LEAF_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
74#define MERKLE_TREE_INTERNAL_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010075
76#define D_CONST_LEN (2)
Gilles Peskine449bd832023-01-11 14:50:10 +010077static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = { 0x82, 0x82 };
78static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = { 0x83, 0x83 };
Raef Coles8ff6df52021-07-21 12:42:15 +010079
Raef Coles0a967cc2022-09-02 17:46:15 +010080
Raef Coles285d44b2022-10-10 15:44:17 +010081/* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
Raef Coles0a967cc2022-09-02 17:46:15 +010082 * public key and some other parameters like the leaf index). This function
83 * implements RFC8554 section 5.3, in the case where r >= 2^h.
84 *
Raef Coles98d6e222022-09-23 09:04:04 +010085 * params The LMS parameter set, the underlying LMOTS
86 * parameter set, and I value which describe the key
87 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +010088 *
Raef Coles98d6e222022-09-23 09:04:04 +010089 * pub_key The public key of the private whose index
90 * corresponds to the index of this leaf node. This
91 * is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010092 *
Raef Coles285d44b2022-10-10 15:44:17 +010093 * r_node_idx The index of this node in the Merkle tree. Note
94 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +010095 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +010096 *
Raef Coles98d6e222022-09-23 09:04:04 +010097 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010098 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099static int create_merkle_leaf_value(const mbedtls_lms_parameters_t *params,
100 unsigned char *pub_key,
101 unsigned int r_node_idx,
102 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100103{
Raef Colesc8f96042022-08-25 13:49:54 +0100104 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100105 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100106 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100107 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 op = psa_hash_operation_init();
110 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
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 status = psa_hash_update(&op, params->I_key_identifier,
116 MBEDTLS_LMOTS_I_KEY_ID_LEN);
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 mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
122 status = psa_hash_update(&op, r_node_idx_bytes, 4);
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, D_LEAF_CONSTANT_BYTES, D_CONST_LEN);
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_update(&op, pub_key,
133 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype));
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
139 &output_hash_len);
140 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100143
Raef Coles01c71a12022-08-31 15:55:00 +0100144exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100146
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500147 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100148}
149
Raef Coles285d44b2022-10-10 15:44:17 +0100150/* Calculate the value of an internal node of the Merkle tree (which is a hash
Raef Coles0a967cc2022-09-02 17:46:15 +0100151 * of a public key and some other parameters like the node index). This function
152 * implements RFC8554 section 5.3, in the case where r < 2^h.
153 *
Raef Coles98d6e222022-09-23 09:04:04 +0100154 * params The LMS parameter set, the underlying LMOTS
155 * parameter set, and I value which describe the key
156 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100157 *
Raef Coles98d6e222022-09-23 09:04:04 +0100158 * left_node The value of the child of this node which is on
159 * the left-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100160 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100161 *
Raef Coles98d6e222022-09-23 09:04:04 +0100162 * right_node The value of the child of this node which is on
163 * the right-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100164 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100165 *
Raef Coles285d44b2022-10-10 15:44:17 +0100166 * r_node_idx The index of this node in the Merkle tree. Note
167 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100168 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100169 *
Raef Coles98d6e222022-09-23 09:04:04 +0100170 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100171 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172static int create_merkle_internal_value(const mbedtls_lms_parameters_t *params,
173 const unsigned char *left_node,
174 const unsigned char *right_node,
175 unsigned int r_node_idx,
176 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100177{
Raef Colesc8f96042022-08-25 13:49:54 +0100178 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100180 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100181 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 op = psa_hash_operation_init();
184 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
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 status = psa_hash_update(&op, params->I_key_identifier,
190 MBEDTLS_LMOTS_I_KEY_ID_LEN);
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 mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
196 status = psa_hash_update(&op, r_node_idx_bytes, 4);
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, D_INTR_CONSTANT_BYTES, D_CONST_LEN);
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, left_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_update(&op, right_node,
213 MBEDTLS_LMS_M_NODE_BYTES(params->type));
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
219 &output_hash_len);
220 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100221 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100223
Raef Coles01c71a12022-08-31 15:55:00 +0100224exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100226
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500227 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100228}
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 memset(ctx, 0, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100233}
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100236{
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100238}
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx,
241 const unsigned char *key, size_t key_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100242{
Raef Coles01c71a12022-08-31 15:55:00 +0100243 mbedtls_lms_algorithm_type_t type;
244 mbedtls_lmots_algorithm_type_t otstype;
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 type = mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
247 key + PUBLIC_KEY_TYPE_OFFSET);
248 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
249 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100250 }
Raef Colesf5632d32022-09-01 09:56:52 +0100251 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
254 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese89488d2022-10-07 16:06:35 +0100255 }
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 otstype = mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
258 key + PUBLIC_KEY_OTSTYPE_OFFSET);
259 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
260 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100261 }
Raef Colesf5632d32022-09-01 09:56:52 +0100262 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 memcpy(ctx->params.I_key_identifier,
265 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
266 MBEDTLS_LMOTS_I_KEY_ID_LEN);
267 memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
268 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100269
Raef Colesf5632d32022-09-01 09:56:52 +0100270 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100273}
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
276 unsigned char *key,
277 size_t key_size, size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100278{
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
280 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100281 }
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (!ctx->have_public_key) {
284 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100285 }
286
287 mbedtls_lms_unsigned_int_to_network_bytes(
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 ctx->params.type,
289 MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET);
290 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.otstype,
291 MBEDTLS_LMOTS_TYPE_LEN,
292 key + PUBLIC_KEY_OTSTYPE_OFFSET);
293 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
294 ctx->params.I_key_identifier,
295 MBEDTLS_LMOTS_I_KEY_ID_LEN);
296 memcpy(key +PUBLIC_KEY_ROOT_NODE_OFFSET,
297 ctx->T_1_pub_key,
298 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100301 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
302 }
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100305}
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx,
308 const unsigned char *msg, size_t msg_size,
309 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100310{
311 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100312 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
313 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100314 unsigned int height;
315 unsigned int curr_node_id;
316 unsigned int parent_node_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 const unsigned char *left_node;
318 const unsigned char *right_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100319 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (!ctx->have_public_key) {
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 (ctx->params.type
327 != MBEDTLS_LMS_SHA256_M32_H10) {
328 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100329 }
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if (ctx->params.otstype
332 != MBEDTLS_LMOTS_SHA256_N32_W8) {
333 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100334 }
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
337 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100338 }
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
341 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100342 }
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
345 sig + SIG_OTS_SIG_OFFSET +
346 MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
347 != MBEDTLS_LMOTS_SHA256_N32_W8) {
348 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100349 }
350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
352 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100353 }
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
356 sig + SIG_TYPE_OFFSET(ctx->params.otstype))
357 != MBEDTLS_LMS_SHA256_M32_H10) {
358 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100359 }
360
361
Raef Colesad054252022-09-27 10:59:16 +0100362 q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
366 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100367 }
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 memcpy(ots_params.I_key_identifier,
370 ctx->params.I_key_identifier,
371 MBEDTLS_LMOTS_I_KEY_ID_LEN);
372 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
Raef Colesad054252022-09-27 10:59:16 +0100373 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 ots_params.q_leaf_identifier);
Raef Colesf5632d32022-09-01 09:56:52 +0100375 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
378 msg,
379 msg_size,
380 sig + SIG_OTS_SIG_OFFSET,
381 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
382 Kc_candidate_ots_pub_key,
383 sizeof(Kc_candidate_ots_pub_key),
384 NULL);
385 if (ret != 0) {
386 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100387 }
388
Raef Colesebd35b52022-09-01 11:52:17 +0100389 create_merkle_leaf_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 &ctx->params,
391 Kc_candidate_ots_pub_key,
392 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
393 Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100394
Raef Coles366d67d2022-09-01 17:23:12 +0100395 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
396 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
399 height++) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100400 parent_node_id = curr_node_id / 2;
401
402 /* Left/right node ordering matters for the hash */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (curr_node_id & 1) {
Raef Coles57d53282022-09-27 11:30:51 +0100404 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100405 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100406 right_node = Tc_candidate_root_node;
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 } else {
Raef Coles8ff6df52021-07-21 12:42:15 +0100408 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100409 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100410 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100411 }
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 create_merkle_internal_value(&ctx->params, left_node, right_node,
414 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100415
416 curr_node_id /= 2;
417 }
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
420 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
421 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100422 }
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100425}
426
Raef Coles5127e852022-10-07 10:35:56 +0100427#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100428
Raef Coles285d44b2022-10-10 15:44:17 +0100429/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100430 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100431 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100432 *
Raef Coles98d6e222022-09-23 09:04:04 +0100433 * ctx The LMS private context, containing a parameter
434 * set and private key material consisting of both
435 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100436 *
Raef Coles98d6e222022-09-23 09:04:04 +0100437 * tree The output tree, which is 2^(H + 1) hash outputs.
438 * In the case of H=10 we have 2048 tree nodes (of
439 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100440 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100441 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
444 unsigned char *tree)
Raef Colesab4f8742022-09-01 12:24:31 +0100445{
446 unsigned int priv_key_idx;
447 unsigned int r_node_idx;
448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
449
450 /* First create the leaf nodes, in ascending order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 for (priv_key_idx = 0;
Raef Colese9479a02022-09-01 16:06:35 +0100452 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 priv_key_idx++) {
Raef Colese9479a02022-09-01 16:06:35 +0100454 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 ret = create_merkle_leaf_value(&ctx->params,
457 ctx->ots_public_keys[priv_key_idx].public_key,
458 r_node_idx,
459 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(
460 ctx->params.type)]);
461 if (ret != 0) {
462 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100463 }
464 }
465
466 /* Then the internal nodes, in reverse order so that we can guarantee the
467 * parent has been created */
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
Raef Colese9479a02022-09-01 16:06:35 +0100469 r_node_idx > 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 r_node_idx--) {
471 ret = create_merkle_internal_value(&ctx->params,
472 &tree[(r_node_idx * 2) *
473 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
474 &tree[(r_node_idx * 2 + 1) *
475 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
476 r_node_idx,
477 &tree[r_node_idx *
478 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]);
479 if (ret != 0) {
480 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100481 }
482 }
483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100485}
486
Raef Coles285d44b2022-10-10 15:44:17 +0100487/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100488 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100489 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100490 *
Raef Coles98d6e222022-09-23 09:04:04 +0100491 * ctx The LMS private context, containing a parameter
492 * set and private key material consisting of both
493 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100494 *
Raef Coles98d6e222022-09-23 09:04:04 +0100495 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100496 *
Raef Coles75b4c772022-10-10 13:58:28 +0100497 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100498 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100499static int get_merkle_path(mbedtls_lms_private_t *ctx,
500 unsigned int leaf_node_id,
501 unsigned char *path)
Raef Colesab4f8742022-09-01 12:24:31 +0100502{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800503 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100504 unsigned int curr_node_id = leaf_node_id;
505 unsigned int adjacent_node_id;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800506 unsigned char *tree = NULL;
Raef Colesab4f8742022-09-01 12:24:31 +0100507 unsigned int height;
508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(ctx->params.type),
511 node_bytes);
512 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800513 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
514 }
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 ret = calculate_merkle_tree(ctx, tree);
517 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100518 goto exit;
Raef Colesab4f8742022-09-01 12:24:31 +0100519 }
520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
522 height++) {
Raef Colesab4f8742022-09-01 12:24:31 +0100523 adjacent_node_id = curr_node_id ^ 1;
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 memcpy(&path[height * node_bytes],
526 &tree[adjacent_node_id * node_bytes], node_bytes);
Raef Colesab4f8742022-09-01 12:24:31 +0100527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 curr_node_id >>= 1;
Raef Colesab4f8742022-09-01 12:24:31 +0100529 }
530
Raef Coles142e5772022-10-12 10:47:27 +0100531 ret = 0;
532
533exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 mbedtls_platform_zeroize(tree, node_bytes *
535 MERKLE_TREE_NODE_AM(ctx->params.type));
536 mbedtls_free(tree);
Raef Coles142e5772022-10-12 10:47:27 +0100537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100539}
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100542{
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100544}
Raef Coles8ff6df52021-07-21 12:42:15 +0100545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100547{
548 unsigned int idx;
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (ctx->have_private_key) {
551 if (ctx->ots_private_keys != NULL) {
552 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
553 mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100554 }
Raef Coles01c71a12022-08-31 15:55:00 +0100555 }
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (ctx->ots_public_keys != NULL) {
558 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
559 mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100560 }
561 }
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 mbedtls_free(ctx->ots_private_keys);
564 mbedtls_free(ctx->ots_public_keys);
Raef Coles8ff6df52021-07-21 12:42:15 +0100565 }
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100568}
Raef Coles8ff6df52021-07-21 12:42:15 +0100569
Raef Coles01c71a12022-08-31 15:55:00 +0100570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
572 mbedtls_lms_algorithm_type_t type,
573 mbedtls_lmots_algorithm_type_t otstype,
574 int (*f_rng)(void *, unsigned char *, size_t),
575 void *p_rng, const unsigned char *seed,
576 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100577{
578 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
582 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100583 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
586 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100587 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (ctx->have_private_key) {
590 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100591 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100592
Raef Colesf5632d32022-09-01 09:56:52 +0100593 ctx->params.type = type;
594 ctx->params.otstype = otstype;
Raef Colescbd02ad2022-10-13 14:11:49 +0100595 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 ret = f_rng(p_rng,
598 ctx->params.I_key_identifier,
599 MBEDTLS_LMOTS_I_KEY_ID_LEN);
600 if (ret != 0) {
Raef Coles3f6cdd72022-10-07 14:07:59 +0100601 goto exit;
602 }
Raef Coles01c71a12022-08-31 15:55:00 +0100603
Raef Coles45c4ff92022-10-12 15:22:48 +0100604 /* Requires a cast to size_t to avoid an implicit cast warning on certain
605 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
607 sizeof(*ctx->ots_private_keys));
608 if (ctx->ots_private_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100609 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
610 goto exit;
611 }
612
Raef Coles45c4ff92022-10-12 15:22:48 +0100613 /* Requires a cast to size_t to avoid an implicit cast warning on certain
614 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
616 sizeof(*ctx->ots_public_keys));
617 if (ctx->ots_public_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100618 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
619 goto exit;
620 }
621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
623 mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
624 mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]);
Raef Coles01c71a12022-08-31 15:55:00 +0100625 }
626
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
629 ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx],
630 otstype,
631 ctx->params.I_key_identifier,
632 idx, seed, seed_size);
633 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100634 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 }
Raef Coles01c71a12022-08-31 15:55:00 +0100636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
638 &ctx->ots_private_keys[idx]);
639 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100640 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 }
Raef Coles01c71a12022-08-31 15:55:00 +0100642 }
643
Raef Colesf5632d32022-09-01 09:56:52 +0100644 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100645
646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (ret != 0) {
Raef Colesdc8fb792022-10-07 13:27:54 +0100648 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100649 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100652}
653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
655 const mbedtls_lms_private_t *priv_ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100656{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800657 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800659 unsigned char *tree = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +0100660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (!priv_ctx->have_private_key) {
662 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100663 }
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (priv_ctx->params.type
666 != MBEDTLS_LMS_SHA256_M32_H10) {
667 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100668 }
669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (priv_ctx->params.otstype
671 != MBEDTLS_LMOTS_SHA256_N32_W8) {
672 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100673 }
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(priv_ctx->params.type),
676 node_bytes);
677 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800678 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
679 }
680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 memcpy(&ctx->params, &priv_ctx->params,
682 sizeof(mbedtls_lmots_parameters_t));
Raef Coles8ff6df52021-07-21 12:42:15 +0100683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 ret = calculate_merkle_tree(priv_ctx, tree);
685 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100686 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100687 }
688
689 /* Root node is always at position 1, due to 1-based indexing */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100691
Raef Colesf5632d32022-09-01 09:56:52 +0100692 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100693
Raef Coles142e5772022-10-12 10:47:27 +0100694 ret = 0;
695
696exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 mbedtls_platform_zeroize(tree, node_bytes *
698 MERKLE_TREE_NODE_AM(priv_ctx->params.type));
699 mbedtls_free(tree);
Raef Coles142e5772022-10-12 10:47:27 +0100700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100702}
703
Raef Coles01c71a12022-08-31 15:55:00 +0100704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
706 int (*f_rng)(void *, unsigned char *, size_t),
707 void *p_rng, const unsigned char *msg,
708 unsigned int msg_size, unsigned char *sig, size_t sig_size,
709 size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100710{
711 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (!ctx->have_private_key) {
715 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100716 }
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
719 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100720 }
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
723 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100724 }
725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (ctx->params.otstype
727 != MBEDTLS_LMOTS_SHA256_N32_W8) {
728 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100729 }
730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
732 return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
Raef Coles8ff6df52021-07-21 12:42:15 +0100733 }
734
735
Raef Colesf5632d32022-09-01 09:56:52 +0100736 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100737 /* This new value must _always_ be written back to the disk before the
738 * signature is returned.
739 */
Raef Colesf5632d32022-09-01 09:56:52 +0100740 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
743 < SIG_OTS_SIG_OFFSET) {
744 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles1fb2f322022-10-10 11:23:07 +0100745 }
746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
748 f_rng,
749 p_rng,
750 msg,
751 msg_size,
752 sig + SIG_OTS_SIG_OFFSET,
753 MBEDTLS_LMS_SIG_LEN(ctx->params.type,
754 ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
755 NULL);
756 if (ret != 0) {
757 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100758 }
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
761 MBEDTLS_LMS_TYPE_LEN,
762 sig + SIG_TYPE_OFFSET(ctx->params.otstype));
763 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
764 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
765 sig + SIG_Q_LEAF_ID_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 ret = get_merkle_path(ctx,
768 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
769 sig + SIG_PATH_OFFSET(ctx->params.otstype));
770 if (ret != 0) {
771 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100772 }
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100775 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100776 }
777
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100780}
781
Raef Coles5127e852022-10-07 10:35:56 +0100782#endif /* defined(MBEDTLS_LMS_PRIVATE) */
783#endif /* defined(MBEDTLS_LMS_C) */