1999-04-25 17:04:56 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2014-09-19 16:33:14 +00:00
|
|
|
| PHP Version 7 |
|
1999-04-25 17:04:56 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2016-01-01 17:19:27 +00:00
|
|
|
| Copyright (c) 1997-2016 The PHP Group |
|
1999-04-25 17:04:56 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 12:51:34 +00:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
1999-07-16 13:13:16 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-10 20:04:29 +00:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 12:51:34 +00:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
1999-07-16 13:13:16 +00:00
|
|
|
| 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. |
|
1999-04-25 17:04:56 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2000-07-10 10:09:15 +00:00
|
|
|
| Authors: Sascha Schumann <sascha@schumann.cx> |
|
2003-10-13 09:33:00 +00:00
|
|
|
| Derick Rethans <derick@derickrethans.nl> |
|
1999-04-25 17:04:56 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
2001-12-20 16:50:49 +00:00
|
|
|
/* $Id$ */
|
1999-04-25 16:50:40 +00:00
|
|
|
|
2001-05-24 10:07:29 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
1999-04-25 16:50:40 +00:00
|
|
|
#include "php.h"
|
|
|
|
|
|
|
|
#if HAVE_LIBMCRYPT
|
|
|
|
|
2008-07-15 17:05:02 +00:00
|
|
|
#if PHP_WIN32
|
2010-06-08 18:27:23 +00:00
|
|
|
# include "win32/winutil.h"
|
2008-07-15 17:05:02 +00:00
|
|
|
#endif
|
|
|
|
|
1999-04-25 16:50:40 +00:00
|
|
|
#include "php_mcrypt.h"
|
1999-05-01 11:14:22 +00:00
|
|
|
#include "fcntl.h"
|
1999-04-25 16:50:40 +00:00
|
|
|
|
1999-05-01 11:14:22 +00:00
|
|
|
#define NON_FREE
|
1999-05-21 08:20:13 +00:00
|
|
|
#define MCRYPT2
|
1999-05-01 11:14:22 +00:00
|
|
|
#include "mcrypt.h"
|
2000-08-11 18:45:17 +00:00
|
|
|
#include "php_ini.h"
|
2000-08-13 11:53:04 +00:00
|
|
|
#include "php_globals.h"
|
2000-08-11 18:45:17 +00:00
|
|
|
#include "ext/standard/info.h"
|
2007-04-05 01:48:56 +00:00
|
|
|
#include "ext/standard/php_rand.h"
|
2014-09-20 20:42:02 +00:00
|
|
|
#include "zend_smart_str.h"
|
2010-01-15 21:02:20 +00:00
|
|
|
#include "php_mcrypt_filter.h"
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2001-09-07 10:29:06 +00:00
|
|
|
static int le_mcrypt;
|
|
|
|
|
2015-01-03 09:22:58 +00:00
|
|
|
typedef struct _php_mcrypt {
|
2005-11-30 23:51:25 +00:00
|
|
|
MCRYPT td;
|
|
|
|
zend_bool init;
|
|
|
|
} php_mcrypt;
|
|
|
|
|
2008-07-01 19:43:42 +00:00
|
|
|
/* {{{ 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()
|
|
|
|
|
2014-03-11 12:42:16 +00:00
|
|
|
ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_create_iv, 0, 0, 1)
|
2008-07-01 19:43:42 +00:00
|
|
|
ZEND_ARG_INFO(0, size)
|
|
|
|
ZEND_ARG_INFO(0, source)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
/* }}} */
|
|
|
|
|
2007-09-27 18:00:48 +00:00
|
|
|
const zend_function_entry mcrypt_functions[] = { /* {{{ */
|
2008-07-01 19:43:42 +00:00
|
|
|
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_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)
|
2011-07-25 11:35:02 +00:00
|
|
|
PHP_FE_END
|
1999-04-25 16:50:40 +00:00
|
|
|
};
|
2007-07-19 14:32:55 +00:00
|
|
|
/* }}} */
|
1999-04-25 16:50:40 +00:00
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
static PHP_MINFO_FUNCTION(mcrypt);
|
1999-08-02 15:02:52 +00:00
|
|
|
static PHP_MINIT_FUNCTION(mcrypt);
|
2000-08-11 18:45:17 +00:00
|
|
|
static PHP_MSHUTDOWN_FUNCTION(mcrypt);
|
2015-08-09 16:40:29 +00:00
|
|
|
static PHP_GINIT_FUNCTION(mcrypt);
|
|
|
|
static PHP_GSHUTDOWN_FUNCTION(mcrypt);
|
1999-04-25 16:50:40 +00:00
|
|
|
|
2006-06-15 18:33:09 +00:00
|
|
|
ZEND_DECLARE_MODULE_GLOBALS(mcrypt)
|
|
|
|
|
1999-04-25 16:50:40 +00:00
|
|
|
zend_module_entry mcrypt_module_entry = {
|
2003-01-03 19:49:44 +00:00
|
|
|
STANDARD_MODULE_HEADER,
|
2015-01-03 09:22:58 +00:00
|
|
|
"mcrypt",
|
1999-04-25 16:50:40 +00:00
|
|
|
mcrypt_functions,
|
2000-08-11 18:45:17 +00:00
|
|
|
PHP_MINIT(mcrypt), PHP_MSHUTDOWN(mcrypt),
|
1999-04-25 16:50:40 +00:00
|
|
|
NULL, NULL,
|
2000-08-11 18:45:17 +00:00
|
|
|
PHP_MINFO(mcrypt),
|
2015-03-23 19:33:54 +00:00
|
|
|
PHP_MCRYPT_VERSION,
|
2006-06-15 18:33:09 +00:00
|
|
|
PHP_MODULE_GLOBALS(mcrypt),
|
2015-08-09 16:40:29 +00:00
|
|
|
PHP_GINIT(mcrypt),
|
|
|
|
PHP_GSHUTDOWN(mcrypt),
|
2006-06-15 18:33:09 +00:00
|
|
|
NULL,
|
|
|
|
STANDARD_MODULE_PROPERTIES_EX
|
1999-04-25 16:50:40 +00:00
|
|
|
};
|
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
#ifdef COMPILE_DL_MCRYPT
|
2015-08-11 10:32:00 +00:00
|
|
|
#ifdef ZTS
|
|
|
|
ZEND_TSRMLS_CACHE_DEFINE();
|
|
|
|
#endif
|
2000-08-11 18:45:17 +00:00
|
|
|
ZEND_GET_MODULE(mcrypt)
|
1999-04-25 16:50:40 +00:00
|
|
|
#endif
|
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
#define MCRYPT_ENCRYPT 0
|
|
|
|
#define MCRYPT_DECRYPT 1
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2014-03-11 12:42:16 +00:00
|
|
|
typedef enum {
|
|
|
|
RANDOM = 0,
|
|
|
|
URANDOM,
|
|
|
|
RAND
|
|
|
|
} iv_source;
|
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
#define MCRYPT_GET_INI \
|
2000-08-13 11:53:04 +00:00
|
|
|
cipher_dir_string = MCG(algorithms_dir); \
|
|
|
|
module_dir_string = MCG(modes_dir);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
/*
|
|
|
|
* #warning is not ANSI C
|
|
|
|
* #warning Invalidate resource if the param count is wrong, or other problems
|
|
|
|
* #warning occurred during functions.
|
|
|
|
*/
|
|
|
|
|
2008-08-10 05:38:07 +00:00
|
|
|
#define MCRYPT_GET_CRYPT_ARGS \
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssz|s", \
|
2008-08-10 05:38:07 +00:00
|
|
|
&cipher, &cipher_len, &key, &key_len, &data, &data_len, &mode, &iv, &iv_len) == FAILURE) { \
|
|
|
|
return; \
|
2008-07-04 07:47:18 +00:00
|
|
|
}
|
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
#define MCRYPT_GET_TD_ARG \
|
2008-08-10 05:38:07 +00:00
|
|
|
zval *mcryptind; \
|
2008-07-04 07:47:18 +00:00
|
|
|
php_mcrypt *pm; \
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &mcryptind) == FAILURE) { \
|
2008-08-10 05:38:07 +00:00
|
|
|
return; \
|
2008-07-04 07:47:18 +00:00
|
|
|
} \
|
2015-02-03 03:58:11 +00:00
|
|
|
if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) { \
|
|
|
|
RETURN_FALSE; \
|
|
|
|
}
|
2000-08-11 18:45:17 +00:00
|
|
|
|
|
|
|
#define MCRYPT_GET_MODE_DIR_ARGS(DIRECTORY) \
|
2001-12-26 10:01:50 +00:00
|
|
|
char *dir = NULL; \
|
2014-08-27 17:25:28 +00:00
|
|
|
size_t dir_len; \
|
2001-12-26 10:01:50 +00:00
|
|
|
char *module; \
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t module_len; \
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters (ZEND_NUM_ARGS(), \
|
2001-12-26 10:01:50 +00:00
|
|
|
"s|s", &module, &module_len, &dir, &dir_len) == FAILURE) { \
|
|
|
|
return; \
|
|
|
|
}
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
#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) { \
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Operation disallowed prior to mcrypt_generic_init()."); \
|
2008-07-04 07:47:18 +00:00
|
|
|
RETURN_FALSE; \
|
|
|
|
} \
|
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
PHP_INI_BEGIN()
|
2004-04-18 14:04:48 +00:00
|
|
|
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)
|
2000-08-11 18:45:17 +00:00
|
|
|
PHP_INI_END()
|
1999-04-25 16:50:40 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static void php_mcrypt_module_dtor(zend_resource *rsrc) /* {{{ */
|
2001-09-07 10:25:26 +00:00
|
|
|
{
|
2005-11-30 23:51:25 +00:00
|
|
|
php_mcrypt *pm = (php_mcrypt *) rsrc->ptr;
|
2015-01-03 09:22:58 +00:00
|
|
|
if (pm) {
|
2005-11-30 23:51:25 +00:00
|
|
|
mcrypt_generic_deinit(pm->td);
|
|
|
|
mcrypt_module_close(pm->td);
|
|
|
|
efree(pm);
|
2008-07-04 07:47:18 +00:00
|
|
|
pm = NULL;
|
2005-11-30 23:51:25 +00:00
|
|
|
}
|
2001-09-07 10:25:26 +00:00
|
|
|
}
|
2007-07-19 14:32:55 +00:00
|
|
|
/* }}} */
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2015-08-09 16:40:29 +00:00
|
|
|
static PHP_GINIT_FUNCTION(mcrypt)
|
2015-08-11 10:11:24 +00:00
|
|
|
{/*{{{*/
|
2015-08-11 10:32:00 +00:00
|
|
|
#if defined(COMPILE_DL_MCRYPT) && defined(ZTS)
|
|
|
|
ZEND_TSRMLS_CACHE_UPDATE();
|
|
|
|
#endif
|
2015-08-09 16:40:29 +00:00
|
|
|
mcrypt_globals->fd[RANDOM] = -1;
|
|
|
|
mcrypt_globals->fd[URANDOM] = -1;
|
2015-08-11 10:11:24 +00:00
|
|
|
}/*}}}*/
|
2015-08-09 16:40:29 +00:00
|
|
|
|
|
|
|
static PHP_GSHUTDOWN_FUNCTION(mcrypt)
|
2015-08-11 10:11:24 +00:00
|
|
|
{/*{{{*/
|
2015-08-09 16:54:20 +00:00
|
|
|
if (mcrypt_globals->fd[RANDOM] > 0) {
|
2015-08-09 16:40:29 +00:00
|
|
|
close(mcrypt_globals->fd[RANDOM]);
|
|
|
|
mcrypt_globals->fd[RANDOM] = -1;
|
|
|
|
}
|
|
|
|
|
2015-08-09 16:54:20 +00:00
|
|
|
if (mcrypt_globals->fd[URANDOM] > 0) {
|
2015-08-09 16:40:29 +00:00
|
|
|
close(mcrypt_globals->fd[URANDOM]);
|
|
|
|
mcrypt_globals->fd[URANDOM] = -1;
|
|
|
|
}
|
2015-08-11 10:11:24 +00:00
|
|
|
}/*}}}*/
|
2015-08-09 16:40:29 +00:00
|
|
|
|
2007-07-19 14:32:55 +00:00
|
|
|
static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
|
1999-04-25 16:50:40 +00:00
|
|
|
{
|
2001-09-07 10:29:06 +00:00
|
|
|
le_mcrypt = zend_register_list_destructors_ex(php_mcrypt_module_dtor, NULL, "mcrypt", module_number);
|
2010-06-02 10:07:26 +00:00
|
|
|
|
1999-04-25 21:09:07 +00:00
|
|
|
/* modes for mcrypt_??? routines */
|
2014-08-25 17:24:55 +00:00
|
|
|
REGISTER_LONG_CONSTANT("MCRYPT_ENCRYPT", 0, CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MCRYPT_DECRYPT", 1, CONST_PERSISTENT);
|
2010-06-02 10:07:26 +00:00
|
|
|
|
1999-04-25 21:09:07 +00:00
|
|
|
/* sources for mcrypt_create_iv */
|
2014-08-25 17:24:55 +00:00
|
|
|
REGISTER_LONG_CONSTANT("MCRYPT_DEV_RANDOM", RANDOM, CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MCRYPT_DEV_URANDOM", URANDOM, CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("MCRYPT_RAND", RAND, CONST_PERSISTENT);
|
2010-06-02 10:07:26 +00:00
|
|
|
|
1999-04-25 21:09:07 +00:00
|
|
|
/* ciphers */
|
2001-09-23 14:14:27 +00:00
|
|
|
MCRYPT_ENTRY2_2_4(3DES, "tripledes");
|
2000-08-11 18:45:17 +00:00
|
|
|
MCRYPT_ENTRY2_2_4(ARCFOUR_IV, "arcfour-iv");
|
|
|
|
MCRYPT_ENTRY2_2_4(ARCFOUR, "arcfour");
|
|
|
|
MCRYPT_ENTRY2_2_4(BLOWFISH, "blowfish");
|
2001-01-13 20:55:15 +00:00
|
|
|
MCRYPT_ENTRY2_2_4(BLOWFISH_COMPAT, "blowfish-compat");
|
2000-08-11 18:45:17 +00:00
|
|
|
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");
|
2008-07-04 07:47:18 +00:00
|
|
|
MCRYPT_ENTRY2_2_4(ENIGNA, "crypt");
|
2000-08-11 18:45:17 +00:00
|
|
|
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();
|
2010-01-15 21:02:20 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
php_stream_filter_register_factory("mcrypt.*", &php_mcrypt_filter_factory);
|
|
|
|
php_stream_filter_register_factory("mdecrypt.*", &php_mcrypt_filter_factory);
|
2010-01-15 21:02:20 +00:00
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2007-07-19 14:32:55 +00:00
|
|
|
/* }}} */
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2007-07-19 14:32:55 +00:00
|
|
|
static PHP_MSHUTDOWN_FUNCTION(mcrypt) /* {{{ */
|
2000-08-11 18:45:17 +00:00
|
|
|
{
|
2014-12-13 22:06:14 +00:00
|
|
|
php_stream_filter_unregister_factory("mcrypt.*");
|
|
|
|
php_stream_filter_unregister_factory("mdecrypt.*");
|
2010-01-15 21:02:20 +00:00
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
UNREGISTER_INI_ENTRIES();
|
1999-04-25 16:50:40 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2007-07-19 14:32:55 +00:00
|
|
|
/* }}} */
|
1999-04-25 20:32:15 +00:00
|
|
|
|
2014-09-20 20:42:02 +00:00
|
|
|
#include "zend_smart_str.h"
|
2001-04-10 00:07:53 +00:00
|
|
|
|
2007-07-19 14:32:55 +00:00
|
|
|
PHP_MINFO_FUNCTION(mcrypt) /* {{{ */
|
2000-08-11 18:45:17 +00:00
|
|
|
{
|
|
|
|
char **modules;
|
2003-01-03 19:49:44 +00:00
|
|
|
char mcrypt_api_no[16];
|
2000-08-11 18:45:17 +00:00
|
|
|
int i, count;
|
2001-04-10 00:07:53 +00:00
|
|
|
smart_str tmp1 = {0};
|
|
|
|
smart_str tmp2 = {0};
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2003-03-13 21:09:27 +00:00
|
|
|
modules = mcrypt_list_algorithms(MCG(algorithms_dir), &count);
|
2000-08-11 18:45:17 +00:00
|
|
|
if (count == 0) {
|
2003-03-13 21:09:27 +00:00
|
|
|
smart_str_appends(&tmp1, "none");
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < count; i++) {
|
2003-03-13 21:09:27 +00:00
|
|
|
smart_str_appends(&tmp1, modules[i]);
|
|
|
|
smart_str_appendc(&tmp1, ' ');
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2003-03-13 21:09:27 +00:00
|
|
|
smart_str_0(&tmp1);
|
|
|
|
mcrypt_free_p(modules, count);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2003-03-13 21:09:27 +00:00
|
|
|
modules = mcrypt_list_modes(MCG(modes_dir), &count);
|
2000-08-11 18:45:17 +00:00
|
|
|
if (count == 0) {
|
2003-03-13 21:09:27 +00:00
|
|
|
smart_str_appends(&tmp2, "none");
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < count; i++) {
|
2003-03-13 21:09:27 +00:00
|
|
|
smart_str_appends(&tmp2, modules[i]);
|
|
|
|
smart_str_appendc(&tmp2, ' ');
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2001-04-10 00:07:53 +00:00
|
|
|
smart_str_0 (&tmp2);
|
2000-08-11 18:45:17 +00:00
|
|
|
mcrypt_free_p (modules, count);
|
2003-01-03 19:49:44 +00:00
|
|
|
|
|
|
|
snprintf (mcrypt_api_no, 16, "%d", MCRYPT_API_VERSION);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
|
|
|
php_info_print_table_start();
|
|
|
|
php_info_print_table_header(2, "mcrypt support", "enabled");
|
2010-01-15 21:02:20 +00:00
|
|
|
php_info_print_table_header(2, "mcrypt_filter support", "enabled");
|
2003-01-03 19:49:44 +00:00
|
|
|
php_info_print_table_row(2, "Version", LIBMCRYPT_VERSION);
|
|
|
|
php_info_print_table_row(2, "Api No", mcrypt_api_no);
|
2015-06-30 01:05:24 +00:00
|
|
|
php_info_print_table_row(2, "Supported ciphers", ZSTR_VAL(tmp1.s));
|
|
|
|
php_info_print_table_row(2, "Supported modes", ZSTR_VAL(tmp2.s));
|
2003-03-13 21:09:27 +00:00
|
|
|
smart_str_free(&tmp1);
|
|
|
|
smart_str_free(&tmp2);
|
2014-05-06 10:20:54 +00:00
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
php_info_print_table_end();
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
DISPLAY_INI_ENTRIES();
|
|
|
|
}
|
2007-07-19 14:32:55 +00:00
|
|
|
/* }}} */
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
/* {{{ proto resource mcrypt_module_open(string cipher, string cipher_directory, string mode, string mode_directory)
|
2000-08-11 18:45:17 +00:00
|
|
|
Opens the module of the algorithm and the mode to be used */
|
|
|
|
PHP_FUNCTION(mcrypt_module_open)
|
|
|
|
{
|
2002-06-16 18:20:21 +00:00
|
|
|
char *cipher, *cipher_dir;
|
|
|
|
char *mode, *mode_dir;
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t cipher_len, cipher_dir_len;
|
|
|
|
size_t mode_len, mode_dir_len;
|
2000-08-11 18:45:17 +00:00
|
|
|
MCRYPT td;
|
2005-11-30 23:51:25 +00:00
|
|
|
php_mcrypt *pm;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters (ZEND_NUM_ARGS(), "ssss",
|
2002-06-16 18:20:21 +00:00
|
|
|
&cipher, &cipher_len, &cipher_dir, &cipher_dir_len,
|
|
|
|
&mode, &mode_len, &mode_dir, &mode_dir_len)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
td = mcrypt_module_open (
|
|
|
|
cipher,
|
2002-06-16 18:20:21 +00:00
|
|
|
cipher_dir_len > 0 ? cipher_dir : MCG(algorithms_dir),
|
2015-01-03 09:22:58 +00:00
|
|
|
mode,
|
2003-01-03 19:49:44 +00:00
|
|
|
mode_dir_len > 0 ? mode_dir : MCG(modes_dir)
|
|
|
|
);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
|
|
|
if (td == MCRYPT_FAILED) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Could not open encryption module");
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
} else {
|
2005-11-30 23:51:25 +00:00
|
|
|
pm = emalloc(sizeof(php_mcrypt));
|
|
|
|
pm->td = td;
|
|
|
|
pm->init = 0;
|
2015-02-03 03:58:11 +00:00
|
|
|
RETURN_RES(zend_register_resource(pm, le_mcrypt));
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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)
|
|
|
|
{
|
2008-08-10 05:38:07 +00:00
|
|
|
char *key, *iv;
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t key_len, iv_len;
|
2008-08-10 05:38:07 +00:00
|
|
|
zval *mcryptind;
|
2008-07-04 07:47:18 +00:00
|
|
|
unsigned char *key_s, *iv_s;
|
|
|
|
int max_key_size, key_size, iv_size;
|
2005-11-30 23:51:25 +00:00
|
|
|
php_mcrypt *pm;
|
2001-12-15 13:17:55 +00:00
|
|
|
int result = 0;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rss", &mcryptind, &key, &key_len, &iv, &iv_len) == FAILURE) {
|
2008-08-10 05:38:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-03 03:58:11 +00:00
|
|
|
if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2005-11-30 23:51:25 +00:00
|
|
|
max_key_size = mcrypt_enc_get_key_size(pm->td);
|
2008-07-04 07:47:18 +00:00
|
|
|
iv_size = mcrypt_enc_get_iv_size(pm->td);
|
2001-04-10 00:07:53 +00:00
|
|
|
|
2008-08-10 05:38:07 +00:00
|
|
|
if (key_len == 0) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Key size is 0");
|
2001-09-07 07:31:39 +00:00
|
|
|
}
|
|
|
|
|
2008-08-10 05:38:07 +00:00
|
|
|
key_s = emalloc(key_len);
|
|
|
|
memset(key_s, 0, key_len);
|
2008-07-04 07:47:18 +00:00
|
|
|
|
|
|
|
iv_s = emalloc(iv_size + 1);
|
|
|
|
memset(iv_s, 0, iv_size + 1);
|
|
|
|
|
2008-08-10 05:38:07 +00:00
|
|
|
if (key_len > max_key_size) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Key size too large; supplied length: %d, max: %d", key_len, max_key_size);
|
2001-05-18 20:54:15 +00:00
|
|
|
key_size = max_key_size;
|
2008-07-04 07:47:18 +00:00
|
|
|
} else {
|
2014-10-31 17:35:55 +00:00
|
|
|
key_size = (int)key_len;
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2008-08-10 05:38:07 +00:00
|
|
|
memcpy(key_s, key, key_len);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2008-08-10 05:38:07 +00:00
|
|
|
if (iv_len != iv_size) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Iv size incorrect; supplied length: %d, needed: %d", iv_len, iv_size);
|
2014-12-08 09:18:27 +00:00
|
|
|
if (iv_len > iv_size) {
|
|
|
|
iv_len = iv_size;
|
|
|
|
}
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2014-12-08 09:18:27 +00:00
|
|
|
memcpy(iv_s, iv, iv_len);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2005-11-30 23:51:25 +00:00
|
|
|
mcrypt_generic_deinit(pm->td);
|
2008-07-04 07:47:18 +00:00
|
|
|
result = mcrypt_generic_init(pm->td, key_s, key_size, iv_s);
|
2001-12-15 13:17:55 +00:00
|
|
|
|
|
|
|
/* If this function fails, close the mcrypt module to prevent crashes
|
|
|
|
* when further functions want to access this resource */
|
|
|
|
if (result < 0) {
|
2014-05-13 09:18:00 +00:00
|
|
|
zend_list_close(Z_RES_P(mcryptind));
|
2001-12-15 13:17:55 +00:00
|
|
|
switch (result) {
|
|
|
|
case -3:
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Key length incorrect");
|
2001-12-15 13:17:55 +00:00
|
|
|
break;
|
|
|
|
case -4:
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Memory allocation error");
|
2001-12-15 13:17:55 +00:00
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
default:
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Unknown error");
|
2001-12-15 13:17:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-12-08 09:18:27 +00:00
|
|
|
} else {
|
|
|
|
pm->init = 1;
|
2001-12-15 13:17:55 +00:00
|
|
|
}
|
2014-08-25 17:24:55 +00:00
|
|
|
RETVAL_LONG(result);
|
2008-07-04 07:47:18 +00:00
|
|
|
|
|
|
|
efree(iv_s);
|
|
|
|
efree(key_s);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto string mcrypt_generic(resource td, string data)
|
|
|
|
This function encrypts the plaintext */
|
|
|
|
PHP_FUNCTION(mcrypt_generic)
|
|
|
|
{
|
2008-08-10 05:38:07 +00:00
|
|
|
zval *mcryptind;
|
|
|
|
char *data;
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t data_len;
|
2005-11-30 23:51:25 +00:00
|
|
|
php_mcrypt *pm;
|
2015-07-01 11:12:46 +00:00
|
|
|
zend_string* data_str;
|
2008-07-04 07:47:18 +00:00
|
|
|
int block_size, data_size;
|
2008-08-10 05:38:07 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &mcryptind, &data, &data_len) == FAILURE) {
|
2008-08-10 05:38:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2015-02-03 03:58:11 +00:00
|
|
|
if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2008-07-04 07:47:18 +00:00
|
|
|
PHP_MCRYPT_INIT_CHECK
|
2008-08-10 05:38:07 +00:00
|
|
|
|
|
|
|
if (data_len == 0) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "An empty string was passed");
|
2006-05-31 20:09:07 +00:00
|
|
|
RETURN_FALSE
|
|
|
|
}
|
2000-08-11 18:45:17 +00:00
|
|
|
|
|
|
|
/* Check blocksize */
|
2008-07-04 07:47:18 +00:00
|
|
|
if (mcrypt_enc_is_block_mode(pm->td) == 1) { /* It's a block algorithm */
|
2005-11-30 23:51:25 +00:00
|
|
|
block_size = mcrypt_enc_get_block_size(pm->td);
|
2014-10-31 17:35:55 +00:00
|
|
|
data_size = ((((int)data_len - 1) / block_size) + 1) * block_size;
|
2015-07-01 11:12:46 +00:00
|
|
|
data_str = zend_string_alloc(data_size, 0);
|
|
|
|
memset(ZSTR_VAL(data_str), 0, data_size);
|
|
|
|
memcpy(ZSTR_VAL(data_str), data, data_len);
|
2008-07-04 07:47:18 +00:00
|
|
|
} else { /* It's not a block algorithm */
|
2014-10-31 17:35:55 +00:00
|
|
|
data_size = (int)data_len;
|
2015-07-01 11:12:46 +00:00
|
|
|
data_str = zend_string_alloc(data_size, 0);
|
|
|
|
memset(ZSTR_VAL(data_str), 0, data_size);
|
|
|
|
memcpy(ZSTR_VAL(data_str), data, data_len);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2015-07-01 11:12:46 +00:00
|
|
|
mcrypt_generic(pm->td, ZSTR_VAL(data_str), data_size);
|
|
|
|
ZSTR_VAL(data_str)[data_size] = '\0';
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2015-07-01 11:12:46 +00:00
|
|
|
RETVAL_NEW_STR(data_str);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto string mdecrypt_generic(resource td, string data)
|
|
|
|
This function decrypts the plaintext */
|
|
|
|
PHP_FUNCTION(mdecrypt_generic)
|
|
|
|
{
|
2008-08-10 05:38:07 +00:00
|
|
|
zval *mcryptind;
|
|
|
|
char *data;
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t data_len;
|
2005-11-30 23:51:25 +00:00
|
|
|
php_mcrypt *pm;
|
2008-07-04 07:47:18 +00:00
|
|
|
char* data_s;
|
|
|
|
int block_size, data_size;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &mcryptind, &data, &data_len) == FAILURE) {
|
2008-08-10 05:38:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2015-02-03 03:58:11 +00:00
|
|
|
if ((pm = (php_mcrypt *)zend_fetch_resource(Z_RES_P(mcryptind), "MCrypt", le_mcrypt)) == NULL) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2008-07-04 07:47:18 +00:00
|
|
|
PHP_MCRYPT_INIT_CHECK
|
2008-08-10 05:38:07 +00:00
|
|
|
|
|
|
|
if (data_len == 0) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "An empty string was passed");
|
2006-05-31 20:09:07 +00:00
|
|
|
RETURN_FALSE
|
|
|
|
}
|
2000-08-11 18:45:17 +00:00
|
|
|
|
|
|
|
/* Check blocksize */
|
2008-07-04 07:47:18 +00:00
|
|
|
if (mcrypt_enc_is_block_mode(pm->td) == 1) { /* It's a block algorithm */
|
2005-11-30 23:51:25 +00:00
|
|
|
block_size = mcrypt_enc_get_block_size(pm->td);
|
2014-10-31 17:35:55 +00:00
|
|
|
data_size = ((((int)data_len - 1) / block_size) + 1) * block_size;
|
2008-07-04 07:47:18 +00:00
|
|
|
data_s = emalloc(data_size + 1);
|
|
|
|
memset(data_s, 0, data_size);
|
2008-08-10 05:38:07 +00:00
|
|
|
memcpy(data_s, data, data_len);
|
2008-07-04 07:47:18 +00:00
|
|
|
} else { /* It's not a block algorithm */
|
2014-10-31 17:35:55 +00:00
|
|
|
data_size = (int)data_len;
|
2008-07-04 07:47:18 +00:00
|
|
|
data_s = emalloc(data_size + 1);
|
|
|
|
memset(data_s, 0, data_size);
|
2008-08-10 05:38:07 +00:00
|
|
|
memcpy(data_s, data, data_len);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
mdecrypt_generic(pm->td, data_s, data_size);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2014-05-06 08:08:02 +00:00
|
|
|
RETVAL_STRINGL(data_s, data_size);
|
2008-07-04 07:47:18 +00:00
|
|
|
efree(data_s);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-06-12 12:00:20 +00:00
|
|
|
/* {{{ proto array mcrypt_enc_get_supported_key_sizes(resource td)
|
2000-08-11 18:45:17 +00:00
|
|
|
This function decrypts the crypttext */
|
|
|
|
PHP_FUNCTION(mcrypt_enc_get_supported_key_sizes)
|
|
|
|
{
|
2003-01-03 19:49:44 +00:00
|
|
|
int i, count = 0;
|
2000-08-11 18:45:17 +00:00
|
|
|
int *key_sizes;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
MCRYPT_GET_TD_ARG
|
2003-01-18 19:41:56 +00:00
|
|
|
array_init(return_value);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2005-11-30 23:51:25 +00:00
|
|
|
key_sizes = mcrypt_enc_get_supported_key_sizes(pm->td, &count);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2014-08-25 17:24:55 +00:00
|
|
|
add_index_long(return_value, i, key_sizes[i]);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2003-01-03 19:49:44 +00:00
|
|
|
|
|
|
|
mcrypt_free(key_sizes);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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
|
2014-08-25 17:24:55 +00:00
|
|
|
RETURN_LONG(mcrypt_enc_self_test(pm->td));
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-07 11:39:00 +00:00
|
|
|
/* {{{ proto bool mcrypt_module_close(resource td)
|
|
|
|
Free the descriptor td */
|
2000-08-11 18:45:17 +00:00
|
|
|
PHP_FUNCTION(mcrypt_module_close)
|
|
|
|
{
|
|
|
|
MCRYPT_GET_TD_ARG
|
2014-05-13 09:18:00 +00:00
|
|
|
zend_list_close(Z_RES_P(mcryptind));
|
2001-09-07 10:29:06 +00:00
|
|
|
RETURN_TRUE;
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2001-09-07 11:39:00 +00:00
|
|
|
/* }}} */
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2001-09-23 14:14:27 +00:00
|
|
|
/* {{{ 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
|
|
|
|
|
2005-11-30 23:51:25 +00:00
|
|
|
if (mcrypt_generic_deinit(pm->td) < 0) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Could not terminate encryption specifier");
|
2001-09-23 14:14:27 +00:00
|
|
|
RETURN_FALSE
|
|
|
|
}
|
2009-10-02 00:13:53 +00:00
|
|
|
pm->init = 0;
|
2001-09-23 14:14:27 +00:00
|
|
|
RETURN_TRUE
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
/* {{{ 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
|
|
|
|
|
2005-11-30 23:51:25 +00:00
|
|
|
if (mcrypt_enc_is_block_algorithm_mode(pm->td) == 1) {
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_TRUE
|
2003-01-03 19:49:44 +00:00
|
|
|
} else {
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_FALSE
|
2003-01-03 19:49:44 +00:00
|
|
|
}
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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
|
|
|
|
|
2005-11-30 23:51:25 +00:00
|
|
|
if (mcrypt_enc_is_block_algorithm(pm->td) == 1) {
|
2015-01-03 09:22:58 +00:00
|
|
|
RETURN_TRUE
|
2003-01-03 19:49:44 +00:00
|
|
|
} else {
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_FALSE
|
2003-01-03 19:49:44 +00:00
|
|
|
}
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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
|
|
|
|
|
2005-11-30 23:51:25 +00:00
|
|
|
if (mcrypt_enc_is_block_mode(pm->td) == 1) {
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_TRUE
|
2003-01-03 19:49:44 +00:00
|
|
|
} else {
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_FALSE
|
2003-01-03 19:49:44 +00:00
|
|
|
}
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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
|
2014-08-25 17:24:55 +00:00
|
|
|
RETURN_LONG(mcrypt_enc_get_block_size(pm->td));
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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
|
2014-08-25 17:24:55 +00:00
|
|
|
RETURN_LONG(mcrypt_enc_get_key_size(pm->td));
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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
|
2014-08-25 17:24:55 +00:00
|
|
|
RETURN_LONG(mcrypt_enc_get_iv_size(pm->td));
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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
|
|
|
|
|
2005-11-30 23:51:25 +00:00
|
|
|
name = mcrypt_enc_get_algorithms_name(pm->td);
|
2014-05-06 08:08:02 +00:00
|
|
|
RETVAL_STRING(name);
|
2003-01-03 19:49:44 +00:00
|
|
|
mcrypt_free(name);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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
|
|
|
|
|
2005-11-30 23:51:25 +00:00
|
|
|
name = mcrypt_enc_get_modes_name(pm->td);
|
2014-05-06 08:08:02 +00:00
|
|
|
RETVAL_STRING(name);
|
2003-01-03 19:49:44 +00:00
|
|
|
mcrypt_free(name);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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)
|
|
|
|
{
|
2000-08-13 11:53:04 +00:00
|
|
|
MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir);
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
if (mcrypt_module_self_test(module, dir) == 0) {
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_TRUE;
|
2002-06-16 18:20:21 +00:00
|
|
|
} else {
|
2000-08-11 18:45:17 +00:00
|
|
|
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)
|
|
|
|
{
|
2000-08-13 11:53:04 +00:00
|
|
|
MCRYPT_GET_MODE_DIR_ARGS(modes_dir)
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
if (mcrypt_module_is_block_algorithm_mode(module, dir) == 1) {
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_TRUE;
|
2001-12-26 10:01:50 +00:00
|
|
|
} else {
|
2000-08-11 18:45:17 +00:00
|
|
|
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)
|
|
|
|
{
|
2000-08-13 11:53:04 +00:00
|
|
|
MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir)
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
if (mcrypt_module_is_block_algorithm(module, dir) == 1) {
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_TRUE;
|
2001-12-26 10:01:50 +00:00
|
|
|
} else {
|
2000-08-11 18:45:17 +00:00
|
|
|
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)
|
|
|
|
{
|
2000-08-13 11:53:04 +00:00
|
|
|
MCRYPT_GET_MODE_DIR_ARGS(modes_dir)
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
if (mcrypt_module_is_block_mode(module, dir) == 1) {
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_TRUE;
|
2001-12-26 10:01:50 +00:00
|
|
|
} else {
|
2000-08-11 18:45:17 +00:00
|
|
|
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)
|
|
|
|
{
|
2000-08-13 11:53:04 +00:00
|
|
|
MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir)
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-08-25 17:24:55 +00:00
|
|
|
RETURN_LONG(mcrypt_module_get_algo_block_size(module, dir));
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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)
|
|
|
|
{
|
2000-08-13 11:53:04 +00:00
|
|
|
MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir);
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-08-25 17:24:55 +00:00
|
|
|
RETURN_LONG(mcrypt_module_get_algo_key_size(module, dir));
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-06-12 12:00:20 +00:00
|
|
|
/* {{{ proto array mcrypt_module_get_supported_key_sizes(string algorithm [, string lib_dir])
|
2000-08-11 18:45:17 +00:00
|
|
|
This function decrypts the crypttext */
|
|
|
|
PHP_FUNCTION(mcrypt_module_get_supported_key_sizes)
|
|
|
|
{
|
2003-01-03 19:49:44 +00:00
|
|
|
int i, count = 0;
|
2000-08-11 18:45:17 +00:00
|
|
|
int *key_sizes;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2000-08-13 11:53:04 +00:00
|
|
|
MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir)
|
2003-01-18 19:41:56 +00:00
|
|
|
array_init(return_value);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
key_sizes = mcrypt_module_get_algo_supported_key_sizes(module, dir, &count);
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2014-08-25 17:24:55 +00:00
|
|
|
add_index_long(return_value, i, key_sizes[i]);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2003-01-03 19:49:44 +00:00
|
|
|
mcrypt_free(key_sizes);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto array mcrypt_list_algorithms([string lib_dir])
|
|
|
|
List all algorithms in "module_dir" */
|
|
|
|
PHP_FUNCTION(mcrypt_list_algorithms)
|
|
|
|
{
|
|
|
|
char **modules;
|
2003-01-03 19:49:44 +00:00
|
|
|
char *lib_dir = MCG(algorithms_dir);
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t lib_dir_len;
|
2003-01-03 19:49:44 +00:00
|
|
|
int i, count;
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s",
|
2003-01-03 19:49:44 +00:00
|
|
|
&lib_dir, &lib_dir_len) == FAILURE) {
|
2000-08-11 18:45:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-01-18 19:41:56 +00:00
|
|
|
array_init(return_value);
|
2003-01-03 19:49:44 +00:00
|
|
|
modules = mcrypt_list_algorithms(lib_dir, &count);
|
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
if (count == 0) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "No algorithms found in module dir");
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < count; i++) {
|
2014-04-15 11:40:40 +00:00
|
|
|
add_index_string(return_value, i, modules[i]);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2003-01-03 19:49:44 +00:00
|
|
|
mcrypt_free_p(modules, count);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto array mcrypt_list_modes([string lib_dir])
|
|
|
|
List all modes "module_dir" */
|
|
|
|
PHP_FUNCTION(mcrypt_list_modes)
|
|
|
|
{
|
|
|
|
char **modules;
|
2003-01-03 19:49:44 +00:00
|
|
|
char *lib_dir = MCG(modes_dir);
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t lib_dir_len;
|
2003-01-03 19:49:44 +00:00
|
|
|
int i, count;
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s",
|
2003-01-03 19:49:44 +00:00
|
|
|
&lib_dir, &lib_dir_len) == FAILURE) {
|
2000-08-11 18:45:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-03 19:49:44 +00:00
|
|
|
|
2003-01-18 19:41:56 +00:00
|
|
|
array_init(return_value);
|
2003-01-03 19:49:44 +00:00
|
|
|
modules = mcrypt_list_modes(lib_dir, &count);
|
|
|
|
|
2000-08-11 18:45:17 +00:00
|
|
|
if (count == 0) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "No modes found in module dir");
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < count; i++) {
|
2014-04-15 11:40:40 +00:00
|
|
|
add_index_string(return_value, i, modules[i]);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2003-01-03 19:49:44 +00:00
|
|
|
mcrypt_free_p(modules, count);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto int mcrypt_get_key_size(string cipher, string module)
|
|
|
|
Get the key size of cipher */
|
|
|
|
PHP_FUNCTION(mcrypt_get_key_size)
|
|
|
|
{
|
2003-01-03 19:49:44 +00:00
|
|
|
char *cipher;
|
|
|
|
char *module;
|
2015-01-03 09:22:58 +00:00
|
|
|
size_t cipher_len, module_len;
|
2000-08-11 18:45:17 +00:00
|
|
|
char *cipher_dir_string;
|
|
|
|
char *module_dir_string;
|
|
|
|
MCRYPT td;
|
|
|
|
|
|
|
|
MCRYPT_GET_INI
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
|
2003-01-03 19:49:44 +00:00
|
|
|
&cipher, &cipher_len, &module, &module_len) == FAILURE) {
|
|
|
|
return;
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string);
|
|
|
|
if (td != MCRYPT_FAILED) {
|
2014-08-25 17:24:55 +00:00
|
|
|
RETVAL_LONG(mcrypt_enc_get_key_size(td));
|
2000-08-11 18:45:17 +00:00
|
|
|
mcrypt_module_close(td);
|
2003-01-03 19:49:44 +00:00
|
|
|
} else {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
|
2003-01-03 19:49:44 +00:00
|
|
|
RETURN_FALSE;
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto int mcrypt_get_block_size(string cipher, string module)
|
|
|
|
Get the key size of cipher */
|
|
|
|
PHP_FUNCTION(mcrypt_get_block_size)
|
|
|
|
{
|
2003-01-03 19:49:44 +00:00
|
|
|
char *cipher;
|
|
|
|
char *module;
|
2015-01-03 09:22:58 +00:00
|
|
|
size_t cipher_len, module_len;
|
2000-08-11 18:45:17 +00:00
|
|
|
char *cipher_dir_string;
|
|
|
|
char *module_dir_string;
|
|
|
|
MCRYPT td;
|
|
|
|
|
|
|
|
MCRYPT_GET_INI
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
|
2003-01-03 19:49:44 +00:00
|
|
|
&cipher, &cipher_len, &module, &module_len) == FAILURE) {
|
|
|
|
return;
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string);
|
|
|
|
if (td != MCRYPT_FAILED) {
|
2014-08-25 17:24:55 +00:00
|
|
|
RETVAL_LONG(mcrypt_enc_get_block_size(td));
|
2000-08-11 18:45:17 +00:00
|
|
|
mcrypt_module_close(td);
|
2003-01-03 19:49:44 +00:00
|
|
|
} else {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
|
2003-01-03 19:49:44 +00:00
|
|
|
RETURN_FALSE;
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ 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)
|
|
|
|
{
|
2003-01-03 19:49:44 +00:00
|
|
|
char *cipher;
|
|
|
|
char *module;
|
2015-01-03 09:22:58 +00:00
|
|
|
size_t cipher_len, module_len;
|
2000-08-11 18:45:17 +00:00
|
|
|
char *cipher_dir_string;
|
|
|
|
char *module_dir_string;
|
|
|
|
MCRYPT td;
|
|
|
|
|
|
|
|
MCRYPT_GET_INI
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
|
2003-01-03 19:49:44 +00:00
|
|
|
&cipher, &cipher_len, &module, &module_len) == FAILURE) {
|
|
|
|
return;
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string);
|
|
|
|
if (td != MCRYPT_FAILED) {
|
2014-08-25 17:24:55 +00:00
|
|
|
RETVAL_LONG(mcrypt_enc_get_iv_size(td));
|
2000-08-11 18:45:17 +00:00
|
|
|
mcrypt_module_close(td);
|
2003-01-03 19:49:44 +00:00
|
|
|
} else {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto string mcrypt_get_cipher_name(string cipher)
|
2008-07-04 07:47:18 +00:00
|
|
|
Get the key size of cipher */
|
2000-08-11 18:45:17 +00:00
|
|
|
PHP_FUNCTION(mcrypt_get_cipher_name)
|
|
|
|
{
|
|
|
|
char *cipher_dir_string;
|
|
|
|
char *module_dir_string;
|
|
|
|
char *cipher_name;
|
2003-01-03 19:49:44 +00:00
|
|
|
char *cipher;
|
2014-08-27 17:25:28 +00:00
|
|
|
size_t cipher_len;
|
2000-08-11 18:45:17 +00:00
|
|
|
MCRYPT td;
|
|
|
|
|
|
|
|
MCRYPT_GET_INI
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
|
2003-01-03 19:49:44 +00:00
|
|
|
&cipher, &cipher_len) == FAILURE) {
|
|
|
|
return;
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
2003-01-03 19:49:44 +00:00
|
|
|
/* 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) {
|
2000-08-11 18:45:17 +00:00
|
|
|
cipher_name = mcrypt_enc_get_algorithms_name(td);
|
|
|
|
mcrypt_module_close(td);
|
2014-05-06 08:08:02 +00:00
|
|
|
RETVAL_STRING(cipher_name);
|
2003-01-03 19:49:44 +00:00
|
|
|
mcrypt_free(cipher_name);
|
|
|
|
} else {
|
|
|
|
td = mcrypt_module_open(cipher, cipher_dir_string, "stream", module_dir_string);
|
|
|
|
if (td != MCRYPT_FAILED) {
|
2000-08-11 18:45:17 +00:00
|
|
|
cipher_name = mcrypt_enc_get_algorithms_name(td);
|
|
|
|
mcrypt_module_close(td);
|
2014-05-06 08:08:02 +00:00
|
|
|
RETVAL_STRING(cipher_name);
|
2003-01-03 19:49:44 +00:00
|
|
|
mcrypt_free(cipher_name);
|
|
|
|
} else {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
|
2000-08-11 18:45:17 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-03-02 22:23:12 +00:00
|
|
|
static char *php_mcrypt_get_key_size_str(
|
|
|
|
int max_key_size, const int *key_sizes, int key_size_count) /* {{{ */
|
2014-03-01 23:42:13 +00:00
|
|
|
{
|
2014-03-02 22:23:12 +00:00
|
|
|
if (key_size_count == 0) {
|
|
|
|
char *str;
|
|
|
|
spprintf(&str, 0, "Only keys of size 1 to %d supported", max_key_size);
|
|
|
|
return str;
|
|
|
|
} else if (key_size_count == 1) {
|
|
|
|
char *str;
|
|
|
|
spprintf(&str, 0, "Only keys of size %d supported", key_sizes[0]);
|
|
|
|
return str;
|
|
|
|
} else {
|
|
|
|
int i;
|
2014-08-19 06:07:31 +00:00
|
|
|
char *result = NULL;
|
2014-03-02 22:23:12 +00:00
|
|
|
smart_str str = {0};
|
|
|
|
smart_str_appends(&str, "Only keys of sizes ");
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-03-02 22:23:12 +00:00
|
|
|
for (i = 0; i < key_size_count; ++i) {
|
|
|
|
if (i == key_size_count - 1) {
|
|
|
|
smart_str_appends(&str, " or ");
|
|
|
|
} else if (i != 0) {
|
|
|
|
smart_str_appends(&str, ", ");
|
|
|
|
}
|
|
|
|
|
2014-08-25 17:24:55 +00:00
|
|
|
smart_str_append_long(&str, key_sizes[i]);
|
2014-03-02 22:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
smart_str_appends(&str, " supported");
|
|
|
|
smart_str_0(&str);
|
2015-06-30 01:05:24 +00:00
|
|
|
result = estrndup(ZSTR_VAL(str.s), ZSTR_LEN(str.s));
|
2014-05-06 10:20:54 +00:00
|
|
|
smart_str_free(&str);
|
|
|
|
|
|
|
|
return result;
|
2014-03-01 23:42:13 +00:00
|
|
|
}
|
2014-03-02 22:23:12 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
2014-03-01 23:42:13 +00:00
|
|
|
|
2014-03-02 22:23:12 +00:00
|
|
|
static zend_bool php_mcrypt_is_valid_key_size(
|
|
|
|
int key_size, int max_key_size, int *key_sizes, int key_size_count) /* {{{ */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (key_size <= 0 || key_size > max_key_size) {
|
2014-03-01 23:42:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-02 22:23:12 +00:00
|
|
|
if (key_size_count == 0) {
|
|
|
|
/* All key sizes are valid */
|
|
|
|
return 1;
|
|
|
|
}
|
2014-03-01 23:42:13 +00:00
|
|
|
|
2014-03-02 22:23:12 +00:00
|
|
|
for (i = 0; i < key_size_count; i++) {
|
|
|
|
if (key_sizes[i] == key_size) {
|
2014-03-01 23:42:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2014-03-02 22:23:12 +00:00
|
|
|
}
|
2014-03-01 23:42:13 +00:00
|
|
|
|
2014-03-02 22:23:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_mcrypt_ensure_valid_key_size(MCRYPT td, int key_size) /* {{{ */
|
2014-03-02 22:23:12 +00:00
|
|
|
{
|
|
|
|
int key_size_count;
|
|
|
|
int max_key_size = mcrypt_enc_get_key_size(td);
|
|
|
|
int *key_sizes = mcrypt_enc_get_supported_key_sizes(td, &key_size_count);
|
|
|
|
|
|
|
|
zend_bool is_valid_key_size = php_mcrypt_is_valid_key_size(
|
|
|
|
key_size, max_key_size, key_sizes, key_size_count
|
|
|
|
);
|
|
|
|
if (!is_valid_key_size) {
|
|
|
|
char *key_size_str = php_mcrypt_get_key_size_str(
|
|
|
|
max_key_size, key_sizes, key_size_count
|
|
|
|
);
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING,
|
2014-03-02 22:23:12 +00:00
|
|
|
"Key of size %d not supported by this algorithm. %s", key_size, key_size_str
|
|
|
|
);
|
|
|
|
efree(key_size_str);
|
|
|
|
}
|
2014-03-01 23:42:13 +00:00
|
|
|
|
2014-03-02 22:23:12 +00:00
|
|
|
if (key_sizes) {
|
2014-03-01 23:42:13 +00:00
|
|
|
mcrypt_free(key_sizes);
|
|
|
|
}
|
2014-03-02 22:23:12 +00:00
|
|
|
|
|
|
|
return is_valid_key_size ? SUCCESS : FAILURE;
|
2014-03-01 23:42:13 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_mcrypt_ensure_valid_iv(MCRYPT td, const char *iv, int iv_size) /* {{{ */
|
2014-03-05 11:42:01 +00:00
|
|
|
{
|
|
|
|
if (mcrypt_enc_mode_has_iv(td) == 1) {
|
|
|
|
int expected_iv_size = mcrypt_enc_get_iv_size(td);
|
2015-10-03 08:12:11 +00:00
|
|
|
if (expected_iv_size == 0) {
|
|
|
|
/* Algorithm does not use IV, even though mode supports it */
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2014-03-05 11:42:01 +00:00
|
|
|
|
|
|
|
if (!iv) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING,
|
2014-03-05 11:42:01 +00:00
|
|
|
"Encryption mode requires an initialization vector of size %d", expected_iv_size
|
|
|
|
);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iv_size != expected_iv_size) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING,
|
2014-03-05 11:42:01 +00:00
|
|
|
"Received initialization vector of size %d, but size %d is required "
|
|
|
|
"for this encryption mode", iv_size, expected_iv_size
|
|
|
|
);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
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) /* {{{ */
|
2000-08-11 18:45:17 +00:00
|
|
|
{
|
2008-07-04 07:47:18 +00:00
|
|
|
char *cipher_dir_string;
|
|
|
|
char *module_dir_string;
|
2014-08-27 19:53:06 +00:00
|
|
|
zend_long data_size;
|
2008-07-04 07:47:18 +00:00
|
|
|
char *data_s;
|
|
|
|
MCRYPT td;
|
2008-08-10 05:38:07 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
MCRYPT_GET_INI
|
|
|
|
|
|
|
|
td = mcrypt_module_open(cipher, cipher_dir_string, mode, module_dir_string);
|
|
|
|
if (td == MCRYPT_FAILED) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
|
2008-07-04 07:47:18 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2014-03-01 22:51:03 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (php_mcrypt_ensure_valid_key_size(td, (int)key_len) == FAILURE) {
|
2014-03-02 22:26:46 +00:00
|
|
|
mcrypt_module_close(td);
|
2014-03-01 23:42:13 +00:00
|
|
|
RETURN_FALSE;
|
2003-01-03 19:49:44 +00:00
|
|
|
}
|
2014-03-01 23:42:13 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (php_mcrypt_ensure_valid_iv(td, iv, (int)iv_len) == FAILURE) {
|
2014-03-05 11:42:01 +00:00
|
|
|
mcrypt_module_close(td);
|
|
|
|
RETURN_FALSE;
|
2008-07-04 07:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check blocksize */
|
|
|
|
if (mcrypt_enc_is_block_mode(td) == 1) { /* It's a block algorithm */
|
2014-03-01 23:42:13 +00:00
|
|
|
int block_size = mcrypt_enc_get_block_size(td);
|
2014-10-31 17:35:55 +00:00
|
|
|
data_size = ((((zend_long)data_len - 1) / block_size) + 1) * block_size;
|
2014-03-01 23:42:13 +00:00
|
|
|
data_s = emalloc(data_size + 1);
|
2008-07-04 07:47:18 +00:00
|
|
|
memset(data_s, 0, data_size);
|
2008-08-10 05:38:07 +00:00
|
|
|
memcpy(data_s, data, data_len);
|
2008-07-04 07:47:18 +00:00
|
|
|
} else { /* It's not a block algorithm */
|
2008-08-10 05:38:07 +00:00
|
|
|
data_size = data_len;
|
2014-03-01 23:42:13 +00:00
|
|
|
data_s = emalloc(data_size + 1);
|
2008-08-10 05:38:07 +00:00
|
|
|
memcpy(data_s, data, data_len);
|
2008-07-04 07:47:18 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 17:35:55 +00:00
|
|
|
if (mcrypt_generic_init(td, (void *) key, (int)key_len, (void *) iv) < 0) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_RECOVERABLE_ERROR, "Mcrypt initialisation failed");
|
2014-03-02 22:26:46 +00:00
|
|
|
mcrypt_module_close(td);
|
2008-07-03 13:50:14 +00:00
|
|
|
RETURN_FALSE;
|
2003-03-13 21:09:27 +00:00
|
|
|
}
|
2014-03-01 23:42:13 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
if (dencrypt == MCRYPT_ENCRYPT) {
|
2014-10-31 17:35:55 +00:00
|
|
|
mcrypt_generic(td, data_s, (int)data_size);
|
2008-07-04 07:47:18 +00:00
|
|
|
} else {
|
2014-10-31 17:35:55 +00:00
|
|
|
mdecrypt_generic(td, data_s, (int)data_size);
|
2008-07-04 07:47:18 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-03-01 23:42:13 +00:00
|
|
|
data_s[data_size] = 0;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-05-06 08:08:02 +00:00
|
|
|
RETVAL_STRINGL(data_s, data_size);
|
2014-05-06 10:20:54 +00:00
|
|
|
efree(data_s);
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
/* freeing vars */
|
|
|
|
mcrypt_generic_end(td);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2007-07-19 14:32:55 +00:00
|
|
|
/* }}} */
|
2000-08-11 18:45:17 +00:00
|
|
|
|
|
|
|
/* {{{ 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)
|
|
|
|
{
|
2014-05-06 10:22:15 +00:00
|
|
|
char *cipher, *key, *data, *mode, *iv = NULL;
|
2014-08-27 17:25:28 +00:00
|
|
|
size_t cipher_len, key_len, data_len, mode_len, iv_len = 0;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssss|s", &cipher, &cipher_len,
|
2014-03-05 14:18:27 +00:00
|
|
|
&key, &key_len, &data, &data_len, &mode, &mode_len, &iv, &iv_len) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
2008-07-04 07:47:18 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, mode, iv, iv_len, MCRYPT_ENCRYPT, return_value);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2001-06-06 13:06:12 +00:00
|
|
|
/* }}} */
|
2000-08-11 18:45:17 +00:00
|
|
|
|
|
|
|
/* {{{ 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)
|
|
|
|
{
|
2014-03-05 14:18:27 +00:00
|
|
|
char *cipher, *key, *data, *mode, *iv = NULL;
|
2014-08-27 17:25:28 +00:00
|
|
|
size_t cipher_len, key_len, data_len, mode_len, iv_len = 0;
|
2008-07-04 07:47:18 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssss|s", &cipher, &cipher_len,
|
2014-03-05 14:18:27 +00:00
|
|
|
&key, &key_len, &data, &data_len, &mode, &mode_len, &iv, &iv_len) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
2008-07-04 07:47:18 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
php_mcrypt_do_crypt(cipher, key, key_len, data, data_len, mode, iv, iv_len, MCRYPT_DECRYPT, return_value);
|
2000-08-11 18:45:17 +00:00
|
|
|
}
|
2001-06-06 13:06:12 +00:00
|
|
|
/* }}} */
|
2000-08-11 18:45:17 +00:00
|
|
|
|
2000-05-26 17:20:36 +00:00
|
|
|
/* {{{ proto string mcrypt_create_iv(int size, int source)
|
2001-12-13 20:44:40 +00:00
|
|
|
Create an initialization vector (IV) */
|
1999-04-25 21:09:07 +00:00
|
|
|
PHP_FUNCTION(mcrypt_create_iv)
|
|
|
|
{
|
2008-07-04 07:47:18 +00:00
|
|
|
char *iv;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long source = URANDOM;
|
|
|
|
zend_long size;
|
2008-07-04 07:47:18 +00:00
|
|
|
int n = 0;
|
1999-04-25 21:09:07 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &size, &source) == FAILURE) {
|
2001-12-13 20:44:40 +00:00
|
|
|
return;
|
1999-04-25 21:09:07 +00:00
|
|
|
}
|
|
|
|
|
2007-02-15 01:40:36 +00:00
|
|
|
if (size <= 0 || size >= INT_MAX) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Cannot create an IV with a size of less than 1 or greater than %d", INT_MAX);
|
2008-07-04 07:47:18 +00:00
|
|
|
RETURN_FALSE;
|
1999-05-01 11:14:22 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
iv = ecalloc(size + 1, 1);
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
if (source == RANDOM || source == URANDOM) {
|
2008-07-15 17:05:02 +00:00
|
|
|
#if PHP_WIN32
|
2010-06-08 18:27:23 +00:00
|
|
|
/* random/urandom equivalent on Windows */
|
|
|
|
BYTE *iv_b = (BYTE *) iv;
|
|
|
|
if (php_win32_get_random_bytes(iv_b, (size_t) size) == FAILURE){
|
|
|
|
efree(iv);
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
|
2010-06-08 18:27:23 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2014-10-31 17:35:55 +00:00
|
|
|
n = (int)size;
|
2008-07-15 17:05:02 +00:00
|
|
|
#else
|
2015-03-27 13:33:30 +00:00
|
|
|
int *fd = &MCG(fd[source]);
|
2008-07-04 07:47:18 +00:00
|
|
|
size_t read_bytes = 0;
|
|
|
|
|
2015-03-27 13:33:30 +00:00
|
|
|
if (*fd < 0) {
|
|
|
|
*fd = open(source == RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY);
|
|
|
|
if (*fd < 0) {
|
|
|
|
efree(iv);
|
2015-05-13 12:21:19 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Cannot open source device");
|
2015-03-27 13:33:30 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
1999-04-25 21:09:07 +00:00
|
|
|
}
|
2015-03-27 13:33:30 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
while (read_bytes < size) {
|
2015-03-27 13:33:30 +00:00
|
|
|
n = read(*fd, iv + read_bytes, size - read_bytes);
|
2015-08-12 08:58:21 +00:00
|
|
|
if (n <= 0) {
|
2008-07-04 07:47:18 +00:00
|
|
|
break;
|
2003-03-13 21:09:27 +00:00
|
|
|
}
|
2008-07-04 07:47:18 +00:00
|
|
|
read_bytes += n;
|
2000-07-25 22:17:20 +00:00
|
|
|
}
|
2008-07-04 07:47:18 +00:00
|
|
|
n = read_bytes;
|
2015-03-27 13:33:30 +00:00
|
|
|
|
2008-07-04 07:47:18 +00:00
|
|
|
if (n < size) {
|
|
|
|
efree(iv);
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data");
|
2008-07-04 07:47:18 +00:00
|
|
|
RETURN_FALSE;
|
2001-12-13 20:44:40 +00:00
|
|
|
}
|
2008-07-15 17:05:02 +00:00
|
|
|
#endif
|
1999-04-25 21:09:07 +00:00
|
|
|
} else {
|
2014-10-31 17:35:55 +00:00
|
|
|
n = (int)size;
|
2008-07-04 07:47:18 +00:00
|
|
|
while (size) {
|
2014-12-13 22:06:14 +00:00
|
|
|
iv[--size] = (char) (255.0 * php_rand() / RAND_MAX);
|
1999-04-25 21:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 10:20:54 +00:00
|
|
|
RETVAL_STRINGL(iv, n);
|
|
|
|
efree(iv);
|
1999-04-25 21:09:07 +00:00
|
|
|
}
|
2000-05-26 17:20:36 +00:00
|
|
|
/* }}} */
|
1999-04-25 21:09:07 +00:00
|
|
|
|
1999-04-25 16:50:40 +00:00
|
|
|
#endif
|
2001-06-06 13:06:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2001-09-09 13:29:31 +00:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
2001-06-06 13:06:12 +00:00
|
|
|
*/
|