[vlc-devel] [PATCH 1/2] rand: always use bcrypt instead of the old API

Steve Lhomme robux4 at ycbcr.xyz
Thu Mar 26 16:33:36 CET 2020


The API is available since Vista and the minimum we support is Win7.

This will result in depending on the bcrypt.dll, instead of advapi32.dll
---
 configure.ac     |  8 +-------
 src/win32/rand.c | 42 +-----------------------------------------
 2 files changed, 2 insertions(+), 48 deletions(-)

diff --git a/configure.ac b/configure.ac
index 4001e51c163..8e834464eda 100644
--- a/configure.ac
+++ b/configure.ac
@@ -290,7 +290,7 @@ case "${host_os}" in
 
     if test "${SYS}" = "mingw32"; then
         # DEP, ASLR, NO SEH
-        LDFLAGS="${LDFLAGS} -Wl,--nxcompat -Wl,--no-seh -Wl,--dynamicbase"
+        LDFLAGS="${LDFLAGS} -lbcrypt -Wl,--nxcompat -Wl,--no-seh -Wl,--dynamicbase"
 
         AC_CHECK_PROGS(U2D, [unix2dos todos], unix2dos)
         ac_default_prefix="`pwd`/_win32"
@@ -1253,12 +1253,6 @@ AS_IF([test "${enable_ssp}" != "no" -a "${enable_optimizations}" != "no"], [
   ])
 ])
 
-AS_IF([test "${SYS}" = "mingw32"], [
-    dnl library for BCrypt APIs
-    AS_IF([test "${vlc_winstore_app}" = 1],
-          [LDFLAGS="${LDFLAGS} -lbcrypt"])
-])
-
 VLC_SAVE_FLAGS
 LDFLAGS="${LDFLAGS} -Wl,-Bsymbolic"
 AC_CACHE_CHECK([if linker supports -Bsymbolic], [ac_cv_ld_bsymbolic], [
diff --git a/src/win32/rand.c b/src/win32/rand.c
index 5a48522764c..1af02aa5984 100644
--- a/src/win32/rand.c
+++ b/src/win32/rand.c
@@ -25,15 +25,10 @@
 #include <vlc_common.h>
 #include <vlc_rand.h>
 
-#if VLC_WINSTORE_APP
-# include <bcrypt.h>
-#else
-# include <wincrypt.h>
-#endif
+#include <bcrypt.h>
 
 void vlc_rand_bytes (void *buf, size_t len)
 {
-#if VLC_WINSTORE_APP
     BCRYPT_ALG_HANDLE algo_handle;
     NTSTATUS ret = BCryptOpenAlgorithmProvider(&algo_handle, BCRYPT_RNG_ALGORITHM,
                                                MS_PRIMITIVE_PROVIDER, 0);
@@ -42,39 +37,4 @@ void vlc_rand_bytes (void *buf, size_t len)
         BCryptGenRandom(algo_handle, buf, len, 0);
         BCryptCloseAlgorithmProvider(algo_handle, 0);
     }
-#else
-    size_t count = len;
-    uint8_t *p_buf = (uint8_t *)buf;
-
-    /* fill buffer with pseudo-random data */
-    while (count > 0)
-    {
-        unsigned int val;
-        val = rand();
-        if (count < sizeof (val))
-        {
-            memcpy (p_buf, &val, count);
-            break;
-        }
-
-        memcpy (p_buf, &val, sizeof (val));
-        count -= sizeof (val);
-        p_buf += sizeof (val);
-    }
-
-    HCRYPTPROV hProv;
-    /* acquire default encryption context */
-    if( CryptAcquireContext(
-        &hProv,                 // Variable to hold returned handle.
-        NULL,                   // Use default key container.
-        MS_DEF_PROV,            // Use default CSP.
-        PROV_RSA_FULL,          // Type of provider to acquire.
-        CRYPT_VERIFYCONTEXT) )  // Flag values
-    {
-        /* fill buffer with pseudo-random data, initial buffer content
-           is used as auxiliary random seed */
-        CryptGenRandom(hProv, len, buf);
-        CryptReleaseContext(hProv, 0);
-    }
-#endif /* VLC_WINSTORE_APP */
 }
-- 
2.17.1



More information about the vlc-devel mailing list