blob: f30f349ad55647ad43b7218d9e9f77c17420b436 [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/*
2 * The LMS stateful-hash public-key signature scheme
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/*
21 * The following sources were referenced in the design of this implementation
22 * of the LMS algorithm:
23 *
24 * [1] IETF RFC8554
25 * D. McGrew, M. Curcio, S.Fluhrer
26 * https://datatracker.ietf.org/doc/html/rfc8554
27 *
28 * [2] NIST Special Publication 800-208
29 * David A. Cooper et. al.
30 * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
31 */
32
33#include "common.h"
34
Raef Coles5127e852022-10-07 10:35:56 +010035#if defined(MBEDTLS_LMS_C)
Raef Coles8ff6df52021-07-21 12:42:15 +010036
37#include <string.h>
38
Raef Coles7dce69a2022-08-24 14:07:06 +010039#include "lmots.h"
40
Raef Colesc8f96042022-08-25 13:49:54 +010041#include "psa/crypto.h"
42
Raef Coles8ff6df52021-07-21 12:42:15 +010043#include "mbedtls/lms.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010044#include "mbedtls/error.h"
45#include "mbedtls/platform_util.h"
46
47#if defined(MBEDTLS_PLATFORM_C)
48#include "mbedtls/platform.h"
49#else
50#include <stdlib.h>
51#include <stdio.h>
52#define mbedtls_printf printf
53#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Raef Coles57d53282022-09-27 11:30:51 +010057#define SIG_Q_LEAF_ID_OFFSET (0)
58#define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
59 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
60#define SIG_TYPE_OFFSET(otstype) (SIG_OTS_SIG_OFFSET + \
61 MBEDTLS_LMOTS_SIG_LEN(otstype))
62#define SIG_PATH_OFFSET(otstype) (SIG_TYPE_OFFSET(otstype) + \
63 MBEDTLS_LMS_TYPE_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010064
Raef Coles57d53282022-09-27 11:30:51 +010065#define PUBLIC_KEY_TYPE_OFFSET (0)
66#define PUBLIC_KEY_OTSTYPE_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
67 MBEDTLS_LMS_TYPE_LEN)
68#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_OTSTYPE_OFFSET + \
69 MBEDTLS_LMOTS_TYPE_LEN)
70#define PUBLIC_KEY_ROOT_NODE_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
71 MBEDTLS_LMOTS_I_KEY_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010072
73
Raef Colese9479a02022-09-01 16:06:35 +010074/* Currently only support H=10 */
Raef Coles57d53282022-09-27 11:30:51 +010075#define H_TREE_HEIGHT_MAX 10
76#define MERKLE_TREE_NODE_AM_MAX (1u << (H_TREE_HEIGHT_MAX + 1u))
Raef Colese9479a02022-09-01 16:06:35 +010077#define MERKLE_TREE_NODE_AM(type) (1u << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
78#define MERKLE_TREE_LEAF_NODE_AM(type) (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type))
79#define MERKLE_TREE_INTERNAL_NODE_AM(type) (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010080
81#define D_CONST_LEN (2)
Raef Coles01c71a12022-08-31 15:55:00 +010082static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = {0x82, 0x82};
Raef Coles0a967cc2022-09-02 17:46:15 +010083static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83};
Raef Coles8ff6df52021-07-21 12:42:15 +010084
Raef Coles0a967cc2022-09-02 17:46:15 +010085
Raef Coles285d44b2022-10-10 15:44:17 +010086/* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
Raef Coles0a967cc2022-09-02 17:46:15 +010087 * public key and some other parameters like the leaf index). This function
88 * implements RFC8554 section 5.3, in the case where r >= 2^h.
89 *
Raef Coles98d6e222022-09-23 09:04:04 +010090 * params The LMS parameter set, the underlying LMOTS
91 * parameter set, and I value which describe the key
92 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +010093 *
Raef Coles98d6e222022-09-23 09:04:04 +010094 * pub_key The public key of the private whose index
95 * corresponds to the index of this leaf node. This
96 * is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +010097 *
Raef Coles285d44b2022-10-10 15:44:17 +010098 * r_node_idx The index of this node in the Merkle tree. Note
99 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100100 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100101 *
Raef Coles98d6e222022-09-23 09:04:04 +0100102 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100103 */
Raef Colese9479a02022-09-01 16:06:35 +0100104static int create_merkle_leaf_value( const mbedtls_lms_parameters_t *params,
105 unsigned char *pub_key,
Raef Colesebd35b52022-09-01 11:52:17 +0100106 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +0100107 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100108{
Raef Colesc8f96042022-08-25 13:49:54 +0100109 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100111 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100112 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100113
Raef Colesc8f96042022-08-25 13:49:54 +0100114 op = psa_hash_operation_init( );
115 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100116 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100117 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100118
Raef Colese9479a02022-09-01 16:06:35 +0100119 status = psa_hash_update( &op, params->I_key_identifier,
120 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100121 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100122 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100123
Raef Colesad054252022-09-27 10:59:16 +0100124 mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100125 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
Raef Coles29117d22022-10-07 11:46:06 +0100126 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100127 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100128
Raef Coles01c71a12022-08-31 15:55:00 +0100129 status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100130 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100131 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100132
Raef Colese9479a02022-09-01 16:06:35 +0100133 status = psa_hash_update( &op, pub_key,
134 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype) );
Raef Coles29117d22022-10-07 11:46:06 +0100135 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100136 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100137
Raef Colese9479a02022-09-01 16:06:35 +0100138 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100139 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100140 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100141 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100142
Raef Coles01c71a12022-08-31 15:55:00 +0100143exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100144 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100145
Raef Coles29117d22022-10-07 11:46:06 +0100146 return ( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100147}
148
Raef Coles285d44b2022-10-10 15:44:17 +0100149/* Calculate the value of an internal node of the Merkle tree (which is a hash
Raef Coles0a967cc2022-09-02 17:46:15 +0100150 * of a public key and some other parameters like the node index). This function
151 * implements RFC8554 section 5.3, in the case where r < 2^h.
152 *
Raef Coles98d6e222022-09-23 09:04:04 +0100153 * params The LMS parameter set, the underlying LMOTS
154 * parameter set, and I value which describe the key
155 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100156 *
Raef Coles98d6e222022-09-23 09:04:04 +0100157 * left_node The value of the child of this node which is on
158 * the left-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100159 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100160 *
Raef Coles98d6e222022-09-23 09:04:04 +0100161 * right_node The value of the child of this node which is on
162 * the right-hand side. As with all nodes on the
Raef Coles285d44b2022-10-10 15:44:17 +0100163 * Merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100164 *
Raef Coles285d44b2022-10-10 15:44:17 +0100165 * r_node_idx The index of this node in the Merkle tree. Note
166 * that the root node of the Merkle tree is
Raef Coles98d6e222022-09-23 09:04:04 +0100167 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100168 *
Raef Coles98d6e222022-09-23 09:04:04 +0100169 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100170 */
Raef Colese9479a02022-09-01 16:06:35 +0100171static int create_merkle_internal_value( const mbedtls_lms_parameters_t *params,
172 const unsigned char *left_node,
173 const unsigned char *right_node,
Raef Colesebd35b52022-09-01 11:52:17 +0100174 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +0100175 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100176{
Raef Colesc8f96042022-08-25 13:49:54 +0100177 psa_hash_operation_t op;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100178 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100179 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100180 unsigned char r_node_idx_bytes[4];
Raef Coles8ff6df52021-07-21 12:42:15 +0100181
Raef Colesc8f96042022-08-25 13:49:54 +0100182 op = psa_hash_operation_init( );
183 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100184 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100185 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100186
Raef Colese9479a02022-09-01 16:06:35 +0100187 status = psa_hash_update( &op, params->I_key_identifier,
188 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100189 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100190 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100191
Raef Colesad054252022-09-27 10:59:16 +0100192 mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100193 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
Raef Coles29117d22022-10-07 11:46:06 +0100194 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100195 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100196
Raef Colesfa24f9d2022-09-02 17:46:52 +0100197 status = psa_hash_update( &op, D_INTR_CONSTANT_BYTES, D_CONST_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100198 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100199 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100200
Raef Colese9479a02022-09-01 16:06:35 +0100201 status = psa_hash_update( &op, left_node,
202 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100203 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100204 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100205
Raef Colese9479a02022-09-01 16:06:35 +0100206 status = psa_hash_update( &op, right_node,
207 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100208 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100209 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100210
Raef Coles29117d22022-10-07 11:46:06 +0100211 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100212 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100213 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100214 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100215
Raef Coles01c71a12022-08-31 15:55:00 +0100216exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100217 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100218
Raef Coles29117d22022-10-07 11:46:06 +0100219 return( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100220}
221
Raef Colesbe3bdd82022-10-07 12:04:24 +0100222void mbedtls_lms_public_init( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100223{
Raef Colesfbd60ec2022-10-10 15:09:33 +0100224 memset( ctx, 0, sizeof( *ctx ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100225}
226
Raef Colesbe3bdd82022-10-07 12:04:24 +0100227void mbedtls_lms_public_free( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100228{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100229 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100230}
231
Raef Coles01c71a12022-08-31 15:55:00 +0100232int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
233 const unsigned char *key, size_t key_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100234{
Raef Coles01c71a12022-08-31 15:55:00 +0100235 mbedtls_lms_algorithm_type_t type;
236 mbedtls_lmots_algorithm_type_t otstype;
237
Raef Colesad054252022-09-27 10:59:16 +0100238 type = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100239 key + PUBLIC_KEY_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100240 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100241 {
242 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
243 }
Raef Colesf5632d32022-09-01 09:56:52 +0100244 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100245
Raef Colese89488d2022-10-07 16:06:35 +0100246 if( key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
247 {
248 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
249 }
250
Raef Colesad054252022-09-27 10:59:16 +0100251 otstype = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100252 key + PUBLIC_KEY_OTSTYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100253 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
254 {
255 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
256 }
Raef Colesf5632d32022-09-01 09:56:52 +0100257 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100258
Raef Colesf5632d32022-09-01 09:56:52 +0100259 memcpy( ctx->params.I_key_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100260 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Coles01c71a12022-08-31 15:55:00 +0100261 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles57d53282022-09-27 11:30:51 +0100262 memcpy( ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100263 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100264
Raef Colesf5632d32022-09-01 09:56:52 +0100265 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100266
267 return( 0 );
268}
269
Raef Coles370cc432022-10-07 16:07:33 +0100270int mbedtls_lms_export_public_key( const mbedtls_lms_public_t *ctx,
271 unsigned char *key,
272 size_t key_size, size_t *key_len )
273{
274 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
275 {
276 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
277 }
278
279 if( ! ctx->have_public_key )
280 {
281 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
282 }
283
284 mbedtls_lms_unsigned_int_to_network_bytes(
285 ctx->params.type,
286 MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET );
287 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.otstype,
288 MBEDTLS_LMOTS_TYPE_LEN,
289 key + PUBLIC_KEY_OTSTYPE_OFFSET );
290 memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
291 ctx->params.I_key_identifier,
292 MBEDTLS_LMOTS_I_KEY_ID_LEN );
293 memcpy( key +PUBLIC_KEY_ROOT_NODE_OFFSET,
294 ctx->T_1_pub_key,
295 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
296
297 if( key_len != NULL )
298 {
299 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
300 }
301
302 return( 0 );
303}
304
Raef Coles01c71a12022-08-31 15:55:00 +0100305int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
306 const unsigned char *msg, size_t msg_size,
307 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100308{
309 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100310 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
311 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100312 unsigned int height;
313 unsigned int curr_node_id;
314 unsigned int parent_node_id;
315 const unsigned char* left_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100316 const unsigned char* right_node;
317 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
319
Raef Colesf5632d32022-09-01 09:56:52 +0100320 if( ! ctx->have_public_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100321 {
322 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
323 }
324
Raef Colesf5632d32022-09-01 09:56:52 +0100325 if( ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100326 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100327 {
328 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
329 }
330
Raef Colesf5632d32022-09-01 09:56:52 +0100331 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100332 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100333 {
334 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
335 }
336
Raef Colesfaf59ba2022-10-10 15:40:56 +0100337 if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
338 {
339 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
340 }
341
342 if( sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN )
343 {
344 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
345 }
346
Raef Colesad054252022-09-27 10:59:16 +0100347 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100348 sig + SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET )
Raef Colese9479a02022-09-01 16:06:35 +0100349 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100350 {
351 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
352 }
353
Raef Colesfaf59ba2022-10-10 15:40:56 +0100354 if( sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN )
355 {
356 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
357 }
358
Raef Colesad054252022-09-27 10:59:16 +0100359 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100360 sig + SIG_TYPE_OFFSET(ctx->params.otstype))
Raef Colese9479a02022-09-01 16:06:35 +0100361 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100362 {
363 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
364 }
365
366
Raef Colesad054252022-09-27 10:59:16 +0100367 q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
Raef Coles57d53282022-09-27 11:30:51 +0100368 MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100369
Raef Colese9479a02022-09-01 16:06:35 +0100370 if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100371 {
372 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
373 }
374
Raef Coles9b88ee52022-09-02 12:04:21 +0100375 memcpy( ots_params.I_key_identifier,
376 ctx->params.I_key_identifier,
377 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesad054252022-09-27 10:59:16 +0100378 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
379 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
380 ots_params.q_leaf_identifier );
Raef Colesf5632d32022-09-01 09:56:52 +0100381 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100382
Raef Coles366d67d2022-09-01 17:23:12 +0100383 ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg,
Raef Coles57d53282022-09-27 11:30:51 +0100384 msg_size, sig + SIG_OTS_SIG_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100385 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), Kc_candidate_ots_pub_key,
Raef Coles9b88ee52022-09-02 12:04:21 +0100386 sizeof( Kc_candidate_ots_pub_key ), NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100387 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100388 {
Raef Colesfaf59ba2022-10-10 15:40:56 +0100389 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
Raef Coles8ff6df52021-07-21 12:42:15 +0100390 }
391
Raef Colesebd35b52022-09-01 11:52:17 +0100392 create_merkle_leaf_value(
Raef Colese9479a02022-09-01 16:06:35 +0100393 &ctx->params,
394 Kc_candidate_ots_pub_key,
395 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100396 Tc_candidate_root_node );
Raef Coles8ff6df52021-07-21 12:42:15 +0100397
Raef Coles366d67d2022-09-01 17:23:12 +0100398 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
399 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100400
Raef Colese9479a02022-09-01 16:06:35 +0100401 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
402 height++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100403 {
404 parent_node_id = curr_node_id / 2;
405
406 /* Left/right node ordering matters for the hash */
407 if( curr_node_id & 1 )
408 {
Raef Coles57d53282022-09-27 11:30:51 +0100409 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100410 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100411 right_node = Tc_candidate_root_node;
Raef Coles8ff6df52021-07-21 12:42:15 +0100412 }
413 else
414 {
415 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100416 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100417 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100418 }
419
Raef Colese9479a02022-09-01 16:06:35 +0100420 create_merkle_internal_value( &ctx->params, left_node, right_node,
421 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100422
423 curr_node_id /= 2;
424 }
425
Raef Colesf5632d32022-09-01 09:56:52 +0100426 if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100427 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100428 {
429 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
430 }
431
432 return( 0 );
433}
434
Raef Coles5127e852022-10-07 10:35:56 +0100435#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100436
Raef Coles285d44b2022-10-10 15:44:17 +0100437/* Calculate a full Merkle tree based on a private key. This function
Raef Coles0a967cc2022-09-02 17:46:15 +0100438 * implements RFC8554 section 5.3, and is used to generate a public key (as the
Raef Coles285d44b2022-10-10 15:44:17 +0100439 * public key is the root node of the Merkle tree).
Raef Coles0a967cc2022-09-02 17:46:15 +0100440 *
Raef Coles98d6e222022-09-23 09:04:04 +0100441 * ctx The LMS private context, containing a parameter
442 * set and private key material consisting of both
443 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100444 *
Raef Coles98d6e222022-09-23 09:04:04 +0100445 * tree The output tree, which is 2^(H + 1) hash outputs.
446 * In the case of H=10 we have 2048 tree nodes (of
447 * which 1024 of them are leaf nodes). Note that
Raef Coles285d44b2022-10-10 15:44:17 +0100448 * because the Merkle tree root is 1-indexed, the 0
Raef Coles98d6e222022-09-23 09:04:04 +0100449 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100450 */
Raef Coles2ac352a2022-10-07 11:12:27 +0100451static int calculate_merkle_tree( const mbedtls_lms_private_t *ctx,
Raef Colese9479a02022-09-01 16:06:35 +0100452 unsigned char *tree )
Raef Colesab4f8742022-09-01 12:24:31 +0100453{
454 unsigned int priv_key_idx;
455 unsigned int r_node_idx;
456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
457
458 /* First create the leaf nodes, in ascending order */
Raef Colese9479a02022-09-01 16:06:35 +0100459 for( priv_key_idx = 0;
460 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100461 priv_key_idx++ )
462 {
Raef Colese9479a02022-09-01 16:06:35 +0100463 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100464
Raef Colese9479a02022-09-01 16:06:35 +0100465 ret = create_merkle_leaf_value( &ctx->params,
466 ctx->ots_public_keys[priv_key_idx].public_key, r_node_idx,
467 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100468 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100469 {
470 return( ret );
471 }
472 }
473
474 /* Then the internal nodes, in reverse order so that we can guarantee the
475 * parent has been created */
Raef Colese9479a02022-09-01 16:06:35 +0100476 for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
477 r_node_idx > 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100478 r_node_idx-- )
479 {
Raef Colese9479a02022-09-01 16:06:35 +0100480 ret = create_merkle_internal_value( &ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100481 &tree[( r_node_idx * 2 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
482 &tree[( r_node_idx * 2 + 1 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
Raef Coles366d67d2022-09-01 17:23:12 +0100483 r_node_idx,
484 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100485 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100486 {
487 return( ret );
488 }
489 }
490
491 return( 0 );
492}
493
Raef Coles285d44b2022-10-10 15:44:17 +0100494/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
Raef Coles0a967cc2022-09-02 17:46:15 +0100495 * and return the full path. This function implements RFC8554 section 5.4.1, as
Raef Coles285d44b2022-10-10 15:44:17 +0100496 * the Merkle path is the main component of an LMS signature.
Raef Coles0a967cc2022-09-02 17:46:15 +0100497 *
Raef Coles98d6e222022-09-23 09:04:04 +0100498 * ctx The LMS private context, containing a parameter
499 * set and private key material consisting of both
500 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100501 *
Raef Coles98d6e222022-09-23 09:04:04 +0100502 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100503 *
Raef Coles75b4c772022-10-10 13:58:28 +0100504 * path The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100505 */
Raef Colesab4f8742022-09-01 12:24:31 +0100506static int get_merkle_path( mbedtls_lms_private_t *ctx,
507 unsigned int leaf_node_id,
Raef Colese9479a02022-09-01 16:06:35 +0100508 unsigned char *path )
Raef Colesab4f8742022-09-01 12:24:31 +0100509{
Raef Colese9479a02022-09-01 16:06:35 +0100510 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Colesab4f8742022-09-01 12:24:31 +0100511 unsigned int curr_node_id = leaf_node_id;
512 unsigned int adjacent_node_id;
513 unsigned int height;
514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
515
Raef Coles9b88ee52022-09-02 12:04:21 +0100516 ret = calculate_merkle_tree( ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100517 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100518 {
519 return( ret );
520 }
521
Raef Colese9479a02022-09-01 16:06:35 +0100522 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
523 height++ )
Raef Colesab4f8742022-09-01 12:24:31 +0100524 {
525 adjacent_node_id = curr_node_id ^ 1;
526
Raef Colese9479a02022-09-01 16:06:35 +0100527 memcpy( &path[height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
528 &tree[adjacent_node_id],
529 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Colesab4f8742022-09-01 12:24:31 +0100530
531 curr_node_id >>=1;
532 }
533
534 return( 0 );
535}
536
Raef Colesbe3bdd82022-10-07 12:04:24 +0100537void mbedtls_lms_private_init( mbedtls_lms_private_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100538{
Raef Colesfbd60ec2022-10-10 15:09:33 +0100539 memset( ctx, 0, sizeof( *ctx ) ) ;
Raef Coles01c71a12022-08-31 15:55:00 +0100540}
Raef Coles8ff6df52021-07-21 12:42:15 +0100541
Raef Colesbe3bdd82022-10-07 12:04:24 +0100542void mbedtls_lms_private_free( mbedtls_lms_private_t *ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100543{
544 unsigned int idx;
545
Raef Colesf5632d32022-09-01 09:56:52 +0100546 if( ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100547 {
Raef Colese9479a02022-09-01 16:06:35 +0100548 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100549 {
Raef Colesbe3bdd82022-10-07 12:04:24 +0100550 mbedtls_lmots_private_free( &ctx->ots_private_keys[idx] );
551 mbedtls_lmots_public_free( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100552 }
553
Raef Colesdc8fb792022-10-07 13:27:54 +0100554 if( ctx->ots_private_keys != NULL )
555 mbedtls_free( ctx->ots_private_keys );
556
557 if( ctx->ots_public_keys != NULL )
558 mbedtls_free( ctx->ots_public_keys );
Raef Coles8ff6df52021-07-21 12:42:15 +0100559 }
560
Raef Colesbe3bdd82022-10-07 12:04:24 +0100561 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100562}
Raef Coles8ff6df52021-07-21 12:42:15 +0100563
Raef Coles01c71a12022-08-31 15:55:00 +0100564
565int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
566 mbedtls_lms_algorithm_type_t type,
567 mbedtls_lmots_algorithm_type_t otstype,
568 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100569 void* p_rng, const unsigned char *seed,
Raef Coles01c71a12022-08-31 15:55:00 +0100570 size_t seed_size )
571{
572 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
574
Raef Coles8ff6df52021-07-21 12:42:15 +0100575 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
576 {
577 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
578 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100579
Raef Coles8ff6df52021-07-21 12:42:15 +0100580 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
581 {
582 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
583 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100584
Raef Colesf5632d32022-09-01 09:56:52 +0100585 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100586 {
587 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
588 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100589
Raef Colesf5632d32022-09-01 09:56:52 +0100590 ctx->params.type = type;
591 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100592
Raef Coles3f6cdd72022-10-07 14:07:59 +0100593 ret = f_rng( p_rng,
594 ctx->params.I_key_identifier,
595 MBEDTLS_LMOTS_I_KEY_ID_LEN );
596 if( ret != 0 )
597 {
598 goto exit;
599 }
Raef Coles01c71a12022-08-31 15:55:00 +0100600
Raef Colese34e3c02022-10-10 11:11:30 +0100601 ctx->ots_private_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100602 sizeof( *ctx->ots_private_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100603 if( ctx->ots_private_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100604 {
605 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
606 goto exit;
607 }
608
Raef Colese34e3c02022-10-10 11:11:30 +0100609 ctx->ots_public_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100610 sizeof( *ctx->ots_public_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100611 if( ctx->ots_public_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100612 {
Raef Colesdc8fb792022-10-07 13:27:54 +0100613 /* Free just the ots private keys (since they've been allocated at this
614 * point) so that we can pass the context to lms_private_free (which
615 * will not try to free the private keys since have_private_key is not
616 * set.
617 */
618 mbedtls_free(ctx->ots_private_keys);
619 ctx->ots_private_keys = NULL;
Raef Coles01c71a12022-08-31 15:55:00 +0100620 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
621 goto exit;
622 }
623
Raef Colesdc8fb792022-10-07 13:27:54 +0100624 /* Now that all the allocation has succeeded we set have_private_key, since
625 * that causes lms_private_free to free the ots keys.
626 */
627 ctx->have_private_key = 1;
628
Raef Colese9479a02022-09-01 16:06:35 +0100629 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100630 {
Raef Colesbe3bdd82022-10-07 12:04:24 +0100631 mbedtls_lmots_private_init( &ctx->ots_private_keys[idx] );
632 mbedtls_lmots_public_init( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100633 }
634
635
Raef Colese9479a02022-09-01 16:06:35 +0100636 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100637 {
Raef Colesf5632d32022-09-01 09:56:52 +0100638 ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100639 otstype,
Raef Colesf5632d32022-09-01 09:56:52 +0100640 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100641 idx, seed, seed_size );
Raef Colese0a17612022-09-02 16:04:47 +0100642 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100643 goto exit;
644
Raef Colesf5632d32022-09-01 09:56:52 +0100645 ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
646 &ctx->ots_private_keys[idx] );
Raef Colese0a17612022-09-02 16:04:47 +0100647 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100648 goto exit;
649 }
650
Raef Colesf5632d32022-09-01 09:56:52 +0100651 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100652
653exit:
Raef Colese0a17612022-09-02 16:04:47 +0100654 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100655 {
Raef Colesdc8fb792022-10-07 13:27:54 +0100656 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100657 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100658
Raef Colesdc8fb792022-10-07 13:27:54 +0100659 return( ret );
Raef Coles8ff6df52021-07-21 12:42:15 +0100660}
661
Raef Coles01c71a12022-08-31 15:55:00 +0100662int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
Raef Coles2ac352a2022-10-07 11:12:27 +0100663 const mbedtls_lms_private_t *priv_ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100664{
Raef Colese9479a02022-09-01 16:06:35 +0100665 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
667
Raef Coles9b88ee52022-09-02 12:04:21 +0100668 if( ! priv_ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100669 {
670 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
671 }
672
Raef Colesf5632d32022-09-01 09:56:52 +0100673 if( priv_ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100674 != MBEDTLS_LMS_SHA256_M32_H10 )
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.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100680 != MBEDTLS_LMOTS_SHA256_N32_W8 )
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 memcpy( &ctx->params, &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100686 sizeof( mbedtls_lmots_parameters_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100687
Raef Coles9b88ee52022-09-02 12:04:21 +0100688 ret = calculate_merkle_tree( priv_ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100689 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100690 {
691 return( ret );
692 }
693
694 /* Root node is always at position 1, due to 1-based indexing */
Raef Colese9479a02022-09-01 16:06:35 +0100695 memcpy( ctx->T_1_pub_key, &tree[1],
Raef Coles9b88ee52022-09-02 12:04:21 +0100696 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100697
Raef Colesf5632d32022-09-01 09:56:52 +0100698 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100699
700 return( 0 );
701}
702
Raef Coles01c71a12022-08-31 15:55:00 +0100703
Raef Coles01c71a12022-08-31 15:55:00 +0100704int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
705 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100706 void* p_rng, const unsigned char *msg,
707 unsigned int msg_size, unsigned char *sig, size_t sig_size,
708 size_t *sig_len )
Raef Coles01c71a12022-08-31 15:55:00 +0100709{
710 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
712
Raef Colesf5632d32022-09-01 09:56:52 +0100713 if( ! ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100714 {
715 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
716 }
717
Raef Colese9479a02022-09-01 16:06:35 +0100718 if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles01c71a12022-08-31 15:55:00 +0100719 {
720 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
721 }
722
Raef Colesf5632d32022-09-01 09:56:52 +0100723 if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100724 {
725 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
726 }
727
Raef Colesf5632d32022-09-01 09:56:52 +0100728 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100729 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100730 {
731 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
732 }
733
Raef Colese9479a02022-09-01 16:06:35 +0100734 if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100735 {
Raef Coles01c71a12022-08-31 15:55:00 +0100736 return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
Raef Coles8ff6df52021-07-21 12:42:15 +0100737 }
738
739
Raef Colesf5632d32022-09-01 09:56:52 +0100740 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100741 /* This new value must _always_ be written back to the disk before the
742 * signature is returned.
743 */
Raef Colesf5632d32022-09-01 09:56:52 +0100744 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100745
Raef Coles1fb2f322022-10-10 11:23:07 +0100746 if ( MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
747 < SIG_OTS_SIG_OFFSET )
748 {
Raef Colesf6cb5a42022-10-10 14:15:53 +0100749 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles1fb2f322022-10-10 11:23:07 +0100750 }
751
Raef Colesf5632d32022-09-01 09:56:52 +0100752 ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
Raef Coles01c71a12022-08-31 15:55:00 +0100753 f_rng, p_rng, msg, msg_size,
Raef Coles57d53282022-09-27 11:30:51 +0100754 sig + SIG_OTS_SIG_OFFSET,
Raef Coles02cf8232022-10-07 13:52:47 +0100755 MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100756 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100757 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100758 {
Raef Coles8ff6df52021-07-21 12:42:15 +0100759 return( ret );
760 }
761
Raef Colesad054252022-09-27 10:59:16 +0100762 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
763 MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100764 sig + SIG_TYPE_OFFSET(ctx->params.otstype) );
Raef Colesad054252022-09-27 10:59:16 +0100765 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
766 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100767 sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100768
Raef Colese9479a02022-09-01 16:06:35 +0100769 ret = get_merkle_path( ctx,
Raef Coles366d67d2022-09-01 17:23:12 +0100770 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100771 sig + SIG_PATH_OFFSET(ctx->params.otstype) );
Raef Colese0a17612022-09-02 16:04:47 +0100772 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100773 {
774 return( ret );
775 }
776
Raef Coles9b88ee52022-09-02 12:04:21 +0100777 if( sig_len != NULL )
778 {
Raef Colese9479a02022-09-01 16:06:35 +0100779 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100780 }
781
782
Raef Coles8ff6df52021-07-21 12:42:15 +0100783 return( 0 );
784}
785
Raef Coles5127e852022-10-07 10:35:56 +0100786#endif /* defined(MBEDTLS_LMS_PRIVATE) */
787#endif /* defined(MBEDTLS_LMS_C) */