blob: 50595703de7acb12f2335d8ae70d37e90a332a45 [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 Kurek00644842023-05-30 05:45:00 -040049/* Define a local translating function to save code size by not using too many
50 * arguments in each translating place. */
51static int local_err_translation(psa_status_t status)
52{
53 return psa_status_to_mbedtls(status, psa_to_lms_errors,
54 sizeof(psa_to_lms_errors),
55 psa_generic_status_to_mbedtls);
56}
57#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050058
Raef Coles57d53282022-09-27 11:30:51 +010059#define SIG_Q_LEAF_ID_OFFSET (0)
60#define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
61 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
62#define SIG_TYPE_OFFSET(otstype) (SIG_OTS_SIG_OFFSET + \
63 MBEDTLS_LMOTS_SIG_LEN(otstype))
64#define SIG_PATH_OFFSET(otstype) (SIG_TYPE_OFFSET(otstype) + \
65 MBEDTLS_LMS_TYPE_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010066
Raef Coles57d53282022-09-27 11:30:51 +010067#define PUBLIC_KEY_TYPE_OFFSET (0)
68#define PUBLIC_KEY_OTSTYPE_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
69 MBEDTLS_LMS_TYPE_LEN)
70#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_OTSTYPE_OFFSET + \
71 MBEDTLS_LMOTS_TYPE_LEN)
72#define PUBLIC_KEY_ROOT_NODE_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
73 MBEDTLS_LMOTS_I_KEY_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010074
75
Raef Colese9479a02022-09-01 16:06:35 +010076/* Currently only support H=10 */
Raef Coles57d53282022-09-27 11:30:51 +010077#define H_TREE_HEIGHT_MAX 10
Moritz Fischera6a94ad2022-11-12 08:28:04 -080078#define MERKLE_TREE_NODE_AM(type) ((size_t) 1 << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
79#define MERKLE_TREE_LEAF_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
80#define MERKLE_TREE_INTERNAL_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010081
82#define D_CONST_LEN (2)
Gilles Peskine449bd832023-01-11 14:50:10 +010083static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = { 0x82, 0x82 };
84static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = { 0x83, 0x83 };
Raef Coles8ff6df52021-07-21 12:42:15 +010085
Raef Coles0a967cc2022-09-02 17:46:15 +010086
Raef Coles285d44b2022-10-10 15:44:17 +010087/* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
Raef Coles0a967cc2022-09-02 17:46:15 +010088 * public key and some other parameters like the leaf index). This function
89 * implements RFC8554 section 5.3, in the case where r >= 2^h.
90 *
Raef Coles98d6e222022-09-23 09:04:04 +010091 * params The LMS parameter set, the underlying LMOTS
92 * parameter set, and I value which describe the key
93 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +010094 *
Raef Coles98d6e222022-09-23 09:04:04 +010095 * pub_key The public key of the private whose index
96 * corresponds to the index of this leaf node. This
97 * is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010098 *
Raef Coles285d44b2022-10-10 15:44:17 +010099 * r_node_idx The index of this node in the Merkle tree. Note
100 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100101 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100102 *
Raef Coles98d6e222022-09-23 09:04:04 +0100103 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105static int create_merkle_leaf_value(const mbedtls_lms_parameters_t *params,
106 unsigned char *pub_key,
107 unsigned int r_node_idx,
108 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100109{
Raef Colesc8f96042022-08-25 13:49:54 +0100110 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100111 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100112 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100113 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 op = psa_hash_operation_init();
116 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
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, params->I_key_identifier,
122 MBEDTLS_LMOTS_I_KEY_ID_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 mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
128 status = psa_hash_update(&op, r_node_idx_bytes, 4);
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_update(&op, D_LEAF_CONSTANT_BYTES, D_CONST_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
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 status = psa_hash_update(&op, pub_key,
139 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype));
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
145 &output_hash_len);
146 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100149
Raef Coles01c71a12022-08-31 15:55:00 +0100150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100152
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500153 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100154}
155
Raef Coles285d44b2022-10-10 15:44:17 +0100156/* Calculate the value of an internal node of the Merkle tree (which is a hash
Raef Coles0a967cc2022-09-02 17:46:15 +0100157 * of a public key and some other parameters like the node index). This function
158 * implements RFC8554 section 5.3, in the case where r < 2^h.
159 *
Raef Coles98d6e222022-09-23 09:04:04 +0100160 * params The LMS parameter set, the underlying LMOTS
161 * parameter set, and I value which describe the key
162 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100163 *
Raef Coles98d6e222022-09-23 09:04:04 +0100164 * left_node The value of the child of this node which is on
165 * the left-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100166 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100167 *
Raef Coles98d6e222022-09-23 09:04:04 +0100168 * right_node The value of the child of this node which is on
169 * the right-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100170 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100171 *
Raef Coles285d44b2022-10-10 15:44:17 +0100172 * r_node_idx The index of this node in the Merkle tree. Note
173 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100174 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100175 *
Raef Coles98d6e222022-09-23 09:04:04 +0100176 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100177 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178static int create_merkle_internal_value(const mbedtls_lms_parameters_t *params,
179 const unsigned char *left_node,
180 const unsigned char *right_node,
181 unsigned int r_node_idx,
182 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100183{
Raef Colesc8f96042022-08-25 13:49:54 +0100184 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100185 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100186 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100187 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 op = psa_hash_operation_init();
190 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
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, params->I_key_identifier,
196 MBEDTLS_LMOTS_I_KEY_ID_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 mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
202 status = psa_hash_update(&op, r_node_idx_bytes, 4);
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, D_INTR_CONSTANT_BYTES, D_CONST_LEN);
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, left_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_update(&op, right_node,
219 MBEDTLS_LMS_M_NODE_BYTES(params->type));
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
225 &output_hash_len);
226 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100229
Raef Coles01c71a12022-08-31 15:55:00 +0100230exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100232
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500233 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100234}
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100237{
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 memset(ctx, 0, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100239}
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100242{
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100244}
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx,
247 const unsigned char *key, size_t key_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100248{
Raef Coles01c71a12022-08-31 15:55:00 +0100249 mbedtls_lms_algorithm_type_t type;
250 mbedtls_lmots_algorithm_type_t otstype;
251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 type = mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
253 key + PUBLIC_KEY_TYPE_OFFSET);
254 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
255 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100256 }
Raef Colesf5632d32022-09-01 09:56:52 +0100257 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
260 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese89488d2022-10-07 16:06:35 +0100261 }
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 otstype = mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
264 key + PUBLIC_KEY_OTSTYPE_OFFSET);
265 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
266 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100267 }
Raef Colesf5632d32022-09-01 09:56:52 +0100268 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 memcpy(ctx->params.I_key_identifier,
271 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
272 MBEDTLS_LMOTS_I_KEY_ID_LEN);
273 memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
274 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100275
Raef Colesf5632d32022-09-01 09:56:52 +0100276 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100279}
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
282 unsigned char *key,
283 size_t key_size, size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100284{
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
286 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100287 }
288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (!ctx->have_public_key) {
290 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100291 }
292
293 mbedtls_lms_unsigned_int_to_network_bytes(
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 ctx->params.type,
295 MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET);
296 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.otstype,
297 MBEDTLS_LMOTS_TYPE_LEN,
298 key + PUBLIC_KEY_OTSTYPE_OFFSET);
299 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
300 ctx->params.I_key_identifier,
301 MBEDTLS_LMOTS_I_KEY_ID_LEN);
302 memcpy(key +PUBLIC_KEY_ROOT_NODE_OFFSET,
303 ctx->T_1_pub_key,
304 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100307 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
308 }
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100311}
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx,
314 const unsigned char *msg, size_t msg_size,
315 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100316{
317 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100318 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
319 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100320 unsigned int height;
321 unsigned int curr_node_id;
322 unsigned int parent_node_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 const unsigned char *left_node;
324 const unsigned char *right_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100325 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (!ctx->have_public_key) {
329 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100330 }
331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (ctx->params.type
333 != MBEDTLS_LMS_SHA256_M32_H10) {
334 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100335 }
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (ctx->params.otstype
338 != MBEDTLS_LMOTS_SHA256_N32_W8) {
339 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100340 }
341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
343 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100344 }
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
347 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100348 }
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
351 sig + SIG_OTS_SIG_OFFSET +
352 MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
353 != MBEDTLS_LMOTS_SHA256_N32_W8) {
354 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100355 }
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
358 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100359 }
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
362 sig + SIG_TYPE_OFFSET(ctx->params.otstype))
363 != MBEDTLS_LMS_SHA256_M32_H10) {
364 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100365 }
366
367
Raef Colesad054252022-09-27 10:59:16 +0100368 q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
372 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100373 }
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 memcpy(ots_params.I_key_identifier,
376 ctx->params.I_key_identifier,
377 MBEDTLS_LMOTS_I_KEY_ID_LEN);
378 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
Raef Colesad054252022-09-27 10:59:16 +0100379 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 ots_params.q_leaf_identifier);
Raef Colesf5632d32022-09-01 09:56:52 +0100381 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
384 msg,
385 msg_size,
386 sig + SIG_OTS_SIG_OFFSET,
387 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
388 Kc_candidate_ots_pub_key,
389 sizeof(Kc_candidate_ots_pub_key),
390 NULL);
391 if (ret != 0) {
392 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100393 }
394
Raef Colesebd35b52022-09-01 11:52:17 +0100395 create_merkle_leaf_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 &ctx->params,
397 Kc_candidate_ots_pub_key,
398 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
399 Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100400
Raef Coles366d67d2022-09-01 17:23:12 +0100401 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
402 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
405 height++) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100406 parent_node_id = curr_node_id / 2;
407
408 /* Left/right node ordering matters for the hash */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (curr_node_id & 1) {
Raef Coles57d53282022-09-27 11:30:51 +0100410 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100411 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100412 right_node = Tc_candidate_root_node;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 } else {
Raef Coles8ff6df52021-07-21 12:42:15 +0100414 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100415 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100416 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100417 }
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 create_merkle_internal_value(&ctx->params, left_node, right_node,
420 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100421
422 curr_node_id /= 2;
423 }
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
426 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
427 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100428 }
429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100431}
432
Raef Coles5127e852022-10-07 10:35:56 +0100433#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100434
Raef Coles285d44b2022-10-10 15:44:17 +0100435/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100436 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100437 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100438 *
Raef Coles98d6e222022-09-23 09:04:04 +0100439 * ctx The LMS private context, containing a parameter
440 * set and private key material consisting of both
441 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100442 *
Raef Coles98d6e222022-09-23 09:04:04 +0100443 * tree The output tree, which is 2^(H + 1) hash outputs.
444 * In the case of H=10 we have 2048 tree nodes (of
445 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100446 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100447 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100448 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100449static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
450 unsigned char *tree)
Raef Colesab4f8742022-09-01 12:24:31 +0100451{
452 unsigned int priv_key_idx;
453 unsigned int r_node_idx;
454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
455
456 /* First create the leaf nodes, in ascending order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 for (priv_key_idx = 0;
Raef Colese9479a02022-09-01 16:06:35 +0100458 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 priv_key_idx++) {
Raef Colese9479a02022-09-01 16:06:35 +0100460 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 ret = create_merkle_leaf_value(&ctx->params,
463 ctx->ots_public_keys[priv_key_idx].public_key,
464 r_node_idx,
465 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(
466 ctx->params.type)]);
467 if (ret != 0) {
468 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100469 }
470 }
471
472 /* Then the internal nodes, in reverse order so that we can guarantee the
473 * parent has been created */
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
Raef Colese9479a02022-09-01 16:06:35 +0100475 r_node_idx > 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 r_node_idx--) {
477 ret = create_merkle_internal_value(&ctx->params,
478 &tree[(r_node_idx * 2) *
479 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
480 &tree[(r_node_idx * 2 + 1) *
481 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
482 r_node_idx,
483 &tree[r_node_idx *
484 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]);
485 if (ret != 0) {
486 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100487 }
488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100491}
492
Raef Coles285d44b2022-10-10 15:44:17 +0100493/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100494 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100495 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100496 *
Raef Coles98d6e222022-09-23 09:04:04 +0100497 * ctx The LMS private context, containing a parameter
498 * set and private key material consisting of both
499 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100500 *
Raef Coles98d6e222022-09-23 09:04:04 +0100501 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100502 *
Raef Coles75b4c772022-10-10 13:58:28 +0100503 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100504 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505static int get_merkle_path(mbedtls_lms_private_t *ctx,
506 unsigned int leaf_node_id,
507 unsigned char *path)
Raef Colesab4f8742022-09-01 12:24:31 +0100508{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800509 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100510 unsigned int curr_node_id = leaf_node_id;
511 unsigned int adjacent_node_id;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800512 unsigned char *tree = NULL;
Raef Colesab4f8742022-09-01 12:24:31 +0100513 unsigned int height;
514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(ctx->params.type),
517 node_bytes);
518 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800519 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 ret = calculate_merkle_tree(ctx, tree);
523 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100524 goto exit;
Raef Colesab4f8742022-09-01 12:24:31 +0100525 }
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
528 height++) {
Raef Colesab4f8742022-09-01 12:24:31 +0100529 adjacent_node_id = curr_node_id ^ 1;
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 memcpy(&path[height * node_bytes],
532 &tree[adjacent_node_id * node_bytes], node_bytes);
Raef Colesab4f8742022-09-01 12:24:31 +0100533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 curr_node_id >>= 1;
Raef Colesab4f8742022-09-01 12:24:31 +0100535 }
536
Raef Coles142e5772022-10-12 10:47:27 +0100537 ret = 0;
538
539exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_platform_zeroize(tree, node_bytes *
541 MERKLE_TREE_NODE_AM(ctx->params.type));
542 mbedtls_free(tree);
Raef Coles142e5772022-10-12 10:47:27 +0100543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100545}
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100548{
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100550}
Raef Coles8ff6df52021-07-21 12:42:15 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100553{
554 unsigned int idx;
555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (ctx->have_private_key) {
557 if (ctx->ots_private_keys != NULL) {
558 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
559 mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100560 }
Raef Coles01c71a12022-08-31 15:55:00 +0100561 }
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (ctx->ots_public_keys != NULL) {
564 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
565 mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100566 }
567 }
568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 mbedtls_free(ctx->ots_private_keys);
570 mbedtls_free(ctx->ots_public_keys);
Raef Coles8ff6df52021-07-21 12:42:15 +0100571 }
572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100574}
Raef Coles8ff6df52021-07-21 12:42:15 +0100575
Raef Coles01c71a12022-08-31 15:55:00 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
578 mbedtls_lms_algorithm_type_t type,
579 mbedtls_lmots_algorithm_type_t otstype,
580 int (*f_rng)(void *, unsigned char *, size_t),
581 void *p_rng, const unsigned char *seed,
582 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100583{
584 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
588 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100589 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
592 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100593 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (ctx->have_private_key) {
596 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100597 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100598
Raef Colesf5632d32022-09-01 09:56:52 +0100599 ctx->params.type = type;
600 ctx->params.otstype = otstype;
Raef Colescbd02ad2022-10-13 14:11:49 +0100601 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 ret = f_rng(p_rng,
604 ctx->params.I_key_identifier,
605 MBEDTLS_LMOTS_I_KEY_ID_LEN);
606 if (ret != 0) {
Raef Coles3f6cdd72022-10-07 14:07:59 +0100607 goto exit;
608 }
Raef Coles01c71a12022-08-31 15:55:00 +0100609
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_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
613 sizeof(*ctx->ots_private_keys));
614 if (ctx->ots_private_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100615 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
616 goto exit;
617 }
618
Raef Coles45c4ff92022-10-12 15:22:48 +0100619 /* Requires a cast to size_t to avoid an implicit cast warning on certain
620 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
622 sizeof(*ctx->ots_public_keys));
623 if (ctx->ots_public_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100624 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
625 goto exit;
626 }
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
629 mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
630 mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]);
Raef Coles01c71a12022-08-31 15:55:00 +0100631 }
632
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
635 ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx],
636 otstype,
637 ctx->params.I_key_identifier,
638 idx, seed, seed_size);
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
644 &ctx->ots_private_keys[idx]);
645 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100646 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 }
Raef Coles01c71a12022-08-31 15:55:00 +0100648 }
649
Raef Colesf5632d32022-09-01 09:56:52 +0100650 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100651
652exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (ret != 0) {
Raef Colesdc8fb792022-10-07 13:27:54 +0100654 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100655 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100658}
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
661 const mbedtls_lms_private_t *priv_ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100662{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800663 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800665 unsigned char *tree = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +0100666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (!priv_ctx->have_private_key) {
668 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100669 }
670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (priv_ctx->params.type
672 != MBEDTLS_LMS_SHA256_M32_H10) {
673 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100674 }
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (priv_ctx->params.otstype
677 != MBEDTLS_LMOTS_SHA256_N32_W8) {
678 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100679 }
680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(priv_ctx->params.type),
682 node_bytes);
683 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800684 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
685 }
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 memcpy(&ctx->params, &priv_ctx->params,
688 sizeof(mbedtls_lmots_parameters_t));
Raef Coles8ff6df52021-07-21 12:42:15 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 ret = calculate_merkle_tree(priv_ctx, tree);
691 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100692 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100693 }
694
695 /* Root node is always at position 1, due to 1-based indexing */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100697
Raef Colesf5632d32022-09-01 09:56:52 +0100698 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100699
Raef Coles142e5772022-10-12 10:47:27 +0100700 ret = 0;
701
702exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 mbedtls_platform_zeroize(tree, node_bytes *
704 MERKLE_TREE_NODE_AM(priv_ctx->params.type));
705 mbedtls_free(tree);
Raef Coles142e5772022-10-12 10:47:27 +0100706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100708}
709
Raef Coles01c71a12022-08-31 15:55:00 +0100710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
712 int (*f_rng)(void *, unsigned char *, size_t),
713 void *p_rng, const unsigned char *msg,
714 unsigned int msg_size, unsigned char *sig, size_t sig_size,
715 size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100716{
717 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (!ctx->have_private_key) {
721 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100722 }
723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
725 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100726 }
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
729 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100730 }
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (ctx->params.otstype
733 != MBEDTLS_LMOTS_SHA256_N32_W8) {
734 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100735 }
736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
738 return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
Raef Coles8ff6df52021-07-21 12:42:15 +0100739 }
740
741
Raef Colesf5632d32022-09-01 09:56:52 +0100742 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100743 /* This new value must _always_ be written back to the disk before the
744 * signature is returned.
745 */
Raef Colesf5632d32022-09-01 09:56:52 +0100746 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
749 < SIG_OTS_SIG_OFFSET) {
750 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles1fb2f322022-10-10 11:23:07 +0100751 }
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
754 f_rng,
755 p_rng,
756 msg,
757 msg_size,
758 sig + SIG_OTS_SIG_OFFSET,
759 MBEDTLS_LMS_SIG_LEN(ctx->params.type,
760 ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
761 NULL);
762 if (ret != 0) {
763 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100764 }
765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
767 MBEDTLS_LMS_TYPE_LEN,
768 sig + SIG_TYPE_OFFSET(ctx->params.otstype));
769 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
770 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
771 sig + SIG_Q_LEAF_ID_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 ret = get_merkle_path(ctx,
774 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
775 sig + SIG_PATH_OFFSET(ctx->params.otstype));
776 if (ret != 0) {
777 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100778 }
779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100781 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100782 }
783
784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100786}
787
Raef Coles5127e852022-10-07 10:35:56 +0100788#endif /* defined(MBEDTLS_LMS_PRIVATE) */
789#endif /* defined(MBEDTLS_LMS_C) */