@@ -415,7 +415,13 @@ static void php_mcrypt_module_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{
415
415
}
416
416
}
417
417
/* }}} */
418
-
418
+
419
+ typedef enum {
420
+ RANDOM = 0 ,
421
+ URANDOM ,
422
+ RAND
423
+ } iv_source ;
424
+
419
425
static PHP_MINIT_FUNCTION (mcrypt ) /* {{{ */
420
426
{
421
427
le_mcrypt = zend_register_list_destructors_ex (php_mcrypt_module_dtor , NULL , "mcrypt" , module_number );
@@ -473,6 +479,9 @@ static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
473
479
php_stream_filter_register_factory ("mcrypt.*" , & php_mcrypt_filter_factory TSRMLS_CC );
474
480
php_stream_filter_register_factory ("mdecrypt.*" , & php_mcrypt_filter_factory TSRMLS_CC );
475
481
482
+ MCG (fd [RANDOM ]) = -1 ;
483
+ MCG (fd [URANDOM ]) = -1 ;
484
+
476
485
return SUCCESS ;
477
486
}
478
487
/* }}} */
@@ -536,12 +545,6 @@ PHP_MINFO_FUNCTION(mcrypt) /* {{{ */
536
545
}
537
546
/* }}} */
538
547
539
- typedef enum {
540
- RANDOM = 0 ,
541
- URANDOM ,
542
- RAND
543
- } iv_source ;
544
-
545
548
/* {{{ proto resource mcrypt_module_open(string cipher, string cipher_directory, string mode, string mode_directory)
546
549
Opens the module of the algorithm and the mode to be used */
547
550
PHP_FUNCTION (mcrypt_module_open )
@@ -1403,24 +1406,27 @@ PHP_FUNCTION(mcrypt_create_iv)
1403
1406
}
1404
1407
n = size ;
1405
1408
#else
1406
- int fd ;
1409
+ int * fd = & MCG ( fd [ source ]) ;
1407
1410
size_t read_bytes = 0 ;
1408
1411
1409
- fd = open (source == RANDOM ? "/dev/random" : "/dev/urandom" , O_RDONLY );
1410
- if (fd < 0 ) {
1411
- efree (iv );
1412
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Cannot open source device" );
1413
- RETURN_FALSE ;
1412
+ if (* fd < 0 ) {
1413
+ * fd = open (source == RANDOM ? "/dev/random" : "/dev/urandom" , O_RDONLY );
1414
+ if (* fd < 0 ) {
1415
+ efree (iv );
1416
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Cannot open source device" );
1417
+ RETURN_FALSE ;
1418
+ }
1414
1419
}
1420
+
1415
1421
while (read_bytes < size ) {
1416
- n = read (fd , iv + read_bytes , size - read_bytes );
1422
+ n = read (* fd , iv + read_bytes , size - read_bytes );
1417
1423
if (n < 0 ) {
1418
1424
break ;
1419
1425
}
1420
1426
read_bytes += n ;
1421
1427
}
1422
1428
n = read_bytes ;
1423
- close ( fd );
1429
+
1424
1430
if (n < size ) {
1425
1431
efree (iv );
1426
1432
php_error_docref (NULL TSRMLS_CC , E_WARNING , "Could not gather sufficient random data" );
0 commit comments