├── CREDITS ├── .gitattributes ├── TODO ├── tests ├── mcrypt_list_algorithms.phpt ├── mcrypt_enc_self_test.phpt ├── bug8040.phpt ├── mcrypt_list_modes.phpt ├── mcrypt_module_open.phpt ├── bug41252.phpt ├── mcrypt_enc_get_supported_key_sizes.phpt ├── mcrypt_module_self_test.phpt ├── mcrypt_get_key_size.phpt ├── bug70625.phpt ├── mcrypt_get_block_size.phpt ├── mcrypt_module_get_supported_key_sizes.phpt ├── bug35496.phpt ├── mcrypt_get_cipher_name.phpt ├── bug46010.phpt ├── bug49738.phpt ├── bug43143.phpt ├── mcrypt_get_iv_size.phpt ├── mcrypt_module_is_block_algorithm.phpt ├── mcrypt_module_is_block_algorithm_mode.phpt ├── mcrypt_module_is_block_mode.phpt ├── mcrypt_create_iv.phpt ├── mcrypt_enc_get_iv_size.phpt ├── mcrypt_enc_get_key_size.phpt ├── mcrypt_enc_get_block_size.phpt ├── mcrypt_ecb.phpt ├── mcrypt_enc_is_block_algorithm.phpt ├── mcrypt_ofb.phpt ├── mcrypt_enc_is_block_algorithm_mode.phpt ├── mcrypt_filters.phpt ├── mcrypt_cfb.phpt ├── mcrypt_cbc.phpt ├── mcrypt_module_get_algo_key_size.phpt ├── mcrypt_module_get_algo_block_size.phpt ├── bug55169.phpt ├── mcrypt_enc_is_block_mode.phpt ├── mcrypt_decrypt.phpt ├── mcrypt_enc_get_algorithms_name.phpt ├── vectors.txt ├── mcrypt_enc_get_mode_name.phpt ├── mcrypt_ecb_3des_encrypt.phpt ├── mcrypt_cbc_3des_encrypt.phpt ├── mcrypt_encrypt_3des_ecb.phpt ├── mcrypt_ecb_3des_decrypt.phpt ├── mcrypt_decrypt_3des_ecb.phpt ├── bug37595.phpt ├── mcrypt_cbc_3des_decrypt.phpt ├── mcrypt_encrypt_3des_cbc.phpt ├── mcrypt_decrypt_3des_cbc.phpt ├── mcrypt_rijndael128_128BitKey.phpt ├── mcrypt_rijndael128_256BitKey.phpt └── blowfish.phpt ├── .gitignore ├── composer.json ├── .travis.yml ├── config.w32 ├── php_mcrypt_filter.h ├── config.m4 ├── .github └── workflows │ └── build.yml ├── LICENSE ├── php_mcrypt.h ├── package.xml ├── mcrypt_filter.c └── mcrypt.c /CREDITS: -------------------------------------------------------------------------------- 1 | mcrypt 2 | Sascha Schumann, Derick Rethans 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Treat binary PHP test files as text when displaying diffs 2 | *.phpt diff 3 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | ToDo: 2 | - Convert to zend_parse_parameters 3 | - Unify error handling 4 | - Implement encryption streams 5 | -------------------------------------------------------------------------------- /tests/mcrypt_list_algorithms.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_list_algorithms 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | = 7.2.0, <= 8.6.0" 8 | }, 9 | "php-ext": { 10 | "priority": 20, 11 | "extension-name": "mcrypt", 12 | "configure-options": [ 13 | { 14 | "name": "with-mcrypt", 15 | "description": "Include mcrypt support", 16 | "needs-value": true 17 | } 18 | ] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/bug8040.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Bug #8040 (MCRYPT_MODE_* do not seem to exist) 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 19 | --EXPECTF-- 20 | twofish 21 | cbc 22 | cbc 23 | cipher=twofish mode1=cbc 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | dist: bionic 3 | php: 4 | - 7.2 5 | - 7.3 6 | - 7.4 7 | - nightly 8 | sudo: false 9 | matrix: 10 | fast_finish: true 11 | env: 12 | - REPORT_EXIT_STATUS=1 NO_INTERACTION=1 13 | addons: 14 | apt: 15 | packages: 16 | - libmcrypt-dev 17 | update: true 18 | install: 19 | - phpize 20 | - ./configure 21 | - make 22 | script: TEST_PHP_EXECUTABLE=$(which php) php -n 23 | -d open_basedir= -d output_buffering=0 -d memory_limit=-1 24 | run-tests.php -n 25 | -d extension_dir=modules -d extension=mcrypt.so --show-diff 26 | tests 27 | -------------------------------------------------------------------------------- /tests/mcrypt_list_modes.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_list_modes 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 12 | string(3) "cbc" 13 | [1]=> 14 | string(3) "cfb" 15 | [2]=> 16 | string(3) "ctr" 17 | [3]=> 18 | string(3) "ecb" 19 | [4]=> 20 | string(4) "ncfb" 21 | [5]=> 22 | string(4) "nofb" 23 | [6]=> 24 | string(3) "ofb" 25 | [7]=> 26 | string(6) "stream" 27 | } -------------------------------------------------------------------------------- /tests/mcrypt_module_open.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_module_open 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 11 | --EXPECTF-- 12 | Deprecated: Function mcrypt_module_open() is deprecated in %s%ebug41252.php on line 2 13 | 14 | Deprecated: Function mcrypt_generic() is deprecated in %s%ebug41252.php on line 3 15 | 16 | Warning: mcrypt_generic(): Operation disallowed prior to mcrypt_generic_init(). in %sbug41252.php on line 3 17 | I'm alive! 18 | -------------------------------------------------------------------------------- /tests/mcrypt_enc_get_supported_key_sizes.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_enc_get_supported_key_sizes 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 16 | int(16) 17 | [1]=> 18 | int(24) 19 | [2]=> 20 | int(32) 21 | } -------------------------------------------------------------------------------- /config.w32: -------------------------------------------------------------------------------- 1 | // vim:ft=javascript 2 | 3 | ARG_WITH("mcrypt", "mcrypt support", "no"); 4 | 5 | if (PHP_MCRYPT != "no") { 6 | 7 | if (CHECK_HEADER_ADD_INCLUDE('mcrypt.h', 'CFLAGS_MCRYPT') && 8 | CHECK_HEADER_ADD_INCLUDE('dirent.h', 'CFLAGS_MCRYPT') && 9 | CHECK_LIB('libmcrypt_a.lib;libmcrypt.lib', 'mcrypt') && 10 | CHECK_LIB('dirent_a.lib', 'mcrypt') && 11 | CHECK_LIB('Advapi32.lib', 'mcrypt') 12 | ) { 13 | 14 | EXTENSION('mcrypt', 'mcrypt.c mcrypt_filter.c', PHP_MCRYPT_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); 15 | AC_DEFINE('HAVE_LIBMCRYPT', 1); 16 | AC_DEFINE('HAVE_LIBMCRYPT24', 1); 17 | } else { 18 | WARNING("mcrypt not enabled; libraries and headers not found"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/mcrypt_module_self_test.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_module_self_test 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 15 | --EXPECTF-- 16 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%ebug70625.php on line 4 17 | string(14) "d5c9a57023d0f1" 18 | 19 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%ebug70625.php on line 6 20 | string(7) "payload" 21 | -------------------------------------------------------------------------------- /tests/mcrypt_get_block_size.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_get_block_size 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 13 | int(16) 14 | [1]=> 15 | int(24) 16 | [2]=> 17 | int(32) 18 | } 19 | 20 | Deprecated: Function mcrypt_module_get_supported_key_sizes() is deprecated in %s%emcrypt_module_get_supported_key_sizes.php on line 3 21 | array(0) { 22 | } -------------------------------------------------------------------------------- /tests/bug35496.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Bug #35496 (Crash in mcrypt_generic()/mdecrypt_generic() without proper init). 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 11 | --EXPECTF-- 12 | Deprecated: Function mcrypt_module_open() is deprecated in %s%ebug35496.php on line 2 13 | 14 | Deprecated: Function mcrypt_generic() is deprecated in %s%ebug35496.php on line 3 15 | 16 | Warning: mcrypt_generic(): Operation disallowed prior to mcrypt_generic_init(). in %sbug35496.php on line 3 17 | 18 | Deprecated: Function mdecrypt_generic() is deprecated in %s%ebug35496.php on line 4 19 | 20 | Warning: mdecrypt_generic(): Operation disallowed prior to mcrypt_generic_init(). in %sbug35496.php on line 4 21 | -------------------------------------------------------------------------------- /tests/mcrypt_get_cipher_name.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_get_cipher_name 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 14 | --EXPECTF-- 15 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%ebug46010.php on line 4 16 | string(16) "f7a2ce11d4002294" 17 | 18 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%ebug46010.php on line 5 19 | string(16) "f7a2ce11d4002294" 20 | 21 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%ebug46010.php on line 6 22 | string(16) "f7a2ce11d4002294" 23 | -------------------------------------------------------------------------------- /tests/bug49738.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Bug #49738 (calling mcrypt after mcrypt_generic_deinit crashes) 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 12 | --EXPECTF-- 13 | Deprecated: Function mcrypt_module_open() is deprecated in %s%ebug49738.php on line 2 14 | 15 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug49738.php on line 3 16 | 17 | Deprecated: Function mcrypt_generic_deinit() is deprecated in %s%ebug49738.php on line 4 18 | 19 | Deprecated: Function mcrypt_generic() is deprecated in %s%ebug49738.php on line 5 20 | 21 | Warning: mcrypt_generic(): Operation disallowed prior to mcrypt_generic_init(). in %sbug49738.php on line 5 22 | -------------------------------------------------------------------------------- /tests/bug43143.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Bug #43143 (Warning about empty IV with MCRYPT_MODE_ECB) 3 | --SKIPIF-- 4 | 6 | --FILE-- 7 | 18 | --EXPECTF-- 19 | ECB 20 | 21 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%ebug43143.php on line 5 22 | CFB 23 | 24 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%ebug43143.php on line 9 25 | 26 | Warning: mcrypt_encrypt(): Encryption mode requires an initialization vector of size 32 in %sbug43143.php on line 9 27 | END 28 | -------------------------------------------------------------------------------- /tests/mcrypt_get_iv_size.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_enc_get_iv_size 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | $iv, 'key'=>$key); 20 | 21 | $fp = fopen($secretfile, 'wb'); 22 | stream_filter_append($fp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts); 23 | fwrite($fp, 'Secret secret secret data'); 24 | fclose($fp); 25 | 26 | echo md5_file($secretfile)."\n"; 27 | 28 | $fp = fopen($secretfile, 'rb'); 29 | stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_READ, $opts); 30 | $data = stream_get_contents($fp); 31 | fclose($fp); 32 | 33 | echo $data."\n"; 34 | 35 | @unlink($secretfile); 36 | 37 | --EXPECTF-- 38 | FOUND 39 | FOUND 40 | 41 | Deprecated: stream_filter_append(): mcrypt and mdecrypt stream filters have been deprecated in %s%emcrypt_filters.php on line 17 42 | 32e14bd3c31f2bd666e4290ebdb166a7 43 | 44 | Deprecated: stream_filter_append(): mcrypt and mdecrypt stream filters have been deprecated in %s%emcrypt_filters.php on line 24 45 | Secret secret secret data -------------------------------------------------------------------------------- /tests/mcrypt_cfb.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_cfb 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 21 | --EXPECTF-- 22 | Deprecated: Function mcrypt_get_iv_size() is deprecated in %s%emcrypt_cbc.php on line 6 23 | 24 | Deprecated: Function mcrypt_create_iv() is deprecated in %s%emcrypt_cbc.php on line 6 25 | 26 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc.php on line 7 27 | 28 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc.php on line 10 29 | PHP Testfest 2008 30 | 31 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc.php on line 13 32 | 33 | Warning: mcrypt_decrypt(): Encryption mode requires an initialization vector of size 16 in %s on line %d 34 | bool(false) 35 | -------------------------------------------------------------------------------- /tests/mcrypt_module_get_algo_key_size.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_module_get_algo_key_size 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 14 | --EXPECTF-- 15 | Deprecated: Function mcrypt_module_get_algo_key_size() is deprecated in %s%emcrypt_module_get_algo_key_size.php on line 2 16 | int(32) 17 | 18 | Deprecated: Function mcrypt_module_get_algo_key_size() is deprecated in %s%emcrypt_module_get_algo_key_size.php on line 3 19 | int(32) 20 | 21 | Deprecated: Function mcrypt_module_get_algo_key_size() is deprecated in %s%emcrypt_module_get_algo_key_size.php on line 4 22 | int(128) 23 | 24 | Deprecated: Function mcrypt_module_get_algo_key_size() is deprecated in %s%emcrypt_module_get_algo_key_size.php on line 5 25 | int(16) 26 | 27 | Deprecated: Function mcrypt_module_get_algo_key_size() is deprecated in %s%emcrypt_module_get_algo_key_size.php on line 6 28 | int(32) 29 | 30 | Deprecated: Function mcrypt_module_get_algo_key_size() is deprecated in %s%emcrypt_module_get_algo_key_size.php on line 7 31 | int(56) 32 | -------------------------------------------------------------------------------- /tests/mcrypt_module_get_algo_block_size.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_module_get_algo_block_size 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 14 | --EXPECTF-- 15 | Deprecated: Function mcrypt_module_get_algo_block_size() is deprecated in %s%emcrypt_module_get_algo_block_size.php on line 2 16 | int(32) 17 | 18 | Deprecated: Function mcrypt_module_get_algo_block_size() is deprecated in %s%emcrypt_module_get_algo_block_size.php on line 3 19 | int(24) 20 | 21 | Deprecated: Function mcrypt_module_get_algo_block_size() is deprecated in %s%emcrypt_module_get_algo_block_size.php on line 4 22 | int(8) 23 | 24 | Deprecated: Function mcrypt_module_get_algo_block_size() is deprecated in %s%emcrypt_module_get_algo_block_size.php on line 5 25 | int(8) 26 | 27 | Deprecated: Function mcrypt_module_get_algo_block_size() is deprecated in %s%emcrypt_module_get_algo_block_size.php on line 6 28 | int(16) 29 | 30 | Deprecated: Function mcrypt_module_get_algo_block_size() is deprecated in %s%emcrypt_module_get_algo_block_size.php on line 7 31 | int(8) 32 | -------------------------------------------------------------------------------- /tests/bug55169.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_create_iv https://bugs.php.net/bug.php?id=55169 3 | --CREDITS-- 4 | Ryan Biesemeyer 5 | --SKIPIF-- 6 | 7 | --FILE-- 8 | 17 | --EXPECTF-- 18 | Input: 1 19 | 20 | Deprecated: Function mcrypt_create_iv() is deprecated in %s%ebug55169.php on line 4 21 | Length: 1 22 | Hex: %x 23 | 24 | Input: 2 25 | 26 | Deprecated: Function mcrypt_create_iv() is deprecated in %s%ebug55169.php on line 4 27 | Length: 2 28 | Hex: %x 29 | 30 | Input: 4 31 | 32 | Deprecated: Function mcrypt_create_iv() is deprecated in %s%ebug55169.php on line 4 33 | Length: 4 34 | Hex: %x 35 | 36 | Input: 8 37 | 38 | Deprecated: Function mcrypt_create_iv() is deprecated in %s%ebug55169.php on line 4 39 | Length: 8 40 | Hex: %x 41 | 42 | Input: 16 43 | 44 | Deprecated: Function mcrypt_create_iv() is deprecated in %s%ebug55169.php on line 4 45 | Length: 16 46 | Hex: %x 47 | 48 | Input: 32 49 | 50 | Deprecated: Function mcrypt_create_iv() is deprecated in %s%ebug55169.php on line 4 51 | Length: 32 52 | Hex: %x 53 | 54 | Input: 64 55 | 56 | Deprecated: Function mcrypt_create_iv() is deprecated in %s%ebug55169.php on line 4 57 | Length: 64 58 | Hex: %x 59 | -------------------------------------------------------------------------------- /tests/mcrypt_enc_is_block_mode.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_enc_is_block_mode 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | | 16 | +----------------------------------------------------------------------+ 17 | */ 18 | 19 | #ifndef PHP_MCRYPT_FILTER_H 20 | #define PHP_MCRYPT_FILTER_H 21 | 22 | extern php_stream_filter_factory php_mcrypt_filter_factory; 23 | 24 | PHP_MINIT_FUNCTION(mcrypt_filter); 25 | PHP_MSHUTDOWN_FUNCTION(mcrypt_filter); 26 | PHP_MINFO_FUNCTION(mcrypt_filter); 27 | 28 | #endif /* PHP_MCRYPT_FILTER_H */ 29 | 30 | 31 | /* 32 | * Local variables: 33 | * tab-width: 4 34 | * c-basic-offset: 4 35 | * indent-tabs-mode: t 36 | * End: 37 | */ 38 | -------------------------------------------------------------------------------- /config.m4: -------------------------------------------------------------------------------- 1 | AC_DEFUN([PHP_MCRYPT_CHECK_VERSION],[ 2 | old_CPPFLAGS=$CPPFLAGS 3 | CPPFLAGS=-I$MCRYPT_DIR/include 4 | AC_MSG_CHECKING(for libmcrypt version) 5 | AC_EGREP_CPP(yes,[ 6 | #include 7 | #if MCRYPT_API_VERSION >= 20021217 8 | yes 9 | #endif 10 | ],[ 11 | AC_MSG_RESULT(>= 2.5.6) 12 | ],[ 13 | AC_MSG_ERROR(libmcrypt version 2.5.6 or greater required.) 14 | ]) 15 | CPPFLAGS=$old_CPPFLAGS 16 | ]) 17 | 18 | 19 | PHP_ARG_WITH(mcrypt, for mcrypt support, 20 | [ --with-mcrypt[=DIR] Include mcrypt support]) 21 | 22 | if test "$PHP_MCRYPT" != "no"; then 23 | for i in $PHP_MCRYPT /usr/local /usr; do 24 | test -f $i/include/mcrypt.h && MCRYPT_DIR=$i && break 25 | done 26 | 27 | if test -z "$MCRYPT_DIR"; then 28 | AC_MSG_ERROR(mcrypt.h not found. Please reinstall libmcrypt.) 29 | fi 30 | 31 | PHP_MCRYPT_CHECK_VERSION 32 | 33 | PHP_CHECK_LIBRARY(mcrypt, mcrypt_module_open, 34 | [ 35 | PHP_ADD_LIBRARY(ltdl,, MCRYPT_SHARED_LIBADD) 36 | AC_DEFINE(HAVE_LIBMCRYPT,1,[ ]) 37 | ],[ 38 | PHP_CHECK_LIBRARY(mcrypt, mcrypt_module_open, 39 | [ 40 | AC_DEFINE(HAVE_LIBMCRYPT,1,[ ]) 41 | ],[ 42 | AC_MSG_ERROR([Sorry, I was not able to diagnose which libmcrypt version you have installed.]) 43 | ],[ 44 | -L$MCRYPT_DIR/$PHP_LIBDIR 45 | ]) 46 | ],[ 47 | -L$MCRYPT_DIR/$PHP_LIBDIR -lltdl 48 | ]) 49 | 50 | PHP_ADD_LIBRARY_WITH_PATH(mcrypt, $MCRYPT_DIR/$PHP_LIBDIR, MCRYPT_SHARED_LIBADD) 51 | PHP_ADD_INCLUDE($MCRYPT_DIR/include) 52 | 53 | PHP_SUBST(MCRYPT_SHARED_LIBADD) 54 | PHP_NEW_EXTENSION(mcrypt, mcrypt.c mcrypt_filter.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) 55 | fi 56 | -------------------------------------------------------------------------------- /tests/mcrypt_decrypt.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | mcrypt_decrypt 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | 5 | --FILE-- 6 | ". 43 | 44 | THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND 45 | ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 46 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 47 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PHP 48 | DEVELOPMENT TEAM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 49 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 50 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 51 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 53 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 54 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 55 | OF THE POSSIBILITY OF SUCH DAMAGE. 56 | 57 | -------------------------------------------------------------------- 58 | 59 | This software consists of voluntary contributions made by many 60 | individuals on behalf of the PHP Group. 61 | 62 | The PHP Group can be contacted via Email at group@php.net. 63 | 64 | For more information on the PHP Group and the PHP project, 65 | please see . 66 | 67 | PHP includes the Zend Engine, freely available at 68 | . 69 | -------------------------------------------------------------------------------- /tests/mcrypt_ecb_3des_encrypt.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_ecb() function : basic functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 50 | ===DONE=== 51 | --EXPECTF-- 52 | --- testing different key lengths 53 | 54 | key length=8 55 | 56 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_ecb_3des_encrypt.php on line 31 57 | 58 | Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 59 | string(0) "" 60 | 61 | key length=20 62 | 63 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_ecb_3des_encrypt.php on line 31 64 | 65 | Warning: mcrypt_encrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 66 | string(0) "" 67 | 68 | key length=24 69 | 70 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_ecb_3des_encrypt.php on line 31 71 | string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27" 72 | 73 | key length=26 74 | 75 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_ecb_3des_encrypt.php on line 31 76 | 77 | Warning: mcrypt_encrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 78 | string(0) "" 79 | 80 | --- testing different iv lengths 81 | 82 | iv length=4 83 | 84 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_ecb_3des_encrypt.php on line 38 85 | string(112) "440a6f54601969b15e81df09cd381ef585fede5f3620587fd1a949c520aed9f6d10ebbabf2cea3e1f04c9251c2878c0ca37d51c80d490165" 86 | 87 | iv length=8 88 | 89 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_ecb_3des_encrypt.php on line 38 90 | string(112) "440a6f54601969b15e81df09cd381ef585fede5f3620587fd1a949c520aed9f6d10ebbabf2cea3e1f04c9251c2878c0ca37d51c80d490165" 91 | 92 | iv length=9 93 | 94 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_ecb_3des_encrypt.php on line 38 95 | string(112) "440a6f54601969b15e81df09cd381ef585fede5f3620587fd1a949c520aed9f6d10ebbabf2cea3e1f04c9251c2878c0ca37d51c80d490165" 96 | ===DONE=== 97 | -------------------------------------------------------------------------------- /tests/mcrypt_cbc_3des_encrypt.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_cbc() function : basic functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 47 | ===DONE=== 48 | --EXPECTF-- 49 | --- testing different key lengths 50 | 51 | key length=8 52 | 53 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 28 54 | 55 | Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 56 | string(0) "" 57 | 58 | key length=20 59 | 60 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 28 61 | 62 | Warning: mcrypt_encrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 63 | string(0) "" 64 | 65 | key length=24 66 | 67 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 28 68 | string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac" 69 | 70 | key length=26 71 | 72 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 28 73 | 74 | Warning: mcrypt_encrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 75 | string(0) "" 76 | 77 | --- testing different iv lengths 78 | 79 | iv length=4 80 | 81 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 35 82 | 83 | Warning: mcrypt_encrypt(): Received initialization vector of size 4, but size 8 is required for this encryption mode in %s on line %d 84 | string(0) "" 85 | 86 | iv length=8 87 | 88 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 35 89 | string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac" 90 | 91 | iv length=9 92 | 93 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 35 94 | 95 | Warning: mcrypt_encrypt(): Received initialization vector of size 9, but size 8 is required for this encryption mode in %s on line %d 96 | string(0) "" 97 | ===DONE=== 98 | -------------------------------------------------------------------------------- /php_mcrypt.h: -------------------------------------------------------------------------------- 1 | /* 2 | +----------------------------------------------------------------------+ 3 | | PHP Version 7 | 4 | +----------------------------------------------------------------------+ 5 | | Copyright (c) 1997-2019 The PHP Group | 6 | +----------------------------------------------------------------------+ 7 | | This source file is subject to version 3.01 of the PHP license, | 8 | | that is bundled with this package in the file LICENSE, and is | 9 | | available through the world-wide-web at the following url: | 10 | | http://www.php.net/license/3_01.txt | 11 | | If you did not receive a copy of the PHP license and are unable to | 12 | | obtain it through the world-wide-web, please send a note to | 13 | | license@php.net so we can mail you a copy immediately. | 14 | +----------------------------------------------------------------------+ 15 | | Authors: Sascha Schumann | 16 | | Derick Rethans | 17 | +----------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifndef PHP_MCRYPT_H 21 | #define PHP_MCRYPT_H 22 | 23 | #if HAVE_LIBMCRYPT 24 | 25 | #ifdef ZTS 26 | #include "TSRM.h" 27 | #endif 28 | 29 | extern zend_module_entry mcrypt_module_entry; 30 | #define mcrypt_module_ptr &mcrypt_module_entry 31 | 32 | #define PHP_MCRYPT_VERSION "1.0.9" 33 | 34 | /* Functions for both old and new API */ 35 | PHP_FUNCTION(mcrypt_ecb); 36 | PHP_FUNCTION(mcrypt_cbc); 37 | PHP_FUNCTION(mcrypt_cfb); 38 | PHP_FUNCTION(mcrypt_ofb); 39 | PHP_FUNCTION(mcrypt_get_cipher_name); 40 | PHP_FUNCTION(mcrypt_get_block_size); 41 | PHP_FUNCTION(mcrypt_get_key_size); 42 | PHP_FUNCTION(mcrypt_create_iv); 43 | 44 | /* Support functions for old API */ 45 | PHP_FUNCTION(mcrypt_list_algorithms); 46 | PHP_FUNCTION(mcrypt_list_modes); 47 | PHP_FUNCTION(mcrypt_get_iv_size); 48 | PHP_FUNCTION(mcrypt_encrypt); 49 | PHP_FUNCTION(mcrypt_decrypt); 50 | 51 | /* Functions for new API */ 52 | PHP_FUNCTION(mcrypt_module_open); 53 | PHP_FUNCTION(mcrypt_generic_init); 54 | PHP_FUNCTION(mcrypt_generic); 55 | PHP_FUNCTION(mdecrypt_generic); 56 | PHP_FUNCTION(mcrypt_generic_deinit); 57 | 58 | PHP_FUNCTION(mcrypt_enc_self_test); 59 | PHP_FUNCTION(mcrypt_enc_is_block_algorithm_mode); 60 | PHP_FUNCTION(mcrypt_enc_is_block_algorithm); 61 | PHP_FUNCTION(mcrypt_enc_is_block_mode); 62 | PHP_FUNCTION(mcrypt_enc_get_block_size); 63 | PHP_FUNCTION(mcrypt_enc_get_key_size); 64 | PHP_FUNCTION(mcrypt_enc_get_supported_key_sizes); 65 | PHP_FUNCTION(mcrypt_enc_get_iv_size); 66 | PHP_FUNCTION(mcrypt_enc_get_algorithms_name); 67 | PHP_FUNCTION(mcrypt_enc_get_modes_name); 68 | PHP_FUNCTION(mcrypt_module_self_test); 69 | PHP_FUNCTION(mcrypt_module_is_block_algorithm_mode); 70 | PHP_FUNCTION(mcrypt_module_is_block_algorithm); 71 | PHP_FUNCTION(mcrypt_module_is_block_mode); 72 | PHP_FUNCTION(mcrypt_module_get_algo_block_size); 73 | PHP_FUNCTION(mcrypt_module_get_algo_key_size); 74 | PHP_FUNCTION(mcrypt_module_get_supported_key_sizes); 75 | PHP_FUNCTION(mcrypt_module_close); 76 | 77 | ZEND_BEGIN_MODULE_GLOBALS(mcrypt) 78 | int le_h; 79 | char *modes_dir; 80 | char *algorithms_dir; 81 | int fd[2]; // RANDOM = 0, URANDOM = 1 82 | ZEND_END_MODULE_GLOBALS(mcrypt) 83 | 84 | #define MCG(v) ZEND_MODULE_GLOBALS_ACCESSOR(mcrypt, v) 85 | 86 | #else 87 | #define mcrypt_module_ptr NULL 88 | #endif 89 | 90 | #define phpext_mcrypt_ptr mcrypt_module_ptr 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /tests/mcrypt_encrypt_3des_ecb.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_encrypt() function : TripleDES functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 52 | ===DONE=== 53 | --EXPECTF-- 54 | *** Testing mcrypt_encrypt() : TripleDES functionality *** 55 | 56 | --- testing different key lengths 57 | 58 | key length=8 59 | 60 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_ecb.php on line 25 61 | 62 | Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 63 | string(0) "" 64 | 65 | key length=20 66 | 67 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_ecb.php on line 25 68 | 69 | Warning: mcrypt_encrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 70 | string(0) "" 71 | 72 | key length=24 73 | 74 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_ecb.php on line 25 75 | string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27" 76 | 77 | key length=26 78 | 79 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_ecb.php on line 25 80 | 81 | Warning: mcrypt_encrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 82 | string(0) "" 83 | 84 | --- testing different iv lengths 85 | 86 | iv length=4 87 | 88 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_ecb.php on line 39 89 | string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27" 90 | 91 | iv length=8 92 | 93 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_ecb.php on line 39 94 | string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27" 95 | 96 | iv length=9 97 | 98 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_ecb.php on line 39 99 | string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27" 100 | ===DONE=== 101 | -------------------------------------------------------------------------------- /tests/mcrypt_ecb_3des_decrypt.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_cbc() function : basic functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 65 | ===DONE=== 66 | --EXPECTF-- 67 | --- testing different key lengths 68 | 69 | key length=8 70 | 71 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_ecb_3des_decrypt.php on line 42 72 | 73 | Warning: mcrypt_decrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 74 | string(0) "" 75 | 76 | key length=20 77 | 78 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_ecb_3des_decrypt.php on line 42 79 | 80 | Warning: mcrypt_decrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 81 | string(0) "" 82 | 83 | key length=24 84 | 85 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_ecb_3des_decrypt.php on line 42 86 | string(32) "736563726574206d6573736167650000" 87 | 88 | key length=26 89 | 90 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_ecb_3des_decrypt.php on line 42 91 | 92 | Warning: mcrypt_decrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 93 | string(0) "" 94 | 95 | --- testing different iv lengths 96 | 97 | iv length=4 98 | 99 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_ecb_3des_decrypt.php on line 49 100 | string(32) "a9298896ed1b7335f8f10f7ff6d7a239" 101 | 102 | iv length=8 103 | 104 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_ecb_3des_decrypt.php on line 49 105 | string(32) "a9298896ed1b7335f8f10f7ff6d7a239" 106 | 107 | iv length=9 108 | 109 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_ecb_3des_decrypt.php on line 49 110 | string(32) "a9298896ed1b7335f8f10f7ff6d7a239" 111 | ===DONE=== 112 | -------------------------------------------------------------------------------- /tests/mcrypt_decrypt_3des_ecb.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_decrypt() function : basic functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 66 | ===DONE=== 67 | --EXPECTF-- 68 | *** Testing mcrypt_decrypt() : basic functionality *** 69 | 70 | --- testing different key lengths 71 | 72 | key length=8 73 | 74 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_ecb.php on line 43 75 | 76 | Warning: mcrypt_decrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 77 | string(0) "" 78 | 79 | key length=20 80 | 81 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_ecb.php on line 43 82 | 83 | Warning: mcrypt_decrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 84 | string(0) "" 85 | 86 | key length=24 87 | 88 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_ecb.php on line 43 89 | string(32) "736563726574206d6573736167650000" 90 | 91 | key length=26 92 | 93 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_ecb.php on line 43 94 | 95 | Warning: mcrypt_decrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 96 | string(0) "" 97 | 98 | --- testing different iv lengths 99 | 100 | iv length=4 101 | 102 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_ecb.php on line 50 103 | string(32) "a9298896ed1b7335f8f10f7ff6d7a239" 104 | 105 | iv length=8 106 | 107 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_ecb.php on line 50 108 | string(32) "a9298896ed1b7335f8f10f7ff6d7a239" 109 | 110 | iv length=9 111 | 112 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_ecb.php on line 50 113 | string(32) "a9298896ed1b7335f8f10f7ff6d7a239" 114 | ===DONE=== 115 | -------------------------------------------------------------------------------- /tests/bug37595.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Bug #37595 (mcrypt_generic calculates data length in wrong way) 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 48 | --EXPECTF-- 49 | Deprecated: Function mcrypt_module_open() is deprecated in %s%ebug37595.php on line 18 50 | 51 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 30 52 | 53 | Deprecated: Function mcrypt_generic() is deprecated in %s%ebug37595.php on line 31 54 | 55 | Deprecated: Function mcrypt_generic_deinit() is deprecated in %s%ebug37595.php on line 33 56 | 57 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 35 58 | string(8) "12345678" 59 | 60 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 30 61 | 62 | Deprecated: Function mcrypt_generic() is deprecated in %s%ebug37595.php on line 31 63 | 64 | Deprecated: Function mcrypt_generic_deinit() is deprecated in %s%ebug37595.php on line 33 65 | 66 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 35 67 | string(16) "123456789" 68 | 69 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 30 70 | 71 | Deprecated: Function mcrypt_generic() is deprecated in %s%ebug37595.php on line 31 72 | 73 | Deprecated: Function mcrypt_generic_deinit() is deprecated in %s%ebug37595.php on line 33 74 | 75 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 35 76 | string(8) "1234567" 77 | 78 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 30 79 | 80 | Deprecated: Function mcrypt_generic() is deprecated in %s%ebug37595.php on line 31 81 | 82 | Warning: mcrypt_generic(): An empty string was passed in %s%ebug37595.php on line 31 83 | 84 | Deprecated: Function mcrypt_generic_deinit() is deprecated in %s%ebug37595.php on line 33 85 | 86 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 35 87 | bool(false) 88 | 89 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 30 90 | 91 | Deprecated: Function mcrypt_generic() is deprecated in %s%ebug37595.php on line 31 92 | 93 | Deprecated: Function mcrypt_generic_deinit() is deprecated in %s%ebug37595.php on line 33 94 | 95 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 35 96 | string(16) "1234567812345678" 97 | 98 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 30 99 | 100 | Deprecated: Function mcrypt_generic() is deprecated in %s%ebug37595.php on line 31 101 | 102 | Deprecated: Function mcrypt_generic_deinit() is deprecated in %s%ebug37595.php on line 33 103 | 104 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%ebug37595.php on line 35 105 | string(24) "12345678123456789" 106 | 107 | Deprecated: Function mcrypt_module_close() is deprecated in %s%ebug37595.php on line 39 108 | Done 109 | -------------------------------------------------------------------------------- /tests/mcrypt_cbc_3des_decrypt.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_cbc() function : basic functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 64 | ===DONE=== 65 | --EXPECTF-- 66 | --- testing different key lengths 67 | 68 | key length=8 69 | 70 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 41 71 | 72 | Warning: mcrypt_decrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 73 | string(0) "" 74 | 75 | key length=20 76 | 77 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 41 78 | 79 | Warning: mcrypt_decrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 80 | string(0) "" 81 | 82 | key length=24 83 | 84 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 41 85 | string(32) "736563726574206d6573736167650000" 86 | 87 | key length=26 88 | 89 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 41 90 | 91 | Warning: mcrypt_decrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 92 | string(0) "" 93 | 94 | --- testing different iv lengths 95 | 96 | iv length=4 97 | 98 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 48 99 | 100 | Warning: mcrypt_decrypt(): Received initialization vector of size 4, but size 8 is required for this encryption mode in %s on line %d 101 | string(0) "" 102 | 103 | iv length=8 104 | 105 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 48 106 | string(32) "659ec947f4dc3a3b9c50de744598d3c8" 107 | 108 | iv length=9 109 | 110 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 48 111 | 112 | Warning: mcrypt_decrypt(): Received initialization vector of size 9, but size 8 is required for this encryption mode in %s on line %d 113 | string(0) "" 114 | ===DONE=== 115 | -------------------------------------------------------------------------------- /tests/mcrypt_encrypt_3des_cbc.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_encrypt() function : TripleDES functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 60 | ===DONE=== 61 | --EXPECTF-- 62 | *** Testing mcrypt_encrypt() : TripleDES functionality *** 63 | 64 | --- testing different key lengths 65 | 66 | key length=8 67 | 68 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_cbc.php on line 40 69 | 70 | Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 71 | string(0) "" 72 | 73 | key length=20 74 | 75 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_cbc.php on line 40 76 | 77 | Warning: mcrypt_encrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 78 | string(0) "" 79 | 80 | key length=24 81 | 82 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_cbc.php on line 40 83 | string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac" 84 | 85 | key length=26 86 | 87 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_cbc.php on line 40 88 | 89 | Warning: mcrypt_encrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 90 | string(0) "" 91 | 92 | --- testing different iv lengths 93 | 94 | iv length=4 95 | 96 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_cbc.php on line 47 97 | 98 | Warning: mcrypt_encrypt(): Received initialization vector of size 4, but size 8 is required for this encryption mode in %s on line %d 99 | string(0) "" 100 | 101 | iv length=8 102 | 103 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_cbc.php on line 47 104 | string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac" 105 | 106 | iv length=9 107 | 108 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_encrypt_3des_cbc.php on line 47 109 | 110 | Warning: mcrypt_encrypt(): Received initialization vector of size 9, but size 8 is required for this encryption mode in %s on line %d 111 | string(0) "" 112 | ===DONE=== 113 | -------------------------------------------------------------------------------- /tests/mcrypt_decrypt_3des_cbc.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_decrypt() function : basic functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 67 | ===DONE=== 68 | --EXPECTF-- 69 | *** Testing mcrypt_decrypt() : basic functionality *** 70 | 71 | --- testing different key lengths 72 | 73 | key length=8 74 | 75 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_cbc.php on line 44 76 | 77 | Warning: mcrypt_decrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 78 | string(0) "" 79 | 80 | key length=20 81 | 82 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_cbc.php on line 44 83 | 84 | Warning: mcrypt_decrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 85 | string(0) "" 86 | 87 | key length=24 88 | 89 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_cbc.php on line 44 90 | string(32) "736563726574206d6573736167650000" 91 | 92 | key length=26 93 | 94 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_cbc.php on line 44 95 | 96 | Warning: mcrypt_decrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d 97 | string(0) "" 98 | 99 | --- testing different iv lengths 100 | 101 | iv length=4 102 | 103 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_cbc.php on line 51 104 | 105 | Warning: mcrypt_decrypt(): Received initialization vector of size 4, but size 8 is required for this encryption mode in %s on line %d 106 | string(0) "" 107 | 108 | iv length=8 109 | 110 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_cbc.php on line 51 111 | string(32) "659ec947f4dc3a3b9c50de744598d3c8" 112 | 113 | iv length=9 114 | 115 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_decrypt_3des_cbc.php on line 51 116 | 117 | Warning: mcrypt_decrypt(): Received initialization vector of size 9, but size 8 is required for this encryption mode in %s on line %d 118 | string(0) "" 119 | ===DONE=== 120 | -------------------------------------------------------------------------------- /tests/mcrypt_rijndael128_128BitKey.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_encrypt() function : AES functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 65 | ===DONE=== 66 | --EXPECTF-- 67 | *** Testing mcrypt : Rijndael128 functionality *** 68 | 69 | --- testing different key lengths 70 | 71 | key length=0 72 | 73 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_rijndael128_128BitKey.php on line %d 74 | 75 | Warning: mcrypt_encrypt(): Key of size 0 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d 76 | string(0) "" 77 | 78 | key length=8 79 | 80 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_rijndael128_128BitKey.php on line %d 81 | 82 | Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d 83 | string(0) "" 84 | 85 | key length=16 86 | 87 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_rijndael128_128BitKey.php on line %d 88 | string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101" 89 | 90 | --- testing different iv lengths 91 | 92 | iv length=0 93 | 94 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_rijndael128_128BitKey.php on line %d 95 | 96 | Warning: mcrypt_decrypt(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d 97 | string(0) "" 98 | 99 | iv length=8 100 | 101 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_rijndael128_128BitKey.php on line %d 102 | 103 | Warning: mcrypt_decrypt(): Received initialization vector of size 8, but size 16 is required for this encryption mode in %s on line %d 104 | string(0) "" 105 | 106 | iv length=16 107 | 108 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_rijndael128_128BitKey.php on line %d 109 | string(32) "42adc8c0db19473f2c684ff2d6e828a5" 110 | 111 | iv length=17 112 | 113 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_rijndael128_128BitKey.php on line %d 114 | 115 | Warning: mcrypt_decrypt(): Received initialization vector of size 17, but size 16 is required for this encryption mode in %s on line %d 116 | string(0) "" 117 | ===DONE=== 118 | -------------------------------------------------------------------------------- /tests/mcrypt_rijndael128_256BitKey.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test mcrypt_encrypt() function : AES functionality 3 | --SKIPIF-- 4 | 9 | --FILE-- 10 | 58 | ===DONE=== 59 | --EXPECTF-- 60 | *** Testing mcrypt : Rijndael128 functionality *** 61 | 62 | --- testing different key lengths 63 | 64 | key length=20 65 | 66 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 43 67 | 68 | Warning: mcrypt_encrypt(): Key of size 20 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d 69 | string(0) "" 70 | 71 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 45 72 | 73 | Warning: mcrypt_decrypt(): Key of size 20 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d 74 | string(0) "" 75 | 76 | key length=24 77 | 78 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 43 79 | string(128) "8ecdf1ed5742aff16ef34c819c8d22c707c54f4d9ffc18e5f6ab79fe68c25705351e2c001a0b9f29e5def67570ca9da644efb69a8bb97940cb4bec094dae8bb5" 80 | 81 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 45 82 | string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" 83 | 84 | key length=30 85 | 86 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 43 87 | 88 | Warning: mcrypt_encrypt(): Key of size 30 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d 89 | string(0) "" 90 | 91 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 45 92 | 93 | Warning: mcrypt_decrypt(): Key of size 30 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d 94 | string(0) "" 95 | 96 | key length=32 97 | 98 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 43 99 | string(128) "f23bc103bfd0859a8318acee6d96e5f43dff68f3cdeae817a1e77c33492e32bdb82c5f660fcd1a2bfda70d9de4d5d8028ce179a9e2f7f9ee7dd61c7b4b409e95" 100 | 101 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 45 102 | string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" 103 | 104 | key length=40 105 | 106 | Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 43 107 | 108 | Warning: mcrypt_encrypt(): Key of size 40 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d 109 | string(0) "" 110 | 111 | Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_rijndael128_256BitKey.php on line 45 112 | 113 | Warning: mcrypt_decrypt(): Key of size 40 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d 114 | string(0) "" 115 | ===DONE=== 116 | -------------------------------------------------------------------------------- /package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | mcrypt 4 | pecl.php.net 5 | Bindings for the libmcrypt library 6 | 7 | Provides bindings for the unmaintained libmcrypt. 8 | 9 | 10 | Derick Rethans 11 | derick 12 | derick@php.net 13 | no 14 | 15 | 16 | Sascha Schumann 17 | sas 18 | sascha@schumann.cx 19 | no 20 | 21 | 22 | Leigh 23 | leigh 24 | leigh@php.net 25 | yes 26 | 27 | 2024-08-05 28 | 29 | 1.0.9 30 | 1.0.0 31 | 32 | 33 | stable 34 | stable 35 | 36 | PHP License 37 | 38 | - Make changes to how the extension uses random, to make it work with PHP 8.4 (and PHP 8.5). 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 7.2.0 109 | 8.6.0 110 | 8.6.0 111 | 112 | 113 | 1.4.0 114 | 115 | 116 | 117 | mcrypt 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /mcrypt_filter.c: -------------------------------------------------------------------------------- 1 | /* 2 | +----------------------------------------------------------------------+ 3 | | PHP Version 7 | 4 | +----------------------------------------------------------------------+ 5 | | Copyright (c) 1997-2016 The PHP Group | 6 | +----------------------------------------------------------------------+ 7 | | This source file is subject to version 3.01 of the PHP license, | 8 | | that is bundled with this package in the file LICENSE, and is | 9 | | available through the world-wide-web at the following url: | 10 | | http://www.php.net/license/3_01.txt | 11 | | If you did not receive a copy of the PHP license and are unable to | 12 | | obtain it through the world-wide-web, please send a note to | 13 | | license@php.net so we can mail you a copy immediately. | 14 | +----------------------------------------------------------------------+ 15 | | Author: Sara Golemon | 16 | +----------------------------------------------------------------------+ 17 | */ 18 | 19 | #include "php.h" 20 | 21 | #include "php_mcrypt_filter.h" 22 | #include "php_ini.h" 23 | #include 24 | 25 | typedef struct _php_mcrypt_filter_data { 26 | MCRYPT module; 27 | char encrypt; 28 | int blocksize; 29 | char *block_buffer; 30 | int block_used; 31 | char persistent; 32 | } php_mcrypt_filter_data; 33 | 34 | static php_stream_filter_status_t php_mcrypt_filter( 35 | php_stream *stream, 36 | php_stream_filter *thisfilter, 37 | php_stream_bucket_brigade *buckets_in, 38 | php_stream_bucket_brigade *buckets_out, 39 | size_t *bytes_consumed, 40 | int flags) 41 | { 42 | php_mcrypt_filter_data *data; 43 | php_stream_bucket *bucket; 44 | size_t consumed = 0; 45 | php_stream_filter_status_t exit_status = PSFS_FEED_ME; 46 | 47 | if (!thisfilter || !Z_PTR(thisfilter->abstract)) { 48 | /* Should never happen */ 49 | return PSFS_ERR_FATAL; 50 | } 51 | 52 | data = (php_mcrypt_filter_data *)(Z_PTR(thisfilter->abstract)); 53 | while(buckets_in->head) { 54 | bucket = buckets_in->head; 55 | 56 | consumed += bucket->buflen; 57 | 58 | if (data->blocksize) { 59 | /* Blockmode cipher */ 60 | char *outchunk; 61 | int chunklen = (int)(bucket->buflen + data->block_used), n; 62 | php_stream_bucket *newbucket; 63 | 64 | outchunk = pemalloc(chunklen, data->persistent); 65 | if (data->block_used) { 66 | memcpy(outchunk, data->block_buffer, data->block_used); 67 | } 68 | memcpy(outchunk + data->block_used, bucket->buf, bucket->buflen); 69 | 70 | for(n=0; (n + data->blocksize) <= chunklen; n += data->blocksize) { 71 | 72 | if (data->encrypt) { 73 | mcrypt_generic(data->module, outchunk + n, data->blocksize); 74 | } else { 75 | mdecrypt_generic(data->module, outchunk + n, data->blocksize); 76 | } 77 | } 78 | data->block_used = chunklen - n; 79 | memcpy(data->block_buffer, outchunk + n, data->block_used); 80 | 81 | newbucket = php_stream_bucket_new(stream, outchunk, n, 1, data->persistent); 82 | php_stream_bucket_append(buckets_out, newbucket); 83 | 84 | exit_status = PSFS_PASS_ON; 85 | 86 | php_stream_bucket_unlink(bucket); 87 | php_stream_bucket_delref(bucket); 88 | } else { 89 | /* Stream cipher */ 90 | php_stream_bucket_make_writeable(bucket); 91 | if (data->encrypt) { 92 | mcrypt_generic(data->module, bucket->buf, (int)bucket->buflen); 93 | } else { 94 | mdecrypt_generic(data->module, bucket->buf, (int)bucket->buflen); 95 | } 96 | php_stream_bucket_append(buckets_out, bucket); 97 | 98 | exit_status = PSFS_PASS_ON; 99 | } 100 | } 101 | 102 | if ((flags & PSFS_FLAG_FLUSH_CLOSE) && data->blocksize && data->block_used) { 103 | php_stream_bucket *newbucket; 104 | 105 | memset(data->block_buffer + data->block_used, 0, data->blocksize - data->block_used); 106 | if (data->encrypt) { 107 | mcrypt_generic(data->module, data->block_buffer, data->blocksize); 108 | } else { 109 | mdecrypt_generic(data->module, data->block_buffer, data->blocksize); 110 | } 111 | 112 | newbucket = php_stream_bucket_new(stream, data->block_buffer, data->blocksize, 0, data->persistent); 113 | php_stream_bucket_append(buckets_out, newbucket); 114 | 115 | exit_status = PSFS_PASS_ON; 116 | } 117 | 118 | if (bytes_consumed) { 119 | *bytes_consumed = consumed; 120 | } 121 | 122 | return exit_status; 123 | } 124 | 125 | static void php_mcrypt_filter_dtor(php_stream_filter *thisfilter) 126 | { 127 | if (thisfilter && Z_PTR(thisfilter->abstract)) { 128 | php_mcrypt_filter_data *data = (php_mcrypt_filter_data*) Z_PTR(thisfilter->abstract); 129 | 130 | if (data->block_buffer) { 131 | pefree(data->block_buffer, data->persistent); 132 | } 133 | 134 | mcrypt_generic_deinit(data->module); 135 | mcrypt_module_close(data->module); 136 | 137 | pefree(data, data->persistent); 138 | } 139 | } 140 | 141 | static php_stream_filter_ops php_mcrypt_filter_ops = { 142 | php_mcrypt_filter, 143 | php_mcrypt_filter_dtor, 144 | "mcrypt.*" 145 | }; 146 | 147 | /* {{{ php_mcrypt_filter_create 148 | * Instantiate mcrypt filter 149 | */ 150 | static php_stream_filter *php_mcrypt_filter_create(const char *filtername, zval *filterparams, uint8_t persistent) 151 | { 152 | int encrypt = 1, iv_len, key_len, keyl, result; 153 | const char *cipher = filtername + sizeof("mcrypt.") - 1; 154 | zval *tmpzval; 155 | MCRYPT mcrypt_module; 156 | char *iv = NULL, *key = NULL; 157 | char *algo_dir = INI_STR("mcrypt.algorithms_dir"); 158 | char *mode_dir = INI_STR("mcrypt.modes_dir"); 159 | char *mode = "cbc"; 160 | php_mcrypt_filter_data *data; 161 | 162 | php_error_docref(NULL, E_DEPRECATED, "mcrypt and mdecrypt stream filters have been deprecated"); 163 | 164 | if (strncasecmp(filtername, "mdecrypt.", sizeof("mdecrypt.") - 1) == 0) { 165 | encrypt = 0; 166 | cipher += sizeof("de") - 1; 167 | } else if (strncasecmp(filtername, "mcrypt.", sizeof("mcrypt.") - 1) != 0) { 168 | /* Should never happen */ 169 | return NULL; 170 | } 171 | 172 | if (!filterparams || Z_TYPE_P(filterparams) != IS_ARRAY) { 173 | php_error_docref(NULL, E_WARNING, "Filter parameters for %s must be an array", filtername); 174 | return NULL; 175 | } 176 | 177 | if ((tmpzval = zend_hash_str_find(Z_ARRVAL_P(filterparams), ZEND_STRL("mode")))) { 178 | if (Z_TYPE_P(tmpzval) == IS_STRING) { 179 | mode = Z_STRVAL_P(tmpzval); 180 | } else { 181 | php_error_docref(NULL, E_WARNING, "mode is not a string, ignoring"); 182 | } 183 | } 184 | 185 | if ((tmpzval=zend_hash_str_find(Z_ARRVAL_P(filterparams), ZEND_STRL("algorithms_dir")))) { 186 | if (Z_TYPE_P(tmpzval) == IS_STRING) { 187 | algo_dir = Z_STRVAL_P(tmpzval); 188 | } else { 189 | php_error_docref(NULL, E_WARNING, "algorithms_dir is not a string, ignoring"); 190 | } 191 | } 192 | 193 | if ((tmpzval=zend_hash_str_find(Z_ARRVAL_P(filterparams), ZEND_STRL("modes_dir")))) { 194 | if (Z_TYPE_P(tmpzval) == IS_STRING) { 195 | mode_dir = Z_STRVAL_P(tmpzval); 196 | } else { 197 | php_error_docref(NULL, E_WARNING, "modes_dir is not a string, ignoring"); 198 | } 199 | } 200 | 201 | if ((tmpzval = zend_hash_str_find(Z_ARRVAL_P(filterparams), ZEND_STRL("key"))) && 202 | Z_TYPE_P(tmpzval) == IS_STRING) { 203 | key = Z_STRVAL_P(tmpzval); 204 | key_len = (int)Z_STRLEN_P(tmpzval); 205 | } else { 206 | php_error_docref(NULL, E_WARNING, "key not specified or is not a string"); 207 | return NULL; 208 | } 209 | 210 | mcrypt_module = mcrypt_module_open((char *)cipher, algo_dir, mode, mode_dir); 211 | if (mcrypt_module == MCRYPT_FAILED) { 212 | php_error_docref(NULL, E_WARNING, "Could not open encryption module"); 213 | return NULL; 214 | } 215 | iv_len = mcrypt_enc_get_iv_size(mcrypt_module); 216 | keyl = mcrypt_enc_get_key_size(mcrypt_module); 217 | if (keyl < key_len) { 218 | key_len = keyl; 219 | } 220 | 221 | if (!(tmpzval = zend_hash_str_find(Z_ARRVAL_P(filterparams), ZEND_STRL("iv"))) || 222 | Z_TYPE_P(tmpzval) != IS_STRING) { 223 | php_error_docref(NULL, E_WARNING, "Filter parameter[iv] not provided or not of type: string"); 224 | mcrypt_module_close(mcrypt_module); 225 | return NULL; 226 | } 227 | 228 | iv = emalloc(iv_len + 1); 229 | if ((size_t)iv_len <= Z_STRLEN_P(tmpzval)) { 230 | memcpy(iv, Z_STRVAL_P(tmpzval), iv_len); 231 | } else { 232 | memcpy(iv, Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval)); 233 | memset(iv + Z_STRLEN_P(tmpzval), 0, iv_len - Z_STRLEN_P(tmpzval)); 234 | } 235 | 236 | result = mcrypt_generic_init(mcrypt_module, key, key_len, iv); 237 | efree(iv); 238 | if (result < 0) { 239 | switch (result) { 240 | case -3: 241 | php_error_docref(NULL, E_WARNING, "Key length incorrect"); 242 | break; 243 | case -4: 244 | php_error_docref(NULL, E_WARNING, "Memory allocation error"); 245 | break; 246 | case -1: 247 | default: 248 | php_error_docref(NULL, E_WARNING, "Unknown error"); 249 | break; 250 | } 251 | mcrypt_module_close(mcrypt_module); 252 | return NULL; 253 | } 254 | 255 | data = pemalloc(sizeof(php_mcrypt_filter_data), persistent); 256 | data->module = mcrypt_module; 257 | data->encrypt = encrypt; 258 | if (mcrypt_enc_is_block_mode(mcrypt_module)) { 259 | data->blocksize = mcrypt_enc_get_block_size(mcrypt_module); 260 | data->block_buffer = pemalloc(data->blocksize, persistent); 261 | } else { 262 | data->blocksize = 0; 263 | data->block_buffer = NULL; 264 | } 265 | data->block_used = 0; 266 | data->persistent = persistent; 267 | 268 | return php_stream_filter_alloc(&php_mcrypt_filter_ops, data, persistent); 269 | } 270 | /* }}} */ 271 | 272 | php_stream_filter_factory php_mcrypt_filter_factory = { 273 | php_mcrypt_filter_create 274 | }; 275 | 276 | /* 277 | * Local variables: 278 | * tab-width: 4 279 | * c-basic-offset: 4 280 | * End: 281 | * vim600: noet sw=4 ts=4 fdm=marker 282 | * vim<600: noet sw=4 ts=4 283 | */ 284 | -------------------------------------------------------------------------------- /tests/blowfish.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for blowfish compatibility 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 55 | --EXPECTF-- 56 | key plain crypt guess stat 57 | 58 | Deprecated: Function mcrypt_module_open() is deprecated in %s%eblowfish.php on line %d 59 | 0000000000000000 0000000000000000 60 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 61 | 62 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 63 | 4ef997456198dd78 4ef997456198dd78 OK 64 | FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 65 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 66 | 67 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 68 | 51866fd5b85ecb8a 51866fd5b85ecb8a OK 69 | 3000000000000000 1000000000000001 70 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 71 | 72 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 73 | 7d856f9a613063f2 7d856f9a613063f2 OK 74 | 1111111111111111 1111111111111111 75 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 76 | 77 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 78 | 2466dd878b963c9d 2466dd878b963c9d OK 79 | 0123456789ABCDEF 1111111111111111 80 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 81 | 82 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 83 | 61f9c3802281b096 61f9c3802281b096 OK 84 | 1111111111111111 0123456789ABCDEF 85 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 86 | 87 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 88 | 7d0cc630afda1ec7 7d0cc630afda1ec7 OK 89 | FEDCBA9876543210 0123456789ABCDEF 90 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 91 | 92 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 93 | 0aceab0fc6a0a28d 0aceab0fc6a0a28d OK 94 | 7CA110454A1A6E57 01A1D6D039776742 95 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 96 | 97 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 98 | 59c68245eb05282b 59c68245eb05282b OK 99 | 0131D9619DC1376E 5CD54CA83DEF57DA 100 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 101 | 102 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 103 | b1b8cc0b250f09a0 b1b8cc0b250f09a0 OK 104 | 07A1133E4A0B2686 0248D43806F67172 105 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 106 | 107 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 108 | 1730e5778bea1da4 1730e5778bea1da4 OK 109 | 3849674C2602319E 51454B582DDF440A 110 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 111 | 112 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 113 | a25e7856cf2651eb a25e7856cf2651eb OK 114 | 04B915BA43FEB5B6 42FD443059577FA2 115 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 116 | 117 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 118 | 353882b109ce8f1a 353882b109ce8f1a OK 119 | 0113B970FD34F2CE 059B5E0851CF143A 120 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 121 | 122 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 123 | 48f4d0884c379918 48f4d0884c379918 OK 124 | 0170F175468FB5E6 0756D8E0774761D2 125 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 126 | 127 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 128 | 432193b78951fc98 432193b78951fc98 OK 129 | 43297FAD38E373FE 762514B829BF486A 130 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 131 | 132 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 133 | 13f04154d69d1ae5 13f04154d69d1ae5 OK 134 | 07A7137045DA2A16 3BDD119049372802 135 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 136 | 137 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 138 | 2eedda93ffd39c79 2eedda93ffd39c79 OK 139 | 04689104C2FD3B2F 26955F6835AF609A 140 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 141 | 142 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 143 | d887e0393c2da6e3 d887e0393c2da6e3 OK 144 | 37D06BB516CB7546 164D5E404F275232 145 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 146 | 147 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 148 | 5f99d04f5b163969 5f99d04f5b163969 OK 149 | 1F08260D1AC2465E 6B056E18759F5CCA 150 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 151 | 152 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 153 | 4a057a3b24d3977b 4a057a3b24d3977b OK 154 | 584023641ABA6176 004BD6EF09176062 155 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 156 | 157 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 158 | 452031c1e4fada8e 452031c1e4fada8e OK 159 | 025816164629B007 480D39006EE762F2 160 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 161 | 162 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 163 | 7555ae39f59b87bd 7555ae39f59b87bd OK 164 | 49793EBC79B3258F 437540C8698F3CFA 165 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 166 | 167 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 168 | 53c55f9cb49fc019 53c55f9cb49fc019 OK 169 | 4FB05E1515AB73A7 072D43A077075292 170 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 171 | 172 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 173 | 7a8e7bfa937e89a3 7a8e7bfa937e89a3 OK 174 | 49E95D6D4CA229BF 02FE55778117F12A 175 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 176 | 177 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 178 | cf9c5d7a4986adb5 cf9c5d7a4986adb5 OK 179 | 018310DC409B26D6 1D9D5C5018F728C2 180 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 181 | 182 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 183 | d1abb290658bc778 d1abb290658bc778 OK 184 | 1C587F1C13924FEF 305532286D6F295A 185 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 186 | 187 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 188 | 55cb3774d13ef201 55cb3774d13ef201 OK 189 | 0101010101010101 0123456789ABCDEF 190 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 191 | 192 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 193 | fa34ec4847b268b2 fa34ec4847b268b2 OK 194 | 1F1F1F1F0E0E0E0E 0123456789ABCDEF 195 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 196 | 197 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 198 | a790795108ea3cae a790795108ea3cae OK 199 | E0FEE0FEF1FEF1FE 0123456789ABCDEF 200 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 201 | 202 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 203 | c39e072d9fac631d c39e072d9fac631d OK 204 | 0000000000000000 FFFFFFFFFFFFFFFF 205 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 206 | 207 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 208 | 014933e0cdaff6e4 014933e0cdaff6e4 OK 209 | FFFFFFFFFFFFFFFF 0000000000000000 210 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 211 | 212 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 213 | f21e9a77b71c49bc f21e9a77b71c49bc OK 214 | 0123456789ABCDEF 0000000000000000 215 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 216 | 217 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 218 | 245946885754369a 245946885754369a OK 219 | FEDCBA9876543210 FFFFFFFFFFFFFFFF 220 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 221 | 222 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 223 | 6b5c5a9c5d9e0a5a 6b5c5a9c5d9e0a5a OK 224 | 225 | Deprecated: Function mcrypt_module_open() is deprecated in %s%eblowfish.php on line %d 226 | 227 | Deprecated: Function mcrypt_generic_init() is deprecated in %s%eblowfish.php on line %d 228 | 229 | Deprecated: Function mcrypt_generic() is deprecated in %s%eblowfish.php on line %d 230 | 231 | 6b77b4d63006dee605b156e27403979358deb9e7154616d959f1652bd5ff92cc 232 | -------------------------------------------------------------------------------- /mcrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | +----------------------------------------------------------------------+ 3 | | PHP Version 7 | 4 | +----------------------------------------------------------------------+ 5 | | Copyright (c) 1997-2016 The PHP Group | 6 | +----------------------------------------------------------------------+ 7 | | This source file is subject to version 3.01 of the PHP license, | 8 | | that is bundled with this package in the file LICENSE, and is | 9 | | available through the world-wide-web at the following url: | 10 | | http://www.php.net/license/3_01.txt | 11 | | If you did not receive a copy of the PHP license and are unable to | 12 | | obtain it through the world-wide-web, please send a note to | 13 | | license@php.net so we can mail you a copy immediately. | 14 | +----------------------------------------------------------------------+ 15 | | Authors: Sascha Schumann | 16 | | Derick Rethans | 17 | +----------------------------------------------------------------------+ 18 | */ 19 | 20 | #ifdef HAVE_CONFIG_H 21 | #include "config.h" 22 | #endif 23 | 24 | #include "php.h" 25 | 26 | #if HAVE_LIBMCRYPT 27 | 28 | #if PHP_WIN32 29 | # include "win32/winutil.h" 30 | #endif 31 | 32 | #include "php_mcrypt.h" 33 | #include "fcntl.h" 34 | 35 | #define NON_FREE 36 | #define MCRYPT2 37 | #include "mcrypt.h" 38 | #include "php_ini.h" 39 | #include "php_globals.h" 40 | #include "ext/standard/info.h" 41 | #if PHP_VERSION_ID >= 80400 42 | # include "ext/random/php_random.h" 43 | #else 44 | # include "ext/standard/php_rand.h" 45 | #endif 46 | #include "zend_smart_str.h" 47 | #include "php_mcrypt_filter.h" 48 | 49 | static int le_mcrypt; 50 | 51 | typedef struct _php_mcrypt { 52 | MCRYPT td; 53 | zend_bool init; 54 | } php_mcrypt; 55 | 56 | /* {{{ arginfo */ 57 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_open, 0, 0, 4) 58 | ZEND_ARG_INFO(0, cipher) 59 | ZEND_ARG_INFO(0, cipher_directory) 60 | ZEND_ARG_INFO(0, mode) 61 | ZEND_ARG_INFO(0, mode_directory) 62 | ZEND_END_ARG_INFO() 63 | 64 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_generic_init, 0, 0, 3) 65 | ZEND_ARG_INFO(0, td) 66 | ZEND_ARG_INFO(0, key) 67 | ZEND_ARG_INFO(0, iv) 68 | ZEND_END_ARG_INFO() 69 | 70 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_generic, 0, 0, 2) 71 | ZEND_ARG_INFO(0, td) 72 | ZEND_ARG_INFO(0, data) 73 | ZEND_END_ARG_INFO() 74 | 75 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mdecrypt_generic, 0, 0, 2) 76 | ZEND_ARG_INFO(0, td) 77 | ZEND_ARG_INFO(0, data) 78 | ZEND_END_ARG_INFO() 79 | 80 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_supported_key_sizes, 0, 0, 1) 81 | ZEND_ARG_INFO(0, td) 82 | ZEND_END_ARG_INFO() 83 | 84 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_self_test, 0, 0, 1) 85 | ZEND_ARG_INFO(0, td) 86 | ZEND_END_ARG_INFO() 87 | 88 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_close, 0, 0, 1) 89 | ZEND_ARG_INFO(0, td) 90 | ZEND_END_ARG_INFO() 91 | 92 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_generic_deinit, 0, 0, 1) 93 | ZEND_ARG_INFO(0, td) 94 | ZEND_END_ARG_INFO() 95 | 96 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_is_block_algorithm_mode, 0, 0, 1) 97 | ZEND_ARG_INFO(0, td) 98 | ZEND_END_ARG_INFO() 99 | 100 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_is_block_algorithm, 0, 0, 1) 101 | ZEND_ARG_INFO(0, td) 102 | ZEND_END_ARG_INFO() 103 | 104 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_is_block_mode, 0, 0, 1) 105 | ZEND_ARG_INFO(0, td) 106 | ZEND_END_ARG_INFO() 107 | 108 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_block_size, 0, 0, 1) 109 | ZEND_ARG_INFO(0, td) 110 | ZEND_END_ARG_INFO() 111 | 112 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_key_size, 0, 0, 1) 113 | ZEND_ARG_INFO(0, td) 114 | ZEND_END_ARG_INFO() 115 | 116 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_iv_size, 0, 0, 1) 117 | ZEND_ARG_INFO(0, td) 118 | ZEND_END_ARG_INFO() 119 | 120 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_algorithms_name, 0, 0, 1) 121 | ZEND_ARG_INFO(0, td) 122 | ZEND_END_ARG_INFO() 123 | 124 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_modes_name, 0, 0, 1) 125 | ZEND_ARG_INFO(0, td) 126 | ZEND_END_ARG_INFO() 127 | 128 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_self_test, 0, 0, 1) 129 | ZEND_ARG_INFO(0, algorithm) 130 | ZEND_ARG_INFO(0, lib_dir) 131 | ZEND_END_ARG_INFO() 132 | 133 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_is_block_algorithm_mode, 0, 0, 1) 134 | ZEND_ARG_INFO(0, mode) 135 | ZEND_ARG_INFO(0, lib_dir) 136 | ZEND_END_ARG_INFO() 137 | 138 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_is_block_algorithm, 0, 0, 1) 139 | ZEND_ARG_INFO(0, algorithm) 140 | ZEND_ARG_INFO(0, lib_dir) 141 | ZEND_END_ARG_INFO() 142 | 143 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_is_block_mode, 0, 0, 1) 144 | ZEND_ARG_INFO(0, mode) 145 | ZEND_ARG_INFO(0, lib_dir) 146 | ZEND_END_ARG_INFO() 147 | 148 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_get_algo_block_size, 0, 0, 1) 149 | ZEND_ARG_INFO(0, algorithm) 150 | ZEND_ARG_INFO(0, lib_dir) 151 | ZEND_END_ARG_INFO() 152 | 153 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_get_algo_key_size, 0, 0, 1) 154 | ZEND_ARG_INFO(0, algorithm) 155 | ZEND_ARG_INFO(0, lib_dir) 156 | ZEND_END_ARG_INFO() 157 | 158 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_get_supported_key_sizes, 0, 0, 1) 159 | ZEND_ARG_INFO(0, algorithm) 160 | ZEND_ARG_INFO(0, lib_dir) 161 | ZEND_END_ARG_INFO() 162 | 163 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_list_algorithms, 0, 0, 0) 164 | ZEND_ARG_INFO(0, lib_dir) 165 | ZEND_END_ARG_INFO() 166 | 167 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_list_modes, 0, 0, 0) 168 | ZEND_ARG_INFO(0, lib_dir) 169 | ZEND_END_ARG_INFO() 170 | 171 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_key_size, 0, 0, 2) 172 | ZEND_ARG_INFO(0, cipher) 173 | ZEND_ARG_INFO(0, module) 174 | ZEND_END_ARG_INFO() 175 | 176 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_block_size, 0, 0, 2) 177 | ZEND_ARG_INFO(0, cipher) 178 | ZEND_ARG_INFO(0, module) 179 | ZEND_END_ARG_INFO() 180 | 181 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_iv_size, 0, 0, 2) 182 | ZEND_ARG_INFO(0, cipher) 183 | ZEND_ARG_INFO(0, module) 184 | ZEND_END_ARG_INFO() 185 | 186 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_cipher_name, 0, 0, 1) 187 | ZEND_ARG_INFO(0, cipher) 188 | ZEND_END_ARG_INFO() 189 | 190 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_encrypt, 0, 0, 4) 191 | ZEND_ARG_INFO(0, cipher) 192 | ZEND_ARG_INFO(0, key) 193 | ZEND_ARG_INFO(0, data) 194 | ZEND_ARG_INFO(0, mode) 195 | ZEND_ARG_INFO(0, iv) 196 | ZEND_END_ARG_INFO() 197 | 198 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_decrypt, 0, 0, 4) 199 | ZEND_ARG_INFO(0, cipher) 200 | ZEND_ARG_INFO(0, key) 201 | ZEND_ARG_INFO(0, data) 202 | ZEND_ARG_INFO(0, mode) 203 | ZEND_ARG_INFO(0, iv) 204 | ZEND_END_ARG_INFO() 205 | 206 | ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_create_iv, 0, 0, 1) 207 | ZEND_ARG_INFO(0, size) 208 | ZEND_ARG_INFO(0, source) 209 | ZEND_END_ARG_INFO() 210 | /* }}} */ 211 | 212 | const zend_function_entry mcrypt_functions[] = { /* {{{ */ 213 | PHP_DEP_FE(mcrypt_get_key_size, arginfo_mcrypt_get_key_size) 214 | PHP_DEP_FE(mcrypt_get_block_size, arginfo_mcrypt_get_block_size) 215 | PHP_DEP_FE(mcrypt_get_cipher_name, arginfo_mcrypt_get_cipher_name) 216 | PHP_DEP_FE(mcrypt_create_iv, arginfo_mcrypt_create_iv) 217 | 218 | PHP_DEP_FE(mcrypt_list_algorithms, arginfo_mcrypt_list_algorithms) 219 | PHP_DEP_FE(mcrypt_list_modes, arginfo_mcrypt_list_modes) 220 | PHP_DEP_FE(mcrypt_get_iv_size, arginfo_mcrypt_get_iv_size) 221 | PHP_DEP_FE(mcrypt_encrypt, arginfo_mcrypt_encrypt) 222 | PHP_DEP_FE(mcrypt_decrypt, arginfo_mcrypt_decrypt) 223 | 224 | PHP_DEP_FE(mcrypt_module_open, arginfo_mcrypt_module_open) 225 | PHP_DEP_FE(mcrypt_generic_init, arginfo_mcrypt_generic_init) 226 | PHP_DEP_FE(mcrypt_generic, arginfo_mcrypt_generic) 227 | PHP_DEP_FE(mdecrypt_generic, arginfo_mdecrypt_generic) 228 | PHP_DEP_FE(mcrypt_generic_deinit, arginfo_mcrypt_generic_deinit) 229 | 230 | PHP_DEP_FE(mcrypt_enc_self_test, arginfo_mcrypt_enc_self_test) 231 | PHP_DEP_FE(mcrypt_enc_is_block_algorithm_mode, arginfo_mcrypt_enc_is_block_algorithm_mode) 232 | PHP_DEP_FE(mcrypt_enc_is_block_algorithm, arginfo_mcrypt_enc_is_block_algorithm) 233 | PHP_DEP_FE(mcrypt_enc_is_block_mode, arginfo_mcrypt_enc_is_block_mode) 234 | PHP_DEP_FE(mcrypt_enc_get_block_size, arginfo_mcrypt_enc_get_block_size) 235 | PHP_DEP_FE(mcrypt_enc_get_key_size, arginfo_mcrypt_enc_get_key_size) 236 | PHP_DEP_FE(mcrypt_enc_get_supported_key_sizes, arginfo_mcrypt_enc_get_supported_key_sizes) 237 | PHP_DEP_FE(mcrypt_enc_get_iv_size, arginfo_mcrypt_enc_get_iv_size) 238 | PHP_DEP_FE(mcrypt_enc_get_algorithms_name, arginfo_mcrypt_enc_get_algorithms_name) 239 | PHP_DEP_FE(mcrypt_enc_get_modes_name, arginfo_mcrypt_enc_get_modes_name) 240 | PHP_DEP_FE(mcrypt_module_self_test, arginfo_mcrypt_module_self_test) 241 | 242 | PHP_DEP_FE(mcrypt_module_is_block_algorithm_mode, arginfo_mcrypt_module_is_block_algorithm_mode) 243 | PHP_DEP_FE(mcrypt_module_is_block_algorithm, arginfo_mcrypt_module_is_block_algorithm) 244 | PHP_DEP_FE(mcrypt_module_is_block_mode, arginfo_mcrypt_module_is_block_mode) 245 | PHP_DEP_FE(mcrypt_module_get_algo_block_size, arginfo_mcrypt_module_get_algo_block_size) 246 | PHP_DEP_FE(mcrypt_module_get_algo_key_size, arginfo_mcrypt_module_get_algo_key_size) 247 | PHP_DEP_FE(mcrypt_module_get_supported_key_sizes, arginfo_mcrypt_module_get_supported_key_sizes) 248 | 249 | PHP_DEP_FE(mcrypt_module_close, arginfo_mcrypt_module_close) 250 | PHP_FE_END 251 | }; 252 | /* }}} */ 253 | 254 | static PHP_MINFO_FUNCTION(mcrypt); 255 | static PHP_MINIT_FUNCTION(mcrypt); 256 | static PHP_MSHUTDOWN_FUNCTION(mcrypt); 257 | static PHP_GINIT_FUNCTION(mcrypt); 258 | static PHP_GSHUTDOWN_FUNCTION(mcrypt); 259 | 260 | ZEND_DECLARE_MODULE_GLOBALS(mcrypt) 261 | 262 | zend_module_entry mcrypt_module_entry = { 263 | STANDARD_MODULE_HEADER, 264 | "mcrypt", 265 | mcrypt_functions, 266 | PHP_MINIT(mcrypt), PHP_MSHUTDOWN(mcrypt), 267 | NULL, NULL, 268 | PHP_MINFO(mcrypt), 269 | PHP_MCRYPT_VERSION, 270 | PHP_MODULE_GLOBALS(mcrypt), 271 | PHP_GINIT(mcrypt), 272 | PHP_GSHUTDOWN(mcrypt), 273 | NULL, 274 | STANDARD_MODULE_PROPERTIES_EX 275 | }; 276 | 277 | #ifdef COMPILE_DL_MCRYPT 278 | #ifdef ZTS 279 | ZEND_TSRMLS_CACHE_DEFINE() 280 | #endif 281 | ZEND_GET_MODULE(mcrypt) 282 | #endif 283 | 284 | #define MCRYPT_ENCRYPT 0 285 | #define MCRYPT_DECRYPT 1 286 | 287 | typedef enum { 288 | RANDOM = 0, 289 | URANDOM, 290 | RAND 291 | } iv_source; 292 | 293 | #define MCRYPT_GET_INI \ 294 | cipher_dir_string = MCG(algorithms_dir); \ 295 | module_dir_string = MCG(modes_dir); 296 | 297 | /* 298 | * #warning is not ANSI C 299 | * #warning Invalidate resource if the param count is wrong, or other problems 300 | * #warning occurred during functions. 301 | */ 302 | 303 | #define MCRYPT_GET_CRYPT_ARGS \ 304 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssz|s", \ 305 | &cipher, &cipher_len, &key, &key_len, &data, &data_len, &mode, &iv, &iv_len) == FAILURE) { \ 306 | return; \ 307 | } 308 | 309 | #define MCRYPT_GET_TD_ARG \ 310 | zval *mcryptind; \ 311 | php_mcrypt *pm; \ 312 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &mcryptind) == FAILURE) { \ 313 | return; \ 314 | } \ 315 | if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) { \ 316 | RETURN_FALSE; \ 317 | } 318 | 319 | #define MCRYPT_GET_MODE_DIR_ARGS(DIRECTORY) \ 320 | char *dir = NULL; \ 321 | size_t dir_len; \ 322 | char *module; \ 323 | size_t module_len; \ 324 | if (zend_parse_parameters (ZEND_NUM_ARGS(), \ 325 | "s|s", &module, &module_len, &dir, &dir_len) == FAILURE) { \ 326 | return; \ 327 | } 328 | 329 | #define MCRYPT_OPEN_MODULE_FAILED "Module initialization failed" 330 | 331 | #define MCRYPT_ENTRY2_2_4(a,b) REGISTER_STRING_CONSTANT("MCRYPT_" #a, b, CONST_PERSISTENT) 332 | #define MCRYPT_ENTRY2_4(a) MCRYPT_ENTRY_NAMED(a, a) 333 | 334 | #define PHP_MCRYPT_INIT_CHECK \ 335 | if (!pm->init) { \ 336 | php_error_docref(NULL, E_WARNING, "Operation disallowed prior to mcrypt_generic_init()."); \ 337 | RETURN_FALSE; \ 338 | } \ 339 | 340 | PHP_INI_BEGIN() 341 | STD_PHP_INI_ENTRY("mcrypt.algorithms_dir", NULL, PHP_INI_ALL, OnUpdateString, algorithms_dir, zend_mcrypt_globals, mcrypt_globals) 342 | STD_PHP_INI_ENTRY("mcrypt.modes_dir", NULL, PHP_INI_ALL, OnUpdateString, modes_dir, zend_mcrypt_globals, mcrypt_globals) 343 | PHP_INI_END() 344 | 345 | static void php_mcrypt_module_dtor(zend_resource *rsrc) /* {{{ */ 346 | { 347 | php_mcrypt *pm = (php_mcrypt *) rsrc->ptr; 348 | if (pm) { 349 | mcrypt_generic_deinit(pm->td); 350 | mcrypt_module_close(pm->td); 351 | efree(pm); 352 | pm = NULL; 353 | } 354 | } 355 | /* }}} */ 356 | 357 | static PHP_GINIT_FUNCTION(mcrypt) 358 | {/*{{{*/ 359 | #if defined(COMPILE_DL_MCRYPT) && defined(ZTS) 360 | ZEND_TSRMLS_CACHE_UPDATE(); 361 | #endif 362 | mcrypt_globals->fd[RANDOM] = -1; 363 | mcrypt_globals->fd[URANDOM] = -1; 364 | }/*}}}*/ 365 | 366 | static PHP_GSHUTDOWN_FUNCTION(mcrypt) 367 | {/*{{{*/ 368 | if (mcrypt_globals->fd[RANDOM] > 0) { 369 | close(mcrypt_globals->fd[RANDOM]); 370 | mcrypt_globals->fd[RANDOM] = -1; 371 | } 372 | 373 | if (mcrypt_globals->fd[URANDOM] > 0) { 374 | close(mcrypt_globals->fd[URANDOM]); 375 | mcrypt_globals->fd[URANDOM] = -1; 376 | } 377 | }/*}}}*/ 378 | 379 | static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */ 380 | { 381 | le_mcrypt = zend_register_list_destructors_ex(php_mcrypt_module_dtor, NULL, "mcrypt", module_number); 382 | 383 | /* modes for mcrypt_??? routines */ 384 | REGISTER_LONG_CONSTANT("MCRYPT_ENCRYPT", 0, CONST_PERSISTENT); 385 | REGISTER_LONG_CONSTANT("MCRYPT_DECRYPT", 1, CONST_PERSISTENT); 386 | 387 | /* sources for mcrypt_create_iv */ 388 | REGISTER_LONG_CONSTANT("MCRYPT_DEV_RANDOM", RANDOM, CONST_PERSISTENT); 389 | REGISTER_LONG_CONSTANT("MCRYPT_DEV_URANDOM", URANDOM, CONST_PERSISTENT); 390 | REGISTER_LONG_CONSTANT("MCRYPT_RAND", RAND, CONST_PERSISTENT); 391 | 392 | /* ciphers */ 393 | MCRYPT_ENTRY2_2_4(3DES, "tripledes"); 394 | MCRYPT_ENTRY2_2_4(ARCFOUR_IV, "arcfour-iv"); 395 | MCRYPT_ENTRY2_2_4(ARCFOUR, "arcfour"); 396 | MCRYPT_ENTRY2_2_4(BLOWFISH, "blowfish"); 397 | MCRYPT_ENTRY2_2_4(BLOWFISH_COMPAT, "blowfish-compat"); 398 | MCRYPT_ENTRY2_2_4(CAST_128, "cast-128"); 399 | MCRYPT_ENTRY2_2_4(CAST_256, "cast-256"); 400 | MCRYPT_ENTRY2_2_4(CRYPT, "crypt"); 401 | MCRYPT_ENTRY2_2_4(DES, "des"); 402 | MCRYPT_ENTRY2_2_4(ENIGNA, "crypt"); 403 | MCRYPT_ENTRY2_2_4(GOST, "gost"); 404 | MCRYPT_ENTRY2_2_4(LOKI97, "loki97"); 405 | MCRYPT_ENTRY2_2_4(PANAMA, "panama"); 406 | MCRYPT_ENTRY2_2_4(RC2, "rc2"); 407 | MCRYPT_ENTRY2_2_4(RIJNDAEL_128, "rijndael-128"); 408 | MCRYPT_ENTRY2_2_4(RIJNDAEL_192, "rijndael-192"); 409 | MCRYPT_ENTRY2_2_4(RIJNDAEL_256, "rijndael-256"); 410 | MCRYPT_ENTRY2_2_4(SAFER64, "safer-sk64"); 411 | MCRYPT_ENTRY2_2_4(SAFER128, "safer-sk128"); 412 | MCRYPT_ENTRY2_2_4(SAFERPLUS, "saferplus"); 413 | MCRYPT_ENTRY2_2_4(SERPENT, "serpent"); 414 | MCRYPT_ENTRY2_2_4(THREEWAY, "threeway"); 415 | MCRYPT_ENTRY2_2_4(TRIPLEDES, "tripledes"); 416 | MCRYPT_ENTRY2_2_4(TWOFISH, "twofish"); 417 | MCRYPT_ENTRY2_2_4(WAKE, "wake"); 418 | MCRYPT_ENTRY2_2_4(XTEA, "xtea"); 419 | 420 | MCRYPT_ENTRY2_2_4(IDEA, "idea"); 421 | MCRYPT_ENTRY2_2_4(MARS, "mars"); 422 | MCRYPT_ENTRY2_2_4(RC6, "rc6"); 423 | MCRYPT_ENTRY2_2_4(SKIPJACK, "skipjack"); 424 | /* modes */ 425 | MCRYPT_ENTRY2_2_4(MODE_CBC, "cbc"); 426 | MCRYPT_ENTRY2_2_4(MODE_CFB, "cfb"); 427 | MCRYPT_ENTRY2_2_4(MODE_ECB, "ecb"); 428 | MCRYPT_ENTRY2_2_4(MODE_NOFB, "nofb"); 429 | MCRYPT_ENTRY2_2_4(MODE_OFB, "ofb"); 430 | MCRYPT_ENTRY2_2_4(MODE_STREAM, "stream"); 431 | REGISTER_INI_ENTRIES(); 432 | 433 | php_stream_filter_register_factory("mcrypt.*", &php_mcrypt_filter_factory); 434 | php_stream_filter_register_factory("mdecrypt.*", &php_mcrypt_filter_factory); 435 | 436 | return SUCCESS; 437 | } 438 | /* }}} */ 439 | 440 | static PHP_MSHUTDOWN_FUNCTION(mcrypt) /* {{{ */ 441 | { 442 | php_stream_filter_unregister_factory("mcrypt.*"); 443 | php_stream_filter_unregister_factory("mdecrypt.*"); 444 | 445 | UNREGISTER_INI_ENTRIES(); 446 | return SUCCESS; 447 | } 448 | /* }}} */ 449 | 450 | #include "zend_smart_str.h" 451 | 452 | PHP_MINFO_FUNCTION(mcrypt) /* {{{ */ 453 | { 454 | char **modules; 455 | char mcrypt_api_no[16]; 456 | int i, count; 457 | smart_str tmp1 = {0}; 458 | smart_str tmp2 = {0}; 459 | 460 | modules = mcrypt_list_algorithms(MCG(algorithms_dir), &count); 461 | if (count == 0) { 462 | smart_str_appends(&tmp1, "none"); 463 | } 464 | for (i = 0; i < count; i++) { 465 | smart_str_appends(&tmp1, modules[i]); 466 | smart_str_appendc(&tmp1, ' '); 467 | } 468 | smart_str_0(&tmp1); 469 | mcrypt_free_p(modules, count); 470 | 471 | modules = mcrypt_list_modes(MCG(modes_dir), &count); 472 | if (count == 0) { 473 | smart_str_appends(&tmp2, "none"); 474 | } 475 | for (i = 0; i < count; i++) { 476 | smart_str_appends(&tmp2, modules[i]); 477 | smart_str_appendc(&tmp2, ' '); 478 | } 479 | smart_str_0 (&tmp2); 480 | mcrypt_free_p (modules, count); 481 | 482 | snprintf (mcrypt_api_no, 16, "%d", MCRYPT_API_VERSION); 483 | 484 | php_info_print_table_start(); 485 | php_info_print_table_header(2, "mcrypt support", "enabled"); 486 | php_info_print_table_header(2, "mcrypt_filter support", "enabled"); 487 | php_info_print_table_row(2, "Extension version", PHP_MCRYPT_VERSION); 488 | php_info_print_table_row(2, "Library version", LIBMCRYPT_VERSION); 489 | php_info_print_table_row(2, "Api No", mcrypt_api_no); 490 | php_info_print_table_row(2, "Supported ciphers", ZSTR_VAL(tmp1.s)); 491 | php_info_print_table_row(2, "Supported modes", ZSTR_VAL(tmp2.s)); 492 | smart_str_free(&tmp1); 493 | smart_str_free(&tmp2); 494 | 495 | php_info_print_table_end(); 496 | 497 | DISPLAY_INI_ENTRIES(); 498 | } 499 | /* }}} */ 500 | 501 | /* {{{ proto resource mcrypt_module_open(string cipher, string cipher_directory, string mode, string mode_directory) 502 | Opens the module of the algorithm and the mode to be used */ 503 | PHP_FUNCTION(mcrypt_module_open) 504 | { 505 | char *cipher, *cipher_dir; 506 | char *mode, *mode_dir; 507 | size_t cipher_len, cipher_dir_len; 508 | size_t mode_len, mode_dir_len; 509 | MCRYPT td; 510 | php_mcrypt *pm; 511 | 512 | if (zend_parse_parameters (ZEND_NUM_ARGS(), "ssss", 513 | &cipher, &cipher_len, &cipher_dir, &cipher_dir_len, 514 | &mode, &mode_len, &mode_dir, &mode_dir_len)) { 515 | return; 516 | } 517 | 518 | td = mcrypt_module_open ( 519 | cipher, 520 | cipher_dir_len > 0 ? cipher_dir : MCG(algorithms_dir), 521 | mode, 522 | mode_dir_len > 0 ? mode_dir : MCG(modes_dir) 523 | ); 524 | 525 | if (td == MCRYPT_FAILED) { 526 | php_error_docref(NULL, E_WARNING, "Could not open encryption module"); 527 | RETURN_FALSE; 528 | } else { 529 | pm = emalloc(sizeof(php_mcrypt)); 530 | pm->td = td; 531 | pm->init = 0; 532 | RETURN_RES(zend_register_resource(pm, le_mcrypt)); 533 | } 534 | } 535 | /* }}} */ 536 | 537 | /* {{{ proto int mcrypt_generic_init(resource td, string key, string iv) 538 | This function initializes all buffers for the specific module */ 539 | PHP_FUNCTION(mcrypt_generic_init) 540 | { 541 | char *key, *iv; 542 | size_t key_len, iv_len; 543 | zval *mcryptind; 544 | unsigned char *key_s, *iv_s; 545 | int max_key_size, key_size, iv_size; 546 | php_mcrypt *pm; 547 | int result = 0; 548 | 549 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "rss", &mcryptind, &key, &key_len, &iv, &iv_len) == FAILURE) { 550 | return; 551 | } 552 | 553 | if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) { 554 | RETURN_FALSE; 555 | } 556 | 557 | max_key_size = mcrypt_enc_get_key_size(pm->td); 558 | iv_size = mcrypt_enc_get_iv_size(pm->td); 559 | 560 | if (key_len == 0) { 561 | php_error_docref(NULL, E_WARNING, "Key size is 0"); 562 | } 563 | 564 | key_s = emalloc(key_len); 565 | memset(key_s, 0, key_len); 566 | 567 | iv_s = emalloc(iv_size + 1); 568 | memset(iv_s, 0, iv_size + 1); 569 | 570 | if (key_len > (size_t)max_key_size) { 571 | php_error_docref(NULL, E_WARNING, "Key size too large; supplied length: %zd, max: %d", key_len, max_key_size); 572 | key_size = max_key_size; 573 | } else { 574 | key_size = (int)key_len; 575 | } 576 | memcpy(key_s, key, key_len); 577 | 578 | if (iv_len != (size_t)iv_size) { 579 | php_error_docref(NULL, E_WARNING, "Iv size incorrect; supplied length: %zd, needed: %d", iv_len, iv_size); 580 | if (iv_len > (size_t)iv_size) { 581 | iv_len = iv_size; 582 | } 583 | } 584 | memcpy(iv_s, iv, iv_len); 585 | 586 | mcrypt_generic_deinit(pm->td); 587 | result = mcrypt_generic_init(pm->td, key_s, key_size, iv_s); 588 | 589 | /* If this function fails, close the mcrypt module to prevent crashes 590 | * when further functions want to access this resource */ 591 | if (result < 0) { 592 | zend_list_close(Z_RES_P(mcryptind)); 593 | switch (result) { 594 | case -3: 595 | php_error_docref(NULL, E_WARNING, "Key length incorrect"); 596 | break; 597 | case -4: 598 | php_error_docref(NULL, E_WARNING, "Memory allocation error"); 599 | break; 600 | case -1: 601 | default: 602 | php_error_docref(NULL, E_WARNING, "Unknown error"); 603 | break; 604 | } 605 | } else { 606 | pm->init = 1; 607 | } 608 | RETVAL_LONG(result); 609 | 610 | efree(iv_s); 611 | efree(key_s); 612 | } 613 | /* }}} */ 614 | 615 | /* {{{ proto string mcrypt_generic(resource td, string data) 616 | This function encrypts the plaintext */ 617 | PHP_FUNCTION(mcrypt_generic) 618 | { 619 | zval *mcryptind; 620 | char *data; 621 | size_t data_len; 622 | php_mcrypt *pm; 623 | zend_string* data_str; 624 | int block_size, data_size; 625 | 626 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &mcryptind, &data, &data_len) == FAILURE) { 627 | return; 628 | } 629 | 630 | if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) { 631 | RETURN_FALSE; 632 | } 633 | PHP_MCRYPT_INIT_CHECK 634 | 635 | if (data_len == 0) { 636 | php_error_docref(NULL, E_WARNING, "An empty string was passed"); 637 | RETURN_FALSE; 638 | } 639 | 640 | if (data_len > INT_MAX) { 641 | php_error_docref(NULL, E_WARNING, "Data size too large, %d maximum", INT_MAX); 642 | RETURN_FALSE; 643 | } 644 | /* Check blocksize */ 645 | if (mcrypt_enc_is_block_mode(pm->td) == 1) { /* It's a block algorithm */ 646 | block_size = mcrypt_enc_get_block_size(pm->td); 647 | data_size = ((((int)data_len - 1) / block_size) + 1) * block_size; 648 | if (data_size <= 0) { 649 | php_error_docref(NULL, E_WARNING, "Integer overflow in data size"); 650 | RETURN_FALSE; 651 | } 652 | data_str = zend_string_alloc(data_size, 0); 653 | memset(ZSTR_VAL(data_str), 0, data_size); 654 | memcpy(ZSTR_VAL(data_str), data, data_len); 655 | } else { /* It's not a block algorithm */ 656 | data_size = (int)data_len; 657 | data_str = zend_string_alloc(data_size, 0); 658 | memset(ZSTR_VAL(data_str), 0, data_size); 659 | memcpy(ZSTR_VAL(data_str), data, data_len); 660 | } 661 | 662 | mcrypt_generic(pm->td, ZSTR_VAL(data_str), data_size); 663 | ZSTR_VAL(data_str)[data_size] = '\0'; 664 | 665 | RETVAL_NEW_STR(data_str); 666 | } 667 | /* }}} */ 668 | 669 | /* {{{ proto string mdecrypt_generic(resource td, string data) 670 | This function decrypts the plaintext */ 671 | PHP_FUNCTION(mdecrypt_generic) 672 | { 673 | zval *mcryptind; 674 | char *data; 675 | size_t data_len; 676 | php_mcrypt *pm; 677 | char* data_s; 678 | int block_size, data_size; 679 | 680 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &mcryptind, &data, &data_len) == FAILURE) { 681 | return; 682 | } 683 | 684 | if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) { 685 | RETURN_FALSE; 686 | } 687 | PHP_MCRYPT_INIT_CHECK 688 | 689 | if (data_len == 0) { 690 | php_error_docref(NULL, E_WARNING, "An empty string was passed"); 691 | RETURN_FALSE; 692 | } 693 | 694 | /* Check blocksize */ 695 | if (data_len > INT_MAX) { 696 | php_error_docref(NULL, E_WARNING, "Data size too large, %d maximum", INT_MAX); 697 | RETURN_FALSE; 698 | } 699 | if (mcrypt_enc_is_block_mode(pm->td) == 1) { /* It's a block algorithm */ 700 | block_size = mcrypt_enc_get_block_size(pm->td); 701 | data_size = ((((int)data_len - 1) / block_size) + 1) * block_size; 702 | if (data_size <= 0) { 703 | php_error_docref(NULL, E_WARNING, "Integer overflow in data size"); 704 | RETURN_FALSE; 705 | } 706 | data_s = emalloc((size_t)data_size + 1); 707 | memset(data_s, 0, data_size); 708 | memcpy(data_s, data, data_len); 709 | } else { /* It's not a block algorithm */ 710 | data_size = (int)data_len; 711 | data_s = emalloc(data_size + 1); 712 | memset(data_s, 0, data_size); 713 | memcpy(data_s, data, data_len); 714 | } 715 | 716 | mdecrypt_generic(pm->td, data_s, data_size); 717 | 718 | RETVAL_STRINGL(data_s, data_size); 719 | efree(data_s); 720 | } 721 | /* }}} */ 722 | 723 | /* {{{ proto array mcrypt_enc_get_supported_key_sizes(resource td) 724 | This function decrypts the crypttext */ 725 | PHP_FUNCTION(mcrypt_enc_get_supported_key_sizes) 726 | { 727 | int i, count = 0; 728 | int *key_sizes; 729 | 730 | MCRYPT_GET_TD_ARG 731 | array_init(return_value); 732 | 733 | key_sizes = mcrypt_enc_get_supported_key_sizes(pm->td, &count); 734 | 735 | for (i = 0; i < count; i++) { 736 | add_index_long(return_value, i, key_sizes[i]); 737 | } 738 | 739 | mcrypt_free(key_sizes); 740 | } 741 | /* }}} */ 742 | 743 | /* {{{ proto int mcrypt_enc_self_test(resource td) 744 | This function runs the self test on the algorithm specified by the descriptor td */ 745 | PHP_FUNCTION(mcrypt_enc_self_test) 746 | { 747 | MCRYPT_GET_TD_ARG 748 | RETURN_LONG(mcrypt_enc_self_test(pm->td)); 749 | } 750 | /* }}} */ 751 | 752 | /* {{{ proto bool mcrypt_module_close(resource td) 753 | Free the descriptor td */ 754 | PHP_FUNCTION(mcrypt_module_close) 755 | { 756 | MCRYPT_GET_TD_ARG 757 | zend_list_close(Z_RES_P(mcryptind)); 758 | RETURN_TRUE; 759 | } 760 | /* }}} */ 761 | 762 | /* {{{ proto bool mcrypt_generic_deinit(resource td) 763 | This function terminates encrypt specified by the descriptor td */ 764 | PHP_FUNCTION(mcrypt_generic_deinit) 765 | { 766 | MCRYPT_GET_TD_ARG 767 | 768 | if (mcrypt_generic_deinit(pm->td) < 0) { 769 | php_error_docref(NULL, E_WARNING, "Could not terminate encryption specifier"); 770 | RETURN_FALSE; 771 | } 772 | pm->init = 0; 773 | RETURN_TRUE; 774 | } 775 | /* }}} */ 776 | 777 | /* {{{ proto bool mcrypt_enc_is_block_algorithm_mode(resource td) 778 | Returns TRUE if the mode is for use with block algorithms */ 779 | PHP_FUNCTION(mcrypt_enc_is_block_algorithm_mode) 780 | { 781 | MCRYPT_GET_TD_ARG 782 | 783 | if (mcrypt_enc_is_block_algorithm_mode(pm->td) == 1) { 784 | RETURN_TRUE; 785 | } else { 786 | RETURN_FALSE; 787 | } 788 | } 789 | /* }}} */ 790 | 791 | /* {{{ proto bool mcrypt_enc_is_block_algorithm(resource td) 792 | Returns TRUE if the alrogithm is a block algorithms */ 793 | PHP_FUNCTION(mcrypt_enc_is_block_algorithm) 794 | { 795 | MCRYPT_GET_TD_ARG 796 | 797 | if (mcrypt_enc_is_block_algorithm(pm->td) == 1) { 798 | RETURN_TRUE; 799 | } else { 800 | RETURN_FALSE; 801 | } 802 | } 803 | /* }}} */ 804 | 805 | /* {{{ proto bool mcrypt_enc_is_block_mode(resource td) 806 | Returns TRUE if the mode outputs blocks */ 807 | PHP_FUNCTION(mcrypt_enc_is_block_mode) 808 | { 809 | MCRYPT_GET_TD_ARG 810 | 811 | if (mcrypt_enc_is_block_mode(pm->td) == 1) { 812 | RETURN_TRUE; 813 | } else { 814 | RETURN_FALSE; 815 | } 816 | } 817 | /* }}} */ 818 | 819 | /* {{{ proto int mcrypt_enc_get_block_size(resource td) 820 | Returns the block size of the cipher specified by the descriptor td */ 821 | PHP_FUNCTION(mcrypt_enc_get_block_size) 822 | { 823 | MCRYPT_GET_TD_ARG 824 | RETURN_LONG(mcrypt_enc_get_block_size(pm->td)); 825 | } 826 | /* }}} */ 827 | 828 | /* {{{ proto int mcrypt_enc_get_key_size(resource td) 829 | Returns the maximum supported key size in bytes of the algorithm specified by the descriptor td */ 830 | PHP_FUNCTION(mcrypt_enc_get_key_size) 831 | { 832 | MCRYPT_GET_TD_ARG 833 | RETURN_LONG(mcrypt_enc_get_key_size(pm->td)); 834 | } 835 | /* }}} */ 836 | 837 | /* {{{ proto int mcrypt_enc_get_iv_size(resource td) 838 | Returns the size of the IV in bytes of the algorithm specified by the descriptor td */ 839 | PHP_FUNCTION(mcrypt_enc_get_iv_size) 840 | { 841 | MCRYPT_GET_TD_ARG 842 | RETURN_LONG(mcrypt_enc_get_iv_size(pm->td)); 843 | } 844 | /* }}} */ 845 | 846 | /* {{{ proto string mcrypt_enc_get_algorithms_name(resource td) 847 | Returns the name of the algorithm specified by the descriptor td */ 848 | PHP_FUNCTION(mcrypt_enc_get_algorithms_name) 849 | { 850 | char *name; 851 | MCRYPT_GET_TD_ARG 852 | 853 | name = mcrypt_enc_get_algorithms_name(pm->td); 854 | RETVAL_STRING(name); 855 | mcrypt_free(name); 856 | } 857 | /* }}} */ 858 | 859 | /* {{{ proto string mcrypt_enc_get_modes_name(resource td) 860 | Returns the name of the mode specified by the descriptor td */ 861 | PHP_FUNCTION(mcrypt_enc_get_modes_name) 862 | { 863 | char *name; 864 | MCRYPT_GET_TD_ARG 865 | 866 | name = mcrypt_enc_get_modes_name(pm->td); 867 | RETVAL_STRING(name); 868 | mcrypt_free(name); 869 | } 870 | /* }}} */ 871 | 872 | /* {{{ proto bool mcrypt_module_self_test(string algorithm [, string lib_dir]) 873 | Does a self test of the module "module" */ 874 | PHP_FUNCTION(mcrypt_module_self_test) 875 | { 876 | MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir); 877 | 878 | if (mcrypt_module_self_test(module, dir) == 0) { 879 | RETURN_TRUE; 880 | } else { 881 | RETURN_FALSE; 882 | } 883 | } 884 | /* }}} */ 885 | 886 | /* {{{ proto bool mcrypt_module_is_block_algorithm_mode(string mode [, string lib_dir]) 887 | Returns TRUE if the mode is for use with block algorithms */ 888 | PHP_FUNCTION(mcrypt_module_is_block_algorithm_mode) 889 | { 890 | MCRYPT_GET_MODE_DIR_ARGS(modes_dir) 891 | 892 | if (mcrypt_module_is_block_algorithm_mode(module, dir) == 1) { 893 | RETURN_TRUE; 894 | } else { 895 | RETURN_FALSE; 896 | } 897 | } 898 | /* }}} */ 899 | 900 | /* {{{ proto bool mcrypt_module_is_block_algorithm(string algorithm [, string lib_dir]) 901 | Returns TRUE if the algorithm is a block algorithm */ 902 | PHP_FUNCTION(mcrypt_module_is_block_algorithm) 903 | { 904 | MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir) 905 | 906 | if (mcrypt_module_is_block_algorithm(module, dir) == 1) { 907 | RETURN_TRUE; 908 | } else { 909 | RETURN_FALSE; 910 | } 911 | } 912 | /* }}} */ 913 | 914 | /* {{{ proto bool mcrypt_module_is_block_mode(string mode [, string lib_dir]) 915 | Returns TRUE if the mode outputs blocks of bytes */ 916 | PHP_FUNCTION(mcrypt_module_is_block_mode) 917 | { 918 | MCRYPT_GET_MODE_DIR_ARGS(modes_dir) 919 | 920 | if (mcrypt_module_is_block_mode(module, dir) == 1) { 921 | RETURN_TRUE; 922 | } else { 923 | RETURN_FALSE; 924 | } 925 | } 926 | /* }}} */ 927 | 928 | /* {{{ proto int mcrypt_module_get_algo_block_size(string algorithm [, string lib_dir]) 929 | Returns the block size of the algorithm */ 930 | PHP_FUNCTION(mcrypt_module_get_algo_block_size) 931 | { 932 | MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir) 933 | 934 | RETURN_LONG(mcrypt_module_get_algo_block_size(module, dir)); 935 | } 936 | /* }}} */ 937 | 938 | /* {{{ proto int mcrypt_module_get_algo_key_size(string algorithm [, string lib_dir]) 939 | Returns the maximum supported key size of the algorithm */ 940 | PHP_FUNCTION(mcrypt_module_get_algo_key_size) 941 | { 942 | MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir); 943 | 944 | RETURN_LONG(mcrypt_module_get_algo_key_size(module, dir)); 945 | } 946 | /* }}} */ 947 | 948 | /* {{{ proto array mcrypt_module_get_supported_key_sizes(string algorithm [, string lib_dir]) 949 | This function decrypts the crypttext */ 950 | PHP_FUNCTION(mcrypt_module_get_supported_key_sizes) 951 | { 952 | int i, count = 0; 953 | int *key_sizes; 954 | 955 | MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir) 956 | array_init(return_value); 957 | 958 | key_sizes = mcrypt_module_get_algo_supported_key_sizes(module, dir, &count); 959 | 960 | for (i = 0; i < count; i++) { 961 | add_index_long(return_value, i, key_sizes[i]); 962 | } 963 | mcrypt_free(key_sizes); 964 | } 965 | /* }}} */ 966 | 967 | /* {{{ proto array mcrypt_list_algorithms([string lib_dir]) 968 | List all algorithms in "module_dir" */ 969 | PHP_FUNCTION(mcrypt_list_algorithms) 970 | { 971 | char **modules; 972 | char *lib_dir = MCG(algorithms_dir); 973 | size_t lib_dir_len; 974 | int i, count; 975 | 976 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", 977 | &lib_dir, &lib_dir_len) == FAILURE) { 978 | return; 979 | } 980 | 981 | array_init(return_value); 982 | modules = mcrypt_list_algorithms(lib_dir, &count); 983 | 984 | if (count == 0) { 985 | php_error_docref(NULL, E_WARNING, "No algorithms found in module dir"); 986 | } 987 | for (i = 0; i < count; i++) { 988 | add_index_string(return_value, i, modules[i]); 989 | } 990 | mcrypt_free_p(modules, count); 991 | } 992 | /* }}} */ 993 | 994 | /* {{{ proto array mcrypt_list_modes([string lib_dir]) 995 | List all modes "module_dir" */ 996 | PHP_FUNCTION(mcrypt_list_modes) 997 | { 998 | char **modules; 999 | char *lib_dir = MCG(modes_dir); 1000 | size_t lib_dir_len; 1001 | int i, count; 1002 | 1003 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", 1004 | &lib_dir, &lib_dir_len) == FAILURE) { 1005 | return; 1006 | } 1007 | 1008 | array_init(return_value); 1009 | modules = mcrypt_list_modes(lib_dir, &count); 1010 | 1011 | if (count == 0) { 1012 | php_error_docref(NULL, E_WARNING, "No modes found in module dir"); 1013 | } 1014 | for (i = 0; i < count; i++) { 1015 | add_index_string(return_value, i, modules[i]); 1016 | } 1017 | mcrypt_free_p(modules, count); 1018 | } 1019 | /* }}} */ 1020 | 1021 | /* {{{ proto int mcrypt_get_key_size(string cipher, string module) 1022 | Get the key size of cipher */ 1023 | PHP_FUNCTION(mcrypt_get_key_size) 1024 | { 1025 | char *cipher; 1026 | char *module; 1027 | size_t cipher_len, module_len; 1028 | char *cipher_dir_string; 1029 | char *module_dir_string; 1030 | MCRYPT td; 1031 | 1032 | MCRYPT_GET_INI 1033 | 1034 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", 1035 | &cipher, &cipher_len, &module, &module_len) == FAILURE) { 1036 | return; 1037 | } 1038 | 1039 | td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string); 1040 | if (td != MCRYPT_FAILED) { 1041 | RETVAL_LONG(mcrypt_enc_get_key_size(td)); 1042 | mcrypt_module_close(td); 1043 | } else { 1044 | php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); 1045 | RETURN_FALSE; 1046 | } 1047 | } 1048 | /* }}} */ 1049 | 1050 | /* {{{ proto int mcrypt_get_block_size(string cipher, string module) 1051 | Get the key size of cipher */ 1052 | PHP_FUNCTION(mcrypt_get_block_size) 1053 | { 1054 | char *cipher; 1055 | char *module; 1056 | size_t cipher_len, module_len; 1057 | char *cipher_dir_string; 1058 | char *module_dir_string; 1059 | MCRYPT td; 1060 | 1061 | MCRYPT_GET_INI 1062 | 1063 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", 1064 | &cipher, &cipher_len, &module, &module_len) == FAILURE) { 1065 | return; 1066 | } 1067 | 1068 | td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string); 1069 | if (td != MCRYPT_FAILED) { 1070 | RETVAL_LONG(mcrypt_enc_get_block_size(td)); 1071 | mcrypt_module_close(td); 1072 | } else { 1073 | php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); 1074 | RETURN_FALSE; 1075 | } 1076 | } 1077 | /* }}} */ 1078 | 1079 | /* {{{ proto int mcrypt_get_iv_size(string cipher, string module) 1080 | Get the IV size of cipher (Usually the same as the blocksize) */ 1081 | PHP_FUNCTION(mcrypt_get_iv_size) 1082 | { 1083 | char *cipher; 1084 | char *module; 1085 | size_t cipher_len, module_len; 1086 | char *cipher_dir_string; 1087 | char *module_dir_string; 1088 | MCRYPT td; 1089 | 1090 | MCRYPT_GET_INI 1091 | 1092 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", 1093 | &cipher, &cipher_len, &module, &module_len) == FAILURE) { 1094 | return; 1095 | } 1096 | 1097 | td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string); 1098 | if (td != MCRYPT_FAILED) { 1099 | RETVAL_LONG(mcrypt_enc_get_iv_size(td)); 1100 | mcrypt_module_close(td); 1101 | } else { 1102 | php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); 1103 | RETURN_FALSE; 1104 | } 1105 | } 1106 | /* }}} */ 1107 | 1108 | /* {{{ proto string mcrypt_get_cipher_name(string cipher) 1109 | Get the key size of cipher */ 1110 | PHP_FUNCTION(mcrypt_get_cipher_name) 1111 | { 1112 | char *cipher_dir_string; 1113 | char *module_dir_string; 1114 | char *cipher_name; 1115 | char *cipher; 1116 | size_t cipher_len; 1117 | MCRYPT td; 1118 | 1119 | MCRYPT_GET_INI 1120 | 1121 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", 1122 | &cipher, &cipher_len) == FAILURE) { 1123 | return; 1124 | } 1125 | 1126 | /* The code below is actually not very nice, but I didn't see a better 1127 | * method */ 1128 | td = mcrypt_module_open(cipher, cipher_dir_string, "ecb", module_dir_string); 1129 | if (td != MCRYPT_FAILED) { 1130 | cipher_name = mcrypt_enc_get_algorithms_name(td); 1131 | mcrypt_module_close(td); 1132 | RETVAL_STRING(cipher_name); 1133 | mcrypt_free(cipher_name); 1134 | } else { 1135 | td = mcrypt_module_open(cipher, cipher_dir_string, "stream", module_dir_string); 1136 | if (td != MCRYPT_FAILED) { 1137 | cipher_name = mcrypt_enc_get_algorithms_name(td); 1138 | mcrypt_module_close(td); 1139 | RETVAL_STRING(cipher_name); 1140 | mcrypt_free(cipher_name); 1141 | } else { 1142 | php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); 1143 | RETURN_FALSE; 1144 | } 1145 | } 1146 | } 1147 | /* }}} */ 1148 | 1149 | static char *php_mcrypt_get_key_size_str( 1150 | int max_key_size, const int *key_sizes, int key_size_count) /* {{{ */ 1151 | { 1152 | if (key_size_count == 0) { 1153 | char *str; 1154 | spprintf(&str, 0, "Only keys of size 1 to %d supported", max_key_size); 1155 | return str; 1156 | } else if (key_size_count == 1) { 1157 | char *str; 1158 | spprintf(&str, 0, "Only keys of size %d supported", key_sizes[0]); 1159 | return str; 1160 | } else { 1161 | int i; 1162 | char *result = NULL; 1163 | smart_str str = {0}; 1164 | smart_str_appends(&str, "Only keys of sizes "); 1165 | 1166 | for (i = 0; i < key_size_count; ++i) { 1167 | if (i == key_size_count - 1) { 1168 | smart_str_appends(&str, " or "); 1169 | } else if (i != 0) { 1170 | smart_str_appends(&str, ", "); 1171 | } 1172 | 1173 | smart_str_append_long(&str, key_sizes[i]); 1174 | } 1175 | 1176 | smart_str_appends(&str, " supported"); 1177 | smart_str_0(&str); 1178 | result = estrndup(ZSTR_VAL(str.s), ZSTR_LEN(str.s)); 1179 | smart_str_free(&str); 1180 | 1181 | return result; 1182 | } 1183 | } 1184 | /* }}} */ 1185 | 1186 | static zend_bool php_mcrypt_is_valid_key_size( 1187 | int key_size, int max_key_size, int *key_sizes, int key_size_count) /* {{{ */ 1188 | { 1189 | int i; 1190 | 1191 | if (key_size <= 0 || key_size > max_key_size) { 1192 | return 0; 1193 | } 1194 | 1195 | if (key_size_count == 0) { 1196 | /* All key sizes are valid */ 1197 | return 1; 1198 | } 1199 | 1200 | for (i = 0; i < key_size_count; i++) { 1201 | if (key_sizes[i] == key_size) { 1202 | return 1; 1203 | } 1204 | } 1205 | 1206 | return 0; 1207 | } 1208 | /* }}} */ 1209 | 1210 | static int php_mcrypt_ensure_valid_key_size(MCRYPT td, int key_size) /* {{{ */ 1211 | { 1212 | int key_size_count; 1213 | int max_key_size = mcrypt_enc_get_key_size(td); 1214 | int *key_sizes = mcrypt_enc_get_supported_key_sizes(td, &key_size_count); 1215 | 1216 | zend_bool is_valid_key_size = php_mcrypt_is_valid_key_size( 1217 | key_size, max_key_size, key_sizes, key_size_count 1218 | ); 1219 | if (!is_valid_key_size) { 1220 | char *key_size_str = php_mcrypt_get_key_size_str( 1221 | max_key_size, key_sizes, key_size_count 1222 | ); 1223 | php_error_docref(NULL, E_WARNING, 1224 | "Key of size %d not supported by this algorithm. %s", key_size, key_size_str 1225 | ); 1226 | efree(key_size_str); 1227 | } 1228 | 1229 | if (key_sizes) { 1230 | mcrypt_free(key_sizes); 1231 | } 1232 | 1233 | return is_valid_key_size ? SUCCESS : FAILURE; 1234 | } 1235 | /* }}} */ 1236 | 1237 | static int php_mcrypt_ensure_valid_iv(MCRYPT td, const char *iv, int iv_size) /* {{{ */ 1238 | { 1239 | if (mcrypt_enc_mode_has_iv(td) == 1) { 1240 | int expected_iv_size = mcrypt_enc_get_iv_size(td); 1241 | if (expected_iv_size == 0) { 1242 | /* Algorithm does not use IV, even though mode supports it */ 1243 | return SUCCESS; 1244 | } 1245 | 1246 | if (!iv) { 1247 | php_error_docref(NULL, E_WARNING, 1248 | "Encryption mode requires an initialization vector of size %d", expected_iv_size 1249 | ); 1250 | return FAILURE; 1251 | } 1252 | 1253 | if (iv_size != expected_iv_size) { 1254 | php_error_docref(NULL, E_WARNING, 1255 | "Received initialization vector of size %d, but size %d is required " 1256 | "for this encryption mode", iv_size, expected_iv_size 1257 | ); 1258 | return FAILURE; 1259 | } 1260 | } 1261 | 1262 | return SUCCESS; 1263 | } 1264 | /* }}} */ 1265 | 1266 | static void php_mcrypt_do_crypt(char* cipher, const char *key, size_t key_len, const char *data, size_t data_len, char *mode, const char *iv, size_t iv_len, size_t dencrypt, zval* return_value) /* {{{ */ 1267 | { 1268 | char *cipher_dir_string; 1269 | char *module_dir_string; 1270 | zend_long data_size; 1271 | char *data_s; 1272 | MCRYPT td; 1273 | 1274 | MCRYPT_GET_INI 1275 | 1276 | td = mcrypt_module_open(cipher, cipher_dir_string, mode, module_dir_string); 1277 | if (td == MCRYPT_FAILED) { 1278 | php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); 1279 | RETURN_FALSE; 1280 | } 1281 | 1282 | if (php_mcrypt_ensure_valid_key_size(td, (int)key_len) == FAILURE) { 1283 | mcrypt_module_close(td); 1284 | RETURN_FALSE; 1285 | } 1286 | 1287 | if (php_mcrypt_ensure_valid_iv(td, iv, (int)iv_len) == FAILURE) { 1288 | mcrypt_module_close(td); 1289 | RETURN_FALSE; 1290 | } 1291 | 1292 | /* Check blocksize */ 1293 | if (mcrypt_enc_is_block_mode(td) == 1) { /* It's a block algorithm */ 1294 | int block_size = mcrypt_enc_get_block_size(td); 1295 | data_size = ((((zend_long)data_len - 1) / block_size) + 1) * block_size; 1296 | data_s = emalloc(data_size + 1); 1297 | memset(data_s, 0, data_size); 1298 | memcpy(data_s, data, data_len); 1299 | } else { /* It's not a block algorithm */ 1300 | data_size = data_len; 1301 | data_s = emalloc(data_size + 1); 1302 | memcpy(data_s, data, data_len); 1303 | } 1304 | 1305 | if (mcrypt_generic_init(td, (void *) key, (int)key_len, (void *) iv) < 0) { 1306 | efree(data_s); 1307 | zend_throw_error(NULL, "Mcrypt initialisation failed"); 1308 | mcrypt_module_close(td); 1309 | RETURN_FALSE; 1310 | } 1311 | 1312 | if (dencrypt == MCRYPT_ENCRYPT) { 1313 | mcrypt_generic(td, data_s, (int)data_size); 1314 | } else { 1315 | mdecrypt_generic(td, data_s, (int)data_size); 1316 | } 1317 | 1318 | data_s[data_size] = 0; 1319 | 1320 | RETVAL_STRINGL(data_s, data_size); 1321 | efree(data_s); 1322 | 1323 | /* freeing vars */ 1324 | mcrypt_generic_end(td); 1325 | } 1326 | /* }}} */ 1327 | 1328 | /* {{{ proto string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) 1329 | OFB crypt/decrypt data using key key with cipher cipher starting with iv */ 1330 | PHP_FUNCTION(mcrypt_encrypt) 1331 | { 1332 | char *cipher, *key, *data, *mode, *iv = NULL; 1333 | size_t cipher_len, key_len, data_len, mode_len, iv_len = 0; 1334 | 1335 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssss|s", &cipher, &cipher_len, 1336 | &key, &key_len, &data, &data_len, &mode, &mode_len, &iv, &iv_len) == FAILURE) { 1337 | return; 1338 | } 1339 | 1340 | php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, mode, iv, iv_len, MCRYPT_ENCRYPT, return_value); 1341 | } 1342 | /* }}} */ 1343 | 1344 | /* {{{ proto string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) 1345 | OFB crypt/decrypt data using key key with cipher cipher starting with iv */ 1346 | PHP_FUNCTION(mcrypt_decrypt) 1347 | { 1348 | char *cipher, *key, *data, *mode, *iv = NULL; 1349 | size_t cipher_len, key_len, data_len, mode_len, iv_len = 0; 1350 | 1351 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssss|s", &cipher, &cipher_len, 1352 | &key, &key_len, &data, &data_len, &mode, &mode_len, &iv, &iv_len) == FAILURE) { 1353 | return; 1354 | } 1355 | 1356 | php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, mode, iv, iv_len, MCRYPT_DECRYPT, return_value); 1357 | } 1358 | /* }}} */ 1359 | 1360 | /* {{{ proto string mcrypt_create_iv(int size, int source) 1361 | Create an initialization vector (IV) */ 1362 | PHP_FUNCTION(mcrypt_create_iv) 1363 | { 1364 | char *iv; 1365 | zend_long source = URANDOM; 1366 | zend_long size; 1367 | int n = 0; 1368 | 1369 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &size, &source) == FAILURE) { 1370 | return; 1371 | } 1372 | 1373 | if (size <= 0 || size >= INT_MAX) { 1374 | php_error_docref(NULL, E_WARNING, "Cannot create an IV with a size of less than 1 or greater than %d", INT_MAX); 1375 | RETURN_FALSE; 1376 | } 1377 | 1378 | iv = ecalloc(size + 1, 1); 1379 | 1380 | if (source == RANDOM || source == URANDOM) { 1381 | #if PHP_WIN32 1382 | /* random/urandom equivalent on Windows */ 1383 | BYTE *iv_b = (BYTE *) iv; 1384 | if (php_win32_get_random_bytes(iv_b, (size_t) size) == FAILURE){ 1385 | efree(iv); 1386 | php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); 1387 | RETURN_FALSE; 1388 | } 1389 | n = (int)size; 1390 | #else 1391 | int *fd = &MCG(fd[source]); 1392 | size_t read_bytes = 0; 1393 | 1394 | if (*fd < 0) { 1395 | *fd = open(source == RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY); 1396 | if (*fd < 0) { 1397 | efree(iv); 1398 | php_error_docref(NULL, E_WARNING, "Cannot open source device"); 1399 | RETURN_FALSE; 1400 | } 1401 | } 1402 | 1403 | while ((zend_long)read_bytes < size) { 1404 | n = read(*fd, iv + read_bytes, size - read_bytes); 1405 | if (n <= 0) { 1406 | break; 1407 | } 1408 | read_bytes += n; 1409 | } 1410 | n = read_bytes; 1411 | 1412 | if (n < size) { 1413 | efree(iv); 1414 | php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); 1415 | RETURN_FALSE; 1416 | } 1417 | #endif 1418 | } else { 1419 | n = (int)size; 1420 | while (size) { 1421 | #if PHP_VERSION_ID >= 80400 1422 | iv[--size] = (char) php_mt_rand_range(0, 255); 1423 | #else 1424 | iv[--size] = (char) (255.0 * php_rand() / RAND_MAX); 1425 | #endif 1426 | } 1427 | } 1428 | RETVAL_STRINGL(iv, n); 1429 | efree(iv); 1430 | } 1431 | /* }}} */ 1432 | 1433 | #endif 1434 | 1435 | /* 1436 | * Local variables: 1437 | * tab-width: 4 1438 | * c-basic-offset: 4 1439 | * End: 1440 | * vim600: sw=4 ts=4 fdm=marker 1441 | * vim<600: sw=4 ts=4 1442 | */ 1443 | --------------------------------------------------------------------------------