diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/crypt/arc4random.c | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c index fd8570565e..13b94ed111 100644 --- a/src/lib/libc/crypt/arc4random.c +++ b/src/lib/libc/crypt/arc4random.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: arc4random.c,v 1.39 2014/06/27 21:31:12 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random.c,v 1.40 2014/07/09 16:52:09 bcook Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| @@ -25,13 +25,13 @@ | |||
| 25 | #include <fcntl.h> | 25 | #include <fcntl.h> |
| 26 | #include <limits.h> | 26 | #include <limits.h> |
| 27 | #include <signal.h> | 27 | #include <signal.h> |
| 28 | #include <stdint.h> | ||
| 28 | #include <stdlib.h> | 29 | #include <stdlib.h> |
| 29 | #include <string.h> | 30 | #include <string.h> |
| 30 | #include <unistd.h> | 31 | #include <unistd.h> |
| 31 | #include <sys/types.h> | 32 | #include <sys/types.h> |
| 32 | #include <sys/param.h> | 33 | #include <sys/param.h> |
| 33 | #include <sys/time.h> | 34 | #include <sys/time.h> |
| 34 | #include <sys/sysctl.h> | ||
| 35 | #include <sys/mman.h> | 35 | #include <sys/mman.h> |
| 36 | 36 | ||
| 37 | #include "thread_private.h" | 37 | #include "thread_private.h" |
| @@ -39,6 +39,7 @@ | |||
| 39 | #define KEYSTREAM_ONLY | 39 | #define KEYSTREAM_ONLY |
| 40 | #include "chacha_private.h" | 40 | #include "chacha_private.h" |
| 41 | 41 | ||
| 42 | #define min(a, b) ((a) < (b) ? (a) : (b)) | ||
| 42 | #ifdef __GNUC__ | 43 | #ifdef __GNUC__ |
| 43 | #define inline __inline | 44 | #define inline __inline |
| 44 | #else /* !__GNUC__ */ | 45 | #else /* !__GNUC__ */ |
| @@ -145,7 +146,7 @@ _rs_rekey(u_char *dat, size_t datlen) | |||
| 145 | if (dat) { | 146 | if (dat) { |
| 146 | size_t i, m; | 147 | size_t i, m; |
| 147 | 148 | ||
| 148 | m = MIN(datlen, KEYSZ + IVSZ); | 149 | m = min(datlen, KEYSZ + IVSZ); |
| 149 | for (i = 0; i < m; i++) | 150 | for (i = 0; i < m; i++) |
| 150 | rsx->rs_buf[i] ^= dat[i]; | 151 | rsx->rs_buf[i] ^= dat[i]; |
| 151 | } | 152 | } |
| @@ -165,7 +166,7 @@ _rs_random_buf(void *_buf, size_t n) | |||
| 165 | _rs_stir_if_needed(n); | 166 | _rs_stir_if_needed(n); |
| 166 | while (n > 0) { | 167 | while (n > 0) { |
| 167 | if (rs->rs_have > 0) { | 168 | if (rs->rs_have > 0) { |
| 168 | m = MIN(n, rs->rs_have); | 169 | m = min(n, rs->rs_have); |
| 169 | keystream = rsx->rs_buf + sizeof(rsx->rs_buf) | 170 | keystream = rsx->rs_buf + sizeof(rsx->rs_buf) |
| 170 | - rs->rs_have; | 171 | - rs->rs_have; |
| 171 | memcpy(buf, keystream, m); | 172 | memcpy(buf, keystream, m); |
| @@ -180,7 +181,7 @@ _rs_random_buf(void *_buf, size_t n) | |||
| 180 | } | 181 | } |
| 181 | 182 | ||
| 182 | static inline void | 183 | static inline void |
| 183 | _rs_random_u32(u_int32_t *val) | 184 | _rs_random_u32(uint32_t *val) |
| 184 | { | 185 | { |
| 185 | u_char *keystream; | 186 | u_char *keystream; |
| 186 | _rs_stir_if_needed(sizeof(*val)); | 187 | _rs_stir_if_needed(sizeof(*val)); |
| @@ -192,10 +193,10 @@ _rs_random_u32(u_int32_t *val) | |||
| 192 | rs->rs_have -= sizeof(*val); | 193 | rs->rs_have -= sizeof(*val); |
| 193 | } | 194 | } |
| 194 | 195 | ||
| 195 | u_int32_t | 196 | uint32_t |
| 196 | arc4random(void) | 197 | arc4random(void) |
| 197 | { | 198 | { |
| 198 | u_int32_t val; | 199 | uint32_t val; |
| 199 | 200 | ||
| 200 | _ARC4_LOCK(); | 201 | _ARC4_LOCK(); |
| 201 | _rs_random_u32(&val); | 202 | _rs_random_u32(&val); |
| @@ -221,10 +222,10 @@ arc4random_buf(void *buf, size_t n) | |||
| 221 | * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound) | 222 | * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound) |
| 222 | * after reduction modulo upper_bound. | 223 | * after reduction modulo upper_bound. |
| 223 | */ | 224 | */ |
| 224 | u_int32_t | 225 | uint32_t |
| 225 | arc4random_uniform(u_int32_t upper_bound) | 226 | arc4random_uniform(uint32_t upper_bound) |
| 226 | { | 227 | { |
| 227 | u_int32_t r, min; | 228 | uint32_t r, min; |
| 228 | 229 | ||
| 229 | if (upper_bound < 2) | 230 | if (upper_bound < 2) |
| 230 | return 0; | 231 | return 0; |
