blob: 72f93192bfe005f608275746c7ef4e9e653d7b64 [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
35#ifdef MBEDTLS_LMS_C
36
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 Colese9479a02022-09-01 16:06:35 +010057#define MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET (0)
Raef Coles366d67d2022-09-01 17:23:12 +010058#define MBEDTLS_LMS_SIG_OTS_SIG_OFFSET (MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET + \
59 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
60#define MBEDTLS_LMS_SIG_TYPE_OFFSET(otstype) (MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + \
61 MBEDTLS_LMOTS_SIG_LEN(otstype))
62#define MBEDTLS_LMS_SIG_PATH_OFFSET(otstype) (MBEDTLS_LMS_SIG_TYPE_OFFSET(otstype) + \
63 MBEDTLS_LMS_TYPE_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010064
65#define MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET (0)
Raef Coles366d67d2022-09-01 17:23:12 +010066#define MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET + \
67 MBEDTLS_LMS_TYPE_LEN)
68#define MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET + \
69 MBEDTLS_LMOTS_TYPE_LEN)
70#define MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET (MBEDTLS_LMS_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 */
75#define MBEDTLS_LMS_H_TREE_HEIGHT_MAX 10
76#define MERKLE_TREE_NODE_AM_MAX (1u << (MBEDTLS_LMS_H_TREE_HEIGHT_MAX + 1u))
77#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};
83static const unsigned char D_INTERNAL_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83};
Raef Coles8ff6df52021-07-21 12:42:15 +010084
Raef Colese9479a02022-09-01 16:06:35 +010085static int create_merkle_leaf_value( const mbedtls_lms_parameters_t *params,
86 unsigned char *pub_key,
Raef Colesebd35b52022-09-01 11:52:17 +010087 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +010088 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +010089{
Raef Colesc8f96042022-08-25 13:49:54 +010090 psa_hash_operation_t op;
91 psa_status_t status;
92 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +010093 unsigned char r_node_idx_bytes[4];
94 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
95
Raef Colesc8f96042022-08-25 13:49:54 +010096 op = psa_hash_operation_init( );
97 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
98 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +010099 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100100 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100101
Raef Colese9479a02022-09-01 16:06:35 +0100102 status = psa_hash_update( &op, params->I_key_identifier,
103 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100104 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100105 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100106 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100107
Raef Coles01c71a12022-08-31 15:55:00 +0100108 unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100109 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
110 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100111 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100112 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100113
Raef Coles01c71a12022-08-31 15:55:00 +0100114 status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100115 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100116 if( ret != 0 )
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, pub_key,
120 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype) );
Raef Colesc8f96042022-08-25 13:49:54 +0100121 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100122 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100123 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100124
Raef Colese9479a02022-09-01 16:06:35 +0100125 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100126 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100127 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100128 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100129 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100130
Raef Coles01c71a12022-08-31 15:55:00 +0100131exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100132 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100133
134 return( ret );
135}
136
Raef Colese9479a02022-09-01 16:06:35 +0100137static int create_merkle_internal_value( const mbedtls_lms_parameters_t *params,
138 const unsigned char *left_node,
139 const unsigned char *right_node,
Raef Colesebd35b52022-09-01 11:52:17 +0100140 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +0100141 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100142{
Raef Colesc8f96042022-08-25 13:49:54 +0100143 psa_hash_operation_t op;
144 psa_status_t status;
145 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100146 unsigned char r_node_idx_bytes[4];
147 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
148
Raef Colesc8f96042022-08-25 13:49:54 +0100149 op = psa_hash_operation_init( );
150 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
151 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100152 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100153 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100154
Raef Colese9479a02022-09-01 16:06:35 +0100155 status = psa_hash_update( &op, params->I_key_identifier,
156 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100157 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100158 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100159 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100160
Raef Coles01c71a12022-08-31 15:55:00 +0100161 unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100162 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
163 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100164 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100165 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100166
Raef Coles01c71a12022-08-31 15:55:00 +0100167 status = psa_hash_update( &op, D_INTERNAL_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100168 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100169 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100170 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100171
Raef Colese9479a02022-09-01 16:06:35 +0100172 status = psa_hash_update( &op, left_node,
173 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100174 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100175 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100176 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100177
Raef Colese9479a02022-09-01 16:06:35 +0100178 status = psa_hash_update( &op, right_node,
179 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100180 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100181 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100182 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100183
Raef Colese9479a02022-09-01 16:06:35 +0100184 ret = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100185 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100186 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100187 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100188 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100189
Raef Coles01c71a12022-08-31 15:55:00 +0100190exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100191 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100192
193 return ret;
194}
195
Raef Coles01c71a12022-08-31 15:55:00 +0100196void mbedtls_lms_init_public( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100197{
Raef Coles01c71a12022-08-31 15:55:00 +0100198 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100199}
200
Raef Coles01c71a12022-08-31 15:55:00 +0100201void mbedtls_lms_free_public( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100202{
Raef Coles01c71a12022-08-31 15:55:00 +0100203 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100204}
205
Raef Coles01c71a12022-08-31 15:55:00 +0100206int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
207 const unsigned char *key, size_t key_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100208{
Raef Coles01c71a12022-08-31 15:55:00 +0100209 mbedtls_lms_algorithm_type_t type;
210 mbedtls_lmots_algorithm_type_t otstype;
211
Raef Colese9479a02022-09-01 16:06:35 +0100212 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100213 {
214 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
215 }
216
Raef Coles366d67d2022-09-01 17:23:12 +0100217 type = network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
218 key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100219 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100220 {
221 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
222 }
Raef Colesf5632d32022-09-01 09:56:52 +0100223 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100224
Raef Coles01c71a12022-08-31 15:55:00 +0100225 otstype = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100226 key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100227 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
228 {
229 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
230 }
Raef Colesf5632d32022-09-01 09:56:52 +0100231 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100232
Raef Colesf5632d32022-09-01 09:56:52 +0100233 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100234 key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET,
235 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesf5632d32022-09-01 09:56:52 +0100236 memcpy( ctx->T_1_pub_key, key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100237 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100238
Raef Colesf5632d32022-09-01 09:56:52 +0100239 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100240
241 return( 0 );
242}
243
Raef Coles01c71a12022-08-31 15:55:00 +0100244int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
245 const unsigned char *msg, size_t msg_size,
246 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100247{
248 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100249 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
250 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100251 unsigned int height;
252 unsigned int curr_node_id;
253 unsigned int parent_node_id;
254 const unsigned char* left_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100255 const unsigned char* right_node;
256 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
258
Raef Colesf5632d32022-09-01 09:56:52 +0100259 if( ! ctx->have_public_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100260 {
261 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
262 }
263
Raef Colese9479a02022-09-01 16:06:35 +0100264 if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100265 {
266 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
267 }
268
Raef Colesf5632d32022-09-01 09:56:52 +0100269 if( ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100270 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100271 {
272 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
273 }
274
Raef Colesf5632d32022-09-01 09:56:52 +0100275 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100276 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100277 {
278 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
279 }
280
Raef Colese9479a02022-09-01 16:06:35 +0100281 if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles9b88ee52022-09-02 12:04:21 +0100282 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET )
Raef Colese9479a02022-09-01 16:06:35 +0100283 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100284 {
285 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
286 }
287
Raef Colese9479a02022-09-01 16:06:35 +0100288 if( network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100289 sig + MBEDTLS_LMS_SIG_TYPE_OFFSET(ctx->params.otstype))
Raef Colese9479a02022-09-01 16:06:35 +0100290 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100291 {
292 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
293 }
294
295
Raef Coles01c71a12022-08-31 15:55:00 +0100296 q_leaf_identifier = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100297 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100298
Raef Colese9479a02022-09-01 16:06:35 +0100299 if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100300 {
301 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
302 }
303
Raef Coles9b88ee52022-09-02 12:04:21 +0100304 memcpy( ots_params.I_key_identifier,
305 ctx->params.I_key_identifier,
306 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100307 unsigned_int_to_network_bytes( q_leaf_identifier,
308 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Colesf5632d32022-09-01 09:56:52 +0100309 ots_params.q_leaf_identifier );
310 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100311
Raef Coles366d67d2022-09-01 17:23:12 +0100312 ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg,
313 msg_size, sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
314 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), Kc_candidate_ots_pub_key,
Raef Coles9b88ee52022-09-02 12:04:21 +0100315 sizeof( Kc_candidate_ots_pub_key ), NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100316 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100317 {
318 return( ret );
319 }
320
Raef Colesebd35b52022-09-01 11:52:17 +0100321 create_merkle_leaf_value(
Raef Colese9479a02022-09-01 16:06:35 +0100322 &ctx->params,
323 Kc_candidate_ots_pub_key,
324 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100325 Tc_candidate_root_node );
Raef Coles8ff6df52021-07-21 12:42:15 +0100326
Raef Coles366d67d2022-09-01 17:23:12 +0100327 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
328 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100329
Raef Colese9479a02022-09-01 16:06:35 +0100330 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
331 height++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100332 {
333 parent_node_id = curr_node_id / 2;
334
335 /* Left/right node ordering matters for the hash */
336 if( curr_node_id & 1 )
337 {
Raef Colese9479a02022-09-01 16:06:35 +0100338 left_node = sig + MBEDTLS_LMS_SIG_PATH_OFFSET(ctx->params.otstype) +
339 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100340 right_node = Tc_candidate_root_node;
Raef Coles8ff6df52021-07-21 12:42:15 +0100341 }
342 else
343 {
344 left_node = Tc_candidate_root_node;
Raef Colese9479a02022-09-01 16:06:35 +0100345 right_node = sig + MBEDTLS_LMS_SIG_PATH_OFFSET(ctx->params.otstype) +
346 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100347 }
348
Raef Colese9479a02022-09-01 16:06:35 +0100349 create_merkle_internal_value( &ctx->params, left_node, right_node,
350 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100351
352 curr_node_id /= 2;
353 }
354
Raef Colesf5632d32022-09-01 09:56:52 +0100355 if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100356 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100357 {
358 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
359 }
360
361 return( 0 );
362}
363
Raef Colesab4f8742022-09-01 12:24:31 +0100364#ifdef MBEDTLS_LMS_PRIVATE
365
366static int calculate_merkle_tree( mbedtls_lms_private_t *ctx,
Raef Colese9479a02022-09-01 16:06:35 +0100367 unsigned char *tree )
Raef Colesab4f8742022-09-01 12:24:31 +0100368{
369 unsigned int priv_key_idx;
370 unsigned int r_node_idx;
371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
372
373 /* First create the leaf nodes, in ascending order */
Raef Colese9479a02022-09-01 16:06:35 +0100374 for( priv_key_idx = 0;
375 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100376 priv_key_idx++ )
377 {
Raef Colese9479a02022-09-01 16:06:35 +0100378 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100379
Raef Colese9479a02022-09-01 16:06:35 +0100380 ret = create_merkle_leaf_value( &ctx->params,
381 ctx->ots_public_keys[priv_key_idx].public_key, r_node_idx,
382 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100383 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100384 {
385 return( ret );
386 }
387 }
388
389 /* Then the internal nodes, in reverse order so that we can guarantee the
390 * parent has been created */
Raef Colese9479a02022-09-01 16:06:35 +0100391 for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
392 r_node_idx > 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100393 r_node_idx-- )
394 {
Raef Colese9479a02022-09-01 16:06:35 +0100395 ret = create_merkle_internal_value( &ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100396 &tree[( r_node_idx * 2 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
397 &tree[( r_node_idx * 2 + 1 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
Raef Coles366d67d2022-09-01 17:23:12 +0100398 r_node_idx,
399 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100400 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100401 {
402 return( ret );
403 }
404 }
405
406 return( 0 );
407}
408
409static int get_merkle_path( mbedtls_lms_private_t *ctx,
410 unsigned int leaf_node_id,
Raef Colese9479a02022-09-01 16:06:35 +0100411 unsigned char *path )
Raef Colesab4f8742022-09-01 12:24:31 +0100412{
Raef Colese9479a02022-09-01 16:06:35 +0100413 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Colesab4f8742022-09-01 12:24:31 +0100414 unsigned int curr_node_id = leaf_node_id;
415 unsigned int adjacent_node_id;
416 unsigned int height;
417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
418
Raef Coles9b88ee52022-09-02 12:04:21 +0100419 ret = calculate_merkle_tree( ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100420 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100421 {
422 return( ret );
423 }
424
Raef Colese9479a02022-09-01 16:06:35 +0100425 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
426 height++ )
Raef Colesab4f8742022-09-01 12:24:31 +0100427 {
428 adjacent_node_id = curr_node_id ^ 1;
429
Raef Colese9479a02022-09-01 16:06:35 +0100430 memcpy( &path[height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
431 &tree[adjacent_node_id],
432 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Colesab4f8742022-09-01 12:24:31 +0100433
434 curr_node_id >>=1;
435 }
436
437 return( 0 );
438}
439
Raef Coles01c71a12022-08-31 15:55:00 +0100440void mbedtls_lms_init_private( mbedtls_lms_private_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100441{
Raef Coles01c71a12022-08-31 15:55:00 +0100442 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ;
443}
Raef Coles8ff6df52021-07-21 12:42:15 +0100444
Raef Coles01c71a12022-08-31 15:55:00 +0100445void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx )
446{
447 unsigned int idx;
448
Raef Colesf5632d32022-09-01 09:56:52 +0100449 if( ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100450 {
Raef Colese9479a02022-09-01 16:06:35 +0100451 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100452 {
Raef Colesf5632d32022-09-01 09:56:52 +0100453 mbedtls_lmots_free_private( &ctx->ots_private_keys[idx] );
454 mbedtls_lmots_free_public( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100455 }
456
Raef Colesf5632d32022-09-01 09:56:52 +0100457 mbedtls_free( ctx->ots_private_keys );
458 mbedtls_free( ctx->ots_public_keys );
Raef Coles8ff6df52021-07-21 12:42:15 +0100459 }
460
Raef Coles01c71a12022-08-31 15:55:00 +0100461 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) );
462}
Raef Coles8ff6df52021-07-21 12:42:15 +0100463
Raef Coles01c71a12022-08-31 15:55:00 +0100464
465int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
466 mbedtls_lms_algorithm_type_t type,
467 mbedtls_lmots_algorithm_type_t otstype,
468 int (*f_rng)(void *, unsigned char *, size_t),
469 void* p_rng, unsigned char *seed,
470 size_t seed_size )
471{
472 unsigned int idx = 0;
473 unsigned int free_idx = 0;
474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
475
Raef Coles8ff6df52021-07-21 12:42:15 +0100476 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
477 {
478 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
479 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100480
Raef Coles8ff6df52021-07-21 12:42:15 +0100481 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
482 {
483 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
484 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100485
Raef Colesf5632d32022-09-01 09:56:52 +0100486 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100487 {
488 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
489 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100490
Raef Colesf5632d32022-09-01 09:56:52 +0100491 ctx->params.type = type;
492 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100493
494 f_rng( p_rng,
Raef Colesf5632d32022-09-01 09:56:52 +0100495 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100496 MBEDTLS_LMOTS_I_KEY_ID_LEN );
497
Raef Colese9479a02022-09-01 16:06:35 +0100498 ctx->ots_private_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100499 sizeof( mbedtls_lmots_private_t ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100500 if( ctx->ots_private_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100501 {
502 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
503 goto exit;
504 }
505
Raef Colese9479a02022-09-01 16:06:35 +0100506 ctx->ots_public_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100507 sizeof( mbedtls_lmots_public_t ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100508 if( ctx->ots_public_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100509 {
510 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
511 goto exit;
512 }
513
Raef Colese9479a02022-09-01 16:06:35 +0100514 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100515 {
Raef Colesf5632d32022-09-01 09:56:52 +0100516 mbedtls_lmots_init_private( &ctx->ots_private_keys[idx] );
517 mbedtls_lmots_init_public( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100518 }
519
520
Raef Colese9479a02022-09-01 16:06:35 +0100521 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100522 {
Raef Colesf5632d32022-09-01 09:56:52 +0100523 ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100524 otstype,
Raef Colesf5632d32022-09-01 09:56:52 +0100525 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100526 idx, seed, seed_size );
Raef Colese0a17612022-09-02 16:04:47 +0100527 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100528 goto exit;
529
Raef Colesf5632d32022-09-01 09:56:52 +0100530 ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
531 &ctx->ots_private_keys[idx] );
Raef Colese0a17612022-09-02 16:04:47 +0100532 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100533 goto exit;
534 }
535
Raef Colesf5632d32022-09-01 09:56:52 +0100536 ctx->q_next_usable_key = 0;
537 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100538
539exit:
Raef Colese0a17612022-09-02 16:04:47 +0100540 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100541 {
Raef Coles9b88ee52022-09-02 12:04:21 +0100542 for ( free_idx = 0; free_idx < idx; free_idx++ )
543 {
Raef Colesf5632d32022-09-01 09:56:52 +0100544 mbedtls_lmots_free_private( &ctx->ots_private_keys[free_idx] );
545 mbedtls_lmots_free_public( &ctx->ots_public_keys[free_idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100546 }
547
Raef Colesf5632d32022-09-01 09:56:52 +0100548 mbedtls_free( ctx->ots_private_keys );
549 mbedtls_free( ctx->ots_public_keys );
Raef Coles01c71a12022-08-31 15:55:00 +0100550 return( ret );
551 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100552
553 return( 0 );
554}
555
Raef Coles01c71a12022-08-31 15:55:00 +0100556int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
557 mbedtls_lms_private_t *priv_ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100558{
Raef Colese9479a02022-09-01 16:06:35 +0100559 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
561
Raef Coles9b88ee52022-09-02 12:04:21 +0100562 if( ! priv_ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100563 {
564 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
565 }
566
Raef Colesf5632d32022-09-01 09:56:52 +0100567 if( priv_ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100568 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100569 {
570 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
571 }
572
Raef Colesf5632d32022-09-01 09:56:52 +0100573 if( priv_ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100574 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100575 {
576 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
577 }
578
Raef Colesf5632d32022-09-01 09:56:52 +0100579 memcpy( &ctx->params, &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100580 sizeof( mbedtls_lmots_parameters_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100581
Raef Coles9b88ee52022-09-02 12:04:21 +0100582 ret = calculate_merkle_tree( priv_ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100583 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100584 {
585 return( ret );
586 }
587
588 /* Root node is always at position 1, due to 1-based indexing */
Raef Colese9479a02022-09-01 16:06:35 +0100589 memcpy( ctx->T_1_pub_key, &tree[1],
Raef Coles9b88ee52022-09-02 12:04:21 +0100590 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100591
Raef Colesf5632d32022-09-01 09:56:52 +0100592 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100593
594 return( 0 );
595}
596
Raef Coles01c71a12022-08-31 15:55:00 +0100597
Raef Coles366d67d2022-09-01 17:23:12 +0100598int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx,
599 unsigned char *key,
Raef Coles01c71a12022-08-31 15:55:00 +0100600 size_t key_size, size_t *key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100601{
Raef Coles9b88ee52022-09-02 12:04:21 +0100602 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
603 {
Raef Coles01c71a12022-08-31 15:55:00 +0100604 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
605 }
606
Raef Colesf5632d32022-09-01 09:56:52 +0100607 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100608 {
609 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
610 }
611
612 unsigned_int_to_network_bytes(
Raef Colesf5632d32022-09-01 09:56:52 +0100613 ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100614 MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET );
Raef Coles366d67d2022-09-01 17:23:12 +0100615 unsigned_int_to_network_bytes( ctx->params.otstype, MBEDTLS_LMOTS_TYPE_LEN,
616 key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100617 memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100618 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100619 MBEDTLS_LMOTS_I_KEY_ID_LEN );
620 memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100621 ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100622 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100623
Raef Coles9b88ee52022-09-02 12:04:21 +0100624 if( key_len != NULL )
625 {
Raef Colese9479a02022-09-01 16:06:35 +0100626 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100627 }
628
629 return( 0 );
630}
631
632
633int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
634 int (*f_rng)(void *, unsigned char *, size_t),
635 void* p_rng, unsigned char *msg, unsigned int msg_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100636 unsigned char *sig, size_t sig_size, size_t *sig_len )
Raef Coles01c71a12022-08-31 15:55:00 +0100637{
638 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
640
Raef Colesf5632d32022-09-01 09:56:52 +0100641 if( ! ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100642 {
643 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
644 }
645
Raef Colese9479a02022-09-01 16:06:35 +0100646 if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles01c71a12022-08-31 15:55:00 +0100647 {
648 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
649 }
650
Raef Colesf5632d32022-09-01 09:56:52 +0100651 if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100652 {
653 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
654 }
655
Raef Colesf5632d32022-09-01 09:56:52 +0100656 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100657 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100658 {
659 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
660 }
661
Raef Colese9479a02022-09-01 16:06:35 +0100662 if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100663 {
Raef Coles01c71a12022-08-31 15:55:00 +0100664 return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
Raef Coles8ff6df52021-07-21 12:42:15 +0100665 }
666
667
Raef Colesf5632d32022-09-01 09:56:52 +0100668 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100669 /* This new value must _always_ be written back to the disk before the
670 * signature is returned.
671 */
Raef Colesf5632d32022-09-01 09:56:52 +0100672 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100673
Raef Colesf5632d32022-09-01 09:56:52 +0100674 ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
Raef Coles01c71a12022-08-31 15:55:00 +0100675 f_rng, p_rng, msg, msg_size,
676 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100677 MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype),
678 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100679 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100680 {
Raef Coles8ff6df52021-07-21 12:42:15 +0100681 return( ret );
682 }
683
Raef Coles366d67d2022-09-01 17:23:12 +0100684 unsigned_int_to_network_bytes( ctx->params.type, MBEDTLS_LMS_TYPE_LEN,
685 sig + MBEDTLS_LMS_SIG_TYPE_OFFSET(ctx->params.otstype) );
Raef Coles01c71a12022-08-31 15:55:00 +0100686 unsigned_int_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles9b88ee52022-09-02 12:04:21 +0100687 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100688
Raef Colese9479a02022-09-01 16:06:35 +0100689 ret = get_merkle_path( ctx,
Raef Coles366d67d2022-09-01 17:23:12 +0100690 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
691 sig + MBEDTLS_LMS_SIG_PATH_OFFSET(ctx->params.otstype) );
Raef Colese0a17612022-09-02 16:04:47 +0100692 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100693 {
694 return( ret );
695 }
696
Raef Coles9b88ee52022-09-02 12:04:21 +0100697 if( sig_len != NULL )
698 {
Raef Colese9479a02022-09-01 16:06:35 +0100699 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100700 }
701
702
Raef Coles8ff6df52021-07-21 12:42:15 +0100703 return( 0 );
704}
705
Raef Colesab4f8742022-09-01 12:24:31 +0100706#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100707#endif /* MBEDTLS_LMS_C */