Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src make the random hypercall more generic
details: https://anonhg.NetBSD.org/src/rev/58bde69dd58a
branches: trunk
changeset: 786370:58bde69dd58a
user: pooka <pooka%NetBSD.org@localhost>
date: Sat Apr 27 16:56:29 2013 +0000
description:
make the random hypercall more generic
diffstat:
lib/librumpuser/rumpuser.c | 25 ++++++++++++++++++-------
lib/librumpuser/rumpuser_port.h | 8 +++++---
lib/librumpuser/rumpuser_sp.c | 9 ++++-----
sys/rump/include/rump/rumpuser.h | 7 +++++--
sys/rump/librump/rumpkern/cprng_stub.c | 28 ++++++++--------------------
5 files changed, 40 insertions(+), 37 deletions(-)
diffs (180 lines):
diff -r 22924a8f1a7f -r 58bde69dd58a lib/librumpuser/rumpuser.c
--- a/lib/librumpuser/rumpuser.c Sat Apr 27 16:43:13 2013 +0000
+++ b/lib/librumpuser/rumpuser.c Sat Apr 27 16:56:29 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: rumpuser.c,v 1.32 2013/04/27 15:34:53 pooka Exp $ */
+/* $NetBSD: rumpuser.c,v 1.33 2013/04/27 16:56:29 pooka Exp $ */
/*
* Copyright (c) 2007-2010 Antti Kantee. All Rights Reserved.
@@ -28,7 +28,7 @@
#include "rumpuser_port.h"
#if !defined(lint)
-__RCSID("$NetBSD: rumpuser.c,v 1.32 2013/04/27 15:34:53 pooka Exp $");
+__RCSID("$NetBSD: rumpuser.c,v 1.33 2013/04/27 16:56:29 pooka Exp $");
#endif /* !lint */
#include <sys/ioctl.h>
@@ -79,7 +79,7 @@
return 1;
}
-#ifdef RUMPUSER_USE_RANDOM
+#ifdef RUMPUSER_USE_DEVRANDOM
uint32_t rv;
int fd;
@@ -808,10 +808,21 @@
return ncpu;
}
-/* XXX: this hypercall needs a better name */
-uint32_t
-rumpuser_arc4random(void)
+size_t
+rumpuser_getrandom(void *buf, size_t buflen, int flags)
{
+ size_t origlen = buflen;
+ uint32_t *p = buf;
+ uint32_t tmp;
+ int chunk;
- return arc4random();
+ do {
+ chunk = buflen < 4 ? buflen : 4; /* portable MIN ... */
+ tmp = RUMPUSER_RANDOM();
+ memcpy(p, &tmp, chunk);
+ p++;
+ buflen -= chunk;
+ } while (chunk);
+
+ return origlen;
}
diff -r 22924a8f1a7f -r 58bde69dd58a lib/librumpuser/rumpuser_port.h
--- a/lib/librumpuser/rumpuser_port.h Sat Apr 27 16:43:13 2013 +0000
+++ b/lib/librumpuser/rumpuser_port.h Sat Apr 27 16:56:29 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: rumpuser_port.h,v 1.16 2013/03/20 12:59:10 pooka Exp $ */
+/* $NetBSD: rumpuser_port.h,v 1.17 2013/04/27 16:56:29 pooka Exp $ */
/*
* Portability header for non-NetBSD platforms.
@@ -151,8 +151,10 @@
#endif
#if defined(__linux__) || defined(__sun__) || defined (__CYGWIN__)
-#define arc4random() random()
-#define RUMPUSER_USE_RANDOM
+#define RUMPUSER_RANDOM() random()
+#define RUMPUSER_USE_DEVRANDOM
+#else
+#define RUMPUSER_RANDOM() arc4random()
#endif
#ifndef __NetBSD_Prereq__
diff -r 22924a8f1a7f -r 58bde69dd58a lib/librumpuser/rumpuser_sp.c
--- a/lib/librumpuser/rumpuser_sp.c Sat Apr 27 16:43:13 2013 +0000
+++ b/lib/librumpuser/rumpuser_sp.c Sat Apr 27 16:56:29 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: rumpuser_sp.c,v 1.53 2013/04/27 16:02:56 pooka Exp $ */
+/* $NetBSD: rumpuser_sp.c,v 1.54 2013/04/27 16:56:29 pooka Exp $ */
/*
* Copyright (c) 2010, 2011 Antti Kantee. All Rights Reserved.
@@ -37,7 +37,7 @@
#include "rumpuser_port.h"
#if !defined(lint)
-__RCSID("$NetBSD: rumpuser_sp.c,v 1.53 2013/04/27 16:02:56 pooka Exp $");
+__RCSID("$NetBSD: rumpuser_sp.c,v 1.54 2013/04/27 16:56:29 pooka Exp $");
#endif /* !lint */
#include <sys/types.h>
@@ -1104,9 +1104,8 @@
}
/* Ok, we have a new process context and a new curlwp */
- for (i = 0; i < AUTHLEN; i++) {
- pf->pf_auth[i] = auth[i] = arc4random();
- }
+ rumpuser_getrandom(auth, sizeof(auth), 0);
+ memcpy(pf->pf_auth, auth, sizeof(pf->pf_auth));
pf->pf_lwp = lwproc_curlwp();
lwproc_switch(NULL);
diff -r 22924a8f1a7f -r 58bde69dd58a sys/rump/include/rump/rumpuser.h
--- a/sys/rump/include/rump/rumpuser.h Sat Apr 27 16:43:13 2013 +0000
+++ b/sys/rump/include/rump/rumpuser.h Sat Apr 27 16:56:29 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: rumpuser.h,v 1.84 2013/04/27 16:32:56 pooka Exp $ */
+/* $NetBSD: rumpuser.h,v 1.85 2013/04/27 16:56:30 pooka Exp $ */
/*
* Copyright (c) 2007-2011 Antti Kantee. All Rights Reserved.
@@ -121,7 +121,10 @@
int rumpuser_getnhostcpu(void);
-uint32_t rumpuser_arc4random(void);
+/* always succeeds unless NOWAIT is given */
+#define RUMPUSER_RANDOM_HARD 0x01
+#define RUMPUSER_RANDOM_NOWAIT 0x02
+size_t rumpuser_getrandom(void *, size_t, int);
__dead void rumpuser_biothread(void *);
diff -r 22924a8f1a7f -r 58bde69dd58a sys/rump/librump/rumpkern/cprng_stub.c
--- a/sys/rump/librump/rumpkern/cprng_stub.c Sat Apr 27 16:43:13 2013 +0000
+++ b/sys/rump/librump/rumpkern/cprng_stub.c Sat Apr 27 16:56:29 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cprng_stub.c,v 1.4 2011/12/17 20:05:40 tls Exp $ */
+/* $NetBSD: cprng_stub.c,v 1.5 2013/04/27 16:56:30 pooka Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -100,37 +100,25 @@
size_t
cprng_fast(void *p, size_t len)
{
- uint8_t *resp, *pchar = (uint8_t *)p;
- uint32_t res;
- size_t i;
- do {
- res = rumpuser_arc4random();
- resp = (uint8_t *)&res;
-
- for (i = 0; i < sizeof(res); i++) {
- *pchar++ = resp[i];
- if (pchar == (uint8_t *)p + len) {
- return len;
- }
- }
- } while(1);
+ rumpuser_getrandom(p, len, 0);
+ return len;
}
uint32_t
cprng_fast32(void)
{
- return rumpuser_arc4random();
+ uint32_t ret;
+
+ rumpuser_getrandom(&ret, sizeof(ret), 0);
+ return ret;
}
uint64_t
cprng_fast64(void)
{
uint64_t ret;
- uint32_t *ret32;
- ret32 = (uint32_t *)&ret;
- ret32[0] = rumpuser_arc4random();
- ret32[1] = rumpuser_arc4random();
+ rumpuser_getrandom(&ret, sizeof(ret), 0);
return ret;
}
Home |
Main Index |
Thread Index |
Old Index