blob: 055db8fb3005a34711bc885afc36ba631db98801 [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 Colesc8f96042022-08-25 13:49:54 +0100155 psa_hash_operation_t op;
156 psa_status_t status;
157 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 Coles9b88ee52022-09-02 12:04:21 +0100161 op = psa_hash_operation_init( );
Raef Colesc8f96042022-08-25 13:49:54 +0100162 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
163 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100164 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100165 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100166
Raef Colesf5632d32022-09-01 09:56:52 +0100167 status = psa_hash_update( &op, params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100168 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100169 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100170 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100171 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100172
Raef Colesf5632d32022-09-01 09:56:52 +0100173 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100174 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100175 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100176 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100177 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100178
Raef Coles01c71a12022-08-31 15:55:00 +0100179 status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100180 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100181 if( ret != 0 )
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_update( &op, C_random_value,
185 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100186 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100187 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100188 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100189
Raef Colesc8f96042022-08-25 13:49:54 +0100190 status = psa_hash_update( &op, msg, msg_len );
191 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100192 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100193 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100194
Raef Colese9479a02022-09-01 16:06:35 +0100195 status = psa_hash_finish( &op, out,
Raef Colesfa24f9d2022-09-02 17:46:52 +0100196 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colesc8f96042022-08-25 13:49:54 +0100197 &output_hash_len );
198 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100199 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100200 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100201
Raef Colese9479a02022-09-01 16:06:35 +0100202 checksum = lmots_checksum_calculate( params, out );
Raef Colesad054252022-09-27 10:59:16 +0100203 mbedtls_lms_unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN,
204 out + MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100205
Raef Coles01c71a12022-08-31 15:55:00 +0100206exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100207 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100208
209 return( ret );
210}
211
Raef Coles0a967cc2022-09-02 17:46:15 +0100212/* Hash each element of the string of digits (+ checksum), producing a hash
213 * output for each element. This is used in several places (by varying the
214 * hash_idx_min/max_values) in order to calculate a public key from a private
215 * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554
216 * Algorithm 3 step 5), and to calculate a public key candidate from a
217 * signature and message (RFC8554 Algorithm 4b step 3).
218 *
Raef Coles98d6e222022-09-23 09:04:04 +0100219 * params The LMOTS parameter set, I and q values which
220 * describe the key being used.
Raef Coles0a967cc2022-09-02 17:46:15 +0100221 *
Raef Coles98d6e222022-09-23 09:04:04 +0100222 * x_digit_array The array of digits (of size P, 34 in the case of
223 * MBEDTLS_LMOTS_SHA256_N32_W8).
Raef Coles0a967cc2022-09-02 17:46:15 +0100224 *
Raef Coles98d6e222022-09-23 09:04:04 +0100225 * hash_idx_min_values An array of the starting values of the j iterator
226 * for each of the members of the digit array. If
227 * this value in NULL, then all iterators will start
228 * at 0.
Raef Coles0a967cc2022-09-02 17:46:15 +0100229 *
Raef Coles98d6e222022-09-23 09:04:04 +0100230 * hash_idx_max_values An array of the upper bound values of the j
231 * iterator for each of the members of the digit
232 * array. If this value in NULL, then iterator is
233 * bounded to be less than 2^w - 1 (255 in the case
234 * of MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100235 *
Raef Coles98d6e222022-09-23 09:04:04 +0100236 * output An array containing a hash output for each member
237 * of the digit string P. In the case of
238 * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 *
239 * 34.
Raef Coles0a967cc2022-09-02 17:46:15 +0100240 */
Raef Coles01c71a12022-08-31 15:55:00 +0100241static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100242 const unsigned char *x_digit_array,
Raef Coles01c71a12022-08-31 15:55:00 +0100243 const unsigned char *hash_idx_min_values,
244 const unsigned char *hash_idx_max_values,
Raef Colese9479a02022-09-01 16:06:35 +0100245 unsigned char *output )
Raef Coles8ff6df52021-07-21 12:42:15 +0100246{
Raef Coles8738a492022-09-02 17:13:01 +0100247 unsigned int i_digit_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100248 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8738a492022-09-02 17:13:01 +0100249 unsigned int j_hash_idx;
250 unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100251 unsigned int j_hash_idx_min;
252 unsigned int j_hash_idx_max;
Raef Colesc8f96042022-08-25 13:49:54 +0100253 psa_hash_operation_t op;
254 psa_status_t status;
255 size_t output_hash_len;
Raef Colese9479a02022-09-01 16:06:35 +0100256 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
258
Raef Coles9b88ee52022-09-02 12:04:21 +0100259 op = psa_hash_operation_init( );
Raef Coles01c71a12022-08-31 15:55:00 +0100260
Raef Colese9479a02022-09-01 16:06:35 +0100261 for ( i_digit_idx = 0;
262 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
263 i_digit_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100264 {
265
Raef Coles366d67d2022-09-01 17:23:12 +0100266 memcpy( tmp_hash,
267 &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
Raef Colese9479a02022-09-01 16:06:35 +0100268 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100269
Raef Coles366d67d2022-09-01 17:23:12 +0100270 j_hash_idx_min = hash_idx_min_values != NULL ?
271 hash_idx_min_values[i_digit_idx] : 0;
272 j_hash_idx_max = hash_idx_max_values != NULL ?
273 hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100274
Raef Coles8738a492022-09-02 17:13:01 +0100275 for ( j_hash_idx = j_hash_idx_min;
Raef Coles366d67d2022-09-01 17:23:12 +0100276 j_hash_idx < j_hash_idx_max;
277 j_hash_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100278 {
Raef Colesc8f96042022-08-25 13:49:54 +0100279 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
280 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100281 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100282 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100283
Raef Coles01c71a12022-08-31 15:55:00 +0100284 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100285 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100286 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100287 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100288 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100289 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100290
Raef Coles01c71a12022-08-31 15:55:00 +0100291 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100292 params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100293 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100294 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100295 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100296 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100297
Raef Colesad054252022-09-27 10:59:16 +0100298 mbedtls_lms_unsigned_int_to_network_bytes( i_digit_idx,
299 I_DIGIT_IDX_LEN,
300 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100301 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100302 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100303 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100304 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100305
Raef Colesad054252022-09-27 10:59:16 +0100306 mbedtls_lms_unsigned_int_to_network_bytes( j_hash_idx,
307 J_HASH_IDX_LEN,
308 j_hash_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100309 status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN );
310 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100311 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100312 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100313
Raef Colese9479a02022-09-01 16:06:35 +0100314 status = psa_hash_update( &op, tmp_hash,
315 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100316 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100317 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100318 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100319
Raef Coles366d67d2022-09-01 17:23:12 +0100320 status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ),
321 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100322 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100323 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100324 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100325
Raef Colesc8f96042022-08-25 13:49:54 +0100326 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100327 }
328
Raef Coles366d67d2022-09-01 17:23:12 +0100329 memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
330 tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100331 }
332
Raef Coles01c71a12022-08-31 15:55:00 +0100333exit:
Raef Colese0a17612022-09-02 16:04:47 +0100334 if( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100335 {
Raef Colesc8f96042022-08-25 13:49:54 +0100336 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100337 return( ret );
338 }
339
Raef Coles01c71a12022-08-31 15:55:00 +0100340 mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) );
341
Raef Coles8ff6df52021-07-21 12:42:15 +0100342 return ret;
343}
344
Raef Coles0a967cc2022-09-02 17:46:15 +0100345/* Combine the hashes of the digit array into a public key. This is used in
346 * in order to calculate a public key from a private key (RFC8554 Algorithm 1
347 * step 4), and to calculate a public key candidate from a signature and message
348 * (RFC8554 Algorithm 4b step 3).
349 *
Raef Coles98d6e222022-09-23 09:04:04 +0100350 * params The LMOTS parameter set, I and q values which describe
351 * the key being used.
352 * y_hashed_digits The array of hashes, one hash for each digit of the
353 * symbol array (which is of size P, 34 in the case of
354 * MBEDTLS_LMOTS_SHA256_N32_W8)
Raef Coles0a967cc2022-09-02 17:46:15 +0100355 *
Raef Coles98d6e222022-09-23 09:04:04 +0100356 * pub_key The output public key (or candidate public key in
357 * case this is being run as part of signature
358 * verification), in the form of a hash output.
Raef Coles0a967cc2022-09-02 17:46:15 +0100359 */
Raef Coles01c71a12022-08-31 15:55:00 +0100360static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
Raef Colese9479a02022-09-01 16:06:35 +0100361 const unsigned char *y_hashed_digits,
Raef Coles01c71a12022-08-31 15:55:00 +0100362 unsigned char *pub_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100363{
Raef Colesc8f96042022-08-25 13:49:54 +0100364 psa_hash_operation_t op;
365 psa_status_t status;
366 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
368
Raef Colesc8f96042022-08-25 13:49:54 +0100369 op = psa_hash_operation_init( );
370 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
371 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100372 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100373 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100374
Raef Coles01c71a12022-08-31 15:55:00 +0100375 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100376 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100377 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100378 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100379 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100380 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100381
Raef Colesf5632d32022-09-01 09:56:52 +0100382 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100383 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100384 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100385 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100386 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100387
Raef Coles01c71a12022-08-31 15:55:00 +0100388 status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100389 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100390 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100391 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100392
Raef Colese9479a02022-09-01 16:06:35 +0100393 status = psa_hash_update( &op, y_hashed_digits,
394 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
395 MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
Raef Colesc8f96042022-08-25 13:49:54 +0100396 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100397 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100398 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100399
Raef Coles366d67d2022-09-01 17:23:12 +0100400 status = psa_hash_finish( &op, pub_key,
401 MBEDTLS_LMOTS_N_HASH_LEN(params->type),
Raef Colese9479a02022-09-01 16:06:35 +0100402 &output_hash_len );
Raef Colesc8f96042022-08-25 13:49:54 +0100403 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100404
Raef Coles01c71a12022-08-31 15:55:00 +0100405exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100406 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100407 return( ret );
408}
409
Raef Coles9b88ee52022-09-02 12:04:21 +0100410int mbedtls_lms_error_from_psa( psa_status_t status )
Raef Colesc8f96042022-08-25 13:49:54 +0100411{
Raef Coles9b88ee52022-09-02 12:04:21 +0100412 switch( status )
413 {
Raef Colesc8f96042022-08-25 13:49:54 +0100414 case PSA_SUCCESS:
415 return( 0 );
416 case PSA_ERROR_HARDWARE_FAILURE:
417 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
418 case PSA_ERROR_NOT_SUPPORTED:
419 return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
420 case PSA_ERROR_BUFFER_TOO_SMALL:
421 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
422 case PSA_ERROR_INVALID_ARGUMENT:
423 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
424 default:
425 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
426 }
427}
428
Raef Coles01c71a12022-08-31 15:55:00 +0100429void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100430{
Raef Coles01c71a12022-08-31 15:55:00 +0100431 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100432}
433
Raef Coles01c71a12022-08-31 15:55:00 +0100434void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100435{
Raef Coles01c71a12022-08-31 15:55:00 +0100436 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100437}
438
Raef Coles01c71a12022-08-31 15:55:00 +0100439int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
440 const unsigned char *key, size_t key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100441{
Raef Colesf5632d32022-09-01 09:56:52 +0100442 ctx->params.type =
Raef Colesad054252022-09-27 10:59:16 +0100443 mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
444 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100445
Raef Colese0a17612022-09-02 16:04:47 +0100446 if( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Colese9479a02022-09-01 16:06:35 +0100447 {
448 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
449 }
450
Raef Colesf5632d32022-09-01 09:56:52 +0100451 memcpy( ctx->params.I_key_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100452 key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100453 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100454
Raef Colesf5632d32022-09-01 09:56:52 +0100455 memcpy( ctx->params.q_leaf_identifier,
Raef Coles57d53282022-09-27 11:30:51 +0100456 key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
Raef Coles366d67d2022-09-01 17:23:12 +0100457 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100458
Raef Colesf5632d32022-09-01 09:56:52 +0100459 memcpy( ctx->public_key,
Raef Coles57d53282022-09-27 11:30:51 +0100460 key + PUBLIC_KEY_KEY_HASH_OFFSET,
Raef Colese9479a02022-09-01 16:06:35 +0100461 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100462
Raef Colesf5632d32022-09-01 09:56:52 +0100463 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100464
465 return( 0 );
466}
467
Raef Coles01c71a12022-08-31 15:55:00 +0100468int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
469 const unsigned char *msg,
470 size_t msg_size,
471 const unsigned char *sig,
472 size_t sig_size,
473 unsigned char *out,
474 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100475 size_t *out_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100476{
Raef Colese9479a02022-09-01 16:06:35 +0100477 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
478 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 +0100479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
480
Raef Colese0a17612022-09-02 16:04:47 +0100481 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100482 {
483 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
484 }
485
Raef Colese0a17612022-09-02 16:04:47 +0100486 if( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
Raef Colese9479a02022-09-01 16:06:35 +0100487 out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type) )
Raef Coles8ff6df52021-07-21 12:42:15 +0100488 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100489 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100490 }
491
Raef Coles01c71a12022-08-31 15:55:00 +0100492 ret = create_digit_array_with_checksum( params, msg, msg_size,
493 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
494 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100495 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100496 {
497 return ( ret );
498 }
499
Raef Coles01c71a12022-08-31 15:55:00 +0100500 ret = hash_digit_array( params,
Raef Colese9479a02022-09-01 16:06:35 +0100501 sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100502 tmp_digit_array, NULL, ( unsigned char * )y_hashed_digits );
Raef Colese0a17612022-09-02 16:04:47 +0100503 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100504 {
505 return ( ret );
506 }
507
Raef Colese9479a02022-09-01 16:06:35 +0100508 ret = public_key_from_hashed_digit_array( params,
Raef Coles9b88ee52022-09-02 12:04:21 +0100509 ( unsigned char * )y_hashed_digits,
Raef Colese9479a02022-09-01 16:06:35 +0100510 out );
Raef Colese0a17612022-09-02 16:04:47 +0100511 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100512 {
513 return ( ret );
514 }
515
Raef Colese0a17612022-09-02 16:04:47 +0100516 if( out_len != NULL )
Raef Coles01c71a12022-08-31 15:55:00 +0100517 {
Raef Colese9479a02022-09-01 16:06:35 +0100518 *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
Raef Coles01c71a12022-08-31 15:55:00 +0100519 }
520
Raef Coles8ff6df52021-07-21 12:42:15 +0100521 return( 0 );
522}
523
Raef Coles2ac352a2022-10-07 11:12:27 +0100524int mbedtls_lmots_verify( const mbedtls_lmots_public_t *ctx,
525 const unsigned char *msg, size_t msg_size,
526 const unsigned char *sig, size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100527{
Raef Colese9479a02022-09-01 16:06:35 +0100528 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles8ff6df52021-07-21 12:42:15 +0100529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
530
Raef Colese0a17612022-09-02 16:04:47 +0100531 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100532 {
533 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
534 }
535
Raef Colese0a17612022-09-02 16:04:47 +0100536 if( !ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100537 {
538 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
539 }
540
Raef Coles9b88ee52022-09-02 12:04:21 +0100541 if( ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100542 {
543 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
544 }
545
Raef Colesad054252022-09-27 10:59:16 +0100546 if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
Raef Coles366d67d2022-09-01 17:23:12 +0100547 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles01c71a12022-08-31 15:55:00 +0100548 {
549 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
550 }
551
Raef Colesf5632d32022-09-01 09:56:52 +0100552 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100553 msg, msg_size, sig, sig_size,
554 Kc_public_key_candidate,
Raef Colese9479a02022-09-01 16:06:35 +0100555 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
Raef Coles9b88ee52022-09-02 12:04:21 +0100556 NULL );
Raef Colese0a17612022-09-02 16:04:47 +0100557 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100558 {
559 return( ret );
560 }
561
Raef Colese0a17612022-09-02 16:04:47 +0100562 if( memcmp( &Kc_public_key_candidate, ctx->public_key,
Raef Colesf5632d32022-09-01 09:56:52 +0100563 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100564 {
565 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
566 }
567
568 return( 0 );
569}
570
Raef Coles5127e852022-10-07 10:35:56 +0100571#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100572
Raef Coles01c71a12022-08-31 15:55:00 +0100573void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx )
574{
575 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
576}
577
578void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx )
579{
580 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
581}
582
583int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
584 mbedtls_lmots_algorithm_type_t type,
585 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
586 uint32_t q_leaf_identifier,
587 const unsigned char *seed,
588 size_t seed_size )
589{
590 psa_hash_operation_t op;
591 psa_status_t status;
592 size_t output_hash_len;
593 unsigned int i_digit_idx;
594 unsigned char i_digit_idx_bytes[2];
595 unsigned char const_bytes[1];
596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
597
Raef Colese0a17612022-09-02 16:04:47 +0100598 if( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100599 {
600 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
601 }
602
Raef Colese0a17612022-09-02 16:04:47 +0100603 if( type != MBEDTLS_LMOTS_SHA256_N32_W8 )
Raef Coles9b88ee52022-09-02 12:04:21 +0100604 {
Raef Coles01c71a12022-08-31 15:55:00 +0100605 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
606 }
607
Raef Colesebd35b52022-09-01 11:52:17 +0100608 op = psa_hash_operation_init( );
609
Raef Colesf5632d32022-09-01 09:56:52 +0100610 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100611
Raef Colesf5632d32022-09-01 09:56:52 +0100612 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100613 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100614 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100615
Raef Colesad054252022-09-27 10:59:16 +0100616 mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
617 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
618 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100619
Raef Colesad054252022-09-27 10:59:16 +0100620 mbedtls_lms_unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ),
621 const_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100622
Raef Colese9479a02022-09-01 16:06:35 +0100623 for ( i_digit_idx = 0;
624 i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
625 i_digit_idx++ )
Raef Coles01c71a12022-08-31 15:55:00 +0100626 {
Raef Coles01c71a12022-08-31 15:55:00 +0100627 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
628 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100629 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100630 goto exit;
631
632 ret = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100633 ctx->params.I_key_identifier,
634 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100635 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100636 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100637 goto exit;
638
639 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100640 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100641 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
642 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100643 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100644 goto exit;
645
Raef Colesad054252022-09-27 10:59:16 +0100646 mbedtls_lms_unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN,
647 i_digit_idx_bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100648 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
649 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100650 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100651 goto exit;
652
Raef Coles9b88ee52022-09-02 12:04:21 +0100653 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100654 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100655 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100656 goto exit;
657
658 status = psa_hash_update( &op, seed, seed_size );
659 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100660 if( ret )
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 Coles01c71a12022-08-31 15:55:00 +0100667 ret = mbedtls_lms_error_from_psa( status );
Raef Colese0a17612022-09-02 16:04:47 +0100668 if( ret )
Raef Coles01c71a12022-08-31 15:55:00 +0100669 goto exit;
670
671 psa_hash_abort( &op );
672 }
673
Raef Colesf5632d32022-09-01 09:56:52 +0100674 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100675
676exit:
Raef Colese0a17612022-09-02 16:04:47 +0100677 if( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100678 {
679 psa_hash_abort( &op );
680 return( ret );
681 }
682
683 return ret;
684}
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 {
703 return( ret );
704 }
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 {
711 return( ret );
712 }
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
719 return( ret );
720}
721
722
Raef Coles2ac352a2022-10-07 11:12:27 +0100723int mbedtls_lmots_export_public_key( const mbedtls_lmots_public_t *ctx,
Raef Coles01c71a12022-08-31 15:55:00 +0100724 unsigned char *key, size_t key_size,
725 size_t *key_len )
726{
Raef Colese9479a02022-09-01 16:06:35 +0100727 if( key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100728 {
729 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
730 }
731
Raef Colesf5632d32022-09-01 09:56:52 +0100732 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100733 {
734 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
735 }
736
Raef Colesad054252022-09-27 10:59:16 +0100737 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
738 MBEDTLS_LMOTS_TYPE_LEN,
739 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles01c71a12022-08-31 15:55:00 +0100740
Raef Coles57d53282022-09-27 11:30:51 +0100741 memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100742 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100743 MBEDTLS_LMOTS_I_KEY_ID_LEN );
744
Raef Coles57d53282022-09-27 11:30:51 +0100745 memcpy( key + PUBLIC_KEY_Q_LEAF_ID_OFFSET,
Raef Coles9b88ee52022-09-02 12:04:21 +0100746 ctx->params.q_leaf_identifier,
747 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Coles01c71a12022-08-31 15:55:00 +0100748
Raef Coles57d53282022-09-27 11:30:51 +0100749 memcpy( key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
Raef Colese9479a02022-09-01 16:06:35 +0100750 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles01c71a12022-08-31 15:55:00 +0100751
752 if( key_len != NULL )
753 {
Raef Colese9479a02022-09-01 16:06:35 +0100754 *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
Raef Coles01c71a12022-08-31 15:55:00 +0100755 }
756
757 return( 0 );
758}
759
760int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
761 int (*f_rng)(void *, unsigned char *, size_t),
762 void *p_rng, const unsigned char *msg, size_t msg_size,
763 unsigned char *sig, size_t sig_size, size_t* sig_len )
764{
Raef Colese9479a02022-09-01 16:06:35 +0100765 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
Raef Coles891c6132022-09-01 11:05:48 +0100766 /* Create a temporary buffer to prepare the signature in. This allows us to
767 * finish creating a signature (ensuring the process doesn't fail), and then
768 * erase the private key **before** writing any data into the sig parameter
769 * buffer. If data were directly written into the sig buffer, it might leak
770 * a partial signature on failure, which effectively compromises the private
771 * key.
772 */
Raef Colese9479a02022-09-01 16:06:35 +0100773 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles57d53282022-09-27 11:30:51 +0100774 unsigned char tmp_c_random[C_RANDOM_VALUE_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
776
Raef Colese0a17612022-09-02 16:04:47 +0100777 if( msg == NULL && msg_size != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100778 {
779 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
780 }
781
Raef Colese9479a02022-09-01 16:06:35 +0100782 if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) )
Raef Coles01c71a12022-08-31 15:55:00 +0100783 {
784 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
785 }
786
787 /* Check that a private key is loaded */
Raef Colese0a17612022-09-02 16:04:47 +0100788 if( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100789 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100790 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100791 }
792
Raef Coles366d67d2022-09-01 17:23:12 +0100793 ret = f_rng( p_rng, tmp_c_random,
794 MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Colese0a17612022-09-02 16:04:47 +0100795 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100796 {
797 return( ret );
798 }
799
Raef Colesf5632d32022-09-01 09:56:52 +0100800 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100801 msg, msg_size,
Raef Coles891c6132022-09-01 11:05:48 +0100802 tmp_c_random,
Raef Coles01c71a12022-08-31 15:55:00 +0100803 tmp_digit_array );
Raef Colese0a17612022-09-02 16:04:47 +0100804 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100805 {
806 return( ret );
807 }
808
Raef Coles9b88ee52022-09-02 12:04:21 +0100809 ret = hash_digit_array( &ctx->params, ( unsigned char * )ctx->private_key,
810 NULL, tmp_digit_array, ( unsigned char * )tmp_sig );
Raef Colese0a17612022-09-02 16:04:47 +0100811 if( ret )
Raef Coles8ff6df52021-07-21 12:42:15 +0100812 {
813 return( ret );
814 }
815
Raef Colesad054252022-09-27 10:59:16 +0100816 mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
817 MBEDTLS_LMOTS_TYPE_LEN,
818 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100819
Raef Coles9c9027b2022-09-02 18:26:31 +0100820 /* Test hook to check if sig is being written to before we invalidate the
821 * private key.
822 */
823#if defined(MBEDTLS_TEST_HOOKS)
824 if( mbedtls_lmots_sign_private_key_invalidated_hook != NULL )
825 {
826 ret = ( *mbedtls_lmots_sign_private_key_invalidated_hook )( sig );
827 if( ret != 0 )
828 return( ret );
829 }
830#endif /* defined(MBEDTLS_TEST_HOOKS) */
831
Raef Coles8ff6df52021-07-21 12:42:15 +0100832 /* We've got a valid signature now, so it's time to make sure the private
833 * key can't be reused.
834 */
Raef Colesf5632d32022-09-01 09:56:52 +0100835 ctx->have_private_key = 0;
Raef Coles9b88ee52022-09-02 12:04:21 +0100836 mbedtls_platform_zeroize( ctx->private_key,
837 sizeof( ctx->private_key ) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100838
Raef Coles891c6132022-09-01 11:05:48 +0100839 memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
Raef Colese9479a02022-09-01 16:06:35 +0100840 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) );
Raef Coles891c6132022-09-01 11:05:48 +0100841
Raef Colese9479a02022-09-01 16:06:35 +0100842 memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
843 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
844 * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
Raef Coles8ff6df52021-07-21 12:42:15 +0100845
Raef Coles01c71a12022-08-31 15:55:00 +0100846 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100847 {
Raef Colese9479a02022-09-01 16:06:35 +0100848 *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
Raef Coles8ff6df52021-07-21 12:42:15 +0100849 }
850
851 return( 0 );
852}
853
Raef Coles5127e852022-10-07 10:35:56 +0100854#endif /* defined(MBEDTLS_LMS_PRIVATE) */
855#endif /* defined(MBEDTLS_LMS_C) */