blob: c6b45ed59249c396024684124af040e2b3ac4029 [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"
44
Raef Colesc8f96042022-08-25 13:49:54 +010045#include "psa/crypto.h"
46
Raef Coles57d53282022-09-27 11:30:51 +010047#define PUBLIC_KEY_TYPE_OFFSET (0)
48#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
49 MBEDTLS_LMOTS_TYPE_LEN)
50#define PUBLIC_KEY_Q_LEAF_ID_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
51 MBEDTLS_LMOTS_I_KEY_ID_LEN)
52#define PUBLIC_KEY_KEY_HASH_OFFSET (PUBLIC_KEY_Q_LEAF_ID_OFFSET + \
53 MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
Raef Coles01c71a12022-08-31 15:55:00 +010054
55/* We only support parameter sets that use 8-bit digits, as it does not require
56 * translation logic between digits and bytes */
57#define W_WINTERNITZ_PARAMETER (8u)
58#define CHECKSUM_LEN (2)
59#define I_DIGIT_IDX_LEN (2)
60#define J_HASH_IDX_LEN (1)
61#define D_CONST_LEN (2)
62
Raef Coles9b88ee52022-09-02 12:04:21 +010063#define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
Raef Coles01c71a12022-08-31 15:55:00 +010064
Raef Coles9b88ee52022-09-02 12:04:21 +010065#define D_CONST_LEN (2)
Gilles Peskine449bd832023-01-11 14:50:10 +010066static const unsigned char D_PUBLIC_CONSTANT_BYTES[D_CONST_LEN] = { 0x80, 0x80 };
67static const unsigned char D_MESSAGE_CONSTANT_BYTES[D_CONST_LEN] = { 0x81, 0x81 };
Raef Coles8ff6df52021-07-21 12:42:15 +010068
Raef Coles9c9027b2022-09-02 18:26:31 +010069#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +010070int (*mbedtls_lmots_sign_private_key_invalidated_hook)(unsigned char *) = NULL;
Raef Coles9c9027b2022-09-02 18:26:31 +010071#endif /* defined(MBEDTLS_TEST_HOOKS) */
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073void mbedtls_lms_unsigned_int_to_network_bytes(unsigned int val, size_t len,
74 unsigned char *bytes)
Raef Coles8ff6df52021-07-21 12:42:15 +010075{
76 size_t idx;
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078 for (idx = 0; idx < len; idx++) {
79 bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF;
Raef Coles8ff6df52021-07-21 12:42:15 +010080 }
81}
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083unsigned int mbedtls_lms_network_bytes_to_unsigned_int(size_t len,
84 const unsigned char *bytes)
Raef Coles8ff6df52021-07-21 12:42:15 +010085{
86 size_t idx;
87 unsigned int val = 0;
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089 for (idx = 0; idx < len; idx++) {
90 val |= ((unsigned int) bytes[idx]) << (8 * (len - 1 - idx));
Raef Coles8ff6df52021-07-21 12:42:15 +010091 }
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return val;
Raef Coles8ff6df52021-07-21 12:42:15 +010094}
95
Raef Coles0a967cc2022-09-02 17:46:15 +010096/* Calculate the checksum digits that are appended to the end of the LMOTS digit
97 * string. See NIST SP800-208 section 3.1 or RFC8554 Algorithm 2 for details of
98 * the checksum algorithm.
99 *
Raef Coles98d6e222022-09-23 09:04:04 +0100100 * params The LMOTS parameter set, I and q values which
101 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100102 *
Raef Coles98d6e222022-09-23 09:04:04 +0100103 * digest The digit string to create the digest from. As
104 * this does not contain a checksum, it is the same
105 * size as a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static unsigned short lmots_checksum_calculate(const mbedtls_lmots_parameters_t *params,
108 const unsigned char *digest)
Raef Coles8ff6df52021-07-21 12:42:15 +0100109{
110 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100111 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 for (idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++) {
Raef Coles01c71a12022-08-31 15:55:00 +0100114 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +0100115 }
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return sum;
Raef Coles8ff6df52021-07-21 12:42:15 +0100118}
119
Raef Coles0a967cc2022-09-02 17:46:15 +0100120/* Create the string of digest digits (in the base determined by the Winternitz
121 * parameter with the checksum appended to the end (Q || cksm(Q)). See NIST
122 * SP800-208 section 3.1 or RFC8554 Algorithm 3 step 5 (also used in Algorithm
123 * 4b step 3) for details.
124 *
Raef Coles98d6e222022-09-23 09:04:04 +0100125 * params The LMOTS parameter set, I and q values which
126 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100127 *
Raef Coles98d6e222022-09-23 09:04:04 +0100128 * msg The message that will be hashed to create the
129 * digest.
Raef Coles0a967cc2022-09-02 17:46:15 +0100130 *
Raef Coles98d6e222022-09-23 09:04:04 +0100131 * msg_size The size of the message.
Raef Coles0a967cc2022-09-02 17:46:15 +0100132 *
Raef Coles98d6e222022-09-23 09:04:04 +0100133 * C_random_value The random value that will be combined with the
134 * message digest. This is always the same size as a
135 * hash output for whichever hash algorithm is
136 * determined by the parameter set.
Raef Coles0a967cc2022-09-02 17:46:15 +0100137 *
Raef Coles98d6e222022-09-23 09:04:04 +0100138 * output An output containing the digit string (+
139 * checksum) of length P digits (in the case of
140 * MBEDTLS_LMOTS_SHA256_N32_W8, this means it is of
141 * size P bytes).
Raef Coles0a967cc2022-09-02 17:46:15 +0100142 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143static int create_digit_array_with_checksum(const mbedtls_lmots_parameters_t *params,
144 const unsigned char *msg,
145 size_t msg_len,
146 const unsigned char *C_random_value,
147 unsigned char *out)
Raef Coles8ff6df52021-07-21 12:42:15 +0100148{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100149 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100151 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100152 unsigned short checksum;
Raef Coles8ff6df52021-07-21 12:42:15 +0100153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
155 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100156 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 status = psa_hash_update(&op, params->I_key_identifier,
160 MBEDTLS_LMOTS_I_KEY_ID_LEN);
161 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 status = psa_hash_update(&op, params->q_leaf_identifier,
166 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
167 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100168 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 status = psa_hash_update(&op, D_MESSAGE_CONSTANT_BYTES, D_CONST_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, C_random_value,
177 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type));
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, msg, msg_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_finish(&op, out,
188 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
189 &output_hash_len);
190 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100191 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 checksum = lmots_checksum_calculate(params, out);
195 mbedtls_lms_unsigned_int_to_network_bytes(checksum, CHECKSUM_LEN,
196 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100197
Raef Coles01c71a12022-08-31 15:55:00 +0100198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return mbedtls_lms_error_from_psa(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100202}
203
Raef Coles0a967cc2022-09-02 17:46:15 +0100204/* Hash each element of the string of digits (+ checksum), producing a hash
205 * output for each element. This is used in several places (by varying the
206 * hash_idx_min/max_values) in order to calculate a public key from a private
207 * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554
208 * Algorithm 3 step 5), and to calculate a public key candidate from a
209 * signature and message (RFC8554 Algorithm 4b step 3).
210 *
Raef Coles98d6e222022-09-23 09:04:04 +0100211 * params The LMOTS parameter set, I and q values which
212 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100213 *
Raef Coles98d6e222022-09-23 09:04:04 +0100214 * x_digit_array The array of digits (of size P, 34 in the case of
215 * MBEDTLS_LMOTS_SHA256_N32_W8).
Raef Coles0a967cc2022-09-02 17:46:15 +0100216 *
Raef Coles98d6e222022-09-23 09:04:04 +0100217 * hash_idx_min_values An array of the starting values of the j iterator
218 * for each of the members of the digit array. If
219 * this value in NULL, then all iterators will start
220 * at 0.
Raef Coles0a967cc2022-09-02 17:46:15 +0100221 *
Raef Coles98d6e222022-09-23 09:04:04 +0100222 * hash_idx_max_values An array of the upper bound values of the j
223 * iterator for each of the members of the digit
224 * array. If this value in NULL, then iterator is
225 * bounded to be less than 2^w - 1 (255 in the case
226 * of MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100227 *
Raef Coles98d6e222022-09-23 09:04:04 +0100228 * output An array containing a hash output for each member
229 * of the digit string P. In the case of
230 * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 *
231 * 34.
Raef Coles0a967cc2022-09-02 17:46:15 +0100232 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233static int hash_digit_array(const mbedtls_lmots_parameters_t *params,
234 const unsigned char *x_digit_array,
235 const unsigned char *hash_idx_min_values,
236 const unsigned char *hash_idx_max_values,
237 unsigned char *output)
Raef Coles8ff6df52021-07-21 12:42:15 +0100238{
Raef Coles8738a492022-09-02 17:13:01 +0100239 unsigned int i_digit_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100240 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8738a492022-09-02 17:13:01 +0100241 unsigned int j_hash_idx;
242 unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100243 unsigned int j_hash_idx_min;
244 unsigned int j_hash_idx_max;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100245 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100247 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100248 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 for (i_digit_idx = 0;
251 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
252 i_digit_idx++) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 memcpy(tmp_hash,
255 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
256 MBEDTLS_LMOTS_N_HASH_LEN(params->type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100257
Raef Coles366d67d2022-09-01 17:23:12 +0100258 j_hash_idx_min = hash_idx_min_values != NULL ?
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 hash_idx_min_values[i_digit_idx] : 0;
Raef Coles366d67d2022-09-01 17:23:12 +0100260 j_hash_idx_max = hash_idx_max_values != NULL ?
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 for (j_hash_idx = j_hash_idx_min;
264 j_hash_idx < j_hash_idx_max;
265 j_hash_idx++) {
266 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
267 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100268 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 status = psa_hash_update(&op,
272 params->I_key_identifier,
273 MBEDTLS_LMOTS_I_KEY_ID_LEN);
274 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100275 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 status = psa_hash_update(&op,
279 params->q_leaf_identifier,
280 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
281 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 mbedtls_lms_unsigned_int_to_network_bytes(i_digit_idx,
286 I_DIGIT_IDX_LEN,
287 i_digit_idx_bytes);
288 status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN);
289 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 mbedtls_lms_unsigned_int_to_network_bytes(j_hash_idx,
294 J_HASH_IDX_LEN,
295 j_hash_idx_bytes);
296 status = psa_hash_update(&op, j_hash_idx_bytes, J_HASH_IDX_LEN);
297 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100298 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 status = psa_hash_update(&op, tmp_hash,
302 MBEDTLS_LMOTS_N_HASH_LEN(params->type));
303 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100304 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 status = psa_hash_finish(&op, tmp_hash, sizeof(tmp_hash),
308 &output_hash_len);
309 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100310 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 psa_hash_abort(&op);
Raef Coles8ff6df52021-07-21 12:42:15 +0100314 }
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 memcpy(&output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
317 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100318 }
319
Raef Coles01c71a12022-08-31 15:55:00 +0100320exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 psa_hash_abort(&op);
322 mbedtls_platform_zeroize(tmp_hash, sizeof(tmp_hash));
Raef Coles01c71a12022-08-31 15:55:00 +0100323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return mbedtls_lms_error_from_psa(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100325}
326
Raef Coles0a967cc2022-09-02 17:46:15 +0100327/* Combine the hashes of the digit array into a public key. This is used in
328 * in order to calculate a public key from a private key (RFC8554 Algorithm 1
329 * step 4), and to calculate a public key candidate from a signature and message
330 * (RFC8554 Algorithm 4b step 3).
331 *
Raef Coles98d6e222022-09-23 09:04:04 +0100332 * params The LMOTS parameter set, I and q values which describe
333 * the key being used.
334 * y_hashed_digits The array of hashes, one hash for each digit of the
335 * symbol array (which is of size P, 34 in the case of
336 * MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100337 *
Raef Coles98d6e222022-09-23 09:04:04 +0100338 * pub_key The output public key (or candidate public key in
339 * case this is being run as part of signature
340 * verification), in the form of a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342static int public_key_from_hashed_digit_array(const mbedtls_lmots_parameters_t *params,
343 const unsigned char *y_hashed_digits,
344 unsigned char *pub_key)
Raef Coles8ff6df52021-07-21 12:42:15 +0100345{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100346 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100348 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
351 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100352 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 status = psa_hash_update(&op,
356 params->I_key_identifier,
357 MBEDTLS_LMOTS_I_KEY_ID_LEN);
358 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100359 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 status = psa_hash_update(&op, params->q_leaf_identifier,
363 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
364 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 status = psa_hash_update(&op, D_PUBLIC_CONSTANT_BYTES, D_CONST_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, y_hashed_digits,
374 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
375 MBEDTLS_LMOTS_N_HASH_LEN(params->type));
376 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100377 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 }
Raef Coles8ff6df52021-07-21 12:42:15 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 status = psa_hash_finish(&op, pub_key,
381 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
382 &output_hash_len);
383 if (status != PSA_SUCCESS) {
Raef Coles8ff6df52021-07-21 12:42:15 +0100384
Raef Coles01c71a12022-08-31 15:55:00 +0100385exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 psa_hash_abort(&op);
387 }
Raef Coles29117d22022-10-07 11:46:06 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return mbedtls_lms_error_from_psa(status);
Raef Coles8ff6df52021-07-21 12:42:15 +0100390}
391
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}
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410void mbedtls_lmots_public_init(mbedtls_lmots_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100411{
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 memset(ctx, 0, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100413}
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415void mbedtls_lmots_public_free(mbedtls_lmots_public_t *ctx)
Raef Coles8ff6df52021-07-21 12:42:15 +0100416{
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 mbedtls_platform_zeroize(ctx, sizeof(*ctx));
Raef Coles8ff6df52021-07-21 12:42:15 +0100418}
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420int mbedtls_lmots_import_public_key(mbedtls_lmots_public_t *ctx,
421 const unsigned char *key, size_t key_len)
Raef Coles8ff6df52021-07-21 12:42:15 +0100422{
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (key_len < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
424 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese89488d2022-10-07 16:06:35 +0100425 }
426
Raef Colesf5632d32022-09-01 09:56:52 +0100427 ctx->params.type =
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
429 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (key_len != MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
432 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese9479a02022-09-01 16:06:35 +0100433 }
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 memcpy(ctx->params.I_key_identifier,
436 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
437 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Raef Coles01c71a12022-08-31 15:55:00 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 memcpy(ctx->params.q_leaf_identifier,
440 key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
441 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
Raef Coles01c71a12022-08-31 15:55:00 +0100442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 memcpy(ctx->public_key,
444 key + PUBLIC_KEY_KEY_HASH_OFFSET,
445 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100446
Raef Colesf5632d32022-09-01 09:56:52 +0100447 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100450}
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452int mbedtls_lmots_export_public_key(const mbedtls_lmots_public_t *ctx,
453 unsigned char *key, size_t key_size,
454 size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100455{
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
457 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100458 }
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (!ctx->have_public_key) {
461 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100462 }
463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
465 MBEDTLS_LMOTS_TYPE_LEN,
466 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles370cc432022-10-07 16:07:33 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
469 ctx->params.I_key_identifier,
470 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Raef Coles370cc432022-10-07 16:07:33 +0100471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 memcpy(key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
473 ctx->params.q_leaf_identifier,
474 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
Raef Coles370cc432022-10-07 16:07:33 +0100475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 memcpy(key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
477 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100480 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100484}
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486int mbedtls_lmots_calculate_public_key_candidate(const mbedtls_lmots_parameters_t *params,
487 const unsigned char *msg,
488 size_t msg_size,
489 const unsigned char *sig,
490 size_t sig_size,
491 unsigned char *out,
492 size_t out_size,
493 size_t *out_len)
Raef Coles8ff6df52021-07-21 12:42:15 +0100494{
Raef Colese9479a02022-09-01 16:06:35 +0100495 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
496 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 +0100497 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (msg == NULL && msg_size != 0) {
500 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100501 }
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
504 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type)) {
505 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100506 }
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 ret = create_digit_array_with_checksum(params, msg, msg_size,
509 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
510 tmp_digit_array);
511 if (ret) {
512 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100513 }
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 ret = hash_digit_array(params,
516 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
517 tmp_digit_array, NULL, (unsigned char *) y_hashed_digits);
518 if (ret) {
519 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 ret = public_key_from_hashed_digit_array(params,
523 (unsigned char *) y_hashed_digits,
524 out);
525 if (ret) {
526 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100527 }
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (out_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100530 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100531 }
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100534}
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536int mbedtls_lmots_verify(const mbedtls_lmots_public_t *ctx,
537 const unsigned char *msg, size_t msg_size,
538 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100539{
Raef Colese9479a02022-09-01 16:06:35 +0100540 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (msg == NULL && msg_size != 0) {
544 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100545 }
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (!ctx->have_public_key) {
548 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100549 }
550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8) {
552 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100553 }
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (sig_size < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
556 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles48294592022-10-10 16:40:00 +0100557 }
558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
560 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET) !=
561 MBEDTLS_LMOTS_SHA256_N32_W8) {
562 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100563 }
564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 ret = mbedtls_lmots_calculate_public_key_candidate(&ctx->params,
566 msg, msg_size, sig, sig_size,
567 Kc_public_key_candidate,
568 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
569 NULL);
570 if (ret) {
571 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100572 }
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (memcmp(&Kc_public_key_candidate, ctx->public_key,
575 sizeof(ctx->public_key))) {
576 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100577 }
578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100580}
581
Raef Coles5127e852022-10-07 10:35:56 +0100582#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584void mbedtls_lmots_private_init(mbedtls_lmots_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100585{
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100587}
588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589void mbedtls_lmots_private_free(mbedtls_lmots_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100590{
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 mbedtls_platform_zeroize(ctx,
592 sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100593}
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595int mbedtls_lmots_generate_private_key(mbedtls_lmots_private_t *ctx,
596 mbedtls_lmots_algorithm_type_t type,
597 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
598 uint32_t q_leaf_identifier,
599 const unsigned char *seed,
600 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100601{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100602 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
603 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Coles01c71a12022-08-31 15:55:00 +0100604 size_t output_hash_len;
605 unsigned int i_digit_idx;
606 unsigned char i_digit_idx_bytes[2];
607 unsigned char const_bytes[1];
Raef Coles01c71a12022-08-31 15:55:00 +0100608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if (ctx->have_private_key) {
610 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100611 }
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (type != MBEDTLS_LMOTS_SHA256_N32_W8) {
614 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100615 }
616
Raef Colesf5632d32022-09-01 09:56:52 +0100617 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 memcpy(ctx->params.I_key_identifier,
620 I_key_identifier,
621 sizeof(ctx->params.I_key_identifier));
Raef Coles01c71a12022-08-31 15:55:00 +0100622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
624 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
625 ctx->params.q_leaf_identifier);
Raef Coles01c71a12022-08-31 15:55:00 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 mbedtls_lms_unsigned_int_to_network_bytes(0xFF, sizeof(const_bytes),
628 const_bytes);
Raef Coles01c71a12022-08-31 15:55:00 +0100629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 for (i_digit_idx = 0;
631 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
632 i_digit_idx++) {
633 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
634 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100635 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 }
Raef Coles01c71a12022-08-31 15:55:00 +0100637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 status = psa_hash_update(&op,
639 ctx->params.I_key_identifier,
640 sizeof(ctx->params.I_key_identifier));
641 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 }
Raef Coles01c71a12022-08-31 15:55:00 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 status = psa_hash_update(&op,
646 ctx->params.q_leaf_identifier,
647 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
648 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100649 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 }
Raef Coles01c71a12022-08-31 15:55:00 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 mbedtls_lms_unsigned_int_to_network_bytes(i_digit_idx, I_DIGIT_IDX_LEN,
653 i_digit_idx_bytes);
654 status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN);
655 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100656 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 }
Raef Coles01c71a12022-08-31 15:55:00 +0100658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 status = psa_hash_update(&op, const_bytes, sizeof(const_bytes));
660 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100661 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 }
Raef Coles01c71a12022-08-31 15:55:00 +0100663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 status = psa_hash_update(&op, seed, seed_size);
665 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 }
Raef Coles01c71a12022-08-31 15:55:00 +0100668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 status = psa_hash_finish(&op,
670 ctx->private_key[i_digit_idx],
671 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
672 &output_hash_len);
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 psa_hash_abort(&op);
Raef Coles01c71a12022-08-31 15:55:00 +0100678 }
679
Raef Colesf5632d32022-09-01 09:56:52 +0100680 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100681
682exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 psa_hash_abort(&op);
Raef Coles01c71a12022-08-31 15:55:00 +0100684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return mbedtls_lms_error_from_psa(status);
Raef Coles01c71a12022-08-31 15:55:00 +0100686}
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688int mbedtls_lmots_calculate_public_key(mbedtls_lmots_public_t *ctx,
689 const mbedtls_lmots_private_t *priv_ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100690{
Raef Colese9479a02022-09-01 16:06:35 +0100691 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 +0100692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
693
Raef Coles8ff6df52021-07-21 12:42:15 +0100694 /* Check that a private key is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (!priv_ctx->have_private_key) {
696 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100697 }
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 ret = hash_digit_array(&priv_ctx->params,
700 (unsigned char *) priv_ctx->private_key, NULL,
701 NULL, (unsigned char *) y_hashed_digits);
702 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100703 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100704 }
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 ret = public_key_from_hashed_digit_array(&priv_ctx->params,
707 (unsigned char *) y_hashed_digits,
708 ctx->public_key);
709 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100710 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100711 }
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 memcpy(&ctx->params, &priv_ctx->params,
714 sizeof(ctx->params));
Raef Coles01c71a12022-08-31 15:55:00 +0100715
Raef Coles9b88ee52022-09-02 12:04:21 +0100716 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100717
Raef Coles142e5772022-10-12 10:47:27 +0100718exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 mbedtls_platform_zeroize(y_hashed_digits, sizeof(y_hashed_digits));
Raef Coles142e5772022-10-12 10:47:27 +0100720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100722}
723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724int mbedtls_lmots_sign(mbedtls_lmots_private_t *ctx,
725 int (*f_rng)(void *, unsigned char *, size_t),
726 void *p_rng, const unsigned char *msg, size_t msg_size,
727 unsigned char *sig, size_t sig_size, size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100728{
Raef Colese9479a02022-09-01 16:06:35 +0100729 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100730 /* Create a temporary buffer to prepare the signature in. This allows us to
731 * finish creating a signature (ensuring the process doesn't fail), and then
732 * erase the private key **before** writing any data into the sig parameter
733 * buffer. If data were directly written into the sig buffer, it might leak
734 * a partial signature on failure, which effectively compromises the private
735 * key.
736 */
Raef Colese9479a02022-09-01 16:06:35 +0100737 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Colesd48f7e92022-10-10 13:10:07 +0100738 unsigned char tmp_c_random[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (msg == NULL && msg_size != 0) {
742 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100743 }
744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type)) {
746 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100747 }
748
749 /* Check that a private key is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (!ctx->have_private_key) {
751 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100752 }
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 ret = f_rng(p_rng, tmp_c_random,
755 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
756 if (ret) {
757 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100758 }
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 ret = create_digit_array_with_checksum(&ctx->params,
761 msg, msg_size,
762 tmp_c_random,
763 tmp_digit_array);
764 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100765 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100766 }
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 ret = hash_digit_array(&ctx->params, (unsigned char *) ctx->private_key,
769 NULL, tmp_digit_array, (unsigned char *) tmp_sig);
770 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100771 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100772 }
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
775 MBEDTLS_LMOTS_TYPE_LEN,
776 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100777
Raef Coles9c9027b2022-09-02 18:26:31 +0100778 /* Test hook to check if sig is being written to before we invalidate the
779 * private key.
780 */
781#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (mbedtls_lmots_sign_private_key_invalidated_hook != NULL) {
783 ret = (*mbedtls_lmots_sign_private_key_invalidated_hook)(sig);
784 if (ret != 0) {
785 return ret;
786 }
Raef Coles9c9027b2022-09-02 18:26:31 +0100787 }
788#endif /* defined(MBEDTLS_TEST_HOOKS) */
789
Raef Coles8ff6df52021-07-21 12:42:15 +0100790 /* We've got a valid signature now, so it's time to make sure the private
791 * key can't be reused.
792 */
Raef Colesf5632d32022-09-01 09:56:52 +0100793 ctx->have_private_key = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 mbedtls_platform_zeroize(ctx->private_key,
795 sizeof(ctx->private_key));
Raef Coles8ff6df52021-07-21 12:42:15 +0100796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 memcpy(sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
798 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type));
Raef Coles891c6132022-09-01 11:05:48 +0100799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
801 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
802 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100805 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100806 }
807
Raef Coles142e5772022-10-12 10:47:27 +0100808 ret = 0;
809
810exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 mbedtls_platform_zeroize(tmp_digit_array, sizeof(tmp_digit_array));
812 mbedtls_platform_zeroize(tmp_sig, sizeof(tmp_sig));
Raef Coles142e5772022-10-12 10:47:27 +0100813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100815}
816
Raef Coles5127e852022-10-07 10:35:56 +0100817#endif /* defined(MBEDTLS_LMS_PRIVATE) */
818#endif /* defined(MBEDTLS_LMS_C) */