blob: 82da4c1391de99bbad66454a2cb27d1dbe15d6a7 [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 Coles01c71a12022-08-31 15:55:00 +010071#define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
72
Raef Coles8ff6df52021-07-21 12:42:15 +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 Coles366d67d2022-09-01 17:23:12 +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
82 for (idx = 0; idx < len; idx++) {
83 bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF;
84 }
85}
86
Raef Coles366d67d2022-09-01 17:23:12 +010087unsigned int network_bytes_to_unsigned_int(size_t len,
88 const unsigned char *bytes)
Raef Coles8ff6df52021-07-21 12:42:15 +010089{
90 size_t idx;
91 unsigned int val = 0;
92
93 for (idx = 0; idx < len; idx++) {
94 val |= ((unsigned int)bytes[idx]) << (8 * (len - 1 - idx));
95 }
96
97 return val;
98}
99
Raef Colese9479a02022-09-01 16:06:35 +0100100static unsigned short lmots_checksum_calculate( const mbedtls_lmots_parameters_t *params,
101 const unsigned char* digest )
Raef Coles8ff6df52021-07-21 12:42:15 +0100102{
103 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100104 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100105
Raef Colese9479a02022-09-01 16:06:35 +0100106 for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100107 {
Raef Coles01c71a12022-08-31 15:55:00 +0100108 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +0100109 }
110
111 return sum;
112}
113
Raef Coles01c71a12022-08-31 15:55:00 +0100114static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *params,
115 const unsigned char *msg,
116 size_t msg_len,
Raef Colese9479a02022-09-01 16:06:35 +0100117 const unsigned char *C_random_value,
118 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100119{
Raef Colesc8f96042022-08-25 13:49:54 +0100120 psa_hash_operation_t op;
121 psa_status_t status;
122 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100123 unsigned short checksum;
124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
125
Raef Colesc8f96042022-08-25 13:49:54 +0100126 op = psa_hash_operation_init();
127 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
128 ret = mbedtls_lms_error_from_psa( status );
129 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100130 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100131
Raef Colesf5632d32022-09-01 09:56:52 +0100132 status = psa_hash_update( &op, params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100133 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100134 ret = mbedtls_lms_error_from_psa( status );
135 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100136 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100137
Raef Colesf5632d32022-09-01 09:56:52 +0100138 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100139 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100140 ret = mbedtls_lms_error_from_psa( status );
141 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100142 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100143
Raef Coles01c71a12022-08-31 15:55:00 +0100144 status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100145 ret = mbedtls_lms_error_from_psa( status );
146 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100147 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100148
Raef Colese9479a02022-09-01 16:06:35 +0100149 status = psa_hash_update( &op, C_random_value,
150 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100151 ret = mbedtls_lms_error_from_psa( status );
152 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100153 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100154
Raef Colesc8f96042022-08-25 13:49:54 +0100155 status = psa_hash_update( &op, msg, msg_len );
156 ret = mbedtls_lms_error_from_psa( status );
157 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100158 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100159
Raef Colese9479a02022-09-01 16:06:35 +0100160 status = psa_hash_finish( &op, out,
161 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type),
Raef Colesc8f96042022-08-25 13:49:54 +0100162 &output_hash_len );
163 ret = mbedtls_lms_error_from_psa( status );
164 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100165 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100166
Raef Colese9479a02022-09-01 16:06:35 +0100167 checksum = lmots_checksum_calculate( params, out );
168 unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN,
169 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100170
Raef Coles01c71a12022-08-31 15:55:00 +0100171exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100172 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100173
174 return( ret );
175}
176
Raef Coles01c71a12022-08-31 15:55:00 +0100177static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100178 const unsigned char *x_digit_array,
Raef Coles01c71a12022-08-31 15:55:00 +0100179 const unsigned char *hash_idx_min_values,
180 const unsigned char *hash_idx_max_values,
Raef Colese9479a02022-09-01 16:06:35 +0100181 unsigned char *output )
Raef Coles8ff6df52021-07-21 12:42:15 +0100182{
Raef Coles01c71a12022-08-31 15:55:00 +0100183 unsigned char i_digit_idx;
Raef Coles8ff6df52021-07-21 12:42:15 +0100184 unsigned char j_hash_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100185 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8ff6df52021-07-21 12:42:15 +0100186 unsigned char j_hash_idx_bytes[1];
Raef Coles01c71a12022-08-31 15:55:00 +0100187 /* These can't be unsigned chars, because they are sometimes set to
188 * #DIGIT_MAX_VALUE, which has a value of 256
189 */
190 unsigned int j_hash_idx_min;
191 unsigned int j_hash_idx_max;
Raef Colesc8f96042022-08-25 13:49:54 +0100192 psa_hash_operation_t op;
193 psa_status_t status;
194 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100195 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
197
Raef Coles01c71a12022-08-31 15:55:00 +0100198 op = psa_hash_operation_init();
199
Raef Colese9479a02022-09-01 16:06:35 +0100200 for ( i_digit_idx = 0;
201 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
202 i_digit_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100203 {
204
Raef Coles366d67d2022-09-01 17:23:12 +0100205 memcpy( tmp_hash,
206 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
Raef Colese9479a02022-09-01 16:06:35 +0100207 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100208
Raef Coles366d67d2022-09-01 17:23:12 +0100209 j_hash_idx_min = hash_idx_min_values != NULL ?
210 hash_idx_min_values[i_digit_idx] : 0;
211 j_hash_idx_max = hash_idx_max_values != NULL ?
212 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100213
Raef Coles366d67d2022-09-01 17:23:12 +0100214 for ( j_hash_idx = (unsigned char)j_hash_idx_min;
215 j_hash_idx < j_hash_idx_max;
216 j_hash_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100217 {
Raef Colesc8f96042022-08-25 13:49:54 +0100218 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
219 ret = mbedtls_lms_error_from_psa( status );
220 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100221 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100222
Raef Coles01c71a12022-08-31 15:55:00 +0100223 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100224 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100225 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100226 ret = mbedtls_lms_error_from_psa( status );
227 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100228 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100229
Raef Coles01c71a12022-08-31 15:55:00 +0100230 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100231 params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100232 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100233 ret = mbedtls_lms_error_from_psa( status );
234 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100235 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100236
Raef Coles366d67d2022-09-01 17:23:12 +0100237 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
238 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100239 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100240 ret = mbedtls_lms_error_from_psa( status );
241 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100242 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100243
Raef Coles366d67d2022-09-01 17:23:12 +0100244 unsigned_int_to_network_bytes( j_hash_idx, J_HASH_IDX_LEN,
245 j_hash_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100246 status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN );
247 ret = mbedtls_lms_error_from_psa( status );
248 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100249 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100250
Raef Colese9479a02022-09-01 16:06:35 +0100251 status = psa_hash_update( &op, tmp_hash,
252 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100253 ret = mbedtls_lms_error_from_psa( status );
254 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100255 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100256
Raef Coles366d67d2022-09-01 17:23:12 +0100257 status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ),
258 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100259 ret = mbedtls_lms_error_from_psa( status );
260 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100261 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100262
Raef Colesc8f96042022-08-25 13:49:54 +0100263 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100264 }
265
Raef Coles366d67d2022-09-01 17:23:12 +0100266 memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
267 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100268 }
269
Raef Coles01c71a12022-08-31 15:55:00 +0100270exit:
Raef Coles8ff6df52021-07-21 12:42:15 +0100271 if( ret )
272 {
Raef Colesc8f96042022-08-25 13:49:54 +0100273 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100274 return( ret );
275 }
276
Raef Coles01c71a12022-08-31 15:55:00 +0100277 mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) );
278
Raef Coles8ff6df52021-07-21 12:42:15 +0100279 return ret;
280}
281
Raef Coles01c71a12022-08-31 15:55:00 +0100282static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100283 const unsigned char *y_hashed_digits,
Raef Coles01c71a12022-08-31 15:55:00 +0100284 unsigned char *pub_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100285{
Raef Colesc8f96042022-08-25 13:49:54 +0100286 psa_hash_operation_t op;
287 psa_status_t status;
288 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
290
Raef Colesc8f96042022-08-25 13:49:54 +0100291 op = psa_hash_operation_init( );
292 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
293 ret = mbedtls_lms_error_from_psa( status );
294 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100295 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100296
Raef Coles01c71a12022-08-31 15:55:00 +0100297 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100298 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100299 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100300 ret = mbedtls_lms_error_from_psa( status );
301 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100302 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100303
Raef Colesf5632d32022-09-01 09:56:52 +0100304 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100305 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100306 ret = mbedtls_lms_error_from_psa( status );
307 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100308 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100309
Raef Coles01c71a12022-08-31 15:55:00 +0100310 status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100311 ret = mbedtls_lms_error_from_psa( status );
312 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100313 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100314
Raef Colese9479a02022-09-01 16:06:35 +0100315 status = psa_hash_update( &op, y_hashed_digits,
316 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
317 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100318 ret = mbedtls_lms_error_from_psa( status );
319 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100320 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100321
Raef Coles366d67d2022-09-01 17:23:12 +0100322 status = psa_hash_finish( &op, pub_key,
323 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colese9479a02022-09-01 16:06:35 +0100324 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100325 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100326
Raef Coles01c71a12022-08-31 15:55:00 +0100327exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100328 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100329 return( ret );
330}
331
Raef Colesc8f96042022-08-25 13:49:54 +0100332int mbedtls_lms_error_from_psa(psa_status_t status)
333{
334 switch( status ) {
335 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 Colese9479a02022-09-01 16:06:35 +0100367 if ( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
368 {
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,
396 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 Coles01c71a12022-08-31 15:55:00 +0100402 if ( msg == NULL && msg_size != 0 )
403 {
404 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
405 }
406
Raef Colese9479a02022-09-01 16:06:35 +0100407 if ( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
408 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 Coles8ff6df52021-07-21 12:42:15 +0100416 if ( ret )
417 {
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),
423 tmp_digit_array, NULL, (unsigned char *)y_hashed_digits );
Raef Coles8ff6df52021-07-21 12:42:15 +0100424 if ( ret )
425 {
426 return ( ret );
427 }
428
Raef Colese9479a02022-09-01 16:06:35 +0100429 ret = public_key_from_hashed_digit_array( params,
430 (unsigned char *)y_hashed_digits,
431 out );
Raef Coles8ff6df52021-07-21 12:42:15 +0100432 if ( ret )
433 {
434 return ( ret );
435 }
436
Raef Coles01c71a12022-08-31 15:55:00 +0100437 if ( out_len != NULL )
438 {
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 Coles01c71a12022-08-31 15:55:00 +0100452 if ( msg == NULL && msg_size != 0 )
453 {
454 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
455 }
456
Raef Colesf5632d32022-09-01 09:56:52 +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 Colesf5632d32022-09-01 09:56:52 +0100462 if( ctx->params.MBEDTLS_PRIVATE( type )
Raef Coles01c71a12022-08-31 15:55:00 +0100463 != MBEDTLS_LMOTS_SHA256_N32_W8 )
464 {
465 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
466 }
467
468 if ( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100469 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100470 {
471 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
472 }
473
Raef Colesf5632d32022-09-01 09:56:52 +0100474 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100475 msg, msg_size, sig, sig_size,
476 Kc_public_key_candidate,
Raef Colese9479a02022-09-01 16:06:35 +0100477 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
Raef Coles01c71a12022-08-31 15:55:00 +0100478 NULL);
479 if ( ret )
480 {
481 return( ret );
482 }
483
Raef Colesf5632d32022-09-01 09:56:52 +0100484 if ( memcmp( &Kc_public_key_candidate, ctx->public_key,
485 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100486 {
487 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
488 }
489
490 return( 0 );
491}
492
Raef Colesab4f8742022-09-01 12:24:31 +0100493#ifdef MBEDTLS_LMS_PRIVATE
494
Raef Coles01c71a12022-08-31 15:55:00 +0100495void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx )
496{
497 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
498}
499
500void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx )
501{
502 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
503}
504
505int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
506 mbedtls_lmots_algorithm_type_t type,
507 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
508 uint32_t q_leaf_identifier,
509 const unsigned char *seed,
510 size_t seed_size )
511{
512 psa_hash_operation_t op;
513 psa_status_t status;
514 size_t output_hash_len;
515 unsigned int i_digit_idx;
516 unsigned char i_digit_idx_bytes[2];
517 unsigned char const_bytes[1];
518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
519
Raef Colesf5632d32022-09-01 09:56:52 +0100520 if ( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100521 {
522 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
523 }
524
525 if ( type != MBEDTLS_LMOTS_SHA256_N32_W8 ) {
526 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
537 unsigned_int_to_network_bytes(q_leaf_identifier,
538 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Colesf5632d32022-09-01 09:56:52 +0100539 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 );
549 if ( ret != 0 )
550 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 );
556 if ( ret )
557 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 );
563 if ( ret )
564 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 );
570 if ( ret )
571 goto exit;
572
573 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes) );
574 ret = mbedtls_lms_error_from_psa( status );
575 if ( ret )
576 goto exit;
577
578 status = psa_hash_update( &op, seed, seed_size );
579 ret = mbedtls_lms_error_from_psa( status );
580 if ( ret )
581 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 );
588 if ( ret )
589 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:
597 if( ret )
598 {
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,
607 mbedtls_lmots_private_t *priv_ctx)
608{
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 Colesf5632d32022-09-01 09:56:52 +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,
619 (unsigned char *)priv_ctx->private_key, NULL,
620 NULL, (unsigned char *)y_hashed_digits );
Raef Coles01c71a12022-08-31 15:55:00 +0100621 if ( ret )
622 {
623 return( ret );
624 }
625
Raef Colesf5632d32022-09-01 09:56:52 +0100626 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Colese9479a02022-09-01 16:06:35 +0100627 (unsigned char *)y_hashed_digits,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100628 ctx->public_key );
Raef Coles01c71a12022-08-31 15:55:00 +0100629 if ( ret )
630 {
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
637 ctx->MBEDTLS_PRIVATE(have_public_key = 1);
638
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
665 memcpy(key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100666 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100667 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
668
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
697 if ( msg == NULL && msg_size != 0 )
698 {
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 Colesf5632d32022-09-01 09:56:52 +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 Coles8ff6df52021-07-21 12:42:15 +0100715 if ( ret )
716 {
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 Coles8ff6df52021-07-21 12:42:15 +0100724 if ( ret )
725 {
726 return( ret );
727 }
728
Raef Colese9479a02022-09-01 16:06:35 +0100729 ret = hash_digit_array( &ctx->params, (unsigned char *)ctx->private_key,
730 NULL, tmp_digit_array, (unsigned char *)tmp_sig );
Raef Coles8ff6df52021-07-21 12:42:15 +0100731 if ( ret )
732 {
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;
744 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 */