0

I am new to openssl library and from what I could understand it is possible to generate large primes say 512 bit using the command line argument :openssl prime -generate -bits 512. But I am not sure how can I do this using a C code?

I tried using system() command to generate prime using following code:

int main()
{
  char c[50];
  strcpy(c, "openssl prime -generate -bits 512");
  long long p = system(c); // IS THIS POSSIBLE??
  return 0;
 }

But I have read there are some security concerns on this as given in this link here

So is it possible to write this large prime generated in a text file or something so that I can use it later in my code.

My final motive is to generate two large prime numbers of order 512 bits.

Alex Probert
  • 493
  • 1
  • 3
  • 17
skii
  • 3
  • 3

1 Answers1

0

If you want to know how you can do the functionality of openssl prime -generate in C code have a look at the source code of OpenSSL which is open source and easily accessible. Specifically have a look at apps/prime.c which implements openssl prime. There you can see that BN_generate_prime_ex is used to generate the prime:

if (generate) {
    ...
    bn = BN_new();
    ...
    if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {
        BIO_printf(bio_err, "Failed to generate prime.\n");
        goto end;
    }
Steffen Ullrich
  • 184,332
  • 29
  • 363
  • 424
  • Thank you for guiding me to right path in openssl repo , but I am getting an error as undefined refernce to bn_NEW(). I have included header file. What am I missing ? – skii Jan 05 '18 at 06:52
  • @AkankshaDixit: This is security.stackexchange.com. This is about concepts and to point you in the right direction. If you have problems with your specific code then 1. ask at stackoverflow.com and 2. provide all the information needed to understand and reproduce your problem. This includes usually providing the actual code in a minimal but complete example and providing the exact error messages. – Steffen Ullrich Jan 05 '18 at 07:00
  • where safe is defined as: int safe = 1 – CipherX Oct 25 '21 at 08:17