Skip to content

Commit 25d801f

Browse files
committed
Abort on missing IV if the enc_mode requires it
Previously the code fell back on using a NUL IV if no IV was passed and the encryption mode required it. This is dangerous and makes no sense from a practical point of view (as you could just as well use ECB then).
1 parent c4b7cdb commit 25d801f

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

ext/mcrypt/mcrypt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,9 +1230,9 @@ static void php_mcrypt_do_crypt(char* cipher, const char *key, int key_len, cons
12301230
memcpy(iv_s, iv, iv_size);
12311231
}
12321232
} else if (argc == 4) {
1233-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to use an empty IV, which is NOT recommend");
1234-
iv_s = emalloc(iv_size + 1);
1235-
memset(iv_s, 0, iv_size + 1);
1233+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Encryption mode requires an initialization vector");
1234+
efree(key_s);
1235+
RETURN_FALSE;
12361236
}
12371237
}
12381238

ext/mcrypt/tests/mcrypt_cbc.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $enc_data = mcrypt_cbc($cipher, $key, $secret, MCRYPT_ENCRYPT, $iv);
1515
echo trim(mcrypt_cbc($cipher, $key, $enc_data, MCRYPT_DECRYPT, $iv)) . "\n";
1616

1717
// a warning must be issued if we don't use a IV on a AES cipher, that usually requires an IV
18-
mcrypt_cbc($cipher, $key, $enc_data, MCRYPT_DECRYPT);
18+
var_dump(mcrypt_cbc($cipher, $key, $enc_data, MCRYPT_DECRYPT));
1919

2020
--EXPECTF--
2121

@@ -26,4 +26,5 @@ PHP Testfest 2008
2626

2727
Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
2828

29-
Warning: mcrypt_cbc(): Attempt to use an empty IV, which is NOT recommend in %s on line %d
29+
Warning: mcrypt_cbc(): Encryption mode requires an initialization vector in %s on line %d
30+
bool(false)

ext/mcrypt/tests/mcrypt_cfb.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $enc_data = mcrypt_cfb($cipher, $key, $secret, MCRYPT_ENCRYPT, $iv);
1515
echo trim(mcrypt_cfb($cipher, $key, $enc_data, MCRYPT_DECRYPT, $iv)) . "\n";
1616

1717
// a warning must be issued if we don't use a IV on a AES cipher, that usually requires an IV
18-
mcrypt_cfb($cipher, $key, $enc_data, MCRYPT_DECRYPT);
18+
var_dump(mcrypt_cfb($cipher, $key, $enc_data, MCRYPT_DECRYPT));
1919

2020
--EXPECTF--
2121

@@ -26,4 +26,5 @@ PHP Testfest 2008
2626

2727
Deprecated: Function mcrypt_cfb() is deprecated in %s on line %d
2828

29-
Warning: mcrypt_cfb(): Attempt to use an empty IV, which is NOT recommend in %s on line %d
29+
Warning: mcrypt_cfb(): Encryption mode requires an initialization vector in %s on line %d
30+
bool(false)

ext/mcrypt/tests/mcrypt_decrypt.phpt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ $enc_data = mcrypt_encrypt($cipher, $key, $secret, $mode, $iv);
1616
echo trim(mcrypt_decrypt($cipher, $key, $enc_data, $mode, $iv)) . "\n";
1717

1818
// a warning must be issued if we don't use a IV on a AES cipher, that usually requires an IV
19-
mcrypt_decrypt($cipher, $key, $enc_data, MCRYPT_MODE_CBC);
19+
var_dump(mcrypt_decrypt($cipher, $key, $enc_data, MCRYPT_MODE_CBC));
2020

21-
var_dump(strpos(mcrypt_decrypt(MCRYPT_BLOWFISH, "FooBar", $enc_data, MCRYPT_MODE_CBC, $iv), "Testfest") !== false);
21+
var_dump(mcrypt_decrypt(MCRYPT_BLOWFISH, "FooBar", $enc_data, MCRYPT_MODE_CBC, $iv));
2222
--EXPECTF--
2323
PHP Testfest 2008
2424

25-
Warning: mcrypt_decrypt(): Attempt to use an empty IV, which is NOT recommend in %s on line %d
25+
Warning: mcrypt_decrypt(): Encryption mode requires an initialization vector in %s on line %d
26+
bool(false)
2627

2728
Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d
28-
bool(false)
29+
bool(false)

0 commit comments

Comments
 (0)