Update LMS API to support multiple parameter sets
Parameterise macros to allow variation of sizes Signed-off-by: Raef Coles <raef.coles@arm.com>
This commit is contained in:
parent
ab4f87413a
commit
e9479a0264
6 changed files with 226 additions and 161 deletions
|
@ -40,15 +40,28 @@
|
|||
#define MBEDTLS_ERR_LMS_ALLOC_FAILED -0x0017 /**< LMS failed to allocate space for a private key */
|
||||
#define MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL -0x0019 /**< Input/output buffer is too small to contain requited data */
|
||||
|
||||
#define MBEDTLS_LMS_M_NODE_BYTES (32) /* The length of a hash output, 32 for SHA256 */
|
||||
#define MBEDTLS_LMS_TYPE_LEN (4)
|
||||
#define MBEDTLS_LMS_H_TREE_HEIGHT (10u)
|
||||
#define MBEDTLS_LMS_H_TREE_HEIGHT(type) (type == MBEDTLS_LMS_SHA256_M32_H10 ? 10u : 0)
|
||||
|
||||
#define MBEDTLS_LMS_SIG_LEN (MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_SIG_LEN + \
|
||||
MBEDTLS_LMS_TYPE_LEN + MBEDTLS_LMS_H_TREE_HEIGHT * MBEDTLS_LMS_M_NODE_BYTES)
|
||||
/* The length of a hash output, Currently only imlemented for SHA256.
|
||||
* Max is 32 bytes.
|
||||
*/
|
||||
/* The length of a hash output, Currently only imlemented for SHA256.
|
||||
* Max is 32 bytes.
|
||||
*/
|
||||
#define MBEDTLS_LMS_M_NODE_BYTES(type) (type == MBEDTLS_LMS_SHA256_M32_H10 ? 32 : 0)
|
||||
#define MBEDTLS_LMS_M_NODE_BYTES_MAX 32
|
||||
|
||||
#define MBEDTLS_LMS_PUBLIC_KEY_LEN (MBEDTLS_LMS_TYPE_LEN + MBEDTLS_LMOTS_TYPE_LEN + \
|
||||
MBEDTLS_LMOTS_I_KEY_ID_LEN + MBEDTLS_LMS_M_NODE_BYTES)
|
||||
#define MBEDTLS_LMS_SIG_LEN(type, otstype) (MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \
|
||||
MBEDTLS_LMOTS_SIG_LEN(otstype) + \
|
||||
MBEDTLS_LMS_TYPE_LEN + \
|
||||
(MBEDTLS_LMS_H_TREE_HEIGHT(type) * \
|
||||
MBEDTLS_LMS_M_NODE_BYTES(type)))
|
||||
|
||||
#define MBEDTLS_LMS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMS_TYPE_LEN + \
|
||||
MBEDTLS_LMOTS_TYPE_LEN + \
|
||||
MBEDTLS_LMOTS_I_KEY_ID_LEN + \
|
||||
MBEDTLS_LMS_M_NODE_BYTES(type))
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -99,7 +112,7 @@ typedef struct {
|
|||
*/
|
||||
typedef struct {
|
||||
mbedtls_lms_parameters_t MBEDTLS_PRIVATE(params);
|
||||
unsigned char MBEDTLS_PRIVATE(T_1_pub_key)[MBEDTLS_LMS_M_NODE_BYTES]; /*!< The public key, in
|
||||
unsigned char MBEDTLS_PRIVATE(T_1_pub_key)[MBEDTLS_LMS_M_NODE_BYTES_MAX]; /*!< The public key, in
|
||||
the form of the merkle tree root node. */
|
||||
unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key.
|
||||
Boolean values only. */
|
||||
|
|
133
library/lmots.c
133
library/lmots.c
|
@ -45,7 +45,7 @@
|
|||
#include "psa/crypto.h"
|
||||
|
||||
#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
|
||||
#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN)
|
||||
#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(type) (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type))
|
||||
|
||||
#define MBEDTLS_LMOTS_PUBLIC_KEY_TYPE_OFFSET (0)
|
||||
#define MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
|
||||
|
@ -60,6 +60,9 @@
|
|||
#define J_HASH_IDX_LEN (1)
|
||||
#define D_CONST_LEN (2)
|
||||
|
||||
/* Currently only defined for SHA256, 32 is the max hash output size */
|
||||
#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN_MAX (MBEDTLS_LMOTS_N_HASH_LEN_MAX)
|
||||
|
||||
#define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u)
|
||||
|
||||
#define D_CONST_LEN (2)
|
||||
|
@ -87,12 +90,13 @@ unsigned int network_bytes_to_unsigned_int(size_t len, const unsigned char *byte
|
|||
return val;
|
||||
}
|
||||
|
||||
static unsigned short lmots_checksum_calculate( const unsigned char* digest )
|
||||
static unsigned short lmots_checksum_calculate( const mbedtls_lmots_parameters_t *params,
|
||||
const unsigned char* digest )
|
||||
{
|
||||
size_t idx;
|
||||
unsigned sum = 0;
|
||||
|
||||
for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN; idx++ )
|
||||
for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++ )
|
||||
{
|
||||
sum += DIGIT_MAX_VALUE - digest[idx];
|
||||
}
|
||||
|
@ -103,8 +107,8 @@ static unsigned short lmots_checksum_calculate( const unsigned char* digest )
|
|||
static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *params,
|
||||
const unsigned char *msg,
|
||||
size_t msg_len,
|
||||
const unsigned char C_random_value[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN],
|
||||
unsigned char out[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT] )
|
||||
const unsigned char *C_random_value,
|
||||
unsigned char *out )
|
||||
{
|
||||
psa_hash_operation_t op;
|
||||
psa_status_t status;
|
||||
|
@ -135,7 +139,8 @@ static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *p
|
|||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_update( &op, C_random_value, MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN );
|
||||
status = psa_hash_update( &op, C_random_value,
|
||||
MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type) );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
@ -145,14 +150,16 @@ static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *p
|
|||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_finish( &op, out, MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT,
|
||||
status = psa_hash_finish( &op, out,
|
||||
MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type),
|
||||
&output_hash_len );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
||||
checksum = lmots_checksum_calculate( out );
|
||||
unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN, out + MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
checksum = lmots_checksum_calculate( params, out );
|
||||
unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN,
|
||||
out + MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
|
||||
|
||||
exit:
|
||||
psa_hash_abort( &op );
|
||||
|
@ -161,10 +168,10 @@ exit:
|
|||
}
|
||||
|
||||
static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
|
||||
const unsigned char x_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN],
|
||||
const unsigned char *x_digit_array,
|
||||
const unsigned char *hash_idx_min_values,
|
||||
const unsigned char *hash_idx_max_values,
|
||||
unsigned char output[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN] )
|
||||
unsigned char *output )
|
||||
{
|
||||
unsigned char i_digit_idx;
|
||||
unsigned char j_hash_idx;
|
||||
|
@ -178,15 +185,18 @@ static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
|
|||
psa_hash_operation_t op;
|
||||
psa_status_t status;
|
||||
size_t output_hash_len;
|
||||
unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN];
|
||||
unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
op = psa_hash_operation_init();
|
||||
|
||||
for ( i_digit_idx = 0; i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT; i_digit_idx++ )
|
||||
for ( i_digit_idx = 0;
|
||||
i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type);
|
||||
i_digit_idx++ )
|
||||
{
|
||||
|
||||
memcpy( tmp_hash, &x_digit_array[i_digit_idx], MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
memcpy( tmp_hash, &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)],
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
|
||||
|
||||
j_hash_idx_min = hash_idx_min_values != NULL ? hash_idx_min_values[i_digit_idx] : 0;
|
||||
j_hash_idx_max = hash_idx_max_values != NULL ? hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE;
|
||||
|
@ -224,7 +234,8 @@ static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
|
|||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_update( &op, tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
status = psa_hash_update( &op, tmp_hash,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
@ -237,7 +248,8 @@ static int hash_digit_array( const mbedtls_lmots_parameters_t *params,
|
|||
psa_hash_abort( &op );
|
||||
}
|
||||
|
||||
memcpy( &output[i_digit_idx], tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)], tmp_hash,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
|
||||
}
|
||||
|
||||
exit:
|
||||
|
@ -253,7 +265,7 @@ exit:
|
|||
}
|
||||
|
||||
static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params,
|
||||
const unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN],
|
||||
const unsigned char *y_hashed_digits,
|
||||
unsigned char *pub_key )
|
||||
{
|
||||
psa_hash_operation_t op;
|
||||
|
@ -285,13 +297,15 @@ static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t
|
|||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_update( &op, ( unsigned char * )y_hashed_digits,
|
||||
MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
status = psa_hash_update( &op, y_hashed_digits,
|
||||
MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) *
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(params->type) );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_finish( &op, pub_key, 32, &output_hash_len );
|
||||
status = psa_hash_finish( &op, pub_key, MBEDTLS_LMOTS_N_HASH_LEN(params->type),
|
||||
&output_hash_len );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
|
||||
exit:
|
||||
|
@ -330,15 +344,15 @@ void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx )
|
|||
int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
|
||||
const unsigned char *key, size_t key_len )
|
||||
{
|
||||
if ( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
ctx->params.type =
|
||||
network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
|
||||
key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
|
||||
|
||||
if ( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
memcpy( ctx->params.I_key_identifier,
|
||||
key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET, MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
||||
|
||||
|
@ -347,7 +361,7 @@ int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
|
|||
|
||||
memcpy( ctx->public_key,
|
||||
key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
|
||||
|
||||
ctx->have_public_key = 1;
|
||||
|
||||
|
@ -363,8 +377,8 @@ int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters
|
|||
size_t out_size,
|
||||
size_t *out_len)
|
||||
{
|
||||
unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT];
|
||||
unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
|
||||
unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
|
||||
unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if ( msg == NULL && msg_size != 0 )
|
||||
|
@ -372,7 +386,8 @@ int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters
|
|||
return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
if ( sig_size != MBEDTLS_LMOTS_SIG_LEN || out_size < MBEDTLS_LMOTS_N_HASH_LEN )
|
||||
if ( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) ||
|
||||
out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
@ -386,14 +401,16 @@ int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters
|
|||
}
|
||||
|
||||
ret = hash_digit_array( params,
|
||||
( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET),
|
||||
tmp_digit_array, NULL, y_hashed_digits );
|
||||
sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type),
|
||||
tmp_digit_array, NULL, (unsigned char *)y_hashed_digits );
|
||||
if ( ret )
|
||||
{
|
||||
return ( ret );
|
||||
}
|
||||
|
||||
ret = public_key_from_hashed_digit_array( params, y_hashed_digits, out );
|
||||
ret = public_key_from_hashed_digit_array( params,
|
||||
(unsigned char *)y_hashed_digits,
|
||||
out );
|
||||
if ( ret )
|
||||
{
|
||||
return ( ret );
|
||||
|
@ -401,7 +418,7 @@ int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters
|
|||
|
||||
if ( out_len != NULL )
|
||||
{
|
||||
*out_len = MBEDTLS_LMOTS_N_HASH_LEN;
|
||||
*out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type);
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
|
@ -411,7 +428,7 @@ int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
|
|||
size_t msg_size, const unsigned char *sig,
|
||||
size_t sig_size )
|
||||
{
|
||||
unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN];
|
||||
unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if ( msg == NULL && msg_size != 0 )
|
||||
|
@ -439,7 +456,7 @@ int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
|
|||
ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params,
|
||||
msg, msg_size, sig, sig_size,
|
||||
Kc_public_key_candidate,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
|
||||
NULL);
|
||||
if ( ret )
|
||||
{
|
||||
|
@ -505,7 +522,9 @@ int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
|
|||
|
||||
unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes );
|
||||
|
||||
for ( i_digit_idx = 0; i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT; i_digit_idx++ )
|
||||
for ( i_digit_idx = 0;
|
||||
i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type);
|
||||
i_digit_idx++ )
|
||||
{
|
||||
status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
|
@ -544,7 +563,8 @@ int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
|
|||
|
||||
status = psa_hash_finish( &op,
|
||||
ctx->private_key[i_digit_idx],
|
||||
32, &output_hash_len );
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type),
|
||||
&output_hash_len );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if ( ret )
|
||||
goto exit;
|
||||
|
@ -567,7 +587,7 @@ exit:
|
|||
int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
|
||||
mbedtls_lmots_private_t *priv_ctx)
|
||||
{
|
||||
unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
|
||||
unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
/* Check that a private key is loaded */
|
||||
|
@ -576,15 +596,16 @@ int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
|
|||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
ret = hash_digit_array( &priv_ctx->params, priv_ctx->private_key, NULL,
|
||||
NULL, y_hashed_digits );
|
||||
ret = hash_digit_array( &priv_ctx->params,
|
||||
(unsigned char *)priv_ctx->private_key, NULL,
|
||||
NULL, (unsigned char *)y_hashed_digits );
|
||||
if ( ret )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
ret = public_key_from_hashed_digit_array( &priv_ctx->params,
|
||||
y_hashed_digits,
|
||||
(unsigned char *)y_hashed_digits,
|
||||
ctx->public_key );
|
||||
if ( ret )
|
||||
{
|
||||
|
@ -604,7 +625,7 @@ int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
|
|||
unsigned char *key, size_t key_size,
|
||||
size_t *key_len )
|
||||
{
|
||||
if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN )
|
||||
if( key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
|
||||
}
|
||||
|
@ -627,11 +648,11 @@ int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
|
|||
MBEDTLS_LMOTS_Q_LEAF_ID_LEN);
|
||||
|
||||
memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
|
||||
|
||||
if( key_len != NULL )
|
||||
{
|
||||
*key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN;
|
||||
*key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type);
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
|
@ -642,7 +663,7 @@ int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
|
|||
void *p_rng, const unsigned char *msg, size_t msg_size,
|
||||
unsigned char *sig, size_t sig_size, size_t* sig_len )
|
||||
{
|
||||
unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT];
|
||||
unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX];
|
||||
/* Create a temporary buffer to prepare the signature in. This allows us to
|
||||
* finish creating a signature (ensuring the process doesn't fail), and then
|
||||
* erase the private key **before** writing any data into the sig parameter
|
||||
|
@ -650,8 +671,8 @@ int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
|
|||
* a partial signature on failure, which effectively compromises the private
|
||||
* key.
|
||||
*/
|
||||
unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN];
|
||||
unsigned char tmp_c_random[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN];
|
||||
unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
|
||||
unsigned char tmp_c_random[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN_MAX];
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if ( msg == NULL && msg_size != 0 )
|
||||
|
@ -659,7 +680,7 @@ int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
|
|||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
if( sig_size < MBEDTLS_LMOTS_SIG_LEN )
|
||||
if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
|
||||
}
|
||||
|
@ -670,7 +691,7 @@ int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
|
|||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
ret = f_rng( p_rng, tmp_c_random, MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
ret = f_rng( p_rng, tmp_c_random, MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
|
||||
if ( ret )
|
||||
{
|
||||
return( ret );
|
||||
|
@ -685,9 +706,8 @@ int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
|
|||
return( ret );
|
||||
}
|
||||
|
||||
ret = hash_digit_array( &ctx->params,
|
||||
ctx->private_key,
|
||||
NULL, tmp_digit_array, tmp_sig );
|
||||
ret = hash_digit_array( &ctx->params, (unsigned char *)ctx->private_key,
|
||||
NULL, tmp_digit_array, (unsigned char *)tmp_sig );
|
||||
if ( ret )
|
||||
{
|
||||
return( ret );
|
||||
|
@ -705,14 +725,15 @@ int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
|
|||
sizeof(ctx->private_key));
|
||||
|
||||
memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random,
|
||||
MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN );
|
||||
MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) );
|
||||
|
||||
memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET, tmp_sig,
|
||||
MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig,
|
||||
MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type)
|
||||
* MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) );
|
||||
|
||||
if( sig_len != NULL )
|
||||
{
|
||||
*sig_len = MBEDTLS_LMS_SIG_LEN;
|
||||
*sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type);
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
|
|
|
@ -33,18 +33,25 @@
|
|||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define MBEDTLS_LMOTS_N_HASH_LEN (32)
|
||||
#define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT (34)
|
||||
#define MBEDTLS_LMOTS_TYPE_LEN (4)
|
||||
#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN (MBEDTLS_LMOTS_N_HASH_LEN)
|
||||
#define MBEDTLS_LMOTS_I_KEY_ID_LEN (16)
|
||||
#define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4)
|
||||
/* Currently only defined for SHA256, 32 is the max hash output size */
|
||||
#define MBEDTLS_LMOTS_N_HASH_LEN_MAX (32u)
|
||||
#define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX (34u)
|
||||
#define MBEDTLS_LMOTS_N_HASH_LEN(type) (type == MBEDTLS_LMOTS_SHA256_N32_W8 ? 32u : 0)
|
||||
#define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) (type == MBEDTLS_LMOTS_SHA256_N32_W8 ? 34u : 0)
|
||||
#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) (MBEDTLS_LMOTS_N_HASH_LEN(type))
|
||||
#define MBEDTLS_LMOTS_TYPE_LEN (4u)
|
||||
#define MBEDTLS_LMOTS_I_KEY_ID_LEN (16u)
|
||||
#define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4u)
|
||||
|
||||
#define MBEDTLS_LMOTS_SIG_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN + \
|
||||
(MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN))
|
||||
#define MBEDTLS_LMOTS_SIG_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \
|
||||
MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) + \
|
||||
(MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) * \
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(type)))
|
||||
|
||||
#define MBEDTLS_LMOTS_PUBLIC_KEY_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_I_KEY_ID_LEN + \
|
||||
MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_N_HASH_LEN)
|
||||
#define MBEDTLS_LMOTS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \
|
||||
MBEDTLS_LMOTS_I_KEY_ID_LEN + \
|
||||
MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(type))
|
||||
|
||||
#define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
|
||||
|
||||
|
@ -121,7 +128,7 @@ typedef struct {
|
|||
*/
|
||||
typedef struct {
|
||||
mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
|
||||
unsigned char MBEDTLS_PRIVATE(private_key)[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][32];
|
||||
unsigned char MBEDTLS_PRIVATE(private_key)[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][32];
|
||||
unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key.
|
||||
Boolean values only. */
|
||||
} mbedtls_lmots_private_t;
|
||||
|
|
181
library/lms.c
181
library/lms.c
|
@ -54,10 +54,10 @@
|
|||
#define mbedtls_free free
|
||||
#endif
|
||||
|
||||
#define MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET (0)
|
||||
#define MBEDTLS_LMS_SIG_OTS_SIG_OFFSET (MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
|
||||
#define MBEDTLS_LMS_SIG_TYPE_OFFSET (MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_LEN)
|
||||
#define MBEDTLS_LMS_SIG_PATH_OFFSET (MBEDTLS_LMS_SIG_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN)
|
||||
#define MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET (0)
|
||||
#define MBEDTLS_LMS_SIG_OTS_SIG_OFFSET (MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
|
||||
#define MBEDTLS_LMS_SIG_TYPE_OFFSET(otstype) (MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_LEN(otstype))
|
||||
#define MBEDTLS_LMS_SIG_PATH_OFFSET(otstype) (MBEDTLS_LMS_SIG_TYPE_OFFSET(otstype) + MBEDTLS_LMS_TYPE_LEN)
|
||||
|
||||
#define MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET (0)
|
||||
#define MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN)
|
||||
|
@ -65,18 +65,21 @@
|
|||
#define MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN)
|
||||
|
||||
|
||||
#define MERKLE_TREE_NODE_AM (1u << (MBEDTLS_LMS_H_TREE_HEIGHT + 1u))
|
||||
#define MERKLE_TREE_LEAF_NODE_AM (1u << MBEDTLS_LMS_H_TREE_HEIGHT)
|
||||
#define MERKLE_TREE_INTERNAL_NODE_AM (1u << MBEDTLS_LMS_H_TREE_HEIGHT)
|
||||
/* Currently only support H=10 */
|
||||
#define MBEDTLS_LMS_H_TREE_HEIGHT_MAX 10
|
||||
#define MERKLE_TREE_NODE_AM_MAX (1u << (MBEDTLS_LMS_H_TREE_HEIGHT_MAX + 1u))
|
||||
#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))
|
||||
|
||||
#define D_CONST_LEN (2)
|
||||
static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = {0x82, 0x82};
|
||||
static const unsigned char D_INTERNAL_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83};
|
||||
|
||||
static int create_merkle_leaf_value( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
|
||||
unsigned char pub_key[MBEDTLS_LMOTS_N_HASH_LEN],
|
||||
static int create_merkle_leaf_value( const mbedtls_lms_parameters_t *params,
|
||||
unsigned char *pub_key,
|
||||
unsigned int r_node_idx,
|
||||
unsigned char out[MBEDTLS_LMS_M_NODE_BYTES] )
|
||||
unsigned char *out )
|
||||
{
|
||||
psa_hash_operation_t op;
|
||||
psa_status_t status;
|
||||
|
@ -90,7 +93,8 @@ static int create_merkle_leaf_value( const unsigned char I_key_identifier[MBEDTL
|
|||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_update( &op, I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
||||
status = psa_hash_update( &op, params->I_key_identifier,
|
||||
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if( ret )
|
||||
goto exit;
|
||||
|
@ -106,12 +110,14 @@ static int create_merkle_leaf_value( const unsigned char I_key_identifier[MBEDTL
|
|||
if( ret )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_update( &op, pub_key, MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
status = psa_hash_update( &op, pub_key,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN(params->otstype) );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if( ret )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES, &output_hash_len);
|
||||
status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
|
||||
&output_hash_len);
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if( ret )
|
||||
goto exit;
|
||||
|
@ -122,11 +128,11 @@ exit:
|
|||
return( ret );
|
||||
}
|
||||
|
||||
static int create_merkle_internal_value( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
|
||||
const unsigned char left_node[MBEDTLS_LMS_M_NODE_BYTES],
|
||||
const unsigned char right_node[MBEDTLS_LMS_M_NODE_BYTES],
|
||||
static int create_merkle_internal_value( const mbedtls_lms_parameters_t *params,
|
||||
const unsigned char *left_node,
|
||||
const unsigned char *right_node,
|
||||
unsigned int r_node_idx,
|
||||
unsigned char out[MBEDTLS_LMS_M_NODE_BYTES] )
|
||||
unsigned char *out )
|
||||
{
|
||||
psa_hash_operation_t op;
|
||||
psa_status_t status;
|
||||
|
@ -140,7 +146,8 @@ static int create_merkle_internal_value( const unsigned char I_key_identifier[MB
|
|||
if ( ret != 0 )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_update( &op, I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
||||
status = psa_hash_update( &op, params->I_key_identifier,
|
||||
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if( ret )
|
||||
goto exit;
|
||||
|
@ -156,17 +163,20 @@ static int create_merkle_internal_value( const unsigned char I_key_identifier[MB
|
|||
if( ret )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_update( &op, left_node, MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
status = psa_hash_update( &op, left_node,
|
||||
MBEDTLS_LMS_M_NODE_BYTES(params->type) );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if( ret )
|
||||
goto exit;
|
||||
|
||||
status = psa_hash_update( &op, right_node, MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
status = psa_hash_update( &op, right_node,
|
||||
MBEDTLS_LMS_M_NODE_BYTES(params->type) );
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if( ret )
|
||||
goto exit;
|
||||
|
||||
ret = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES, &output_hash_len);
|
||||
ret = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
|
||||
&output_hash_len);
|
||||
ret = mbedtls_lms_error_from_psa( status );
|
||||
if( ret )
|
||||
goto exit;
|
||||
|
@ -193,7 +203,7 @@ int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
|
|||
mbedtls_lms_algorithm_type_t type;
|
||||
mbedtls_lmots_algorithm_type_t otstype;
|
||||
|
||||
if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN )
|
||||
if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
|
||||
}
|
||||
|
@ -217,7 +227,7 @@ int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
|
|||
key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET,
|
||||
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
||||
memcpy( ctx->T_1_pub_key, key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
|
||||
|
||||
ctx->have_public_key = 1;
|
||||
|
||||
|
@ -229,8 +239,8 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
|
|||
const unsigned char *sig, size_t sig_size )
|
||||
{
|
||||
unsigned int q_leaf_identifier;
|
||||
unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN];
|
||||
unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES];
|
||||
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];
|
||||
unsigned int height;
|
||||
unsigned int curr_node_id;
|
||||
unsigned int parent_node_id;
|
||||
|
@ -244,7 +254,7 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
|
|||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
if( sig_size != MBEDTLS_LMS_SIG_LEN )
|
||||
if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
@ -261,15 +271,16 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
|
|||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
if( network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
|
||||
sig + MBEDTLS_LMS_SIG_TYPE_OFFSET) != MBEDTLS_LMS_SHA256_M32_H10 )
|
||||
if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
|
||||
sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
|
||||
!= MBEDTLS_LMOTS_SHA256_N32_W8 )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
|
||||
}
|
||||
|
||||
if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
|
||||
sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
|
||||
!= MBEDTLS_LMOTS_SHA256_N32_W8 )
|
||||
if( network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
|
||||
sig + MBEDTLS_LMS_SIG_TYPE_OFFSET(ctx->params.otstype))
|
||||
!= MBEDTLS_LMS_SHA256_M32_H10 )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
|
||||
}
|
||||
|
@ -278,7 +289,7 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
|
|||
q_leaf_identifier = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
|
||||
sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET );
|
||||
|
||||
if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM )
|
||||
if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
|
||||
}
|
||||
|
@ -293,7 +304,7 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
|
|||
|
||||
ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg, msg_size,
|
||||
sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
|
||||
MBEDTLS_LMOTS_SIG_LEN,
|
||||
MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
|
||||
Kc_candidate_ots_pub_key,
|
||||
sizeof(Kc_candidate_ots_pub_key),
|
||||
NULL );
|
||||
|
@ -303,37 +314,40 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
|
|||
}
|
||||
|
||||
create_merkle_leaf_value(
|
||||
ctx->params.I_key_identifier,
|
||||
Kc_candidate_ots_pub_key, MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier,
|
||||
&ctx->params,
|
||||
Kc_candidate_ots_pub_key,
|
||||
MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
|
||||
Tc_candidate_root_node );
|
||||
|
||||
curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier;
|
||||
curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier;
|
||||
|
||||
for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
|
||||
for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
|
||||
height++ )
|
||||
{
|
||||
parent_node_id = curr_node_id / 2;
|
||||
|
||||
/* Left/right node ordering matters for the hash */
|
||||
if( curr_node_id & 1 )
|
||||
{
|
||||
left_node = ( ( const unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
|
||||
left_node = sig + MBEDTLS_LMS_SIG_PATH_OFFSET(ctx->params.otstype) +
|
||||
height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
|
||||
right_node = Tc_candidate_root_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
left_node = Tc_candidate_root_node;
|
||||
right_node = ( ( const unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
|
||||
right_node = sig + MBEDTLS_LMS_SIG_PATH_OFFSET(ctx->params.otstype) +
|
||||
height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
|
||||
}
|
||||
|
||||
create_merkle_internal_value(
|
||||
ctx->params.I_key_identifier,
|
||||
left_node, right_node, parent_node_id, Tc_candidate_root_node);
|
||||
create_merkle_internal_value( &ctx->params, left_node, right_node,
|
||||
parent_node_id, Tc_candidate_root_node);
|
||||
|
||||
curr_node_id /= 2;
|
||||
}
|
||||
|
||||
if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN) )
|
||||
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
|
||||
}
|
||||
|
@ -344,22 +358,22 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
|
|||
#ifdef MBEDTLS_LMS_PRIVATE
|
||||
|
||||
static int calculate_merkle_tree( mbedtls_lms_private_t *ctx,
|
||||
unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES] )
|
||||
unsigned char *tree )
|
||||
{
|
||||
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 */
|
||||
for( priv_key_idx = 0; priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM;
|
||||
for( priv_key_idx = 0;
|
||||
priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
|
||||
priv_key_idx++ )
|
||||
{
|
||||
r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM + priv_key_idx;
|
||||
r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
|
||||
|
||||
ret = create_merkle_leaf_value(
|
||||
ctx->params.I_key_identifier,
|
||||
ctx->ots_public_keys[priv_key_idx].public_key,
|
||||
r_node_idx, tree[r_node_idx] );
|
||||
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)] );
|
||||
if( ret )
|
||||
{
|
||||
return( ret );
|
||||
|
@ -368,12 +382,14 @@ static int calculate_merkle_tree( mbedtls_lms_private_t *ctx,
|
|||
|
||||
/* Then the internal nodes, in reverse order so that we can guarantee the
|
||||
* parent has been created */
|
||||
for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM - 1; r_node_idx > 0;
|
||||
for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
|
||||
r_node_idx > 0;
|
||||
r_node_idx-- )
|
||||
{
|
||||
ret = create_merkle_internal_value(
|
||||
ctx->params.I_key_identifier,
|
||||
tree[(r_node_idx * 2)], tree[(r_node_idx * 2 + 1)], r_node_idx, tree[r_node_idx] );
|
||||
ret = create_merkle_internal_value( &ctx->params,
|
||||
&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)],
|
||||
r_node_idx, &tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)] );
|
||||
if( ret )
|
||||
{
|
||||
return( ret );
|
||||
|
@ -385,25 +401,28 @@ static int calculate_merkle_tree( mbedtls_lms_private_t *ctx,
|
|||
|
||||
static int get_merkle_path( mbedtls_lms_private_t *ctx,
|
||||
unsigned int leaf_node_id,
|
||||
unsigned char path[MBEDTLS_LMS_H_TREE_HEIGHT][MBEDTLS_LMS_M_NODE_BYTES] )
|
||||
unsigned char *path )
|
||||
{
|
||||
unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES];
|
||||
unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
|
||||
unsigned int curr_node_id = leaf_node_id;
|
||||
unsigned int adjacent_node_id;
|
||||
unsigned int height;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
ret = calculate_merkle_tree( ctx, tree);
|
||||
ret = calculate_merkle_tree( ctx, (unsigned char *)tree);
|
||||
if( ret )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
|
||||
for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
|
||||
height++ )
|
||||
{
|
||||
adjacent_node_id = curr_node_id ^ 1;
|
||||
|
||||
memcpy( &path[height], &tree[adjacent_node_id], MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
memcpy( &path[height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
|
||||
&tree[adjacent_node_id],
|
||||
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
|
||||
|
||||
curr_node_id >>=1;
|
||||
}
|
||||
|
@ -422,7 +441,7 @@ void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx )
|
|||
|
||||
if( ctx->have_private_key )
|
||||
{
|
||||
for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ )
|
||||
for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
|
||||
{
|
||||
mbedtls_lmots_free_private( &ctx->ots_private_keys[idx] );
|
||||
mbedtls_lmots_free_public( &ctx->ots_public_keys[idx] );
|
||||
|
@ -469,30 +488,30 @@ int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
|
|||
ctx->params.I_key_identifier,
|
||||
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
||||
|
||||
ctx->ots_private_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM,
|
||||
sizeof( mbedtls_lmots_private_t));
|
||||
ctx->ots_private_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
|
||||
sizeof( mbedtls_lmots_private_t));
|
||||
if( ctx->ots_private_keys == NULL )
|
||||
{
|
||||
ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ctx->ots_public_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM,
|
||||
sizeof( mbedtls_lmots_public_t));
|
||||
ctx->ots_public_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
|
||||
sizeof( mbedtls_lmots_public_t));
|
||||
if( ctx->ots_public_keys == NULL )
|
||||
{
|
||||
ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ )
|
||||
for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
|
||||
{
|
||||
mbedtls_lmots_init_private( &ctx->ots_private_keys[idx] );
|
||||
mbedtls_lmots_init_public( &ctx->ots_public_keys[idx] );
|
||||
}
|
||||
|
||||
|
||||
for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ )
|
||||
for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
|
||||
{
|
||||
ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx],
|
||||
otstype,
|
||||
|
@ -529,7 +548,7 @@ exit:
|
|||
int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
|
||||
mbedtls_lms_private_t *priv_ctx )
|
||||
{
|
||||
unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES];
|
||||
unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX];
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if( ! priv_ctx->MBEDTLS_PRIVATE( have_private_key ) )
|
||||
|
@ -552,14 +571,15 @@ int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
|
|||
memcpy( &ctx->params, &priv_ctx->params,
|
||||
sizeof(mbedtls_lmots_parameters_t) );
|
||||
|
||||
ret = calculate_merkle_tree( priv_ctx, tree);
|
||||
ret = calculate_merkle_tree( priv_ctx, (unsigned char *)tree);
|
||||
if( ret )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
/* Root node is always at position 1, due to 1-based indexing */
|
||||
memcpy( ctx->T_1_pub_key, &tree[1], MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
memcpy( ctx->T_1_pub_key, &tree[1],
|
||||
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
|
||||
|
||||
ctx->have_public_key = 1;
|
||||
|
||||
|
@ -570,7 +590,7 @@ int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
|
|||
int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx, unsigned char *key,
|
||||
size_t key_size, size_t *key_len )
|
||||
{
|
||||
if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN ) {
|
||||
if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) ) {
|
||||
return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
|
||||
}
|
||||
|
||||
|
@ -590,10 +610,10 @@ int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx, unsigned char *key
|
|||
MBEDTLS_LMOTS_I_KEY_ID_LEN );
|
||||
memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET,
|
||||
ctx->T_1_pub_key,
|
||||
MBEDTLS_LMOTS_N_HASH_LEN );
|
||||
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
|
||||
|
||||
if( key_len != NULL ) {
|
||||
*key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN;
|
||||
*key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
|
@ -613,7 +633,7 @@ int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
|
|||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
if( sig_size < MBEDTLS_LMS_SIG_LEN )
|
||||
if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
|
||||
}
|
||||
|
@ -629,7 +649,7 @@ int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
|
|||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM )
|
||||
if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
|
||||
{
|
||||
return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
|
||||
}
|
||||
|
@ -644,26 +664,29 @@ int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
|
|||
ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
|
||||
f_rng, p_rng, msg, msg_size,
|
||||
sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
|
||||
MBEDTLS_LMS_SIG_LEN, NULL );
|
||||
MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype),
|
||||
NULL );
|
||||
if( ret )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
unsigned_int_to_network_bytes( ctx->params.type,
|
||||
MBEDTLS_LMS_TYPE_LEN, sig + MBEDTLS_LMS_SIG_TYPE_OFFSET );
|
||||
MBEDTLS_LMS_TYPE_LEN,
|
||||
sig + MBEDTLS_LMS_SIG_TYPE_OFFSET(ctx->params.otstype) );
|
||||
unsigned_int_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
|
||||
sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET);
|
||||
|
||||
ret = get_merkle_path( ctx, MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier,
|
||||
( unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) );
|
||||
ret = get_merkle_path( ctx,
|
||||
MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
|
||||
sig + MBEDTLS_LMS_SIG_PATH_OFFSET(ctx->params.otstype) );
|
||||
if( ret )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
if( sig_len != NULL ) {
|
||||
*sig_len = MBEDTLS_LMS_SIG_LEN;
|
||||
*sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ void lmots_sign_verify_test ( data_t * msg )
|
|||
{
|
||||
mbedtls_lmots_public_t pub_ctx;
|
||||
mbedtls_lmots_private_t priv_ctx;
|
||||
unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
|
||||
unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
|
||||
mbedtls_entropy_context entropy_ctx;
|
||||
mbedtls_ctr_drbg_context drbg_ctx;
|
||||
uint8_t seed[16];
|
||||
|
@ -67,13 +67,14 @@ exit:
|
|||
void lmots_import_export_test ( data_t * pub_key )
|
||||
{
|
||||
mbedtls_lmots_public_t ctx;
|
||||
uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN];
|
||||
uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
|
||||
|
||||
mbedtls_lmots_init_public( &ctx );
|
||||
TEST_ASSERT( mbedtls_lmots_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
|
||||
TEST_ASSERT( mbedtls_lmots_export_public_key( &ctx, exported_pub_key, sizeof( exported_pub_key ), NULL ) == 0 );
|
||||
|
||||
TEST_ASSERT( memcmp( pub_key->x, exported_pub_key, MBEDTLS_LMOTS_PUBLIC_KEY_LEN ) == 0 );
|
||||
TEST_ASSERT( memcmp( pub_key->x, exported_pub_key,
|
||||
MBEDTLS_LMOTS_PUBLIC_KEY_LEN(MBEDTLS_LMOTS_SHA256_N32_W8) ) == 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_lmots_free_public( &ctx );
|
||||
|
@ -84,7 +85,7 @@ exit:
|
|||
void lmots_reuse_test ( data_t * msg )
|
||||
{
|
||||
mbedtls_lmots_private_t ctx;
|
||||
unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
|
||||
unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
|
||||
mbedtls_entropy_context entropy_ctx;
|
||||
mbedtls_ctr_drbg_context drbg_ctx;
|
||||
uint8_t seed[16];
|
||||
|
|
|
@ -15,7 +15,7 @@ void lms_sign_verify_test ( data_t * msg )
|
|||
{
|
||||
mbedtls_lms_public_t pub_ctx;
|
||||
mbedtls_lms_private_t priv_ctx;
|
||||
unsigned char sig[MBEDTLS_LMS_SIG_LEN];
|
||||
unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8)];
|
||||
mbedtls_entropy_context entropy_ctx;
|
||||
mbedtls_ctr_drbg_context drbg_ctx;
|
||||
uint8_t seed[16];
|
||||
|
@ -76,15 +76,15 @@ exit:
|
|||
void lms_import_export_test ( data_t * pub_key )
|
||||
{
|
||||
mbedtls_lms_public_t ctx;
|
||||
uint8_t exported_pub_key[MBEDTLS_LMS_PUBLIC_KEY_LEN];
|
||||
uint8_t exported_pub_key[MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10)];
|
||||
|
||||
mbedtls_lms_init_public(&ctx);
|
||||
TEST_ASSERT( mbedtls_lms_import_public_key( &ctx, pub_key->x, pub_key->len ) == 0 );
|
||||
TEST_ASSERT( mbedtls_lms_export_public_key( &ctx, exported_pub_key,
|
||||
sizeof(exported_pub_key), NULL ) == 0 );
|
||||
|
||||
ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBLIC_KEY_LEN,
|
||||
exported_pub_key, MBEDTLS_LMS_PUBLIC_KEY_LEN );
|
||||
ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10),
|
||||
exported_pub_key, MBEDTLS_LMS_PUBLIC_KEY_LEN(MBEDTLS_LMS_SHA256_M32_H10) );
|
||||
|
||||
exit:
|
||||
mbedtls_lms_free_public( &ctx );
|
||||
|
|
Loading…
Reference in a new issue