Skip to content
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

Merged
merged 1 commit into from
Dec 23, 2022

Conversation

mfischer
Copy link
Contributor

@mfischer mfischer commented Nov 12, 2022

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

  • changelog provided, or not required
  • backport done, or not required
  • tests provided, or not required

Notes for the submitter

Please refer to the contributing guidelines, especially the
checklist for PR contributors.

@wernerlewis wernerlewis added enhancement needs-ci Needs to pass CI tests size-s Estimated task size: small (~2d) priority-medium Medium priority - this can be reviewed as time permits labels Nov 14, 2022
@mfischer
Copy link
Contributor Author

library/lms.c Outdated Show resolved Hide resolved
@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review and removed needs-work needs-ci Needs to pass CI tests labels Nov 18, 2022
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a 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.

library/lms.c Outdated Show resolved Hide resolved
library/lms.c Outdated Show resolved Hide resolved
library/lms.c Outdated Show resolved Hide resolved
@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Nov 22, 2022
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]>
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a 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.

@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review and removed needs-work labels Nov 23, 2022
@mprse mprse self-requested a review December 1, 2022 07:46
Copy link
Contributor

@mprse mprse left a 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 );
Copy link
Contributor

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]

Copy link
Contributor Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 );

Copy link
Contributor

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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 );
Copy link
Contributor

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.

Copy link
Contributor Author

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) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mbedtls_free ( tree );
mbedtls_free( tree );

@mprse mprse removed the needs-reviewer This PR needs someone to pick it up for review label Dec 1, 2022
@mfischer
Copy link
Contributor Author

mfischer commented Dec 3, 2022

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.

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).

@gilles-peskine-arm
Copy link
Contributor

@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.

@mfischer mfischer requested a review from mprse December 19, 2022 18:34
Copy link
Contributor

@mprse mprse left a 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.

@mprse mprse added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Dec 21, 2022
@gilles-peskine-arm gilles-peskine-arm merged commit b402e4b into Mbed-TLS:development Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports enhancement priority-medium Medium priority - this can be reviewed as time permits size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants