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