Random prime numbers using OpenSSL bignum
This simple C program shows how to generate random prime numbers using openssl bignum libraries; it takes as argument the length of the primes in bits.Here’s the source
Version: 0.1
/**********************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
************************************************************************
(c) 2004 by Paolo Ardoino < paolo.ardoino@gmail.com >
***********************************************************************/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bn.h>
void status() {}
void print_prime(char *prime)
{
int i;
for(i = 0; i < strlen(prime) && prime[i] == ‘0′; i++);
for(; i < strlen(prime); i++)
printf("%c", prime[i]);
printf("\n");
}
int main(int argc, char *argv[])
{
char *prime;
BIGNUM *num_tmp;
long int num_bits = 0;
if(argc >= 2 && argv[1])
num_bits = atol(argv[1]);
else
num_bits = 1024;
printf("Prime generator by (c) 2004 Paolo Ardoino < paolo.ardoino@gmail.com >\n usage: ./genprimes [num_bits]\nGenerating %ld bits primes.\nWait…\n",num_bits);
num_tmp = BN_new();
for (;;) {
BN_generate_prime(num_tmp,num_bits,1,NULL,NULL,status,NULL);
prime = (char *)malloc(BN_num_bytes(num_tmp));
prime = BN_bn2dec(num_tmp);
print_prime(prime);
free(prime);
}
BN_free(num_tmp);
}
Download this code: openssl_primes_random.txt


Please explain what is the purpose of the “#include ”
in your header file?
fredi
“Don’t ask me no questions and I wont tell you no lies”
#include
General purpose input/output routines
#include
Some UNIX standard
None of the above are required, but use to be a good idea to include them, since these are probably the most common headers.
#include
Another standard UNIX header, which declares free, malloc etc…
(more info at http://en.wikipedia.org/wiki/Stdlib.h)
#include
This one is very useful, contains functions, macros and routines to work with (char *) as strings (includes strlen, strtol, memset and much more things)
#include
Openssl BIGNUM declarations, for arithmetics with huge numbers (such as the primes needed to implement RSA algorithm)
Hmm, OK, compiling with OpenSSL ver 0.9.8h on a Sparc 5 running Solaris 2.5.1 yields the following:
# ./genprimes 56
Prime generator by (c) 2004 Paolo Ardoino
usage: ./genprimes [num_bits]
Generating 56 bits primes.
Wait…
69834779657420543
61474498444152383
66239213586814127
61351200631567103
71739027417375587
59701796016635783
65300592723857159
59533171614579239
55576223021154503
^C#
so far looks impressive…
# ./genprimes 8
Prime generator by (c) 2004 Paolo Ardoino
usage: ./genprimes [num_bits]
Generating 8 bits primes.
Wait…
35879
35879
35879
35879
35879
^X35879
^C#
# ./genprimes 4
Prime generator by (c) 2004 Paolo Ardoino
usage: ./genprimes [num_bits]
Generating 4 bits primes.
Wait…
35879
35879
^C# ./genprimes 1
Prime generator by (c) 2004 Paolo Ardoino
usage: ./genprimes [num_bits]
Generating 1 bits primes.
Wait…
35879
35879
^C# l
Hmmmmm……
Well, I found this:
http://www.openssl.org/docs/apps/genrsa.html
“…BUGS
A quirk of the prime generation algorithm is that it cannot generate small primes. Therefore the number of bits should not be less that 64. For typical private keys this will not matter because for security reasons they will be much larger (typically 1024 bits). …”
I wonder if this is the issue?
Yes, it think this should be the problem. It works for higher number of bits, isn’t it? :)