weidai11 / cryptopp

free C++ class library of cryptographic schemes
https://cryptopp.com
Other
4.82k stars 1.49k forks source link

GenerateRandomWithKeySize calls debug break #1279

Closed TheNormalnij closed 3 months ago

TheNormalnij commented 3 months ago

OS: Windows 10 19045.4529 x64 Crypto++ version: on commit (Guard use of ModularSquareRoot (GH https://github.com/weidai11/cryptopp/issues/1249[)](https://github.com/weidai11/cryptopp/commit/9aa07aebbdc62461c3ac32f958d7c3ab89ff6f73) Compiler: MSVC 17.9.34607.119 (x86)

Code:

struct KeyPair {
    std::string publicKey, privateKey;
};

// size if 1024, 2048 or 4096
inline KeyPair GenerateRsaKeyPair(const unsigned int size)
    {
        KeyPair rsaKeyPair;

        CryptoPP::AutoSeededRandomPool asrp;

        CryptoPP::RSA::PrivateKey rsaPrivateKey;

        // next line causes debug break
        rsaPrivateKey.GenerateRandomWithKeySize(asrp, size);
        CryptoPP::RSA::PublicKey rsaPublicKey(rsaPrivateKey);

        CryptoPP::StringSink rawPrivateKeySink(rsaKeyPair.privateKey);
        rsaPrivateKey.DEREncode(rawPrivateKeySink);

        CryptoPP::StringSink rawPublicKeySink(rsaKeyPair.publicKey);
        rsaPublicKey.DEREncode(rawPublicKeySink);

        return rsaKeyPair;
    }

When i debug my application, this function calls debug break here:

// nbtheory.cpp:526
Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u)
{
    // Callers must ensure p and q are prime, GH #1249
    CRYPTOPP_ASSERT(IsPrime(p) && IsPrime(q));

    // isn't operator overloading great?
    return p * (u * (xq-xp) % q) + xp;
/*
    Integer t1 = xq-xp;
    cout << hex << t1 << endl;
    Integer t2 = u * t1;
    cout << hex << t2 << endl;
    Integer t3 = t2 % q;
    cout << hex << t3 << endl;
    Integer t4 = p * t3;
    cout << hex << t4 << endl;
    Integer t5 = t4 + xp;
    cout << hex << t5 << endl;
    return t5;
*/
}

AutoSeededX917RNG<CryptoPP::AES> asrp; doesn't work too.

Expected behavior: rsaPrivateKey.GenerateRandomWithKeySize doesn't cause debug break.

Thanks!

TheNormalnij commented 3 months ago

It was fast. Thanks!