use bcrypt instead of the old wincrypt API

Message ID 20180330072413.13672-1-robux4@gmail.com
State New
Headers show
Series
  • use bcrypt instead of the old wincrypt API
Related show

Commit Message

Steve Lhomme March 30, 2018, 7:24 a.m.
When targeting Windows Vista and above
The wincrypt API is deprecated and not allowed for Windows Store apps.
---
 configure               |  4 +++-
 libavutil/random_seed.c | 16 ++++++++++++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

Patch

diff --git a/configure b/configure
index 7612a6052c..20064f44d8 100755
--- a/configure
+++ b/configure
@@ -1708,6 +1708,7 @@  SYSTEM_LIBRARIES="
     vaapi_drm
     vaapi_x11
     vdpau_x11
+    bcrypt
     wincrypt
 "
 
@@ -2611,7 +2612,7 @@  avdevice_extralibs="libm_extralibs"
 avformat_extralibs="libm_extralibs"
 avfilter_extralibs="pthreads_extralibs libm_extralibs"
 avresample_extralibs="libm_extralibs"
-avutil_extralibs="clock_gettime_extralibs cuda_extralibs cuvid_extralibs d3d11va_extralibs libm_extralibs libmfx_extralibs nanosleep_extralibs pthreads_extralibs user32_extralibs vaapi_extralibs vaapi_drm_extralibs vaapi_x11_extralibs vdpau_x11_extralibs wincrypt_extralibs"
+avutil_extralibs="clock_gettime_extralibs cuda_extralibs cuvid_extralibs d3d11va_extralibs libm_extralibs libmfx_extralibs nanosleep_extralibs pthreads_extralibs user32_extralibs vaapi_extralibs vaapi_drm_extralibs vaapi_x11_extralibs vdpau_x11_extralibs bcrypt_extralibs wincrypt_extralibs"
 swscale_extralibs="libm_extralibs"
 
 # programs
@@ -4581,6 +4582,7 @@  check_lib ole32    "windows.h"            CoTaskMemFree        -lole32
 check_lib shell32  "windows.h shellapi.h" CommandLineToArgvW   -lshell32
 check_lib wincrypt "windows.h wincrypt.h" CryptGenRandom       -ladvapi32
 check_lib psapi    "windows.h psapi.h"    GetProcessMemoryInfo -lpsapi
+check_cpp_condition windows.h "_WIN32_WINNT >= 0x0600" && check_lib bcrypt "windows.h bcrypt.h" BCryptGenRandom  -lbcrypt
 
 check_struct "sys/time.h sys/resource.h" "struct rusage" ru_maxrss
 
diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
index 089d883916..f9686dfae2 100644
--- a/libavutil/random_seed.c
+++ b/libavutil/random_seed.c
@@ -23,7 +23,9 @@ 
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#if HAVE_WINCRYPT
+#if HAVE_BCRYPT
+#include <bcrypt.h>
+#elif HAVE_WINCRYPT
 #include <windows.h>
 #include <wincrypt.h>
 #endif
@@ -96,7 +98,17 @@  uint32_t av_get_random_seed(void)
 {
     uint32_t seed;
 
-#if HAVE_WINCRYPT
+#if HAVE_BCRYPT
+    BCRYPT_ALG_HANDLE algo_handle;
+    NTSTATUS ret = BCryptOpenAlgorithmProvider(&algo_handle, BCRYPT_RNG_ALGORITHM,
+                                               MS_PRIMITIVE_PROVIDER, 0);
+    if (BCRYPT_SUCCESS(ret)) {
+        NTSTATUS ret = BCryptGenRandom(algo_handle, &seed, sizeof(seed), 0);
+        BCryptCloseAlgorithmProvider(algo_handle, 0);
+        if (BCRYPT_SUCCESS(ret))
+            return seed;
+    }
+#elif HAVE_WINCRYPT
     HCRYPTPROV provider;
     if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
                             CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {