blob: 46ea567f2f24ec45500c51fbc581b499da122812 [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
Raef Coles8ff6df52021-07-21 12:42:15 +010047#include "mbedtls/platform.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010048
Raef Coles57d53282022-09-27 11:30:51 +010049#define SIG_Q_LEAF_ID_OFFSET (0)
50#define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
51 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
52#define SIG_TYPE_OFFSET(otstype) (SIG_OTS_SIG_OFFSET + \
53 MBEDTLS_LMOTS_SIG_LEN(otstype))
54#define SIG_PATH_OFFSET(otstype) (SIG_TYPE_OFFSET(otstype) + \
55 MBEDTLS_LMS_TYPE_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010056
Raef Coles57d53282022-09-27 11:30:51 +010057#define PUBLIC_KEY_TYPE_OFFSET (0)
58#define PUBLIC_KEY_OTSTYPE_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
59 MBEDTLS_LMS_TYPE_LEN)
60#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_OTSTYPE_OFFSET + \
61 MBEDTLS_LMOTS_TYPE_LEN)
62#define PUBLIC_KEY_ROOT_NODE_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
63 MBEDTLS_LMOTS_I_KEY_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010064
65
Raef Colese9479a02022-09-01 16:06:35 +010066/* Currently only support H=10 */
Raef Coles57d53282022-09-27 11:30:51 +010067#define H_TREE_HEIGHT_MAX 10
68#define MERKLE_TREE_NODE_AM_MAX (1u << (H_TREE_HEIGHT_MAX + 1u))
Raef Colese9479a02022-09-01 16:06:35 +010069#define MERKLE_TREE_NODE_AM(type) (1u << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
70#define MERKLE_TREE_LEAF_NODE_AM(type) (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type))
71#define MERKLE_TREE_INTERNAL_NODE_AM(type) (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010072
73#define D_CONST_LEN (2)
Raef Coles01c71a12022-08-31 15:55:00 +010074static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = {0x82, 0x82};
Raef Coles0a967cc2022-09-02 17:46:15 +010075static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83};
Raef Coles8ff6df52021-07-21 12:42:15 +010076
Raef Coles0a967cc2022-09-02 17:46:15 +010077
Raef Coles285d44b2022-10-10 15:44:17 +010078/* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
Raef Coles0a967cc2022-09-02 17:46:15 +010079 * public key and some other parameters like the leaf index). This function
80 * implements RFC8554 section 5.3, in the case where r >= 2^h.
81 *
Raef Coles98d6e222022-09-23 09:04:04 +010082 * params The LMS parameter set, the underlying LMOTS
83 * parameter set, and I value which describe the key
84 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +010085 *
Raef Coles98d6e222022-09-23 09:04:04 +010086 * pub_key The public key of the private whose index
87 * corresponds to the index of this leaf node. This
88 * is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010089 *
Raef Coles285d44b2022-10-10 15:44:17 +010090 * r_node_idx The index of this node in the Merkle tree. Note
91 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +010092 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +010093 *
Raef Coles98d6e222022-09-23 09:04:04 +010094 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010095 */
Raef Colese9479a02022-09-01 16:06:35 +010096static int create_merkle_leaf_value( const mbedtls_lms_parameters_t *params,
97 unsigned char *pub_key,
Raef Colesebd35b52022-09-01 11:52:17 +010098 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +010099 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100100{
Raef Colesc8f96042022-08-25 13:49:54 +0100101 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100103 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100104 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100105
Raef Colesc8f96042022-08-25 13:49:54 +0100106 op = psa_hash_operation_init( );
107 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100108 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100109 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100110
Raef Colese9479a02022-09-01 16:06:35 +0100111 status = psa_hash_update( &op, params->I_key_identifier,
112 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100113 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100114 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100115
Raef Colesad054252022-09-27 10:59:16 +0100116 mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100117 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
Raef Coles29117d22022-10-07 11:46:06 +0100118 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100119 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100120
Raef Coles01c71a12022-08-31 15:55:00 +0100121 status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100122 if( status != PSA_SUCCESS )
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_update( &op, pub_key,
126 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype) );
Raef Coles29117d22022-10-07 11:46:06 +0100127 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100128 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100129
Raef Colese9479a02022-09-01 16:06:35 +0100130 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100131 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100132 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100133 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100134
Raef Coles01c71a12022-08-31 15:55:00 +0100135exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100136 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100137
Raef Coles29117d22022-10-07 11:46:06 +0100138 return ( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100139}
140
Raef Coles285d44b2022-10-10 15:44:17 +0100141/* Calculate the value of an internal node of the Merkle tree (which is a hash
Raef Coles0a967cc2022-09-02 17:46:15 +0100142 * of a public key and some other parameters like the node index). This function
143 * implements RFC8554 section 5.3, in the case where r < 2^h.
144 *
Raef Coles98d6e222022-09-23 09:04:04 +0100145 * params The LMS parameter set, the underlying LMOTS
146 * parameter set, and I value which describe the key
147 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100148 *
Raef Coles98d6e222022-09-23 09:04:04 +0100149 * left_node The value of the child of this node which is on
150 * the left-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100151 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100152 *
Raef Coles98d6e222022-09-23 09:04:04 +0100153 * right_node The value of the child of this node which is on
154 * the right-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100155 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100156 *
Raef Coles285d44b2022-10-10 15:44:17 +0100157 * r_node_idx The index of this node in the Merkle tree. Note
158 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100159 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100160 *
Raef Coles98d6e222022-09-23 09:04:04 +0100161 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100162 */
Raef Colese9479a02022-09-01 16:06:35 +0100163static int create_merkle_internal_value( const mbedtls_lms_parameters_t *params,
164 const unsigned char *left_node,
165 const unsigned char *right_node,
Raef Colesebd35b52022-09-01 11:52:17 +0100166 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +0100167 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100168{
Raef Colesc8f96042022-08-25 13:49:54 +0100169 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100171 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100172 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100173
Raef Colesc8f96042022-08-25 13:49:54 +0100174 op = psa_hash_operation_init( );
175 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100176 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100177 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100178
Raef Colese9479a02022-09-01 16:06:35 +0100179 status = psa_hash_update( &op, params->I_key_identifier,
180 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100181 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100182 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100183
Raef Colesad054252022-09-27 10:59:16 +0100184 mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100185 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
Raef Coles29117d22022-10-07 11:46:06 +0100186 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100187 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100188
Raef Colesfa24f9d2022-09-02 17:46:52 +0100189 status = psa_hash_update( &op, D_INTR_CONSTANT_BYTES, D_CONST_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100190 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100191 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100192
Raef Colese9479a02022-09-01 16:06:35 +0100193 status = psa_hash_update( &op, left_node,
194 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100195 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100196 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100197
Raef Colese9479a02022-09-01 16:06:35 +0100198 status = psa_hash_update( &op, right_node,
199 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100200 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100201 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100202
Raef Coles29117d22022-10-07 11:46:06 +0100203 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100204 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100205 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100206 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100207
Raef Coles01c71a12022-08-31 15:55:00 +0100208exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100209 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100210
Raef Coles29117d22022-10-07 11:46:06 +0100211 return( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100212}
213
Raef Colesbe3bdd82022-10-07 12:04:24 +0100214void mbedtls_lms_public_init( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100215{
Raef Colesfbd60ec2022-10-10 15:09:33 +0100216 memset( ctx, 0, sizeof( *ctx ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100217}
218
Raef Colesbe3bdd82022-10-07 12:04:24 +0100219void mbedtls_lms_public_free( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100220{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100221 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100222}
223
Raef Coles01c71a12022-08-31 15:55:00 +0100224int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
225 const unsigned char *key, size_t key_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100226{
Raef Coles01c71a12022-08-31 15:55:00 +0100227 mbedtls_lms_algorithm_type_t type;
228 mbedtls_lmots_algorithm_type_t otstype;
229
Raef Colesad054252022-09-27 10:59:16 +0100230 type = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100231 key + PUBLIC_KEY_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100232 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100233 {
234 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
235 }
Raef Colesf5632d32022-09-01 09:56:52 +0100236 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100237
Raef Colese89488d2022-10-07 16:06:35 +0100238 if( key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
239 {
240 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
241 }
242
Raef Colesad054252022-09-27 10:59:16 +0100243 otstype = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100244 key + PUBLIC_KEY_OTSTYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100245 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
246 {
247 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
248 }
Raef Colesf5632d32022-09-01 09:56:52 +0100249 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100250
Raef Colesf5632d32022-09-01 09:56:52 +0100251 memcpy( ctx->params.I_key_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100252 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Coles01c71a12022-08-31 15:55:00 +0100253 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles57d53282022-09-27 11:30:51 +0100254 memcpy( ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100255 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100256
Raef Colesf5632d32022-09-01 09:56:52 +0100257 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100258
259 return( 0 );
260}
261
Raef Coles370cc432022-10-07 16:07:33 +0100262int mbedtls_lms_export_public_key( const mbedtls_lms_public_t *ctx,
263 unsigned char *key,
264 size_t key_size, size_t *key_len )
265{
266 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
267 {
268 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
269 }
270
271 if( ! ctx->have_public_key )
272 {
273 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
274 }
275
276 mbedtls_lms_unsigned_int_to_network_bytes(
277 ctx->params.type,
278 MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET );
279 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.otstype,
280 MBEDTLS_LMOTS_TYPE_LEN,
281 key + PUBLIC_KEY_OTSTYPE_OFFSET );
282 memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
283 ctx->params.I_key_identifier,
284 MBEDTLS_LMOTS_I_KEY_ID_LEN );
285 memcpy( key +PUBLIC_KEY_ROOT_NODE_OFFSET,
286 ctx->T_1_pub_key,
287 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
288
289 if( key_len != NULL )
290 {
291 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
292 }
293
294 return( 0 );
295}
296
Raef Coles01c71a12022-08-31 15:55:00 +0100297int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
298 const unsigned char *msg, size_t msg_size,
299 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100300{
301 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100302 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
303 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100304 unsigned int height;
305 unsigned int curr_node_id;
306 unsigned int parent_node_id;
307 const unsigned char* left_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100308 const unsigned char* right_node;
309 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
311
Raef Colesf5632d32022-09-01 09:56:52 +0100312 if( ! ctx->have_public_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100313 {
314 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
315 }
316
Raef Colesf5632d32022-09-01 09:56:52 +0100317 if( ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100318 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100319 {
320 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
321 }
322
Raef Colesf5632d32022-09-01 09:56:52 +0100323 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100324 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100325 {
326 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
327 }
328
Raef Colesfaf59ba2022-10-10 15:40:56 +0100329 if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
330 {
331 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
332 }
333
334 if( sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN )
335 {
336 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
337 }
338
Raef Colesad054252022-09-27 10:59:16 +0100339 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100340 sig + SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET )
Raef Colese9479a02022-09-01 16:06:35 +0100341 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100342 {
343 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
344 }
345
Raef Colesfaf59ba2022-10-10 15:40:56 +0100346 if( sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN )
347 {
348 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
349 }
350
Raef Colesad054252022-09-27 10:59:16 +0100351 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100352 sig + SIG_TYPE_OFFSET(ctx->params.otstype))
Raef Colese9479a02022-09-01 16:06:35 +0100353 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100354 {
355 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
356 }
357
358
Raef Colesad054252022-09-27 10:59:16 +0100359 q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
Raef Coles57d53282022-09-27 11:30:51 +0100360 MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100361
Raef Colese9479a02022-09-01 16:06:35 +0100362 if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100363 {
364 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
365 }
366
Raef Coles9b88ee52022-09-02 12:04:21 +0100367 memcpy( ots_params.I_key_identifier,
368 ctx->params.I_key_identifier,
369 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesad054252022-09-27 10:59:16 +0100370 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
371 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
372 ots_params.q_leaf_identifier );
Raef Colesf5632d32022-09-01 09:56:52 +0100373 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100374
Raef Coles366d67d2022-09-01 17:23:12 +0100375 ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg,
Raef Coles57d53282022-09-27 11:30:51 +0100376 msg_size, sig + SIG_OTS_SIG_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100377 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), Kc_candidate_ots_pub_key,
Raef Coles9b88ee52022-09-02 12:04:21 +0100378 sizeof( Kc_candidate_ots_pub_key ), NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100379 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100380 {
Raef Colesfaf59ba2022-10-10 15:40:56 +0100381 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
Raef Coles8ff6df52021-07-21 12:42:15 +0100382 }
383
Raef Colesebd35b52022-09-01 11:52:17 +0100384 create_merkle_leaf_value(
Raef Colese9479a02022-09-01 16:06:35 +0100385 &ctx->params,
386 Kc_candidate_ots_pub_key,
387 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100388 Tc_candidate_root_node );
Raef Coles8ff6df52021-07-21 12:42:15 +0100389
Raef Coles366d67d2022-09-01 17:23:12 +0100390 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
391 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100392
Raef Colese9479a02022-09-01 16:06:35 +0100393 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
394 height++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100395 {
396 parent_node_id = curr_node_id / 2;
397
398 /* Left/right node ordering matters for the hash */
399 if( curr_node_id & 1 )
400 {
Raef Coles57d53282022-09-27 11:30:51 +0100401 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100402 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100403 right_node = Tc_candidate_root_node;
Raef Coles8ff6df52021-07-21 12:42:15 +0100404 }
405 else
406 {
407 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100408 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100409 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100410 }
411
Raef Colese9479a02022-09-01 16:06:35 +0100412 create_merkle_internal_value( &ctx->params, left_node, right_node,
413 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100414
415 curr_node_id /= 2;
416 }
417
Raef Colesf5632d32022-09-01 09:56:52 +0100418 if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100419 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100420 {
421 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
422 }
423
424 return( 0 );
425}
426
Raef Coles5127e852022-10-07 10:35:56 +0100427#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100428
Raef Coles285d44b2022-10-10 15:44:17 +0100429/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100430 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100431 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100432 *
Raef Coles98d6e222022-09-23 09:04:04 +0100433 * ctx The LMS private context, containing a parameter
434 * set and private key material consisting of both
435 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100436 *
Raef Coles98d6e222022-09-23 09:04:04 +0100437 * tree The output tree, which is 2^(H + 1) hash outputs.
438 * In the case of H=10 we have 2048 tree nodes (of
439 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100440 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100441 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100442 */
Raef Coles2ac352a2022-10-07 11:12:27 +0100443static int calculate_merkle_tree( const mbedtls_lms_private_t *ctx,
Raef Colese9479a02022-09-01 16:06:35 +0100444 unsigned char *tree )
Raef Colesab4f8742022-09-01 12:24:31 +0100445{
446 unsigned int priv_key_idx;
447 unsigned int r_node_idx;
448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
449
450 /* First create the leaf nodes, in ascending order */
Raef Colese9479a02022-09-01 16:06:35 +0100451 for( priv_key_idx = 0;
452 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100453 priv_key_idx++ )
454 {
Raef Colese9479a02022-09-01 16:06:35 +0100455 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100456
Raef Colese9479a02022-09-01 16:06:35 +0100457 ret = create_merkle_leaf_value( &ctx->params,
458 ctx->ots_public_keys[priv_key_idx].public_key, r_node_idx,
459 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100460 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100461 {
462 return( ret );
463 }
464 }
465
466 /* Then the internal nodes, in reverse order so that we can guarantee the
467 * parent has been created */
Raef Colese9479a02022-09-01 16:06:35 +0100468 for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
469 r_node_idx > 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100470 r_node_idx-- )
471 {
Raef Colese9479a02022-09-01 16:06:35 +0100472 ret = create_merkle_internal_value( &ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100473 &tree[( r_node_idx * 2 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
474 &tree[( r_node_idx * 2 + 1 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
Raef Coles366d67d2022-09-01 17:23:12 +0100475 r_node_idx,
476 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100477 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100478 {
479 return( ret );
480 }
481 }
482
483 return( 0 );
484}
485
Raef Coles285d44b2022-10-10 15:44:17 +0100486/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100487 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100488 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100489 *
Raef Coles98d6e222022-09-23 09:04:04 +0100490 * ctx The LMS private context, containing a parameter
491 * set and private key material consisting of both
492 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100493 *
Raef Coles98d6e222022-09-23 09:04:04 +0100494 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100495 *
Raef Coles75b4c772022-10-10 13:58:28 +0100496 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100497 */
Raef Colesab4f8742022-09-01 12:24:31 +0100498static int get_merkle_path( mbedtls_lms_private_t *ctx,
499 unsigned int leaf_node_id,
Raef Colese9479a02022-09-01 16:06:35 +0100500 unsigned char *path )
Raef Colesab4f8742022-09-01 12:24:31 +0100501{
Raef Colese9479a02022-09-01 16:06:35 +0100502 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Colesab4f8742022-09-01 12:24:31 +0100503 unsigned int curr_node_id = leaf_node_id;
504 unsigned int adjacent_node_id;
505 unsigned int height;
506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
507
Raef Coles9b88ee52022-09-02 12:04:21 +0100508 ret = calculate_merkle_tree( ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100509 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100510 {
Raef Coles142e5772022-10-12 10:47:27 +0100511 goto exit;
Raef Colesab4f8742022-09-01 12:24:31 +0100512 }
513
Raef Colese9479a02022-09-01 16:06:35 +0100514 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
515 height++ )
Raef Colesab4f8742022-09-01 12:24:31 +0100516 {
517 adjacent_node_id = curr_node_id ^ 1;
518
Raef Colese9479a02022-09-01 16:06:35 +0100519 memcpy( &path[height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
520 &tree[adjacent_node_id],
521 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Colesab4f8742022-09-01 12:24:31 +0100522
523 curr_node_id >>=1;
524 }
525
Raef Coles142e5772022-10-12 10:47:27 +0100526 ret = 0;
527
528exit:
529 mbedtls_platform_zeroize( tree, sizeof( tree ) );
530
531 return( ret );
Raef Colesab4f8742022-09-01 12:24:31 +0100532}
533
Raef Colesbe3bdd82022-10-07 12:04:24 +0100534void mbedtls_lms_private_init( mbedtls_lms_private_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100535{
Raef Colesfbd60ec2022-10-10 15:09:33 +0100536 memset( ctx, 0, sizeof( *ctx ) ) ;
Raef Coles01c71a12022-08-31 15:55:00 +0100537}
Raef Coles8ff6df52021-07-21 12:42:15 +0100538
Raef Colesbe3bdd82022-10-07 12:04:24 +0100539void mbedtls_lms_private_free( mbedtls_lms_private_t *ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100540{
541 unsigned int idx;
542
Raef Colesf5632d32022-09-01 09:56:52 +0100543 if( ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100544 {
Raef Colescbd02ad2022-10-13 14:11:49 +0100545 if( ctx->ots_private_keys != NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100546 {
Raef Colescbd02ad2022-10-13 14:11:49 +0100547 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
548 {
549 mbedtls_lmots_private_free( &ctx->ots_private_keys[idx] );
550 }
Raef Coles01c71a12022-08-31 15:55:00 +0100551 }
552
Raef Colesdc8fb792022-10-07 13:27:54 +0100553 if( ctx->ots_public_keys != NULL )
Raef Colescbd02ad2022-10-13 14:11:49 +0100554 {
555 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
556 {
557 mbedtls_lmots_public_free( &ctx->ots_public_keys[idx] );
558 }
559 }
560
561 mbedtls_free( ctx->ots_private_keys );
562 mbedtls_free( ctx->ots_public_keys );
Raef Coles8ff6df52021-07-21 12:42:15 +0100563 }
564
Raef Colesbe3bdd82022-10-07 12:04:24 +0100565 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100566}
Raef Coles8ff6df52021-07-21 12:42:15 +0100567
Raef Coles01c71a12022-08-31 15:55:00 +0100568
569int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
570 mbedtls_lms_algorithm_type_t type,
571 mbedtls_lmots_algorithm_type_t otstype,
572 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100573 void* p_rng, const unsigned char *seed,
Raef Coles01c71a12022-08-31 15:55:00 +0100574 size_t seed_size )
575{
576 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
578
Raef Coles8ff6df52021-07-21 12:42:15 +0100579 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
580 {
581 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
582 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100583
Raef Coles8ff6df52021-07-21 12:42:15 +0100584 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
585 {
586 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
587 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100588
Raef Colesf5632d32022-09-01 09:56:52 +0100589 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100590 {
591 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
592 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100593
Raef Colesf5632d32022-09-01 09:56:52 +0100594 ctx->params.type = type;
595 ctx->params.otstype = otstype;
Raef Colescbd02ad2022-10-13 14:11:49 +0100596 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100597
Raef Coles3f6cdd72022-10-07 14:07:59 +0100598 ret = f_rng( p_rng,
599 ctx->params.I_key_identifier,
600 MBEDTLS_LMOTS_I_KEY_ID_LEN );
601 if( ret != 0 )
602 {
603 goto exit;
604 }
Raef Coles01c71a12022-08-31 15:55:00 +0100605
Raef Coles45c4ff92022-10-12 15:22:48 +0100606 /* Requires a cast to size_t to avoid an implicit cast warning on certain
607 * platforms (particularly Windows) */
608 ctx->ots_private_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100609 sizeof( *ctx->ots_private_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100610 if( ctx->ots_private_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100611 {
612 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
613 goto exit;
614 }
615
Raef Coles45c4ff92022-10-12 15:22:48 +0100616 /* Requires a cast to size_t to avoid an implicit cast warning on certain
617 * platforms (particularly Windows) */
618 ctx->ots_public_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100619 sizeof( *ctx->ots_public_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100620 if( ctx->ots_public_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100621 {
622 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
623 goto exit;
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 Colesbe3bdd82022-10-07 12:04:24 +0100628 mbedtls_lmots_private_init( &ctx->ots_private_keys[idx] );
629 mbedtls_lmots_public_init( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100630 }
631
632
Raef Colese9479a02022-09-01 16:06:35 +0100633 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100634 {
Raef Colesf5632d32022-09-01 09:56:52 +0100635 ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100636 otstype,
Raef Colesf5632d32022-09-01 09:56:52 +0100637 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100638 idx, seed, seed_size );
Raef Colese0a17612022-09-02 16:04:47 +0100639 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100640 goto exit;
641
Raef Colesf5632d32022-09-01 09:56:52 +0100642 ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
643 &ctx->ots_private_keys[idx] );
Raef Colese0a17612022-09-02 16:04:47 +0100644 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100645 goto exit;
646 }
647
Raef Colesf5632d32022-09-01 09:56:52 +0100648 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100649
650exit:
Raef Colese0a17612022-09-02 16:04:47 +0100651 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100652 {
Raef Colesdc8fb792022-10-07 13:27:54 +0100653 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100654 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100655
Raef Colesdc8fb792022-10-07 13:27:54 +0100656 return( ret );
Raef Coles8ff6df52021-07-21 12:42:15 +0100657}
658
Raef Coles01c71a12022-08-31 15:55:00 +0100659int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
Raef Coles2ac352a2022-10-07 11:12:27 +0100660 const mbedtls_lms_private_t *priv_ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100661{
Raef Colese9479a02022-09-01 16:06:35 +0100662 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100663 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
664
Raef Coles9b88ee52022-09-02 12:04:21 +0100665 if( ! priv_ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100666 {
667 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
668 }
669
Raef Colesf5632d32022-09-01 09:56:52 +0100670 if( priv_ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100671 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100672 {
673 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
674 }
675
Raef Colesf5632d32022-09-01 09:56:52 +0100676 if( priv_ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100677 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100678 {
679 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
680 }
681
Raef Colesf5632d32022-09-01 09:56:52 +0100682 memcpy( &ctx->params, &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100683 sizeof( mbedtls_lmots_parameters_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100684
Raef Coles9b88ee52022-09-02 12:04:21 +0100685 ret = calculate_merkle_tree( priv_ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100686 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100687 {
Raef Coles142e5772022-10-12 10:47:27 +0100688 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100689 }
690
691 /* Root node is always at position 1, due to 1-based indexing */
Raef Colese9479a02022-09-01 16:06:35 +0100692 memcpy( ctx->T_1_pub_key, &tree[1],
Raef Coles9b88ee52022-09-02 12:04:21 +0100693 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100694
Raef Colesf5632d32022-09-01 09:56:52 +0100695 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100696
Raef Coles142e5772022-10-12 10:47:27 +0100697 ret = 0;
698
699exit:
700 mbedtls_platform_zeroize( tree, sizeof( tree ) );
701
702 return( ret );
Raef Coles8ff6df52021-07-21 12:42:15 +0100703}
704
Raef Coles01c71a12022-08-31 15:55:00 +0100705
Raef Coles01c71a12022-08-31 15:55:00 +0100706int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
707 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100708 void* p_rng, const unsigned char *msg,
709 unsigned int msg_size, unsigned char *sig, size_t sig_size,
710 size_t *sig_len )
Raef Coles01c71a12022-08-31 15:55:00 +0100711{
712 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100713 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
714
Raef Colesf5632d32022-09-01 09:56:52 +0100715 if( ! ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100716 {
717 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
718 }
719
Raef Colese9479a02022-09-01 16:06:35 +0100720 if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles01c71a12022-08-31 15:55:00 +0100721 {
722 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
723 }
724
Raef Colesf5632d32022-09-01 09:56:52 +0100725 if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100726 {
727 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
728 }
729
Raef Colesf5632d32022-09-01 09:56:52 +0100730 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100731 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100732 {
733 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
734 }
735
Raef Colese9479a02022-09-01 16:06:35 +0100736 if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100737 {
Raef Coles01c71a12022-08-31 15:55:00 +0100738 return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
Raef Coles8ff6df52021-07-21 12:42:15 +0100739 }
740
741
Raef Colesf5632d32022-09-01 09:56:52 +0100742 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100743 /* This new value must _always_ be written back to the disk before the
744 * signature is returned.
745 */
Raef Colesf5632d32022-09-01 09:56:52 +0100746 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100747
Raef Coles1fb2f322022-10-10 11:23:07 +0100748 if ( MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
749 < SIG_OTS_SIG_OFFSET )
750 {
Raef Colesf6cb5a42022-10-10 14:15:53 +0100751 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles1fb2f322022-10-10 11:23:07 +0100752 }
753
Raef Colesf5632d32022-09-01 09:56:52 +0100754 ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
Raef Coles01c71a12022-08-31 15:55:00 +0100755 f_rng, p_rng, msg, msg_size,
Raef Coles57d53282022-09-27 11:30:51 +0100756 sig + SIG_OTS_SIG_OFFSET,
Raef Coles02cf8232022-10-07 13:52:47 +0100757 MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100758 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100759 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100760 {
Raef Coles8ff6df52021-07-21 12:42:15 +0100761 return( ret );
762 }
763
Raef Colesad054252022-09-27 10:59:16 +0100764 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
765 MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100766 sig + SIG_TYPE_OFFSET(ctx->params.otstype) );
Raef Colesad054252022-09-27 10:59:16 +0100767 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
768 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100769 sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100770
Raef Colese9479a02022-09-01 16:06:35 +0100771 ret = get_merkle_path( ctx,
Raef Coles366d67d2022-09-01 17:23:12 +0100772 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100773 sig + SIG_PATH_OFFSET(ctx->params.otstype) );
Raef Colese0a17612022-09-02 16:04:47 +0100774 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100775 {
776 return( ret );
777 }
778
Raef Coles9b88ee52022-09-02 12:04:21 +0100779 if( sig_len != NULL )
780 {
Raef Colese9479a02022-09-01 16:06:35 +0100781 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100782 }
783
784
Raef Coles8ff6df52021-07-21 12:42:15 +0100785 return( 0 );
786}
787
Raef Coles5127e852022-10-07 10:35:56 +0100788#endif /* defined(MBEDTLS_LMS_PRIVATE) */
789#endif /* defined(MBEDTLS_LMS_C) */