mirror of
https://github.com/php/php-src.git
synced 2024-11-27 03:44:07 +08:00
add CBC
This commit is contained in:
parent
11fc109ca6
commit
1db171deb8
@ -1,3 +1,31 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| PHP HTML Embedded Scripting Language Version 4.0 |
|
||||
+----------------------------------------------------------------------+
|
||||
| Copyright (c) 1999 PHP Development Team (See Credits file) |
|
||||
+----------------------------------------------------------------------+
|
||||
| This program is free software; you can redistribute it and/or modify |
|
||||
| it under the terms of one of the following licenses: |
|
||||
| |
|
||||
| A) the GNU General Public License as published by the Free Software |
|
||||
| Foundation; either version 2 of the License, or (at your option) |
|
||||
| any later version. |
|
||||
| |
|
||||
| B) the PHP License as published by the PHP Development Team and |
|
||||
| included in the distribution in the file: LICENSE |
|
||||
| |
|
||||
| This program is distributed in the hope that it will be useful, |
|
||||
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||
| GNU General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of both licenses referred to here. |
|
||||
| If you did not, or have any questions about PHP licensing, please |
|
||||
| contact core@php.net. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Authors: Sascha Schumann <sascha@schumann.2ns.de> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include "php.h"
|
||||
|
||||
@ -9,6 +37,7 @@
|
||||
|
||||
function_entry mcrypt_functions[] = {
|
||||
PHP_FE(mcrypt_ecb, NULL)
|
||||
PHP_FE(mcrypt_cbc, NULL)
|
||||
{0},
|
||||
};
|
||||
|
||||
@ -35,7 +64,7 @@ static mcrypt_global_struct mcryptg;
|
||||
|
||||
#endif
|
||||
|
||||
#define MCRYPT_ENTRY(a) REGISTER_LONG_CONSTANT("MC_" #a, a, 0)
|
||||
#define MCRYPT_ENTRY(a) REGISTER_LONG_CONSTANT("MCRYPT_" #a, a, 0)
|
||||
|
||||
static int php3_minit_mcrypt(INIT_FUNC_ARGS)
|
||||
{
|
||||
@ -56,6 +85,51 @@ static int php3_minit_mcrypt(INIT_FUNC_ARGS)
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/* proto mcrypt_cbc(int cipher, string key, string data, int mode)
|
||||
CBC crypt/decrypt data using key key with cipher cipher */
|
||||
PHP_FUNCTION(mcrypt_cbc)
|
||||
{
|
||||
pval *cipher, *data, *key, *mode;
|
||||
int td;
|
||||
char *ndata;
|
||||
size_t bsize;
|
||||
size_t nr;
|
||||
size_t nsize;
|
||||
|
||||
if(ARG_COUNT(ht) != 4 ||
|
||||
getParameters(ht, 4, &cipher, &key, &data, &mode) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_long(cipher);
|
||||
convert_to_long(mode);
|
||||
convert_to_string(data);
|
||||
convert_to_string(key);
|
||||
|
||||
bsize = get_block_size(cipher->value.lval);
|
||||
nr = (data->value.str.len + bsize - 1) / bsize;
|
||||
nsize = nr * bsize;
|
||||
|
||||
td = init_mcrypt_cbc(cipher->value.lval, key->value.str.val, key->value.str.len);
|
||||
if(td == -1) {
|
||||
php3_error(E_WARNING, "mcrypt initialization failed");
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
ndata = ecalloc(nr, bsize);
|
||||
memcpy(ndata, data->value.str.val, data->value.str.len);
|
||||
|
||||
if(mode->value.lval == 0)
|
||||
mcrypt_cbc(td, ndata, nsize);
|
||||
else
|
||||
mdecrypt_cbc(td, ndata, nsize);
|
||||
|
||||
end_mcrypt_cbc(td);
|
||||
|
||||
RETURN_STRINGL(ndata, nsize, 0);
|
||||
}
|
||||
|
||||
/* proto mcrypt_ecb(int cipher, string key, string data, int mode)
|
||||
ECB crypt/decrypt data using key key with cipher cipher */
|
||||
PHP_FUNCTION(mcrypt_ecb)
|
||||
{
|
||||
pval *cipher, *data, *key, *mode;
|
||||
@ -65,7 +139,8 @@ PHP_FUNCTION(mcrypt_ecb)
|
||||
size_t nr;
|
||||
size_t nsize;
|
||||
|
||||
if(ARG_COUNT(ht) != 4 || getParameters(ht, 4, &cipher, &key, &data, &mode) == FAILURE) {
|
||||
if(ARG_COUNT(ht) != 4 ||
|
||||
getParameters(ht, 4, &cipher, &key, &data, &mode) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_long(cipher);
|
||||
@ -91,7 +166,7 @@ PHP_FUNCTION(mcrypt_ecb)
|
||||
else
|
||||
mdecrypt_ecb(td, ndata, nsize);
|
||||
|
||||
end_mcrypt(td);
|
||||
end_mcrypt_ecb(td);
|
||||
|
||||
RETURN_STRINGL(ndata, nsize, 0);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ extern zend_module_entry mcrypt_module_entry;
|
||||
#define mcrypt_module_ptr &mcrypt_module_entry
|
||||
|
||||
PHP_FUNCTION(mcrypt_ecb);
|
||||
PHP_FUNCTION(mcrypt_cbc);
|
||||
|
||||
#else
|
||||
#define mcrypt_module_ptr NULL
|
||||
|
Loading…
Reference in New Issue
Block a user