diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2013-03-14 05:42:27 +0000 |
---|---|---|
committer | <> | 2013-04-03 16:25:08 +0000 |
commit | c4dd7a1a684490673e25aaf4fabec5df138854c4 (patch) | |
tree | 4d57c44caae4480efff02b90b9be86f44bf25409 /ext/mcrypt | |
download | php2-master.tar.gz |
Imported from /home/lorry/working-area/delta_php2/php-5.4.13.tar.bz2.HEADphp-5.4.13master
Diffstat (limited to 'ext/mcrypt')
84 files changed, 8459 insertions, 0 deletions
diff --git a/ext/mcrypt/CREDITS b/ext/mcrypt/CREDITS new file mode 100644 index 0000000..00f871b --- /dev/null +++ b/ext/mcrypt/CREDITS @@ -0,0 +1,2 @@ +mcrypt +Sascha Schumann, Derick Rethans diff --git a/ext/mcrypt/TODO b/ext/mcrypt/TODO new file mode 100644 index 0000000..a9d2d3a --- /dev/null +++ b/ext/mcrypt/TODO @@ -0,0 +1,6 @@ +/* $Id$ */ + +ToDo: +- Convert to zend_parse_parameters +- Unify error handling +- Implement encryption streams diff --git a/ext/mcrypt/config.m4 b/ext/mcrypt/config.m4 new file mode 100644 index 0000000..cc68d86 --- /dev/null +++ b/ext/mcrypt/config.m4 @@ -0,0 +1,59 @@ +dnl +dnl $Id$ +dnl + +AC_DEFUN([PHP_MCRYPT_CHECK_VERSION],[ + old_CPPFLAGS=$CPPFLAGS + CPPFLAGS=-I$MCRYPT_DIR/include + AC_MSG_CHECKING(for libmcrypt version) + AC_EGREP_CPP(yes,[ +#include <mcrypt.h> +#if MCRYPT_API_VERSION >= 20021217 + yes +#endif + ],[ + AC_MSG_RESULT(>= 2.5.6) + ],[ + AC_MSG_ERROR(libmcrypt version 2.5.6 or greater required.) + ]) + CPPFLAGS=$old_CPPFLAGS +]) + + +PHP_ARG_WITH(mcrypt, for mcrypt support, +[ --with-mcrypt[=DIR] Include mcrypt support]) + +if test "$PHP_MCRYPT" != "no"; then + for i in $PHP_MCRYPT /usr/local /usr; do + test -f $i/include/mcrypt.h && MCRYPT_DIR=$i && break + done + + if test -z "$MCRYPT_DIR"; then + AC_MSG_ERROR(mcrypt.h not found. Please reinstall libmcrypt.) + fi + + PHP_MCRYPT_CHECK_VERSION + + PHP_CHECK_LIBRARY(mcrypt, mcrypt_module_open, + [ + PHP_ADD_LIBRARY(ltdl,, MCRYPT_SHARED_LIBADD) + AC_DEFINE(HAVE_LIBMCRYPT,1,[ ]) + ],[ + PHP_CHECK_LIBRARY(mcrypt, mcrypt_module_open, + [ + AC_DEFINE(HAVE_LIBMCRYPT,1,[ ]) + ],[ + AC_MSG_ERROR([Sorry, I was not able to diagnose which libmcrypt version you have installed.]) + ],[ + -L$MCRYPT_DIR/$PHP_LIBDIR + ]) + ],[ + -L$MCRYPT_DIR/$PHP_LIBDIR -lltdl + ]) + + PHP_ADD_LIBRARY_WITH_PATH(mcrypt, $MCRYPT_DIR/$PHP_LIBDIR, MCRYPT_SHARED_LIBADD) + PHP_ADD_INCLUDE($MCRYPT_DIR/include) + + PHP_SUBST(MCRYPT_SHARED_LIBADD) + PHP_NEW_EXTENSION(mcrypt, mcrypt.c mcrypt_filter.c, $ext_shared) +fi diff --git a/ext/mcrypt/config.w32 b/ext/mcrypt/config.w32 new file mode 100644 index 0000000..8d30db7 --- /dev/null +++ b/ext/mcrypt/config.w32 @@ -0,0 +1,19 @@ +// $Id$ +// vim:ft=javascript + +ARG_WITH("mcrypt", "mcrypt support", "no"); + +if (PHP_MCRYPT != "no") { + + if (CHECK_HEADER_ADD_INCLUDE('mcrypt.h', 'CFLAGS_MCRYPT') && + CHECK_LIB('libmcrypt_a.lib;libmcrypt.lib', 'mcrypt') && + CHECK_LIB('Advapi32.lib', 'mcrypt') + ) { + + EXTENSION('mcrypt', 'mcrypt.c mcrypt_filter.c', false); + AC_DEFINE('HAVE_LIBMCRYPT', 1); + AC_DEFINE('HAVE_LIBMCRYPT24', 1); + } else { + WARNING("mcrypt not enabled; libraries and headers not found"); + } +} diff --git a/ext/mcrypt/mcrypt.c b/ext/mcrypt/mcrypt.c new file mode 100644 index 0000000..0fcdddd --- /dev/null +++ b/ext/mcrypt/mcrypt.c @@ -0,0 +1,1445 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sascha Schumann <sascha@schumann.cx> | + | Derick Rethans <derick@derickrethans.nl> | + +----------------------------------------------------------------------+ + */ +/* $Id$ */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" + +#if HAVE_LIBMCRYPT + +#if PHP_WIN32 +# include "win32/winutil.h" +#endif + +#include "php_mcrypt.h" +#include "fcntl.h" + +#define NON_FREE +#define MCRYPT2 +#include "mcrypt.h" +#include "php_ini.h" +#include "php_globals.h" +#include "ext/standard/info.h" +#include "ext/standard/php_rand.h" +#include "php_mcrypt_filter.h" + +static int le_mcrypt; + +typedef struct _php_mcrypt { + MCRYPT td; + zend_bool init; +} php_mcrypt; + +/* {{{ arginfo */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_open, 0, 0, 4) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, cipher_directory) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, mode_directory) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_generic_init, 0, 0, 3) + ZEND_ARG_INFO(0, td) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, iv) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_generic, 0, 0, 2) + ZEND_ARG_INFO(0, td) + ZEND_ARG_INFO(0, data) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mdecrypt_generic, 0, 0, 2) + ZEND_ARG_INFO(0, td) + ZEND_ARG_INFO(0, data) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_supported_key_sizes, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_self_test, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_close, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_generic_deinit, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_is_block_algorithm_mode, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_is_block_algorithm, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_is_block_mode, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_block_size, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_key_size, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_iv_size, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_algorithms_name, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_modes_name, 0, 0, 1) + ZEND_ARG_INFO(0, td) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_self_test, 0, 0, 1) + ZEND_ARG_INFO(0, algorithm) + ZEND_ARG_INFO(0, lib_dir) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_is_block_algorithm_mode, 0, 0, 1) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, lib_dir) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_is_block_algorithm, 0, 0, 1) + ZEND_ARG_INFO(0, algorithm) + ZEND_ARG_INFO(0, lib_dir) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_is_block_mode, 0, 0, 1) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, lib_dir) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_get_algo_block_size, 0, 0, 1) + ZEND_ARG_INFO(0, algorithm) + ZEND_ARG_INFO(0, lib_dir) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_get_algo_key_size, 0, 0, 1) + ZEND_ARG_INFO(0, algorithm) + ZEND_ARG_INFO(0, lib_dir) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_get_supported_key_sizes, 0, 0, 1) + ZEND_ARG_INFO(0, algorithm) + ZEND_ARG_INFO(0, lib_dir) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_list_algorithms, 0, 0, 0) + ZEND_ARG_INFO(0, lib_dir) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_list_modes, 0, 0, 0) + ZEND_ARG_INFO(0, lib_dir) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_key_size, 0, 0, 2) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, module) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_block_size, 0, 0, 2) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, module) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_iv_size, 0, 0, 2) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, module) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_cipher_name, 0, 0, 1) + ZEND_ARG_INFO(0, cipher) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_encrypt, 0, 0, 5) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, iv) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_decrypt, 0, 0, 5) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, iv) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_ecb, 0, 0, 5) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, iv) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_cbc, 0, 0, 5) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, iv) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_cfb, 0, 0, 5) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, iv) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_ofb, 0, 0, 5) + ZEND_ARG_INFO(0, cipher) + ZEND_ARG_INFO(0, key) + ZEND_ARG_INFO(0, data) + ZEND_ARG_INFO(0, mode) + ZEND_ARG_INFO(0, iv) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_create_iv, 0, 0, 2) + ZEND_ARG_INFO(0, size) + ZEND_ARG_INFO(0, source) +ZEND_END_ARG_INFO() +/* }}} */ + +const zend_function_entry mcrypt_functions[] = { /* {{{ */ + PHP_FE(mcrypt_ecb, arginfo_mcrypt_ecb) + PHP_FE(mcrypt_cbc, arginfo_mcrypt_cbc) + PHP_FE(mcrypt_cfb, arginfo_mcrypt_cfb) + PHP_FE(mcrypt_ofb, arginfo_mcrypt_ofb) + PHP_FE(mcrypt_get_key_size, arginfo_mcrypt_get_key_size) + PHP_FE(mcrypt_get_block_size, arginfo_mcrypt_get_block_size) + PHP_FE(mcrypt_get_cipher_name, arginfo_mcrypt_get_cipher_name) + PHP_FE(mcrypt_create_iv, arginfo_mcrypt_create_iv) + + PHP_FE(mcrypt_list_algorithms, arginfo_mcrypt_list_algorithms) + PHP_FE(mcrypt_list_modes, arginfo_mcrypt_list_modes) + PHP_FE(mcrypt_get_iv_size, arginfo_mcrypt_get_iv_size) + PHP_FE(mcrypt_encrypt, arginfo_mcrypt_encrypt) + PHP_FE(mcrypt_decrypt, arginfo_mcrypt_decrypt) + + PHP_FE(mcrypt_module_open, arginfo_mcrypt_module_open) + PHP_FE(mcrypt_generic_init, arginfo_mcrypt_generic_init) + PHP_FE(mcrypt_generic, arginfo_mcrypt_generic) + PHP_FE(mdecrypt_generic, arginfo_mdecrypt_generic) + PHP_DEP_FALIAS(mcrypt_generic_end, mcrypt_generic_deinit, arginfo_mcrypt_generic_deinit) + PHP_FE(mcrypt_generic_deinit, arginfo_mcrypt_generic_deinit) + + PHP_FE(mcrypt_enc_self_test, arginfo_mcrypt_enc_self_test) + PHP_FE(mcrypt_enc_is_block_algorithm_mode, arginfo_mcrypt_enc_is_block_algorithm_mode) + PHP_FE(mcrypt_enc_is_block_algorithm, arginfo_mcrypt_enc_is_block_algorithm) + PHP_FE(mcrypt_enc_is_block_mode, arginfo_mcrypt_enc_is_block_mode) + PHP_FE(mcrypt_enc_get_block_size, arginfo_mcrypt_enc_get_block_size) + PHP_FE(mcrypt_enc_get_key_size, arginfo_mcrypt_enc_get_key_size) + PHP_FE(mcrypt_enc_get_supported_key_sizes, arginfo_mcrypt_enc_get_supported_key_sizes) + PHP_FE(mcrypt_enc_get_iv_size, arginfo_mcrypt_enc_get_iv_size) + PHP_FE(mcrypt_enc_get_algorithms_name, arginfo_mcrypt_enc_get_algorithms_name) + PHP_FE(mcrypt_enc_get_modes_name, arginfo_mcrypt_enc_get_modes_name) + PHP_FE(mcrypt_module_self_test, arginfo_mcrypt_module_self_test) + + PHP_FE(mcrypt_module_is_block_algorithm_mode, arginfo_mcrypt_module_is_block_algorithm_mode) + PHP_FE(mcrypt_module_is_block_algorithm, arginfo_mcrypt_module_is_block_algorithm) + PHP_FE(mcrypt_module_is_block_mode, arginfo_mcrypt_module_is_block_mode) + PHP_FE(mcrypt_module_get_algo_block_size, arginfo_mcrypt_module_get_algo_block_size) + PHP_FE(mcrypt_module_get_algo_key_size, arginfo_mcrypt_module_get_algo_key_size) + PHP_FE(mcrypt_module_get_supported_key_sizes, arginfo_mcrypt_module_get_supported_key_sizes) + + PHP_FE(mcrypt_module_close, arginfo_mcrypt_module_close) + PHP_FE_END +}; +/* }}} */ + +static PHP_MINFO_FUNCTION(mcrypt); +static PHP_MINIT_FUNCTION(mcrypt); +static PHP_MSHUTDOWN_FUNCTION(mcrypt); + +ZEND_DECLARE_MODULE_GLOBALS(mcrypt) + +zend_module_entry mcrypt_module_entry = { + STANDARD_MODULE_HEADER, + "mcrypt", + mcrypt_functions, + PHP_MINIT(mcrypt), PHP_MSHUTDOWN(mcrypt), + NULL, NULL, + PHP_MINFO(mcrypt), + NO_VERSION_YET, + PHP_MODULE_GLOBALS(mcrypt), + NULL, + NULL, + NULL, + STANDARD_MODULE_PROPERTIES_EX +}; + +#ifdef COMPILE_DL_MCRYPT +ZEND_GET_MODULE(mcrypt) +#endif + +#define MCRYPT_ARGS2 \ + zval **cipher, **data, **key, **mode; \ + int td; \ + char *ndata; \ + size_t bsize; \ + size_t nr; \ + size_t nsize + +#define MCRYPT_ARGS \ + MCRYPT_ARGS2; \ + zval **iv + +#define MCRYPT_SIZE \ + bsize = mcrypt_get_block_size(Z_LVAL_PP(cipher)); \ + nr = (Z_STRLEN_PP(data) + bsize - 1) / bsize; \ + nsize = nr * bsize + +#define MCRYPT_CHECK_TD_CPY \ + if (td < 0) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_FAILED); \ + RETURN_FALSE; \ + } \ + ndata = ecalloc(nr, bsize); \ + memcpy(ndata, Z_STRVAL_PP(data), Z_STRLEN_PP(data)) + +#define MCRYPT_CHECK_IV \ + convert_to_string_ex(iv); \ + if (Z_STRLEN_PP(iv) != bsize) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_IV_WRONG_SIZE); \ + RETURN_FALSE; \ + } + +#define MCRYPT_ACTION(x) \ + if (Z_LVAL_PP(mode) == 0) { \ + mcrypt_##x(td, ndata, nsize); \ + } else { \ + mdecrypt_##x(td, ndata, nsize); \ + } \ + end_mcrypt_##x(td) + +#define MCRYPT_IV_WRONG_SIZE "The IV parameter must be as long as the blocksize" + +#define MCRYPT_ENCRYPT 0 +#define MCRYPT_DECRYPT 1 + +#define MCRYPT_GET_INI \ + cipher_dir_string = MCG(algorithms_dir); \ + module_dir_string = MCG(modes_dir); + +/* + * #warning is not ANSI C + * #warning Invalidate resource if the param count is wrong, or other problems + * #warning occurred during functions. + */ + +#define MCRYPT_GET_CRYPT_ARGS \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sssZ|s", \ + &cipher, &cipher_len, &key, &key_len, &data, &data_len, &mode, &iv, &iv_len) == FAILURE) { \ + return; \ + } + +#define MCRYPT_GET_TD_ARG \ + zval *mcryptind; \ + php_mcrypt *pm; \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &mcryptind) == FAILURE) { \ + return; \ + } \ + ZEND_FETCH_RESOURCE (pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt); + +#define MCRYPT_GET_MODE_DIR_ARGS(DIRECTORY) \ + char *dir = NULL; \ + int dir_len; \ + char *module; \ + int module_len; \ + if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, \ + "s|s", &module, &module_len, &dir, &dir_len) == FAILURE) { \ + return; \ + } + +#define MCRYPT_OPEN_MODULE_FAILED "Module initialization failed" + +#define MCRYPT_ENTRY2_2_4(a,b) REGISTER_STRING_CONSTANT("MCRYPT_" #a, b, CONST_PERSISTENT) +#define MCRYPT_ENTRY2_4(a) MCRYPT_ENTRY_NAMED(a, a) + +#define PHP_MCRYPT_INIT_CHECK \ + if (!pm->init) { \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Operation disallowed prior to mcrypt_generic_init()."); \ + RETURN_FALSE; \ + } \ + +PHP_INI_BEGIN() + STD_PHP_INI_ENTRY("mcrypt.algorithms_dir", NULL, PHP_INI_ALL, OnUpdateString, algorithms_dir, zend_mcrypt_globals, mcrypt_globals) + STD_PHP_INI_ENTRY("mcrypt.modes_dir", NULL, PHP_INI_ALL, OnUpdateString, modes_dir, zend_mcrypt_globals, mcrypt_globals) +PHP_INI_END() + +static void php_mcrypt_module_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */ +{ + php_mcrypt *pm = (php_mcrypt *) rsrc->ptr; + if (pm) { + mcrypt_generic_deinit(pm->td); + mcrypt_module_close(pm->td); + efree(pm); + pm = NULL; + } +} +/* }}} */ + +static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */ +{ + le_mcrypt = zend_register_list_destructors_ex(php_mcrypt_module_dtor, NULL, "mcrypt", module_number); + + /* modes for mcrypt_??? routines */ + REGISTER_LONG_CONSTANT("MCRYPT_ENCRYPT", 0, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("MCRYPT_DECRYPT", 1, CONST_PERSISTENT); + + /* sources for mcrypt_create_iv */ + REGISTER_LONG_CONSTANT("MCRYPT_DEV_RANDOM", 0, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("MCRYPT_DEV_URANDOM", 1, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("MCRYPT_RAND", 2, CONST_PERSISTENT); + + /* ciphers */ + MCRYPT_ENTRY2_2_4(3DES, "tripledes"); + MCRYPT_ENTRY2_2_4(ARCFOUR_IV, "arcfour-iv"); + MCRYPT_ENTRY2_2_4(ARCFOUR, "arcfour"); + MCRYPT_ENTRY2_2_4(BLOWFISH, "blowfish"); + MCRYPT_ENTRY2_2_4(BLOWFISH_COMPAT, "blowfish-compat"); + MCRYPT_ENTRY2_2_4(CAST_128, "cast-128"); + MCRYPT_ENTRY2_2_4(CAST_256, "cast-256"); + MCRYPT_ENTRY2_2_4(CRYPT, "crypt"); + MCRYPT_ENTRY2_2_4(DES, "des"); + MCRYPT_ENTRY2_2_4(ENIGNA, "crypt"); + MCRYPT_ENTRY2_2_4(GOST, "gost"); + MCRYPT_ENTRY2_2_4(LOKI97, "loki97"); + MCRYPT_ENTRY2_2_4(PANAMA, "panama"); + MCRYPT_ENTRY2_2_4(RC2, "rc2"); + MCRYPT_ENTRY2_2_4(RIJNDAEL_128, "rijndael-128"); + MCRYPT_ENTRY2_2_4(RIJNDAEL_192, "rijndael-192"); + MCRYPT_ENTRY2_2_4(RIJNDAEL_256, "rijndael-256"); + MCRYPT_ENTRY2_2_4(SAFER64, "safer-sk64"); + MCRYPT_ENTRY2_2_4(SAFER128, "safer-sk128"); + MCRYPT_ENTRY2_2_4(SAFERPLUS, "saferplus"); + MCRYPT_ENTRY2_2_4(SERPENT, "serpent"); + MCRYPT_ENTRY2_2_4(THREEWAY, "threeway"); + MCRYPT_ENTRY2_2_4(TRIPLEDES, "tripledes"); + MCRYPT_ENTRY2_2_4(TWOFISH, "twofish"); + MCRYPT_ENTRY2_2_4(WAKE, "wake"); + MCRYPT_ENTRY2_2_4(XTEA, "xtea"); + + MCRYPT_ENTRY2_2_4(IDEA, "idea"); + MCRYPT_ENTRY2_2_4(MARS, "mars"); + MCRYPT_ENTRY2_2_4(RC6, "rc6"); + MCRYPT_ENTRY2_2_4(SKIPJACK, "skipjack"); +/* modes */ + MCRYPT_ENTRY2_2_4(MODE_CBC, "cbc"); + MCRYPT_ENTRY2_2_4(MODE_CFB, "cfb"); + MCRYPT_ENTRY2_2_4(MODE_ECB, "ecb"); + MCRYPT_ENTRY2_2_4(MODE_NOFB, "nofb"); + MCRYPT_ENTRY2_2_4(MODE_OFB, "ofb"); + MCRYPT_ENTRY2_2_4(MODE_STREAM, "stream"); + REGISTER_INI_ENTRIES(); + + php_stream_filter_register_factory("mcrypt.*", &php_mcrypt_filter_factory TSRMLS_CC); + php_stream_filter_register_factory("mdecrypt.*", &php_mcrypt_filter_factory TSRMLS_CC); + + return SUCCESS; +} +/* }}} */ + +static PHP_MSHUTDOWN_FUNCTION(mcrypt) /* {{{ */ +{ + php_stream_filter_unregister_factory("mcrypt.*" TSRMLS_CC); + php_stream_filter_unregister_factory("mdecrypt.*" TSRMLS_CC); + + UNREGISTER_INI_ENTRIES(); + return SUCCESS; +} +/* }}} */ + +#include "ext/standard/php_smart_str.h" + +PHP_MINFO_FUNCTION(mcrypt) /* {{{ */ +{ + char **modules; + char mcrypt_api_no[16]; + int i, count; + smart_str tmp1 = {0}; + smart_str tmp2 = {0}; + + modules = mcrypt_list_algorithms(MCG(algorithms_dir), &count); + if (count == 0) { + smart_str_appends(&tmp1, "none"); + } + for (i = 0; i < count; i++) { + smart_str_appends(&tmp1, modules[i]); + smart_str_appendc(&tmp1, ' '); + } + smart_str_0(&tmp1); + mcrypt_free_p(modules, count); + + modules = mcrypt_list_modes(MCG(modes_dir), &count); + if (count == 0) { + smart_str_appends(&tmp2, "none"); + } + for (i = 0; i < count; i++) { + smart_str_appends(&tmp2, modules[i]); + smart_str_appendc(&tmp2, ' '); + } + smart_str_0 (&tmp2); + mcrypt_free_p (modules, count); + + snprintf (mcrypt_api_no, 16, "%d", MCRYPT_API_VERSION); + + php_info_print_table_start(); + php_info_print_table_header(2, "mcrypt support", "enabled"); + php_info_print_table_header(2, "mcrypt_filter support", "enabled"); + php_info_print_table_row(2, "Version", LIBMCRYPT_VERSION); + php_info_print_table_row(2, "Api No", mcrypt_api_no); + php_info_print_table_row(2, "Supported ciphers", tmp1.c); + php_info_print_table_row(2, "Supported modes", tmp2.c); + smart_str_free(&tmp1); + smart_str_free(&tmp2); + php_info_print_table_end(); + + DISPLAY_INI_ENTRIES(); +} +/* }}} */ + +typedef enum { + RANDOM = 0, + URANDOM, + RAND +} iv_source; + +/* {{{ proto resource mcrypt_module_open(string cipher, string cipher_directory, string mode, string mode_directory) + Opens the module of the algorithm and the mode to be used */ +PHP_FUNCTION(mcrypt_module_open) +{ + char *cipher, *cipher_dir; + char *mode, *mode_dir; + int cipher_len, cipher_dir_len; + int mode_len, mode_dir_len; + MCRYPT td; + php_mcrypt *pm; + + if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, "ssss", + &cipher, &cipher_len, &cipher_dir, &cipher_dir_len, + &mode, &mode_len, &mode_dir, &mode_dir_len)) { + return; + } + + td = mcrypt_module_open ( + cipher, + cipher_dir_len > 0 ? cipher_dir : MCG(algorithms_dir), + mode, + mode_dir_len > 0 ? mode_dir : MCG(modes_dir) + ); + + if (td == MCRYPT_FAILED) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not open encryption module"); + RETURN_FALSE; + } else { + pm = emalloc(sizeof(php_mcrypt)); + pm->td = td; + pm->init = 0; + ZEND_REGISTER_RESOURCE(return_value, pm, le_mcrypt); + } +} +/* }}} */ + +/* {{{ proto int mcrypt_generic_init(resource td, string key, string iv) + This function initializes all buffers for the specific module */ +PHP_FUNCTION(mcrypt_generic_init) +{ + char *key, *iv; + int key_len, iv_len; + zval *mcryptind; + unsigned char *key_s, *iv_s; + int max_key_size, key_size, iv_size; + php_mcrypt *pm; + int result = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &mcryptind, &key, &key_len, &iv, &iv_len) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt); + + max_key_size = mcrypt_enc_get_key_size(pm->td); + iv_size = mcrypt_enc_get_iv_size(pm->td); + + if (key_len == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key size is 0"); + } + + key_s = emalloc(key_len); + memset(key_s, 0, key_len); + + iv_s = emalloc(iv_size + 1); + memset(iv_s, 0, iv_size + 1); + + if (key_len > max_key_size) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key size too large; supplied length: %d, max: %d", key_len, max_key_size); + key_size = max_key_size; + } else { + key_size = key_len; + } + memcpy(key_s, key, key_len); + + if (iv_len != iv_size) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Iv size incorrect; supplied length: %d, needed: %d", iv_len, iv_size); + } + memcpy(iv_s, iv, iv_size); + + mcrypt_generic_deinit(pm->td); + result = mcrypt_generic_init(pm->td, key_s, key_size, iv_s); + + /* If this function fails, close the mcrypt module to prevent crashes + * when further functions want to access this resource */ + if (result < 0) { + zend_list_delete(Z_LVAL_P(mcryptind)); + switch (result) { + case -3: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key length incorrect"); + break; + case -4: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Memory allocation error"); + break; + case -1: + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error"); + break; + } + } + pm->init = 1; + RETVAL_LONG(result); + + efree(iv_s); + efree(key_s); +} +/* }}} */ + +/* {{{ proto string mcrypt_generic(resource td, string data) + This function encrypts the plaintext */ +PHP_FUNCTION(mcrypt_generic) +{ + zval *mcryptind; + char *data; + int data_len; + php_mcrypt *pm; + unsigned char* data_s; + int block_size, data_size; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &mcryptind, &data, &data_len) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt); + PHP_MCRYPT_INIT_CHECK + + if (data_len == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "An empty string was passed"); + RETURN_FALSE + } + + /* Check blocksize */ + if (mcrypt_enc_is_block_mode(pm->td) == 1) { /* It's a block algorithm */ + block_size = mcrypt_enc_get_block_size(pm->td); + data_size = (((data_len - 1) / block_size) + 1) * block_size; + data_s = emalloc(data_size + 1); + memset(data_s, 0, data_size); + memcpy(data_s, data, data_len); + } else { /* It's not a block algorithm */ + data_size = data_len; + data_s = emalloc(data_size + 1); + memset(data_s, 0, data_size); + memcpy(data_s, data, data_len); + } + + mcrypt_generic(pm->td, data_s, data_size); + data_s[data_size] = '\0'; + + RETVAL_STRINGL(data_s, data_size, 1); + efree(data_s); +} +/* }}} */ + +/* {{{ proto string mdecrypt_generic(resource td, string data) + This function decrypts the plaintext */ +PHP_FUNCTION(mdecrypt_generic) +{ + zval *mcryptind; + char *data; + int data_len; + php_mcrypt *pm; + char* data_s; + int block_size, data_size; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &mcryptind, &data, &data_len) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(pm, php_mcrypt * , &mcryptind, -1, "MCrypt", le_mcrypt); + PHP_MCRYPT_INIT_CHECK + + if (data_len == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "An empty string was passed"); + RETURN_FALSE + } + + /* Check blocksize */ + if (mcrypt_enc_is_block_mode(pm->td) == 1) { /* It's a block algorithm */ + block_size = mcrypt_enc_get_block_size(pm->td); + data_size = (((data_len - 1) / block_size) + 1) * block_size; + data_s = emalloc(data_size + 1); + memset(data_s, 0, data_size); + memcpy(data_s, data, data_len); + } else { /* It's not a block algorithm */ + data_size = data_len; + data_s = emalloc(data_size + 1); + memset(data_s, 0, data_size); + memcpy(data_s, data, data_len); + } + + mdecrypt_generic(pm->td, data_s, data_size); + + RETVAL_STRINGL(data_s, data_size, 1); + efree(data_s); +} +/* }}} */ + +/* {{{ proto array mcrypt_enc_get_supported_key_sizes(resource td) + This function decrypts the crypttext */ +PHP_FUNCTION(mcrypt_enc_get_supported_key_sizes) +{ + int i, count = 0; + int *key_sizes; + + MCRYPT_GET_TD_ARG + array_init(return_value); + + key_sizes = mcrypt_enc_get_supported_key_sizes(pm->td, &count); + + for (i = 0; i < count; i++) { + add_index_long(return_value, i, key_sizes[i]); + } + + mcrypt_free(key_sizes); +} +/* }}} */ + +/* {{{ proto int mcrypt_enc_self_test(resource td) + This function runs the self test on the algorithm specified by the descriptor td */ +PHP_FUNCTION(mcrypt_enc_self_test) +{ + MCRYPT_GET_TD_ARG + RETURN_LONG(mcrypt_enc_self_test(pm->td)); +} +/* }}} */ + +/* {{{ proto bool mcrypt_module_close(resource td) + Free the descriptor td */ +PHP_FUNCTION(mcrypt_module_close) +{ + MCRYPT_GET_TD_ARG + zend_list_delete(Z_LVAL_P(mcryptind)); + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool mcrypt_generic_deinit(resource td) + This function terminates encrypt specified by the descriptor td */ +PHP_FUNCTION(mcrypt_generic_deinit) +{ + MCRYPT_GET_TD_ARG + + if (mcrypt_generic_deinit(pm->td) < 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not terminate encryption specifier"); + RETURN_FALSE + } + pm->init = 0; + RETURN_TRUE +} +/* }}} */ + +/* {{{ proto bool mcrypt_enc_is_block_algorithm_mode(resource td) + Returns TRUE if the mode is for use with block algorithms */ +PHP_FUNCTION(mcrypt_enc_is_block_algorithm_mode) +{ + MCRYPT_GET_TD_ARG + + if (mcrypt_enc_is_block_algorithm_mode(pm->td) == 1) { + RETURN_TRUE + } else { + RETURN_FALSE + } +} +/* }}} */ + +/* {{{ proto bool mcrypt_enc_is_block_algorithm(resource td) + Returns TRUE if the alrogithm is a block algorithms */ +PHP_FUNCTION(mcrypt_enc_is_block_algorithm) +{ + MCRYPT_GET_TD_ARG + + if (mcrypt_enc_is_block_algorithm(pm->td) == 1) { + RETURN_TRUE + } else { + RETURN_FALSE + } +} +/* }}} */ + +/* {{{ proto bool mcrypt_enc_is_block_mode(resource td) + Returns TRUE if the mode outputs blocks */ +PHP_FUNCTION(mcrypt_enc_is_block_mode) +{ + MCRYPT_GET_TD_ARG + + if (mcrypt_enc_is_block_mode(pm->td) == 1) { + RETURN_TRUE + } else { + RETURN_FALSE + } +} +/* }}} */ + +/* {{{ proto int mcrypt_enc_get_block_size(resource td) + Returns the block size of the cipher specified by the descriptor td */ +PHP_FUNCTION(mcrypt_enc_get_block_size) +{ + MCRYPT_GET_TD_ARG + RETURN_LONG(mcrypt_enc_get_block_size(pm->td)); +} +/* }}} */ + +/* {{{ proto int mcrypt_enc_get_key_size(resource td) + Returns the maximum supported key size in bytes of the algorithm specified by the descriptor td */ +PHP_FUNCTION(mcrypt_enc_get_key_size) +{ + MCRYPT_GET_TD_ARG + RETURN_LONG(mcrypt_enc_get_key_size(pm->td)); +} +/* }}} */ + +/* {{{ proto int mcrypt_enc_get_iv_size(resource td) + Returns the size of the IV in bytes of the algorithm specified by the descriptor td */ +PHP_FUNCTION(mcrypt_enc_get_iv_size) +{ + MCRYPT_GET_TD_ARG + RETURN_LONG(mcrypt_enc_get_iv_size(pm->td)); +} +/* }}} */ + +/* {{{ proto string mcrypt_enc_get_algorithms_name(resource td) + Returns the name of the algorithm specified by the descriptor td */ +PHP_FUNCTION(mcrypt_enc_get_algorithms_name) +{ + char *name; + MCRYPT_GET_TD_ARG + + name = mcrypt_enc_get_algorithms_name(pm->td); + RETVAL_STRING(name, 1); + mcrypt_free(name); +} +/* }}} */ + +/* {{{ proto string mcrypt_enc_get_modes_name(resource td) + Returns the name of the mode specified by the descriptor td */ +PHP_FUNCTION(mcrypt_enc_get_modes_name) +{ + char *name; + MCRYPT_GET_TD_ARG + + name = mcrypt_enc_get_modes_name(pm->td); + RETVAL_STRING(name, 1); + mcrypt_free(name); +} +/* }}} */ + +/* {{{ proto bool mcrypt_module_self_test(string algorithm [, string lib_dir]) + Does a self test of the module "module" */ +PHP_FUNCTION(mcrypt_module_self_test) +{ + MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir); + + if (mcrypt_module_self_test(module, dir) == 0) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto bool mcrypt_module_is_block_algorithm_mode(string mode [, string lib_dir]) + Returns TRUE if the mode is for use with block algorithms */ +PHP_FUNCTION(mcrypt_module_is_block_algorithm_mode) +{ + MCRYPT_GET_MODE_DIR_ARGS(modes_dir) + + if (mcrypt_module_is_block_algorithm_mode(module, dir) == 1) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto bool mcrypt_module_is_block_algorithm(string algorithm [, string lib_dir]) + Returns TRUE if the algorithm is a block algorithm */ +PHP_FUNCTION(mcrypt_module_is_block_algorithm) +{ + MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir) + + if (mcrypt_module_is_block_algorithm(module, dir) == 1) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto bool mcrypt_module_is_block_mode(string mode [, string lib_dir]) + Returns TRUE if the mode outputs blocks of bytes */ +PHP_FUNCTION(mcrypt_module_is_block_mode) +{ + MCRYPT_GET_MODE_DIR_ARGS(modes_dir) + + if (mcrypt_module_is_block_mode(module, dir) == 1) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto int mcrypt_module_get_algo_block_size(string algorithm [, string lib_dir]) + Returns the block size of the algorithm */ +PHP_FUNCTION(mcrypt_module_get_algo_block_size) +{ + MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir) + + RETURN_LONG(mcrypt_module_get_algo_block_size(module, dir)); +} +/* }}} */ + +/* {{{ proto int mcrypt_module_get_algo_key_size(string algorithm [, string lib_dir]) + Returns the maximum supported key size of the algorithm */ +PHP_FUNCTION(mcrypt_module_get_algo_key_size) +{ + MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir); + + RETURN_LONG(mcrypt_module_get_algo_key_size(module, dir)); +} +/* }}} */ + +/* {{{ proto array mcrypt_module_get_supported_key_sizes(string algorithm [, string lib_dir]) + This function decrypts the crypttext */ +PHP_FUNCTION(mcrypt_module_get_supported_key_sizes) +{ + int i, count = 0; + int *key_sizes; + + MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir) + array_init(return_value); + + key_sizes = mcrypt_module_get_algo_supported_key_sizes(module, dir, &count); + + for (i = 0; i < count; i++) { + add_index_long(return_value, i, key_sizes[i]); + } + mcrypt_free(key_sizes); +} +/* }}} */ + +/* {{{ proto array mcrypt_list_algorithms([string lib_dir]) + List all algorithms in "module_dir" */ +PHP_FUNCTION(mcrypt_list_algorithms) +{ + char **modules; + char *lib_dir = MCG(algorithms_dir); + int lib_dir_len; + int i, count; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", + &lib_dir, &lib_dir_len) == FAILURE) { + return; + } + + array_init(return_value); + modules = mcrypt_list_algorithms(lib_dir, &count); + + if (count == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "No algorithms found in module dir"); + } + for (i = 0; i < count; i++) { + add_index_string(return_value, i, modules[i], 1); + } + mcrypt_free_p(modules, count); +} +/* }}} */ + +/* {{{ proto array mcrypt_list_modes([string lib_dir]) + List all modes "module_dir" */ +PHP_FUNCTION(mcrypt_list_modes) +{ + char **modules; + char *lib_dir = MCG(modes_dir); + int lib_dir_len; + int i, count; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", + &lib_dir, &lib_dir_len) == FAILURE) { + return; + } + + array_init(return_value); + modules = mcrypt_list_modes(lib_dir, &count); + + if (count == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "No modes found in module dir"); + } + for (i = 0; i < count; i++) { + add_index_string(return_value, i, modules[i], 1); + } + mcrypt_free_p(modules, count); +} +/* }}} */ + +/* {{{ proto int mcrypt_get_key_size(string cipher, string module) + Get the key size of cipher */ +PHP_FUNCTION(mcrypt_get_key_size) +{ + char *cipher; + char *module; + int cipher_len, module_len; + char *cipher_dir_string; + char *module_dir_string; + MCRYPT td; + + MCRYPT_GET_INI + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", + &cipher, &cipher_len, &module, &module_len) == FAILURE) { + return; + } + + td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string); + if (td != MCRYPT_FAILED) { + RETVAL_LONG(mcrypt_enc_get_key_size(td)); + mcrypt_module_close(td); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto int mcrypt_get_block_size(string cipher, string module) + Get the key size of cipher */ +PHP_FUNCTION(mcrypt_get_block_size) +{ + char *cipher; + char *module; + int cipher_len, module_len; + char *cipher_dir_string; + char *module_dir_string; + MCRYPT td; + + MCRYPT_GET_INI + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", + &cipher, &cipher_len, &module, &module_len) == FAILURE) { + return; + } + + td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string); + if (td != MCRYPT_FAILED) { + RETVAL_LONG(mcrypt_enc_get_block_size(td)); + mcrypt_module_close(td); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto int mcrypt_get_iv_size(string cipher, string module) + Get the IV size of cipher (Usually the same as the blocksize) */ +PHP_FUNCTION(mcrypt_get_iv_size) +{ + char *cipher; + char *module; + int cipher_len, module_len; + char *cipher_dir_string; + char *module_dir_string; + MCRYPT td; + + MCRYPT_GET_INI + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", + &cipher, &cipher_len, &module, &module_len) == FAILURE) { + return; + } + + td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string); + if (td != MCRYPT_FAILED) { + RETVAL_LONG(mcrypt_enc_get_iv_size(td)); + mcrypt_module_close(td); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto string mcrypt_get_cipher_name(string cipher) + Get the key size of cipher */ +PHP_FUNCTION(mcrypt_get_cipher_name) +{ + char *cipher_dir_string; + char *module_dir_string; + char *cipher_name; + char *cipher; + int cipher_len; + MCRYPT td; + + MCRYPT_GET_INI + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", + &cipher, &cipher_len) == FAILURE) { + return; + } + + /* The code below is actually not very nice, but I didn't see a better + * method */ + td = mcrypt_module_open(cipher, cipher_dir_string, "ecb", module_dir_string); + if (td != MCRYPT_FAILED) { + cipher_name = mcrypt_enc_get_algorithms_name(td); + mcrypt_module_close(td); + RETVAL_STRING(cipher_name,1); + mcrypt_free(cipher_name); + } else { + td = mcrypt_module_open(cipher, cipher_dir_string, "stream", module_dir_string); + if (td != MCRYPT_FAILED) { + cipher_name = mcrypt_enc_get_algorithms_name(td); + mcrypt_module_close(td); + RETVAL_STRING(cipher_name,1); + mcrypt_free(cipher_name); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); + RETURN_FALSE; + } + } +} +/* }}} */ + +static void php_mcrypt_do_crypt(char* cipher, const char *key, int key_len, const char *data, int data_len, char *mode, const char *iv, int iv_len, int argc, int dencrypt, zval* return_value TSRMLS_DC) /* {{{ */ +{ + char *cipher_dir_string; + char *module_dir_string; + int block_size, max_key_length, use_key_length, i, count, iv_size; + unsigned long int data_size; + int *key_length_sizes; + char *key_s = NULL, *iv_s; + char *data_s; + MCRYPT td; + + MCRYPT_GET_INI + + td = mcrypt_module_open(cipher, cipher_dir_string, mode, module_dir_string); + if (td == MCRYPT_FAILED) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); + RETURN_FALSE; + } + /* Checking for key-length */ + max_key_length = mcrypt_enc_get_key_size(td); + if (key_len > max_key_length) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Size of key is too large for this algorithm"); + } + key_length_sizes = mcrypt_enc_get_supported_key_sizes(td, &count); + if (count == 0 && key_length_sizes == NULL) { /* all lengths 1 - k_l_s = OK */ + use_key_length = key_len; + key_s = emalloc(use_key_length); + memset(key_s, 0, use_key_length); + memcpy(key_s, key, use_key_length); + } else if (count == 1) { /* only m_k_l = OK */ + key_s = emalloc(key_length_sizes[0]); + memset(key_s, 0, key_length_sizes[0]); + memcpy(key_s, key, MIN(key_len, key_length_sizes[0])); + use_key_length = key_length_sizes[0]; + } else { /* dertermine smallest supported key > length of requested key */ + use_key_length = max_key_length; /* start with max key length */ + for (i = 0; i < count; i++) { + if (key_length_sizes[i] >= key_len && + key_length_sizes[i] < use_key_length) + { + use_key_length = key_length_sizes[i]; + } + } + key_s = emalloc(use_key_length); + memset(key_s, 0, use_key_length); + memcpy(key_s, key, MIN(key_len, use_key_length)); + } + mcrypt_free (key_length_sizes); + + /* Check IV */ + iv_s = NULL; + iv_size = mcrypt_enc_get_iv_size (td); + + /* IV is required */ + if (mcrypt_enc_mode_has_iv(td) == 1) { + if (argc == 5) { + if (iv_size != iv_len) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_IV_WRONG_SIZE); + } else { + iv_s = emalloc(iv_size + 1); + memcpy(iv_s, iv, iv_size); + } + } else if (argc == 4) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to use an empty IV, which is NOT recommend"); + iv_s = emalloc(iv_size + 1); + memset(iv_s, 0, iv_size + 1); + } + } + + /* Check blocksize */ + if (mcrypt_enc_is_block_mode(td) == 1) { /* It's a block algorithm */ + block_size = mcrypt_enc_get_block_size(td); + data_size = (((data_len - 1) / block_size) + 1) * block_size; + data_s = emalloc(data_size); + memset(data_s, 0, data_size); + memcpy(data_s, data, data_len); + } else { /* It's not a block algorithm */ + data_size = data_len; + data_s = emalloc(data_size); + memset(data_s, 0, data_size); + memcpy(data_s, data, data_len); + } + + if (mcrypt_generic_init(td, key_s, use_key_length, iv_s) < 0) { + php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Mcrypt initialisation failed"); + RETURN_FALSE; + } + if (dencrypt == MCRYPT_ENCRYPT) { + mcrypt_generic(td, data_s, data_size); + } else { + mdecrypt_generic(td, data_s, data_size); + } + + RETVAL_STRINGL(data_s, data_size, 1); + + /* freeing vars */ + mcrypt_generic_end(td); + if (key_s != NULL) { + efree (key_s); + } + if (iv_s != NULL) { + efree (iv_s); + } + efree (data_s); +} +/* }}} */ + +/* {{{ proto string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + OFB crypt/decrypt data using key key with cipher cipher starting with iv */ +PHP_FUNCTION(mcrypt_encrypt) +{ + zval **mode; + char *cipher, *key, *data, *iv = NULL; + int cipher_len, key_len, data_len, iv_len = 0; + + MCRYPT_GET_CRYPT_ARGS + + convert_to_string_ex(mode); + + php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, Z_STRVAL_PP(mode), iv, iv_len, ZEND_NUM_ARGS(), MCRYPT_ENCRYPT, return_value TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + OFB crypt/decrypt data using key key with cipher cipher starting with iv */ +PHP_FUNCTION(mcrypt_decrypt) +{ + zval **mode; + char *cipher, *key, *data, *iv = NULL; + int cipher_len, key_len, data_len, iv_len = 0; + + MCRYPT_GET_CRYPT_ARGS + + convert_to_string_ex(mode); + + php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, Z_STRVAL_PP(mode), iv, iv_len, ZEND_NUM_ARGS(), MCRYPT_DECRYPT, return_value TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto string mcrypt_ecb(int cipher, string key, string data, int mode, string iv) + ECB crypt/decrypt data using key key with cipher cipher starting with iv */ +PHP_FUNCTION(mcrypt_ecb) +{ + zval **mode; + char *cipher, *key, *data, *iv = NULL; + int cipher_len, key_len, data_len, iv_len = 0; + + MCRYPT_GET_CRYPT_ARGS + + convert_to_long_ex(mode); + + php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, "ecb", iv, iv_len, ZEND_NUM_ARGS(), Z_LVAL_PP(mode), return_value TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto string mcrypt_cbc(int cipher, string key, string data, int mode, string iv) + CBC crypt/decrypt data using key key with cipher cipher starting with iv */ +PHP_FUNCTION(mcrypt_cbc) +{ + zval **mode; + char *cipher, *key, *data, *iv = NULL; + int cipher_len, key_len, data_len, iv_len = 0; + + MCRYPT_GET_CRYPT_ARGS + + convert_to_long_ex(mode); + + php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, "cbc", iv, iv_len, ZEND_NUM_ARGS(), Z_LVAL_PP(mode), return_value TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto string mcrypt_cfb(int cipher, string key, string data, int mode, string iv) + CFB crypt/decrypt data using key key with cipher cipher starting with iv */ +PHP_FUNCTION(mcrypt_cfb) +{ + zval **mode; + char *cipher, *key, *data, *iv = NULL; + int cipher_len, key_len, data_len, iv_len = 0; + + MCRYPT_GET_CRYPT_ARGS + + convert_to_long_ex(mode); + + php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, "cfb", iv, iv_len, ZEND_NUM_ARGS(), Z_LVAL_PP(mode), return_value TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto string mcrypt_ofb(int cipher, string key, string data, int mode, string iv) + OFB crypt/decrypt data using key key with cipher cipher starting with iv */ +PHP_FUNCTION(mcrypt_ofb) +{ + zval **mode; + char *cipher, *key, *data, *iv = NULL; + int cipher_len, key_len, data_len, iv_len = 0; + + MCRYPT_GET_CRYPT_ARGS + + convert_to_long_ex(mode); + + php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, "ofb", iv, iv_len, ZEND_NUM_ARGS(), Z_LVAL_PP(mode), return_value TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto string mcrypt_create_iv(int size, int source) + Create an initialization vector (IV) */ +PHP_FUNCTION(mcrypt_create_iv) +{ + char *iv; + long source = RANDOM; + long size; + int n = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &size, &source) == FAILURE) { + return; + } + + if (size <= 0 || size >= INT_MAX) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create an IV with a size of less than 1 or greater than %d", INT_MAX); + RETURN_FALSE; + } + + iv = ecalloc(size + 1, 1); + + if (source == RANDOM || source == URANDOM) { +#if PHP_WIN32 + /* random/urandom equivalent on Windows */ + BYTE *iv_b = (BYTE *) iv; + if (php_win32_get_random_bytes(iv_b, (size_t) size) == FAILURE){ + efree(iv); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random data"); + RETURN_FALSE; + } + n = size; +#else + int fd; + size_t read_bytes = 0; + + fd = open(source == RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY); + if (fd < 0) { + efree(iv); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot open source device"); + RETURN_FALSE; + } + while (read_bytes < size) { + n = read(fd, iv + read_bytes, size - read_bytes); + if (n < 0) { + break; + } + read_bytes += n; + } + n = read_bytes; + close(fd); + if (n < size) { + efree(iv); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random data"); + RETURN_FALSE; + } +#endif + } else { + n = size; + while (size) { + iv[--size] = (char) (255.0 * php_rand(TSRMLS_C) / RAND_MAX); + } + } + RETURN_STRINGL(iv, n, 0); +} +/* }}} */ + +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/ext/mcrypt/mcrypt.dsp b/ext/mcrypt/mcrypt.dsp new file mode 100644 index 0000000..a0a1f00 --- /dev/null +++ b/ext/mcrypt/mcrypt.dsp @@ -0,0 +1,117 @@ +# Microsoft Developer Studio Project File - Name="mcrypt" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
+
+CFG=mcrypt - Win32 Release_TS
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "mcrypt.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "mcrypt.mak" CFG="mcrypt - Win32 Release_TS"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "mcrypt - Win32 Release_TS" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE "mcrypt - Win32 Debug_TS" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+MTL=midl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "mcrypt - Win32 Release_TS"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release_TS"
+# PROP BASE Intermediate_Dir "Release_TS"
+# PROP BASE Ignore_Export_Lib 0
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Release_TS"
+# PROP Intermediate_Dir "Release_TS"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MT /W3 /GX /O2 /I "..\.." /I "..\..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\..\TSRM" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "MSSQL_EXPORTS" /D "COMPILE_DL_MCRYPT" /D "DBNTWIN32" /D ZTS=1 /YX /FD /c
+# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\TSRM" /I "..\..\..\php_build\mcrypt\include" /D ZEND_DEBUG=0 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "MCRYPT_EXPORTS" /D "COMPILE_DL_MCRYPT" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_LIBMCRYPT=1 /D HAVE_LIBMCRYPT24=1 /D HAVE_MCRYPT_GENERIC_DEINIT /YX /FD /c
+# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x406 /d "NDEBUG"
+# ADD RSC /l 0x406 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php5ts.lib ntwdblib.lib /nologo /dll /machine:I386 /out:"MSSQL_65_Release/php_mssql.dll" /libpath:"..\..\Release_TS"
+# ADD LINK32 php5ts.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib libmcrypt.lib /nologo /dll /machine:I386 /out:"..\..\Release_TS/php_mcrypt.dll" /libpath:"..\..\Release_TS" /libpath:"..\..\..\php_build\mcrypt" /libpath:"..\..\Release_TS_Inline" /libpath:"..\..\TSRM\Release_TS" /libpath:"..\..\TSRM\Release_TS_Inline"
+
+!ELSEIF "$(CFG)" == "mcrypt - Win32 Debug_TS"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Debug_TS"
+# PROP BASE Intermediate_Dir "Debug_TS"
+# PROP BASE Ignore_Export_Lib 0
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Debug_TS"
+# PROP Intermediate_Dir "Debug_TS"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MT /W3 /GX /O2 /I "..\.." /I "..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\TSRM" /I "mssql-70" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "MSSQL_EXPORTS" /D "COMPILE_DL_MCRYPT" /D "DBNTWIN32" /D ZTS=1 /D MSSQL70=1 /YX /FD /c
+# ADD CPP /nologo /MDd /W3 /GX /O2 /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\TSRM" /I "..\..\..\php_build\mcrypt\include" /D ZEND_DEBUG=1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "MCRYPT_EXPORTS" /D "COMPILE_DL_MCRYPT" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_LIBMCRYPT=1 /D HAVE_LIBMCRYPT24=1 /D HAVE_MCRYPT_GENERIC_DEINIT /YX /FD /c
+# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x406 /d "NDEBUG"
+# ADD RSC /l 0x406 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php5ts.lib ntwdblib.lib /nologo /dll /machine:I386 /out:"MSSQL_65_Release/php_mssql.dll" /libpath:"..\..\Release_TS" /libpath:"mssql-70"
+# ADD LINK32 php5ts_debug.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib libmcrypt.lib /nologo /dll /machine:I386 /out:"..\..\Debug_TS/php_mcrypt.dll" /libpath:"..\..\Debug_TS" /libpath:"..\..\..\php_build\mcrypt\lib"
+
+!ENDIF
+
+# Begin Target
+
+# Name "mcrypt - Win32 Release_TS"
+# Name "mcrypt - Win32 Debug_TS"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=.\mcrypt.c
+# End Source File
+# Begin Source File
+
+SOURCE=..\..\win32\readdir.c
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=.\php_mcrypt.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\..\win32\readdir.h
+# End Source File
+# End Group
+# End Target
+# End Project
diff --git a/ext/mcrypt/mcrypt_filter.c b/ext/mcrypt/mcrypt_filter.c new file mode 100644 index 0000000..da8215b --- /dev/null +++ b/ext/mcrypt/mcrypt_filter.c @@ -0,0 +1,283 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Sara Golemon <pollita@php.net> | + +----------------------------------------------------------------------+ + + $Id$ +*/ + +#include "php.h" + +#include "php_mcrypt_filter.h" +#include "php_ini.h" +#include <mcrypt.h> + +typedef struct _php_mcrypt_filter_data { + MCRYPT module; + char encrypt; + int blocksize; + char *block_buffer; + int block_used; + char persistent; +} php_mcrypt_filter_data; + +static php_stream_filter_status_t php_mcrypt_filter( + php_stream *stream, + php_stream_filter *thisfilter, + php_stream_bucket_brigade *buckets_in, + php_stream_bucket_brigade *buckets_out, + size_t *bytes_consumed, + int flags TSRMLS_DC) +{ + php_mcrypt_filter_data *data; + php_stream_bucket *bucket; + size_t consumed = 0; + php_stream_filter_status_t exit_status = PSFS_FEED_ME; + + if (!thisfilter || !thisfilter->abstract) { + /* Should never happen */ + return PSFS_ERR_FATAL; + } + + data = (php_mcrypt_filter_data *)(thisfilter->abstract); + while(buckets_in->head) { + bucket = buckets_in->head; + + consumed += bucket->buflen; + + if (data->blocksize) { + /* Blockmode cipher */ + char *outchunk; + int chunklen = bucket->buflen + data->block_used, n; + php_stream_bucket *newbucket; + + outchunk = pemalloc(chunklen, data->persistent); + if (data->block_used) { + memcpy(outchunk, data->block_buffer, data->block_used); + } + memcpy(outchunk + data->block_used, bucket->buf, bucket->buflen); + + for(n=0; (n + data->blocksize) <= chunklen; n += data->blocksize) { + + if (data->encrypt) { + mcrypt_generic(data->module, outchunk + n, data->blocksize); + } else { + mdecrypt_generic(data->module, outchunk + n, data->blocksize); + } + } + data->block_used = chunklen - n; + memcpy(data->block_buffer, outchunk + n, data->block_used); + + newbucket = php_stream_bucket_new(stream, outchunk, n, 1, data->persistent TSRMLS_CC); + php_stream_bucket_append(buckets_out, newbucket TSRMLS_CC); + + exit_status = PSFS_PASS_ON; + + php_stream_bucket_unlink(bucket TSRMLS_CC); + php_stream_bucket_delref(bucket TSRMLS_CC); + } else { + /* Stream cipher */ + php_stream_bucket_make_writeable(bucket TSRMLS_CC); + if (data->encrypt) { + mcrypt_generic(data->module, bucket->buf, bucket->buflen); + } else { + mdecrypt_generic(data->module, bucket->buf, bucket->buflen); + } + php_stream_bucket_append(buckets_out, bucket TSRMLS_CC); + + exit_status = PSFS_PASS_ON; + } + } + + if ((flags & PSFS_FLAG_FLUSH_CLOSE) && data->blocksize && data->block_used) { + php_stream_bucket *newbucket; + + memset(data->block_buffer + data->block_used, 0, data->blocksize - data->block_used); + if (data->encrypt) { + mcrypt_generic(data->module, data->block_buffer, data->blocksize); + } else { + mdecrypt_generic(data->module, data->block_buffer, data->blocksize); + } + + newbucket = php_stream_bucket_new(stream, data->block_buffer, data->blocksize, 0, data->persistent TSRMLS_CC); + php_stream_bucket_append(buckets_out, newbucket TSRMLS_CC); + + exit_status = PSFS_PASS_ON; + } + + if (bytes_consumed) { + *bytes_consumed = consumed; + } + + return exit_status; +} + +static void php_mcrypt_filter_dtor(php_stream_filter *thisfilter TSRMLS_DC) +{ + if (thisfilter && thisfilter->abstract) { + php_mcrypt_filter_data *data = (php_mcrypt_filter_data*)thisfilter->abstract; + + if (data->block_buffer) { + pefree(data->block_buffer, data->persistent); + } + + mcrypt_generic_deinit(data->module); + mcrypt_module_close(data->module); + + pefree(data, data->persistent); + } +} + +static php_stream_filter_ops php_mcrypt_filter_ops = { + php_mcrypt_filter, + php_mcrypt_filter_dtor, + "mcrypt.*" +}; + +/* {{{ php_mcrypt_filter_create + * Instantiate mcrypt filter + */ +static php_stream_filter *php_mcrypt_filter_create(const char *filtername, zval *filterparams, int persistent TSRMLS_DC) +{ + int encrypt = 1, iv_len, key_len, keyl, result; + const char *cipher = filtername + sizeof("mcrypt.") - 1; + zval **tmpzval; + MCRYPT mcrypt_module; + char *iv = NULL, *key = NULL; + char *algo_dir = INI_STR("mcrypt.algorithms_dir"); + char *mode_dir = INI_STR("mcrypt.modes_dir"); + char *mode = "cbc"; + php_mcrypt_filter_data *data; + + if (strncasecmp(filtername, "mdecrypt.", sizeof("mdecrypt.") - 1) == 0) { + encrypt = 0; + cipher += sizeof("de") - 1; + } else if (strncasecmp(filtername, "mcrypt.", sizeof("mcrypt.") - 1) != 0) { + /* Should never happen */ + return NULL; + } + + if (!filterparams || Z_TYPE_P(filterparams) != IS_ARRAY) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filter parameters for %s must be an array", filtername); + return NULL; + } + + if (zend_hash_find(HASH_OF(filterparams), "mode", sizeof("mode"), (void**)&tmpzval) == SUCCESS) { + if (Z_TYPE_PP(tmpzval) == IS_STRING) { + mode = Z_STRVAL_PP(tmpzval); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "mode is not a string, ignoring"); + } + } + + if (zend_hash_find(HASH_OF(filterparams), "algorithms_dir", sizeof("algorithms_dir"), (void**)&tmpzval) == SUCCESS) { + if (Z_TYPE_PP(tmpzval) == IS_STRING) { + algo_dir = Z_STRVAL_PP(tmpzval); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "algorithms_dir is not a string, ignoring"); + } + } + + if (zend_hash_find(HASH_OF(filterparams), "modes_dir", sizeof("modes_dir"), (void**)&tmpzval) == SUCCESS) { + if (Z_TYPE_PP(tmpzval) == IS_STRING) { + mode_dir = Z_STRVAL_PP(tmpzval); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "modes_dir is not a string, ignoring"); + } + } + + if (zend_hash_find(HASH_OF(filterparams), "key", sizeof("key"), (void**)&tmpzval) == SUCCESS && + Z_TYPE_PP(tmpzval) == IS_STRING) { + key = Z_STRVAL_PP(tmpzval); + key_len = Z_STRLEN_PP(tmpzval); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "key not specified or is not a string"); + return NULL; + } + + mcrypt_module = mcrypt_module_open(cipher, algo_dir, mode, mode_dir); + if (mcrypt_module == MCRYPT_FAILED) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not open encryption module"); + return NULL; + } + iv_len = mcrypt_enc_get_iv_size(mcrypt_module); + keyl = mcrypt_enc_get_key_size(mcrypt_module); + if (keyl < key_len) { + key_len = keyl; + } + + if (zend_hash_find(HASH_OF(filterparams), "iv", sizeof("iv"), (void**) &tmpzval) == FAILURE || + Z_TYPE_PP(tmpzval) != IS_STRING) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filter parameter[iv] not provided or not of type: string"); + mcrypt_module_close(mcrypt_module); + return NULL; + } + + iv = emalloc(iv_len + 1); + if (iv_len <= Z_STRLEN_PP(tmpzval)) { + memcpy(iv, Z_STRVAL_PP(tmpzval), iv_len); + } else { + memcpy(iv, Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval)); + memset(iv + Z_STRLEN_PP(tmpzval), 0, iv_len - Z_STRLEN_PP(tmpzval)); + } + + result = mcrypt_generic_init(mcrypt_module, key, key_len, iv); + efree(iv); + if (result < 0) { + switch (result) { + case -3: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key length incorrect"); + break; + case -4: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Memory allocation error"); + break; + case -1: + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error"); + break; + } + mcrypt_module_close(mcrypt_module); + return NULL; + } + + data = pemalloc(sizeof(php_mcrypt_filter_data), persistent); + data->module = mcrypt_module; + data->encrypt = encrypt; + if (mcrypt_enc_is_block_mode(mcrypt_module)) { + data->blocksize = mcrypt_enc_get_block_size(mcrypt_module); + data->block_buffer = pemalloc(data->blocksize, persistent); + } else { + data->blocksize = 0; + data->block_buffer = NULL; + } + data->block_used = 0; + data->persistent = persistent; + + return php_stream_filter_alloc(&php_mcrypt_filter_ops, data, persistent); +} +/* }}} */ + +php_stream_filter_factory php_mcrypt_filter_factory = { + php_mcrypt_filter_create +}; + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/mcrypt/php_mcrypt.h b/ext/mcrypt/php_mcrypt.h new file mode 100644 index 0000000..4eb2c56 --- /dev/null +++ b/ext/mcrypt/php_mcrypt.h @@ -0,0 +1,94 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sascha Schumann <sascha@schumann.cx> | + | Derick Rethans <derick@derickrethans.nl> | + +----------------------------------------------------------------------+ + */ +/* $Id$ */ + +#ifndef PHP_MCRYPT_H +#define PHP_MCRYPT_H + +#if HAVE_LIBMCRYPT + +#ifdef ZTS +#include "TSRM.h" +#endif + +extern zend_module_entry mcrypt_module_entry; +#define mcrypt_module_ptr &mcrypt_module_entry + +/* Functions for both old and new API */ +PHP_FUNCTION(mcrypt_ecb); +PHP_FUNCTION(mcrypt_cbc); +PHP_FUNCTION(mcrypt_cfb); +PHP_FUNCTION(mcrypt_ofb); +PHP_FUNCTION(mcrypt_get_cipher_name); +PHP_FUNCTION(mcrypt_get_block_size); +PHP_FUNCTION(mcrypt_get_key_size); +PHP_FUNCTION(mcrypt_create_iv); + +/* Support functions for old API */ +PHP_FUNCTION(mcrypt_list_algorithms); +PHP_FUNCTION(mcrypt_list_modes); +PHP_FUNCTION(mcrypt_get_iv_size); +PHP_FUNCTION(mcrypt_encrypt); +PHP_FUNCTION(mcrypt_decrypt); + +/* Functions for new API */ +PHP_FUNCTION(mcrypt_module_open); +PHP_FUNCTION(mcrypt_generic_init); +PHP_FUNCTION(mcrypt_generic); +PHP_FUNCTION(mdecrypt_generic); +PHP_FUNCTION(mcrypt_generic_deinit); + +PHP_FUNCTION(mcrypt_enc_self_test); +PHP_FUNCTION(mcrypt_enc_is_block_algorithm_mode); +PHP_FUNCTION(mcrypt_enc_is_block_algorithm); +PHP_FUNCTION(mcrypt_enc_is_block_mode); +PHP_FUNCTION(mcrypt_enc_get_block_size); +PHP_FUNCTION(mcrypt_enc_get_key_size); +PHP_FUNCTION(mcrypt_enc_get_supported_key_sizes); +PHP_FUNCTION(mcrypt_enc_get_iv_size); +PHP_FUNCTION(mcrypt_enc_get_algorithms_name); +PHP_FUNCTION(mcrypt_enc_get_modes_name); +PHP_FUNCTION(mcrypt_module_self_test); +PHP_FUNCTION(mcrypt_module_is_block_algorithm_mode); +PHP_FUNCTION(mcrypt_module_is_block_algorithm); +PHP_FUNCTION(mcrypt_module_is_block_mode); +PHP_FUNCTION(mcrypt_module_get_algo_block_size); +PHP_FUNCTION(mcrypt_module_get_algo_key_size); +PHP_FUNCTION(mcrypt_module_get_supported_key_sizes); +PHP_FUNCTION(mcrypt_module_close); + +ZEND_BEGIN_MODULE_GLOBALS(mcrypt) + int le_h; + char *modes_dir; + char *algorithms_dir; +ZEND_END_MODULE_GLOBALS(mcrypt) + +#ifdef ZTS +# define MCG(v) TSRMG(mcrypt_globals_id, zend_mcrypt_globals *, v) +#else +# define MCG(v) (mcrypt_globals.v) +#endif + +#else +#define mcrypt_module_ptr NULL +#endif + +#define phpext_mcrypt_ptr mcrypt_module_ptr + +#endif diff --git a/ext/mcrypt/php_mcrypt_filter.h b/ext/mcrypt/php_mcrypt_filter.h new file mode 100644 index 0000000..0139130 --- /dev/null +++ b/ext/mcrypt/php_mcrypt_filter.h @@ -0,0 +1,39 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Sara Golemon <pollita@php.net> | + +----------------------------------------------------------------------+ + + $Id$ +*/ + +#ifndef PHP_MCRYPT_FILTER_H +#define PHP_MCRYPT_FILTER_H + +extern php_stream_filter_factory php_mcrypt_filter_factory; + +PHP_MINIT_FUNCTION(mcrypt_filter); +PHP_MSHUTDOWN_FUNCTION(mcrypt_filter); +PHP_MINFO_FUNCTION(mcrypt_filter); + +#endif /* PHP_MCRYPT_FILTER_H */ + + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * indent-tabs-mode: t + * End: + */ diff --git a/ext/mcrypt/tests/blowfish.phpt b/ext/mcrypt/tests/blowfish.phpt new file mode 100644 index 0000000..a133091 --- /dev/null +++ b/ext/mcrypt/tests/blowfish.phpt @@ -0,0 +1,91 @@ +--TEST-- +Test for blowfish compatibility +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +if(!function_exists("hex2bin")) { + function hex2bin($data) { + $len = strlen($data); + return pack("H" . $len, $data); + } +} + +print "key plain crypt guess stat\n"; +$null = "\0\0\0\0\0\0\0\0"; +$vectors = file(dirname(__FILE__) . "/vectors.txt"); + +$td = mcrypt_module_open ("blowfish", "", MCRYPT_MODE_ECB, ""); + +foreach($vectors as $data) { + $data = trim($data); + if ($data) { + list($key,$plain,$crypt) = preg_split("/[[:space:]]+/",$data); + printf("%s %s ", + $key, + $plain + ); + $key = hex2bin(trim($key)); + $plain = hex2bin(($plain)); + $crypt = strtolower(trim($crypt)); + + mcrypt_generic_init ($td, $key, $null); + $guess = mcrypt_generic ($td, $plain); + $guess = bin2hex($guess); + printf("%s %s %s\n", + $crypt, + $guess, + ($crypt==$guess ? "OK" : "BAD") + ); + } +} + +// Longer test case from http://www.schneier.com/code/vectors.txt +$td = mcrypt_module_open ("blowfish", "", MCRYPT_MODE_CBC, ""); + +$key = hex2bin( "0123456789ABCDEFF0E1D2C3B4A59687" ); +$iv = hex2bin( "FEDCBA9876543210" ); +$plain = hex2bin( "37363534333231204E6F77206973207468652074696D6520666F722000" ); + +mcrypt_generic_init( $td, $key, $iv ); +$guess = bin2hex( mcrypt_generic( $td, $plain ) ); + +echo "\n", $guess, "\n"; +?> +--EXPECTF-- +key plain crypt guess stat +0000000000000000 0000000000000000 4ef997456198dd78 4ef997456198dd78 OK +FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 51866fd5b85ecb8a 51866fd5b85ecb8a OK +3000000000000000 1000000000000001 7d856f9a613063f2 7d856f9a613063f2 OK +1111111111111111 1111111111111111 2466dd878b963c9d 2466dd878b963c9d OK +0123456789ABCDEF 1111111111111111 61f9c3802281b096 61f9c3802281b096 OK +1111111111111111 0123456789ABCDEF 7d0cc630afda1ec7 7d0cc630afda1ec7 OK +FEDCBA9876543210 0123456789ABCDEF 0aceab0fc6a0a28d 0aceab0fc6a0a28d OK +7CA110454A1A6E57 01A1D6D039776742 59c68245eb05282b 59c68245eb05282b OK +0131D9619DC1376E 5CD54CA83DEF57DA b1b8cc0b250f09a0 b1b8cc0b250f09a0 OK +07A1133E4A0B2686 0248D43806F67172 1730e5778bea1da4 1730e5778bea1da4 OK +3849674C2602319E 51454B582DDF440A a25e7856cf2651eb a25e7856cf2651eb OK +04B915BA43FEB5B6 42FD443059577FA2 353882b109ce8f1a 353882b109ce8f1a OK +0113B970FD34F2CE 059B5E0851CF143A 48f4d0884c379918 48f4d0884c379918 OK +0170F175468FB5E6 0756D8E0774761D2 432193b78951fc98 432193b78951fc98 OK +43297FAD38E373FE 762514B829BF486A 13f04154d69d1ae5 13f04154d69d1ae5 OK +07A7137045DA2A16 3BDD119049372802 2eedda93ffd39c79 2eedda93ffd39c79 OK +04689104C2FD3B2F 26955F6835AF609A d887e0393c2da6e3 d887e0393c2da6e3 OK +37D06BB516CB7546 164D5E404F275232 5f99d04f5b163969 5f99d04f5b163969 OK +1F08260D1AC2465E 6B056E18759F5CCA 4a057a3b24d3977b 4a057a3b24d3977b OK +584023641ABA6176 004BD6EF09176062 452031c1e4fada8e 452031c1e4fada8e OK +025816164629B007 480D39006EE762F2 7555ae39f59b87bd 7555ae39f59b87bd OK +49793EBC79B3258F 437540C8698F3CFA 53c55f9cb49fc019 53c55f9cb49fc019 OK +4FB05E1515AB73A7 072D43A077075292 7a8e7bfa937e89a3 7a8e7bfa937e89a3 OK +49E95D6D4CA229BF 02FE55778117F12A cf9c5d7a4986adb5 cf9c5d7a4986adb5 OK +018310DC409B26D6 1D9D5C5018F728C2 d1abb290658bc778 d1abb290658bc778 OK +1C587F1C13924FEF 305532286D6F295A 55cb3774d13ef201 55cb3774d13ef201 OK +0101010101010101 0123456789ABCDEF fa34ec4847b268b2 fa34ec4847b268b2 OK +1F1F1F1F0E0E0E0E 0123456789ABCDEF a790795108ea3cae a790795108ea3cae OK +E0FEE0FEF1FEF1FE 0123456789ABCDEF c39e072d9fac631d c39e072d9fac631d OK +0000000000000000 FFFFFFFFFFFFFFFF 014933e0cdaff6e4 014933e0cdaff6e4 OK +FFFFFFFFFFFFFFFF 0000000000000000 f21e9a77b71c49bc f21e9a77b71c49bc OK +0123456789ABCDEF 0000000000000000 245946885754369a 245946885754369a OK +FEDCBA9876543210 FFFFFFFFFFFFFFFF 6b5c5a9c5d9e0a5a 6b5c5a9c5d9e0a5a OK + +6b77b4d63006dee605b156e27403979358deb9e7154616d959f1652bd5ff92cc diff --git a/ext/mcrypt/tests/bug35496.phpt b/ext/mcrypt/tests/bug35496.phpt new file mode 100644 index 0000000..3add65e --- /dev/null +++ b/ext/mcrypt/tests/bug35496.phpt @@ -0,0 +1,14 @@ +--TEST-- +Bug #35496 (Crash in mcrypt_generic()/mdecrypt_generic() without proper init). +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open('rijndael-256', '', 'ofb', ''); +mcrypt_generic($td, "foobar"); +mdecrypt_generic($td, "baz"); +?> +--EXPECTF-- +Warning: mcrypt_generic(): Operation disallowed prior to mcrypt_generic_init(). in %sbug35496.php on line 3 + +Warning: mdecrypt_generic(): Operation disallowed prior to mcrypt_generic_init(). in %sbug35496.php on line 4 diff --git a/ext/mcrypt/tests/bug37595.phpt b/ext/mcrypt/tests/bug37595.phpt Binary files differnew file mode 100644 index 0000000..0530f3e --- /dev/null +++ b/ext/mcrypt/tests/bug37595.phpt diff --git a/ext/mcrypt/tests/bug41252.phpt b/ext/mcrypt/tests/bug41252.phpt new file mode 100644 index 0000000..481fc54 --- /dev/null +++ b/ext/mcrypt/tests/bug41252.phpt @@ -0,0 +1,13 @@ +--TEST-- +Bug #41252 (Calling mcrypt_generic without first calling mcrypt_generic_init crashes) +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, ''); +echo mcrypt_generic($td,'aaaaaaaa'); +print "I'm alive!\n"; +?> +--EXPECTF-- +Warning: mcrypt_generic(): Operation disallowed prior to mcrypt_generic_init(). in %sbug41252.php on line 3 +I'm alive! diff --git a/ext/mcrypt/tests/bug43143.phpt b/ext/mcrypt/tests/bug43143.phpt new file mode 100644 index 0000000..d9fccf3 --- /dev/null +++ b/ext/mcrypt/tests/bug43143.phpt @@ -0,0 +1,23 @@ +--TEST-- +Bug #43143 (Warning about empty IV with MCRYPT_MODE_ECB) +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; +if (!extension_loaded("hash")) print "skip"; ?> +--FILE-- +<?php +echo "ECB\n"; +$input = 'to be encrypted'; +$mkey = hash('sha256', 'secret key', TRUE); +$data = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mkey, $input, MCRYPT_MODE_ECB); +echo "CFB\n"; +$input = 'to be encrypted'; +$mkey = hash('sha256', 'secret key', TRUE); +$data = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mkey, $input, MCRYPT_MODE_CFB); +echo "END\n"; +?> +--EXPECTF-- +ECB +CFB + +Warning: mcrypt_encrypt(): Attempt to use an empty IV, which is NOT recommend in %sbug43143.php on line 9 +END diff --git a/ext/mcrypt/tests/bug46010.phpt b/ext/mcrypt/tests/bug46010.phpt new file mode 100644 index 0000000..ddb691e --- /dev/null +++ b/ext/mcrypt/tests/bug46010.phpt @@ -0,0 +1,16 @@ +--TEST-- +Bug #46010 (warnings incorrectly generated for iv in ecb mode) +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php + +var_dump(bin2hex(mcrypt_encrypt(MCRYPT_TRIPLEDES, "key", "data", MCRYPT_MODE_ECB))); +var_dump(bin2hex(mcrypt_encrypt(MCRYPT_TRIPLEDES, "key", "data", MCRYPT_MODE_ECB, "a"))); +var_dump(bin2hex(mcrypt_encrypt(MCRYPT_TRIPLEDES, "key", "data", MCRYPT_MODE_ECB, "12345678"))); + +?> +--EXPECTF-- +string(16) "372eeb4a524b8d31" +string(16) "372eeb4a524b8d31" +string(16) "372eeb4a524b8d31" diff --git a/ext/mcrypt/tests/bug49738.phpt b/ext/mcrypt/tests/bug49738.phpt new file mode 100644 index 0000000..8f01bec --- /dev/null +++ b/ext/mcrypt/tests/bug49738.phpt @@ -0,0 +1,13 @@ +--TEST-- +Bug #49738 (calling mcrypt after mcrypt_generic_deinit crashes) +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php + $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, ''); + mcrypt_generic_init($td, 'aaaaaaaa', 'aaaaaaaa'); + mcrypt_generic_deinit($td); + echo mcrypt_generic($td, 'aaaaaaaa'); +?> +--EXPECTF-- +Warning: mcrypt_generic(): Operation disallowed prior to mcrypt_generic_init(). in %sbug49738.php on line 5 diff --git a/ext/mcrypt/tests/bug55169.phpt b/ext/mcrypt/tests/bug55169.phpt new file mode 100644 index 0000000..bebcd67 --- /dev/null +++ b/ext/mcrypt/tests/bug55169.phpt @@ -0,0 +1,44 @@ +--TEST--
+mcrypt_create_iv https://bugs.php.net/bug.php?id=55169
+--CREDIT--
+Ryan Biesemeyer <ryan@yaauie.com>
+--SKIPIF--
+<?php if (!extension_loaded("mcrypt")) print "skip"; ?>
+--FILE--
+<?php
+for( $i=1; $i<=64; $i = $i*2 ){
+ echo 'Input: '. $i . PHP_EOL;
+ $random = mcrypt_create_iv( $i, MCRYPT_DEV_URANDOM );
+ echo ' Length: ' . strlen( $random ) . PHP_EOL;
+ echo ' Hex: '. bin2hex( $random ) . PHP_EOL;
+ echo PHP_EOL;
+}
+?>
+--EXPECTF--
+Input: 1
+ Length: 1
+ Hex: %x
+
+Input: 2
+ Length: 2
+ Hex: %x
+
+Input: 4
+ Length: 4
+ Hex: %x
+
+Input: 8
+ Length: 8
+ Hex: %x
+
+Input: 16
+ Length: 16
+ Hex: %x
+
+Input: 32
+ Length: 32
+ Hex: %x
+
+Input: 64
+ Length: 64
+ Hex: %x
diff --git a/ext/mcrypt/tests/bug8040.phpt b/ext/mcrypt/tests/bug8040.phpt new file mode 100644 index 0000000..78fef5c --- /dev/null +++ b/ext/mcrypt/tests/bug8040.phpt @@ -0,0 +1,24 @@ +--TEST-- +Bug #8040 (MCRYPT_MODE_* do not seem to exist) +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php + error_reporting (E_ALL ^ E_NOTICE); + echo MCRYPT_TWOFISH."\n"; + echo MCRYPT_MODE_CBC."\n"; + + define ("MODE1", MCRYPT_MODE_CBC); + echo MODE1."\n"; + + define ("CIPHER", MCRYPT_TWOFISH); + define ("MODE2", MCRYPT_MODE_CBC); + define ("MODE3", MCRYPT_CBC); + + printf ("cipher=".CIPHER. " mode1=".MODE2. " mode2=". MODE3."\n"); +?> +--EXPECT-- +twofish +cbc +cbc +cipher=twofish mode1=cbc mode2=MCRYPT_CBC diff --git a/ext/mcrypt/tests/mcrypt_cbc.phpt b/ext/mcrypt/tests/mcrypt_cbc.phpt new file mode 100644 index 0000000..f9160db --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cbc.phpt @@ -0,0 +1,23 @@ +--TEST-- +mcrypt_cbc +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$key = "FooBar"; +$secret = "PHP Testfest 2008"; +$cipher = MCRYPT_RIJNDAEL_128; + +$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher, MCRYPT_MODE_ECB), MCRYPT_RAND); +$enc_data = mcrypt_cbc($cipher, $key, $secret, MCRYPT_ENCRYPT, $iv); + +// we have to trim as AES rounds the blocks and decrypt doesnt detect that +echo trim(mcrypt_cbc($cipher, $key, $enc_data, MCRYPT_DECRYPT, $iv)) . "\n"; + +// a warning must be issued if we don't use a IV on a AES cipher, that usually requires an IV +mcrypt_cbc($cipher, $key, $enc_data, MCRYPT_DECRYPT); + +--EXPECTF-- +PHP Testfest 2008 + +Warning: mcrypt_cbc(): Attempt to use an empty IV, which is NOT recommend in %s on line %d diff --git a/ext/mcrypt/tests/mcrypt_cbc_3des_decrypt.phpt b/ext/mcrypt/tests/mcrypt_cbc_3des_decrypt.phpt new file mode 100644 index 0000000..6df3079 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cbc_3des_decrypt.phpt @@ -0,0 +1,102 @@ +--TEST-- +Test mcrypt_cbc() function : basic functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_cbc(int cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_cbc() : basic functionality ***\n"; + + +$cipher = MCRYPT_TRIPLEDES; +$data = b"This is the secret message which must be encrypted"; +$mode = MCRYPT_DECRYPT; + +// tripledes uses keys upto 192 bits (24 bytes) +$keys = array( + b'12345678', + b'12345678901234567890', + b'123456789012345678901234', + b'12345678901234567890123456' +); +$data1 = array( + 'IleMhoxiOthmHua4tFBHOw==', + 'EeF1s6C+w1IiHj1gdDn81g==', + 'EEuXpjZPueyYoG0LGQ199Q==', + 'EEuXpjZPueyYoG0LGQ199Q==' +); +// tripledes is a block cipher of 64 bits (8 bytes) +$ivs = array( + b'1234', + b'12345678', + b'123456789' +); + // data represented in base64 (ascii) +$data2 = array( + '+G7nGcWIxij3TZjpI9lJdQ==', + '3bJiFMeyScxOLQcE6mZtLg==', + '+G7nGcWIxij3TZjpI9lJdQ==' +); + +$iv = b'12345678'; +echo "\n--- testing different key lengths\n"; +for ($i = 0; $i < sizeof($keys); $i++) { + echo "\nkey length=".strlen($keys[$i])."\n"; + special_var_dump(mcrypt_cbc($cipher, $keys[$i], base64_decode($data1[$i]), $mode, $iv)); +} + +$key = b'1234567890123456'; +echo "\n--- testing different iv lengths\n"; +for ($i = 0; $i < sizeof($ivs); $i++) { + echo "\niv length=".strlen($ivs[$i])."\n"; + special_var_dump(mcrypt_cbc($cipher, $key, base64_decode($data2[$i]), $mode, $ivs[$i])); +} + +function special_var_dump($str) { + var_dump(bin2hex($str)); +} +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_cbc() : basic functionality *** + +--- testing different key lengths + +key length=8 +string(32) "736563726574206d6573736167650000" + +key length=20 +string(32) "736563726574206d6573736167650000" + +key length=24 +string(32) "736563726574206d6573736167650000" + +key length=26 + +Warning: mcrypt_cbc(): Size of key is too large for this algorithm in %s on line %d +string(32) "736563726574206d6573736167650000" + +--- testing different iv lengths + +iv length=4 + +Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d +string(32) "736563726574206d6573736167650000" + +iv length=8 +string(32) "736563726574206d6573736167650000" + +iv length=9 + +Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d +string(32) "736563726574206d6573736167650000" +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_cbc_3des_encrypt.phpt b/ext/mcrypt/tests/mcrypt_cbc_3des_encrypt.phpt new file mode 100644 index 0000000..35fabd9 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cbc_3des_encrypt.phpt @@ -0,0 +1,85 @@ +--TEST-- +Test mcrypt_cbc() function : basic functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_cbc(int cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_cbc() : basic functionality ***\n"; + + +$cipher = MCRYPT_TRIPLEDES; +$data = b"This is the secret message which must be encrypted"; +$mode = MCRYPT_ENCRYPT; + +// tripledes uses keys upto 192 bits (24 bytes) +$keys = array( + b'12345678', + b'12345678901234567890', + b'123456789012345678901234', + b'12345678901234567890123456'); +// tripledes is a block cipher of 64 bits (8 bytes) +$ivs = array( + b'1234', + b'12345678', + b'123456789'); + + +$iv = b'12345678'; +echo "\n--- testing different key lengths\n"; +foreach ($keys as $key) { + echo "\nkey length=".strlen($key)."\n"; + var_dump(bin2hex(mcrypt_cbc($cipher, $key, $data, $mode, $iv))); +} + +$key = b'1234567890123456'; +echo "\n--- testing different iv lengths\n"; +foreach ($ivs as $iv) { + echo "\niv length=".strlen($iv)."\n"; + var_dump(bin2hex(mcrypt_cbc($cipher, $key, $data, $mode, $iv))); +} +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_cbc() : basic functionality *** + +--- testing different key lengths + +key length=8 +string(112) "082b437d039d09418e20dc9de1dafa7ed6da5c6335b78950968441da1faf40c1f886e04da8ca177b80b376811e138c1bf51cb48dae2e7939" + +key length=20 +string(112) "0627351e0f8a082bf7981ae2c700a43fd3d44b270ac67b00fded1c5796eea935be0fef2a23da0b3f5e243929e62ac957bf0bf463aa90fc4f" + +key length=24 +string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac" + +key length=26 + +Warning: mcrypt_cbc(): Size of key is too large for this algorithm in %s on line %d +string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac" + +--- testing different iv lengths + +iv length=4 + +Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d +string(112) "440a6f54601969b127aad3c217ce7583c7f7b29989693130645569301db0020b29a34a3dcd104b2d0e3ba19d6cbd8a33d352b9c27cc34ef1" + +iv length=8 +string(112) "bac347506bf092c5557c4363c301745d78f047028e2953e84fd66b30aeb6005812dadbe8baa871b83278341599b0c448ddaaa52b5a378ce5" + +iv length=9 + +Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d +string(112) "440a6f54601969b127aad3c217ce7583c7f7b29989693130645569301db0020b29a34a3dcd104b2d0e3ba19d6cbd8a33d352b9c27cc34ef1" +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_cbc_error.phpt b/ext/mcrypt/tests/mcrypt_cbc_error.phpt new file mode 100644 index 0000000..ec3912b --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cbc_error.phpt @@ -0,0 +1,52 @@ +--TEST-- +Test mcrypt_cbc() function : error conditions +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_cbc(int cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_cbc() : error conditions ***\n"; + + +//Test mcrypt_cbc with one more than the expected number of arguments +echo "\n-- Testing mcrypt_cbc() function with more than expected no. of arguments --\n"; +$cipher = 10; +$key = 'string_val'; +$data = 'string_val'; +$mode = 10; +$iv = 'string_val'; +$extra_arg = 10; +var_dump( mcrypt_cbc($cipher, $key, $data, $mode, $iv, $extra_arg) ); + +// Testing mcrypt_cbc with one less than the expected number of arguments +echo "\n-- Testing mcrypt_cbc() function with less than expected no. of arguments --\n"; +$cipher = 10; +$key = 'string_val'; +$data = 'string_val'; +var_dump( mcrypt_cbc($cipher, $key, $data) ); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_cbc() : error conditions *** + +-- Testing mcrypt_cbc() function with more than expected no. of arguments -- + +Warning: mcrypt_cbc() expects at most 5 parameters, 6 given in %s on line %d +NULL + +-- Testing mcrypt_cbc() function with less than expected no. of arguments -- + +Warning: mcrypt_cbc() expects at least 4 parameters, 3 given in %s on line %d +NULL +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_cbc_variation1.phpt b/ext/mcrypt/tests/mcrypt_cbc_variation1.phpt new file mode 100644 index 0000000..5482de3 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cbc_variation1.phpt @@ -0,0 +1,230 @@ +--TEST-- +Test mcrypt_cbc() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_cbc(string cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_cbc() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$key = b'string_val'; +$data = b'string_val'; +$mode = MCRYPT_ENCRYPT; +$iv = b'string_val'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return "Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = <<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for cipher + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( mcrypt_cbc($value, $key, $data, $mode, $iv) ); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_cbc() : usage variation *** + +--int 0-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--int 1-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--int 12345-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--int -12345-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--float 10.5-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--float -10.5-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--float 12.3456789000e10-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--float -12.3456789000e10-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--float .5-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--empty array-- +Error: 2 - mcrypt_cbc() expects parameter 1 to be string, array given, %s(%d) +NULL + +--int indexed array-- +Error: 2 - mcrypt_cbc() expects parameter 1 to be string, array given, %s(%d) +NULL + +--associative array-- +Error: 2 - mcrypt_cbc() expects parameter 1 to be string, array given, %s(%d) +NULL + +--nested arrays-- +Error: 2 - mcrypt_cbc() expects parameter 1 to be string, array given, %s(%d) +NULL + +--uppercase NULL-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--lowercase null-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--lowercase true-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--lowercase false-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--uppercase TRUE-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--uppercase FALSE-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--empty string DQ-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--empty string SQ-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithToString-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithoutToString-- +Error: 2 - mcrypt_cbc() expects parameter 1 to be string, object given, %s(%d) +NULL + +--undefined var-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--unset var-- +Error: 2 - mcrypt_cbc(): Module initialization failed, %s(%d) +bool(false) + +--resource-- +Error: 2 - mcrypt_cbc() expects parameter 1 to be string, resource given, %s(%d) +NULL +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_cbc_variation2.phpt b/ext/mcrypt/tests/mcrypt_cbc_variation2.phpt new file mode 100644 index 0000000..f49e80e --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cbc_variation2.phpt @@ -0,0 +1,210 @@ +--TEST-- +Test mcrypt_cbc() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_cbc(string cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_cbc() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$data = b'string_val'; +$mode = MCRYPT_ENCRYPT; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for key + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump(bin2hex(mcrypt_cbc($cipher, $value, $data, $mode, $iv))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_cbc() : usage variation *** + +--int 0-- +string(32) "bc27b3a4e33b531d5983fc7df693cd09" + +--int 1-- +string(32) "bc27b3a4e33b531d5983fc7df693cd09" + +--int 12345-- +string(32) "d109b7973383127002474ae731c4b3a8" + +--int -12345-- +string(32) "3e82a931cedb03a38b91a637ff8c9f9e" + +--float 10.5-- +string(32) "de71833586c1d7132a289960ebeeca7a" + +--float -10.5-- +string(32) "7d0489dd2e99ae910ecc015573f3dd16" + +--float 12.3456789000e10-- +string(32) "978055b42c0506a8947e3c3c8d994baf" + +--float -12.3456789000e10-- +string(32) "4aa84ba400c2b8ef467d4d98372b4f4e" + +--float .5-- +string(32) "e731dc5059b84e0c8774ac490f77d6e6" + +--empty array-- +Error: 2 - mcrypt_cbc() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_cbc() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_cbc() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_cbc() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +string(32) "be722a5ffc361d721fbcab1eacc6acf5" + +--lowercase null-- +string(32) "be722a5ffc361d721fbcab1eacc6acf5" + +--lowercase true-- +string(32) "bc27b3a4e33b531d5983fc7df693cd09" + +--lowercase false-- +string(32) "be722a5ffc361d721fbcab1eacc6acf5" + +--uppercase TRUE-- +string(32) "bc27b3a4e33b531d5983fc7df693cd09" + +--uppercase FALSE-- +string(32) "be722a5ffc361d721fbcab1eacc6acf5" + +--empty string DQ-- +string(32) "be722a5ffc361d721fbcab1eacc6acf5" + +--empty string SQ-- +string(32) "be722a5ffc361d721fbcab1eacc6acf5" + +--instance of classWithToString-- +string(32) "19420fa26f561ee82ed84abbcd2d284b" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_cbc() expects parameter 2 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +string(32) "be722a5ffc361d721fbcab1eacc6acf5" + +--unset var-- +string(32) "be722a5ffc361d721fbcab1eacc6acf5" + +--resource-- +Error: 2 - mcrypt_cbc() expects parameter 2 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_cbc_variation3.phpt b/ext/mcrypt/tests/mcrypt_cbc_variation3.phpt new file mode 100644 index 0000000..5b2398d --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cbc_variation3.phpt @@ -0,0 +1,210 @@ +--TEST-- +Test mcrypt_cbc() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_cbc(string cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_cbc() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$mode = MCRYPT_ENCRYPT; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for data + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump(bin2hex(mcrypt_cbc($cipher, $key, $value, $mode, $iv))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_cbc() : usage variation *** + +--int 0-- +string(16) "ce5fcfe737859795" + +--int 1-- +string(16) "84df495f6cd82dd9" + +--int 12345-- +string(16) "905ab1ae27ee9991" + +--int -12345-- +string(16) "5835174e9c67c3e7" + +--float 10.5-- +string(16) "28ff0601ad9e47fa" + +--float -10.5-- +string(16) "ce9f2b6e2fc3d9f7" + +--float 12.3456789000e10-- +string(32) "24eb882ce9763e4018fba9b7f01b0c3e" + +--float -12.3456789000e10-- +string(32) "5eed30e428f32de1d7a7064d0ed4d3eb" + +--float .5-- +string(16) "bebf2a13676e1e30" + +--empty array-- +Error: 2 - mcrypt_cbc() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_cbc() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_cbc() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_cbc() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +string(16) "206f6d3617a5ab32" + +--lowercase null-- +string(16) "206f6d3617a5ab32" + +--lowercase true-- +string(16) "84df495f6cd82dd9" + +--lowercase false-- +string(16) "206f6d3617a5ab32" + +--uppercase TRUE-- +string(16) "84df495f6cd82dd9" + +--uppercase FALSE-- +string(16) "206f6d3617a5ab32" + +--empty string DQ-- +string(16) "206f6d3617a5ab32" + +--empty string SQ-- +string(16) "206f6d3617a5ab32" + +--instance of classWithToString-- +string(32) "7c91cdf8f8c51485034a9ee528eb016b" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_cbc() expects parameter 3 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +string(16) "206f6d3617a5ab32" + +--unset var-- +string(16) "206f6d3617a5ab32" + +--resource-- +Error: 2 - mcrypt_cbc() expects parameter 3 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_cbc_variation4.phpt b/ext/mcrypt/tests/mcrypt_cbc_variation4.phpt new file mode 100644 index 0000000..f9a511f --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cbc_variation4.phpt @@ -0,0 +1,205 @@ +--TEST-- +Test mcrypt_cbc() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_cbc(string cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_cbc() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$data = b'string_val'; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return "Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = <<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // string data + 'string DQ' => "string", + 'string SQ' => 'string', + 'mixed case string' => "sTrInG", + 'heredoc' => $heredoc, + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for mode + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump(bin2hex(mcrypt_cbc($cipher, $key, $data, $value, $iv))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_cbc() : usage variation *** + +--float 10.5-- +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--float -10.5-- +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--float 12.3456789000e10-- +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--float -12.3456789000e10-- +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--float .5-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--empty array-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--int indexed array-- +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--associative array-- +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--nested arrays-- +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--uppercase NULL-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--lowercase null-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--lowercase true-- +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--lowercase false-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--uppercase TRUE-- +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--uppercase FALSE-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--empty string DQ-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--empty string SQ-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--string DQ-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--string SQ-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--mixed case string-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--heredoc-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--instance of classWithToString-- +Error: 8 - Object of class classWithToString could not be converted to int, %s(%d) +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--instance of classWithoutToString-- +Error: 8 - Object of class classWithoutToString could not be converted to int, %s(%d) +string(32) "983d5edc5f77fe42e2372a0339dc22b0" + +--undefined var-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--unset var-- +string(32) "5f781523f696d596e4b809d72197a0cc" + +--resource-- +string(%d) %s +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_cbc_variation5.phpt b/ext/mcrypt/tests/mcrypt_cbc_variation5.phpt new file mode 100644 index 0000000..7d49db8 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cbc_variation5.phpt @@ -0,0 +1,230 @@ +--TEST-- +Test mcrypt_cbc() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_cbc(string cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_cbc() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$data = b'string_val'; +$mode = MCRYPT_ENCRYPT; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for iv + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( bin2hex(mcrypt_cbc($cipher, $key, $data, $mode, $value)) ); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_cbc() : usage variation *** + +--int 0-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--int 1-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--int 12345-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--int -12345-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float 10.5-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float -10.5-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float 12.3456789000e10-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float -12.3456789000e10-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float .5-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--empty array-- +Error: 2 - mcrypt_cbc() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_cbc() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_cbc() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_cbc() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--lowercase null-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--lowercase true-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--lowercase false-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--uppercase TRUE-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--uppercase FALSE-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--empty string DQ-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--empty string SQ-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--instance of classWithToString-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_cbc() expects parameter 5 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--unset var-- +Error: 2 - mcrypt_cbc(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--resource-- +Error: 2 - mcrypt_cbc() expects parameter 5 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_cfb.phpt b/ext/mcrypt/tests/mcrypt_cfb.phpt new file mode 100644 index 0000000..54919c8 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_cfb.phpt @@ -0,0 +1,23 @@ +--TEST-- +mcrypt_cfb +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$key = "FooBar"; +$secret = "PHP Testfest 2008"; +$cipher = MCRYPT_RIJNDAEL_128; + +$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher, MCRYPT_MODE_CFB), MCRYPT_RAND); +$enc_data = mcrypt_cfb($cipher, $key, $secret, MCRYPT_ENCRYPT, $iv); + +// we have to trim as AES rounds the blocks and decrypt doesnt detect that +echo trim(mcrypt_cfb($cipher, $key, $enc_data, MCRYPT_DECRYPT, $iv)) . "\n"; + +// a warning must be issued if we don't use a IV on a AES cipher, that usually requires an IV +mcrypt_cfb($cipher, $key, $enc_data, MCRYPT_DECRYPT); + +--EXPECTF-- +PHP Testfest 2008 + +Warning: mcrypt_cfb(): Attempt to use an empty IV, which is NOT recommend in %s on line %d diff --git a/ext/mcrypt/tests/mcrypt_create_iv.phpt b/ext/mcrypt/tests/mcrypt_create_iv.phpt new file mode 100644 index 0000000..1aa4886 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_create_iv.phpt @@ -0,0 +1,17 @@ +--TEST-- +mcrypt_create_iv +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$iv1 = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND); +$iv2 = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_DEV_URANDOM); +$iv3 = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_DEV_RANDOM); + +echo strlen($iv1) . "\n"; +echo strlen($iv2) . "\n"; +echo strlen($iv3) . "\n"; +--EXPECT-- +16 +16 +16 diff --git a/ext/mcrypt/tests/mcrypt_decrypt.phpt b/ext/mcrypt/tests/mcrypt_decrypt.phpt new file mode 100644 index 0000000..b4e6284 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_decrypt.phpt @@ -0,0 +1,28 @@ +--TEST-- +mcrypt_decrypt +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$key = "FooBar"; +$secret = "PHP Testfest 2008"; +$mode = MCRYPT_MODE_CBC; +$cipher = MCRYPT_RIJNDAEL_128; + +$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher, $mode), MCRYPT_RAND); +$enc_data = mcrypt_encrypt($cipher, $key, $secret, $mode, $iv); + +// we have to trim as AES rounds the blocks and decrypt doesnt detect that +echo trim(mcrypt_decrypt($cipher, $key, $enc_data, $mode, $iv)) . "\n"; + +// a warning must be issued if we don't use a IV on a AES cipher, that usually requires an IV +mcrypt_decrypt($cipher, $key, $enc_data, MCRYPT_MODE_CBC); + +var_dump(strpos(mcrypt_decrypt(MCRYPT_BLOWFISH, "FooBar", $enc_data, MCRYPT_MODE_CBC, $iv), "Testfest") !== false); +--EXPECTF-- +PHP Testfest 2008 + +Warning: mcrypt_decrypt(): Attempt to use an empty IV, which is NOT recommend in %s on line %d + +Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d +bool(false)
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_decrypt_3des_cbc.phpt b/ext/mcrypt/tests/mcrypt_decrypt_3des_cbc.phpt new file mode 100644 index 0000000..30d0c2b --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_decrypt_3des_cbc.phpt @@ -0,0 +1,101 @@ +--TEST-- +Test mcrypt_decrypt() function : basic functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_decrypt() : basic functionality ***\n"; + + +// Initialise all required variables +$cipher = MCRYPT_3DES; +$mode = MCRYPT_MODE_CBC; + +// tripledes uses keys upto 192 bits (24 bytes) +$keys = array( + b'12345678', + b'12345678901234567890', + b'123456789012345678901234', + b'12345678901234567890123456' +); +$data1 = array( + 'IleMhoxiOthmHua4tFBHOw==', + 'EeF1s6C+w1IiHj1gdDn81g==', + 'EEuXpjZPueyYoG0LGQ199Q==', + 'EEuXpjZPueyYoG0LGQ199Q==' +); +// tripledes is a block cipher of 64 bits (8 bytes) +$ivs = array( + b'1234', + b'12345678', + b'123456789' +); +$data2 = array( + '+G7nGcWIxij3TZjpI9lJdQ==', + '3bJiFMeyScxOLQcE6mZtLg==', + '+G7nGcWIxij3TZjpI9lJdQ==' +); + +$iv = b'12345678'; +echo "\n--- testing different key lengths\n"; +for ($i = 0; $i < sizeof($keys); $i++) { + echo "\nkey length=".strlen($keys[$i])."\n"; + special_var_dump(mcrypt_decrypt($cipher, $keys[$i], base64_decode($data1[$i]), $mode, $iv)); +} + +$key = b'1234567890123456'; +echo "\n--- testing different iv lengths\n"; +for ($i = 0; $i < sizeof($ivs); $i++) { + echo "\niv length=".strlen($ivs[$i])."\n"; + special_var_dump(mcrypt_decrypt($cipher, $key, base64_decode($data2[$i]), $mode, $ivs[$i])); +} + +function special_var_dump($str) { + var_dump(bin2hex($str)); +} +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_decrypt() : basic functionality *** + +--- testing different key lengths + +key length=8 +string(32) "736563726574206d6573736167650000" + +key length=20 +string(32) "736563726574206d6573736167650000" + +key length=24 +string(32) "736563726574206d6573736167650000" + +key length=26 + +Warning: mcrypt_decrypt(): Size of key is too large for this algorithm in %s on line %d +string(32) "736563726574206d6573736167650000" + +--- testing different iv lengths + +iv length=4 + +Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d +string(32) "736563726574206d6573736167650000" + +iv length=8 +string(32) "736563726574206d6573736167650000" + +iv length=9 + +Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d +string(32) "736563726574206d6573736167650000" +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_decrypt_3des_ecb.phpt b/ext/mcrypt/tests/mcrypt_decrypt_3des_ecb.phpt new file mode 100644 index 0000000..5f841c1 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_decrypt_3des_ecb.phpt @@ -0,0 +1,96 @@ +--TEST-- +Test mcrypt_decrypt() function : basic functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_decrypt() : basic functionality ***\n"; + + +// Initialise all required variables +$cipher = MCRYPT_3DES; +$mode = MCRYPT_MODE_ECB; + +// tripledes uses keys upto 192 bits (24 bytes) +$keys = array( + b'12345678', + b'12345678901234567890', + b'123456789012345678901234', + b'12345678901234567890123456' +); +$data1 = array( + '0D4ArM3ejyhic9rnCcIW9A==', + 'q0wt1YeOjLpnKm5WsrzKEw==', + 'zwKEFeqHkhlj+7HZTRA/yA==', + 'zwKEFeqHkhlj+7HZTRA/yA==' +); +// tripledes is a block cipher of 64 bits (8 bytes) +$ivs = array( + b'1234', + b'12345678', + b'123456789' +); +$data2 = array( + '+G7nGcWIxigQcJD+2P14HA==', + '+G7nGcWIxigQcJD+2P14HA==', + '+G7nGcWIxigQcJD+2P14HA==' +); + +echo "\n--- testing different key lengths\n"; +for ($i = 0; $i < sizeof($keys); $i++) { + echo "\nkey length=".strlen($keys[$i])."\n"; + special_var_dump(mcrypt_decrypt($cipher, $keys[$i], base64_decode($data1[$i]), $mode)); +} + +$key = b'1234567890123456'; +echo "\n--- testing different iv lengths\n"; +for ($i = 0; $i < sizeof($ivs); $i++) { + echo "\niv length=".strlen($ivs[$i])."\n"; + special_var_dump(mcrypt_decrypt($cipher, $key, base64_decode($data2[$i]), $mode, $ivs[$i])); +} + +function special_var_dump($str) { + var_dump(bin2hex($str)); +} +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_decrypt() : basic functionality *** + +--- testing different key lengths + +key length=8 +string(32) "736563726574206d6573736167650000" + +key length=20 +string(32) "736563726574206d6573736167650000" + +key length=24 +string(32) "736563726574206d6573736167650000" + +key length=26 + +Warning: mcrypt_decrypt(): Size of key is too large for this algorithm in %s on line %d +string(32) "736563726574206d6573736167650000" + +--- testing different iv lengths + +iv length=4 +string(32) "736563726574206d6573736167650000" + +iv length=8 +string(32) "736563726574206d6573736167650000" + +iv length=9 +string(32) "736563726574206d6573736167650000" +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_decrypt_error.phpt b/ext/mcrypt/tests/mcrypt_decrypt_error.phpt new file mode 100644 index 0000000..aae42d8 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_decrypt_error.phpt @@ -0,0 +1,52 @@ +--TEST-- +Test mcrypt_decrypt() function : error conditions +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_decrypt() : error conditions ***\n"; + + +//Test mcrypt_decrypt with one more than the expected number of arguments +echo "\n-- Testing mcrypt_decrypt() function with more than expected no. of arguments --\n"; +$cipher = 'string_val'; +$key = 'string_val'; +$data = 'string_val'; +$mode = 'string_val'; +$iv = 'string_val'; +$extra_arg = 10; +var_dump( mcrypt_decrypt($cipher, $key, $data, $mode, $iv, $extra_arg) ); + +// Testing mcrypt_decrypt with one less than the expected number of arguments +echo "\n-- Testing mcrypt_decrypt() function with less than expected no. of arguments --\n"; +$cipher = 'string_val'; +$key = 'string_val'; +$data = 'string_val'; +var_dump( mcrypt_decrypt($cipher, $key, $data) ); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_decrypt() : error conditions *** + +-- Testing mcrypt_decrypt() function with more than expected no. of arguments -- + +Warning: mcrypt_decrypt() expects at most 5 parameters, 6 given in %s on line %d +NULL + +-- Testing mcrypt_decrypt() function with less than expected no. of arguments -- + +Warning: mcrypt_decrypt() expects at least 4 parameters, 3 given in %s on line %d +NULL +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_decrypt_variation1.phpt b/ext/mcrypt/tests/mcrypt_decrypt_variation1.phpt new file mode 100644 index 0000000..21fda19 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_decrypt_variation1.phpt @@ -0,0 +1,230 @@ +--TEST-- +Test mcrypt_decrypt() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_decrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$key = b'string_val'; +$data = b'string_val'; +$mode = MCRYPT_MODE_ECB; +$iv = b'string_val'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return "Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = <<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for cipher + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( mcrypt_decrypt($value, $key, $data, $mode, $iv) ); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_decrypt() : usage variation *** + +--int 0-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--int 1-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--int 12345-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--int -12345-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float 10.5-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float -10.5-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float 12.3456789000e10-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float -12.3456789000e10-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float .5-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty array-- +Error: 2 - mcrypt_decrypt() expects parameter 1 to be string, array given, %s(%d) +NULL + +--int indexed array-- +Error: 2 - mcrypt_decrypt() expects parameter 1 to be string, array given, %s(%d) +NULL + +--associative array-- +Error: 2 - mcrypt_decrypt() expects parameter 1 to be string, array given, %s(%d) +NULL + +--nested arrays-- +Error: 2 - mcrypt_decrypt() expects parameter 1 to be string, array given, %s(%d) +NULL + +--uppercase NULL-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase null-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase true-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase false-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase TRUE-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase FALSE-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty string DQ-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty string SQ-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithToString-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithoutToString-- +Error: 2 - mcrypt_decrypt() expects parameter 1 to be string, object given, %s(%d) +NULL + +--undefined var-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--unset var-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--resource-- +Error: 2 - mcrypt_decrypt() expects parameter 1 to be string, resource given, %s(%d) +NULL +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_decrypt_variation2.phpt b/ext/mcrypt/tests/mcrypt_decrypt_variation2.phpt new file mode 100644 index 0000000..85adb9c --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_decrypt_variation2.phpt @@ -0,0 +1,210 @@ +--TEST-- +Test mcrypt_decrypt() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_decrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$data = b'string_val'; +$mode = MCRYPT_MODE_ECB; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for key + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( bin2hex(mcrypt_decrypt($cipher, $value, $data, $mode, $iv))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_decrypt() : usage variation *** + +--int 0-- +string(32) "43a1ae011df36064589d06bc922ecd97" + +--int 1-- +string(32) "43a1ae011df36064589d06bc922ecd97" + +--int 12345-- +string(32) "e5885552e16c44d4eb6164f477b40200" + +--int -12345-- +string(32) "adf7873831a9035cda9f9dc3b7dc626b" + +--float 10.5-- +string(32) "08b0b9fac9c227437b7b5d0147e6153b" + +--float -10.5-- +string(32) "f470cc74d83471b42a3e28d4ec57799a" + +--float 12.3456789000e10-- +string(32) "36c618c00523fadc372b871eaa9c7b16" + +--float -12.3456789000e10-- +string(32) "a554a5bdb7a5ceb6ae6f20566ef02e49" + +--float .5-- +string(32) "bcb840ff76d3788a7911ed36f088a910" + +--empty array-- +Error: 2 - mcrypt_decrypt() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_decrypt() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_decrypt() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_decrypt() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--lowercase null-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--lowercase true-- +string(32) "43a1ae011df36064589d06bc922ecd97" + +--lowercase false-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--uppercase TRUE-- +string(32) "43a1ae011df36064589d06bc922ecd97" + +--uppercase FALSE-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--empty string DQ-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--empty string SQ-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--instance of classWithToString-- +string(32) "478f9d080563835cc3136610802f1433" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_decrypt() expects parameter 2 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--unset var-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--resource-- +Error: 2 - mcrypt_decrypt() expects parameter 2 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_decrypt_variation3.phpt b/ext/mcrypt/tests/mcrypt_decrypt_variation3.phpt new file mode 100644 index 0000000..50da260 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_decrypt_variation3.phpt @@ -0,0 +1,210 @@ +--TEST-- +Test mcrypt_decrypt() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_decrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$mode = MCRYPT_MODE_ECB; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for data + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump(bin2hex(mcrypt_decrypt($cipher, $key, $value, $mode, $iv))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_decrypt() : usage variation *** + +--int 0-- +string(16) "52833a00168e547f" + +--int 1-- +string(16) "82011a0a93098a13" + +--int 12345-- +string(16) "e8b71c21b6acc162" + +--int -12345-- +string(16) "db3c458e975563a8" + +--float 10.5-- +string(16) "6ee8764562f25913" + +--float -10.5-- +string(16) "d63b39fd5f65678e" + +--float 12.3456789000e10-- +string(32) "7712cc4828221be40672239d9c32e742" + +--float -12.3456789000e10-- +string(32) "caa892cb5d28b53c2b75b1e0799427c3" + +--float .5-- +string(16) "99880c86884385d9" + +--empty array-- +Error: 2 - mcrypt_decrypt() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_decrypt() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_decrypt() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_decrypt() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +string(16) "d27689f6fd9700f4" + +--lowercase null-- +string(16) "d27689f6fd9700f4" + +--lowercase true-- +string(16) "82011a0a93098a13" + +--lowercase false-- +string(16) "d27689f6fd9700f4" + +--uppercase TRUE-- +string(16) "82011a0a93098a13" + +--uppercase FALSE-- +string(16) "d27689f6fd9700f4" + +--empty string DQ-- +string(16) "d27689f6fd9700f4" + +--empty string SQ-- +string(16) "d27689f6fd9700f4" + +--instance of classWithToString-- +string(32) "46677e368bc07ef375bd580e0c4b2594" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_decrypt() expects parameter 3 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +string(16) "d27689f6fd9700f4" + +--unset var-- +string(16) "d27689f6fd9700f4" + +--resource-- +Error: 2 - mcrypt_decrypt() expects parameter 3 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_decrypt_variation4.phpt b/ext/mcrypt/tests/mcrypt_decrypt_variation4.phpt new file mode 100644 index 0000000..12e4431 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_decrypt_variation4.phpt @@ -0,0 +1,235 @@ +--TEST-- +Test mcrypt_decrypt() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_decrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$data = b'string_val'; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return "Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = <<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for mode + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( mcrypt_decrypt($cipher, $key, $data, $value, $iv) ); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_decrypt() : usage variation *** + +--int 0-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--int 1-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--int 12345-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--int -12345-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float 10.5-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float -10.5-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float 12.3456789000e10-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float -12.3456789000e10-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--float .5-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty array-- +Error: 8 - Array to string conversion, %s(%d) +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--int indexed array-- +Error: 8 - Array to string conversion, %s(%d) +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--associative array-- +Error: 8 - Array to string conversion, %s(%d) +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--nested arrays-- +Error: 8 - Array to string conversion, %s(%d) +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase NULL-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase null-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase true-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase false-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase TRUE-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase FALSE-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty string DQ-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty string SQ-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithToString-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithoutToString-- +Error: 4096 - Object of class classWithoutToString could not be converted to string, %s(%d) +Error: 8 - Object of class classWithoutToString to string conversion, %s(%d) +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--undefined var-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--unset var-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) + +--resource-- +Error: 2 - mcrypt_decrypt(): Module initialization failed, %s(%d) +bool(false) +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_decrypt_variation5.phpt b/ext/mcrypt/tests/mcrypt_decrypt_variation5.phpt new file mode 100644 index 0000000..f1541bd --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_decrypt_variation5.phpt @@ -0,0 +1,230 @@ +--TEST-- +Test mcrypt_decrypt() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_decrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$data = b'string_val'; +$mode = MCRYPT_MODE_CBC; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for iv + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump(bin2hex(mcrypt_decrypt($cipher, $key, $data, $mode, $value))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_decrypt() : usage variation *** + +--int 0-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--int 1-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--int 12345-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--int -12345-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--float 10.5-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--float -10.5-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--float 12.3456789000e10-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--float -12.3456789000e10-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--float .5-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--empty array-- +Error: 2 - mcrypt_decrypt() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_decrypt() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_decrypt() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_decrypt() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--lowercase null-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--lowercase true-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--lowercase false-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--uppercase TRUE-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--uppercase FALSE-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--empty string DQ-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--empty string SQ-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--instance of classWithToString-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_decrypt() expects parameter 5 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--unset var-- +Error: 2 - mcrypt_decrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "a80c6cef6b42c875e2372a0339dc22b0" + +--resource-- +Error: 2 - mcrypt_decrypt() expects parameter 5 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_ecb.phpt b/ext/mcrypt/tests/mcrypt_ecb.phpt new file mode 100644 index 0000000..e75d9fa --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ecb.phpt @@ -0,0 +1,21 @@ +--TEST-- +mcrypt_ecb +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$key = "FooBar"; +$secret = "PHP Testfest 2008"; +$cipher = MCRYPT_RIJNDAEL_128; + +$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher, MCRYPT_MODE_ECB), MCRYPT_RAND); +$enc_data = mcrypt_ecb($cipher, $key, $secret, MCRYPT_ENCRYPT, $iv); + +// we have to trim as AES rounds the blocks and decrypt doesnt detect that +echo trim(mcrypt_ecb($cipher, $key, $enc_data, MCRYPT_DECRYPT, $iv)) . "\n"; + +// a warning must be issued if we don't use a IV on a AES cipher, that usually requires an IV +mcrypt_ecb($cipher, $key, $enc_data, MCRYPT_DECRYPT); + +--EXPECTF-- +PHP Testfest 2008 diff --git a/ext/mcrypt/tests/mcrypt_ecb_3des_decrypt.phpt b/ext/mcrypt/tests/mcrypt_ecb_3des_decrypt.phpt new file mode 100644 index 0000000..7a5cc27 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ecb_3des_decrypt.phpt @@ -0,0 +1,97 @@ +--TEST-- +Test mcrypt_cbc() function : basic functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_ecb(int cipher, string key, string data, int mode, string iv) + * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_ecb() : basic functionality ***\n"; + + +$cipher = MCRYPT_TRIPLEDES; +$data = b"This is the secret message which must be encrypted"; +$mode = MCRYPT_DECRYPT; + +// tripledes uses keys upto 192 bits (24 bytes) +$keys = array( + b'12345678', + b'12345678901234567890', + b'123456789012345678901234', + b'12345678901234567890123456' +); +$data1 = array( + '0D4ArM3ejyhic9rnCcIW9A==', + 'q0wt1YeOjLpnKm5WsrzKEw==', + 'zwKEFeqHkhlj+7HZTRA/yA==', + 'zwKEFeqHkhlj+7HZTRA/yA==' +); +// tripledes is a block cipher of 64 bits (8 bytes) +$ivs = array( + b'1234', + b'12345678', + b'123456789' +); +$data2 = array( + '+G7nGcWIxigQcJD+2P14HA==', + '+G7nGcWIxigQcJD+2P14HA==', + '+G7nGcWIxigQcJD+2P14HA==' +); + +$iv = b'12345678'; +echo "\n--- testing different key lengths\n"; +for ($i = 0; $i < sizeof($keys); $i++) { + echo "\nkey length=".strlen($keys[$i])."\n"; + special_var_dump(mcrypt_ecb($cipher, $keys[$i], base64_decode($data1[$i]), $mode, $iv)); +} + +$key = b'1234567890123456'; +echo "\n--- testing different iv lengths\n"; +for ($i = 0; $i < sizeof($ivs); $i++) { + echo "\niv length=".strlen($ivs[$i])."\n"; + special_var_dump(mcrypt_ecb($cipher, $key, base64_decode($data2[$i]), $mode, $ivs[$i])); +} + +function special_var_dump($str) { + var_dump(bin2hex($str)); +} +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_ecb() : basic functionality *** + +--- testing different key lengths + +key length=8 +string(32) "736563726574206d6573736167650000" + +key length=20 +string(32) "736563726574206d6573736167650000" + +key length=24 +string(32) "736563726574206d6573736167650000" + +key length=26 + +Warning: mcrypt_ecb(): Size of key is too large for this algorithm in %s on line %d +string(32) "736563726574206d6573736167650000" + +--- testing different iv lengths + +iv length=4 +string(32) "736563726574206d6573736167650000" + +iv length=8 +string(32) "736563726574206d6573736167650000" + +iv length=9 +string(32) "736563726574206d6573736167650000" +===DONE===
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_ecb_3des_encrypt.phpt b/ext/mcrypt/tests/mcrypt_ecb_3des_encrypt.phpt new file mode 100644 index 0000000..da26b4b --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ecb_3des_encrypt.phpt @@ -0,0 +1,82 @@ +--TEST-- +Test mcrypt_ecb() function : basic functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_ecb(int cipher, string key, string data, int mode, string iv) + * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_ecb() : basic functionality ***\n"; + + +$cipher = MCRYPT_TRIPLEDES; +$data = b"This is the secret message which must be encrypted"; +$mode = MCRYPT_ENCRYPT; + +// tripledes uses keys upto 192 bits (24 bytes) +$keys = array( + b'12345678', + b'12345678901234567890', + b'123456789012345678901234', + b'12345678901234567890123456' +); +// tripledes is a block cipher of 64 bits (8 bytes) +$ivs = array( + b'1234', + b'12345678', + b'123456789' +); + +$iv = b'12345678'; +echo "\n--- testing different key lengths\n"; +foreach ($keys as $key) { + echo "\nkey length=".strlen($key)."\n"; + var_dump(bin2hex(mcrypt_ecb($cipher, $key, $data, $mode, $iv))); +} + +$key = b'1234567890123456'; +echo "\n--- testing different iv lengths\n"; +foreach ($ivs as $iv) { + echo "\niv length=".strlen($iv)."\n"; + var_dump(bin2hex(mcrypt_ecb($cipher, $key, $data, $mode, $iv))); +} +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_ecb() : basic functionality *** + +--- testing different key lengths + +key length=8 +string(112) "05c9c4cafb9937d950bdae60ee3abcb8d9f3e1f1fac8acaaa5b11d70b7ca02f3b76d447ab3dd85a4b5df5dedb4b4654595ccdf6da97fa93f" + +key length=20 +string(112) "0fc7045c4fb4dbcf44baf9ed15ab40331a42ff0632318a16b12ed5873f02e7945e4f63f408d6849534cbb7419c22c8854aaa85e0e05a28e6" + +key length=24 +string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27" + +key length=26 + +Warning: mcrypt_ecb(): Size of key is too large for this algorithm in %s on line %d +string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27" + +--- testing different iv lengths + +iv length=4 +string(112) "440a6f54601969b15e81df09cd381ef585fede5f3620587fd1a949c520aed9f6d10ebbabf2cea3e1f04c9251c2878c0ca37d51c80d490165" + +iv length=8 +string(112) "440a6f54601969b15e81df09cd381ef585fede5f3620587fd1a949c520aed9f6d10ebbabf2cea3e1f04c9251c2878c0ca37d51c80d490165" + +iv length=9 +string(112) "440a6f54601969b15e81df09cd381ef585fede5f3620587fd1a949c520aed9f6d10ebbabf2cea3e1f04c9251c2878c0ca37d51c80d490165" +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_ecb_error.phpt b/ext/mcrypt/tests/mcrypt_ecb_error.phpt new file mode 100644 index 0000000..b33034a --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ecb_error.phpt @@ -0,0 +1,52 @@ +--TEST-- +Test mcrypt_ecb() function : error conditions +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_ecb(int cipher, string key, string data, int mode, string iv) + * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_ecb() : error conditions ***\n"; + + +//Test mcrypt_ecb with one more than the expected number of arguments +echo "\n-- Testing mcrypt_ecb() function with more than expected no. of arguments --\n"; +$cipher = 10; +$key = 'string_val'; +$data = 'string_val'; +$mode = 10; +$iv = 'string_val'; +$extra_arg = 10; +var_dump( mcrypt_ecb($cipher, $key, $data, $mode, $iv, $extra_arg) ); + +// Testing mcrypt_ecb with one less than the expected number of arguments +echo "\n-- Testing mcrypt_ecb() function with less than expected no. of arguments --\n"; +$cipher = 10; +$key = 'string_val'; +$data = 'string_val'; +var_dump( mcrypt_ecb($cipher, $key, $data) ); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_ecb() : error conditions *** + +-- Testing mcrypt_ecb() function with more than expected no. of arguments -- + +Warning: mcrypt_ecb() expects at most 5 parameters, 6 given in %s on line %d +NULL + +-- Testing mcrypt_ecb() function with less than expected no. of arguments -- + +Warning: mcrypt_ecb() expects at least 4 parameters, 3 given in %s on line %d +NULL +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_ecb_variation1.phpt b/ext/mcrypt/tests/mcrypt_ecb_variation1.phpt new file mode 100644 index 0000000..282a0cc --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ecb_variation1.phpt @@ -0,0 +1,230 @@ +--TEST-- +Test mcrypt_ecb() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_ecb(string cipher, string key, string data, int mode, string iv) + * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_ecb() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$key = b'string_val'; +$data = b'string_val'; +$mode = MCRYPT_ENCRYPT; +$iv = b'string_val'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return "Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = <<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for cipher + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( mcrypt_ecb($value, $key, $data, $mode, $iv) ); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_ecb() : usage variation *** + +--int 0-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--int 1-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--int 12345-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--int -12345-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--float 10.5-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--float -10.5-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--float 12.3456789000e10-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--float -12.3456789000e10-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--float .5-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--empty array-- +Error: 2 - mcrypt_ecb() expects parameter 1 to be string, array given, %s(%d) +NULL + +--int indexed array-- +Error: 2 - mcrypt_ecb() expects parameter 1 to be string, array given, %s(%d) +NULL + +--associative array-- +Error: 2 - mcrypt_ecb() expects parameter 1 to be string, array given, %s(%d) +NULL + +--nested arrays-- +Error: 2 - mcrypt_ecb() expects parameter 1 to be string, array given, %s(%d) +NULL + +--uppercase NULL-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--lowercase null-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--lowercase true-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--lowercase false-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--uppercase TRUE-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--uppercase FALSE-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--empty string DQ-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--empty string SQ-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithToString-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithoutToString-- +Error: 2 - mcrypt_ecb() expects parameter 1 to be string, object given, %s(%d) +NULL + +--undefined var-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--unset var-- +Error: 2 - mcrypt_ecb(): Module initialization failed, %s(%d) +bool(false) + +--resource-- +Error: 2 - mcrypt_ecb() expects parameter 1 to be string, resource given, %s(%d) +NULL +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_ecb_variation2.phpt b/ext/mcrypt/tests/mcrypt_ecb_variation2.phpt new file mode 100644 index 0000000..49e0a55 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ecb_variation2.phpt @@ -0,0 +1,210 @@ +--TEST-- +Test mcrypt_ecb() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_ecb(string cipher, string key, string data, int mode, string iv) + * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_ecb() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$data = b'string_val'; +$mode = MCRYPT_ENCRYPT; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for key + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump(bin2hex(mcrypt_ecb($cipher, $value, $data, $mode, $iv))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_ecb() : usage variation *** + +--int 0-- +string(32) "e469e6b066f9600e1eefd8f53365f96c" + +--int 1-- +string(32) "e469e6b066f9600e1eefd8f53365f96c" + +--int 12345-- +string(32) "d74e5f51d1199bcfa61f80168e913007" + +--int -12345-- +string(32) "17fe485ed735abb34c1dd4455af7b79c" + +--float 10.5-- +string(32) "cd735509aa4013a130e011686d66ae01" + +--float -10.5-- +string(32) "a57d99d6d5813039abf50fc50d631e47" + +--float 12.3456789000e10-- +string(32) "f17ede0bfdaa4408f545f7f4c8b040d2" + +--float -12.3456789000e10-- +string(32) "326f64e3b9bd5a6beb0a9b52a09a5a48" + +--float .5-- +string(32) "2aedf7661cd4d8c7593f44c58718e2b8" + +--empty array-- +Error: 2 - mcrypt_ecb() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_ecb() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_ecb() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_ecb() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--lowercase null-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--lowercase true-- +string(32) "e469e6b066f9600e1eefd8f53365f96c" + +--lowercase false-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--uppercase TRUE-- +string(32) "e469e6b066f9600e1eefd8f53365f96c" + +--uppercase FALSE-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--empty string DQ-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--empty string SQ-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--instance of classWithToString-- +string(32) "1fd3514d8ced44d04d9dc7511fce33ef" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_ecb() expects parameter 2 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--unset var-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--resource-- +Error: 2 - mcrypt_ecb() expects parameter 2 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_ecb_variation3.phpt b/ext/mcrypt/tests/mcrypt_ecb_variation3.phpt new file mode 100644 index 0000000..cedda49 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ecb_variation3.phpt @@ -0,0 +1,210 @@ +--TEST-- +Test mcrypt_ecb() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_ecb(string cipher, string key, string data, int mode, string iv) + * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_ecb() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$mode = MCRYPT_ENCRYPT; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for data + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump(bin2hex(mcrypt_ecb($cipher, $key, $value, $mode, $iv))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_ecb() : usage variation *** + +--int 0-- +string(16) "51dc9cd9179b718b" + +--int 1-- +string(16) "619c335f8c4f9cbf" + +--int 12345-- +string(16) "b1258d67ab73de00" + +--int -12345-- +string(16) "8eecf134443bd6b9" + +--float 10.5-- +string(16) "34b5750a793baff5" + +--float -10.5-- +string(16) "7a605f2aacc8a11d" + +--float 12.3456789000e10-- +string(32) "74a0d7026ae586f476d4b17808851e86" + +--float -12.3456789000e10-- +string(32) "bfb155997017986c01090afebd62c7ca" + +--float .5-- +string(16) "cc60ac201164b6c7" + +--empty array-- +Error: 2 - mcrypt_ecb() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_ecb() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_ecb() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_ecb() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +string(16) "6ece228c41457539" + +--lowercase null-- +string(16) "6ece228c41457539" + +--lowercase true-- +string(16) "619c335f8c4f9cbf" + +--lowercase false-- +string(16) "6ece228c41457539" + +--uppercase TRUE-- +string(16) "619c335f8c4f9cbf" + +--uppercase FALSE-- +string(16) "6ece228c41457539" + +--empty string DQ-- +string(16) "6ece228c41457539" + +--empty string SQ-- +string(16) "6ece228c41457539" + +--instance of classWithToString-- +string(32) "749c3b4d16731d98370128754b7c930f" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_ecb() expects parameter 3 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +string(16) "6ece228c41457539" + +--unset var-- +string(16) "6ece228c41457539" + +--resource-- +Error: 2 - mcrypt_ecb() expects parameter 3 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_ecb_variation4.phpt b/ext/mcrypt/tests/mcrypt_ecb_variation4.phpt new file mode 100644 index 0000000..599b4fb --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ecb_variation4.phpt @@ -0,0 +1,205 @@ +--TEST-- +Test mcrypt_ecb() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_ecb(string cipher, string key, string data, int mode, string iv) + * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_ecb() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$data = b'string_val'; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return "Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = <<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // string data + 'string DQ' => "string", + 'string SQ' => 'string', + 'mixed case string' => "sTrInG", + 'heredoc' => $heredoc, + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for mode + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump(bin2hex(mcrypt_ecb($cipher, $key, $data, $value, $iv))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_ecb() : usage variation *** + +--float 10.5-- +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--float -10.5-- +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--float 12.3456789000e10-- +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--float -12.3456789000e10-- +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--float .5-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--empty array-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--int indexed array-- +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--associative array-- +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--nested arrays-- +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--uppercase NULL-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--lowercase null-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--lowercase true-- +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--lowercase false-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--uppercase TRUE-- +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--uppercase FALSE-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--empty string DQ-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--empty string SQ-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--string DQ-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--string SQ-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--mixed case string-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--heredoc-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--instance of classWithToString-- +Error: 8 - Object of class classWithToString could not be converted to int, %s(%d) +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--instance of classWithoutToString-- +Error: 8 - Object of class classWithoutToString could not be converted to int, %s(%d) +string(32) "a80c6cef6b42c8759143586a57bb7dc6" + +--undefined var-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--unset var-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--resource-- +string(%d) %s +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_ecb_variation5.phpt b/ext/mcrypt/tests/mcrypt_ecb_variation5.phpt new file mode 100644 index 0000000..f64744f --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ecb_variation5.phpt @@ -0,0 +1,210 @@ +--TEST-- +Test mcrypt_ecb() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_ecb(string cipher, string key, string data, int mode, string iv) + * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_ecb() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$data = b'string_val'; +$mode = MCRYPT_ENCRYPT; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for iv + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump(bin2hex( mcrypt_ecb($cipher, $key, $data, $mode, $value))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_ecb() : usage variation *** + +--int 0-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--int 1-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--int 12345-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--int -12345-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--float 10.5-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--float -10.5-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--float 12.3456789000e10-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--float -12.3456789000e10-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--float .5-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--empty array-- +Error: 2 - mcrypt_ecb() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_ecb() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_ecb() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_ecb() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--lowercase null-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--lowercase true-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--lowercase false-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--uppercase TRUE-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--uppercase FALSE-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--empty string DQ-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--empty string SQ-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--instance of classWithToString-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_ecb() expects parameter 5 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--unset var-- +string(32) "6438db90653c4d300909aa02fd6163c2" + +--resource-- +Error: 2 - mcrypt_ecb() expects parameter 5 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_enc_get_algorithms_name.phpt b/ext/mcrypt/tests/mcrypt_enc_get_algorithms_name.phpt new file mode 100644 index 0000000..5b0bb01 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_get_algorithms_name.phpt @@ -0,0 +1,22 @@ +--TEST-- +mcrypt_enc_get_algorithms_name +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open('rijndael-128', '', MCRYPT_MODE_ECB, ''); +echo mcrypt_enc_get_algorithms_name($td) . "\n"; +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); +echo mcrypt_enc_get_algorithms_name($td) . "\n"; +$td = mcrypt_module_open(MCRYPT_RC2, '', MCRYPT_MODE_CBC, ''); +echo mcrypt_enc_get_algorithms_name($td) . "\n"; +$td = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, ''); +echo mcrypt_enc_get_algorithms_name($td) . "\n"; +$td = mcrypt_module_open('des', '', 'ecb', ''); +echo mcrypt_enc_get_algorithms_name($td) . "\n"; +--EXPECT-- +Rijndael-128 +Rijndael-128 +RC2 +Blowfish +DES
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_enc_get_block_size.phpt b/ext/mcrypt/tests/mcrypt_enc_get_block_size.phpt new file mode 100644 index 0000000..06f5add --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_get_block_size.phpt @@ -0,0 +1,16 @@ +--TEST-- +mcrypt_enc_get_block_size +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_get_block_size($td)); +$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_get_block_size($td)); +$td = mcrypt_module_open(MCRYPT_WAKE, '', MCRYPT_MODE_STREAM, ''); +var_dump(mcrypt_enc_get_block_size($td)); +--EXPECT-- +int(32) +int(8) +int(1) diff --git a/ext/mcrypt/tests/mcrypt_enc_get_iv_size.phpt b/ext/mcrypt/tests/mcrypt_enc_get_iv_size.phpt new file mode 100644 index 0000000..b4f9d6e --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_get_iv_size.phpt @@ -0,0 +1,16 @@ +--TEST-- +mcrypt_enc_get_iv_size +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_get_iv_size($td)); +$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_get_iv_size($td)); +$td = mcrypt_module_open(MCRYPT_WAKE, '', MCRYPT_MODE_STREAM, ''); +var_dump(mcrypt_enc_get_iv_size($td)); +--EXPECT-- +int(32) +int(8) +int(0) diff --git a/ext/mcrypt/tests/mcrypt_enc_get_key_size.phpt b/ext/mcrypt/tests/mcrypt_enc_get_key_size.phpt new file mode 100644 index 0000000..22328e1 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_get_key_size.phpt @@ -0,0 +1,16 @@ +--TEST-- +mcrypt_enc_get_key_size +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_get_key_size($td)); +$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_get_key_size($td)); +$td = mcrypt_module_open(MCRYPT_WAKE, '', MCRYPT_MODE_STREAM, ''); +var_dump(mcrypt_enc_get_key_size($td)); +--EXPECT-- +int(32) +int(24) +int(32) diff --git a/ext/mcrypt/tests/mcrypt_enc_get_mode_name.phpt b/ext/mcrypt/tests/mcrypt_enc_get_mode_name.phpt new file mode 100644 index 0000000..21c41f9 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_get_mode_name.phpt @@ -0,0 +1,25 @@ +--TEST-- +mcrypt_enc_get_modes_name +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open('rijndael-128', '', MCRYPT_MODE_ECB, ''); +echo mcrypt_enc_get_modes_name($td) . "\n"; +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); +echo mcrypt_enc_get_modes_name($td) . "\n"; +$td = mcrypt_module_open(MCRYPT_WAKE, '', MCRYPT_MODE_STREAM, ''); +echo mcrypt_enc_get_modes_name($td) . "\n"; +$td = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_OFB, ''); +echo mcrypt_enc_get_modes_name($td) . "\n"; +$td = mcrypt_module_open('des', '', 'ecb', ''); +echo mcrypt_enc_get_modes_name($td) . "\n"; +$td = mcrypt_module_open('des', '', 'cbc', ''); +echo mcrypt_enc_get_modes_name($td) . "\n"; +--EXPECT-- +ECB +CBC +STREAM +OFB +ECB +CBC diff --git a/ext/mcrypt/tests/mcrypt_enc_get_supported_key_sizes.phpt b/ext/mcrypt/tests/mcrypt_enc_get_supported_key_sizes.phpt new file mode 100644 index 0000000..0ce1f15 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_get_supported_key_sizes.phpt @@ -0,0 +1,18 @@ +--TEST-- +mcrypt_enc_get_supported_key_sizes +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open('rijndael-256', '', 'ecb', ''); +$var = mcrypt_enc_get_supported_key_sizes($td); +var_dump($var); +--EXPECT-- +array(3) { + [0]=> + int(16) + [1]=> + int(24) + [2]=> + int(32) +}
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_enc_is_block_algorithm.phpt b/ext/mcrypt/tests/mcrypt_enc_is_block_algorithm.phpt new file mode 100644 index 0000000..21a0ed2 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_is_block_algorithm.phpt @@ -0,0 +1,16 @@ +--TEST-- +mcrypt_enc_is_block_algorithm +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_is_block_algorithm($td)); +$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_is_block_algorithm($td)); +$td = mcrypt_module_open(MCRYPT_WAKE, '', MCRYPT_MODE_STREAM, ''); +var_dump(mcrypt_enc_is_block_algorithm($td)); +--EXPECT-- +bool(true) +bool(true) +bool(false) diff --git a/ext/mcrypt/tests/mcrypt_enc_is_block_algorithm_mode.phpt b/ext/mcrypt/tests/mcrypt_enc_is_block_algorithm_mode.phpt new file mode 100644 index 0000000..69c9654 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_is_block_algorithm_mode.phpt @@ -0,0 +1,16 @@ +--TEST-- +mcrypt_enc_is_block_algorithm_mode +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_ECB, ''); +var_dump(mcrypt_enc_is_block_algorithm_mode($td)); +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_is_block_algorithm_mode($td)); +$td = mcrypt_module_open(MCRYPT_WAKE, '', MCRYPT_MODE_STREAM, ''); +var_dump(mcrypt_enc_is_block_algorithm_mode($td)); +--EXPECT-- +bool(true) +bool(true) +bool(false) diff --git a/ext/mcrypt/tests/mcrypt_enc_is_block_mode.phpt b/ext/mcrypt/tests/mcrypt_enc_is_block_mode.phpt new file mode 100644 index 0000000..551f7a6 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_is_block_mode.phpt @@ -0,0 +1,19 @@ +--TEST-- +mcrypt_enc_is_block_mode +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_is_block_mode($td)); +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_ECB, ''); +var_dump(mcrypt_enc_is_block_mode($td)); +$td = mcrypt_module_open(MCRYPT_ARCFOUR, '', MCRYPT_MODE_STREAM, ''); +var_dump(mcrypt_enc_is_block_mode($td)); +$td = mcrypt_module_open(MCRYPT_WAKE, '', MCRYPT_MODE_STREAM, ''); +var_dump(mcrypt_enc_is_block_mode($td)); +--EXPECT-- +bool(true) +bool(true) +bool(false) +bool(false)
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_enc_self_test.phpt b/ext/mcrypt/tests/mcrypt_enc_self_test.phpt new file mode 100644 index 0000000..a161e0e --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_enc_self_test.phpt @@ -0,0 +1,10 @@ +--TEST-- +mcrypt_enc_self_test +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); +var_dump(mcrypt_enc_self_test($td)); +--EXPECT-- +int(0)
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_encrypt_3des_cbc.phpt b/ext/mcrypt/tests/mcrypt_encrypt_3des_cbc.phpt new file mode 100644 index 0000000..f86bd39 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_encrypt_3des_cbc.phpt @@ -0,0 +1,94 @@ +--TEST-- +Test mcrypt_encrypt() function : TripleDES functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_encrypt() : TripleDES functionality ***\n"; + +//test CBC, ECB modes +//test encrypt decrypt +//test tripledes, aes +//test different lengths of key, iv +//test no iv being passed on CBC, ECB +//test upto 32 bytes with unlimited strength + +$cipher = MCRYPT_TRIPLEDES; +$mode = MCRYPT_MODE_CBC; +$data = b'This is the secret message which must be encrypted'; + +// tripledes uses keys upto 192 bits (24 bytes) +$keys = array( + b'12345678', + b'12345678901234567890', + b'123456789012345678901234', + b'12345678901234567890123456' +); +// tripledes is a block cipher of 64 bits (8 bytes) +$ivs = array( + b'1234', + b'12345678', + b'123456789' +); + + +$iv = b'12345678'; +echo "\n--- testing different key lengths\n"; +foreach ($keys as $key) { + echo "\nkey length=".strlen($key)."\n"; + var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $iv))); +} + +$key = b'1234567890123456'; +echo "\n--- testing different iv lengths\n"; +foreach ($ivs as $iv) { + echo "\niv length=".strlen($iv)."\n"; + var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $iv))); +} + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_encrypt() : TripleDES functionality *** + +--- testing different key lengths + +key length=8 +string(112) "082b437d039d09418e20dc9de1dafa7ed6da5c6335b78950968441da1faf40c1f886e04da8ca177b80b376811e138c1bf51cb48dae2e7939" + +key length=20 +string(112) "0627351e0f8a082bf7981ae2c700a43fd3d44b270ac67b00fded1c5796eea935be0fef2a23da0b3f5e243929e62ac957bf0bf463aa90fc4f" + +key length=24 +string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac" + +key length=26 + +Warning: mcrypt_encrypt(): Size of key is too large for this algorithm in %s on line %d +string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac" + +--- testing different iv lengths + +iv length=4 + +Warning: mcrypt_encrypt(): The IV parameter must be as long as the blocksize in %s on line %d +string(112) "440a6f54601969b127aad3c217ce7583c7f7b29989693130645569301db0020b29a34a3dcd104b2d0e3ba19d6cbd8a33d352b9c27cc34ef1" + +iv length=8 +string(112) "bac347506bf092c5557c4363c301745d78f047028e2953e84fd66b30aeb6005812dadbe8baa871b83278341599b0c448ddaaa52b5a378ce5" + +iv length=9 + +Warning: mcrypt_encrypt(): The IV parameter must be as long as the blocksize in %s on line %d +string(112) "440a6f54601969b127aad3c217ce7583c7f7b29989693130645569301db0020b29a34a3dcd104b2d0e3ba19d6cbd8a33d352b9c27cc34ef1" +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_encrypt_3des_ecb.phpt b/ext/mcrypt/tests/mcrypt_encrypt_3des_ecb.phpt new file mode 100644 index 0000000..1b3188f --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_encrypt_3des_ecb.phpt @@ -0,0 +1,82 @@ +--TEST-- +Test mcrypt_encrypt() function : TripleDES functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_encrypt() : TripleDES functionality ***\n"; + +$cipher = MCRYPT_TRIPLEDES; +$mode = MCRYPT_MODE_ECB; +$data = b'This is the secret message which must be encrypted'; + +// tripledes uses keys upto 192 bits (24 bytes) +$keys = array( + b'12345678', + b'12345678901234567890', + b'123456789012345678901234', + b'12345678901234567890123456' +); + +echo "\n--- testing different key lengths\n"; +foreach ($keys as $key) { + echo "\nkey length=".strlen($key)."\n"; + var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode))); +} + +$key = b'12345678'; +$ivs = array( + b'1234', + b'12345678', + b'123456789' +); + +// ivs should be ignored in ecb mode +echo "\n--- testing different iv lengths\n"; +foreach ($ivs as $iv) { + echo "\niv length=".strlen($iv)."\n"; + var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $iv))); +} + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_encrypt() : TripleDES functionality *** + +--- testing different key lengths + +key length=8 +string(112) "05c9c4cafb9937d950bdae60ee3abcb8d9f3e1f1fac8acaaa5b11d70b7ca02f3b76d447ab3dd85a4b5df5dedb4b4654595ccdf6da97fa93f" + +key length=20 +string(112) "0fc7045c4fb4dbcf44baf9ed15ab40331a42ff0632318a16b12ed5873f02e7945e4f63f408d6849534cbb7419c22c8854aaa85e0e05a28e6" + +key length=24 +string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27" + +key length=26 + +Warning: mcrypt_encrypt(): Size of key is too large for this algorithm in %s on line %d +string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27" + +--- testing different iv lengths + +iv length=4 +string(112) "05c9c4cafb9937d950bdae60ee3abcb8d9f3e1f1fac8acaaa5b11d70b7ca02f3b76d447ab3dd85a4b5df5dedb4b4654595ccdf6da97fa93f" + +iv length=8 +string(112) "05c9c4cafb9937d950bdae60ee3abcb8d9f3e1f1fac8acaaa5b11d70b7ca02f3b76d447ab3dd85a4b5df5dedb4b4654595ccdf6da97fa93f" + +iv length=9 +string(112) "05c9c4cafb9937d950bdae60ee3abcb8d9f3e1f1fac8acaaa5b11d70b7ca02f3b76d447ab3dd85a4b5df5dedb4b4654595ccdf6da97fa93f" +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_encrypt_error.phpt b/ext/mcrypt/tests/mcrypt_encrypt_error.phpt new file mode 100644 index 0000000..04226c2 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_encrypt_error.phpt @@ -0,0 +1,52 @@ +--TEST-- +Test mcrypt_encrypt() function : error conditions +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_encrypt() : error conditions ***\n"; + + +//Test mcrypt_encrypt with one more than the expected number of arguments +echo "\n-- Testing mcrypt_encrypt() function with more than expected no. of arguments --\n"; +$cipher = 'string_val'; +$key = 'string_val'; +$data = 'string_val'; +$mode = 'string_val'; +$iv = 'string_val'; +$extra_arg = 10; +var_dump( mcrypt_encrypt($cipher, $key, $data, $mode, $iv, $extra_arg) ); + +// Testing mcrypt_encrypt with one less than the expected number of arguments +echo "\n-- Testing mcrypt_encrypt() function with less than expected no. of arguments --\n"; +$cipher = 'string_val'; +$key = 'string_val'; +$data = 'string_val'; +var_dump( mcrypt_encrypt($cipher, $key, $data) ); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_encrypt() : error conditions *** + +-- Testing mcrypt_encrypt() function with more than expected no. of arguments -- + +Warning: mcrypt_encrypt() expects at most 5 parameters, 6 given in %s on line %d +NULL + +-- Testing mcrypt_encrypt() function with less than expected no. of arguments -- + +Warning: mcrypt_encrypt() expects at least 4 parameters, 3 given in %s on line %d +NULL +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_encrypt_variation1.phpt b/ext/mcrypt/tests/mcrypt_encrypt_variation1.phpt new file mode 100644 index 0000000..fdbd901 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_encrypt_variation1.phpt @@ -0,0 +1,230 @@ +--TEST-- +Test mcrypt_encrypt() function : usage variation - different types for cipher +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_encrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$key = b'string_val'; +$data = b'string_val'; +$mode = MCRYPT_MODE_ECB; +$iv = b'string_val'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return "Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = <<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for cipher + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( mcrypt_encrypt($value, $key, $data, $mode, $iv) ); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_encrypt() : usage variation *** + +--int 0-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--int 1-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--int 12345-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--int -12345-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float 10.5-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float -10.5-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float 12.3456789000e10-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float -12.3456789000e10-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float .5-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty array-- +Error: 2 - mcrypt_encrypt() expects parameter 1 to be string, array given, %s(%d) +NULL + +--int indexed array-- +Error: 2 - mcrypt_encrypt() expects parameter 1 to be string, array given, %s(%d) +NULL + +--associative array-- +Error: 2 - mcrypt_encrypt() expects parameter 1 to be string, array given, %s(%d) +NULL + +--nested arrays-- +Error: 2 - mcrypt_encrypt() expects parameter 1 to be string, array given, %s(%d) +NULL + +--uppercase NULL-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase null-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase true-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase false-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase TRUE-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase FALSE-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty string DQ-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty string SQ-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithToString-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithoutToString-- +Error: 2 - mcrypt_encrypt() expects parameter 1 to be string, object given, %s(%d) +NULL + +--undefined var-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--unset var-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--resource-- +Error: 2 - mcrypt_encrypt() expects parameter 1 to be string, resource given, %s(%d) +NULL +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_encrypt_variation2.phpt b/ext/mcrypt/tests/mcrypt_encrypt_variation2.phpt new file mode 100644 index 0000000..0f4fd0f --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_encrypt_variation2.phpt @@ -0,0 +1,210 @@ +--TEST-- +Test mcrypt_encrypt() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_encrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$data = b'string_val'; +$mode = MCRYPT_MODE_ECB; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for key + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( bin2hex(mcrypt_encrypt($cipher, $value, $data, $mode, $iv) )); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_encrypt() : usage variation *** + +--int 0-- +string(32) "e469e6b066f9600e1eefd8f53365f96c" + +--int 1-- +string(32) "e469e6b066f9600e1eefd8f53365f96c" + +--int 12345-- +string(32) "d74e5f51d1199bcfa61f80168e913007" + +--int -12345-- +string(32) "17fe485ed735abb34c1dd4455af7b79c" + +--float 10.5-- +string(32) "cd735509aa4013a130e011686d66ae01" + +--float -10.5-- +string(32) "a57d99d6d5813039abf50fc50d631e47" + +--float 12.3456789000e10-- +string(32) "f17ede0bfdaa4408f545f7f4c8b040d2" + +--float -12.3456789000e10-- +string(32) "326f64e3b9bd5a6beb0a9b52a09a5a48" + +--float .5-- +string(32) "2aedf7661cd4d8c7593f44c58718e2b8" + +--empty array-- +Error: 2 - mcrypt_encrypt() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_encrypt() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_encrypt() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_encrypt() expects parameter 2 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--lowercase null-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--lowercase true-- +string(32) "e469e6b066f9600e1eefd8f53365f96c" + +--lowercase false-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--uppercase TRUE-- +string(32) "e469e6b066f9600e1eefd8f53365f96c" + +--uppercase FALSE-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--empty string DQ-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--empty string SQ-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--instance of classWithToString-- +string(32) "1fd3514d8ced44d04d9dc7511fce33ef" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_encrypt() expects parameter 2 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--unset var-- +string(32) "bfa2cb7240c8d2f6abeb34960c04f6d3" + +--resource-- +Error: 2 - mcrypt_encrypt() expects parameter 2 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_encrypt_variation3.phpt b/ext/mcrypt/tests/mcrypt_encrypt_variation3.phpt new file mode 100644 index 0000000..c472540 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_encrypt_variation3.phpt @@ -0,0 +1,210 @@ +--TEST-- +Test mcrypt_encrypt() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_encrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$mode = MCRYPT_MODE_ECB; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for data + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( bin2hex(mcrypt_encrypt($cipher, $key, $value, $mode, $iv) )); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_encrypt() : usage variation *** + +--int 0-- +string(16) "51dc9cd9179b718b" + +--int 1-- +string(16) "619c335f8c4f9cbf" + +--int 12345-- +string(16) "b1258d67ab73de00" + +--int -12345-- +string(16) "8eecf134443bd6b9" + +--float 10.5-- +string(16) "34b5750a793baff5" + +--float -10.5-- +string(16) "7a605f2aacc8a11d" + +--float 12.3456789000e10-- +string(32) "74a0d7026ae586f476d4b17808851e86" + +--float -12.3456789000e10-- +string(32) "bfb155997017986c01090afebd62c7ca" + +--float .5-- +string(16) "cc60ac201164b6c7" + +--empty array-- +Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +string(16) "6ece228c41457539" + +--lowercase null-- +string(16) "6ece228c41457539" + +--lowercase true-- +string(16) "619c335f8c4f9cbf" + +--lowercase false-- +string(16) "6ece228c41457539" + +--uppercase TRUE-- +string(16) "619c335f8c4f9cbf" + +--uppercase FALSE-- +string(16) "6ece228c41457539" + +--empty string DQ-- +string(16) "6ece228c41457539" + +--empty string SQ-- +string(16) "6ece228c41457539" + +--instance of classWithToString-- +string(32) "749c3b4d16731d98370128754b7c930f" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +string(16) "6ece228c41457539" + +--unset var-- +string(16) "6ece228c41457539" + +--resource-- +Error: 2 - mcrypt_encrypt() expects parameter 3 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_encrypt_variation4.phpt b/ext/mcrypt/tests/mcrypt_encrypt_variation4.phpt new file mode 100644 index 0000000..4c8cef1 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_encrypt_variation4.phpt @@ -0,0 +1,235 @@ +--TEST-- +Test mcrypt_encrypt() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_encrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$data = b'string_val'; +$iv = b'01234567'; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return "Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = <<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for mode + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( mcrypt_encrypt($cipher, $key, $data, $value, $iv) ); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_encrypt() : usage variation *** + +--int 0-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--int 1-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--int 12345-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--int -12345-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float 10.5-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float -10.5-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float 12.3456789000e10-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float -12.3456789000e10-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--float .5-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty array-- +Error: 8 - Array to string conversion, %s(%d) +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--int indexed array-- +Error: 8 - Array to string conversion, %s(%d) +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--associative array-- +Error: 8 - Array to string conversion, %s(%d) +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--nested arrays-- +Error: 8 - Array to string conversion, %s(%d) +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase NULL-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase null-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase true-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--lowercase false-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase TRUE-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--uppercase FALSE-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty string DQ-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--empty string SQ-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithToString-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--instance of classWithoutToString-- +Error: 4096 - Object of class classWithoutToString could not be converted to string, %s(%d) +Error: 8 - Object of class classWithoutToString to string conversion, %s(%d) +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--undefined var-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--unset var-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) + +--resource-- +Error: 2 - mcrypt_encrypt(): Module initialization failed, %s(%d) +bool(false) +===DONE=== diff --git a/ext/mcrypt/tests/mcrypt_encrypt_variation5.phpt b/ext/mcrypt/tests/mcrypt_encrypt_variation5.phpt new file mode 100644 index 0000000..ecadb76 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_encrypt_variation5.phpt @@ -0,0 +1,231 @@ +--TEST-- +Test mcrypt_encrypt() function : usage variation +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt_encrypt() : usage variation ***\n"; + +// Define error handler +function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { + if (error_reporting() != 0) { + // report non-silenced errors + echo "Error: $err_no - $err_msg, $filename($linenum)\n"; + } +} +set_error_handler('test_error_handler'); + +// Initialise function arguments not being substituted (if any) +$cipher = MCRYPT_TRIPLEDES; +$key = b'string_val'; +$data = b'string_val'; +//in php, it incorrectly reports problems with iv in ECB mode. +$mode = MCRYPT_MODE_CBC; + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +// define some classes +class classWithToString +{ + public function __toString() { + return b"Class A object"; + } +} + +class classWithoutToString +{ +} + +// heredoc string +$heredoc = b<<<EOT +hello world +EOT; + +// get a resource variable +$fp = fopen(__FILE__, "r"); + +// add arrays +$index_array = array (1, 2, 3); +$assoc_array = array ('one' => 1, 'two' => 2); + +//array of values to iterate over +$inputs = array( + + // int data + 'int 0' => 0, + 'int 1' => 1, + 'int 12345' => 12345, + 'int -12345' => -2345, + + // float data + 'float 10.5' => 10.5, + 'float -10.5' => -10.5, + 'float 12.3456789000e10' => 12.3456789000e10, + 'float -12.3456789000e10' => -12.3456789000e10, + 'float .5' => .5, + + // array data + 'empty array' => array(), + 'int indexed array' => $index_array, + 'associative array' => $assoc_array, + 'nested arrays' => array('foo', $index_array, $assoc_array), + + // null data + 'uppercase NULL' => NULL, + 'lowercase null' => null, + + // boolean data + 'lowercase true' => true, + 'lowercase false' =>false, + 'uppercase TRUE' =>TRUE, + 'uppercase FALSE' =>FALSE, + + // empty data + 'empty string DQ' => "", + 'empty string SQ' => '', + + // object data + 'instance of classWithToString' => new classWithToString(), + 'instance of classWithoutToString' => new classWithoutToString(), + + // undefined data + 'undefined var' => @$undefined_var, + + // unset data + 'unset var' => @$unset_var, + + // resource variable + 'resource' => $fp +); + +// loop through each element of the array for iv + +foreach($inputs as $valueType =>$value) { + echo "\n--$valueType--\n"; + var_dump( bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $value))); +}; + +fclose($fp); + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt_encrypt() : usage variation *** + +--int 0-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--int 1-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--int 12345-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--int -12345-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float 10.5-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float -10.5-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float 12.3456789000e10-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float -12.3456789000e10-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--float .5-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--empty array-- +Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--int indexed array-- +Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--associative array-- +Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--nested arrays-- +Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, array given, %s(%d) +string(0) "" + +--uppercase NULL-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--lowercase null-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--lowercase true-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--lowercase false-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--uppercase TRUE-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--uppercase FALSE-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--empty string DQ-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--empty string SQ-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--instance of classWithToString-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--instance of classWithoutToString-- +Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, object given, %s(%d) +string(0) "" + +--undefined var-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--unset var-- +Error: 2 - mcrypt_encrypt(): The IV parameter must be as long as the blocksize, %s(%d) +string(32) "6438db90653c4d3080c3ceab43618c05" + +--resource-- +Error: 2 - mcrypt_encrypt() expects parameter 5 to be string, resource given, %s(%d) +string(0) "" +===DONE=== + diff --git a/ext/mcrypt/tests/mcrypt_filters.phpt b/ext/mcrypt/tests/mcrypt_filters.phpt new file mode 100644 index 0000000..f528219 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_filters.phpt @@ -0,0 +1,41 @@ +--TEST-- +mcrypt filters +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +foreach (stream_get_filters() as $f) { + if ($f == "mcrypt.*" || $f == "mdecrypt.*") { + echo "FOUND\n"; + } +} + +$secretfile = 'secert-file.tmp'; +$passphrase = 'My secret'; + +$iv = substr(md5('iv'.$passphrase, true), 0, 8); +$key = substr(md5('pass1'.$passphrase, true) . + md5('pass2'.$passphrase, true), 0, 24); +$opts = array('iv'=>$iv, 'key'=>$key); + +$fp = fopen($secretfile, 'wb'); +stream_filter_append($fp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts); +fwrite($fp, 'Secret secret secret data'); +fclose($fp); + +echo md5_file($secretfile)."\n"; + +$fp = fopen($secretfile, 'rb'); +stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_READ, $opts); +$data = stream_get_contents($fp); +fclose($fp); + +echo $data."\n"; + +@unlink($secretfile); + +--EXPECTF-- +FOUND +FOUND +32e14bd3c31f2bd666e4290ebdb166a7 +Secret secret secret data
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_get_block_size.phpt b/ext/mcrypt/tests/mcrypt_get_block_size.phpt new file mode 100644 index 0000000..bf1f24d --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_get_block_size.phpt @@ -0,0 +1,13 @@ +--TEST-- +mcrypt_get_block_size +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)); +var_dump(mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC)); +var_dump(mcrypt_get_block_size(MCRYPT_WAKE, MCRYPT_MODE_STREAM)); +--EXPECT-- +int(32) +int(8) +int(1) diff --git a/ext/mcrypt/tests/mcrypt_get_cipher_name.phpt b/ext/mcrypt/tests/mcrypt_get_cipher_name.phpt new file mode 100644 index 0000000..9d4961a --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_get_cipher_name.phpt @@ -0,0 +1,15 @@ +--TEST-- +mcrypt_get_cipher_name +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +echo mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_256) . "\n"; +echo mcrypt_get_cipher_name(MCRYPT_RC2) . "\n"; +echo mcrypt_get_cipher_name(MCRYPT_ARCFOUR) . "\n"; +echo mcrypt_get_cipher_name(MCRYPT_WAKE) . "\n"; +--EXPECT-- +Rijndael-256 +RC2 +RC4 +WAKE diff --git a/ext/mcrypt/tests/mcrypt_get_iv_size.phpt b/ext/mcrypt/tests/mcrypt_get_iv_size.phpt new file mode 100644 index 0000000..ad35997 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_get_iv_size.phpt @@ -0,0 +1,17 @@ +--TEST-- +mcrypt_enc_get_iv_size +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)); +var_dump(mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC)); +var_dump(mcrypt_get_iv_size(MCRYPT_WAKE, MCRYPT_MODE_STREAM)); +var_dump(mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_STREAM)); +--EXPECTF-- +int(32) +int(8) +int(0) + +Warning: mcrypt_get_iv_size(): Module initialization failed in %s on line %d +bool(false)
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_get_key_size.phpt b/ext/mcrypt/tests/mcrypt_get_key_size.phpt new file mode 100644 index 0000000..930cf4d --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_get_key_size.phpt @@ -0,0 +1,13 @@ +--TEST-- +mcrypt_get_key_size +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)); +var_dump(mcrypt_get_key_size(MCRYPT_3DES, MCRYPT_MODE_CBC)); +var_dump(mcrypt_get_key_size(MCRYPT_WAKE, MCRYPT_MODE_STREAM)); +--EXPECT-- +int(32) +int(24) +int(32) diff --git a/ext/mcrypt/tests/mcrypt_list_algorithms.phpt b/ext/mcrypt/tests/mcrypt_list_algorithms.phpt new file mode 100644 index 0000000..9baf9a6 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_list_algorithms.phpt @@ -0,0 +1,16 @@ +--TEST-- +mcrypt_list_algorithms +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +foreach (mcrypt_list_algorithms() as $algo) { + if (in_array($algo, array('rijndael-256', 'des', 'blowfish', 'twofish'))) { + echo "FOUND\n"; + } +} +--EXPECT-- +FOUND +FOUND +FOUND +FOUND
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_list_modes.phpt b/ext/mcrypt/tests/mcrypt_list_modes.phpt new file mode 100644 index 0000000..0e5a6ae --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_list_modes.phpt @@ -0,0 +1,26 @@ +--TEST-- +mcrypt_list_modes +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_list_modes()); +--EXPECT-- +array(8) { + [0]=> + string(3) "cbc" + [1]=> + string(3) "cfb" + [2]=> + string(3) "ctr" + [3]=> + string(3) "ecb" + [4]=> + string(4) "ncfb" + [5]=> + string(4) "nofb" + [6]=> + string(3) "ofb" + [7]=> + string(6) "stream" +}
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_module_get_algo_block_size.phpt b/ext/mcrypt/tests/mcrypt_module_get_algo_block_size.phpt new file mode 100644 index 0000000..c89a44a --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_module_get_algo_block_size.phpt @@ -0,0 +1,19 @@ +--TEST-- +mcrypt_module_get_algo_block_size +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_module_get_algo_block_size(MCRYPT_RIJNDAEL_256)); +var_dump(mcrypt_module_get_algo_block_size(MCRYPT_RIJNDAEL_192)); +var_dump(mcrypt_module_get_algo_block_size(MCRYPT_RC2)); +var_dump(mcrypt_module_get_algo_block_size(MCRYPT_XTEA)); +var_dump(mcrypt_module_get_algo_block_size(MCRYPT_CAST_128)); +var_dump(mcrypt_module_get_algo_block_size(MCRYPT_BLOWFISH)); +--EXPECT-- +int(32) +int(24) +int(8) +int(8) +int(8) +int(8) diff --git a/ext/mcrypt/tests/mcrypt_module_get_algo_key_size.phpt b/ext/mcrypt/tests/mcrypt_module_get_algo_key_size.phpt new file mode 100644 index 0000000..7d3841f --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_module_get_algo_key_size.phpt @@ -0,0 +1,19 @@ +--TEST-- +mcrypt_module_get_algo_key_size +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_module_get_algo_key_size(MCRYPT_RIJNDAEL_256)); +var_dump(mcrypt_module_get_algo_key_size(MCRYPT_RIJNDAEL_192)); +var_dump(mcrypt_module_get_algo_key_size(MCRYPT_RC2)); +var_dump(mcrypt_module_get_algo_key_size(MCRYPT_XTEA)); +var_dump(mcrypt_module_get_algo_key_size(MCRYPT_CAST_128)); +var_dump(mcrypt_module_get_algo_key_size(MCRYPT_BLOWFISH)); +--EXPECT-- +int(32) +int(32) +int(128) +int(16) +int(16) +int(56) diff --git a/ext/mcrypt/tests/mcrypt_module_get_supported_key_sizes.phpt b/ext/mcrypt/tests/mcrypt_module_get_supported_key_sizes.phpt new file mode 100644 index 0000000..bf45bb4 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_module_get_supported_key_sizes.phpt @@ -0,0 +1,19 @@ +--TEST-- +mcrypt_module_get_supported_key_sizes +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_module_get_supported_key_sizes(MCRYPT_RIJNDAEL_256)); +var_dump(mcrypt_module_get_supported_key_sizes(MCRYPT_RC2)); +--EXPECT-- +array(3) { + [0]=> + int(16) + [1]=> + int(24) + [2]=> + int(32) +} +array(0) { +}
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_module_is_block_algorithm.phpt b/ext/mcrypt/tests/mcrypt_module_is_block_algorithm.phpt new file mode 100644 index 0000000..2cfc87d --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_module_is_block_algorithm.phpt @@ -0,0 +1,15 @@ +--TEST-- +mcrypt_module_is_block_algorithm +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_module_is_block_algorithm(MCRYPT_RIJNDAEL_128)); +var_dump(mcrypt_module_is_block_algorithm(MCRYPT_DES)); +var_dump(mcrypt_module_is_block_algorithm(MCRYPT_WAKE)); +var_dump(mcrypt_module_is_block_algorithm(MCRYPT_XTEA)); +--EXPECT-- +bool(true) +bool(true) +bool(false) +bool(true)
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_module_is_block_algorithm_mode.phpt b/ext/mcrypt/tests/mcrypt_module_is_block_algorithm_mode.phpt new file mode 100644 index 0000000..2924551 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_module_is_block_algorithm_mode.phpt @@ -0,0 +1,15 @@ +--TEST-- +mcrypt_module_is_block_algorithm_mode +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_module_is_block_algorithm_mode(MCRYPT_MODE_CBC)); +var_dump(mcrypt_module_is_block_algorithm_mode(MCRYPT_MODE_ECB)); +var_dump(mcrypt_module_is_block_algorithm_mode(MCRYPT_MODE_STREAM)); +var_dump(mcrypt_module_is_block_algorithm_mode(MCRYPT_MODE_OFB)); +--EXPECT-- +bool(true) +bool(true) +bool(false) +bool(true)
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_module_is_block_mode.phpt b/ext/mcrypt/tests/mcrypt_module_is_block_mode.phpt new file mode 100644 index 0000000..662279f --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_module_is_block_mode.phpt @@ -0,0 +1,17 @@ +--TEST-- +mcrypt_module_is_block_mode +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_CBC)); +var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_ECB)); +var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_STREAM)); +var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_NOFB)); +var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_OFB)); +--EXPECT-- +bool(true) +bool(true) +bool(false) +bool(false) +bool(false)
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_module_open.phpt b/ext/mcrypt/tests/mcrypt_module_open.phpt new file mode 100644 index 0000000..92f0d55 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_module_open.phpt @@ -0,0 +1,14 @@ +--TEST-- +mcrypt_module_open +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '')); +mcrypt_module_open('', '', '', ''); + +--EXPECTF-- +resource(%d) of type (mcrypt) + +Warning: mcrypt_module_open(): Could not open encryption module in %s on line %d + diff --git a/ext/mcrypt/tests/mcrypt_module_self_test.phpt b/ext/mcrypt/tests/mcrypt_module_self_test.phpt new file mode 100644 index 0000000..cf07bfe --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_module_self_test.phpt @@ -0,0 +1,13 @@ +--TEST-- +mcrypt_module_self_test +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +var_dump(mcrypt_module_self_test(MCRYPT_RIJNDAEL_128)); +var_dump(mcrypt_module_self_test(MCRYPT_RC2)); +var_dump(mcrypt_module_self_test('')); +--EXPECT-- +bool(true) +bool(true) +bool(false) diff --git a/ext/mcrypt/tests/mcrypt_ofb.phpt b/ext/mcrypt/tests/mcrypt_ofb.phpt new file mode 100644 index 0000000..1532c4c --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_ofb.phpt @@ -0,0 +1,21 @@ +--TEST-- +mcrypt_ofb +--SKIPIF-- +<?php if (!extension_loaded("mcrypt")) print "skip"; ?> +--FILE-- +<?php +$key = "FooBar"; +$secret = "PHP Testfest 2008"; +$cipher = MCRYPT_RIJNDAEL_128; + +$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher, MCRYPT_MODE_ECB), MCRYPT_RAND); +$enc_data = mcrypt_ofb($cipher, $key, $secret, MCRYPT_ENCRYPT, $iv); + +// we have to trim as AES rounds the blocks and decrypt doesnt detect that +echo trim(mcrypt_ofb($cipher, $key, $enc_data, MCRYPT_DECRYPT, $iv)) . "\n"; + +// a warning must be issued if we don't use a IV on a AES cipher, that usually requires an IV +mcrypt_ofb($cipher, $key, $enc_data, MCRYPT_DECRYPT, $iv); + +--EXPECT-- +PHP Testfest 2008
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_rijndael128_128BitKey.phpt b/ext/mcrypt/tests/mcrypt_rijndael128_128BitKey.phpt new file mode 100644 index 0000000..100352a --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_rijndael128_128BitKey.phpt @@ -0,0 +1,130 @@ +--TEST-- +Test mcrypt_encrypt() function : TripleDES functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + /* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + /* Prototype : string mcrypt_cbc(int cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt : Rijndael128 functionality ***\n"; + +$cipher = MCRYPT_RIJNDAEL_128; +$mode = MCRYPT_MODE_CBC; +$data = b'This is the secret message which must be encrypted'; + +// keys upto 128 bits (16 bytes) +$keys = array( + null, + '', + b'12345678', + b'1234567890123456' +); +// rijndael128 is a block cipher of 128 bits (16 bytes) +$ivs = array( + null, + '', + b'12345678', + b'1234567890123456', + b'12345678901234567' +); + + +$iv = b'1234567890123456'; +echo "\n--- testing different key lengths\n"; +foreach ($keys as $key) { + echo "\nkey length=".strlen($key)."\n"; + $res = mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv); + var_dump(bin2hex($res)); + $res = mcrypt_cbc($cipher, $key, $res, MCRYPT_DECRYPT, $iv); + var_dump(bin2hex($res)); +} + +$key = b'1234567890123456'; +echo "\n--- testing different iv lengths\n"; +foreach ($ivs as $iv) { + echo "\niv length=".strlen($iv)."\n"; + $res = mcrypt_cbc($cipher, $key, $data, $mode, $iv); + var_dump(bin2hex($res)); + $res = mcrypt_decrypt($cipher, $key, $res, MCRYPT_MODE_CBC, $iv); + var_dump(bin2hex($res)); +} + +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt : Rijndael128 functionality *** + +--- testing different key lengths + +key length=0 +string(128) "4fbf24aaa789f5194260ade1acd9499402c1845cc517e8fe43cfb5b90a0df294db33ecd1a836c47d6bf6d8600512ba415e17008a1e1991f81056258d82099397" +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +key length=0 +string(128) "4fbf24aaa789f5194260ade1acd9499402c1845cc517e8fe43cfb5b90a0df294db33ecd1a836c47d6bf6d8600512ba415e17008a1e1991f81056258d82099397" +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +key length=8 +string(128) "d6a3042b278fa5816dc6f46152acbe5fd7d1813c3808c27cd969d8e10a64d0238724edfda0322f4512308f22d142df0e92bed861c2b732f7650e234df59183dc" +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +key length=16 +string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101" +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +--- testing different iv lengths + +iv length=0 + +Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d +string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4" + +Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +iv length=0 + +Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d +string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4" + +Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +iv length=8 + +Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d +string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4" + +Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +iv length=16 +string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101" +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +iv length=17 + +Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d +string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4" + +Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" +===DONE===
\ No newline at end of file diff --git a/ext/mcrypt/tests/mcrypt_rijndael128_256BitKey.phpt b/ext/mcrypt/tests/mcrypt_rijndael128_256BitKey.phpt new file mode 100644 index 0000000..f69d369 --- /dev/null +++ b/ext/mcrypt/tests/mcrypt_rijndael128_256BitKey.phpt @@ -0,0 +1,87 @@ +--TEST-- +Test mcrypt_encrypt() function : TripleDES functionality +--SKIPIF-- +<?php +if (!extension_loaded("mcrypt")) { + print "skip - mcrypt extension not loaded"; +} +?> +--FILE-- +<?php +/* Prototype : string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + /* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) + * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + /* Prototype : string mcrypt_cbc(int cipher, string key, string data, int mode, string iv) + * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + /* Prototype : string mcrypt_ecb(int cipher, string key, string data, int mode, string iv) + * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv + * Source code: ext/mcrypt/mcrypt.c + * Alias to functions: + */ + +echo "*** Testing mcrypt : Rijndael128 functionality ***\n"; + +$cipher = MCRYPT_RIJNDAEL_128; +$mode = MCRYPT_MODE_CBC; +$data = b'This is the secret message which must be encrypted'; + +// keys : 20 bytes, 24 bytes, 30 Bytes, 32 Bytes, 40 Bytes +$keys = array( + b'12345678901234567890', + b'123456789012345678901234', + b'123456789012345678901234567890', + b'12345678901234567890123456789012', + b'1234567890123456789012345678901234567890' + ); +// rijndael128 is a block cipher of 128 bits (16 bytes) +$iv = b'1234567890123456'; + +echo "\n--- testing different key lengths\n"; +foreach ($keys as $key) { + echo "\nkey length=".strlen($key)."\n"; + $res = mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv); + var_dump(bin2hex($res)); + $res = mcrypt_decrypt($cipher, $key, $res, MCRYPT_MODE_CBC, $iv); + var_dump(bin2hex($res)); +} +?> +===DONE=== +--EXPECTF-- +*** Testing mcrypt : Rijndael128 functionality *** + +--- testing different key lengths + +key length=20 +string(128) "6369830bfc89a39c9981c9a40e349e3bbc8599c28d7ffbd7a330a67690dac6dfb76a55814e95c83cced68eb1544cdd8406d272c249bd0a60fa5b605d4aefbaa0" +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +key length=24 +string(128) "8ecdf1ed5742aff16ef34c819c8d22c707c54f4d9ffc18e5f6ab79fe68c25705351e2c001a0b9f29e5def67570ca9da644efb69a8bb97940cb4bec094dae8bb5" +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +key length=30 +string(128) "f7731f0c0ab22270b2f516c7837256ed731ba6658ca8f78cda2ab1588e204f990843719ae88474f6572711674fcda9f40d99155e4cc4f5a31aa461ad36a7871d" +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +key length=32 +string(128) "f23bc103bfd0859a8318acee6d96e5f43dff68f3cdeae817a1e77c33492e32bdb82c5f660fcd1a2bfda70d9de4d5d8028ce179a9e2f7f9ee7dd61c7b4b409e95" +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" + +key length=40 + +Warning: mcrypt_encrypt(): Size of key is too large for this algorithm in %s on line %d +string(128) "f23bc103bfd0859a8318acee6d96e5f43dff68f3cdeae817a1e77c33492e32bdb82c5f660fcd1a2bfda70d9de4d5d8028ce179a9e2f7f9ee7dd61c7b4b409e95" + +Warning: mcrypt_decrypt(): Size of key is too large for this algorithm in %s on line %d +string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000" +===DONE===
\ No newline at end of file diff --git a/ext/mcrypt/tests/vectors.txt b/ext/mcrypt/tests/vectors.txt new file mode 100644 index 0000000..3624ed6 --- /dev/null +++ b/ext/mcrypt/tests/vectors.txt @@ -0,0 +1,33 @@ +0000000000000000 0000000000000000 4EF997456198DD78 +FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 51866FD5B85ECB8A +3000000000000000 1000000000000001 7D856F9A613063F2 +1111111111111111 1111111111111111 2466DD878B963C9D +0123456789ABCDEF 1111111111111111 61F9C3802281B096 +1111111111111111 0123456789ABCDEF 7D0CC630AFDA1EC7 +FEDCBA9876543210 0123456789ABCDEF 0ACEAB0FC6A0A28D +7CA110454A1A6E57 01A1D6D039776742 59C68245EB05282B +0131D9619DC1376E 5CD54CA83DEF57DA B1B8CC0B250F09A0 +07A1133E4A0B2686 0248D43806F67172 1730E5778BEA1DA4 +3849674C2602319E 51454B582DDF440A A25E7856CF2651EB +04B915BA43FEB5B6 42FD443059577FA2 353882B109CE8F1A +0113B970FD34F2CE 059B5E0851CF143A 48F4D0884C379918 +0170F175468FB5E6 0756D8E0774761D2 432193B78951FC98 +43297FAD38E373FE 762514B829BF486A 13F04154D69D1AE5 +07A7137045DA2A16 3BDD119049372802 2EEDDA93FFD39C79 +04689104C2FD3B2F 26955F6835AF609A D887E0393C2DA6E3 +37D06BB516CB7546 164D5E404F275232 5F99D04F5B163969 +1F08260D1AC2465E 6B056E18759F5CCA 4A057A3B24D3977B +584023641ABA6176 004BD6EF09176062 452031C1E4FADA8E +025816164629B007 480D39006EE762F2 7555AE39F59B87BD +49793EBC79B3258F 437540C8698F3CFA 53C55F9CB49FC019 +4FB05E1515AB73A7 072D43A077075292 7A8E7BFA937E89A3 +49E95D6D4CA229BF 02FE55778117F12A CF9C5D7A4986ADB5 +018310DC409B26D6 1D9D5C5018F728C2 D1ABB290658BC778 +1C587F1C13924FEF 305532286D6F295A 55CB3774D13EF201 +0101010101010101 0123456789ABCDEF FA34EC4847B268B2 +1F1F1F1F0E0E0E0E 0123456789ABCDEF A790795108EA3CAE +E0FEE0FEF1FEF1FE 0123456789ABCDEF C39E072D9FAC631D +0000000000000000 FFFFFFFFFFFFFFFF 014933E0CDAFF6E4 +FFFFFFFFFFFFFFFF 0000000000000000 F21E9A77B71C49BC +0123456789ABCDEF 0000000000000000 245946885754369A +FEDCBA9876543210 FFFFFFFFFFFFFFFF 6B5C5A9C5D9E0A5A |