blob: a3bfff89f8b2e32b991965ccf2ab2ea21bb62026 [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 Coles5127e852022-10-07 10:35:56 +010035#if defined(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"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050044#include "mbedtls/psa_util.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010045
Raef Colesc8f96042022-08-25 13:49:54 +010046#include "psa/crypto.h"
47
Andrzej Kurek00644842023-05-30 05:45:00 -040048/* Define a local translating function to save code size by not using too many
49 * arguments in each translating place. */
50static int local_err_translation(psa_status_t status)
51{
52 return psa_status_to_mbedtls(status, psa_to_lms_errors,
53 sizeof(psa_to_lms_errors),
54 psa_generic_status_to_mbedtls);
55}
56#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050057
Raef Coles57d53282022-09-27 11:30:51 +010058#define PUBLIC_KEY_TYPE_OFFSET (0)
59#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
60 MBEDTLS_LMOTS_TYPE_LEN)
61#define PUBLIC_KEY_Q_LEAF_ID_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
62 MBEDTLS_LMOTS_I_KEY_ID_LEN)
63#define PUBLIC_KEY_KEY_HASH_OFFSET (PUBLIC_KEY_Q_LEAF_ID_OFFSET + \
64 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010065
66/* We only support parameter sets that use 8-bit digits, as it does not require
67 * translation logic between digits and bytes */
68#define W_WINTERNITZ_PARAMETER (8u)
69#define CHECKSUM_LEN (2)
70#define I_DIGIT_IDX_LEN (2)
71#define J_HASH_IDX_LEN (1)
72#define D_CONST_LEN (2)
73
Raef Coles9b88ee52022-09-02 12:04:21 +010074#define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
Raef Coles01c71a12022-08-31 15:55:00 +010075
Raef Coles9b88ee52022-09-02 12:04:21 +010076#define D_CONST_LEN (2)
Gilles Peskine449bd832023-01-11 14:50:10 +010077static const unsigned char D_PUBLIC_CONSTANT_BYTES[D_CONST_LEN] = { 0x80, 0x80 };
78static const unsigned char D_MESSAGE_CONSTANT_BYTES[D_CONST_LEN] = { 0x81, 0x81 };
Raef Coles8ff6df52021-07-21 12:42:15 +010079
Raef Coles9c9027b2022-09-02 18:26:31 +010080#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +010081int (*mbedtls_lmots_sign_private_key_invalidated_hook)(unsigned char *) = NULL;
Raef Coles9c9027b2022-09-02 18:26:31 +010082#endif /* defined(MBEDTLS_TEST_HOOKS) */
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084void mbedtls_lms_unsigned_int_to_network_bytes(unsigned int val, size_t len,
85 unsigned char *bytes)
Raef Coles8ff6df52021-07-21 12:42:15 +010086{
87 size_t idx;
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089 for (idx = 0; idx < len; idx++) {
90 bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF;
Raef Coles8ff6df52021-07-21 12:42:15 +010091 }
92}
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094unsigned int mbedtls_lms_network_bytes_to_unsigned_int(size_t len,
95 const unsigned char *bytes)
Raef Coles8ff6df52021-07-21 12:42:15 +010096{
97 size_t idx;
98 unsigned int val = 0;
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 for (idx = 0; idx < len; idx++) {
101 val |= ((unsigned int) bytes[idx]) << (8 * (len - 1 - idx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100102 }
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return val;
Raef Coles8ff6df52021-07-21 12:42:15 +0100105}
106
Raef Coles0a967cc2022-09-02 17:46:15 +0100107/* Calculate the checksum digits that are appended to the end of the LMOTS digit
108 * string. See NIST SP800-208 section 3.1 or RFC8554 Algorithm 2 for details of
109 * the checksum algorithm.
110 *
Raef Coles98d6e222022-09-23 09:04:04 +0100111 * params The LMOTS parameter set, I and q values which
112 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100113 *
Raef Coles98d6e222022-09-23 09:04:04 +0100114 * digest The digit string to create the digest from. As
115 * this does not contain a checksum, it is the same
116 * size as a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118static unsigned short lmots_checksum_calculate(const mbedtls_lmots_parameters_t *params,
119 const unsigned char *digest)
Raef Coles8ff6df52021-07-21 12:42:15 +0100120{
121 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100122 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 for (idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++) {
Raef Coles01c71a12022-08-31 15:55:00 +0100125 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +0100126 }
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 return sum;
Raef Coles8ff6df52021-07-21 12:42:15 +0100129}
130
Raef Coles0a967cc2022-09-02 17:46:15 +0100131/* Create the string of digest digits (in the base determined by the Winternitz
132 * parameter with the checksum appended to the end (Q || cksm(Q)). See NIST
133 * SP800-208 section 3.1 or RFC8554 Algorithm 3 step 5 (also used in Algorithm
134 * 4b step 3) for details.
135 *
Raef Coles98d6e222022-09-23 09:04:04 +0100136 * params The LMOTS parameter set, I and q values which
137 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100138 *
Raef Coles98d6e222022-09-23 09:04:04 +0100139 * msg The message that will be hashed to create the
140 * digest.
Raef Coles0a967cc2022-09-02 17:46:15 +0100141 *
Raef Coles98d6e222022-09-23 09:04:04 +0100142 * msg_size The size of the message.
Raef Coles0a967cc2022-09-02 17:46:15 +0100143 *
Raef Coles98d6e222022-09-23 09:04:04 +0100144 * C_random_value The random value that will be combined with the
145 * message digest. This is always the same size as a
146 * hash output for whichever hash algorithm is
147 * determined by the parameter set.
Raef Coles0a967cc2022-09-02 17:46:15 +0100148 *
Raef Coles98d6e222022-09-23 09:04:04 +0100149 * output An output containing the digit string (+
150 * checksum) of length P digits (in the case of
151 * MBEDTLS_LMOTS_SHA256_N32_W8, this means it is of
152 * size P bytes).
Raef Coles0a967cc2022-09-02 17:46:15 +0100153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154static int create_digit_array_with_checksum(const mbedtls_lmots_parameters_t *params,
155 const unsigned char *msg,
156 size_t msg_len,
157 const unsigned char *C_random_value,
158 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100159{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100160 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
161 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100162 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100163 unsigned short checksum;
Raef Coles8ff6df52021-07-21 12:42:15 +0100164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
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, params->I_key_identifier,
171 MBEDTLS_LMOTS_I_KEY_ID_LEN);
172 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 status = psa_hash_update(&op, params->q_leaf_identifier,
177 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
178 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100179 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 status = psa_hash_update(&op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN);
183 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 status = psa_hash_update(&op, C_random_value,
188 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type));
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 status = psa_hash_update(&op, msg, msg_len);
194 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100195 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 status = psa_hash_finish(&op, out,
199 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
200 &output_hash_len);
201 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100202 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 checksum = lmots_checksum_calculate(params, out);
206 mbedtls_lms_unsigned_int_to_network_bytes(checksum, CHECKSUM_LEN,
207 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100208
Raef Coles01c71a12022-08-31 15:55:00 +0100209exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100211
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500212 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100213}
214
Raef Coles0a967cc2022-09-02 17:46:15 +0100215/* Hash each element of the string of digits (+ checksum), producing a hash
216 * output for each element. This is used in several places (by varying the
217 * hash_idx_min/max_values) in order to calculate a public key from a private
218 * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554
219 * Algorithm 3 step 5), and to calculate a public key candidate from a
220 * signature and message (RFC8554 Algorithm 4b step 3).
221 *
Raef Coles98d6e222022-09-23 09:04:04 +0100222 * params The LMOTS parameter set, I and q values which
223 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100224 *
Raef Coles98d6e222022-09-23 09:04:04 +0100225 * x_digit_array The array of digits (of size P, 34 in the case of
226 * MBEDTLS_LMOTS_SHA256_N32_W8).
Raef Coles0a967cc2022-09-02 17:46:15 +0100227 *
Raef Coles98d6e222022-09-23 09:04:04 +0100228 * hash_idx_min_values An array of the starting values of the j iterator
229 * for each of the members of the digit array. If
230 * this value in NULL, then all iterators will start
231 * at 0.
Raef Coles0a967cc2022-09-02 17:46:15 +0100232 *
Raef Coles98d6e222022-09-23 09:04:04 +0100233 * hash_idx_max_values An array of the upper bound values of the j
234 * iterator for each of the members of the digit
235 * array. If this value in NULL, then iterator is
236 * bounded to be less than 2^w - 1 (255 in the case
237 * of MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100238 *
Raef Coles98d6e222022-09-23 09:04:04 +0100239 * output An array containing a hash output for each member
240 * of the digit string P. In the case of
241 * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 *
242 * 34.
Raef Coles0a967cc2022-09-02 17:46:15 +0100243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244static int hash_digit_array(const mbedtls_lmots_parameters_t *params,
245 const unsigned char *x_digit_array,
246 const unsigned char *hash_idx_min_values,
247 const unsigned char *hash_idx_max_values,
248 unsigned char *output)
Raef Coles8ff6df52021-07-21 12:42:15 +0100249{
Raef Coles8738a492022-09-02 17:13:01 +0100250 unsigned int i_digit_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100251 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8738a492022-09-02 17:13:01 +0100252 unsigned int j_hash_idx;
253 unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100254 unsigned int j_hash_idx_min;
255 unsigned int j_hash_idx_max;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100256 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
257 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100258 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100259 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100260
Gilles Peskine449bd832023-01-11 14:50:10 +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
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 memcpy(tmp_hash,
266 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
267 MBEDTLS_LMOTS_N_HASH_LEN(params->type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100268
Raef Coles366d67d2022-09-01 17:23:12 +0100269 j_hash_idx_min = hash_idx_min_values != NULL ?
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 hash_idx_min_values[i_digit_idx] : 0;
Raef Coles366d67d2022-09-01 17:23:12 +0100271 j_hash_idx_max = hash_idx_max_values != NULL ?
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 for (j_hash_idx = j_hash_idx_min;
275 j_hash_idx < j_hash_idx_max;
276 j_hash_idx++) {
277 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
278 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 status = psa_hash_update(&op,
283 params->I_key_identifier,
284 MBEDTLS_LMOTS_I_KEY_ID_LEN);
285 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100286 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 status = psa_hash_update(&op,
290 params->q_leaf_identifier,
291 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
292 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100293 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 mbedtls_lms_unsigned_int_to_network_bytes(i_digit_idx,
297 I_DIGIT_IDX_LEN,
298 i_digit_idx_bytes);
299 status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN);
300 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100301 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 mbedtls_lms_unsigned_int_to_network_bytes(j_hash_idx,
305 J_HASH_IDX_LEN,
306 j_hash_idx_bytes);
307 status = psa_hash_update(&op, j_hash_idx_bytes, J_HASH_IDX_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 status = psa_hash_update(&op, tmp_hash,
313 MBEDTLS_LMOTS_N_HASH_LEN(params->type));
314 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100315 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 status = psa_hash_finish(&op, tmp_hash, sizeof(tmp_hash),
319 &output_hash_len);
320 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100321 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100325 }
326
Gilles Peskine449bd832023-01-11 14:50:10 +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:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 psa_hash_abort(&op);
333 mbedtls_platform_zeroize(tmp_hash, sizeof(tmp_hash));
Raef Coles01c71a12022-08-31 15:55:00 +0100334
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500335 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100336}
337
Raef Coles0a967cc2022-09-02 17:46:15 +0100338/* Combine the hashes of the digit array into a public key. This is used in
339 * in order to calculate a public key from a private key (RFC8554 Algorithm 1
340 * step 4), and to calculate a public key candidate from a signature and message
341 * (RFC8554 Algorithm 4b step 3).
342 *
Raef Coles98d6e222022-09-23 09:04:04 +0100343 * params The LMOTS parameter set, I and q values which describe
344 * the key being used.
345 * y_hashed_digits The array of hashes, one hash for each digit of the
346 * symbol array (which is of size P, 34 in the case of
347 * MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100348 *
Raef Coles98d6e222022-09-23 09:04:04 +0100349 * pub_key The output public key (or candidate public key in
350 * case this is being run as part of signature
351 * verification), in the form of a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100352 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353static int public_key_from_hashed_digit_array(const mbedtls_lmots_parameters_t *params,
354 const unsigned char *y_hashed_digits,
355 unsigned char *pub_key)
Raef Coles8ff6df52021-07-21 12:42:15 +0100356{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100357 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100359 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
362 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100363 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 status = psa_hash_update(&op,
367 params->I_key_identifier,
368 MBEDTLS_LMOTS_I_KEY_ID_LEN);
369 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100370 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 status = psa_hash_update(&op, params->q_leaf_identifier,
374 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
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_update(&op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN);
380 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100381 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 status = psa_hash_update(&op, y_hashed_digits,
385 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
386 MBEDTLS_LMOTS_N_HASH_LEN(params->type));
387 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100388 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 status = psa_hash_finish(&op, pub_key,
392 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
393 &output_hash_len);
394 if (status != PSA_SUCCESS) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100395
Raef Coles01c71a12022-08-31 15:55:00 +0100396exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 psa_hash_abort(&op);
398 }
Raef Coles29117d22022-10-07 11:46:06 +0100399
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500400 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100401}
402
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500403#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100404int mbedtls_lms_error_from_psa(psa_status_t status)
Raef Colesc8f96042022-08-25 13:49:54 +0100405{
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 switch (status) {
Raef Colesc8f96042022-08-25 13:49:54 +0100407 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return 0;
Raef Colesc8f96042022-08-25 13:49:54 +0100409 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Raef Colesc8f96042022-08-25 13:49:54 +0100411 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100413 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Colesc8f96042022-08-25 13:49:54 +0100415 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colesc8f96042022-08-25 13:49:54 +0100417 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Raef Colesc8f96042022-08-25 13:49:54 +0100419 }
420}
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500421#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Raef Colesc8f96042022-08-25 13:49:54 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423void mbedtls_lmots_public_init(mbedtls_lmots_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100424{
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 memset(ctx, 0, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100426}
427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428void mbedtls_lmots_public_free(mbedtls_lmots_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100429{
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100431}
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433int mbedtls_lmots_import_public_key(mbedtls_lmots_public_t *ctx,
434 const unsigned char *key, size_t key_len)
Raef Coles8ff6df52021-07-21 12:42:15 +0100435{
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (key_len < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
437 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese89488d2022-10-07 16:06:35 +0100438 }
439
Raef Colesf5632d32022-09-01 09:56:52 +0100440 ctx->params.type =
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
442 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (key_len != MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
445 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese9479a02022-09-01 16:06:35 +0100446 }
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 memcpy(ctx->params.I_key_identifier,
449 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
450 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Raef Coles01c71a12022-08-31 15:55:00 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 memcpy(ctx->params.q_leaf_identifier,
453 key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
454 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
Raef Coles01c71a12022-08-31 15:55:00 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 memcpy(ctx->public_key,
457 key + PUBLIC_KEY_KEY_HASH_OFFSET,
458 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100459
Raef Colesf5632d32022-09-01 09:56:52 +0100460 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100463}
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465int mbedtls_lmots_export_public_key(const mbedtls_lmots_public_t *ctx,
466 unsigned char *key, size_t key_size,
467 size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100468{
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
470 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100471 }
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (!ctx->have_public_key) {
474 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100475 }
476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
478 MBEDTLS_LMOTS_TYPE_LEN,
479 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles370cc432022-10-07 16:07:33 +0100480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
482 ctx->params.I_key_identifier,
483 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Raef Coles370cc432022-10-07 16:07:33 +0100484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 memcpy(key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
486 ctx->params.q_leaf_identifier,
487 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
Raef Coles370cc432022-10-07 16:07:33 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 memcpy(key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
490 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100493 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
494 }
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100497}
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499int mbedtls_lmots_calculate_public_key_candidate(const mbedtls_lmots_parameters_t *params,
500 const unsigned char *msg,
501 size_t msg_size,
502 const unsigned char *sig,
503 size_t sig_size,
504 unsigned char *out,
505 size_t out_size,
506 size_t *out_len)
Raef Coles8ff6df52021-07-21 12:42:15 +0100507{
Raef Colese9479a02022-09-01 16:06:35 +0100508 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
509 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 +0100510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (msg == NULL && msg_size != 0) {
513 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100514 }
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
517 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type)) {
518 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100519 }
520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 ret = create_digit_array_with_checksum(params, msg, msg_size,
522 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
523 tmp_digit_array);
524 if (ret) {
525 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 ret = hash_digit_array(params,
529 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
530 tmp_digit_array, NULL, (unsigned char *) y_hashed_digits);
531 if (ret) {
532 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100533 }
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 ret = public_key_from_hashed_digit_array(params,
536 (unsigned char *) y_hashed_digits,
537 out);
538 if (ret) {
539 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100540 }
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (out_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100543 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100544 }
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100547}
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549int mbedtls_lmots_verify(const mbedtls_lmots_public_t *ctx,
550 const unsigned char *msg, size_t msg_size,
551 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100552{
Raef Colese9479a02022-09-01 16:06:35 +0100553 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (msg == NULL && msg_size != 0) {
557 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100558 }
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (!ctx->have_public_key) {
561 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100562 }
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8) {
565 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100566 }
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (sig_size < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
569 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles48294592022-10-10 16:40:00 +0100570 }
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
573 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET) !=
574 MBEDTLS_LMOTS_SHA256_N32_W8) {
575 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100576 }
577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 ret = mbedtls_lmots_calculate_public_key_candidate(&ctx->params,
579 msg, msg_size, sig, sig_size,
580 Kc_public_key_candidate,
581 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
582 NULL);
583 if (ret) {
584 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100585 }
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (memcmp(&Kc_public_key_candidate, ctx->public_key,
588 sizeof(ctx->public_key))) {
589 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100590 }
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100593}
594
Raef Coles5127e852022-10-07 10:35:56 +0100595#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597void mbedtls_lmots_private_init(mbedtls_lmots_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100598{
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100600}
601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602void mbedtls_lmots_private_free(mbedtls_lmots_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100603{
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 mbedtls_platform_zeroize(ctx,
605 sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100606}
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608int mbedtls_lmots_generate_private_key(mbedtls_lmots_private_t *ctx,
609 mbedtls_lmots_algorithm_type_t type,
610 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
611 uint32_t q_leaf_identifier,
612 const unsigned char *seed,
613 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100614{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100615 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
616 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Coles01c71a12022-08-31 15:55:00 +0100617 size_t output_hash_len;
618 unsigned int i_digit_idx;
619 unsigned char i_digit_idx_bytes[2];
620 unsigned char const_bytes[1];
Raef Coles01c71a12022-08-31 15:55:00 +0100621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (ctx->have_private_key) {
623 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100624 }
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (type != MBEDTLS_LMOTS_SHA256_N32_W8) {
627 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100628 }
629
Raef Colesf5632d32022-09-01 09:56:52 +0100630 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 memcpy(ctx->params.I_key_identifier,
633 I_key_identifier,
634 sizeof(ctx->params.I_key_identifier));
Raef Coles01c71a12022-08-31 15:55:00 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
637 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
638 ctx->params.q_leaf_identifier);
Raef Coles01c71a12022-08-31 15:55:00 +0100639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 mbedtls_lms_unsigned_int_to_network_bytes(0xFF, sizeof(const_bytes),
641 const_bytes);
Raef Coles01c71a12022-08-31 15:55:00 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 for (i_digit_idx = 0;
644 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
645 i_digit_idx++) {
646 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
647 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100648 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 }
Raef Coles01c71a12022-08-31 15:55:00 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 status = psa_hash_update(&op,
652 ctx->params.I_key_identifier,
653 sizeof(ctx->params.I_key_identifier));
654 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100655 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 }
Raef Coles01c71a12022-08-31 15:55:00 +0100657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 status = psa_hash_update(&op,
659 ctx->params.q_leaf_identifier,
660 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
661 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100662 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 }
Raef Coles01c71a12022-08-31 15:55:00 +0100664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 mbedtls_lms_unsigned_int_to_network_bytes(i_digit_idx, I_DIGIT_IDX_LEN,
666 i_digit_idx_bytes);
667 status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN);
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_update(&op, const_bytes, sizeof(const_bytes));
673 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100674 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 }
Raef Coles01c71a12022-08-31 15:55:00 +0100676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 status = psa_hash_update(&op, seed, seed_size);
678 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100679 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 }
Raef Coles01c71a12022-08-31 15:55:00 +0100681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 status = psa_hash_finish(&op,
683 ctx->private_key[i_digit_idx],
684 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
685 &output_hash_len);
686 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100687 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 }
Raef Coles01c71a12022-08-31 15:55:00 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 psa_hash_abort(&op);
Raef Coles01c71a12022-08-31 15:55:00 +0100691 }
692
Raef Colesf5632d32022-09-01 09:56:52 +0100693 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100694
695exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 psa_hash_abort(&op);
Raef Coles01c71a12022-08-31 15:55:00 +0100697
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500698 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles01c71a12022-08-31 15:55:00 +0100699}
700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701int mbedtls_lmots_calculate_public_key(mbedtls_lmots_public_t *ctx,
702 const mbedtls_lmots_private_t *priv_ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100703{
Raef Colese9479a02022-09-01 16:06:35 +0100704 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 +0100705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
706
Raef Coles8ff6df52021-07-21 12:42:15 +0100707 /* Check that a private key is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (!priv_ctx->have_private_key) {
709 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100710 }
711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 ret = hash_digit_array(&priv_ctx->params,
713 (unsigned char *) priv_ctx->private_key, NULL,
714 NULL, (unsigned char *) y_hashed_digits);
715 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100716 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100717 }
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 ret = public_key_from_hashed_digit_array(&priv_ctx->params,
720 (unsigned char *) y_hashed_digits,
721 ctx->public_key);
722 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100723 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100724 }
725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 memcpy(&ctx->params, &priv_ctx->params,
727 sizeof(ctx->params));
Raef Coles01c71a12022-08-31 15:55:00 +0100728
Raef Coles9b88ee52022-09-02 12:04:21 +0100729 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100730
Raef Coles142e5772022-10-12 10:47:27 +0100731exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 mbedtls_platform_zeroize(y_hashed_digits, sizeof(y_hashed_digits));
Raef Coles142e5772022-10-12 10:47:27 +0100733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100735}
736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737int mbedtls_lmots_sign(mbedtls_lmots_private_t *ctx,
738 int (*f_rng)(void *, unsigned char *, size_t),
739 void *p_rng, const unsigned char *msg, size_t msg_size,
740 unsigned char *sig, size_t sig_size, size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100741{
Raef Colese9479a02022-09-01 16:06:35 +0100742 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100743 /* Create a temporary buffer to prepare the signature in. This allows us to
744 * finish creating a signature (ensuring the process doesn't fail), and then
745 * erase the private key **before** writing any data into the sig parameter
746 * buffer. If data were directly written into the sig buffer, it might leak
747 * a partial signature on failure, which effectively compromises the private
748 * key.
749 */
Raef Colese9479a02022-09-01 16:06:35 +0100750 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Colesd48f7e92022-10-10 13:10:07 +0100751 unsigned char tmp_c_random[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (msg == NULL && msg_size != 0) {
755 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100756 }
757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type)) {
759 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100760 }
761
762 /* Check that a private key is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (!ctx->have_private_key) {
764 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100765 }
766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 ret = f_rng(p_rng, tmp_c_random,
768 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
769 if (ret) {
770 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100771 }
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 ret = create_digit_array_with_checksum(&ctx->params,
774 msg, msg_size,
775 tmp_c_random,
776 tmp_digit_array);
777 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100778 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100779 }
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 ret = hash_digit_array(&ctx->params, (unsigned char *) ctx->private_key,
782 NULL, tmp_digit_array, (unsigned char *) tmp_sig);
783 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100784 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100785 }
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
788 MBEDTLS_LMOTS_TYPE_LEN,
789 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100790
Raef Coles9c9027b2022-09-02 18:26:31 +0100791 /* Test hook to check if sig is being written to before we invalidate the
792 * private key.
793 */
794#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (mbedtls_lmots_sign_private_key_invalidated_hook != NULL) {
796 ret = (*mbedtls_lmots_sign_private_key_invalidated_hook)(sig);
797 if (ret != 0) {
798 return ret;
799 }
Raef Coles9c9027b2022-09-02 18:26:31 +0100800 }
801#endif /* defined(MBEDTLS_TEST_HOOKS) */
802
Raef Coles8ff6df52021-07-21 12:42:15 +0100803 /* We've got a valid signature now, so it's time to make sure the private
804 * key can't be reused.
805 */
Raef Colesf5632d32022-09-01 09:56:52 +0100806 ctx->have_private_key = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 mbedtls_platform_zeroize(ctx->private_key,
808 sizeof(ctx->private_key));
Raef Coles8ff6df52021-07-21 12:42:15 +0100809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 memcpy(sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
811 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type));
Raef Coles891c6132022-09-01 11:05:48 +0100812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
814 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
815 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100818 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100819 }
820
Raef Coles142e5772022-10-12 10:47:27 +0100821 ret = 0;
822
823exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 mbedtls_platform_zeroize(tmp_digit_array, sizeof(tmp_digit_array));
825 mbedtls_platform_zeroize(tmp_sig, sizeof(tmp_sig));
Raef Coles142e5772022-10-12 10:47:27 +0100826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100828}
829
Raef Coles5127e852022-10-07 10:35:56 +0100830#endif /* defined(MBEDTLS_LMS_PRIVATE) */
831#endif /* defined(MBEDTLS_LMS_C) */