]> git.saurik.com Git - apple/security.git/blobdiff - OSX/libsecurity_utilities/lib/devrandom.cpp
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / devrandom.cpp
index f08c2d9787180404c7b727223eac4f0b7880eda7..626ed7ef2e4e46187e3400533d635c38b3e35ea9 100644 (file)
@@ -27,6 +27,7 @@
 //
 #include <security_utilities/devrandom.h>
 #include <security_utilities/logging.h>
+#include <CommonCrypto/CommonRandomSPI.h>
 
 using namespace UnixPlusPlus;
 
@@ -37,7 +38,6 @@ namespace Security {
 //
 // The common (shared) open file descriptor to /dev/random
 //
-ModuleNexus<DevRandomGenerator::Readonly> DevRandomGenerator::mReader;
 ModuleNexus<DevRandomGenerator::Writable> DevRandomGenerator::mWriter;
 
 
@@ -54,18 +54,10 @@ DevRandomGenerator::DevRandomGenerator(bool writable)
 //
 void DevRandomGenerator::random(void *data, size_t length)
 {
-    try {
-               size_t bytesRead = mReader().read(data, length);
-               if (bytesRead != length) {      // short read (shouldn't happen)
-                       Syslog::error("DevRandomGenerator: wanted %ld got %ld bytes",
-                               length, bytesRead);
-                       UnixError::throwMe(EIO);
-               }
-       } catch(const UnixError &uerr) {
-               Syslog::error("DevRandomGenerator: error %d reading /dev/random",
-                       uerr.error);
-               throw;
-       }
+    if (CCRandomCopyBytes(kCCRandomDefault, data, length)) {
+        Syslog::error("DevRandomGenerator: failed to generate random");
+        UnixError::throwMe(EIO);
+    }
 }