blob: 8e56696e5768d6d5a624042f351f1d8f000a166a [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 Coles0a967cc2022-09-02 17:46:15 +0100102/* Calculate the checksum digits that are appended to the end of the LMOTS digit
103 * string. See NIST SP800-208 section 3.1 or RFC8554 Algorithm 2 for details of
104 * the checksum algorithm.
105 *
106 * \param params The LMOTS parameter set, I and q values which
107 * describe the key being used.
108 *
109 * \param digest The digit string to create the digest from. As
110 * this does not contain a checksum, it is the same
111 * size as a hash output.
112 */
Raef Colese9479a02022-09-01 16:06:35 +0100113static unsigned short lmots_checksum_calculate( const mbedtls_lmots_parameters_t *params,
114 const unsigned char* digest )
Raef Coles8ff6df52021-07-21 12:42:15 +0100115{
116 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100117 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100118
Raef Colese9479a02022-09-01 16:06:35 +0100119 for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100120 {
Raef Coles01c71a12022-08-31 15:55:00 +0100121 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +0100122 }
123
124 return sum;
125}
126
Raef Coles0a967cc2022-09-02 17:46:15 +0100127/* Create the string of digest digits (in the base determined by the Winternitz
128 * parameter with the checksum appended to the end (Q || cksm(Q)). See NIST
129 * SP800-208 section 3.1 or RFC8554 Algorithm 3 step 5 (also used in Algorithm
130 * 4b step 3) for details.
131 *
132 * \param params The LMOTS parameter set, I and q values which
133 * describe the key being used.
134 *
135 * \param msg The message that will be hashed to create the
136 * digest.
137 *
138 * \param msg_size The size of the message.
139 *
140 * \param C_random_value The random value that will be combined with the
141 * message digest. This is always the same size as a
142 * hash output for whichever hash algorithm is
143 * determined by the parameter set.
144 *
145 * \param output An output containing the digit string (+
146 * checksum) of length P digits (in the case of
147 * MBEDTLS_LMOTS_SHA256_N32_W8, this means it is of
148 * size P bytes).
149 */
Raef Coles01c71a12022-08-31 15:55:00 +0100150static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *params,
151 const unsigned char *msg,
152 size_t msg_len,
Raef Colese9479a02022-09-01 16:06:35 +0100153 const unsigned char *C_random_value,
154 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100155{
Raef Colesc8f96042022-08-25 13:49:54 +0100156 psa_hash_operation_t op;
157 psa_status_t status;
158 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100159 unsigned short checksum;
160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
161
Raef Coles9b88ee52022-09-02 12:04:21 +0100162 op = psa_hash_operation_init( );
Raef Colesc8f96042022-08-25 13:49:54 +0100163 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
164 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100165 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100166 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100167
Raef Colesf5632d32022-09-01 09:56:52 +0100168 status = psa_hash_update( &op, params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100169 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100170 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100171 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100172 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100173
Raef Colesf5632d32022-09-01 09:56:52 +0100174 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100175 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100176 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100177 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100178 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100179
Raef Coles01c71a12022-08-31 15:55:00 +0100180 status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100181 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100182 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100183 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100184
Raef Colese9479a02022-09-01 16:06:35 +0100185 status = psa_hash_update( &op, C_random_value,
186 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100187 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100188 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100189 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100190
Raef Colesc8f96042022-08-25 13:49:54 +0100191 status = psa_hash_update( &op, msg, msg_len );
192 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100193 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100194 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100195
Raef Colese9479a02022-09-01 16:06:35 +0100196 status = psa_hash_finish( &op, out,
Raef Colesfa24f9d2022-09-02 17:46:52 +0100197 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colesc8f96042022-08-25 13:49:54 +0100198 &output_hash_len );
199 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100200 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100201 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100202
Raef Colese9479a02022-09-01 16:06:35 +0100203 checksum = lmots_checksum_calculate( params, out );
204 unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN,
205 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100206
Raef Coles01c71a12022-08-31 15:55:00 +0100207exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100208 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100209
210 return( ret );
211}
212
Raef Coles0a967cc2022-09-02 17:46:15 +0100213/* Hash each element of the string of digits (+ checksum), producing a hash
214 * output for each element. This is used in several places (by varying the
215 * hash_idx_min/max_values) in order to calculate a public key from a private
216 * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554
217 * Algorithm 3 step 5), and to calculate a public key candidate from a
218 * signature and message (RFC8554 Algorithm 4b step 3).
219 *
220 * \param params The LMOTS parameter set, I and q values which
221 * describe the key being used.
222 *
223 * \param x_digit_array The array of digits (of size P, 34 in the case of
224 * MBEDTLS_LMOTS_SHA256_N32_W8).
225 *
226 * \param hash_idx_min_values An array of the starting values of the j iterator
227 * for each of the members of the digit array. If
228 * this value in NULL, then all iterators will start
229 * at 0.
230 *
231 * \param hash_idx_max_values An array of the upper bound values of the j
232 * iterator for each of the members of the digit
233 * array. If this value in NULL, then iterator is
234 * bounded to be less than 2^w - 1 (255 in the case
235 * of MBEDTLS_LMOTS_SHA256_N32_W8)
236 *
237 * \param output An array containing a hash output for each member
238 * of the digit string P. In the case of
239 * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 *
240 * 34.
241 */
Raef Coles01c71a12022-08-31 15:55:00 +0100242static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100243 const unsigned char *x_digit_array,
Raef Coles01c71a12022-08-31 15:55:00 +0100244 const unsigned char *hash_idx_min_values,
245 const unsigned char *hash_idx_max_values,
Raef Colese9479a02022-09-01 16:06:35 +0100246 unsigned char *output )
Raef Coles8ff6df52021-07-21 12:42:15 +0100247{
Raef Coles8738a492022-09-02 17:13:01 +0100248 unsigned int i_digit_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100249 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8738a492022-09-02 17:13:01 +0100250 unsigned int j_hash_idx;
251 unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100252 unsigned int j_hash_idx_min;
253 unsigned int j_hash_idx_max;
Raef Colesc8f96042022-08-25 13:49:54 +0100254 psa_hash_operation_t op;
255 psa_status_t status;
256 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100257 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
259
Raef Coles9b88ee52022-09-02 12:04:21 +0100260 op = psa_hash_operation_init( );
Raef Coles01c71a12022-08-31 15:55:00 +0100261
Raef Colese9479a02022-09-01 16:06:35 +0100262 for ( i_digit_idx = 0;
263 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
264 i_digit_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100265 {
266
Raef Coles366d67d2022-09-01 17:23:12 +0100267 memcpy( tmp_hash,
268 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
Raef Colese9479a02022-09-01 16:06:35 +0100269 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100270
Raef Coles366d67d2022-09-01 17:23:12 +0100271 j_hash_idx_min = hash_idx_min_values != NULL ?
272 hash_idx_min_values[i_digit_idx] : 0;
273 j_hash_idx_max = hash_idx_max_values != NULL ?
274 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100275
Raef Coles8738a492022-09-02 17:13:01 +0100276 for ( j_hash_idx = j_hash_idx_min;
Raef Coles366d67d2022-09-01 17:23:12 +0100277 j_hash_idx < j_hash_idx_max;
278 j_hash_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100279 {
Raef Colesc8f96042022-08-25 13:49:54 +0100280 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
281 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100282 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100283 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100284
Raef Coles01c71a12022-08-31 15:55:00 +0100285 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100286 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100287 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100288 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100289 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100290 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100291
Raef Coles01c71a12022-08-31 15:55:00 +0100292 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100293 params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100294 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100295 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100296 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100297 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100298
Raef Coles366d67d2022-09-01 17:23:12 +0100299 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
300 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100301 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100302 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100303 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100304 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100305
Raef Coles366d67d2022-09-01 17:23:12 +0100306 unsigned_int_to_network_bytes( j_hash_idx, J_HASH_IDX_LEN,
307 j_hash_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100308 status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN );
309 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100310 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100311 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100312
Raef Colese9479a02022-09-01 16:06:35 +0100313 status = psa_hash_update( &op, tmp_hash,
314 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100315 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100316 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100317 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100318
Raef Coles366d67d2022-09-01 17:23:12 +0100319 status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ),
320 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100321 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100322 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100323 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100324
Raef Colesc8f96042022-08-25 13:49:54 +0100325 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100326 }
327
Raef Coles366d67d2022-09-01 17:23:12 +0100328 memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
329 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100330 }
331
Raef Coles01c71a12022-08-31 15:55:00 +0100332exit:
Raef Colese0a17612022-09-02 16:04:47 +0100333 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100334 {
Raef Colesc8f96042022-08-25 13:49:54 +0100335 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100336 return( ret );
337 }
338
Raef Coles01c71a12022-08-31 15:55:00 +0100339 mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) );
340
Raef Coles8ff6df52021-07-21 12:42:15 +0100341 return ret;
342}
343
Raef Coles0a967cc2022-09-02 17:46:15 +0100344/* Combine the hashes of the digit array into a public key. This is used in
345 * in order to calculate a public key from a private key (RFC8554 Algorithm 1
346 * step 4), and to calculate a public key candidate from a signature and message
347 * (RFC8554 Algorithm 4b step 3).
348 *
349 * \param params The LMOTS parameter set, I and q values which describe
350 * the key being used.
351 * \param y_hashed_digits The array of hashes, one hash for each digit of the
352 * symbol array (which is of size P, 34 in the case of
353 * MBEDTLS_LMOTS_SHA256_N32_W8)
354 *
355 * \param pub_key The output public key (or candidate public key in
356 * case this is being run as part of signature
357 * verification), in the form of a hash output.
358 */
Raef Coles01c71a12022-08-31 15:55:00 +0100359static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100360 const unsigned char *y_hashed_digits,
Raef Coles01c71a12022-08-31 15:55:00 +0100361 unsigned char *pub_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100362{
Raef Colesc8f96042022-08-25 13:49:54 +0100363 psa_hash_operation_t op;
364 psa_status_t status;
365 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
367
Raef Colesc8f96042022-08-25 13:49:54 +0100368 op = psa_hash_operation_init( );
369 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
370 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100371 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100372 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100373
Raef Coles01c71a12022-08-31 15:55:00 +0100374 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100375 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100376 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100377 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100378 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100379 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100380
Raef Colesf5632d32022-09-01 09:56:52 +0100381 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100382 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100383 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100384 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100385 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100386
Raef Coles01c71a12022-08-31 15:55:00 +0100387 status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100388 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100389 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100390 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100391
Raef Colese9479a02022-09-01 16:06:35 +0100392 status = psa_hash_update( &op, y_hashed_digits,
393 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
394 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100395 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100396 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100397 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100398
Raef Coles366d67d2022-09-01 17:23:12 +0100399 status = psa_hash_finish( &op, pub_key,
400 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colese9479a02022-09-01 16:06:35 +0100401 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100402 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100403
Raef Coles01c71a12022-08-31 15:55:00 +0100404exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100405 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100406 return( ret );
407}
408
Raef Coles9b88ee52022-09-02 12:04:21 +0100409int mbedtls_lms_error_from_psa( psa_status_t status )
Raef Colesc8f96042022-08-25 13:49:54 +0100410{
Raef Coles9b88ee52022-09-02 12:04:21 +0100411 switch( status )
412 {
Raef Colesc8f96042022-08-25 13:49:54 +0100413 case PSA_SUCCESS:
414 return( 0 );
415 case PSA_ERROR_HARDWARE_FAILURE:
416 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
417 case PSA_ERROR_NOT_SUPPORTED:
418 return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
419 case PSA_ERROR_BUFFER_TOO_SMALL:
420 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
421 case PSA_ERROR_INVALID_ARGUMENT:
422 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
423 default:
424 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
425 }
426}
427
Raef Coles01c71a12022-08-31 15:55:00 +0100428void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100429{
Raef Coles01c71a12022-08-31 15:55:00 +0100430 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100431}
432
Raef Coles01c71a12022-08-31 15:55:00 +0100433void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100434{
Raef Coles01c71a12022-08-31 15:55:00 +0100435 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100436}
437
Raef Coles01c71a12022-08-31 15:55:00 +0100438int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
439 const unsigned char *key, size_t key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100440{
Raef Colesf5632d32022-09-01 09:56:52 +0100441 ctx->params.type =
Raef Coles01c71a12022-08-31 15:55:00 +0100442 network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
443 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
444
Raef Colese0a17612022-09-02 16:04:47 +0100445 if( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Colese9479a02022-09-01 16:06:35 +0100446 {
447 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
448 }
449
Raef Colesf5632d32022-09-01 09:56:52 +0100450 memcpy( ctx->params.I_key_identifier,
Raef Coles366d67d2022-09-01 17:23:12 +0100451 key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
452 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100453
Raef Colesf5632d32022-09-01 09:56:52 +0100454 memcpy( ctx->params.q_leaf_identifier,
Raef Coles366d67d2022-09-01 17:23:12 +0100455 key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
456 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100457
Raef Colesf5632d32022-09-01 09:56:52 +0100458 memcpy( ctx->public_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100459 key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100460 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100461
Raef Colesf5632d32022-09-01 09:56:52 +0100462 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100463
464 return( 0 );
465}
466
Raef Coles01c71a12022-08-31 15:55:00 +0100467int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
468 const unsigned char *msg,
469 size_t msg_size,
470 const unsigned char *sig,
471 size_t sig_size,
472 unsigned char *out,
473 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100474 size_t *out_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100475{
Raef Colese9479a02022-09-01 16:06:35 +0100476 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
477 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 +0100478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
479
Raef Colese0a17612022-09-02 16:04:47 +0100480 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100481 {
482 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
483 }
484
Raef Colese0a17612022-09-02 16:04:47 +0100485 if( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
Raef Colese9479a02022-09-01 16:06:35 +0100486 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100487 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100488 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100489 }
490
Raef Coles01c71a12022-08-31 15:55:00 +0100491 ret = create_digit_array_with_checksum( params, msg, msg_size,
492 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
493 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100494 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100495 {
496 return ( ret );
497 }
498
Raef Coles01c71a12022-08-31 15:55:00 +0100499 ret = hash_digit_array( params,
Raef Colese9479a02022-09-01 16:06:35 +0100500 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100501 tmp_digit_array, NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100502 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100503 {
504 return ( ret );
505 }
506
Raef Colese9479a02022-09-01 16:06:35 +0100507 ret = public_key_from_hashed_digit_array( params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100508 ( unsigned char * )y_hashed_digits,
Raef Colese9479a02022-09-01 16:06:35 +0100509 out );
Raef Colese0a17612022-09-02 16:04:47 +0100510 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100511 {
512 return ( ret );
513 }
514
Raef Colese0a17612022-09-02 16:04:47 +0100515 if( out_len != NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100516 {
Raef Colese9479a02022-09-01 16:06:35 +0100517 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100518 }
519
Raef Coles8ff6df52021-07-21 12:42:15 +0100520 return( 0 );
521}
522
Raef Coles01c71a12022-08-31 15:55:00 +0100523int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
524 size_t msg_size, const unsigned char *sig,
525 size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100526{
Raef Colese9479a02022-09-01 16:06:35 +0100527 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
529
Raef Colese0a17612022-09-02 16:04:47 +0100530 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100531 {
532 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
533 }
534
Raef Colese0a17612022-09-02 16:04:47 +0100535 if( !ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100536 {
537 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
538 }
539
Raef Coles9b88ee52022-09-02 12:04:21 +0100540 if( ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100541 {
542 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
543 }
544
Raef Colese0a17612022-09-02 16:04:47 +0100545 if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100546 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100547 {
548 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
549 }
550
Raef Colesf5632d32022-09-01 09:56:52 +0100551 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100552 msg, msg_size, sig, sig_size,
553 Kc_public_key_candidate,
Raef Colese9479a02022-09-01 16:06:35 +0100554 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100555 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100556 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100557 {
558 return( ret );
559 }
560
Raef Colese0a17612022-09-02 16:04:47 +0100561 if( memcmp( &Kc_public_key_candidate, ctx->public_key,
Raef Colesf5632d32022-09-01 09:56:52 +0100562 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100563 {
564 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
565 }
566
567 return( 0 );
568}
569
Raef Colesab4f8742022-09-01 12:24:31 +0100570#ifdef MBEDTLS_LMS_PRIVATE
571
Raef Coles01c71a12022-08-31 15:55:00 +0100572void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx )
573{
574 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
575}
576
577void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx )
578{
579 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
580}
581
582int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
583 mbedtls_lmots_algorithm_type_t type,
584 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
585 uint32_t q_leaf_identifier,
586 const unsigned char *seed,
587 size_t seed_size )
588{
589 psa_hash_operation_t op;
590 psa_status_t status;
591 size_t output_hash_len;
592 unsigned int i_digit_idx;
593 unsigned char i_digit_idx_bytes[2];
594 unsigned char const_bytes[1];
595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
596
Raef Colese0a17612022-09-02 16:04:47 +0100597 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100598 {
599 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
600 }
601
Raef Colese0a17612022-09-02 16:04:47 +0100602 if( type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles9b88ee52022-09-02 12:04:21 +0100603 {
Raef Coles01c71a12022-08-31 15:55:00 +0100604 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
605 }
606
Raef Colesebd35b52022-09-01 11:52:17 +0100607 op = psa_hash_operation_init( );
608
Raef Colesf5632d32022-09-01 09:56:52 +0100609 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100610
Raef Colesf5632d32022-09-01 09:56:52 +0100611 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100612 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100613 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100614
Raef Coles9b88ee52022-09-02 12:04:21 +0100615 unsigned_int_to_network_bytes( q_leaf_identifier,
616 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
617 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100618
619 unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes );
620
Raef Colese9479a02022-09-01 16:06:35 +0100621 for ( i_digit_idx = 0;
622 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
623 i_digit_idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100624 {
Raef Coles01c71a12022-08-31 15:55:00 +0100625 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
626 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100627 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100628 goto exit;
629
630 ret = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100631 ctx->params.I_key_identifier,
632 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100633 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100634 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100635 goto exit;
636
637 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100638 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100639 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
640 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100641 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100642 goto exit;
643
Raef Coles366d67d2022-09-01 17:23:12 +0100644 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
645 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100646 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
647 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100648 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100649 goto exit;
650
Raef Coles9b88ee52022-09-02 12:04:21 +0100651 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100652 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100653 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100654 goto exit;
655
656 status = psa_hash_update( &op, seed, seed_size );
657 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100658 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100659 goto exit;
660
661 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100662 ctx->private_key[i_digit_idx],
Raef Colese9479a02022-09-01 16:06:35 +0100663 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
664 &output_hash_len );
Raef Coles01c71a12022-08-31 15:55:00 +0100665 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100666 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100667 goto exit;
668
669 psa_hash_abort( &op );
670 }
671
Raef Colesf5632d32022-09-01 09:56:52 +0100672 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100673
674exit:
Raef Colese0a17612022-09-02 16:04:47 +0100675 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100676 {
677 psa_hash_abort( &op );
678 return( ret );
679 }
680
681 return ret;
682}
683
684int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles9b88ee52022-09-02 12:04:21 +0100685 mbedtls_lmots_private_t *priv_ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100686{
Raef Colese9479a02022-09-01 16:06:35 +0100687 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 +0100688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
689
Raef Coles8ff6df52021-07-21 12:42:15 +0100690 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100691 if( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100692 {
693 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
694 }
695
Raef Colese9479a02022-09-01 16:06:35 +0100696 ret = hash_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100697 ( unsigned char * )priv_ctx->private_key, NULL,
698 NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100699 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100700 {
701 return( ret );
702 }
703
Raef Colesf5632d32022-09-01 09:56:52 +0100704 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100705 ( unsigned char * )y_hashed_digits,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100706 ctx->public_key );
Raef Colese0a17612022-09-02 16:04:47 +0100707 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100708 {
709 return( ret );
710 }
711
Raef Colesf5632d32022-09-01 09:56:52 +0100712 memcpy( &ctx->params, &priv_ctx->params,
713 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100714
Raef Coles9b88ee52022-09-02 12:04:21 +0100715 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100716
717 return( ret );
718}
719
720
721int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
722 unsigned char *key, size_t key_size,
723 size_t *key_len )
724{
Raef Colese9479a02022-09-01 16:06:35 +0100725 if( key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100726 {
727 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
728 }
729
Raef Colesf5632d32022-09-01 09:56:52 +0100730 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100731 {
732 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
733 }
734
Raef Colesf5632d32022-09-01 09:56:52 +0100735 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100736 MBEDTLS_LMOTS_TYPE_LEN,
737 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
738
739 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100740 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100741 MBEDTLS_LMOTS_I_KEY_ID_LEN );
742
Raef Coles9b88ee52022-09-02 12:04:21 +0100743 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
744 ctx->params.q_leaf_identifier,
745 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100746
Raef Colesf5632d32022-09-01 09:56:52 +0100747 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
Raef Colese9479a02022-09-01 16:06:35 +0100748 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100749
750 if( key_len != NULL )
751 {
Raef Colese9479a02022-09-01 16:06:35 +0100752 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100753 }
754
755 return( 0 );
756}
757
758int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
759 int (*f_rng)(void *, unsigned char *, size_t),
760 void *p_rng, const unsigned char *msg, size_t msg_size,
761 unsigned char *sig, size_t sig_size, size_t* sig_len )
762{
Raef Colese9479a02022-09-01 16:06:35 +0100763 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100764 /* Create a temporary buffer to prepare the signature in. This allows us to
765 * finish creating a signature (ensuring the process doesn't fail), and then
766 * erase the private key **before** writing any data into the sig parameter
767 * buffer. If data were directly written into the sig buffer, it might leak
768 * a partial signature on failure, which effectively compromises the private
769 * key.
770 */
Raef Colese9479a02022-09-01 16:06:35 +0100771 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
772 unsigned char tmp_c_random[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
774
Raef Colese0a17612022-09-02 16:04:47 +0100775 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100776 {
777 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
778 }
779
Raef Colese9479a02022-09-01 16:06:35 +0100780 if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100781 {
782 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
783 }
784
785 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100786 if( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100787 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100788 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100789 }
790
Raef Coles366d67d2022-09-01 17:23:12 +0100791 ret = f_rng( p_rng, tmp_c_random,
792 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Colese0a17612022-09-02 16:04:47 +0100793 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100794 {
795 return( ret );
796 }
797
Raef Colesf5632d32022-09-01 09:56:52 +0100798 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100799 msg, msg_size,
Raef Coles891c6132022-09-01 11:05:48 +0100800 tmp_c_random,
Raef Coles01c71a12022-08-31 15:55:00 +0100801 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100802 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100803 {
804 return( ret );
805 }
806
Raef Coles9b88ee52022-09-02 12:04:21 +0100807 ret = hash_digit_array( &ctx->params, ( unsigned char * )ctx->private_key,
808 NULL, tmp_digit_array, ( unsigned char * )tmp_sig );
Raef Colese0a17612022-09-02 16:04:47 +0100809 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100810 {
811 return( ret );
812 }
813
Raef Colesf5632d32022-09-01 09:56:52 +0100814 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100815 MBEDTLS_LMOTS_TYPE_LEN,
816 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100817
818 /* We've got a valid signature now, so it's time to make sure the private
819 * key can't be reused.
820 */
Raef Colesf5632d32022-09-01 09:56:52 +0100821 ctx->have_private_key = 0;
Raef Coles9b88ee52022-09-02 12:04:21 +0100822 mbedtls_platform_zeroize( ctx->private_key,
823 sizeof( ctx->private_key ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100824
Raef Coles891c6132022-09-01 11:05:48 +0100825 memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
Raef Colese9479a02022-09-01 16:06:35 +0100826 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) );
Raef Coles891c6132022-09-01 11:05:48 +0100827
Raef Colese9479a02022-09-01 16:06:35 +0100828 memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
829 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
830 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100831
Raef Coles01c71a12022-08-31 15:55:00 +0100832 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100833 {
Raef Colese9479a02022-09-01 16:06:35 +0100834 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100835 }
836
837 return( 0 );
838}
839
Raef Colesab4f8742022-09-01 12:24:31 +0100840#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles7dce69a2022-08-24 14:07:06 +0100841#endif /* MBEDTLS_LMS_C */