c++parameterscrypto++declspec

Cannot pass AutoSeededRandomPool as parameter when using CryptoPP::RSA, error C2729


I am working with implement RSA using Crypto++. I am trying to generate a pair of RSA keys (public and private) to file like this.

The code can run perfect when I put all in main. When I try to split it to a function and pass AutoSeededRandomPool object as parameter like this:

int generateKeyToFile(
    AutoSeededRandomPool rnd, 
    string publicKeyFileName, string privateKeyFileName){
    try
    {
        RSA::PrivateKey rsaPrivate;
        rsaPrivate.GenerateRandomWithKeySize(rnd, 3072);

        RSA::PublicKey rsaPublic(rsaPrivate);
        EncodePrivateKey(privateKeyFileName, rsaPrivate);
        EncodePublicKey(publicKeyFileName, rsaPublic);

        cout << "Successfully generated and saved RSA keys" << endl;
        return 1;
    }

    catch (CryptoPP::Exception& e)
    {
        cerr << e.what() << endl;
        return -1;
    }
}

When build project, I got the error:

error C2719: 'rnd': formal parameter with __declspec(align('8')) won't be aligned

I cannot found a exact Crypto++ related result for this error from Google, but I found some result for error code C2719. Its content:

'parameter': formal parameter with __declspec(align('#')) won't be aligned

The align __declspec modifier is not permitted on function parameters. Function parameter alignment is controlled by the calling convention used. For more information, see Calling Conventions.

The following sample generates C2719 and shows how to fix it:

// C2719.cpp  
void func(int __declspec(align(32)) i);   // C2719  
// try the following line instead  
 void func(int i);

I didn't get any idea from this apply this "solution" to my case yet.

Seem like AutoSeededRandomPool cannot be passed as parameter. Is there someway to work around this?


Solution

  • int generateKeyToFile(
         AutoSeededRandomPool rnd, 
        string publicKeyFileName, string privateKeyFileName){
        ...
    }
    

    Use a reference:

    int GenerateKeyToFile(
        RandomNumberGenerator& rnd, 
        const string& publicKeyFileName,
        const string& privateKeyFileName)
        {
            ...
        }
    

    I'm not sure AutoSeededRandomPool is copy constructable. I think things are working as expected because you probably should not copy one. Just pass it by reference or by pointer.