]> granicus.if.org Git - php/commitdiff
- MFH: Port mcrypt_create_iv to windows (aka fix it on windows)
authorPierre Joye <pajoye@php.net>
Tue, 15 Jul 2008 17:05:02 +0000 (17:05 +0000)
committerPierre Joye <pajoye@php.net>
Tue, 15 Jul 2008 17:05:02 +0000 (17:05 +0000)
ext/mcrypt/config.w32
ext/mcrypt/mcrypt.c

index da9b54d346e4844fe5449afb38813e5a7d6365c9..8bbab3e0570e4d7e82a21f3591fb159bc249321a 100644 (file)
@@ -6,7 +6,9 @@ ARG_WITH("mcrypt", "mcrypt support", "no");
 if (PHP_MCRYPT != "no") {
 
        if (CHECK_HEADER_ADD_INCLUDE('mcrypt.h', 'CFLAGS_MCRYPT') &&
-                       CHECK_LIB('libmcrypt.lib', 'mcrypt')) {
+                       CHECK_LIB('libmcrypt.lib', 'mcrypt') &&
+                       CHECK_LIB('Advapi32.lib', 'mcrypt')
+                       ) {
                EXTENSION('mcrypt', 'mcrypt.c');
                AC_DEFINE('HAVE_LIBMCRYPT', 1);
                AC_DEFINE('HAVE_LIBMCRYPT24', 1);
@@ -14,4 +16,3 @@ if (PHP_MCRYPT != "no") {
                WARNING("mcrypt not enabled; libraries and headers not found");
        }
 }
-
index 62fa61634ee337d293a1535b089785e85d70ae02..e9534abd38b0144bd6d364634720dc0b0b3831a4 100644 (file)
 
 #if HAVE_LIBMCRYPT
 
+#if PHP_WIN32
+# include <Wincrypt.h>
+# include <Ntsecapi.h>
+#endif
+
 #include "php_mcrypt.h"
 #include "fcntl.h"
 
@@ -1452,6 +1457,23 @@ PHP_FUNCTION(mcrypt_create_iv)
        iv = ecalloc(size + 1, 1);
        
        if (source == RANDOM || source == URANDOM) {
+#if PHP_WIN32
+                       /* random/urandom equivalent on Windows */
+                       HCRYPTPROV     hCryptProv;
+                       BYTE *iv_b = (BYTE *) iv;
+
+                       /* It could be done using LoadLibrary but as we rely on 2k+ for 5.3, cleaner to use a clear dependency (Advapi32) and a 
+                               standard API call (no f=getAddr..; f();) */
+                       if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
+                               php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot open random device");
+                               RETURN_FALSE;
+                       }
+                       if(!CryptGenRandom(hCryptProv, size,  iv_b)) {
+                               php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not gather sufficient random data");
+                               RETURN_FALSE;
+                       }
+                       n = size;
+#else
                int    fd;
                size_t read_bytes = 0;
 
@@ -1475,6 +1497,7 @@ PHP_FUNCTION(mcrypt_create_iv)
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random data");
                        RETURN_FALSE;
                }
+#endif
        } else {
                n = size;
                while (size) {