]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/random.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / random.c
index 3f5919173676a898128eb97af8b301cb60a92a17..acfbc3123cf9b6469802a601985eac2baf2324db 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
-#include <string.h>
 #include <stdlib.h>
-#include <assert.h>
 #include <time.h>
 
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#include <wincrypt.h>
+#endif
+
 #include <pulsecore/core-util.h>
 #include <pulsecore/log.h>
+#include <pulsecore/macro.h>
 
 #include "random.h"
 
-static int has_whined = 0;
+static bool has_whined = false;
 
-static const char *devices[] = { "/dev/urandom", "/dev/random", NULL };
+static const char * const devices[] = { "/dev/urandom", "/dev/random", NULL };
 
 static int random_proper(void *ret_data, size_t length) {
 #ifdef OS_IS_WIN32
-    assert(ret_data && length);
+    int ret = -1;
 
-    return -1;
+    HCRYPTPROV hCryptProv = 0;
+
+    pa_assert(ret_data);
+    pa_assert(length > 0);
+
+    if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
+        if (CryptGenRandom(hCryptProv, length, ret_data))
+            ret = 0;
+        CryptReleaseContext(hCryptProv, 0);
+    }
+
+    return ret;
 
 #else /* OS_IS_WIN32 */
 
     int fd, ret = -1;
     ssize_t r = 0;
-    const char **device;
+    const char *const * device;
 
-    assert(ret_data && length);
+    pa_assert(ret_data);
+    pa_assert(length > 0);
 
     device = devices;
 
     while (*device) {
         ret = 0;
 
-        if ((fd = open(*device, O_RDONLY)) >= 0) {
+        if ((fd = pa_open_cloexec(*device, O_RDONLY, 0)) >= 0) {
 
             if ((r = pa_loop_read(fd, ret_data, length, NULL)) < 0 || (size_t) r != length)
                 ret = -1;
 
-            close(fd);
+            pa_close(fd);
         } else
             ret = -1;
 
         if (ret == 0)
             break;
+
+        device++;
     }
 
     return ret;
@@ -83,9 +99,11 @@ void pa_random_seed(void) {
     unsigned int seed;
 
     if (random_proper(&seed, sizeof(unsigned int)) < 0) {
-        if (!has_whined)
-            pa_log_warn("failed to get proper entropy. Falling back to seeding with current time.");
-        has_whined = 1;
+
+        if (!has_whined) {
+            pa_log_warn("Failed to get proper entropy. Falling back to seeding with current time.");
+            has_whined = true;
+        }
 
         seed = (unsigned int) time(NULL);
     }
@@ -97,14 +115,16 @@ void pa_random(void *ret_data, size_t length) {
     uint8_t *p;
     size_t l;
 
-    assert(ret_data && length);
+    pa_assert(ret_data);
+    pa_assert(length > 0);
 
     if (random_proper(ret_data, length) >= 0)
         return;
 
-    if (!has_whined)
-        pa_log_warn("failed to get proper entropy. Falling back to unsecure pseudo RNG.");
-    has_whined = 1;
+    if (!has_whined) {
+        pa_log_warn("Failed to get proper entropy. Falling back to unsecure pseudo RNG.");
+        has_whined = true;
+    }
 
     for (p = ret_data, l = length; l > 0; p++, l--)
         *p = (uint8_t) rand();