rand.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 1998-2021 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, i, num = -1, r, ret = 1;
  46. prog = opt_init(argc, argv, rand_options);
  47. while ((o = opt_next()) != OPT_EOF) {
  48. switch (o) {
  49. case OPT_EOF:
  50. case OPT_ERR:
  51. opthelp:
  52. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  53. goto end;
  54. case OPT_HELP:
  55. opt_help(rand_options);
  56. ret = 0;
  57. goto end;
  58. case OPT_OUT:
  59. outfile = opt_arg();
  60. break;
  61. case OPT_ENGINE:
  62. e = setup_engine(opt_arg(), 0);
  63. break;
  64. case OPT_R_CASES:
  65. if (!opt_rand(o))
  66. goto end;
  67. break;
  68. case OPT_BASE64:
  69. format = FORMAT_BASE64;
  70. break;
  71. case OPT_HEX:
  72. format = FORMAT_TEXT;
  73. break;
  74. case OPT_PROV_CASES:
  75. if (!opt_provider(o))
  76. goto end;
  77. break;
  78. }
  79. }
  80. /* Optional argument is number of bytes to generate. */
  81. argc = opt_num_rest();
  82. argv = opt_rest();
  83. if (argc == 1) {
  84. if (!opt_int(argv[0], &num) || num <= 0)
  85. goto opthelp;
  86. } else if (argc != 0) {
  87. goto opthelp;
  88. }
  89. if (!app_RAND_load())
  90. goto end;
  91. out = bio_open_default(outfile, 'w', format);
  92. if (out == NULL)
  93. goto end;
  94. if (format == FORMAT_BASE64) {
  95. BIO *b64 = BIO_new(BIO_f_base64());
  96. if (b64 == NULL)
  97. goto end;
  98. out = BIO_push(b64, out);
  99. }
  100. while (num > 0) {
  101. unsigned char buf[4096];
  102. int chunk;
  103. chunk = num;
  104. if (chunk > (int)sizeof(buf))
  105. chunk = sizeof(buf);
  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. release_engine(e);
  128. BIO_free_all(out);
  129. return ret;
  130. }