diff options
Diffstat (limited to 'src/lib/libc')
| -rw-r--r-- | src/lib/libc/crypt/arc4random.c | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c index ef2332cc45..57acc0d56d 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.27 2014/05/04 20:40:08 deraadt Exp $ */ | 1 | /* $OpenBSD: arc4random.c,v 1.28 2014/05/06 02:31:45 tedu Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| @@ -31,6 +31,8 @@ | |||
| 31 | #include <sys/param.h> | 31 | #include <sys/param.h> |
| 32 | #include <sys/time.h> | 32 | #include <sys/time.h> |
| 33 | #include <sys/sysctl.h> | 33 | #include <sys/sysctl.h> |
| 34 | #include <sys/mman.h> | ||
| 35 | |||
| 34 | #include "thread_private.h" | 36 | #include "thread_private.h" |
| 35 | 37 | ||
| 36 | #define KEYSTREAM_ONLY | 38 | #define KEYSTREAM_ONLY |
| @@ -48,8 +50,8 @@ | |||
| 48 | #define RSBUFSZ (16*BLOCKSZ) | 50 | #define RSBUFSZ (16*BLOCKSZ) |
| 49 | static int rs_initialized; | 51 | static int rs_initialized; |
| 50 | static pid_t rs_stir_pid; | 52 | static pid_t rs_stir_pid; |
| 51 | static chacha_ctx rs; /* chacha context for random keystream */ | 53 | static chacha_ctx *rs; /* chacha context for random keystream */ |
| 52 | static u_char rs_buf[RSBUFSZ]; /* keystream blocks */ | 54 | static u_char *rs_buf; /* keystream blocks */ |
| 53 | static size_t rs_have; /* valid bytes at end of rs_buf */ | 55 | static size_t rs_have; /* valid bytes at end of rs_buf */ |
| 54 | static size_t rs_count; /* bytes till reseed */ | 56 | static size_t rs_count; /* bytes till reseed */ |
| 55 | 57 | ||
| @@ -60,8 +62,16 @@ _rs_init(u_char *buf, size_t n) | |||
| 60 | { | 62 | { |
| 61 | if (n < KEYSZ + IVSZ) | 63 | if (n < KEYSZ + IVSZ) |
| 62 | return; | 64 | return; |
| 63 | chacha_keysetup(&rs, buf, KEYSZ * 8, 0); | 65 | |
| 64 | chacha_ivsetup(&rs, buf + KEYSZ); | 66 | if ((rs = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE, |
| 67 | MAP_ANON, -1, 0)) == MAP_FAILED) | ||
| 68 | abort(); | ||
| 69 | if ((rs_buf = mmap(NULL, RSBUFSZ, PROT_READ|PROT_WRITE, | ||
| 70 | MAP_ANON, -1, 0)) == MAP_FAILED) | ||
| 71 | abort(); | ||
| 72 | |||
| 73 | chacha_keysetup(rs, buf, KEYSZ * 8, 0); | ||
| 74 | chacha_ivsetup(rs, buf + KEYSZ); | ||
| 65 | } | 75 | } |
| 66 | 76 | ||
| 67 | static void | 77 | static void |
| @@ -110,7 +120,7 @@ _rs_rekey(u_char *dat, size_t datlen) | |||
| 110 | memset(rs_buf, 0,RSBUFSZ); | 120 | memset(rs_buf, 0,RSBUFSZ); |
| 111 | #endif | 121 | #endif |
| 112 | /* fill rs_buf with the keystream */ | 122 | /* fill rs_buf with the keystream */ |
| 113 | chacha_encrypt_bytes(&rs, rs_buf, rs_buf, RSBUFSZ); | 123 | chacha_encrypt_bytes(rs, rs_buf, rs_buf, RSBUFSZ); |
| 114 | /* mix in optional user provided data */ | 124 | /* mix in optional user provided data */ |
| 115 | if (dat) { | 125 | if (dat) { |
| 116 | size_t i, m; | 126 | size_t i, m; |
