10adf0e95d81e3f3810987b9ee169afb145e5769
[openssl.git] / apps / rand.c
1 /* apps/rand.c */
2
3 #include "apps.h"
4
5 #include <ctype.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <openssl/bio.h>
10 #include <openssl/err.h>
11 #include <openssl/rand.h>
12 #include <openssl/engine.h>
13
14 #undef PROG
15 #define PROG rand_main
16
17 /* -out file         - write to file
18  * -rand file:file   - PRNG seed files
19  * -base64           - encode output
20  * num               - write 'num' bytes
21  */
22
23 int MAIN(int, char **);
24
25 int MAIN(int argc, char **argv)
26         {
27         ENGINE *e = NULL;
28         int i, r, ret = 1;
29         int badopt;
30         char *outfile = NULL;
31         char *inrand = NULL;
32         int base64 = 0;
33         BIO *out = NULL;
34         int num = -1;
35         char *engine=NULL;
36
37         apps_startup();
38
39         if (bio_err == NULL)
40                 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
41                         BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
42
43         badopt = 0;
44         i = 0;
45         while (!badopt && argv[++i] != NULL)
46                 {
47                 if (strcmp(argv[i], "-out") == 0)
48                         {
49                         if ((argv[i+1] != NULL) && (outfile == NULL))
50                                 outfile = argv[++i];
51                         else
52                                 badopt = 1;
53                         }
54                 else if (strcmp(argv[i], "-engine") == 0)
55                         {
56                         if ((argv[i+1] != NULL) && (engine == NULL))
57                                 engine = argv[++i];
58                         else
59                                 badopt = 1;
60                         }
61                 else if (strcmp(argv[i], "-rand") == 0)
62                         {
63                         if ((argv[i+1] != NULL) && (inrand == NULL))
64                                 inrand = argv[++i];
65                         else
66                                 badopt = 1;
67                         }
68                 else if (strcmp(argv[i], "-base64") == 0)
69                         {
70                         if (!base64)
71                                 base64 = 1;
72                         else
73                                 badopt = 1;
74                         }
75                 else if (isdigit((unsigned char)argv[i][0]))
76                         {
77                         if (num < 0)
78                                 {
79                                 r = sscanf(argv[i], "%d", &num);
80                                 if (r == 0 || num < 0)
81                                         badopt = 1;
82                                 }
83                         else
84                                 badopt = 1;
85                         }
86                 else
87                         badopt = 1;
88                 }
89
90         if (num < 0)
91                 badopt = 1;
92         
93         if (badopt) 
94                 {
95                 BIO_printf(bio_err, "Usage: rand [options] num\n");
96                 BIO_printf(bio_err, "where options are\n");
97                 BIO_printf(bio_err, "-out file             - write to file\n");
98                 BIO_printf(bio_err, "-engine e             - use engine e, possibly a hardware device.\n");
99                 BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
100                 BIO_printf(bio_err, "-base64               - encode output\n");
101                 goto err;
102                 }
103
104         e = setup_engine(bio_err, engine, 0);
105
106         app_RAND_load_file(NULL, bio_err, (inrand != NULL));
107         if (inrand != NULL)
108                 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
109                         app_RAND_load_files(inrand));
110
111         out = BIO_new(BIO_s_file());
112         if (out == NULL)
113                 goto err;
114         if (outfile != NULL)
115                 r = BIO_write_filename(out, outfile);
116         else
117                 {
118                 r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
119 #ifdef OPENSSL_SYS_VMS
120                 {
121                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
122                 out = BIO_push(tmpbio, out);
123                 }
124 #endif
125                 }
126         if (r <= 0)
127                 goto err;
128
129         if (base64)
130                 {
131                 BIO *b64 = BIO_new(BIO_f_base64());
132                 if (b64 == NULL)
133                         goto err;
134                 out = BIO_push(b64, out);
135                 }
136         
137         while (num > 0) 
138                 {
139                 unsigned char buf[4096];
140                 int chunk;
141
142                 chunk = num;
143                 if (chunk > sizeof buf)
144                         chunk = sizeof buf;
145                 r = RAND_bytes(buf, chunk);
146                 if (r <= 0)
147                         goto err;
148                 BIO_write(out, buf, chunk);
149                 num -= chunk;
150                 }
151         BIO_flush(out);
152
153         app_RAND_write_file(NULL, bio_err);
154         ret = 0;
155         
156 err:
157         ERR_print_errors(bio_err);
158         if (out)
159                 BIO_free_all(out);
160         EXIT(ret);
161         }