]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/rand.c
More tweaks for comments due indent issues
[thirdparty/openssl.git] / apps / rand.c
CommitLineData
27b78273 1/* apps/rand.c */
a661b653
BM
2/* ====================================================================
3 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
27b78273
BM
55
56#include "apps.h"
57
58#include <ctype.h>
59#include <stdio.h>
8824ec7c 60#include <string.h>
27b78273
BM
61
62#include <openssl/bio.h>
63#include <openssl/err.h>
64#include <openssl/rand.h>
65
66#undef PROG
67#define PROG rand_main
68
3e9a08ec
TH
69/*-
70 * -out file - write to file
27b78273 71 * -rand file:file - PRNG seed files
7ca1cfba
BM
72 * -base64 - base64 encode output
73 * -hex - hex encode output
27b78273
BM
74 * num - write 'num' bytes
75 */
76
77int MAIN(int, char **);
78
79int MAIN(int argc, char **argv)
80 {
81 int i, r, ret = 1;
82 int badopt;
83 char *outfile = NULL;
f365611c 84 char *inrand = NULL;
27b78273 85 int base64 = 0;
7ca1cfba 86 int hex = 0;
27b78273
BM
87 BIO *out = NULL;
88 int num = -1;
0b13e9f0 89#ifndef OPENSSL_NO_ENGINE
5270e702 90 char *engine=NULL;
0b13e9f0 91#endif
27b78273
BM
92
93 apps_startup();
94
95 if (bio_err == NULL)
96 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
97 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
98
3647bee2
DSH
99 if (!load_config(bio_err, NULL))
100 goto err;
101
27b78273
BM
102 badopt = 0;
103 i = 0;
104 while (!badopt && argv[++i] != NULL)
105 {
106 if (strcmp(argv[i], "-out") == 0)
107 {
108 if ((argv[i+1] != NULL) && (outfile == NULL))
109 outfile = argv[++i];
110 else
111 badopt = 1;
112 }
0b13e9f0 113#ifndef OPENSSL_NO_ENGINE
73fc98a7 114 else if (strcmp(argv[i], "-engine") == 0)
5270e702
RL
115 {
116 if ((argv[i+1] != NULL) && (engine == NULL))
117 engine = argv[++i];
118 else
119 badopt = 1;
120 }
0b13e9f0 121#endif
27b78273
BM
122 else if (strcmp(argv[i], "-rand") == 0)
123 {
124 if ((argv[i+1] != NULL) && (inrand == NULL))
125 inrand = argv[++i];
126 else
127 badopt = 1;
128 }
129 else if (strcmp(argv[i], "-base64") == 0)
130 {
131 if (!base64)
132 base64 = 1;
133 else
134 badopt = 1;
135 }
7ca1cfba
BM
136 else if (strcmp(argv[i], "-hex") == 0)
137 {
138 if (!hex)
139 hex = 1;
140 else
141 badopt = 1;
142 }
d9586857 143 else if (isdigit((unsigned char)argv[i][0]))
27b78273
BM
144 {
145 if (num < 0)
146 {
147 r = sscanf(argv[i], "%d", &num);
148 if (r == 0 || num < 0)
149 badopt = 1;
150 }
151 else
152 badopt = 1;
153 }
154 else
155 badopt = 1;
156 }
157
7ca1cfba
BM
158 if (hex && base64)
159 badopt = 1;
160
27b78273
BM
161 if (num < 0)
162 badopt = 1;
163
164 if (badopt)
165 {
166 BIO_printf(bio_err, "Usage: rand [options] num\n");
167 BIO_printf(bio_err, "where options are\n");
5270e702 168 BIO_printf(bio_err, "-out file - write to file\n");
0b13e9f0 169#ifndef OPENSSL_NO_ENGINE
1613c4d3 170 BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n");
0b13e9f0 171#endif
5270e702 172 BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
7ca1cfba
BM
173 BIO_printf(bio_err, "-base64 - base64 encode output\n");
174 BIO_printf(bio_err, "-hex - hex encode output\n");
27b78273
BM
175 goto err;
176 }
177
0b13e9f0 178#ifndef OPENSSL_NO_ENGINE
e9735943 179 setup_engine(bio_err, engine, 0);
0b13e9f0 180#endif
5270e702 181
f365611c 182 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
27b78273
BM
183 if (inrand != NULL)
184 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
185 app_RAND_load_files(inrand));
186
187 out = BIO_new(BIO_s_file());
188 if (out == NULL)
189 goto err;
190 if (outfile != NULL)
191 r = BIO_write_filename(out, outfile);
192 else
645749ef 193 {
27b78273 194 r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
bc36ee62 195#ifdef OPENSSL_SYS_VMS
645749ef
RL
196 {
197 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
198 out = BIO_push(tmpbio, out);
199 }
200#endif
201 }
27b78273
BM
202 if (r <= 0)
203 goto err;
204
205 if (base64)
206 {
207 BIO *b64 = BIO_new(BIO_f_base64());
208 if (b64 == NULL)
209 goto err;
210 out = BIO_push(b64, out);
211 }
212
213 while (num > 0)
214 {
215 unsigned char buf[4096];
216 int chunk;
217
218 chunk = num;
27545970 219 if (chunk > (int)sizeof(buf))
27b78273
BM
220 chunk = sizeof buf;
221 r = RAND_bytes(buf, chunk);
222 if (r <= 0)
223 goto err;
7ca1cfba
BM
224 if (!hex)
225 BIO_write(out, buf, chunk);
226 else
227 {
228 for (i = 0; i < chunk; i++)
229 BIO_printf(out, "%02x", buf[i]);
7ca1cfba 230 }
27b78273
BM
231 num -= chunk;
232 }
d615bceb
BM
233 if (hex)
234 BIO_puts(out, "\n");
710069c1 235 (void)BIO_flush(out);
27b78273
BM
236
237 app_RAND_write_file(NULL, bio_err);
238 ret = 0;
239
240err:
241 ERR_print_errors(bio_err);
242 if (out)
243 BIO_free_all(out);
c04f8cf4 244 apps_shutdown();
1c3e4a36 245 OPENSSL_EXIT(ret);
27b78273 246 }