blob: cb56cb31c68de6e3dcb18daaba175a08e2017f78 [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/*
2 * The LMS stateful-hash public-key signature scheme
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/*
21 * The following sources were referenced in the design of this implementation
22 * of the LMS algorithm:
23 *
24 * [1] IETF RFC8554
25 * D. McGrew, M. Curcio, S.Fluhrer
26 * https://datatracker.ietf.org/doc/html/rfc8554
27 *
28 * [2] NIST Special Publication 800-208
29 * David A. Cooper et. al.
30 * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
31 */
32
33#include "common.h"
34
35#ifdef MBEDTLS_LMS_C
36
37#include <string.h>
38
Raef Coles7dce69a2022-08-24 14:07:06 +010039#include "lmots.h"
40
Raef Colesc8f96042022-08-25 13:49:54 +010041#include "psa/crypto.h"
42
Raef Coles8ff6df52021-07-21 12:42:15 +010043#include "mbedtls/lms.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010044#include "mbedtls/error.h"
45#include "mbedtls/platform_util.h"
46
47#if defined(MBEDTLS_PLATFORM_C)
48#include "mbedtls/platform.h"
49#else
50#include <stdlib.h>
51#include <stdio.h>
52#define mbedtls_printf printf
53#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Raef Coles01c71a12022-08-31 15:55:00 +010057#define MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET (0)
58#define MBEDTLS_LMS_SIG_OTS_SIG_OFFSET (MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
59#define MBEDTLS_LMS_SIG_TYPE_OFFSET (MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_LEN)
60#define MBEDTLS_LMS_SIG_PATH_OFFSET (MBEDTLS_LMS_SIG_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN)
61
62#define MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET (0)
63#define MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN)
64#define MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
65#define MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN)
66
67
68#define MERKLE_TREE_NODE_AM (1u << (MBEDTLS_LMS_H_TREE_HEIGHT + 1u))
69#define MERKLE_TREE_LEAF_NODE_AM (1u << MBEDTLS_LMS_H_TREE_HEIGHT)
70#define MERKLE_TREE_INTERNAL_NODE_AM (1u << MBEDTLS_LMS_H_TREE_HEIGHT)
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};
74static const unsigned char D_INTERNAL_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83};
Raef Coles8ff6df52021-07-21 12:42:15 +010075
Raef Colesebd35b52022-09-01 11:52:17 +010076static int create_merkle_leaf_value( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
77 unsigned char pub_key[MBEDTLS_LMOTS_N_HASH_LEN],
78 unsigned int r_node_idx,
79 unsigned char out[MBEDTLS_LMS_M_NODE_BYTES] )
Raef Coles8ff6df52021-07-21 12:42:15 +010080{
Raef Colesc8f96042022-08-25 13:49:54 +010081 psa_hash_operation_t op;
82 psa_status_t status;
83 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +010084 unsigned char r_node_idx_bytes[4];
85 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
86
Raef Colesc8f96042022-08-25 13:49:54 +010087 op = psa_hash_operation_init( );
88 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
89 ret = mbedtls_lms_error_from_psa( status );
90 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +010091 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +010092
Raef Coles01c71a12022-08-31 15:55:00 +010093 status = psa_hash_update( &op, I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +010094 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +010095 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +010096 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +010097
Raef Coles01c71a12022-08-31 15:55:00 +010098 unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +010099 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
100 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100101 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100102 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100103
Raef Coles01c71a12022-08-31 15:55:00 +0100104 status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100105 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100106 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100107 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100108
Raef Colesc8f96042022-08-25 13:49:54 +0100109 status = psa_hash_update( &op, pub_key, MBEDTLS_LMOTS_N_HASH_LEN );
110 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100111 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100112 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100113
Raef Coles01c71a12022-08-31 15:55:00 +0100114 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES, &output_hash_len);
Raef Colesc8f96042022-08-25 13:49:54 +0100115 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100116 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100117 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100118
Raef Coles01c71a12022-08-31 15:55:00 +0100119exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100120 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100121
122 return( ret );
123}
124
Raef Colesebd35b52022-09-01 11:52:17 +0100125static int create_merkle_internal_value( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
126 const unsigned char left_node[MBEDTLS_LMS_M_NODE_BYTES],
127 const unsigned char right_node[MBEDTLS_LMS_M_NODE_BYTES],
128 unsigned int r_node_idx,
129 unsigned char out[MBEDTLS_LMS_M_NODE_BYTES] )
Raef Coles8ff6df52021-07-21 12:42:15 +0100130{
Raef Colesc8f96042022-08-25 13:49:54 +0100131 psa_hash_operation_t op;
132 psa_status_t status;
133 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100134 unsigned char r_node_idx_bytes[4];
135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
136
Raef Colesc8f96042022-08-25 13:49:54 +0100137 op = psa_hash_operation_init( );
138 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
139 ret = mbedtls_lms_error_from_psa( status );
140 if ( ret != 0 )
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 +0100143 status = psa_hash_update( &op, I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100144 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100145 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100146 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100147
Raef Coles01c71a12022-08-31 15:55:00 +0100148 unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100149 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
150 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100151 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100152 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100153
Raef Coles01c71a12022-08-31 15:55:00 +0100154 status = psa_hash_update( &op, D_INTERNAL_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100155 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100156 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100157 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100158
Raef Colesc8f96042022-08-25 13:49:54 +0100159 status = psa_hash_update( &op, left_node, MBEDTLS_LMOTS_N_HASH_LEN );
160 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100161 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100162 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100163
Raef Coles01c71a12022-08-31 15:55:00 +0100164 status = psa_hash_update( &op, right_node, MBEDTLS_LMOTS_N_HASH_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100165 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100166 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100167 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100168
Raef Coles01c71a12022-08-31 15:55:00 +0100169 ret = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES, &output_hash_len);
Raef Colesc8f96042022-08-25 13:49:54 +0100170 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100171 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100172 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100173
Raef Coles01c71a12022-08-31 15:55:00 +0100174exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100175 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100176
177 return ret;
178}
179
Raef Coles01c71a12022-08-31 15:55:00 +0100180void mbedtls_lms_init_public( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100181{
Raef Coles01c71a12022-08-31 15:55:00 +0100182 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100183}
184
Raef Coles01c71a12022-08-31 15:55:00 +0100185void mbedtls_lms_free_public( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100186{
Raef Coles01c71a12022-08-31 15:55:00 +0100187 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100188}
189
Raef Coles01c71a12022-08-31 15:55:00 +0100190int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
191 const unsigned char *key, size_t key_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100192{
Raef Coles01c71a12022-08-31 15:55:00 +0100193 mbedtls_lms_algorithm_type_t type;
194 mbedtls_lmots_algorithm_type_t otstype;
195
196 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN )
197 {
198 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
199 }
200
201 type = network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET );
202 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100203 {
204 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
205 }
Raef Colesf5632d32022-09-01 09:56:52 +0100206 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100207
Raef Coles01c71a12022-08-31 15:55:00 +0100208 otstype = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
209 key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET );
210 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
211 {
212 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
213 }
Raef Colesf5632d32022-09-01 09:56:52 +0100214 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100215
Raef Colesf5632d32022-09-01 09:56:52 +0100216 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100217 key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET,
218 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesf5632d32022-09-01 09:56:52 +0100219 memcpy( ctx->T_1_pub_key, key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Coles01c71a12022-08-31 15:55:00 +0100220 MBEDTLS_LMOTS_N_HASH_LEN );
221
Raef Colesf5632d32022-09-01 09:56:52 +0100222 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100223
224 return( 0 );
225}
226
Raef Coles01c71a12022-08-31 15:55:00 +0100227int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
228 const unsigned char *msg, size_t msg_size,
229 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100230{
231 unsigned int q_leaf_identifier;
232 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100233 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES];
Raef Coles8ff6df52021-07-21 12:42:15 +0100234 unsigned int height;
235 unsigned int curr_node_id;
236 unsigned int parent_node_id;
237 const unsigned char* left_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100238 const unsigned char* right_node;
239 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
241
Raef Colesf5632d32022-09-01 09:56:52 +0100242 if( ! ctx->have_public_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100243 {
244 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
245 }
246
Raef Coles01c71a12022-08-31 15:55:00 +0100247 if( sig_size != MBEDTLS_LMS_SIG_LEN )
Raef Coles8ff6df52021-07-21 12:42:15 +0100248 {
249 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
250 }
251
Raef Colesf5632d32022-09-01 09:56:52 +0100252 if( ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100253 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100254 {
255 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
256 }
257
Raef Colesf5632d32022-09-01 09:56:52 +0100258 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100259 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100260 {
261 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
262 }
263
Raef Coles01c71a12022-08-31 15:55:00 +0100264 if( network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles8ff6df52021-07-21 12:42:15 +0100265 sig + MBEDTLS_LMS_SIG_TYPE_OFFSET) != MBEDTLS_LMS_SHA256_M32_H10 )
266 {
267 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
268 }
269
Raef Coles01c71a12022-08-31 15:55:00 +0100270 if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles8ff6df52021-07-21 12:42:15 +0100271 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
272 != MBEDTLS_LMOTS_SHA256_N32_W8 )
273 {
274 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
275 }
276
277
Raef Coles01c71a12022-08-31 15:55:00 +0100278 q_leaf_identifier = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles8ff6df52021-07-21 12:42:15 +0100279 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET );
280
Raef Coles01c71a12022-08-31 15:55:00 +0100281 if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM )
Raef Coles8ff6df52021-07-21 12:42:15 +0100282 {
283 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
284 }
285
Raef Colesf5632d32022-09-01 09:56:52 +0100286 memcpy(ots_params.I_key_identifier,
287 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100288 MBEDTLS_LMOTS_I_KEY_ID_LEN);
289 unsigned_int_to_network_bytes( q_leaf_identifier,
290 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Colesf5632d32022-09-01 09:56:52 +0100291 ots_params.q_leaf_identifier );
292 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100293
294 ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg, msg_size,
295 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
296 MBEDTLS_LMOTS_SIG_LEN,
297 Kc_candidate_ots_pub_key,
298 sizeof(Kc_candidate_ots_pub_key),
299 NULL );
Raef Coles8ff6df52021-07-21 12:42:15 +0100300 if( ret )
301 {
302 return( ret );
303 }
304
Raef Colesebd35b52022-09-01 11:52:17 +0100305 create_merkle_leaf_value(
Raef Colesf5632d32022-09-01 09:56:52 +0100306 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100307 Kc_candidate_ots_pub_key, MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier,
308 Tc_candidate_root_node );
Raef Coles8ff6df52021-07-21 12:42:15 +0100309
Raef Coles01c71a12022-08-31 15:55:00 +0100310 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100311
312 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
313 {
314 parent_node_id = curr_node_id / 2;
315
316 /* Left/right node ordering matters for the hash */
317 if( curr_node_id & 1 )
318 {
Raef Coles01c71a12022-08-31 15:55:00 +0100319 left_node = ( ( const unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
320 right_node = Tc_candidate_root_node;
Raef Coles8ff6df52021-07-21 12:42:15 +0100321 }
322 else
323 {
324 left_node = Tc_candidate_root_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100325 right_node = ( ( const unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
Raef Coles8ff6df52021-07-21 12:42:15 +0100326 }
327
Raef Colesebd35b52022-09-01 11:52:17 +0100328 create_merkle_internal_value(
Raef Colesf5632d32022-09-01 09:56:52 +0100329 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100330 left_node, right_node, parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100331
332 curr_node_id /= 2;
333 }
334
Raef Colesf5632d32022-09-01 09:56:52 +0100335 if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
Raef Coles8ff6df52021-07-21 12:42:15 +0100336 MBEDTLS_LMOTS_N_HASH_LEN) )
337 {
338 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
339 }
340
341 return( 0 );
342}
343
Raef Colesab4f8742022-09-01 12:24:31 +0100344#ifdef MBEDTLS_LMS_PRIVATE
345
346static int calculate_merkle_tree( mbedtls_lms_private_t *ctx,
347 unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES] )
348{
349 unsigned int priv_key_idx;
350 unsigned int r_node_idx;
351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
352
353 /* First create the leaf nodes, in ascending order */
354 for( priv_key_idx = 0; priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM;
355 priv_key_idx++ )
356 {
357 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM + priv_key_idx;
358
359 ret = create_merkle_leaf_value(
360 ctx->params.I_key_identifier,
361 ctx->ots_public_keys[priv_key_idx].public_key,
362 r_node_idx, tree[r_node_idx] );
363 if( ret )
364 {
365 return( ret );
366 }
367 }
368
369 /* Then the internal nodes, in reverse order so that we can guarantee the
370 * parent has been created */
371 for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM - 1; r_node_idx > 0;
372 r_node_idx-- )
373 {
374 ret = create_merkle_internal_value(
375 ctx->params.I_key_identifier,
376 tree[(r_node_idx * 2)], tree[(r_node_idx * 2 + 1)], r_node_idx, tree[r_node_idx] );
377 if( ret )
378 {
379 return( ret );
380 }
381 }
382
383 return( 0 );
384}
385
386static int get_merkle_path( mbedtls_lms_private_t *ctx,
387 unsigned int leaf_node_id,
388 unsigned char path[MBEDTLS_LMS_H_TREE_HEIGHT][MBEDTLS_LMS_M_NODE_BYTES] )
389{
390 unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES];
391 unsigned int curr_node_id = leaf_node_id;
392 unsigned int adjacent_node_id;
393 unsigned int height;
394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
395
396 ret = calculate_merkle_tree( ctx, tree);
397 if( ret )
398 {
399 return( ret );
400 }
401
402 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
403 {
404 adjacent_node_id = curr_node_id ^ 1;
405
406 memcpy( &path[height], &tree[adjacent_node_id], MBEDTLS_LMOTS_N_HASH_LEN );
407
408 curr_node_id >>=1;
409 }
410
411 return( 0 );
412}
413
Raef Coles01c71a12022-08-31 15:55:00 +0100414void mbedtls_lms_init_private( mbedtls_lms_private_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100415{
Raef Coles01c71a12022-08-31 15:55:00 +0100416 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ;
417}
Raef Coles8ff6df52021-07-21 12:42:15 +0100418
Raef Coles01c71a12022-08-31 15:55:00 +0100419void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx )
420{
421 unsigned int idx;
422
Raef Colesf5632d32022-09-01 09:56:52 +0100423 if( ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100424 {
Raef Coles01c71a12022-08-31 15:55:00 +0100425 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ )
426 {
Raef Colesf5632d32022-09-01 09:56:52 +0100427 mbedtls_lmots_free_private( &ctx->ots_private_keys[idx] );
428 mbedtls_lmots_free_public( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100429 }
430
Raef Colesf5632d32022-09-01 09:56:52 +0100431 mbedtls_free( ctx->ots_private_keys );
432 mbedtls_free( ctx->ots_public_keys );
Raef Coles8ff6df52021-07-21 12:42:15 +0100433 }
434
Raef Coles01c71a12022-08-31 15:55:00 +0100435 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) );
436}
Raef Coles8ff6df52021-07-21 12:42:15 +0100437
Raef Coles01c71a12022-08-31 15:55:00 +0100438
439int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
440 mbedtls_lms_algorithm_type_t type,
441 mbedtls_lmots_algorithm_type_t otstype,
442 int (*f_rng)(void *, unsigned char *, size_t),
443 void* p_rng, unsigned char *seed,
444 size_t seed_size )
445{
446 unsigned int idx = 0;
447 unsigned int free_idx = 0;
448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
449
Raef Coles8ff6df52021-07-21 12:42:15 +0100450 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
451 {
452 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
453 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100454
Raef Coles8ff6df52021-07-21 12:42:15 +0100455 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
456 {
457 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
458 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100459
Raef Colesf5632d32022-09-01 09:56:52 +0100460 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100461 {
462 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
463 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100464
Raef Colesf5632d32022-09-01 09:56:52 +0100465 ctx->params.type = type;
466 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100467
468 f_rng( p_rng,
Raef Colesf5632d32022-09-01 09:56:52 +0100469 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100470 MBEDTLS_LMOTS_I_KEY_ID_LEN );
471
Raef Colesf5632d32022-09-01 09:56:52 +0100472 ctx->ots_private_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM,
Raef Coles01c71a12022-08-31 15:55:00 +0100473 sizeof( mbedtls_lmots_private_t));
Raef Colesf5632d32022-09-01 09:56:52 +0100474 if( ctx->ots_private_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100475 {
476 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
477 goto exit;
478 }
479
Raef Colesf5632d32022-09-01 09:56:52 +0100480 ctx->ots_public_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM,
Raef Coles01c71a12022-08-31 15:55:00 +0100481 sizeof( mbedtls_lmots_public_t));
Raef Colesf5632d32022-09-01 09:56:52 +0100482 if( ctx->ots_public_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100483 {
484 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
485 goto exit;
486 }
487
488 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ )
489 {
Raef Colesf5632d32022-09-01 09:56:52 +0100490 mbedtls_lmots_init_private( &ctx->ots_private_keys[idx] );
491 mbedtls_lmots_init_public( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100492 }
493
494
495 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ )
496 {
Raef Colesf5632d32022-09-01 09:56:52 +0100497 ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100498 otstype,
Raef Colesf5632d32022-09-01 09:56:52 +0100499 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100500 idx, seed, seed_size );
501 if( ret)
502 goto exit;
503
Raef Colesf5632d32022-09-01 09:56:52 +0100504 ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
505 &ctx->ots_private_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100506 if( ret)
507 goto exit;
508 }
509
Raef Colesf5632d32022-09-01 09:56:52 +0100510 ctx->q_next_usable_key = 0;
511 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100512
513exit:
514 if( ret )
515 {
516 for ( free_idx = 0; free_idx < idx; free_idx++ ) {
Raef Colesf5632d32022-09-01 09:56:52 +0100517 mbedtls_lmots_free_private( &ctx->ots_private_keys[free_idx] );
518 mbedtls_lmots_free_public( &ctx->ots_public_keys[free_idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100519 }
520
Raef Colesf5632d32022-09-01 09:56:52 +0100521 mbedtls_free( ctx->ots_private_keys );
522 mbedtls_free( ctx->ots_public_keys );
Raef Coles01c71a12022-08-31 15:55:00 +0100523 return( ret );
524 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100525
526 return( 0 );
527}
528
Raef Coles01c71a12022-08-31 15:55:00 +0100529int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
530 mbedtls_lms_private_t *priv_ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100531{
Raef Coles01c71a12022-08-31 15:55:00 +0100532 unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES];
Raef Coles8ff6df52021-07-21 12:42:15 +0100533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
534
Raef Coles01c71a12022-08-31 15:55:00 +0100535 if( ! priv_ctx->MBEDTLS_PRIVATE( have_private_key ) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100536 {
537 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
538 }
539
Raef Colesf5632d32022-09-01 09:56:52 +0100540 if( priv_ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100541 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100542 {
543 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
544 }
545
Raef Colesf5632d32022-09-01 09:56:52 +0100546 if( priv_ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100547 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100548 {
549 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
550 }
551
Raef Colesf5632d32022-09-01 09:56:52 +0100552 memcpy( &ctx->params, &priv_ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100553 sizeof(mbedtls_lmots_parameters_t) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100554
Raef Coles01c71a12022-08-31 15:55:00 +0100555 ret = calculate_merkle_tree( priv_ctx, tree);
Raef Coles8ff6df52021-07-21 12:42:15 +0100556 if( ret )
557 {
558 return( ret );
559 }
560
561 /* Root node is always at position 1, due to 1-based indexing */
Raef Colesf5632d32022-09-01 09:56:52 +0100562 memcpy( ctx->T_1_pub_key, &tree[1], MBEDTLS_LMOTS_N_HASH_LEN );
Raef Coles8ff6df52021-07-21 12:42:15 +0100563
Raef Colesf5632d32022-09-01 09:56:52 +0100564 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100565
566 return( 0 );
567}
568
Raef Coles01c71a12022-08-31 15:55:00 +0100569
570int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx, unsigned char *key,
571 size_t key_size, size_t *key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100572{
Raef Coles01c71a12022-08-31 15:55:00 +0100573 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN ) {
574 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
575 }
576
Raef Colesf5632d32022-09-01 09:56:52 +0100577 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100578 {
579 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
580 }
581
582 unsigned_int_to_network_bytes(
Raef Colesf5632d32022-09-01 09:56:52 +0100583 ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100584 MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET );
585 unsigned_int_to_network_bytes(
Raef Colesf5632d32022-09-01 09:56:52 +0100586 ctx->params.otstype,
Raef Coles01c71a12022-08-31 15:55:00 +0100587 MBEDTLS_LMOTS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET );
588 memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100589 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100590 MBEDTLS_LMOTS_I_KEY_ID_LEN );
591 memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100592 ctx->T_1_pub_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100593 MBEDTLS_LMOTS_N_HASH_LEN );
594
595 if( key_len != NULL ) {
596 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN;
597 }
598
599 return( 0 );
600}
601
602
603int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
604 int (*f_rng)(void *, unsigned char *, size_t),
605 void* p_rng, unsigned char *msg, unsigned int msg_size,
606 unsigned char *sig, size_t sig_size, size_t *sig_len)
607{
608 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
610
Raef Colesf5632d32022-09-01 09:56:52 +0100611 if( ! ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100612 {
613 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
614 }
615
Raef Coles01c71a12022-08-31 15:55:00 +0100616 if( sig_size < MBEDTLS_LMS_SIG_LEN )
617 {
618 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
619 }
620
Raef Colesf5632d32022-09-01 09:56:52 +0100621 if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100622 {
623 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
624 }
625
Raef Colesf5632d32022-09-01 09:56:52 +0100626 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100627 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100628 {
629 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
630 }
631
Raef Colesf5632d32022-09-01 09:56:52 +0100632 if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM )
Raef Coles8ff6df52021-07-21 12:42:15 +0100633 {
Raef Coles01c71a12022-08-31 15:55:00 +0100634 return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
Raef Coles8ff6df52021-07-21 12:42:15 +0100635 }
636
637
Raef Colesf5632d32022-09-01 09:56:52 +0100638 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100639 /* This new value must _always_ be written back to the disk before the
640 * signature is returned.
641 */
Raef Colesf5632d32022-09-01 09:56:52 +0100642 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100643
Raef Colesf5632d32022-09-01 09:56:52 +0100644 ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
Raef Coles01c71a12022-08-31 15:55:00 +0100645 f_rng, p_rng, msg, msg_size,
646 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
647 MBEDTLS_LMS_SIG_LEN, NULL );
Raef Coles8ff6df52021-07-21 12:42:15 +0100648 if( ret )
649 {
Raef Coles8ff6df52021-07-21 12:42:15 +0100650 return( ret );
651 }
652
Raef Colesf5632d32022-09-01 09:56:52 +0100653 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100654 MBEDTLS_LMS_TYPE_LEN, sig + MBEDTLS_LMS_SIG_TYPE_OFFSET );
655 unsigned_int_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
656 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET);
657
658 ret = get_merkle_path( ctx, MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier,
659 ( unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) );
660 if( ret )
661 {
662 return( ret );
663 }
664
665 if( sig_len != NULL ) {
666 *sig_len = MBEDTLS_LMS_SIG_LEN;
667 }
668
669
Raef Coles8ff6df52021-07-21 12:42:15 +0100670 return( 0 );
671}
672
Raef Colesab4f8742022-09-01 12:24:31 +0100673#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100674#endif /* MBEDTLS_LMS_C */