prime.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /* Optional arguments are numbers to check. */
  75. argc = opt_num_rest();
  76. argv = opt_rest();
  77. if (generate) {
  78. if (argc != 0)
  79. goto opthelp;
  80. } else if (argc == 0) {
  81. goto opthelp;
  82. }
  83. if (generate) {
  84. char *s;
  85. if (!bits) {
  86. BIO_printf(bio_err, "Specify the number of bits.\n");
  87. goto end;
  88. }
  89. bn = BN_new();
  90. if (bn == NULL) {
  91. BIO_printf(bio_err, "Out of memory.\n");
  92. goto end;
  93. }
  94. if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {
  95. BIO_printf(bio_err, "Failed to generate prime.\n");
  96. goto end;
  97. }
  98. s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
  99. if (s == NULL) {
  100. BIO_printf(bio_err, "Out of memory.\n");
  101. goto end;
  102. }
  103. BIO_printf(bio_out, "%s\n", s);
  104. OPENSSL_free(s);
  105. } else {
  106. for ( ; *argv; argv++) {
  107. int r;
  108. if (hex)
  109. r = BN_hex2bn(&bn, argv[0]);
  110. else
  111. r = BN_dec2bn(&bn, argv[0]);
  112. if (!r) {
  113. BIO_printf(bio_err, "Failed to process value (%s)\n", argv[0]);
  114. goto end;
  115. }
  116. BN_print(bio_out, bn);
  117. BIO_printf(bio_out, " (%s) %s prime\n",
  118. argv[0],
  119. BN_check_prime(bn, NULL, NULL)
  120. ? "is" : "is not");
  121. }
  122. }
  123. ret = 0;
  124. end:
  125. BN_free(bn);
  126. return ret;
  127. }