Document my latest changes.
[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
13 #undef PROG
14 #define PROG rand_main
15
16 /* -out file         - write to file
17  * -rand file:file   - PRNG seed files
18  * -egd file         - PRNG seed from EGD named socket
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         int i, r, ret = 1;
28         int badopt;
29         char *outfile = NULL;
30         char *inrand = NULL,*inegd=NULL;
31         int base64 = 0;
32         BIO *out = NULL;
33         int num = -1;
34
35         apps_startup();
36
37         if (bio_err == NULL)
38                 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
39                         BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
40
41         badopt = 0;
42         i = 0;
43         while (!badopt && argv[++i] != NULL)
44                 {
45                 if (strcmp(argv[i], "-out") == 0)
46                         {
47                         if ((argv[i+1] != NULL) && (outfile == NULL))
48                                 outfile = argv[++i];
49                         else
50                                 badopt = 1;
51                         }
52                 else if (strcmp(argv[i], "-rand") == 0)
53                         {
54                         if ((argv[i+1] != NULL) && (inrand == NULL))
55                                 inrand = argv[++i];
56                         else
57                                 badopt = 1;
58                         }
59                 else if (strcmp(argv[i], "-egd") == 0)
60                         {
61                         if ((argv[i+1] != NULL) && (inegd == NULL))
62                                 inegd = argv[++i];
63                         else
64                                 badopt = 1;
65                         }
66                 else if (strcmp(argv[i], "-base64") == 0)
67                         {
68                         if (!base64)
69                                 base64 = 1;
70                         else
71                                 badopt = 1;
72                         }
73                 else if (isdigit((unsigned char)argv[i][0]))
74                         {
75                         if (num < 0)
76                                 {
77                                 r = sscanf(argv[i], "%d", &num);
78                                 if (r == 0 || num < 0)
79                                         badopt = 1;
80                                 }
81                         else
82                                 badopt = 1;
83                         }
84                 else
85                         badopt = 1;
86                 }
87
88         if (num < 0)
89                 badopt = 1;
90         
91         if (badopt) 
92                 {
93                 BIO_printf(bio_err, "Usage: rand [options] num\n");
94                 BIO_printf(bio_err, "where options are\n");
95                 BIO_printf(bio_err, "-out file            - write to file\n");
96                 BIO_printf(bio_err, "-rand file%cfile%c...  - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
97                 BIO_printf(bio_err, "-egd file            - seed PRNG from EGD named socket\n");
98                 BIO_printf(bio_err, "-base64              - encode output\n");
99                 goto err;
100                 }
101
102         app_RAND_load_file(NULL, bio_err, (inrand != NULL || inegd != NULL));
103         if (inrand != NULL)
104                 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
105                         app_RAND_load_files(inrand));
106         if (inegd != NULL)
107                 BIO_printf(bio_err,"%ld egd bytes loaded\n",
108                         RAND_egd(inegd));
109
110         out = BIO_new(BIO_s_file());
111         if (out == NULL)
112                 goto err;
113         if (outfile != NULL)
114                 r = BIO_write_filename(out, outfile);
115         else
116                 r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
117         if (r <= 0)
118                 goto err;
119
120         if (base64)
121                 {
122                 BIO *b64 = BIO_new(BIO_f_base64());
123                 if (b64 == NULL)
124                         goto err;
125                 out = BIO_push(b64, out);
126                 }
127         
128         while (num > 0) 
129                 {
130                 unsigned char buf[4096];
131                 int chunk;
132
133                 chunk = num;
134                 if (chunk > sizeof buf)
135                         chunk = sizeof buf;
136                 r = RAND_bytes(buf, chunk);
137                 if (r <= 0)
138                         goto err;
139                 BIO_write(out, buf, chunk);
140                 num -= chunk;
141                 }
142         BIO_flush(out);
143
144         app_RAND_write_file(NULL, bio_err);
145         ret = 0;
146         
147 err:
148         ERR_print_errors(bio_err);
149         if (out)
150                 BIO_free_all(out);
151         EXIT(ret);
152         }