Hello,
> I want to generate an RSA key pair from a c/c++ program using Openssl's API.
>
> I saw that there is an RSA_generate_key() function, but it requires an
> initialization of the random generator.
RSA_generate_key() when generating RSA p and q primes requires probably
seeded RNG but all rand functions perform self seeding (if possible).
If RSA_generate_key() returns no error than RNG was properly self seeded
(if not seeded before).
> Could anyone explain me how to initialize the random generator (on Linux)?
Something like:
RAND_load_file("/dev/urandom", 1024);
should be enough.
> A
> short c sample of the generation would be of a great help!
Some simple example attached.
Best regards,
--
Marek Marcola <[EMAIL PROTECTED]>
#include <stdio.h>
#include <openssl/bn.h>
#include <openssl/rsa.h>
int main()
{
RSA *r;
r = RSA_generate_key(32,656537,NULL,NULL);
if( r==NULL ) {
printf("Key failed");
exit(1);
} else {
printf("public modulus (n):\n");
printf(" %s\n",BN_bn2hex(r->n));
printf("public exponent (e):\n");
printf(" %s\n",BN_bn2hex(r->e));
printf("private exponent (d):\n");
printf(" %s\n",BN_bn2hex(r->d));
printf("secret prime factor (p):\n");
printf(" %s\n",BN_bn2hex(r->p));
printf("secret prime factor (q):\n");
printf(" %s\n",BN_bn2hex(r->q));
printf("dmp1 [ d mod (p-1) ]:\n");
printf(" %s\n",BN_bn2hex(r->dmp1));
printf("dmq1 [ d mod (q-1) ]:\n");
printf(" %s\n",BN_bn2hex(r->dmq1));
printf("iqmp [ q^-1 mod p ]:\n");
printf(" %s\n",BN_bn2hex(r->iqmp));
}
printf("RSA SIZE: %d\n", RSA_size(r));
return(0);
}