summaryrefslogtreecommitdiff
path: root/ext/mcrypt/mcrypt.c
diff options
context:
space:
mode:
authorPierre Joye <pajoye@php.net>2008-07-15 17:05:02 +0000
committerPierre Joye <pajoye@php.net>2008-07-15 17:05:02 +0000
commit597d411627b9309d6918a9bde0a48135d82f9876 (patch)
tree27e94de96f666de1f91fae3ee58303702ade848d /ext/mcrypt/mcrypt.c
parenta9d60c014bea718eab1ab813f51a0648a0c4d91f (diff)
downloadphp-git-597d411627b9309d6918a9bde0a48135d82f9876.tar.gz
- MFH: Port mcrypt_create_iv to windows (aka fix it on windows)
Diffstat (limited to 'ext/mcrypt/mcrypt.c')
-rw-r--r--ext/mcrypt/mcrypt.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/ext/mcrypt/mcrypt.c b/ext/mcrypt/mcrypt.c
index 62fa61634e..e9534abd38 100644
--- a/ext/mcrypt/mcrypt.c
+++ b/ext/mcrypt/mcrypt.c
@@ -26,6 +26,11 @@
#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) {