Commit bc6d5a8d authored by jan.koester's avatar jan.koester
Browse files

test

parent 7a399d53
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -1162,6 +1162,10 @@ namespace netplus {
            e = next_e;
        }

        // Store primes for CRT key export
        this->p = p;
        this->q = q;

        // Calculate d = e^-1 mod phi
        this->d = modInverse(e, phi);

+5 −2
Original line number Diff line number Diff line
@@ -119,6 +119,8 @@ namespace netplus {
        const bigInt& getN() const { return n; }
        const bigInt& getE() const { return e; }
        const bigInt& getD() const { return d; }
        const bigInt& getP() const { return p; }
        const bigInt& getQ() const { return q; }

        // Static Math Engine
        void reserve(size_t new_cap);
@@ -140,13 +142,14 @@ namespace netplus {
        static bigInt bigIntFromBytesBE (const uint8_t* bytes, size_t len);
        static std::vector<uint8_t> bigIntToBytesBE(const bigInt& x, size_t outLen);
        
        static bigInt modInverse(const bigInt& e, const bigInt& phi);

    private:
        bigInt n, e, d;
        bigInt n, e, d, p, q;
        bool isProbablyPrime(const bigInt& n, int k);
        void findPrime(bigInt& p, size_t digits);
        void generateSecureRandom(bigInt& n, size_t requested_digits);
        static bigInt modPow(const bigInt& base, const bigInt& exp, const bigInt& mod);
        static bigInt modInverse(const bigInt& e, const bigInt& phi);
        static bigInt gcd(bigInt a, bigInt b);
        static limb_t calculateNPrime(limb_t n0);
        static bigInt calculateRMod(const bigInt& mod);
+27 −14
Original line number Diff line number Diff line
@@ -190,27 +190,37 @@ static std::vector<uint8_t> bigIntBytes(const rsa::bigInt &bi) {
//   modulus           INTEGER,
//   publicExponent    INTEGER,
//   privateExponent   INTEGER,
//   prime1            INTEGER (0 -- not stored, placeholder),
//   prime2            INTEGER (0),
//   exponent1         INTEGER (0),
//   exponent2         INTEGER (0),
//   coefficient       INTEGER (0)
//   prime1            INTEGER,
//   prime2            INTEGER,
//   exponent1         INTEGER,  -- d mod (p-1)
//   exponent2         INTEGER,  -- d mod (q-1)
//   coefficient       INTEGER   -- q^-1 mod p
// }
// Note: p, q, dp, dq, qinv are not available from the rsa class
// which only stores n, e, d. We encode them as 0.
static std::vector<uint8_t> buildRsaPrivateKeyDer(const rsa::bigInt &n,
                                                   const rsa::bigInt &e,
                                                   const rsa::bigInt &d) {
                                                   const rsa::bigInt &d,
                                                   const rsa::bigInt &p,
                                                   const rsa::bigInt &q) {
    // Compute CRT parameters
    rsa::bigInt one(1U, 1);
    rsa::bigInt pm1(p.capacity), qm1(q.capacity);
    rsa::bigInt dp_q, dp_r, dq_q, dq_r;
    rsa::subtract(p, one, pm1);    // p - 1
    rsa::subtract(q, one, qm1);    // q - 1
    rsa::divide(d, pm1, dp_q, dp_r);   // dp = d mod (p-1)
    rsa::divide(d, qm1, dq_q, dq_r);   // dq = d mod (q-1)
    rsa::bigInt qInv = rsa::modInverse(q, p);  // qInv = q^-1 mod p

    std::vector<uint8_t> body;
    derAppend(body, derIntegerSmall(0));                    // version = 0
    derAppend(body, derInteger(bigIntBytes(n)));            // modulus
    derAppend(body, derInteger(bigIntBytes(e)));            // publicExponent
    derAppend(body, derInteger(bigIntBytes(d)));            // privateExponent
    derAppend(body, derIntegerSmall(0));                    // prime1 (unavailable)
    derAppend(body, derIntegerSmall(0));                    // prime2
    derAppend(body, derIntegerSmall(0));                    // exponent1
    derAppend(body, derIntegerSmall(0));                    // exponent2
    derAppend(body, derIntegerSmall(0));                    // coefficient
    derAppend(body, derInteger(bigIntBytes(p)));            // prime1
    derAppend(body, derInteger(bigIntBytes(q)));            // prime2
    derAppend(body, derInteger(bigIntBytes(dp_r)));         // exponent1 (dp)
    derAppend(body, derInteger(bigIntBytes(dq_r)));         // exponent2 (dq)
    derAppend(body, derInteger(bigIntBytes(qInv)));         // coefficient
    return derSequence(body);
}

@@ -326,8 +336,11 @@ bool generateSelfSignedCert(const CertGenConfig &cfg, CertKeyPair &out) {
    if (n.isZero() || e.isZero() || d.isZero())
        return false;

    const auto &p = key.getP();
    const auto &q = key.getQ();

    // 2. Build key DER
    out.keyDer = buildRsaPrivateKeyDer(n, e, d);
    out.keyDer = buildRsaPrivateKeyDer(n, e, d, p, q);

    // 3. Build TBSCertificate
    auto dn = buildDN(cfg);