rand.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 1998-2022 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 "apps.h"
  10. #include "progs.h"
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/err.h>
  16. #include <openssl/rand.h>
  17. typedef enum OPTION_choice {
  18. OPT_COMMON,
  19. OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX,
  20. OPT_R_ENUM, OPT_PROV_ENUM
  21. } OPTION_CHOICE;
  22. const OPTIONS rand_options[] = {
  23. {OPT_HELP_STR, 1, '-', "Usage: %s [options] num\n"},
  24. OPT_SECTION("General"),
  25. {"help", OPT_HELP, '-', "Display this summary"},
  26. #ifndef OPENSSL_NO_ENGINE
  27. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  28. #endif
  29. OPT_SECTION("Output"),
  30. {"out", OPT_OUT, '>', "Output file"},
  31. {"base64", OPT_BASE64, '-', "Base64 encode output"},
  32. {"hex", OPT_HEX, '-', "Hex encode output"},
  33. OPT_R_OPTIONS,
  34. OPT_PROV_OPTIONS,
  35. OPT_PARAMETERS(),
  36. {"num", 0, 0, "Number of bytes to generate"},
  37. {NULL}
  38. };
  39. int rand_main(int argc, char **argv)
  40. {
  41. ENGINE *e = NULL;
  42. BIO *out = NULL;
  43. char *outfile = NULL, *prog;
  44. OPTION_CHOICE o;
  45. int format = FORMAT_BINARY, r, i, ret = 1, buflen = 131072;
  46. long num = -1;
  47. uint8_t *buf = NULL;
  48. prog = opt_init(argc, argv, rand_options);
  49. while ((o = opt_next()) != OPT_EOF) {
  50. switch (o) {
  51. case OPT_EOF:
  52. case OPT_ERR:
  53. opthelp:
  54. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  55. goto end;
  56. case OPT_HELP:
  57. opt_help(rand_options);
  58. ret = 0;
  59. goto end;
  60. case OPT_OUT:
  61. outfile = opt_arg();
  62. break;
  63. case OPT_ENGINE:
  64. e = setup_engine(opt_arg(), 0);
  65. break;
  66. case OPT_R_CASES:
  67. if (!opt_rand(o))
  68. goto end;
  69. break;
  70. case OPT_BASE64:
  71. format = FORMAT_BASE64;
  72. break;
  73. case OPT_HEX:
  74. format = FORMAT_TEXT;
  75. break;
  76. case OPT_PROV_CASES:
  77. if (!opt_provider(o))
  78. goto end;
  79. break;
  80. }
  81. }
  82. /* Optional argument is number of bytes to generate. */
  83. argc = opt_num_rest();
  84. argv = opt_rest();
  85. if (argc == 1) {
  86. if (!opt_long(argv[0], &num) || num <= 0)
  87. goto opthelp;
  88. } else if (!opt_check_rest_arg(NULL)) {
  89. goto opthelp;
  90. }
  91. if (!app_RAND_load())
  92. goto end;
  93. out = bio_open_default(outfile, 'w', format);
  94. if (out == NULL)
  95. goto end;
  96. if (format == FORMAT_BASE64) {
  97. BIO *b64 = BIO_new(BIO_f_base64());
  98. if (b64 == NULL)
  99. goto end;
  100. out = BIO_push(b64, out);
  101. }
  102. buf = app_malloc(buflen, "buffer for output file");
  103. while (num > 0) {
  104. long chunk;
  105. chunk = (num > buflen) ? buflen : num;
  106. r = RAND_bytes(buf, chunk);
  107. if (r <= 0)
  108. goto end;
  109. if (format != FORMAT_TEXT) {
  110. if (BIO_write(out, buf, chunk) != chunk)
  111. goto end;
  112. } else {
  113. for (i = 0; i < chunk; i++)
  114. if (BIO_printf(out, "%02x", buf[i]) != 2)
  115. goto end;
  116. }
  117. num -= chunk;
  118. }
  119. if (format == FORMAT_TEXT)
  120. BIO_puts(out, "\n");
  121. if (BIO_flush(out) <= 0)
  122. goto end;
  123. ret = 0;
  124. end:
  125. if (ret != 0)
  126. ERR_print_errors(bio_err);
  127. OPENSSL_free(buf);
  128. release_engine(e);
  129. BIO_free_all(out);
  130. return ret;
  131. }