rand.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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[K|M|G|T]\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;
  46. size_t buflen = (1 << 16); /* max rand chunk size is 2^16 bytes */
  47. long num = -1;
  48. uint64_t scaled_num = 0;
  49. uint8_t *buf = NULL;
  50. prog = opt_init(argc, argv, rand_options);
  51. while ((o = opt_next()) != OPT_EOF) {
  52. switch (o) {
  53. case OPT_EOF:
  54. case OPT_ERR:
  55. opthelp:
  56. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  57. goto end;
  58. case OPT_HELP:
  59. opt_help(rand_options);
  60. ret = 0;
  61. goto end;
  62. case OPT_OUT:
  63. outfile = opt_arg();
  64. break;
  65. case OPT_ENGINE:
  66. e = setup_engine(opt_arg(), 0);
  67. break;
  68. case OPT_R_CASES:
  69. if (!opt_rand(o))
  70. goto end;
  71. break;
  72. case OPT_BASE64:
  73. format = FORMAT_BASE64;
  74. break;
  75. case OPT_HEX:
  76. format = FORMAT_TEXT;
  77. break;
  78. case OPT_PROV_CASES:
  79. if (!opt_provider(o))
  80. goto end;
  81. break;
  82. }
  83. }
  84. /* Optional argument is number of bytes to generate. */
  85. argc = opt_num_rest();
  86. argv = opt_rest();
  87. if (argc == 1) {
  88. int factoridx = 0;
  89. int shift = 0;
  90. /*
  91. * special case for requesting the max allowed
  92. * number of random bytes to be generated
  93. */
  94. if (!strcmp(argv[0], "max")) {
  95. /*
  96. * 2^61 bytes is the limit of random output
  97. * per drbg instantiation
  98. */
  99. scaled_num = UINT64_MAX >> 3;
  100. } else {
  101. /*
  102. * iterate over the value and check to see if there are
  103. * any non-numerical chars
  104. * A non digit suffix indicates we need to shift the
  105. * number of requested bytes by a factor of:
  106. * K = 1024^1 (1 << (10 * 1))
  107. * M = 1024^2 (1 << (10 * 2))
  108. * G = 1024^3 (1 << (10 * 3))
  109. * T = 1024^4 (1 << (10 * 4))
  110. * which can be achieved by bit-shifting the number
  111. */
  112. while (argv[0][factoridx]) {
  113. if (!isdigit((int)(argv[0][factoridx]))) {
  114. switch(argv[0][factoridx]) {
  115. case 'K':
  116. shift = 10;
  117. break;
  118. case 'M':
  119. shift = 20;
  120. break;
  121. case 'G':
  122. shift = 30;
  123. break;
  124. case 'T':
  125. shift = 40;
  126. break;
  127. default:
  128. BIO_printf(bio_err, "Invalid size suffix %s\n",
  129. &argv[0][factoridx]);
  130. goto opthelp;
  131. }
  132. break;
  133. }
  134. factoridx++;
  135. }
  136. if (shift != 0 && strlen(&argv[0][factoridx]) != 1) {
  137. BIO_printf(bio_err, "Invalid size suffix %s\n",
  138. &argv[0][factoridx]);
  139. goto opthelp;
  140. }
  141. }
  142. /* Remove the suffix from the arg so that opt_long works */
  143. if (shift != 0)
  144. argv[0][factoridx] = '\0';
  145. if ((scaled_num == 0) && (!opt_long(argv[0], &num) || num <= 0))
  146. goto opthelp;
  147. if (shift != 0) {
  148. /* check for overflow */
  149. if ((UINT64_MAX >> shift) < (size_t)num) {
  150. BIO_printf(bio_err, "%lu bytes with suffix overflows\n",
  151. num);
  152. goto opthelp;
  153. }
  154. scaled_num = num << shift;
  155. if (scaled_num > (UINT64_MAX >> 3)) {
  156. BIO_printf(bio_err, "Request exceeds max allowed output\n");
  157. goto opthelp;
  158. }
  159. } else {
  160. if (scaled_num == 0)
  161. scaled_num = num;
  162. }
  163. } else if (!opt_check_rest_arg(NULL)) {
  164. goto opthelp;
  165. }
  166. if (!app_RAND_load())
  167. goto end;
  168. out = bio_open_default(outfile, 'w', format);
  169. if (out == NULL)
  170. goto end;
  171. if (format == FORMAT_BASE64) {
  172. BIO *b64 = BIO_new(BIO_f_base64());
  173. if (b64 == NULL)
  174. goto end;
  175. out = BIO_push(b64, out);
  176. }
  177. buf = app_malloc(buflen, "buffer for output file");
  178. while (scaled_num > 0) {
  179. int chunk;
  180. chunk = scaled_num > buflen ? (int)buflen : (int)scaled_num;
  181. r = RAND_bytes(buf, chunk);
  182. if (r <= 0)
  183. goto end;
  184. if (format != FORMAT_TEXT) {
  185. if (BIO_write(out, buf, chunk) != chunk)
  186. goto end;
  187. } else {
  188. for (i = 0; i < chunk; i++)
  189. if (BIO_printf(out, "%02x", buf[i]) != 2)
  190. goto end;
  191. }
  192. scaled_num -= chunk;
  193. }
  194. if (format == FORMAT_TEXT)
  195. BIO_puts(out, "\n");
  196. if (BIO_flush(out) <= 0)
  197. goto end;
  198. ret = 0;
  199. end:
  200. if (ret != 0)
  201. ERR_print_errors(bio_err);
  202. OPENSSL_free(buf);
  203. release_engine(e);
  204. BIO_free_all(out);
  205. return ret;
  206. }