155714Skris#!/usr/local/bin/perl
255714Skris# bn_prime.pl
355714Skris
455714Skris$num=2048;
555714Skris$num=$ARGV[0] if ($#ARGV >= 0);
655714Skris
755714Skrispush(@primes,2);
855714Skris$p=1;
955714Skrisloop: while ($#primes < $num-1)
1055714Skris	{
1155714Skris	$p+=2;
1255714Skris	$s=int(sqrt($p));
1355714Skris
14160814Ssimon	for ($i=0; defined($primes[$i]) && $primes[$i]<=$s; $i++)
1555714Skris		{
1655714Skris		next loop if (($p%$primes[$i]) == 0);
1755714Skris		}
1855714Skris	push(@primes,$p);
1955714Skris	}
2055714Skris
2159191Skris# print <<"EOF";
2259191Skris# /* Auto generated by bn_prime.pl */
2359191Skris# /* Copyright (C) 1995-1997 Eric Young (eay\@mincom.oz.au).
2459191Skris#  * All rights reserved.
2559191Skris#  * Copyright remains Eric Young's, and as such any Copyright notices in
2659191Skris#  * the code are not to be removed.
2759191Skris#  * See the COPYRIGHT file in the SSLeay distribution for more details.
2859191Skris#  */
2959191Skris#
3059191Skris# EOF
3159191Skris
3259191Skrisprint <<\EOF;
3355714Skris/* Auto generated by bn_prime.pl */
3459191Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3555714Skris * All rights reserved.
3659191Skris *
3759191Skris * This package is an SSL implementation written
3859191Skris * by Eric Young (eay@cryptsoft.com).
3959191Skris * The implementation was written so as to conform with Netscapes SSL.
4059191Skris *
4159191Skris * This library is free for commercial and non-commercial use as long as
4259191Skris * the following conditions are aheared to.  The following conditions
4359191Skris * apply to all code found in this distribution, be it the RC4, RSA,
4459191Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
4559191Skris * included with this distribution is covered by the same copyright terms
4659191Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
4759191Skris *
4855714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
4955714Skris * the code are not to be removed.
5059191Skris * If this package is used in a product, Eric Young should be given attribution
5159191Skris * as the author of the parts of the library used.
5259191Skris * This can be in the form of a textual message at program startup or
5359191Skris * in documentation (online or textual) provided with the package.
5459191Skris *
5559191Skris * Redistribution and use in source and binary forms, with or without
5659191Skris * modification, are permitted provided that the following conditions
5759191Skris * are met:
5859191Skris * 1. Redistributions of source code must retain the copyright
5959191Skris *    notice, this list of conditions and the following disclaimer.
6059191Skris * 2. Redistributions in binary form must reproduce the above copyright
6159191Skris *    notice, this list of conditions and the following disclaimer in the
6259191Skris *    documentation and/or other materials provided with the distribution.
6359191Skris * 3. All advertising materials mentioning features or use of this software
6459191Skris *    must display the following acknowledgement:
6559191Skris *    "This product includes cryptographic software written by
6659191Skris *     Eric Young (eay@cryptsoft.com)"
6759191Skris *    The word 'cryptographic' can be left out if the rouines from the library
6859191Skris *    being used are not cryptographic related :-).
6959191Skris * 4. If you include any Windows specific code (or a derivative thereof) from
7059191Skris *    the apps directory (application code) you must include an acknowledgement:
7159191Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
7259191Skris *
7359191Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
7459191Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7559191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7659191Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
7759191Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
7859191Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
7959191Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8059191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8159191Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8259191Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8359191Skris * SUCH DAMAGE.
8459191Skris *
8559191Skris * The licence and distribution terms for any publically available version or
8659191Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
8759191Skris * copied and put under another distribution licence
8859191Skris * [including the GNU Public Licence.]
8955714Skris */
9055714Skris
9155714SkrisEOF
9255714Skris
9355714Skrisfor ($i=0; $i <= $#primes; $i++)
9455714Skris	{
9555714Skris	if ($primes[$i] > 256)
9655714Skris		{
9755714Skris		$eight=$i;
9855714Skris		last;
9955714Skris		}
10055714Skris	}
10155714Skris
10255714Skrisprintf "#ifndef EIGHT_BIT\n";
10355714Skrisprintf "#define NUMPRIMES %d\n",$num;
104194206Ssimonprintf "typedef unsigned short prime_t;\n";
10555714Skrisprintf "#else\n";
10655714Skrisprintf "#define NUMPRIMES %d\n",$eight;
107194206Ssimonprintf "typedef unsigned char prime_t;\n";
10855714Skrisprintf "#endif\n";
109194206Ssimonprint "static const prime_t primes[NUMPRIMES]=\n\t{\n\t";
11055714Skris$init=0;
11155714Skrisfor ($i=0; $i <= $#primes; $i++)
11255714Skris	{
11355714Skris	printf "\n#ifndef EIGHT_BIT\n\t" if ($primes[$i] > 256) && !($init++);
11455714Skris	printf("\n\t") if (($i%8) == 0) && ($i != 0);
11555714Skris	printf("%4d,",$primes[$i]);
11655714Skris	}
11755714Skrisprint "\n#endif\n\t};\n";
11855714Skris
11955714Skris
120