]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/dev/random/randomdev.c
xnu-792.18.15.tar.gz
[apple/xnu.git] / bsd / dev / random / randomdev.c
index 96f997e24d0324d9da3231448ce3c48ec9d78580..d98b1e29c47a832eaa863c0961552cd985eaf5aa 100644 (file)
@@ -1,16 +1,19 @@
 /*
 /*
- * Copyright (c) 1999, 2000-2002 Apple Computer, Inc. All rights reserved.
+ * Copyright (c)1999-2004 Apple Computer, Inc. All rights reserved.
  *
  *
- * @APPLE_LICENSE_HEADER_START@
- * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
@@ -20,7 +23,7 @@
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
 #include <sys/ioctl.h>
 #include <sys/conf.h>
 #include <sys/fcntl.h>
 #include <sys/ioctl.h>
 #include <sys/conf.h>
 #include <sys/fcntl.h>
+#include <string.h>
 #include <miscfs/devfs/devfs.h>
 #include <kern/lock.h>
 #include <sys/time.h>
 #include <sys/malloc.h>
 #include <miscfs/devfs/devfs.h>
 #include <kern/lock.h>
 #include <sys/time.h>
 #include <sys/malloc.h>
+#include <sys/uio_internal.h>
 
 #include <dev/random/randomdev.h>
 #include <dev/random/YarrowCoreLib/include/yarrow.h>
 
 #include <dev/random/randomdev.h>
 #include <dev/random/YarrowCoreLib/include/yarrow.h>
+#include <crypto/sha1.h>
 
 #define RANDOM_MAJOR  -1 /* let the kernel pick the device number */
 
 
 #define RANDOM_MAJOR  -1 /* let the kernel pick the device number */
 
+d_ioctl_t       random_ioctl;
+
 /*
  * A struct describing which functions will get invoked for certain
  * actions.
 /*
  * A struct describing which functions will get invoked for certain
  * actions.
@@ -50,9 +58,9 @@ static struct cdevsw random_cdevsw =
        random_close,           /* close */
        random_read,            /* read */
        random_write,           /* write */
        random_close,           /* close */
        random_read,            /* read */
        random_write,           /* write */
-       eno_ioctl,                      /* ioctl */
-       nulldev,                        /* stop */
-       nulldev,                        /* reset */
+       random_ioctl,           /* ioctl */
+       (stop_fcn_t *)nulldev, /* stop */
+       (reset_fcn_t *)nulldev, /* reset */
        NULL,                           /* tty's */
        eno_select,                     /* select */
        eno_mmap,                       /* mmap */
        NULL,                           /* tty's */
        eno_select,                     /* select */
        eno_mmap,                       /* mmap */
@@ -70,10 +78,84 @@ static mutex_t *gYarrowMutex = 0;
 
 #define RESEED_TICKS 50 /* how long a reseed operation can take */
 
 
 #define RESEED_TICKS 50 /* how long a reseed operation can take */
 
+
+enum {kBSizeInBits = 160}; // MUST be a multiple of 32!!!
+enum {kBSizeInBytes = kBSizeInBits / 8};
+typedef u_int32_t BlockWord;
+enum {kWordSizeInBits = 32};
+enum {kBSize = 5};
+typedef BlockWord Block[kBSize];
+
+/* define prototypes to keep the compiler happy... */
+
+void add_blocks(Block a, Block b, BlockWord carry);
+void fips_initialize(void);
+void random_block(Block b);
+
+/*
+ * Get 120 bits from yarrow
+ */
+
+/*
+ * add block b to block a
+ */
+void
+add_blocks(Block a, Block b, BlockWord carry)
+{
+       int i = kBSize;
+       while (--i >= 0)
+       {
+               u_int64_t c = (u_int64_t)carry +
+                                         (u_int64_t)a[i] +
+                                         (u_int64_t)b[i];
+               a[i] = c & ((1LL << kWordSizeInBits) - 1);
+               carry = c >> kWordSizeInBits;
+       }
+}
+
+
+
+struct sha1_ctxt g_sha1_ctx;
+char zeros[(512 - kBSizeInBits) / 8];
+Block g_xkey;
+Block g_random_data;
+int g_bytes_used;
+
+/*
+ * Setup for fips compliance
+ */
+
+/*
+ * get a random block of data per fips 186-2
+ */
+void
+random_block(Block b)
+{
+       // do one iteration
+       Block xSeed;
+       prngOutput (gPrngRef, (BYTE*) &xSeed, sizeof (xSeed));
+       
+       // add the seed to the previous value of g_xkey
+       add_blocks (g_xkey, xSeed, 0);
+
+       // compute "G"
+       SHA1Update (&g_sha1_ctx, (const u_int8_t *) &g_xkey, sizeof (g_xkey));
+       
+       // add zeros to fill the internal SHA-1 buffer
+       SHA1Update (&g_sha1_ctx, (const u_int8_t *)zeros, sizeof (zeros));
+       
+       // write the resulting block
+       memmove(b, g_sha1_ctx.h.b8, sizeof (Block));
+       
+       // fix up the next value of g_xkey
+       add_blocks (g_xkey, b, 1);
+}
+
 /*
  *Initialize ONLY the Yarrow generator.
  */
 /*
  *Initialize ONLY the Yarrow generator.
  */
-void PreliminarySetup ()
+void
+PreliminarySetup(void)
 {
     prng_error_status perr;
     struct timeval tt;
 {
     prng_error_status perr;
     struct timeval tt;
@@ -107,13 +189,30 @@ void PreliminarySetup ()
     }
     
     /* turn the data around */
     }
     
     /* turn the data around */
-    perr = prngOutput(gPrngRef, (BYTE*) buffer, sizeof (buffer));
+    perr = prngOutput(gPrngRef, (BYTE*)buffer, sizeof (buffer));
     
     /* and scramble it some more */
     perr = prngForceReseed(gPrngRef, RESEED_TICKS);
     
     /* make a mutex to control access */
     gYarrowMutex = mutex_alloc(0);
     
     /* and scramble it some more */
     perr = prngForceReseed(gPrngRef, RESEED_TICKS);
     
     /* make a mutex to control access */
     gYarrowMutex = mutex_alloc(0);
+       
+       fips_initialize ();
+}
+
+void
+fips_initialize(void)
+{
+       /* Read the initial value of g_xkey from yarrow */
+       prngOutput (gPrngRef, (BYTE*) &g_xkey, sizeof (g_xkey));
+       
+       /* initialize our SHA1 generator */
+       SHA1Init (&g_sha1_ctx);
+       
+       /* other initializations */
+       memset (zeros, 0, sizeof (zeros));
+       g_bytes_used = 0;
+       random_block(g_random_data);
 }
 
 /*
 }
 
 /*
@@ -121,7 +220,7 @@ void PreliminarySetup ()
  * and to register ourselves with devfs
  */
 void
  * and to register ourselves with devfs
  */
 void
-random_init()
+random_init(void)
 {
        int ret;
 
 {
        int ret;
 
@@ -142,14 +241,29 @@ random_init()
        }
 
        devfs_make_node(makedev (ret, 0), DEVFS_CHAR,
        }
 
        devfs_make_node(makedev (ret, 0), DEVFS_CHAR,
-               UID_ROOT, GID_WHEEL, 0644, "random", 0);
+               UID_ROOT, GID_WHEEL, 0666, "random", 0);
 
        /*
         * also make urandom 
         * (which is exactly the same thing in our context)
         */
        devfs_make_node(makedev (ret, 1), DEVFS_CHAR,
 
        /*
         * also make urandom 
         * (which is exactly the same thing in our context)
         */
        devfs_make_node(makedev (ret, 1), DEVFS_CHAR,
-               UID_ROOT, GID_WHEEL, 0644, "urandom", 0);
+               UID_ROOT, GID_WHEEL, 0666, "urandom", 0);
+}
+
+int
+random_ioctl(  __unused dev_t dev, u_long cmd, __unused caddr_t data, 
+                               __unused int flag, __unused struct proc *p  )
+{
+       switch (cmd) {
+       case FIONBIO:
+       case FIOASYNC:
+               break;
+       default:
+               return ENODEV;
+       }
+
+       return (0);
 }
 
 /*
 }
 
 /*
@@ -158,7 +272,7 @@ random_init()
  */
  
 int
  */
  
 int
-random_open(dev_t dev, int flags, int devtype, struct proc *p)
+random_open(__unused dev_t dev, int flags, __unused int devtype, __unused struct proc *p)
 {
        if (gRandomError != 0) {
                /* forget it, yarrow didn't come up */
 {
        if (gRandomError != 0) {
                /* forget it, yarrow didn't come up */
@@ -172,8 +286,10 @@ random_open(dev_t dev, int flags, int devtype, struct proc *p)
        if (flags & FWRITE) {
                if (securelevel >= 2)
                        return (EPERM);
        if (flags & FWRITE) {
                if (securelevel >= 2)
                        return (EPERM);
-               if ((securelevel >= 1) && suser(p->p_ucred, &p->p_acflag))
+#ifndef __APPLE__
+               if ((securelevel >= 1) && proc_suser(p))
                        return (EPERM);
                        return (EPERM);
+#endif /* !__APPLE__ */
        }
 
        return (0);
        }
 
        return (0);
@@ -185,7 +301,7 @@ random_open(dev_t dev, int flags, int devtype, struct proc *p)
  */
  
 int
  */
  
 int
-random_close(dev_t dev, int flags, int mode, struct proc *p)
+random_close(__unused dev_t dev, __unused int flags, __unused int mode, __unused struct proc *p)
 {
        return (0);
 }
 {
        return (0);
 }
@@ -196,7 +312,7 @@ random_close(dev_t dev, int flags, int mode, struct proc *p)
  * prng.
  */
 int
  * prng.
  */
 int
-random_write (dev_t dev, struct uio *uio, int ioflag)
+random_write (__unused dev_t dev, struct uio *uio, __unused int ioflag)
 {
     int retCode = 0;
     char rdBuffer[256];
 {
     int retCode = 0;
     char rdBuffer[256];
@@ -210,17 +326,18 @@ random_write (dev_t dev, struct uio *uio, int ioflag)
     
     /* Security server is sending us entropy */
 
     
     /* Security server is sending us entropy */
 
-    while (uio->uio_resid > 0 && retCode == 0) {
+    while (uio_resid(uio) > 0 && retCode == 0) {
         /* get the user's data */
         /* get the user's data */
-        int bytesToInput = min(uio->uio_resid, sizeof (rdBuffer));
+        // LP64todo - fix this!  uio_resid may be 64-bit value
+        int bytesToInput = min(uio_resid(uio), sizeof (rdBuffer));
         retCode = uiomove(rdBuffer, bytesToInput, uio);
         if (retCode != 0)
             goto /*ugh*/ error_exit;
         
         /* put it in Yarrow */
         retCode = uiomove(rdBuffer, bytesToInput, uio);
         if (retCode != 0)
             goto /*ugh*/ error_exit;
         
         /* put it in Yarrow */
-        if (prngInput(gPrngRef, (BYTE*) rdBuffer,
-                       sizeof (rdBuffer), SYSTEM_SOURCE,
-               sizeof (rdBuffer) * 8) != 0) {
+        if (prngInput(gPrngRef, (BYTE*)rdBuffer,
+                       bytesToInput, SYSTEM_SOURCE,
+               bytesToInput * 8) != 0) {
             retCode = EIO;
             goto error_exit;
         }
             retCode = EIO;
             goto error_exit;
         }
@@ -242,35 +359,38 @@ error_exit: /* do this to make sure the mutex unlocks. */
 /*
  * return data to the caller.  Results unpredictable.
  */ 
 /*
  * return data to the caller.  Results unpredictable.
  */ 
-int
-random_read(dev_t dev, struct uio *uio, int ioflag)
+int random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
 {
     int retCode = 0;
 {
     int retCode = 0;
-    char wrBuffer[512];
-
+       
     if (gRandomError != 0)
         return (ENOTSUP);
 
    /* lock down the mutex */
     mutex_lock(gYarrowMutex);
 
     if (gRandomError != 0)
         return (ENOTSUP);
 
    /* lock down the mutex */
     mutex_lock(gYarrowMutex);
 
-    while (uio->uio_resid > 0 && retCode == 0) {
+       int bytes_remaining = uio_resid(uio);
+    while (bytes_remaining > 0 && retCode == 0) {
         /* get the user's data */
         /* get the user's data */
-        int bytesToRead = min(uio->uio_resid, sizeof (wrBuffer));
-        
-        /* get the data from Yarrow */
-        if (prngOutput(gPrngRef, (BYTE *) wrBuffer, sizeof (wrBuffer)) != 0) {
-            printf ("Couldn't read data from Yarrow.\n");
-            
-            /* something's really weird */
-            retCode = EIO;
-            goto error_exit;
-        }
-        
-        retCode = uiomove(wrBuffer, bytesToRead, uio);
-        
+               int bytes_to_read = 0;
+               
+               int bytes_available = kBSizeInBytes - g_bytes_used;
+        if (bytes_available == 0)
+               {
+                       random_block(g_random_data);
+                       g_bytes_used = 0;
+                       bytes_available = kBSizeInBytes;
+               }
+               
+               bytes_to_read = min (bytes_remaining, bytes_available);
+               
+        retCode = uiomove(((u_int8_t*)g_random_data)+ g_bytes_used, bytes_to_read, uio);
+        g_bytes_used += bytes_to_read;
+
         if (retCode != 0)
             goto error_exit;
         if (retCode != 0)
             goto error_exit;
+               
+               bytes_remaining = uio_resid(uio);
     }
     
     retCode = 0;
     }
     
     retCode = 0;
@@ -289,7 +409,23 @@ read_random(void* buffer, u_int numbytes)
     }
     
     mutex_lock(gYarrowMutex);
     }
     
     mutex_lock(gYarrowMutex);
-    prngOutput(gPrngRef, (BYTE *) buffer, numbytes);
+
+       int bytes_remaining = numbytes;
+    while (bytes_remaining > 0) {
+        int bytes_to_read = min(bytes_remaining, kBSizeInBytes - g_bytes_used);
+        if (bytes_to_read == 0)
+               {
+                       random_block(g_random_data);
+                       g_bytes_used = 0;
+                       bytes_to_read = min(bytes_remaining, kBSizeInBytes);
+               }
+               
+               memmove (buffer, ((u_int8_t*)g_random_data)+ g_bytes_used, bytes_to_read);
+               g_bytes_used += bytes_to_read;
+               
+               bytes_remaining -= bytes_to_read;
+    }
+
     mutex_unlock(gYarrowMutex);
 }
 
     mutex_unlock(gYarrowMutex);
 }
 
@@ -297,7 +433,7 @@ read_random(void* buffer, u_int numbytes)
  * Return an unsigned long pseudo-random number.
  */
 u_long
  * Return an unsigned long pseudo-random number.
  */
 u_long
-RandomULong()
+RandomULong(void)
 {
        u_long buf;
        read_random(&buf, sizeof (buf));
 {
        u_long buf;
        read_random(&buf, sizeof (buf));