blob: 06bae9dffd1bc9097580c7f69128392fde8f078c [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
86/* Calculate the value of a leaf node of the merkle tree (which is a hash of a
87 * 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 Coles98d6e222022-09-23 09:04:04 +010098 * r_node_idx The index of this node in the merkle tree. Note
99 * that the root node of the merkle tree is
100 * 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 Coles0a967cc2022-09-02 17:46:15 +0100149/* Calculate the value of an internal node of the merkle tree (which is a hash
150 * 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
159 * 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
163 * merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100164 *
Raef Coles98d6e222022-09-23 09:04:04 +0100165 * r_node_idx The index of this node in the merkle tree. Note
166 * that the root node of the merkle tree is
167 * 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 Colesbe3bdd82022-10-07 12:04:24 +0100224 mbedtls_platform_zeroize( ctx, 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 Colese9479a02022-09-01 16:06:35 +0100238 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100239 {
240 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
241 }
242
Raef Colesad054252022-09-27 10:59:16 +0100243 type = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100244 key + PUBLIC_KEY_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100245 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100246 {
247 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
248 }
Raef Colesf5632d32022-09-01 09:56:52 +0100249 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100250
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 Coles01c71a12022-08-31 15:55:00 +0100270int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
271 const unsigned char *msg, size_t msg_size,
272 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100273{
274 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100275 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
276 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100277 unsigned int height;
278 unsigned int curr_node_id;
279 unsigned int parent_node_id;
280 const unsigned char* left_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100281 const unsigned char* right_node;
282 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
284
Raef Colesf5632d32022-09-01 09:56:52 +0100285 if( ! ctx->have_public_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100286 {
287 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
288 }
289
Raef Colese9479a02022-09-01 16:06:35 +0100290 if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100291 {
292 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
293 }
294
Raef Colesf5632d32022-09-01 09:56:52 +0100295 if( ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100296 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100297 {
298 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
299 }
300
Raef Colesf5632d32022-09-01 09:56:52 +0100301 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100302 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100303 {
304 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
305 }
306
Raef Colesad054252022-09-27 10:59:16 +0100307 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100308 sig + SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET )
Raef Colese9479a02022-09-01 16:06:35 +0100309 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100310 {
311 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
312 }
313
Raef Colesad054252022-09-27 10:59:16 +0100314 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100315 sig + SIG_TYPE_OFFSET(ctx->params.otstype))
Raef Colese9479a02022-09-01 16:06:35 +0100316 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100317 {
318 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
319 }
320
321
Raef Colesad054252022-09-27 10:59:16 +0100322 q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
Raef Coles57d53282022-09-27 11:30:51 +0100323 MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100324
Raef Colese9479a02022-09-01 16:06:35 +0100325 if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100326 {
327 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
328 }
329
Raef Coles9b88ee52022-09-02 12:04:21 +0100330 memcpy( ots_params.I_key_identifier,
331 ctx->params.I_key_identifier,
332 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesad054252022-09-27 10:59:16 +0100333 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
334 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
335 ots_params.q_leaf_identifier );
Raef Colesf5632d32022-09-01 09:56:52 +0100336 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100337
Raef Coles366d67d2022-09-01 17:23:12 +0100338 ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg,
Raef Coles57d53282022-09-27 11:30:51 +0100339 msg_size, sig + SIG_OTS_SIG_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100340 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), Kc_candidate_ots_pub_key,
Raef Coles9b88ee52022-09-02 12:04:21 +0100341 sizeof( Kc_candidate_ots_pub_key ), NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100342 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100343 {
344 return( ret );
345 }
346
Raef Colesebd35b52022-09-01 11:52:17 +0100347 create_merkle_leaf_value(
Raef Colese9479a02022-09-01 16:06:35 +0100348 &ctx->params,
349 Kc_candidate_ots_pub_key,
350 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100351 Tc_candidate_root_node );
Raef Coles8ff6df52021-07-21 12:42:15 +0100352
Raef Coles366d67d2022-09-01 17:23:12 +0100353 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
354 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100355
Raef Colese9479a02022-09-01 16:06:35 +0100356 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
357 height++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100358 {
359 parent_node_id = curr_node_id / 2;
360
361 /* Left/right node ordering matters for the hash */
362 if( curr_node_id & 1 )
363 {
Raef Coles57d53282022-09-27 11:30:51 +0100364 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100365 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100366 right_node = Tc_candidate_root_node;
Raef Coles8ff6df52021-07-21 12:42:15 +0100367 }
368 else
369 {
370 left_node = Tc_candidate_root_node;
Raef Coles57d53282022-09-27 11:30:51 +0100371 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
Raef Colese9479a02022-09-01 16:06:35 +0100372 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100373 }
374
Raef Colese9479a02022-09-01 16:06:35 +0100375 create_merkle_internal_value( &ctx->params, left_node, right_node,
376 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100377
378 curr_node_id /= 2;
379 }
380
Raef Colesf5632d32022-09-01 09:56:52 +0100381 if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100382 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100383 {
384 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
385 }
386
387 return( 0 );
388}
389
Raef Coles5127e852022-10-07 10:35:56 +0100390#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100391
Raef Coles0a967cc2022-09-02 17:46:15 +0100392/* Calculate a full merkle tree based on a private key. This function
393 * implements RFC8554 section 5.3, and is used to generate a public key (as the
394 * public key is the root node of the merkle tree).
395 *
Raef Coles98d6e222022-09-23 09:04:04 +0100396 * ctx The LMS private context, containing a parameter
397 * set and private key material consisting of both
398 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100399 *
Raef Coles98d6e222022-09-23 09:04:04 +0100400 * tree The output tree, which is 2^(H + 1) hash outputs.
401 * In the case of H=10 we have 2048 tree nodes (of
402 * which 1024 of them are leaf nodes). Note that
403 * because the merkle tree root is 1-indexed, the 0
404 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100405 */
Raef Coles2ac352a2022-10-07 11:12:27 +0100406static int calculate_merkle_tree( const mbedtls_lms_private_t *ctx,
Raef Colese9479a02022-09-01 16:06:35 +0100407 unsigned char *tree )
Raef Colesab4f8742022-09-01 12:24:31 +0100408{
409 unsigned int priv_key_idx;
410 unsigned int r_node_idx;
411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
412
413 /* First create the leaf nodes, in ascending order */
Raef Colese9479a02022-09-01 16:06:35 +0100414 for( priv_key_idx = 0;
415 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100416 priv_key_idx++ )
417 {
Raef Colese9479a02022-09-01 16:06:35 +0100418 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100419
Raef Colese9479a02022-09-01 16:06:35 +0100420 ret = create_merkle_leaf_value( &ctx->params,
421 ctx->ots_public_keys[priv_key_idx].public_key, r_node_idx,
422 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100423 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100424 {
425 return( ret );
426 }
427 }
428
429 /* Then the internal nodes, in reverse order so that we can guarantee the
430 * parent has been created */
Raef Colese9479a02022-09-01 16:06:35 +0100431 for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
432 r_node_idx > 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100433 r_node_idx-- )
434 {
Raef Colese9479a02022-09-01 16:06:35 +0100435 ret = create_merkle_internal_value( &ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100436 &tree[( r_node_idx * 2 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
437 &tree[( r_node_idx * 2 + 1 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
Raef Coles366d67d2022-09-01 17:23:12 +0100438 r_node_idx,
439 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100440 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100441 {
442 return( ret );
443 }
444 }
445
446 return( 0 );
447}
448
Raef Coles0a967cc2022-09-02 17:46:15 +0100449/* Calculate a path from a leaf node of the merkle tree to the root of the tree,
450 * and return the full path. This function implements RFC8554 section 5.4.1, as
451 * the merkle path is the main component of an LMS signature.
452 *
Raef Coles98d6e222022-09-23 09:04:04 +0100453 * ctx The LMS private context, containing a parameter
454 * set and private key material consisting of both
455 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100456 *
Raef Coles98d6e222022-09-23 09:04:04 +0100457 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100458 *
Raef Coles98d6e222022-09-23 09:04:04 +0100459 * tree The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100460 */
Raef Colesab4f8742022-09-01 12:24:31 +0100461static int get_merkle_path( mbedtls_lms_private_t *ctx,
462 unsigned int leaf_node_id,
Raef Colese9479a02022-09-01 16:06:35 +0100463 unsigned char *path )
Raef Colesab4f8742022-09-01 12:24:31 +0100464{
Raef Colese9479a02022-09-01 16:06:35 +0100465 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Colesab4f8742022-09-01 12:24:31 +0100466 unsigned int curr_node_id = leaf_node_id;
467 unsigned int adjacent_node_id;
468 unsigned int height;
469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
470
Raef Coles9b88ee52022-09-02 12:04:21 +0100471 ret = calculate_merkle_tree( ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100472 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100473 {
474 return( ret );
475 }
476
Raef Colese9479a02022-09-01 16:06:35 +0100477 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
478 height++ )
Raef Colesab4f8742022-09-01 12:24:31 +0100479 {
480 adjacent_node_id = curr_node_id ^ 1;
481
Raef Colese9479a02022-09-01 16:06:35 +0100482 memcpy( &path[height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
483 &tree[adjacent_node_id],
484 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Colesab4f8742022-09-01 12:24:31 +0100485
486 curr_node_id >>=1;
487 }
488
489 return( 0 );
490}
491
Raef Colesbe3bdd82022-10-07 12:04:24 +0100492void mbedtls_lms_private_init( mbedtls_lms_private_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100493{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100494 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) ) ;
Raef Coles01c71a12022-08-31 15:55:00 +0100495}
Raef Coles8ff6df52021-07-21 12:42:15 +0100496
Raef Colesbe3bdd82022-10-07 12:04:24 +0100497void mbedtls_lms_private_free( mbedtls_lms_private_t *ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100498{
499 unsigned int idx;
500
Raef Colesf5632d32022-09-01 09:56:52 +0100501 if( ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100502 {
Raef Colese9479a02022-09-01 16:06:35 +0100503 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100504 {
Raef Colesbe3bdd82022-10-07 12:04:24 +0100505 mbedtls_lmots_private_free( &ctx->ots_private_keys[idx] );
506 mbedtls_lmots_public_free( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100507 }
508
Raef Colesdc8fb792022-10-07 13:27:54 +0100509 if( ctx->ots_private_keys != NULL )
510 mbedtls_free( ctx->ots_private_keys );
511
512 if( ctx->ots_public_keys != NULL )
513 mbedtls_free( ctx->ots_public_keys );
Raef Coles8ff6df52021-07-21 12:42:15 +0100514 }
515
Raef Colesbe3bdd82022-10-07 12:04:24 +0100516 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100517}
Raef Coles8ff6df52021-07-21 12:42:15 +0100518
Raef Coles01c71a12022-08-31 15:55:00 +0100519
520int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
521 mbedtls_lms_algorithm_type_t type,
522 mbedtls_lmots_algorithm_type_t otstype,
523 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100524 void* p_rng, const unsigned char *seed,
Raef Coles01c71a12022-08-31 15:55:00 +0100525 size_t seed_size )
526{
527 unsigned int idx = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
529
Raef Coles8ff6df52021-07-21 12:42:15 +0100530 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
531 {
532 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
533 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100534
Raef Coles8ff6df52021-07-21 12:42:15 +0100535 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
536 {
537 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
538 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100539
Raef Colesf5632d32022-09-01 09:56:52 +0100540 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100541 {
542 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
543 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100544
Raef Colesf5632d32022-09-01 09:56:52 +0100545 ctx->params.type = type;
546 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100547
548 f_rng( p_rng,
Raef Colesf5632d32022-09-01 09:56:52 +0100549 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100550 MBEDTLS_LMOTS_I_KEY_ID_LEN );
551
Raef Coles40f184c2022-09-22 18:30:33 +0100552 ctx->ots_private_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100553 sizeof( *ctx->ots_private_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100554 if( ctx->ots_private_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100555 {
556 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
557 goto exit;
558 }
559
Raef Coles40f184c2022-09-22 18:30:33 +0100560 ctx->ots_public_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Colesdc8fb792022-10-07 13:27:54 +0100561 sizeof( *ctx->ots_public_keys ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100562 if( ctx->ots_public_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100563 {
Raef Colesdc8fb792022-10-07 13:27:54 +0100564 /* Free just the ots private keys (since they've been allocated at this
565 * point) so that we can pass the context to lms_private_free (which
566 * will not try to free the private keys since have_private_key is not
567 * set.
568 */
569 mbedtls_free(ctx->ots_private_keys);
570 ctx->ots_private_keys = NULL;
Raef Coles01c71a12022-08-31 15:55:00 +0100571 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
572 goto exit;
573 }
574
Raef Colesdc8fb792022-10-07 13:27:54 +0100575 /* Now that all the allocation has succeeded we set have_private_key, since
576 * that causes lms_private_free to free the ots keys.
577 */
578 ctx->have_private_key = 1;
579
Raef Colese9479a02022-09-01 16:06:35 +0100580 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100581 {
Raef Colesbe3bdd82022-10-07 12:04:24 +0100582 mbedtls_lmots_private_init( &ctx->ots_private_keys[idx] );
583 mbedtls_lmots_public_init( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100584 }
585
586
Raef Colese9479a02022-09-01 16:06:35 +0100587 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100588 {
Raef Colesf5632d32022-09-01 09:56:52 +0100589 ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100590 otstype,
Raef Colesf5632d32022-09-01 09:56:52 +0100591 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100592 idx, seed, seed_size );
Raef Colese0a17612022-09-02 16:04:47 +0100593 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100594 goto exit;
595
Raef Colesf5632d32022-09-01 09:56:52 +0100596 ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
597 &ctx->ots_private_keys[idx] );
Raef Colese0a17612022-09-02 16:04:47 +0100598 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100599 goto exit;
600 }
601
Raef Colesf5632d32022-09-01 09:56:52 +0100602 ctx->q_next_usable_key = 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100603
604exit:
Raef Colese0a17612022-09-02 16:04:47 +0100605 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100606 {
Raef Colesdc8fb792022-10-07 13:27:54 +0100607 mbedtls_lms_private_free(ctx);
Raef Coles01c71a12022-08-31 15:55:00 +0100608 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100609
Raef Colesdc8fb792022-10-07 13:27:54 +0100610 return( ret );
Raef Coles8ff6df52021-07-21 12:42:15 +0100611}
612
Raef Coles01c71a12022-08-31 15:55:00 +0100613int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
Raef Coles2ac352a2022-10-07 11:12:27 +0100614 const mbedtls_lms_private_t *priv_ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100615{
Raef Colese9479a02022-09-01 16:06:35 +0100616 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
618
Raef Coles9b88ee52022-09-02 12:04:21 +0100619 if( ! priv_ctx->have_private_key )
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( priv_ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100625 != MBEDTLS_LMS_SHA256_M32_H10 )
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( priv_ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100631 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100632 {
633 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
634 }
635
Raef Colesf5632d32022-09-01 09:56:52 +0100636 memcpy( &ctx->params, &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100637 sizeof( mbedtls_lmots_parameters_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100638
Raef Coles9b88ee52022-09-02 12:04:21 +0100639 ret = calculate_merkle_tree( priv_ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100640 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100641 {
642 return( ret );
643 }
644
645 /* Root node is always at position 1, due to 1-based indexing */
Raef Colese9479a02022-09-01 16:06:35 +0100646 memcpy( ctx->T_1_pub_key, &tree[1],
Raef Coles9b88ee52022-09-02 12:04:21 +0100647 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100648
Raef Colesf5632d32022-09-01 09:56:52 +0100649 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100650
651 return( 0 );
652}
653
Raef Coles01c71a12022-08-31 15:55:00 +0100654
Raef Coles2ac352a2022-10-07 11:12:27 +0100655int mbedtls_lms_export_public_key( const mbedtls_lms_public_t *ctx,
Raef Coles366d67d2022-09-01 17:23:12 +0100656 unsigned char *key,
Raef Coles01c71a12022-08-31 15:55:00 +0100657 size_t key_size, size_t *key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100658{
Raef Coles9b88ee52022-09-02 12:04:21 +0100659 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
660 {
Raef Coles01c71a12022-08-31 15:55:00 +0100661 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
662 }
663
Raef Colesf5632d32022-09-01 09:56:52 +0100664 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100665 {
666 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
667 }
668
Raef Colesad054252022-09-27 10:59:16 +0100669 mbedtls_lms_unsigned_int_to_network_bytes(
Raef Colesf5632d32022-09-01 09:56:52 +0100670 ctx->params.type,
Raef Coles57d53282022-09-27 11:30:51 +0100671 MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET );
Raef Colesad054252022-09-27 10:59:16 +0100672 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.otstype,
673 MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100674 key + PUBLIC_KEY_OTSTYPE_OFFSET );
675 memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100676 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100677 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles57d53282022-09-27 11:30:51 +0100678 memcpy( key +PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100679 ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100680 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100681
Raef Coles9b88ee52022-09-02 12:04:21 +0100682 if( key_len != NULL )
683 {
Raef Colese9479a02022-09-01 16:06:35 +0100684 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100685 }
686
687 return( 0 );
688}
689
690
691int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
692 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles2ac352a2022-10-07 11:12:27 +0100693 void* p_rng, const unsigned char *msg,
694 unsigned int msg_size, unsigned char *sig, size_t sig_size,
695 size_t *sig_len )
Raef Coles01c71a12022-08-31 15:55:00 +0100696{
697 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
699
Raef Colesf5632d32022-09-01 09:56:52 +0100700 if( ! ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100701 {
702 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
703 }
704
Raef Colese9479a02022-09-01 16:06:35 +0100705 if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles01c71a12022-08-31 15:55:00 +0100706 {
707 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
708 }
709
Raef Colesf5632d32022-09-01 09:56:52 +0100710 if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100711 {
712 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
713 }
714
Raef Colesf5632d32022-09-01 09:56:52 +0100715 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100716 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100717 {
718 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
719 }
720
Raef Colese9479a02022-09-01 16:06:35 +0100721 if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100722 {
Raef Coles01c71a12022-08-31 15:55:00 +0100723 return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
Raef Coles8ff6df52021-07-21 12:42:15 +0100724 }
725
726
Raef Colesf5632d32022-09-01 09:56:52 +0100727 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100728 /* This new value must _always_ be written back to the disk before the
729 * signature is returned.
730 */
Raef Colesf5632d32022-09-01 09:56:52 +0100731 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100732
Raef Colesf5632d32022-09-01 09:56:52 +0100733 ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
Raef Coles01c71a12022-08-31 15:55:00 +0100734 f_rng, p_rng, msg, msg_size,
Raef Coles57d53282022-09-27 11:30:51 +0100735 sig + SIG_OTS_SIG_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100736 MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype),
737 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100738 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100739 {
Raef Coles8ff6df52021-07-21 12:42:15 +0100740 return( ret );
741 }
742
Raef Colesad054252022-09-27 10:59:16 +0100743 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
744 MBEDTLS_LMS_TYPE_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100745 sig + SIG_TYPE_OFFSET(ctx->params.otstype) );
Raef Colesad054252022-09-27 10:59:16 +0100746 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
747 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles57d53282022-09-27 11:30:51 +0100748 sig + SIG_Q_LEAF_ID_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100749
Raef Colese9479a02022-09-01 16:06:35 +0100750 ret = get_merkle_path( ctx,
Raef Coles366d67d2022-09-01 17:23:12 +0100751 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100752 sig + SIG_PATH_OFFSET(ctx->params.otstype) );
Raef Colese0a17612022-09-02 16:04:47 +0100753 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100754 {
755 return( ret );
756 }
757
Raef Coles9b88ee52022-09-02 12:04:21 +0100758 if( sig_len != NULL )
759 {
Raef Colese9479a02022-09-01 16:06:35 +0100760 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100761 }
762
763
Raef Coles8ff6df52021-07-21 12:42:15 +0100764 return( 0 );
765}
766
Raef Coles5127e852022-10-07 10:35:56 +0100767#endif /* defined(MBEDTLS_LMS_PRIVATE) */
768#endif /* defined(MBEDTLS_LMS_C) */