Update contrib.
2 /* ====================================================================
3 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
62 #include <openssl/bio.h>
63 #include <openssl/err.h>
64 #include <openssl/rand.h>
67 #define PROG rand_main
69 /* -out file - write to file
70 * -rand file:file - PRNG seed files
71 * -base64 - encode output
72 * num - write 'num' bytes
76 int MAIN(int, char **);
78 int MAIN(int argc, char **argv)
80 #ifndef OPENSSL_NO_ENGINE
90 #ifndef OPENSSL_NO_ENGINE
97 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
98 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
101 if (!load_config(bio_err, NULL))
106 while (!badopt && argv[++i] != NULL)
108 if (strcmp(argv[i], "-out") == 0)
110 if ((argv[i+1] != NULL) && (outfile == NULL))
115 #ifndef OPENSSL_NO_ENGINE
116 else if (strcmp(argv[i], "-engine") == 0)
118 if ((argv[i+1] != NULL) && (engine == NULL))
124 else if (strcmp(argv[i], "-rand") == 0)
126 if ((argv[i+1] != NULL) && (inrand == NULL))
131 else if (strcmp(argv[i], "-base64") == 0)
138 else if (isdigit((unsigned char)argv[i][0]))
142 r = sscanf(argv[i], "%d", &num);
143 if (r == 0 || num < 0)
158 BIO_printf(bio_err, "Usage: rand [options] num\n");
159 BIO_printf(bio_err, "where options are\n");
160 BIO_printf(bio_err, "-out file - write to file\n");
161 #ifndef OPENSSL_NO_ENGINE
162 BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n");
164 BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
165 BIO_printf(bio_err, "-base64 - encode output\n");
169 #ifndef OPENSSL_NO_ENGINE
170 e = setup_engine(bio_err, engine, 0);
173 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
175 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
176 app_RAND_load_files(inrand));
178 out = BIO_new(BIO_s_file());
182 r = BIO_write_filename(out, outfile);
185 r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
187 #ifdef OPENSSL_SYS_VMS
189 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
190 out = BIO_push(tmpbio, out);
199 BIO *b64 = BIO_new(BIO_f_base64());
202 out = BIO_push(b64, out);
207 unsigned char buf[4096];
211 if (chunk > (int)sizeof(buf))
213 r = RAND_bytes(buf, chunk);
216 BIO_write(out, buf, chunk);
219 (void)BIO_flush(out);
221 app_RAND_write_file(NULL, bio_err);
225 ERR_print_errors(bio_err);