summaryrefslogtreecommitdiff
path: root/ext/mcrypt/mcrypt.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/mcrypt/mcrypt.c')
-rw-r--r--ext/mcrypt/mcrypt.c428
1 files changed, 174 insertions, 254 deletions
diff --git a/ext/mcrypt/mcrypt.c b/ext/mcrypt/mcrypt.c
index 45dd5a5f6f..536edde425 100644
--- a/ext/mcrypt/mcrypt.c
+++ b/ext/mcrypt/mcrypt.c
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
@@ -40,12 +40,12 @@
#include "php_globals.h"
#include "ext/standard/info.h"
#include "ext/standard/php_rand.h"
-#include "ext/standard/php_smart_str.h"
+#include "zend_smart_str.h"
#include "php_mcrypt_filter.h"
static int le_mcrypt;
-typedef struct _php_mcrypt {
+typedef struct _php_mcrypt {
MCRYPT td;
zend_bool init;
} php_mcrypt;
@@ -200,38 +200,6 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_decrypt, 0, 0, 5)
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, 1)
ZEND_ARG_INFO(0, size)
ZEND_ARG_INFO(0, source)
@@ -239,10 +207,6 @@ ZEND_END_ARG_INFO()
/* }}} */
const zend_function_entry mcrypt_functions[] = { /* {{{ */
- PHP_DEP_FE(mcrypt_ecb, arginfo_mcrypt_ecb)
- PHP_DEP_FE(mcrypt_cbc, arginfo_mcrypt_cbc)
- PHP_DEP_FE(mcrypt_cfb, arginfo_mcrypt_cfb)
- PHP_DEP_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)
@@ -258,7 +222,6 @@ const zend_function_entry mcrypt_functions[] = { /* {{{ */
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)
@@ -295,12 +258,12 @@ ZEND_DECLARE_MODULE_GLOBALS(mcrypt)
zend_module_entry mcrypt_module_entry = {
STANDARD_MODULE_HEADER,
- "mcrypt",
+ "mcrypt",
mcrypt_functions,
PHP_MINIT(mcrypt), PHP_MSHUTDOWN(mcrypt),
NULL, NULL,
PHP_MINFO(mcrypt),
- NO_VERSION_YET,
+ PHP_MCRYPT_VERSION,
PHP_MODULE_GLOBALS(mcrypt),
PHP_GINIT(mcrypt),
PHP_GSHUTDOWN(mcrypt),
@@ -309,6 +272,9 @@ zend_module_entry mcrypt_module_entry = {
};
#ifdef COMPILE_DL_MCRYPT
+#ifdef ZTS
+ZEND_TSRMLS_CACHE_DEFINE();
+#endif
ZEND_GET_MODULE(mcrypt)
#endif
@@ -332,7 +298,7 @@ typedef enum {
*/
#define MCRYPT_GET_CRYPT_ARGS \
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sssZ|s", \
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssz|s", \
&cipher, &cipher_len, &key, &key_len, &data, &data_len, &mode, &iv, &iv_len) == FAILURE) { \
return; \
}
@@ -340,17 +306,19 @@ typedef enum {
#define MCRYPT_GET_TD_ARG \
zval *mcryptind; \
php_mcrypt *pm; \
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &mcryptind) == FAILURE) { \
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &mcryptind) == FAILURE) { \
return; \
} \
- ZEND_FETCH_RESOURCE (pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt);
+ if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) { \
+ RETURN_FALSE; \
+ }
#define MCRYPT_GET_MODE_DIR_ARGS(DIRECTORY) \
char *dir = NULL; \
- int dir_len; \
+ size_t dir_len; \
char *module; \
- int module_len; \
- if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, \
+ size_t module_len; \
+ if (zend_parse_parameters (ZEND_NUM_ARGS(), \
"s|s", &module, &module_len, &dir, &dir_len) == FAILURE) { \
return; \
}
@@ -362,7 +330,7 @@ typedef enum {
#define PHP_MCRYPT_INIT_CHECK \
if (!pm->init) { \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Operation disallowed prior to mcrypt_generic_init()."); \
+ php_error_docref(NULL, E_WARNING, "Operation disallowed prior to mcrypt_generic_init()."); \
RETURN_FALSE; \
} \
@@ -371,10 +339,10 @@ PHP_INI_BEGIN()
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) /* {{{ */
+static void php_mcrypt_module_dtor(zend_resource *rsrc) /* {{{ */
{
php_mcrypt *pm = (php_mcrypt *) rsrc->ptr;
- if (pm) {
+ if (pm) {
mcrypt_generic_deinit(pm->td);
mcrypt_module_close(pm->td);
efree(pm);
@@ -384,13 +352,16 @@ static void php_mcrypt_module_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{
/* }}} */
static PHP_GINIT_FUNCTION(mcrypt)
-{
+{/*{{{*/
+#if defined(COMPILE_DL_MCRYPT) && defined(ZTS)
+ ZEND_TSRMLS_CACHE_UPDATE();
+#endif
mcrypt_globals->fd[RANDOM] = -1;
mcrypt_globals->fd[URANDOM] = -1;
-}
+}/*}}}*/
static PHP_GSHUTDOWN_FUNCTION(mcrypt)
-{
+{/*{{{*/
if (mcrypt_globals->fd[RANDOM] > 0) {
close(mcrypt_globals->fd[RANDOM]);
mcrypt_globals->fd[RANDOM] = -1;
@@ -400,7 +371,7 @@ static PHP_GSHUTDOWN_FUNCTION(mcrypt)
close(mcrypt_globals->fd[URANDOM]);
mcrypt_globals->fd[URANDOM] = -1;
}
-}
+}/*}}}*/
static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
{
@@ -456,8 +427,8 @@ static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
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);
+ php_stream_filter_register_factory("mcrypt.*", &php_mcrypt_filter_factory);
+ php_stream_filter_register_factory("mdecrypt.*", &php_mcrypt_filter_factory);
return SUCCESS;
}
@@ -465,15 +436,15 @@ static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
static PHP_MSHUTDOWN_FUNCTION(mcrypt) /* {{{ */
{
- php_stream_filter_unregister_factory("mcrypt.*" TSRMLS_CC);
- php_stream_filter_unregister_factory("mdecrypt.*" TSRMLS_CC);
+ php_stream_filter_unregister_factory("mcrypt.*");
+ php_stream_filter_unregister_factory("mdecrypt.*");
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
-#include "ext/standard/php_smart_str.h"
+#include "zend_smart_str.h"
PHP_MINFO_FUNCTION(mcrypt) /* {{{ */
{
@@ -512,12 +483,13 @@ PHP_MINFO_FUNCTION(mcrypt) /* {{{ */
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);
+ php_info_print_table_row(2, "Supported ciphers", ZSTR_VAL(tmp1.s));
+ php_info_print_table_row(2, "Supported modes", ZSTR_VAL(tmp2.s));
smart_str_free(&tmp1);
smart_str_free(&tmp2);
+
php_info_print_table_end();
-
+
DISPLAY_INI_ENTRIES();
}
/* }}} */
@@ -528,32 +500,32 @@ 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;
+ size_t cipher_len, cipher_dir_len;
+ size_t mode_len, mode_dir_len;
MCRYPT td;
php_mcrypt *pm;
-
- if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, "ssss",
+
+ if (zend_parse_parameters (ZEND_NUM_ARGS(), "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,
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");
+ php_error_docref(NULL, 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);
+ RETURN_RES(zend_register_resource(pm, le_mcrypt));
}
}
/* }}} */
@@ -563,24 +535,26 @@ PHP_FUNCTION(mcrypt_module_open)
PHP_FUNCTION(mcrypt_generic_init)
{
char *key, *iv;
- int key_len, iv_len;
+ size_t 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) {
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "rss", &mcryptind, &key, &key_len, &iv, &iv_len) == FAILURE) {
return;
}
- ZEND_FETCH_RESOURCE(pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt);
+ if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) {
+ RETURN_FALSE;
+ }
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");
+ php_error_docref(NULL, E_WARNING, "Key size is 0");
}
key_s = emalloc(key_len);
@@ -590,15 +564,15 @@ PHP_FUNCTION(mcrypt_generic_init)
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);
+ php_error_docref(NULL, 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;
+ key_size = (int)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);
+ php_error_docref(NULL, E_WARNING, "Iv size incorrect; supplied length: %d, needed: %d", iv_len, iv_size);
if (iv_len > iv_size) {
iv_len = iv_size;
}
@@ -611,17 +585,17 @@ PHP_FUNCTION(mcrypt_generic_init)
/* 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));
+ zend_list_close(Z_RES_P(mcryptind));
switch (result) {
case -3:
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key length incorrect");
+ php_error_docref(NULL, E_WARNING, "Key length incorrect");
break;
case -4:
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Memory allocation error");
+ php_error_docref(NULL, E_WARNING, "Memory allocation error");
break;
case -1:
default:
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error");
+ php_error_docref(NULL, E_WARNING, "Unknown error");
break;
}
} else {
@@ -640,42 +614,43 @@ PHP_FUNCTION(mcrypt_generic)
{
zval *mcryptind;
char *data;
- int data_len;
+ size_t data_len;
php_mcrypt *pm;
- char* data_s;
+ zend_string* data_str;
int block_size, data_size;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &mcryptind, &data, &data_len) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &mcryptind, &data, &data_len) == FAILURE) {
return;
}
-
- ZEND_FETCH_RESOURCE(pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt);
+
+ if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) {
+ RETURN_FALSE;
+ }
PHP_MCRYPT_INIT_CHECK
if (data_len == 0) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "An empty string was passed");
+ php_error_docref(NULL, 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);
+ data_size = ((((int)data_len - 1) / block_size) + 1) * block_size;
+ data_str = zend_string_alloc(data_size, 0);
+ memset(ZSTR_VAL(data_str), 0, data_size);
+ memcpy(ZSTR_VAL(data_str), 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);
+ data_size = (int)data_len;
+ data_str = zend_string_alloc(data_size, 0);
+ memset(ZSTR_VAL(data_str), 0, data_size);
+ memcpy(ZSTR_VAL(data_str), 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);
+ mcrypt_generic(pm->td, ZSTR_VAL(data_str), data_size);
+ ZSTR_VAL(data_str)[data_size] = '\0';
+
+ RETVAL_NEW_STR(data_str);
}
/* }}} */
@@ -685,40 +660,42 @@ PHP_FUNCTION(mdecrypt_generic)
{
zval *mcryptind;
char *data;
- int data_len;
+ size_t 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) {
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &mcryptind, &data, &data_len) == FAILURE) {
return;
}
-
- ZEND_FETCH_RESOURCE(pm, php_mcrypt * , &mcryptind, -1, "MCrypt", le_mcrypt);
+
+ if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) {
+ RETURN_FALSE;
+ }
PHP_MCRYPT_INIT_CHECK
if (data_len == 0) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "An empty string was passed");
+ php_error_docref(NULL, 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_size = ((((int)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_size = (int)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);
+ RETVAL_STRINGL(data_s, data_size);
efree(data_s);
}
/* }}} */
@@ -729,7 +706,7 @@ PHP_FUNCTION(mcrypt_enc_get_supported_key_sizes)
{
int i, count = 0;
int *key_sizes;
-
+
MCRYPT_GET_TD_ARG
array_init(return_value);
@@ -757,7 +734,7 @@ PHP_FUNCTION(mcrypt_enc_self_test)
PHP_FUNCTION(mcrypt_module_close)
{
MCRYPT_GET_TD_ARG
- zend_list_delete(Z_LVAL_P(mcryptind));
+ zend_list_close(Z_RES_P(mcryptind));
RETURN_TRUE;
}
/* }}} */
@@ -769,7 +746,7 @@ 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");
+ php_error_docref(NULL, E_WARNING, "Could not terminate encryption specifier");
RETURN_FALSE
}
pm->init = 0;
@@ -798,7 +775,7 @@ PHP_FUNCTION(mcrypt_enc_is_block_algorithm)
MCRYPT_GET_TD_ARG
if (mcrypt_enc_is_block_algorithm(pm->td) == 1) {
- RETURN_TRUE
+ RETURN_TRUE
} else {
RETURN_FALSE
}
@@ -854,7 +831,7 @@ PHP_FUNCTION(mcrypt_enc_get_algorithms_name)
MCRYPT_GET_TD_ARG
name = mcrypt_enc_get_algorithms_name(pm->td);
- RETVAL_STRING(name, 1);
+ RETVAL_STRING(name);
mcrypt_free(name);
}
/* }}} */
@@ -867,7 +844,7 @@ PHP_FUNCTION(mcrypt_enc_get_modes_name)
MCRYPT_GET_TD_ARG
name = mcrypt_enc_get_modes_name(pm->td);
- RETVAL_STRING(name, 1);
+ RETVAL_STRING(name);
mcrypt_free(name);
}
/* }}} */
@@ -877,7 +854,7 @@ PHP_FUNCTION(mcrypt_enc_get_modes_name)
PHP_FUNCTION(mcrypt_module_self_test)
{
MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir);
-
+
if (mcrypt_module_self_test(module, dir) == 0) {
RETURN_TRUE;
} else {
@@ -891,7 +868,7 @@ PHP_FUNCTION(mcrypt_module_self_test)
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 {
@@ -905,7 +882,7 @@ PHP_FUNCTION(mcrypt_module_is_block_algorithm_mode)
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 {
@@ -919,7 +896,7 @@ PHP_FUNCTION(mcrypt_module_is_block_algorithm)
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 {
@@ -933,7 +910,7 @@ PHP_FUNCTION(mcrypt_module_is_block_mode)
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));
}
/* }}} */
@@ -943,7 +920,7 @@ PHP_FUNCTION(mcrypt_module_get_algo_block_size)
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));
}
/* }}} */
@@ -954,7 +931,7 @@ 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);
@@ -973,22 +950,22 @@ PHP_FUNCTION(mcrypt_list_algorithms)
{
char **modules;
char *lib_dir = MCG(algorithms_dir);
- int lib_dir_len;
+ size_t lib_dir_len;
int i, count;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s",
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "|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");
+ php_error_docref(NULL, 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_string(return_value, i, modules[i]);
}
mcrypt_free_p(modules, count);
}
@@ -1000,10 +977,10 @@ PHP_FUNCTION(mcrypt_list_modes)
{
char **modules;
char *lib_dir = MCG(modes_dir);
- int lib_dir_len;
+ size_t lib_dir_len;
int i, count;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s",
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s",
&lib_dir, &lib_dir_len) == FAILURE) {
return;
}
@@ -1012,10 +989,10 @@ PHP_FUNCTION(mcrypt_list_modes)
modules = mcrypt_list_modes(lib_dir, &count);
if (count == 0) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "No modes found in module dir");
+ php_error_docref(NULL, 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_string(return_value, i, modules[i]);
}
mcrypt_free_p(modules, count);
}
@@ -1027,24 +1004,24 @@ PHP_FUNCTION(mcrypt_get_key_size)
{
char *cipher;
char *module;
- int cipher_len, module_len;
+ size_t 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",
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "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);
+ php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
RETURN_FALSE;
}
}
@@ -1056,24 +1033,24 @@ PHP_FUNCTION(mcrypt_get_block_size)
{
char *cipher;
char *module;
- int cipher_len, module_len;
+ size_t 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",
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "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);
+ php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
RETURN_FALSE;
}
}
@@ -1085,24 +1062,24 @@ PHP_FUNCTION(mcrypt_get_iv_size)
{
char *cipher;
char *module;
- int cipher_len, module_len;
+ size_t 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",
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "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);
+ php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
RETURN_FALSE;
}
}
@@ -1116,12 +1093,12 @@ PHP_FUNCTION(mcrypt_get_cipher_name)
char *module_dir_string;
char *cipher_name;
char *cipher;
- int cipher_len;
+ size_t cipher_len;
MCRYPT td;
MCRYPT_GET_INI
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
&cipher, &cipher_len) == FAILURE) {
return;
}
@@ -1132,17 +1109,17 @@ PHP_FUNCTION(mcrypt_get_cipher_name)
if (td != MCRYPT_FAILED) {
cipher_name = mcrypt_enc_get_algorithms_name(td);
mcrypt_module_close(td);
- RETVAL_STRING(cipher_name,1);
+ RETVAL_STRING(cipher_name);
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_STRING(cipher_name);
mcrypt_free(cipher_name);
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
+ php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
RETURN_FALSE;
}
}
@@ -1162,6 +1139,7 @@ static char *php_mcrypt_get_key_size_str(
return str;
} else {
int i;
+ char *result = NULL;
smart_str str = {0};
smart_str_appends(&str, "Only keys of sizes ");
@@ -1177,7 +1155,10 @@ static char *php_mcrypt_get_key_size_str(
smart_str_appends(&str, " supported");
smart_str_0(&str);
- return str.c;
+ result = estrndup(ZSTR_VAL(str.s), ZSTR_LEN(str.s));
+ smart_str_free(&str);
+
+ return result;
}
}
/* }}} */
@@ -1206,7 +1187,7 @@ static zend_bool php_mcrypt_is_valid_key_size(
}
/* }}} */
-static int php_mcrypt_ensure_valid_key_size(MCRYPT td, int key_size TSRMLS_DC) /* {{{ */
+static int php_mcrypt_ensure_valid_key_size(MCRYPT td, int key_size) /* {{{ */
{
int key_size_count;
int max_key_size = mcrypt_enc_get_key_size(td);
@@ -1219,7 +1200,7 @@ static int php_mcrypt_ensure_valid_key_size(MCRYPT td, int key_size TSRMLS_DC) /
char *key_size_str = php_mcrypt_get_key_size_str(
max_key_size, key_sizes, key_size_count
);
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ php_error_docref(NULL, E_WARNING,
"Key of size %d not supported by this algorithm. %s", key_size, key_size_str
);
efree(key_size_str);
@@ -1233,7 +1214,7 @@ static int php_mcrypt_ensure_valid_key_size(MCRYPT td, int key_size TSRMLS_DC) /
}
/* }}} */
-static int php_mcrypt_ensure_valid_iv(MCRYPT td, const char *iv, int iv_size TSRMLS_DC) /* {{{ */
+static int php_mcrypt_ensure_valid_iv(MCRYPT td, const char *iv, int iv_size) /* {{{ */
{
if (mcrypt_enc_mode_has_iv(td) == 1) {
int expected_iv_size = mcrypt_enc_get_iv_size(td);
@@ -1243,14 +1224,14 @@ static int php_mcrypt_ensure_valid_iv(MCRYPT td, const char *iv, int iv_size TSR
}
if (!iv) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ php_error_docref(NULL, E_WARNING,
"Encryption mode requires an initialization vector of size %d", expected_iv_size
);
return FAILURE;
}
if (iv_size != expected_iv_size) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ php_error_docref(NULL, E_WARNING,
"Received initialization vector of size %d, but size %d is required "
"for this encryption mode", iv_size, expected_iv_size
);
@@ -1262,11 +1243,11 @@ static int php_mcrypt_ensure_valid_iv(MCRYPT td, const char *iv, int iv_size TSR
}
/* }}} */
-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 dencrypt, zval* return_value TSRMLS_DC) /* {{{ */
+static void php_mcrypt_do_crypt(char* cipher, const char *key, size_t key_len, const char *data, size_t data_len, char *mode, const char *iv, size_t iv_len, size_t dencrypt, zval* return_value) /* {{{ */
{
char *cipher_dir_string;
char *module_dir_string;
- unsigned long int data_size;
+ zend_long data_size;
char *data_s;
MCRYPT td;
@@ -1274,16 +1255,16 @@ static void php_mcrypt_do_crypt(char* cipher, const char *key, int key_len, cons
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);
+ php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
RETURN_FALSE;
}
- if (php_mcrypt_ensure_valid_key_size(td, key_len TSRMLS_CC) == FAILURE) {
+ if (php_mcrypt_ensure_valid_key_size(td, (int)key_len) == FAILURE) {
mcrypt_module_close(td);
RETURN_FALSE;
}
- if (php_mcrypt_ensure_valid_iv(td, iv, iv_len TSRMLS_CC) == FAILURE) {
+ if (php_mcrypt_ensure_valid_iv(td, iv, (int)iv_len) == FAILURE) {
mcrypt_module_close(td);
RETURN_FALSE;
}
@@ -1291,7 +1272,7 @@ static void php_mcrypt_do_crypt(char* cipher, const char *key, int key_len, cons
/* Check blocksize */
if (mcrypt_enc_is_block_mode(td) == 1) { /* It's a block algorithm */
int block_size = mcrypt_enc_get_block_size(td);
- data_size = (((data_len - 1) / block_size) + 1) * block_size;
+ data_size = ((((zend_long)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);
@@ -1301,20 +1282,22 @@ static void php_mcrypt_do_crypt(char* cipher, const char *key, int key_len, cons
memcpy(data_s, data, data_len);
}
- if (mcrypt_generic_init(td, (void *) key, key_len, (void *) iv) < 0) {
- php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Mcrypt initialisation failed");
+ if (mcrypt_generic_init(td, (void *) key, (int)key_len, (void *) iv) < 0) {
+ php_error_docref(NULL, E_RECOVERABLE_ERROR, "Mcrypt initialisation failed");
mcrypt_module_close(td);
RETURN_FALSE;
}
if (dencrypt == MCRYPT_ENCRYPT) {
- mcrypt_generic(td, data_s, data_size);
+ mcrypt_generic(td, data_s, (int)data_size);
} else {
- mdecrypt_generic(td, data_s, data_size);
+ mdecrypt_generic(td, data_s, (int)data_size);
}
-
+
data_s[data_size] = 0;
- RETVAL_STRINGL(data_s, data_size, 0);
+
+ RETVAL_STRINGL(data_s, data_size);
+ efree(data_s);
/* freeing vars */
mcrypt_generic_end(td);
@@ -1326,14 +1309,14 @@ static void php_mcrypt_do_crypt(char* cipher, const char *key, int key_len, cons
PHP_FUNCTION(mcrypt_encrypt)
{
char *cipher, *key, *data, *mode, *iv = NULL;
- int cipher_len, key_len, data_len, mode_len, iv_len = 0;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss|s", &cipher, &cipher_len,
+ size_t cipher_len, key_len, data_len, mode_len, iv_len = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssss|s", &cipher, &cipher_len,
&key, &key_len, &data, &data_len, &mode, &mode_len, &iv, &iv_len) == FAILURE) {
return;
}
- php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, mode, iv, iv_len, MCRYPT_ENCRYPT, return_value TSRMLS_CC);
+ php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, mode, iv, iv_len, MCRYPT_ENCRYPT, return_value);
}
/* }}} */
@@ -1342,78 +1325,14 @@ PHP_FUNCTION(mcrypt_encrypt)
PHP_FUNCTION(mcrypt_decrypt)
{
char *cipher, *key, *data, *mode, *iv = NULL;
- int cipher_len, key_len, data_len, mode_len, iv_len = 0;
+ size_t cipher_len, key_len, data_len, mode_len, iv_len = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss|s", &cipher, &cipher_len,
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssss|s", &cipher, &cipher_len,
&key, &key_len, &data, &data_len, &mode, &mode_len, &iv, &iv_len) == FAILURE) {
return;
}
- php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, mode, iv, iv_len, 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, 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, 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, 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, Z_LVAL_PP(mode), return_value TSRMLS_CC);
+ php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, mode, iv, iv_len, MCRYPT_DECRYPT, return_value);
}
/* }}} */
@@ -1422,31 +1341,31 @@ PHP_FUNCTION(mcrypt_ofb)
PHP_FUNCTION(mcrypt_create_iv)
{
char *iv;
- long source = URANDOM;
- long size;
+ zend_long source = URANDOM;
+ zend_long size;
int n = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &size, &source) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "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);
+ php_error_docref(NULL, 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");
+ php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
RETURN_FALSE;
}
- n = size;
+ n = (int)size;
#else
int *fd = &MCG(fd[source]);
size_t read_bytes = 0;
@@ -1455,14 +1374,14 @@ PHP_FUNCTION(mcrypt_create_iv)
*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");
+ php_error_docref(NULL, E_WARNING, "Cannot open source device");
RETURN_FALSE;
}
}
while (read_bytes < size) {
n = read(*fd, iv + read_bytes, size - read_bytes);
- if (n < 0) {
+ if (n <= 0) {
break;
}
read_bytes += n;
@@ -1471,17 +1390,18 @@ PHP_FUNCTION(mcrypt_create_iv)
if (n < size) {
efree(iv);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random data");
+ php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
RETURN_FALSE;
}
#endif
} else {
- n = size;
+ n = (int)size;
while (size) {
- iv[--size] = (char) (255.0 * php_rand(TSRMLS_C) / RAND_MAX);
+ iv[--size] = (char) (255.0 * php_rand() / RAND_MAX);
}
}
- RETURN_STRINGL(iv, n, 0);
+ RETVAL_STRINGL(iv, n);
+ efree(iv);
}
/* }}} */