A variable-length loop is an iterative construct whose iteration count depends on runtime data. In cryptographic code, controlling that count with secret or user-controlled data can expose information through timing and related side channels. section 10.5
The danger is control flow that reveals sensitive values indirectly. OpenSSL 0.9.8o’s elliptic-curve Montgomery ladder leaked how many leading bits of an ECDSA nonce were zero; enough signatures enabled a lattice attack to recover the nonce and private key. section 10.5
Use these implementation rules:
- Avoid iteration bounds derived from secrets or user-controlled data. section 10.5
- Make unavoidable loops execute the same number of times, including when errors occur. section 10.5
- Where appropriate, replace loops with fixed operations or manually unrolled comparisons, accepting possible performance costs. section 10.5
