2 * Copyright 1998-2022 The OpenSSL Project Authors. All Rights Reserved.
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
17 #include <openssl/bio.h>
18 #include <openssl/err.h>
19 #include <openssl/rand.h>
21 typedef enum OPTION_choice {
23 OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX,
24 OPT_R_ENUM, OPT_PROV_ENUM
27 const OPTIONS rand_options[] = {
28 {OPT_HELP_STR, 1, '-', "Usage: %s [options] num[K|M|G|T]\n"},
30 OPT_SECTION("General"),
31 {"help", OPT_HELP, '-', "Display this summary"},
32 #ifndef OPENSSL_NO_ENGINE
33 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
36 OPT_SECTION("Output"),
37 {"out", OPT_OUT, '>', "Output file"},
38 {"base64", OPT_BASE64, '-', "Base64 encode output"},
39 {"hex", OPT_HEX, '-', "Hex encode output"},
45 {"num", 0, 0, "Number of bytes to generate"},
49 int rand_main(int argc, char **argv)
53 char *outfile = NULL, *prog;
55 int format = FORMAT_BINARY, r, i, ret = 1;
56 size_t buflen = (1 << 16); /* max rand chunk size is 2^16 bytes */
58 uint64_t scaled_num = 0;
61 prog = opt_init(argc, argv, rand_options);
62 while ((o = opt_next()) != OPT_EOF) {
67 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
70 opt_help(rand_options);
77 e = setup_engine(opt_arg(), 0);
84 format = FORMAT_BASE64;
96 /* Optional argument is number of bytes to generate. */
97 argc = opt_num_rest();
104 * special case for requesting the max allowed
105 * number of random bytes to be generated
107 if (!strcmp(argv[0], "max")) {
109 * 2^61 bytes is the limit of random output
110 * per drbg instantiation
112 scaled_num = UINT64_MAX >> 3;
115 * iterate over the value and check to see if there are
116 * any non-numerical chars
117 * A non digit suffix indicates we need to shift the
118 * number of requested bytes by a factor of:
119 * K = 1024^1 (1 << (10 * 1))
120 * M = 1024^2 (1 << (10 * 2))
121 * G = 1024^3 (1 << (10 * 3))
122 * T = 1024^4 (1 << (10 * 4))
123 * which can be achieved by bit-shifting the number
125 while (argv[0][factoridx]) {
126 if (!isdigit((int)(argv[0][factoridx]))) {
127 switch(argv[0][factoridx]) {
141 BIO_printf(bio_err, "Invalid size suffix %s\n",
142 &argv[0][factoridx]);
150 if (shift != 0 && strlen(&argv[0][factoridx]) != 1) {
151 BIO_printf(bio_err, "Invalid size suffix %s\n",
152 &argv[0][factoridx]);
156 /* Remove the suffix from the arg so that opt_long works */
158 argv[0][factoridx] = '\0';
160 if ((scaled_num == 0) && (!opt_long(argv[0], &num) || num <= 0))
164 /* check for overflow */
165 if ((UINT64_MAX >> shift) < (size_t)num) {
166 BIO_printf(bio_err, "%lu bytes with suffix overflows\n",
170 scaled_num = num << shift;
171 if (scaled_num > (UINT64_MAX >> 3)) {
172 BIO_printf(bio_err, "Request exceeds max allowed output\n");
179 } else if (!opt_check_rest_arg(NULL)) {
183 if (!app_RAND_load())
186 out = bio_open_default(outfile, 'w', format);
190 if (format == FORMAT_BASE64) {
191 BIO *b64 = BIO_new(BIO_f_base64());
194 out = BIO_push(b64, out);
197 buf = app_malloc(buflen, "buffer for output file");
198 while (scaled_num > 0) {
201 chunk = scaled_num > buflen ? (int)buflen : (int)scaled_num;
202 r = RAND_bytes(buf, chunk);
205 if (format != FORMAT_TEXT) {
206 if (BIO_write(out, buf, chunk) != chunk)
209 for (i = 0; i < chunk; i++)
210 if (BIO_printf(out, "%02x", buf[i]) != 2)
215 if (format == FORMAT_TEXT)
217 if (BIO_flush(out) <= 0)
224 ERR_print_errors(bio_err);