blob: c06f9c26055eabb5ca09b9d155c8efdabc02852f [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"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020042#include "psa_util_internal.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,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040054 ARRAY_LENGTH(psa_to_lms_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040055 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
Agathiyan Bragadeesh10b67752023-07-12 11:19:17 +0100252 type = (mbedtls_lms_algorithm_type_t) mbedtls_lms_network_bytes_to_unsigned_int(
253 MBEDTLS_LMS_TYPE_LEN,
254 key +
255 PUBLIC_KEY_TYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
257 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100258 }
Raef Colesf5632d32022-09-01 09:56:52 +0100259 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
262 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese89488d2022-10-07 16:06:35 +0100263 }
264
Agathiyan Bragadeesh10b67752023-07-12 11:19:17 +0100265 otstype = (mbedtls_lmots_algorithm_type_t) mbedtls_lms_network_bytes_to_unsigned_int(
266 MBEDTLS_LMOTS_TYPE_LEN,
267 key +
268 PUBLIC_KEY_OTSTYPE_OFFSET);
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
270 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100271 }
Raef Colesf5632d32022-09-01 09:56:52 +0100272 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 memcpy(ctx->params.I_key_identifier,
275 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
276 MBEDTLS_LMOTS_I_KEY_ID_LEN);
277 memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
278 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100279
Raef Colesf5632d32022-09-01 09:56:52 +0100280 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100283}
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
286 unsigned char *key,
287 size_t key_size, size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100288{
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
290 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100291 }
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (!ctx->have_public_key) {
294 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100295 }
296
297 mbedtls_lms_unsigned_int_to_network_bytes(
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 ctx->params.type,
299 MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET);
300 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.otstype,
301 MBEDTLS_LMOTS_TYPE_LEN,
302 key + PUBLIC_KEY_OTSTYPE_OFFSET);
303 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
304 ctx->params.I_key_identifier,
305 MBEDTLS_LMOTS_I_KEY_ID_LEN);
306 memcpy(key +PUBLIC_KEY_ROOT_NODE_OFFSET,
307 ctx->T_1_pub_key,
308 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100311 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
312 }
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100315}
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx,
318 const unsigned char *msg, size_t msg_size,
319 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100320{
321 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100322 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
323 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100324 unsigned int height;
325 unsigned int curr_node_id;
326 unsigned int parent_node_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 const unsigned char *left_node;
328 const unsigned char *right_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100329 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (!ctx->have_public_key) {
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 (ctx->params.type
337 != MBEDTLS_LMS_SHA256_M32_H10) {
338 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100339 }
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (ctx->params.otstype
342 != MBEDTLS_LMOTS_SHA256_N32_W8) {
343 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100344 }
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
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 (sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
351 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100352 }
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
355 sig + SIG_OTS_SIG_OFFSET +
356 MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
357 != MBEDTLS_LMOTS_SHA256_N32_W8) {
358 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100359 }
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
362 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Colesfaf59ba2022-10-10 15:40:56 +0100363 }
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
366 sig + SIG_TYPE_OFFSET(ctx->params.otstype))
367 != MBEDTLS_LMS_SHA256_M32_H10) {
368 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100369 }
370
371
Raef Colesad054252022-09-27 10:59:16 +0100372 q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
376 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100377 }
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 memcpy(ots_params.I_key_identifier,
380 ctx->params.I_key_identifier,
381 MBEDTLS_LMOTS_I_KEY_ID_LEN);
382 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
Raef Colesad054252022-09-27 10:59:16 +0100383 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 ots_params.q_leaf_identifier);
Raef Colesf5632d32022-09-01 09:56:52 +0100385 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
388 msg,
389 msg_size,
390 sig + SIG_OTS_SIG_OFFSET,
391 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
392 Kc_candidate_ots_pub_key,
393 sizeof(Kc_candidate_ots_pub_key),
394 NULL);
395 if (ret != 0) {
396 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100397 }
398
Raef Colesebd35b52022-09-01 11:52:17 +0100399 create_merkle_leaf_value(
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 &ctx->params,
401 Kc_candidate_ots_pub_key,
402 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
403 Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100404
Raef Coles366d67d2022-09-01 17:23:12 +0100405 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
406 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
409 height++) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100410 parent_node_id = curr_node_id / 2;
411
412 /* Left/right node ordering matters for the hash */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (curr_node_id & 1) {
Raef Coles57d53282022-09-27 11:30:51 +0100414 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100415 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100416 right_node = Tc_candidate_root_node;
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 } else {
Raef Coles8ff6df52021-07-21 12:42:15 +0100418 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100419 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100420 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100421 }
422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 create_merkle_internal_value(&ctx->params, left_node, right_node,
424 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100425
426 curr_node_id /= 2;
427 }
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
430 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
431 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles8ff6df52021-07-21 12:42:15 +0100432 }
433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100435}
436
Raef Coles5127e852022-10-07 10:35:56 +0100437#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100438
Raef Coles285d44b2022-10-10 15:44:17 +0100439/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100440 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100441 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100442 *
Raef Coles98d6e222022-09-23 09:04:04 +0100443 * ctx The LMS private context, containing a parameter
444 * set and private key material consisting of both
445 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100446 *
Raef Coles98d6e222022-09-23 09:04:04 +0100447 * tree The output tree, which is 2^(H + 1) hash outputs.
448 * In the case of H=10 we have 2048 tree nodes (of
449 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100450 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100451 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100452 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100453static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
454 unsigned char *tree)
Raef Colesab4f8742022-09-01 12:24:31 +0100455{
456 unsigned int priv_key_idx;
457 unsigned int r_node_idx;
458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
459
460 /* First create the leaf nodes, in ascending order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 for (priv_key_idx = 0;
Raef Colese9479a02022-09-01 16:06:35 +0100462 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 priv_key_idx++) {
Raef Colese9479a02022-09-01 16:06:35 +0100464 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 ret = create_merkle_leaf_value(&ctx->params,
467 ctx->ots_public_keys[priv_key_idx].public_key,
468 r_node_idx,
469 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(
470 ctx->params.type)]);
471 if (ret != 0) {
472 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100473 }
474 }
475
476 /* Then the internal nodes, in reverse order so that we can guarantee the
477 * parent has been created */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
Raef Colese9479a02022-09-01 16:06:35 +0100479 r_node_idx > 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 r_node_idx--) {
481 ret = create_merkle_internal_value(&ctx->params,
482 &tree[(r_node_idx * 2) *
483 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
484 &tree[(r_node_idx * 2 + 1) *
485 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
486 r_node_idx,
487 &tree[r_node_idx *
488 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]);
489 if (ret != 0) {
490 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100491 }
492 }
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 return 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100495}
496
Raef Coles285d44b2022-10-10 15:44:17 +0100497/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100498 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100499 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100500 *
Raef Coles98d6e222022-09-23 09:04:04 +0100501 * ctx The LMS private context, containing a parameter
502 * set and private key material consisting of both
503 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100504 *
Raef Coles98d6e222022-09-23 09:04:04 +0100505 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100506 *
Raef Coles75b4c772022-10-10 13:58:28 +0100507 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100508 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509static int get_merkle_path(mbedtls_lms_private_t *ctx,
510 unsigned int leaf_node_id,
511 unsigned char *path)
Raef Colesab4f8742022-09-01 12:24:31 +0100512{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800513 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100514 unsigned int curr_node_id = leaf_node_id;
515 unsigned int adjacent_node_id;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800516 unsigned char *tree = NULL;
Raef Colesab4f8742022-09-01 12:24:31 +0100517 unsigned int height;
518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(ctx->params.type),
521 node_bytes);
522 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800523 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
524 }
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 ret = calculate_merkle_tree(ctx, tree);
527 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100528 goto exit;
Raef Colesab4f8742022-09-01 12:24:31 +0100529 }
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
532 height++) {
Raef Colesab4f8742022-09-01 12:24:31 +0100533 adjacent_node_id = curr_node_id ^ 1;
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 memcpy(&path[height * node_bytes],
536 &tree[adjacent_node_id * node_bytes], node_bytes);
Raef Colesab4f8742022-09-01 12:24:31 +0100537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 curr_node_id >>= 1;
Raef Colesab4f8742022-09-01 12:24:31 +0100539 }
540
Raef Coles142e5772022-10-12 10:47:27 +0100541 ret = 0;
542
543exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100544 mbedtls_zeroize_and_free(tree, node_bytes *
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 MERKLE_TREE_NODE_AM(ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return ret;
Raef Colesab4f8742022-09-01 12:24:31 +0100548}
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100551{
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100553}
Raef Coles8ff6df52021-07-21 12:42:15 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100556{
557 unsigned int idx;
558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (ctx->have_private_key) {
560 if (ctx->ots_private_keys != NULL) {
561 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
562 mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100563 }
Raef Coles01c71a12022-08-31 15:55:00 +0100564 }
565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (ctx->ots_public_keys != NULL) {
567 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
568 mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
Raef Colescbd02ad2022-10-13 14:11:49 +0100569 }
570 }
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 mbedtls_free(ctx->ots_private_keys);
573 mbedtls_free(ctx->ots_public_keys);
Raef Coles8ff6df52021-07-21 12:42:15 +0100574 }
575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100577}
Raef Coles8ff6df52021-07-21 12:42:15 +0100578
Raef Coles01c71a12022-08-31 15:55:00 +0100579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
581 mbedtls_lms_algorithm_type_t type,
582 mbedtls_lmots_algorithm_type_t otstype,
583 int (*f_rng)(void *, unsigned char *, size_t),
584 void *p_rng, const unsigned char *seed,
585 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100586{
587 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (type != MBEDTLS_LMS_SHA256_M32_H10) {
591 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100592 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
595 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100596 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if (ctx->have_private_key) {
599 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100600 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100601
Raef Colesf5632d32022-09-01 09:56:52 +0100602 ctx->params.type = type;
603 ctx->params.otstype = otstype;
Raef Colescbd02ad2022-10-13 14:11:49 +0100604 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 ret = f_rng(p_rng,
607 ctx->params.I_key_identifier,
608 MBEDTLS_LMOTS_I_KEY_ID_LEN);
609 if (ret != 0) {
Raef Coles3f6cdd72022-10-07 14:07:59 +0100610 goto exit;
611 }
Raef Coles01c71a12022-08-31 15:55:00 +0100612
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_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
616 sizeof(*ctx->ots_private_keys));
617 if (ctx->ots_private_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100618 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
619 goto exit;
620 }
621
Raef Coles45c4ff92022-10-12 15:22:48 +0100622 /* Requires a cast to size_t to avoid an implicit cast warning on certain
623 * platforms (particularly Windows) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
625 sizeof(*ctx->ots_public_keys));
626 if (ctx->ots_public_keys == NULL) {
Raef Coles01c71a12022-08-31 15:55:00 +0100627 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
628 goto exit;
629 }
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
632 mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
633 mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]);
Raef Coles01c71a12022-08-31 15:55:00 +0100634 }
635
636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
638 ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx],
639 otstype,
640 ctx->params.I_key_identifier,
641 idx, seed, seed_size);
642 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 }
Raef Coles01c71a12022-08-31 15:55:00 +0100645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
647 &ctx->ots_private_keys[idx]);
648 if (ret != 0) {
Raef Coles01c71a12022-08-31 15:55:00 +0100649 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 }
Raef Coles01c71a12022-08-31 15:55:00 +0100651 }
652
Raef Colesf5632d32022-09-01 09:56:52 +0100653 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100654
655exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (ret != 0) {
Raef Colesdc8fb792022-10-07 13:27:54 +0100657 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100658 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100661}
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
664 const mbedtls_lms_private_t *priv_ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100665{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800666 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800668 unsigned char *tree = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +0100669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (!priv_ctx->have_private_key) {
671 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100672 }
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (priv_ctx->params.type
675 != MBEDTLS_LMS_SHA256_M32_H10) {
676 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (priv_ctx->params.otstype
680 != MBEDTLS_LMOTS_SHA256_N32_W8) {
681 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100682 }
683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(priv_ctx->params.type),
685 node_bytes);
686 if (tree == NULL) {
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800687 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
688 }
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 memcpy(&ctx->params, &priv_ctx->params,
691 sizeof(mbedtls_lmots_parameters_t));
Raef Coles8ff6df52021-07-21 12:42:15 +0100692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 ret = calculate_merkle_tree(priv_ctx, tree);
694 if (ret != 0) {
Raef Coles142e5772022-10-12 10:47:27 +0100695 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100696 }
697
698 /* Root node is always at position 1, due to 1-based indexing */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100700
Raef Colesf5632d32022-09-01 09:56:52 +0100701 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100702
Raef Coles142e5772022-10-12 10:47:27 +0100703 ret = 0;
704
705exit:
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100706 mbedtls_zeroize_and_free(tree, node_bytes *
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MERKLE_TREE_NODE_AM(priv_ctx->params.type));
Raef Coles142e5772022-10-12 10:47:27 +0100708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100710}
711
Raef Coles01c71a12022-08-31 15:55:00 +0100712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
714 int (*f_rng)(void *, unsigned char *, size_t),
715 void *p_rng, const unsigned char *msg,
716 unsigned int msg_size, unsigned char *sig, size_t sig_size,
717 size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100718{
719 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if (!ctx->have_private_key) {
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 (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
727 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100728 }
729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
731 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100732 }
733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (ctx->params.otstype
735 != MBEDTLS_LMOTS_SHA256_N32_W8) {
736 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100737 }
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
740 return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
Raef Coles8ff6df52021-07-21 12:42:15 +0100741 }
742
743
Raef Colesf5632d32022-09-01 09:56:52 +0100744 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100745 /* This new value must _always_ be written back to the disk before the
746 * signature is returned.
747 */
Raef Colesf5632d32022-09-01 09:56:52 +0100748 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
751 < SIG_OTS_SIG_OFFSET) {
752 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles1fb2f322022-10-10 11:23:07 +0100753 }
754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
756 f_rng,
757 p_rng,
758 msg,
759 msg_size,
760 sig + SIG_OTS_SIG_OFFSET,
761 MBEDTLS_LMS_SIG_LEN(ctx->params.type,
762 ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
763 NULL);
764 if (ret != 0) {
765 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100766 }
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
769 MBEDTLS_LMS_TYPE_LEN,
770 sig + SIG_TYPE_OFFSET(ctx->params.otstype));
771 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
772 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
773 sig + SIG_Q_LEAF_ID_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 ret = get_merkle_path(ctx,
776 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
777 sig + SIG_PATH_OFFSET(ctx->params.otstype));
778 if (ret != 0) {
779 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100780 }
781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100783 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100784 }
785
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100788}
789
Raef Coles5127e852022-10-07 10:35:56 +0100790#endif /* defined(MBEDTLS_LMS_PRIVATE) */
791#endif /* defined(MBEDTLS_LMS_C) */