2
0

prime.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* ====================================================================
  2. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. */
  49. #include <string.h>
  50. #include "apps.h"
  51. #include <openssl/bn.h>
  52. typedef enum OPTION_choice {
  53. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  54. OPT_HEX, OPT_GENERATE, OPT_BITS, OPT_SAFE, OPT_CHECKS
  55. } OPTION_CHOICE;
  56. OPTIONS prime_options[] = {
  57. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [number...]\n"},
  58. {OPT_HELP_STR, 1, '-',
  59. " number Number to check for primality\n"},
  60. {"help", OPT_HELP, '-', "Display this summary"},
  61. {"hex", OPT_HEX, '-', "Hex output"},
  62. {"generate", OPT_GENERATE, '-', "Generate a prime"},
  63. {"bits", OPT_BITS, 'p', "Size of number in bits"},
  64. {"safe", OPT_SAFE, '-',
  65. "When used with -generate, generate a safe prime"},
  66. {"checks", OPT_CHECKS, 'p', "Number of checks"},
  67. {NULL}
  68. };
  69. int prime_main(int argc, char **argv)
  70. {
  71. BIGNUM *bn = NULL;
  72. int hex = 0, checks = 20, generate = 0, bits = 0, safe = 0, ret = 1;
  73. char *prog;
  74. OPTION_CHOICE o;
  75. prog = opt_init(argc, argv, prime_options);
  76. while ((o = opt_next()) != OPT_EOF) {
  77. switch (o) {
  78. case OPT_EOF:
  79. case OPT_ERR:
  80. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  81. goto end;
  82. case OPT_HELP:
  83. opt_help(prime_options);
  84. ret = 0;
  85. goto end;
  86. case OPT_HEX:
  87. hex = 1;
  88. break;
  89. case OPT_GENERATE:
  90. generate = 1;
  91. break;
  92. case OPT_BITS:
  93. bits = atoi(opt_arg());
  94. break;
  95. case OPT_SAFE:
  96. safe = 1;
  97. break;
  98. case OPT_CHECKS:
  99. checks = atoi(opt_arg());
  100. break;
  101. }
  102. }
  103. argc = opt_num_rest();
  104. argv = opt_rest();
  105. if (argc == 0 && !generate) {
  106. BIO_printf(bio_err, "%s: No prime specified\n", prog);
  107. goto end;
  108. }
  109. if (generate) {
  110. char *s;
  111. if (!bits) {
  112. BIO_printf(bio_err, "Specify the number of bits.\n");
  113. goto end;
  114. }
  115. bn = BN_new();
  116. BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL);
  117. s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
  118. BIO_printf(bio_out, "%s\n", s);
  119. OPENSSL_free(s);
  120. } else {
  121. for ( ; *argv; argv++) {
  122. if (hex)
  123. BN_hex2bn(&bn, argv[0]);
  124. else
  125. BN_dec2bn(&bn, argv[0]);
  126. BN_print(bio_out, bn);
  127. BIO_printf(bio_out, " (%s) %s prime\n",
  128. argv[0],
  129. BN_is_prime_ex(bn, checks, NULL, NULL)
  130. ? "is" : "is not");
  131. }
  132. }
  133. BN_free(bn);
  134. end:
  135. return ret;
  136. }