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