From f5632d3efca722296ec59a7c7d99773b36b8708e Mon Sep 17 00:00:00 2001 From: Raef Coles Date: Thu, 1 Sep 2022 09:56:52 +0100 Subject: [PATCH] Remove MBEDTLS_PRIVATE usage from LMS and LMOTS Signed-off-by: Raef Coles --- library/lmots.c | 92 ++++++++++++++++++------------------ library/lms.c | 122 ++++++++++++++++++++++++------------------------ 2 files changed, 107 insertions(+), 107 deletions(-) diff --git a/library/lmots.c b/library/lmots.c index e2f86e62d2..74191ebbca 100644 --- a/library/lmots.c +++ b/library/lmots.c @@ -118,13 +118,13 @@ static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *p if ( ret != 0 ) goto exit; - status = psa_hash_update( &op, params->MBEDTLS_PRIVATE(I_key_identifier), + status = psa_hash_update( &op, params->I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN ); ret = mbedtls_lms_error_from_psa( status ); if ( ret != 0 ) goto exit; - status = psa_hash_update( &op, params->MBEDTLS_PRIVATE(q_leaf_identifier), + status = psa_hash_update( &op, params->q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); ret = mbedtls_lms_error_from_psa( status ); if ( ret != 0 ) @@ -199,14 +199,14 @@ static int hash_digit_array( const mbedtls_lmots_parameters_t *params, goto exit; status = psa_hash_update( &op, - params->MBEDTLS_PRIVATE(I_key_identifier), + params->I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN ); ret = mbedtls_lms_error_from_psa( status ); if ( ret != 0 ) goto exit; status = psa_hash_update( &op, - params->MBEDTLS_PRIVATE(q_leaf_identifier), + params->q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); ret = mbedtls_lms_error_from_psa( status ); if ( ret != 0 ) @@ -268,13 +268,13 @@ static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t goto exit; status = psa_hash_update( &op, - params->MBEDTLS_PRIVATE(I_key_identifier), + params->I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN ); ret = mbedtls_lms_error_from_psa( status ); if ( ret != 0 ) goto exit; - status = psa_hash_update( &op, params->MBEDTLS_PRIVATE(q_leaf_identifier), + status = psa_hash_update( &op, params->q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); ret = mbedtls_lms_error_from_psa( status ); if ( ret != 0 ) @@ -335,21 +335,21 @@ int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx, return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type) = + ctx->params.type = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN, key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ); - memcpy( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + memcpy( ctx->params.I_key_identifier, key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET, MBEDTLS_LMOTS_I_KEY_ID_LEN ); - memcpy( ctx->MBEDTLS_PRIVATE(MBEDTLS_PRIVATE(params).q_leaf_identifier), + memcpy( ctx->params.q_leaf_identifier, key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET, MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); - memcpy( ctx->MBEDTLS_PRIVATE(public_key), + memcpy( ctx->public_key, key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, MBEDTLS_LMOTS_N_HASH_LEN ); - ctx->MBEDTLS_PRIVATE(have_public_key) = 1; + ctx->have_public_key = 1; return( 0 ); } @@ -421,12 +421,12 @@ int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg, return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - if ( !ctx->MBEDTLS_PRIVATE(have_public_key) ) + if ( !ctx->have_public_key ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - if( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE( type ) + if( ctx->params.MBEDTLS_PRIVATE( type ) != MBEDTLS_LMOTS_SHA256_N32_W8 ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); @@ -438,7 +438,7 @@ int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg, return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); } - ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->MBEDTLS_PRIVATE(params), + ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params, msg, msg_size, sig, sig_size, Kc_public_key_candidate, MBEDTLS_LMOTS_N_HASH_LEN, @@ -448,8 +448,8 @@ int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg, return( ret ); } - if ( memcmp( &Kc_public_key_candidate, ctx->MBEDTLS_PRIVATE(public_key), - sizeof( ctx->MBEDTLS_PRIVATE(public_key) ) ) ) + if ( memcmp( &Kc_public_key_candidate, ctx->public_key, + sizeof( ctx->public_key ) ) ) { return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); } @@ -482,7 +482,7 @@ int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx, unsigned char const_bytes[1]; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; - if ( ctx->MBEDTLS_PRIVATE(have_private_key) ) + if ( ctx->have_private_key ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } @@ -491,15 +491,15 @@ int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx, return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type) = type; + ctx->params.type = type; - memcpy( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + memcpy( ctx->params.I_key_identifier, I_key_identifier, - sizeof( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier) ) ); + sizeof( ctx->params.I_key_identifier ) ); unsigned_int_to_network_bytes(q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN, - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(q_leaf_identifier) ); + ctx->params.q_leaf_identifier ); unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes ); @@ -512,14 +512,14 @@ int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx, goto exit; ret = psa_hash_update( &op, - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), - sizeof( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier) ) ); + ctx->params.I_key_identifier, + sizeof( ctx->params.I_key_identifier ) ); ret = mbedtls_lms_error_from_psa( status ); if ( ret ) goto exit; status = psa_hash_update( &op, - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(q_leaf_identifier), + ctx->params.q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); ret = mbedtls_lms_error_from_psa( status ); if ( ret ) @@ -542,7 +542,7 @@ int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx, goto exit; status = psa_hash_finish( &op, - ctx->MBEDTLS_PRIVATE(private_key)[i_digit_idx], + ctx->private_key[i_digit_idx], 32, &output_hash_len ); ret = mbedtls_lms_error_from_psa( status ); if ( ret ) @@ -551,7 +551,7 @@ int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx, psa_hash_abort( &op ); } - ctx->MBEDTLS_PRIVATE(have_private_key) = 1; + ctx->have_private_key = 1; exit: if( ret ) @@ -575,29 +575,29 @@ int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx, } /* Check that a private key is loaded */ - if ( !priv_ctx->MBEDTLS_PRIVATE(have_private_key) ) + if ( !priv_ctx->have_private_key ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - ret = hash_digit_array( &priv_ctx->MBEDTLS_PRIVATE(params), - ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(priv_ctx->MBEDTLS_PRIVATE(private_key)), + ret = hash_digit_array( &priv_ctx->params, + ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(priv_ctx->private_key), NULL, NULL, y_hashed_digits ); if ( ret ) { return( ret ); } - ret = public_key_from_hashed_digit_array( &priv_ctx->MBEDTLS_PRIVATE(params), + ret = public_key_from_hashed_digit_array( &priv_ctx->params, ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )y_hashed_digits, - ctx->MBEDTLS_PRIVATE(public_key) ); + ctx->public_key ); if ( ret ) { return( ret ); } - memcpy( &ctx->MBEDTLS_PRIVATE(params), &priv_ctx->MBEDTLS_PRIVATE(params), - sizeof( ctx->MBEDTLS_PRIVATE(params) ) ); + memcpy( &ctx->params, &priv_ctx->params, + sizeof( ctx->params ) ); ctx->MBEDTLS_PRIVATE(have_public_key = 1); @@ -614,24 +614,24 @@ int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx, return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); } - if( ! ctx->MBEDTLS_PRIVATE(have_public_key) ) + if( ! ctx->have_public_key ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - unsigned_int_to_network_bytes( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type), + unsigned_int_to_network_bytes( ctx->params.type, MBEDTLS_LMOTS_TYPE_LEN, key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ); memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET, - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + ctx->params.I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN ); memcpy(key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET, - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(q_leaf_identifier), + ctx->params.q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN); - memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->MBEDTLS_PRIVATE(public_key), + memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key, MBEDTLS_LMOTS_N_HASH_LEN ); if( key_len != NULL ) @@ -662,7 +662,7 @@ int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx, } /* Check that a private key is loaded */ - if ( !ctx->MBEDTLS_PRIVATE(have_private_key) ) + if ( !ctx->have_private_key ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } @@ -673,7 +673,7 @@ int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx, return( ret ); } - ret = create_digit_array_with_checksum( &ctx->MBEDTLS_PRIVATE(params), + ret = create_digit_array_with_checksum( &ctx->params, msg, msg_size, sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_digit_array ); @@ -682,24 +682,24 @@ int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx, return( ret ); } - ret = hash_digit_array( &ctx->MBEDTLS_PRIVATE(params), - ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(ctx->MBEDTLS_PRIVATE(private_key)), + ret = hash_digit_array( &ctx->params, + ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(ctx->private_key), NULL, tmp_digit_array, tmp_sig ); if ( ret ) { return( ret ); } - unsigned_int_to_network_bytes( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type), + unsigned_int_to_network_bytes( ctx->params.type, MBEDTLS_LMOTS_TYPE_LEN, sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ); /* We've got a valid signature now, so it's time to make sure the private * key can't be reused. */ - ctx->MBEDTLS_PRIVATE(have_private_key) = 0; - mbedtls_platform_zeroize(ctx->MBEDTLS_PRIVATE(private_key), - sizeof(ctx->MBEDTLS_PRIVATE(private_key))); + ctx->have_private_key = 0; + mbedtls_platform_zeroize(ctx->private_key, + sizeof(ctx->private_key)); memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET, tmp_sig, MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN); diff --git a/library/lms.c b/library/lms.c index b58aeea7f7..94f5abe5cc 100644 --- a/library/lms.c +++ b/library/lms.c @@ -191,8 +191,8 @@ static int calculate_merkle_tree( mbedtls_lms_private_t *ctx, r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM + priv_key_idx; ret = create_merkle_leaf_node( - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), - ctx->MBEDTLS_PRIVATE(ots_public_keys)[priv_key_idx].MBEDTLS_PRIVATE(public_key), + ctx->params.I_key_identifier, + ctx->ots_public_keys[priv_key_idx].public_key, r_node_idx, tree[r_node_idx] ); if( ret ) { @@ -206,7 +206,7 @@ static int calculate_merkle_tree( mbedtls_lms_private_t *ctx, r_node_idx-- ) { ret = create_merkle_internal_node( - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + ctx->params.I_key_identifier, tree[(r_node_idx * 2)], tree[(r_node_idx * 2 + 1)], r_node_idx, tree[r_node_idx] ); if( ret ) { @@ -271,7 +271,7 @@ int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx, { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type) = type; + ctx->params.type = type; otstype = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET ); @@ -279,15 +279,15 @@ int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx, { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(otstype) = otstype; + ctx->params.otstype = otstype; - memcpy( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + memcpy( ctx->params.I_key_identifier, key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET, MBEDTLS_LMOTS_I_KEY_ID_LEN ); - memcpy( ctx->MBEDTLS_PRIVATE(T_1_pub_key), key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET, + memcpy( ctx->T_1_pub_key, key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET, MBEDTLS_LMOTS_N_HASH_LEN ); - ctx->MBEDTLS_PRIVATE(have_public_key) = 1; + ctx->have_public_key = 1; return( 0 ); } @@ -307,7 +307,7 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx, mbedtls_lmots_parameters_t ots_params; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; - if( ! ctx->MBEDTLS_PRIVATE(have_public_key) ) + if( ! ctx->have_public_key ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } @@ -317,13 +317,13 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx, return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - if( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type) + if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - if( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(otstype) + if( ctx->params.otstype != MBEDTLS_LMOTS_SHA256_N32_W8 ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); @@ -351,13 +351,13 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx, return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); } - memcpy(ots_params.MBEDTLS_PRIVATE(I_key_identifier), - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + memcpy(ots_params.I_key_identifier, + ctx->params.I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN); unsigned_int_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN, - ots_params.MBEDTLS_PRIVATE(q_leaf_identifier) ); - ots_params.type = ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(otstype); + ots_params.q_leaf_identifier ); + ots_params.type = ctx->params.otstype; ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg, msg_size, sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET, @@ -371,7 +371,7 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx, } create_merkle_leaf_node( - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + ctx->params.I_key_identifier, Kc_candidate_ots_pub_key, MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier, Tc_candidate_root_node ); @@ -394,13 +394,13 @@ int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx, } create_merkle_internal_node( - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + ctx->params.I_key_identifier, left_node, right_node, parent_node_id, Tc_candidate_root_node); curr_node_id /= 2; } - if( memcmp( Tc_candidate_root_node, ctx->MBEDTLS_PRIVATE(T_1_pub_key), + if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key, MBEDTLS_LMOTS_N_HASH_LEN) ) { return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); @@ -418,16 +418,16 @@ void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx ) { unsigned int idx; - if( ctx->MBEDTLS_PRIVATE(have_private_key) ) + if( ctx->have_private_key ) { for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ ) { - mbedtls_lmots_free_private( &ctx->MBEDTLS_PRIVATE(ots_private_keys)[idx] ); - mbedtls_lmots_free_public( &ctx->MBEDTLS_PRIVATE(ots_public_keys)[idx] ); + mbedtls_lmots_free_private( &ctx->ots_private_keys[idx] ); + mbedtls_lmots_free_public( &ctx->ots_public_keys[idx] ); } - mbedtls_free( ctx->MBEDTLS_PRIVATE(ots_private_keys) ); - mbedtls_free( ctx->MBEDTLS_PRIVATE(ots_public_keys) ); + mbedtls_free( ctx->ots_private_keys ); + mbedtls_free( ctx->ots_public_keys ); } mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ); @@ -455,29 +455,29 @@ int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx, return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - if( ctx->MBEDTLS_PRIVATE(have_private_key) ) + if( ctx->have_private_key ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type) = type; - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(otstype) = otstype; + ctx->params.type = type; + ctx->params.otstype = otstype; f_rng( p_rng, - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + ctx->params.I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN ); - ctx->MBEDTLS_PRIVATE(ots_private_keys) = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM, + ctx->ots_private_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM, sizeof( mbedtls_lmots_private_t)); - if( ctx->MBEDTLS_PRIVATE(ots_private_keys) == NULL ) + if( ctx->ots_private_keys == NULL ) { ret = MBEDTLS_ERR_LMS_ALLOC_FAILED; goto exit; } - ctx->MBEDTLS_PRIVATE(ots_public_keys) = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM, + ctx->ots_public_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM, sizeof( mbedtls_lmots_public_t)); - if( ctx->MBEDTLS_PRIVATE(ots_public_keys) == NULL ) + if( ctx->ots_public_keys == NULL ) { ret = MBEDTLS_ERR_LMS_ALLOC_FAILED; goto exit; @@ -485,39 +485,39 @@ int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx, for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ ) { - mbedtls_lmots_init_private( &ctx->MBEDTLS_PRIVATE(ots_private_keys)[idx] ); - mbedtls_lmots_init_public( &ctx->MBEDTLS_PRIVATE(ots_public_keys)[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++ ) { - ret = mbedtls_lmots_generate_private_key( &ctx->MBEDTLS_PRIVATE(ots_private_keys)[idx], + ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx], otstype, - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + ctx->params.I_key_identifier, idx, seed, seed_size ); if( ret) goto exit; - ret = mbedtls_lmots_calculate_public_key( &ctx->MBEDTLS_PRIVATE(ots_public_keys)[idx], - &ctx->MBEDTLS_PRIVATE(ots_private_keys)[idx] ); + ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx], + &ctx->ots_private_keys[idx] ); if( ret) goto exit; } - ctx->MBEDTLS_PRIVATE(q_next_usable_key) = 0; - ctx->MBEDTLS_PRIVATE(have_private_key) = 1; + ctx->q_next_usable_key = 0; + ctx->have_private_key = 1; exit: if( ret ) { for ( free_idx = 0; free_idx < idx; free_idx++ ) { - mbedtls_lmots_free_private( &ctx->MBEDTLS_PRIVATE(ots_private_keys)[free_idx] ); - mbedtls_lmots_free_public( &ctx->MBEDTLS_PRIVATE(ots_public_keys)[free_idx] ); + mbedtls_lmots_free_private( &ctx->ots_private_keys[free_idx] ); + mbedtls_lmots_free_public( &ctx->ots_public_keys[free_idx] ); } - mbedtls_free( ctx->MBEDTLS_PRIVATE(ots_private_keys) ); - mbedtls_free( ctx->MBEDTLS_PRIVATE(ots_public_keys) ); + mbedtls_free( ctx->ots_private_keys ); + mbedtls_free( ctx->ots_public_keys ); return( ret ); } @@ -535,19 +535,19 @@ int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx, return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - if( priv_ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type) + if( priv_ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - if( priv_ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(otstype) + if( priv_ctx->params.otstype != MBEDTLS_LMOTS_SHA256_N32_W8 ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - memcpy( &ctx->MBEDTLS_PRIVATE(params), &priv_ctx->MBEDTLS_PRIVATE(params), + memcpy( &ctx->params, &priv_ctx->params, sizeof(mbedtls_lmots_parameters_t) ); ret = calculate_merkle_tree( priv_ctx, tree); @@ -557,9 +557,9 @@ int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx, } /* Root node is always at position 1, due to 1-based indexing */ - memcpy( ctx->MBEDTLS_PRIVATE(T_1_pub_key), &tree[1], MBEDTLS_LMOTS_N_HASH_LEN ); + memcpy( ctx->T_1_pub_key, &tree[1], MBEDTLS_LMOTS_N_HASH_LEN ); - ctx->MBEDTLS_PRIVATE(have_public_key) = 1; + ctx->have_public_key = 1; return( 0 ); } @@ -572,22 +572,22 @@ int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx, unsigned char *key return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); } - if( ! ctx->MBEDTLS_PRIVATE(have_public_key) ) + if( ! ctx->have_public_key ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } unsigned_int_to_network_bytes( - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type), + ctx->params.type, MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET ); unsigned_int_to_network_bytes( - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(otstype), + ctx->params.otstype, MBEDTLS_LMOTS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET ); memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET, - ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), + ctx->params.I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN ); memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET, - ctx->MBEDTLS_PRIVATE(T_1_pub_key), + ctx->T_1_pub_key, MBEDTLS_LMOTS_N_HASH_LEN ); if( key_len != NULL ) { @@ -606,7 +606,7 @@ int mbedtls_lms_sign( mbedtls_lms_private_t *ctx, uint32_t q_leaf_identifier; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; - if( ! ctx->MBEDTLS_PRIVATE(have_private_key) ) + if( ! ctx->have_private_key ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } @@ -616,30 +616,30 @@ int mbedtls_lms_sign( mbedtls_lms_private_t *ctx, return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); } - if( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type) != MBEDTLS_LMS_SHA256_M32_H10 ) + if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - if( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(otstype) + if( ctx->params.otstype != MBEDTLS_LMOTS_SHA256_N32_W8 ) { return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); } - if( ctx->MBEDTLS_PRIVATE(q_next_usable_key) >= MERKLE_TREE_LEAF_NODE_AM ) + if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM ) { return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS ); } - q_leaf_identifier = ctx->MBEDTLS_PRIVATE(q_next_usable_key); + q_leaf_identifier = ctx->q_next_usable_key; /* This new value must _always_ be written back to the disk before the * signature is returned. */ - ctx->MBEDTLS_PRIVATE(q_next_usable_key) += 1; + ctx->q_next_usable_key += 1; - ret = mbedtls_lmots_sign( &ctx->MBEDTLS_PRIVATE(ots_private_keys)[q_leaf_identifier], + 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 ); @@ -648,7 +648,7 @@ int mbedtls_lms_sign( mbedtls_lms_private_t *ctx, return( ret ); } - unsigned_int_to_network_bytes( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type), + unsigned_int_to_network_bytes( ctx->params.type, MBEDTLS_LMS_TYPE_LEN, sig + MBEDTLS_LMS_SIG_TYPE_OFFSET ); unsigned_int_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET);