bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problems compiling 'getrandom' with MinGW


From: Bruno Haible
Subject: Re: Problems compiling 'getrandom' with MinGW
Date: Sun, 28 Jun 2020 19:19:10 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-179-generic; KDE/5.18.0; x86_64; ; )

Hi Eli,

> 1. It assumes that the header bcrypt.h is always available.  This is
> true for MinGW64, but not for mingw.org's MinGW.  A proposed patch to
> allow the code be compiled without bcrypt.h is below

Thanks for the report and draft patch. I'm applying the patch below.

My patch tests for the presence of <bcrypt.h>. _WIN32_WINNT >= 0x0600
does not guarantee that <bcrypt.h> is present, because someone might,
on a Windows XP machine, compile with -D_WIN32_WINNT=_WIN32_WINNT_WIN7.

> 2. It causes the calling program to be linked against bcrypt.dll if
> that library is available at build time.

No, it links the program against bcrypt.dll if you are compiling with
a _WIN32_WINNT value >= _WIN32_WINNT_WIN7 - consistently with
<https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom>
and
<https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt>.

> This will cause problems if
> the produced program is then copied to a system where bcrypt.dll is
> not available, because the program will refuse to start.
> ...
> In Emacs, I
> needed to set gl_cv_lib_assume_bcrypt=no to avoid producing such
> problematic binaries

You can get into such a scenario if you did not specify _WIN32_WINNT
as documented in
<https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt>.

Specifying values for *_cv_* works too, but is a makeshift. Specifying
_WIN32_WINNT is more future-proof.

> Since the
> code to dynamically load bcrypt.dll is already in getrandom.c, I
> suggest to leave only it, and remove the possibility of linking
> against the DLL

Declined. The code is optimal for both the future and the past versions
of Windows. Your proposal would make it less optimal for all versions
starting with Windows 7.

Bruno


2020-06-28  Bruno Haible  <bruno@clisp.org>

        getrandom: Fix compilation errors on older versions of mingw.
        Reported by Eli Zaretskii <eliz@gnu.org> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2020-06/msg00059.html>.
        * m4/getrandom.m4 (gl_FUNC_GETRANDOM): Test whether <bcrypt.h> exists.
        * lib/getrandom.c: If <bcrypt.h> is not available, include <ntdef.h> and
        define/declare BCRYPT_ALG_HANDLE, BCRYPT_USE_SYSTEM_PREFERRED_RNG,
        BCryptGenRandom ourselves.

diff --git a/lib/getrandom.c b/lib/getrandom.c
index f0b3f53..030a78b 100644
--- a/lib/getrandom.c
+++ b/lib/getrandom.c
@@ -29,7 +29,16 @@
 #if defined _WIN32 && ! defined __CYGWIN__
 # define WIN32_LEAN_AND_MEAN
 # include <windows.h>
-# include <bcrypt.h>
+# if HAVE_BCRYPT_H
+#  include <bcrypt.h>
+# else
+#  include <ntdef.h> /* NTSTATUS */
+typedef void * BCRYPT_ALG_HANDLE;
+#  define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002
+#  if HAVE_LIB_BCRYPT
+extern NTSTATUS WINAPI BCryptGenRandom (BCRYPT_ALG_HANDLE, UCHAR *, ULONG, 
ULONG);
+#  endif
+# endif
 # if !HAVE_LIB_BCRYPT
 #  include <wincrypt.h>
 #  ifndef CRYPT_VERIFY_CONTEXT
diff --git a/m4/getrandom.m4 b/m4/getrandom.m4
index 37fb100..2a0034b 100644
--- a/m4/getrandom.m4
+++ b/m4/getrandom.m4
@@ -1,4 +1,4 @@
-# getrandom.m4 serial 5
+# getrandom.m4 serial 6
 dnl Copyright 2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -36,6 +36,7 @@ AC_DEFUN([gl_FUNC_GETRANDOM],
 
   case "$host_os" in
     mingw*)
+      AC_CHECK_HEADERS([bcrypt.h])
       AC_CACHE_CHECK([whether the bcrypt library is guaranteed to be present],
         [gl_cv_lib_assume_bcrypt],
         [AC_COMPILE_IFELSE(




reply via email to

[Prev in Thread] Current Thread [Next in Thread]