-
Notifications
You must be signed in to change notification settings - Fork 7.9k
PHP 5.6 mcrypt_create_iv() improvement #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,13 @@ typedef struct _php_mcrypt { | |
zend_bool init; | ||
} php_mcrypt; | ||
|
||
typedef enum { | ||
RANDOM = 0, | ||
URANDOM, | ||
RAND, | ||
ARANDOM | ||
} iv_source; | ||
|
||
/* {{{ arginfo */ | ||
ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_open, 0, 0, 4) | ||
ZEND_ARG_INFO(0, cipher) | ||
|
@@ -425,9 +432,10 @@ static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */ | |
REGISTER_LONG_CONSTANT("MCRYPT_DECRYPT", 1, CONST_PERSISTENT); | ||
|
||
/* sources for mcrypt_create_iv */ | ||
REGISTER_LONG_CONSTANT("MCRYPT_DEV_RANDOM", 0, CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("MCRYPT_DEV_URANDOM", 1, CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("MCRYPT_RAND", 2, CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("MCRYPT_DEV_RANDOM", RANDOM, CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("MCRYPT_DEV_URANDOM", URANDOM, CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("MCRYPT_RAND", RAND, CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("MCRYPT_DEV_ARANDOM", ARANDOM, CONST_PERSISTENT); | ||
|
||
/* ciphers */ | ||
MCRYPT_ENTRY2_2_4(3DES, "tripledes"); | ||
|
@@ -536,11 +544,6 @@ PHP_MINFO_FUNCTION(mcrypt) /* {{{ */ | |
} | ||
/* }}} */ | ||
|
||
typedef enum { | ||
RANDOM = 0, | ||
URANDOM, | ||
RAND | ||
} iv_source; | ||
|
||
/* {{{ proto resource mcrypt_module_open(string cipher, string cipher_directory, string mode, string mode_directory) | ||
Opens the module of the algorithm and the mode to be used */ | ||
|
@@ -1372,7 +1375,7 @@ PHP_FUNCTION(mcrypt_ofb) | |
Create an initialization vector (IV) */ | ||
PHP_FUNCTION(mcrypt_create_iv) | ||
{ | ||
char *iv; | ||
char *iv, *random_source; | ||
long source = RANDOM; | ||
long size; | ||
int n = 0; | ||
|
@@ -1387,8 +1390,26 @@ PHP_FUNCTION(mcrypt_create_iv) | |
} | ||
|
||
iv = ecalloc(size + 1, 1); | ||
|
||
if (source == RANDOM || source == URANDOM) { | ||
|
||
switch(source) { | ||
case RAND: | ||
random_source = NULL; | ||
break; | ||
case URANDOM: | ||
random_source = "/dev/urandom"; | ||
break; | ||
case ARANDOM: | ||
random_source = "/dev/arandom"; | ||
break; | ||
case RANDOM: | ||
random_source = "/dev/random"; | ||
break; | ||
default: | ||
random_source = "/dev/random"; | ||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid random source specified(%dl)", source); | ||
} | ||
|
||
if (random_source) { | ||
#if PHP_WIN32 | ||
/* random/urandom equivalent on Windows */ | ||
BYTE *iv_b = (BYTE *) iv; | ||
|
@@ -1402,10 +1423,10 @@ PHP_FUNCTION(mcrypt_create_iv) | |
int fd; | ||
size_t read_bytes = 0; | ||
|
||
fd = open(source == RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY); | ||
fd = open(random_source, O_RDONLY); | ||
if (fd < 0) { | ||
efree(iv); | ||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot open source device"); | ||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot open source device(%s)", random_source); | ||
RETURN_FALSE; | ||
} | ||
while (read_bytes < size) { | ||
|
@@ -1428,6 +1449,7 @@ PHP_FUNCTION(mcrypt_create_iv) | |
while (size) { | ||
iv[--size] = (char) (255.0 * php_rand(TSRMLS_C) / RAND_MAX); | ||
} | ||
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "RAND is not safe"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we find this undesirable it should be deprecated, not eternally spit out E_NOTICE errors, |
||
} | ||
RETURN_STRINGL(iv, n, 0); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use a default setting that may exhaust the entropy source? I would opt for either RAND or URANDOM as the fallback, the former giving two notices :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it may exhaust. The only reason why I coded like this is "it was the default".
IV should be unpredictable, but it's not a secret. I would rather make /dev/urandom a default. (Windows uses their own API) Since this is for PHP 5.6, it would better to be changed now.