I am trying to generate RSA keys using OpenSSL with the following function:
RSA *genRSA() {
clear();
mvprintw(0, 0, "Generating RSA key...\n");
RAND_load_file("/dev/random", 4096);
BIGNUM *e = BN_new();
BN_set_word(e, RSA_F4);
RSA *rsa;
while (getch() != '\n'); // the program does reach this point
if (!RSA_generate_key_ex(rsa, 4096, e, 0)) { // seg fault must occur on this line
while (getch() != '\n'); // never gets here
printw("ERROR: Failed to create RSA key\n");
return NULL;
}
while (getch() != '\n'); // or here
BN_free(e);
if (!RSA_check_key(rsa)) {
printw("ERROR: Key failed validation\n");
return NULL;
}
printw("Key generation completed successfully\n");
return rsa;
}
I'm not receiving any compiler warnings other than some deprecated on OS X ones (could that be causing an issue?). Why am I getting a seg fault?
RAND_load_file("/dev/random", 4096);. You are asking for bytes, not bits. And a lot of them. It could deplete the device and you could block a long time. To achieve the equivalent of 4096-bit key, you need about 140-bit security level. 140/8 = 17.5 bytes:RAND_load_file("/dev/random", 18);.RAND_load_file("/dev/random", 4096);probably failed. You should check the return value and fall back to"/dev/urandom".