blob: 653530c75d7d30f4d31647484672e24cde7cff3c [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 Colese9479a02022-09-01 16:06:35 +010057#define MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET (0)
Raef Coles366d67d2022-09-01 17:23:12 +010058#define MBEDTLS_LMS_SIG_OTS_SIG_OFFSET (MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET + \
59 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
60#define MBEDTLS_LMS_SIG_TYPE_OFFSET(otstype) (MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + \
61 MBEDTLS_LMOTS_SIG_LEN(otstype))
62#define MBEDTLS_LMS_SIG_PATH_OFFSET(otstype) (MBEDTLS_LMS_SIG_TYPE_OFFSET(otstype) + \
63 MBEDTLS_LMS_TYPE_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010064
65#define MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET (0)
Raef Coles366d67d2022-09-01 17:23:12 +010066#define MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET + \
67 MBEDTLS_LMS_TYPE_LEN)
68#define MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET + \
69 MBEDTLS_LMOTS_TYPE_LEN)
70#define MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET (MBEDTLS_LMS_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 */
75#define MBEDTLS_LMS_H_TREE_HEIGHT_MAX 10
76#define MERKLE_TREE_NODE_AM_MAX (1u << (MBEDTLS_LMS_H_TREE_HEIGHT_MAX + 1u))
77#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;
110 psa_status_t status;
111 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100112 unsigned char r_node_idx_bytes[4];
113 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
114
Raef Colesc8f96042022-08-25 13:49:54 +0100115 op = psa_hash_operation_init( );
116 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
117 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100118 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100119 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100120
Raef Colese9479a02022-09-01 16:06:35 +0100121 status = psa_hash_update( &op, params->I_key_identifier,
122 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100123 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100124 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100125 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100126
Raef Coles01c71a12022-08-31 15:55:00 +0100127 unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100128 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
129 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100130 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100131 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100132
Raef Coles01c71a12022-08-31 15:55:00 +0100133 status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100134 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100135 if( ret != 0 )
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_update( &op, pub_key,
139 MBEDTLS_LMOTS_N_HASH_LEN(params->otstype) );
Raef Colesc8f96042022-08-25 13:49:54 +0100140 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100141 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100142 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100143
Raef Colese9479a02022-09-01 16:06:35 +0100144 status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100145 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100146 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100147 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100148 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100149
Raef Coles01c71a12022-08-31 15:55:00 +0100150exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100151 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100152
153 return( ret );
154}
155
Raef Coles0a967cc2022-09-02 17:46:15 +0100156/* Calculate the value of an internal node of the merkle tree (which is a hash
157 * of a public key and some other parameters like the node index). This function
158 * implements RFC8554 section 5.3, in the case where r < 2^h.
159 *
Raef Coles98d6e222022-09-23 09:04:04 +0100160 * params The LMS parameter set, the underlying LMOTS
161 * parameter set, and I value which describe the key
162 * being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100163 *
Raef Coles98d6e222022-09-23 09:04:04 +0100164 * left_node The value of the child of this node which is on
165 * the left-hand side. As with all nodes on the
166 * merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100167 *
Raef Coles98d6e222022-09-23 09:04:04 +0100168 * right_node The value of the child of this node which is on
169 * the right-hand side. As with all nodes on the
170 * merkle tree, this is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100171 *
Raef Coles98d6e222022-09-23 09:04:04 +0100172 * r_node_idx The index of this node in the merkle tree. Note
173 * that the root node of the merkle tree is
174 * 1-indexed.
Raef Coles0a967cc2022-09-02 17:46:15 +0100175 *
Raef Coles98d6e222022-09-23 09:04:04 +0100176 * out The output node value, which is a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100177 */
Raef Colese9479a02022-09-01 16:06:35 +0100178static int create_merkle_internal_value( const mbedtls_lms_parameters_t *params,
179 const unsigned char *left_node,
180 const unsigned char *right_node,
Raef Colesebd35b52022-09-01 11:52:17 +0100181 unsigned int r_node_idx,
Raef Colese9479a02022-09-01 16:06:35 +0100182 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100183{
Raef Colesc8f96042022-08-25 13:49:54 +0100184 psa_hash_operation_t op;
185 psa_status_t status;
186 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100187 unsigned char r_node_idx_bytes[4];
188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
189
Raef Colesc8f96042022-08-25 13:49:54 +0100190 op = psa_hash_operation_init( );
191 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
192 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100193 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100194 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100195
Raef Colese9479a02022-09-01 16:06:35 +0100196 status = psa_hash_update( &op, params->I_key_identifier,
197 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100198 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100199 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100200 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100201
Raef Coles01c71a12022-08-31 15:55:00 +0100202 unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100203 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
204 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100205 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100206 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100207
Raef Colesfa24f9d2022-09-02 17:46:52 +0100208 status = psa_hash_update( &op, D_INTR_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100209 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100210 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100211 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100212
Raef Colese9479a02022-09-01 16:06:35 +0100213 status = psa_hash_update( &op, left_node,
214 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100215 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100216 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100217 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100218
Raef Colese9479a02022-09-01 16:06:35 +0100219 status = psa_hash_update( &op, right_node,
220 MBEDTLS_LMS_M_NODE_BYTES(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100221 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100222 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100223 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100224
Raef Colese9479a02022-09-01 16:06:35 +0100225 ret = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100226 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100227 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100228 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100229 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100230
Raef Coles01c71a12022-08-31 15:55:00 +0100231exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100232 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100233
234 return ret;
235}
236
Raef Coles01c71a12022-08-31 15:55:00 +0100237void mbedtls_lms_init_public( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100238{
Raef Coles01c71a12022-08-31 15:55:00 +0100239 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100240}
241
Raef Coles01c71a12022-08-31 15:55:00 +0100242void mbedtls_lms_free_public( mbedtls_lms_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100243{
Raef Coles01c71a12022-08-31 15:55:00 +0100244 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100245}
246
Raef Coles01c71a12022-08-31 15:55:00 +0100247int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
248 const unsigned char *key, size_t key_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100249{
Raef Coles01c71a12022-08-31 15:55:00 +0100250 mbedtls_lms_algorithm_type_t type;
251 mbedtls_lmots_algorithm_type_t otstype;
252
Raef Colese9479a02022-09-01 16:06:35 +0100253 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100254 {
255 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
256 }
257
Raef Coles366d67d2022-09-01 17:23:12 +0100258 type = network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
259 key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100260 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100261 {
262 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
263 }
Raef Colesf5632d32022-09-01 09:56:52 +0100264 ctx->params.type = type;
Raef Coles8ff6df52021-07-21 12:42:15 +0100265
Raef Coles01c71a12022-08-31 15:55:00 +0100266 otstype = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100267 key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100268 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
269 {
270 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
271 }
Raef Colesf5632d32022-09-01 09:56:52 +0100272 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100273
Raef Colesf5632d32022-09-01 09:56:52 +0100274 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100275 key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET,
276 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesf5632d32022-09-01 09:56:52 +0100277 memcpy( ctx->T_1_pub_key, key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100278 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100279
Raef Colesf5632d32022-09-01 09:56:52 +0100280 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100281
282 return( 0 );
283}
284
Raef Coles01c71a12022-08-31 15:55:00 +0100285int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
286 const unsigned char *msg, size_t msg_size,
287 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100288{
289 unsigned int q_leaf_identifier;
Raef Colese9479a02022-09-01 16:06:35 +0100290 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
291 unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100292 unsigned int height;
293 unsigned int curr_node_id;
294 unsigned int parent_node_id;
295 const unsigned char* left_node;
Raef Coles01c71a12022-08-31 15:55:00 +0100296 const unsigned char* right_node;
297 mbedtls_lmots_parameters_t ots_params;
Raef Coles8ff6df52021-07-21 12:42:15 +0100298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299
Raef Colesf5632d32022-09-01 09:56:52 +0100300 if( ! ctx->have_public_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100301 {
302 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
303 }
304
Raef Colese9479a02022-09-01 16:06:35 +0100305 if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100306 {
307 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
308 }
309
Raef Colesf5632d32022-09-01 09:56:52 +0100310 if( ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100311 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100312 {
313 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
314 }
315
Raef Colesf5632d32022-09-01 09:56:52 +0100316 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100317 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100318 {
319 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
320 }
321
Raef Colese9479a02022-09-01 16:06:35 +0100322 if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles9b88ee52022-09-02 12:04:21 +0100323 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET )
Raef Colese9479a02022-09-01 16:06:35 +0100324 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100325 {
326 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
327 }
328
Raef Colese9479a02022-09-01 16:06:35 +0100329 if( network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100330 sig + MBEDTLS_LMS_SIG_TYPE_OFFSET(ctx->params.otstype))
Raef Colese9479a02022-09-01 16:06:35 +0100331 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100332 {
333 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
334 }
335
336
Raef Coles01c71a12022-08-31 15:55:00 +0100337 q_leaf_identifier = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100338 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100339
Raef Colese9479a02022-09-01 16:06:35 +0100340 if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100341 {
342 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
343 }
344
Raef Coles9b88ee52022-09-02 12:04:21 +0100345 memcpy( ots_params.I_key_identifier,
346 ctx->params.I_key_identifier,
347 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100348 unsigned_int_to_network_bytes( q_leaf_identifier,
349 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Colesf5632d32022-09-01 09:56:52 +0100350 ots_params.q_leaf_identifier );
351 ots_params.type = ctx->params.otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100352
Raef Coles366d67d2022-09-01 17:23:12 +0100353 ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg,
354 msg_size, sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
355 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), Kc_candidate_ots_pub_key,
Raef Coles9b88ee52022-09-02 12:04:21 +0100356 sizeof( Kc_candidate_ots_pub_key ), NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100357 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100358 {
359 return( ret );
360 }
361
Raef Colesebd35b52022-09-01 11:52:17 +0100362 create_merkle_leaf_value(
Raef Colese9479a02022-09-01 16:06:35 +0100363 &ctx->params,
364 Kc_candidate_ots_pub_key,
365 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100366 Tc_candidate_root_node );
Raef Coles8ff6df52021-07-21 12:42:15 +0100367
Raef Coles366d67d2022-09-01 17:23:12 +0100368 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
369 q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100370
Raef Colese9479a02022-09-01 16:06:35 +0100371 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
372 height++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100373 {
374 parent_node_id = curr_node_id / 2;
375
376 /* Left/right node ordering matters for the hash */
377 if( curr_node_id & 1 )
378 {
Raef Colese9479a02022-09-01 16:06:35 +0100379 left_node = sig + MBEDTLS_LMS_SIG_PATH_OFFSET(ctx->params.otstype) +
380 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100381 right_node = Tc_candidate_root_node;
Raef Coles8ff6df52021-07-21 12:42:15 +0100382 }
383 else
384 {
385 left_node = Tc_candidate_root_node;
Raef Colese9479a02022-09-01 16:06:35 +0100386 right_node = sig + MBEDTLS_LMS_SIG_PATH_OFFSET(ctx->params.otstype) +
387 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100388 }
389
Raef Colese9479a02022-09-01 16:06:35 +0100390 create_merkle_internal_value( &ctx->params, left_node, right_node,
391 parent_node_id, Tc_candidate_root_node);
Raef Coles8ff6df52021-07-21 12:42:15 +0100392
393 curr_node_id /= 2;
394 }
395
Raef Colesf5632d32022-09-01 09:56:52 +0100396 if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100397 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100398 {
399 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
400 }
401
402 return( 0 );
403}
404
Raef Colesab4f8742022-09-01 12:24:31 +0100405#ifdef MBEDTLS_LMS_PRIVATE
406
Raef Coles0a967cc2022-09-02 17:46:15 +0100407/* Calculate a full merkle tree based on a private key. This function
408 * implements RFC8554 section 5.3, and is used to generate a public key (as the
409 * public key is the root node of the merkle tree).
410 *
Raef Coles98d6e222022-09-23 09:04:04 +0100411 * ctx The LMS private context, containing a parameter
412 * set and private key material consisting of both
413 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100414 *
Raef Coles98d6e222022-09-23 09:04:04 +0100415 * tree The output tree, which is 2^(H + 1) hash outputs.
416 * In the case of H=10 we have 2048 tree nodes (of
417 * which 1024 of them are leaf nodes). Note that
418 * because the merkle tree root is 1-indexed, the 0
419 * index tree node is never used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100420 */
Raef Colesab4f8742022-09-01 12:24:31 +0100421static int calculate_merkle_tree( mbedtls_lms_private_t *ctx,
Raef Colese9479a02022-09-01 16:06:35 +0100422 unsigned char *tree )
Raef Colesab4f8742022-09-01 12:24:31 +0100423{
424 unsigned int priv_key_idx;
425 unsigned int r_node_idx;
426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
427
428 /* First create the leaf nodes, in ascending order */
Raef Colese9479a02022-09-01 16:06:35 +0100429 for( priv_key_idx = 0;
430 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
Raef Colesab4f8742022-09-01 12:24:31 +0100431 priv_key_idx++ )
432 {
Raef Colese9479a02022-09-01 16:06:35 +0100433 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
Raef Colesab4f8742022-09-01 12:24:31 +0100434
Raef Colese9479a02022-09-01 16:06:35 +0100435 ret = create_merkle_leaf_value( &ctx->params,
436 ctx->ots_public_keys[priv_key_idx].public_key, r_node_idx,
437 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100438 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100439 {
440 return( ret );
441 }
442 }
443
444 /* Then the internal nodes, in reverse order so that we can guarantee the
445 * parent has been created */
Raef Colese9479a02022-09-01 16:06:35 +0100446 for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
447 r_node_idx > 0;
Raef Colesab4f8742022-09-01 12:24:31 +0100448 r_node_idx-- )
449 {
Raef Colese9479a02022-09-01 16:06:35 +0100450 ret = create_merkle_internal_value( &ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100451 &tree[( r_node_idx * 2 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
452 &tree[( r_node_idx * 2 + 1 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
Raef Coles366d67d2022-09-01 17:23:12 +0100453 r_node_idx,
454 &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
Raef Colese0a17612022-09-02 16:04:47 +0100455 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100456 {
457 return( ret );
458 }
459 }
460
461 return( 0 );
462}
463
Raef Coles0a967cc2022-09-02 17:46:15 +0100464/* Calculate a path from a leaf node of the merkle tree to the root of the tree,
465 * and return the full path. This function implements RFC8554 section 5.4.1, as
466 * the merkle path is the main component of an LMS signature.
467 *
Raef Coles98d6e222022-09-23 09:04:04 +0100468 * ctx The LMS private context, containing a parameter
469 * set and private key material consisting of both
470 * public and private OTS.
Raef Coles0a967cc2022-09-02 17:46:15 +0100471 *
Raef Coles98d6e222022-09-23 09:04:04 +0100472 * leaf_node_id Which leaf node to calculate the path from.
Raef Coles0a967cc2022-09-02 17:46:15 +0100473 *
Raef Coles98d6e222022-09-23 09:04:04 +0100474 * tree The output path, which is H hash outputs.
Raef Coles0a967cc2022-09-02 17:46:15 +0100475 */
Raef Colesab4f8742022-09-01 12:24:31 +0100476static int get_merkle_path( mbedtls_lms_private_t *ctx,
477 unsigned int leaf_node_id,
Raef Colese9479a02022-09-01 16:06:35 +0100478 unsigned char *path )
Raef Colesab4f8742022-09-01 12:24:31 +0100479{
Raef Colese9479a02022-09-01 16:06:35 +0100480 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Colesab4f8742022-09-01 12:24:31 +0100481 unsigned int curr_node_id = leaf_node_id;
482 unsigned int adjacent_node_id;
483 unsigned int height;
484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
485
Raef Coles9b88ee52022-09-02 12:04:21 +0100486 ret = calculate_merkle_tree( ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100487 if( ret != 0 )
Raef Colesab4f8742022-09-01 12:24:31 +0100488 {
489 return( ret );
490 }
491
Raef Colese9479a02022-09-01 16:06:35 +0100492 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
493 height++ )
Raef Colesab4f8742022-09-01 12:24:31 +0100494 {
495 adjacent_node_id = curr_node_id ^ 1;
496
Raef Colese9479a02022-09-01 16:06:35 +0100497 memcpy( &path[height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
498 &tree[adjacent_node_id],
499 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Colesab4f8742022-09-01 12:24:31 +0100500
501 curr_node_id >>=1;
502 }
503
504 return( 0 );
505}
506
Raef Coles01c71a12022-08-31 15:55:00 +0100507void mbedtls_lms_init_private( mbedtls_lms_private_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100508{
Raef Coles01c71a12022-08-31 15:55:00 +0100509 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ;
510}
Raef Coles8ff6df52021-07-21 12:42:15 +0100511
Raef Coles01c71a12022-08-31 15:55:00 +0100512void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx )
513{
514 unsigned int idx;
515
Raef Colesf5632d32022-09-01 09:56:52 +0100516 if( ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100517 {
Raef Colese9479a02022-09-01 16:06:35 +0100518 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100519 {
Raef Colesf5632d32022-09-01 09:56:52 +0100520 mbedtls_lmots_free_private( &ctx->ots_private_keys[idx] );
521 mbedtls_lmots_free_public( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100522 }
523
Raef Colesf5632d32022-09-01 09:56:52 +0100524 mbedtls_free( ctx->ots_private_keys );
525 mbedtls_free( ctx->ots_public_keys );
Raef Coles8ff6df52021-07-21 12:42:15 +0100526 }
527
Raef Coles39820402022-09-23 09:12:54 +0100528 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_private_t ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100529}
Raef Coles8ff6df52021-07-21 12:42:15 +0100530
Raef Coles01c71a12022-08-31 15:55:00 +0100531
532int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
533 mbedtls_lms_algorithm_type_t type,
534 mbedtls_lmots_algorithm_type_t otstype,
535 int (*f_rng)(void *, unsigned char *, size_t),
536 void* p_rng, unsigned char *seed,
537 size_t seed_size )
538{
539 unsigned int idx = 0;
540 unsigned int free_idx = 0;
541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
542
Raef Coles8ff6df52021-07-21 12:42:15 +0100543 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
544 {
545 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
546 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100547
Raef Coles8ff6df52021-07-21 12:42:15 +0100548 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
549 {
550 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
551 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100552
Raef Colesf5632d32022-09-01 09:56:52 +0100553 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100554 {
555 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
556 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100557
Raef Colesf5632d32022-09-01 09:56:52 +0100558 ctx->params.type = type;
559 ctx->params.otstype = otstype;
Raef Coles01c71a12022-08-31 15:55:00 +0100560
561 f_rng( p_rng,
Raef Colesf5632d32022-09-01 09:56:52 +0100562 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100563 MBEDTLS_LMOTS_I_KEY_ID_LEN );
564
Raef Coles40f184c2022-09-22 18:30:33 +0100565 ctx->ots_private_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100566 sizeof( mbedtls_lmots_private_t ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100567 if( ctx->ots_private_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100568 {
569 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
570 goto exit;
571 }
572
Raef Coles40f184c2022-09-22 18:30:33 +0100573 ctx->ots_public_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100574 sizeof( mbedtls_lmots_public_t ) );
Raef Colesf5632d32022-09-01 09:56:52 +0100575 if( ctx->ots_public_keys == NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100576 {
577 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
578 goto exit;
579 }
580
Raef Colese9479a02022-09-01 16:06:35 +0100581 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100582 {
Raef Colesf5632d32022-09-01 09:56:52 +0100583 mbedtls_lmots_init_private( &ctx->ots_private_keys[idx] );
584 mbedtls_lmots_init_public( &ctx->ots_public_keys[idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100585 }
586
587
Raef Colese9479a02022-09-01 16:06:35 +0100588 for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100589 {
Raef Colesf5632d32022-09-01 09:56:52 +0100590 ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100591 otstype,
Raef Colesf5632d32022-09-01 09:56:52 +0100592 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100593 idx, seed, seed_size );
Raef Colese0a17612022-09-02 16:04:47 +0100594 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100595 goto exit;
596
Raef Colesf5632d32022-09-01 09:56:52 +0100597 ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
598 &ctx->ots_private_keys[idx] );
Raef Colese0a17612022-09-02 16:04:47 +0100599 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100600 goto exit;
601 }
602
Raef Colesf5632d32022-09-01 09:56:52 +0100603 ctx->q_next_usable_key = 0;
604 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100605
606exit:
Raef Colese0a17612022-09-02 16:04:47 +0100607 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100608 {
Raef Coles9b88ee52022-09-02 12:04:21 +0100609 for ( free_idx = 0; free_idx < idx; free_idx++ )
610 {
Raef Colesf5632d32022-09-01 09:56:52 +0100611 mbedtls_lmots_free_private( &ctx->ots_private_keys[free_idx] );
612 mbedtls_lmots_free_public( &ctx->ots_public_keys[free_idx] );
Raef Coles01c71a12022-08-31 15:55:00 +0100613 }
614
Raef Colesf5632d32022-09-01 09:56:52 +0100615 mbedtls_free( ctx->ots_private_keys );
616 mbedtls_free( ctx->ots_public_keys );
Raef Coles01c71a12022-08-31 15:55:00 +0100617 return( ret );
618 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100619
620 return( 0 );
621}
622
Raef Coles01c71a12022-08-31 15:55:00 +0100623int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
624 mbedtls_lms_private_t *priv_ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100625{
Raef Colese9479a02022-09-01 16:06:35 +0100626 unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100627 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
628
Raef Coles9b88ee52022-09-02 12:04:21 +0100629 if( ! priv_ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100630 {
631 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
632 }
633
Raef Colesf5632d32022-09-01 09:56:52 +0100634 if( priv_ctx->params.type
Raef Coles01c71a12022-08-31 15:55:00 +0100635 != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100636 {
637 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
638 }
639
Raef Colesf5632d32022-09-01 09:56:52 +0100640 if( priv_ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100641 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100642 {
643 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
644 }
645
Raef Colesf5632d32022-09-01 09:56:52 +0100646 memcpy( &ctx->params, &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100647 sizeof( mbedtls_lmots_parameters_t ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100648
Raef Coles9b88ee52022-09-02 12:04:21 +0100649 ret = calculate_merkle_tree( priv_ctx, ( unsigned char * )tree );
Raef Colese0a17612022-09-02 16:04:47 +0100650 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100651 {
652 return( ret );
653 }
654
655 /* Root node is always at position 1, due to 1-based indexing */
Raef Colese9479a02022-09-01 16:06:35 +0100656 memcpy( ctx->T_1_pub_key, &tree[1],
Raef Coles9b88ee52022-09-02 12:04:21 +0100657 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100658
Raef Colesf5632d32022-09-01 09:56:52 +0100659 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100660
661 return( 0 );
662}
663
Raef Coles01c71a12022-08-31 15:55:00 +0100664
Raef Coles366d67d2022-09-01 17:23:12 +0100665int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx,
666 unsigned char *key,
Raef Coles01c71a12022-08-31 15:55:00 +0100667 size_t key_size, size_t *key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100668{
Raef Coles9b88ee52022-09-02 12:04:21 +0100669 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
670 {
Raef Coles01c71a12022-08-31 15:55:00 +0100671 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
672 }
673
Raef Colesf5632d32022-09-01 09:56:52 +0100674 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100675 {
676 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
677 }
678
679 unsigned_int_to_network_bytes(
Raef Colesf5632d32022-09-01 09:56:52 +0100680 ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100681 MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET );
Raef Coles366d67d2022-09-01 17:23:12 +0100682 unsigned_int_to_network_bytes( ctx->params.otstype, MBEDTLS_LMOTS_TYPE_LEN,
683 key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100684 memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100685 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100686 MBEDTLS_LMOTS_I_KEY_ID_LEN );
687 memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100688 ctx->T_1_pub_key,
Raef Colese9479a02022-09-01 16:06:35 +0100689 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100690
Raef Coles9b88ee52022-09-02 12:04:21 +0100691 if( key_len != NULL )
692 {
Raef Colese9479a02022-09-01 16:06:35 +0100693 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100694 }
695
696 return( 0 );
697}
698
699
700int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
701 int (*f_rng)(void *, unsigned char *, size_t),
702 void* p_rng, unsigned char *msg, unsigned int msg_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100703 unsigned char *sig, size_t sig_size, size_t *sig_len )
Raef Coles01c71a12022-08-31 15:55:00 +0100704{
705 uint32_t q_leaf_identifier;
Raef Coles8ff6df52021-07-21 12:42:15 +0100706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
707
Raef Colesf5632d32022-09-01 09:56:52 +0100708 if( ! ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100709 {
710 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
711 }
712
Raef Colese9479a02022-09-01 16:06:35 +0100713 if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
Raef Coles01c71a12022-08-31 15:55:00 +0100714 {
715 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
716 }
717
Raef Colesf5632d32022-09-01 09:56:52 +0100718 if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100719 {
720 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
721 }
722
Raef Colesf5632d32022-09-01 09:56:52 +0100723 if( ctx->params.otstype
Raef Coles01c71a12022-08-31 15:55:00 +0100724 != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100725 {
726 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
727 }
728
Raef Colese9479a02022-09-01 16:06:35 +0100729 if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100730 {
Raef Coles01c71a12022-08-31 15:55:00 +0100731 return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
Raef Coles8ff6df52021-07-21 12:42:15 +0100732 }
733
734
Raef Colesf5632d32022-09-01 09:56:52 +0100735 q_leaf_identifier = ctx->q_next_usable_key;
Raef Coles01c71a12022-08-31 15:55:00 +0100736 /* This new value must _always_ be written back to the disk before the
737 * signature is returned.
738 */
Raef Colesf5632d32022-09-01 09:56:52 +0100739 ctx->q_next_usable_key += 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100740
Raef Colesf5632d32022-09-01 09:56:52 +0100741 ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
Raef Coles01c71a12022-08-31 15:55:00 +0100742 f_rng, p_rng, msg, msg_size,
743 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100744 MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype),
745 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100746 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100747 {
Raef Coles8ff6df52021-07-21 12:42:15 +0100748 return( ret );
749 }
750
Raef Coles366d67d2022-09-01 17:23:12 +0100751 unsigned_int_to_network_bytes( ctx->params.type, MBEDTLS_LMS_TYPE_LEN,
752 sig + MBEDTLS_LMS_SIG_TYPE_OFFSET(ctx->params.otstype) );
Raef Coles01c71a12022-08-31 15:55:00 +0100753 unsigned_int_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Coles9b88ee52022-09-02 12:04:21 +0100754 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100755
Raef Colese9479a02022-09-01 16:06:35 +0100756 ret = get_merkle_path( ctx,
Raef Coles366d67d2022-09-01 17:23:12 +0100757 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
758 sig + MBEDTLS_LMS_SIG_PATH_OFFSET(ctx->params.otstype) );
Raef Colese0a17612022-09-02 16:04:47 +0100759 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100760 {
761 return( ret );
762 }
763
Raef Coles9b88ee52022-09-02 12:04:21 +0100764 if( sig_len != NULL )
765 {
Raef Colese9479a02022-09-01 16:06:35 +0100766 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
Raef Coles01c71a12022-08-31 15:55:00 +0100767 }
768
769
Raef Coles8ff6df52021-07-21 12:42:15 +0100770 return( 0 );
771}
772
Raef Colesab4f8742022-09-01 12:24:31 +0100773#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100774#endif /* MBEDTLS_LMS_C */