Skip to content

Commit c02c4ac

Browse files
ltJulien Pauli
authored andcommitted
Add file descriptor caching to mcrypt_create_iv()
This improves performance for applications that make repeated calls to mcrypt_create_iv()
1 parent 43c24da commit c02c4ac

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

ext/mcrypt/mcrypt.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,13 @@ static void php_mcrypt_module_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{
415415
}
416416
}
417417
/* }}} */
418-
418+
419+
typedef enum {
420+
RANDOM = 0,
421+
URANDOM,
422+
RAND
423+
} iv_source;
424+
419425
static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
420426
{
421427
le_mcrypt = zend_register_list_destructors_ex(php_mcrypt_module_dtor, NULL, "mcrypt", module_number);
@@ -473,6 +479,9 @@ static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
473479
php_stream_filter_register_factory("mcrypt.*", &php_mcrypt_filter_factory TSRMLS_CC);
474480
php_stream_filter_register_factory("mdecrypt.*", &php_mcrypt_filter_factory TSRMLS_CC);
475481

482+
MCG(fd[RANDOM]) = -1;
483+
MCG(fd[URANDOM]) = -1;
484+
476485
return SUCCESS;
477486
}
478487
/* }}} */
@@ -536,12 +545,6 @@ PHP_MINFO_FUNCTION(mcrypt) /* {{{ */
536545
}
537546
/* }}} */
538547

539-
typedef enum {
540-
RANDOM = 0,
541-
URANDOM,
542-
RAND
543-
} iv_source;
544-
545548
/* {{{ proto resource mcrypt_module_open(string cipher, string cipher_directory, string mode, string mode_directory)
546549
Opens the module of the algorithm and the mode to be used */
547550
PHP_FUNCTION(mcrypt_module_open)
@@ -1403,24 +1406,27 @@ PHP_FUNCTION(mcrypt_create_iv)
14031406
}
14041407
n = size;
14051408
#else
1406-
int fd;
1409+
int *fd = &MCG(fd[source]);
14071410
size_t read_bytes = 0;
14081411

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+
}
14141419
}
1420+
14151421
while (read_bytes < size) {
1416-
n = read(fd, iv + read_bytes, size - read_bytes);
1422+
n = read(*fd, iv + read_bytes, size - read_bytes);
14171423
if (n < 0) {
14181424
break;
14191425
}
14201426
read_bytes += n;
14211427
}
14221428
n = read_bytes;
1423-
close(fd);
1429+
14241430
if (n < size) {
14251431
efree(iv);
14261432
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random data");

ext/mcrypt/php_mcrypt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ ZEND_BEGIN_MODULE_GLOBALS(mcrypt)
7777
int le_h;
7878
char *modes_dir;
7979
char *algorithms_dir;
80+
int fd[2]; // RANDOM = 0, URANDOM = 1
8081
ZEND_END_MODULE_GLOBALS(mcrypt)
8182

8283
#ifdef ZTS

0 commit comments

Comments
 (0)