blob: 78c7d2666336f1c6d32da868d4b13d468fbcef50 [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
Moritz Fischera6a94ad2022-11-12 08:28:04 -080068#define MERKLE_TREE_NODE_AM(type) ((size_t) 1 << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
69#define MERKLE_TREE_LEAF_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
70#define MERKLE_TREE_INTERNAL_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010071
72#define D_CONST_LEN (2)
Raef Coles01c71a12022-08-31 15:55:00 +010073static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = {0x82, 0x82};
Raef Coles0a967cc2022-09-02 17:46:15 +010074static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83};
Raef Coles8ff6df52021-07-21 12:42:15 +010075
Raef Coles0a967cc2022-09-02 17:46:15 +010076
Raef Coles285d44b2022-10-10 15:44:17 +010077/* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
Raef Coles0a967cc2022-09-02 17:46:15 +010078 * public key and some other parameters like the leaf index). This function
79 * implements RFC8554 section 5.3, in the case where r >= 2^h.
80 *
Raef Coles98d6e222022-09-23 09:04:04 +010081 * params The LMS parameter set, the underlying LMOTS
82 * parameter set, and I value which describe the key
83 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +010084 *
Raef Coles98d6e222022-09-23 09:04:04 +010085 * pub_key The public key of the private whose index
86 * corresponds to the index of this leaf node. This
87 * is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010088 *
Raef Coles285d44b2022-10-10 15:44:17 +010089 * r_node_idx The index of this node in the Merkle tree. Note
90 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +010091 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +010092 *
Raef Coles98d6e222022-09-23 09:04:04 +010093 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010094 */
Raef Colese9479a02022-09-01 16:06:35 +010095static int create_merkle_leaf_value( const mbedtls_lms_parameters_t *params,
96 unsigned char *pub_key,
Raef Colesebd35b52022-09-01 11:52:17 +010097 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +010098 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +010099{
Raef Colesc8f96042022-08-25 13:49:54 +0100100 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100102 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100103 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100104
Raef Colesc8f96042022-08-25 13:49:54 +0100105 op = psa_hash_operation_init( );
106 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100107 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100108 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100109
Raef Colese9479a02022-09-01 16:06:35 +0100110 status = psa_hash_update( &op, params->I_key_identifier,
111 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100112 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100113 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100114
Raef Colesad054252022-09-27 10:59:16 +0100115 mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100116 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
Raef Coles29117d22022-10-07 11:46:06 +0100117 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100118 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100119
Raef Coles01c71a12022-08-31 15:55:00 +0100120 status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_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 Colese9479a02022-09-01 16:06:35 +0100124 status = psa_hash_update( &op, pub_key,
125 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype) );
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 Colese9479a02022-09-01 16:06:35 +0100129 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100130 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100131 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100132 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100133
Raef Coles01c71a12022-08-31 15:55:00 +0100134exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100135 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100136
Raef Coles29117d22022-10-07 11:46:06 +0100137 return ( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100138}
139
Raef Coles285d44b2022-10-10 15:44:17 +0100140/* Calculate the value of an internal node of the Merkle tree (which is a hash
Raef Coles0a967cc2022-09-02 17:46:15 +0100141 * of a public key and some other parameters like the node index). This function
142 * implements RFC8554 section 5.3, in the case where r < 2^h.
143 *
Raef Coles98d6e222022-09-23 09:04:04 +0100144 * params The LMS parameter set, the underlying LMOTS
145 * parameter set, and I value which describe the key
146 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100147 *
Raef Coles98d6e222022-09-23 09:04:04 +0100148 * left_node The value of the child of this node which is on
149 * the left-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100150 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100151 *
Raef Coles98d6e222022-09-23 09:04:04 +0100152 * right_node The value of the child of this node which is on
153 * the right-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100154 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100155 *
Raef Coles285d44b2022-10-10 15:44:17 +0100156 * r_node_idx The index of this node in the Merkle tree. Note
157 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100158 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100159 *
Raef Coles98d6e222022-09-23 09:04:04 +0100160 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100161 */
Raef Colese9479a02022-09-01 16:06:35 +0100162static int create_merkle_internal_value( const mbedtls_lms_parameters_t *params,
163 const unsigned char *left_node,
164 const unsigned char *right_node,
Raef Colesebd35b52022-09-01 11:52:17 +0100165 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +0100166 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100167{
Raef Colesc8f96042022-08-25 13:49:54 +0100168 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100170 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100171 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100172
Raef Colesc8f96042022-08-25 13:49:54 +0100173 op = psa_hash_operation_init( );
174 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100175 if( status != PSA_SUCCESS )
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, params->I_key_identifier,
179 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100180 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100181 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100182
Raef Colesad054252022-09-27 10:59:16 +0100183 mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100184 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
Raef Coles29117d22022-10-07 11:46:06 +0100185 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100186 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100187
Raef Colesfa24f9d2022-09-02 17:46:52 +0100188 status = psa_hash_update( &op, D_INTR_CONSTANT_BYTES, D_CONST_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 Colese9479a02022-09-01 16:06:35 +0100192 status = psa_hash_update( &op, left_node,
193 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
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 Colese9479a02022-09-01 16:06:35 +0100197 status = psa_hash_update( &op, right_node,
198 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100199 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100200 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100201
Raef Coles29117d22022-10-07 11:46:06 +0100202 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100203 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100204 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100205 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100206
Raef Coles01c71a12022-08-31 15:55:00 +0100207exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100208 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100209
Raef Coles29117d22022-10-07 11:46:06 +0100210 return( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100211}
212
Raef Colesbe3bdd82022-10-07 12:04:24 +0100213void mbedtls_lms_public_init( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100214{
Raef Colesfbd60ec2022-10-10 15:09:33 +0100215 memset( ctx, 0, sizeof( *ctx ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100216}
217
Raef Colesbe3bdd82022-10-07 12:04:24 +0100218void mbedtls_lms_public_free( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100219{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100220 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100221}
222
Raef Coles01c71a12022-08-31 15:55:00 +0100223int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
224 const unsigned char *key, size_t key_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100225{
Raef Coles01c71a12022-08-31 15:55:00 +0100226 mbedtls_lms_algorithm_type_t type;
227 mbedtls_lmots_algorithm_type_t otstype;
228
Raef Colesad054252022-09-27 10:59:16 +0100229 type = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100230 key + PUBLIC_KEY_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100231 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100232 {
233 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
234 }
Raef Colesf5632d32022-09-01 09:56:52 +0100235 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100236
Raef Colese89488d2022-10-07 16:06:35 +0100237 if( key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
238 {
239 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
240 }
241
Raef Colesad054252022-09-27 10:59:16 +0100242 otstype = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100243 key + PUBLIC_KEY_OTSTYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100244 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
245 {
246 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
247 }
Raef Colesf5632d32022-09-01 09:56:52 +0100248 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100249
Raef Colesf5632d32022-09-01 09:56:52 +0100250 memcpy( ctx->params.I_key_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100251 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Coles01c71a12022-08-31 15:55:00 +0100252 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles57d53282022-09-27 11:30:51 +0100253 memcpy( ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100254 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100255
Raef Colesf5632d32022-09-01 09:56:52 +0100256 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100257
258 return( 0 );
259}
260
Raef Coles370cc432022-10-07 16:07:33 +0100261int mbedtls_lms_export_public_key( const mbedtls_lms_public_t *ctx,
262 unsigned char *key,
263 size_t key_size, size_t *key_len )
264{
265 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
266 {
267 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
268 }
269
270 if( ! ctx->have_public_key )
271 {
272 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
273 }
274
275 mbedtls_lms_unsigned_int_to_network_bytes(
276 ctx->params.type,
277 MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET );
278 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.otstype,
279 MBEDTLS_LMOTS_TYPE_LEN,
280 key + PUBLIC_KEY_OTSTYPE_OFFSET );
281 memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
282 ctx->params.I_key_identifier,
283 MBEDTLS_LMOTS_I_KEY_ID_LEN );
284 memcpy( key +PUBLIC_KEY_ROOT_NODE_OFFSET,
285 ctx->T_1_pub_key,
286 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
287
288 if( key_len != NULL )
289 {
290 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
291 }
292
293 return( 0 );
294}
295
Raef Coles01c71a12022-08-31 15:55:00 +0100296int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
297 const unsigned char *msg, size_t msg_size,
298 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100299{
300 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100301 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
302 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100303 unsigned int height;
304 unsigned int curr_node_id;
305 unsigned int parent_node_id;
306 const unsigned char* left_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100307 const unsigned char* right_node;
308 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
310
Raef Colesf5632d32022-09-01 09:56:52 +0100311 if( ! ctx->have_public_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100312 {
313 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
314 }
315
Raef Colesf5632d32022-09-01 09:56:52 +0100316 if( ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100317 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100318 {
319 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
320 }
321
Raef Colesf5632d32022-09-01 09:56:52 +0100322 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100323 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100324 {
325 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
326 }
327
Raef Colesfaf59ba2022-10-10 15:40:56 +0100328 if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
329 {
330 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
331 }
332
333 if( sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN )
334 {
335 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
336 }
337
Raef Colesad054252022-09-27 10:59:16 +0100338 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100339 sig + SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET )
Raef Colese9479a02022-09-01 16:06:35 +0100340 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100341 {
342 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
343 }
344
Raef Colesfaf59ba2022-10-10 15:40:56 +0100345 if( sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN )
346 {
347 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
348 }
349
Raef Colesad054252022-09-27 10:59:16 +0100350 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100351 sig + SIG_TYPE_OFFSET(ctx->params.otstype))
Raef Colese9479a02022-09-01 16:06:35 +0100352 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100353 {
354 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
355 }
356
357
Raef Colesad054252022-09-27 10:59:16 +0100358 q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
Raef Coles57d53282022-09-27 11:30:51 +0100359 MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100360
Raef Colese9479a02022-09-01 16:06:35 +0100361 if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100362 {
363 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
364 }
365
Raef Coles9b88ee52022-09-02 12:04:21 +0100366 memcpy( ots_params.I_key_identifier,
367 ctx->params.I_key_identifier,
368 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesad054252022-09-27 10:59:16 +0100369 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
370 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
371 ots_params.q_leaf_identifier );
Raef Colesf5632d32022-09-01 09:56:52 +0100372 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100373
Raef Coles366d67d2022-09-01 17:23:12 +0100374 ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg,
Raef Coles57d53282022-09-27 11:30:51 +0100375 msg_size, sig + SIG_OTS_SIG_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100376 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), Kc_candidate_ots_pub_key,
Raef Coles9b88ee52022-09-02 12:04:21 +0100377 sizeof( Kc_candidate_ots_pub_key ), NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100378 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100379 {
Raef Colesfaf59ba2022-10-10 15:40:56 +0100380 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
Raef Coles8ff6df52021-07-21 12:42:15 +0100381 }
382
Raef Colesebd35b52022-09-01 11:52:17 +0100383 create_merkle_leaf_value(
Raef Colese9479a02022-09-01 16:06:35 +0100384 &ctx->params,
385 Kc_candidate_ots_pub_key,
386 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100387 Tc_candidate_root_node );
Raef Coles8ff6df52021-07-21 12:42:15 +0100388
Raef Coles366d67d2022-09-01 17:23:12 +0100389 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
390 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100391
Raef Colese9479a02022-09-01 16:06:35 +0100392 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
393 height++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100394 {
395 parent_node_id = curr_node_id / 2;
396
397 /* Left/right node ordering matters for the hash */
398 if( curr_node_id & 1 )
399 {
Raef Coles57d53282022-09-27 11:30:51 +0100400 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100401 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100402 right_node = Tc_candidate_root_node;
Raef Coles8ff6df52021-07-21 12:42:15 +0100403 }
404 else
405 {
406 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100407 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100408 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100409 }
410
Raef Colese9479a02022-09-01 16:06:35 +0100411 create_merkle_internal_value( &ctx->params, left_node, right_node,
412 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100413
414 curr_node_id /= 2;
415 }
416
Raef Colesf5632d32022-09-01 09:56:52 +0100417 if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100418 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100419 {
420 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
421 }
422
423 return( 0 );
424}
425
Raef Coles5127e852022-10-07 10:35:56 +0100426#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100427
Raef Coles285d44b2022-10-10 15:44:17 +0100428/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100429 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100430 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100431 *
Raef Coles98d6e222022-09-23 09:04:04 +0100432 * ctx The LMS private context, containing a parameter
433 * set and private key material consisting of both
434 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100435 *
Raef Coles98d6e222022-09-23 09:04:04 +0100436 * tree The output tree, which is 2^(H + 1) hash outputs.
437 * In the case of H=10 we have 2048 tree nodes (of
438 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100439 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100440 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100441 */
Raef Coles2ac352a2022-10-07 11:12:27 +0100442static int calculate_merkle_tree( const mbedtls_lms_private_t *ctx,
Raef Colese9479a02022-09-01 16:06:35 +0100443 unsigned char *tree )
Raef Colesab4f8742022-09-01 12:24:31 +0100444{
445 unsigned int priv_key_idx;
446 unsigned int r_node_idx;
447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
448
449 /* First create the leaf nodes, in ascending order */
Raef Colese9479a02022-09-01 16:06:35 +0100450 for( priv_key_idx = 0;
451 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100452 priv_key_idx++ )
453 {
Raef Colese9479a02022-09-01 16:06:35 +0100454 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100455
Raef Colese9479a02022-09-01 16:06:35 +0100456 ret = create_merkle_leaf_value( &ctx->params,
457 ctx->ots_public_keys[priv_key_idx].public_key, r_node_idx,
458 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100459 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100460 {
461 return( ret );
462 }
463 }
464
465 /* Then the internal nodes, in reverse order so that we can guarantee the
466 * parent has been created */
Raef Colese9479a02022-09-01 16:06:35 +0100467 for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
468 r_node_idx > 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100469 r_node_idx-- )
470 {
Raef Colese9479a02022-09-01 16:06:35 +0100471 ret = create_merkle_internal_value( &ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100472 &tree[( r_node_idx * 2 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
473 &tree[( r_node_idx * 2 + 1 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
Raef Coles366d67d2022-09-01 17:23:12 +0100474 r_node_idx,
475 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100476 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100477 {
478 return( ret );
479 }
480 }
481
482 return( 0 );
483}
484
Raef Coles285d44b2022-10-10 15:44:17 +0100485/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100486 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100487 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100488 *
Raef Coles98d6e222022-09-23 09:04:04 +0100489 * ctx The LMS private context, containing a parameter
490 * set and private key material consisting of both
491 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100492 *
Raef Coles98d6e222022-09-23 09:04:04 +0100493 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100494 *
Raef Coles75b4c772022-10-10 13:58:28 +0100495 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100496 */
Raef Colesab4f8742022-09-01 12:24:31 +0100497static int get_merkle_path( mbedtls_lms_private_t *ctx,
498 unsigned int leaf_node_id,
Raef Colese9479a02022-09-01 16:06:35 +0100499 unsigned char *path )
Raef Colesab4f8742022-09-01 12:24:31 +0100500{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800501 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100502 unsigned int curr_node_id = leaf_node_id;
503 unsigned int adjacent_node_id;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800504 unsigned char *tree = NULL;
Raef Colesab4f8742022-09-01 12:24:31 +0100505 unsigned int height;
506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
507
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800508 tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(ctx->params.type),
509 node_bytes );
510 if ( tree == NULL )
511 {
512 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
513 }
514
515 ret = calculate_merkle_tree( ctx, tree );
Raef Colese0a17612022-09-02 16:04:47 +0100516 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100517 {
Raef Coles142e5772022-10-12 10:47:27 +0100518 goto exit;
Raef Colesab4f8742022-09-01 12:24:31 +0100519 }
520
Raef Colese9479a02022-09-01 16:06:35 +0100521 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
522 height++ )
Raef Colesab4f8742022-09-01 12:24:31 +0100523 {
524 adjacent_node_id = curr_node_id ^ 1;
525
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800526 memcpy( &path[height * node_bytes],
527 &tree[adjacent_node_id * node_bytes], node_bytes );
Raef Colesab4f8742022-09-01 12:24:31 +0100528
529 curr_node_id >>=1;
530 }
531
Raef Coles142e5772022-10-12 10:47:27 +0100532 ret = 0;
533
534exit:
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800535 mbedtls_platform_zeroize( tree, node_bytes *
536 MERKLE_TREE_NODE_AM(ctx->params.type) );
537 mbedtls_free ( tree );
Raef Coles142e5772022-10-12 10:47:27 +0100538
539 return( ret );
Raef Colesab4f8742022-09-01 12:24:31 +0100540}
541
Raef Colesbe3bdd82022-10-07 12:04:24 +0100542void mbedtls_lms_private_init( mbedtls_lms_private_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100543{
Raef Colesfbd60ec2022-10-10 15:09:33 +0100544 memset( ctx, 0, sizeof( *ctx ) ) ;
Raef Coles01c71a12022-08-31 15:55:00 +0100545}
Raef Coles8ff6df52021-07-21 12:42:15 +0100546
Raef Colesbe3bdd82022-10-07 12:04:24 +0100547void mbedtls_lms_private_free( mbedtls_lms_private_t *ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100548{
549 unsigned int idx;
550
Raef Colesf5632d32022-09-01 09:56:52 +0100551 if( ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100552 {
Raef Colescbd02ad2022-10-13 14:11:49 +0100553 if( ctx->ots_private_keys != NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100554 {
Raef Colescbd02ad2022-10-13 14:11:49 +0100555 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
556 {
557 mbedtls_lmots_private_free( &ctx->ots_private_keys[idx] );
558 }
Raef Coles01c71a12022-08-31 15:55:00 +0100559 }
560
Raef Colesdc8fb792022-10-07 13:27:54 +0100561 if( ctx->ots_public_keys != NULL )
Raef Colescbd02ad2022-10-13 14:11:49 +0100562 {
563 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
564 {
565 mbedtls_lmots_public_free( &ctx->ots_public_keys[idx] );
566 }
567 }
568
569 mbedtls_free( ctx->ots_private_keys );
570 mbedtls_free( ctx->ots_public_keys );
Raef Coles8ff6df52021-07-21 12:42:15 +0100571 }
572
Raef Colesbe3bdd82022-10-07 12:04:24 +0100573 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100574}
Raef Coles8ff6df52021-07-21 12:42:15 +0100575
Raef Coles01c71a12022-08-31 15:55:00 +0100576
577int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
578 mbedtls_lms_algorithm_type_t type,
579 mbedtls_lmots_algorithm_type_t otstype,
580 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100581 void* p_rng, const unsigned char *seed,
Raef Coles01c71a12022-08-31 15:55:00 +0100582 size_t seed_size )
583{
584 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
586
Raef Coles8ff6df52021-07-21 12:42:15 +0100587 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
588 {
589 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
590 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100591
Raef Coles8ff6df52021-07-21 12:42:15 +0100592 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
593 {
594 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
595 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100596
Raef Colesf5632d32022-09-01 09:56:52 +0100597 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100598 {
599 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
600 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100601
Raef Colesf5632d32022-09-01 09:56:52 +0100602 ctx->params.type = type;
603 ctx->params.otstype = otstype;
Raef Colescbd02ad2022-10-13 14:11:49 +0100604 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100605
Raef Coles3f6cdd72022-10-07 14:07:59 +0100606 ret = f_rng( p_rng,
607 ctx->params.I_key_identifier,
608 MBEDTLS_LMOTS_I_KEY_ID_LEN );
609 if( ret != 0 )
610 {
611 goto exit;
612 }
Raef Coles01c71a12022-08-31 15:55:00 +0100613
Raef Coles45c4ff92022-10-12 15:22:48 +0100614 /* Requires a cast to size_t to avoid an implicit cast warning on certain
615 * platforms (particularly Windows) */
616 ctx->ots_private_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100617 sizeof( *ctx->ots_private_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100618 if( ctx->ots_private_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100619 {
620 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
621 goto exit;
622 }
623
Raef Coles45c4ff92022-10-12 15:22:48 +0100624 /* Requires a cast to size_t to avoid an implicit cast warning on certain
625 * platforms (particularly Windows) */
626 ctx->ots_public_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100627 sizeof( *ctx->ots_public_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100628 if( ctx->ots_public_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100629 {
630 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
631 goto exit;
632 }
633
Raef Colese9479a02022-09-01 16:06:35 +0100634 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100635 {
Raef Colesbe3bdd82022-10-07 12:04:24 +0100636 mbedtls_lmots_private_init( &ctx->ots_private_keys[idx] );
637 mbedtls_lmots_public_init( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100638 }
639
640
Raef Colese9479a02022-09-01 16:06:35 +0100641 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100642 {
Raef Colesf5632d32022-09-01 09:56:52 +0100643 ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100644 otstype,
Raef Colesf5632d32022-09-01 09:56:52 +0100645 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100646 idx, seed, seed_size );
Raef Colese0a17612022-09-02 16:04:47 +0100647 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100648 goto exit;
649
Raef Colesf5632d32022-09-01 09:56:52 +0100650 ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
651 &ctx->ots_private_keys[idx] );
Raef Colese0a17612022-09-02 16:04:47 +0100652 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100653 goto exit;
654 }
655
Raef Colesf5632d32022-09-01 09:56:52 +0100656 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100657
658exit:
Raef Colese0a17612022-09-02 16:04:47 +0100659 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100660 {
Raef Colesdc8fb792022-10-07 13:27:54 +0100661 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100662 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100663
Raef Colesdc8fb792022-10-07 13:27:54 +0100664 return( ret );
Raef Coles8ff6df52021-07-21 12:42:15 +0100665}
666
Raef Coles01c71a12022-08-31 15:55:00 +0100667int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
Raef Coles2ac352a2022-10-07 11:12:27 +0100668 const mbedtls_lms_private_t *priv_ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100669{
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800670 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800672 unsigned char *tree = NULL;
Raef Coles8ff6df52021-07-21 12:42:15 +0100673
Raef Coles9b88ee52022-09-02 12:04:21 +0100674 if( ! priv_ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100675 {
676 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
677 }
678
Raef Colesf5632d32022-09-01 09:56:52 +0100679 if( priv_ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100680 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100681 {
682 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
683 }
684
Raef Colesf5632d32022-09-01 09:56:52 +0100685 if( priv_ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100686 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100687 {
688 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
689 }
690
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800691 tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(priv_ctx->params.type),
692 node_bytes );
693 if ( tree == NULL )
694 {
695 return MBEDTLS_ERR_LMS_ALLOC_FAILED;
696 }
697
Raef Colesf5632d32022-09-01 09:56:52 +0100698 memcpy( &ctx->params, &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100699 sizeof( mbedtls_lmots_parameters_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100700
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800701 ret = calculate_merkle_tree( priv_ctx, tree );
Raef Colese0a17612022-09-02 16:04:47 +0100702 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100703 {
Raef Coles142e5772022-10-12 10:47:27 +0100704 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100705 }
706
707 /* Root node is always at position 1, due to 1-based indexing */
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800708 memcpy( ctx->T_1_pub_key, &tree[node_bytes], node_bytes );
Raef Coles8ff6df52021-07-21 12:42:15 +0100709
Raef Colesf5632d32022-09-01 09:56:52 +0100710 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100711
Raef Coles142e5772022-10-12 10:47:27 +0100712 ret = 0;
713
714exit:
Moritz Fischera6a94ad2022-11-12 08:28:04 -0800715 mbedtls_platform_zeroize( tree, node_bytes *
716 MERKLE_TREE_NODE_AM(priv_ctx->params.type) );
717 mbedtls_free ( tree );
Raef Coles142e5772022-10-12 10:47:27 +0100718
719 return( ret );
Raef Coles8ff6df52021-07-21 12:42:15 +0100720}
721
Raef Coles01c71a12022-08-31 15:55:00 +0100722
Raef Coles01c71a12022-08-31 15:55:00 +0100723int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
724 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100725 void* p_rng, const unsigned char *msg,
726 unsigned int msg_size, unsigned char *sig, size_t sig_size,
727 size_t *sig_len )
Raef Coles01c71a12022-08-31 15:55:00 +0100728{
729 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
731
Raef Colesf5632d32022-09-01 09:56:52 +0100732 if( ! ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100733 {
734 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
735 }
736
Raef Colese9479a02022-09-01 16:06:35 +0100737 if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles01c71a12022-08-31 15:55:00 +0100738 {
739 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
740 }
741
Raef Colesf5632d32022-09-01 09:56:52 +0100742 if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100743 {
744 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
745 }
746
Raef Colesf5632d32022-09-01 09:56:52 +0100747 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100748 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100749 {
750 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
751 }
752
Raef Colese9479a02022-09-01 16:06:35 +0100753 if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100754 {
Raef Coles01c71a12022-08-31 15:55:00 +0100755 return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
Raef Coles8ff6df52021-07-21 12:42:15 +0100756 }
757
758
Raef Colesf5632d32022-09-01 09:56:52 +0100759 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100760 /* This new value must _always_ be written back to the disk before the
761 * signature is returned.
762 */
Raef Colesf5632d32022-09-01 09:56:52 +0100763 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100764
Raef Coles1fb2f322022-10-10 11:23:07 +0100765 if ( MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
766 < SIG_OTS_SIG_OFFSET )
767 {
Raef Colesf6cb5a42022-10-10 14:15:53 +0100768 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles1fb2f322022-10-10 11:23:07 +0100769 }
770
Raef Colesf5632d32022-09-01 09:56:52 +0100771 ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
Raef Coles01c71a12022-08-31 15:55:00 +0100772 f_rng, p_rng, msg, msg_size,
Raef Coles57d53282022-09-27 11:30:51 +0100773 sig + SIG_OTS_SIG_OFFSET,
Raef Coles02cf8232022-10-07 13:52:47 +0100774 MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100775 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100776 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100777 {
Raef Coles8ff6df52021-07-21 12:42:15 +0100778 return( ret );
779 }
780
Raef Colesad054252022-09-27 10:59:16 +0100781 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
782 MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100783 sig + SIG_TYPE_OFFSET(ctx->params.otstype) );
Raef Colesad054252022-09-27 10:59:16 +0100784 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
785 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100786 sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100787
Raef Colese9479a02022-09-01 16:06:35 +0100788 ret = get_merkle_path( ctx,
Raef Coles366d67d2022-09-01 17:23:12 +0100789 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100790 sig + SIG_PATH_OFFSET(ctx->params.otstype) );
Raef Colese0a17612022-09-02 16:04:47 +0100791 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100792 {
793 return( ret );
794 }
795
Raef Coles9b88ee52022-09-02 12:04:21 +0100796 if( sig_len != NULL )
797 {
Raef Colese9479a02022-09-01 16:06:35 +0100798 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100799 }
800
801
Raef Coles8ff6df52021-07-21 12:42:15 +0100802 return( 0 );
803}
804
Raef Coles5127e852022-10-07 10:35:56 +0100805#endif /* defined(MBEDTLS_LMS_PRIVATE) */
806#endif /* defined(MBEDTLS_LMS_C) */