prime.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include "apps.h"
  11. #include "progs.h"
  12. #include <openssl/bn.h>
  13. typedef enum OPTION_choice {
  14. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  15. OPT_HEX, OPT_GENERATE, OPT_BITS, OPT_SAFE, OPT_CHECKS
  16. } OPTION_CHOICE;
  17. const OPTIONS prime_options[] = {
  18. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [number...]\n"},
  19. {OPT_HELP_STR, 1, '-',
  20. " number Number to check for primality\n"},
  21. {"help", OPT_HELP, '-', "Display this summary"},
  22. {"hex", OPT_HEX, '-', "Hex output"},
  23. {"generate", OPT_GENERATE, '-', "Generate a prime"},
  24. {"bits", OPT_BITS, 'p', "Size of number in bits"},
  25. {"safe", OPT_SAFE, '-',
  26. "When used with -generate, generate a safe prime"},
  27. {"checks", OPT_CHECKS, 'p', "Number of checks"},
  28. {NULL}
  29. };
  30. int prime_main(int argc, char **argv)
  31. {
  32. BIGNUM *bn = NULL;
  33. int hex = 0, checks = 20, generate = 0, bits = 0, safe = 0, ret = 1;
  34. char *prog;
  35. OPTION_CHOICE o;
  36. prog = opt_init(argc, argv, prime_options);
  37. while ((o = opt_next()) != OPT_EOF) {
  38. switch (o) {
  39. case OPT_EOF:
  40. case OPT_ERR:
  41. opthelp:
  42. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  43. goto end;
  44. case OPT_HELP:
  45. opt_help(prime_options);
  46. ret = 0;
  47. goto end;
  48. case OPT_HEX:
  49. hex = 1;
  50. break;
  51. case OPT_GENERATE:
  52. generate = 1;
  53. break;
  54. case OPT_BITS:
  55. bits = atoi(opt_arg());
  56. break;
  57. case OPT_SAFE:
  58. safe = 1;
  59. break;
  60. case OPT_CHECKS:
  61. checks = atoi(opt_arg());
  62. break;
  63. }
  64. }
  65. argc = opt_num_rest();
  66. argv = opt_rest();
  67. if (generate) {
  68. if (argc != 0) {
  69. BIO_printf(bio_err, "Extra arguments given.\n");
  70. goto opthelp;
  71. }
  72. } else if (argc == 0) {
  73. BIO_printf(bio_err, "%s: No prime specified\n", prog);
  74. goto opthelp;
  75. }
  76. if (generate) {
  77. char *s;
  78. if (!bits) {
  79. BIO_printf(bio_err, "Specify the number of bits.\n");
  80. goto end;
  81. }
  82. bn = BN_new();
  83. if (bn == NULL) {
  84. BIO_printf(bio_err, "Out of memory.\n");
  85. goto end;
  86. }
  87. if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {
  88. BIO_printf(bio_err, "Failed to generate prime.\n");
  89. goto end;
  90. }
  91. s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
  92. if (s == NULL) {
  93. BIO_printf(bio_err, "Out of memory.\n");
  94. goto end;
  95. }
  96. BIO_printf(bio_out, "%s\n", s);
  97. OPENSSL_free(s);
  98. } else {
  99. for ( ; *argv; argv++) {
  100. int r;
  101. if (hex)
  102. r = BN_hex2bn(&bn, argv[0]);
  103. else
  104. r = BN_dec2bn(&bn, argv[0]);
  105. if (!r) {
  106. BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]);
  107. goto end;
  108. }
  109. BN_print(bio_out, bn);
  110. BIO_printf(bio_out, " (%s) %s prime\n",
  111. argv[0],
  112. BN_is_prime_ex(bn, checks, NULL, NULL)
  113. ? "is" : "is not");
  114. }
  115. }
  116. ret = 0;
  117. end:
  118. BN_free(bn);
  119. return ret;
  120. }