1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Sascha Schumann <sascha@schumann.cx> |
16 : | Derick Rethans <derick@derickrethans.nl> |
17 : +----------------------------------------------------------------------+
18 : */
19 : /* $Id: mcrypt.c 289076 2009-10-02 00:13:53Z srinatar $ */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include "config.h"
23 : #endif
24 :
25 : #include "php.h"
26 :
27 : #if HAVE_LIBMCRYPT
28 :
29 : #if PHP_WIN32
30 : # include <Wincrypt.h>
31 : # include <Ntsecapi.h>
32 : #endif
33 :
34 : #include "php_mcrypt.h"
35 : #include "fcntl.h"
36 :
37 : #define NON_FREE
38 : #define MCRYPT2
39 : #include "mcrypt.h"
40 : #include "php_ini.h"
41 : #include "php_globals.h"
42 : #include "ext/standard/info.h"
43 : #include "ext/standard/php_rand.h"
44 :
45 : static int le_mcrypt;
46 :
47 : typedef struct _php_mcrypt {
48 : MCRYPT td;
49 : zend_bool init;
50 : } php_mcrypt;
51 :
52 : /* {{{ arginfo */
53 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_open, 0, 0, 4)
54 : ZEND_ARG_INFO(0, cipher)
55 : ZEND_ARG_INFO(0, cipher_directory)
56 : ZEND_ARG_INFO(0, mode)
57 : ZEND_ARG_INFO(0, mode_directory)
58 : ZEND_END_ARG_INFO()
59 :
60 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_generic_init, 0, 0, 3)
61 : ZEND_ARG_INFO(0, td)
62 : ZEND_ARG_INFO(0, key)
63 : ZEND_ARG_INFO(0, iv)
64 : ZEND_END_ARG_INFO()
65 :
66 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_generic, 0, 0, 2)
67 : ZEND_ARG_INFO(0, td)
68 : ZEND_ARG_INFO(0, data)
69 : ZEND_END_ARG_INFO()
70 :
71 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mdecrypt_generic, 0, 0, 2)
72 : ZEND_ARG_INFO(0, td)
73 : ZEND_ARG_INFO(0, data)
74 : ZEND_END_ARG_INFO()
75 :
76 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_supported_key_sizes, 0, 0, 1)
77 : ZEND_ARG_INFO(0, td)
78 : ZEND_END_ARG_INFO()
79 :
80 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_self_test, 0, 0, 1)
81 : ZEND_ARG_INFO(0, td)
82 : ZEND_END_ARG_INFO()
83 :
84 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_close, 0, 0, 1)
85 : ZEND_ARG_INFO(0, td)
86 : ZEND_END_ARG_INFO()
87 :
88 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_generic_deinit, 0, 0, 1)
89 : ZEND_ARG_INFO(0, td)
90 : ZEND_END_ARG_INFO()
91 :
92 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_is_block_algorithm_mode, 0, 0, 1)
93 : ZEND_ARG_INFO(0, td)
94 : ZEND_END_ARG_INFO()
95 :
96 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_is_block_algorithm, 0, 0, 1)
97 : ZEND_ARG_INFO(0, td)
98 : ZEND_END_ARG_INFO()
99 :
100 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_is_block_mode, 0, 0, 1)
101 : ZEND_ARG_INFO(0, td)
102 : ZEND_END_ARG_INFO()
103 :
104 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_block_size, 0, 0, 1)
105 : ZEND_ARG_INFO(0, td)
106 : ZEND_END_ARG_INFO()
107 :
108 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_key_size, 0, 0, 1)
109 : ZEND_ARG_INFO(0, td)
110 : ZEND_END_ARG_INFO()
111 :
112 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_iv_size, 0, 0, 1)
113 : ZEND_ARG_INFO(0, td)
114 : ZEND_END_ARG_INFO()
115 :
116 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_algorithms_name, 0, 0, 1)
117 : ZEND_ARG_INFO(0, td)
118 : ZEND_END_ARG_INFO()
119 :
120 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_enc_get_modes_name, 0, 0, 1)
121 : ZEND_ARG_INFO(0, td)
122 : ZEND_END_ARG_INFO()
123 :
124 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_self_test, 0, 0, 1)
125 : ZEND_ARG_INFO(0, algorithm)
126 : ZEND_ARG_INFO(0, lib_dir)
127 : ZEND_END_ARG_INFO()
128 :
129 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_is_block_algorithm_mode, 0, 0, 1)
130 : ZEND_ARG_INFO(0, mode)
131 : ZEND_ARG_INFO(0, lib_dir)
132 : ZEND_END_ARG_INFO()
133 :
134 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_is_block_algorithm, 0, 0, 1)
135 : ZEND_ARG_INFO(0, algorithm)
136 : ZEND_ARG_INFO(0, lib_dir)
137 : ZEND_END_ARG_INFO()
138 :
139 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_is_block_mode, 0, 0, 1)
140 : ZEND_ARG_INFO(0, mode)
141 : ZEND_ARG_INFO(0, lib_dir)
142 : ZEND_END_ARG_INFO()
143 :
144 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_get_algo_block_size, 0, 0, 1)
145 : ZEND_ARG_INFO(0, algorithm)
146 : ZEND_ARG_INFO(0, lib_dir)
147 : ZEND_END_ARG_INFO()
148 :
149 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_get_algo_key_size, 0, 0, 1)
150 : ZEND_ARG_INFO(0, algorithm)
151 : ZEND_ARG_INFO(0, lib_dir)
152 : ZEND_END_ARG_INFO()
153 :
154 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_module_get_supported_key_sizes, 0, 0, 1)
155 : ZEND_ARG_INFO(0, algorithm)
156 : ZEND_ARG_INFO(0, lib_dir)
157 : ZEND_END_ARG_INFO()
158 :
159 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_list_algorithms, 0, 0, 0)
160 : ZEND_ARG_INFO(0, lib_dir)
161 : ZEND_END_ARG_INFO()
162 :
163 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_list_modes, 0, 0, 0)
164 : ZEND_ARG_INFO(0, lib_dir)
165 : ZEND_END_ARG_INFO()
166 :
167 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_key_size, 0, 0, 2)
168 : ZEND_ARG_INFO(0, cipher)
169 : ZEND_ARG_INFO(0, module)
170 : ZEND_END_ARG_INFO()
171 :
172 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_block_size, 0, 0, 2)
173 : ZEND_ARG_INFO(0, cipher)
174 : ZEND_ARG_INFO(0, module)
175 : ZEND_END_ARG_INFO()
176 :
177 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_iv_size, 0, 0, 2)
178 : ZEND_ARG_INFO(0, cipher)
179 : ZEND_ARG_INFO(0, module)
180 : ZEND_END_ARG_INFO()
181 :
182 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_get_cipher_name, 0, 0, 1)
183 : ZEND_ARG_INFO(0, cipher)
184 : ZEND_END_ARG_INFO()
185 :
186 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_encrypt, 0, 0, 5)
187 : ZEND_ARG_INFO(0, cipher)
188 : ZEND_ARG_INFO(0, key)
189 : ZEND_ARG_INFO(0, data)
190 : ZEND_ARG_INFO(0, mode)
191 : ZEND_ARG_INFO(0, iv)
192 : ZEND_END_ARG_INFO()
193 :
194 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_decrypt, 0, 0, 5)
195 : ZEND_ARG_INFO(0, cipher)
196 : ZEND_ARG_INFO(0, key)
197 : ZEND_ARG_INFO(0, data)
198 : ZEND_ARG_INFO(0, mode)
199 : ZEND_ARG_INFO(0, iv)
200 : ZEND_END_ARG_INFO()
201 :
202 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_ecb, 0, 0, 5)
203 : ZEND_ARG_INFO(0, cipher)
204 : ZEND_ARG_INFO(0, key)
205 : ZEND_ARG_INFO(0, data)
206 : ZEND_ARG_INFO(0, mode)
207 : ZEND_ARG_INFO(0, iv)
208 : ZEND_END_ARG_INFO()
209 :
210 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_cbc, 0, 0, 5)
211 : ZEND_ARG_INFO(0, cipher)
212 : ZEND_ARG_INFO(0, key)
213 : ZEND_ARG_INFO(0, data)
214 : ZEND_ARG_INFO(0, mode)
215 : ZEND_ARG_INFO(0, iv)
216 : ZEND_END_ARG_INFO()
217 :
218 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_cfb, 0, 0, 5)
219 : ZEND_ARG_INFO(0, cipher)
220 : ZEND_ARG_INFO(0, key)
221 : ZEND_ARG_INFO(0, data)
222 : ZEND_ARG_INFO(0, mode)
223 : ZEND_ARG_INFO(0, iv)
224 : ZEND_END_ARG_INFO()
225 :
226 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_ofb, 0, 0, 5)
227 : ZEND_ARG_INFO(0, cipher)
228 : ZEND_ARG_INFO(0, key)
229 : ZEND_ARG_INFO(0, data)
230 : ZEND_ARG_INFO(0, mode)
231 : ZEND_ARG_INFO(0, iv)
232 : ZEND_END_ARG_INFO()
233 :
234 : ZEND_BEGIN_ARG_INFO_EX(arginfo_mcrypt_create_iv, 0, 0, 2)
235 : ZEND_ARG_INFO(0, size)
236 : ZEND_ARG_INFO(0, source)
237 : ZEND_END_ARG_INFO()
238 : /* }}} */
239 :
240 : const zend_function_entry mcrypt_functions[] = { /* {{{ */
241 : PHP_FE(mcrypt_ecb, arginfo_mcrypt_ecb)
242 : PHP_FE(mcrypt_cbc, arginfo_mcrypt_cbc)
243 : PHP_FE(mcrypt_cfb, arginfo_mcrypt_cfb)
244 : PHP_FE(mcrypt_ofb, arginfo_mcrypt_ofb)
245 : PHP_FE(mcrypt_get_key_size, arginfo_mcrypt_get_key_size)
246 : PHP_FE(mcrypt_get_block_size, arginfo_mcrypt_get_block_size)
247 : PHP_FE(mcrypt_get_cipher_name, arginfo_mcrypt_get_cipher_name)
248 : PHP_FE(mcrypt_create_iv, arginfo_mcrypt_create_iv)
249 :
250 : PHP_FE(mcrypt_list_algorithms, arginfo_mcrypt_list_algorithms)
251 : PHP_FE(mcrypt_list_modes, arginfo_mcrypt_list_modes)
252 : PHP_FE(mcrypt_get_iv_size, arginfo_mcrypt_get_iv_size)
253 : PHP_FE(mcrypt_encrypt, arginfo_mcrypt_encrypt)
254 : PHP_FE(mcrypt_decrypt, arginfo_mcrypt_decrypt)
255 :
256 : PHP_FE(mcrypt_module_open, arginfo_mcrypt_module_open)
257 : PHP_FE(mcrypt_generic_init, arginfo_mcrypt_generic_init)
258 : PHP_FE(mcrypt_generic, arginfo_mcrypt_generic)
259 : PHP_FE(mdecrypt_generic, arginfo_mdecrypt_generic)
260 : PHP_DEP_FALIAS(mcrypt_generic_end, mcrypt_generic_deinit, arginfo_mcrypt_generic_deinit)
261 : PHP_FE(mcrypt_generic_deinit, arginfo_mcrypt_generic_deinit)
262 :
263 : PHP_FE(mcrypt_enc_self_test, arginfo_mcrypt_enc_self_test)
264 : PHP_FE(mcrypt_enc_is_block_algorithm_mode, arginfo_mcrypt_enc_is_block_algorithm_mode)
265 : PHP_FE(mcrypt_enc_is_block_algorithm, arginfo_mcrypt_enc_is_block_algorithm)
266 : PHP_FE(mcrypt_enc_is_block_mode, arginfo_mcrypt_enc_is_block_mode)
267 : PHP_FE(mcrypt_enc_get_block_size, arginfo_mcrypt_enc_get_block_size)
268 : PHP_FE(mcrypt_enc_get_key_size, arginfo_mcrypt_enc_get_key_size)
269 : PHP_FE(mcrypt_enc_get_supported_key_sizes, arginfo_mcrypt_enc_get_supported_key_sizes)
270 : PHP_FE(mcrypt_enc_get_iv_size, arginfo_mcrypt_enc_get_iv_size)
271 : PHP_FE(mcrypt_enc_get_algorithms_name, arginfo_mcrypt_enc_get_algorithms_name)
272 : PHP_FE(mcrypt_enc_get_modes_name, arginfo_mcrypt_enc_get_modes_name)
273 : PHP_FE(mcrypt_module_self_test, arginfo_mcrypt_module_self_test)
274 :
275 : PHP_FE(mcrypt_module_is_block_algorithm_mode, arginfo_mcrypt_module_is_block_algorithm_mode)
276 : PHP_FE(mcrypt_module_is_block_algorithm, arginfo_mcrypt_module_is_block_algorithm)
277 : PHP_FE(mcrypt_module_is_block_mode, arginfo_mcrypt_module_is_block_mode)
278 : PHP_FE(mcrypt_module_get_algo_block_size, arginfo_mcrypt_module_get_algo_block_size)
279 : PHP_FE(mcrypt_module_get_algo_key_size, arginfo_mcrypt_module_get_algo_key_size)
280 : PHP_FE(mcrypt_module_get_supported_key_sizes, arginfo_mcrypt_module_get_supported_key_sizes)
281 :
282 : PHP_FE(mcrypt_module_close, arginfo_mcrypt_module_close)
283 : {NULL, NULL, NULL}
284 : };
285 : /* }}} */
286 :
287 : static PHP_MINFO_FUNCTION(mcrypt);
288 : static PHP_MINIT_FUNCTION(mcrypt);
289 : static PHP_MSHUTDOWN_FUNCTION(mcrypt);
290 :
291 : ZEND_DECLARE_MODULE_GLOBALS(mcrypt)
292 :
293 : zend_module_entry mcrypt_module_entry = {
294 : STANDARD_MODULE_HEADER,
295 : "mcrypt",
296 : mcrypt_functions,
297 : PHP_MINIT(mcrypt), PHP_MSHUTDOWN(mcrypt),
298 : NULL, NULL,
299 : PHP_MINFO(mcrypt),
300 : NO_VERSION_YET,
301 : PHP_MODULE_GLOBALS(mcrypt),
302 : NULL,
303 : NULL,
304 : NULL,
305 : STANDARD_MODULE_PROPERTIES_EX
306 : };
307 :
308 : #ifdef COMPILE_DL_MCRYPT
309 : ZEND_GET_MODULE(mcrypt)
310 : #endif
311 :
312 : #define MCRYPT_IV_WRONG_SIZE "The IV parameter must be as long as the blocksize"
313 : #define MCRYPT_OPEN_MODULE_FAILED "Module initialization failed"
314 :
315 : #define MCRYPT_ENTRY2_2_4(a,b) REGISTER_STRING_CONSTANT("MCRYPT_" #a, b, CONST_PERSISTENT)
316 : #define MCRYPT_ENTRY2_4(a) MCRYPT_ENTRY_NAMED(a, a)
317 :
318 : #define PHP_MCRYPT_INIT_CHECK \
319 : if (!pm->init) { \
320 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Operation disallowed prior to mcrypt_generic_init()"); \
321 : RETURN_FALSE; \
322 : } \
323 :
324 : #define MCRYPT_GET_INI \
325 : cipher_dir_string = MCG(algorithms_dir); \
326 : module_dir_string = MCG(modes_dir);
327 :
328 : #define MCRYPT_GET_TD_ARG \
329 : zval *mcryptind; \
330 : php_mcrypt *pm; \
331 : if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &mcryptind)) { \
332 : return; \
333 : } \
334 : ZEND_FETCH_RESOURCE (pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt);
335 :
336 : #define MCRYPT_GET_MODE_DIR_ARGS(DIRECTORY) \
337 : char *dir = NULL; \
338 : int dir_len; \
339 : char *module; \
340 : int module_len; \
341 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&|s&", \
342 : &module, &module_len, UG(ascii_conv), \
343 : &dir, &dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv))) == FAILURE) { \
344 : return; \
345 : }
346 :
347 : PHP_INI_BEGIN()
348 : STD_PHP_INI_ENTRY("mcrypt.algorithms_dir", NULL, PHP_INI_ALL, OnUpdateString, algorithms_dir, zend_mcrypt_globals, mcrypt_globals)
349 : STD_PHP_INI_ENTRY("mcrypt.modes_dir", NULL, PHP_INI_ALL, OnUpdateString, modes_dir, zend_mcrypt_globals, mcrypt_globals)
350 : PHP_INI_END()
351 :
352 : static void php_mcrypt_module_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
353 39 : {
354 39 : php_mcrypt *pm = (php_mcrypt *) rsrc->ptr;
355 39 : if (pm) {
356 39 : mcrypt_generic_deinit(pm->td);
357 39 : mcrypt_module_close(pm->td);
358 39 : efree(pm);
359 39 : rsrc->ptr = NULL;
360 : }
361 39 : }
362 : /* }}} */
363 :
364 : static PHP_MINIT_FUNCTION(mcrypt) /* {{{ */
365 17007 : {
366 17007 : le_mcrypt = zend_register_list_destructors_ex(php_mcrypt_module_dtor, NULL, "mcrypt", module_number);
367 :
368 : /* modes for mcrypt_??? routines */
369 17007 : REGISTER_LONG_CONSTANT("MCRYPT_ENCRYPT", PHP_MCRYPT_ENCRYPT, CONST_PERSISTENT);
370 17007 : REGISTER_LONG_CONSTANT("MCRYPT_DECRYPT", PHP_MCRYPT_DECRYPT, CONST_PERSISTENT);
371 :
372 : /* sources for mcrypt_create_iv */
373 17007 : REGISTER_LONG_CONSTANT("MCRYPT_DEV_RANDOM", PHP_MCRYPT_IV_SOURCE_RANDOM, CONST_PERSISTENT);
374 17007 : REGISTER_LONG_CONSTANT("MCRYPT_DEV_URANDOM", PHP_MCRYPT_IV_SOURCE_URANDOM, CONST_PERSISTENT);
375 17007 : REGISTER_LONG_CONSTANT("MCRYPT_RAND", PHP_MCRYPT_IV_SOURCE_RAND, CONST_PERSISTENT);
376 :
377 : /* ciphers */
378 17007 : MCRYPT_ENTRY2_2_4(3DES, "tripledes");
379 17007 : MCRYPT_ENTRY2_2_4(ARCFOUR_IV, "arcfour-iv");
380 17007 : MCRYPT_ENTRY2_2_4(ARCFOUR, "arcfour");
381 17007 : MCRYPT_ENTRY2_2_4(BLOWFISH, "blowfish");
382 17007 : MCRYPT_ENTRY2_2_4(BLOWFISH_COMPAT, "blowfish-compat");
383 17007 : MCRYPT_ENTRY2_2_4(CAST_128, "cast-128");
384 17007 : MCRYPT_ENTRY2_2_4(CAST_256, "cast-256");
385 17007 : MCRYPT_ENTRY2_2_4(CRYPT, "crypt");
386 17007 : MCRYPT_ENTRY2_2_4(DES, "des");
387 17007 : MCRYPT_ENTRY2_2_4(ENIGMA, "crypt");
388 17007 : MCRYPT_ENTRY2_2_4(GOST, "gost");
389 17007 : MCRYPT_ENTRY2_2_4(LOKI97, "loki97");
390 17007 : MCRYPT_ENTRY2_2_4(PANAMA, "panama");
391 17007 : MCRYPT_ENTRY2_2_4(RC2, "rc2");
392 17007 : MCRYPT_ENTRY2_2_4(RIJNDAEL_128, "rijndael-128");
393 17007 : MCRYPT_ENTRY2_2_4(RIJNDAEL_192, "rijndael-192");
394 17007 : MCRYPT_ENTRY2_2_4(RIJNDAEL_256, "rijndael-256");
395 17007 : MCRYPT_ENTRY2_2_4(SAFER64, "safer-sk64");
396 17007 : MCRYPT_ENTRY2_2_4(SAFER128, "safer-sk128");
397 17007 : MCRYPT_ENTRY2_2_4(SAFERPLUS, "saferplus");
398 17007 : MCRYPT_ENTRY2_2_4(SERPENT, "serpent");
399 17007 : MCRYPT_ENTRY2_2_4(THREEWAY, "threeway");
400 17007 : MCRYPT_ENTRY2_2_4(TRIPLEDES, "tripledes");
401 17007 : MCRYPT_ENTRY2_2_4(TWOFISH, "twofish");
402 17007 : MCRYPT_ENTRY2_2_4(WAKE, "wake");
403 17007 : MCRYPT_ENTRY2_2_4(XTEA, "xtea");
404 :
405 17007 : MCRYPT_ENTRY2_2_4(IDEA, "idea");
406 17007 : MCRYPT_ENTRY2_2_4(MARS, "mars");
407 17007 : MCRYPT_ENTRY2_2_4(RC6, "rc6");
408 17007 : MCRYPT_ENTRY2_2_4(SKIPJACK, "skipjack");
409 : /* modes */
410 17007 : MCRYPT_ENTRY2_2_4(MODE_CBC, "cbc");
411 17007 : MCRYPT_ENTRY2_2_4(MODE_CFB, "cfb");
412 17007 : MCRYPT_ENTRY2_2_4(MODE_ECB, "ecb");
413 17007 : MCRYPT_ENTRY2_2_4(MODE_NOFB, "nofb");
414 17007 : MCRYPT_ENTRY2_2_4(MODE_OFB, "ofb");
415 17007 : MCRYPT_ENTRY2_2_4(MODE_STREAM, "stream");
416 17007 : REGISTER_INI_ENTRIES();
417 17007 : return SUCCESS;
418 : }
419 : /* }}} */
420 :
421 : static PHP_MSHUTDOWN_FUNCTION(mcrypt) /* {{{ */
422 17039 : {
423 17039 : UNREGISTER_INI_ENTRIES();
424 17039 : return SUCCESS;
425 : }
426 : /* }}} */
427 :
428 : #include "ext/standard/php_smart_str.h"
429 :
430 : PHP_MINFO_FUNCTION(mcrypt) /* {{{ */
431 43 : {
432 : char **modules;
433 : char mcrypt_api_no[16];
434 : int i, count;
435 43 : smart_str tmp1 = {0};
436 43 : smart_str tmp2 = {0};
437 :
438 43 : modules = mcrypt_list_algorithms(MCG(algorithms_dir), &count);
439 43 : if (count == 0) {
440 0 : smart_str_appends(&tmp1, "none");
441 : }
442 860 : for (i = 0; i < count; i++) {
443 817 : smart_str_appends(&tmp1, modules[i]);
444 817 : smart_str_appendc(&tmp1, ' ');
445 : }
446 43 : smart_str_0(&tmp1);
447 43 : mcrypt_free_p(modules, count);
448 :
449 43 : modules = mcrypt_list_modes(MCG(modes_dir), &count);
450 43 : if (count == 0) {
451 0 : smart_str_appends(&tmp2, "none");
452 : }
453 387 : for (i = 0; i < count; i++) {
454 344 : smart_str_appends(&tmp2, modules[i]);
455 344 : smart_str_appendc(&tmp2, ' ');
456 : }
457 43 : smart_str_0 (&tmp2);
458 43 : mcrypt_free_p (modules, count);
459 :
460 43 : snprintf (mcrypt_api_no, 16, "%d", MCRYPT_API_VERSION);
461 :
462 43 : php_info_print_table_start();
463 43 : php_info_print_table_header(2, "mcrypt support", "enabled");
464 43 : php_info_print_table_row(2, "Version", LIBMCRYPT_VERSION);
465 43 : php_info_print_table_row(2, "Api No", mcrypt_api_no);
466 43 : php_info_print_table_row(2, "Supported ciphers", tmp1.c);
467 43 : php_info_print_table_row(2, "Supported modes", tmp2.c);
468 43 : smart_str_free(&tmp1);
469 43 : smart_str_free(&tmp2);
470 43 : php_info_print_table_end();
471 :
472 43 : DISPLAY_INI_ENTRIES();
473 43 : }
474 : /* }}} */
475 :
476 : /* {{{ proto resource mcrypt_module_open(string cipher, string cipher_directory, string mode, string mode_directory) U
477 : Opens the module of the algorithm and the mode to be used */
478 : PHP_FUNCTION(mcrypt_module_open)
479 40 : {
480 : char *cipher, *cipher_dir;
481 : char *mode, *mode_dir;
482 : int cipher_len, cipher_dir_len;
483 : int mode_len, mode_dir_len;
484 : MCRYPT td;
485 : php_mcrypt *pm;
486 :
487 40 : if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&s&s&s&",
488 : &cipher, &cipher_len, UG(ascii_conv),
489 : &cipher_dir, &cipher_dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv)),
490 : &mode, &mode_len, UG(ascii_conv),
491 : &mode_dir, &mode_dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv)))) {
492 0 : return;
493 : }
494 :
495 40 : td = mcrypt_module_open (
496 : cipher,
497 : cipher_dir_len > 0 ? cipher_dir : MCG(algorithms_dir),
498 : mode,
499 : mode_dir_len > 0 ? mode_dir : MCG(modes_dir)
500 : );
501 :
502 40 : if (td == MCRYPT_FAILED) {
503 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not open encryption module");
504 1 : RETURN_FALSE;
505 : } else {
506 39 : pm = emalloc(sizeof(php_mcrypt));
507 39 : pm->td = td;
508 39 : pm->init = 0;
509 39 : ZEND_REGISTER_RESOURCE(return_value, pm, le_mcrypt);
510 : }
511 : }
512 : /* }}} */
513 :
514 : /* {{{ proto int mcrypt_generic_init(resource td, binary key, binary iv) U
515 : This function initializes all buffers for the specific module */
516 : PHP_FUNCTION(mcrypt_generic_init)
517 47 : {
518 : zval *mcryptind;
519 : char *key, *iv;
520 : int max_key_size, req_iv_size, key_size, iv_size;
521 : php_mcrypt *pm;
522 47 : int result = 0;
523 :
524 47 : if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rSS", &mcryptind, &key, &key_size, &iv, &iv_size)) {
525 1 : return;
526 : }
527 46 : ZEND_FETCH_RESOURCE(pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt);
528 :
529 46 : max_key_size = mcrypt_enc_get_key_size(pm->td);
530 46 : req_iv_size = mcrypt_enc_get_iv_size(pm->td);
531 :
532 46 : if (!key_size) {
533 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key size is 0");
534 : }
535 :
536 46 : if (key_size > max_key_size) {
537 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key size too large; supplied length: %d, max: %d", key_size, max_key_size);
538 0 : key_size = max_key_size;
539 : }
540 :
541 46 : if (iv_size != req_iv_size) {
542 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Iv size incorrect; supplied length: %d, needed: %d", iv_size, req_iv_size);
543 : }
544 :
545 46 : mcrypt_generic_deinit(pm->td);
546 46 : result = mcrypt_generic_init(pm->td, key, key_size, iv);
547 46 : pm->init = 1;
548 :
549 : /* If this function fails, close the mcrypt module to prevent crashes
550 : * when further functions want to access this resource */
551 46 : if (result < 0) {
552 0 : zend_list_delete(Z_LVAL_P(mcryptind));
553 0 : switch (result) {
554 : case -3:
555 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key length incorrect");
556 0 : break;
557 : case -4:
558 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Memory allocation error");
559 0 : break;
560 : case -1:
561 : default:
562 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error");
563 : break;
564 : }
565 : }
566 46 : RETVAL_LONG(result);
567 : }
568 : /* }}} */
569 :
570 : /* {{{ proto binary mcrypt_generic(resource td, binary data) U
571 : This function encrypts the plaintext */
572 : PHP_FUNCTION(mcrypt_generic)
573 43 : {
574 : zval *mcryptind;
575 : php_mcrypt *pm;
576 : char *data_copy, *data_str;
577 : int block_size, data_size, data_len;
578 :
579 43 : if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rS", &mcryptind, &data_str, &data_len)) {
580 1 : return;
581 : }
582 42 : ZEND_FETCH_RESOURCE(pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt);
583 42 : PHP_MCRYPT_INIT_CHECK
584 :
585 40 : if (!data_len) {
586 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "An empty string was passed");
587 1 : RETURN_FALSE
588 : }
589 :
590 : /* Check blocksize */
591 39 : if (mcrypt_enc_is_block_mode(pm->td) == 1) {
592 : /* It's a block algorithm */
593 39 : block_size = mcrypt_enc_get_block_size(pm->td);
594 39 : data_size = (((data_len - 1) / block_size) + 1) * block_size;
595 39 : data_copy = ecalloc(1, data_size + 1);
596 39 : memcpy(data_copy, data_str, data_len);
597 : } else {
598 : /* It's not a block algorithm */
599 0 : data_copy = estrndup(data_str, data_size = data_len);
600 : }
601 :
602 39 : mcrypt_generic(pm->td, data_copy, data_size);
603 39 : data_copy[data_size] = '\0';
604 :
605 39 : RETVAL_STRINGL(data_copy, data_size, 0);
606 : }
607 : /* }}} */
608 :
609 : /* {{{ proto binary mdecrypt_generic(resource td, binary data) U
610 : This function decrypts the plaintext */
611 : PHP_FUNCTION(mdecrypt_generic)
612 7 : {
613 : zval *mcryptind;
614 : php_mcrypt *pm;
615 : char *data_str, *data_copy;
616 : int block_size, data_len, data_size;
617 :
618 7 : if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rS", &mcryptind, &data_str, &data_len)) {
619 0 : return;
620 : }
621 7 : ZEND_FETCH_RESOURCE(pm, php_mcrypt *, &mcryptind, -1, "MCrypt", le_mcrypt);
622 7 : PHP_MCRYPT_INIT_CHECK
623 :
624 6 : if (!data_len) {
625 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "An empty string was passed");
626 1 : RETURN_FALSE
627 : }
628 :
629 : /* Check blocksize */
630 5 : if (mcrypt_enc_is_block_mode(pm->td) == 1) {
631 : /* It's a block algorithm */
632 5 : block_size = mcrypt_enc_get_block_size(pm->td);
633 5 : data_size = (((data_len - 1) / block_size) + 1) * block_size;
634 5 : data_copy = ecalloc(1, data_size + 1);
635 5 : memcpy(data_copy, data_str, data_len);
636 : } else {
637 : /* It's not a block algorithm */
638 0 : data_copy = estrndup(data_str, data_size = data_len);
639 : }
640 :
641 5 : mdecrypt_generic(pm->td, data_copy, data_size);
642 :
643 5 : RETVAL_STRINGL(data_copy, data_size, 0);
644 : }
645 : /* }}} */
646 :
647 : /* {{{ proto array mcrypt_enc_get_supported_key_sizes(resource td) U
648 : This function decrypts the crypttext */
649 : PHP_FUNCTION(mcrypt_enc_get_supported_key_sizes)
650 1 : {
651 1 : int i, count = 0;
652 : int *key_sizes;
653 :
654 1 : MCRYPT_GET_TD_ARG
655 1 : array_init(return_value);
656 :
657 1 : key_sizes = mcrypt_enc_get_supported_key_sizes(pm->td, &count);
658 :
659 4 : for (i = 0; i < count; i++) {
660 3 : add_index_long(return_value, i, key_sizes[i]);
661 : }
662 :
663 1 : mcrypt_free(key_sizes);
664 : }
665 : /* }}} */
666 :
667 : /* {{{ proto int mcrypt_enc_self_test(resource td) U
668 : This function runs the self test on the algorithm specified by the descriptor td */
669 : PHP_FUNCTION(mcrypt_enc_self_test)
670 1 : {
671 1 : MCRYPT_GET_TD_ARG
672 1 : RETURN_LONG(mcrypt_enc_self_test(pm->td));
673 : }
674 : /* }}} */
675 :
676 : /* {{{ proto bool mcrypt_module_close(resource td) U
677 : Free the descriptor td */
678 : PHP_FUNCTION(mcrypt_module_close)
679 1 : {
680 1 : MCRYPT_GET_TD_ARG
681 1 : zend_list_delete(Z_LVAL_P(mcryptind));
682 1 : RETURN_TRUE;
683 : }
684 : /* }}} */
685 :
686 : /* {{{ proto bool mcrypt_generic_deinit(resource td) U
687 : This function terminates encrypt specified by the descriptor td */
688 : PHP_FUNCTION(mcrypt_generic_deinit)
689 7 : {
690 7 : MCRYPT_GET_TD_ARG
691 :
692 7 : if (mcrypt_generic_deinit(pm->td) < 0) {
693 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not terminate encryption specifier");
694 1 : RETURN_FALSE
695 : }
696 6 : pm->init = 0;
697 6 : RETURN_TRUE
698 : }
699 : /* }}} */
700 :
701 : /* {{{ proto bool mcrypt_enc_is_block_algorithm_mode(resource td) U
702 : Returns TRUE if the mode is for use with block algorithms */
703 : PHP_FUNCTION(mcrypt_enc_is_block_algorithm_mode)
704 3 : {
705 3 : MCRYPT_GET_TD_ARG
706 :
707 3 : if (mcrypt_enc_is_block_algorithm_mode(pm->td) == 1) {
708 2 : RETURN_TRUE
709 : } else {
710 1 : RETURN_FALSE
711 : }
712 : }
713 : /* }}} */
714 :
715 : /* {{{ proto bool mcrypt_enc_is_block_algorithm(resource td) U
716 : Returns TRUE if the alrogithm is a block algorithms */
717 : PHP_FUNCTION(mcrypt_enc_is_block_algorithm)
718 3 : {
719 3 : MCRYPT_GET_TD_ARG
720 :
721 3 : if (mcrypt_enc_is_block_algorithm(pm->td) == 1) {
722 2 : RETURN_TRUE
723 : } else {
724 1 : RETURN_FALSE
725 : }
726 : }
727 : /* }}} */
728 :
729 : /* {{{ proto bool mcrypt_enc_is_block_mode(resource td) U
730 : Returns TRUE if the mode outputs blocks */
731 : PHP_FUNCTION(mcrypt_enc_is_block_mode)
732 4 : {
733 4 : MCRYPT_GET_TD_ARG
734 :
735 4 : if (mcrypt_enc_is_block_mode(pm->td) == 1) {
736 2 : RETURN_TRUE
737 : } else {
738 2 : RETURN_FALSE
739 : }
740 : }
741 : /* }}} */
742 :
743 : /* {{{ proto int mcrypt_enc_get_block_size(resource td) U
744 : Returns the block size of the cipher specified by the descriptor td */
745 : PHP_FUNCTION(mcrypt_enc_get_block_size)
746 3 : {
747 3 : MCRYPT_GET_TD_ARG
748 3 : RETURN_LONG(mcrypt_enc_get_block_size(pm->td));
749 : }
750 : /* }}} */
751 :
752 : /* {{{ proto int mcrypt_enc_get_key_size(resource td) U
753 : Returns the maximum supported key size in bytes of the algorithm specified by the descriptor td */
754 : PHP_FUNCTION(mcrypt_enc_get_key_size)
755 3 : {
756 3 : MCRYPT_GET_TD_ARG
757 3 : RETURN_LONG(mcrypt_enc_get_key_size(pm->td));
758 : }
759 : /* }}} */
760 :
761 : /* {{{ proto int mcrypt_enc_get_iv_size(resource td) U
762 : Returns the size of the IV in bytes of the algorithm specified by the descriptor td */
763 : PHP_FUNCTION(mcrypt_enc_get_iv_size)
764 3 : {
765 3 : MCRYPT_GET_TD_ARG
766 3 : RETURN_LONG(mcrypt_enc_get_iv_size(pm->td));
767 : }
768 : /* }}} */
769 :
770 : /* {{{ proto string mcrypt_enc_get_algorithms_name(resource td) U
771 : Returns the name of the algorithm specified by the descriptor td */
772 : PHP_FUNCTION(mcrypt_enc_get_algorithms_name)
773 5 : {
774 : char *name;
775 5 : MCRYPT_GET_TD_ARG
776 :
777 5 : name = mcrypt_enc_get_algorithms_name(pm->td);
778 5 : RETVAL_ASCII_STRING(name, 1);
779 5 : mcrypt_free(name);
780 : }
781 : /* }}} */
782 :
783 : /* {{{ proto string mcrypt_enc_get_modes_name(resource td) U
784 : Returns the name of the mode specified by the descriptor td */
785 : PHP_FUNCTION(mcrypt_enc_get_modes_name)
786 6 : {
787 : char *name;
788 6 : MCRYPT_GET_TD_ARG
789 :
790 6 : name = mcrypt_enc_get_modes_name(pm->td);
791 6 : RETVAL_ASCII_STRING(name, 1);
792 6 : mcrypt_free(name);
793 : }
794 : /* }}} */
795 :
796 : /* {{{ proto bool mcrypt_module_self_test(string algorithm [, string lib_dir]) U
797 : Does a self test of the module "module" */
798 : PHP_FUNCTION(mcrypt_module_self_test)
799 3 : {
800 3 : MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir);
801 :
802 3 : if (mcrypt_module_self_test(module, dir) == 0) {
803 2 : RETURN_TRUE;
804 : } else {
805 1 : RETURN_FALSE;
806 : }
807 : }
808 : /* }}} */
809 :
810 : /* {{{ proto bool mcrypt_module_is_block_algorithm_mode(string mode [, string lib_dir]) U
811 : Returns TRUE if the mode is for use with block algorithms */
812 : PHP_FUNCTION(mcrypt_module_is_block_algorithm_mode)
813 19 : {
814 19 : MCRYPT_GET_MODE_DIR_ARGS(modes_dir)
815 :
816 19 : if (mcrypt_module_is_block_algorithm_mode(module, dir) == 1) {
817 18 : RETURN_TRUE;
818 : } else {
819 1 : RETURN_FALSE;
820 : }
821 : }
822 : /* }}} */
823 :
824 : /* {{{ proto bool mcrypt_module_is_block_algorithm(string algorithm [, string lib_dir]) U
825 : Returns TRUE if the algorithm is a block algorithm */
826 : PHP_FUNCTION(mcrypt_module_is_block_algorithm)
827 19 : {
828 19 : MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir)
829 :
830 19 : if (mcrypt_module_is_block_algorithm(module, dir) == 1) {
831 15 : RETURN_TRUE;
832 : } else {
833 4 : RETURN_FALSE;
834 : }
835 : }
836 : /* }}} */
837 :
838 : /* {{{ proto bool mcrypt_module_is_block_mode(string mode [, string lib_dir]) U
839 : Returns TRUE if the mode outputs blocks of bytes */
840 : PHP_FUNCTION(mcrypt_module_is_block_mode)
841 17 : {
842 17 : MCRYPT_GET_MODE_DIR_ARGS(modes_dir)
843 :
844 17 : if (mcrypt_module_is_block_mode(module, dir) == 1) {
845 2 : RETURN_TRUE;
846 : } else {
847 15 : RETURN_FALSE;
848 : }
849 : }
850 : /* }}} */
851 :
852 : /* {{{ proto int mcrypt_module_get_algo_block_size(string algorithm [, string lib_dir]) U
853 : Returns the block size of the algorithm */
854 : PHP_FUNCTION(mcrypt_module_get_algo_block_size)
855 6 : {
856 6 : MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir)
857 :
858 6 : RETURN_LONG(mcrypt_module_get_algo_block_size(module, dir));
859 : }
860 : /* }}} */
861 :
862 : /* {{{ proto int mcrypt_module_get_algo_key_size(string algorithm [, string lib_dir]) U
863 : Returns the maximum supported key size of the algorithm */
864 : PHP_FUNCTION(mcrypt_module_get_algo_key_size)
865 6 : {
866 6 : MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir);
867 :
868 6 : RETURN_LONG(mcrypt_module_get_algo_key_size(module, dir));
869 : }
870 : /* }}} */
871 :
872 : /* {{{ proto array mcrypt_module_get_supported_key_sizes(string algorithm [, string lib_dir]) U
873 : This function decrypts the crypttext */
874 : PHP_FUNCTION(mcrypt_module_get_supported_key_sizes)
875 2 : {
876 2 : int i, count = 0;
877 : int *key_sizes;
878 :
879 2 : MCRYPT_GET_MODE_DIR_ARGS(algorithms_dir)
880 2 : array_init(return_value);
881 :
882 2 : key_sizes = mcrypt_module_get_algo_supported_key_sizes(module, dir, &count);
883 :
884 5 : for (i = 0; i < count; i++) {
885 3 : add_index_long(return_value, i, key_sizes[i]);
886 : }
887 2 : mcrypt_free(key_sizes);
888 : }
889 : /* }}} */
890 :
891 : /* {{{ proto array mcrypt_list_algorithms([string lib_dir]) U
892 : List all algorithms in "module_dir" */
893 : PHP_FUNCTION(mcrypt_list_algorithms)
894 2 : {
895 : char **modules;
896 2 : char *lib_dir = MCG(algorithms_dir);
897 : int lib_dir_len;
898 : int i, count;
899 :
900 2 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s&",
901 : &lib_dir, &lib_dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv))) == FAILURE) {
902 0 : return;
903 : }
904 :
905 2 : array_init(return_value);
906 2 : modules = mcrypt_list_algorithms(lib_dir, &count);
907 :
908 2 : if (count == 0) {
909 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No algorithms found in module dir");
910 : }
911 40 : for (i = 0; i < count; i++) {
912 38 : add_index_ascii_string(return_value, i, modules[i], ZSTR_DUPLICATE);
913 : }
914 2 : mcrypt_free_p(modules, count);
915 : }
916 : /* }}} */
917 :
918 : /* {{{ proto array mcrypt_list_modes([string lib_dir]) U
919 : List all modes "module_dir" */
920 : PHP_FUNCTION(mcrypt_list_modes)
921 2 : {
922 : char **modules;
923 2 : char *lib_dir = MCG(modes_dir);
924 : int lib_dir_len;
925 : int i, count;
926 :
927 2 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s&",
928 : &lib_dir, &lib_dir_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv))) == FAILURE) {
929 0 : return;
930 : }
931 :
932 2 : array_init(return_value);
933 2 : modules = mcrypt_list_modes(lib_dir, &count);
934 :
935 2 : if (count == 0) {
936 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No modes found in module dir");
937 : }
938 18 : for (i = 0; i < count; i++) {
939 16 : add_index_ascii_string(return_value, i, modules[i], ZSTR_DUPLICATE);
940 : }
941 2 : mcrypt_free_p(modules, count);
942 : }
943 : /* }}} */
944 :
945 : /* {{{ proto int mcrypt_get_key_size(string cipher, string module) U
946 : Get the key size of cipher */
947 : PHP_FUNCTION(mcrypt_get_key_size)
948 3 : {
949 : char *cipher;
950 : char *module;
951 : int cipher_len, module_len;
952 : char *cipher_dir_string;
953 : char *module_dir_string;
954 : MCRYPT td;
955 :
956 3 : MCRYPT_GET_INI
957 :
958 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&s&",
959 : &cipher, &cipher_len, UG(ascii_conv), &module, &module_len, UG(ascii_conv)) == FAILURE) {
960 0 : return;
961 : }
962 :
963 3 : td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string);
964 3 : if (td != MCRYPT_FAILED) {
965 3 : RETVAL_LONG(mcrypt_enc_get_key_size(td));
966 3 : mcrypt_module_close(td);
967 : } else {
968 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
969 0 : RETURN_FALSE;
970 : }
971 : }
972 : /* }}} */
973 :
974 : /* {{{ proto int mcrypt_get_block_size(string cipher, string module) U
975 : Get the key size of cipher */
976 : PHP_FUNCTION(mcrypt_get_block_size)
977 3 : {
978 : char *cipher;
979 : char *module;
980 : int cipher_len, module_len;
981 : char *cipher_dir_string;
982 : char *module_dir_string;
983 : MCRYPT td;
984 :
985 3 : MCRYPT_GET_INI
986 :
987 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&s&",
988 : &cipher, &cipher_len, UG(ascii_conv), &module, &module_len, UG(ascii_conv)) == FAILURE) {
989 0 : return;
990 : }
991 :
992 3 : td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string);
993 3 : if (td != MCRYPT_FAILED) {
994 3 : RETVAL_LONG(mcrypt_enc_get_block_size(td));
995 3 : mcrypt_module_close(td);
996 : } else {
997 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
998 0 : RETURN_FALSE;
999 : }
1000 : }
1001 : /* }}} */
1002 :
1003 : /* {{{ proto int mcrypt_get_iv_size(string cipher, string module) U
1004 : Get the IV size of cipher (Usually the same as the blocksize) */
1005 : PHP_FUNCTION(mcrypt_get_iv_size)
1006 24 : {
1007 : char *cipher;
1008 : char *module;
1009 : int cipher_len, module_len;
1010 : char *cipher_dir_string;
1011 : char *module_dir_string;
1012 : MCRYPT td;
1013 :
1014 24 : MCRYPT_GET_INI
1015 :
1016 24 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&s&",
1017 : &cipher, &cipher_len, UG(ascii_conv), &module, &module_len, UG(ascii_conv)) == FAILURE) {
1018 0 : return;
1019 : }
1020 :
1021 24 : td = mcrypt_module_open(cipher, cipher_dir_string, module, module_dir_string);
1022 24 : if (td != MCRYPT_FAILED) {
1023 23 : RETVAL_LONG(mcrypt_enc_get_iv_size(td));
1024 23 : mcrypt_module_close(td);
1025 : } else {
1026 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
1027 1 : RETURN_FALSE;
1028 : }
1029 : }
1030 : /* }}} */
1031 :
1032 : /* {{{ proto string mcrypt_get_cipher_name(string cipher) U
1033 : Get the name of cipher */
1034 : PHP_FUNCTION(mcrypt_get_cipher_name)
1035 4 : {
1036 : char *cipher_dir_string;
1037 : char *module_dir_string;
1038 : char *cipher_name;
1039 : char *cipher;
1040 : int cipher_len;
1041 : MCRYPT td;
1042 :
1043 4 : MCRYPT_GET_INI
1044 :
1045 4 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&",
1046 : &cipher, &cipher_len, UG(ascii_conv)) == FAILURE) {
1047 0 : return;
1048 : }
1049 :
1050 : /* The code below is actually not very nice, but I didn't see a better
1051 : * method */
1052 4 : td = mcrypt_module_open(cipher, cipher_dir_string, "ecb", module_dir_string);
1053 4 : if (td != MCRYPT_FAILED) {
1054 2 : cipher_name = mcrypt_enc_get_algorithms_name(td);
1055 2 : mcrypt_module_close(td);
1056 2 : RETVAL_ASCII_STRING(cipher_name,1);
1057 2 : mcrypt_free(cipher_name);
1058 : } else {
1059 2 : td = mcrypt_module_open(cipher, cipher_dir_string, "stream", module_dir_string);
1060 2 : if (td != MCRYPT_FAILED) {
1061 2 : cipher_name = mcrypt_enc_get_algorithms_name(td);
1062 2 : mcrypt_module_close(td);
1063 2 : RETVAL_ASCII_STRING(cipher_name,1);
1064 2 : mcrypt_free(cipher_name);
1065 : } else {
1066 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
1067 0 : RETURN_FALSE;
1068 : }
1069 : }
1070 : }
1071 : /* }}} */
1072 :
1073 : /* {{{ inline _php_mcrypt_func */
1074 : static inline void _php_mcrypt_func(INTERNAL_FUNCTION_PARAMETERS, long op, char *mode_str, int mode_len)
1075 656 : {
1076 656 : char *cipher_str, *key_str, *iv_str = NULL, *data_str, *data_copy = NULL;
1077 656 : int cipher_len, key_len, iv_len = 0, data_len, data_size = 0;
1078 :
1079 656 : if (mode_len) {
1080 313 : if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&SSl|S",
1081 : &cipher_str, &cipher_len, UG(ascii_conv),
1082 : &key_str, &key_len, &data_str, &data_len,
1083 : &op, &iv_str, &iv_len)) {
1084 96 : return;
1085 : }
1086 : } else {
1087 343 : if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&SSs&|S",
1088 : &cipher_str, &cipher_len, UG(ascii_conv),
1089 : &key_str, &key_len, &data_str, &data_len,
1090 : &mode_str, &mode_len, UG(ascii_conv),
1091 : &iv_str, &iv_len)) {
1092 83 : return;
1093 : }
1094 : }
1095 :
1096 477 : if (SUCCESS != php_mcrypt_func(op, cipher_str, mode_str, key_str, key_len, iv_str, iv_len, data_str, data_len, &data_copy, &data_size TSRMLS_CC)) {
1097 120 : RETURN_FALSE;
1098 : }
1099 356 : RETURN_STRINGL(data_copy, data_size, 0);
1100 : }
1101 : /* }}} */
1102 :
1103 : /* {{{ proto string mcrypt_encrypt(string cipher, string key, string data, string mode, string iv) U
1104 : OFB crypt/decrypt data using key key with cipher cipher starting with iv */
1105 : PHP_FUNCTION(mcrypt_encrypt)
1106 173 : {
1107 173 : _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MCRYPT_ENCRYPT, NULL, 0);
1108 172 : }
1109 : /* }}} */
1110 :
1111 : /* {{{ proto string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv) U
1112 : OFB crypt/decrypt data using key key with cipher cipher starting with iv */
1113 : PHP_FUNCTION(mcrypt_decrypt)
1114 170 : {
1115 170 : _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MCRYPT_DECRYPT, NULL, 0);
1116 170 : }
1117 : /* }}} */
1118 :
1119 : /* {{{ proto string mcrypt_ecb(int cipher, string key, string data, int mode, string iv) U
1120 : ECB crypt/decrypt data using key key with cipher cipher starting with iv */
1121 : PHP_FUNCTION(mcrypt_ecb)
1122 149 : {
1123 149 : _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, ZEND_STRL("ecb"));
1124 149 : }
1125 : /* }}} */
1126 :
1127 : /* {{{ proto string mcrypt_cbc(int cipher, string key, string data, int mode, string iv) U
1128 : CBC crypt/decrypt data using key key with cipher cipher starting with iv */
1129 : PHP_FUNCTION(mcrypt_cbc)
1130 158 : {
1131 158 : _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, ZEND_STRL("cbc"));
1132 158 : }
1133 : /* }}} */
1134 :
1135 : /* {{{ proto string mcrypt_cfb(int cipher, string key, string data, int mode, string iv) U
1136 : CFB crypt/decrypt data using key key with cipher cipher starting with iv */
1137 : PHP_FUNCTION(mcrypt_cfb)
1138 3 : {
1139 3 : _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, ZEND_STRL("cfb"));
1140 3 : }
1141 : /* }}} */
1142 :
1143 : /* {{{ proto string mcrypt_ofb(int cipher, string key, string data, int mode, string iv) U
1144 : OFB crypt/decrypt data using key key with cipher cipher starting with iv */
1145 : PHP_FUNCTION(mcrypt_ofb)
1146 3 : {
1147 3 : _php_mcrypt_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, ZEND_STRL("ofb"));
1148 3 : }
1149 : /* }}} */
1150 :
1151 : /* {{{ proto binary mcrypt_create_iv(int size, int source) U
1152 : Create an initialization vector (IV) */
1153 : PHP_FUNCTION(mcrypt_create_iv)
1154 20 : {
1155 : char *iv_str;
1156 : int iv_len;
1157 20 : long size, source = PHP_MCRYPT_IV_SOURCE_RANDOM;
1158 :
1159 20 : if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &size, &source)) {
1160 0 : return;
1161 : }
1162 20 : if (SUCCESS != php_mcrypt_iv(source, size, &iv_str, &iv_len TSRMLS_CC)) {
1163 0 : RETURN_FALSE;
1164 : }
1165 20 : RETURN_STRINGL(iv_str, iv_len, 0);
1166 : }
1167 : /* }}} */
1168 :
1169 : /* {{{ php_mcrypt_iv */
1170 : int php_mcrypt_iv(php_mcrypt_iv_source source, int size, char **iv_str, int *iv_len TSRMLS_DC)
1171 20 : {
1172 20 : if (size <= 0 || size >= INT_MAX) {
1173 0 : 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);
1174 0 : return FAILURE;
1175 : }
1176 :
1177 20 : *iv_str = ecalloc(size + 1, 1);
1178 :
1179 20 : switch (source) {
1180 : case PHP_MCRYPT_IV_SOURCE_RANDOM:
1181 : case PHP_MCRYPT_IV_SOURCE_URANDOM: {
1182 : #if PHP_WIN32
1183 : /* random/urandom equivalent on Windows */
1184 : HCRYPTPROV hCryptProv;
1185 : BYTE *iv = (BYTE *) *iv_str;
1186 :
1187 : /* It could be done using LoadLibrary but as we rely on 2k+ for 5.3, cleaner to use a clear dependency (Advapi32) and a
1188 : standard API call (no f=getAddr..; f();) */
1189 : if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
1190 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot open random device");
1191 : return FAILURE;
1192 : }
1193 : if(!CryptGenRandom(hCryptProv, size, iv)) {
1194 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not gather sufficient random data");
1195 : return FAILURE;
1196 : }
1197 : *iv_len = size;
1198 : #else
1199 14 : size_t read_bytes = 0;
1200 : int fd, n;
1201 :
1202 14 : fd = open(source == PHP_MCRYPT_IV_SOURCE_RANDOM ? "/dev/random" : "/dev/urandom", O_RDONLY);
1203 14 : if (fd < 0) {
1204 0 : efree(*iv_str);
1205 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot open source device");
1206 0 : return FAILURE;
1207 : }
1208 :
1209 63 : while (read_bytes < size) {
1210 35 : n = read(fd, *iv_str + read_bytes, size - read_bytes);
1211 35 : if (n < 0) {
1212 0 : break;
1213 : }
1214 35 : read_bytes += n;
1215 : }
1216 14 : *iv_len = read_bytes;
1217 14 : close(fd);
1218 :
1219 14 : if (*iv_len < size) {
1220 0 : efree(*iv_str);
1221 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not gather sufficient random data");
1222 0 : return FAILURE;
1223 : }
1224 : #endif
1225 14 : break;
1226 : }
1227 : case PHP_MCRYPT_IV_SOURCE_RAND:
1228 6 : *iv_len = size;
1229 108 : while (size) {
1230 96 : (*iv_str)[--size] = (char) (255.0 * php_rand(TSRMLS_C) / RAND_MAX);
1231 : }
1232 : break;
1233 : }
1234 :
1235 20 : return SUCCESS;
1236 : }
1237 : /* }}} */
1238 :
1239 : /* {{{ php_mcrypt */
1240 : int php_mcrypt_func(php_mcrypt_op op, char *cipher, char *mode, char *key_str, int key_len, char *iv_str, int iv_len, char *data_str, int data_len, char **data_copy, int *data_size TSRMLS_DC)
1241 477 : {
1242 : MCRYPT td;
1243 477 : char *cipher_dir_string, *module_dir_string, *key_copy, *iv_copy = NULL;
1244 477 : int i, status = SUCCESS, count, *key_sizes, key_size, iv_size, block_size;
1245 :
1246 477 : MCRYPT_GET_INI
1247 :
1248 477 : td = mcrypt_module_open(cipher, cipher_dir_string, mode, module_dir_string);
1249 477 : if (td == MCRYPT_FAILED) {
1250 120 : php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_OPEN_MODULE_FAILED);
1251 120 : return FAILURE;
1252 : }
1253 :
1254 357 : if ((key_size = mcrypt_enc_get_key_size(td)) < key_len) {
1255 9 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Size of key is too large for this algorithm");
1256 : }
1257 :
1258 357 : key_sizes = mcrypt_enc_get_supported_key_sizes(td, &count);
1259 357 : switch (count) {
1260 : case 0:
1261 1 : key_copy = estrndup(key_str, key_len);
1262 1 : break;
1263 : case 1:
1264 304 : key_copy = ecalloc(1, key_sizes[0]);
1265 304 : memcpy(key_copy, key_str, MIN(key_len, key_sizes[0]));
1266 304 : key_len = key_sizes[0];
1267 304 : break;
1268 : default:
1269 67 : for (i = 0; i < count; ++i) {
1270 66 : if (key_sizes[i] >= key_len && key_sizes[i] <= key_size) {
1271 51 : key_copy = ecalloc(1, key_sizes[i]);
1272 51 : memcpy(key_copy, key_str, MIN(key_len, key_sizes[i]));
1273 51 : key_len = key_sizes[i];
1274 51 : break;
1275 : }
1276 : }
1277 : break;
1278 : }
1279 357 : mcrypt_free(key_sizes);
1280 :
1281 357 : iv_size = mcrypt_enc_get_iv_size(td);
1282 : /* IV is required */
1283 357 : if (mcrypt_enc_mode_has_iv(td) == 1) {
1284 187 : if (iv_len) {
1285 165 : if (iv_len == iv_size) {
1286 118 : iv_copy = estrndup(iv_str, iv_len);
1287 : } else {
1288 47 : php_error_docref(NULL TSRMLS_CC, E_WARNING, MCRYPT_IV_WRONG_SIZE);
1289 47 : iv_copy = ecalloc(1, iv_size);
1290 47 : memcpy(iv_copy, iv_str, MIN(iv_len, iv_size));
1291 : }
1292 : } else {
1293 22 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to use an empty IV, which is NOT recommended");
1294 22 : iv_copy = ecalloc(1, iv_size);
1295 : }
1296 : }
1297 :
1298 357 : if (mcrypt_enc_is_block_mode(td) == 1) {
1299 327 : block_size = mcrypt_enc_get_block_size(td);
1300 327 : *data_size = (((data_len - 1) / block_size) + 1) * block_size;
1301 327 : *data_copy = ecalloc(1, *data_size + 1);
1302 327 : memcpy(*data_copy, data_str, data_len);
1303 : } else {
1304 30 : *data_copy = estrndup(data_str, *data_size = data_len);
1305 : }
1306 :
1307 357 : if (mcrypt_generic_init(td, key_copy, key_len, iv_copy) >= 0) {
1308 356 : switch (op) {
1309 : case PHP_MCRYPT_ENCRYPT:
1310 228 : if (mcrypt_generic(td, *data_copy, *data_size)) {
1311 0 : efree(*data_copy);
1312 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "encryption failed");
1313 0 : status = FAILURE;
1314 : }
1315 228 : break;
1316 : case PHP_MCRYPT_DECRYPT:
1317 120 : if (mdecrypt_generic(td, *data_copy, *data_size)) {
1318 0 : efree(*data_copy);
1319 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "decryption failed");
1320 0 : status = FAILURE;
1321 : }
1322 : break;
1323 : }
1324 : } else {
1325 1 : php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Mcrypt initialisation failed"); /* huh? error? */
1326 0 : status = FAILURE;
1327 : }
1328 :
1329 356 : efree(key_copy);
1330 356 : if (iv_copy) {
1331 186 : efree(iv_copy);
1332 : }
1333 :
1334 356 : return status;
1335 : }
1336 : /* }}} */
1337 :
1338 : #endif
1339 :
1340 : /*
1341 : * Local variables:
1342 : * tab-width: 4
1343 : * c-basic-offset: 4
1344 : * End:
1345 : * vim600: sw=4 ts=4 fdm=marker
1346 : * vim<600: sw=4 ts=4
1347 : */
|