blob: e09e3e52939c4ac69ba8e922bb3e2ae0c5b36d2f [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Raef Coles8ff6df52021-07-21 12:42:15 +01006 */
7
8/*
9 * The following sources were referenced in the design of this implementation
10 * of the LM-OTS algorithm:
11 *
12 * [1] IETF RFC8554
13 * D. McGrew, M. Curcio, S.Fluhrer
14 * https://datatracker.ietf.org/doc/html/rfc8554
15 *
16 * [2] NIST Special Publication 800-208
17 * David A. Cooper et. al.
18 * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
19 */
20
21#include "common.h"
22
Raef Coles5127e852022-10-07 10:35:56 +010023#if defined(MBEDTLS_LMS_C)
Raef Coles8ff6df52021-07-21 12:42:15 +010024
25#include <string.h>
26
Raef Coles7dce69a2022-08-24 14:07:06 +010027#include "lmots.h"
28
Raef Colesc8f96042022-08-25 13:49:54 +010029#include "mbedtls/lms.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010030#include "mbedtls/platform_util.h"
31#include "mbedtls/error.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020032#include "psa_util_internal.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010033
Raef Colesc8f96042022-08-25 13:49:54 +010034#include "psa/crypto.h"
35
Andrzej Kurek00644842023-05-30 05:45:00 -040036/* Define a local translating function to save code size by not using too many
37 * arguments in each translating place. */
38static int local_err_translation(psa_status_t status)
39{
40 return psa_status_to_mbedtls(status, psa_to_lms_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040041 ARRAY_LENGTH(psa_to_lms_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040042 psa_generic_status_to_mbedtls);
43}
44#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050045
Raef Coles57d53282022-09-27 11:30:51 +010046#define PUBLIC_KEY_TYPE_OFFSET (0)
47#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
48 MBEDTLS_LMOTS_TYPE_LEN)
49#define PUBLIC_KEY_Q_LEAF_ID_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
50 MBEDTLS_LMOTS_I_KEY_ID_LEN)
51#define PUBLIC_KEY_KEY_HASH_OFFSET (PUBLIC_KEY_Q_LEAF_ID_OFFSET + \
52 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010053
54/* We only support parameter sets that use 8-bit digits, as it does not require
55 * translation logic between digits and bytes */
56#define W_WINTERNITZ_PARAMETER (8u)
57#define CHECKSUM_LEN (2)
58#define I_DIGIT_IDX_LEN (2)
59#define J_HASH_IDX_LEN (1)
60#define D_CONST_LEN (2)
61
Raef Coles9b88ee52022-09-02 12:04:21 +010062#define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
Raef Coles01c71a12022-08-31 15:55:00 +010063
Raef Coles9b88ee52022-09-02 12:04:21 +010064#define D_CONST_LEN (2)
Gilles Peskine449bd832023-01-11 14:50:10 +010065static const unsigned char D_PUBLIC_CONSTANT_BYTES[D_CONST_LEN] = { 0x80, 0x80 };
66static const unsigned char D_MESSAGE_CONSTANT_BYTES[D_CONST_LEN] = { 0x81, 0x81 };
Raef Coles8ff6df52021-07-21 12:42:15 +010067
Raef Coles9c9027b2022-09-02 18:26:31 +010068#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +010069int (*mbedtls_lmots_sign_private_key_invalidated_hook)(unsigned char *) = NULL;
Raef Coles9c9027b2022-09-02 18:26:31 +010070#endif /* defined(MBEDTLS_TEST_HOOKS) */
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072void mbedtls_lms_unsigned_int_to_network_bytes(unsigned int val, size_t len,
73 unsigned char *bytes)
Raef Coles8ff6df52021-07-21 12:42:15 +010074{
75 size_t idx;
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 for (idx = 0; idx < len; idx++) {
78 bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF;
Raef Coles8ff6df52021-07-21 12:42:15 +010079 }
80}
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082unsigned int mbedtls_lms_network_bytes_to_unsigned_int(size_t len,
83 const unsigned char *bytes)
Raef Coles8ff6df52021-07-21 12:42:15 +010084{
85 size_t idx;
86 unsigned int val = 0;
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 for (idx = 0; idx < len; idx++) {
89 val |= ((unsigned int) bytes[idx]) << (8 * (len - 1 - idx));
Raef Coles8ff6df52021-07-21 12:42:15 +010090 }
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return val;
Raef Coles8ff6df52021-07-21 12:42:15 +010093}
94
Raef Coles0a967cc2022-09-02 17:46:15 +010095/* Calculate the checksum digits that are appended to the end of the LMOTS digit
96 * string. See NIST SP800-208 section 3.1 or RFC8554 Algorithm 2 for details of
97 * the checksum algorithm.
98 *
Raef Coles98d6e222022-09-23 09:04:04 +010099 * params The LMOTS parameter set, I and q values which
100 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100101 *
Raef Coles98d6e222022-09-23 09:04:04 +0100102 * digest The digit string to create the digest from. As
103 * this does not contain a checksum, it is the same
104 * size as a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100105 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106static unsigned short lmots_checksum_calculate(const mbedtls_lmots_parameters_t *params,
107 const unsigned char *digest)
Raef Coles8ff6df52021-07-21 12:42:15 +0100108{
109 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100110 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 for (idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++) {
Raef Coles01c71a12022-08-31 15:55:00 +0100113 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +0100114 }
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 return sum;
Raef Coles8ff6df52021-07-21 12:42:15 +0100117}
118
Raef Coles0a967cc2022-09-02 17:46:15 +0100119/* Create the string of digest digits (in the base determined by the Winternitz
120 * parameter with the checksum appended to the end (Q || cksm(Q)). See NIST
121 * SP800-208 section 3.1 or RFC8554 Algorithm 3 step 5 (also used in Algorithm
122 * 4b step 3) for details.
123 *
Raef Coles98d6e222022-09-23 09:04:04 +0100124 * params The LMOTS parameter set, I and q values which
125 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100126 *
Raef Coles98d6e222022-09-23 09:04:04 +0100127 * msg The message that will be hashed to create the
128 * digest.
Raef Coles0a967cc2022-09-02 17:46:15 +0100129 *
Raef Coles98d6e222022-09-23 09:04:04 +0100130 * msg_size The size of the message.
Raef Coles0a967cc2022-09-02 17:46:15 +0100131 *
Raef Coles98d6e222022-09-23 09:04:04 +0100132 * C_random_value The random value that will be combined with the
133 * message digest. This is always the same size as a
134 * hash output for whichever hash algorithm is
135 * determined by the parameter set.
Raef Coles0a967cc2022-09-02 17:46:15 +0100136 *
Raef Coles98d6e222022-09-23 09:04:04 +0100137 * output An output containing the digit string (+
138 * checksum) of length P digits (in the case of
139 * MBEDTLS_LMOTS_SHA256_N32_W8, this means it is of
140 * size P bytes).
Raef Coles0a967cc2022-09-02 17:46:15 +0100141 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142static int create_digit_array_with_checksum(const mbedtls_lmots_parameters_t *params,
143 const unsigned char *msg,
144 size_t msg_len,
145 const unsigned char *C_random_value,
146 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100147{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100148 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100150 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100151 unsigned short checksum;
Raef Coles8ff6df52021-07-21 12:42:15 +0100152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
154 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 status = psa_hash_update(&op, params->I_key_identifier,
159 MBEDTLS_LMOTS_I_KEY_ID_LEN);
160 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 status = psa_hash_update(&op, params->q_leaf_identifier,
165 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
166 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 status = psa_hash_update(&op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN);
171 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100172 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 status = psa_hash_update(&op, C_random_value,
176 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type));
177 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 status = psa_hash_update(&op, msg, msg_len);
182 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100183 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 status = psa_hash_finish(&op, out,
187 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
188 &output_hash_len);
189 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 checksum = lmots_checksum_calculate(params, out);
194 mbedtls_lms_unsigned_int_to_network_bytes(checksum, CHECKSUM_LEN,
195 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100196
Raef Coles01c71a12022-08-31 15:55:00 +0100197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100199
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500200 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100201}
202
Raef Coles0a967cc2022-09-02 17:46:15 +0100203/* Hash each element of the string of digits (+ checksum), producing a hash
204 * output for each element. This is used in several places (by varying the
205 * hash_idx_min/max_values) in order to calculate a public key from a private
206 * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554
207 * Algorithm 3 step 5), and to calculate a public key candidate from a
208 * signature and message (RFC8554 Algorithm 4b step 3).
209 *
Raef Coles98d6e222022-09-23 09:04:04 +0100210 * params The LMOTS parameter set, I and q values which
211 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100212 *
Raef Coles98d6e222022-09-23 09:04:04 +0100213 * x_digit_array The array of digits (of size P, 34 in the case of
214 * MBEDTLS_LMOTS_SHA256_N32_W8).
Raef Coles0a967cc2022-09-02 17:46:15 +0100215 *
Raef Coles98d6e222022-09-23 09:04:04 +0100216 * hash_idx_min_values An array of the starting values of the j iterator
217 * for each of the members of the digit array. If
218 * this value in NULL, then all iterators will start
219 * at 0.
Raef Coles0a967cc2022-09-02 17:46:15 +0100220 *
Raef Coles98d6e222022-09-23 09:04:04 +0100221 * hash_idx_max_values An array of the upper bound values of the j
222 * iterator for each of the members of the digit
223 * array. If this value in NULL, then iterator is
224 * bounded to be less than 2^w - 1 (255 in the case
225 * of MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100226 *
Raef Coles98d6e222022-09-23 09:04:04 +0100227 * output An array containing a hash output for each member
228 * of the digit string P. In the case of
229 * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 *
230 * 34.
Raef Coles0a967cc2022-09-02 17:46:15 +0100231 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232static int hash_digit_array(const mbedtls_lmots_parameters_t *params,
233 const unsigned char *x_digit_array,
234 const unsigned char *hash_idx_min_values,
235 const unsigned char *hash_idx_max_values,
236 unsigned char *output)
Raef Coles8ff6df52021-07-21 12:42:15 +0100237{
Raef Coles8738a492022-09-02 17:13:01 +0100238 unsigned int i_digit_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100239 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8738a492022-09-02 17:13:01 +0100240 unsigned int j_hash_idx;
241 unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100242 unsigned int j_hash_idx_min;
243 unsigned int j_hash_idx_max;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100244 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100246 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100247 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 for (i_digit_idx = 0;
250 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
251 i_digit_idx++) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 memcpy(tmp_hash,
254 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
255 MBEDTLS_LMOTS_N_HASH_LEN(params->type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100256
Raef Coles366d67d2022-09-01 17:23:12 +0100257 j_hash_idx_min = hash_idx_min_values != NULL ?
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 hash_idx_min_values[i_digit_idx] : 0;
Raef Coles366d67d2022-09-01 17:23:12 +0100259 j_hash_idx_max = hash_idx_max_values != NULL ?
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 for (j_hash_idx = j_hash_idx_min;
263 j_hash_idx < j_hash_idx_max;
264 j_hash_idx++) {
265 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
266 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100267 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 status = psa_hash_update(&op,
271 params->I_key_identifier,
272 MBEDTLS_LMOTS_I_KEY_ID_LEN);
273 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100274 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 status = psa_hash_update(&op,
278 params->q_leaf_identifier,
279 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
280 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_lms_unsigned_int_to_network_bytes(i_digit_idx,
285 I_DIGIT_IDX_LEN,
286 i_digit_idx_bytes);
287 status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN);
288 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100289 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 mbedtls_lms_unsigned_int_to_network_bytes(j_hash_idx,
293 J_HASH_IDX_LEN,
294 j_hash_idx_bytes);
295 status = psa_hash_update(&op, j_hash_idx_bytes, J_HASH_IDX_LEN);
296 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 status = psa_hash_update(&op, tmp_hash,
301 MBEDTLS_LMOTS_N_HASH_LEN(params->type));
302 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100303 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 status = psa_hash_finish(&op, tmp_hash, sizeof(tmp_hash),
307 &output_hash_len);
308 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100309 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 memcpy(&output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
316 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100317 }
318
Raef Coles01c71a12022-08-31 15:55:00 +0100319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 psa_hash_abort(&op);
321 mbedtls_platform_zeroize(tmp_hash, sizeof(tmp_hash));
Raef Coles01c71a12022-08-31 15:55:00 +0100322
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500323 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100324}
325
Raef Coles0a967cc2022-09-02 17:46:15 +0100326/* Combine the hashes of the digit array into a public key. This is used in
327 * in order to calculate a public key from a private key (RFC8554 Algorithm 1
328 * step 4), and to calculate a public key candidate from a signature and message
329 * (RFC8554 Algorithm 4b step 3).
330 *
Raef Coles98d6e222022-09-23 09:04:04 +0100331 * params The LMOTS parameter set, I and q values which describe
332 * the key being used.
333 * y_hashed_digits The array of hashes, one hash for each digit of the
334 * symbol array (which is of size P, 34 in the case of
335 * MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100336 *
Raef Coles98d6e222022-09-23 09:04:04 +0100337 * pub_key The output public key (or candidate public key in
338 * case this is being run as part of signature
339 * verification), in the form of a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100340 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341static int public_key_from_hashed_digit_array(const mbedtls_lmots_parameters_t *params,
342 const unsigned char *y_hashed_digits,
343 unsigned char *pub_key)
Raef Coles8ff6df52021-07-21 12:42:15 +0100344{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100345 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100347 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
350 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100351 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 status = psa_hash_update(&op,
355 params->I_key_identifier,
356 MBEDTLS_LMOTS_I_KEY_ID_LEN);
357 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 status = psa_hash_update(&op, params->q_leaf_identifier,
362 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
363 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 status = psa_hash_update(&op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN);
368 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100369 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 status = psa_hash_update(&op, y_hashed_digits,
373 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
374 MBEDTLS_LMOTS_N_HASH_LEN(params->type));
375 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100376 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 status = psa_hash_finish(&op, pub_key,
380 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
381 &output_hash_len);
382 if (status != PSA_SUCCESS) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100383
Raef Coles01c71a12022-08-31 15:55:00 +0100384exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 psa_hash_abort(&op);
386 }
Raef Coles29117d22022-10-07 11:46:06 +0100387
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500388 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100389}
390
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500391#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100392int mbedtls_lms_error_from_psa(psa_status_t status)
Raef Colesc8f96042022-08-25 13:49:54 +0100393{
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 switch (status) {
Raef Colesc8f96042022-08-25 13:49:54 +0100395 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return 0;
Raef Colesc8f96042022-08-25 13:49:54 +0100397 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Raef Colesc8f96042022-08-25 13:49:54 +0100399 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100401 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Colesc8f96042022-08-25 13:49:54 +0100403 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colesc8f96042022-08-25 13:49:54 +0100405 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Raef Colesc8f96042022-08-25 13:49:54 +0100407 }
408}
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500409#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Raef Colesc8f96042022-08-25 13:49:54 +0100410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411void mbedtls_lmots_public_init(mbedtls_lmots_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100412{
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 memset(ctx, 0, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100414}
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416void mbedtls_lmots_public_free(mbedtls_lmots_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100417{
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100419}
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421int mbedtls_lmots_import_public_key(mbedtls_lmots_public_t *ctx,
422 const unsigned char *key, size_t key_len)
Raef Coles8ff6df52021-07-21 12:42:15 +0100423{
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (key_len < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
425 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese89488d2022-10-07 16:06:35 +0100426 }
427
Raef Colesf5632d32022-09-01 09:56:52 +0100428 ctx->params.type =
Agathiyan Bragadeesh10b67752023-07-12 11:19:17 +0100429 (mbedtls_lmots_algorithm_type_t) mbedtls_lms_network_bytes_to_unsigned_int(
430 MBEDTLS_LMOTS_TYPE_LEN,
431 key +
432 MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (key_len != MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
435 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese9479a02022-09-01 16:06:35 +0100436 }
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 memcpy(ctx->params.I_key_identifier,
439 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
440 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Raef Coles01c71a12022-08-31 15:55:00 +0100441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 memcpy(ctx->params.q_leaf_identifier,
443 key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
444 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
Raef Coles01c71a12022-08-31 15:55:00 +0100445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 memcpy(ctx->public_key,
447 key + PUBLIC_KEY_KEY_HASH_OFFSET,
448 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100449
Raef Colesf5632d32022-09-01 09:56:52 +0100450 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100453}
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455int mbedtls_lmots_export_public_key(const mbedtls_lmots_public_t *ctx,
456 unsigned char *key, size_t key_size,
457 size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100458{
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
460 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100461 }
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (!ctx->have_public_key) {
464 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100465 }
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
468 MBEDTLS_LMOTS_TYPE_LEN,
469 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles370cc432022-10-07 16:07:33 +0100470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
472 ctx->params.I_key_identifier,
473 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Raef Coles370cc432022-10-07 16:07:33 +0100474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 memcpy(key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
476 ctx->params.q_leaf_identifier,
477 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
Raef Coles370cc432022-10-07 16:07:33 +0100478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 memcpy(key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
480 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100483 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
484 }
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100487}
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489int mbedtls_lmots_calculate_public_key_candidate(const mbedtls_lmots_parameters_t *params,
490 const unsigned char *msg,
491 size_t msg_size,
492 const unsigned char *sig,
493 size_t sig_size,
494 unsigned char *out,
495 size_t out_size,
496 size_t *out_len)
Raef Coles8ff6df52021-07-21 12:42:15 +0100497{
Raef Colese9479a02022-09-01 16:06:35 +0100498 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
499 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 +0100500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (msg == NULL && msg_size != 0) {
503 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100504 }
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
507 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type)) {
508 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100509 }
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 ret = create_digit_array_with_checksum(params, msg, msg_size,
512 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
513 tmp_digit_array);
514 if (ret) {
515 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100516 }
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 ret = hash_digit_array(params,
519 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
520 tmp_digit_array, NULL, (unsigned char *) y_hashed_digits);
521 if (ret) {
522 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100523 }
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 ret = public_key_from_hashed_digit_array(params,
526 (unsigned char *) y_hashed_digits,
527 out);
528 if (ret) {
529 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100530 }
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (out_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100533 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100534 }
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100537}
538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539int mbedtls_lmots_verify(const mbedtls_lmots_public_t *ctx,
540 const unsigned char *msg, size_t msg_size,
541 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100542{
Raef Colese9479a02022-09-01 16:06:35 +0100543 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (msg == NULL && msg_size != 0) {
547 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100548 }
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (!ctx->have_public_key) {
551 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100552 }
553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8) {
555 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100556 }
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (sig_size < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
559 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles48294592022-10-10 16:40:00 +0100560 }
561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
563 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET) !=
564 MBEDTLS_LMOTS_SHA256_N32_W8) {
565 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100566 }
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 ret = mbedtls_lmots_calculate_public_key_candidate(&ctx->params,
569 msg, msg_size, sig, sig_size,
570 Kc_public_key_candidate,
571 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
572 NULL);
573 if (ret) {
574 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100575 }
576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (memcmp(&Kc_public_key_candidate, ctx->public_key,
578 sizeof(ctx->public_key))) {
579 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100580 }
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100583}
584
Raef Coles5127e852022-10-07 10:35:56 +0100585#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587void mbedtls_lmots_private_init(mbedtls_lmots_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100588{
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100590}
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592void mbedtls_lmots_private_free(mbedtls_lmots_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100593{
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 mbedtls_platform_zeroize(ctx,
595 sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100596}
597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598int mbedtls_lmots_generate_private_key(mbedtls_lmots_private_t *ctx,
599 mbedtls_lmots_algorithm_type_t type,
600 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
601 uint32_t q_leaf_identifier,
602 const unsigned char *seed,
603 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100604{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100605 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Coles01c71a12022-08-31 15:55:00 +0100607 size_t output_hash_len;
608 unsigned int i_digit_idx;
609 unsigned char i_digit_idx_bytes[2];
610 unsigned char const_bytes[1];
Raef Coles01c71a12022-08-31 15:55:00 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (ctx->have_private_key) {
613 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100614 }
615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (type != MBEDTLS_LMOTS_SHA256_N32_W8) {
617 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100618 }
619
Raef Colesf5632d32022-09-01 09:56:52 +0100620 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 memcpy(ctx->params.I_key_identifier,
623 I_key_identifier,
624 sizeof(ctx->params.I_key_identifier));
Raef Coles01c71a12022-08-31 15:55:00 +0100625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
627 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
628 ctx->params.q_leaf_identifier);
Raef Coles01c71a12022-08-31 15:55:00 +0100629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 mbedtls_lms_unsigned_int_to_network_bytes(0xFF, sizeof(const_bytes),
631 const_bytes);
Raef Coles01c71a12022-08-31 15:55:00 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 for (i_digit_idx = 0;
634 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
635 i_digit_idx++) {
636 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
637 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 }
Raef Coles01c71a12022-08-31 15:55:00 +0100640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 status = psa_hash_update(&op,
642 ctx->params.I_key_identifier,
643 sizeof(ctx->params.I_key_identifier));
644 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100645 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 }
Raef Coles01c71a12022-08-31 15:55:00 +0100647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 status = psa_hash_update(&op,
649 ctx->params.q_leaf_identifier,
650 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
651 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100652 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 }
Raef Coles01c71a12022-08-31 15:55:00 +0100654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 mbedtls_lms_unsigned_int_to_network_bytes(i_digit_idx, I_DIGIT_IDX_LEN,
656 i_digit_idx_bytes);
657 status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN);
658 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100659 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 }
Raef Coles01c71a12022-08-31 15:55:00 +0100661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 status = psa_hash_update(&op, const_bytes, sizeof(const_bytes));
663 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100664 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 }
Raef Coles01c71a12022-08-31 15:55:00 +0100666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 status = psa_hash_update(&op, seed, seed_size);
668 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100669 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 }
Raef Coles01c71a12022-08-31 15:55:00 +0100671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 status = psa_hash_finish(&op,
673 ctx->private_key[i_digit_idx],
674 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
675 &output_hash_len);
676 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100677 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 }
Raef Coles01c71a12022-08-31 15:55:00 +0100679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 psa_hash_abort(&op);
Raef Coles01c71a12022-08-31 15:55:00 +0100681 }
682
Raef Colesf5632d32022-09-01 09:56:52 +0100683 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100684
685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 psa_hash_abort(&op);
Raef Coles01c71a12022-08-31 15:55:00 +0100687
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500688 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles01c71a12022-08-31 15:55:00 +0100689}
690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691int mbedtls_lmots_calculate_public_key(mbedtls_lmots_public_t *ctx,
692 const mbedtls_lmots_private_t *priv_ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100693{
Raef Colese9479a02022-09-01 16:06:35 +0100694 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 +0100695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
696
Raef Coles8ff6df52021-07-21 12:42:15 +0100697 /* Check that a private key is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (!priv_ctx->have_private_key) {
699 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100700 }
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 ret = hash_digit_array(&priv_ctx->params,
703 (unsigned char *) priv_ctx->private_key, NULL,
704 NULL, (unsigned char *) y_hashed_digits);
705 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100706 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100707 }
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 ret = public_key_from_hashed_digit_array(&priv_ctx->params,
710 (unsigned char *) y_hashed_digits,
711 ctx->public_key);
712 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100713 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100714 }
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 memcpy(&ctx->params, &priv_ctx->params,
717 sizeof(ctx->params));
Raef Coles01c71a12022-08-31 15:55:00 +0100718
Raef Coles9b88ee52022-09-02 12:04:21 +0100719 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100720
Raef Coles142e5772022-10-12 10:47:27 +0100721exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 mbedtls_platform_zeroize(y_hashed_digits, sizeof(y_hashed_digits));
Raef Coles142e5772022-10-12 10:47:27 +0100723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100725}
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727int mbedtls_lmots_sign(mbedtls_lmots_private_t *ctx,
728 int (*f_rng)(void *, unsigned char *, size_t),
729 void *p_rng, const unsigned char *msg, size_t msg_size,
730 unsigned char *sig, size_t sig_size, size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100731{
Raef Colese9479a02022-09-01 16:06:35 +0100732 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100733 /* Create a temporary buffer to prepare the signature in. This allows us to
734 * finish creating a signature (ensuring the process doesn't fail), and then
735 * erase the private key **before** writing any data into the sig parameter
736 * buffer. If data were directly written into the sig buffer, it might leak
737 * a partial signature on failure, which effectively compromises the private
738 * key.
739 */
Raef Colese9479a02022-09-01 16:06:35 +0100740 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Colesd48f7e92022-10-10 13:10:07 +0100741 unsigned char tmp_c_random[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (msg == NULL && msg_size != 0) {
745 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100746 }
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type)) {
749 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100750 }
751
752 /* Check that a private key is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (!ctx->have_private_key) {
754 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100755 }
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 ret = f_rng(p_rng, tmp_c_random,
758 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
759 if (ret) {
760 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100761 }
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 ret = create_digit_array_with_checksum(&ctx->params,
764 msg, msg_size,
765 tmp_c_random,
766 tmp_digit_array);
767 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100768 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100769 }
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 ret = hash_digit_array(&ctx->params, (unsigned char *) ctx->private_key,
772 NULL, tmp_digit_array, (unsigned char *) tmp_sig);
773 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100774 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100775 }
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
778 MBEDTLS_LMOTS_TYPE_LEN,
779 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100780
Raef Coles9c9027b2022-09-02 18:26:31 +0100781 /* Test hook to check if sig is being written to before we invalidate the
782 * private key.
783 */
784#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (mbedtls_lmots_sign_private_key_invalidated_hook != NULL) {
786 ret = (*mbedtls_lmots_sign_private_key_invalidated_hook)(sig);
787 if (ret != 0) {
788 return ret;
789 }
Raef Coles9c9027b2022-09-02 18:26:31 +0100790 }
791#endif /* defined(MBEDTLS_TEST_HOOKS) */
792
Raef Coles8ff6df52021-07-21 12:42:15 +0100793 /* We've got a valid signature now, so it's time to make sure the private
794 * key can't be reused.
795 */
Raef Colesf5632d32022-09-01 09:56:52 +0100796 ctx->have_private_key = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 mbedtls_platform_zeroize(ctx->private_key,
798 sizeof(ctx->private_key));
Raef Coles8ff6df52021-07-21 12:42:15 +0100799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 memcpy(sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
801 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type));
Raef Coles891c6132022-09-01 11:05:48 +0100802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
804 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
805 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100808 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100809 }
810
Raef Coles142e5772022-10-12 10:47:27 +0100811 ret = 0;
812
813exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 mbedtls_platform_zeroize(tmp_digit_array, sizeof(tmp_digit_array));
815 mbedtls_platform_zeroize(tmp_sig, sizeof(tmp_sig));
Raef Coles142e5772022-10-12 10:47:27 +0100816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100818}
819
Raef Coles5127e852022-10-07 10:35:56 +0100820#endif /* defined(MBEDTLS_LMS_PRIVATE) */
821#endif /* defined(MBEDTLS_LMS_C) */