blob: 788063c549bb55a216af2ef91631a492ca160ef9 [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 Coles9fc303a2022-10-12 10:32:15 +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 Coles48294592022-10-10 16:40:00 +0100555 if( sig_size < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN )
556 {
557 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
558 }
559
Raef Colesad054252022-09-27 10:59:16 +0100560 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100561 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100562 {
563 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
564 }
565
Raef Colesf5632d32022-09-01 09:56:52 +0100566 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100567 msg, msg_size, sig, sig_size,
568 Kc_public_key_candidate,
Raef Colese9479a02022-09-01 16:06:35 +0100569 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100570 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100571 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100572 {
Raef Colesfaf59ba2022-10-10 15:40:56 +0100573 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
Raef Coles01c71a12022-08-31 15:55:00 +0100574 }
575
Raef Colese0a17612022-09-02 16:04:47 +0100576 if( memcmp( &Kc_public_key_candidate, ctx->public_key,
Raef Colesf5632d32022-09-01 09:56:52 +0100577 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100578 {
579 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
580 }
581
582 return( 0 );
583}
584
Raef Coles5127e852022-10-07 10:35:56 +0100585#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100586
Raef Colesbe3bdd82022-10-07 12:04:24 +0100587void mbedtls_lmots_private_init( mbedtls_lmots_private_t *ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100588{
Raef Colesfbd60ec2022-10-10 15:09:33 +0100589 memset( ctx, 0, sizeof( *ctx ) ) ;
Raef Coles01c71a12022-08-31 15:55:00 +0100590}
591
Raef Colesbe3bdd82022-10-07 12:04:24 +0100592void mbedtls_lmots_private_free( mbedtls_lmots_private_t *ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100593{
Raef Colesbe3bdd82022-10-07 12:04:24 +0100594 mbedtls_platform_zeroize( ctx, sizeof( *ctx ) ) ;
Raef Coles01c71a12022-08-31 15:55:00 +0100595}
596
597int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
598 mbedtls_lmots_algorithm_type_t type,
599 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
600 uint32_t q_leaf_identifier,
601 const unsigned char *seed,
602 size_t seed_size )
603{
Raef Colesbe0c2f92022-10-07 11:27:35 +0100604 psa_hash_operation_t op = PSA_HASH_OPERATION_INIT;
605 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Raef Coles01c71a12022-08-31 15:55:00 +0100606 size_t output_hash_len;
607 unsigned int i_digit_idx;
608 unsigned char i_digit_idx_bytes[2];
609 unsigned char const_bytes[1];
Raef Coles01c71a12022-08-31 15:55:00 +0100610
Raef Colese0a17612022-09-02 16:04:47 +0100611 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100612 {
613 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
614 }
615
Raef Colese0a17612022-09-02 16:04:47 +0100616 if( type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles9b88ee52022-09-02 12:04:21 +0100617 {
Raef Coles01c71a12022-08-31 15:55:00 +0100618 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
619 }
620
Raef Colesf5632d32022-09-01 09:56:52 +0100621 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100622
Raef Colesf5632d32022-09-01 09:56:52 +0100623 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100624 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100625 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100626
Raef Colesad054252022-09-27 10:59:16 +0100627 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
628 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
629 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100630
Raef Colesad054252022-09-27 10:59:16 +0100631 mbedtls_lms_unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ),
632 const_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100633
Raef Colese9479a02022-09-01 16:06:35 +0100634 for ( i_digit_idx = 0;
635 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
636 i_digit_idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100637 {
Raef Coles01c71a12022-08-31 15:55:00 +0100638 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
Raef Coles29117d22022-10-07 11:46:06 +0100639 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100640 goto exit;
641
Raef Coles29117d22022-10-07 11:46:06 +0100642 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100643 ctx->params.I_key_identifier,
644 sizeof( ctx->params.I_key_identifier ) );
Raef Coles29117d22022-10-07 11:46:06 +0100645 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100646 goto exit;
647
648 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100649 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100650 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100651 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100652 goto exit;
653
Raef Colesad054252022-09-27 10:59:16 +0100654 mbedtls_lms_unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
655 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100656 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Coles29117d22022-10-07 11:46:06 +0100657 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100658 goto exit;
659
Raef Coles9b88ee52022-09-02 12:04:21 +0100660 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes ) );
Raef Coles29117d22022-10-07 11:46:06 +0100661 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100662 goto exit;
663
664 status = psa_hash_update( &op, seed, seed_size );
Raef Coles29117d22022-10-07 11:46:06 +0100665 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100666 goto exit;
667
668 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100669 ctx->private_key[i_digit_idx],
Raef Colese9479a02022-09-01 16:06:35 +0100670 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
671 &output_hash_len );
Raef Coles29117d22022-10-07 11:46:06 +0100672 if( status != PSA_SUCCESS )
Raef Coles01c71a12022-08-31 15:55:00 +0100673 goto exit;
674
675 psa_hash_abort( &op );
676 }
677
Raef Colesf5632d32022-09-01 09:56:52 +0100678 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100679
680exit:
Raef Coles29117d22022-10-07 11:46:06 +0100681 psa_hash_abort( &op );
Raef Coles01c71a12022-08-31 15:55:00 +0100682
Raef Coles29117d22022-10-07 11:46:06 +0100683 return ( mbedtls_lms_error_from_psa( status ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100684}
685
686int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles2ac352a2022-10-07 11:12:27 +0100687 const mbedtls_lmots_private_t *priv_ctx )
Raef Coles01c71a12022-08-31 15:55:00 +0100688{
Raef Colese9479a02022-09-01 16:06:35 +0100689 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 +0100690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
691
Raef Coles8ff6df52021-07-21 12:42:15 +0100692 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100693 if( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100694 {
695 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
696 }
697
Raef Colese9479a02022-09-01 16:06:35 +0100698 ret = hash_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100699 ( unsigned char * )priv_ctx->private_key, NULL,
700 NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100701 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100702 {
Raef Coles142e5772022-10-12 10:47:27 +0100703 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100704 }
705
Raef Colesf5632d32022-09-01 09:56:52 +0100706 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100707 ( unsigned char * )y_hashed_digits,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100708 ctx->public_key );
Raef Colese0a17612022-09-02 16:04:47 +0100709 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100710 {
Raef Coles142e5772022-10-12 10:47:27 +0100711 goto exit;
Raef Coles01c71a12022-08-31 15:55:00 +0100712 }
713
Raef Colesf5632d32022-09-01 09:56:52 +0100714 memcpy( &ctx->params, &priv_ctx->params,
715 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100716
Raef Coles9b88ee52022-09-02 12:04:21 +0100717 ctx->have_public_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100718
Raef Coles142e5772022-10-12 10:47:27 +0100719exit:
720 mbedtls_platform_zeroize( y_hashed_digits, sizeof( y_hashed_digits ) );
721
Raef Coles01c71a12022-08-31 15:55:00 +0100722 return( ret );
723}
724
Raef Coles01c71a12022-08-31 15:55:00 +0100725int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
726 int (*f_rng)(void *, unsigned char *, size_t),
727 void *p_rng, const unsigned char *msg, size_t msg_size,
728 unsigned char *sig, size_t sig_size, size_t* sig_len )
729{
Raef Colese9479a02022-09-01 16:06:35 +0100730 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100731 /* Create a temporary buffer to prepare the signature in. This allows us to
732 * finish creating a signature (ensuring the process doesn't fail), and then
733 * erase the private key **before** writing any data into the sig parameter
734 * buffer. If data were directly written into the sig buffer, it might leak
735 * a partial signature on failure, which effectively compromises the private
736 * key.
737 */
Raef Colese9479a02022-09-01 16:06:35 +0100738 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Colesd48f7e92022-10-10 13:10:07 +0100739 unsigned char tmp_c_random[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
741
Raef Colese0a17612022-09-02 16:04:47 +0100742 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100743 {
744 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
745 }
746
Raef Colese9479a02022-09-01 16:06:35 +0100747 if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100748 {
749 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
750 }
751
752 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100753 if( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100754 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100755 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100756 }
757
Raef Coles366d67d2022-09-01 17:23:12 +0100758 ret = f_rng( p_rng, tmp_c_random,
759 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Colese0a17612022-09-02 16:04:47 +0100760 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100761 {
762 return( ret );
763 }
764
Raef Colesf5632d32022-09-01 09:56:52 +0100765 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100766 msg, msg_size,
Raef Coles891c6132022-09-01 11:05:48 +0100767 tmp_c_random,
Raef Coles01c71a12022-08-31 15:55:00 +0100768 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100769 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100770 {
Raef Coles142e5772022-10-12 10:47:27 +0100771 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100772 }
773
Raef Coles9b88ee52022-09-02 12:04:21 +0100774 ret = hash_digit_array( &ctx->params, ( unsigned char * )ctx->private_key,
775 NULL, tmp_digit_array, ( unsigned char * )tmp_sig );
Raef Colese0a17612022-09-02 16:04:47 +0100776 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100777 {
Raef Coles142e5772022-10-12 10:47:27 +0100778 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100779 }
780
Raef Colesad054252022-09-27 10:59:16 +0100781 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
782 MBEDTLS_LMOTS_TYPE_LEN,
783 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100784
Raef Coles9c9027b2022-09-02 18:26:31 +0100785 /* Test hook to check if sig is being written to before we invalidate the
786 * private key.
787 */
788#if defined(MBEDTLS_TEST_HOOKS)
789 if( mbedtls_lmots_sign_private_key_invalidated_hook != NULL )
790 {
791 ret = ( *mbedtls_lmots_sign_private_key_invalidated_hook )( sig );
792 if( ret != 0 )
793 return( ret );
794 }
795#endif /* defined(MBEDTLS_TEST_HOOKS) */
796
Raef Coles8ff6df52021-07-21 12:42:15 +0100797 /* We've got a valid signature now, so it's time to make sure the private
798 * key can't be reused.
799 */
Raef Colesf5632d32022-09-01 09:56:52 +0100800 ctx->have_private_key = 0;
Raef Coles9b88ee52022-09-02 12:04:21 +0100801 mbedtls_platform_zeroize( ctx->private_key,
802 sizeof( ctx->private_key ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100803
Raef Coles891c6132022-09-01 11:05:48 +0100804 memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
Raef Colese9479a02022-09-01 16:06:35 +0100805 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) );
Raef Coles891c6132022-09-01 11:05:48 +0100806
Raef Colese9479a02022-09-01 16:06:35 +0100807 memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
808 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
809 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100810
Raef Coles01c71a12022-08-31 15:55:00 +0100811 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100812 {
Raef Colese9479a02022-09-01 16:06:35 +0100813 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100814 }
815
Raef Coles142e5772022-10-12 10:47:27 +0100816 ret = 0;
817
818exit:
819 mbedtls_platform_zeroize( tmp_digit_array, sizeof( tmp_digit_array ) );
820 mbedtls_platform_zeroize( tmp_sig, sizeof( tmp_sig ) );
821
822 return ( ret );
Raef Coles8ff6df52021-07-21 12:42:15 +0100823}
824
Raef Coles5127e852022-10-07 10:35:56 +0100825#endif /* defined(MBEDTLS_LMS_PRIVATE) */
826#endif /* defined(MBEDTLS_LMS_C) */