prime.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. OPT_PROV_ENUM
  17. } OPTION_CHOICE;
  18. const OPTIONS prime_options[] = {
  19. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [number...]\n"},
  20. OPT_SECTION("General"),
  21. {"help", OPT_HELP, '-', "Display this summary"},
  22. {"bits", OPT_BITS, 'p', "Size of number in bits"},
  23. {"checks", OPT_CHECKS, 'p', "Number of checks"},
  24. OPT_SECTION("Output"),
  25. {"hex", OPT_HEX, '-', "Hex output"},
  26. {"generate", OPT_GENERATE, '-', "Generate a prime"},
  27. {"safe", OPT_SAFE, '-',
  28. "When used with -generate, generate a safe prime"},
  29. OPT_PROV_OPTIONS,
  30. OPT_PARAMETERS(),
  31. {"number", 0, 0, "Number(s) to check for primality if not generating"},
  32. {NULL}
  33. };
  34. int prime_main(int argc, char **argv)
  35. {
  36. BIGNUM *bn = NULL;
  37. int hex = 0, generate = 0, bits = 0, safe = 0, ret = 1;
  38. char *prog;
  39. OPTION_CHOICE o;
  40. prog = opt_init(argc, argv, prime_options);
  41. while ((o = opt_next()) != OPT_EOF) {
  42. switch (o) {
  43. case OPT_EOF:
  44. case OPT_ERR:
  45. opthelp:
  46. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  47. goto end;
  48. case OPT_HELP:
  49. opt_help(prime_options);
  50. ret = 0;
  51. goto end;
  52. case OPT_HEX:
  53. hex = 1;
  54. break;
  55. case OPT_GENERATE:
  56. generate = 1;
  57. break;
  58. case OPT_BITS:
  59. bits = atoi(opt_arg());
  60. break;
  61. case OPT_SAFE:
  62. safe = 1;
  63. break;
  64. case OPT_CHECKS:
  65. /* ignore parameter and argument */
  66. opt_arg();
  67. break;
  68. case OPT_PROV_CASES:
  69. if (!opt_provider(o))
  70. goto end;
  71. break;
  72. }
  73. }
  74. argc = opt_num_rest();
  75. argv = opt_rest();
  76. if (generate) {
  77. if (argc != 0) {
  78. BIO_printf(bio_err, "Extra arguments given.\n");
  79. goto opthelp;
  80. }
  81. } else if (argc == 0) {
  82. BIO_printf(bio_err, "%s: No prime specified\n", prog);
  83. goto opthelp;
  84. }
  85. if (generate) {
  86. char *s;
  87. if (!bits) {
  88. BIO_printf(bio_err, "Specify the number of bits.\n");
  89. goto end;
  90. }
  91. bn = BN_new();
  92. if (bn == NULL) {
  93. BIO_printf(bio_err, "Out of memory.\n");
  94. goto end;
  95. }
  96. if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {
  97. BIO_printf(bio_err, "Failed to generate prime.\n");
  98. goto end;
  99. }
  100. s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
  101. if (s == NULL) {
  102. BIO_printf(bio_err, "Out of memory.\n");
  103. goto end;
  104. }
  105. BIO_printf(bio_out, "%s\n", s);
  106. OPENSSL_free(s);
  107. } else {
  108. for ( ; *argv; argv++) {
  109. int r;
  110. if (hex)
  111. r = BN_hex2bn(&bn, argv[0]);
  112. else
  113. r = BN_dec2bn(&bn, argv[0]);
  114. if (!r) {
  115. BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]);
  116. goto end;
  117. }
  118. BN_print(bio_out, bn);
  119. BIO_printf(bio_out, " (%s) %s prime\n",
  120. argv[0],
  121. BN_check_prime(bn, NULL, NULL)
  122. ? "is" : "is not");
  123. }
  124. }
  125. ret = 0;
  126. end:
  127. BN_free(bn);
  128. return ret;
  129. }