2021-07-21 13:42:15 +02:00
|
|
|
/*
|
|
|
|
* The LMS stateful-hash public-key signature scheme
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following sources were referenced in the design of this implementation
|
|
|
|
* of the LMS algorithm:
|
|
|
|
*
|
|
|
|
* [1] IETF RFC8554
|
|
|
|
* D. McGrew, M. Curcio, S.Fluhrer
|
|
|
|
* https://datatracker.ietf.org/doc/html/rfc8554
|
|
|
|
*
|
|
|
|
* [2] NIST Special Publication 800-208
|
|
|
|
* David A. Cooper et. al.
|
|
|
|
* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2022-10-07 11:35:56 +02:00
|
|
|
#if defined(MBEDTLS_LMS_C)
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-08-24 15:07:06 +02:00
|
|
|
#include "lmots.h"
|
|
|
|
|
2022-08-25 14:49:54 +02:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
|
2021-07-21 13:42:15 +02:00
|
|
|
#include "mbedtls/lms.h"
|
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/platform_util.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#define mbedtls_printf printf
|
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#define mbedtls_free free
|
|
|
|
#endif
|
|
|
|
|
2022-09-27 12:30:51 +02:00
|
|
|
#define SIG_Q_LEAF_ID_OFFSET (0)
|
|
|
|
#define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
|
|
|
|
MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
|
|
|
|
#define SIG_TYPE_OFFSET(otstype) (SIG_OTS_SIG_OFFSET + \
|
|
|
|
MBEDTLS_LMOTS_SIG_LEN(otstype))
|
|
|
|
#define SIG_PATH_OFFSET(otstype) (SIG_TYPE_OFFSET(otstype) + \
|
|
|
|
MBEDTLS_LMS_TYPE_LEN)
|
|
|
|
|
|
|
|
#define PUBLIC_KEY_TYPE_OFFSET (0)
|
|
|
|
#define PUBLIC_KEY_OTSTYPE_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
|
|
|
|
MBEDTLS_LMS_TYPE_LEN)
|
|
|
|
#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_OTSTYPE_OFFSET + \
|
|
|
|
MBEDTLS_LMOTS_TYPE_LEN)
|
|
|
|
#define PUBLIC_KEY_ROOT_NODE_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN)
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
/* Currently only support H=10 */
|
2022-09-27 12:30:51 +02:00
|
|
|
#define H_TREE_HEIGHT_MAX 10
|
|
|
|
#define MERKLE_TREE_NODE_AM_MAX (1u << (H_TREE_HEIGHT_MAX + 1u))
|
2022-09-01 17:06:35 +02:00
|
|
|
#define MERKLE_TREE_NODE_AM(type) (1u << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
|
|
|
|
#define MERKLE_TREE_LEAF_NODE_AM(type) (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type))
|
|
|
|
#define MERKLE_TREE_INTERNAL_NODE_AM(type) (1u << MBEDTLS_LMS_H_TREE_HEIGHT(type))
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
#define D_CONST_LEN (2)
|
|
|
|
static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = {0x82, 0x82};
|
2022-09-02 18:46:15 +02:00
|
|
|
static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83};
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-02 18:46:15 +02:00
|
|
|
|
|
|
|
/* Calculate the value of a leaf node of the merkle tree (which is a hash of a
|
|
|
|
* public key and some other parameters like the leaf index). This function
|
|
|
|
* implements RFC8554 section 5.3, in the case where r >= 2^h.
|
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* params The LMS parameter set, the underlying LMOTS
|
|
|
|
* parameter set, and I value which describe the key
|
|
|
|
* being used.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* pub_key The public key of the private whose index
|
|
|
|
* corresponds to the index of this leaf node. This
|
|
|
|
* is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* r_node_idx The index of this node in the merkle tree. Note
|
|
|
|
* that the root node of the merkle tree is
|
|
|
|
* 1-indexed.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* out The output node value, which is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*/
|
2022-09-01 17:06:35 +02:00
|
|
|
static int create_merkle_leaf_value( const mbedtls_lms_parameters_t *params,
|
|
|
|
unsigned char *pub_key,
|
2022-09-01 12:52:17 +02:00
|
|
|
unsigned int r_node_idx,
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char *out )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-25 14:49:54 +02:00
|
|
|
psa_hash_operation_t op;
|
2022-10-07 12:27:35 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-08-25 14:49:54 +02:00
|
|
|
size_t output_hash_len;
|
2021-07-21 13:42:15 +02:00
|
|
|
unsigned char r_node_idx_bytes[4];
|
|
|
|
|
2022-08-25 14:49:54 +02:00
|
|
|
op = psa_hash_operation_init( );
|
|
|
|
status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
status = psa_hash_update( &op, params->I_key_identifier,
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
|
2022-08-25 14:49:54 +02:00
|
|
|
status = psa_hash_update( &op, r_node_idx_bytes, 4 );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
status = psa_hash_update( &op, pub_key,
|
|
|
|
MBEDTLS_LMOTS_N_HASH_LEN(params->otstype) );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
|
2022-09-02 13:04:21 +02:00
|
|
|
&output_hash_len );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
exit:
|
2022-08-25 14:49:54 +02:00
|
|
|
psa_hash_abort( &op );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-10-07 12:46:06 +02:00
|
|
|
return ( mbedtls_lms_error_from_psa( status ) );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 18:46:15 +02:00
|
|
|
/* Calculate the value of an internal node of the merkle tree (which is a hash
|
|
|
|
* of a public key and some other parameters like the node index). This function
|
|
|
|
* implements RFC8554 section 5.3, in the case where r < 2^h.
|
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* params The LMS parameter set, the underlying LMOTS
|
|
|
|
* parameter set, and I value which describe the key
|
|
|
|
* being used.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* left_node The value of the child of this node which is on
|
|
|
|
* the left-hand side. As with all nodes on the
|
|
|
|
* merkle tree, this is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* right_node The value of the child of this node which is on
|
|
|
|
* the right-hand side. As with all nodes on the
|
|
|
|
* merkle tree, this is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* r_node_idx The index of this node in the merkle tree. Note
|
|
|
|
* that the root node of the merkle tree is
|
|
|
|
* 1-indexed.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* out The output node value, which is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*/
|
2022-09-01 17:06:35 +02:00
|
|
|
static int create_merkle_internal_value( const mbedtls_lms_parameters_t *params,
|
|
|
|
const unsigned char *left_node,
|
|
|
|
const unsigned char *right_node,
|
2022-09-01 12:52:17 +02:00
|
|
|
unsigned int r_node_idx,
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char *out )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-25 14:49:54 +02:00
|
|
|
psa_hash_operation_t op;
|
2022-10-07 12:27:35 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-08-25 14:49:54 +02:00
|
|
|
size_t output_hash_len;
|
2021-07-21 13:42:15 +02:00
|
|
|
unsigned char r_node_idx_bytes[4];
|
|
|
|
|
2022-08-25 14:49:54 +02:00
|
|
|
op = psa_hash_operation_init( );
|
|
|
|
status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
status = psa_hash_update( &op, params->I_key_identifier,
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
|
2022-08-25 14:49:54 +02:00
|
|
|
status = psa_hash_update( &op, r_node_idx_bytes, 4 );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-02 18:46:52 +02:00
|
|
|
status = psa_hash_update( &op, D_INTR_CONSTANT_BYTES, D_CONST_LEN );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
status = psa_hash_update( &op, left_node,
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(params->type) );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
status = psa_hash_update( &op, right_node,
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(params->type) );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-10-07 12:46:06 +02:00
|
|
|
status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
|
2022-09-02 13:04:21 +02:00
|
|
|
&output_hash_len );
|
2022-10-07 12:46:06 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
exit:
|
2022-08-25 14:49:54 +02:00
|
|
|
psa_hash_abort( &op );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-10-07 12:46:06 +02:00
|
|
|
return( mbedtls_lms_error_from_psa( status ) );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 13:04:24 +02:00
|
|
|
void mbedtls_lms_public_init( mbedtls_lms_public_t *ctx )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-10-07 13:04:24 +02:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof( *ctx ) ) ;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 13:04:24 +02:00
|
|
|
void mbedtls_lms_public_free( mbedtls_lms_public_t *ctx )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-10-07 13:04:24 +02:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
|
|
|
|
const unsigned char *key, size_t key_size )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_algorithm_type_t type;
|
|
|
|
mbedtls_lmots_algorithm_type_t otstype;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-10-07 14:39:22 +02:00
|
|
|
if( key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-10-07 14:39:22 +02:00
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
type = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
|
2022-09-27 12:30:51 +02:00
|
|
|
key + PUBLIC_KEY_TYPE_OFFSET );
|
2022-08-31 16:55:00 +02:00
|
|
|
if( type != MBEDTLS_LMS_SHA256_M32_H10 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.type = type;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
otstype = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
|
2022-09-27 12:30:51 +02:00
|
|
|
key + PUBLIC_KEY_OTSTYPE_OFFSET );
|
2022-08-31 16:55:00 +02:00
|
|
|
if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.otstype = otstype;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
memcpy( ctx->params.I_key_identifier,
|
2022-09-27 12:30:51 +02:00
|
|
|
key + PUBLIC_KEY_I_KEY_ID_OFFSET,
|
2022-08-31 16:55:00 +02:00
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
2022-09-27 12:30:51 +02:00
|
|
|
memcpy( ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
|
2022-09-01 17:06:35 +02:00
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->have_public_key = 1;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
|
|
|
|
const unsigned char *msg, size_t msg_size,
|
|
|
|
const unsigned char *sig, size_t sig_size )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
unsigned int q_leaf_identifier;
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
|
|
|
|
unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
|
2021-07-21 13:42:15 +02:00
|
|
|
unsigned int height;
|
|
|
|
unsigned int curr_node_id;
|
|
|
|
unsigned int parent_node_id;
|
|
|
|
const unsigned char* left_node;
|
2022-08-31 16:55:00 +02:00
|
|
|
const unsigned char* right_node;
|
|
|
|
mbedtls_lmots_parameters_t ots_params;
|
2021-07-21 13:42:15 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ! ctx->have_public_key )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ctx->params.type
|
2022-08-31 16:55:00 +02:00
|
|
|
!= MBEDTLS_LMS_SHA256_M32_H10 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ctx->params.otstype
|
2022-08-31 16:55:00 +02:00
|
|
|
!= MBEDTLS_LMOTS_SHA256_N32_W8 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
|
2022-09-27 12:30:51 +02:00
|
|
|
sig + SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET )
|
2022-09-01 17:06:35 +02:00
|
|
|
!= MBEDTLS_LMOTS_SHA256_N32_W8 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
|
|
|
|
}
|
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
|
2022-09-27 12:30:51 +02:00
|
|
|
sig + SIG_TYPE_OFFSET(ctx->params.otstype))
|
2022-09-01 17:06:35 +02:00
|
|
|
!= MBEDTLS_LMS_SHA256_M32_H10 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
|
2022-09-27 12:30:51 +02:00
|
|
|
MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:04:21 +02:00
|
|
|
memcpy( ots_params.I_key_identifier,
|
|
|
|
ctx->params.I_key_identifier,
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
2022-09-27 11:59:16 +02:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
|
|
|
|
MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
|
|
|
|
ots_params.q_leaf_identifier );
|
2022-09-01 10:56:52 +02:00
|
|
|
ots_params.type = ctx->params.otstype;
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2022-09-01 18:23:12 +02:00
|
|
|
ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg,
|
2022-09-27 12:30:51 +02:00
|
|
|
msg_size, sig + SIG_OTS_SIG_OFFSET,
|
2022-09-01 18:23:12 +02:00
|
|
|
MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), Kc_candidate_ots_pub_key,
|
2022-09-02 13:04:21 +02:00
|
|
|
sizeof( Kc_candidate_ots_pub_key ), NULL );
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2022-09-01 12:52:17 +02:00
|
|
|
create_merkle_leaf_value(
|
2022-09-01 17:06:35 +02:00
|
|
|
&ctx->params,
|
|
|
|
Kc_candidate_ots_pub_key,
|
|
|
|
MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
|
2022-08-31 16:55:00 +02:00
|
|
|
Tc_candidate_root_node );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 18:23:12 +02:00
|
|
|
curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
|
|
|
|
q_leaf_identifier;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
|
|
|
|
height++ )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
parent_node_id = curr_node_id / 2;
|
|
|
|
|
|
|
|
/* Left/right node ordering matters for the hash */
|
|
|
|
if( curr_node_id & 1 )
|
|
|
|
{
|
2022-09-27 12:30:51 +02:00
|
|
|
left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
|
2022-09-01 17:06:35 +02:00
|
|
|
height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
|
2022-08-31 16:55:00 +02:00
|
|
|
right_node = Tc_candidate_root_node;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
left_node = Tc_candidate_root_node;
|
2022-09-27 12:30:51 +02:00
|
|
|
right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
|
2022-09-01 17:06:35 +02:00
|
|
|
height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
create_merkle_internal_value( &ctx->params, left_node, right_node,
|
|
|
|
parent_node_id, Tc_candidate_root_node);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
curr_node_id /= 2;
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
|
2022-09-01 17:06:35 +02:00
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)) )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-10-07 11:35:56 +02:00
|
|
|
#if defined(MBEDTLS_LMS_PRIVATE)
|
2022-09-01 13:24:31 +02:00
|
|
|
|
2022-09-02 18:46:15 +02:00
|
|
|
/* Calculate a full merkle tree based on a private key. This function
|
|
|
|
* implements RFC8554 section 5.3, and is used to generate a public key (as the
|
|
|
|
* public key is the root node of the merkle tree).
|
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* ctx The LMS private context, containing a parameter
|
|
|
|
* set and private key material consisting of both
|
|
|
|
* public and private OTS.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* tree The output tree, which is 2^(H + 1) hash outputs.
|
|
|
|
* In the case of H=10 we have 2048 tree nodes (of
|
|
|
|
* which 1024 of them are leaf nodes). Note that
|
|
|
|
* because the merkle tree root is 1-indexed, the 0
|
|
|
|
* index tree node is never used.
|
2022-09-02 18:46:15 +02:00
|
|
|
*/
|
2022-10-07 12:12:27 +02:00
|
|
|
static int calculate_merkle_tree( const mbedtls_lms_private_t *ctx,
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char *tree )
|
2022-09-01 13:24:31 +02:00
|
|
|
{
|
|
|
|
unsigned int priv_key_idx;
|
|
|
|
unsigned int r_node_idx;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
/* First create the leaf nodes, in ascending order */
|
2022-09-01 17:06:35 +02:00
|
|
|
for( priv_key_idx = 0;
|
|
|
|
priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
|
2022-09-01 13:24:31 +02:00
|
|
|
priv_key_idx++ )
|
|
|
|
{
|
2022-09-01 17:06:35 +02:00
|
|
|
r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
|
2022-09-01 13:24:31 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
ret = create_merkle_leaf_value( &ctx->params,
|
|
|
|
ctx->ots_public_keys[priv_key_idx].public_key, r_node_idx,
|
|
|
|
&tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2022-09-01 13:24:31 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then the internal nodes, in reverse order so that we can guarantee the
|
|
|
|
* parent has been created */
|
2022-09-01 17:06:35 +02:00
|
|
|
for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
|
|
|
|
r_node_idx > 0;
|
2022-09-01 13:24:31 +02:00
|
|
|
r_node_idx-- )
|
|
|
|
{
|
2022-09-01 17:06:35 +02:00
|
|
|
ret = create_merkle_internal_value( &ctx->params,
|
2022-09-02 13:04:21 +02:00
|
|
|
&tree[( r_node_idx * 2 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
|
|
|
|
&tree[( r_node_idx * 2 + 1 ) * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
|
2022-09-01 18:23:12 +02:00
|
|
|
r_node_idx,
|
|
|
|
&tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2022-09-01 13:24:31 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-09-02 18:46:15 +02:00
|
|
|
/* Calculate a path from a leaf node of the merkle tree to the root of the tree,
|
|
|
|
* and return the full path. This function implements RFC8554 section 5.4.1, as
|
|
|
|
* the merkle path is the main component of an LMS signature.
|
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* ctx The LMS private context, containing a parameter
|
|
|
|
* set and private key material consisting of both
|
|
|
|
* public and private OTS.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* leaf_node_id Which leaf node to calculate the path from.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* tree The output path, which is H hash outputs.
|
2022-09-02 18:46:15 +02:00
|
|
|
*/
|
2022-09-01 13:24:31 +02:00
|
|
|
static int get_merkle_path( mbedtls_lms_private_t *ctx,
|
|
|
|
unsigned int leaf_node_id,
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char *path )
|
2022-09-01 13:24:31 +02:00
|
|
|
{
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
|
2022-09-01 13:24:31 +02:00
|
|
|
unsigned int curr_node_id = leaf_node_id;
|
|
|
|
unsigned int adjacent_node_id;
|
|
|
|
unsigned int height;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2022-09-02 13:04:21 +02:00
|
|
|
ret = calculate_merkle_tree( ctx, ( unsigned char * )tree );
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2022-09-01 13:24:31 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
|
|
|
|
height++ )
|
2022-09-01 13:24:31 +02:00
|
|
|
{
|
|
|
|
adjacent_node_id = curr_node_id ^ 1;
|
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
memcpy( &path[height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
|
|
|
|
&tree[adjacent_node_id],
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
|
2022-09-01 13:24:31 +02:00
|
|
|
|
|
|
|
curr_node_id >>=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:04:24 +02:00
|
|
|
void mbedtls_lms_private_init( mbedtls_lms_private_t *ctx )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-10-07 13:04:24 +02:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof( *ctx ) ) ;
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 13:04:24 +02:00
|
|
|
void mbedtls_lms_private_free( mbedtls_lms_private_t *ctx )
|
2022-08-31 16:55:00 +02:00
|
|
|
{
|
|
|
|
unsigned int idx;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ctx->have_private_key )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-09-01 17:06:35 +02:00
|
|
|
for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
|
2022-08-31 16:55:00 +02:00
|
|
|
{
|
2022-10-07 13:04:24 +02:00
|
|
|
mbedtls_lmots_private_free( &ctx->ots_private_keys[idx] );
|
|
|
|
mbedtls_lmots_public_free( &ctx->ots_public_keys[idx] );
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 14:27:54 +02:00
|
|
|
if( ctx->ots_private_keys != NULL )
|
|
|
|
mbedtls_free( ctx->ots_private_keys );
|
|
|
|
|
|
|
|
if( ctx->ots_public_keys != NULL )
|
|
|
|
mbedtls_free( ctx->ots_public_keys );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 13:04:24 +02:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
|
|
|
|
mbedtls_lms_algorithm_type_t type,
|
|
|
|
mbedtls_lmots_algorithm_type_t otstype,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
2022-10-07 12:12:27 +02:00
|
|
|
void* p_rng, const unsigned char *seed,
|
2022-08-31 16:55:00 +02:00
|
|
|
size_t seed_size )
|
|
|
|
{
|
|
|
|
unsigned int idx = 0;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
if( type != MBEDTLS_LMS_SHA256_M32_H10 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ctx->have_private_key )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.type = type;
|
|
|
|
ctx->params.otstype = otstype;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
f_rng( p_rng,
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.I_key_identifier,
|
2022-08-31 16:55:00 +02:00
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-22 19:30:33 +02:00
|
|
|
ctx->ots_private_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
|
2022-10-07 14:27:54 +02:00
|
|
|
sizeof( *ctx->ots_private_keys ) );
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ctx->ots_private_keys == NULL )
|
2022-08-31 16:55:00 +02:00
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
|
|
|
|
goto exit;
|
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-22 19:30:33 +02:00
|
|
|
ctx->ots_public_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
|
2022-10-07 14:27:54 +02:00
|
|
|
sizeof( *ctx->ots_public_keys ) );
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ctx->ots_public_keys == NULL )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-10-07 14:27:54 +02:00
|
|
|
/* Free just the ots private keys (since they've been allocated at this
|
|
|
|
* point) so that we can pass the context to lms_private_free (which
|
|
|
|
* will not try to free the private keys since have_private_key is not
|
|
|
|
* set.
|
|
|
|
*/
|
|
|
|
mbedtls_free(ctx->ots_private_keys);
|
|
|
|
ctx->ots_private_keys = NULL;
|
2022-08-31 16:55:00 +02:00
|
|
|
ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
|
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 14:27:54 +02:00
|
|
|
/* Now that all the allocation has succeeded we set have_private_key, since
|
|
|
|
* that causes lms_private_free to free the ots keys.
|
|
|
|
*/
|
|
|
|
ctx->have_private_key = 1;
|
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-10-07 13:04:24 +02:00
|
|
|
mbedtls_lmots_private_init( &ctx->ots_private_keys[idx] );
|
|
|
|
mbedtls_lmots_public_init( &ctx->ots_public_keys[idx] );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-09-01 10:56:52 +02:00
|
|
|
ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
|
2022-08-31 16:55:00 +02:00
|
|
|
otstype,
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.I_key_identifier,
|
2022-08-31 16:55:00 +02:00
|
|
|
idx, seed, seed_size );
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
|
|
|
|
&ctx->ots_private_keys[idx] );
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->q_next_usable_key = 0;
|
2022-08-31 16:55:00 +02:00
|
|
|
|
|
|
|
exit:
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2022-08-31 16:55:00 +02:00
|
|
|
{
|
2022-10-07 14:27:54 +02:00
|
|
|
mbedtls_lms_private_free(ctx);
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-10-07 14:27:54 +02:00
|
|
|
return( ret );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
|
2022-10-07 12:12:27 +02:00
|
|
|
const mbedtls_lms_private_t *priv_ctx )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
|
2021-07-21 13:42:15 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2022-09-02 13:04:21 +02:00
|
|
|
if( ! priv_ctx->have_private_key )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( priv_ctx->params.type
|
2022-08-31 16:55:00 +02:00
|
|
|
!= MBEDTLS_LMS_SHA256_M32_H10 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( priv_ctx->params.otstype
|
2022-08-31 16:55:00 +02:00
|
|
|
!= MBEDTLS_LMOTS_SHA256_N32_W8 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
memcpy( &ctx->params, &priv_ctx->params,
|
2022-09-02 13:04:21 +02:00
|
|
|
sizeof( mbedtls_lmots_parameters_t ) );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-02 13:04:21 +02:00
|
|
|
ret = calculate_merkle_tree( priv_ctx, ( unsigned char * )tree );
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Root node is always at position 1, due to 1-based indexing */
|
2022-09-01 17:06:35 +02:00
|
|
|
memcpy( ctx->T_1_pub_key, &tree[1],
|
2022-09-02 13:04:21 +02:00
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->have_public_key = 1;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2022-10-07 12:12:27 +02:00
|
|
|
int mbedtls_lms_export_public_key( const mbedtls_lms_public_t *ctx,
|
2022-09-01 18:23:12 +02:00
|
|
|
unsigned char *key,
|
2022-08-31 16:55:00 +02:00
|
|
|
size_t key_size, size_t *key_len )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-09-02 13:04:21 +02:00
|
|
|
if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
|
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
|
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ! ctx->have_public_key )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes(
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.type,
|
2022-09-27 12:30:51 +02:00
|
|
|
MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET );
|
2022-09-27 11:59:16 +02:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.otstype,
|
|
|
|
MBEDTLS_LMOTS_TYPE_LEN,
|
2022-09-27 12:30:51 +02:00
|
|
|
key + PUBLIC_KEY_OTSTYPE_OFFSET );
|
|
|
|
memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.I_key_identifier,
|
2022-08-31 16:55:00 +02:00
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
2022-09-27 12:30:51 +02:00
|
|
|
memcpy( key +PUBLIC_KEY_ROOT_NODE_OFFSET,
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->T_1_pub_key,
|
2022-09-01 17:06:35 +02:00
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2022-09-02 13:04:21 +02:00
|
|
|
if( key_len != NULL )
|
|
|
|
{
|
2022-09-01 17:06:35 +02:00
|
|
|
*key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
2022-10-07 12:12:27 +02:00
|
|
|
void* p_rng, const unsigned char *msg,
|
|
|
|
unsigned int msg_size, unsigned char *sig, size_t sig_size,
|
|
|
|
size_t *sig_len )
|
2022-08-31 16:55:00 +02:00
|
|
|
{
|
|
|
|
uint32_t q_leaf_identifier;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ! ctx->have_private_key )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
if( ctx->params.otstype
|
2022-08-31 16:55:00 +02:00
|
|
|
!= MBEDTLS_LMOTS_SHA256_N32_W8 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
q_leaf_identifier = ctx->q_next_usable_key;
|
2022-08-31 16:55:00 +02:00
|
|
|
/* This new value must _always_ be written back to the disk before the
|
|
|
|
* signature is returned.
|
|
|
|
*/
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->q_next_usable_key += 1;
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
|
2022-08-31 16:55:00 +02:00
|
|
|
f_rng, p_rng, msg, msg_size,
|
2022-09-27 12:30:51 +02:00
|
|
|
sig + SIG_OTS_SIG_OFFSET,
|
2022-09-01 17:06:35 +02:00
|
|
|
MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype),
|
|
|
|
NULL );
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
return( ret );
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
|
|
|
|
MBEDTLS_LMS_TYPE_LEN,
|
2022-09-27 12:30:51 +02:00
|
|
|
sig + SIG_TYPE_OFFSET(ctx->params.otstype) );
|
2022-09-27 11:59:16 +02:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
|
|
|
|
MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
|
2022-09-27 12:30:51 +02:00
|
|
|
sig + SIG_Q_LEAF_ID_OFFSET );
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
ret = get_merkle_path( ctx,
|
2022-09-01 18:23:12 +02:00
|
|
|
MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
|
2022-09-27 12:30:51 +02:00
|
|
|
sig + SIG_PATH_OFFSET(ctx->params.otstype) );
|
2022-09-02 17:04:47 +02:00
|
|
|
if( ret != 0 )
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:04:21 +02:00
|
|
|
if( sig_len != NULL )
|
|
|
|
{
|
2022-09-01 17:06:35 +02:00
|
|
|
*sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-21 13:42:15 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2022-10-07 11:35:56 +02:00
|
|
|
#endif /* defined(MBEDTLS_LMS_PRIVATE) */
|
|
|
|
#endif /* defined(MBEDTLS_LMS_C) */
|