Switch to the new code style
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/library/lms.c b/library/lms.c
index 78c7d26..76bcc19 100644
--- a/library/lms.c
+++ b/library/lms.c
@@ -70,8 +70,8 @@
#define MERKLE_TREE_INTERNAL_NODE_AM(type) ((size_t) 1 << 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_INTR_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83};
+static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = { 0x82, 0x82 };
+static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = { 0x83, 0x83 };
/* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
@@ -92,49 +92,55 @@
*
* out The output node value, which is a hash output.
*/
-static int create_merkle_leaf_value( const mbedtls_lms_parameters_t *params,
- unsigned char *pub_key,
- unsigned int r_node_idx,
- unsigned char *out )
+static int create_merkle_leaf_value(const mbedtls_lms_parameters_t *params,
+ unsigned char *pub_key,
+ unsigned int r_node_idx,
+ unsigned char *out)
{
psa_hash_operation_t op;
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
size_t output_hash_len;
unsigned char r_node_idx_bytes[4];
- op = psa_hash_operation_init( );
- status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
- if( status != PSA_SUCCESS )
+ op = psa_hash_operation_init();
+ status = psa_hash_setup(&op, PSA_ALG_SHA_256);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- status = psa_hash_update( &op, params->I_key_identifier,
- MBEDTLS_LMOTS_I_KEY_ID_LEN );
- if( status != PSA_SUCCESS )
+ status = psa_hash_update(&op, params->I_key_identifier,
+ MBEDTLS_LMOTS_I_KEY_ID_LEN);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
- status = psa_hash_update( &op, r_node_idx_bytes, 4 );
- if( status != PSA_SUCCESS )
+ mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
+ status = psa_hash_update(&op, r_node_idx_bytes, 4);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN );
- if( status != PSA_SUCCESS )
+ status = psa_hash_update(&op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- status = psa_hash_update( &op, pub_key,
- MBEDTLS_LMOTS_N_HASH_LEN(params->otstype) );
- if( status != PSA_SUCCESS )
+ status = psa_hash_update(&op, pub_key,
+ MBEDTLS_LMOTS_N_HASH_LEN(params->otstype));
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
- &output_hash_len );
- if( status != PSA_SUCCESS )
+ status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
+ &output_hash_len);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
exit:
- psa_hash_abort( &op );
+ psa_hash_abort(&op);
- return ( mbedtls_lms_error_from_psa( status ) );
+ return mbedtls_lms_error_from_psa(status);
}
/* Calculate the value of an internal node of the Merkle tree (which is a hash
@@ -159,143 +165,144 @@
*
* out The output node value, which is a hash output.
*/
-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 )
+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)
{
psa_hash_operation_t op;
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
size_t output_hash_len;
unsigned char r_node_idx_bytes[4];
- op = psa_hash_operation_init( );
- status = psa_hash_setup( &op, PSA_ALG_SHA_256 );
- if( status != PSA_SUCCESS )
+ op = psa_hash_operation_init();
+ status = psa_hash_setup(&op, PSA_ALG_SHA_256);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- status = psa_hash_update( &op, params->I_key_identifier,
- MBEDTLS_LMOTS_I_KEY_ID_LEN );
- if( status != PSA_SUCCESS )
+ status = psa_hash_update(&op, params->I_key_identifier,
+ MBEDTLS_LMOTS_I_KEY_ID_LEN);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- mbedtls_lms_unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
- status = psa_hash_update( &op, r_node_idx_bytes, 4 );
- if( status != PSA_SUCCESS )
+ mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
+ status = psa_hash_update(&op, r_node_idx_bytes, 4);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- status = psa_hash_update( &op, D_INTR_CONSTANT_BYTES, D_CONST_LEN );
- if( status != PSA_SUCCESS )
+ status = psa_hash_update(&op, D_INTR_CONSTANT_BYTES, D_CONST_LEN);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- status = psa_hash_update( &op, left_node,
- MBEDTLS_LMS_M_NODE_BYTES(params->type) );
- if( status != PSA_SUCCESS )
+ status = psa_hash_update(&op, left_node,
+ MBEDTLS_LMS_M_NODE_BYTES(params->type));
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- status = psa_hash_update( &op, right_node,
- MBEDTLS_LMS_M_NODE_BYTES(params->type) );
- if( status != PSA_SUCCESS )
+ status = psa_hash_update(&op, right_node,
+ MBEDTLS_LMS_M_NODE_BYTES(params->type));
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
- status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
- &output_hash_len );
- if( status != PSA_SUCCESS )
+ status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
+ &output_hash_len);
+ if (status != PSA_SUCCESS) {
goto exit;
+ }
exit:
- psa_hash_abort( &op );
+ psa_hash_abort(&op);
- return( mbedtls_lms_error_from_psa( status ) );
+ return mbedtls_lms_error_from_psa(status);
}
-void mbedtls_lms_public_init( mbedtls_lms_public_t *ctx )
+void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)
{
- memset( ctx, 0, sizeof( *ctx ) ) ;
+ memset(ctx, 0, sizeof(*ctx));
}
-void mbedtls_lms_public_free( mbedtls_lms_public_t *ctx )
+void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx)
{
- mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
+ mbedtls_platform_zeroize(ctx, sizeof(*ctx));
}
-int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
- const unsigned char *key, size_t key_size )
+int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx,
+ const unsigned char *key, size_t key_size)
{
mbedtls_lms_algorithm_type_t type;
mbedtls_lmots_algorithm_type_t otstype;
- type = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
- key + PUBLIC_KEY_TYPE_OFFSET );
- if( type != MBEDTLS_LMS_SHA256_M32_H10 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ type = mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
+ key + PUBLIC_KEY_TYPE_OFFSET);
+ if (type != MBEDTLS_LMS_SHA256_M32_H10) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
ctx->params.type = type;
- if( key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- otstype = mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
- key + PUBLIC_KEY_OTSTYPE_OFFSET );
- if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ otstype = mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
+ key + PUBLIC_KEY_OTSTYPE_OFFSET);
+ if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
ctx->params.otstype = otstype;
- memcpy( ctx->params.I_key_identifier,
- key + PUBLIC_KEY_I_KEY_ID_OFFSET,
- MBEDTLS_LMOTS_I_KEY_ID_LEN );
- memcpy( ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
- MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
+ memcpy(ctx->params.I_key_identifier,
+ key + PUBLIC_KEY_I_KEY_ID_OFFSET,
+ MBEDTLS_LMOTS_I_KEY_ID_LEN);
+ memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
+ MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
ctx->have_public_key = 1;
- return( 0 );
+ return 0;
}
-int mbedtls_lms_export_public_key( const mbedtls_lms_public_t *ctx,
- unsigned char *key,
- size_t key_size, size_t *key_len )
+int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
+ unsigned char *key,
+ size_t key_size, size_t *key_len)
{
- if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type) )
- {
- return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
+ if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
+ return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
}
- if( ! ctx->have_public_key )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (!ctx->have_public_key) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
mbedtls_lms_unsigned_int_to_network_bytes(
- ctx->params.type,
- MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET );
- mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.otstype,
- MBEDTLS_LMOTS_TYPE_LEN,
- key + PUBLIC_KEY_OTSTYPE_OFFSET );
- memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET,
- ctx->params.I_key_identifier,
- MBEDTLS_LMOTS_I_KEY_ID_LEN );
- memcpy( key +PUBLIC_KEY_ROOT_NODE_OFFSET,
- ctx->T_1_pub_key,
- MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) );
+ ctx->params.type,
+ MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET);
+ mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.otstype,
+ MBEDTLS_LMOTS_TYPE_LEN,
+ key + PUBLIC_KEY_OTSTYPE_OFFSET);
+ memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
+ ctx->params.I_key_identifier,
+ MBEDTLS_LMOTS_I_KEY_ID_LEN);
+ memcpy(key +PUBLIC_KEY_ROOT_NODE_OFFSET,
+ ctx->T_1_pub_key,
+ MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
- if( key_len != NULL )
- {
+ if (key_len != NULL) {
*key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
}
- return( 0 );
+ return 0;
}
-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 )
+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)
{
unsigned int q_leaf_identifier;
unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
@@ -303,124 +310,114 @@
unsigned int height;
unsigned int curr_node_id;
unsigned int parent_node_id;
- const unsigned char* left_node;
- const unsigned char* right_node;
+ const unsigned char *left_node;
+ const unsigned char *right_node;
mbedtls_lmots_parameters_t ots_params;
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
- if( ! ctx->have_public_key )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (!ctx->have_public_key) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( ctx->params.type
- != MBEDTLS_LMS_SHA256_M32_H10 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (ctx->params.type
+ != MBEDTLS_LMS_SHA256_M32_H10) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( ctx->params.otstype
- != MBEDTLS_LMOTS_SHA256_N32_W8 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (ctx->params.otstype
+ != MBEDTLS_LMOTS_SHA256_N32_W8) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
- {
- return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
+ if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
+ return MBEDTLS_ERR_LMS_VERIFY_FAILED;
}
- if( sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN )
- {
- return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
+ if (sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
+ return MBEDTLS_ERR_LMS_VERIFY_FAILED;
}
- if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN,
- sig + SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET )
- != MBEDTLS_LMOTS_SHA256_N32_W8 )
- {
- return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
+ if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
+ sig + SIG_OTS_SIG_OFFSET +
+ MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
+ != MBEDTLS_LMOTS_SHA256_N32_W8) {
+ return MBEDTLS_ERR_LMS_VERIFY_FAILED;
}
- if( sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN )
- {
- return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
+ if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
+ return MBEDTLS_ERR_LMS_VERIFY_FAILED;
}
- if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN,
- sig + SIG_TYPE_OFFSET(ctx->params.otstype))
- != MBEDTLS_LMS_SHA256_M32_H10 )
- {
- return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
+ if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
+ sig + SIG_TYPE_OFFSET(ctx->params.otstype))
+ != MBEDTLS_LMS_SHA256_M32_H10) {
+ return MBEDTLS_ERR_LMS_VERIFY_FAILED;
}
q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
- MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET );
+ MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET);
- if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
- {
- return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
+ if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
+ return MBEDTLS_ERR_LMS_VERIFY_FAILED;
}
- memcpy( ots_params.I_key_identifier,
- ctx->params.I_key_identifier,
- MBEDTLS_LMOTS_I_KEY_ID_LEN );
- mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
+ memcpy(ots_params.I_key_identifier,
+ ctx->params.I_key_identifier,
+ MBEDTLS_LMOTS_I_KEY_ID_LEN);
+ mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
- ots_params.q_leaf_identifier );
+ ots_params.q_leaf_identifier);
ots_params.type = ctx->params.otstype;
- ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg,
- msg_size, sig + SIG_OTS_SIG_OFFSET,
- MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), Kc_candidate_ots_pub_key,
- sizeof( Kc_candidate_ots_pub_key ), NULL );
- if( ret != 0 )
- {
- return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
+ ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
+ msg,
+ msg_size,
+ sig + SIG_OTS_SIG_OFFSET,
+ MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
+ Kc_candidate_ots_pub_key,
+ sizeof(Kc_candidate_ots_pub_key),
+ NULL);
+ if (ret != 0) {
+ return MBEDTLS_ERR_LMS_VERIFY_FAILED;
}
create_merkle_leaf_value(
- &ctx->params,
- Kc_candidate_ots_pub_key,
- MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
- Tc_candidate_root_node );
+ &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(ctx->params.type) +
q_leaf_identifier;
- for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
- 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 )
- {
+ if (curr_node_id & 1) {
left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
right_node = Tc_candidate_root_node;
- }
- else
- {
+ } else {
left_node = Tc_candidate_root_node;
right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
}
- create_merkle_internal_value( &ctx->params, 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_LMS_M_NODE_BYTES(ctx->params.type)) )
- {
- return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
+ if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
+ MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
+ return MBEDTLS_ERR_LMS_VERIFY_FAILED;
}
- return( 0 );
+ return 0;
}
#if defined(MBEDTLS_LMS_PRIVATE)
@@ -439,47 +436,48 @@
* because the Merkle tree root is 1-indexed, the 0
* index tree node is never used.
*/
-static int calculate_merkle_tree( const mbedtls_lms_private_t *ctx,
- unsigned char *tree )
+static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
+ 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;
+ for (priv_key_idx = 0;
priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
- priv_key_idx++ )
- {
+ priv_key_idx++) {
r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_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 != 0 )
- {
- return( ret );
+ 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 != 0) {
+ return ret;
}
}
/* 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(ctx->params.type) - 1;
+ 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,
- &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 != 0 )
- {
- return( ret );
+ 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 != 0) {
+ return ret;
}
}
- return( 0 );
+ return 0;
}
/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
@@ -494,9 +492,9 @@
*
* path The output path, which is H hash outputs.
*/
-static int get_merkle_path( mbedtls_lms_private_t *ctx,
- unsigned int leaf_node_id,
- unsigned char *path )
+static int get_merkle_path(mbedtls_lms_private_t *ctx,
+ unsigned int leaf_node_id,
+ unsigned char *path)
{
const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
unsigned int curr_node_id = leaf_node_id;
@@ -505,254 +503,229 @@
unsigned int height;
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
- tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(ctx->params.type),
- node_bytes );
- if ( tree == NULL )
- {
+ tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(ctx->params.type),
+ node_bytes);
+ if (tree == NULL) {
return MBEDTLS_ERR_LMS_ALLOC_FAILED;
}
- ret = calculate_merkle_tree( ctx, tree );
- if( ret != 0 )
- {
+ ret = calculate_merkle_tree(ctx, tree);
+ if (ret != 0) {
goto exit;
}
- for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
- height++ )
- {
+ for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
+ height++) {
adjacent_node_id = curr_node_id ^ 1;
- memcpy( &path[height * node_bytes],
- &tree[adjacent_node_id * node_bytes], node_bytes );
+ memcpy(&path[height * node_bytes],
+ &tree[adjacent_node_id * node_bytes], node_bytes);
- curr_node_id >>=1;
+ curr_node_id >>= 1;
}
ret = 0;
exit:
- mbedtls_platform_zeroize( tree, node_bytes *
- MERKLE_TREE_NODE_AM(ctx->params.type) );
- mbedtls_free ( tree );
+ mbedtls_platform_zeroize(tree, node_bytes *
+ MERKLE_TREE_NODE_AM(ctx->params.type));
+ mbedtls_free(tree);
- return( ret );
+ return ret;
}
-void mbedtls_lms_private_init( mbedtls_lms_private_t *ctx )
+void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
{
- memset( ctx, 0, sizeof( *ctx ) ) ;
+ memset(ctx, 0, sizeof(*ctx));
}
-void mbedtls_lms_private_free( mbedtls_lms_private_t *ctx )
+void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
{
unsigned int idx;
- if( ctx->have_private_key )
- {
- if( ctx->ots_private_keys != NULL )
- {
- for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
- {
- mbedtls_lmots_private_free( &ctx->ots_private_keys[idx] );
+ if (ctx->have_private_key) {
+ if (ctx->ots_private_keys != NULL) {
+ for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
+ mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
}
}
- if( ctx->ots_public_keys != NULL )
- {
- for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
- {
- mbedtls_lmots_public_free( &ctx->ots_public_keys[idx] );
+ if (ctx->ots_public_keys != NULL) {
+ for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
+ mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
}
}
- mbedtls_free( ctx->ots_private_keys );
- mbedtls_free( ctx->ots_public_keys );
+ mbedtls_free(ctx->ots_private_keys);
+ mbedtls_free(ctx->ots_public_keys);
}
- mbedtls_platform_zeroize( ctx, sizeof( *ctx ) );
+ mbedtls_platform_zeroize(ctx, sizeof(*ctx));
}
-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),
- void* p_rng, const unsigned char *seed,
- size_t seed_size )
+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),
+ void *p_rng, const unsigned char *seed,
+ size_t seed_size)
{
unsigned int idx = 0;
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
- if( type != MBEDTLS_LMS_SHA256_M32_H10 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (type != MBEDTLS_LMS_SHA256_M32_H10) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( ctx->have_private_key )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (ctx->have_private_key) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
ctx->params.type = type;
ctx->params.otstype = otstype;
ctx->have_private_key = 1;
- ret = f_rng( p_rng,
- ctx->params.I_key_identifier,
- MBEDTLS_LMOTS_I_KEY_ID_LEN );
- if( ret != 0 )
- {
+ ret = f_rng(p_rng,
+ ctx->params.I_key_identifier,
+ MBEDTLS_LMOTS_I_KEY_ID_LEN);
+ if (ret != 0) {
goto exit;
}
/* Requires a cast to size_t to avoid an implicit cast warning on certain
* platforms (particularly Windows) */
- ctx->ots_private_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
- sizeof( *ctx->ots_private_keys ) );
- if( ctx->ots_private_keys == NULL )
- {
+ ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
+ sizeof(*ctx->ots_private_keys));
+ if (ctx->ots_private_keys == NULL) {
ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
goto exit;
}
/* Requires a cast to size_t to avoid an implicit cast warning on certain
* platforms (particularly Windows) */
- ctx->ots_public_keys = mbedtls_calloc( ( size_t )MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
- sizeof( *ctx->ots_public_keys ) );
- if( ctx->ots_public_keys == NULL )
- {
+ ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
+ sizeof(*ctx->ots_public_keys));
+ if (ctx->ots_public_keys == NULL) {
ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
goto exit;
}
- for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++ )
- {
- mbedtls_lmots_private_init( &ctx->ots_private_keys[idx] );
- mbedtls_lmots_public_init( &ctx->ots_public_keys[idx] );
+ for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
+ mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
+ mbedtls_lmots_public_init(&ctx->ots_public_keys[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,
- ctx->params.I_key_identifier,
- idx, seed, seed_size );
- if( ret != 0 )
+ 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,
+ ctx->params.I_key_identifier,
+ idx, seed, seed_size);
+ if (ret != 0) {
goto exit;
+ }
- ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx],
- &ctx->ots_private_keys[idx] );
- if( ret != 0 )
+ ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
+ &ctx->ots_private_keys[idx]);
+ if (ret != 0) {
goto exit;
+ }
}
ctx->q_next_usable_key = 0;
exit:
- if( ret != 0 )
- {
+ if (ret != 0) {
mbedtls_lms_private_free(ctx);
}
- return( ret );
+ return ret;
}
-int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
- const mbedtls_lms_private_t *priv_ctx )
+int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
+ const mbedtls_lms_private_t *priv_ctx)
{
const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned char *tree = NULL;
- if( ! priv_ctx->have_private_key )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (!priv_ctx->have_private_key) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( priv_ctx->params.type
- != MBEDTLS_LMS_SHA256_M32_H10 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (priv_ctx->params.type
+ != MBEDTLS_LMS_SHA256_M32_H10) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( priv_ctx->params.otstype
- != MBEDTLS_LMOTS_SHA256_N32_W8 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (priv_ctx->params.otstype
+ != MBEDTLS_LMOTS_SHA256_N32_W8) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(priv_ctx->params.type),
- node_bytes );
- if ( tree == NULL )
- {
+ tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(priv_ctx->params.type),
+ node_bytes);
+ if (tree == NULL) {
return MBEDTLS_ERR_LMS_ALLOC_FAILED;
}
- memcpy( &ctx->params, &priv_ctx->params,
- sizeof( mbedtls_lmots_parameters_t ) );
+ memcpy(&ctx->params, &priv_ctx->params,
+ sizeof(mbedtls_lmots_parameters_t));
- ret = calculate_merkle_tree( priv_ctx, tree );
- if( ret != 0 )
- {
+ ret = calculate_merkle_tree(priv_ctx, tree);
+ if (ret != 0) {
goto exit;
}
/* Root node is always at position 1, due to 1-based indexing */
- memcpy( ctx->T_1_pub_key, &tree[node_bytes], node_bytes );
+ memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
ctx->have_public_key = 1;
ret = 0;
exit:
- mbedtls_platform_zeroize( tree, node_bytes *
- MERKLE_TREE_NODE_AM(priv_ctx->params.type) );
- mbedtls_free ( tree );
+ mbedtls_platform_zeroize(tree, node_bytes *
+ MERKLE_TREE_NODE_AM(priv_ctx->params.type));
+ mbedtls_free(tree);
- return( ret );
+ return ret;
}
-int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
- int (*f_rng)(void *, unsigned char *, size_t),
- void* p_rng, const unsigned char *msg,
- unsigned int msg_size, unsigned char *sig, size_t sig_size,
- size_t *sig_len )
+int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
+ int (*f_rng)(void *, unsigned char *, size_t),
+ void *p_rng, const unsigned char *msg,
+ unsigned int msg_size, unsigned char *sig, size_t sig_size,
+ size_t *sig_len)
{
uint32_t q_leaf_identifier;
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
- if( ! ctx->have_private_key )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (!ctx->have_private_key) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) )
- {
- return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL );
+ if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
+ return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
}
- if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( ctx->params.otstype
- != MBEDTLS_LMOTS_SHA256_N32_W8 )
- {
- return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (ctx->params.otstype
+ != MBEDTLS_LMOTS_SHA256_N32_W8) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type) )
- {
- return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS );
+ if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
+ return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
}
@@ -762,44 +735,44 @@
*/
ctx->q_next_usable_key += 1;
- if ( MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
- < SIG_OTS_SIG_OFFSET )
- {
- return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
+ if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
+ < SIG_OTS_SIG_OFFSET) {
+ return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
}
- ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier],
- f_rng, p_rng, msg, msg_size,
- sig + SIG_OTS_SIG_OFFSET,
- MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
- NULL );
- if( ret != 0 )
- {
- return( ret );
+ ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
+ f_rng,
+ p_rng,
+ msg,
+ msg_size,
+ sig + SIG_OTS_SIG_OFFSET,
+ MBEDTLS_LMS_SIG_LEN(ctx->params.type,
+ ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
+ NULL);
+ if (ret != 0) {
+ return ret;
}
- mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type,
- MBEDTLS_LMS_TYPE_LEN,
- sig + SIG_TYPE_OFFSET(ctx->params.otstype) );
- mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier,
- MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
- sig + SIG_Q_LEAF_ID_OFFSET );
+ mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
+ MBEDTLS_LMS_TYPE_LEN,
+ sig + SIG_TYPE_OFFSET(ctx->params.otstype));
+ mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
+ MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
+ sig + SIG_Q_LEAF_ID_OFFSET);
- ret = get_merkle_path( ctx,
- MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
- sig + SIG_PATH_OFFSET(ctx->params.otstype) );
- if( ret != 0 )
- {
- return( ret );
+ ret = get_merkle_path(ctx,
+ MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
+ sig + SIG_PATH_OFFSET(ctx->params.otstype));
+ if (ret != 0) {
+ return ret;
}
- if( sig_len != NULL )
- {
+ if (sig_len != NULL) {
*sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
}
- return( 0 );
+ return 0;
}
#endif /* defined(MBEDTLS_LMS_PRIVATE) */