blob: e30511b0c196e11f7d91c0a60cff235ab65daa65 [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,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040053 ARRAY_LENGTH(psa_to_lms_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040054 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 =
Agathiyan Bragadeesh10b67752023-07-12 11:19:17 +0100441 (mbedtls_lmots_algorithm_type_t) mbedtls_lms_network_bytes_to_unsigned_int(
442 MBEDTLS_LMOTS_TYPE_LEN,
443 key +
444 MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles01c71a12022-08-31 15:55:00 +0100445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (key_len != MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
447 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Colese9479a02022-09-01 16:06:35 +0100448 }
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 memcpy(ctx->params.I_key_identifier,
451 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
452 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Raef Coles01c71a12022-08-31 15:55:00 +0100453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 memcpy(ctx->params.q_leaf_identifier,
455 key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
456 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
Raef Coles01c71a12022-08-31 15:55:00 +0100457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 memcpy(ctx->public_key,
459 key + PUBLIC_KEY_KEY_HASH_OFFSET,
460 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles01c71a12022-08-31 15:55:00 +0100461
Raef Colesf5632d32022-09-01 09:56:52 +0100462 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100465}
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467int mbedtls_lmots_export_public_key(const mbedtls_lmots_public_t *ctx,
468 unsigned char *key, size_t key_size,
469 size_t *key_len)
Raef Coles370cc432022-10-07 16:07:33 +0100470{
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) {
472 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles370cc432022-10-07 16:07:33 +0100473 }
474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (!ctx->have_public_key) {
476 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles370cc432022-10-07 16:07:33 +0100477 }
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
480 MBEDTLS_LMOTS_TYPE_LEN,
481 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles370cc432022-10-07 16:07:33 +0100482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
484 ctx->params.I_key_identifier,
485 MBEDTLS_LMOTS_I_KEY_ID_LEN);
Raef Coles370cc432022-10-07 16:07:33 +0100486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 memcpy(key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
488 ctx->params.q_leaf_identifier,
489 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
Raef Coles370cc432022-10-07 16:07:33 +0100490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 memcpy(key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
492 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles370cc432022-10-07 16:07:33 +0100493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (key_len != NULL) {
Raef Coles370cc432022-10-07 16:07:33 +0100495 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
496 }
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return 0;
Raef Coles370cc432022-10-07 16:07:33 +0100499}
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501int mbedtls_lmots_calculate_public_key_candidate(const mbedtls_lmots_parameters_t *params,
502 const unsigned char *msg,
503 size_t msg_size,
504 const unsigned char *sig,
505 size_t sig_size,
506 unsigned char *out,
507 size_t out_size,
508 size_t *out_len)
Raef Coles8ff6df52021-07-21 12:42:15 +0100509{
Raef Colese9479a02022-09-01 16:06:35 +0100510 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
511 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 +0100512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (msg == NULL && msg_size != 0) {
515 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100516 }
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if (sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
519 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type)) {
520 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100521 }
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 ret = create_digit_array_with_checksum(params, msg, msg_size,
524 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
525 tmp_digit_array);
526 if (ret) {
527 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100528 }
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 ret = hash_digit_array(params,
531 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
532 tmp_digit_array, NULL, (unsigned char *) y_hashed_digits);
533 if (ret) {
534 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100535 }
536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 ret = public_key_from_hashed_digit_array(params,
538 (unsigned char *) y_hashed_digits,
539 out);
540 if (ret) {
541 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100542 }
543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if (out_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100545 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100546 }
547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100549}
550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551int mbedtls_lmots_verify(const mbedtls_lmots_public_t *ctx,
552 const unsigned char *msg, size_t msg_size,
553 const unsigned char *sig, size_t sig_size)
Raef Coles8ff6df52021-07-21 12:42:15 +0100554{
Raef Colese9479a02022-09-01 16:06:35 +0100555 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (msg == NULL && msg_size != 0) {
559 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100560 }
561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (!ctx->have_public_key) {
563 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100564 }
565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8) {
567 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100568 }
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (sig_size < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
571 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles48294592022-10-10 16:40:00 +0100572 }
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
575 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET) !=
576 MBEDTLS_LMOTS_SHA256_N32_W8) {
577 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100578 }
579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 ret = mbedtls_lmots_calculate_public_key_candidate(&ctx->params,
581 msg, msg_size, sig, sig_size,
582 Kc_public_key_candidate,
583 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
584 NULL);
585 if (ret) {
586 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100587 }
588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (memcmp(&Kc_public_key_candidate, ctx->public_key,
590 sizeof(ctx->public_key))) {
591 return MBEDTLS_ERR_LMS_VERIFY_FAILED;
Raef Coles01c71a12022-08-31 15:55:00 +0100592 }
593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return 0;
Raef Coles01c71a12022-08-31 15:55:00 +0100595}
596
Raef Coles5127e852022-10-07 10:35:56 +0100597#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599void mbedtls_lmots_private_init(mbedtls_lmots_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100600{
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 memset(ctx, 0, sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100602}
603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604void mbedtls_lmots_private_free(mbedtls_lmots_private_t *ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100605{
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 mbedtls_platform_zeroize(ctx,
607 sizeof(*ctx));
Raef Coles01c71a12022-08-31 15:55:00 +0100608}
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610int mbedtls_lmots_generate_private_key(mbedtls_lmots_private_t *ctx,
611 mbedtls_lmots_algorithm_type_t type,
612 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
613 uint32_t q_leaf_identifier,
614 const unsigned char *seed,
615 size_t seed_size)
Raef Coles01c71a12022-08-31 15:55:00 +0100616{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100617 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
618 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Coles01c71a12022-08-31 15:55:00 +0100619 size_t output_hash_len;
620 unsigned int i_digit_idx;
621 unsigned char i_digit_idx_bytes[2];
622 unsigned char const_bytes[1];
Raef Coles01c71a12022-08-31 15:55:00 +0100623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ctx->have_private_key) {
625 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100626 }
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (type != MBEDTLS_LMOTS_SHA256_N32_W8) {
629 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100630 }
631
Raef Colesf5632d32022-09-01 09:56:52 +0100632 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 memcpy(ctx->params.I_key_identifier,
635 I_key_identifier,
636 sizeof(ctx->params.I_key_identifier));
Raef Coles01c71a12022-08-31 15:55:00 +0100637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
639 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
640 ctx->params.q_leaf_identifier);
Raef Coles01c71a12022-08-31 15:55:00 +0100641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 mbedtls_lms_unsigned_int_to_network_bytes(0xFF, sizeof(const_bytes),
643 const_bytes);
Raef Coles01c71a12022-08-31 15:55:00 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 for (i_digit_idx = 0;
646 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
647 i_digit_idx++) {
648 status = psa_hash_setup(&op, PSA_ALG_SHA_256);
649 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100650 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 }
Raef Coles01c71a12022-08-31 15:55:00 +0100652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 status = psa_hash_update(&op,
654 ctx->params.I_key_identifier,
655 sizeof(ctx->params.I_key_identifier));
656 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100657 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 }
Raef Coles01c71a12022-08-31 15:55:00 +0100659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 status = psa_hash_update(&op,
661 ctx->params.q_leaf_identifier,
662 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
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 mbedtls_lms_unsigned_int_to_network_bytes(i_digit_idx, I_DIGIT_IDX_LEN,
668 i_digit_idx_bytes);
669 status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN);
670 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100671 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 }
Raef Coles01c71a12022-08-31 15:55:00 +0100673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 status = psa_hash_update(&op, const_bytes, sizeof(const_bytes));
675 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100676 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 }
Raef Coles01c71a12022-08-31 15:55:00 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 status = psa_hash_update(&op, seed, seed_size);
680 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100681 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 }
Raef Coles01c71a12022-08-31 15:55:00 +0100683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 status = psa_hash_finish(&op,
685 ctx->private_key[i_digit_idx],
686 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
687 &output_hash_len);
688 if (status != PSA_SUCCESS) {
Raef Coles01c71a12022-08-31 15:55:00 +0100689 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 }
Raef Coles01c71a12022-08-31 15:55:00 +0100691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 psa_hash_abort(&op);
Raef Coles01c71a12022-08-31 15:55:00 +0100693 }
694
Raef Colesf5632d32022-09-01 09:56:52 +0100695 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100696
697exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 psa_hash_abort(&op);
Raef Coles01c71a12022-08-31 15:55:00 +0100699
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500700 return PSA_TO_MBEDTLS_ERR(status);
Raef Coles01c71a12022-08-31 15:55:00 +0100701}
702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703int mbedtls_lmots_calculate_public_key(mbedtls_lmots_public_t *ctx,
704 const mbedtls_lmots_private_t *priv_ctx)
Raef Coles01c71a12022-08-31 15:55:00 +0100705{
Raef Colese9479a02022-09-01 16:06:35 +0100706 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 +0100707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
708
Raef Coles8ff6df52021-07-21 12:42:15 +0100709 /* Check that a private key is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (!priv_ctx->have_private_key) {
711 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100712 }
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 ret = hash_digit_array(&priv_ctx->params,
715 (unsigned char *) priv_ctx->private_key, NULL,
716 NULL, (unsigned char *) y_hashed_digits);
717 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100718 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100719 }
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 ret = public_key_from_hashed_digit_array(&priv_ctx->params,
722 (unsigned char *) y_hashed_digits,
723 ctx->public_key);
724 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100725 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100726 }
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 memcpy(&ctx->params, &priv_ctx->params,
729 sizeof(ctx->params));
Raef Coles01c71a12022-08-31 15:55:00 +0100730
Raef Coles9b88ee52022-09-02 12:04:21 +0100731 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100732
Raef Coles142e5772022-10-12 10:47:27 +0100733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 mbedtls_platform_zeroize(y_hashed_digits, sizeof(y_hashed_digits));
Raef Coles142e5772022-10-12 10:47:27 +0100735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 return ret;
Raef Coles01c71a12022-08-31 15:55:00 +0100737}
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739int mbedtls_lmots_sign(mbedtls_lmots_private_t *ctx,
740 int (*f_rng)(void *, unsigned char *, size_t),
741 void *p_rng, const unsigned char *msg, size_t msg_size,
742 unsigned char *sig, size_t sig_size, size_t *sig_len)
Raef Coles01c71a12022-08-31 15:55:00 +0100743{
Raef Colese9479a02022-09-01 16:06:35 +0100744 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100745 /* Create a temporary buffer to prepare the signature in. This allows us to
746 * finish creating a signature (ensuring the process doesn't fail), and then
747 * erase the private key **before** writing any data into the sig parameter
748 * buffer. If data were directly written into the sig buffer, it might leak
749 * a partial signature on failure, which effectively compromises the private
750 * key.
751 */
Raef Colese9479a02022-09-01 16:06:35 +0100752 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Colesd48f7e92022-10-10 13:10:07 +0100753 unsigned char tmp_c_random[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (msg == NULL && msg_size != 0) {
757 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles01c71a12022-08-31 15:55:00 +0100758 }
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type)) {
761 return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
Raef Coles01c71a12022-08-31 15:55:00 +0100762 }
763
764 /* Check that a private key is loaded */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (!ctx->have_private_key) {
766 return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
Raef Coles8ff6df52021-07-21 12:42:15 +0100767 }
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 ret = f_rng(p_rng, tmp_c_random,
770 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
771 if (ret) {
772 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100773 }
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 ret = create_digit_array_with_checksum(&ctx->params,
776 msg, msg_size,
777 tmp_c_random,
778 tmp_digit_array);
779 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100780 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100781 }
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 ret = hash_digit_array(&ctx->params, (unsigned char *) ctx->private_key,
784 NULL, tmp_digit_array, (unsigned char *) tmp_sig);
785 if (ret) {
Raef Coles142e5772022-10-12 10:47:27 +0100786 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100787 }
788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
790 MBEDTLS_LMOTS_TYPE_LEN,
791 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET);
Raef Coles8ff6df52021-07-21 12:42:15 +0100792
Raef Coles9c9027b2022-09-02 18:26:31 +0100793 /* Test hook to check if sig is being written to before we invalidate the
794 * private key.
795 */
796#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (mbedtls_lmots_sign_private_key_invalidated_hook != NULL) {
798 ret = (*mbedtls_lmots_sign_private_key_invalidated_hook)(sig);
799 if (ret != 0) {
800 return ret;
801 }
Raef Coles9c9027b2022-09-02 18:26:31 +0100802 }
803#endif /* defined(MBEDTLS_TEST_HOOKS) */
804
Raef Coles8ff6df52021-07-21 12:42:15 +0100805 /* We've got a valid signature now, so it's time to make sure the private
806 * key can't be reused.
807 */
Raef Colesf5632d32022-09-01 09:56:52 +0100808 ctx->have_private_key = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 mbedtls_platform_zeroize(ctx->private_key,
810 sizeof(ctx->private_key));
Raef Coles8ff6df52021-07-21 12:42:15 +0100811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 memcpy(sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
813 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type));
Raef Coles891c6132022-09-01 11:05:48 +0100814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
816 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
817 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type));
Raef Coles8ff6df52021-07-21 12:42:15 +0100818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (sig_len != NULL) {
Raef Colese9479a02022-09-01 16:06:35 +0100820 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100821 }
822
Raef Coles142e5772022-10-12 10:47:27 +0100823 ret = 0;
824
825exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 mbedtls_platform_zeroize(tmp_digit_array, sizeof(tmp_digit_array));
827 mbedtls_platform_zeroize(tmp_sig, sizeof(tmp_sig));
Raef Coles142e5772022-10-12 10:47:27 +0100828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 return ret;
Raef Coles8ff6df52021-07-21 12:42:15 +0100830}
831
Raef Coles5127e852022-10-07 10:35:56 +0100832#endif /* defined(MBEDTLS_LMS_PRIVATE) */
833#endif /* defined(MBEDTLS_LMS_C) */