bn_prime.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/local/bin/perl
  2. # bn_prime.pl
  3. $num=2048;
  4. $num=$ARGV[0] if ($#ARGV >= 0);
  5. push(@primes,2);
  6. $p=1;
  7. loop: while ($#primes < $num-1)
  8. {
  9. $p+=2;
  10. $s=int(sqrt($p));
  11. for ($i=0; defined($primes[$i]) && $primes[$i]<=$s; $i++)
  12. {
  13. next loop if (($p%$primes[$i]) == 0);
  14. }
  15. push(@primes,$p);
  16. }
  17. # print <<"EOF";
  18. # /* Auto generated by bn_prime.pl */
  19. # /* Copyright (C) 1995-1997 Eric Young (eay\@mincom.oz.au).
  20. # * All rights reserved.
  21. # * Copyright remains Eric Young's, and as such any Copyright notices in
  22. # * the code are not to be removed.
  23. # * See the COPYRIGHT file in the SSLeay distribution for more details.
  24. # */
  25. #
  26. # EOF
  27. print <<\EOF;
  28. /* Auto generated by bn_prime.pl */
  29. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  30. * All rights reserved.
  31. *
  32. * This package is an SSL implementation written
  33. * by Eric Young (eay@cryptsoft.com).
  34. * The implementation was written so as to conform with Netscapes SSL.
  35. *
  36. * This library is free for commercial and non-commercial use as long as
  37. * the following conditions are aheared to. The following conditions
  38. * apply to all code found in this distribution, be it the RC4, RSA,
  39. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  40. * included with this distribution is covered by the same copyright terms
  41. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  42. *
  43. * Copyright remains Eric Young's, and as such any Copyright notices in
  44. * the code are not to be removed.
  45. * If this package is used in a product, Eric Young should be given attribution
  46. * as the author of the parts of the library used.
  47. * This can be in the form of a textual message at program startup or
  48. * in documentation (online or textual) provided with the package.
  49. *
  50. * Redistribution and use in source and binary forms, with or without
  51. * modification, are permitted provided that the following conditions
  52. * are met:
  53. * 1. Redistributions of source code must retain the copyright
  54. * notice, this list of conditions and the following disclaimer.
  55. * 2. Redistributions in binary form must reproduce the above copyright
  56. * notice, this list of conditions and the following disclaimer in the
  57. * documentation and/or other materials provided with the distribution.
  58. * 3. All advertising materials mentioning features or use of this software
  59. * must display the following acknowledgement:
  60. * "This product includes cryptographic software written by
  61. * Eric Young (eay@cryptsoft.com)"
  62. * The word 'cryptographic' can be left out if the rouines from the library
  63. * being used are not cryptographic related :-).
  64. * 4. If you include any Windows specific code (or a derivative thereof) from
  65. * the apps directory (application code) you must include an acknowledgement:
  66. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  67. *
  68. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  69. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  71. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  72. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  73. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  74. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  75. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  76. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  77. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  78. * SUCH DAMAGE.
  79. *
  80. * The licence and distribution terms for any publically available version or
  81. * derivative of this code cannot be changed. i.e. this code cannot simply be
  82. * copied and put under another distribution licence
  83. * [including the GNU Public Licence.]
  84. */
  85. EOF
  86. for ($i=0; $i <= $#primes; $i++)
  87. {
  88. if ($primes[$i] > 256)
  89. {
  90. $eight=$i;
  91. last;
  92. }
  93. }
  94. printf "#ifndef EIGHT_BIT\n";
  95. printf "#define NUMPRIMES %d\n",$num;
  96. printf "typedef unsigned short prime_t;\n";
  97. printf "#else\n";
  98. printf "#define NUMPRIMES %d\n",$eight;
  99. printf "typedef unsigned char prime_t;\n";
  100. printf "#endif\n";
  101. print "static const prime_t primes[NUMPRIMES]=\n\t{\n\t";
  102. $init=0;
  103. for ($i=0; $i <= $#primes; $i++)
  104. {
  105. printf "\n#ifndef EIGHT_BIT\n\t" if ($primes[$i] > 256) && !($init++);
  106. printf("\n\t") if (($i%8) == 0) && ($i != 0);
  107. printf("%4d,",$primes[$i]);
  108. }
  109. print "\n#endif\n\t};\n";