blob: 74191ebbcadbe7e1596353aa4024b159e1ab3a26 [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 Coles01c71a12022-08-31 15:55:00 +0100396 ret = public_key_from_hashed_digit_array( params,
397 ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )y_hashed_digits,
Raef Coles8ff6df52021-07-21 12:42:15 +0100398 out );
399 if ( ret )
400 {
401 return ( ret );
402 }
403
Raef Coles01c71a12022-08-31 15:55:00 +0100404 if ( out_len != NULL )
405 {
406 *out_len = MBEDTLS_LMOTS_N_HASH_LEN;
407 }
408
Raef Coles8ff6df52021-07-21 12:42:15 +0100409 return( 0 );
410}
411
Raef Coles01c71a12022-08-31 15:55:00 +0100412int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
413 size_t msg_size, const unsigned char *sig,
414 size_t sig_size )
Raef Coles8ff6df52021-07-21 12:42:15 +0100415{
Raef Coles01c71a12022-08-31 15:55:00 +0100416 unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN];
Raef Coles8ff6df52021-07-21 12:42:15 +0100417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
418
Raef Coles01c71a12022-08-31 15:55:00 +0100419 if ( msg == NULL && msg_size != 0 )
420 {
421 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
422 }
423
Raef Colesf5632d32022-09-01 09:56:52 +0100424 if ( !ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100425 {
426 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
427 }
428
Raef Colesf5632d32022-09-01 09:56:52 +0100429 if( ctx->params.MBEDTLS_PRIVATE( type )
Raef Coles01c71a12022-08-31 15:55:00 +0100430 != MBEDTLS_LMOTS_SHA256_N32_W8 )
431 {
432 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
433 }
434
435 if ( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
436 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
437 {
438 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
439 }
440
Raef Colesf5632d32022-09-01 09:56:52 +0100441 ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100442 msg, msg_size, sig, sig_size,
443 Kc_public_key_candidate,
444 MBEDTLS_LMOTS_N_HASH_LEN,
445 NULL);
446 if ( ret )
447 {
448 return( ret );
449 }
450
Raef Colesf5632d32022-09-01 09:56:52 +0100451 if ( memcmp( &Kc_public_key_candidate, ctx->public_key,
452 sizeof( ctx->public_key ) ) )
Raef Coles01c71a12022-08-31 15:55:00 +0100453 {
454 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
455 }
456
457 return( 0 );
458}
459
460void 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 Colesf5632d32022-09-01 09:56:52 +0100494 ctx->params.type = type;
Raef Coles01c71a12022-08-31 15:55:00 +0100495
Raef Colesf5632d32022-09-01 09:56:52 +0100496 memcpy( ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100497 I_key_identifier,
Raef Colesf5632d32022-09-01 09:56:52 +0100498 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100499
500 unsigned_int_to_network_bytes(q_leaf_identifier,
501 MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
Raef Colesf5632d32022-09-01 09:56:52 +0100502 ctx->params.q_leaf_identifier );
Raef Coles01c71a12022-08-31 15:55:00 +0100503
504 unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes );
505
506 for ( i_digit_idx = 0; i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT; i_digit_idx++ )
507 {
508 op = psa_hash_operation_init( );
509 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
510 ret = mbedtls_lms_error_from_psa( status );
511 if ( ret != 0 )
512 goto exit;
513
514 ret = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100515 ctx->params.I_key_identifier,
516 sizeof( ctx->params.I_key_identifier ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100517 ret = mbedtls_lms_error_from_psa( status );
518 if ( ret )
519 goto exit;
520
521 status = psa_hash_update( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100522 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100523 MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
524 ret = mbedtls_lms_error_from_psa( status );
525 if ( ret )
526 goto exit;
527
528 unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN, i_digit_idx_bytes );
529 status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN );
530 ret = mbedtls_lms_error_from_psa( status );
531 if ( ret )
532 goto exit;
533
534 status = psa_hash_update( &op, const_bytes, sizeof( const_bytes) );
535 ret = mbedtls_lms_error_from_psa( status );
536 if ( ret )
537 goto exit;
538
539 status = psa_hash_update( &op, seed, seed_size );
540 ret = mbedtls_lms_error_from_psa( status );
541 if ( ret )
542 goto exit;
543
544 status = psa_hash_finish( &op,
Raef Colesf5632d32022-09-01 09:56:52 +0100545 ctx->private_key[i_digit_idx],
Raef Coles01c71a12022-08-31 15:55:00 +0100546 32, &output_hash_len );
547 ret = mbedtls_lms_error_from_psa( status );
548 if ( ret )
549 goto exit;
550
551 psa_hash_abort( &op );
552 }
553
Raef Colesf5632d32022-09-01 09:56:52 +0100554 ctx->have_private_key = 1;
Raef Coles01c71a12022-08-31 15:55:00 +0100555
556exit:
557 if( ret )
558 {
559 psa_hash_abort( &op );
560 return( ret );
561 }
562
563 return ret;
564}
565
566int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
567 mbedtls_lmots_private_t *priv_ctx)
568{
569 unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
571
572 if( ctx == NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100573 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100574 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100575 }
576
577 /* Check that a private key is loaded */
Raef Colesf5632d32022-09-01 09:56:52 +0100578 if ( !priv_ctx->have_private_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100579 {
580 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
581 }
582
Raef Colesf5632d32022-09-01 09:56:52 +0100583 ret = hash_digit_array( &priv_ctx->params,
584 ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(priv_ctx->private_key),
Raef Coles01c71a12022-08-31 15:55:00 +0100585 NULL, NULL, y_hashed_digits );
586 if ( ret )
587 {
588 return( ret );
589 }
590
Raef Colesf5632d32022-09-01 09:56:52 +0100591 ret = public_key_from_hashed_digit_array( &priv_ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100592 ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )y_hashed_digits,
Raef Colesf5632d32022-09-01 09:56:52 +0100593 ctx->public_key );
Raef Coles01c71a12022-08-31 15:55:00 +0100594 if ( ret )
595 {
596 return( ret );
597 }
598
Raef Colesf5632d32022-09-01 09:56:52 +0100599 memcpy( &ctx->params, &priv_ctx->params,
600 sizeof( ctx->params ) );
Raef Coles01c71a12022-08-31 15:55:00 +0100601
602 ctx->MBEDTLS_PRIVATE(have_public_key = 1);
603
604 return( ret );
605}
606
607
608int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
609 unsigned char *key, size_t key_size,
610 size_t *key_len )
611{
612 if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN )
613 {
614 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
615 }
616
Raef Colesf5632d32022-09-01 09:56:52 +0100617 if( ! ctx->have_public_key )
Raef Coles01c71a12022-08-31 15:55:00 +0100618 {
619 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
620 }
621
Raef Colesf5632d32022-09-01 09:56:52 +0100622 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100623 MBEDTLS_LMOTS_TYPE_LEN,
624 key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
625
626 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100627 ctx->params.I_key_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100628 MBEDTLS_LMOTS_I_KEY_ID_LEN );
629
630 memcpy(key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET,
Raef Colesf5632d32022-09-01 09:56:52 +0100631 ctx->params.q_leaf_identifier,
Raef Coles01c71a12022-08-31 15:55:00 +0100632 MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
633
Raef Colesf5632d32022-09-01 09:56:52 +0100634 memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
Raef Coles01c71a12022-08-31 15:55:00 +0100635 MBEDTLS_LMOTS_N_HASH_LEN );
636
637 if( key_len != NULL )
638 {
639 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN;
640 }
641
642 return( 0 );
643}
644
645int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
646 int (*f_rng)(void *, unsigned char *, size_t),
647 void *p_rng, const unsigned char *msg, size_t msg_size,
648 unsigned char *sig, size_t sig_size, size_t* sig_len )
649{
650 unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT];
651 unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
653
654 if ( msg == NULL && msg_size != 0 )
655 {
656 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
657 }
658
659 if( sig_size < MBEDTLS_LMOTS_SIG_LEN )
660 {
661 return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
662 }
663
664 /* Check that a private key is loaded */
Raef Colesf5632d32022-09-01 09:56:52 +0100665 if ( !ctx->have_private_key )
Raef Coles8ff6df52021-07-21 12:42:15 +0100666 {
Raef Coles7dce69a2022-08-24 14:07:06 +0100667 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
Raef Coles8ff6df52021-07-21 12:42:15 +0100668 }
669
670 ret = f_rng( p_rng, sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, MBEDTLS_LMOTS_N_HASH_LEN );
671 if ( ret )
672 {
673 return( ret );
674 }
675
Raef Colesf5632d32022-09-01 09:56:52 +0100676 ret = create_digit_array_with_checksum( &ctx->params,
Raef Coles01c71a12022-08-31 15:55:00 +0100677 msg, msg_size,
678 sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
679 tmp_digit_array );
Raef Coles8ff6df52021-07-21 12:42:15 +0100680 if ( ret )
681 {
682 return( ret );
683 }
684
Raef Colesf5632d32022-09-01 09:56:52 +0100685 ret = hash_digit_array( &ctx->params,
686 ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(ctx->private_key),
Raef Coles01c71a12022-08-31 15:55:00 +0100687 NULL, tmp_digit_array, tmp_sig );
Raef Coles8ff6df52021-07-21 12:42:15 +0100688 if ( ret )
689 {
690 return( ret );
691 }
692
Raef Colesf5632d32022-09-01 09:56:52 +0100693 unsigned_int_to_network_bytes( ctx->params.type,
Raef Coles01c71a12022-08-31 15:55:00 +0100694 MBEDTLS_LMOTS_TYPE_LEN,
695 sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
Raef Coles8ff6df52021-07-21 12:42:15 +0100696
697 /* We've got a valid signature now, so it's time to make sure the private
698 * key can't be reused.
699 */
Raef Colesf5632d32022-09-01 09:56:52 +0100700 ctx->have_private_key = 0;
701 mbedtls_platform_zeroize(ctx->private_key,
702 sizeof(ctx->private_key));
Raef Coles8ff6df52021-07-21 12:42:15 +0100703
704 memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET, tmp_sig,
Raef Coles01c71a12022-08-31 15:55:00 +0100705 MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN);
Raef Coles8ff6df52021-07-21 12:42:15 +0100706
Raef Coles01c71a12022-08-31 15:55:00 +0100707 if( sig_len != NULL )
Raef Coles8ff6df52021-07-21 12:42:15 +0100708 {
Raef Coles01c71a12022-08-31 15:55:00 +0100709 *sig_len = MBEDTLS_LMS_SIG_LEN;
Raef Coles8ff6df52021-07-21 12:42:15 +0100710 }
711
712 return( 0 );
713}
714
Raef Coles7dce69a2022-08-24 14:07:06 +0100715#endif /* MBEDTLS_LMS_C */