chore(crypto): hardening against potential side-channel leakage
What changed, and why it matters
This commit hardens cryptographic code in Trezor firmware against two potential side-channel leaks. First, it simplifies how a secret condition value is checked inside an assert statement so the compiler produces only one branch instead of two, reducing the chance that timing or power analysis could reveal whether the value was 0 or 1. Second, it replaces direct table lookups that depended on a secret scalar with a constant-time selection helper that always reads every table entry. The commit message explicitly says the memory-access issue is not exploitable on Trezor because the device has no cache, but the assert issue does affect production firmware because asserts are compiled in.
Treat as a legitimate hardening patch with limited real-world exploitability on current Trezor hardware. Review whether similar assert patterns or secret-dependent table lookups exist elsewhere in the crypto codebase, especially in newer curves or third-party imported code. No urgent security release action is required solely on the basis of this diff, but the patch should be included in the next firmware build.
Security signals we found
Side-channel hardening in constant-time cryptographic primitives
Replacement of double-evaluation volatile assert with single-branch assert
Elimination of secret-dependent memory access in elliptic-curve scalar multiplication
Use of LOCAL_CONFIDENTIAL and memzero for sensitive intermediate point
Commit message states asserts are compiled into production core firmware
Evidence from the diff
The patch modifies crypto/bignum.c, crypto/ecdsa.c, and crypto/elligator2.c. In bignum.c and elligator2.c, assertions of the form assert((int)(cond == 1) | (int)(cond == 0)) are replaced with assert(cond <= 1). The original form evaluates the volatile cond twice and can generate two conditional branches; the new form evaluates it once and yields a single branch. In crypto/ecdsa.c, a new point_select() helper is added. It iterates over all eight entries of a curve_point lookup table and uses bn_cmov to copy only the entry matching the secret index, so memory addresses accessed no longer depend on the secret exponent. point_multiply() and scalar_multiply() are updated to use point_select() before curve_to_jacobian() and point_jacobian_add(). The selected buffer is marked LOCAL_CONFIDENTIAL and zeroed after use.
Changed components
crypto/bignum.c (bn_cmov, bn_cnegate)crypto/ecdsa.c (point_multiply, scalar_multiply, new point_select)crypto/elligator2.c (curve25519_cmov)Inspect captured patch +27 / −9
### crypto/bignum.c
@@ -304,8 +304,7 @@ int bn_is_equal(const bignum256 *x, const bignum256 *y) {
// &truecase == &falsecase or &res == &truecase == &falsecase
void bn_cmov(bignum256 *res, volatile uint32_t cond, const bignum256 *truecase,
const bignum256 *falsecase) {
- // Intentional use of bitwise OR operator to ensure constant-time
- assert((int)(cond == 1) | (int)(cond == 0));
+ assert(cond <= 1);
uint32_t tmask = -cond; // tmask = 0xFFFFFFFF if cond else 0x00000000
uint32_t fmask = ~tmask; // fmask = 0x00000000 if cond else 0xFFFFFFFF
@@ -324,8 +323,7 @@ void bn_cmov(bignum256 *res, volatile uint32_t cond, const bignum256 *truecase,
// Assumes prime is normalized and
// 0 < prime < 2**260 == 2**(BITS_PER_LIMB * LIMBS - 1)
void bn_cnegate(volatile uint32_t cond, bignum256 *x, const bignum256 *prime) {
- // Intentional use of bitwise OR operator to ensure constant time
- assert((int)(cond == 1) | (int)(cond == 0));
+ assert(cond <= 1);
uint32_t tmask = -cond; // tmask = 0xFFFFFFFF if cond else 0x00000000
uint32_t fmask = ~tmask; // fmask = 0x00000000 if cond else 0xFFFFFFFF
### crypto/ecdsa.c
@@ -405,6 +405,18 @@ void point_jacobian_double(jacobian_curve_point *p, const ecdsa_curve *curve) {
bn_fast_mod(&p->y, prime);
}
+// res = table[index] for index < 8, reading every entry, so that the
+// addresses read do not depend on index
+static void point_select(const curve_point table[8], size_t index,
+ curve_point *res) {
+ assert(index < 8);
+ *res = (curve_point){0};
+ for (size_t i = 0; i < 8; i++) {
+ bn_cmov(&res->x, i == index, &table[i].x, &res->x);
+ bn_cmov(&res->y, i == index, &table[i].y, &res->y);
+ }
+}
+
// res = k * p
// returns 0 on success
int point_multiply(const ecdsa_curve *curve, const bignum256 *k,
@@ -426,6 +438,7 @@ int point_multiply(const ecdsa_curve *curve, const bignum256 *k,
uint32_t bits = {0}, sign = {0}, nsign = {0};
LOCAL_CONFIDENTIAL jacobian_curve_point jres;
curve_point pmult[8] = {0};
+ LOCAL_CONFIDENTIAL curve_point selected = {0};
const bignum256 *prime = &curve->prime;
// is_even = 0xffffffff if k is even, 0 otherwise.
@@ -488,7 +501,8 @@ int point_multiply(const ecdsa_curve *curve, const bignum256 *k,
sign = (bits >> 4) - 1;
bits ^= sign;
bits &= 15;
- curve_to_jacobian(&pmult[bits >> 1], &jres, prime);
+ point_select(pmult, bits >> 1, &selected);
+ curve_to_jacobian(&selected, &jres, prime);
for (i = 62; i >= 0; i--) {
// sign = sign(a[i+1]) (0xffffffff for negative, 0 for positive)
// invariant jres = (-1)^sign sum_{j=i+1..63} (a[j] * 16^{j-i-1} * p)
@@ -521,13 +535,15 @@ int point_multiply(const ecdsa_curve *curve, const bignum256 *k,
bn_cnegate((sign ^ nsign) & 1, &jres.z, prime);
// add odd factor
- point_jacobian_add(&pmult[bits >> 1], &jres, curve);
+ point_select(pmult, bits >> 1, &selected);
+ point_jacobian_add(&selected, &jres, curve);
sign = nsign;
}
bn_cnegate(sign & 1, &jres.z, prime);
jacobian_to_curve(&jres, res, prime);
memzero(&a, sizeof(a));
memzero(&jres, sizeof(jres));
+ memzero(&selected, sizeof(selected));
return 0;
}
@@ -548,6 +564,7 @@ int scalar_multiply(const ecdsa_curve *curve, const bignum256 *k,
uint32_t is_even = (k->val[0] & 1) - 1;
uint32_t lowbits = 0;
LOCAL_CONFIDENTIAL jacobian_curve_point jres;
+ LOCAL_CONFIDENTIAL curve_point selected = {0};
const bignum256 *prime = &curve->prime;
// is_even = 0xffffffff if k is even, 0 otherwise.
@@ -595,7 +612,8 @@ int scalar_multiply(const ecdsa_curve *curve, const bignum256 *k,
lowbits = a.val[0] & ((1 << 5) - 1);
lowbits ^= (lowbits >> 4) - 1;
lowbits &= 15;
- curve_to_jacobian(&curve->cp[0][lowbits >> 1], &jres, prime);
+ point_select(curve->cp[0], lowbits >> 1, &selected);
+ curve_to_jacobian(&selected, &jres, prime);
for (i = 1; i < 64; i++) {
// invariant res = sign(a[i-1]) sum_{j=0..i-1} (a[j] * 16^j * G)
@@ -616,12 +634,14 @@ int scalar_multiply(const ecdsa_curve *curve, const bignum256 *k,
bn_cnegate(~lowbits & 1, &jres.y, prime);
// add odd factor
- point_jacobian_add(&curve->cp[i][lowbits >> 1], &jres, curve);
+ point_select(curve->cp[i], lowbits >> 1, &selected);
+ point_jacobian_add(&selected, &jres, curve);
}
bn_cnegate(~(a.val[0] >> 4) & 1, &jres.y, prime);
jacobian_to_curve(&jres, res, prime);
memzero(&a, sizeof(a));
memzero(&jres, sizeof(jres));
+ memzero(&selected, sizeof(selected));
return 0;
}
### crypto/elligator2.c
@@ -36,7 +36,7 @@ static int curve25519_isequal(bignum25519 a, const bignum25519 b) {
// Sets out to a if c equals 0, sets out to b if c equals 1.
static void curve25519_cmov(bignum25519 out, const bignum25519 a,
const bignum25519 b, uint32_t c) {
- assert((int)(c == 1) | (int)(c == 0));
+ assert(c <= 1);
bignum25519 a_copy = {0}, b_copy = {0};
curve25519_copy(a_copy, a);Why this scored 47/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.