[vlc-devel] [PATCH] rand: use bcrypt instead of CryptographicBufferStatics for Winstore builds

Steve Lhomme robux4 at ycbcr.xyz
Wed Mar 28 11:26:20 CEST 2018


It's available to winstore apps and on desktop since Vista.
The old API is deprecated.
---
 configure.ac     |  5 ++++-
 src/win32/rand.c | 50 +++++++++++---------------------------------------
 2 files changed, 15 insertions(+), 40 deletions(-)

diff --git a/configure.ac b/configure.ac
index 8baf2337cf..0a1b43ea8e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1124,7 +1124,10 @@ AH_BOTTOM([
   dnl Win32 requires linking to ssp for stack-protection
   AS_IF([test "${SYS}" = "mingw32"], [
       LDFLAGS="${LDFLAGS} -lssp"
-      AS_IF([test "${vlc_winstore_app}" != 1], [LDFLAGS="${LDFLAGS} -ladvapi32"])
+      dnl library for Crypt/BCrypt APIs
+      AS_IF([test "${vlc_winstore_app}" != 1], 
+            [LDFLAGS="${LDFLAGS} -ladvapi32"],
+            [LDFLAGS="${LDFLAGS} -lbcrypt"])
   ])
 ])
 
diff --git a/src/win32/rand.c b/src/win32/rand.c
index 1aeb656a8a..723a32b26e 100644
--- a/src/win32/rand.c
+++ b/src/win32/rand.c
@@ -27,17 +27,23 @@
 #include <vlc_rand.h>
 
 #if VLC_WINSTORE_APP
-# define COBJMACROS
-# define INITGUID
-# include <winstring.h>
-# include <roapi.h>
-# include <windows.security.cryptography.h>
+# include <bcrypt.h>
 #else
 # include <wincrypt.h>
 #endif
 
 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);
+    if (BCRYPT_SUCCESS(ret))
+    {
+        BCryptGenRandom(algo_handle, buf, len, 0);
+        BCryptCloseAlgorithmProvider(algo_handle, 0);
+    }
+#else
     size_t count = len;
     uint8_t *p_buf = (uint8_t *)buf;
 
@@ -57,40 +63,6 @@ void vlc_rand_bytes (void *buf, size_t len)
         p_buf += sizeof (val);
     }
 
-#if VLC_WINSTORE_APP
-    static const WCHAR *className = L"Windows.Security.Cryptography.CryptographicBuffer";
-    const UINT32 clen = wcslen(className);
-
-    HSTRING hClassName = NULL;
-    HSTRING_HEADER header;
-    HRESULT hr = WindowsCreateStringReference(className, clen, &header, &hClassName);
-    if (hr) {
-        WindowsDeleteString(hClassName);
-        return;
-    }
-
-    ICryptographicBufferStatics *cryptoStatics = NULL;
-    hr = RoGetActivationFactory(hClassName, &IID_ICryptographicBufferStatics, (void**)&cryptoStatics);
-    WindowsDeleteString(hClassName);
-
-    if (hr)
-        return;
-
-    IBuffer *buffer = NULL;
-    hr = ICryptographicBufferStatics_GenerateRandom(cryptoStatics, len, &buffer);
-    if (hr) {
-        ICryptographicBufferStatics_Release(cryptoStatics);
-        return;
-    }
-
-    UINT32 olength;
-    unsigned char *rnd = NULL;
-    hr = ICryptographicBufferStatics_CopyToByteArray(cryptoStatics, buffer, &olength, (BYTE**)&rnd);
-    memcpy(buf, rnd, len);
-
-    IBuffer_Release(buffer);
-    ICryptographicBufferStatics_Release(cryptoStatics);
-#else
     HCRYPTPROV hProv;
     /* acquire default encryption context */
     if( CryptAcquireContext(
-- 
2.16.2



More information about the vlc-devel mailing list