urandom_read() starts with a random 8 byte seed.  If we need less than
that, we can (probably) just go with those bytes, as if we called
random_read() directly.

This also exposes random_read() and urandom_read() to external callers.

Signed-off-by: Barret Rhoden <[email protected]>
---
 kern/drivers/dev/random.c | 13 +++++++------
 kern/include/ns.h         |  2 ++
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/kern/drivers/dev/random.c b/kern/drivers/dev/random.c
index 4e54c03088ef..bba11d887ed7 100644
--- a/kern/drivers/dev/random.c
+++ b/kern/drivers/dev/random.c
@@ -47,7 +47,7 @@ void random_add(void *xp)
 /*
  *  consume random bytes
  */
-static uint32_t _randomread(void *xp, uint32_t n)
+uint32_t random_read(void *xp, uint32_t n)
 {
        ERRSTACK(1);
 
@@ -69,17 +69,18 @@ static uint32_t _randomread(void *xp, uint32_t n)
 /**
  * Fast random generator
  **/
-uint32_t urandomread(void *xp, uint32_t n)
+uint32_t urandom_read(void *xp, uint32_t n)
 {
-       ERRSTACK(1);
        uint64_t seed[16];
        uint8_t *e, *p;
        uint32_t x = 0;
        uint64_t s0;
        uint64_t s1;
 
+       if (n <= sizeof(seed))
+               return random_read(xp, n);
        // The initial seed is from a good random pool.
-       _randomread(seed, sizeof(seed));
+       random_read(seed, sizeof(seed));
        p = xp;
        for (e = p + n; p < e;) {
                s0 = seed[x];
@@ -157,9 +158,9 @@ static long randomread(struct chan *c, void *va, long n, 
int64_t ignored)
                        return devdirread(c, va, n, randomdir,
                                          ARRAY_SIZE(randomdir), devgen);
                case Qrandom:
-                       return _randomread(va, n);
+                       return random_read(va, n);
                case Qurandom:
-                       return urandomread(va, n);
+                       return urandom_read(va, n);
                default:
                        panic("randomread: qid %d is impossible", c->qid.path);
        }
diff --git a/kern/include/ns.h b/kern/include/ns.h
index c0e715f7aba3..3f22c8e835ca 100644
--- a/kern/include/ns.h
+++ b/kern/include/ns.h
@@ -793,6 +793,8 @@ char *nextelem(char *unused_char_p_t, char *);
 struct cname *newcname(char *unused_char_p_t);
 void notkilled(void);
 int nrand(int);
+uint32_t random_read(void *xp, uint32_t n);
+uint32_t urandom_read(void *xp, uint32_t n);
 uint64_t ns2fastticks(uint64_t);
 int okaddr(uint32_t, uint32_t, int);
 int omode_to_rwx(int);
-- 
2.7.0.rc3.207.g0ac5344

-- 
You received this message because you are subscribed to the Google Groups 
"Akaros" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to