blob: 97325aa8e183f149b3d75add84a88ded87f57ce4 [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)
Raef Coles01c71a12022-08-31 15:55:00 +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)
70int( *mbedtls_lmots_sign_private_key_invalidated_hook )( unsigned char * ) = NULL;
71#endif /* defined(MBEDTLS_TEST_HOOKS) */
72
Raef Colesad054252022-09-27 10:59:16 +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
Raef Coles9b88ee52022-09-02 12:04:21 +010078 for ( idx = 0; idx < len; idx++ )
79 {
80 bytes[idx] = ( val >> ( ( len - 1 - idx ) * 8 ) ) & 0xFF;
Raef Coles8ff6df52021-07-21 12:42:15 +010081 }
82}
83
Raef Colesad054252022-09-27 10:59:16 +010084unsigned int mbedtls_lms_network_bytes_to_unsigned_int( size_t len,
85 const unsigned char *bytes )
Raef Coles8ff6df52021-07-21 12:42:15 +010086{
87 size_t idx;
88 unsigned int val = 0;
89
Raef Coles9b88ee52022-09-02 12:04:21 +010090 for ( idx = 0; idx < len; idx++ )
91 {
92 val |= ( ( unsigned int )bytes[idx] ) << (8 * ( len - 1 - idx ) );
Raef Coles8ff6df52021-07-21 12:42:15 +010093 }
94
Raef Colesf6cb5a42022-10-10 14:15:53 +010095 return ( val );
Raef Coles8ff6df52021-07-21 12:42:15 +010096}
97
Raef Coles0a967cc2022-09-02 17:46:15 +010098/* Calculate the checksum digits that are appended to the end of the LMOTS digit
99 * string. See NIST SP800-208 section 3.1 or RFC8554 Algorithm 2 for details of
100 * the checksum algorithm.
101 *
Raef Coles98d6e222022-09-23 09:04:04 +0100102 * params The LMOTS parameter set, I and q values which
103 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100104 *
Raef Coles98d6e222022-09-23 09:04:04 +0100105 * digest The digit string to create the digest from. As
106 * this does not contain a checksum, it is the same
107 * size as a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100108 */
Raef Colese9479a02022-09-01 16:06:35 +0100109static unsigned short lmots_checksum_calculate( const mbedtls_lmots_parameters_t *params,
110 const unsigned char* digest )
Raef Coles8ff6df52021-07-21 12:42:15 +0100111{
112 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100113 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +0100114
Raef Colese9479a02022-09-01 16:06:35 +0100115 for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100116 {
Raef Coles01c71a12022-08-31 15:55:00 +0100117 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +0100118 }
119
Raef Colesf6cb5a42022-10-10 14:15:53 +0100120 return ( sum );
Raef Coles8ff6df52021-07-21 12:42:15 +0100121}
122
Raef Coles0a967cc2022-09-02 17:46:15 +0100123/* Create the string of digest digits (in the base determined by the Winternitz
124 * parameter with the checksum appended to the end (Q || cksm(Q)). See NIST
125 * SP800-208 section 3.1 or RFC8554 Algorithm 3 step 5 (also used in Algorithm
126 * 4b step 3) for details.
127 *
Raef Coles98d6e222022-09-23 09:04:04 +0100128 * params The LMOTS parameter set, I and q values which
129 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100130 *
Raef Coles98d6e222022-09-23 09:04:04 +0100131 * msg The message that will be hashed to create the
132 * digest.
Raef Coles0a967cc2022-09-02 17:46:15 +0100133 *
Raef Coles98d6e222022-09-23 09:04:04 +0100134 * msg_size The size of the message.
Raef Coles0a967cc2022-09-02 17:46:15 +0100135 *
Raef Coles98d6e222022-09-23 09:04:04 +0100136 * C_random_value The random value that will be combined with the
137 * message digest. This is always the same size as a
138 * hash output for whichever hash algorithm is
139 * determined by the parameter set.
Raef Coles0a967cc2022-09-02 17:46:15 +0100140 *
Raef Coles98d6e222022-09-23 09:04:04 +0100141 * output An output containing the digit string (+
142 * checksum) of length P digits (in the case of
143 * MBEDTLS_LMOTS_SHA256_N32_W8, this means it is of
144 * size P bytes).
Raef Coles0a967cc2022-09-02 17:46:15 +0100145 */
Raef Coles01c71a12022-08-31 15:55:00 +0100146static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *params,
147 const unsigned char *msg,
148 size_t msg_len,
Raef Colese9479a02022-09-01 16:06:35 +0100149 const unsigned char *C_random_value,
150 unsigned char *out )
Raef Coles8ff6df52021-07-21 12:42:15 +0100151{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100152 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100154 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100155 unsigned short checksum;
Raef Coles8ff6df52021-07-21 12:42:15 +0100156
Raef Colesc8f96042022-08-25 13:49:54 +0100157 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100158 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100159 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100160
Raef Colesf5632d32022-09-01 09:56:52 +0100161 status = psa_hash_update( &op, params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100162 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100163 if( status != PSA_SUCCESS )
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->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100167 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100168 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100169 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100170
Raef Coles01c71a12022-08-31 15:55:00 +0100171 status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100172 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100173 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100174
Raef Colese9479a02022-09-01 16:06:35 +0100175 status = psa_hash_update( &op, C_random_value,
176 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100177 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100178 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100179
Raef Colesc8f96042022-08-25 13:49:54 +0100180 status = psa_hash_update( &op, msg, msg_len );
Raef Coles29117d22022-10-07 11:46:06 +0100181 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100182 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100183
Raef Colese9479a02022-09-01 16:06:35 +0100184 status = psa_hash_finish( &op, out,
Raef Colesfa24f9d2022-09-02 17:46:52 +0100185 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colesc8f96042022-08-25 13:49:54 +0100186 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100187 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100188 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100189
Raef Colese9479a02022-09-01 16:06:35 +0100190 checksum = lmots_checksum_calculate( params, out );
Raef Colesad054252022-09-27 10:59:16 +0100191 mbedtls_lms_unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN,
192 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100193
Raef Coles01c71a12022-08-31 15:55:00 +0100194exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100195 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100196
Raef Coles29117d22022-10-07 11:46:06 +0100197 return( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100198}
199
Raef Coles0a967cc2022-09-02 17:46:15 +0100200/* Hash each element of the string of digits (+ checksum), producing a hash
201 * output for each element. This is used in several places (by varying the
202 * hash_idx_min/max_values) in order to calculate a public key from a private
203 * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554
204 * Algorithm 3 step 5), and to calculate a public key candidate from a
205 * signature and message (RFC8554 Algorithm 4b step 3).
206 *
Raef Coles98d6e222022-09-23 09:04:04 +0100207 * params The LMOTS parameter set, I and q values which
208 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100209 *
Raef Coles98d6e222022-09-23 09:04:04 +0100210 * x_digit_array The array of digits (of size P, 34 in the case of
211 * MBEDTLS_LMOTS_SHA256_N32_W8).
Raef Coles0a967cc2022-09-02 17:46:15 +0100212 *
Raef Coles98d6e222022-09-23 09:04:04 +0100213 * hash_idx_min_values An array of the starting values of the j iterator
214 * for each of the members of the digit array. If
215 * this value in NULL, then all iterators will start
216 * at 0.
Raef Coles0a967cc2022-09-02 17:46:15 +0100217 *
Raef Coles98d6e222022-09-23 09:04:04 +0100218 * hash_idx_max_values An array of the upper bound values of the j
219 * iterator for each of the members of the digit
220 * array. If this value in NULL, then iterator is
221 * bounded to be less than 2^w - 1 (255 in the case
222 * of MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100223 *
Raef Coles98d6e222022-09-23 09:04:04 +0100224 * output An array containing a hash output for each member
225 * of the digit string P. In the case of
226 * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 *
227 * 34.
Raef Coles0a967cc2022-09-02 17:46:15 +0100228 */
Raef Coles01c71a12022-08-31 15:55:00 +0100229static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100230 const unsigned char *x_digit_array,
Raef Coles01c71a12022-08-31 15:55:00 +0100231 const unsigned char *hash_idx_min_values,
232 const unsigned char *hash_idx_max_values,
Raef Colese9479a02022-09-01 16:06:35 +0100233 unsigned char *output )
Raef Coles8ff6df52021-07-21 12:42:15 +0100234{
Raef Coles8738a492022-09-02 17:13:01 +0100235 unsigned int i_digit_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100236 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8738a492022-09-02 17:13:01 +0100237 unsigned int j_hash_idx;
238 unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100239 unsigned int j_hash_idx_min;
240 unsigned int j_hash_idx_max;
Raef Colesbe0c2f92022-10-07 11:27:35 +0100241 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100243 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100244 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100245
Raef Colese9479a02022-09-01 16:06:35 +0100246 for ( i_digit_idx = 0;
247 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
248 i_digit_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100249 {
250
Raef Coles366d67d2022-09-01 17:23:12 +0100251 memcpy( tmp_hash,
252 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
Raef Colese9479a02022-09-01 16:06:35 +0100253 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100254
Raef Coles366d67d2022-09-01 17:23:12 +0100255 j_hash_idx_min = hash_idx_min_values != NULL ?
256 hash_idx_min_values[i_digit_idx] : 0;
257 j_hash_idx_max = hash_idx_max_values != NULL ?
258 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100259
Raef Coles8738a492022-09-02 17:13:01 +0100260 for ( j_hash_idx = j_hash_idx_min;
Raef Coles366d67d2022-09-01 17:23:12 +0100261 j_hash_idx < j_hash_idx_max;
262 j_hash_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100263 {
Raef Colesc8f96042022-08-25 13:49:54 +0100264 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100265 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100266 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100267
Raef Coles01c71a12022-08-31 15:55:00 +0100268 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100269 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100270 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100271 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100272 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100273
Raef Coles01c71a12022-08-31 15:55:00 +0100274 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100275 params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100276 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100277 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100278 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100279
Raef Colesad054252022-09-27 10:59:16 +0100280 mbedtls_lms_unsigned_int_to_network_bytes( i_digit_idx,
281 I_DIGIT_IDX_LEN,
282 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100283 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100284 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100285 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100286
Raef Colesad054252022-09-27 10:59:16 +0100287 mbedtls_lms_unsigned_int_to_network_bytes( j_hash_idx,
288 J_HASH_IDX_LEN,
289 j_hash_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100290 status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100291 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100292 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100293
Raef Colese9479a02022-09-01 16:06:35 +0100294 status = psa_hash_update( &op, tmp_hash,
295 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100296 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100297 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100298
Raef Coles366d67d2022-09-01 17:23:12 +0100299 status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ),
300 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100301 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100302 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100303
Raef Colesc8f96042022-08-25 13:49:54 +0100304 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100305 }
306
Raef Coles366d67d2022-09-01 17:23:12 +0100307 memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
308 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100309 }
310
Raef Coles01c71a12022-08-31 15:55:00 +0100311exit:
Raef Coles29117d22022-10-07 11:46:06 +0100312 psa_hash_abort( &op );
Raef Coles01c71a12022-08-31 15:55:00 +0100313 mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) );
314
Raef Coles29117d22022-10-07 11:46:06 +0100315 return( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100316}
317
Raef Coles0a967cc2022-09-02 17:46:15 +0100318/* Combine the hashes of the digit array into a public key. This is used in
319 * in order to calculate a public key from a private key (RFC8554 Algorithm 1
320 * step 4), and to calculate a public key candidate from a signature and message
321 * (RFC8554 Algorithm 4b step 3).
322 *
Raef Coles98d6e222022-09-23 09:04:04 +0100323 * params The LMOTS parameter set, I and q values which describe
324 * the key being used.
325 * y_hashed_digits The array of hashes, one hash for each digit of the
326 * symbol array (which is of size P, 34 in the case of
327 * MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100328 *
Raef Coles98d6e222022-09-23 09:04:04 +0100329 * pub_key The output public key (or candidate public key in
330 * case this is being run as part of signature
331 * verification), in the form of a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100332 */
Raef Coles01c71a12022-08-31 15:55:00 +0100333static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100334 const unsigned char *y_hashed_digits,
Raef Coles01c71a12022-08-31 15:55:00 +0100335 unsigned char *pub_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100336{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100337 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Colesc8f96042022-08-25 13:49:54 +0100339 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100340
Raef Colesc8f96042022-08-25 13:49:54 +0100341 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100342 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100343 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100344
Raef Coles01c71a12022-08-31 15:55:00 +0100345 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100346 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100347 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100348 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100349 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100350
Raef Colesf5632d32022-09-01 09:56:52 +0100351 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100352 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100353 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100354 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100355
Raef Coles01c71a12022-08-31 15:55:00 +0100356 status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100357 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100358 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100359
Raef Colese9479a02022-09-01 16:06:35 +0100360 status = psa_hash_update( &op, y_hashed_digits,
361 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
362 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles29117d22022-10-07 11:46:06 +0100363 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100364 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100365
Raef Coles366d67d2022-09-01 17:23:12 +0100366 status = psa_hash_finish( &op, pub_key,
367 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colese9479a02022-09-01 16:06:35 +0100368 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100369 if( status != PSA_SUCCESS )
Raef Coles8ff6df52021-07-21 12:42:15 +0100370
Raef Coles01c71a12022-08-31 15:55:00 +0100371exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100372 psa_hash_abort( &op );
Raef Coles29117d22022-10-07 11:46:06 +0100373
374 return( mbedtls_lms_error_from_psa( status ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100375}
376
Raef Coles9b88ee52022-09-02 12:04:21 +0100377int mbedtls_lms_error_from_psa( psa_status_t status )
Raef Colesc8f96042022-08-25 13:49:54 +0100378{
Raef Coles9b88ee52022-09-02 12:04:21 +0100379 switch( status )
380 {
Raef Colesc8f96042022-08-25 13:49:54 +0100381 case PSA_SUCCESS:
382 return( 0 );
383 case PSA_ERROR_HARDWARE_FAILURE:
384 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
385 case PSA_ERROR_NOT_SUPPORTED:
386 return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
387 case PSA_ERROR_BUFFER_TOO_SMALL:
388 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
389 case PSA_ERROR_INVALID_ARGUMENT:
390 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
391 default:
392 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
393 }
394}
395
Raef Colesbe3bdd82022-10-07 12:04:24 +0100396void mbedtls_lmots_public_init( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100397{
Raef Colesfbd60ec2022-10-10 15:09:33 +0100398 memset( ctx, 0, sizeof( *ctx ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100399}
400
Raef Colesbe3bdd82022-10-07 12:04:24 +0100401void mbedtls_lmots_public_free( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100402{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100403 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100404}
405
Raef Coles01c71a12022-08-31 15:55:00 +0100406int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
407 const unsigned char *key, size_t key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100408{
Raef Colese89488d2022-10-07 16:06:35 +0100409 if( key_len < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN )
410 {
411 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
412 }
413
Raef Colesf5632d32022-09-01 09:56:52 +0100414 ctx->params.type =
Raef Colesad054252022-09-27 10:59:16 +0100415 mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
416 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100417
Raef Colese0a17612022-09-02 16:04:47 +0100418 if( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Colese9479a02022-09-01 16:06:35 +0100419 {
420 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
421 }
422
Raef Colesf5632d32022-09-01 09:56:52 +0100423 memcpy( ctx->params.I_key_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100424 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100425 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100426
Raef Colesf5632d32022-09-01 09:56:52 +0100427 memcpy( ctx->params.q_leaf_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100428 key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100429 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100430
Raef Colesf5632d32022-09-01 09:56:52 +0100431 memcpy( ctx->public_key,
Raef Coles57d53282022-09-27 11:30:51 +0100432 key + PUBLIC_KEY_KEY_HASH_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100433 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100434
Raef Colesf5632d32022-09-01 09:56:52 +0100435 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100436
437 return( 0 );
438}
439
Raef Coles370cc432022-10-07 16:07:33 +0100440int mbedtls_lmots_export_public_key( const mbedtls_lmots_public_t *ctx,
441 unsigned char *key, size_t key_size,
442 size_t *key_len )
443{
444 if( key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
445 {
446 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
447 }
448
449 if( ! ctx->have_public_key )
450 {
451 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
452 }
453
454 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
455 MBEDTLS_LMOTS_TYPE_LEN,
456 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
457
458 memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
459 ctx->params.I_key_identifier,
460 MBEDTLS_LMOTS_I_KEY_ID_LEN );
461
462 memcpy( key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
463 ctx->params.q_leaf_identifier,
464 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
465
466 memcpy( key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
467 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
468
469 if( key_len != NULL )
470 {
471 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
472 }
473
474 return( 0 );
475}
476
Raef Coles01c71a12022-08-31 15:55:00 +0100477int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
478 const unsigned char *msg,
479 size_t msg_size,
480 const unsigned char *sig,
481 size_t sig_size,
482 unsigned char *out,
483 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100484 size_t *out_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100485{
Raef Colese9479a02022-09-01 16:06:35 +0100486 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
487 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 +0100488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
489
Raef Colese0a17612022-09-02 16:04:47 +0100490 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100491 {
492 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
493 }
494
Raef Colese0a17612022-09-02 16:04:47 +0100495 if( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
Raef Colese9479a02022-09-01 16:06:35 +0100496 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100497 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100498 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100499 }
500
Raef Coles01c71a12022-08-31 15:55:00 +0100501 ret = create_digit_array_with_checksum( params, msg, msg_size,
502 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
503 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100504 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100505 {
506 return ( ret );
507 }
508
Raef Coles01c71a12022-08-31 15:55:00 +0100509 ret = hash_digit_array( params,
Raef Colese9479a02022-09-01 16:06:35 +0100510 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100511 tmp_digit_array, NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100512 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100513 {
514 return ( ret );
515 }
516
Raef Colese9479a02022-09-01 16:06:35 +0100517 ret = public_key_from_hashed_digit_array( params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100518 ( unsigned char * )y_hashed_digits,
Raef Colese9479a02022-09-01 16:06:35 +0100519 out );
Raef Colese0a17612022-09-02 16:04:47 +0100520 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100521 {
522 return ( ret );
523 }
524
Raef Colese0a17612022-09-02 16:04:47 +0100525 if( out_len != NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100526 {
Raef Colese9479a02022-09-01 16:06:35 +0100527 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100528 }
529
Raef Coles8ff6df52021-07-21 12:42:15 +0100530 return( 0 );
531}
532
Raef Coles2ac352a2022-10-07 11:12:27 +0100533int mbedtls_lmots_verify( const mbedtls_lmots_public_t *ctx,
534 const unsigned char *msg, size_t msg_size,
535 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100536{
Raef Colese9479a02022-09-01 16:06:35 +0100537 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
539
Raef Colese0a17612022-09-02 16:04:47 +0100540 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100541 {
542 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
543 }
544
Raef Colese0a17612022-09-02 16:04:47 +0100545 if( !ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100546 {
547 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
548 }
549
Raef Coles9b88ee52022-09-02 12:04:21 +0100550 if( ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100551 {
552 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
553 }
554
Raef Colesad054252022-09-27 10:59:16 +0100555 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100556 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100557 {
558 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
559 }
560
Raef Colesf5632d32022-09-01 09:56:52 +0100561 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100562 msg, msg_size, sig, sig_size,
563 Kc_public_key_candidate,
Raef Colese9479a02022-09-01 16:06:35 +0100564 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100565 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100566 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100567 {
Raef Colesfaf59ba2022-10-10 15:40:56 +0100568 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
Raef Coles01c71a12022-08-31 15:55:00 +0100569 }
570
Raef Colese0a17612022-09-02 16:04:47 +0100571 if( memcmp( &Kc_public_key_candidate, ctx->public_key,
Raef Colesf5632d32022-09-01 09:56:52 +0100572 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100573 {
574 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
575 }
576
577 return( 0 );
578}
579
Raef Coles5127e852022-10-07 10:35:56 +0100580#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100581
Raef Colesbe3bdd82022-10-07 12:04:24 +0100582void mbedtls_lmots_private_init( mbedtls_lmots_private_t *ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100583{
Raef Colesfbd60ec2022-10-10 15:09:33 +0100584 memset( ctx, 0, sizeof( *ctx ) ) ;
Raef Coles01c71a12022-08-31 15:55:00 +0100585}
586
Raef Colesbe3bdd82022-10-07 12:04:24 +0100587void mbedtls_lmots_private_free( mbedtls_lmots_private_t *ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100588{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100589 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) ) ;
Raef Coles01c71a12022-08-31 15:55:00 +0100590}
591
592int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
593 mbedtls_lmots_algorithm_type_t type,
594 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
595 uint32_t q_leaf_identifier,
596 const unsigned char *seed,
597 size_t seed_size )
598{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100599 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Coles01c71a12022-08-31 15:55:00 +0100601 size_t output_hash_len;
602 unsigned int i_digit_idx;
603 unsigned char i_digit_idx_bytes[2];
604 unsigned char const_bytes[1];
Raef Coles01c71a12022-08-31 15:55:00 +0100605
Raef Colese0a17612022-09-02 16:04:47 +0100606 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100607 {
608 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
609 }
610
Raef Colese0a17612022-09-02 16:04:47 +0100611 if( type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles9b88ee52022-09-02 12:04:21 +0100612 {
Raef Coles01c71a12022-08-31 15:55:00 +0100613 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
614 }
615
Raef Colesf5632d32022-09-01 09:56:52 +0100616 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100617
Raef Colesf5632d32022-09-01 09:56:52 +0100618 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100619 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100620 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100621
Raef Colesad054252022-09-27 10:59:16 +0100622 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
623 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
624 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100625
Raef Colesad054252022-09-27 10:59:16 +0100626 mbedtls_lms_unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ),
627 const_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100628
Raef Colese9479a02022-09-01 16:06:35 +0100629 for ( i_digit_idx = 0;
630 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
631 i_digit_idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100632 {
Raef Coles01c71a12022-08-31 15:55:00 +0100633 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100634 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100635 goto exit;
636
Raef Coles29117d22022-10-07 11:46:06 +0100637 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100638 ctx->params.I_key_identifier,
639 sizeof( ctx->params.I_key_identifier ) );
Raef Coles29117d22022-10-07 11:46:06 +0100640 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100641 goto exit;
642
643 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100644 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100645 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100646 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100647 goto exit;
648
Raef Colesad054252022-09-27 10:59:16 +0100649 mbedtls_lms_unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
650 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100651 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100652 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100653 goto exit;
654
Raef Coles9b88ee52022-09-02 12:04:21 +0100655 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes ) );
Raef Coles29117d22022-10-07 11:46:06 +0100656 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100657 goto exit;
658
659 status = psa_hash_update( &op, seed, seed_size );
Raef Coles29117d22022-10-07 11:46:06 +0100660 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100661 goto exit;
662
663 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100664 ctx->private_key[i_digit_idx],
Raef Colese9479a02022-09-01 16:06:35 +0100665 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
666 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100667 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100668 goto exit;
669
670 psa_hash_abort( &op );
671 }
672
Raef Colesf5632d32022-09-01 09:56:52 +0100673 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100674
675exit:
Raef Coles29117d22022-10-07 11:46:06 +0100676 psa_hash_abort( &op );
Raef Coles01c71a12022-08-31 15:55:00 +0100677
Raef Coles29117d22022-10-07 11:46:06 +0100678 return ( mbedtls_lms_error_from_psa( status ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100679}
680
681int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles2ac352a2022-10-07 11:12:27 +0100682 const mbedtls_lmots_private_t *priv_ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100683{
Raef Colese9479a02022-09-01 16:06:35 +0100684 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 +0100685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
686
Raef Coles8ff6df52021-07-21 12:42:15 +0100687 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100688 if( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100689 {
690 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
691 }
692
Raef Colese9479a02022-09-01 16:06:35 +0100693 ret = hash_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100694 ( unsigned char * )priv_ctx->private_key, NULL,
695 NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100696 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100697 {
698 return( ret );
699 }
700
Raef Colesf5632d32022-09-01 09:56:52 +0100701 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100702 ( unsigned char * )y_hashed_digits,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100703 ctx->public_key );
Raef Colese0a17612022-09-02 16:04:47 +0100704 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100705 {
706 return( ret );
707 }
708
Raef Colesf5632d32022-09-01 09:56:52 +0100709 memcpy( &ctx->params, &priv_ctx->params,
710 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100711
Raef Coles9b88ee52022-09-02 12:04:21 +0100712 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100713
714 return( ret );
715}
716
Raef Coles01c71a12022-08-31 15:55:00 +0100717int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
718 int (*f_rng)(void *, unsigned char *, size_t),
719 void *p_rng, const unsigned char *msg, size_t msg_size,
720 unsigned char *sig, size_t sig_size, size_t* sig_len )
721{
Raef Colese9479a02022-09-01 16:06:35 +0100722 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100723 /* Create a temporary buffer to prepare the signature in. This allows us to
724 * finish creating a signature (ensuring the process doesn't fail), and then
725 * erase the private key **before** writing any data into the sig parameter
726 * buffer. If data were directly written into the sig buffer, it might leak
727 * a partial signature on failure, which effectively compromises the private
728 * key.
729 */
Raef Colese9479a02022-09-01 16:06:35 +0100730 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Colesd48f7e92022-10-10 13:10:07 +0100731 unsigned char tmp_c_random[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
733
Raef Colese0a17612022-09-02 16:04:47 +0100734 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100735 {
736 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
737 }
738
Raef Colese9479a02022-09-01 16:06:35 +0100739 if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100740 {
741 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
742 }
743
744 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100745 if( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100746 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100747 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100748 }
749
Raef Coles366d67d2022-09-01 17:23:12 +0100750 ret = f_rng( p_rng, tmp_c_random,
751 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Colese0a17612022-09-02 16:04:47 +0100752 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100753 {
754 return( ret );
755 }
756
Raef Colesf5632d32022-09-01 09:56:52 +0100757 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100758 msg, msg_size,
Raef Coles891c6132022-09-01 11:05:48 +0100759 tmp_c_random,
Raef Coles01c71a12022-08-31 15:55:00 +0100760 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100761 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100762 {
763 return( ret );
764 }
765
Raef Coles9b88ee52022-09-02 12:04:21 +0100766 ret = hash_digit_array( &ctx->params, ( unsigned char * )ctx->private_key,
767 NULL, tmp_digit_array, ( unsigned char * )tmp_sig );
Raef Colese0a17612022-09-02 16:04:47 +0100768 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100769 {
770 return( ret );
771 }
772
Raef Colesad054252022-09-27 10:59:16 +0100773 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
774 MBEDTLS_LMOTS_TYPE_LEN,
775 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100776
Raef Coles9c9027b2022-09-02 18:26:31 +0100777 /* Test hook to check if sig is being written to before we invalidate the
778 * private key.
779 */
780#if defined(MBEDTLS_TEST_HOOKS)
781 if( mbedtls_lmots_sign_private_key_invalidated_hook != NULL )
782 {
783 ret = ( *mbedtls_lmots_sign_private_key_invalidated_hook )( sig );
784 if( ret != 0 )
785 return( ret );
786 }
787#endif /* defined(MBEDTLS_TEST_HOOKS) */
788
Raef Coles8ff6df52021-07-21 12:42:15 +0100789 /* We've got a valid signature now, so it's time to make sure the private
790 * key can't be reused.
791 */
Raef Colesf5632d32022-09-01 09:56:52 +0100792 ctx->have_private_key = 0;
Raef Coles9b88ee52022-09-02 12:04:21 +0100793 mbedtls_platform_zeroize( ctx->private_key,
794 sizeof( ctx->private_key ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100795
Raef Coles891c6132022-09-01 11:05:48 +0100796 memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
Raef Colese9479a02022-09-01 16:06:35 +0100797 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) );
Raef Coles891c6132022-09-01 11:05:48 +0100798
Raef Colese9479a02022-09-01 16:06:35 +0100799 memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
800 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
801 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100802
Raef Coles01c71a12022-08-31 15:55:00 +0100803 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100804 {
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
808 return( 0 );
809}
810
Raef Coles5127e852022-10-07 10:35:56 +0100811#endif /* defined(MBEDTLS_LMS_PRIVATE) */
812#endif /* defined(MBEDTLS_LMS_C) */