Loading...
bsd/dev/random/randomdev.c xnu-792.21.3 xnu-1228
--- xnu/xnu-792.21.3/bsd/dev/random/randomdev.c
+++ xnu/xnu-1228/bsd/dev/random/randomdev.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c)1999-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1999-2006 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
@@ -36,13 +36,17 @@
 #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 */
 
@@ -74,7 +78,12 @@
 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 */
 
@@ -252,8 +261,6 @@
 PreliminarySetup(void)
 {
     prng_error_status perr;
-    struct timeval tt;
-    char buffer [16];
 
     /* create a Yarrow object */
     perr = prngInitialize(&gPrngRef);
@@ -264,6 +271,10 @@
 
 	/* 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);
@@ -283,13 +294,17 @@
     }
     
     /* 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 ();
 }
@@ -324,8 +339,10 @@
 	/* 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) {
@@ -416,7 +433,7 @@
     }
     
     /* get control of the Yarrow instance, Yarrow is NOT thread safe */
-    mutex_lock(gYarrowMutex);
+    lck_mtx_lock(gYarrowMutex);
     
     /* Security server is sending us entropy */
 
@@ -429,7 +446,7 @@
             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;
@@ -446,14 +463,15 @@
     /* 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;
 	
@@ -461,8 +479,9 @@
         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 */
@@ -478,7 +497,7 @@
 		
 		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)
@@ -490,7 +509,7 @@
     retCode = 0;
     
 error_exit:
-    mutex_unlock(gYarrowMutex);
+    lck_mtx_unlock(gYarrowMutex);
     return retCode;
 }
 
@@ -499,11 +518,14 @@
 read_random(void* buffer, u_int numbytes)
 {
     if (gYarrowMutex == 0) { /* are we initialized? */
+#ifndef ARM_BOARD_CONFIG_S5L8900XFPGA_1136JFS
         PreliminarySetup ();
-    }
-    
-    mutex_lock(gYarrowMutex);
-	
+#endif
+    }
+    
+    lck_mtx_lock(gYarrowMutex);
+    CheckReseed();
+
 	int bytes_read = 0;
 
 	int bytes_remaining = numbytes;
@@ -516,13 +538,13 @@
 			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);
 }
 
 /*
@@ -536,3 +558,7 @@
 	return (buf);
 }
 
+void
+CheckReseed(void)
+{
+}