xref: /freebsd/crypto/heimdal/lib/krb5/crypto-rand.c (revision c6879c6c14eedbd060ba588a3129a6c60ebbe783)
1ae771770SStanislav Sedov /*
2ae771770SStanislav Sedov  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3ae771770SStanislav Sedov  * (Royal Institute of Technology, Stockholm, Sweden).
4ae771770SStanislav Sedov  * All rights reserved.
5ae771770SStanislav Sedov  *
6ae771770SStanislav Sedov  * Redistribution and use in source and binary forms, with or without
7ae771770SStanislav Sedov  * modification, are permitted provided that the following conditions
8ae771770SStanislav Sedov  * are met:
9ae771770SStanislav Sedov  *
10ae771770SStanislav Sedov  * 1. Redistributions of source code must retain the above copyright
11ae771770SStanislav Sedov  *    notice, this list of conditions and the following disclaimer.
12ae771770SStanislav Sedov  *
13ae771770SStanislav Sedov  * 2. Redistributions in binary form must reproduce the above copyright
14ae771770SStanislav Sedov  *    notice, this list of conditions and the following disclaimer in the
15ae771770SStanislav Sedov  *    documentation and/or other materials provided with the distribution.
16ae771770SStanislav Sedov  *
17ae771770SStanislav Sedov  * 3. Neither the name of the Institute nor the names of its contributors
18ae771770SStanislav Sedov  *    may be used to endorse or promote products derived from this software
19ae771770SStanislav Sedov  *    without specific prior written permission.
20ae771770SStanislav Sedov  *
21ae771770SStanislav Sedov  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22ae771770SStanislav Sedov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ae771770SStanislav Sedov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ae771770SStanislav Sedov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25ae771770SStanislav Sedov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ae771770SStanislav Sedov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ae771770SStanislav Sedov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ae771770SStanislav Sedov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ae771770SStanislav Sedov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ae771770SStanislav Sedov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ae771770SStanislav Sedov  * SUCH DAMAGE.
32ae771770SStanislav Sedov  */
33ae771770SStanislav Sedov 
34ae771770SStanislav Sedov #include "krb5_locl.h"
35ae771770SStanislav Sedov 
36ae771770SStanislav Sedov #define ENTROPY_NEEDED 128
37ae771770SStanislav Sedov 
38ae771770SStanislav Sedov static HEIMDAL_MUTEX crypto_mutex = HEIMDAL_MUTEX_INITIALIZER;
39ae771770SStanislav Sedov 
40ae771770SStanislav Sedov static int
seed_something(void)41ae771770SStanislav Sedov seed_something(void)
42ae771770SStanislav Sedov {
43ae771770SStanislav Sedov     char buf[1024], seedfile[256];
44ae771770SStanislav Sedov 
45ae771770SStanislav Sedov     /* If there is a seed file, load it. But such a file cannot be trusted,
46ae771770SStanislav Sedov        so use 0 for the entropy estimate */
47ae771770SStanislav Sedov     if (RAND_file_name(seedfile, sizeof(seedfile))) {
48ae771770SStanislav Sedov 	int fd;
49ae771770SStanislav Sedov 	fd = open(seedfile, O_RDONLY | O_BINARY | O_CLOEXEC);
50ae771770SStanislav Sedov 	if (fd >= 0) {
51ae771770SStanislav Sedov 	    ssize_t ret;
52ae771770SStanislav Sedov 	    rk_cloexec(fd);
53ae771770SStanislav Sedov 	    ret = read(fd, buf, sizeof(buf));
54ae771770SStanislav Sedov 	    if (ret > 0)
55ae771770SStanislav Sedov 		RAND_add(buf, ret, 0.0);
56ae771770SStanislav Sedov 	    close(fd);
57ae771770SStanislav Sedov 	} else
58ae771770SStanislav Sedov 	    seedfile[0] = '\0';
59ae771770SStanislav Sedov     } else
60ae771770SStanislav Sedov 	seedfile[0] = '\0';
61ae771770SStanislav Sedov 
62ae771770SStanislav Sedov     /* Calling RAND_status() will try to use /dev/urandom if it exists so
63ae771770SStanislav Sedov        we do not have to deal with it. */
64ae771770SStanislav Sedov     if (RAND_status() != 1) {
65ae771770SStanislav Sedov #ifndef _WIN32
66*e4456411SJohn Baldwin #ifndef OPENSSL_NO_EGD
67ae771770SStanislav Sedov 	krb5_context context;
68ae771770SStanislav Sedov 	const char *p;
69ae771770SStanislav Sedov 
70ae771770SStanislav Sedov 	/* Try using egd */
71ae771770SStanislav Sedov 	if (!krb5_init_context(&context)) {
72ae771770SStanislav Sedov 	    p = krb5_config_get_string(context, NULL, "libdefaults",
73ae771770SStanislav Sedov 				       "egd_socket", NULL);
74ae771770SStanislav Sedov 	    if (p != NULL)
75ae771770SStanislav Sedov 		RAND_egd_bytes(p, ENTROPY_NEEDED);
76ae771770SStanislav Sedov 	    krb5_free_context(context);
77ae771770SStanislav Sedov 	}
78*e4456411SJohn Baldwin #endif
79ae771770SStanislav Sedov #else
80ae771770SStanislav Sedov 	/* TODO: Once a Windows CryptoAPI RAND method is defined, we
81ae771770SStanislav Sedov 	   can use that and failover to another method. */
82ae771770SStanislav Sedov #endif
83ae771770SStanislav Sedov     }
84ae771770SStanislav Sedov 
85ae771770SStanislav Sedov     if (RAND_status() == 1)	{
86ae771770SStanislav Sedov 	/* Update the seed file */
87ae771770SStanislav Sedov 	if (seedfile[0])
88ae771770SStanislav Sedov 	    RAND_write_file(seedfile);
89ae771770SStanislav Sedov 
90ae771770SStanislav Sedov 	return 0;
91ae771770SStanislav Sedov     } else
92ae771770SStanislav Sedov 	return -1;
93ae771770SStanislav Sedov }
94ae771770SStanislav Sedov 
95ae771770SStanislav Sedov KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_generate_random_block(void * buf,size_t len)96ae771770SStanislav Sedov krb5_generate_random_block(void *buf, size_t len)
97ae771770SStanislav Sedov {
98ae771770SStanislav Sedov     static int rng_initialized = 0;
99ae771770SStanislav Sedov 
100ae771770SStanislav Sedov     HEIMDAL_MUTEX_lock(&crypto_mutex);
101ae771770SStanislav Sedov     if (!rng_initialized) {
102ae771770SStanislav Sedov 	if (seed_something())
103ae771770SStanislav Sedov 	    krb5_abortx(NULL, "Fatal: could not seed the "
104ae771770SStanislav Sedov 			"random number generator");
105ae771770SStanislav Sedov 
106ae771770SStanislav Sedov 	rng_initialized = 1;
107ae771770SStanislav Sedov     }
108ae771770SStanislav Sedov     HEIMDAL_MUTEX_unlock(&crypto_mutex);
109ae771770SStanislav Sedov     if (RAND_bytes(buf, len) <= 0)
110ae771770SStanislav Sedov 	krb5_abortx(NULL, "Failed to generate random block");
111ae771770SStanislav Sedov }
112