diff options
author | bcook <> | 2015-01-19 20:21:40 +0000 |
---|---|---|
committer | bcook <> | 2015-01-19 20:21:40 +0000 |
commit | 1dd653753c9c694999d9de6c6468beeb2e0793f5 (patch) | |
tree | 184795d028e938e83e5050568117bc0542549b3f /src/lib/libcrypto/crypto/getentropy_netbsd.c | |
parent | f1a5194b172185541e9828a950d827914e7679bd (diff) | |
download | openbsd-1dd653753c9c694999d9de6c6468beeb2e0793f5.tar.gz openbsd-1dd653753c9c694999d9de6c6468beeb2e0793f5.tar.bz2 openbsd-1dd653753c9c694999d9de6c6468beeb2e0793f5.zip |
Add arc4random/getentropy shims for NetBSD.
The latest NetBSD (6.1.5) arc4random does not appear to reseed the CRNG state
after a fork, so provide an override until the fork-safe version in CVS appears
in a release.
These are the same as the FreeBSD shims.
ok deraadt@
Diffstat (limited to 'src/lib/libcrypto/crypto/getentropy_netbsd.c')
-rw-r--r-- | src/lib/libcrypto/crypto/getentropy_netbsd.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/lib/libcrypto/crypto/getentropy_netbsd.c b/src/lib/libcrypto/crypto/getentropy_netbsd.c new file mode 100644 index 0000000000..a9710ef923 --- /dev/null +++ b/src/lib/libcrypto/crypto/getentropy_netbsd.c | |||
@@ -0,0 +1,64 @@ | |||
1 | /* $OpenBSD: getentropy_netbsd.c,v 1.1 2015/01/19 20:21:40 bcook Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2014 Pawel Jakub Dawidek <pjd@FreeBSD.org> | ||
5 | * Copyright (c) 2014 Brent Cook <bcook@openbsd.org> | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose with or without fee is hereby granted, provided that the above | ||
9 | * copyright notice and this permission notice appear in all copies. | ||
10 | * | ||
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
18 | * | ||
19 | * Emulation of getentropy(2) as documented at: | ||
20 | * http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2 | ||
21 | */ | ||
22 | |||
23 | #include <sys/types.h> | ||
24 | #include <sys/sysctl.h> | ||
25 | |||
26 | #include <errno.h> | ||
27 | #include <stddef.h> | ||
28 | |||
29 | /* | ||
30 | * Derived from lib/libc/gen/arc4random.c from FreeBSD. | ||
31 | */ | ||
32 | static size_t | ||
33 | getentropy_sysctl(u_char *buf, size_t size) | ||
34 | { | ||
35 | int mib[2]; | ||
36 | size_t len, done; | ||
37 | |||
38 | mib[0] = CTL_KERN; | ||
39 | mib[1] = KERN_ARND; | ||
40 | done = 0; | ||
41 | |||
42 | do { | ||
43 | len = size; | ||
44 | if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) | ||
45 | return (done); | ||
46 | done += len; | ||
47 | buf += len; | ||
48 | size -= len; | ||
49 | } while (size > 0); | ||
50 | |||
51 | return (done); | ||
52 | } | ||
53 | |||
54 | int | ||
55 | getentropy(void *buf, size_t len) | ||
56 | { | ||
57 | if (len <= 256 && | ||
58 | getentropy_sysctl(buf, len) == len) { | ||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | errno = EIO; | ||
63 | return -1; | ||
64 | } | ||