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);
#if HAVE_LIBMCRYPT
+#if PHP_WIN32
+# include <Wincrypt.h>
+# include <Ntsecapi.h>
+#endif
+
#include "php_mcrypt.h"
#include "fcntl.h"
/* {{{ php_mcrypt_iv */
int php_mcrypt_iv(php_mcrypt_iv_source source, int size, char **iv_str, int *iv_len TSRMLS_DC)
{
- int fd, n;
- size_t read_bytes;
-
if (size <= 0 || size >= INT_MAX) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can not create an IV with a size of less then 1 or greater then %d", INT_MAX);
return FAILURE;
switch (source) {
case PHP_MCRYPT_IV_SOURCE_RANDOM:
- case PHP_MCRYPT_IV_SOURCE_URANDOM:
- read_bytes = 0;
-
+ case PHP_MCRYPT_IV_SOURCE_URANDOM: {
+#if PHP_WIN32
+ /* random/urandom equivalent on Windows */
+ HCRYPTPROV hCryptProv;
+ BYTE *iv = (BYTE *) *iv_str;
+
+ /* 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 FAILURE;
+ }
+ if(!CryptGenRandom(hCryptProv, size, iv)) {
+ php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not gather sufficient random data");
+ return FAILURE;
+ }
+ *iv_len = size;
+#else
+ size_t read_bytes = 0;
+ int fd;
+
fd = open(source == PHP_MCRYPT_IV_SOURCE_RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY);
if (fd < 0) {
efree(*iv_str);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random data");
return FAILURE;
}
+#endif
break;
+ }
case PHP_MCRYPT_IV_SOURCE_RAND:
*iv_len = size;
while (size) {