1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include "apps.h"
11e1051a39Sopenharmony_ci#include "progs.h"
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#include <ctype.h>
14e1051a39Sopenharmony_ci#include <stdio.h>
15e1051a39Sopenharmony_ci#include <string.h>
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci#include <openssl/bio.h>
18e1051a39Sopenharmony_ci#include <openssl/err.h>
19e1051a39Sopenharmony_ci#include <openssl/rand.h>
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_citypedef enum OPTION_choice {
22e1051a39Sopenharmony_ci    OPT_COMMON,
23e1051a39Sopenharmony_ci    OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX,
24e1051a39Sopenharmony_ci    OPT_R_ENUM, OPT_PROV_ENUM
25e1051a39Sopenharmony_ci} OPTION_CHOICE;
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ciconst OPTIONS rand_options[] = {
28e1051a39Sopenharmony_ci    {OPT_HELP_STR, 1, '-', "Usage: %s [options] num\n"},
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ci    OPT_SECTION("General"),
31e1051a39Sopenharmony_ci    {"help", OPT_HELP, '-', "Display this summary"},
32e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE
33e1051a39Sopenharmony_ci    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
34e1051a39Sopenharmony_ci#endif
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    OPT_SECTION("Output"),
37e1051a39Sopenharmony_ci    {"out", OPT_OUT, '>', "Output file"},
38e1051a39Sopenharmony_ci    {"base64", OPT_BASE64, '-', "Base64 encode output"},
39e1051a39Sopenharmony_ci    {"hex", OPT_HEX, '-', "Hex encode output"},
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    OPT_R_OPTIONS,
42e1051a39Sopenharmony_ci    OPT_PROV_OPTIONS,
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_ci    OPT_PARAMETERS(),
45e1051a39Sopenharmony_ci    {"num", 0, 0, "Number of bytes to generate"},
46e1051a39Sopenharmony_ci    {NULL}
47e1051a39Sopenharmony_ci};
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_ciint rand_main(int argc, char **argv)
50e1051a39Sopenharmony_ci{
51e1051a39Sopenharmony_ci    ENGINE *e = NULL;
52e1051a39Sopenharmony_ci    BIO *out = NULL;
53e1051a39Sopenharmony_ci    char *outfile = NULL, *prog;
54e1051a39Sopenharmony_ci    OPTION_CHOICE o;
55e1051a39Sopenharmony_ci    int format = FORMAT_BINARY, i, num = -1, r, ret = 1;
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci    prog = opt_init(argc, argv, rand_options);
58e1051a39Sopenharmony_ci    while ((o = opt_next()) != OPT_EOF) {
59e1051a39Sopenharmony_ci        switch (o) {
60e1051a39Sopenharmony_ci        case OPT_EOF:
61e1051a39Sopenharmony_ci        case OPT_ERR:
62e1051a39Sopenharmony_ci opthelp:
63e1051a39Sopenharmony_ci            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
64e1051a39Sopenharmony_ci            goto end;
65e1051a39Sopenharmony_ci        case OPT_HELP:
66e1051a39Sopenharmony_ci            opt_help(rand_options);
67e1051a39Sopenharmony_ci            ret = 0;
68e1051a39Sopenharmony_ci            goto end;
69e1051a39Sopenharmony_ci        case OPT_OUT:
70e1051a39Sopenharmony_ci            outfile = opt_arg();
71e1051a39Sopenharmony_ci            break;
72e1051a39Sopenharmony_ci        case OPT_ENGINE:
73e1051a39Sopenharmony_ci            e = setup_engine(opt_arg(), 0);
74e1051a39Sopenharmony_ci            break;
75e1051a39Sopenharmony_ci        case OPT_R_CASES:
76e1051a39Sopenharmony_ci            if (!opt_rand(o))
77e1051a39Sopenharmony_ci                goto end;
78e1051a39Sopenharmony_ci            break;
79e1051a39Sopenharmony_ci        case OPT_BASE64:
80e1051a39Sopenharmony_ci            format = FORMAT_BASE64;
81e1051a39Sopenharmony_ci            break;
82e1051a39Sopenharmony_ci        case OPT_HEX:
83e1051a39Sopenharmony_ci            format = FORMAT_TEXT;
84e1051a39Sopenharmony_ci            break;
85e1051a39Sopenharmony_ci        case OPT_PROV_CASES:
86e1051a39Sopenharmony_ci            if (!opt_provider(o))
87e1051a39Sopenharmony_ci                goto end;
88e1051a39Sopenharmony_ci            break;
89e1051a39Sopenharmony_ci        }
90e1051a39Sopenharmony_ci    }
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci    /* Optional argument is number of bytes to generate. */
93e1051a39Sopenharmony_ci    argc = opt_num_rest();
94e1051a39Sopenharmony_ci    argv = opt_rest();
95e1051a39Sopenharmony_ci    if (argc == 1) {
96e1051a39Sopenharmony_ci        if (!opt_int(argv[0], &num) || num <= 0)
97e1051a39Sopenharmony_ci            goto opthelp;
98e1051a39Sopenharmony_ci    } else if (argc != 0) {
99e1051a39Sopenharmony_ci        goto opthelp;
100e1051a39Sopenharmony_ci    }
101e1051a39Sopenharmony_ci
102e1051a39Sopenharmony_ci    if (!app_RAND_load())
103e1051a39Sopenharmony_ci        goto end;
104e1051a39Sopenharmony_ci
105e1051a39Sopenharmony_ci    out = bio_open_default(outfile, 'w', format);
106e1051a39Sopenharmony_ci    if (out == NULL)
107e1051a39Sopenharmony_ci        goto end;
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_ci    if (format == FORMAT_BASE64) {
110e1051a39Sopenharmony_ci        BIO *b64 = BIO_new(BIO_f_base64());
111e1051a39Sopenharmony_ci        if (b64 == NULL)
112e1051a39Sopenharmony_ci            goto end;
113e1051a39Sopenharmony_ci        out = BIO_push(b64, out);
114e1051a39Sopenharmony_ci    }
115e1051a39Sopenharmony_ci
116e1051a39Sopenharmony_ci    while (num > 0) {
117e1051a39Sopenharmony_ci        unsigned char buf[4096];
118e1051a39Sopenharmony_ci        int chunk;
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_ci        chunk = num;
121e1051a39Sopenharmony_ci        if (chunk > (int)sizeof(buf))
122e1051a39Sopenharmony_ci            chunk = sizeof(buf);
123e1051a39Sopenharmony_ci        r = RAND_bytes(buf, chunk);
124e1051a39Sopenharmony_ci        if (r <= 0)
125e1051a39Sopenharmony_ci            goto end;
126e1051a39Sopenharmony_ci        if (format != FORMAT_TEXT) {
127e1051a39Sopenharmony_ci            if (BIO_write(out, buf, chunk) != chunk)
128e1051a39Sopenharmony_ci                goto end;
129e1051a39Sopenharmony_ci        } else {
130e1051a39Sopenharmony_ci            for (i = 0; i < chunk; i++)
131e1051a39Sopenharmony_ci                if (BIO_printf(out, "%02x", buf[i]) != 2)
132e1051a39Sopenharmony_ci                    goto end;
133e1051a39Sopenharmony_ci        }
134e1051a39Sopenharmony_ci        num -= chunk;
135e1051a39Sopenharmony_ci    }
136e1051a39Sopenharmony_ci    if (format == FORMAT_TEXT)
137e1051a39Sopenharmony_ci        BIO_puts(out, "\n");
138e1051a39Sopenharmony_ci    if (BIO_flush(out) <= 0)
139e1051a39Sopenharmony_ci        goto end;
140e1051a39Sopenharmony_ci
141e1051a39Sopenharmony_ci    ret = 0;
142e1051a39Sopenharmony_ci
143e1051a39Sopenharmony_ci end:
144e1051a39Sopenharmony_ci    if (ret != 0)
145e1051a39Sopenharmony_ci        ERR_print_errors(bio_err);
146e1051a39Sopenharmony_ci    release_engine(e);
147e1051a39Sopenharmony_ci    BIO_free_all(out);
148e1051a39Sopenharmony_ci    return ret;
149e1051a39Sopenharmony_ci}
150