xref: /openbsd-src/usr.bin/openssl/rand.c (revision 440d141494672caa7e8d231fced157bd3b6b7448)
1*440d1414Stb /* $OpenBSD: rand.c,v 1.18 2023/07/23 11:39:29 tb Exp $ */
2dab3f910Sjsing /* ====================================================================
3dab3f910Sjsing  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
4dab3f910Sjsing  *
5dab3f910Sjsing  * Redistribution and use in source and binary forms, with or without
6dab3f910Sjsing  * modification, are permitted provided that the following conditions
7dab3f910Sjsing  * are met:
8dab3f910Sjsing  *
9dab3f910Sjsing  * 1. Redistributions of source code must retain the above copyright
10dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer.
11dab3f910Sjsing  *
12dab3f910Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
13dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer in
14dab3f910Sjsing  *    the documentation and/or other materials provided with the
15dab3f910Sjsing  *    distribution.
16dab3f910Sjsing  *
17dab3f910Sjsing  * 3. All advertising materials mentioning features or use of this
18dab3f910Sjsing  *    software must display the following acknowledgment:
19dab3f910Sjsing  *    "This product includes software developed by the OpenSSL Project
20dab3f910Sjsing  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21dab3f910Sjsing  *
22dab3f910Sjsing  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23dab3f910Sjsing  *    endorse or promote products derived from this software without
24dab3f910Sjsing  *    prior written permission. For written permission, please contact
25dab3f910Sjsing  *    openssl-core@openssl.org.
26dab3f910Sjsing  *
27dab3f910Sjsing  * 5. Products derived from this software may not be called "OpenSSL"
28dab3f910Sjsing  *    nor may "OpenSSL" appear in their names without prior written
29dab3f910Sjsing  *    permission of the OpenSSL Project.
30dab3f910Sjsing  *
31dab3f910Sjsing  * 6. Redistributions of any form whatsoever must retain the following
32dab3f910Sjsing  *    acknowledgment:
33dab3f910Sjsing  *    "This product includes software developed by the OpenSSL Project
34dab3f910Sjsing  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35dab3f910Sjsing  *
36dab3f910Sjsing  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37dab3f910Sjsing  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38dab3f910Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39dab3f910Sjsing  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40dab3f910Sjsing  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41dab3f910Sjsing  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42dab3f910Sjsing  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43dab3f910Sjsing  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44dab3f910Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45dab3f910Sjsing  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46dab3f910Sjsing  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47dab3f910Sjsing  * OF THE POSSIBILITY OF SUCH DAMAGE.
48dab3f910Sjsing  * ====================================================================
49dab3f910Sjsing  *
50dab3f910Sjsing  * This product includes cryptographic software written by Eric Young
51dab3f910Sjsing  * (eay@cryptsoft.com).  This product includes software written by Tim
52dab3f910Sjsing  * Hudson (tjh@cryptsoft.com).
53dab3f910Sjsing  *
54dab3f910Sjsing  */
55dab3f910Sjsing 
56dab3f910Sjsing #include <ctype.h>
57dab3f910Sjsing #include <stdio.h>
58dab3f910Sjsing #include <string.h>
59dab3f910Sjsing 
60dab3f910Sjsing #include "apps.h"
61dab3f910Sjsing 
62dab3f910Sjsing #include <openssl/bio.h>
63dab3f910Sjsing #include <openssl/err.h>
64dab3f910Sjsing 
65efc7e65fStb static struct {
666b8b4364Sjsing 	int base64;
676b8b4364Sjsing 	int hex;
686b8b4364Sjsing 	char *outfile;
69e7718adaStb } cfg;
706b8b4364Sjsing 
71ea149709Sguenther static const struct option rand_options[] = {
726b8b4364Sjsing 	{
736b8b4364Sjsing 		.name = "base64",
746b8b4364Sjsing 		.desc = "Perform base64 encoding on output",
756b8b4364Sjsing 		.type = OPTION_FLAG,
76e7718adaStb 		.opt.flag = &cfg.base64,
776b8b4364Sjsing 	},
786b8b4364Sjsing 	{
796b8b4364Sjsing 		.name = "hex",
806b8b4364Sjsing 		.desc = "Hexadecimal output",
816b8b4364Sjsing 		.type = OPTION_FLAG,
82e7718adaStb 		.opt.flag = &cfg.hex,
836b8b4364Sjsing 	},
846b8b4364Sjsing 	{
856b8b4364Sjsing 		.name = "out",
866b8b4364Sjsing 		.argname = "file",
876b8b4364Sjsing 		.desc = "Write to the given file instead of standard output",
886b8b4364Sjsing 		.type = OPTION_ARG,
89e7718adaStb 		.opt.arg = &cfg.outfile,
906b8b4364Sjsing 	},
91d220e929Sbcook 	{NULL},
926b8b4364Sjsing };
936b8b4364Sjsing 
946b8b4364Sjsing static void
rand_usage(void)95*440d1414Stb rand_usage(void)
966b8b4364Sjsing {
976b8b4364Sjsing 	fprintf(stderr,
985284dfeaSbcook 	    "usage: rand [-base64 | -hex] [-out file] num\n");
996b8b4364Sjsing 	options_usage(rand_options);
1006b8b4364Sjsing }
101dab3f910Sjsing 
102dab3f910Sjsing int
rand_main(int argc,char ** argv)103dab3f910Sjsing rand_main(int argc, char **argv)
104dab3f910Sjsing {
1056b8b4364Sjsing 	char *num_bytes = NULL;
1066b8b4364Sjsing 	int ret = 1;
1076b8b4364Sjsing 	int badopt = 0;
108dab3f910Sjsing 	int num = -1;
1096b8b4364Sjsing 	int i, r;
1106b8b4364Sjsing 	BIO *out = NULL;
111dab3f910Sjsing 
11251811eadSderaadt 	if (pledge("stdio cpath wpath rpath", NULL) == -1) {
1139bc487adSdoug 		perror("pledge");
114e370f0eeSdoug 		exit(1);
115e370f0eeSdoug 	}
1169bc487adSdoug 
117e7718adaStb 	memset(&cfg, 0, sizeof(cfg));
11870f52a02Sjsing 
119beba0da1Sjsing 	if (options_parse(argc, argv, rand_options, &num_bytes, NULL) != 0) {
1206b8b4364Sjsing 		rand_usage();
1216b8b4364Sjsing 		return (1);
122dab3f910Sjsing 	}
1236b8b4364Sjsing 
1246b8b4364Sjsing 	if (num_bytes != NULL) {
1256b8b4364Sjsing 		r = sscanf(num_bytes, "%d", &num);
126dab3f910Sjsing 		if (r == 0 || num < 0)
127dab3f910Sjsing 			badopt = 1;
128dab3f910Sjsing 	} else
129dab3f910Sjsing 		badopt = 1;
130dab3f910Sjsing 
131e7718adaStb 	if (cfg.hex && cfg.base64)
132dab3f910Sjsing 		badopt = 1;
133dab3f910Sjsing 
134dab3f910Sjsing 	if (badopt) {
1356b8b4364Sjsing 		rand_usage();
136dab3f910Sjsing 		goto err;
137dab3f910Sjsing 	}
1386b8b4364Sjsing 
139dab3f910Sjsing 	out = BIO_new(BIO_s_file());
140dab3f910Sjsing 	if (out == NULL)
141dab3f910Sjsing 		goto err;
142e7718adaStb 	if (cfg.outfile != NULL)
143e7718adaStb 		r = BIO_write_filename(out, cfg.outfile);
1446b8b4364Sjsing 	else
145dab3f910Sjsing 		r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
146dab3f910Sjsing 	if (r <= 0)
147dab3f910Sjsing 		goto err;
148e7718adaStb 	if (cfg.base64) {
149dab3f910Sjsing 		BIO *b64 = BIO_new(BIO_f_base64());
150dab3f910Sjsing 		if (b64 == NULL)
151dab3f910Sjsing 			goto err;
152dab3f910Sjsing 		out = BIO_push(b64, out);
153dab3f910Sjsing 	}
1546b8b4364Sjsing 
155dab3f910Sjsing 	while (num > 0) {
156dab3f910Sjsing 		unsigned char buf[4096];
157dab3f910Sjsing 		int chunk;
158dab3f910Sjsing 
159dab3f910Sjsing 		chunk = num;
160dab3f910Sjsing 		if (chunk > (int) sizeof(buf))
1616b8b4364Sjsing 			chunk = sizeof(buf);
162fd6ab616Sjsing 		arc4random_buf(buf, chunk);
163e7718adaStb 		if (cfg.hex) {
164dab3f910Sjsing 			for (i = 0; i < chunk; i++)
165dab3f910Sjsing 				BIO_printf(out, "%02x", buf[i]);
1666b8b4364Sjsing 		} else
1676b8b4364Sjsing 			BIO_write(out, buf, chunk);
168dab3f910Sjsing 		num -= chunk;
169dab3f910Sjsing 	}
1706b8b4364Sjsing 
171e7718adaStb 	if (cfg.hex)
172dab3f910Sjsing 		BIO_puts(out, "\n");
173dab3f910Sjsing 	(void) BIO_flush(out);
174dab3f910Sjsing 
175dab3f910Sjsing 	ret = 0;
176dab3f910Sjsing 
177dab3f910Sjsing  err:
178dab3f910Sjsing 	ERR_print_errors(bio_err);
179dab3f910Sjsing 	BIO_free_all(out);
180dab3f910Sjsing 
181dab3f910Sjsing 	return (ret);
182dab3f910Sjsing }
183