blob: 50559e5948f4ae9909a2959dbebc2f91339f12c0 [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
458void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx )
459{
460 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
461}
462
463void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx )
464{
465 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ;
466}
467
468int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
469 mbedtls_lmots_algorithm_type_t type,
470 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
471 uint32_t q_leaf_identifier,
472 const unsigned char *seed,
473 size_t seed_size )
474{
475 psa_hash_operation_t op;
476 psa_status_t status;
477 size_t output_hash_len;
478 unsigned int i_digit_idx;
479 unsigned char i_digit_idx_bytes[2];
480 unsigned char const_bytes[1];
481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
482
Raef Colesf5632d32022-09-01 09:56:52 +0100483 if ( ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100484 {
485 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
486 }
487
488 if ( type != MBEDTLS_LMOTS_SHA256_N32_W8 ) {
489 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
490 }
491
Raef Colesf5632d32022-09-01 09:56:52 +0100492 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100493
Raef Colesf5632d32022-09-01 09:56:52 +0100494 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100495 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100496 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100497
498 unsigned_int_to_network_bytes(q_leaf_identifier,
499 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Colesf5632d32022-09-01 09:56:52 +0100500 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100501
502 unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes );
503
504 for ( i_digit_idx = 0; i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT; i_digit_idx++ )
505 {
506 op = psa_hash_operation_init( );
507 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
508 ret = mbedtls_lms_error_from_psa( status );
509 if ( ret != 0 )
510 goto exit;
511
512 ret = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100513 ctx->params.I_key_identifier,
514 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100515 ret = mbedtls_lms_error_from_psa( status );
516 if ( ret )
517 goto exit;
518
519 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100520 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100521 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
522 ret = mbedtls_lms_error_from_psa( status );
523 if ( ret )
524 goto exit;
525
526 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN, i_digit_idx_bytes );
527 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
528 ret = mbedtls_lms_error_from_psa( status );
529 if ( ret )
530 goto exit;
531
532 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes) );
533 ret = mbedtls_lms_error_from_psa( status );
534 if ( ret )
535 goto exit;
536
537 status = psa_hash_update( &op, seed, seed_size );
538 ret = mbedtls_lms_error_from_psa( status );
539 if ( ret )
540 goto exit;
541
542 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100543 ctx->private_key[i_digit_idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100544 32, &output_hash_len );
545 ret = mbedtls_lms_error_from_psa( status );
546 if ( ret )
547 goto exit;
548
549 psa_hash_abort( &op );
550 }
551
Raef Colesf5632d32022-09-01 09:56:52 +0100552 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100553
554exit:
555 if( ret )
556 {
557 psa_hash_abort( &op );
558 return( ret );
559 }
560
561 return ret;
562}
563
564int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
565 mbedtls_lmots_private_t *priv_ctx)
566{
567 unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
568 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
569
Raef Coles8ff6df52021-07-21 12:42:15 +0100570 /* Check that a private key is loaded */
Raef Colesf5632d32022-09-01 09:56:52 +0100571 if ( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100572 {
573 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
574 }
575
Raef Coles0c88d4e2022-09-01 10:48:32 +0100576 ret = hash_digit_array( &priv_ctx->params, priv_ctx->private_key, NULL,
577 NULL, y_hashed_digits );
Raef Coles01c71a12022-08-31 15:55:00 +0100578 if ( ret )
579 {
580 return( ret );
581 }
582
Raef Colesf5632d32022-09-01 09:56:52 +0100583 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100584 y_hashed_digits,
585 ctx->public_key );
Raef Coles01c71a12022-08-31 15:55:00 +0100586 if ( ret )
587 {
588 return( ret );
589 }
590
Raef Colesf5632d32022-09-01 09:56:52 +0100591 memcpy( &ctx->params, &priv_ctx->params,
592 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100593
594 ctx->MBEDTLS_PRIVATE(have_public_key = 1);
595
596 return( ret );
597}
598
599
600int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
601 unsigned char *key, size_t key_size,
602 size_t *key_len )
603{
604 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN )
605 {
606 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
607 }
608
Raef Colesf5632d32022-09-01 09:56:52 +0100609 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100610 {
611 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
612 }
613
Raef Colesf5632d32022-09-01 09:56:52 +0100614 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100615 MBEDTLS_LMOTS_TYPE_LEN,
616 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
617
618 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100619 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100620 MBEDTLS_LMOTS_I_KEY_ID_LEN );
621
622 memcpy(key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100623 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100624 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
625
Raef Colesf5632d32022-09-01 09:56:52 +0100626 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100627 MBEDTLS_LMOTS_N_HASH_LEN );
628
629 if( key_len != NULL )
630 {
631 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN;
632 }
633
634 return( 0 );
635}
636
637int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
638 int (*f_rng)(void *, unsigned char *, size_t),
639 void *p_rng, const unsigned char *msg, size_t msg_size,
640 unsigned char *sig, size_t sig_size, size_t* sig_len )
641{
642 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT];
643 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
645
646 if ( msg == NULL && msg_size != 0 )
647 {
648 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
649 }
650
651 if( sig_size < MBEDTLS_LMOTS_SIG_LEN )
652 {
653 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
654 }
655
656 /* Check that a private key is loaded */
Raef Colesf5632d32022-09-01 09:56:52 +0100657 if ( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100658 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100659 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100660 }
661
662 ret = f_rng( p_rng, sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, MBEDTLS_LMOTS_N_HASH_LEN );
663 if ( ret )
664 {
665 return( ret );
666 }
667
Raef Colesf5632d32022-09-01 09:56:52 +0100668 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100669 msg, msg_size,
670 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
671 tmp_digit_array );
Raef Coles8ff6df52021-07-21 12:42:15 +0100672 if ( ret )
673 {
674 return( ret );
675 }
676
Raef Colesf5632d32022-09-01 09:56:52 +0100677 ret = hash_digit_array( &ctx->params,
Raef Coles0c88d4e2022-09-01 10:48:32 +0100678 ctx->private_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100679 NULL, tmp_digit_array, tmp_sig );
Raef Coles8ff6df52021-07-21 12:42:15 +0100680 if ( ret )
681 {
682 return( ret );
683 }
684
Raef Colesf5632d32022-09-01 09:56:52 +0100685 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100686 MBEDTLS_LMOTS_TYPE_LEN,
687 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100688
689 /* We've got a valid signature now, so it's time to make sure the private
690 * key can't be reused.
691 */
Raef Colesf5632d32022-09-01 09:56:52 +0100692 ctx->have_private_key = 0;
693 mbedtls_platform_zeroize(ctx->private_key,
694 sizeof(ctx->private_key));
Raef Coles8ff6df52021-07-21 12:42:15 +0100695
696 memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET, tmp_sig,
Raef Coles01c71a12022-08-31 15:55:00 +0100697 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN);
Raef Coles8ff6df52021-07-21 12:42:15 +0100698
Raef Coles01c71a12022-08-31 15:55:00 +0100699 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100700 {
Raef Coles01c71a12022-08-31 15:55:00 +0100701 *sig_len = MBEDTLS_LMS_SIG_LEN;
Raef Coles8ff6df52021-07-21 12:42:15 +0100702 }
703
704 return( 0 );
705}
706
Raef Coles7dce69a2022-08-24 14:07:06 +0100707#endif /* MBEDTLS_LMS_C */