diff mbox

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

Message ID 20180330124452.4800-1-robux4@ycbcr.xyz
State Superseded
Headers show

Commit Message

Steve Lhomme March 30, 2018, 12:44 p.m. UTC
Remove the wincrypt API calls since we don't support XP anymore and bcrypt is
available since Vista, even on Windows Store builds.
---
 configure               |  6 +++---
 libavutil/random_seed.c | 20 ++++++++++----------
 2 files changed, 13 insertions(+), 13 deletions(-)

Comments

Derek Buitenhuis March 31, 2018, 2:46 p.m. UTC | #1
On 3/30/2018 1:44 PM, Steve Lhomme wrote:
> +    if (BCRYPT_SUCCESS(ret)) {
> +        NTSTATUS ret = BCryptGenRandom(algo_handle, &seed, sizeof(seed), 0);
> +        BCryptCloseAlgorithmProvider(algo_handle, 0);
> +        if (BCRYPT_SUCCESS(ret))
>              return seed;
>      }

How does this behave in the case of failure?

- Derek
Michael Niedermayer April 1, 2018, 1:37 a.m. UTC | #2
On Fri, Mar 30, 2018 at 02:44:52PM +0200, Steve Lhomme wrote:
> Remove the wincrypt API calls since we don't support XP anymore and bcrypt is
> available since Vista, even on Windows Store builds.
> ---
>  configure               |  6 +++---
>  libavutil/random_seed.c | 20 ++++++++++----------
>  2 files changed, 13 insertions(+), 13 deletions(-)

fails to build on ubuntu mingw64

CC	libavutil/random_seed.o
In file included from src/libavutil/random_seed.c:30:0:
/usr/share/mingw-w64/include/bcrypt.h:23:3: error: unknown type name ‘LONG’
   typedef LONG NTSTATUS,*PNTSTATUS;
   ^


[...]
Steve Lhomme April 3, 2018, 9:39 a.m. UTC | #3
Le 31/03/2018 à 16:46, Derek Buitenhuis a écrit :
> On 3/30/2018 1:44 PM, Steve Lhomme wrote:
>> +    if (BCRYPT_SUCCESS(ret)) {
>> +        NTSTATUS ret = BCryptGenRandom(algo_handle, &seed, sizeof(seed), 0);
>> +        BCryptCloseAlgorithmProvider(algo_handle, 0);
>> +        if (BCRYPT_SUCCESS(ret))
>>               return seed;
>>       }
> How does this behave in the case of failure?

It falls through the rest of the code until something works, like 
get_generic_seed().

>
> - Derek
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Steve Lhomme April 3, 2018, 9:39 a.m. UTC | #4
Le 01/04/2018 à 03:37, Michael Niedermayer a écrit :
> On Fri, Mar 30, 2018 at 02:44:52PM +0200, Steve Lhomme wrote:
>> Remove the wincrypt API calls since we don't support XP anymore and bcrypt is
>> available since Vista, even on Windows Store builds.
>> ---
>>   configure               |  6 +++---
>>   libavutil/random_seed.c | 20 ++++++++++----------
>>   2 files changed, 13 insertions(+), 13 deletions(-)
> fails to build on ubuntu mingw64
>
> CC	libavutil/random_seed.o
> In file included from src/libavutil/random_seed.c:30:0:
> /usr/share/mingw-w64/include/bcrypt.h:23:3: error: unknown type name ‘LONG’
>     typedef LONG NTSTATUS,*PNTSTATUS;
>     ^

Updated patch incoming.

>
> [...]
>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
diff mbox

Patch

diff --git a/configure b/configure
index 99570a1415..dae52d8f3b 100755
--- a/configure
+++ b/configure
@@ -2141,10 +2141,10 @@  SYSTEM_FUNCS="
 "
 
 SYSTEM_LIBRARIES="
+    bcrypt
     vaapi_drm
     vaapi_x11
     vdpau_x11
-    wincrypt
 "
 
 TOOLCHAIN_FEATURES="
@@ -3442,7 +3442,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"
 postproc_deps="avutil gpl"
 postproc_suggest="libm"
 swresample_deps="avutil"
@@ -5823,9 +5823,9 @@  check_header asm/types.h
 check_builtin stdatomic stdatomic.h "atomic_int foo, bar = ATOMIC_VAR_INIT(-1); atomic_store(&foo, 0)"
 
 check_lib advapi32 "windows.h"            RegCloseKey          -ladvapi32
+check_lib bcrypt   "windows.h bcrypt.h"   BCryptGenRandom      -lbcrypt
 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_lib android android/native_window.h ANativeWindow_acquire -landroid
diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
index 881c23c8c8..ae13a27b8a 100644
--- a/libavutil/random_seed.c
+++ b/libavutil/random_seed.c
@@ -26,9 +26,8 @@ 
 #if HAVE_IO_H
 #include <io.h>
 #endif
-#if HAVE_WINCRYPT
-#include <windows.h>
-#include <wincrypt.h>
+#if HAVE_BCRYPT
+#include <bcrypt.h>
 #endif
 #include <fcntl.h>
 #include <math.h>
@@ -121,13 +120,14 @@  uint32_t av_get_random_seed(void)
 {
     uint32_t seed;
 
-#if HAVE_WINCRYPT
-    HCRYPTPROV provider;
-    if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
-                            CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
-        BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
-        CryptReleaseContext(provider, 0);
-        if (ret)
+#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;
     }
 #endif