Merge pull request #88 from jelmer/rm-krb4-references
[heimdal.git] / lib / krb5 / crypto-rand.c
blob9ae550b06a23e53836d28b35c87ad612553f7488
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 #undef HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE
37 #define HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE
39 #define ENTROPY_NEEDED 128
41 static HEIMDAL_MUTEX crypto_mutex = HEIMDAL_MUTEX_INITIALIZER;
43 static int
44 seed_something(void)
46 #ifndef NO_RANDFILE
47 char buf[1024], seedfile[256];
49 /* If there is a seed file, load it. But such a file cannot be trusted,
50 so use 0 for the entropy estimate */
51 if (RAND_file_name(seedfile, sizeof(seedfile))) {
52 int fd;
53 fd = open(seedfile, O_RDONLY | O_BINARY | O_CLOEXEC);
54 if (fd >= 0) {
55 ssize_t ret;
56 rk_cloexec(fd);
57 ret = read(fd, buf, sizeof(buf));
58 if (ret > 0)
59 RAND_add(buf, ret, 0.0);
60 close(fd);
61 } else
62 seedfile[0] = '\0';
63 } else
64 seedfile[0] = '\0';
65 #endif
67 /* Calling RAND_status() will try to use /dev/urandom if it exists so
68 we do not have to deal with it. */
69 if (RAND_status() != 1) {
70 #ifndef NO_RAND_EGD_METHOD
71 krb5_context context;
72 const char *p;
74 /* Try using egd */
75 if (!krb5_init_context(&context)) {
76 p = krb5_config_get_string(context, NULL, "libdefaults",
77 "egd_socket", NULL);
78 if (p != NULL)
79 RAND_egd_bytes(p, ENTROPY_NEEDED);
80 krb5_free_context(context);
82 #else
83 /* TODO: Once a Windows CryptoAPI RAND method is defined, we
84 can use that and failover to another method. */
85 #endif
88 if (RAND_status() == 1) {
89 #ifndef NO_RANDFILE
90 /* Update the seed file */
91 if (seedfile[0])
92 RAND_write_file(seedfile);
93 #endif
95 return 0;
96 } else
97 return -1;
101 * Fill buffer buf with len bytes of PRNG randomness that is ok to use
102 * for key generation, padding and public diclosing the randomness w/o
103 * disclosing the randomness source.
105 * This function can fail, and callers must check the return value.
107 * @param buf a buffer to fill with randomness
108 * @param len length of memory that buf points to.
110 * @return return 0 on success or HEIM_ERR_RANDOM_OFFLINE if the
111 * funcation failed to initialize the randomness source.
113 * @ingroup krb5_crypto
116 HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE
117 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
118 krb5_generate_random(void *buf, size_t len)
120 static int rng_initialized = 0;
121 int ret;
123 HEIMDAL_MUTEX_lock(&crypto_mutex);
124 if (!rng_initialized) {
125 if (seed_something())
126 return HEIM_ERR_RANDOM_OFFLINE;
127 rng_initialized = 1;
129 if (RAND_bytes(buf, len) <= 0)
130 ret = HEIM_ERR_RANDOM_OFFLINE;
131 else
132 ret = 0;
133 HEIMDAL_MUTEX_unlock(&crypto_mutex);
135 return ret;
139 * Fill buffer buf with len bytes of PRNG randomness that is ok to use
140 * for key generation, padding and public diclosing the randomness w/o
141 * disclosing the randomness source.
143 * This function can NOT fail, instead it will abort() and program will crash.
145 * If this function is called after a successful krb5_init_context(),
146 * the chance of it failing is low due to that krb5_init_context()
147 * pulls out some random, and quite commonly the randomness sources
148 * will not fail once it have started to produce good output,
149 * /dev/urandom behavies that way.
151 * @param buf a buffer to fill with randomness
152 * @param len length of memory that buf points to.
154 * @ingroup krb5_crypto
158 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
159 krb5_generate_random_block(void *buf, size_t len)
161 int ret = krb5_generate_random(buf, len);
162 if (ret)
163 krb5_abortx(NULL, "Failed to generate random block");