make update
[openssl.git] / apps / rand.c
1 /* apps/rand.c */
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  */
55
56 #include "apps.h"
57
58 #include <ctype.h>
59 #include <stdio.h>
60 #include <string.h>
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
69 /* -out file         - write to file
70  * -rand file:file   - PRNG seed files
71  * -base64           - encode output
72  * num               - write 'num' bytes
73  */
74
75 int MAIN(int, char **);
76
77 int MAIN(int argc, char **argv)
78         {
79         ENGINE *e = NULL;
80         int i, r, ret = 1;
81         int badopt;
82         char *outfile = NULL;
83         char *inrand = NULL;
84         int base64 = 0;
85         BIO *out = NULL;
86         int num = -1;
87         char *engine=NULL;
88
89         apps_startup();
90
91         if (bio_err == NULL)
92                 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
93                         BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
94
95         badopt = 0;
96         i = 0;
97         while (!badopt && argv[++i] != NULL)
98                 {
99                 if (strcmp(argv[i], "-out") == 0)
100                         {
101                         if ((argv[i+1] != NULL) && (outfile == NULL))
102                                 outfile = argv[++i];
103                         else
104                                 badopt = 1;
105                         }
106                 else if (strcmp(argv[i], "-engine") == 0)
107                         {
108                         if ((argv[i+1] != NULL) && (engine == NULL))
109                                 engine = argv[++i];
110                         else
111                                 badopt = 1;
112                         }
113                 else if (strcmp(argv[i], "-rand") == 0)
114                         {
115                         if ((argv[i+1] != NULL) && (inrand == NULL))
116                                 inrand = argv[++i];
117                         else
118                                 badopt = 1;
119                         }
120                 else if (strcmp(argv[i], "-base64") == 0)
121                         {
122                         if (!base64)
123                                 base64 = 1;
124                         else
125                                 badopt = 1;
126                         }
127                 else if (isdigit((unsigned char)argv[i][0]))
128                         {
129                         if (num < 0)
130                                 {
131                                 r = sscanf(argv[i], "%d", &num);
132                                 if (r == 0 || num < 0)
133                                         badopt = 1;
134                                 }
135                         else
136                                 badopt = 1;
137                         }
138                 else
139                         badopt = 1;
140                 }
141
142         if (num < 0)
143                 badopt = 1;
144         
145         if (badopt) 
146                 {
147                 BIO_printf(bio_err, "Usage: rand [options] num\n");
148                 BIO_printf(bio_err, "where options are\n");
149                 BIO_printf(bio_err, "-out file             - write to file\n");
150                 BIO_printf(bio_err, "-engine e             - use engine e, possibly a hardware device.\n");
151                 BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
152                 BIO_printf(bio_err, "-base64               - encode output\n");
153                 goto err;
154                 }
155
156         e = setup_engine(bio_err, engine, 0);
157
158         app_RAND_load_file(NULL, bio_err, (inrand != NULL));
159         if (inrand != NULL)
160                 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
161                         app_RAND_load_files(inrand));
162
163         out = BIO_new(BIO_s_file());
164         if (out == NULL)
165                 goto err;
166         if (outfile != NULL)
167                 r = BIO_write_filename(out, outfile);
168         else
169                 {
170                 r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
171 #ifdef OPENSSL_SYS_VMS
172                 {
173                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
174                 out = BIO_push(tmpbio, out);
175                 }
176 #endif
177                 }
178         if (r <= 0)
179                 goto err;
180
181         if (base64)
182                 {
183                 BIO *b64 = BIO_new(BIO_f_base64());
184                 if (b64 == NULL)
185                         goto err;
186                 out = BIO_push(b64, out);
187                 }
188         
189         while (num > 0) 
190                 {
191                 unsigned char buf[4096];
192                 int chunk;
193
194                 chunk = num;
195                 if (chunk > sizeof buf)
196                         chunk = sizeof buf;
197                 r = RAND_bytes(buf, chunk);
198                 if (r <= 0)
199                         goto err;
200                 BIO_write(out, buf, chunk);
201                 num -= chunk;
202                 }
203         BIO_flush(out);
204
205         app_RAND_write_file(NULL, bio_err);
206         ret = 0;
207         
208 err:
209         ERR_print_errors(bio_err);
210         if (out)
211                 BIO_free_all(out);
212         apps_shutdown();
213         EXIT(ret);
214         }