Commit 664b2f23 authored by jan.koester's avatar jan.koester
Browse files

sec fixes

parent 374f20a5
Loading
Loading
Loading
Loading
+38 −13
Changes for src/crypto/ecc_p256.cpp: 38 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -233,30 +233,55 @@ static bool scalar_is_zero(const uint8_t k[32]) {
    return true;
}

// Constant-time Montgomery ladder (general group law -- works with regular
// Jacobian point addition/doubling, not just the x-only differential
// addition available on Montgomery-form curves like Curve25519).
//
// Invariant maintained every iteration: R1 = R0 + Q. At each bit, R0/R1 are
// conditionally swapped (constant-time), then EXACTLY one add and one
// double always run -- never conditionally skipped -- then swapped back.
// This replaces the previous textbook double-and-add
// ("if (bit) R = R.addJacobian(Q);"), which conditionally skipped the add
// entirely for zero bits: a massive, dudect-confirmed timing leak
// (t > 300, instantly) correlating with the scalar's Hamming weight --
// serious for both ECDHE private keys and, especially, ECDSA's ephemeral
// nonce k, where partial nonce leakage across signatures is a known path
// to full private-key recovery via lattice attacks.
//
// Note: R0 still starts at the point at infinity, so leading *zero* bits of
// the scalar take doubled()/addJacobian()'s cheap infinity short-circuit
// until the first 1-bit -- a much smaller residual timing signal
// (correlated only with the scalar's bit-length/magnitude, not its
// detailed bit pattern) than the fixed one above. Real private
// keys/nonces are always full-width random 256-bit values, so this is not
// considered a practical leak; eliminating it fully would require unified
// "complete" addition formulas that never special-case infinity at all.
P256Point P256Point::scalarMul(const uint8_t k[32]) const
{
    // R = INF (Jacobian, MONT)
    P256Point R;
    R.inf  = true;
    R.mont = true;
    // R0 = INF (Jacobian, MONT)
    P256Point R0;
    R0.inf  = true;
    R0.mont = true;
    u256 z0 = u256::zero();
    R.x = z0.toMontgomery();
    R.y = z0.toMontgomery();
    R.z = z0.toMontgomery();
    R0.x = z0.toMontgomery();
    R0.y = z0.toMontgomery();
    R0.z = z0.toMontgomery();

    // Q = this as Jacobian (MONT)
    P256Point Q = toJacobian();
    // R1 = this as Jacobian (MONT)
    P256Point R1 = toJacobian();

    // classic double-and-add MSB->LSB (matches get_bit_be)
    // MSB->LSB (matches get_bit_be); see the constant-time ladder comment above.
    for (int i=0;i<256;i++){
        uint32_t bit = get_bit_be(k,i);

        R = R.doubled();
        if(bit) R = R.addJacobian(Q);
        cswap_jac(R0, R1, bit);
        R1 = R0.addJacobian(R1);
        R0 = R0.doubled();
        cswap_jac(R0, R1, bit);
    }

    // ✅ Return affine STD
    return R.toAffine();
    return R0.toAffine();
}


+36 −13
Changes for src/crypto/ecc_u256.cpp: 36 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -232,13 +232,28 @@ u256 u256::redc(const u256& a, const u256& b) {
    u256 r;
    for (int i=0; i<8; i++) r.w[i] = t[i+8];

    if (extra || r.compare(P256_P) >= 0) {
        uint32_t br=0;
        r = r.subRaw(P256_P, br);
    }
    if (r.compare(P256_P) >= 0) {
        uint32_t br=0;
        r = r.subRaw(P256_P, br);
    // Constant-time final reduction: REDC's raw result can exceed p by up
    // to ~2p, so up to two conditional subtractions may be needed. Compute
    // each subtraction unconditionally and select the result via a mask
    // instead of branching on the (secret-derived) comparison -- a branch
    // here is exactly the kind of thing dudect caught in the point-mult
    // ladder (see the comment on scalarMul()), just one layer deeper: every
    // montgomeryMul() call in that ladder bottoms out here.
    {
        uint32_t br1 = 0;
        u256 sub1 = r.subRaw(P256_P, br1);
        uint32_t extraMask = 0u - (uint32_t)(extra != 0);
        uint32_t geMask    = 0u - (uint32_t)(br1 == 0);
        uint32_t applyMask = extraMask | geMask;
        for (int i = 0; i < 8; i++)
            r.w[i] = (sub1.w[i] & applyMask) | (r.w[i] & ~applyMask);
    }
    {
        uint32_t br2 = 0;
        u256 sub2 = r.subRaw(P256_P, br2);
        uint32_t applyMask = 0u - (uint32_t)(br2 == 0);
        for (int i = 0; i < 8; i++)
            r.w[i] = (sub2.w[i] & applyMask) | (r.w[i] & ~applyMask);
    }

    return r;
@@ -261,20 +276,28 @@ u256 u256::fromMontgomery() const {
u256 u256::montgomeryAdd(const u256& other) const {
    uint32_t carry = 0;
    u256 r = addRaw(other, carry);
    if (carry || r.compare(P256_P) >= 0) {

    // Constant-time conditional subtract -- see the note in redc() above.
    uint32_t br = 0;
        r = r.subRaw(P256_P, br);
    }
    u256 sub = r.subRaw(P256_P, br);
    uint32_t carryMask = 0u - (uint32_t)(carry != 0);
    uint32_t geMask    = 0u - (uint32_t)(br == 0);
    uint32_t applyMask = carryMask | geMask;
    for (int i = 0; i < 8; i++)
        r.w[i] = (sub.w[i] & applyMask) | (r.w[i] & ~applyMask);
    return r;
}

u256 u256::montgomerySub(const u256& other) const {
    uint32_t br = 0;
    u256 r = subRaw(other, br);
    if (br) {

    // Constant-time conditional add-back -- see the note in redc() above.
    uint32_t c = 0;
        r = r.addRaw(P256_P, c);
    }
    u256 added = r.addRaw(P256_P, c);
    uint32_t applyMask = 0u - (uint32_t)(br != 0);
    for (int i = 0; i < 8; i++)
        r.w[i] = (added.w[i] & applyMask) | (r.w[i] & ~applyMask);
    return r;
}