blob: 7dbf8a206bbfbfe4286bab55be68bdabf29c1609 [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 );
Raef Colese0a17612022-09-02 16:04:47 +0100131 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 );
Raef Colese0a17612022-09-02 16:04:47 +0100137 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 );
Raef Colese0a17612022-09-02 16:04:47 +0100143 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 );
Raef Colese0a17612022-09-02 16:04:47 +0100148 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 );
Raef Colese0a17612022-09-02 16:04:47 +0100154 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 );
Raef Colese0a17612022-09-02 16:04:47 +0100159 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 );
Raef Colese0a17612022-09-02 16:04:47 +0100166 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 Coles8738a492022-09-02 17:13:01 +0100185 unsigned int i_digit_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100186 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8738a492022-09-02 17:13:01 +0100187 unsigned int j_hash_idx;
188 unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100189 unsigned int j_hash_idx_min;
190 unsigned int j_hash_idx_max;
Raef Colesc8f96042022-08-25 13:49:54 +0100191 psa_hash_operation_t op;
192 psa_status_t status;
193 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100194 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
196
Raef Coles9b88ee52022-09-02 12:04:21 +0100197 op = psa_hash_operation_init( );
Raef Coles01c71a12022-08-31 15:55:00 +0100198
Raef Colese9479a02022-09-01 16:06:35 +0100199 for ( i_digit_idx = 0;
200 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
201 i_digit_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100202 {
203
Raef Coles366d67d2022-09-01 17:23:12 +0100204 memcpy( tmp_hash,
205 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
Raef Colese9479a02022-09-01 16:06:35 +0100206 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100207
Raef Coles366d67d2022-09-01 17:23:12 +0100208 j_hash_idx_min = hash_idx_min_values != NULL ?
209 hash_idx_min_values[i_digit_idx] : 0;
210 j_hash_idx_max = hash_idx_max_values != NULL ?
211 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100212
Raef Coles8738a492022-09-02 17:13:01 +0100213 for ( j_hash_idx = j_hash_idx_min;
Raef Coles366d67d2022-09-01 17:23:12 +0100214 j_hash_idx < j_hash_idx_max;
215 j_hash_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100216 {
Raef Colesc8f96042022-08-25 13:49:54 +0100217 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
218 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100219 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100220 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100221
Raef Coles01c71a12022-08-31 15:55:00 +0100222 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100223 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100224 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100225 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100226 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100227 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100228
Raef Coles01c71a12022-08-31 15:55:00 +0100229 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100230 params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100231 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100232 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100233 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100234 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100235
Raef Coles366d67d2022-09-01 17:23:12 +0100236 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
237 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100238 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100239 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100240 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100241 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100242
Raef Coles366d67d2022-09-01 17:23:12 +0100243 unsigned_int_to_network_bytes( j_hash_idx, J_HASH_IDX_LEN,
244 j_hash_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100245 status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN );
246 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100247 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100248 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100249
Raef Colese9479a02022-09-01 16:06:35 +0100250 status = psa_hash_update( &op, tmp_hash,
251 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100252 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100253 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100254 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100255
Raef Coles366d67d2022-09-01 17:23:12 +0100256 status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ),
257 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100258 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100259 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100260 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100261
Raef Colesc8f96042022-08-25 13:49:54 +0100262 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100263 }
264
Raef Coles366d67d2022-09-01 17:23:12 +0100265 memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
266 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100267 }
268
Raef Coles01c71a12022-08-31 15:55:00 +0100269exit:
Raef Colese0a17612022-09-02 16:04:47 +0100270 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100271 {
Raef Colesc8f96042022-08-25 13:49:54 +0100272 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100273 return( ret );
274 }
275
Raef Coles01c71a12022-08-31 15:55:00 +0100276 mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) );
277
Raef Coles8ff6df52021-07-21 12:42:15 +0100278 return ret;
279}
280
Raef Coles01c71a12022-08-31 15:55:00 +0100281static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100282 const unsigned char *y_hashed_digits,
Raef Coles01c71a12022-08-31 15:55:00 +0100283 unsigned char *pub_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100284{
Raef Colesc8f96042022-08-25 13:49:54 +0100285 psa_hash_operation_t op;
286 psa_status_t status;
287 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
289
Raef Colesc8f96042022-08-25 13:49:54 +0100290 op = psa_hash_operation_init( );
291 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
292 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100293 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100294 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100295
Raef Coles01c71a12022-08-31 15:55:00 +0100296 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100297 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100298 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100299 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100300 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100301 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100302
Raef Colesf5632d32022-09-01 09:56:52 +0100303 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100304 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100305 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100306 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100307 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100308
Raef Coles01c71a12022-08-31 15:55:00 +0100309 status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100310 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100311 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100312 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100313
Raef Colese9479a02022-09-01 16:06:35 +0100314 status = psa_hash_update( &op, y_hashed_digits,
315 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
316 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100317 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100318 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100319 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100320
Raef Coles366d67d2022-09-01 17:23:12 +0100321 status = psa_hash_finish( &op, pub_key,
322 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colese9479a02022-09-01 16:06:35 +0100323 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100324 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100325
Raef Coles01c71a12022-08-31 15:55:00 +0100326exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100327 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100328 return( ret );
329}
330
Raef Coles9b88ee52022-09-02 12:04:21 +0100331int mbedtls_lms_error_from_psa( psa_status_t status )
Raef Colesc8f96042022-08-25 13:49:54 +0100332{
Raef Coles9b88ee52022-09-02 12:04:21 +0100333 switch( status )
334 {
Raef Colesc8f96042022-08-25 13:49:54 +0100335 case PSA_SUCCESS:
336 return( 0 );
337 case PSA_ERROR_HARDWARE_FAILURE:
338 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
339 case PSA_ERROR_NOT_SUPPORTED:
340 return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
341 case PSA_ERROR_BUFFER_TOO_SMALL:
342 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
343 case PSA_ERROR_INVALID_ARGUMENT:
344 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
345 default:
346 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
347 }
348}
349
Raef Coles01c71a12022-08-31 15:55:00 +0100350void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100351{
Raef Coles01c71a12022-08-31 15:55:00 +0100352 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100353}
354
Raef Coles01c71a12022-08-31 15:55:00 +0100355void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100356{
Raef Coles01c71a12022-08-31 15:55:00 +0100357 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100358}
359
Raef Coles01c71a12022-08-31 15:55:00 +0100360int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
361 const unsigned char *key, size_t key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100362{
Raef Colesf5632d32022-09-01 09:56:52 +0100363 ctx->params.type =
Raef Coles01c71a12022-08-31 15:55:00 +0100364 network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
365 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
366
Raef Colese0a17612022-09-02 16:04:47 +0100367 if( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Colese9479a02022-09-01 16:06:35 +0100368 {
369 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
370 }
371
Raef Colesf5632d32022-09-01 09:56:52 +0100372 memcpy( ctx->params.I_key_identifier,
Raef Coles366d67d2022-09-01 17:23:12 +0100373 key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
374 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100375
Raef Colesf5632d32022-09-01 09:56:52 +0100376 memcpy( ctx->params.q_leaf_identifier,
Raef Coles366d67d2022-09-01 17:23:12 +0100377 key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
378 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100379
Raef Colesf5632d32022-09-01 09:56:52 +0100380 memcpy( ctx->public_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100381 key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100382 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100383
Raef Colesf5632d32022-09-01 09:56:52 +0100384 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100385
386 return( 0 );
387}
388
Raef Coles01c71a12022-08-31 15:55:00 +0100389int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
390 const unsigned char *msg,
391 size_t msg_size,
392 const unsigned char *sig,
393 size_t sig_size,
394 unsigned char *out,
395 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100396 size_t *out_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100397{
Raef Colese9479a02022-09-01 16:06:35 +0100398 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
399 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 +0100400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
401
Raef Colese0a17612022-09-02 16:04:47 +0100402 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100403 {
404 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
405 }
406
Raef Colese0a17612022-09-02 16:04:47 +0100407 if( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
Raef Colese9479a02022-09-01 16:06:35 +0100408 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100409 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100410 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100411 }
412
Raef Coles01c71a12022-08-31 15:55:00 +0100413 ret = create_digit_array_with_checksum( params, msg, msg_size,
414 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
415 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100416 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100417 {
418 return ( ret );
419 }
420
Raef Coles01c71a12022-08-31 15:55:00 +0100421 ret = hash_digit_array( params,
Raef Colese9479a02022-09-01 16:06:35 +0100422 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100423 tmp_digit_array, NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100424 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100425 {
426 return ( ret );
427 }
428
Raef Colese9479a02022-09-01 16:06:35 +0100429 ret = public_key_from_hashed_digit_array( params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100430 ( unsigned char * )y_hashed_digits,
Raef Colese9479a02022-09-01 16:06:35 +0100431 out );
Raef Colese0a17612022-09-02 16:04:47 +0100432 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100433 {
434 return ( ret );
435 }
436
Raef Colese0a17612022-09-02 16:04:47 +0100437 if( out_len != NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100438 {
Raef Colese9479a02022-09-01 16:06:35 +0100439 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100440 }
441
Raef Coles8ff6df52021-07-21 12:42:15 +0100442 return( 0 );
443}
444
Raef Coles01c71a12022-08-31 15:55:00 +0100445int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
446 size_t msg_size, const unsigned char *sig,
447 size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100448{
Raef Colese9479a02022-09-01 16:06:35 +0100449 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
451
Raef Colese0a17612022-09-02 16:04:47 +0100452 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100453 {
454 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
455 }
456
Raef Colese0a17612022-09-02 16:04:47 +0100457 if( !ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100458 {
459 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
460 }
461
Raef Coles9b88ee52022-09-02 12:04:21 +0100462 if( ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100463 {
464 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
465 }
466
Raef Colese0a17612022-09-02 16:04:47 +0100467 if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100468 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100469 {
470 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
471 }
472
Raef Colesf5632d32022-09-01 09:56:52 +0100473 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100474 msg, msg_size, sig, sig_size,
475 Kc_public_key_candidate,
Raef Colese9479a02022-09-01 16:06:35 +0100476 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100477 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100478 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100479 {
480 return( ret );
481 }
482
Raef Colese0a17612022-09-02 16:04:47 +0100483 if( memcmp( &Kc_public_key_candidate, ctx->public_key,
Raef Colesf5632d32022-09-01 09:56:52 +0100484 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100485 {
486 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
487 }
488
489 return( 0 );
490}
491
Raef Colesab4f8742022-09-01 12:24:31 +0100492#ifdef MBEDTLS_LMS_PRIVATE
493
Raef Coles01c71a12022-08-31 15:55:00 +0100494void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx )
495{
496 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
497}
498
499void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx )
500{
501 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
502}
503
504int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
505 mbedtls_lmots_algorithm_type_t type,
506 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
507 uint32_t q_leaf_identifier,
508 const unsigned char *seed,
509 size_t seed_size )
510{
511 psa_hash_operation_t op;
512 psa_status_t status;
513 size_t output_hash_len;
514 unsigned int i_digit_idx;
515 unsigned char i_digit_idx_bytes[2];
516 unsigned char const_bytes[1];
517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
518
Raef Colese0a17612022-09-02 16:04:47 +0100519 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100520 {
521 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
522 }
523
Raef Colese0a17612022-09-02 16:04:47 +0100524 if( type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles9b88ee52022-09-02 12:04:21 +0100525 {
Raef Coles01c71a12022-08-31 15:55:00 +0100526 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
527 }
528
Raef Colesebd35b52022-09-01 11:52:17 +0100529 op = psa_hash_operation_init( );
530
Raef Colesf5632d32022-09-01 09:56:52 +0100531 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100532
Raef Colesf5632d32022-09-01 09:56:52 +0100533 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100534 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100535 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100536
Raef Coles9b88ee52022-09-02 12:04:21 +0100537 unsigned_int_to_network_bytes( q_leaf_identifier,
538 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
539 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100540
541 unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes );
542
Raef Colese9479a02022-09-01 16:06:35 +0100543 for ( i_digit_idx = 0;
544 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
545 i_digit_idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100546 {
Raef Coles01c71a12022-08-31 15:55:00 +0100547 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
548 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100549 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100550 goto exit;
551
552 ret = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100553 ctx->params.I_key_identifier,
554 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100555 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100556 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100557 goto exit;
558
559 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100560 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100561 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
562 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100563 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100564 goto exit;
565
Raef Coles366d67d2022-09-01 17:23:12 +0100566 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
567 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100568 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
569 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100570 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100571 goto exit;
572
Raef Coles9b88ee52022-09-02 12:04:21 +0100573 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100574 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100575 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100576 goto exit;
577
578 status = psa_hash_update( &op, seed, seed_size );
579 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100580 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100581 goto exit;
582
583 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100584 ctx->private_key[i_digit_idx],
Raef Colese9479a02022-09-01 16:06:35 +0100585 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
586 &output_hash_len );
Raef Coles01c71a12022-08-31 15:55:00 +0100587 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100588 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100589 goto exit;
590
591 psa_hash_abort( &op );
592 }
593
Raef Colesf5632d32022-09-01 09:56:52 +0100594 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100595
596exit:
Raef Colese0a17612022-09-02 16:04:47 +0100597 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100598 {
599 psa_hash_abort( &op );
600 return( ret );
601 }
602
603 return ret;
604}
605
606int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles9b88ee52022-09-02 12:04:21 +0100607 mbedtls_lmots_private_t *priv_ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100608{
Raef Colese9479a02022-09-01 16:06:35 +0100609 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 +0100610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
611
Raef Coles8ff6df52021-07-21 12:42:15 +0100612 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100613 if( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100614 {
615 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
616 }
617
Raef Colese9479a02022-09-01 16:06:35 +0100618 ret = hash_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100619 ( unsigned char * )priv_ctx->private_key, NULL,
620 NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100621 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100622 {
623 return( ret );
624 }
625
Raef Colesf5632d32022-09-01 09:56:52 +0100626 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100627 ( unsigned char * )y_hashed_digits,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100628 ctx->public_key );
Raef Colese0a17612022-09-02 16:04:47 +0100629 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100630 {
631 return( ret );
632 }
633
Raef Colesf5632d32022-09-01 09:56:52 +0100634 memcpy( &ctx->params, &priv_ctx->params,
635 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100636
Raef Coles9b88ee52022-09-02 12:04:21 +0100637 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100638
639 return( ret );
640}
641
642
643int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
644 unsigned char *key, size_t key_size,
645 size_t *key_len )
646{
Raef Colese9479a02022-09-01 16:06:35 +0100647 if( key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100648 {
649 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
650 }
651
Raef Colesf5632d32022-09-01 09:56:52 +0100652 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100653 {
654 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
655 }
656
Raef Colesf5632d32022-09-01 09:56:52 +0100657 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100658 MBEDTLS_LMOTS_TYPE_LEN,
659 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
660
661 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100662 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100663 MBEDTLS_LMOTS_I_KEY_ID_LEN );
664
Raef Coles9b88ee52022-09-02 12:04:21 +0100665 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
666 ctx->params.q_leaf_identifier,
667 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100668
Raef Colesf5632d32022-09-01 09:56:52 +0100669 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
Raef Colese9479a02022-09-01 16:06:35 +0100670 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100671
672 if( key_len != NULL )
673 {
Raef Colese9479a02022-09-01 16:06:35 +0100674 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100675 }
676
677 return( 0 );
678}
679
680int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
681 int (*f_rng)(void *, unsigned char *, size_t),
682 void *p_rng, const unsigned char *msg, size_t msg_size,
683 unsigned char *sig, size_t sig_size, size_t* sig_len )
684{
Raef Colese9479a02022-09-01 16:06:35 +0100685 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100686 /* Create a temporary buffer to prepare the signature in. This allows us to
687 * finish creating a signature (ensuring the process doesn't fail), and then
688 * erase the private key **before** writing any data into the sig parameter
689 * buffer. If data were directly written into the sig buffer, it might leak
690 * a partial signature on failure, which effectively compromises the private
691 * key.
692 */
Raef Colese9479a02022-09-01 16:06:35 +0100693 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
694 unsigned char tmp_c_random[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
696
Raef Colese0a17612022-09-02 16:04:47 +0100697 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100698 {
699 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
700 }
701
Raef Colese9479a02022-09-01 16:06:35 +0100702 if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100703 {
704 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
705 }
706
707 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100708 if( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100709 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100710 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100711 }
712
Raef Coles366d67d2022-09-01 17:23:12 +0100713 ret = f_rng( p_rng, tmp_c_random,
714 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Colese0a17612022-09-02 16:04:47 +0100715 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100716 {
717 return( ret );
718 }
719
Raef Colesf5632d32022-09-01 09:56:52 +0100720 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100721 msg, msg_size,
Raef Coles891c6132022-09-01 11:05:48 +0100722 tmp_c_random,
Raef Coles01c71a12022-08-31 15:55:00 +0100723 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100724 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100725 {
726 return( ret );
727 }
728
Raef Coles9b88ee52022-09-02 12:04:21 +0100729 ret = hash_digit_array( &ctx->params, ( unsigned char * )ctx->private_key,
730 NULL, tmp_digit_array, ( unsigned char * )tmp_sig );
Raef Colese0a17612022-09-02 16:04:47 +0100731 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100732 {
733 return( ret );
734 }
735
Raef Colesf5632d32022-09-01 09:56:52 +0100736 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100737 MBEDTLS_LMOTS_TYPE_LEN,
738 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100739
740 /* We've got a valid signature now, so it's time to make sure the private
741 * key can't be reused.
742 */
Raef Colesf5632d32022-09-01 09:56:52 +0100743 ctx->have_private_key = 0;
Raef Coles9b88ee52022-09-02 12:04:21 +0100744 mbedtls_platform_zeroize( ctx->private_key,
745 sizeof( ctx->private_key ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100746
Raef Coles891c6132022-09-01 11:05:48 +0100747 memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
Raef Colese9479a02022-09-01 16:06:35 +0100748 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) );
Raef Coles891c6132022-09-01 11:05:48 +0100749
Raef Colese9479a02022-09-01 16:06:35 +0100750 memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
751 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
752 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100753
Raef Coles01c71a12022-08-31 15:55:00 +0100754 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100755 {
Raef Colese9479a02022-09-01 16:06:35 +0100756 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100757 }
758
759 return( 0 );
760}
761
Raef Colesab4f8742022-09-01 12:24:31 +0100762#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles7dce69a2022-08-24 14:07:06 +0100763#endif /* MBEDTLS_LMS_C */