Loading src/crypto/rsa.cpp +25 −6 Changes for src/crypto/rsa.cpp: 25 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -810,17 +810,36 @@ namespace netplus { bigInt res_bar(n + 2); montgomeryMultiply_into(one, R2, mod, n_prime, scratch, res_bar); // Exponentiation (left-to-right square-and-multiply) — zero allocations // Exponentiation (left-to-right square-and-multiply) — zero allocations. // // Both the square and the square-then-multiply are ALWAYS computed; // the result is chosen via constant-time select (data words AND the // .used length) instead of branching on exp.isBitSet(i) to decide // whether to multiply at all. The previous // "if (exp.isBitSet(i)) { multiply; }" is the textbook Kocher (1996) // RSA timing attack: an attacker who can time many signatures under // the same private key can recover d's bits from which iterations // took the extra multiply. bigInt tempMul(n + 2); for (size_t i = exp.bitLength(); i-- > 0; ) { // res_bar = montMul(res_bar, res_bar) — square // res_bar = montMul(res_bar, res_bar) — square (always) montgomeryMultiply_into(res_bar, res_bar, mod, n_prime, scratch, temp); std::swap(res_bar, temp); if (exp.isBitSet(i)) { // res_bar = montMul(res_bar, a_bar) — multiply montgomeryMultiply_into(res_bar, a_bar, mod, n_prime, scratch, temp); std::swap(res_bar, temp); // tempMul = montMul(res_bar, a_bar) — multiply (always, not just when the bit is set) montgomeryMultiply_into(res_bar, a_bar, mod, n_prime, scratch, tempMul); // Constant-time select: keep tempMul (squared-and-multiplied) if // the bit is set, else keep res_bar (squared only) unchanged. const bool bit = exp.isBitSet(i); const limb_t bitMask = (limb_t)0 - (limb_t)(bit ? 1u : 0u); limb_t* __restrict__ rp = res_bar.data.get(); const limb_t* __restrict__ mp2 = tempMul.data.get(); for (size_t w = 0; w <= n; ++w) { rp[w] = (mp2[w] & bitMask) | (rp[w] & ~bitMask); } const size_t usedMask = (size_t)0 - (size_t)(bit ? 1u : 0u); res_bar.used = (tempMul.used & usedMask) | (res_bar.used & ~usedMask); } // Convert out of Montgomery domain: montMul(res_bar, 1) = result Loading Loading
src/crypto/rsa.cpp +25 −6 Changes for src/crypto/rsa.cpp: 25 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -810,17 +810,36 @@ namespace netplus { bigInt res_bar(n + 2); montgomeryMultiply_into(one, R2, mod, n_prime, scratch, res_bar); // Exponentiation (left-to-right square-and-multiply) — zero allocations // Exponentiation (left-to-right square-and-multiply) — zero allocations. // // Both the square and the square-then-multiply are ALWAYS computed; // the result is chosen via constant-time select (data words AND the // .used length) instead of branching on exp.isBitSet(i) to decide // whether to multiply at all. The previous // "if (exp.isBitSet(i)) { multiply; }" is the textbook Kocher (1996) // RSA timing attack: an attacker who can time many signatures under // the same private key can recover d's bits from which iterations // took the extra multiply. bigInt tempMul(n + 2); for (size_t i = exp.bitLength(); i-- > 0; ) { // res_bar = montMul(res_bar, res_bar) — square // res_bar = montMul(res_bar, res_bar) — square (always) montgomeryMultiply_into(res_bar, res_bar, mod, n_prime, scratch, temp); std::swap(res_bar, temp); if (exp.isBitSet(i)) { // res_bar = montMul(res_bar, a_bar) — multiply montgomeryMultiply_into(res_bar, a_bar, mod, n_prime, scratch, temp); std::swap(res_bar, temp); // tempMul = montMul(res_bar, a_bar) — multiply (always, not just when the bit is set) montgomeryMultiply_into(res_bar, a_bar, mod, n_prime, scratch, tempMul); // Constant-time select: keep tempMul (squared-and-multiplied) if // the bit is set, else keep res_bar (squared only) unchanged. const bool bit = exp.isBitSet(i); const limb_t bitMask = (limb_t)0 - (limb_t)(bit ? 1u : 0u); limb_t* __restrict__ rp = res_bar.data.get(); const limb_t* __restrict__ mp2 = tempMul.data.get(); for (size_t w = 0; w <= n; ++w) { rp[w] = (mp2[w] & bitMask) | (rp[w] & ~bitMask); } const size_t usedMask = (size_t)0 - (size_t)(bit ? 1u : 0u); res_bar.used = (tempMul.used & usedMask) | (res_bar.used & ~usedMask); } // Convert out of Montgomery domain: montMul(res_bar, 1) = result Loading