Skip to content

Commit 36c953d

Browse files
committed
LMS: Cast constants before shifting left
Compiling for 16-bit results in some constants type being too small for shift amount without cast.
1 parent 832e23a commit 36c953d

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

wolfcrypt/src/wc_lms_impl.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ static WC_INLINE int wc_lmots_q_expand(byte* q, word8 n, word8 w, word8 ls,
744744

745745
if (ret == 0) {
746746
/* Start sum with all 2^w - 1s and subtract from that. */
747-
sum = ((1 << w) - 1) * ((n * 8) / w);
747+
sum = (((word16)1 << w) - 1) * ((n * 8) / w);
748748
/* For each byte of the hash. */
749749
for (i = 0; i < n; i++) {
750750
/* Get next byte. */
@@ -1984,7 +1984,7 @@ static int wc_lms_treehash_init(LmsState* state, LmsPrivState* privState,
19841984
/* Copy out top root nodes. */
19851985
if ((h > params->height - params->rootLevels) &&
19861986
((i >> (h-1)) != ((i + 1) >> (h - 1)))) {
1987-
int off = (1 << (params->height - h)) + (i >> h) - 1;
1987+
int off = ((int)1 << (params->height - h)) + (i >> h) - 1;
19881988
XMEMCPY(root + off * params->hash_len, temp, params->hash_len);
19891989
}
19901990

@@ -2135,7 +2135,7 @@ static int wc_lms_treehash_update(LmsState* state, LmsPrivState* privState,
21352135
if ((ret == 0) && (q == 0) && (!useRoot) &&
21362136
(h > params->height - params->rootLevels) &&
21372137
((i >> (h-1)) != ((i + 1) >> (h - 1)))) {
2138-
int off = (1 << (params->height - h)) + (i >> h) - 1;
2138+
int off = ((int)1 << (params->height - h)) + (i >> h) - 1;
21392139
XMEMCPY(privState->root + off * params->hash_len, temp,
21402140
params->hash_len);
21412141
}
@@ -2292,7 +2292,7 @@ static int wc_lms_compute_root(LmsState* state, word32 q, const byte* kc,
22922292
byte* node = ip + LMS_P_LEN;
22932293
byte* b[2][2];
22942294
/* node_num = 2^h + q */
2295-
word32 r = (1 << params->height) + q;
2295+
word32 r = ((word32)1 << params->height) + q;
22962296

22972297
/* tmp = H(I || u32str(node_num) || u16str(D_LEAF) || Kc) */
22982298
c32toa(r, rp);
@@ -2752,11 +2752,11 @@ 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) & ((1 << params->height) - 1);
2755+
pq = (pq + 1) & (((word32)1 << params->height) - 1);
27562756
c32toa(pq, priv_q);
27572757

27582758
privState->stack.offset = 0;
2759-
privState->leaf.idx = (word32)-(1 << params->cacheBits);
2759+
privState->leaf.idx = (word32)-((word32)1 << params->cacheBits);
27602760
privState->leaf.offset = 0;
27612761

27622762
/* Derive SEED and I for next tree. */
@@ -2999,7 +2999,7 @@ static int wc_hss_update_auth_path(LmsState* state, HssPrivKey* priv_key,
29992999
word32 qm1a = LMS_AUTH_PATH_IDX(q - 1, h);
30003000
/* If different then copy in cached hash. */
30013001
if ((qa != qm1a) && (qa > maxq)) {
3002-
int off = (1 << (params->height - h)) + (qa >> h) - 1;
3002+
int off = ((int)1 << (params->height - h)) + (qa >> h) - 1;
30033003
XMEMCPY(privState->auth_path + h * params->hash_len,
30043004
privState->root + off * params->hash_len,
30053005
params->hash_len);

0 commit comments

Comments
 (0)