Skip to content

Commit fd73296

Browse files
author
Derick Rethans
committed
- MFH: Fixed bug #43143 (Warning about empty IV with MCRYPT_MODE_ECB).
1 parent f5b1ee4 commit fd73296

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ PHP NEWS
5959

6060
- Fixed possible crash in ext/soap because of uninitialized value. (Zdash Urf)
6161

62+
- Fixed bug #43143 (Warning about empty IV with MCRYPT_MODE_ECB). (Derick)
6263
- Fixed bug #43136 (possible crash on script execution timeout.
6364
The EG(function_state_ptr) is completely removed,
6465
EG(current_execute_data)->function_state must be used instead). (Dmitry)

ext/mcrypt/mcrypt.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ static void php_mcrypt_do_crypt (char* cipher, zval **key, zval **data, char *mo
993993
{
994994
char *cipher_dir_string;
995995
char *module_dir_string;
996-
int block_size, max_key_length, use_key_length, i, count, iv_size;
996+
int block_size, max_key_length, use_key_length, i, count, iv_size, req_iv;
997997
unsigned long int data_size;
998998
int *key_length_sizes;
999999
char *key_s = NULL, *iv_s;
@@ -1041,6 +1041,7 @@ static void php_mcrypt_do_crypt (char* cipher, zval **key, zval **data, char *mo
10411041
/* Check IV */
10421042
iv_s = NULL;
10431043
iv_size = mcrypt_enc_get_iv_size (td);
1044+
req_iv = mcrypt_enc_mode_has_iv(td);
10441045
if (argc == 5) {
10451046
if (iv_size != Z_STRLEN_PP(iv)) {
10461047
php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_IV_WRONG_SIZE);
@@ -1049,7 +1050,7 @@ static void php_mcrypt_do_crypt (char* cipher, zval **key, zval **data, char *mo
10491050
memcpy(iv_s, Z_STRVAL_PP(iv), iv_size);
10501051
}
10511052
} else if (argc == 4) {
1052-
if (iv_size != 0) {
1053+
if (req_iv == 1) {
10531054
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to use an empty IV, which is NOT recommend");
10541055
iv_s = emalloc(iv_size + 1);
10551056
memset(iv_s, 0, iv_size + 1);

ext/mcrypt/tests/bug43143.phpt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Bug #43143 (Warning about empty IV with MCRYPT_MODE_ECB)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("mcrypt")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
echo "ECB\n";
8+
$input = 'to be encrypted';
9+
$mkey = hash('sha256', 'secret key', TRUE);
10+
$data = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mkey, $input, MCRYPT_MODE_ECB);
11+
echo "CFB\n";
12+
$input = 'to be encrypted';
13+
$mkey = hash('sha256', 'secret key', TRUE);
14+
$data = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mkey, $input, MCRYPT_MODE_CFB);
15+
echo "END\n";
16+
?>
17+
--EXPECTF--
18+
ECB
19+
CFB
20+
21+
Warning: mcrypt_encrypt(): Attempt to use an empty IV, which is NOT recommend in %sbug43143.php on line 9
22+
END

0 commit comments

Comments
 (0)