-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lms: Move merkle tree generation to heap allocation #6595
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good to me, just a few minor things.
Larger height (e.g. H=20) trees cannot be put on the stack. Allocate memory for them based on need using mbedtls_calloc(). Signed-off-by: Moritz Fischer <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
For future reference, please don't rebase a pull request during review if it isn't needed. (Needed is, for example, to fix a commit message, or because there's a conflict with the base branch.) Work done in response to change requests should go into new commits (broken down into meaningful pieces, not a giant “addressed review comments” commit). Rebasing makes the reviewer's job more difficult because the differences aren't grouped, it's harder to keep track of where you left off, and it leaves an inaccurate history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally looks good.
Marked only style issues (I don't know if they are important now as we will change code style soon anyway) and left two questions to clarify.
&tree[adjacent_node_id], | ||
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) ); | ||
memcpy( &path[height * node_bytes], | ||
&tree[adjacent_node_id * node_bytes], node_bytes ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the second parameter (source address) has been changed?
&tree[adjacent_node_id]
-> &tree[adjacent_node_id * node_bytes]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type of tree changed, now it's a flat unsigned char array.
@@ -499,13 +498,21 @@ static int get_merkle_path( mbedtls_lms_private_t *ctx, | |||
unsigned int leaf_node_id, | |||
unsigned char *path ) | |||
{ | |||
unsigned char tree[MERKLE_TREE_NODE_AM_MAX][MBEDTLS_LMS_M_NODE_BYTES_MAX]; | |||
const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type); | |
const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES( ctx->params.type ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're switching to the new style at the end of the month (or maybe first week of January). So let's not be picky about it now.
unsigned int height; | ||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||
|
||
ret = calculate_merkle_tree( ctx, ( unsigned char * )tree ); | ||
tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(ctx->params.type), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(ctx->params.type), | |
tree = mbedtls_calloc( MERKLE_TREE_NODE_AM( ctx->params.type ), |
ret = calculate_merkle_tree( ctx, ( unsigned char * )tree ); | ||
tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(ctx->params.type), | ||
node_bytes ); | ||
if ( tree == NULL ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ( tree == NULL ) | |
if( tree == NULL ) | |
return MBEDTLS_ERR_LMS_ALLOC_FAILED; |
|
||
curr_node_id >>=1; | ||
} | ||
|
||
ret = 0; | ||
|
||
exit: | ||
mbedtls_platform_zeroize( tree, sizeof( tree ) ); | ||
mbedtls_platform_zeroize( tree, node_bytes * | ||
MERKLE_TREE_NODE_AM(ctx->params.type) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MERKLE_TREE_NODE_AM(ctx->params.type) ); | |
MERKLE_TREE_NODE_AM( ctx->params.type ) ); |
@@ -679,25 +688,33 @@ int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx, | |||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); | |||
} | |||
|
|||
tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(priv_ctx->params.type), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(priv_ctx->params.type), | |
tree = mbedtls_calloc( MERKLE_TREE_NODE_AM( priv_ctx->params.type ), |
@@ -679,25 +688,33 @@ int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx, | |||
return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); | |||
} | |||
|
|||
tree = mbedtls_calloc( MERKLE_TREE_NODE_AM(priv_ctx->params.type), | |||
node_bytes ); | |||
if ( tree == NULL ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ( tree == NULL ) | |
if ( tree == NULL ) | |
return MBEDTLS_ERR_LMS_ALLOC_FAILED; |
if( ret != 0 ) | ||
{ | ||
goto exit; | ||
} | ||
|
||
/* Root node is always at position 1, due to 1-based indexing */ | ||
memcpy( ctx->T_1_pub_key, &tree[1], | ||
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type) ); | ||
memcpy( ctx->T_1_pub_key, &tree[node_bytes], node_bytes ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the second parameter (source address) has been changed?
&tree[1]
-> &tree[node_bytes]
The comment above informs why index 1
was used here which is not the case now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type of tree changed to a flat unsigned char array. Any suggestions on how to reword the comment?
|
||
ctx->have_public_key = 1; | ||
|
||
ret = 0; | ||
|
||
exit: | ||
mbedtls_platform_zeroize( tree, sizeof( tree ) ); | ||
mbedtls_platform_zeroize( tree, node_bytes * | ||
MERKLE_TREE_NODE_AM(priv_ctx->params.type) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MERKLE_TREE_NODE_AM(priv_ctx->params.type) ); | |
MERKLE_TREE_NODE_AM( priv_ctx->params.type ) ); |
mbedtls_platform_zeroize( tree, sizeof( tree ) ); | ||
mbedtls_platform_zeroize( tree, node_bytes * | ||
MERKLE_TREE_NODE_AM(priv_ctx->params.type) ); | ||
mbedtls_free ( tree ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mbedtls_free ( tree ); | |
mbedtls_free( tree ); |
Thanks for the explanation. In terms of process will you guys then squash the commits together when merging? I'm not super familiar with github flows for this (yet, I'm used to sending patches by email to mailing list). |
@mfischer We don't squash. We want to retain the development history in version control, not just the merge history. It's very useful when you want to understand how the code came to be the way it is. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Style issues ignored.
Larger height (e.g. H=20) trees cannot be put on the stack. Allocate memory for them based on need using mbedtls_calloc().
Signed-off-by: Moritz Fischer [email protected]
Description
This is in preparation of adding H=20 support in a separate PR #6584.
Gatekeeper checklist
Notes for the submitter
Please refer to the contributing guidelines, especially the
checklist for PR contributors.