]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/dev/random/randomdev.c
xnu-1228.5.18.tar.gz
[apple/xnu.git] / bsd / dev / random / randomdev.c
index 04e1883a4d1941ad194ae6cf80ccfdcb1f4a48de..9208ff6b69e6cb449fc8c34be2b29e310b2e6c1d 100644 (file)
@@ -1,23 +1,29 @@
 /*
- * Copyright (c)1999-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1999-2006 Apple Computer, Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * 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. 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.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * 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 <string.h>
 #include <miscfs/devfs/devfs.h>
 #include <kern/lock.h>
+#include <kern/clock.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 <crypto/sha1.h>
+#include <libkern/crypto/sha1.h>
+
+#include <mach/mach_time.h>
+#include <machine/machine_routines.h>
 
 #define RANDOM_MAJOR  -1 /* let the kernel pick the device number */
 
@@ -68,7 +78,12 @@ static struct cdevsw random_cdevsw =
 static int gRandomInstalled = 0;
 static PrngRef gPrngRef;
 static int gRandomError = 1;
-static mutex_t *gYarrowMutex = 0;
+static lck_grp_t *gYarrowGrp;
+static lck_attr_t *gYarrowAttr;
+static lck_grp_attr_t *gYarrowGrpAttr;
+static lck_mtx_t *gYarrowMutex = 0;
+
+void CheckReseed(void);
 
 #define RESEED_TICKS 50 /* how long a reseed operation can take */
 
@@ -184,9 +199,10 @@ random_block(Block b, int addOptional)
        int repeatCount = 0;
        do
        {
+               // do one iteration
+               
                if (addOptional)
                {
-                       // do one iteration
                        Block xSeed;
                        prngOutput (gPrngRef, (BYTE*) &xSeed, sizeof (xSeed));
                        
@@ -249,8 +265,6 @@ void
 PreliminarySetup(void)
 {
     prng_error_status perr;
-    struct timeval tt;
-    char buffer [16];
 
     /* create a Yarrow object */
     perr = prngInitialize(&gPrngRef);
@@ -262,6 +276,10 @@ PreliminarySetup(void)
        /* clear the error flag, reads and write should then work */
     gRandomError = 0;
 
+   {
+    struct timeval tt;
+    char buffer [16];
+
     /* get a little non-deterministic data as an initial seed. */
     microtime(&tt);
 
@@ -280,13 +298,17 @@ PreliminarySetup(void)
     }
     
     /* 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);
+    gYarrowGrpAttr = lck_grp_attr_alloc_init();
+    gYarrowGrp     = lck_grp_alloc_init("random", gYarrowGrpAttr);
+    gYarrowAttr    = lck_attr_alloc_init();
+    gYarrowMutex   = lck_mtx_alloc_init(gYarrowGrp, gYarrowAttr);
        
        fips_initialize ();
 }
@@ -316,8 +338,8 @@ fips_initialize(void)
                        panic("FIPS random self test failed");
                }
        }
-
-       // now do the random block again to make sure that userland doesn't get predictable data
+       
+       // now do the random block again to make sure that userland doesn't get predicatable data
        random_block(g_random_data, TRUE);
 }
 
@@ -336,8 +358,10 @@ random_init(void)
        /* install us in the file system */
        gRandomInstalled = 1;
 
+#ifndef ARM_BOARD_CONFIG_S5L8900XFPGA_1136JFS
        /* setup yarrow and the mutex */
        PreliminarySetup();
+#endif
 
        ret = cdevsw_add(RANDOM_MAJOR, &random_cdevsw);
        if (ret < 0) {
@@ -428,7 +452,7 @@ random_write (__unused dev_t dev, struct uio *uio, __unused int ioflag)
     }
     
     /* get control of the Yarrow instance, Yarrow is NOT thread safe */
-    mutex_lock(gYarrowMutex);
+    lck_mtx_lock(gYarrowMutex);
     
     /* Security server is sending us entropy */
 
@@ -441,7 +465,7 @@ random_write (__unused dev_t dev, struct uio *uio, __unused int ioflag)
             goto /*ugh*/ error_exit;
         
         /* put it in Yarrow */
-        if (prngInput(gPrngRef, (BYTE*)rdBuffer,
+        if (prngInput(gPrngRef, (BYTE*) rdBuffer,
                        bytesToInput, SYSTEM_SOURCE,
                bytesToInput * 8) != 0) {
             retCode = EIO;
@@ -458,14 +482,15 @@ random_write (__unused dev_t dev, struct uio *uio, __unused int ioflag)
     /* retCode should be 0 at this point */
     
 error_exit: /* do this to make sure the mutex unlocks. */
-    mutex_unlock(gYarrowMutex);
+    lck_mtx_unlock(gYarrowMutex);
     return (retCode);
 }
 
 /*
  * return data to the caller.  Results unpredictable.
  */ 
-int random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
+int
+random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
 {
     int retCode = 0;
        
@@ -473,8 +498,9 @@ int random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
         return (ENOTSUP);
 
    /* lock down the mutex */
-    mutex_lock(gYarrowMutex);
+    lck_mtx_lock(gYarrowMutex);
 
+    CheckReseed();
        int bytes_remaining = uio_resid(uio);
     while (bytes_remaining > 0 && retCode == 0) {
         /* get the user's data */
@@ -490,7 +516,7 @@ int random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
                
                bytes_to_read = min (bytes_remaining, bytes_available);
                
-        retCode = uiomove(((u_int8_t*)g_random_data)+ g_bytes_used, bytes_to_read, uio);
+        retCode = uiomove(((caddr_t)g_random_data)+ g_bytes_used, bytes_to_read, uio);
         g_bytes_used += bytes_to_read;
 
         if (retCode != 0)
@@ -502,7 +528,7 @@ int random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
     retCode = 0;
     
 error_exit:
-    mutex_unlock(gYarrowMutex);
+    lck_mtx_unlock(gYarrowMutex);
     return retCode;
 }
 
@@ -511,11 +537,14 @@ void
 read_random(void* buffer, u_int numbytes)
 {
     if (gYarrowMutex == 0) { /* are we initialized? */
+#ifndef ARM_BOARD_CONFIG_S5L8900XFPGA_1136JFS
         PreliminarySetup ();
+#endif
     }
     
-    mutex_lock(gYarrowMutex);
-       
+    lck_mtx_lock(gYarrowMutex);
+    CheckReseed();
+
        int bytes_read = 0;
 
        int bytes_remaining = numbytes;
@@ -528,13 +557,13 @@ read_random(void* buffer, u_int numbytes)
                        bytes_to_read = min(bytes_remaining, kBSizeInBytes);
                }
                
-               memmove (buffer, ((u_int8_t*)g_random_data)+ bytes_read, bytes_to_read);
+               memmove ((u_int8_t*) buffer + bytes_read, ((u_int8_t*)g_random_data)+ g_bytes_used, bytes_to_read);
                g_bytes_used += bytes_to_read;
                bytes_read += bytes_to_read;
                bytes_remaining -= bytes_to_read;
     }
 
-    mutex_unlock(gYarrowMutex);
+    lck_mtx_unlock(gYarrowMutex);
 }
 
 /*
@@ -548,3 +577,7 @@ RandomULong(void)
        return (buf);
 }
 
+void
+CheckReseed(void)
+{
+}