]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/rand.c
Update copyright year
[thirdparty/openssl.git] / apps / rand.c
CommitLineData
846e33c7 1/*
6738bf14 2 * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved.
a661b653 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (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
a661b653 8 */
27b78273
BM
9
10#include "apps.h"
dab2cd68 11#include "progs.h"
27b78273
BM
12
13#include <ctype.h>
14#include <stdio.h>
8824ec7c 15#include <string.h>
27b78273
BM
16
17#include <openssl/bio.h>
18#include <openssl/err.h>
19#include <openssl/rand.h>
20
7e1b7485
RS
21typedef enum OPTION_choice {
22 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
3ee1eac2
RS
23 OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX,
24 OPT_R_ENUM
7e1b7485
RS
25} OPTION_CHOICE;
26
44c83ebd 27const OPTIONS rand_options[] = {
7e1b7485
RS
28 {OPT_HELP_STR, 1, '-', "Usage: %s [flags] num\n"},
29 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
30 {"help", OPT_HELP, '-', "Display this summary"},
31 {"out", OPT_OUT, '>', "Output file"},
3ee1eac2 32 OPT_R_OPTIONS,
7e1b7485
RS
33 {"base64", OPT_BASE64, '-', "Base64 encode output"},
34 {"hex", OPT_HEX, '-', "Hex encode output"},
0b13e9f0 35#ifndef OPENSSL_NO_ENGINE
7e1b7485 36 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
0b13e9f0 37#endif
7e1b7485
RS
38 {NULL}
39};
27b78273 40
7e1b7485
RS
41int rand_main(int argc, char **argv)
42{
dd1abd44 43 ENGINE *e = NULL;
7e1b7485 44 BIO *out = NULL;
3ee1eac2 45 char *outfile = NULL, *prog;
7e1b7485 46 OPTION_CHOICE o;
bdd58d98 47 int format = FORMAT_BINARY, i, num = -1, r, ret = 1;
7e1b7485
RS
48
49 prog = opt_init(argc, argv, rand_options);
50 while ((o = opt_next()) != OPT_EOF) {
51 switch (o) {
52 case OPT_EOF:
53 case OPT_ERR:
54 opthelp:
55 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
56 goto end;
57 case OPT_HELP:
58 opt_help(rand_options);
59 ret = 0;
60 goto end;
61 case OPT_OUT:
62 outfile = opt_arg();
63 break;
64 case OPT_ENGINE:
dd1abd44 65 e = setup_engine(opt_arg(), 0);
7e1b7485 66 break;
3ee1eac2
RS
67 case OPT_R_CASES:
68 if (!opt_rand(o))
69 goto end;
7e1b7485
RS
70 break;
71 case OPT_BASE64:
bdd58d98 72 format = FORMAT_BASE64;
7e1b7485
RS
73 break;
74 case OPT_HEX:
bdd58d98 75 format = FORMAT_TEXT;
7e1b7485 76 break;
0f113f3e 77 }
0f113f3e 78 }
7e1b7485
RS
79 argc = opt_num_rest();
80 argv = opt_rest();
c27363f5
RS
81 if (argc == 1) {
82 if (!opt_int(argv[0], &num) || num <= 0)
83 goto end;
84 } else if (argc > 0) {
85 BIO_printf(bio_err, "Extra arguments given.\n");
7e1b7485 86 goto opthelp;
c27363f5 87 }
0f113f3e 88
bdd58d98 89 out = bio_open_default(outfile, 'w', format);
0f113f3e 90 if (out == NULL)
7e1b7485 91 goto end;
0f113f3e 92
bdd58d98 93 if (format == FORMAT_BASE64) {
0f113f3e
MC
94 BIO *b64 = BIO_new(BIO_f_base64());
95 if (b64 == NULL)
7e1b7485 96 goto end;
0f113f3e
MC
97 out = BIO_push(b64, out);
98 }
99
100 while (num > 0) {
101 unsigned char buf[4096];
102 int chunk;
103
104 chunk = num;
105 if (chunk > (int)sizeof(buf))
cbe29648 106 chunk = sizeof(buf);
0f113f3e
MC
107 r = RAND_bytes(buf, chunk);
108 if (r <= 0)
7e1b7485 109 goto end;
0f91e1df
RS
110 if (format != FORMAT_TEXT) {
111 if (BIO_write(out, buf, chunk) != chunk)
112 goto end;
113 } else {
0f113f3e 114 for (i = 0; i < chunk; i++)
0f91e1df
RS
115 if (BIO_printf(out, "%02x", buf[i]) != 2)
116 goto end;
0f113f3e
MC
117 }
118 num -= chunk;
119 }
bdd58d98 120 if (format == FORMAT_TEXT)
0f113f3e 121 BIO_puts(out, "\n");
3ee1eac2 122 if (BIO_flush(out) <= 0)
0f91e1df 123 goto end;
0f113f3e 124
0f113f3e
MC
125 ret = 0;
126
7e1b7485 127 end:
0f91e1df
RS
128 if (ret != 0)
129 ERR_print_errors(bio_err);
dd1abd44 130 release_engine(e);
ca3a82c3 131 BIO_free_all(out);
26a7d938 132 return ret;
0f113f3e 133}