blob: 4b4f1512837bd903d197ded6fd7a8a9aebea7348 [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/*
2 * The LMS stateful-hash 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 LMS 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
35#ifdef MBEDTLS_LMS_C
36
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 "psa/crypto.h"
42
Raef Coles8ff6df52021-07-21 12:42:15 +010043#include "mbedtls/lms.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010044#include "mbedtls/error.h"
45#include "mbedtls/platform_util.h"
46
47#if defined(MBEDTLS_PLATFORM_C)
48#include "mbedtls/platform.h"
49#else
50#include <stdlib.h>
51#include <stdio.h>
52#define mbedtls_printf printf
53#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
57#define MERKLE_TREE_NODE_AM (1 << (MBEDTLS_LMS_H_TREE_HEIGHT + 1))
58#define MERKLE_TREE_LEAF_AM (1 << MBEDTLS_LMS_H_TREE_HEIGHT)
59#define MERKLE_TREE_INTR_AM (1 << MBEDTLS_LMS_H_TREE_HEIGHT)
60
61#define D_CONST_LEN (2)
62
63#define D_LEAF_CONSTANT (0x8282)
64#define D_INTR_CONSTANT (0x8383)
65
66static void val_to_network_bytes(unsigned int val, size_t len, unsigned char *bytes)
67{
68 size_t idx;
69
70 for (idx = 0; idx < len; idx++) {
71 bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF;
72 }
73}
74
75static unsigned int network_bytes_to_val(size_t len, const unsigned char *bytes)
76{
77 size_t idx;
78 unsigned int val = 0;
79
80 for (idx = 0; idx < len; idx++) {
81 val |= ((unsigned int)bytes[idx]) << (8 * (len - 1 - idx));
82 }
83
84 return val;
85}
86
87static int create_merkle_leaf_node( const mbedtls_lms_context *ctx,
88 unsigned char pub_key[MBEDTLS_LMOTS_N_HASH_LEN],
89 unsigned int r_node_idx,
90 unsigned char out[32] )
91{
Raef Colesc8f96042022-08-25 13:49:54 +010092 psa_hash_operation_t op;
93 psa_status_t status;
94 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +010095 unsigned char D_LEAF_bytes[D_CONST_LEN];
96 unsigned char r_node_idx_bytes[4];
97 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
98
Raef Colesc8f96042022-08-25 13:49:54 +010099 op = psa_hash_operation_init( );
100 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
101 ret = mbedtls_lms_error_from_psa( status );
102 if ( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100103 {
104 goto out;
105 }
106
Raef Colesc8f96042022-08-25 13:49:54 +0100107 status = psa_hash_update( &op, ctx->MBEDTLS_PRIVATE(I_key_identifier),
108 MBEDTLS_LMOTS_I_KEY_ID_LEN );
109 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100110 if( ret )
111 {
112 goto out;
113 }
114
115 val_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100116 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
117 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100118 if( ret )
119 {
120 goto out;
121 }
122
123 val_to_network_bytes( D_LEAF_CONSTANT, D_CONST_LEN, D_LEAF_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100124 status = psa_hash_update( &op, D_LEAF_bytes, D_CONST_LEN );
125 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100126 if( ret )
127 {
128 goto out;
129 }
130
Raef Colesc8f96042022-08-25 13:49:54 +0100131 status = psa_hash_update( &op, pub_key, MBEDTLS_LMOTS_N_HASH_LEN );
132 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100133 if( ret )
134 {
135 goto out;
136 }
137
Raef Colesc8f96042022-08-25 13:49:54 +0100138 status = psa_hash_finish( &op, out, 32, &output_hash_len);
139 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100140 if( ret )
141 {
142 goto out;
143 }
144
145out:
Raef Colesc8f96042022-08-25 13:49:54 +0100146 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100147
148 return( ret );
149}
150
151static int create_merkle_intr_node( const mbedtls_lms_context *ctx,
152 const unsigned char left_node[32],
153 const unsigned char rght_node[32],
154 unsigned int r_node_idx,
155 unsigned char out[32] )
156{
Raef Colesc8f96042022-08-25 13:49:54 +0100157 psa_hash_operation_t op;
158 psa_status_t status;
159 size_t output_hash_len;
Raef Coles8ff6df52021-07-21 12:42:15 +0100160 unsigned char D_INTR_bytes[D_CONST_LEN];
161 unsigned char r_node_idx_bytes[4];
162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
163
Raef Colesc8f96042022-08-25 13:49:54 +0100164 op = psa_hash_operation_init( );
165 status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
166 ret = mbedtls_lms_error_from_psa( status );
167 if ( ret != 0 )
Raef Coles8ff6df52021-07-21 12:42:15 +0100168 {
169 goto out;
170 }
171
Raef Colesc8f96042022-08-25 13:49:54 +0100172 status = psa_hash_update( &op, ctx->MBEDTLS_PRIVATE(I_key_identifier),
173 MBEDTLS_LMOTS_I_KEY_ID_LEN );
174 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100175 if( ret )
176 {
177 goto out;
178 }
179
180 val_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100181 status = psa_hash_update( &op, r_node_idx_bytes, 4 );
182 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100183 if( ret )
184 {
185 goto out;
186 }
187
188 val_to_network_bytes( D_INTR_CONSTANT, D_CONST_LEN, D_INTR_bytes );
Raef Colesc8f96042022-08-25 13:49:54 +0100189 status = psa_hash_update( &op, D_INTR_bytes, D_CONST_LEN );
190 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100191 if( ret )
192 {
193 goto out;
194 }
195
Raef Colesc8f96042022-08-25 13:49:54 +0100196 status = psa_hash_update( &op, left_node, MBEDTLS_LMOTS_N_HASH_LEN );
197 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100198 if( ret )
199 {
200 goto out;
201 }
202
Raef Colesc8f96042022-08-25 13:49:54 +0100203 status = psa_hash_update( &op, rght_node, MBEDTLS_LMOTS_N_HASH_LEN );
204 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100205 if( ret )
206 {
207 goto out;
208 }
209
Raef Colesc8f96042022-08-25 13:49:54 +0100210 ret = psa_hash_finish( &op, out, 32, &output_hash_len);
211 ret = mbedtls_lms_error_from_psa( status );
Raef Coles8ff6df52021-07-21 12:42:15 +0100212 if( ret )
213 {
214 goto out;
215 }
216
217out:
Raef Colesc8f96042022-08-25 13:49:54 +0100218 psa_hash_abort( &op );
Raef Coles8ff6df52021-07-21 12:42:15 +0100219
220 return ret;
221}
222
223static int generate_merkle_tree( mbedtls_lms_context *ctx,
224 unsigned char tree[MERKLE_TREE_NODE_AM][32] )
225{
226 unsigned int priv_key_idx;
227 unsigned int r_node_idx;
228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
229
230 /* First create the leaf nodes, in ascending order */
231 for( priv_key_idx = 0; priv_key_idx < MERKLE_TREE_INTR_AM; priv_key_idx++ )
232 {
233 r_node_idx = MERKLE_TREE_INTR_AM + priv_key_idx;
234
235 ret = create_merkle_leaf_node( ctx, ctx->MBEDTLS_PRIVATE(priv_keys)[priv_key_idx].pub_key,
236 r_node_idx, tree[r_node_idx] );
237 if( ret )
238 {
239 return( ret );
240 }
241 }
242
243 /* Then the internal nodes, in reverse order so that we can guarantee the
244 * parent has been created */
245 for( r_node_idx = MERKLE_TREE_INTR_AM - 1; r_node_idx > 0; r_node_idx-- )
246 {
247 ret = create_merkle_intr_node( ctx, tree[(r_node_idx * 2)],
248 tree[(r_node_idx * 2 + 1)],
249 r_node_idx, tree[r_node_idx] );
250 if( ret )
251 {
252 return( ret );
253 }
254 }
255
256 return( 0 );
257}
258
259static int get_merkle_path( mbedtls_lms_context *ctx,
260 unsigned int leaf_node_id, unsigned char path[MBEDTLS_LMS_H_TREE_HEIGHT][32] )
261{
262 unsigned char tree[MERKLE_TREE_NODE_AM][32];
263 unsigned int curr_node_id = leaf_node_id;
264 unsigned int parent_node_id;
265 unsigned char sibling_relative_id;
266 unsigned int adjacent_node_id;
267 unsigned int height;
268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
269
270 ret = generate_merkle_tree( ctx, tree);
271 if( ret )
272 {
273 return( ret );
274 }
275
276 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
277 {
278 parent_node_id = ( curr_node_id / 2 );
279
280 /* 0 if the node is a left child, 1 if the node is a right child */
281 sibling_relative_id = curr_node_id & 1;
282
283 adjacent_node_id = ( parent_node_id * 2 ) + ( 1 - sibling_relative_id );
284
285 memcpy( &path[height], &tree[adjacent_node_id], MBEDTLS_LMOTS_N_HASH_LEN );
286
287 curr_node_id = parent_node_id;
288 }
289
290 return( 0 );
291}
292
293void mbedtls_lms_init( mbedtls_lms_context *ctx )
294{
295 if( ctx == NULL )
296 {
297 return;
298 }
299
300 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_context ) ) ;
301}
302
303void mbedtls_lms_free( mbedtls_lms_context *ctx )
304{
305 unsigned int idx;
306
307 if( ctx == NULL )
308 {
309 return;
310 }
311
312 if( ctx->MBEDTLS_PRIVATE(have_privkey) )
313 {
314 for( idx = 0; idx < MERKLE_TREE_LEAF_AM; idx++ )
315 {
316 mbedtls_lmots_free( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx] );
317 }
318
319 mbedtls_free( ctx->MBEDTLS_PRIVATE(priv_keys) );
320 }
321
322 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_context ) );
323}
324
325int mbedtls_lms_set_algorithm_type( mbedtls_lms_context *ctx,
326 mbedtls_lms_algorithm_type_t type,
327 mbedtls_lmots_algorithm_type_t otstype )
328{
329 if( ctx == NULL )
330 {
331 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
332 }
333
334 ctx->MBEDTLS_PRIVATE(type) = type;
335 ctx->MBEDTLS_PRIVATE(otstype) = otstype;
336
337 return( 0 );
338}
339
340int mbedtls_lms_sign( mbedtls_lms_context *ctx,
341 int ( *f_rng)(void *, unsigned char *, size_t),
342 void* p_rng, unsigned char *msg, unsigned int msg_len,
343 unsigned char *sig )
344{
345 unsigned int q_leaf_identifier;
346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
347
348 if( ctx == NULL )
349 {
350 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
351 }
352
353 if( ! ctx->MBEDTLS_PRIVATE(have_privkey) )
354 {
355 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
356 }
357
358 if( msg == NULL )
359 {
360 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
361 }
362
363 if( sig == NULL )
364 {
365 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
366 }
367
368
369 if( ctx->MBEDTLS_PRIVATE(type) != MBEDTLS_LMS_SHA256_M32_H10 )
370 {
371 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
372 }
373
374 if( ctx->MBEDTLS_PRIVATE(otstype) != MBEDTLS_LMOTS_SHA256_N32_W8 )
375 {
376 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
377 }
378
379
380 if( ctx->MBEDTLS_PRIVATE(q_next_usable_key) >= MERKLE_TREE_LEAF_AM )
381 {
382 return( MBEDTLS_ERR_LMS_OUT_OF_PRIV_KEYS );
383 }
384
385
386 q_leaf_identifier = ctx->MBEDTLS_PRIVATE(q_next_usable_key);
387 /* This new value must _always_ be written back to the disk before the
388 * signature is returned.
389 */
390 ctx->MBEDTLS_PRIVATE(q_next_usable_key) += 1;
391
392 ret = mbedtls_lmots_sign( &ctx->MBEDTLS_PRIVATE(priv_keys)[q_leaf_identifier],
393 f_rng, p_rng, msg, msg_len,
394 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET );
395 if( ret )
396 {
397 return( ret );
398 }
399
400 val_to_network_bytes( ctx->MBEDTLS_PRIVATE(type), MBEDTLS_LMS_TYPE_LEN,
401 sig + MBEDTLS_LMS_SIG_TYPE_OFFSET );
402 val_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
403 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET);
404
405 ret = get_merkle_path( ctx, MERKLE_TREE_INTR_AM + q_leaf_identifier,
406 ( unsigned char( * )[32] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) );
407 if( ret )
408 {
409 return( ret );
410 }
411
412 return( 0 );
413}
414
415int mbedtls_lms_verify( const mbedtls_lms_context *ctx,
416 const unsigned char *msg, unsigned int msg_len,
417 const unsigned char *sig )
418{
419 unsigned int q_leaf_identifier;
420 unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN];
421 unsigned char Tc_candidate_root_node[32];
422 unsigned int height;
423 unsigned int curr_node_id;
424 unsigned int parent_node_id;
425 const unsigned char* left_node;
426 const unsigned char* rght_node;
427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
428
429 if( ctx == NULL )
430 {
431 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
432 }
433
434 if( ! ctx->MBEDTLS_PRIVATE(have_pubkey) )
435 {
436 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
437 }
438
439 if( msg == NULL)
440 {
441 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
442 }
443
444 if( sig == NULL)
445 {
446 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
447 }
448
449 if( ctx->MBEDTLS_PRIVATE(type) != MBEDTLS_LMS_SHA256_M32_H10 )
450 {
451 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
452 }
453
454 if( ctx->MBEDTLS_PRIVATE(otstype) != MBEDTLS_LMOTS_SHA256_N32_W8 )
455 {
456 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
457 }
458
459
460 if( network_bytes_to_val( MBEDTLS_LMS_TYPE_LEN,
461 sig + MBEDTLS_LMS_SIG_TYPE_OFFSET) != MBEDTLS_LMS_SHA256_M32_H10 )
462 {
463 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
464 }
465
466 if( network_bytes_to_val( MBEDTLS_LMOTS_TYPE_LEN,
467 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
468 != MBEDTLS_LMOTS_SHA256_N32_W8 )
469 {
470 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
471 }
472
473
474 q_leaf_identifier = network_bytes_to_val( MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
475 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET );
476
477 if( q_leaf_identifier >= MERKLE_TREE_LEAF_AM )
478 {
479 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
480 }
481
482 ret = mbedtls_lmots_generate_pub_key_candidate( ctx->MBEDTLS_PRIVATE(I_key_identifier),
483 sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET,
484 msg, msg_len,
485 sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
486 Kc_candidate_ots_pub_key );
487 if( ret )
488 {
489 return( ret );
490 }
491
492 create_merkle_leaf_node( ctx, Kc_candidate_ots_pub_key,
493 MERKLE_TREE_INTR_AM + q_leaf_identifier,
494 Tc_candidate_root_node );
495
496 curr_node_id = MERKLE_TREE_INTR_AM + q_leaf_identifier;
497
498 for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
499 {
500 parent_node_id = curr_node_id / 2;
501
502 /* Left/right node ordering matters for the hash */
503 if( curr_node_id & 1 )
504 {
505 left_node = ( ( const unsigned char( * )[32] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
506 rght_node = Tc_candidate_root_node;
507 }
508 else
509 {
510 left_node = Tc_candidate_root_node;
511 rght_node = ( ( const unsigned char( * )[32] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
512 }
513
514 create_merkle_intr_node( ctx, left_node, rght_node, parent_node_id,
515 Tc_candidate_root_node);
516
517 curr_node_id /= 2;
518 }
519
520 if( memcmp( Tc_candidate_root_node, ctx->MBEDTLS_PRIVATE(T_1_pub_key),
521 MBEDTLS_LMOTS_N_HASH_LEN) )
522 {
523 return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
524 }
525
526 return( 0 );
527}
528
529int mbedtls_lms_import_pubkey( mbedtls_lms_context *ctx,
530 const unsigned char *key )
531{
532 mbedtls_lms_algorithm_type_t type;
533 mbedtls_lmots_algorithm_type_t otstype;
534
535 if( ctx == NULL )
536 {
537 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
538 }
539
540 if( key == NULL )
541 {
542 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
543 }
544
545 type = network_bytes_to_val( MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBKEY_TYPE_OFFSET );
546 if( type != MBEDTLS_LMS_SHA256_M32_H10 )
547 {
548 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
549 }
550 ctx->MBEDTLS_PRIVATE(type) = type;
551
552 otstype = network_bytes_to_val( MBEDTLS_LMOTS_TYPE_LEN,
553 key + MBEDTLS_LMS_PUBKEY_OTSTYPE_OFFSET );
554 if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
555 {
556 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
557 }
558 ctx->MBEDTLS_PRIVATE(otstype) = otstype;
559
560 memcpy( ctx->MBEDTLS_PRIVATE(I_key_identifier), key + MBEDTLS_LMS_PUBKEY_I_KEY_ID_OFFSET,
561 MBEDTLS_LMOTS_I_KEY_ID_LEN );
562 memcpy( ctx->MBEDTLS_PRIVATE(T_1_pub_key), key + MBEDTLS_LMS_PUBKEY_ROOT_NODE_OFFSET,
563 MBEDTLS_LMOTS_N_HASH_LEN );
564
565 ctx->MBEDTLS_PRIVATE(have_pubkey) = 1;
566
567 return( 0 );
568}
569
570int mbedtls_lms_export_pubkey( mbedtls_lms_context *ctx,
571 unsigned char *key )
572{
573 if( ctx == NULL )
574 {
575 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
576 }
577
578 if( key == NULL )
579 {
580 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
581 }
582
583 if( ! ctx->MBEDTLS_PRIVATE(have_pubkey) )
584 {
585 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
586 }
587
588 val_to_network_bytes( ctx->MBEDTLS_PRIVATE(type),
589 MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBKEY_TYPE_OFFSET );
590 val_to_network_bytes( ctx->MBEDTLS_PRIVATE(otstype),
591 MBEDTLS_LMOTS_TYPE_LEN, key + MBEDTLS_LMS_PUBKEY_OTSTYPE_OFFSET );
592 memcpy( key + MBEDTLS_LMS_PUBKEY_I_KEY_ID_OFFSET,
593 ctx->MBEDTLS_PRIVATE(I_key_identifier),
594 MBEDTLS_LMOTS_I_KEY_ID_LEN );
595 memcpy( key + MBEDTLS_LMS_PUBKEY_ROOT_NODE_OFFSET,
596 ctx->MBEDTLS_PRIVATE(T_1_pub_key),
597 MBEDTLS_LMOTS_N_HASH_LEN );
598
599 return( 0 );
600}
601
602int mbedtls_lms_gen_pubkey( mbedtls_lms_context *ctx )
603{
604 unsigned char tree[MERKLE_TREE_NODE_AM][32];
605 unsigned int idx;
606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
607
608 if( ctx == NULL )
609 {
610 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
611 }
612
613 if( ! ctx->MBEDTLS_PRIVATE( have_privkey ) )
614 {
615 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
616 }
617
618 if( ctx->MBEDTLS_PRIVATE(type) != MBEDTLS_LMS_SHA256_M32_H10 )
619 {
620 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
621 }
622
623 if( ctx->MBEDTLS_PRIVATE(otstype) != MBEDTLS_LMOTS_SHA256_N32_W8 )
624 {
625 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
626 }
627
628 for( idx = 0; idx < MERKLE_TREE_LEAF_AM; idx++ )
629 {
630 ret = mbedtls_lmots_gen_pubkey( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx] );
631 if( ret )
632 {
633 return( ret );
634 }
635 }
636
637 ret = generate_merkle_tree( ctx, tree);
638 if( ret )
639 {
640 return( ret );
641 }
642
643 /* Root node is always at position 1, due to 1-based indexing */
644 memcpy( ctx->MBEDTLS_PRIVATE(T_1_pub_key), &tree[1], MBEDTLS_LMOTS_N_HASH_LEN );
645
646 ctx->MBEDTLS_PRIVATE(have_pubkey) = 1;
647
648 return( 0 );
649}
650
651int mbedtls_lms_gen_privkey( mbedtls_lms_context *ctx,
652 int ( *f_rng)(void *, unsigned char *, size_t),
653 void* p_rng, unsigned char *seed,
654 size_t seed_len )
655{
656 unsigned int idx;
657 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
658
659 if( ctx == NULL )
660 {
661 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
662 }
663
664 if( ctx->MBEDTLS_PRIVATE(type) != MBEDTLS_LMS_SHA256_M32_H10 )
665 {
666 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
667 }
668
669 if( ctx->MBEDTLS_PRIVATE(otstype) != MBEDTLS_LMOTS_SHA256_N32_W8 )
670 {
671 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
672 }
673
674 if( ctx->MBEDTLS_PRIVATE(have_privkey) )
675 {
676 return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
677 }
678
679 f_rng( p_rng, ctx->MBEDTLS_PRIVATE(I_key_identifier),
680 sizeof( ctx->MBEDTLS_PRIVATE(I_key_identifier) ) );
681
682 ctx->MBEDTLS_PRIVATE(priv_keys) = mbedtls_calloc( MERKLE_TREE_LEAF_AM,
683 sizeof( mbedtls_lmots_context));
684 if( ctx->MBEDTLS_PRIVATE(priv_keys) == NULL )
685 {
686 ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
687 goto out;
688 }
689
690 for( idx = 0; idx < MERKLE_TREE_LEAF_AM; idx++ )
691 {
692 mbedtls_lmots_init( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx] );
693 ret = mbedtls_lmots_set_algorithm_type( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx],
694 ctx->MBEDTLS_PRIVATE(otstype) );
695 if( ret)
696 {
697 goto out;
698 }
699 }
700
701
702 for( idx = 0; idx < MERKLE_TREE_LEAF_AM; idx++ )
703 {
704 ret = mbedtls_lmots_gen_privkey( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx],
705 ctx->MBEDTLS_PRIVATE(I_key_identifier),
706 idx, seed, seed_len );
707 if( ret)
708 {
709 goto out;
710 }
711 }
712
713 ctx->MBEDTLS_PRIVATE(q_next_usable_key) = 0;
714 ctx->MBEDTLS_PRIVATE(have_privkey) = 1;
715
716out:
717 if( ret )
718 {
719 mbedtls_free( ctx->MBEDTLS_PRIVATE(priv_keys) );
720 return( ret );
721 }
722
723 return( 0 );
724}
725
726#endif /* MBEDTLS_LMS_C */