Skip to content

Commit 2bb0b23

Browse files
author
Derick Rethans
committed
- Fixed bug #43143 (Warning about empty IV with MCRYPT_MODE_ECB).
1 parent 9f50175 commit 2bb0b23

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

ext/mcrypt/mcrypt.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ int php_mcrypt_func(php_mcrypt_op op, char *cipher, char *mode, char *key_str, i
10301030
{
10311031
MCRYPT td;
10321032
char *cipher_dir_string, *module_dir_string, *key_copy, *iv_copy;
1033-
int i, status = SUCCESS, count, *key_sizes, key_size, iv_size, block_size;
1033+
int i, status = SUCCESS, count, *key_sizes, key_size, iv_size, block_size, iv_req;
10341034

10351035
MCRYPT_GET_INI
10361036

@@ -1068,6 +1068,7 @@ int php_mcrypt_func(php_mcrypt_op op, char *cipher, char *mode, char *key_str, i
10681068
mcrypt_free(key_sizes);
10691069

10701070
iv_size = mcrypt_enc_get_iv_size(td);
1071+
iv_req = mcrypt_enc_mode_has_iv(td);
10711072
if (iv_len) {
10721073
if (iv_len == iv_size) {
10731074
iv_copy = estrndup(iv_str, iv_len);
@@ -1077,7 +1078,9 @@ int php_mcrypt_func(php_mcrypt_op op, char *cipher, char *mode, char *key_str, i
10771078
memcpy(iv_copy, iv_str, MIN(iv_len, iv_size));
10781079
}
10791080
} else {
1080-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to use an empty IV, which is NOT recommended");
1081+
if (iv_req) {
1082+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to use an empty IV, which is NOT recommended");
1083+
}
10811084
iv_copy = ecalloc(1, iv_size);
10821085
}
10831086

ext/mcrypt/tests/bug43143.phpt

Lines changed: 22 additions & 0 deletions
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 recommended in %sbug43143.php on line 9
22+
END

0 commit comments

Comments
 (0)