blob: 504da2e5d88bd805e2bb93778e9dd2137c7e8757 [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 Colese9479a02022-09-01 16:06:35 +010063/* Currently only defined for SHA256, 32 is the max hash output size */
Raef Coles57d53282022-09-27 11:30:51 +010064#define C_RANDOM_VALUE_LEN_MAX (MBEDTLS_LMOTS_N_HASH_LEN_MAX)
Raef Colese9479a02022-09-01 16:06:35 +010065
Raef Coles9b88ee52022-09-02 12:04:21 +010066#define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
Raef Coles01c71a12022-08-31 15:55:00 +010067
Raef Coles9b88ee52022-09-02 12:04:21 +010068#define D_CONST_LEN (2)
Raef Coles01c71a12022-08-31 15:55:00 +010069static const unsigned char D_PUBLIC_CONSTANT_BYTES[D_CONST_LEN] = {0x80, 0x80};
70static const unsigned char D_MESSAGE_CONSTANT_BYTES[D_CONST_LEN] = {0x81, 0x81};
Raef Coles8ff6df52021-07-21 12:42:15 +010071
Raef Coles9c9027b2022-09-02 18:26:31 +010072#if defined(MBEDTLS_TEST_HOOKS)
73int( *mbedtls_lmots_sign_private_key_invalidated_hook )( unsigned char * ) = NULL;
74#endif /* defined(MBEDTLS_TEST_HOOKS) */
75
Raef Colesad054252022-09-27 10:59:16 +010076void mbedtls_lms_unsigned_int_to_network_bytes( unsigned int val, size_t len,
77 unsigned char *bytes )
Raef Coles8ff6df52021-07-21 12:42:15 +010078{
79 size_t idx;
80
Raef Coles9b88ee52022-09-02 12:04:21 +010081 for ( idx = 0; idx < len; idx++ )
82 {
83 bytes[idx] = ( val >> ( ( len - 1 - idx ) * 8 ) ) & 0xFF;
Raef Coles8ff6df52021-07-21 12:42:15 +010084 }
85}
86
Raef Colesad054252022-09-27 10:59:16 +010087unsigned int mbedtls_lms_network_bytes_to_unsigned_int( size_t len,
88 const unsigned char *bytes )
Raef Coles8ff6df52021-07-21 12:42:15 +010089{
90 size_t idx;
91 unsigned int val = 0;
92
Raef Coles9b88ee52022-09-02 12:04:21 +010093 for ( idx = 0; idx < len; idx++ )
94 {
95 val |= ( ( unsigned int )bytes[idx] ) << (8 * ( len - 1 - idx ) );
Raef Coles8ff6df52021-07-21 12:42:15 +010096 }
97
98 return val;
99}
100
Raef Coles0a967cc2022-09-02 17:46:15 +0100101/* Calculate the checksum digits that are appended to the end of the LMOTS digit
102 * string. See NIST SP800-208 section 3.1 or RFC8554 Algorithm 2 for details of
103 * the checksum algorithm.
104 *
Raef Coles98d6e222022-09-23 09:04:04 +0100105 * params The LMOTS parameter set, I and q values which
106 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100107 *
Raef Coles98d6e222022-09-23 09:04:04 +0100108 * digest The digit string to create the digest from. As
109 * this does not contain a checksum, it is the same
110 * size as a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100111 */
Raef Colese9479a02022-09-01 16:06:35 +0100112static unsigned short lmots_checksum_calculate( const mbedtls_lmots_parameters_t *params,
113 const unsigned char* digest )
Raef Coles8ff6df52021-07-21 12:42:15 +0100114{
115 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100116 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100117
Raef Colese9479a02022-09-01 16:06:35 +0100118 for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100119 {
Raef Coles01c71a12022-08-31 15:55:00 +0100120 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +0100121 }
122
123 return sum;
124}
125
Raef Coles0a967cc2022-09-02 17:46:15 +0100126/* Create the string of digest digits (in the base determined by the Winternitz
127 * parameter with the checksum appended to the end (Q || cksm(Q)). See NIST
128 * SP800-208 section 3.1 or RFC8554 Algorithm 3 step 5 (also used in Algorithm
129 * 4b step 3) for details.
130 *
Raef Coles98d6e222022-09-23 09:04:04 +0100131 * params The LMOTS parameter set, I and q values which
132 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100133 *
Raef Coles98d6e222022-09-23 09:04:04 +0100134 * msg The message that will be hashed to create the
135 * digest.
Raef Coles0a967cc2022-09-02 17:46:15 +0100136 *
Raef Coles98d6e222022-09-23 09:04:04 +0100137 * msg_size The size of the message.
Raef Coles0a967cc2022-09-02 17:46:15 +0100138 *
Raef Coles98d6e222022-09-23 09:04:04 +0100139 * C_random_value The random value that will be combined with the
140 * message digest. This is always the same size as a
141 * hash output for whichever hash algorithm is
142 * determined by the parameter set.
Raef Coles0a967cc2022-09-02 17:46:15 +0100143 *
Raef Coles98d6e222022-09-23 09:04:04 +0100144 * output An output containing the digit string (+
145 * checksum) of length P digits (in the case of
146 * MBEDTLS_LMOTS_SHA256_N32_W8, this means it is of
147 * size P bytes).
Raef Coles0a967cc2022-09-02 17:46:15 +0100148 */
Raef Coles01c71a12022-08-31 15:55:00 +0100149static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *params,
150 const unsigned char *msg,
151 size_t msg_len,
Raef Colese9479a02022-09-01 16:06:35 +0100152 const unsigned char *C_random_value,
153 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100154{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100155 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100157 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100158 unsigned short checksum;
159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
160
Raef Colesc8f96042022-08-25 13:49:54 +0100161 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
162 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100163 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100164 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100165
Raef Colesf5632d32022-09-01 09:56:52 +0100166 status = psa_hash_update( &op, params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100167 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100168 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100169 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100170 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100171
Raef Colesf5632d32022-09-01 09:56:52 +0100172 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100173 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100174 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100175 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100176 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100177
Raef Coles01c71a12022-08-31 15:55:00 +0100178 status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100179 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100180 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100181 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100182
Raef Colese9479a02022-09-01 16:06:35 +0100183 status = psa_hash_update( &op, C_random_value,
184 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100185 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100186 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100187 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100188
Raef Colesc8f96042022-08-25 13:49:54 +0100189 status = psa_hash_update( &op, msg, msg_len );
190 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100191 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100192 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100193
Raef Colese9479a02022-09-01 16:06:35 +0100194 status = psa_hash_finish( &op, out,
Raef Colesfa24f9d2022-09-02 17:46:52 +0100195 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colesc8f96042022-08-25 13:49:54 +0100196 &output_hash_len );
197 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100198 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100199 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100200
Raef Colese9479a02022-09-01 16:06:35 +0100201 checksum = lmots_checksum_calculate( params, out );
Raef Colesad054252022-09-27 10:59:16 +0100202 mbedtls_lms_unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN,
203 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100204
Raef Coles01c71a12022-08-31 15:55:00 +0100205exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100206 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100207
208 return( ret );
209}
210
Raef Coles0a967cc2022-09-02 17:46:15 +0100211/* Hash each element of the string of digits (+ checksum), producing a hash
212 * output for each element. This is used in several places (by varying the
213 * hash_idx_min/max_values) in order to calculate a public key from a private
214 * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554
215 * Algorithm 3 step 5), and to calculate a public key candidate from a
216 * signature and message (RFC8554 Algorithm 4b step 3).
217 *
Raef Coles98d6e222022-09-23 09:04:04 +0100218 * params The LMOTS parameter set, I and q values which
219 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100220 *
Raef Coles98d6e222022-09-23 09:04:04 +0100221 * x_digit_array The array of digits (of size P, 34 in the case of
222 * MBEDTLS_LMOTS_SHA256_N32_W8).
Raef Coles0a967cc2022-09-02 17:46:15 +0100223 *
Raef Coles98d6e222022-09-23 09:04:04 +0100224 * hash_idx_min_values An array of the starting values of the j iterator
225 * for each of the members of the digit array. If
226 * this value in NULL, then all iterators will start
227 * at 0.
Raef Coles0a967cc2022-09-02 17:46:15 +0100228 *
Raef Coles98d6e222022-09-23 09:04:04 +0100229 * hash_idx_max_values An array of the upper bound values of the j
230 * iterator for each of the members of the digit
231 * array. If this value in NULL, then iterator is
232 * bounded to be less than 2^w - 1 (255 in the case
233 * of MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100234 *
Raef Coles98d6e222022-09-23 09:04:04 +0100235 * output An array containing a hash output for each member
236 * of the digit string P. In the case of
237 * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 *
238 * 34.
Raef Coles0a967cc2022-09-02 17:46:15 +0100239 */
Raef Coles01c71a12022-08-31 15:55:00 +0100240static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100241 const unsigned char *x_digit_array,
Raef Coles01c71a12022-08-31 15:55:00 +0100242 const unsigned char *hash_idx_min_values,
243 const unsigned char *hash_idx_max_values,
Raef Colese9479a02022-09-01 16:06:35 +0100244 unsigned char *output )
Raef Coles8ff6df52021-07-21 12:42:15 +0100245{
Raef Coles8738a492022-09-02 17:13:01 +0100246 unsigned int i_digit_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100247 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8738a492022-09-02 17:13:01 +0100248 unsigned int j_hash_idx;
249 unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100250 unsigned int j_hash_idx_min;
251 unsigned int j_hash_idx_max;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100252 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100254 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100255 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
257
Raef Colese9479a02022-09-01 16:06:35 +0100258 for ( i_digit_idx = 0;
259 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
260 i_digit_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100261 {
262
Raef Coles366d67d2022-09-01 17:23:12 +0100263 memcpy( tmp_hash,
264 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
Raef Colese9479a02022-09-01 16:06:35 +0100265 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100266
Raef Coles366d67d2022-09-01 17:23:12 +0100267 j_hash_idx_min = hash_idx_min_values != NULL ?
268 hash_idx_min_values[i_digit_idx] : 0;
269 j_hash_idx_max = hash_idx_max_values != NULL ?
270 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100271
Raef Coles8738a492022-09-02 17:13:01 +0100272 for ( j_hash_idx = j_hash_idx_min;
Raef Coles366d67d2022-09-01 17:23:12 +0100273 j_hash_idx < j_hash_idx_max;
274 j_hash_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100275 {
Raef Colesc8f96042022-08-25 13:49:54 +0100276 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
277 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100278 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100279 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100280
Raef Coles01c71a12022-08-31 15:55:00 +0100281 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100282 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100283 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100284 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100285 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100286 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100287
Raef Coles01c71a12022-08-31 15:55:00 +0100288 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100289 params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100290 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100291 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100292 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100293 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100294
Raef Colesad054252022-09-27 10:59:16 +0100295 mbedtls_lms_unsigned_int_to_network_bytes( i_digit_idx,
296 I_DIGIT_IDX_LEN,
297 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100298 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100299 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100300 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100301 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100302
Raef Colesad054252022-09-27 10:59:16 +0100303 mbedtls_lms_unsigned_int_to_network_bytes( j_hash_idx,
304 J_HASH_IDX_LEN,
305 j_hash_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100306 status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN );
307 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100308 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100309 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100310
Raef Colese9479a02022-09-01 16:06:35 +0100311 status = psa_hash_update( &op, tmp_hash,
312 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100313 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100314 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100315 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100316
Raef Coles366d67d2022-09-01 17:23:12 +0100317 status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ),
318 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100319 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100320 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100321 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100322
Raef Colesc8f96042022-08-25 13:49:54 +0100323 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100324 }
325
Raef Coles366d67d2022-09-01 17:23:12 +0100326 memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
327 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100328 }
329
Raef Coles01c71a12022-08-31 15:55:00 +0100330exit:
Raef Colese0a17612022-09-02 16:04:47 +0100331 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100332 {
Raef Colesc8f96042022-08-25 13:49:54 +0100333 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100334 return( ret );
335 }
336
Raef Coles01c71a12022-08-31 15:55:00 +0100337 mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) );
338
Raef Coles8ff6df52021-07-21 12:42:15 +0100339 return ret;
340}
341
Raef Coles0a967cc2022-09-02 17:46:15 +0100342/* Combine the hashes of the digit array into a public key. This is used in
343 * in order to calculate a public key from a private key (RFC8554 Algorithm 1
344 * step 4), and to calculate a public key candidate from a signature and message
345 * (RFC8554 Algorithm 4b step 3).
346 *
Raef Coles98d6e222022-09-23 09:04:04 +0100347 * params The LMOTS parameter set, I and q values which describe
348 * the key being used.
349 * y_hashed_digits The array of hashes, one hash for each digit of the
350 * symbol array (which is of size P, 34 in the case of
351 * MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100352 *
Raef Coles98d6e222022-09-23 09:04:04 +0100353 * pub_key The output public key (or candidate public key in
354 * case this is being run as part of signature
355 * verification), in the form of a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100356 */
Raef Coles01c71a12022-08-31 15:55:00 +0100357static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100358 const unsigned char *y_hashed_digits,
Raef Coles01c71a12022-08-31 15:55:00 +0100359 unsigned char *pub_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100360{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100361 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
362 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100363 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
365
Raef Colesc8f96042022-08-25 13:49:54 +0100366 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
367 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100368 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100369 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100370
Raef Coles01c71a12022-08-31 15:55:00 +0100371 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100372 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100373 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100374 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100375 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100376 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100377
Raef Colesf5632d32022-09-01 09:56:52 +0100378 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100379 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100380 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100381 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100382 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100383
Raef Coles01c71a12022-08-31 15:55:00 +0100384 status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100385 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100386 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100387 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100388
Raef Colese9479a02022-09-01 16:06:35 +0100389 status = psa_hash_update( &op, y_hashed_digits,
390 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
391 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100392 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100393 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100394 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100395
Raef Coles366d67d2022-09-01 17:23:12 +0100396 status = psa_hash_finish( &op, pub_key,
397 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colese9479a02022-09-01 16:06:35 +0100398 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100399 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100400
Raef Coles01c71a12022-08-31 15:55:00 +0100401exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100402 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100403 return( ret );
404}
405
Raef Coles9b88ee52022-09-02 12:04:21 +0100406int mbedtls_lms_error_from_psa( psa_status_t status )
Raef Colesc8f96042022-08-25 13:49:54 +0100407{
Raef Coles9b88ee52022-09-02 12:04:21 +0100408 switch( status )
409 {
Raef Colesc8f96042022-08-25 13:49:54 +0100410 case PSA_SUCCESS:
411 return( 0 );
412 case PSA_ERROR_HARDWARE_FAILURE:
413 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
414 case PSA_ERROR_NOT_SUPPORTED:
415 return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
416 case PSA_ERROR_BUFFER_TOO_SMALL:
417 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
418 case PSA_ERROR_INVALID_ARGUMENT:
419 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
420 default:
421 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
422 }
423}
424
Raef Coles01c71a12022-08-31 15:55:00 +0100425void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100426{
Raef Coles01c71a12022-08-31 15:55:00 +0100427 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100428}
429
Raef Coles01c71a12022-08-31 15:55:00 +0100430void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100431{
Raef Coles01c71a12022-08-31 15:55:00 +0100432 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100433}
434
Raef Coles01c71a12022-08-31 15:55:00 +0100435int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
436 const unsigned char *key, size_t key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100437{
Raef Colesf5632d32022-09-01 09:56:52 +0100438 ctx->params.type =
Raef Colesad054252022-09-27 10:59:16 +0100439 mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
440 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100441
Raef Colese0a17612022-09-02 16:04:47 +0100442 if( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Colese9479a02022-09-01 16:06:35 +0100443 {
444 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
445 }
446
Raef Colesf5632d32022-09-01 09:56:52 +0100447 memcpy( ctx->params.I_key_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100448 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100449 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100450
Raef Colesf5632d32022-09-01 09:56:52 +0100451 memcpy( ctx->params.q_leaf_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100452 key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100453 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100454
Raef Colesf5632d32022-09-01 09:56:52 +0100455 memcpy( ctx->public_key,
Raef Coles57d53282022-09-27 11:30:51 +0100456 key + PUBLIC_KEY_KEY_HASH_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100457 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100458
Raef Colesf5632d32022-09-01 09:56:52 +0100459 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100460
461 return( 0 );
462}
463
Raef Coles01c71a12022-08-31 15:55:00 +0100464int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
465 const unsigned char *msg,
466 size_t msg_size,
467 const unsigned char *sig,
468 size_t sig_size,
469 unsigned char *out,
470 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100471 size_t *out_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100472{
Raef Colese9479a02022-09-01 16:06:35 +0100473 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
474 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 +0100475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
476
Raef Colese0a17612022-09-02 16:04:47 +0100477 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100478 {
479 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
480 }
481
Raef Colese0a17612022-09-02 16:04:47 +0100482 if( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
Raef Colese9479a02022-09-01 16:06:35 +0100483 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100484 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100485 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100486 }
487
Raef Coles01c71a12022-08-31 15:55:00 +0100488 ret = create_digit_array_with_checksum( params, msg, msg_size,
489 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
490 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100491 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100492 {
493 return ( ret );
494 }
495
Raef Coles01c71a12022-08-31 15:55:00 +0100496 ret = hash_digit_array( params,
Raef Colese9479a02022-09-01 16:06:35 +0100497 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100498 tmp_digit_array, NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100499 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100500 {
501 return ( ret );
502 }
503
Raef Colese9479a02022-09-01 16:06:35 +0100504 ret = public_key_from_hashed_digit_array( params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100505 ( unsigned char * )y_hashed_digits,
Raef Colese9479a02022-09-01 16:06:35 +0100506 out );
Raef Colese0a17612022-09-02 16:04:47 +0100507 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100508 {
509 return ( ret );
510 }
511
Raef Colese0a17612022-09-02 16:04:47 +0100512 if( out_len != NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100513 {
Raef Colese9479a02022-09-01 16:06:35 +0100514 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100515 }
516
Raef Coles8ff6df52021-07-21 12:42:15 +0100517 return( 0 );
518}
519
Raef Coles2ac352a2022-10-07 11:12:27 +0100520int mbedtls_lmots_verify( const mbedtls_lmots_public_t *ctx,
521 const unsigned char *msg, size_t msg_size,
522 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100523{
Raef Colese9479a02022-09-01 16:06:35 +0100524 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
526
Raef Colese0a17612022-09-02 16:04:47 +0100527 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100528 {
529 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
530 }
531
Raef Colese0a17612022-09-02 16:04:47 +0100532 if( !ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100533 {
534 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
535 }
536
Raef Coles9b88ee52022-09-02 12:04:21 +0100537 if( ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100538 {
539 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
540 }
541
Raef Colesad054252022-09-27 10:59:16 +0100542 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100543 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100544 {
545 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
546 }
547
Raef Colesf5632d32022-09-01 09:56:52 +0100548 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100549 msg, msg_size, sig, sig_size,
550 Kc_public_key_candidate,
Raef Colese9479a02022-09-01 16:06:35 +0100551 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100552 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100553 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100554 {
555 return( ret );
556 }
557
Raef Colese0a17612022-09-02 16:04:47 +0100558 if( memcmp( &Kc_public_key_candidate, ctx->public_key,
Raef Colesf5632d32022-09-01 09:56:52 +0100559 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100560 {
561 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
562 }
563
564 return( 0 );
565}
566
Raef Coles5127e852022-10-07 10:35:56 +0100567#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100568
Raef Coles01c71a12022-08-31 15:55:00 +0100569void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx )
570{
571 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
572}
573
574void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx )
575{
576 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
577}
578
579int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
580 mbedtls_lmots_algorithm_type_t type,
581 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
582 uint32_t q_leaf_identifier,
583 const unsigned char *seed,
584 size_t seed_size )
585{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100586 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
587 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Coles01c71a12022-08-31 15:55:00 +0100588 size_t output_hash_len;
589 unsigned int i_digit_idx;
590 unsigned char i_digit_idx_bytes[2];
591 unsigned char const_bytes[1];
592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
593
Raef Colese0a17612022-09-02 16:04:47 +0100594 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100595 {
596 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
597 }
598
Raef Colese0a17612022-09-02 16:04:47 +0100599 if( type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles9b88ee52022-09-02 12:04:21 +0100600 {
Raef Coles01c71a12022-08-31 15:55:00 +0100601 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
602 }
603
Raef Colesf5632d32022-09-01 09:56:52 +0100604 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100605
Raef Colesf5632d32022-09-01 09:56:52 +0100606 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100607 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100608 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100609
Raef Colesad054252022-09-27 10:59:16 +0100610 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
611 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
612 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100613
Raef Colesad054252022-09-27 10:59:16 +0100614 mbedtls_lms_unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ),
615 const_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100616
Raef Colese9479a02022-09-01 16:06:35 +0100617 for ( i_digit_idx = 0;
618 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
619 i_digit_idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100620 {
Raef Coles01c71a12022-08-31 15:55:00 +0100621 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
622 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100623 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100624 goto exit;
625
626 ret = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100627 ctx->params.I_key_identifier,
628 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100629 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100630 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100631 goto exit;
632
633 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100634 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100635 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
636 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100637 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100638 goto exit;
639
Raef Colesad054252022-09-27 10:59:16 +0100640 mbedtls_lms_unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
641 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100642 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
643 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100644 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100645 goto exit;
646
Raef Coles9b88ee52022-09-02 12:04:21 +0100647 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100648 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100649 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100650 goto exit;
651
652 status = psa_hash_update( &op, seed, seed_size );
653 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100654 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100655 goto exit;
656
657 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100658 ctx->private_key[i_digit_idx],
Raef Colese9479a02022-09-01 16:06:35 +0100659 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
660 &output_hash_len );
Raef Coles01c71a12022-08-31 15:55:00 +0100661 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100662 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100663 goto exit;
664
665 psa_hash_abort( &op );
666 }
667
Raef Colesf5632d32022-09-01 09:56:52 +0100668 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100669
670exit:
Raef Colese0a17612022-09-02 16:04:47 +0100671 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100672 {
673 psa_hash_abort( &op );
674 return( ret );
675 }
676
677 return ret;
678}
679
680int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles2ac352a2022-10-07 11:12:27 +0100681 const mbedtls_lmots_private_t *priv_ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100682{
Raef Colese9479a02022-09-01 16:06:35 +0100683 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 +0100684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
685
Raef Coles8ff6df52021-07-21 12:42:15 +0100686 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100687 if( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100688 {
689 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
690 }
691
Raef Colese9479a02022-09-01 16:06:35 +0100692 ret = hash_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100693 ( unsigned char * )priv_ctx->private_key, NULL,
694 NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100695 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100696 {
697 return( ret );
698 }
699
Raef Colesf5632d32022-09-01 09:56:52 +0100700 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100701 ( unsigned char * )y_hashed_digits,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100702 ctx->public_key );
Raef Colese0a17612022-09-02 16:04:47 +0100703 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100704 {
705 return( ret );
706 }
707
Raef Colesf5632d32022-09-01 09:56:52 +0100708 memcpy( &ctx->params, &priv_ctx->params,
709 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100710
Raef Coles9b88ee52022-09-02 12:04:21 +0100711 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100712
713 return( ret );
714}
715
716
Raef Coles2ac352a2022-10-07 11:12:27 +0100717int mbedtls_lmots_export_public_key( const mbedtls_lmots_public_t *ctx,
Raef Coles01c71a12022-08-31 15:55:00 +0100718 unsigned char *key, size_t key_size,
719 size_t *key_len )
720{
Raef Colese9479a02022-09-01 16:06:35 +0100721 if( key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100722 {
723 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
724 }
725
Raef Colesf5632d32022-09-01 09:56:52 +0100726 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100727 {
728 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
729 }
730
Raef Colesad054252022-09-27 10:59:16 +0100731 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
732 MBEDTLS_LMOTS_TYPE_LEN,
733 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100734
Raef Coles57d53282022-09-27 11:30:51 +0100735 memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100736 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100737 MBEDTLS_LMOTS_I_KEY_ID_LEN );
738
Raef Coles57d53282022-09-27 11:30:51 +0100739 memcpy( key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
Raef Coles9b88ee52022-09-02 12:04:21 +0100740 ctx->params.q_leaf_identifier,
741 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100742
Raef Coles57d53282022-09-27 11:30:51 +0100743 memcpy( key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
Raef Colese9479a02022-09-01 16:06:35 +0100744 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100745
746 if( key_len != NULL )
747 {
Raef Colese9479a02022-09-01 16:06:35 +0100748 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100749 }
750
751 return( 0 );
752}
753
754int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
755 int (*f_rng)(void *, unsigned char *, size_t),
756 void *p_rng, const unsigned char *msg, size_t msg_size,
757 unsigned char *sig, size_t sig_size, size_t* sig_len )
758{
Raef Colese9479a02022-09-01 16:06:35 +0100759 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100760 /* Create a temporary buffer to prepare the signature in. This allows us to
761 * finish creating a signature (ensuring the process doesn't fail), and then
762 * erase the private key **before** writing any data into the sig parameter
763 * buffer. If data were directly written into the sig buffer, it might leak
764 * a partial signature on failure, which effectively compromises the private
765 * key.
766 */
Raef Colese9479a02022-09-01 16:06:35 +0100767 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles57d53282022-09-27 11:30:51 +0100768 unsigned char tmp_c_random[C_RANDOM_VALUE_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
770
Raef Colese0a17612022-09-02 16:04:47 +0100771 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100772 {
773 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
774 }
775
Raef Colese9479a02022-09-01 16:06:35 +0100776 if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100777 {
778 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
779 }
780
781 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100782 if( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100783 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100784 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100785 }
786
Raef Coles366d67d2022-09-01 17:23:12 +0100787 ret = f_rng( p_rng, tmp_c_random,
788 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Colese0a17612022-09-02 16:04:47 +0100789 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100790 {
791 return( ret );
792 }
793
Raef Colesf5632d32022-09-01 09:56:52 +0100794 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100795 msg, msg_size,
Raef Coles891c6132022-09-01 11:05:48 +0100796 tmp_c_random,
Raef Coles01c71a12022-08-31 15:55:00 +0100797 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100798 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100799 {
800 return( ret );
801 }
802
Raef Coles9b88ee52022-09-02 12:04:21 +0100803 ret = hash_digit_array( &ctx->params, ( unsigned char * )ctx->private_key,
804 NULL, tmp_digit_array, ( unsigned char * )tmp_sig );
Raef Colese0a17612022-09-02 16:04:47 +0100805 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100806 {
807 return( ret );
808 }
809
Raef Colesad054252022-09-27 10:59:16 +0100810 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
811 MBEDTLS_LMOTS_TYPE_LEN,
812 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100813
Raef Coles9c9027b2022-09-02 18:26:31 +0100814 /* Test hook to check if sig is being written to before we invalidate the
815 * private key.
816 */
817#if defined(MBEDTLS_TEST_HOOKS)
818 if( mbedtls_lmots_sign_private_key_invalidated_hook != NULL )
819 {
820 ret = ( *mbedtls_lmots_sign_private_key_invalidated_hook )( sig );
821 if( ret != 0 )
822 return( ret );
823 }
824#endif /* defined(MBEDTLS_TEST_HOOKS) */
825
Raef Coles8ff6df52021-07-21 12:42:15 +0100826 /* We've got a valid signature now, so it's time to make sure the private
827 * key can't be reused.
828 */
Raef Colesf5632d32022-09-01 09:56:52 +0100829 ctx->have_private_key = 0;
Raef Coles9b88ee52022-09-02 12:04:21 +0100830 mbedtls_platform_zeroize( ctx->private_key,
831 sizeof( ctx->private_key ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100832
Raef Coles891c6132022-09-01 11:05:48 +0100833 memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
Raef Colese9479a02022-09-01 16:06:35 +0100834 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) );
Raef Coles891c6132022-09-01 11:05:48 +0100835
Raef Colese9479a02022-09-01 16:06:35 +0100836 memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
837 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
838 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100839
Raef Coles01c71a12022-08-31 15:55:00 +0100840 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100841 {
Raef Colese9479a02022-09-01 16:06:35 +0100842 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100843 }
844
845 return( 0 );
846}
847
Raef Coles5127e852022-10-07 10:35:56 +0100848#endif /* defined(MBEDTLS_LMS_PRIVATE) */
849#endif /* defined(MBEDTLS_LMS_C) */