blob: fe78894f4c5c332ca1f545e8f6edb5dc896df138 [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/*
2 * The LM-OTS one-time 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 LM-OTS 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 Coles7dce69a2022-08-24 14:07:06 +010035#ifdef 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 "mbedtls/lms.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010042#include "mbedtls/platform_util.h"
43#include "mbedtls/error.h"
44
Raef Colesc8f96042022-08-25 13:49:54 +010045#include "psa/crypto.h"
46
Raef Coles366d67d2022-09-01 17:23:12 +010047#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + \
48 MBEDTLS_LMOTS_TYPE_LEN)
49#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(type) (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + \
50 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type))
Raef Coles01c71a12022-08-31 15:55:00 +010051
Raef Coles366d67d2022-09-01 17:23:12 +010052#define MBEDTLS_LMOTS_PUBLIC_KEY_TYPE_OFFSET (0)
53#define MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_TYPE_OFFSET + \
54 MBEDTLS_LMOTS_TYPE_LEN)
55#define MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET + \
56 MBEDTLS_LMOTS_I_KEY_ID_LEN)
57#define MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET + \
58 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010059
60/* We only support parameter sets that use 8-bit digits, as it does not require
61 * translation logic between digits and bytes */
62#define W_WINTERNITZ_PARAMETER (8u)
63#define CHECKSUM_LEN (2)
64#define I_DIGIT_IDX_LEN (2)
65#define J_HASH_IDX_LEN (1)
66#define D_CONST_LEN (2)
67
Raef Colese9479a02022-09-01 16:06:35 +010068/* Currently only defined for SHA256, 32 is the max hash output size */
69#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN_MAX (MBEDTLS_LMOTS_N_HASH_LEN_MAX)
70
Raef Coles9b88ee52022-09-02 12:04:21 +010071#define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
Raef Coles01c71a12022-08-31 15:55:00 +010072
Raef Coles9b88ee52022-09-02 12:04:21 +010073#define D_CONST_LEN (2)
Raef Coles01c71a12022-08-31 15:55:00 +010074static const unsigned char D_PUBLIC_CONSTANT_BYTES[D_CONST_LEN] = {0x80, 0x80};
75static const unsigned char D_MESSAGE_CONSTANT_BYTES[D_CONST_LEN] = {0x81, 0x81};
Raef Coles8ff6df52021-07-21 12:42:15 +010076
Raef Coles9b88ee52022-09-02 12:04:21 +010077void unsigned_int_to_network_bytes( unsigned int val, size_t len,
78 unsigned char *bytes )
Raef Coles8ff6df52021-07-21 12:42:15 +010079{
80 size_t idx;
81
Raef Coles9b88ee52022-09-02 12:04:21 +010082 for ( idx = 0; idx < len; idx++ )
83 {
84 bytes[idx] = ( val >> ( ( len - 1 - idx ) * 8 ) ) & 0xFF;
Raef Coles8ff6df52021-07-21 12:42:15 +010085 }
86}
87
Raef Coles9b88ee52022-09-02 12:04:21 +010088unsigned int network_bytes_to_unsigned_int( size_t len,
89 const unsigned char *bytes )
Raef Coles8ff6df52021-07-21 12:42:15 +010090{
91 size_t idx;
92 unsigned int val = 0;
93
Raef Coles9b88ee52022-09-02 12:04:21 +010094 for ( idx = 0; idx < len; idx++ )
95 {
96 val |= ( ( unsigned int )bytes[idx] ) << (8 * ( len - 1 - idx ) );
Raef Coles8ff6df52021-07-21 12:42:15 +010097 }
98
99 return val;
100}
101
Raef Colese9479a02022-09-01 16:06:35 +0100102static unsigned short lmots_checksum_calculate( const mbedtls_lmots_parameters_t *params,
103 const unsigned char* digest )
Raef Coles8ff6df52021-07-21 12:42:15 +0100104{
105 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100106 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100107
Raef Colese9479a02022-09-01 16:06:35 +0100108 for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100109 {
Raef Coles01c71a12022-08-31 15:55:00 +0100110 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +0100111 }
112
113 return sum;
114}
115
Raef Coles01c71a12022-08-31 15:55:00 +0100116static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *params,
117 const unsigned char *msg,
118 size_t msg_len,
Raef Colese9479a02022-09-01 16:06:35 +0100119 const unsigned char *C_random_value,
120 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100121{
Raef Colesc8f96042022-08-25 13:49:54 +0100122 psa_hash_operation_t op;
123 psa_status_t status;
124 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100125 unsigned short checksum;
126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
127
Raef Coles9b88ee52022-09-02 12:04:21 +0100128 op = psa_hash_operation_init( );
Raef Colesc8f96042022-08-25 13:49:54 +0100129 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
130 ret = mbedtls_lms_error_from_psa( status );
131 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100132 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100133
Raef Colesf5632d32022-09-01 09:56:52 +0100134 status = psa_hash_update( &op, params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100135 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100136 ret = mbedtls_lms_error_from_psa( status );
137 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100138 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100139
Raef Colesf5632d32022-09-01 09:56:52 +0100140 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100141 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100142 ret = mbedtls_lms_error_from_psa( status );
143 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100144 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100145
Raef Coles01c71a12022-08-31 15:55:00 +0100146 status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100147 ret = mbedtls_lms_error_from_psa( status );
148 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100149 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100150
Raef Colese9479a02022-09-01 16:06:35 +0100151 status = psa_hash_update( &op, C_random_value,
152 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100153 ret = mbedtls_lms_error_from_psa( status );
154 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100155 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100156
Raef Colesc8f96042022-08-25 13:49:54 +0100157 status = psa_hash_update( &op, msg, msg_len );
158 ret = mbedtls_lms_error_from_psa( status );
159 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100160 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100161
Raef Colese9479a02022-09-01 16:06:35 +0100162 status = psa_hash_finish( &op, out,
163 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type),
Raef Colesc8f96042022-08-25 13:49:54 +0100164 &output_hash_len );
165 ret = mbedtls_lms_error_from_psa( status );
166 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100167 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100168
Raef Colese9479a02022-09-01 16:06:35 +0100169 checksum = lmots_checksum_calculate( params, out );
170 unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN,
171 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100172
Raef Coles01c71a12022-08-31 15:55:00 +0100173exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100174 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100175
176 return( ret );
177}
178
Raef Coles01c71a12022-08-31 15:55:00 +0100179static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100180 const unsigned char *x_digit_array,
Raef Coles01c71a12022-08-31 15:55:00 +0100181 const unsigned char *hash_idx_min_values,
182 const unsigned char *hash_idx_max_values,
Raef Colese9479a02022-09-01 16:06:35 +0100183 unsigned char *output )
Raef Coles8ff6df52021-07-21 12:42:15 +0100184{
Raef Coles01c71a12022-08-31 15:55:00 +0100185 unsigned char i_digit_idx;
Raef Coles8ff6df52021-07-21 12:42:15 +0100186 unsigned char j_hash_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100187 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8ff6df52021-07-21 12:42:15 +0100188 unsigned char j_hash_idx_bytes[1];
Raef Coles01c71a12022-08-31 15:55:00 +0100189 /* These can't be unsigned chars, because they are sometimes set to
190 * #DIGIT_MAX_VALUE, which has a value of 256
191 */
192 unsigned int j_hash_idx_min;
193 unsigned int j_hash_idx_max;
Raef Colesc8f96042022-08-25 13:49:54 +0100194 psa_hash_operation_t op;
195 psa_status_t status;
196 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100197 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
199
Raef Coles9b88ee52022-09-02 12:04:21 +0100200 op = psa_hash_operation_init( );
Raef Coles01c71a12022-08-31 15:55:00 +0100201
Raef Colese9479a02022-09-01 16:06:35 +0100202 for ( i_digit_idx = 0;
203 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
204 i_digit_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100205 {
206
Raef Coles366d67d2022-09-01 17:23:12 +0100207 memcpy( tmp_hash,
208 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
Raef Colese9479a02022-09-01 16:06:35 +0100209 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100210
Raef Coles366d67d2022-09-01 17:23:12 +0100211 j_hash_idx_min = hash_idx_min_values != NULL ?
212 hash_idx_min_values[i_digit_idx] : 0;
213 j_hash_idx_max = hash_idx_max_values != NULL ?
214 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100215
Raef Coles9b88ee52022-09-02 12:04:21 +0100216 for ( j_hash_idx = ( unsigned char )j_hash_idx_min;
Raef Coles366d67d2022-09-01 17:23:12 +0100217 j_hash_idx < j_hash_idx_max;
218 j_hash_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100219 {
Raef Colesc8f96042022-08-25 13:49:54 +0100220 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
221 ret = mbedtls_lms_error_from_psa( status );
222 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100223 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100224
Raef Coles01c71a12022-08-31 15:55:00 +0100225 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100226 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100227 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100228 ret = mbedtls_lms_error_from_psa( status );
229 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100230 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100231
Raef Coles01c71a12022-08-31 15:55:00 +0100232 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100233 params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100234 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100235 ret = mbedtls_lms_error_from_psa( status );
236 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100237 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100238
Raef Coles366d67d2022-09-01 17:23:12 +0100239 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
240 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100241 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100242 ret = mbedtls_lms_error_from_psa( status );
243 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100244 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100245
Raef Coles366d67d2022-09-01 17:23:12 +0100246 unsigned_int_to_network_bytes( j_hash_idx, J_HASH_IDX_LEN,
247 j_hash_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100248 status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN );
249 ret = mbedtls_lms_error_from_psa( status );
250 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100251 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100252
Raef Colese9479a02022-09-01 16:06:35 +0100253 status = psa_hash_update( &op, tmp_hash,
254 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100255 ret = mbedtls_lms_error_from_psa( status );
256 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100257 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100258
Raef Coles366d67d2022-09-01 17:23:12 +0100259 status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ),
260 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100261 ret = mbedtls_lms_error_from_psa( status );
262 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100263 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100264
Raef Colesc8f96042022-08-25 13:49:54 +0100265 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100266 }
267
Raef Coles366d67d2022-09-01 17:23:12 +0100268 memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
269 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100270 }
271
Raef Coles01c71a12022-08-31 15:55:00 +0100272exit:
Raef Coles8ff6df52021-07-21 12:42:15 +0100273 if( ret )
274 {
Raef Colesc8f96042022-08-25 13:49:54 +0100275 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100276 return( ret );
277 }
278
Raef Coles01c71a12022-08-31 15:55:00 +0100279 mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) );
280
Raef Coles8ff6df52021-07-21 12:42:15 +0100281 return ret;
282}
283
Raef Coles01c71a12022-08-31 15:55:00 +0100284static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100285 const unsigned char *y_hashed_digits,
Raef Coles01c71a12022-08-31 15:55:00 +0100286 unsigned char *pub_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100287{
Raef Colesc8f96042022-08-25 13:49:54 +0100288 psa_hash_operation_t op;
289 psa_status_t status;
290 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
292
Raef Colesc8f96042022-08-25 13:49:54 +0100293 op = psa_hash_operation_init( );
294 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
295 ret = mbedtls_lms_error_from_psa( status );
296 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100297 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100298
Raef Coles01c71a12022-08-31 15:55:00 +0100299 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100300 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100301 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100302 ret = mbedtls_lms_error_from_psa( status );
303 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100304 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100305
Raef Colesf5632d32022-09-01 09:56:52 +0100306 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100307 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100308 ret = mbedtls_lms_error_from_psa( status );
309 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100310 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100311
Raef Coles01c71a12022-08-31 15:55:00 +0100312 status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100313 ret = mbedtls_lms_error_from_psa( status );
314 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100315 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100316
Raef Colese9479a02022-09-01 16:06:35 +0100317 status = psa_hash_update( &op, y_hashed_digits,
318 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
319 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100320 ret = mbedtls_lms_error_from_psa( status );
321 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100322 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100323
Raef Coles366d67d2022-09-01 17:23:12 +0100324 status = psa_hash_finish( &op, pub_key,
325 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colese9479a02022-09-01 16:06:35 +0100326 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100327 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100328
Raef Coles01c71a12022-08-31 15:55:00 +0100329exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100330 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100331 return( ret );
332}
333
Raef Coles9b88ee52022-09-02 12:04:21 +0100334int mbedtls_lms_error_from_psa( psa_status_t status )
Raef Colesc8f96042022-08-25 13:49:54 +0100335{
Raef Coles9b88ee52022-09-02 12:04:21 +0100336 switch( status )
337 {
Raef Colesc8f96042022-08-25 13:49:54 +0100338 case PSA_SUCCESS:
339 return( 0 );
340 case PSA_ERROR_HARDWARE_FAILURE:
341 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
342 case PSA_ERROR_NOT_SUPPORTED:
343 return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
344 case PSA_ERROR_BUFFER_TOO_SMALL:
345 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
346 case PSA_ERROR_INVALID_ARGUMENT:
347 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
348 default:
349 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
350 }
351}
352
Raef Coles01c71a12022-08-31 15:55:00 +0100353void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100354{
Raef Coles01c71a12022-08-31 15:55:00 +0100355 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100356}
357
Raef Coles01c71a12022-08-31 15:55:00 +0100358void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100359{
Raef Coles01c71a12022-08-31 15:55:00 +0100360 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100361}
362
Raef Coles01c71a12022-08-31 15:55:00 +0100363int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
364 const unsigned char *key, size_t key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100365{
Raef Colesf5632d32022-09-01 09:56:52 +0100366 ctx->params.type =
Raef Coles01c71a12022-08-31 15:55:00 +0100367 network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
368 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
369
Raef Colese9479a02022-09-01 16:06:35 +0100370 if ( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
371 {
372 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
373 }
374
Raef Colesf5632d32022-09-01 09:56:52 +0100375 memcpy( ctx->params.I_key_identifier,
Raef Coles366d67d2022-09-01 17:23:12 +0100376 key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
377 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100378
Raef Colesf5632d32022-09-01 09:56:52 +0100379 memcpy( ctx->params.q_leaf_identifier,
Raef Coles366d67d2022-09-01 17:23:12 +0100380 key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
381 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100382
Raef Colesf5632d32022-09-01 09:56:52 +0100383 memcpy( ctx->public_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100384 key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100385 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100386
Raef Colesf5632d32022-09-01 09:56:52 +0100387 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100388
389 return( 0 );
390}
391
Raef Coles01c71a12022-08-31 15:55:00 +0100392int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
393 const unsigned char *msg,
394 size_t msg_size,
395 const unsigned char *sig,
396 size_t sig_size,
397 unsigned char *out,
398 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100399 size_t *out_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100400{
Raef Colese9479a02022-09-01 16:06:35 +0100401 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
402 unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
404
Raef Coles01c71a12022-08-31 15:55:00 +0100405 if ( msg == NULL && msg_size != 0 )
406 {
407 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
408 }
409
Raef Colese9479a02022-09-01 16:06:35 +0100410 if ( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
411 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100412 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100413 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100414 }
415
Raef Coles01c71a12022-08-31 15:55:00 +0100416 ret = create_digit_array_with_checksum( params, msg, msg_size,
417 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
418 tmp_digit_array );
Raef Coles8ff6df52021-07-21 12:42:15 +0100419 if ( ret )
420 {
421 return ( ret );
422 }
423
Raef Coles01c71a12022-08-31 15:55:00 +0100424 ret = hash_digit_array( params,
Raef Colese9479a02022-09-01 16:06:35 +0100425 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100426 tmp_digit_array, NULL, ( unsigned char * )y_hashed_digits );
Raef Coles8ff6df52021-07-21 12:42:15 +0100427 if ( ret )
428 {
429 return ( ret );
430 }
431
Raef Colese9479a02022-09-01 16:06:35 +0100432 ret = public_key_from_hashed_digit_array( params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100433 ( unsigned char * )y_hashed_digits,
Raef Colese9479a02022-09-01 16:06:35 +0100434 out );
Raef Coles8ff6df52021-07-21 12:42:15 +0100435 if ( ret )
436 {
437 return ( ret );
438 }
439
Raef Coles01c71a12022-08-31 15:55:00 +0100440 if ( out_len != NULL )
441 {
Raef Colese9479a02022-09-01 16:06:35 +0100442 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100443 }
444
Raef Coles8ff6df52021-07-21 12:42:15 +0100445 return( 0 );
446}
447
Raef Coles01c71a12022-08-31 15:55:00 +0100448int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
449 size_t msg_size, const unsigned char *sig,
450 size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100451{
Raef Colese9479a02022-09-01 16:06:35 +0100452 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
454
Raef Coles01c71a12022-08-31 15:55:00 +0100455 if ( msg == NULL && msg_size != 0 )
456 {
457 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
458 }
459
Raef Colesf5632d32022-09-01 09:56:52 +0100460 if ( !ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100461 {
462 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
463 }
464
Raef Coles9b88ee52022-09-02 12:04:21 +0100465 if( ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100466 {
467 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
468 }
469
470 if ( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100471 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100472 {
473 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
474 }
475
Raef Colesf5632d32022-09-01 09:56:52 +0100476 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100477 msg, msg_size, sig, sig_size,
478 Kc_public_key_candidate,
Raef Colese9479a02022-09-01 16:06:35 +0100479 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100480 NULL );
Raef Coles01c71a12022-08-31 15:55:00 +0100481 if ( ret )
482 {
483 return( ret );
484 }
485
Raef Colesf5632d32022-09-01 09:56:52 +0100486 if ( memcmp( &Kc_public_key_candidate, ctx->public_key,
487 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100488 {
489 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
490 }
491
492 return( 0 );
493}
494
Raef Colesab4f8742022-09-01 12:24:31 +0100495#ifdef MBEDTLS_LMS_PRIVATE
496
Raef Coles01c71a12022-08-31 15:55:00 +0100497void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx )
498{
499 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
500}
501
502void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx )
503{
504 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
505}
506
507int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
508 mbedtls_lmots_algorithm_type_t type,
509 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
510 uint32_t q_leaf_identifier,
511 const unsigned char *seed,
512 size_t seed_size )
513{
514 psa_hash_operation_t op;
515 psa_status_t status;
516 size_t output_hash_len;
517 unsigned int i_digit_idx;
518 unsigned char i_digit_idx_bytes[2];
519 unsigned char const_bytes[1];
520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
521
Raef Colesf5632d32022-09-01 09:56:52 +0100522 if ( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100523 {
524 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
525 }
526
Raef Coles9b88ee52022-09-02 12:04:21 +0100527 if ( type != MBEDTLS_LMOTS_SHA256_N32_W8 )
528 {
Raef Coles01c71a12022-08-31 15:55:00 +0100529 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
530 }
531
Raef Colesebd35b52022-09-01 11:52:17 +0100532 op = psa_hash_operation_init( );
533
Raef Colesf5632d32022-09-01 09:56:52 +0100534 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100535
Raef Colesf5632d32022-09-01 09:56:52 +0100536 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100537 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100538 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100539
Raef Coles9b88ee52022-09-02 12:04:21 +0100540 unsigned_int_to_network_bytes( q_leaf_identifier,
541 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
542 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100543
544 unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes );
545
Raef Colese9479a02022-09-01 16:06:35 +0100546 for ( i_digit_idx = 0;
547 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
548 i_digit_idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100549 {
Raef Coles01c71a12022-08-31 15:55:00 +0100550 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
551 ret = mbedtls_lms_error_from_psa( status );
552 if ( ret != 0 )
553 goto exit;
554
555 ret = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100556 ctx->params.I_key_identifier,
557 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100558 ret = mbedtls_lms_error_from_psa( status );
559 if ( ret )
560 goto exit;
561
562 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100563 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100564 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
565 ret = mbedtls_lms_error_from_psa( status );
566 if ( ret )
567 goto exit;
568
Raef Coles366d67d2022-09-01 17:23:12 +0100569 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
570 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100571 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
572 ret = mbedtls_lms_error_from_psa( status );
573 if ( ret )
574 goto exit;
575
Raef Coles9b88ee52022-09-02 12:04:21 +0100576 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100577 ret = mbedtls_lms_error_from_psa( status );
578 if ( ret )
579 goto exit;
580
581 status = psa_hash_update( &op, seed, seed_size );
582 ret = mbedtls_lms_error_from_psa( status );
583 if ( ret )
584 goto exit;
585
586 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100587 ctx->private_key[i_digit_idx],
Raef Colese9479a02022-09-01 16:06:35 +0100588 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
589 &output_hash_len );
Raef Coles01c71a12022-08-31 15:55:00 +0100590 ret = mbedtls_lms_error_from_psa( status );
591 if ( ret )
592 goto exit;
593
594 psa_hash_abort( &op );
595 }
596
Raef Colesf5632d32022-09-01 09:56:52 +0100597 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100598
599exit:
600 if( ret )
601 {
602 psa_hash_abort( &op );
603 return( ret );
604 }
605
606 return ret;
607}
608
609int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles9b88ee52022-09-02 12:04:21 +0100610 mbedtls_lmots_private_t *priv_ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100611{
Raef Colese9479a02022-09-01 16:06:35 +0100612 unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
614
Raef Coles8ff6df52021-07-21 12:42:15 +0100615 /* Check that a private key is loaded */
Raef Colesf5632d32022-09-01 09:56:52 +0100616 if ( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100617 {
618 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
619 }
620
Raef Colese9479a02022-09-01 16:06:35 +0100621 ret = hash_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100622 ( unsigned char * )priv_ctx->private_key, NULL,
623 NULL, ( unsigned char * )y_hashed_digits );
Raef Coles01c71a12022-08-31 15:55:00 +0100624 if ( ret )
625 {
626 return( ret );
627 }
628
Raef Colesf5632d32022-09-01 09:56:52 +0100629 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100630 ( unsigned char * )y_hashed_digits,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100631 ctx->public_key );
Raef Coles01c71a12022-08-31 15:55:00 +0100632 if ( ret )
633 {
634 return( ret );
635 }
636
Raef Colesf5632d32022-09-01 09:56:52 +0100637 memcpy( &ctx->params, &priv_ctx->params,
638 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100639
Raef Coles9b88ee52022-09-02 12:04:21 +0100640 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100641
642 return( ret );
643}
644
645
646int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
647 unsigned char *key, size_t key_size,
648 size_t *key_len )
649{
Raef Colese9479a02022-09-01 16:06:35 +0100650 if( key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100651 {
652 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
653 }
654
Raef Colesf5632d32022-09-01 09:56:52 +0100655 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100656 {
657 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
658 }
659
Raef Colesf5632d32022-09-01 09:56:52 +0100660 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100661 MBEDTLS_LMOTS_TYPE_LEN,
662 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
663
664 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100665 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100666 MBEDTLS_LMOTS_I_KEY_ID_LEN );
667
Raef Coles9b88ee52022-09-02 12:04:21 +0100668 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
669 ctx->params.q_leaf_identifier,
670 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100671
Raef Colesf5632d32022-09-01 09:56:52 +0100672 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
Raef Colese9479a02022-09-01 16:06:35 +0100673 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100674
675 if( key_len != NULL )
676 {
Raef Colese9479a02022-09-01 16:06:35 +0100677 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100678 }
679
680 return( 0 );
681}
682
683int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
684 int (*f_rng)(void *, unsigned char *, size_t),
685 void *p_rng, const unsigned char *msg, size_t msg_size,
686 unsigned char *sig, size_t sig_size, size_t* sig_len )
687{
Raef Colese9479a02022-09-01 16:06:35 +0100688 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100689 /* Create a temporary buffer to prepare the signature in. This allows us to
690 * finish creating a signature (ensuring the process doesn't fail), and then
691 * erase the private key **before** writing any data into the sig parameter
692 * buffer. If data were directly written into the sig buffer, it might leak
693 * a partial signature on failure, which effectively compromises the private
694 * key.
695 */
Raef Colese9479a02022-09-01 16:06:35 +0100696 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
697 unsigned char tmp_c_random[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
699
700 if ( msg == NULL && msg_size != 0 )
701 {
702 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
703 }
704
Raef Colese9479a02022-09-01 16:06:35 +0100705 if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100706 {
707 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
708 }
709
710 /* Check that a private key is loaded */
Raef Colesf5632d32022-09-01 09:56:52 +0100711 if ( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100712 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100713 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100714 }
715
Raef Coles366d67d2022-09-01 17:23:12 +0100716 ret = f_rng( p_rng, tmp_c_random,
717 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100718 if ( ret )
719 {
720 return( ret );
721 }
722
Raef Colesf5632d32022-09-01 09:56:52 +0100723 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100724 msg, msg_size,
Raef Coles891c6132022-09-01 11:05:48 +0100725 tmp_c_random,
Raef Coles01c71a12022-08-31 15:55:00 +0100726 tmp_digit_array );
Raef Coles8ff6df52021-07-21 12:42:15 +0100727 if ( ret )
728 {
729 return( ret );
730 }
731
Raef Coles9b88ee52022-09-02 12:04:21 +0100732 ret = hash_digit_array( &ctx->params, ( unsigned char * )ctx->private_key,
733 NULL, tmp_digit_array, ( unsigned char * )tmp_sig );
Raef Coles8ff6df52021-07-21 12:42:15 +0100734 if ( ret )
735 {
736 return( ret );
737 }
738
Raef Colesf5632d32022-09-01 09:56:52 +0100739 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100740 MBEDTLS_LMOTS_TYPE_LEN,
741 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100742
743 /* We've got a valid signature now, so it's time to make sure the private
744 * key can't be reused.
745 */
Raef Colesf5632d32022-09-01 09:56:52 +0100746 ctx->have_private_key = 0;
Raef Coles9b88ee52022-09-02 12:04:21 +0100747 mbedtls_platform_zeroize( ctx->private_key,
748 sizeof( ctx->private_key ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100749
Raef Coles891c6132022-09-01 11:05:48 +0100750 memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
Raef Colese9479a02022-09-01 16:06:35 +0100751 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) );
Raef Coles891c6132022-09-01 11:05:48 +0100752
Raef Colese9479a02022-09-01 16:06:35 +0100753 memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
754 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
755 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100756
Raef Coles01c71a12022-08-31 15:55:00 +0100757 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100758 {
Raef Colese9479a02022-09-01 16:06:35 +0100759 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100760 }
761
762 return( 0 );
763}
764
Raef Colesab4f8742022-09-01 12:24:31 +0100765#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles7dce69a2022-08-24 14:07:06 +0100766#endif /* MBEDTLS_LMS_C */