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