Search code examples
c++crypto++

Crypto++ 5.6.3rc5 GenerateBlock Not Implemented


I am trying to derive key from password and want to generate randomly the salt(I dont know what size it should be for SHA-256 and does this matter like the IV in AES256, where it should be 128 bit,give a hint if someone know) with AutoSeededRandomPool but exception is cought

RandomNumberGenerator:GenerateBlock Not Implemented

I am using crypto++ 5.6.3rc5 with QT 5.5.1 and /MD release mode, this may be a bug, or unfinished work of someone.

#include <QCoreApplication>
#include <sha.h>
#include <base64.h>
#include <iostream>
#include <string>
#include <pwdbased.h>
#include <cstdio>
#include <iostream>
#include <osrng.h>
using CryptoPP::AutoSeededRandomPool;

#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

#include <string>
using std::string;

#include <cstdlib>
using std::exit;

#include <cryptlib.h>
using CryptoPP::Exception;

#include <hex.h>
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;

#include <filters.h>
using CryptoPP::StringSink;

//#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{  

    QCoreApplication a(argc, argv);

    try
    {
        AutoSeededRandomPool rng;
        byte salt[16*8];
        rng.GenerateBlock(salt, 16*8);

        byte password[] ="password";
        size_t plen = strlen((const char*)password);

        size_t slen = strlen((const char*)salt);

        int c = 1;
        byte derived[32];

        CryptoPP::PKCS5_PBKDF2_HMAC<CryptoPP::SHA256> pbkdf2;
        pbkdf2.DeriveKey(derived, sizeof(derived), 0, password, plen, salt, slen, c);

        string result;
        HexEncoder encoder(new StringSink(result));

        encoder.Put(derived, sizeof(derived));
        encoder.MessageEnd();

        cout << "Derived: " << result << endl;
    }
    catch (const Exception& ex) {
        cerr << ex.what() << endl;
    }
    return a.exec();
}

Solution

  • Crypto++ 5.6.3rc5 GenerateBlock Not Implemented ...

    You can read the history on the change at Crash in RandomNumberGenerator::GenerateWord32 due to stack recursion. The change was eventually backed out.

    It was fixed in RC6, but it has not been announced yet. There's a quasi-pre-RC6 at Crypto++ 5.6.3 Files. But as soon as it is announced, then its set in stone and will not be changed.

    Right now, RC6 is undergoing minor changes due to Cygwin, MinGW and C++11 on Debian Unstable. The changes are not too bad, but testing them is painful. Some of the scripts take half a day to run under emulated platforms, like S/390x.

    If you want to side step the issue and avoid the download of pre-RC6, then you can use one of the following generators. They call GenerateIntoBufferedTransformation:

    • AutoSeededX917RNG< AES >
    • X917RNG
    • RandomPool

    Or, you can use OS_GenerateRandomBlock to draw directly from the OS's pool.

    Or, you can remove the code that throws. Open cryptlib.h, find RandomNumberGenerator, remove the #if 0/#endif guarding the old code and delete the throw.

    Also see RandomNumberGenerator on the Crypto++ wiki.