Skip to content

Commit 3fe534e

Browse files
authored
Merge pull request wolfSSL#9403 from gojimmypi/pr-lms-unary-fix
Fix LMS C4146 unary minus warning in MSVC, new param check
2 parents 6914f08 + ca920ed commit 3fe534e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

wolfcrypt/src/wc_lms_impl.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,7 +2661,7 @@ static int wc_hss_expand_private_key(LmsState* state, byte* priv,
26612661
word32 q32;
26622662
byte* priv_q;
26632663
byte* priv_seed_i;
2664-
int i;
2664+
word32 i;
26652665

26662666
/* Get the 64-bit q value from the raw private key. */
26672667
ato64(priv_raw, &q);
@@ -2685,7 +2685,7 @@ static int wc_hss_expand_private_key(LmsState* state, byte* priv,
26852685
}
26862686

26872687
/* Compute SEED and I for rest of levels. */
2688-
for (i = 1; (ret == 0) && (i < params->levels); i++) {
2688+
for (i = 1U; (ret == 0) && (i < params->levels); i++) {
26892689
/* Don't skip calculating SEED and I. */
26902690
int skip = 0;
26912691

@@ -2752,11 +2752,12 @@ static int wc_lms_next_subtree_init(LmsState* state, LmsPrivState* privState,
27522752
priv += LMS_I_LEN;
27532753

27542754
ato32(curr, &pq);
2755-
pq = (pq + 1) & (((word32)1 << params->height) - 1);
2755+
pq = (pq + 1U) & ((((word32)1U) << params->height) - (word32)1U);
27562756
c32toa(pq, priv_q);
27572757

27582758
privState->stack.offset = 0;
2759-
privState->leaf.idx = (word32)-((word32)1 << params->cacheBits);
2759+
/* No unary minus on unsigned; avoids MSVC C4146 and passes clang-tidy */
2760+
privState->leaf.idx = (word32)(0U - ((word32)1U << params->cacheBits));
27602761
privState->leaf.offset = 0;
27612762

27622763
/* Derive SEED and I for next tree. */
@@ -2789,7 +2790,7 @@ static int wc_hss_next_subtree_inc(LmsState* state, HssPrivKey* priv_key,
27892790
w64wrapper p64 = q64;
27902791
byte tmp_priv[LMS_PRIV_LEN(LMS_MAX_NODE_LEN)];
27912792
int use_tmp = 0;
2792-
int lastQMax = 0;
2793+
word32 lastQMax = 0;
27932794
w64wrapper p64_hi;
27942795
w64wrapper q64_hi;
27952796

@@ -2807,7 +2808,7 @@ static int wc_hss_next_subtree_inc(LmsState* state, HssPrivKey* priv_key,
28072808
cp64_hi = w64ShiftRight(p64, (params->levels - i - 1) * params->height);
28082809
cq64_hi = w64ShiftRight(q64, (params->levels - i - 1) * params->height);
28092810
/* Get the q for the child. */
2810-
ato32(curr + LMS_PRIV_LEN(params->hash_len), &qc);
2811+
ato32(curr + LMS_PRIV_LEN(params->hash_len), (unsigned int*)&qc);
28112812

28122813
/* Compare index of parent node with previous value. */
28132814
if (w64LT(p64_hi, q64_hi)) {
@@ -2842,7 +2843,7 @@ static int wc_hss_next_subtree_inc(LmsState* state, HssPrivKey* priv_key,
28422843
XMEMCPY(tmp_priv, curr + LMS_PRIV_LEN(params->hash_len), LMS_Q_LEN);
28432844
}
28442845

2845-
lastQMax = (qc == ((word32)1 << params->height) - 1);
2846+
lastQMax = (qc == (((word32)1U << params->height) - (word32)1U));
28462847
curr += LMS_PRIV_LEN(params->hash_len);
28472848
priv += LMS_PRIV_LEN(params->hash_len);
28482849
p64_hi = cp64_hi;
@@ -3179,6 +3180,11 @@ int wc_hss_reload_key(LmsState* state, const byte* priv_raw,
31793180

31803181
(void)pub_root;
31813182

3183+
/* Defend against undefined shifts; LmsParams* params = state->params */
3184+
if ((state->params->cacheBits >= 32U) || (state->params->height >= 32U)) {
3185+
return BAD_FUNC_ARG;
3186+
}
3187+
31823188
wc_hss_priv_data_load(state->params, priv_key, priv_data);
31833189
#ifndef WOLFSSL_WC_LMS_SMALL
31843190
priv_key->inited = 0;

0 commit comments

Comments
 (0)