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