Skip to content

Commit 9af907f

Browse files
committed
fix: signed-shift UB in ML-DSA slow reductions
Under MLDSA_MUL_QINV_SLOW / MLDSA_MUL_Q_SLOW, mldsa_mont_red and mldsa_red left-shift signed operands that are routinely negative (NTT zetas), which is undefined behavior. Perform the shifts on word32/word64 and convert back; results are bit-identical. Only opt-in slow-multiply builds compile these paths. Reproduced with -fsanitize=shift --disable-intelasm; clean after this change.
1 parent 9d86960 commit 9af907f

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

wolfcrypt/src/wc_mldsa.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5612,13 +5612,14 @@ static sword32 mldsa_mont_red(sword64 a)
56125612
#ifndef MLDSA_MUL_QINV_SLOW
56135613
sword64 t = (sword32)((sword32)a * (sword32)MLDSA_QINV);
56145614
#else
5615-
sword64 t = (sword32)((sword32)a + (sword32)((sword32)a << 13) -
5616-
(sword32)((sword32)a << 23) + (sword32)((sword32)a << 26));
5615+
sword64 t = (sword32)((word32)a + ((word32)a << 13) -
5616+
((word32)a << 23) + ((word32)a << 26));
56175617
#endif
56185618
#ifndef MLDSA_MUL_Q_SLOW
56195619
return (sword32)((a - ((sword32)t * (sword64)MLDSA_Q)) >> 32);
56205620
#else
5621-
return (sword32)((a - (t << 23) + (t << 13) - t) >> 32);
5621+
return (sword32)((a - (sword64)((word64)t << 23) +
5622+
(sword64)((word64)t << 13) - t) >> 32);
56225623
#endif
56235624
}
56245625

@@ -5642,7 +5643,8 @@ static sword32 mldsa_red(sword32 a)
56425643
#ifndef MLDSA_MUL_Q_SLOW
56435644
return (sword32)(a - (t * MLDSA_Q));
56445645
#else
5645-
return (sword32)(a - (t << 23) + (t << 13) - t);
5646+
return (sword32)((word32)a - ((word32)t << 23) + ((word32)t << 13) -
5647+
(word32)t);
56465648
#endif
56475649
}
56485650
#endif

0 commit comments

Comments
 (0)