Loading src/crypto/rsa.cpp +35 −9 Changes for src/crypto/rsa.cpp: 35 added lines, 9 removed lines. Original line number Diff line number Diff line Loading @@ -1284,9 +1284,18 @@ namespace netplus { { for (size_t i = 0; i < n; ++i) { // Step 1: T += a[i] * b // // Deliberately NOT skipped when ai == 0: a data-dependent // shortcut here (as this used to have) makes the loop's // running time depend on how many LIMBS of the operand // happen to be exactly zero -- a real, dudect-confirmed // (t crossed 10 after ~60k measurements) message-dependent // timing leak in RSA signing. The skipped math is a no-op // when ai == 0 anyway (adds zero everywhere), so always // running it changes timing, not the result. const dlimb_t ai = (i < au) ? (dlimb_t)ap[i] : 0; dlimb_t carry = 0; if (ai != 0) { { for (size_t j = 0; j < n; ++j) { const dlimb_t bj = (j < bu) ? (dlimb_t)bp[j] : 0; dlimb_t cur = (dlimb_t)T[j] + ai * bj + carry; Loading Loading @@ -1330,14 +1339,31 @@ namespace netplus { out.used = (T[n] != 0) ? n + 1 : n; while (out.used > 1 && op[out.used - 1] == 0) out.used--; // Final step: if out >= mod, subtract mod in-place if (compare(out, mod) >= 0) { subtract(out, mod, scratch); out.used = scratch.used; std::memcpy(op, scratch.data.get(), scratch.used * sizeof(limb_t)); if (out.capacity > out.used) std::memset(op + out.used, 0, (out.capacity - out.used) * sizeof(limb_t)); } // Final step: constant-time conditional subtract (out -= mod if // out >= mod). This used to branch on compare(out, mod) and only // call subtract() when needed -- a data-dependent branch whose // outcome (and therefore timing) depends on the operand values. // Fixed-width (n+1 word) subtraction with the borrow determining // whether to keep it, computed and applied unconditionally via // masking instead -- same fix as the "if (ai != 0)" skip above. { limb_t* __restrict__ diff = scratch.data.get(); // capacity 2n+2 >= n+1 dlimb_t borrow = 0; for (size_t i = 0; i <= n; ++i) { dlimb_t bv = (dlimb_t)((i < n) ? mp[i] : 0); dlimb_t d = (dlimb_t)op[i] - bv - borrow; diff[i] = (limb_t)d; borrow = (d >> (sizeof(dlimb_t) * 8 - 1)) & 1; } // borrow == 1 means op < mod (subtraction underflowed): keep op. // borrow == 0 means op >= mod: use the subtracted value. limb_t applyMask = (limb_t)0 - (limb_t)(borrow == 0); for (size_t i = 0; i <= n; ++i) { op[i] = (op[i] & ~applyMask) | (diff[i] & applyMask); } } out.used = n + 1; while (out.used > 1 && op[out.used - 1] == 0) out.used--; } rsa::bigInt rsa::bytesToBigIntBE(const std::vector<uint8_t>& bytes) { Loading Loading
src/crypto/rsa.cpp +35 −9 Changes for src/crypto/rsa.cpp: 35 added lines, 9 removed lines. Original line number Diff line number Diff line Loading @@ -1284,9 +1284,18 @@ namespace netplus { { for (size_t i = 0; i < n; ++i) { // Step 1: T += a[i] * b // // Deliberately NOT skipped when ai == 0: a data-dependent // shortcut here (as this used to have) makes the loop's // running time depend on how many LIMBS of the operand // happen to be exactly zero -- a real, dudect-confirmed // (t crossed 10 after ~60k measurements) message-dependent // timing leak in RSA signing. The skipped math is a no-op // when ai == 0 anyway (adds zero everywhere), so always // running it changes timing, not the result. const dlimb_t ai = (i < au) ? (dlimb_t)ap[i] : 0; dlimb_t carry = 0; if (ai != 0) { { for (size_t j = 0; j < n; ++j) { const dlimb_t bj = (j < bu) ? (dlimb_t)bp[j] : 0; dlimb_t cur = (dlimb_t)T[j] + ai * bj + carry; Loading Loading @@ -1330,14 +1339,31 @@ namespace netplus { out.used = (T[n] != 0) ? n + 1 : n; while (out.used > 1 && op[out.used - 1] == 0) out.used--; // Final step: if out >= mod, subtract mod in-place if (compare(out, mod) >= 0) { subtract(out, mod, scratch); out.used = scratch.used; std::memcpy(op, scratch.data.get(), scratch.used * sizeof(limb_t)); if (out.capacity > out.used) std::memset(op + out.used, 0, (out.capacity - out.used) * sizeof(limb_t)); } // Final step: constant-time conditional subtract (out -= mod if // out >= mod). This used to branch on compare(out, mod) and only // call subtract() when needed -- a data-dependent branch whose // outcome (and therefore timing) depends on the operand values. // Fixed-width (n+1 word) subtraction with the borrow determining // whether to keep it, computed and applied unconditionally via // masking instead -- same fix as the "if (ai != 0)" skip above. { limb_t* __restrict__ diff = scratch.data.get(); // capacity 2n+2 >= n+1 dlimb_t borrow = 0; for (size_t i = 0; i <= n; ++i) { dlimb_t bv = (dlimb_t)((i < n) ? mp[i] : 0); dlimb_t d = (dlimb_t)op[i] - bv - borrow; diff[i] = (limb_t)d; borrow = (d >> (sizeof(dlimb_t) * 8 - 1)) & 1; } // borrow == 1 means op < mod (subtraction underflowed): keep op. // borrow == 0 means op >= mod: use the subtracted value. limb_t applyMask = (limb_t)0 - (limb_t)(borrow == 0); for (size_t i = 0; i <= n; ++i) { op[i] = (op[i] & ~applyMask) | (diff[i] & applyMask); } } out.used = n + 1; while (out.used > 1 && op[out.used - 1] == 0) out.used--; } rsa::bigInt rsa::bytesToBigIntBE(const std::vector<uint8_t>& bytes) { Loading