[FFmpeg-devel] [PATCH] use bcrypt instead of the old wincrypt API

Steve Lhomme robux4 at ycbcr.xyz
Fri Mar 30 10:23:52 EEST 2018


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(-)

diff --git a/configure b/configure
index 99570a1415..6d34728b83 100755
--- a/configure
+++ b/configure
@@ -2144,6 +2144,7 @@ SYSTEM_LIBRARIES="
     vaapi_drm
     vaapi_x11
     vdpau_x11
+    bcrypt
     wincrypt
 "
 
@@ -3442,7 +3443,7 @@ avformat_deps="avcodec avutil"
 avformat_suggest="libm network zlib"
 avresample_deps="avutil"
 avresample_suggest="libm"
-avutil_suggest="clock_gettime ffnvcodec libm libdrm libmfx opencl user32 vaapi videotoolbox corefoundation corevideo coremedia wincrypt"
+avutil_suggest="clock_gettime ffnvcodec libm libdrm libmfx opencl user32 vaapi videotoolbox corefoundation corevideo coremedia bcrypt wincrypt"
 postproc_deps="avutil gpl"
 postproc_suggest="libm"
 swresample_deps="avutil"
@@ -5827,6 +5828,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_lib android android/native_window.h ANativeWindow_acquire -landroid
 check_lib mediandk "stdint.h media/NdkImage.h" AImage_delete -lmediandk
diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
index 881c23c8c8..d7186146d9 100644
--- a/libavutil/random_seed.c
+++ b/libavutil/random_seed.c
@@ -26,7 +26,9 @@
 #if HAVE_IO_H
 #include <io.h>
 #endif
-#if HAVE_WINCRYPT
+#if HAVE_BCRYPT
+#include <bcrypt.h>
+#elif HAVE_WINCRYPT
 #include <windows.h>
 #include <wincrypt.h>
 #endif
@@ -121,7 +123,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)) {
-- 
2.16.2



More information about the ffmpeg-devel mailing list