blob: 94f5abe5cc633879aab598e560fbc2f043045943 [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 Coles01c71a12022-08-31 15:55:00 +010076static int create_merkle_leaf_node( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
Raef Coles8ff6df52021-07-21 12:42:15 +010077 unsigned char pub_key[MBEDTLS_LMOTS_N_HASH_LEN],
78 unsigned int r_node_idx,
Raef Coles01c71a12022-08-31 15:55:00 +010079 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 Coles01c71a12022-08-31 15:55:00 +0100125static int create_merkle_internal_node( 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 +0100180static int calculate_merkle_tree( mbedtls_lms_private_t *ctx,
181 unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES] )
Raef Coles8ff6df52021-07-21 12:42:15 +0100182{
183 unsigned int priv_key_idx;
184 unsigned int r_node_idx;
185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
186
187 /* First create the leaf nodes, in ascending order */
Raef Coles01c71a12022-08-31 15:55:00 +0100188 for( priv_key_idx = 0; priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM;
189 priv_key_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100190 {
Raef Coles01c71a12022-08-31 15:55:00 +0100191 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM + priv_key_idx;
Raef Coles8ff6df52021-07-21 12:42:15 +0100192
Raef Coles01c71a12022-08-31 15:55:00 +0100193 ret = create_merkle_leaf_node(
Raef Colesf5632d32022-09-01 09:56:52 +0100194 ctx->params.I_key_identifier,
195 ctx->ots_public_keys[priv_key_idx].public_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100196 r_node_idx, tree[r_node_idx] );
Raef Coles8ff6df52021-07-21 12:42:15 +0100197 if( ret )
198 {
199 return( ret );
200 }
201 }
202
203 /* Then the internal nodes, in reverse order so that we can guarantee the
204 * parent has been created */
Raef Coles01c71a12022-08-31 15:55:00 +0100205 for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM - 1; r_node_idx > 0;
206 r_node_idx-- )
Raef Coles8ff6df52021-07-21 12:42:15 +0100207 {
Raef Coles01c71a12022-08-31 15:55:00 +0100208 ret = create_merkle_internal_node(
Raef Colesf5632d32022-09-01 09:56:52 +0100209 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100210 tree[(r_node_idx * 2)], tree[(r_node_idx * 2 + 1)], r_node_idx, tree[r_node_idx] );
Raef Coles8ff6df52021-07-21 12:42:15 +0100211 if( ret )
212 {
213 return( ret );
214 }
215 }
216
217 return( 0 );
218}
219
Raef Coles01c71a12022-08-31 15:55:00 +0100220static int get_merkle_path( mbedtls_lms_private_t *ctx,
221 unsigned int leaf_node_id,
222 unsigned char path[MBEDTLS_LMS_H_TREE_HEIGHT][MBEDTLS_LMS_M_NODE_BYTES] )
Raef Coles8ff6df52021-07-21 12:42:15 +0100223{
Raef Coles01c71a12022-08-31 15:55:00 +0100224 unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES];
Raef Coles8ff6df52021-07-21 12:42:15 +0100225 unsigned int curr_node_id = leaf_node_id;
Raef Coles8ff6df52021-07-21 12:42:15 +0100226 unsigned int adjacent_node_id;
227 unsigned int height;
228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
229
Raef Coles01c71a12022-08-31 15:55:00 +0100230 ret = calculate_merkle_tree( ctx, tree);
Raef Coles8ff6df52021-07-21 12:42:15 +0100231 if( ret )
232 {
233 return( ret );
234 }
235
236 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
237 {
Raef Coles01c71a12022-08-31 15:55:00 +0100238 adjacent_node_id = curr_node_id ^ 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100239
240 memcpy( &path[height], &tree[adjacent_node_id], MBEDTLS_LMOTS_N_HASH_LEN );
241
Raef Coles01c71a12022-08-31 15:55:00 +0100242 curr_node_id >>=1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100243 }
244
245 return( 0 );
246}
247
Raef Coles01c71a12022-08-31 15:55:00 +0100248void mbedtls_lms_init_public( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100249{
Raef Coles01c71a12022-08-31 15:55:00 +0100250 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100251}
252
Raef Coles01c71a12022-08-31 15:55:00 +0100253void mbedtls_lms_free_public( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100254{
Raef Coles01c71a12022-08-31 15:55:00 +0100255 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100256}
257
Raef Coles01c71a12022-08-31 15:55:00 +0100258int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
259 const unsigned char *key, size_t key_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100260{
Raef Coles01c71a12022-08-31 15:55:00 +0100261 mbedtls_lms_algorithm_type_t type;
262 mbedtls_lmots_algorithm_type_t otstype;
263
264 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN )
265 {
266 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
267 }
268
269 type = network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET );
270 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100271 {
272 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
273 }
Raef Colesf5632d32022-09-01 09:56:52 +0100274 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100275
Raef Coles01c71a12022-08-31 15:55:00 +0100276 otstype = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
277 key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET );
278 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
279 {
280 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
281 }
Raef Colesf5632d32022-09-01 09:56:52 +0100282 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100283
Raef Colesf5632d32022-09-01 09:56:52 +0100284 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100285 key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET,
286 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesf5632d32022-09-01 09:56:52 +0100287 memcpy( ctx->T_1_pub_key, key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Coles01c71a12022-08-31 15:55:00 +0100288 MBEDTLS_LMOTS_N_HASH_LEN );
289
Raef Colesf5632d32022-09-01 09:56:52 +0100290 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100291
292 return( 0 );
293}
294
Raef Coles01c71a12022-08-31 15:55:00 +0100295int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
296 const unsigned char *msg, size_t msg_size,
297 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100298{
299 unsigned int q_leaf_identifier;
300 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100301 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES];
Raef Coles8ff6df52021-07-21 12:42:15 +0100302 unsigned int height;
303 unsigned int curr_node_id;
304 unsigned int parent_node_id;
305 const unsigned char* left_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100306 const unsigned char* right_node;
307 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
309
Raef Colesf5632d32022-09-01 09:56:52 +0100310 if( ! ctx->have_public_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100311 {
312 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
313 }
314
Raef Coles01c71a12022-08-31 15:55:00 +0100315 if( sig_size != MBEDTLS_LMS_SIG_LEN )
Raef Coles8ff6df52021-07-21 12:42:15 +0100316 {
317 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
318 }
319
Raef Colesf5632d32022-09-01 09:56:52 +0100320 if( ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100321 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100322 {
323 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
324 }
325
Raef Colesf5632d32022-09-01 09:56:52 +0100326 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100327 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100328 {
329 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
330 }
331
Raef Coles01c71a12022-08-31 15:55:00 +0100332 if( network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles8ff6df52021-07-21 12:42:15 +0100333 sig + MBEDTLS_LMS_SIG_TYPE_OFFSET) != MBEDTLS_LMS_SHA256_M32_H10 )
334 {
335 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
336 }
337
Raef Coles01c71a12022-08-31 15:55:00 +0100338 if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles8ff6df52021-07-21 12:42:15 +0100339 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
340 != MBEDTLS_LMOTS_SHA256_N32_W8 )
341 {
342 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
343 }
344
345
Raef Coles01c71a12022-08-31 15:55:00 +0100346 q_leaf_identifier = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles8ff6df52021-07-21 12:42:15 +0100347 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET );
348
Raef Coles01c71a12022-08-31 15:55:00 +0100349 if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM )
Raef Coles8ff6df52021-07-21 12:42:15 +0100350 {
351 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
352 }
353
Raef Colesf5632d32022-09-01 09:56:52 +0100354 memcpy(ots_params.I_key_identifier,
355 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100356 MBEDTLS_LMOTS_I_KEY_ID_LEN);
357 unsigned_int_to_network_bytes( q_leaf_identifier,
358 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Colesf5632d32022-09-01 09:56:52 +0100359 ots_params.q_leaf_identifier );
360 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100361
362 ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg, msg_size,
363 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
364 MBEDTLS_LMOTS_SIG_LEN,
365 Kc_candidate_ots_pub_key,
366 sizeof(Kc_candidate_ots_pub_key),
367 NULL );
Raef Coles8ff6df52021-07-21 12:42:15 +0100368 if( ret )
369 {
370 return( ret );
371 }
372
Raef Coles01c71a12022-08-31 15:55:00 +0100373 create_merkle_leaf_node(
Raef Colesf5632d32022-09-01 09:56:52 +0100374 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100375 Kc_candidate_ots_pub_key, MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier,
376 Tc_candidate_root_node );
Raef Coles8ff6df52021-07-21 12:42:15 +0100377
Raef Coles01c71a12022-08-31 15:55:00 +0100378 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100379
380 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
381 {
382 parent_node_id = curr_node_id / 2;
383
384 /* Left/right node ordering matters for the hash */
385 if( curr_node_id & 1 )
386 {
Raef Coles01c71a12022-08-31 15:55:00 +0100387 left_node = ( ( const unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
388 right_node = Tc_candidate_root_node;
Raef Coles8ff6df52021-07-21 12:42:15 +0100389 }
390 else
391 {
392 left_node = Tc_candidate_root_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100393 right_node = ( ( const unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
Raef Coles8ff6df52021-07-21 12:42:15 +0100394 }
395
Raef Coles01c71a12022-08-31 15:55:00 +0100396 create_merkle_internal_node(
Raef Colesf5632d32022-09-01 09:56:52 +0100397 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100398 left_node, right_node, parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100399
400 curr_node_id /= 2;
401 }
402
Raef Colesf5632d32022-09-01 09:56:52 +0100403 if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
Raef Coles8ff6df52021-07-21 12:42:15 +0100404 MBEDTLS_LMOTS_N_HASH_LEN) )
405 {
406 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
407 }
408
409 return( 0 );
410}
411
Raef Coles01c71a12022-08-31 15:55:00 +0100412void mbedtls_lms_init_private( mbedtls_lms_private_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100413{
Raef Coles01c71a12022-08-31 15:55:00 +0100414 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ;
415}
Raef Coles8ff6df52021-07-21 12:42:15 +0100416
Raef Coles01c71a12022-08-31 15:55:00 +0100417void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx )
418{
419 unsigned int idx;
420
Raef Colesf5632d32022-09-01 09:56:52 +0100421 if( ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100422 {
Raef Coles01c71a12022-08-31 15:55:00 +0100423 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ )
424 {
Raef Colesf5632d32022-09-01 09:56:52 +0100425 mbedtls_lmots_free_private( &ctx->ots_private_keys[idx] );
426 mbedtls_lmots_free_public( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100427 }
428
Raef Colesf5632d32022-09-01 09:56:52 +0100429 mbedtls_free( ctx->ots_private_keys );
430 mbedtls_free( ctx->ots_public_keys );
Raef Coles8ff6df52021-07-21 12:42:15 +0100431 }
432
Raef Coles01c71a12022-08-31 15:55:00 +0100433 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) );
434}
Raef Coles8ff6df52021-07-21 12:42:15 +0100435
Raef Coles01c71a12022-08-31 15:55:00 +0100436
437int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
438 mbedtls_lms_algorithm_type_t type,
439 mbedtls_lmots_algorithm_type_t otstype,
440 int (*f_rng)(void *, unsigned char *, size_t),
441 void* p_rng, unsigned char *seed,
442 size_t seed_size )
443{
444 unsigned int idx = 0;
445 unsigned int free_idx = 0;
446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
447
Raef Coles8ff6df52021-07-21 12:42:15 +0100448 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
449 {
450 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
451 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100452
Raef Coles8ff6df52021-07-21 12:42:15 +0100453 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
454 {
455 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
456 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100457
Raef Colesf5632d32022-09-01 09:56:52 +0100458 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100459 {
460 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
461 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100462
Raef Colesf5632d32022-09-01 09:56:52 +0100463 ctx->params.type = type;
464 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100465
466 f_rng( p_rng,
Raef Colesf5632d32022-09-01 09:56:52 +0100467 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100468 MBEDTLS_LMOTS_I_KEY_ID_LEN );
469
Raef Colesf5632d32022-09-01 09:56:52 +0100470 ctx->ots_private_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM,
Raef Coles01c71a12022-08-31 15:55:00 +0100471 sizeof( mbedtls_lmots_private_t));
Raef Colesf5632d32022-09-01 09:56:52 +0100472 if( ctx->ots_private_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100473 {
474 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
475 goto exit;
476 }
477
Raef Colesf5632d32022-09-01 09:56:52 +0100478 ctx->ots_public_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM,
Raef Coles01c71a12022-08-31 15:55:00 +0100479 sizeof( mbedtls_lmots_public_t));
Raef Colesf5632d32022-09-01 09:56:52 +0100480 if( ctx->ots_public_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100481 {
482 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
483 goto exit;
484 }
485
486 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ )
487 {
Raef Colesf5632d32022-09-01 09:56:52 +0100488 mbedtls_lmots_init_private( &ctx->ots_private_keys[idx] );
489 mbedtls_lmots_init_public( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100490 }
491
492
493 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ )
494 {
Raef Colesf5632d32022-09-01 09:56:52 +0100495 ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100496 otstype,
Raef Colesf5632d32022-09-01 09:56:52 +0100497 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100498 idx, seed, seed_size );
499 if( ret)
500 goto exit;
501
Raef Colesf5632d32022-09-01 09:56:52 +0100502 ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
503 &ctx->ots_private_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100504 if( ret)
505 goto exit;
506 }
507
Raef Colesf5632d32022-09-01 09:56:52 +0100508 ctx->q_next_usable_key = 0;
509 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100510
511exit:
512 if( ret )
513 {
514 for ( free_idx = 0; free_idx < idx; free_idx++ ) {
Raef Colesf5632d32022-09-01 09:56:52 +0100515 mbedtls_lmots_free_private( &ctx->ots_private_keys[free_idx] );
516 mbedtls_lmots_free_public( &ctx->ots_public_keys[free_idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100517 }
518
Raef Colesf5632d32022-09-01 09:56:52 +0100519 mbedtls_free( ctx->ots_private_keys );
520 mbedtls_free( ctx->ots_public_keys );
Raef Coles01c71a12022-08-31 15:55:00 +0100521 return( ret );
522 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100523
524 return( 0 );
525}
526
Raef Coles01c71a12022-08-31 15:55:00 +0100527int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
528 mbedtls_lms_private_t *priv_ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100529{
Raef Coles01c71a12022-08-31 15:55:00 +0100530 unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES];
Raef Coles8ff6df52021-07-21 12:42:15 +0100531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
532
Raef Coles01c71a12022-08-31 15:55:00 +0100533 if( ! priv_ctx->MBEDTLS_PRIVATE( have_private_key ) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100534 {
535 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
536 }
537
Raef Colesf5632d32022-09-01 09:56:52 +0100538 if( priv_ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100539 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100540 {
541 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
542 }
543
Raef Colesf5632d32022-09-01 09:56:52 +0100544 if( priv_ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100545 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100546 {
547 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
548 }
549
Raef Colesf5632d32022-09-01 09:56:52 +0100550 memcpy( &ctx->params, &priv_ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100551 sizeof(mbedtls_lmots_parameters_t) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100552
Raef Coles01c71a12022-08-31 15:55:00 +0100553 ret = calculate_merkle_tree( priv_ctx, tree);
Raef Coles8ff6df52021-07-21 12:42:15 +0100554 if( ret )
555 {
556 return( ret );
557 }
558
559 /* Root node is always at position 1, due to 1-based indexing */
Raef Colesf5632d32022-09-01 09:56:52 +0100560 memcpy( ctx->T_1_pub_key, &tree[1], MBEDTLS_LMOTS_N_HASH_LEN );
Raef Coles8ff6df52021-07-21 12:42:15 +0100561
Raef Colesf5632d32022-09-01 09:56:52 +0100562 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100563
564 return( 0 );
565}
566
Raef Coles01c71a12022-08-31 15:55:00 +0100567
568int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx, unsigned char *key,
569 size_t key_size, size_t *key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100570{
Raef Coles01c71a12022-08-31 15:55:00 +0100571 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN ) {
572 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
573 }
574
Raef Colesf5632d32022-09-01 09:56:52 +0100575 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100576 {
577 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
578 }
579
580 unsigned_int_to_network_bytes(
Raef Colesf5632d32022-09-01 09:56:52 +0100581 ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100582 MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET );
583 unsigned_int_to_network_bytes(
Raef Colesf5632d32022-09-01 09:56:52 +0100584 ctx->params.otstype,
Raef Coles01c71a12022-08-31 15:55:00 +0100585 MBEDTLS_LMOTS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET );
586 memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100587 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100588 MBEDTLS_LMOTS_I_KEY_ID_LEN );
589 memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100590 ctx->T_1_pub_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100591 MBEDTLS_LMOTS_N_HASH_LEN );
592
593 if( key_len != NULL ) {
594 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN;
595 }
596
597 return( 0 );
598}
599
600
601int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
602 int (*f_rng)(void *, unsigned char *, size_t),
603 void* p_rng, unsigned char *msg, unsigned int msg_size,
604 unsigned char *sig, size_t sig_size, size_t *sig_len)
605{
606 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
608
Raef Colesf5632d32022-09-01 09:56:52 +0100609 if( ! ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100610 {
611 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
612 }
613
Raef Coles01c71a12022-08-31 15:55:00 +0100614 if( sig_size < MBEDTLS_LMS_SIG_LEN )
615 {
616 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
617 }
618
Raef Colesf5632d32022-09-01 09:56:52 +0100619 if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100620 {
621 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
622 }
623
Raef Colesf5632d32022-09-01 09:56:52 +0100624 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100625 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100626 {
627 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
628 }
629
Raef Colesf5632d32022-09-01 09:56:52 +0100630 if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM )
Raef Coles8ff6df52021-07-21 12:42:15 +0100631 {
Raef Coles01c71a12022-08-31 15:55:00 +0100632 return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
Raef Coles8ff6df52021-07-21 12:42:15 +0100633 }
634
635
Raef Colesf5632d32022-09-01 09:56:52 +0100636 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100637 /* This new value must _always_ be written back to the disk before the
638 * signature is returned.
639 */
Raef Colesf5632d32022-09-01 09:56:52 +0100640 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100641
Raef Colesf5632d32022-09-01 09:56:52 +0100642 ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
Raef Coles01c71a12022-08-31 15:55:00 +0100643 f_rng, p_rng, msg, msg_size,
644 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
645 MBEDTLS_LMS_SIG_LEN, NULL );
Raef Coles8ff6df52021-07-21 12:42:15 +0100646 if( ret )
647 {
Raef Coles8ff6df52021-07-21 12:42:15 +0100648 return( ret );
649 }
650
Raef Colesf5632d32022-09-01 09:56:52 +0100651 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100652 MBEDTLS_LMS_TYPE_LEN, sig + MBEDTLS_LMS_SIG_TYPE_OFFSET );
653 unsigned_int_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
654 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET);
655
656 ret = get_merkle_path( ctx, MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier,
657 ( unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) );
658 if( ret )
659 {
660 return( ret );
661 }
662
663 if( sig_len != NULL ) {
664 *sig_len = MBEDTLS_LMS_SIG_LEN;
665 }
666
667
Raef Coles8ff6df52021-07-21 12:42:15 +0100668 return( 0 );
669}
670
671#endif /* MBEDTLS_LMS_C */