1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * Copyright (C) 2001, 2002, 2004, 2007, 2008, 2010 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may require
8 * a specific license from the United States Government. It is the
9 * responsibility of any person or organization contemplating export to
10 * obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
27 #include "crypto_int.h"
29 krb5_error_code KRB5_CALLCONV
30 krb5_c_random_seed(krb5_context context, krb5_data *data)
32 return krb5_c_random_add_entropy(context, KRB5_C_RANDSOURCE_OLDAPI, data);
35 /* Routines to get entropy from the OS. */
39 k5_get_os_entropy(unsigned char *buf, size_t len, int strong)
44 /* CryptGenRandom is always considered strong. */
46 if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
49 result = CryptGenRandom(provider, len, buf);
50 (void)CryptReleaseContext(provider, 0);
54 #else /* not Windows */
58 #ifdef HAVE_SYS_STAT_H
62 #include <sys/syscall.h>
63 #endif /* __linux__ */
65 /* Open device, ensure that it is not a regular file, and read entropy. Return
66 * true on success, false on failure. */
68 read_entropy_from_device(const char *device, unsigned char *buf, size_t len)
75 krb5_boolean result = FALSE;
77 fd = open(device, O_RDONLY);
81 if (fstat(fd, &sb) == -1 || S_ISREG(sb.st_mode))
84 for (bp = buf, left = len; left > 0;) {
85 count = read(fd, bp, left);
99 k5_get_os_entropy(unsigned char *buf, size_t len, int strong)
102 #if defined(__linux__) && defined(SYS_getrandom)
107 * Pull from the /dev/urandom pool, but require it to have been seeded.
108 * This ensures strong randomness while only blocking during first
111 * glibc does not currently provide a binding for getrandom:
112 * https://sourceware.org/bugzilla/show_bug.cgi?id=17252
115 r = syscall(SYS_getrandom, buf, len, 0);
120 /* ENOSYS or other unrecoverable failure */
128 #endif /* defined(__linux__) && defined(SYS_getrandom) */
130 device = strong ? "/dev/random" : "/dev/urandom";
131 return read_entropy_from_device(device, buf, len);
134 #endif /* not Windows */