mike Wed Nov 15 22:51:45 2006 UTC Added files: /php-src/ext/mcrypt/tests basic.phpt
Modified files: /php-src/ext/mcrypt mcrypt.c php_mcrypt.h /php-src/ext/mcrypt/tests blowfish.phpt bug35496.phpt bug37595.phpt Log: - unicode upgrade - fix typo: ENIGNA->ENIGMA - add and fix tests
http://cvs.php.net/viewvc.cgi/php-src/ext/mcrypt/mcrypt.c?r1=1.100&r2=1.101&diff_format=u Index: php-src/ext/mcrypt/mcrypt.c diff -u php-src/ext/mcrypt/mcrypt.c:1.100 php-src/ext/mcrypt/mcrypt.c:1.101 --- php-src/ext/mcrypt/mcrypt.c:1.100 Sun Oct 8 13:34:22 2006 +++ php-src/ext/mcrypt/mcrypt.c Wed Nov 15 22:51:45 2006 @@ -16,7 +16,7 @@ | Derick Rethans <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: mcrypt.c,v 1.100 2006/10/08 13:34:22 bjori Exp $ */ +/* $Id: mcrypt.c,v 1.101 2006/11/15 22:51:45 mike Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -114,122 +114,41 @@ 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_CONVERT \ - convert_to_string_ex(cipher); \ - convert_to_string_ex(mode); \ - convert_to_string_ex(data); \ - convert_to_string_ex(key) -#define MCRYPT_CONVERT_WO_MODE \ - convert_to_string_ex(cipher); \ - convert_to_string_ex(data); \ - convert_to_string_ex(key) - -#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_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 MCRYPT_ENCRYPT 0 -#define MCRYPT_DECRYPT 1 +#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; \ + } \ #define MCRYPT_GET_INI \ cipher_dir_string = MCG(algorithms_dir); \ module_dir_string = MCG(modes_dir); -#define MCRYPT_CHECK_PARAM_COUNT(a,b) \ - if (argc < (a) || argc > (b)) { \ - WRONG_PARAM_COUNT; \ - } -/* - * #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 \ - switch (argc) { \ - case 5: \ - if (zend_get_parameters_ex(5, &cipher, &key, &data, &mode, &iv) == FAILURE) { \ - WRONG_PARAM_COUNT; \ - } \ - convert_to_string_ex(iv); \ - break; \ - case 4: \ - if (zend_get_parameters_ex(4, &cipher, &key, &data, &mode) == FAILURE) { \ - WRONG_PARAM_COUNT; \ - } \ - iv = NULL; \ - break; \ - default: \ - WRONG_PARAM_COUNT; \ - } - #define MCRYPT_GET_TD_ARG \ - zval **mcryptind; \ - php_mcrypt *pm; \ - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &mcryptind) == FAILURE) { \ - WRONG_PARAM_COUNT \ - } \ - ZEND_FETCH_RESOURCE (pm, php_mcrypt *, mcryptind, -1, "MCrypt", le_mcrypt); + zval *mcryptind; \ + php_mcrypt *pm; \ + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &mcryptind)) { \ + 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) { \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&|s&", \ + &module, &module_len, UG(ascii_conv), \ + &dir, &dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv))) == 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) @@ -242,7 +161,7 @@ mcrypt_generic_deinit(pm->td); mcrypt_module_close(pm->td); efree(pm); - pm = NULL; + rsrc->ptr = NULL; } } @@ -252,13 +171,13 @@ 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); + REGISTER_LONG_CONSTANT("MCRYPT_ENCRYPT", PHP_MCRYPT_ENCRYPT, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("MCRYPT_DECRYPT", PHP_MCRYPT_DECRYPT, 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); + REGISTER_LONG_CONSTANT("MCRYPT_DEV_RANDOM", PHP_MCRYPT_IV_SOURCE_RANDOM, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("MCRYPT_DEV_URANDOM", PHP_MCRYPT_IV_SOURCE_URANDOM, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("MCRYPT_RAND", PHP_MCRYPT_IV_SOURCE_RAND, CONST_PERSISTENT); /* ciphers */ MCRYPT_ENTRY2_2_4(3DES, "tripledes"); @@ -270,7 +189,7 @@ 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(ENIGMA, "crypt"); MCRYPT_ENTRY2_2_4(GOST, "gost"); MCRYPT_ENTRY2_2_4(LOKI97, "loki97"); MCRYPT_ENTRY2_2_4(PANAMA, "panama"); @@ -356,13 +275,8 @@ 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) +/* {{{ proto resource mcrypt_module_open(string cipher, string cipher_directory, string mode, string mode_directory) U Opens the module of the algorithm and the mode to be used */ PHP_FUNCTION(mcrypt_module_open) { @@ -373,9 +287,11 @@ 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)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&s&s&s&", + &cipher, &cipher_len, UG(ascii_conv), + &cipher_dir, &cipher_dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv)), + &mode, &mode_len, UG(ascii_conv), + &mode_dir, &mode_dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv)))) { return; } @@ -399,59 +315,45 @@ /* }}} */ -/* {{{ proto int mcrypt_generic_init(resource td, string key, string iv) +/* {{{ proto int mcrypt_generic_init(resource td, binary key, binary iv) U This function initializes all buffers for the specific module */ PHP_FUNCTION(mcrypt_generic_init) { - zval **key, **iv; - zval **mcryptind; - unsigned char *key_s, *iv_s; - int max_key_size, key_size, iv_size; + zval *mcryptind; + char *key, *iv; + int max_key_size, req_iv_size, key_size, iv_size; php_mcrypt *pm; - int argc; int result = 0; - argc = ZEND_NUM_ARGS(); - MCRYPT_CHECK_PARAM_COUNT (3,3) - - zend_get_parameters_ex(3, &mcryptind, &key, &iv); - ZEND_FETCH_RESOURCE(pm, php_mcrypt *, mcryptind, -1, "MCrypt", le_mcrypt); - convert_to_string_ex(key); - convert_to_string_ex(iv); + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rSS", &mcryptind, &key, &key_size, &iv, &iv_size)) { + 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); + req_iv_size = mcrypt_enc_get_iv_size(pm->td); - if (Z_STRLEN_PP(key) == 0) { + if (!key_size) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key size is 0"); } - key_s = emalloc(Z_STRLEN_PP(key)); - memset(key_s, 0, Z_STRLEN_PP(key)); - - iv_s = emalloc(iv_size + 1); - memset(iv_s, 0, iv_size + 1); - - if (Z_STRLEN_PP(key) > max_key_size) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key size too large; supplied length: %d, max: %d", Z_STRLEN_PP(key), max_key_size); + if (key_size > max_key_size) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key size too large; supplied length: %d, max: %d", key_size, max_key_size); key_size = max_key_size; - } else { - key_size = Z_STRLEN_PP(key); } - memcpy(key_s, Z_STRVAL_PP(key), Z_STRLEN_PP(key)); - if (Z_STRLEN_PP(iv) != iv_size) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Iv size incorrect; supplied length: %d, needed: %d", Z_STRLEN_PP(iv), iv_size); + if (iv_size != req_iv_size) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Iv size incorrect; supplied length: %d, needed: %d", iv_size, req_iv_size); } - memcpy(iv_s, Z_STRVAL_PP(iv), iv_size); mcrypt_generic_deinit(pm->td); - result = mcrypt_generic_init(pm->td, key_s, key_size, iv_s); + result = mcrypt_generic_init(pm->td, key, key_size, iv); + pm->init = 1; /* 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_PP(mcryptind)); + zend_list_delete(Z_LVAL_P(mcryptind)); switch (result) { case -3: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key length incorrect"); @@ -465,105 +367,91 @@ break; } } - pm->init = 1; RETVAL_LONG(result); - - efree(iv_s); - efree(key_s); } /* }}} */ -/* {{{ proto string mcrypt_generic(resource td, string data) +/* {{{ proto binary mcrypt_generic(resource td, binary data) U This function encrypts the plaintext */ PHP_FUNCTION(mcrypt_generic) { - zval **data, **mcryptind; + zval *mcryptind; php_mcrypt *pm; - int argc; - unsigned char* data_s; - int block_size, data_size; + char *data_copy, *data_str; + int block_size, data_size, data_len; - argc = ZEND_NUM_ARGS(); - MCRYPT_CHECK_PARAM_COUNT (2,2) - - zend_get_parameters_ex(2, &mcryptind, &data); - ZEND_FETCH_RESOURCE(pm, php_mcrypt *, mcryptind, -1, "MCrypt", le_mcrypt); + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rS", &mcryptind, &data_str, &data_len)) { + return; + } + ZEND_FETCH_RESOURCE(pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt); PHP_MCRYPT_INIT_CHECK - convert_to_string_ex(data); - if (Z_STRLEN_PP(data) == 0) { + + if (!data_len) { 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 */ + 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 = (((Z_STRLEN_PP(data) - 1) / block_size) + 1) * block_size; - data_s = emalloc(data_size + 1); - memset(data_s, 0, data_size); - memcpy(data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); - } else { /* It's not a block algorithm */ - data_size = Z_STRLEN_PP(data); - data_s = emalloc(data_size + 1); - memset(data_s, 0, data_size); - memcpy(data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); + data_size = (((data_len - 1) / block_size) + 1) * block_size; + data_copy = ecalloc(1, data_size + 1); + memcpy(data_copy, data_str, data_len); + } else { + /* It's not a block algorithm */ + data_copy = estrndup(data_str, data_size = data_len); } - mcrypt_generic(pm->td, data_s, data_size); - data_s[data_size] = '\0'; + mcrypt_generic(pm->td, data_copy, data_size); + data_copy[data_size] = '\0'; - RETVAL_STRINGL(data_s, data_size, 1); - efree(data_s); + RETVAL_STRINGL(data_copy, data_size, 0); } /* }}} */ -/* {{{ proto string mdecrypt_generic(resource td, string data) +/* {{{ proto binary mdecrypt_generic(resource td, binary data) U This function decrypts the plaintext */ PHP_FUNCTION(mdecrypt_generic) { - zval **data, **mcryptind; + zval *mcryptind; php_mcrypt *pm; - int argc; - char* data_s; - int block_size, data_size; - - argc = ZEND_NUM_ARGS(); - MCRYPT_CHECK_PARAM_COUNT (2,2) + char *data_str, *data_copy; + int block_size, data_len, data_size; - zend_get_parameters_ex(2, &mcryptind, &data); - ZEND_FETCH_RESOURCE(pm, php_mcrypt * , mcryptind, -1, "MCrypt", le_mcrypt); + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rS", &mcryptind, &data_str, &data_len)) { + return; + } + ZEND_FETCH_RESOURCE(pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt); PHP_MCRYPT_INIT_CHECK - convert_to_string_ex(data); - if (Z_STRLEN_PP(data) == 0) { + + if (!data_len) { 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 */ + 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 = (((Z_STRLEN_PP(data) - 1) / block_size) + 1) * block_size; - data_s = emalloc(data_size + 1); - memset(data_s, 0, data_size); - memcpy(data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); - } else { /* It's not a block algorithm */ - data_size = Z_STRLEN_PP(data); - data_s = emalloc(data_size + 1); - memset(data_s, 0, data_size); - memcpy(data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); + data_size = (((data_len - 1) / block_size) + 1) * block_size; + data_copy = ecalloc(1, data_size + 1); + memcpy(data_copy, data_str, data_len); + } else { + /* It's not a block algorithm */ + data_copy = estrndup(data_str, data_size = data_len); } - mdecrypt_generic(pm->td, data_s, data_size); + mdecrypt_generic(pm->td, data_copy, data_size); - RETVAL_STRINGL(data_s, data_size, 1); - efree(data_s); + RETVAL_STRINGL(data_copy, data_size, 0); } /* }}} */ -/* {{{ proto array mcrypt_enc_get_supported_key_sizes(resource td) +/* {{{ proto array mcrypt_enc_get_supported_key_sizes(resource td) U This function decrypts the crypttext */ PHP_FUNCTION(mcrypt_enc_get_supported_key_sizes) { @@ -584,7 +472,7 @@ /* }}} */ -/* {{{ proto int mcrypt_enc_self_test(resource td) +/* {{{ proto int mcrypt_enc_self_test(resource td) U This function runs the self test on the algorithm specified by the descriptor td */ PHP_FUNCTION(mcrypt_enc_self_test) { @@ -593,18 +481,18 @@ } /* }}} */ -/* {{{ proto bool mcrypt_module_close(resource td) +/* {{{ proto bool mcrypt_module_close(resource td) U Free the descriptor td */ PHP_FUNCTION(mcrypt_module_close) { MCRYPT_GET_TD_ARG - zend_list_delete(Z_LVAL_PP(mcryptind)); + zend_list_delete(Z_LVAL_P(mcryptind)); RETURN_TRUE; } /* }}} */ -/* {{{ proto bool mcrypt_generic_deinit(resource td) +/* {{{ proto bool mcrypt_generic_deinit(resource td) U This function terminates encrypt specified by the descriptor td */ PHP_FUNCTION(mcrypt_generic_deinit) { @@ -619,7 +507,7 @@ /* }}} */ -/* {{{ proto bool mcrypt_enc_is_block_algorithm_mode(resource td) +/* {{{ proto bool mcrypt_enc_is_block_algorithm_mode(resource td) U Returns TRUE if the mode is for use with block algorithms */ PHP_FUNCTION(mcrypt_enc_is_block_algorithm_mode) { @@ -634,7 +522,7 @@ /* }}} */ -/* {{{ proto bool mcrypt_enc_is_block_algorithm(resource td) +/* {{{ proto bool mcrypt_enc_is_block_algorithm(resource td) U Returns TRUE if the alrogithm is a block algorithms */ PHP_FUNCTION(mcrypt_enc_is_block_algorithm) { @@ -649,7 +537,7 @@ /* }}} */ -/* {{{ proto bool mcrypt_enc_is_block_mode(resource td) +/* {{{ proto bool mcrypt_enc_is_block_mode(resource td) U Returns TRUE if the mode outputs blocks */ PHP_FUNCTION(mcrypt_enc_is_block_mode) { @@ -664,7 +552,7 @@ /* }}} */ -/* {{{ proto int mcrypt_enc_get_block_size(resource td) +/* {{{ proto int mcrypt_enc_get_block_size(resource td) U Returns the block size of the cipher specified by the descriptor td */ PHP_FUNCTION(mcrypt_enc_get_block_size) { @@ -674,7 +562,7 @@ /* }}} */ -/* {{{ proto int mcrypt_enc_get_key_size(resource td) +/* {{{ proto int mcrypt_enc_get_key_size(resource td) U Returns the maximum supported key size in bytes of the algorithm specified by the descriptor td */ PHP_FUNCTION(mcrypt_enc_get_key_size) { @@ -684,7 +572,7 @@ /* }}} */ -/* {{{ proto int mcrypt_enc_get_iv_size(resource td) +/* {{{ proto int mcrypt_enc_get_iv_size(resource td) U Returns the size of the IV in bytes of the algorithm specified by the descriptor td */ PHP_FUNCTION(mcrypt_enc_get_iv_size) { @@ -694,7 +582,7 @@ /* }}} */ -/* {{{ proto string mcrypt_enc_get_algorithms_name(resource td) +/* {{{ proto string mcrypt_enc_get_algorithms_name(resource td) U Returns the name of the algorithm specified by the descriptor td */ PHP_FUNCTION(mcrypt_enc_get_algorithms_name) { @@ -702,13 +590,13 @@ MCRYPT_GET_TD_ARG name = mcrypt_enc_get_algorithms_name(pm->td); - RETVAL_STRING(name, 1); + RETVAL_ASCII_STRING(name, 1); mcrypt_free(name); } /* }}} */ -/* {{{ proto string mcrypt_enc_get_modes_name(resource td) +/* {{{ proto string mcrypt_enc_get_modes_name(resource td) U Returns the name of the mode specified by the descriptor td */ PHP_FUNCTION(mcrypt_enc_get_modes_name) { @@ -716,13 +604,13 @@ MCRYPT_GET_TD_ARG name = mcrypt_enc_get_modes_name(pm->td); - RETVAL_STRING(name, 1); + RETVAL_ASCII_STRING(name, 1); mcrypt_free(name); } /* }}} */ -/* {{{ proto bool mcrypt_module_self_test(string algorithm [, string lib_dir]) +/* {{{ proto bool mcrypt_module_self_test(string algorithm [, string lib_dir]) U Does a self test of the module "module" */ PHP_FUNCTION(mcrypt_module_self_test) { @@ -737,7 +625,7 @@ /* }}} */ -/* {{{ proto bool mcrypt_module_is_block_algorithm_mode(string mode [, string lib_dir]) +/* {{{ proto bool mcrypt_module_is_block_algorithm_mode(string mode [, string lib_dir]) U Returns TRUE if the mode is for use with block algorithms */ PHP_FUNCTION(mcrypt_module_is_block_algorithm_mode) { @@ -752,7 +640,7 @@ /* }}} */ -/* {{{ proto bool mcrypt_module_is_block_algorithm(string algorithm [, string lib_dir]) +/* {{{ proto bool mcrypt_module_is_block_algorithm(string algorithm [, string lib_dir]) U Returns TRUE if the algorithm is a block algorithm */ PHP_FUNCTION(mcrypt_module_is_block_algorithm) { @@ -767,7 +655,7 @@ /* }}} */ -/* {{{ proto bool mcrypt_module_is_block_mode(string mode [, string lib_dir]) +/* {{{ proto bool mcrypt_module_is_block_mode(string mode [, string lib_dir]) U Returns TRUE if the mode outputs blocks of bytes */ PHP_FUNCTION(mcrypt_module_is_block_mode) { @@ -782,7 +670,7 @@ /* }}} */ -/* {{{ proto int mcrypt_module_get_algo_block_size(string algorithm [, string lib_dir]) +/* {{{ proto int mcrypt_module_get_algo_block_size(string algorithm [, string lib_dir]) U Returns the block size of the algorithm */ PHP_FUNCTION(mcrypt_module_get_algo_block_size) { @@ -793,7 +681,7 @@ /* }}} */ -/* {{{ proto int mcrypt_module_get_algo_key_size(string algorithm [, string lib_dir]) +/* {{{ proto int mcrypt_module_get_algo_key_size(string algorithm [, string lib_dir]) U Returns the maximum supported key size of the algorithm */ PHP_FUNCTION(mcrypt_module_get_algo_key_size) { @@ -804,7 +692,7 @@ /* }}} */ -/* {{{ proto array mcrypt_module_get_supported_key_sizes(string algorithm [, string lib_dir]) +/* {{{ proto array mcrypt_module_get_supported_key_sizes(string algorithm [, string lib_dir]) U This function decrypts the crypttext */ PHP_FUNCTION(mcrypt_module_get_supported_key_sizes) { @@ -824,7 +712,7 @@ /* }}} */ -/* {{{ proto array mcrypt_list_algorithms([string lib_dir]) +/* {{{ proto array mcrypt_list_algorithms([string lib_dir]) U List all algorithms in "module_dir" */ PHP_FUNCTION(mcrypt_list_algorithms) { @@ -833,8 +721,8 @@ int lib_dir_len; int i, count; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", - &lib_dir, &lib_dir_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s&", + &lib_dir, &lib_dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv))) == FAILURE) { return; } @@ -845,14 +733,13 @@ 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); + add_index_ascii_string(return_value, i, modules[i], ZSTR_DUPLICATE); } mcrypt_free_p(modules, count); } /* }}} */ - -/* {{{ proto array mcrypt_list_modes([string lib_dir]) +/* {{{ proto array mcrypt_list_modes([string lib_dir]) U List all modes "module_dir" */ PHP_FUNCTION(mcrypt_list_modes) { @@ -861,8 +748,8 @@ int lib_dir_len; int i, count; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", - &lib_dir, &lib_dir_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s&", + &lib_dir, &lib_dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv))) == FAILURE) { return; } @@ -873,14 +760,14 @@ 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); + add_index_ascii_string(return_value, i, modules[i], ZSTR_DUPLICATE); } mcrypt_free_p(modules, count); } /* }}} */ -/* {{{ proto int mcrypt_get_key_size(string cipher, string module) +/* {{{ proto int mcrypt_get_key_size(string cipher, string module) U Get the key size of cipher */ PHP_FUNCTION(mcrypt_get_key_size) { @@ -893,8 +780,8 @@ MCRYPT_GET_INI - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", - &cipher, &cipher_len, &module, &module_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&s&", + &cipher, &cipher_len, UG(ascii_conv), &module, &module_len, UG(ascii_conv)) == FAILURE) { return; } @@ -910,7 +797,7 @@ /* }}} */ -/* {{{ proto int mcrypt_get_block_size(string cipher, string module) +/* {{{ proto int mcrypt_get_block_size(string cipher, string module) U Get the key size of cipher */ PHP_FUNCTION(mcrypt_get_block_size) { @@ -923,8 +810,8 @@ MCRYPT_GET_INI - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", - &cipher, &cipher_len, &module, &module_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&s&", + &cipher, &cipher_len, UG(ascii_conv), &module, &module_len, UG(ascii_conv)) == FAILURE) { return; } @@ -940,7 +827,7 @@ /* }}} */ -/* {{{ proto int mcrypt_get_iv_size(string cipher, string module) +/* {{{ proto int mcrypt_get_iv_size(string cipher, string module) U Get the IV size of cipher (Usually the same as the blocksize) */ PHP_FUNCTION(mcrypt_get_iv_size) { @@ -953,8 +840,8 @@ MCRYPT_GET_INI - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", - &cipher, &cipher_len, &module, &module_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&s&", + &cipher, &cipher_len, UG(ascii_conv), &module, &module_len, UG(ascii_conv)) == FAILURE) { return; } @@ -970,8 +857,8 @@ /* }}} */ -/* {{{ proto string mcrypt_get_cipher_name(string cipher) - Get the key size of cipher */ +/* {{{ proto string mcrypt_get_cipher_name(string cipher) U + Get the name of cipher */ PHP_FUNCTION(mcrypt_get_cipher_name) { char *cipher_dir_string; @@ -983,8 +870,8 @@ MCRYPT_GET_INI - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", - &cipher, &cipher_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&", + &cipher, &cipher_len, UG(ascii_conv)) == FAILURE) { return; } @@ -994,14 +881,14 @@ if (td != MCRYPT_FAILED) { cipher_name = mcrypt_enc_get_algorithms_name(td); mcrypt_module_close(td); - RETVAL_STRING(cipher_name,1); + RETVAL_ASCII_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); + RETVAL_ASCII_STRING(cipher_name,1); mcrypt_free(cipher_name); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED); @@ -1011,275 +898,247 @@ } /* }}} */ - -static void php_mcrypt_do_crypt (char* cipher, zval **key, zval **data, char *mode, zval **iv, int argc, int dencrypt, zval* return_value TSRMLS_DC) +/* {{{ inline _php_mcrypt_func */ +static inline void _php_mcrypt_func(INTERNAL_FUNCTION_PARAMETERS, long op, char *mode_str, int mode_len) { - 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; + char *cipher_str, *key_str, *iv_str = NULL, *data_str, *data_copy = NULL; + int cipher_len, key_len, iv_len = 0, data_len, data_size = 0; - 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 (Z_STRLEN_PP(key) > 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 = Z_STRLEN_PP(key); - key_s = emalloc(use_key_length); - memset(key_s, 0, use_key_length); - memcpy(key_s, Z_STRVAL_PP(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, Z_STRVAL_PP(key), MIN(Z_STRLEN_PP(key), 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] >= Z_STRLEN_PP(key) && - 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, Z_STRVAL_PP(key), MIN(Z_STRLEN_PP(key), use_key_length)); - } - mcrypt_free (key_length_sizes); - - /* Check IV */ - iv_s = NULL; - iv_size = mcrypt_enc_get_iv_size (td); - if (argc == 5) { - if (iv_size != Z_STRLEN_PP(iv)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_IV_WRONG_SIZE); - } else { - iv_s = emalloc(iv_size + 1); - memcpy(iv_s, Z_STRVAL_PP(iv), iv_size); - } - } else if (argc == 4) { - if (iv_size != 0) { - 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); + if (mode_len) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&SSl|S", + &cipher_str, &cipher_len, UG(ascii_conv), + &key_str, &key_len, &data_str, &data_len, + &op, &iv_str, &iv_len)) { + return; } - } - - /* 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 = (((Z_STRLEN_PP(data) - 1) / block_size) + 1) * block_size; - data_s = emalloc(data_size); - memset(data_s, 0, data_size); - memcpy(data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); - } else { /* It's not a block algorithm */ - data_size = Z_STRLEN_PP(data); - data_s = emalloc(data_size); - memset(data_s, 0, data_size); - memcpy(data_s, Z_STRVAL_PP(data), Z_STRLEN_PP(data)); - } - - 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); + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&SSs&|S", + &cipher_str, &cipher_len, UG(ascii_conv), + &key_str, &key_len, &data_str, &data_len, + &mode_str, &mode_len, UG(ascii_conv), + &iv_str, &iv_len)) { + return; + } } - 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); + if (SUCCESS != php_mcrypt_func(op, cipher_str, mode_str, key_str, key_len, iv_str, iv_len, data_str, data_len, &data_copy, &data_size TSRMLS_CC)) { + RETURN_FALSE; } - efree (data_s); + RETURN_STRINGL(data_copy, data_size, 0); } +/* }}} */ -/* {{{ proto string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) +/* {{{ proto string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) U OFB crypt/decrypt data using key key with cipher cipher starting with iv */ PHP_FUNCTION(mcrypt_encrypt) { - zval **cipher, **key, **data, **mode, **iv; - int argc; - - argc = ZEND_NUM_ARGS(); - - MCRYPT_CHECK_PARAM_COUNT(4, 5) - MCRYPT_GET_CRYPT_ARGS - - MCRYPT_CONVERT; - - php_mcrypt_do_crypt(Z_STRVAL_PP(cipher), key, data, Z_STRVAL_PP(mode), iv, argc, MCRYPT_ENCRYPT, return_value TSRMLS_CC); + _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MCRYPT_ENCRYPT, NULL, 0); } /* }}} */ -/* {{{ proto string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) +/* {{{ proto string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) U OFB crypt/decrypt data using key key with cipher cipher starting with iv */ PHP_FUNCTION(mcrypt_decrypt) { - zval **cipher, **key, **data, **mode, **iv; - int argc; - - argc = ZEND_NUM_ARGS(); - - MCRYPT_CHECK_PARAM_COUNT(4, 5) - MCRYPT_GET_CRYPT_ARGS - - MCRYPT_CONVERT; - - php_mcrypt_do_crypt(Z_STRVAL_PP(cipher), key, data, Z_STRVAL_PP(mode), iv, argc, MCRYPT_DECRYPT, return_value TSRMLS_CC); + _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MCRYPT_DECRYPT, NULL, 0); } /* }}} */ -/* {{{ proto string mcrypt_ecb(int cipher, string key, string data, int mode, string iv) +/* {{{ proto string mcrypt_ecb(int cipher, string key, string data, int mode, string iv) U ECB crypt/decrypt data using key key with cipher cipher starting with iv */ PHP_FUNCTION(mcrypt_ecb) { - zval **cipher, **key, **data, **mode, **iv; - int argc; - - argc = ZEND_NUM_ARGS(); - - MCRYPT_CHECK_PARAM_COUNT(4, 5) - MCRYPT_GET_CRYPT_ARGS - - MCRYPT_CONVERT_WO_MODE; - convert_to_long_ex(mode); - - php_mcrypt_do_crypt(Z_STRVAL_PP(cipher), key, data, "ecb", iv, argc, Z_LVAL_PP(mode), return_value TSRMLS_CC); + _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, ZEND_STRL("ecb")); } /* }}} */ -/* {{{ proto string mcrypt_cbc(int cipher, string key, string data, int mode, string iv) +/* {{{ proto string mcrypt_cbc(int cipher, string key, string data, int mode, string iv) U CBC crypt/decrypt data using key key with cipher cipher starting with iv */ PHP_FUNCTION(mcrypt_cbc) { - zval **cipher, **key, **data, **mode, **iv; - int argc; - - argc = ZEND_NUM_ARGS(); - - MCRYPT_CHECK_PARAM_COUNT(4, 5) - MCRYPT_GET_CRYPT_ARGS - - MCRYPT_CONVERT_WO_MODE; - convert_to_long_ex(mode); - - php_mcrypt_do_crypt(Z_STRVAL_PP(cipher), key, data, "cbc", iv, argc, Z_LVAL_PP(mode), return_value TSRMLS_CC); + _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, ZEND_STRL("cbc")); } /* }}} */ -/* {{{ proto string mcrypt_cfb(int cipher, string key, string data, int mode, string iv) +/* {{{ proto string mcrypt_cfb(int cipher, string key, string data, int mode, string iv) U CFB crypt/decrypt data using key key with cipher cipher starting with iv */ PHP_FUNCTION(mcrypt_cfb) { - zval **cipher, **key, **data, **mode, **iv; - int argc; - - argc = ZEND_NUM_ARGS(); - - MCRYPT_CHECK_PARAM_COUNT(4, 5) - MCRYPT_GET_CRYPT_ARGS - - MCRYPT_CONVERT_WO_MODE; - convert_to_long_ex(mode); - - php_mcrypt_do_crypt(Z_STRVAL_PP(cipher), key, data, "cfb", iv, argc, Z_LVAL_PP(mode), return_value TSRMLS_CC); + _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, ZEND_STRL("cfb")); } /* }}} */ -/* {{{ proto string mcrypt_ofb(int cipher, string key, string data, int mode, string iv) +/* {{{ proto string mcrypt_ofb(int cipher, string key, string data, int mode, string iv) U OFB crypt/decrypt data using key key with cipher cipher starting with iv */ PHP_FUNCTION(mcrypt_ofb) { - zval **cipher, **key, **data, **mode, **iv; - int argc; - - argc = ZEND_NUM_ARGS(); - - MCRYPT_CHECK_PARAM_COUNT(4, 5) - MCRYPT_GET_CRYPT_ARGS - - MCRYPT_CONVERT_WO_MODE; - convert_to_long_ex(mode); - - php_mcrypt_do_crypt(Z_STRVAL_PP(cipher), key, data, "ofb", iv, argc, Z_LVAL_PP(mode), return_value TSRMLS_CC); + _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, ZEND_STRL("ofb")); } /* }}} */ -/* {{{ proto string mcrypt_create_iv(int size, int source) +/* {{{ proto binary mcrypt_create_iv(int size, int source) U Create an initialization vector (IV) */ PHP_FUNCTION(mcrypt_create_iv) { - char *iv; - long source = RANDOM; - long size; - int n = 0; + char *iv_str; + int iv_len; + long size, source = PHP_MCRYPT_IV_SOURCE_RANDOM; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &size, &source) == FAILURE) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &size, &source)) { return; } + if (SUCCESS != php_mcrypt_iv(source, size, &iv_str, &iv_len TSRMLS_CC)) { + RETURN_FALSE; + } + RETURN_STRINGL(iv_str, iv_len, 0); +} +/* }}} */ +/* {{{ php_mcrypt_iv */ +int php_mcrypt_iv(php_mcrypt_iv_source source, int size, char **iv_str, int *iv_len TSRMLS_DC) +{ + int fd, n; + size_t read_bytes; + if (size <= 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can not create an IV with size 0 or smaller"); - RETURN_FALSE; + return FAILURE; } - iv = ecalloc(size + 1, 1); + *iv_str = ecalloc(size + 1, 1); - if (source == RANDOM || source == URANDOM) { - 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; + switch (source) { + case PHP_MCRYPT_IV_SOURCE_RANDOM: + case PHP_MCRYPT_IV_SOURCE_URANDOM: + read_bytes = 0; + + fd = open(source == PHP_MCRYPT_IV_SOURCE_RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY); + if (fd < 0) { + efree(*iv_str); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot open source device"); + return FAILURE; } - 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; + + while (read_bytes < size) { + n = read(fd, *iv_str + read_bytes, size - read_bytes); + if (n < 0) { + break; + } + read_bytes += n; + } + *iv_len = read_bytes; + close(fd); + + if (*iv_len < size) { + efree(*iv_str); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random data"); + return FAILURE; + } + break; + case PHP_MCRYPT_IV_SOURCE_RAND: + *iv_len = size; + while (size) { + (*iv_str)[--size] = 255.0 * rand() / RAND_MAX; + } + break; + } + + return SUCCESS; +} +/* }}} */ + +/* {{{ php_mcrypt */ +int php_mcrypt_func(php_mcrypt_op op, char *cipher, char *mode, char *key_str, int key_len, char *iv_str, int iv_len, char *data_str, int data_len, char **data_copy, int *data_size TSRMLS_DC) +{ + MCRYPT td; + char *cipher_dir_string, *module_dir_string, *key_copy, *iv_copy; + int i, status = SUCCESS, count, *key_sizes, key_size, iv_size, block_size; + + 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 FAILURE; + } + + if ((key_size = mcrypt_enc_get_key_size(td)) < key_len) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Size of key is too large for this algorithm"); + } + + key_sizes = mcrypt_enc_get_supported_key_sizes(td, &count); + switch (count) { + case 0: + key_copy = estrndup(key_str, key_len); + break; + case 1: + key_copy = ecalloc(1, key_sizes[0]); + memcpy(key_copy, key_str, MIN(key_len, key_sizes[0])); + key_len = key_sizes[0]; + break; + default: + for (i = 0; i < count; ++i) { + if (key_sizes[i] >= key_len && key_sizes[i] <= key_size) { + key_copy = ecalloc(1, key_sizes[i]); + memcpy(key_copy, key_str, MIN(key_len, key_sizes[i])); + key_len = key_sizes[i]; + break; + } + } + break; + } + mcrypt_free(key_sizes); + + iv_size = mcrypt_enc_get_iv_size(td); + if (iv_len) { + if (iv_len == iv_size) { + iv_copy = estrndup(iv_str, iv_len); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_IV_WRONG_SIZE); + iv_copy = ecalloc(1, iv_size); + memcpy(iv_copy, iv_str, MIN(iv_len, iv_size)); } } else { - n = size; - while (size) { - iv[--size] = 255.0 * rand() / RAND_MAX; + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to use an empty IV, which is NOT recommended"); + iv_copy = ecalloc(1, iv_size); + } + + if (mcrypt_enc_is_block_mode(td) == 1) { + block_size = mcrypt_enc_get_block_size(td); + *data_size = (((data_len - 1) / block_size) + 1) * block_size; + *data_copy = ecalloc(1, *data_size + 1); + memcpy(*data_copy, data_str, data_len); + } else { + *data_copy = estrndup(data_str, *data_size = data_len); + } + + if (mcrypt_generic_init(td, key_copy, key_len, iv_copy) >= 0) { + switch (op) { + case PHP_MCRYPT_ENCRYPT: + if (mcrypt_generic(td, *data_copy, *data_size)) { + efree(*data_copy); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "encryption failed"); + status = FAILURE; + } + break; + case PHP_MCRYPT_DECRYPT: + if (mdecrypt_generic(td, *data_copy, *data_size)) { + efree(*data_copy); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "decryption failed"); + status = FAILURE; + } + break; } + } else { + php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Mcrypt initialisation failed"); /* huh? error? */ + status = FAILURE; } - RETURN_STRINGL(iv, n, 0); + + efree(key_copy); + efree(iv_copy); + + return status; } /* }}} */ http://cvs.php.net/viewvc.cgi/php-src/ext/mcrypt/php_mcrypt.h?r1=1.28&r2=1.29&diff_format=u Index: php-src/ext/mcrypt/php_mcrypt.h diff -u php-src/ext/mcrypt/php_mcrypt.h:1.28 php-src/ext/mcrypt/php_mcrypt.h:1.29 --- php-src/ext/mcrypt/php_mcrypt.h:1.28 Mon Jun 26 11:31:19 2006 +++ php-src/ext/mcrypt/php_mcrypt.h Wed Nov 15 22:51:45 2006 @@ -16,7 +16,7 @@ | Derick Rethans <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: php_mcrypt.h,v 1.28 2006/06/26 11:31:19 bjori Exp $ */ +/* $Id: php_mcrypt.h,v 1.29 2006/11/15 22:51:45 mike Exp $ */ #ifndef PHP_MCRYPT_H #define PHP_MCRYPT_H @@ -30,6 +30,20 @@ extern zend_module_entry mcrypt_module_entry; #define mcrypt_module_ptr &mcrypt_module_entry +typedef enum { + PHP_MCRYPT_IV_SOURCE_RANDOM = 0, + PHP_MCRYPT_IV_SOURCE_URANDOM, + PHP_MCRYPT_IV_SOURCE_RAND +} php_mcrypt_iv_source; + +typedef enum { + PHP_MCRYPT_ENCRYPT = 0, + PHP_MCRYPT_DECRYPT = 1, +} php_mcrypt_op; + +int php_mcrypt_iv(php_mcrypt_iv_source source, int size, char **iv_str, int *iv_len TSRMLS_DC); +int php_mcrypt_func(php_mcrypt_op op, char *cipher, char *mode, char *key_str, int key_len, char *iv_str, int iv_len, char *data_str, int data_len, char **data_copy, int *data_size TSRMLS_DC); + /* Functions for both old and new API */ PHP_FUNCTION(mcrypt_ecb); PHP_FUNCTION(mcrypt_cbc); http://cvs.php.net/viewvc.cgi/php-src/ext/mcrypt/tests/blowfish.phpt?r1=1.4&r2=1.5&diff_format=u Index: php-src/ext/mcrypt/tests/blowfish.phpt diff -u php-src/ext/mcrypt/tests/blowfish.phpt:1.4 php-src/ext/mcrypt/tests/blowfish.phpt:1.5 --- php-src/ext/mcrypt/tests/blowfish.phpt:1.4 Wed May 31 20:13:14 2006 +++ php-src/ext/mcrypt/tests/blowfish.phpt Wed Nov 15 22:51:45 2006 @@ -6,11 +6,11 @@ <?php function hex2bin($data) { $len = strlen($data); - return pack("H" . $len, $data); + return (binary) pack("H" . $len, $data); } print "key plain crypt guess stat\n"; -$null = "\0\0\0\0\0\0\0\0"; +$null = b"\0\0\0\0\0\0\0\0"; $vectors = file(dirname(__FILE__) . "/vectors.txt"); $td = mcrypt_module_open ("blowfish", "", MCRYPT_MODE_ECB, ""); http://cvs.php.net/viewvc.cgi/php-src/ext/mcrypt/tests/bug35496.phpt?r1=1.3&r2=1.4&diff_format=u Index: php-src/ext/mcrypt/tests/bug35496.phpt diff -u php-src/ext/mcrypt/tests/bug35496.phpt:1.3 php-src/ext/mcrypt/tests/bug35496.phpt:1.4 --- php-src/ext/mcrypt/tests/bug35496.phpt:1.3 Sun Oct 8 13:34:22 2006 +++ php-src/ext/mcrypt/tests/bug35496.phpt Wed Nov 15 22:51:45 2006 @@ -5,8 +5,8 @@ --FILE-- <?php $td = mcrypt_module_open('rijndael-256', '', 'ofb', ''); -mcrypt_generic($td, "foobar"); -mdecrypt_generic($td, "baz"); +mcrypt_generic($td, b"foobar"); +mdecrypt_generic($td, b"baz"); ?> --EXPECTF-- Warning: mcrypt_generic(): Operation disallowed prior to mcrypt_generic_init() in %s/bug35496.php on line 3 http://cvs.php.net/viewvc.cgi/php-src/ext/mcrypt/tests/bug37595.phpt?r1=1.3&r2=1.4&diff_format=u Index: php-src/ext/mcrypt/tests/bug37595.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/mcrypt/tests/basic.phpt?view=markup&rev=1.1 Index: php-src/ext/mcrypt/tests/basic.phpt +++ php-src/ext/mcrypt/tests/basic.phpt --TEST-- mcrypt basic --FILE-- <?php $as = mcrypt_list_algorithms(); shuffle($as); $ms = mcrypt_list_modes(); shuffle($ms); /* test only 5 algos with 3 modes, so we don't timeout */ foreach (array_slice($as, 0, 5) as $a) { foreach (array_slice($ms, 0, 3) as $m) { if ((mcrypt_module_is_block_algorithm($a) == mcrypt_module_is_block_algorithm_mode($m)) && ($is = mcrypt_get_iv_size($a, $m))) { $iv = mcrypt_create_iv($is); $rs = mcrypt_decrypt($a, b"key", mcrypt_encrypt($a, b"key", b"data", $m, $iv), $m, $iv); if (b"data" !== (mcrypt_module_is_block_mode($m) ? rtrim($rs, b"\0") : $rs)) { echo "FAIL: $a/$m "; var_dump($rs); } } } } ?> --EXPECT--
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php