blob: 296adceda0e249deb07c33b57b5f838cf8aebe73 [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"
42
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
47#if defined(MBEDTLS_PLATFORM_C)
48#include "mbedtls/platform.h"
49#else
50#include <stdlib.h>
51#include <stdio.h>
52#define mbedtls_printf printf
53#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Raef Coles57d53282022-09-27 11:30:51 +010057#define SIG_Q_LEAF_ID_OFFSET (0)
58#define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
59 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
60#define SIG_TYPE_OFFSET(otstype) (SIG_OTS_SIG_OFFSET + \
61 MBEDTLS_LMOTS_SIG_LEN(otstype))
62#define SIG_PATH_OFFSET(otstype) (SIG_TYPE_OFFSET(otstype) + \
63 MBEDTLS_LMS_TYPE_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010064
Raef Coles57d53282022-09-27 11:30:51 +010065#define PUBLIC_KEY_TYPE_OFFSET (0)
66#define PUBLIC_KEY_OTSTYPE_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
67 MBEDTLS_LMS_TYPE_LEN)
68#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_OTSTYPE_OFFSET + \
69 MBEDTLS_LMOTS_TYPE_LEN)
70#define PUBLIC_KEY_ROOT_NODE_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
71 MBEDTLS_LMOTS_I_KEY_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010072
73
Raef Colese9479a02022-09-01 16:06:35 +010074/* Currently only support H=10 */
Raef Coles57d53282022-09-27 11:30:51 +010075#define H_TREE_HEIGHT_MAX 10
76#define MERKLE_TREE_NODE_AM_MAX (1u << (H_TREE_HEIGHT_MAX + 1u))
Raef Colese9479a02022-09-01 16:06:35 +010077#define MERKLE_TREE_NODE_AM(type) (1u << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
78#define MERKLE_TREE_LEAF_NODE_AM(type) (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type))
79#define MERKLE_TREE_INTERNAL_NODE_AM(type) (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010080
81#define D_CONST_LEN (2)
Raef Coles01c71a12022-08-31 15:55:00 +010082static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = {0x82, 0x82};
Raef Coles0a967cc2022-09-02 17:46:15 +010083static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83};
Raef Coles8ff6df52021-07-21 12:42:15 +010084
Raef Coles0a967cc2022-09-02 17:46:15 +010085
86/* Calculate the value of a leaf node of the merkle tree (which is a hash of a
87 * public key and some other parameters like the leaf index). This function
88 * implements RFC8554 section 5.3, in the case where r >= 2^h.
89 *
Raef Coles98d6e222022-09-23 09:04:04 +010090 * params The LMS parameter set, the underlying LMOTS
91 * parameter set, and I value which describe the key
92 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +010093 *
Raef Coles98d6e222022-09-23 09:04:04 +010094 * pub_key The public key of the private whose index
95 * corresponds to the index of this leaf node. This
96 * is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010097 *
Raef Coles98d6e222022-09-23 09:04:04 +010098 * r_node_idx The index of this node in the merkle tree. Note
99 * that the root node of the merkle tree is
100 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100101 *
Raef Coles98d6e222022-09-23 09:04:04 +0100102 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100103 */
Raef Colese9479a02022-09-01 16:06:35 +0100104static int create_merkle_leaf_value( const mbedtls_lms_parameters_t *params,
105 unsigned char *pub_key,
Raef Colesebd35b52022-09-01 11:52:17 +0100106 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +0100107 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100108{
Raef Colesc8f96042022-08-25 13:49:54 +0100109 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100111 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100112 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100113
Raef Colesc8f96042022-08-25 13:49:54 +0100114 op = psa_hash_operation_init( );
115 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100116 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100117 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100118
Raef Colese9479a02022-09-01 16:06:35 +0100119 status = psa_hash_update( &op, params->I_key_identifier,
120 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100121 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100122 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100123
Raef Colesad054252022-09-27 10:59:16 +0100124 mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100125 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
Raef Coles29117d22022-10-07 11:46:06 +0100126 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100127 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100128
Raef Coles01c71a12022-08-31 15:55:00 +0100129 status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100130 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100131 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100132
Raef Colese9479a02022-09-01 16:06:35 +0100133 status = psa_hash_update( &op, pub_key,
134 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype) );
Raef Coles29117d22022-10-07 11:46:06 +0100135 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100136 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100137
Raef Colese9479a02022-09-01 16:06:35 +0100138 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100139 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100140 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100141 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100142
Raef Coles01c71a12022-08-31 15:55:00 +0100143exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100144 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100145
Raef Coles29117d22022-10-07 11:46:06 +0100146 return ( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100147}
148
Raef Coles0a967cc2022-09-02 17:46:15 +0100149/* Calculate the value of an internal node of the merkle tree (which is a hash
150 * of a public key and some other parameters like the node index). This function
151 * implements RFC8554 section 5.3, in the case where r < 2^h.
152 *
Raef Coles98d6e222022-09-23 09:04:04 +0100153 * params The LMS parameter set, the underlying LMOTS
154 * parameter set, and I value which describe the key
155 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100156 *
Raef Coles98d6e222022-09-23 09:04:04 +0100157 * left_node The value of the child of this node which is on
158 * the left-hand side. As with all nodes on the
159 * merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100160 *
Raef Coles98d6e222022-09-23 09:04:04 +0100161 * right_node The value of the child of this node which is on
162 * the right-hand side. As with all nodes on the
163 * merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100164 *
Raef Coles98d6e222022-09-23 09:04:04 +0100165 * r_node_idx The index of this node in the merkle tree. Note
166 * that the root node of the merkle tree is
167 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100168 *
Raef Coles98d6e222022-09-23 09:04:04 +0100169 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100170 */
Raef Colese9479a02022-09-01 16:06:35 +0100171static int create_merkle_internal_value( const mbedtls_lms_parameters_t *params,
172 const unsigned char *left_node,
173 const unsigned char *right_node,
Raef Colesebd35b52022-09-01 11:52:17 +0100174 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +0100175 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100176{
Raef Colesc8f96042022-08-25 13:49:54 +0100177 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100178 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100179 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100180 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100181
Raef Colesc8f96042022-08-25 13:49:54 +0100182 op = psa_hash_operation_init( );
183 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100184 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100185 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100186
Raef Colese9479a02022-09-01 16:06:35 +0100187 status = psa_hash_update( &op, params->I_key_identifier,
188 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100189 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100190 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100191
Raef Colesad054252022-09-27 10:59:16 +0100192 mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100193 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
Raef Coles29117d22022-10-07 11:46:06 +0100194 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100195 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100196
Raef Colesfa24f9d2022-09-02 17:46:52 +0100197 status = psa_hash_update( &op, D_INTR_CONSTANT_BYTES, D_CONST_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100198 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100199 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100200
Raef Colese9479a02022-09-01 16:06:35 +0100201 status = psa_hash_update( &op, left_node,
202 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100203 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100204 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100205
Raef Colese9479a02022-09-01 16:06:35 +0100206 status = psa_hash_update( &op, right_node,
207 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100208 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100209 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100210
Raef Coles29117d22022-10-07 11:46:06 +0100211 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100212 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100213 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100214 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100215
Raef Coles01c71a12022-08-31 15:55:00 +0100216exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100217 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100218
Raef Coles29117d22022-10-07 11:46:06 +0100219 return( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100220}
221
Raef Colesbe3bdd82022-10-07 12:04:24 +0100222void mbedtls_lms_public_init( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100223{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100224 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100225}
226
Raef Colesbe3bdd82022-10-07 12:04:24 +0100227void mbedtls_lms_public_free( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100228{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100229 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100230}
231
Raef Coles01c71a12022-08-31 15:55:00 +0100232int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
233 const unsigned char *key, size_t key_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100234{
Raef Coles01c71a12022-08-31 15:55:00 +0100235 mbedtls_lms_algorithm_type_t type;
236 mbedtls_lmots_algorithm_type_t otstype;
237
Raef Colesad054252022-09-27 10:59:16 +0100238 type = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100239 key + PUBLIC_KEY_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100240 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100241 {
242 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
243 }
Raef Colesf5632d32022-09-01 09:56:52 +0100244 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100245
Raef Colese89488d2022-10-07 16:06:35 +0100246 if( key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
247 {
248 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
249 }
250
Raef Colesad054252022-09-27 10:59:16 +0100251 otstype = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100252 key + PUBLIC_KEY_OTSTYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100253 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
254 {
255 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
256 }
Raef Colesf5632d32022-09-01 09:56:52 +0100257 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100258
Raef Colesf5632d32022-09-01 09:56:52 +0100259 memcpy( ctx->params.I_key_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100260 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Coles01c71a12022-08-31 15:55:00 +0100261 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles57d53282022-09-27 11:30:51 +0100262 memcpy( ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100263 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100264
Raef Colesf5632d32022-09-01 09:56:52 +0100265 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100266
267 return( 0 );
268}
269
Raef Coles370cc432022-10-07 16:07:33 +0100270int mbedtls_lms_export_public_key( const mbedtls_lms_public_t *ctx,
271 unsigned char *key,
272 size_t key_size, size_t *key_len )
273{
274 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
275 {
276 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
277 }
278
279 if( ! ctx->have_public_key )
280 {
281 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
282 }
283
284 mbedtls_lms_unsigned_int_to_network_bytes(
285 ctx->params.type,
286 MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET );
287 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.otstype,
288 MBEDTLS_LMOTS_TYPE_LEN,
289 key + PUBLIC_KEY_OTSTYPE_OFFSET );
290 memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
291 ctx->params.I_key_identifier,
292 MBEDTLS_LMOTS_I_KEY_ID_LEN );
293 memcpy( key +PUBLIC_KEY_ROOT_NODE_OFFSET,
294 ctx->T_1_pub_key,
295 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
296
297 if( key_len != NULL )
298 {
299 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
300 }
301
302 return( 0 );
303}
304
Raef Coles01c71a12022-08-31 15:55:00 +0100305int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
306 const unsigned char *msg, size_t msg_size,
307 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100308{
309 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100310 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
311 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100312 unsigned int height;
313 unsigned int curr_node_id;
314 unsigned int parent_node_id;
315 const unsigned char* left_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100316 const unsigned char* right_node;
317 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
319
Raef Colesf5632d32022-09-01 09:56:52 +0100320 if( ! ctx->have_public_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100321 {
322 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
323 }
324
Raef Colese9479a02022-09-01 16:06:35 +0100325 if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100326 {
327 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
328 }
329
Raef Colesf5632d32022-09-01 09:56:52 +0100330 if( ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100331 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100332 {
333 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
334 }
335
Raef Colesf5632d32022-09-01 09:56:52 +0100336 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100337 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100338 {
339 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
340 }
341
Raef Colesad054252022-09-27 10:59:16 +0100342 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100343 sig + SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET )
Raef Colese9479a02022-09-01 16:06:35 +0100344 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100345 {
346 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
347 }
348
Raef Colesad054252022-09-27 10:59:16 +0100349 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100350 sig + SIG_TYPE_OFFSET(ctx->params.otstype))
Raef Colese9479a02022-09-01 16:06:35 +0100351 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100352 {
353 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
354 }
355
356
Raef Colesad054252022-09-27 10:59:16 +0100357 q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
Raef Coles57d53282022-09-27 11:30:51 +0100358 MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100359
Raef Colese9479a02022-09-01 16:06:35 +0100360 if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100361 {
362 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
363 }
364
Raef Coles9b88ee52022-09-02 12:04:21 +0100365 memcpy( ots_params.I_key_identifier,
366 ctx->params.I_key_identifier,
367 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesad054252022-09-27 10:59:16 +0100368 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
369 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
370 ots_params.q_leaf_identifier );
Raef Colesf5632d32022-09-01 09:56:52 +0100371 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100372
Raef Coles366d67d2022-09-01 17:23:12 +0100373 ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg,
Raef Coles57d53282022-09-27 11:30:51 +0100374 msg_size, sig + SIG_OTS_SIG_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100375 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), Kc_candidate_ots_pub_key,
Raef Coles9b88ee52022-09-02 12:04:21 +0100376 sizeof( Kc_candidate_ots_pub_key ), NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100377 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100378 {
379 return( ret );
380 }
381
Raef Colesebd35b52022-09-01 11:52:17 +0100382 create_merkle_leaf_value(
Raef Colese9479a02022-09-01 16:06:35 +0100383 &ctx->params,
384 Kc_candidate_ots_pub_key,
385 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100386 Tc_candidate_root_node );
Raef Coles8ff6df52021-07-21 12:42:15 +0100387
Raef Coles366d67d2022-09-01 17:23:12 +0100388 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
389 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100390
Raef Colese9479a02022-09-01 16:06:35 +0100391 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
392 height++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100393 {
394 parent_node_id = curr_node_id / 2;
395
396 /* Left/right node ordering matters for the hash */
397 if( curr_node_id & 1 )
398 {
Raef Coles57d53282022-09-27 11:30:51 +0100399 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100400 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100401 right_node = Tc_candidate_root_node;
Raef Coles8ff6df52021-07-21 12:42:15 +0100402 }
403 else
404 {
405 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100406 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100407 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100408 }
409
Raef Colese9479a02022-09-01 16:06:35 +0100410 create_merkle_internal_value( &ctx->params, left_node, right_node,
411 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100412
413 curr_node_id /= 2;
414 }
415
Raef Colesf5632d32022-09-01 09:56:52 +0100416 if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100417 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100418 {
419 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
420 }
421
422 return( 0 );
423}
424
Raef Coles5127e852022-10-07 10:35:56 +0100425#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100426
Raef Coles0a967cc2022-09-02 17:46:15 +0100427/* Calculate a full merkle tree based on a private key. This function
428 * implements RFC8554 section 5.3, and is used to generate a public key (as the
429 * public key is the root node of the merkle tree).
430 *
Raef Coles98d6e222022-09-23 09:04:04 +0100431 * ctx The LMS private context, containing a parameter
432 * set and private key material consisting of both
433 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100434 *
Raef Coles98d6e222022-09-23 09:04:04 +0100435 * tree The output tree, which is 2^(H + 1) hash outputs.
436 * In the case of H=10 we have 2048 tree nodes (of
437 * which 1024 of them are leaf nodes). Note that
438 * because the merkle tree root is 1-indexed, the 0
439 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100440 */
Raef Coles2ac352a2022-10-07 11:12:27 +0100441static int calculate_merkle_tree( const mbedtls_lms_private_t *ctx,
Raef Colese9479a02022-09-01 16:06:35 +0100442 unsigned char *tree )
Raef Colesab4f8742022-09-01 12:24:31 +0100443{
444 unsigned int priv_key_idx;
445 unsigned int r_node_idx;
446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
447
448 /* First create the leaf nodes, in ascending order */
Raef Colese9479a02022-09-01 16:06:35 +0100449 for( priv_key_idx = 0;
450 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100451 priv_key_idx++ )
452 {
Raef Colese9479a02022-09-01 16:06:35 +0100453 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100454
Raef Colese9479a02022-09-01 16:06:35 +0100455 ret = create_merkle_leaf_value( &ctx->params,
456 ctx->ots_public_keys[priv_key_idx].public_key, r_node_idx,
457 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100458 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100459 {
460 return( ret );
461 }
462 }
463
464 /* Then the internal nodes, in reverse order so that we can guarantee the
465 * parent has been created */
Raef Colese9479a02022-09-01 16:06:35 +0100466 for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
467 r_node_idx > 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100468 r_node_idx-- )
469 {
Raef Colese9479a02022-09-01 16:06:35 +0100470 ret = create_merkle_internal_value( &ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100471 &tree[( r_node_idx * 2 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
472 &tree[( r_node_idx * 2 + 1 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
Raef Coles366d67d2022-09-01 17:23:12 +0100473 r_node_idx,
474 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100475 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100476 {
477 return( ret );
478 }
479 }
480
481 return( 0 );
482}
483
Raef Coles0a967cc2022-09-02 17:46:15 +0100484/* Calculate a path from a leaf node of the merkle tree to the root of the tree,
485 * and return the full path. This function implements RFC8554 section 5.4.1, as
486 * the merkle path is the main component of an LMS signature.
487 *
Raef Coles98d6e222022-09-23 09:04:04 +0100488 * ctx The LMS private context, containing a parameter
489 * set and private key material consisting of both
490 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100491 *
Raef Coles98d6e222022-09-23 09:04:04 +0100492 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100493 *
Raef Coles98d6e222022-09-23 09:04:04 +0100494 * tree The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100495 */
Raef Colesab4f8742022-09-01 12:24:31 +0100496static int get_merkle_path( mbedtls_lms_private_t *ctx,
497 unsigned int leaf_node_id,
Raef Colese9479a02022-09-01 16:06:35 +0100498 unsigned char *path )
Raef Colesab4f8742022-09-01 12:24:31 +0100499{
Raef Colese9479a02022-09-01 16:06:35 +0100500 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Colesab4f8742022-09-01 12:24:31 +0100501 unsigned int curr_node_id = leaf_node_id;
502 unsigned int adjacent_node_id;
503 unsigned int height;
504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
505
Raef Coles9b88ee52022-09-02 12:04:21 +0100506 ret = calculate_merkle_tree( ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100507 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100508 {
509 return( ret );
510 }
511
Raef Colese9479a02022-09-01 16:06:35 +0100512 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
513 height++ )
Raef Colesab4f8742022-09-01 12:24:31 +0100514 {
515 adjacent_node_id = curr_node_id ^ 1;
516
Raef Colese9479a02022-09-01 16:06:35 +0100517 memcpy( &path[height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
518 &tree[adjacent_node_id],
519 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Colesab4f8742022-09-01 12:24:31 +0100520
521 curr_node_id >>=1;
522 }
523
524 return( 0 );
525}
526
Raef Colesbe3bdd82022-10-07 12:04:24 +0100527void mbedtls_lms_private_init( mbedtls_lms_private_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100528{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100529 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) ) ;
Raef Coles01c71a12022-08-31 15:55:00 +0100530}
Raef Coles8ff6df52021-07-21 12:42:15 +0100531
Raef Colesbe3bdd82022-10-07 12:04:24 +0100532void mbedtls_lms_private_free( mbedtls_lms_private_t *ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100533{
534 unsigned int idx;
535
Raef Colesf5632d32022-09-01 09:56:52 +0100536 if( ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100537 {
Raef Colese9479a02022-09-01 16:06:35 +0100538 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100539 {
Raef Colesbe3bdd82022-10-07 12:04:24 +0100540 mbedtls_lmots_private_free( &ctx->ots_private_keys[idx] );
541 mbedtls_lmots_public_free( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100542 }
543
Raef Colesdc8fb792022-10-07 13:27:54 +0100544 if( ctx->ots_private_keys != NULL )
545 mbedtls_free( ctx->ots_private_keys );
546
547 if( ctx->ots_public_keys != NULL )
548 mbedtls_free( ctx->ots_public_keys );
Raef Coles8ff6df52021-07-21 12:42:15 +0100549 }
550
Raef Colesbe3bdd82022-10-07 12:04:24 +0100551 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100552}
Raef Coles8ff6df52021-07-21 12:42:15 +0100553
Raef Coles01c71a12022-08-31 15:55:00 +0100554
555int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
556 mbedtls_lms_algorithm_type_t type,
557 mbedtls_lmots_algorithm_type_t otstype,
558 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100559 void* p_rng, const unsigned char *seed,
Raef Coles01c71a12022-08-31 15:55:00 +0100560 size_t seed_size )
561{
562 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
564
Raef Coles8ff6df52021-07-21 12:42:15 +0100565 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
566 {
567 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
568 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100569
Raef Coles8ff6df52021-07-21 12:42:15 +0100570 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
571 {
572 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
573 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100574
Raef Colesf5632d32022-09-01 09:56:52 +0100575 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100576 {
577 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
578 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100579
Raef Colesf5632d32022-09-01 09:56:52 +0100580 ctx->params.type = type;
581 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100582
Raef Coles3f6cdd72022-10-07 14:07:59 +0100583 ret = f_rng( p_rng,
584 ctx->params.I_key_identifier,
585 MBEDTLS_LMOTS_I_KEY_ID_LEN );
586 if( ret != 0 )
587 {
588 goto exit;
589 }
Raef Coles01c71a12022-08-31 15:55:00 +0100590
Raef Colese34e3c02022-10-10 11:11:30 +0100591 ctx->ots_private_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100592 sizeof( *ctx->ots_private_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100593 if( ctx->ots_private_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100594 {
595 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
596 goto exit;
597 }
598
Raef Colese34e3c02022-10-10 11:11:30 +0100599 ctx->ots_public_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100600 sizeof( *ctx->ots_public_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100601 if( ctx->ots_public_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100602 {
Raef Colesdc8fb792022-10-07 13:27:54 +0100603 /* Free just the ots private keys (since they've been allocated at this
604 * point) so that we can pass the context to lms_private_free (which
605 * will not try to free the private keys since have_private_key is not
606 * set.
607 */
608 mbedtls_free(ctx->ots_private_keys);
609 ctx->ots_private_keys = NULL;
Raef Coles01c71a12022-08-31 15:55:00 +0100610 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
611 goto exit;
612 }
613
Raef Colesdc8fb792022-10-07 13:27:54 +0100614 /* Now that all the allocation has succeeded we set have_private_key, since
615 * that causes lms_private_free to free the ots keys.
616 */
617 ctx->have_private_key = 1;
618
Raef Colese9479a02022-09-01 16:06:35 +0100619 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100620 {
Raef Colesbe3bdd82022-10-07 12:04:24 +0100621 mbedtls_lmots_private_init( &ctx->ots_private_keys[idx] );
622 mbedtls_lmots_public_init( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100623 }
624
625
Raef Colese9479a02022-09-01 16:06:35 +0100626 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100627 {
Raef Colesf5632d32022-09-01 09:56:52 +0100628 ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100629 otstype,
Raef Colesf5632d32022-09-01 09:56:52 +0100630 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100631 idx, seed, seed_size );
Raef Colese0a17612022-09-02 16:04:47 +0100632 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100633 goto exit;
634
Raef Colesf5632d32022-09-01 09:56:52 +0100635 ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
636 &ctx->ots_private_keys[idx] );
Raef Colese0a17612022-09-02 16:04:47 +0100637 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100638 goto exit;
639 }
640
Raef Colesf5632d32022-09-01 09:56:52 +0100641 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100642
643exit:
Raef Colese0a17612022-09-02 16:04:47 +0100644 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100645 {
Raef Colesdc8fb792022-10-07 13:27:54 +0100646 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100647 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100648
Raef Colesdc8fb792022-10-07 13:27:54 +0100649 return( ret );
Raef Coles8ff6df52021-07-21 12:42:15 +0100650}
651
Raef Coles01c71a12022-08-31 15:55:00 +0100652int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
Raef Coles2ac352a2022-10-07 11:12:27 +0100653 const mbedtls_lms_private_t *priv_ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100654{
Raef Colese9479a02022-09-01 16:06:35 +0100655 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
657
Raef Coles9b88ee52022-09-02 12:04:21 +0100658 if( ! priv_ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100659 {
660 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
661 }
662
Raef Colesf5632d32022-09-01 09:56:52 +0100663 if( priv_ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100664 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100665 {
666 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
667 }
668
Raef Colesf5632d32022-09-01 09:56:52 +0100669 if( priv_ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100670 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100671 {
672 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
673 }
674
Raef Colesf5632d32022-09-01 09:56:52 +0100675 memcpy( &ctx->params, &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100676 sizeof( mbedtls_lmots_parameters_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100677
Raef Coles9b88ee52022-09-02 12:04:21 +0100678 ret = calculate_merkle_tree( priv_ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100679 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100680 {
681 return( ret );
682 }
683
684 /* Root node is always at position 1, due to 1-based indexing */
Raef Colese9479a02022-09-01 16:06:35 +0100685 memcpy( ctx->T_1_pub_key, &tree[1],
Raef Coles9b88ee52022-09-02 12:04:21 +0100686 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100687
Raef Colesf5632d32022-09-01 09:56:52 +0100688 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100689
690 return( 0 );
691}
692
Raef Coles01c71a12022-08-31 15:55:00 +0100693
Raef Coles01c71a12022-08-31 15:55:00 +0100694int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
695 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100696 void* p_rng, const unsigned char *msg,
697 unsigned int msg_size, unsigned char *sig, size_t sig_size,
698 size_t *sig_len )
Raef Coles01c71a12022-08-31 15:55:00 +0100699{
700 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
702
Raef Colesf5632d32022-09-01 09:56:52 +0100703 if( ! ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100704 {
705 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
706 }
707
Raef Colese9479a02022-09-01 16:06:35 +0100708 if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles01c71a12022-08-31 15:55:00 +0100709 {
710 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
711 }
712
Raef Colesf5632d32022-09-01 09:56:52 +0100713 if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100714 {
715 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
716 }
717
Raef Colesf5632d32022-09-01 09:56:52 +0100718 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100719 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100720 {
721 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
722 }
723
Raef Colese9479a02022-09-01 16:06:35 +0100724 if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100725 {
Raef Coles01c71a12022-08-31 15:55:00 +0100726 return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
Raef Coles8ff6df52021-07-21 12:42:15 +0100727 }
728
729
Raef Colesf5632d32022-09-01 09:56:52 +0100730 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100731 /* This new value must _always_ be written back to the disk before the
732 * signature is returned.
733 */
Raef Colesf5632d32022-09-01 09:56:52 +0100734 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100735
Raef Coles1fb2f322022-10-10 11:23:07 +0100736 if ( MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
737 < SIG_OTS_SIG_OFFSET )
738 {
739 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
740 }
741
Raef Colesf5632d32022-09-01 09:56:52 +0100742 ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
Raef Coles01c71a12022-08-31 15:55:00 +0100743 f_rng, p_rng, msg, msg_size,
Raef Coles57d53282022-09-27 11:30:51 +0100744 sig + SIG_OTS_SIG_OFFSET,
Raef Coles02cf8232022-10-07 13:52:47 +0100745 MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100746 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100747 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100748 {
Raef Coles8ff6df52021-07-21 12:42:15 +0100749 return( ret );
750 }
751
Raef Colesad054252022-09-27 10:59:16 +0100752 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
753 MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100754 sig + SIG_TYPE_OFFSET(ctx->params.otstype) );
Raef Colesad054252022-09-27 10:59:16 +0100755 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
756 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100757 sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100758
Raef Colese9479a02022-09-01 16:06:35 +0100759 ret = get_merkle_path( ctx,
Raef Coles366d67d2022-09-01 17:23:12 +0100760 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100761 sig + SIG_PATH_OFFSET(ctx->params.otstype) );
Raef Colese0a17612022-09-02 16:04:47 +0100762 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100763 {
764 return( ret );
765 }
766
Raef Coles9b88ee52022-09-02 12:04:21 +0100767 if( sig_len != NULL )
768 {
Raef Colese9479a02022-09-01 16:06:35 +0100769 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100770 }
771
772
Raef Coles8ff6df52021-07-21 12:42:15 +0100773 return( 0 );
774}
775
Raef Coles5127e852022-10-07 10:35:56 +0100776#endif /* defined(MBEDTLS_LMS_PRIVATE) */
777#endif /* defined(MBEDTLS_LMS_C) */