blob: bf6644935b38055379965f3ed8575228797f5f68 [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 Coles7dce69a2022-08-24 14:07:06 +010035#ifdef 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 Coles01c71a12022-08-31 15:55:00 +010047#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
48#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN)
49
50#define MBEDTLS_LMOTS_PUBLIC_KEY_TYPE_OFFSET (0)
51#define MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
52#define MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN)
53#define MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
54
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
63#define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
64
Raef Coles8ff6df52021-07-21 12:42:15 +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 Coles01c71a12022-08-31 15:55:00 +010069void unsigned_int_to_network_bytes(unsigned int val, size_t len, unsigned char *bytes)
Raef Coles8ff6df52021-07-21 12:42:15 +010070{
71 size_t idx;
72
73 for (idx = 0; idx < len; idx++) {
74 bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF;
75 }
76}
77
Raef Coles01c71a12022-08-31 15:55:00 +010078unsigned int network_bytes_to_unsigned_int(size_t len, const unsigned char *bytes)
Raef Coles8ff6df52021-07-21 12:42:15 +010079{
80 size_t idx;
81 unsigned int val = 0;
82
83 for (idx = 0; idx < len; idx++) {
84 val |= ((unsigned int)bytes[idx]) << (8 * (len - 1 - idx));
85 }
86
87 return val;
88}
89
Raef Coles01c71a12022-08-31 15:55:00 +010090static unsigned short lmots_checksum_calculate( const unsigned char* digest )
Raef Coles8ff6df52021-07-21 12:42:15 +010091{
92 size_t idx;
Raef Coles01c71a12022-08-31 15:55:00 +010093 unsigned sum = 0;
Raef Coles8ff6df52021-07-21 12:42:15 +010094
95 for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN; idx++ )
96 {
Raef Coles01c71a12022-08-31 15:55:00 +010097 sum += DIGIT_MAX_VALUE - digest[idx];
Raef Coles8ff6df52021-07-21 12:42:15 +010098 }
99
100 return sum;
101}
102
Raef Coles01c71a12022-08-31 15:55:00 +0100103static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *params,
104 const unsigned char *msg,
105 size_t msg_len,
106 const unsigned char C_random_value[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN],
107 unsigned char out[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT] )
Raef Coles8ff6df52021-07-21 12:42:15 +0100108{
Raef Colesc8f96042022-08-25 13:49:54 +0100109 psa_hash_operation_t op;
110 psa_status_t status;
111 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100112 unsigned short checksum;
113 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
114
Raef Colesc8f96042022-08-25 13:49:54 +0100115 op = psa_hash_operation_init();
116 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
117 ret = mbedtls_lms_error_from_psa( status );
118 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100119 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100120
Raef Colesf5632d32022-09-01 09:56:52 +0100121 status = psa_hash_update( &op, params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100122 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100123 ret = mbedtls_lms_error_from_psa( status );
124 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100125 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100126
Raef Colesf5632d32022-09-01 09:56:52 +0100127 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100128 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100129 ret = mbedtls_lms_error_from_psa( status );
130 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100131 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100132
Raef Coles01c71a12022-08-31 15:55:00 +0100133 status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100134 ret = mbedtls_lms_error_from_psa( status );
135 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100136 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100137
Raef Colesc8f96042022-08-25 13:49:54 +0100138 status = psa_hash_update( &op, C_random_value, MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN );
139 ret = mbedtls_lms_error_from_psa( status );
140 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100141 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100142
Raef Colesc8f96042022-08-25 13:49:54 +0100143 status = psa_hash_update( &op, msg, msg_len );
144 ret = mbedtls_lms_error_from_psa( status );
145 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100146 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100147
Raef Coles01c71a12022-08-31 15:55:00 +0100148 status = psa_hash_finish( &op, out, MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT,
Raef Colesc8f96042022-08-25 13:49:54 +0100149 &output_hash_len );
150 ret = mbedtls_lms_error_from_psa( status );
151 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100152 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100153
Raef Coles01c71a12022-08-31 15:55:00 +0100154 checksum = lmots_checksum_calculate( out );
155 unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN, out + MBEDTLS_LMOTS_N_HASH_LEN );
Raef Coles8ff6df52021-07-21 12:42:15 +0100156
Raef Coles01c71a12022-08-31 15:55:00 +0100157exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100158 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100159
160 return( ret );
161}
162
Raef Coles01c71a12022-08-31 15:55:00 +0100163static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
164 const unsigned char x_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN],
165 const unsigned char *hash_idx_min_values,
166 const unsigned char *hash_idx_max_values,
167 unsigned char output[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN] )
Raef Coles8ff6df52021-07-21 12:42:15 +0100168{
Raef Coles01c71a12022-08-31 15:55:00 +0100169 unsigned char i_digit_idx;
Raef Coles8ff6df52021-07-21 12:42:15 +0100170 unsigned char j_hash_idx;
Raef Coles01c71a12022-08-31 15:55:00 +0100171 unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN];
Raef Coles8ff6df52021-07-21 12:42:15 +0100172 unsigned char j_hash_idx_bytes[1];
Raef Coles01c71a12022-08-31 15:55:00 +0100173 /* These can't be unsigned chars, because they are sometimes set to
174 * #DIGIT_MAX_VALUE, which has a value of 256
175 */
176 unsigned int j_hash_idx_min;
177 unsigned int j_hash_idx_max;
Raef Colesc8f96042022-08-25 13:49:54 +0100178 psa_hash_operation_t op;
179 psa_status_t status;
180 size_t output_hash_len;
Raef Coles01c71a12022-08-31 15:55:00 +0100181 unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN];
Raef Coles8ff6df52021-07-21 12:42:15 +0100182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
183
Raef Coles01c71a12022-08-31 15:55:00 +0100184 op = psa_hash_operation_init();
185
186 for ( i_digit_idx = 0; i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT; i_digit_idx++ )
Raef Coles8ff6df52021-07-21 12:42:15 +0100187 {
188
Raef Coles01c71a12022-08-31 15:55:00 +0100189 memcpy( tmp_hash, &x_digit_array[i_digit_idx], MBEDTLS_LMOTS_N_HASH_LEN );
Raef Coles8ff6df52021-07-21 12:42:15 +0100190
Raef Coles01c71a12022-08-31 15:55:00 +0100191 j_hash_idx_min = hash_idx_min_values != NULL ? hash_idx_min_values[i_digit_idx] : 0;
192 j_hash_idx_max = hash_idx_max_values != NULL ? hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
Raef Coles8ff6df52021-07-21 12:42:15 +0100193
194 for ( j_hash_idx = (unsigned char)j_hash_idx_min; j_hash_idx < j_hash_idx_max; j_hash_idx++ )
195 {
Raef Colesc8f96042022-08-25 13:49:54 +0100196 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
197 ret = mbedtls_lms_error_from_psa( status );
198 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100199 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100200
Raef Coles01c71a12022-08-31 15:55:00 +0100201 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100202 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100203 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100204 ret = mbedtls_lms_error_from_psa( status );
205 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100206 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100207
Raef Coles01c71a12022-08-31 15:55:00 +0100208 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100209 params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100210 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100211 ret = mbedtls_lms_error_from_psa( status );
212 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100213 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100214
Raef Coles01c71a12022-08-31 15:55:00 +0100215 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN, i_digit_idx_bytes );
216 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100217 ret = mbedtls_lms_error_from_psa( status );
218 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100219 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100220
Raef Coles01c71a12022-08-31 15:55:00 +0100221 unsigned_int_to_network_bytes( j_hash_idx, J_HASH_IDX_LEN, j_hash_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100222 status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN );
223 ret = mbedtls_lms_error_from_psa( status );
224 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100225 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100226
Raef Colesc8f96042022-08-25 13:49:54 +0100227 status = psa_hash_update( &op, tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN );
228 ret = mbedtls_lms_error_from_psa( status );
229 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100230 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100231
Raef Colesc8f96042022-08-25 13:49:54 +0100232 status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ), &output_hash_len );
233 ret = mbedtls_lms_error_from_psa( status );
234 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100235 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100236
Raef Colesc8f96042022-08-25 13:49:54 +0100237 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100238 }
239
Raef Coles01c71a12022-08-31 15:55:00 +0100240 memcpy( &output[i_digit_idx], tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN );
Raef Coles8ff6df52021-07-21 12:42:15 +0100241 }
242
Raef Coles01c71a12022-08-31 15:55:00 +0100243exit:
Raef Coles8ff6df52021-07-21 12:42:15 +0100244 if( ret )
245 {
Raef Colesc8f96042022-08-25 13:49:54 +0100246 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100247 return( ret );
248 }
249
Raef Coles01c71a12022-08-31 15:55:00 +0100250 mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) );
251
Raef Coles8ff6df52021-07-21 12:42:15 +0100252 return ret;
253}
254
Raef Coles01c71a12022-08-31 15:55:00 +0100255static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
256 const unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN],
257 unsigned char *pub_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100258{
Raef Colesc8f96042022-08-25 13:49:54 +0100259 psa_hash_operation_t op;
260 psa_status_t status;
261 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
263
Raef Colesc8f96042022-08-25 13:49:54 +0100264 op = psa_hash_operation_init( );
265 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
266 ret = mbedtls_lms_error_from_psa( status );
267 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100268 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100269
Raef Coles01c71a12022-08-31 15:55:00 +0100270 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100271 params->I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100272 MBEDTLS_LMOTS_I_KEY_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100273 ret = mbedtls_lms_error_from_psa( status );
274 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100275 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100276
Raef Colesf5632d32022-09-01 09:56:52 +0100277 status = psa_hash_update( &op, params->q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100278 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100279 ret = mbedtls_lms_error_from_psa( status );
280 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100281 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100282
Raef Coles01c71a12022-08-31 15:55:00 +0100283 status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100284 ret = mbedtls_lms_error_from_psa( status );
285 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100286 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100287
Raef Coles01c71a12022-08-31 15:55:00 +0100288 status = psa_hash_update( &op, ( unsigned char * )y_hashed_digits,
289 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN );
Raef Colesc8f96042022-08-25 13:49:54 +0100290 ret = mbedtls_lms_error_from_psa( status );
291 if ( ret != 0 )
Raef Coles01c71a12022-08-31 15:55:00 +0100292 goto exit;
Raef Coles8ff6df52021-07-21 12:42:15 +0100293
Raef Colesc8f96042022-08-25 13:49:54 +0100294 status = psa_hash_finish( &op, pub_key, 32, &output_hash_len );
295 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100296
Raef Coles01c71a12022-08-31 15:55:00 +0100297exit:
Raef Colesc8f96042022-08-25 13:49:54 +0100298 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100299 return( ret );
300}
301
Raef Colesc8f96042022-08-25 13:49:54 +0100302int mbedtls_lms_error_from_psa(psa_status_t status)
303{
304 switch( status ) {
305 case PSA_SUCCESS:
306 return( 0 );
307 case PSA_ERROR_HARDWARE_FAILURE:
308 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
309 case PSA_ERROR_NOT_SUPPORTED:
310 return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
311 case PSA_ERROR_BUFFER_TOO_SMALL:
312 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
313 case PSA_ERROR_INVALID_ARGUMENT:
314 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
315 default:
316 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
317 }
318}
319
Raef Coles01c71a12022-08-31 15:55:00 +0100320void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100321{
Raef Coles01c71a12022-08-31 15:55:00 +0100322 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100323}
324
Raef Coles01c71a12022-08-31 15:55:00 +0100325void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx )
Raef Coles8ff6df52021-07-21 12:42:15 +0100326{
Raef Coles01c71a12022-08-31 15:55:00 +0100327 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ;
Raef Coles8ff6df52021-07-21 12:42:15 +0100328}
329
Raef Coles01c71a12022-08-31 15:55:00 +0100330int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
331 const unsigned char *key, size_t key_len )
Raef Coles8ff6df52021-07-21 12:42:15 +0100332{
Raef Coles01c71a12022-08-31 15:55:00 +0100333 if ( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN )
Raef Coles8ff6df52021-07-21 12:42:15 +0100334 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100335 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100336 }
337
Raef Colesf5632d32022-09-01 09:56:52 +0100338 ctx->params.type =
Raef Coles01c71a12022-08-31 15:55:00 +0100339 network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
340 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
341
Raef Colesf5632d32022-09-01 09:56:52 +0100342 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100343 key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET, MBEDTLS_LMOTS_I_KEY_ID_LEN );
344
Raef Colesf5632d32022-09-01 09:56:52 +0100345 memcpy( ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100346 key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET, MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
347
Raef Colesf5632d32022-09-01 09:56:52 +0100348 memcpy( ctx->public_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100349 key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET,
350 MBEDTLS_LMOTS_N_HASH_LEN );
351
Raef Colesf5632d32022-09-01 09:56:52 +0100352 ctx->have_public_key = 1;
Raef Coles8ff6df52021-07-21 12:42:15 +0100353
354 return( 0 );
355}
356
Raef Coles01c71a12022-08-31 15:55:00 +0100357int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
358 const unsigned char *msg,
359 size_t msg_size,
360 const unsigned char *sig,
361 size_t sig_size,
362 unsigned char *out,
363 size_t out_size,
364 size_t *out_len)
Raef Coles8ff6df52021-07-21 12:42:15 +0100365{
Raef Coles01c71a12022-08-31 15:55:00 +0100366 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT];
367 unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
Raef Coles8ff6df52021-07-21 12:42:15 +0100368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
369
Raef Coles01c71a12022-08-31 15:55:00 +0100370 if ( msg == NULL && msg_size != 0 )
371 {
372 return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
373 }
374
375 if ( sig_size != MBEDTLS_LMOTS_SIG_LEN || out_size < MBEDTLS_LMOTS_N_HASH_LEN )
Raef Coles8ff6df52021-07-21 12:42:15 +0100376 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100377 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100378 }
379
Raef Coles01c71a12022-08-31 15:55:00 +0100380 ret = create_digit_array_with_checksum( params, msg, msg_size,
381 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
382 tmp_digit_array );
Raef Coles8ff6df52021-07-21 12:42:15 +0100383 if ( ret )
384 {
385 return ( ret );
386 }
387
Raef Coles01c71a12022-08-31 15:55:00 +0100388 ret = hash_digit_array( params,
389 ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET),
390 tmp_digit_array, NULL, y_hashed_digits );
Raef Coles8ff6df52021-07-21 12:42:15 +0100391 if ( ret )
392 {
393 return ( ret );
394 }
395
Raef Coles0c88d4e2022-09-01 10:48:32 +0100396 ret = public_key_from_hashed_digit_array( params, y_hashed_digits, out );
Raef Coles8ff6df52021-07-21 12:42:15 +0100397 if ( ret )
398 {
399 return ( ret );
400 }
401
Raef Coles01c71a12022-08-31 15:55:00 +0100402 if ( out_len != NULL )
403 {
404 *out_len = MBEDTLS_LMOTS_N_HASH_LEN;
405 }
406
Raef Coles8ff6df52021-07-21 12:42:15 +0100407 return( 0 );
408}
409
Raef Coles01c71a12022-08-31 15:55:00 +0100410int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
411 size_t msg_size, const unsigned char *sig,
412 size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100413{
Raef Coles01c71a12022-08-31 15:55:00 +0100414 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN];
Raef Coles8ff6df52021-07-21 12:42:15 +0100415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
416
Raef Coles01c71a12022-08-31 15:55:00 +0100417 if ( msg == NULL && msg_size != 0 )
418 {
419 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
420 }
421
Raef Colesf5632d32022-09-01 09:56:52 +0100422 if ( !ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100423 {
424 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
425 }
426
Raef Colesf5632d32022-09-01 09:56:52 +0100427 if( ctx->params.MBEDTLS_PRIVATE( type )
Raef Coles01c71a12022-08-31 15:55:00 +0100428 != MBEDTLS_LMOTS_SHA256_N32_W8 )
429 {
430 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
431 }
432
433 if ( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
434 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
435 {
436 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
437 }
438
Raef Colesf5632d32022-09-01 09:56:52 +0100439 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100440 msg, msg_size, sig, sig_size,
441 Kc_public_key_candidate,
442 MBEDTLS_LMOTS_N_HASH_LEN,
443 NULL);
444 if ( ret )
445 {
446 return( ret );
447 }
448
Raef Colesf5632d32022-09-01 09:56:52 +0100449 if ( memcmp( &Kc_public_key_candidate, ctx->public_key,
450 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100451 {
452 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
453 }
454
455 return( 0 );
456}
457
Raef Colesab4f8742022-09-01 12:24:31 +0100458#ifdef MBEDTLS_LMS_PRIVATE
459
Raef Coles01c71a12022-08-31 15:55:00 +0100460void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx )
461{
462 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
463}
464
465void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx )
466{
467 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
468}
469
470int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
471 mbedtls_lmots_algorithm_type_t type,
472 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
473 uint32_t q_leaf_identifier,
474 const unsigned char *seed,
475 size_t seed_size )
476{
477 psa_hash_operation_t op;
478 psa_status_t status;
479 size_t output_hash_len;
480 unsigned int i_digit_idx;
481 unsigned char i_digit_idx_bytes[2];
482 unsigned char const_bytes[1];
483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
484
Raef Colesf5632d32022-09-01 09:56:52 +0100485 if ( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100486 {
487 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
488 }
489
490 if ( type != MBEDTLS_LMOTS_SHA256_N32_W8 ) {
491 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
492 }
493
Raef Colesebd35b52022-09-01 11:52:17 +0100494 op = psa_hash_operation_init( );
495
Raef Colesf5632d32022-09-01 09:56:52 +0100496 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100497
Raef Colesf5632d32022-09-01 09:56:52 +0100498 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100499 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100500 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100501
502 unsigned_int_to_network_bytes(q_leaf_identifier,
503 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Colesf5632d32022-09-01 09:56:52 +0100504 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100505
506 unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes );
507
508 for ( i_digit_idx = 0; i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT; i_digit_idx++ )
509 {
Raef Coles01c71a12022-08-31 15:55:00 +0100510 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
511 ret = mbedtls_lms_error_from_psa( status );
512 if ( ret != 0 )
513 goto exit;
514
515 ret = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100516 ctx->params.I_key_identifier,
517 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100518 ret = mbedtls_lms_error_from_psa( status );
519 if ( ret )
520 goto exit;
521
522 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100523 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100524 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
525 ret = mbedtls_lms_error_from_psa( status );
526 if ( ret )
527 goto exit;
528
529 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN, i_digit_idx_bytes );
530 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
531 ret = mbedtls_lms_error_from_psa( status );
532 if ( ret )
533 goto exit;
534
535 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes) );
536 ret = mbedtls_lms_error_from_psa( status );
537 if ( ret )
538 goto exit;
539
540 status = psa_hash_update( &op, seed, seed_size );
541 ret = mbedtls_lms_error_from_psa( status );
542 if ( ret )
543 goto exit;
544
545 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100546 ctx->private_key[i_digit_idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100547 32, &output_hash_len );
548 ret = mbedtls_lms_error_from_psa( status );
549 if ( ret )
550 goto exit;
551
552 psa_hash_abort( &op );
553 }
554
Raef Colesf5632d32022-09-01 09:56:52 +0100555 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100556
557exit:
558 if( ret )
559 {
560 psa_hash_abort( &op );
561 return( ret );
562 }
563
564 return ret;
565}
566
567int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
568 mbedtls_lmots_private_t *priv_ctx)
569{
570 unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
572
Raef Coles8ff6df52021-07-21 12:42:15 +0100573 /* Check that a private key is loaded */
Raef Colesf5632d32022-09-01 09:56:52 +0100574 if ( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100575 {
576 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
577 }
578
Raef Coles0c88d4e2022-09-01 10:48:32 +0100579 ret = hash_digit_array( &priv_ctx->params, priv_ctx->private_key, NULL,
580 NULL, y_hashed_digits );
Raef Coles01c71a12022-08-31 15:55:00 +0100581 if ( ret )
582 {
583 return( ret );
584 }
585
Raef Colesf5632d32022-09-01 09:56:52 +0100586 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100587 y_hashed_digits,
588 ctx->public_key );
Raef Coles01c71a12022-08-31 15:55:00 +0100589 if ( ret )
590 {
591 return( ret );
592 }
593
Raef Colesf5632d32022-09-01 09:56:52 +0100594 memcpy( &ctx->params, &priv_ctx->params,
595 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100596
597 ctx->MBEDTLS_PRIVATE(have_public_key = 1);
598
599 return( ret );
600}
601
602
603int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
604 unsigned char *key, size_t key_size,
605 size_t *key_len )
606{
607 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN )
608 {
609 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
610 }
611
Raef Colesf5632d32022-09-01 09:56:52 +0100612 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100613 {
614 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
615 }
616
Raef Colesf5632d32022-09-01 09:56:52 +0100617 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100618 MBEDTLS_LMOTS_TYPE_LEN,
619 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
620
621 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100622 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100623 MBEDTLS_LMOTS_I_KEY_ID_LEN );
624
625 memcpy(key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100626 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100627 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
628
Raef Colesf5632d32022-09-01 09:56:52 +0100629 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100630 MBEDTLS_LMOTS_N_HASH_LEN );
631
632 if( key_len != NULL )
633 {
634 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN;
635 }
636
637 return( 0 );
638}
639
640int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
641 int (*f_rng)(void *, unsigned char *, size_t),
642 void *p_rng, const unsigned char *msg, size_t msg_size,
643 unsigned char *sig, size_t sig_size, size_t* sig_len )
644{
645 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT];
Raef Coles891c6132022-09-01 11:05:48 +0100646 /* Create a temporary buffer to prepare the signature in. This allows us to
647 * finish creating a signature (ensuring the process doesn't fail), and then
648 * erase the private key **before** writing any data into the sig parameter
649 * buffer. If data were directly written into the sig buffer, it might leak
650 * a partial signature on failure, which effectively compromises the private
651 * key.
652 */
Raef Coles01c71a12022-08-31 15:55:00 +0100653 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
Raef Coles891c6132022-09-01 11:05:48 +0100654 unsigned char tmp_c_random[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN];
Raef Coles01c71a12022-08-31 15:55:00 +0100655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
656
657 if ( msg == NULL && msg_size != 0 )
658 {
659 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
660 }
661
662 if( sig_size < MBEDTLS_LMOTS_SIG_LEN )
663 {
664 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
665 }
666
667 /* Check that a private key is loaded */
Raef Colesf5632d32022-09-01 09:56:52 +0100668 if ( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100669 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100670 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100671 }
672
Raef Coles891c6132022-09-01 11:05:48 +0100673 ret = f_rng( p_rng, tmp_c_random, MBEDTLS_LMOTS_N_HASH_LEN );
Raef Coles8ff6df52021-07-21 12:42:15 +0100674 if ( ret )
675 {
676 return( ret );
677 }
678
Raef Colesf5632d32022-09-01 09:56:52 +0100679 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100680 msg, msg_size,
Raef Coles891c6132022-09-01 11:05:48 +0100681 tmp_c_random,
Raef Coles01c71a12022-08-31 15:55:00 +0100682 tmp_digit_array );
Raef Coles8ff6df52021-07-21 12:42:15 +0100683 if ( ret )
684 {
685 return( ret );
686 }
687
Raef Colesf5632d32022-09-01 09:56:52 +0100688 ret = hash_digit_array( &ctx->params,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100689 ctx->private_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100690 NULL, tmp_digit_array, tmp_sig );
Raef Coles8ff6df52021-07-21 12:42:15 +0100691 if ( ret )
692 {
693 return( ret );
694 }
695
Raef Colesf5632d32022-09-01 09:56:52 +0100696 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100697 MBEDTLS_LMOTS_TYPE_LEN,
698 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100699
700 /* We've got a valid signature now, so it's time to make sure the private
701 * key can't be reused.
702 */
Raef Colesf5632d32022-09-01 09:56:52 +0100703 ctx->have_private_key = 0;
704 mbedtls_platform_zeroize(ctx->private_key,
705 sizeof(ctx->private_key));
Raef Coles8ff6df52021-07-21 12:42:15 +0100706
Raef Coles891c6132022-09-01 11:05:48 +0100707 memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
708 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN );
709
710 memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET, tmp_sig,
711 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN );
Raef Coles8ff6df52021-07-21 12:42:15 +0100712
Raef Coles01c71a12022-08-31 15:55:00 +0100713 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100714 {
Raef Coles01c71a12022-08-31 15:55:00 +0100715 *sig_len = MBEDTLS_LMS_SIG_LEN;
Raef Coles8ff6df52021-07-21 12:42:15 +0100716 }
717
718 return( 0 );
719}
720
Raef Colesab4f8742022-09-01 12:24:31 +0100721#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles7dce69a2022-08-24 14:07:06 +0100722#endif /* MBEDTLS_LMS_C */