OSDN Git Service

Add support for emulation of Microsoft's rand_s() function.
authorKeith Marshall <keith@users.osdn.me>
Mon, 20 Jul 2020 19:17:27 +0000 (20:17 +0100)
committerKeith Marshall <keith@users.osdn.me>
Mon, 20 Jul 2020 19:17:27 +0000 (20:17 +0100)
VERSION.m4
mingwrt/ChangeLog
mingwrt/Makefile.in
mingwrt/mingwex/cryptnam.c
mingwrt/mingwex/memcrypt.c [new file with mode: 0644]

index 51a3e49..2fd7b24 100644 (file)
@@ -1,6 +1,6 @@
 # VERSION.m4 -- vim: filetype=config
 #
-# $Id: VERSION.m4,v bc6380b6bc4b 2020/07/18 21:59:28 keith $
+# $Id: VERSION.m4,v 5f021e118870 2020/07/20 19:17:27 keith $
 #
 # Non-copyrightable material; adjust the following definition, to
 # assign a common version number to each of the mingwrt and w32api
@@ -23,7 +23,7 @@ m4_define([__VERSION__],[5.4.1])
 #    as required, in accordance with libtool conventions.
 #
 m4_define([MINGW_AC_ENUMERATE_DLLVERSION_SPECS],dnl
-[MINGW_AC_SET_DLLVERSION([LIBMINGWEX], [libmingwex.dll.a], [4:0:0])dnl
+[MINGW_AC_SET_DLLVERSION([LIBMINGWEX], [libmingwex.dll.a], [5:0:1])dnl
 ])
 
 # $RCSfile: VERSION.m4,v $: end of file
index 5bb3460..63e16cb 100644 (file)
@@ -1,3 +1,22 @@
+2020-07-20  Keith Marshall  <keith@users.osdn.me>
+
+       Add support for emulation of Microsoft's rand_s() function.
+
+       * mingwex/cryptnam.c (crypto_provider, crypto_randomize): Factor out
+       this pair of static inline functions; relocate them to...
+       * mingwex/memcrypt.c: ...this new file.
+       (crypto_randomize): Change linkage to extern; rename it as...
+       (__mingw_crypto_randomize): ...this; use it...
+       * mingwex/cryptnam.c (crypto_random_filename_char): ...here, via...
+       (crypto_randomize): ...this static inline reimplementation.
+
+       * Makefile.in (libmingwex.a): Add dependency reference for...
+       (memcrypt.$OBJEXT): ...this new object file.
+
+       * VERSION.m4 (MINGW_AC_SET_DLLVERSION) [LIBMINGWEX]: Increment it
+       to 5:0:1; __mingw_crypto_randomize adds a new interface, but it does
+       not break ABI backward compatibility.
+
 2020-07-08  Keith Marshall  <keith@users.osdn.me>
 
        Prepare and publish MinGW.org WSL-5.4 release.
index 8b5be97..fdc8c03 100644 (file)
@@ -1,5 +1,5 @@
 # @configure_input@
-# $Id$
+# $Id: Makefile.in,v 5f021e118870 2020/07/20 19:17:27 keith $
 #
 # Makefile template for MinGW.org Runtime Library Package
 
@@ -464,7 +464,7 @@ libmingwex.a: $(addsuffix .$(OBJEXT), glob getopt basename dirname nsleep)
 libmingwex.a: $(addsuffix .$(OBJEXT), clockapi clockres clockset clocktime)
 libmingwex.a: $(addsuffix .$(OBJEXT), insque remque tdelete tfind tsearch twalk)
 libmingwex.a: $(addsuffix .$(OBJEXT), dirent wdirent dlfcn strerror_r strtok_r)
-libmingwex.a: $(addsuffix .$(OBJEXT), mkstemp mkdtemp cryptnam setenv)
+libmingwex.a: $(addsuffix .$(OBJEXT), mkstemp mkdtemp memcrypt cryptnam setenv)
 
 vpath %.s ${mingwrt_srcdir}/mingwex
 vpath %.sx ${mingwrt_srcdir}/mingwex
@@ -1028,4 +1028,4 @@ maintainer-clean-local: maintainer-clean-warning distclean-local
 
 clean mostlyclean distclean maintainer-clean: %clean: %clean-local
 
-# $RCSfile$: end of file
+# $RCSfile: Makefile.in,v $: end of file
index 7758518..016e99e 100644 (file)
@@ -7,10 +7,10 @@
  * the file name, or the directory name, in MinGW.org implementations of
  * the mkstemp(3) and mkdtemp(3) functions, respectively.
  *
- * $Id$
+ * $Id: cryptnam.c,v 5f021e118870 2020/07/20 19:17:27 keith $
  *
- * Written by Keith Marshall  <keithmarshall@users.sourceforge.net>
- * Copyright (C) 2013, 2014, 2018, 2019, MinGW.org Project.
+ * Written by Keith Marshall  <keith@users.osdn.me>
+ * Copyright (C) 2013, 2014, 2018-2020, MinGW.org Project.
  *
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * DEALINGS IN THE SOFTWARE.
  *
  */
-#define WIN32_LEAN_AND_MEAN
-
 #include <limits.h>
-#include <windows.h>
-#include <wincrypt.h>
 #include <string.h>
 
 #define CRYPTO_INLINE  static __inline__ __attribute__((__always_inline__))
 
-CRYPTO_INLINE HCRYPTPROV crypto_provider( void )
-#define RSA_MODE( OPT, FLAG )  PROV_RSA_##OPT, CRYPT_##FLAG
-{
-  /* Helper to establish a cryptographic provider context for the
-   * cryptographically secure random number generator.
-   *
-   * At the outset, this provider requires initialization.
-   */
-  static HCRYPTPROV id = (HCRYPTPROV)(0);
-
-  /* On second, and subsequent calls, it should already have been
-   * initialized...
-   */
-  if( id != (HCRYPTPROV)(0) )
-    /*
-     * ...so, simply return the saved context handle...
-     */
-    return id;
-
-  /* If we're still here, this must be the first call, (or any
-   * preceding call failed to initialize the context); initialize
-   * it now, and if successful...
-   */
-  if( CryptAcquireContext( &id, NULL, NULL, RSA_MODE( FULL, VERIFYCONTEXT ) ) )
-    /*
-     * ...return the now-initialized context handle.
-     */
-    return id;
-
-  /* And finally, if we ever get to here, the context remains
-   * uninitialized; ensure that it remains marked as such, and
-   * return the uninitialized context handle.
-   */
-  return id = (HCRYPTPROV)(0);
-}
-
+void *__mingw_crypto_randomize( void *, size_t );
 CRYPTO_INLINE void *crypto_randomize( void *buf, size_t buflen )
-{
-  /* Helper to fill a specified buffer, of specified length,
-   * with cryptographically secure random bytes...
-   */
-  if( CryptGenRandom( crypto_provider(), buflen, buf ) )
-    /*
-     * ...returning a pointer to the buffer, when successful...
-     */
-    return buf;
-
-  /* ...or nothing, otherwise.
-   */
-  return NULL;
-}
+{ return __mingw_crypto_randomize( buf, buflen ); }
 
 CRYPTO_INLINE
 unsigned char *crypto_random_filename_char( unsigned char *caret )
@@ -186,4 +134,4 @@ char *__mingw_crypto_tmpname( char *template )
   return template;
 }
 
-/* $RCSfile$: end of file */
+/* $RCSfile: cryptnam.c,v $: end of file */
diff --git a/mingwrt/mingwex/memcrypt.c b/mingwrt/mingwex/memcrypt.c
new file mode 100644 (file)
index 0000000..4502632
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * memcrypt.c
+ *
+ * Implementation of a cryptographically secure random data generator.
+ * This provides a mechanism for filling a memory buffer, of specified
+ * length, with cryptographically secure randomly generated bytes.
+ *
+ * $Id: memcrypt.c,v 5f021e118870 2020/07/20 19:17:27 keith $
+ *
+ * Written by Keith Marshall  <keith@users.osdn.me>
+ * Copyright (C) 2013, 2014, 2018-2020, MinGW.org Project.
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice, this permission notice, and the following
+ * disclaimer shall be included in all copies or substantial portions of
+ * the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+#define WIN32_LEAN_AND_MEAN
+
+#include <wincrypt.h>
+
+#define CRYPTO_INLINE  static __inline__ __attribute__((__always_inline__))
+
+CRYPTO_INLINE HCRYPTPROV crypto_provider( void )
+#define RSA_MODE( OPT, FLAG )  PROV_RSA_##OPT, CRYPT_##FLAG
+{
+  /* Helper to establish a cryptographic provider context for the
+   * cryptographically secure random number generator.
+   *
+   * At the outset, this provider requires initialization.
+   */
+  static HCRYPTPROV id = (HCRYPTPROV)(0);
+
+  /* On second, and subsequent calls, it should already have been
+   * initialized...
+   */
+  if( id != (HCRYPTPROV)(0) )
+    /*
+     * ...so, simply return the saved context handle...
+     */
+    return id;
+
+  /* If we're still here, this must be the first call, (or any
+   * preceding call failed to initialize the context); initialize
+   * it now, and if successful...
+   */
+  if( CryptAcquireContext( &id, NULL, NULL, RSA_MODE( FULL, VERIFYCONTEXT ) ) )
+    /*
+     * ...return the now-initialized context handle.
+     */
+    return id;
+
+  /* And finally, if we ever get to here, the context remains
+   * uninitialized; ensure that it remains marked as such, and
+   * return the uninitialized context handle.
+   */
+  return id = (HCRYPTPROV)(0);
+}
+
+void *__mingw_crypto_randomize( void *buf, size_t buflen )
+{
+  /* Helper to fill a specified buffer, of specified length,
+   * with cryptographically secure random bytes...
+   */
+  if( CryptGenRandom( crypto_provider(), buflen, buf ) )
+    /*
+     * ...returning a pointer to the buffer, when successful...
+     */
+    return buf;
+
+  /* ...or nothing, otherwise.
+   */
+  return NULL;
+}
+
+/* $RCSfile: memcrypt.c,v $: end of file */