Commit 3bb42289 authored by jan.koester's avatar jan.koester
Browse files

rsa: a 2048-bit key has a 2048-bit modulus

findPrime() set only the top bit of each prime, so two 1024-bit primes
often multiplied to a 2047-bit modulus (7 of 12 in a quick run). Let's
Encrypt refuses such keys ("key size not supported: 2047"). Setting the
second-highest bit as well, as OpenSSL/FIPS 186 do, puts p*q above
2^(2k-1): 20 of 20 keys now come out at exactly 2048 bits.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 617f8d83
Loading
Loading
Loading
Loading
+6 −0
Changes for src/crypto/rsa.cpp: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1177,6 +1177,12 @@ namespace netplus {

        // Initial random start (ensured odd by generateSecureRandom)
        generateSecureRandom(p, bits);
        // Also set the second-highest bit (as OpenSSL/FIPS 186 do): with only the top bit set,
        // two k-bit primes can multiply to 2k-1 bits -- a "2048-bit" key with a 2047-bit
        // modulus, which CAs such as Let's Encrypt refuse. Both p,q >= 1.5*2^(k-1) puts
        // p*q >= 2.25*2^(2k-2) > 2^(2k-1), i.e. exactly 2k bits.
        if (bits >= 2)
            p.data[(bits - 2) / LIMB_BITS] |= (limb_t(1) << ((bits - 2) % LIMB_BITS));

        // Search: skip composites with trial division, then confirm with Miller-Rabin
        for (;;) {