blob: d733dc02e69ca0a788624c6f1c5ecd9422800b07 [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_PUBLIC_KEY_TYPE_OFFSET (0)
48#define MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_TYPE_OFFSET + \
49 MBEDTLS_LMOTS_TYPE_LEN)
50#define MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET + \
51 MBEDTLS_LMOTS_I_KEY_ID_LEN)
52#define MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET + \
53 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010054
55/* We only support parameter sets that use 8-bit digits, as it does not require
56 * translation logic between digits and bytes */
57#define W_WINTERNITZ_PARAMETER (8u)
58#define CHECKSUM_LEN (2)
59#define I_DIGIT_IDX_LEN (2)
60#define J_HASH_IDX_LEN (1)
61#define D_CONST_LEN (2)
62
Raef Colese9479a02022-09-01 16:06:35 +010063/* Currently only defined for SHA256, 32 is the max hash output size */
64#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN_MAX (MBEDTLS_LMOTS_N_HASH_LEN_MAX)
65
Raef Coles9b88ee52022-09-02 12:04:21 +010066#define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
Raef Coles01c71a12022-08-31 15:55:00 +010067
Raef Coles9b88ee52022-09-02 12:04:21 +010068#define D_CONST_LEN (2)
Raef Coles01c71a12022-08-31 15:55:00 +010069static const unsigned char D_PUBLIC_CONSTANT_BYTES[D_CONST_LEN] = {0x80, 0x80};
70static const unsigned char D_MESSAGE_CONSTANT_BYTES[D_CONST_LEN] = {0x81, 0x81};
Raef Coles8ff6df52021-07-21 12:42:15 +010071
Raef Coles9c9027b2022-09-02 18:26:31 +010072#if defined(MBEDTLS_TEST_HOOKS)
73int( *mbedtls_lmots_sign_private_key_invalidated_hook )( unsigned char * ) = NULL;
74#endif /* defined(MBEDTLS_TEST_HOOKS) */
75
Raef Coles9b88ee52022-09-02 12:04:21 +010076void unsigned_int_to_network_bytes( unsigned int val, size_t len,
77 unsigned char *bytes )
Raef Coles8ff6df52021-07-21 12:42:15 +010078{
79 size_t idx;
80
Raef Coles9b88ee52022-09-02 12:04:21 +010081 for ( idx = 0; idx < len; idx++ )
82 {
83 bytes[idx] = ( val >> ( ( len - 1 - idx ) * 8 ) ) & 0xFF;
Raef Coles8ff6df52021-07-21 12:42:15 +010084 }
85}
86
Raef Coles9b88ee52022-09-02 12:04:21 +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
Raef Coles9b88ee52022-09-02 12:04:21 +010093 for ( idx = 0; idx < len; idx++ )
94 {
95 val |= ( ( unsigned int )bytes[idx] ) << (8 * ( len - 1 - idx ) );
Raef Coles8ff6df52021-07-21 12:42:15 +010096 }
97
98 return val;
99}
100
Raef Coles0a967cc2022-09-02 17:46:15 +0100101/* Calculate the checksum digits that are appended to the end of the LMOTS digit
102 * string. See NIST SP800-208 section 3.1 or RFC8554 Algorithm 2 for details of
103 * the checksum algorithm.
104 *
105 * \param params The LMOTS parameter set, I and q values which
106 * describe the key being used.
107 *
108 * \param digest The digit string to create the digest from. As
109 * this does not contain a checksum, it is the same
110 * size as a hash output.
111 */
Raef Colese9479a02022-09-01 16:06:35 +0100112static unsigned short lmots_checksum_calculate( const mbedtls_lmots_parameters_t *params,
113 const unsigned char* digest )
Raef Coles8ff6df52021-07-21 12:42:15 +0100114{
115 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100116 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100117
Raef Colese9479a02022-09-01 16:06:35 +0100118 for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100119 {
Raef Coles01c71a12022-08-31 15:55:00 +0100120 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +0100121 }
122
123 return sum;
124}
125
Raef Coles0a967cc2022-09-02 17:46:15 +0100126/* Create the string of digest digits (in the base determined by the Winternitz
127 * parameter with the checksum appended to the end (Q || cksm(Q)). See NIST
128 * SP800-208 section 3.1 or RFC8554 Algorithm 3 step 5 (also used in Algorithm
129 * 4b step 3) for details.
130 *
131 * \param params The LMOTS parameter set, I and q values which
132 * describe the key being used.
133 *
134 * \param msg The message that will be hashed to create the
135 * digest.
136 *
137 * \param msg_size The size of the message.
138 *
139 * \param C_random_value The random value that will be combined with the
140 * message digest. This is always the same size as a
141 * hash output for whichever hash algorithm is
142 * determined by the parameter set.
143 *
144 * \param output An output containing the digit string (+
145 * checksum) of length P digits (in the case of
146 * MBEDTLS_LMOTS_SHA256_N32_W8, this means it is of
147 * size P bytes).
148 */
Raef Coles01c71a12022-08-31 15:55:00 +0100149static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *params,
150 const unsigned char *msg,
151 size_t msg_len,
Raef Colese9479a02022-09-01 16:06:35 +0100152 const unsigned char *C_random_value,
153 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100154{
Raef Colesc8f96042022-08-25 13:49:54 +0100155 psa_hash_operation_t op;
156 psa_status_t status;
157 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100158 unsigned short checksum;
159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
160
Raef Coles9b88ee52022-09-02 12:04:21 +0100161 op = psa_hash_operation_init( );
Raef Colesc8f96042022-08-25 13:49:54 +0100162 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
163 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100164 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100165 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100166
Raef Colesf5632d32022-09-01 09:56:52 +0100167 status = psa_hash_update( &op, params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100168 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100169 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100170 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100171 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100172
Raef Colesf5632d32022-09-01 09:56:52 +0100173 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100174 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100175 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100176 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100177 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100178
Raef Coles01c71a12022-08-31 15:55:00 +0100179 status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100180 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100181 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100182 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100183
Raef Colese9479a02022-09-01 16:06:35 +0100184 status = psa_hash_update( &op, C_random_value,
185 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100186 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100187 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100188 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100189
Raef Colesc8f96042022-08-25 13:49:54 +0100190 status = psa_hash_update( &op, msg, msg_len );
191 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100192 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100193 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100194
Raef Colese9479a02022-09-01 16:06:35 +0100195 status = psa_hash_finish( &op, out,
Raef Colesfa24f9d2022-09-02 17:46:52 +0100196 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colesc8f96042022-08-25 13:49:54 +0100197 &output_hash_len );
198 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100199 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100200 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100201
Raef Colese9479a02022-09-01 16:06:35 +0100202 checksum = lmots_checksum_calculate( params, out );
203 unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN,
204 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100205
Raef Coles01c71a12022-08-31 15:55:00 +0100206exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100207 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100208
209 return( ret );
210}
211
Raef Coles0a967cc2022-09-02 17:46:15 +0100212/* Hash each element of the string of digits (+ checksum), producing a hash
213 * output for each element. This is used in several places (by varying the
214 * hash_idx_min/max_values) in order to calculate a public key from a private
215 * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554
216 * Algorithm 3 step 5), and to calculate a public key candidate from a
217 * signature and message (RFC8554 Algorithm 4b step 3).
218 *
219 * \param params The LMOTS parameter set, I and q values which
220 * describe the key being used.
221 *
222 * \param x_digit_array The array of digits (of size P, 34 in the case of
223 * MBEDTLS_LMOTS_SHA256_N32_W8).
224 *
225 * \param hash_idx_min_values An array of the starting values of the j iterator
226 * for each of the members of the digit array. If
227 * this value in NULL, then all iterators will start
228 * at 0.
229 *
230 * \param hash_idx_max_values An array of the upper bound values of the j
231 * iterator for each of the members of the digit
232 * array. If this value in NULL, then iterator is
233 * bounded to be less than 2^w - 1 (255 in the case
234 * of MBEDTLS_LMOTS_SHA256_N32_W8)
235 *
236 * \param output An array containing a hash output for each member
237 * of the digit string P. In the case of
238 * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 *
239 * 34.
240 */
Raef Coles01c71a12022-08-31 15:55:00 +0100241static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100242 const unsigned char *x_digit_array,
Raef Coles01c71a12022-08-31 15:55:00 +0100243 const unsigned char *hash_idx_min_values,
244 const unsigned char *hash_idx_max_values,
Raef Colese9479a02022-09-01 16:06:35 +0100245 unsigned char *output )
Raef Coles8ff6df52021-07-21 12:42:15 +0100246{
Raef Coles8738a492022-09-02 17:13:01 +0100247 unsigned int i_digit_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100248 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8738a492022-09-02 17:13:01 +0100249 unsigned int j_hash_idx;
250 unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100251 unsigned int j_hash_idx_min;
252 unsigned int j_hash_idx_max;
Raef Colesc8f96042022-08-25 13:49:54 +0100253 psa_hash_operation_t op;
254 psa_status_t status;
255 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100256 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
258
Raef Coles9b88ee52022-09-02 12:04:21 +0100259 op = psa_hash_operation_init( );
Raef Coles01c71a12022-08-31 15:55:00 +0100260
Raef Colese9479a02022-09-01 16:06:35 +0100261 for ( i_digit_idx = 0;
262 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
263 i_digit_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100264 {
265
Raef Coles366d67d2022-09-01 17:23:12 +0100266 memcpy( tmp_hash,
267 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
Raef Colese9479a02022-09-01 16:06:35 +0100268 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100269
Raef Coles366d67d2022-09-01 17:23:12 +0100270 j_hash_idx_min = hash_idx_min_values != NULL ?
271 hash_idx_min_values[i_digit_idx] : 0;
272 j_hash_idx_max = hash_idx_max_values != NULL ?
273 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100274
Raef Coles8738a492022-09-02 17:13:01 +0100275 for ( j_hash_idx = j_hash_idx_min;
Raef Coles366d67d2022-09-01 17:23:12 +0100276 j_hash_idx < j_hash_idx_max;
277 j_hash_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100278 {
Raef Colesc8f96042022-08-25 13:49:54 +0100279 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
280 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100281 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100282 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100283
Raef Coles01c71a12022-08-31 15:55:00 +0100284 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100285 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100286 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100287 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100288 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100289 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100290
Raef Coles01c71a12022-08-31 15:55:00 +0100291 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100292 params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100293 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100294 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100295 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100296 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100297
Raef Coles366d67d2022-09-01 17:23:12 +0100298 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
299 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100300 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100301 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100302 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100303 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100304
Raef Coles366d67d2022-09-01 17:23:12 +0100305 unsigned_int_to_network_bytes( j_hash_idx, J_HASH_IDX_LEN,
306 j_hash_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100307 status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN );
308 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100309 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100310 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100311
Raef Colese9479a02022-09-01 16:06:35 +0100312 status = psa_hash_update( &op, tmp_hash,
313 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100314 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100315 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100316 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100317
Raef Coles366d67d2022-09-01 17:23:12 +0100318 status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ),
319 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100320 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100321 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100322 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100323
Raef Colesc8f96042022-08-25 13:49:54 +0100324 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100325 }
326
Raef Coles366d67d2022-09-01 17:23:12 +0100327 memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
328 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100329 }
330
Raef Coles01c71a12022-08-31 15:55:00 +0100331exit:
Raef Colese0a17612022-09-02 16:04:47 +0100332 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100333 {
Raef Colesc8f96042022-08-25 13:49:54 +0100334 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100335 return( ret );
336 }
337
Raef Coles01c71a12022-08-31 15:55:00 +0100338 mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) );
339
Raef Coles8ff6df52021-07-21 12:42:15 +0100340 return ret;
341}
342
Raef Coles0a967cc2022-09-02 17:46:15 +0100343/* Combine the hashes of the digit array into a public key. This is used in
344 * in order to calculate a public key from a private key (RFC8554 Algorithm 1
345 * step 4), and to calculate a public key candidate from a signature and message
346 * (RFC8554 Algorithm 4b step 3).
347 *
348 * \param params The LMOTS parameter set, I and q values which describe
349 * the key being used.
350 * \param y_hashed_digits The array of hashes, one hash for each digit of the
351 * symbol array (which is of size P, 34 in the case of
352 * MBEDTLS_LMOTS_SHA256_N32_W8)
353 *
354 * \param pub_key The output public key (or candidate public key in
355 * case this is being run as part of signature
356 * verification), in the form of a hash output.
357 */
Raef Coles01c71a12022-08-31 15:55:00 +0100358static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100359 const unsigned char *y_hashed_digits,
Raef Coles01c71a12022-08-31 15:55:00 +0100360 unsigned char *pub_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100361{
Raef Colesc8f96042022-08-25 13:49:54 +0100362 psa_hash_operation_t op;
363 psa_status_t status;
364 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
366
Raef Colesc8f96042022-08-25 13:49:54 +0100367 op = psa_hash_operation_init( );
368 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
369 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100370 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100371 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100372
Raef Coles01c71a12022-08-31 15:55:00 +0100373 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100374 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100375 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100376 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100377 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100378 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100379
Raef Colesf5632d32022-09-01 09:56:52 +0100380 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100381 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100382 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100383 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100384 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100385
Raef Coles01c71a12022-08-31 15:55:00 +0100386 status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100387 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100388 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100389 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100390
Raef Colese9479a02022-09-01 16:06:35 +0100391 status = psa_hash_update( &op, y_hashed_digits,
392 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
393 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100394 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100395 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100396 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100397
Raef Coles366d67d2022-09-01 17:23:12 +0100398 status = psa_hash_finish( &op, pub_key,
399 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colese9479a02022-09-01 16:06:35 +0100400 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100401 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100402
Raef Coles01c71a12022-08-31 15:55:00 +0100403exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100404 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100405 return( ret );
406}
407
Raef Coles9b88ee52022-09-02 12:04:21 +0100408int mbedtls_lms_error_from_psa( psa_status_t status )
Raef Colesc8f96042022-08-25 13:49:54 +0100409{
Raef Coles9b88ee52022-09-02 12:04:21 +0100410 switch( status )
411 {
Raef Colesc8f96042022-08-25 13:49:54 +0100412 case PSA_SUCCESS:
413 return( 0 );
414 case PSA_ERROR_HARDWARE_FAILURE:
415 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
416 case PSA_ERROR_NOT_SUPPORTED:
417 return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
418 case PSA_ERROR_BUFFER_TOO_SMALL:
419 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
420 case PSA_ERROR_INVALID_ARGUMENT:
421 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
422 default:
423 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
424 }
425}
426
Raef Coles01c71a12022-08-31 15:55:00 +0100427void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100428{
Raef Coles01c71a12022-08-31 15:55:00 +0100429 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100430}
431
Raef Coles01c71a12022-08-31 15:55:00 +0100432void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100433{
Raef Coles01c71a12022-08-31 15:55:00 +0100434 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100435}
436
Raef Coles01c71a12022-08-31 15:55:00 +0100437int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
438 const unsigned char *key, size_t key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100439{
Raef Colesf5632d32022-09-01 09:56:52 +0100440 ctx->params.type =
Raef Coles01c71a12022-08-31 15:55:00 +0100441 network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
442 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
443
Raef Colese0a17612022-09-02 16:04:47 +0100444 if( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Colese9479a02022-09-01 16:06:35 +0100445 {
446 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
447 }
448
Raef Colesf5632d32022-09-01 09:56:52 +0100449 memcpy( ctx->params.I_key_identifier,
Raef Coles366d67d2022-09-01 17:23:12 +0100450 key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
451 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100452
Raef Colesf5632d32022-09-01 09:56:52 +0100453 memcpy( ctx->params.q_leaf_identifier,
Raef Coles366d67d2022-09-01 17:23:12 +0100454 key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
455 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100456
Raef Colesf5632d32022-09-01 09:56:52 +0100457 memcpy( ctx->public_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100458 key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100459 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100460
Raef Colesf5632d32022-09-01 09:56:52 +0100461 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100462
463 return( 0 );
464}
465
Raef Coles01c71a12022-08-31 15:55:00 +0100466int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
467 const unsigned char *msg,
468 size_t msg_size,
469 const unsigned char *sig,
470 size_t sig_size,
471 unsigned char *out,
472 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100473 size_t *out_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100474{
Raef Colese9479a02022-09-01 16:06:35 +0100475 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
476 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 +0100477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
478
Raef Colese0a17612022-09-02 16:04:47 +0100479 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100480 {
481 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
482 }
483
Raef Colese0a17612022-09-02 16:04:47 +0100484 if( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
Raef Colese9479a02022-09-01 16:06:35 +0100485 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100486 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100487 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100488 }
489
Raef Coles01c71a12022-08-31 15:55:00 +0100490 ret = create_digit_array_with_checksum( params, msg, msg_size,
491 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
492 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100493 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100494 {
495 return ( ret );
496 }
497
Raef Coles01c71a12022-08-31 15:55:00 +0100498 ret = hash_digit_array( params,
Raef Colese9479a02022-09-01 16:06:35 +0100499 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100500 tmp_digit_array, NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100501 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100502 {
503 return ( ret );
504 }
505
Raef Colese9479a02022-09-01 16:06:35 +0100506 ret = public_key_from_hashed_digit_array( params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100507 ( unsigned char * )y_hashed_digits,
Raef Colese9479a02022-09-01 16:06:35 +0100508 out );
Raef Colese0a17612022-09-02 16:04:47 +0100509 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100510 {
511 return ( ret );
512 }
513
Raef Colese0a17612022-09-02 16:04:47 +0100514 if( out_len != NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100515 {
Raef Colese9479a02022-09-01 16:06:35 +0100516 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100517 }
518
Raef Coles8ff6df52021-07-21 12:42:15 +0100519 return( 0 );
520}
521
Raef Coles01c71a12022-08-31 15:55:00 +0100522int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
523 size_t msg_size, const unsigned char *sig,
524 size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100525{
Raef Colese9479a02022-09-01 16:06:35 +0100526 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
528
Raef Colese0a17612022-09-02 16:04:47 +0100529 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100530 {
531 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
532 }
533
Raef Colese0a17612022-09-02 16:04:47 +0100534 if( !ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100535 {
536 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
537 }
538
Raef Coles9b88ee52022-09-02 12:04:21 +0100539 if( ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100540 {
541 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
542 }
543
Raef Colese0a17612022-09-02 16:04:47 +0100544 if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100545 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100546 {
547 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
548 }
549
Raef Colesf5632d32022-09-01 09:56:52 +0100550 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100551 msg, msg_size, sig, sig_size,
552 Kc_public_key_candidate,
Raef Colese9479a02022-09-01 16:06:35 +0100553 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100554 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100555 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100556 {
557 return( ret );
558 }
559
Raef Colese0a17612022-09-02 16:04:47 +0100560 if( memcmp( &Kc_public_key_candidate, ctx->public_key,
Raef Colesf5632d32022-09-01 09:56:52 +0100561 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100562 {
563 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
564 }
565
566 return( 0 );
567}
568
Raef Colesab4f8742022-09-01 12:24:31 +0100569#ifdef MBEDTLS_LMS_PRIVATE
570
Raef Coles01c71a12022-08-31 15:55:00 +0100571void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx )
572{
573 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
574}
575
576void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx )
577{
578 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
579}
580
581int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
582 mbedtls_lmots_algorithm_type_t type,
583 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
584 uint32_t q_leaf_identifier,
585 const unsigned char *seed,
586 size_t seed_size )
587{
588 psa_hash_operation_t op;
589 psa_status_t status;
590 size_t output_hash_len;
591 unsigned int i_digit_idx;
592 unsigned char i_digit_idx_bytes[2];
593 unsigned char const_bytes[1];
594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
595
Raef Colese0a17612022-09-02 16:04:47 +0100596 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100597 {
598 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
599 }
600
Raef Colese0a17612022-09-02 16:04:47 +0100601 if( type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles9b88ee52022-09-02 12:04:21 +0100602 {
Raef Coles01c71a12022-08-31 15:55:00 +0100603 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
604 }
605
Raef Colesebd35b52022-09-01 11:52:17 +0100606 op = psa_hash_operation_init( );
607
Raef Colesf5632d32022-09-01 09:56:52 +0100608 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100609
Raef Colesf5632d32022-09-01 09:56:52 +0100610 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100611 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100612 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100613
Raef Coles9b88ee52022-09-02 12:04:21 +0100614 unsigned_int_to_network_bytes( q_leaf_identifier,
615 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
616 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100617
618 unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes );
619
Raef Colese9479a02022-09-01 16:06:35 +0100620 for ( i_digit_idx = 0;
621 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
622 i_digit_idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100623 {
Raef Coles01c71a12022-08-31 15:55:00 +0100624 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
625 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100626 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100627 goto exit;
628
629 ret = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100630 ctx->params.I_key_identifier,
631 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100632 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100633 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100634 goto exit;
635
636 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100637 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100638 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
639 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100640 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100641 goto exit;
642
Raef Coles366d67d2022-09-01 17:23:12 +0100643 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
644 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100645 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
646 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100647 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100648 goto exit;
649
Raef Coles9b88ee52022-09-02 12:04:21 +0100650 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100651 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100652 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100653 goto exit;
654
655 status = psa_hash_update( &op, seed, seed_size );
656 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100657 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100658 goto exit;
659
660 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100661 ctx->private_key[i_digit_idx],
Raef Colese9479a02022-09-01 16:06:35 +0100662 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
663 &output_hash_len );
Raef Coles01c71a12022-08-31 15:55:00 +0100664 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100665 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100666 goto exit;
667
668 psa_hash_abort( &op );
669 }
670
Raef Colesf5632d32022-09-01 09:56:52 +0100671 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100672
673exit:
Raef Colese0a17612022-09-02 16:04:47 +0100674 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100675 {
676 psa_hash_abort( &op );
677 return( ret );
678 }
679
680 return ret;
681}
682
683int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles9b88ee52022-09-02 12:04:21 +0100684 mbedtls_lmots_private_t *priv_ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100685{
Raef Colese9479a02022-09-01 16:06:35 +0100686 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 +0100687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
688
Raef Coles8ff6df52021-07-21 12:42:15 +0100689 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100690 if( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100691 {
692 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
693 }
694
Raef Colese9479a02022-09-01 16:06:35 +0100695 ret = hash_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100696 ( unsigned char * )priv_ctx->private_key, NULL,
697 NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100698 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100699 {
700 return( ret );
701 }
702
Raef Colesf5632d32022-09-01 09:56:52 +0100703 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100704 ( unsigned char * )y_hashed_digits,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100705 ctx->public_key );
Raef Colese0a17612022-09-02 16:04:47 +0100706 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100707 {
708 return( ret );
709 }
710
Raef Colesf5632d32022-09-01 09:56:52 +0100711 memcpy( &ctx->params, &priv_ctx->params,
712 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100713
Raef Coles9b88ee52022-09-02 12:04:21 +0100714 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100715
716 return( ret );
717}
718
719
720int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
721 unsigned char *key, size_t key_size,
722 size_t *key_len )
723{
Raef Colese9479a02022-09-01 16:06:35 +0100724 if( key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100725 {
726 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
727 }
728
Raef Colesf5632d32022-09-01 09:56:52 +0100729 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100730 {
731 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
732 }
733
Raef Colesf5632d32022-09-01 09:56:52 +0100734 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100735 MBEDTLS_LMOTS_TYPE_LEN,
736 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
737
738 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100739 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100740 MBEDTLS_LMOTS_I_KEY_ID_LEN );
741
Raef Coles9b88ee52022-09-02 12:04:21 +0100742 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
743 ctx->params.q_leaf_identifier,
744 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100745
Raef Colesf5632d32022-09-01 09:56:52 +0100746 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
Raef Colese9479a02022-09-01 16:06:35 +0100747 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100748
749 if( key_len != NULL )
750 {
Raef Colese9479a02022-09-01 16:06:35 +0100751 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100752 }
753
754 return( 0 );
755}
756
757int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
758 int (*f_rng)(void *, unsigned char *, size_t),
759 void *p_rng, const unsigned char *msg, size_t msg_size,
760 unsigned char *sig, size_t sig_size, size_t* sig_len )
761{
Raef Colese9479a02022-09-01 16:06:35 +0100762 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100763 /* Create a temporary buffer to prepare the signature in. This allows us to
764 * finish creating a signature (ensuring the process doesn't fail), and then
765 * erase the private key **before** writing any data into the sig parameter
766 * buffer. If data were directly written into the sig buffer, it might leak
767 * a partial signature on failure, which effectively compromises the private
768 * key.
769 */
Raef Colese9479a02022-09-01 16:06:35 +0100770 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
771 unsigned char tmp_c_random[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
773
Raef Colese0a17612022-09-02 16:04:47 +0100774 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100775 {
776 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
777 }
778
Raef Colese9479a02022-09-01 16:06:35 +0100779 if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100780 {
781 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
782 }
783
784 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100785 if( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100786 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100787 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100788 }
789
Raef Coles366d67d2022-09-01 17:23:12 +0100790 ret = f_rng( p_rng, tmp_c_random,
791 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Colese0a17612022-09-02 16:04:47 +0100792 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100793 {
794 return( ret );
795 }
796
Raef Colesf5632d32022-09-01 09:56:52 +0100797 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100798 msg, msg_size,
Raef Coles891c6132022-09-01 11:05:48 +0100799 tmp_c_random,
Raef Coles01c71a12022-08-31 15:55:00 +0100800 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100801 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100802 {
803 return( ret );
804 }
805
Raef Coles9b88ee52022-09-02 12:04:21 +0100806 ret = hash_digit_array( &ctx->params, ( unsigned char * )ctx->private_key,
807 NULL, tmp_digit_array, ( unsigned char * )tmp_sig );
Raef Colese0a17612022-09-02 16:04:47 +0100808 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100809 {
810 return( ret );
811 }
812
Raef Colesf5632d32022-09-01 09:56:52 +0100813 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100814 MBEDTLS_LMOTS_TYPE_LEN,
815 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100816
Raef Coles9c9027b2022-09-02 18:26:31 +0100817 /* Test hook to check if sig is being written to before we invalidate the
818 * private key.
819 */
820#if defined(MBEDTLS_TEST_HOOKS)
821 if( mbedtls_lmots_sign_private_key_invalidated_hook != NULL )
822 {
823 ret = ( *mbedtls_lmots_sign_private_key_invalidated_hook )( sig );
824 if( ret != 0 )
825 return( ret );
826 }
827#endif /* defined(MBEDTLS_TEST_HOOKS) */
828
Raef Coles8ff6df52021-07-21 12:42:15 +0100829 /* We've got a valid signature now, so it's time to make sure the private
830 * key can't be reused.
831 */
Raef Colesf5632d32022-09-01 09:56:52 +0100832 ctx->have_private_key = 0;
Raef Coles9b88ee52022-09-02 12:04:21 +0100833 mbedtls_platform_zeroize( ctx->private_key,
834 sizeof( ctx->private_key ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100835
Raef Coles891c6132022-09-01 11:05:48 +0100836 memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
Raef Colese9479a02022-09-01 16:06:35 +0100837 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) );
Raef Coles891c6132022-09-01 11:05:48 +0100838
Raef Colese9479a02022-09-01 16:06:35 +0100839 memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
840 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
841 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100842
Raef Coles01c71a12022-08-31 15:55:00 +0100843 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100844 {
Raef Colese9479a02022-09-01 16:06:35 +0100845 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100846 }
847
848 return( 0 );
849}
850
Raef Colesab4f8742022-09-01 12:24:31 +0100851#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles7dce69a2022-08-24 14:07:06 +0100852#endif /* MBEDTLS_LMS_C */