[vlc-commits] rand: use bcrypt instead of CryptographicBufferStatics for Winstore

Steve Lhomme git at videolan.org
Wed Mar 28 15:01:39 CEST 2018


vlc | branch: master | Steve Lhomme <robux4 at ycbcr.xyz> | Wed Mar 28 14:49:19 2018 +0200| [12aab7fe8ec76a7eaf7d051762f8d83dfc262b3c] | committer: Steve Lhomme

rand: use bcrypt instead of CryptographicBufferStatics for Winstore

It's available to winstore apps and on desktop since Vista.
The old API is deprecated.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=12aab7fe8ec76a7eaf7d051762f8d83dfc262b3c
---

 configure.ac     |  5 ++++-
 src/win32/rand.c | 50 +++++++++++---------------------------------------
 2 files changed, 15 insertions(+), 40 deletions(-)

diff --git a/configure.ac b/configure.ac
index e1a4800ccb..734a2e2281 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1137,7 +1137,10 @@ AS_IF([test "${enable_ssp}" != "no" -a "${enable_optimizations}" != "no"], [
   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"])
       AC_CACHE_CHECK([if linker supports stack protectors], [ac_cv_ld_ssp], [
         AC_TRY_LINK([#include <stdio.h>],[char buf[100]; fgets(buf, sizeof(buf), stdin);], [
           ac_cv_ld_ssp="yes"
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(



More information about the vlc-commits mailing list