Skip to content

Commit

Permalink
Fix LMS crash
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Ashley <[email protected]>
  • Loading branch information
ashman-p committed Nov 19, 2024
1 parent 507d030 commit 764f62d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/sig_stfl/lms/external/hss_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ struct hss_working_key *allocate_working_key(
/* Assign the memory target to a *signed* variable; signed so that it */
/* can take on negative values meaningfully (to account for cases where */
/* we are "overbudget") */
unsigned long mem_target;
signed long mem_target;
if (memory_target > LONG_MAX) {
mem_target = LONG_MAX;
} else {
mem_target = (unsigned long)memory_target;
mem_target = (signed long)memory_target;
}
#if 0
signed long initial_mem_target = mem_target; /* DEBUG HACK */
Expand All @@ -179,7 +179,7 @@ signed long initial_mem_target = mem_target; /* DEBUG HACK */
info->error_code = hss_error_out_of_memory;
return NULL;
}
mem_target -= (unsigned long)sizeof(*w) + MALLOC_OVERHEAD;
mem_target -= (signed long)sizeof(*w) + MALLOC_OVERHEAD;
unsigned i;
w->levels = levels;
w->status = hss_error_key_uninitialized; /* Not usable until we see a */
Expand Down Expand Up @@ -221,13 +221,13 @@ signed long initial_mem_target = mem_target; /* DEBUG HACK */
info->error_code = hss_error_out_of_memory;
return 0;
}
mem_target -= (unsigned long)w->signed_pk_len[i] + MALLOC_OVERHEAD;
mem_target -= (signed long)w->signed_pk_len[i] + MALLOC_OVERHEAD;
}
w->signature_len = signature_len;

/* Also account for the overhead for the stack allocation (the memory */
/* used by the stack will be accounted as a part of the tree level size */
mem_target -= (unsigned long)MALLOC_OVERHEAD;
mem_target -= (signed long)MALLOC_OVERHEAD;

/*
* Plot out how many subtree sizes we have at each level. We start by
Expand Down Expand Up @@ -306,7 +306,7 @@ signed long initial_mem_target = mem_target; /* DEBUG HACK */
level_height[i], hash_size[i], &subtree_levels[i],
&stack_used );

mem_target -= (unsigned long)mem;
mem_target -= (signed long)mem;
stack_usage += stack_used;
}

Expand Down Expand Up @@ -362,7 +362,7 @@ signed long initial_mem_target = mem_target; /* DEBUG HACK */
/* This is a signed type so that the comparison works as */
/* expected if mem_target is negative */
size_t stack_used;
unsigned long mem = (unsigned long)compute_level_memory_usage(i, j,
signed long mem = (unsigned long)compute_level_memory_usage(i, j,
level_height[i], hash_size[i], &subtree_levels[i],
&stack_used );
/* # of sublevels this would have */
Expand Down
6 changes: 3 additions & 3 deletions src/sig_stfl/lms/external/hss_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ bool hss_generate_signature(
unsigned merkle_levels_below = 0;
int switch_merkle = w->levels;
struct merkle_level *tree;
for (i = w->levels; i>=1; i--, merkle_levels_below += tree->level) {
tree = w->tree[i-1];
for (i = w->levels-1; i>=1; i--, merkle_levels_below += tree->level) {
tree = w->tree[i];

if (0 == (cur_count & (((sequence_t)1 << (merkle_levels_below + tree->level))-1))) {
/* We exhausted this tree */
Expand All @@ -608,7 +608,7 @@ bool hss_generate_signature(
}

/* Remember we'll need to switch to the NEXT_TREE */
switch_merkle = i-1;
switch_merkle = i;
continue;
}

Expand Down

0 comments on commit 764f62d

Please sign in to comment.