]> granicus.if.org Git - php/commitdiff
- Fixed bug #46010 (warnings incorrectly generated for iv in ecb mode)
authorFelipe Pena <felipe@php.net>
Sun, 7 Sep 2008 22:53:20 +0000 (22:53 +0000)
committerFelipe Pena <felipe@php.net>
Sun, 7 Sep 2008 22:53:20 +0000 (22:53 +0000)
ext/mcrypt/mcrypt.c
ext/mcrypt/tests/bug46010.phpt [new file with mode: 0644]

index c317607bc42f7c3b8e91d9c2234f27c627cc4b0b..727057eff88ad52746782cd96c8f853356a41e17 100644 (file)
@@ -1276,7 +1276,7 @@ int php_mcrypt_func(php_mcrypt_op op, char *cipher, char *mode, char *key_str, i
 {
        MCRYPT td;
        char *cipher_dir_string, *module_dir_string, *key_copy, *iv_copy;
-       int i, status = SUCCESS, count, *key_sizes, key_size, iv_size, block_size, iv_req;
+       int i, status = SUCCESS, count, *key_sizes, key_size, iv_size, block_size;
        
        MCRYPT_GET_INI
        
@@ -1314,20 +1314,20 @@ int php_mcrypt_func(php_mcrypt_op op, char *cipher, char *mode, char *key_str, i
        mcrypt_free(key_sizes);
        
        iv_size = mcrypt_enc_get_iv_size(td);
-       iv_req = mcrypt_enc_mode_has_iv(td);
-       if (iv_len) {
-               if (iv_len == iv_size) {
-                       iv_copy = estrndup(iv_str, iv_len);
+       /* IV is required */
+       if (mcrypt_enc_mode_has_iv(td) == 1) {
+               if (iv_len) {
+                       if (iv_len == iv_size) {
+                               iv_copy = estrndup(iv_str, iv_len);
+                       } else {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_IV_WRONG_SIZE);
+                               iv_copy = ecalloc(1, iv_size);
+                               memcpy(iv_copy, iv_str, MIN(iv_len, iv_size));
+                       }
                } else {
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_IV_WRONG_SIZE);
-                       iv_copy = ecalloc(1, iv_size);
-                       memcpy(iv_copy, iv_str, MIN(iv_len, iv_size));
-               }
-       } else {
-               if (iv_req) {
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to use an empty IV, which is NOT recommended");
+                       iv_copy = ecalloc(1, iv_size);
                }
-               iv_copy = ecalloc(1, iv_size);
        }
        
        if (mcrypt_enc_is_block_mode(td) == 1) {
diff --git a/ext/mcrypt/tests/bug46010.phpt b/ext/mcrypt/tests/bug46010.phpt
new file mode 100644 (file)
index 0000000..5aeb311
--- /dev/null
@@ -0,0 +1,14 @@
+--TEST---
+Bug #46010 (warnings incorrectly generated for iv in ecb mode)
+--FILE--
+<?php
+
+var_dump(bin2hex(mcrypt_encrypt(MCRYPT_TRIPLEDES, "key", "data", MCRYPT_MODE_ECB)));
+var_dump(bin2hex(mcrypt_encrypt(MCRYPT_TRIPLEDES, "key", "data", MCRYPT_MODE_ECB, "a")));
+var_dump(bin2hex(mcrypt_encrypt(MCRYPT_TRIPLEDES, "key", "data", MCRYPT_MODE_ECB, "12345678")));
+
+?>
+--EXPECTF--
+string(16) "372eeb4a524b8d31"
+string(16) "372eeb4a524b8d31"
+string(16) "372eeb4a524b8d31"