AW: AW: AW: Key generation with GPGME and GnuPG hangs at gpgme_op_genkey

Sandra Schreiner sasc0041 at
Wed Jan 27 13:49:51 CET 2016

> If your problem is merely lack of entropy on a VM, then I'd recommend installing haveged, available in Jessie. It 
> broadens the sources used by the kernel for the entropy pool.

Yes, that was the problem. I installed haveged and it worked. But it seems
that the key generation in my C++ application will not work, if a custom passphrase 
callback is set. The key generation code is unchanged, but with the callback I get an 
GPG_ERR_GENERAL error. The init and callback code is as follows:

   // Initializes gpgme

    // Initialize the locale environment.
    setlocale(LC_ALL, "");
    gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
    gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));

    gpgme_error_t error = gpgme_new(&mContext);
        return false;
    // Check OpenPGP
    error = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
        return false;
    // load engine info
    error = gpgme_get_engine_info(&info);
        return false;
    while(info && info->protocol != gpgme_get_protocol(mContext)) {
        info = info->next;
    //callback for user passphrase
    gpgme_set_passphrase_cb(mContext, passphrase_cb, NULL);

And the callback (similar to gpgme++):
static gpgme_error_t passphrase_cb(void *hook, const char *uid_hint, const char *passphrase_info,
                                   int prev_was_bad, int fd){

    std::string passphraseString;
    std::cout<< "Enter your password:";
    std::cin >> passphraseString;

        return GPG_ERR_CANCELED;

    size_t passphraseLength = passphraseString.size();
    char* passphrase = new char[passphraseLength + 1];
    std::copy(passphraseString.begin(), passphraseString.end(), passphrase);

    size_t written = 0;
    do {
        size_t nowWritten = gpgme_io_write(fd, passphrase + written, passphraseLength - written);
        if(nowWritten < 0) {
        written += nowWritten;
    } while(written < passphraseLength);
    gpgme_io_write(fd, "\n", 1);
    return 0;

More information about the Gnupg-users mailing list