summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authortedu <>2014-05-06 02:31:45 +0000
committertedu <>2014-05-06 02:31:45 +0000
commit0539604f5771dae2c3ecffa8122b5651ff283719 (patch)
tree00fe2f2398b0d5a19084f02172033e617b13d5c0 /src/lib
parentbdd989002c6541c9e73b1e520122ed1495e5f509 (diff)
downloadopenbsd-0539604f5771dae2c3ecffa8122b5651ff283719.tar.gz
openbsd-0539604f5771dae2c3ecffa8122b5651ff283719.tar.bz2
openbsd-0539604f5771dae2c3ecffa8122b5651ff283719.zip
move chacha context and buffer out of bss and allow mmap to place them
wherever it decides it would like them. first step. ok deraadt dlg djm
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/crypt/arc4random.c22
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)
49static int rs_initialized; 51static int rs_initialized;
50static pid_t rs_stir_pid; 52static pid_t rs_stir_pid;
51static chacha_ctx rs; /* chacha context for random keystream */ 53static chacha_ctx *rs; /* chacha context for random keystream */
52static u_char rs_buf[RSBUFSZ]; /* keystream blocks */ 54static u_char *rs_buf; /* keystream blocks */
53static size_t rs_have; /* valid bytes at end of rs_buf */ 55static size_t rs_have; /* valid bytes at end of rs_buf */
54static size_t rs_count; /* bytes till reseed */ 56static 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
67static void 77static 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;