blob: 2d3f82dc89464f91161e64f4074a80e598607bab [file] [log] [blame]
Andrey Andreev818aedb2014-02-03 11:30:25 +02001<?php
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.2.4 or newer
6 *
7 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
19 * @package CodeIgniter
20 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
23 * @link http://codeigniter.com
24 * @since Version 3.0
25 * @filesource
26 */
27defined('BASEPATH') OR exit('No direct script access allowed');
28
29/**
30 * CodeIgniter Encryption Class
31 *
32 * Provides two-way keyed encryption via PHP's MCrypt and/or OpenSSL extensions.
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
37 * @author Andrey Andreev
38 * @link http://codeigniter.com/user_guide/libraries/encryption.html
39 */
40class CI_Encryption {
41
42 /**
43 * Encryption cipher
44 *
45 * @var string
46 */
47 protected $_cipher = 'rijndael-128';
48
49 /**
50 * Cipher mode
51 *
52 * @var string
53 */
54 protected $_mode = 'cbc';
55
56 /**
57 * Cipher handle
58 *
59 * @var mixed
60 */
61 protected $_handle;
62
63 /**
64 * Encryption key
65 *
66 * @var string
67 */
68 protected $_key;
69
70 /**
71 * PHP extension to be used
72 *
73 * @var string
74 */
75 protected $_driver;
76
77 /**
78 * List of usable drivers (PHP extensions)
79 *
80 * @var array
81 */
82 protected $_drivers = array();
83
Andrey Andreev29cade42014-02-04 14:05:58 +020084 /**
85 * List of supported HMAC algorightms
86 *
87 * name => digest size pairs
88 *
89 * @var array
90 */
91 protected $_digests = array(
92 'sha224' => 28,
93 'sha256' => 32,
94 'sha384' => 48,
95 'sha512' => 64
96 );
97
Andrey Andreev818aedb2014-02-03 11:30:25 +020098 // --------------------------------------------------------------------
99
100 /**
101 * Class constructor
102 *
103 * @param array $params Configuration parameters
104 * @return void
105 */
106 public function __construct(array $params = array())
107 {
108 $this->_drivers = array(
109 'mcrypt' => extension_loaded('mcrypt'),
110 // While OpenSSL is available for PHP 5.3.0, an IV parameter
111 // for the encrypt/decrypt functions is only available since 5.3.3
112 'openssl' => (is_php('5.3.3') && extension_loaded('openssl'))
113 );
114
115 if ( ! $this->_drivers['mcrypt'] && ! $this->_drivers['openssl'])
116 {
117 return show_error('Encryption: Unable to find an available encryption driver.');
118 }
Andrey Andreev29cade42014-02-04 14:05:58 +0200119 // Our configuration validates against the existence of MCRYPT_MODE_* constants,
120 // but MCrypt supports CTR mode without actually having a constant for it, so ...
121 elseif ($this->_drivers['mcrypt'] && ! defined('MCRYPT_MODE_CTR'))
122 {
123 define('MCRYPT_MODE_CTR', 'ctr');
124 }
Andrey Andreev818aedb2014-02-03 11:30:25 +0200125
126 $this->initialize($params);
127
Andrey Andreev818aedb2014-02-03 11:30:25 +0200128 isset($this->_key) OR $this->_key = config_item('encryption_key');
129 if (empty($this->_key))
130 {
131 return show_error('Encryption: You are required to set an encryption key in your configuration.');
132 }
133
134 log_message('debug', 'Encryption Class Initialized');
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Initialize
141 *
142 * @param array $params Configuration parameters
143 * @return CI_Encryption
144 */
145 public function initialize(array $params)
146 {
147 if ( ! empty($params['driver']))
148 {
149 if (isset($this->_drivers[$params['driver']]))
150 {
Andrey Andreev50ccc382014-02-04 23:30:06 +0200151 if ($this->_drivers[$params['driver']])
Andrey Andreev818aedb2014-02-03 11:30:25 +0200152 {
153 $this->_driver = $params['driver'];
154 }
155 else
156 {
157 log_message('error', "Encryption: Driver '".$params['driver']."' is not available.");
158 }
159 }
160 else
161 {
162 log_message('error', "Encryption: Unknown driver '".$params['driver']."' cannot be configured.");
163 }
164 }
165
Andrey Andreev912831f2014-02-04 17:21:37 +0200166 if (empty($this->_driver))
167 {
168 $this->_driver = ($this->_drivers['mcrypt'] === TRUE)
169 ? 'mcrypt'
170 : 'openssl';
171
172 log_message('debug', "Encryption: Auto-configured driver '".$this->_driver."'.");
173 }
174
Andrey Andreev818aedb2014-02-03 11:30:25 +0200175 empty($params['key']) OR $this->_key = $params['key'];
176 $this->{'_'.$this->_driver.'_initialize'}($params);
177 return $this;
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Initialize MCrypt
184 *
185 * @param array $params Configuration parameters
186 * @return void
187 */
188 protected function _mcrypt_initialize($params)
189 {
190 if ( ! empty($params['cipher']))
191 {
192 $params['cipher'] = strtolower($params['cipher']);
193 $this->_cipher_alias($params['cipher']);
194
195 if ( ! in_array($params['cipher'], mcrypt_list_algorithms(), TRUE))
196 {
197 log_message('error', 'Encryption: MCrypt cipher '.strtoupper($params['cipher']).' is not available.');
198 }
199 else
200 {
201 $this->_cipher = $params['cipher'];
202 }
203 }
204
205 if ( ! empty($params['mode']))
206 {
207 if ( ! defined('MCRYPT_MODE_'.$params['mode']))
208 {
209 log_message('error', 'Encryption: MCrypt mode '.strtotupper($params['mode']).' is not available.');
210 }
211 else
212 {
213 $this->_mode = constant('MCRYPT_MODE_'.$params['mode']);
214 }
215 }
216
217 if (isset($this->_cipher, $this->_mode))
218 {
219 if (is_resource($this->_handle)
220 && (strtolower(mcrypt_enc_get_algorithms_name($this->_handle)) !== $this->_cipher
221 OR strtolower(mcrypt_enc_get_modes_name($this->_handle)) !== $this->_mode)
222 )
223 {
224 mcrypt_module_close($this->_handle);
225 }
226
227 if ($this->_handle = mcrypt_module_open($this->_cipher, '', $this->_mode, ''))
228 {
229 log_message('debug', 'Encryption: MCrypt cipher '.strtoupper($this->_cipher).' initialized in '.strtoupper($this->_mode).' mode.');
230 }
231 else
232 {
233 log_message('error', 'Encryption: Unable to initialize MCrypt with cipher '.strtoupper($this->_cipher).' in '.strtoupper($this->_mode).' mode.');
234 }
235 }
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Initialize OpenSSL
242 *
243 * @param array $params Configuration parameters
244 * @return void
245 */
246 protected function _openssl_initialize($params)
247 {
248 if ( ! empty($params['cipher']))
249 {
250 $params['cipher'] = strtolower($params['cipher']);
251 $this->_cipher_alias($params['cipher']);
252 $this->_cipher = $params['cipher'];
253 }
254
255 if ( ! empty($params['mode']))
256 {
257 $this->_mode = strtolower($params['mode']);
258 }
259
260 if (isset($this->_cipher, $this->_mode))
261 {
262 // OpenSSL methods aren't suffixed with '-stream' for this mode
263 $handle = ($this->_mode === 'stream')
264 ? $this->_cipher
265 : $this->_cipher.'-'.$this->_mode;
266
Andrey Andreev50ccc382014-02-04 23:30:06 +0200267 if ( ! in_array($handle, openssl_get_cipher_methods(), TRUE))
Andrey Andreev818aedb2014-02-03 11:30:25 +0200268 {
269 $this->_handle = NULL;
270 log_message('error', 'Encryption: Unable to initialize OpenSSL with method '.strtoupper($handle).'.');
271 }
272 else
273 {
274 $this->_handle = $handle;
275 log_message('debug', 'Encryption: OpenSSL initialized with method '.strtoupper($handle).'.');
276 }
277 }
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Encrypt
284 *
285 * @param string $data Input data
286 * @param array $params Input parameters
287 * @return string
288 */
289 public function encrypt($data, array $params = NULL)
290 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200291 if (($params = $this->_get_params($params)) === FALSE)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200292 {
293 return FALSE;
294 }
295 elseif ( ! isset($params['iv']))
296 {
297 $params['iv'] = ($iv_size = $this->{'_'.$this->_driver.'_get_iv_size'}($params['handle']))
298 ? $this->{'_'.$this->_driver.'_get_iv'}($iv_size)
299 : NULL;
300 }
301
302 if (($data = $this->{'_'.$this->_driver.'_encrypt'}($data, $params)) === FALSE)
303 {
304 return FALSE;
305 }
306
Andrey Andreev29cade42014-02-04 14:05:58 +0200307 if ($params['base64'])
308 {
309 $data = base64_encode($data);
310 }
311
312 if ($params['hmac'] !== FALSE)
313 {
314 if ( ! isset($params['hmac']['key']))
315 {
316 $params['hmac']['key'] = $this->hkdf(
317 $params['key'],
318 $params['hmac']['digest'],
319 NULL,
320 NULL,
321 'authentication'
322 );
323 }
324
Andrey Andreev177144f2014-02-04 18:07:34 +0200325 return hash_hmac($params['hmac']['digest'], $data, $params['hmac']['key'], ! $params['base64']).$data;
Andrey Andreev29cade42014-02-04 14:05:58 +0200326 }
327
328 return $data;
Andrey Andreev818aedb2014-02-03 11:30:25 +0200329 }
330
331 // --------------------------------------------------------------------
332
333 /**
334 * Encrypt via MCrypt
335 *
336 * @param string $data Input data
337 * @param array $params Input parameters
338 * @return string
339 */
340 protected function _mcrypt_encrypt($data, $params)
341 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200342 if ( ! is_resource($params['handle']))
343 {
344 return FALSE;
345 }
346 elseif (mcrypt_generic_init($params['handle'], $params['key'], $params['iv']) < 0)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200347 {
348 if ($params['handle'] !== $this->_handle)
349 {
350 mcrypt_module_close($params['handle']);
351 }
352
353 return FALSE;
354 }
355
356 // Use PKCS#7 padding in order to ensure compatibility with OpenSSL
357 // and other implementations outside of PHP
358 $block_size = mcrypt_enc_get_block_size($params['handle']);
359 $pad = $block_size - (strlen($data) % $block_size);
360 $data .= str_repeat(chr($pad), $pad);
361
362 $data = $params['iv'].mcrypt_generic($params['handle'], $data);
363
364 mcrypt_generic_deinit($params['handle']);
365 if ($params['handle'] !== $this->_handle)
366 {
Andrey Andreev50ccc382014-02-04 23:30:06 +0200367 mcrypt_module_close($params['handle']);
Andrey Andreev818aedb2014-02-03 11:30:25 +0200368 }
369
370 return $data;
371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * Encrypt via OpenSSL
377 *
378 * @param string $data Input data
379 * @param array $params Input parameters
380 * @return string
381 */
382 protected function _openssl_encrypt($data, $params)
383 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200384 if (empty($params['handle']))
385 {
386 return FALSE;
387 }
388
Andrey Andreev818aedb2014-02-03 11:30:25 +0200389 $data = openssl_encrypt(
390 $data,
391 $params['handle'],
392 $params['key'],
393 1, // DO NOT TOUCH!
394 $params['iv']
395 );
396
397 if ($data === FALSE)
398 {
399 return FALSE;
400 }
401
402 return $params['iv'].$data;
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Decrypt
409 *
410 * @param string $data Encrypted data
411 * @param array $params Input parameters
412 * @return string
413 */
414 public function decrypt($data, array $params = NULL)
415 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200416 if (($params = $this->_get_params($params)) === FALSE)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200417 {
418 return FALSE;
419 }
Andrey Andreev29cade42014-02-04 14:05:58 +0200420
421 if ($params['hmac'] !== FALSE)
422 {
423 if ( ! isset($params['hmac']['key']))
424 {
425 $params['hmac']['key'] = $this->hkdf(
426 $params['key'],
427 $params['hmac']['digest'],
428 NULL,
429 NULL,
430 'authentication'
431 );
432 }
433
434 // This might look illogical, but it is done during encryption as well ...
Andrey Andreev177144f2014-02-04 18:07:34 +0200435 // The 'base64' value is effectively an inverted "raw data" parameter
Andrey Andreev29cade42014-02-04 14:05:58 +0200436 $digest_size = ($params['base64'])
437 ? $this->_digests[$params['hmac']['digest']] * 2
438 : $this->_digests[$params['hmac']['digest']];
439 $hmac = substr($data, 0, $digest_size);
440 $data = substr($data, $digest_size);
441
Andrey Andreev177144f2014-02-04 18:07:34 +0200442 if ($hmac !== hash_hmac($params['hmac']['digest'], $data, $params['hmac']['key'], ! $params['base64']))
Andrey Andreev29cade42014-02-04 14:05:58 +0200443 {
444 return FALSE;
445 }
446 }
447
448 if ($params['base64'])
Andrey Andreev818aedb2014-02-03 11:30:25 +0200449 {
450 $data = base64_decode($data);
451 }
452
453 if ( ! isset($params['iv']))
454 {
Andrey Andreev50ccc382014-02-04 23:30:06 +0200455 $iv_size = $this->{'_'.$this->_driver.'_get_iv_size'}($params['handle']);
456 if ($iv_size = $this->{'_'.$this->_driver.'_get_iv_size'}($params['handle']))
Andrey Andreev818aedb2014-02-03 11:30:25 +0200457 {
458 $params['iv'] = substr($data, 0, $iv_size);
459 $data = substr($data, $iv_size);
460 }
461 else
462 {
463 $params['iv'] = NULL;
464 }
465 }
466 elseif (strncmp($params['iv'], $data, $iv_size = strlen($params['iv'])) === 0)
467 {
468 $data = substr($data, $iv_size);
469 }
470
471 return $this->{'_'.$this->_driver.'_decrypt'}($data, $params);
472 }
473
474 // --------------------------------------------------------------------
475
476 /**
477 * Decrypt via MCrypt
478 *
479 * @param string $data Encrypted data
480 * @param array $params Input parameters
481 * @return string
482 */
483 protected function _mcrypt_decrypt($data, $params)
484 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200485 if ( ! is_resource($params['handle']))
486 {
487 return FALSE;
488 }
489 elseif (mcrypt_generic_init($params['handle'], $params['key'], $params['iv']) < 0)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200490 {
491 if ($params['handle'] !== $this->_handle)
492 {
493 mcrypt_module_close($params['handle']);
494 }
495
496 return FALSE;
497 }
498
499 $data = mdecrypt_generic($params['handle'], $data);
500
501 mcrypt_generic_deinit($params['handle']);
502 if ($params['handle'] !== $this->_handle)
503 {
Andrey Andreev50ccc382014-02-04 23:30:06 +0200504 mcrypt_module_close($params['handle']);
Andrey Andreev818aedb2014-02-03 11:30:25 +0200505 }
506
507 // Remove PKCS#7 padding
508 return substr($data, 0, -ord($data[strlen($data)-1]));
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * Decrypt via OpenSSL
515 *
516 * @param string $data Encrypted data
517 * @param array $params Input parameters
518 * @return string
519 */
520 protected function _openssl_decrypt($data, $params)
521 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200522 return empty($params['handle'])
523 ? FALSE
524 : openssl_decrypt(
525 $data,
526 $params['handle'],
527 $params['key'],
528 1, // DO NOT TOUCH!
529 $params['iv']
530 );
Andrey Andreev818aedb2014-02-03 11:30:25 +0200531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Get IV size via MCrypt
537 *
538 * @param resource $handle MCrypt module resource
539 * @return int
540 */
541 protected function _mcrypt_get_iv_size($handle)
542 {
543 return mcrypt_enc_get_iv_size($handle);
544 }
545
546 // --------------------------------------------------------------------
547
548 /**
549 * Get IV size via OpenSSL
550 *
551 * @param string $handle OpenSSL cipher method
552 * @return int
553 */
554 protected function _openssl_get_iv_size($handle)
555 {
556 return openssl_cipher_iv_length($handle);
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * Get IV via MCrypt
563 *
564 * @param int $size
565 * @return int
566 */
567 protected function _mcrypt_get_iv($size)
568 {
569 // If /dev/urandom is available - use it, otherwise there's
570 // also /dev/random, but it is highly unlikely that it would
571 // be available while /dev/urandom is not and it is known to be
572 // blocking anyway.
573 if (defined(MCRYPT_DEV_URANDOM))
574 {
575 $source = MCRYPT_DEV_URANDOM;
576 }
577 else
578 {
579 $source = MCRYPT_RAND;
580 is_php('5.3') OR srand(microtime(TRUE));
581 }
582
583 return mcrypt_create_iv($size, $source);
584 }
585
586 // --------------------------------------------------------------------
587
588 /**
589 * Get IV via OpenSSL
590 *
591 * @param int $size IV size
592 * @return int
593 */
594 protected function _openssl_get_iv($size)
595 {
596 return openssl_random_pseudo_bytes($size);
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
Andrey Andreev29cade42014-02-04 14:05:58 +0200602 * Get params
Andrey Andreev818aedb2014-02-03 11:30:25 +0200603 *
604 * @param array $params Input parameters
605 * @return array
606 */
Andrey Andreev29cade42014-02-04 14:05:58 +0200607 protected function _get_params($params)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200608 {
609 if (empty($params))
610 {
Andrey Andreev50ccc382014-02-04 23:30:06 +0200611 return isset($this->_cipher, $this->_mode, $this->_key, $this->_handle)
Andrey Andreev29cade42014-02-04 14:05:58 +0200612 ? array(
613 'handle' => $this->_handle,
614 'cipher' => $this->_cipher,
615 'mode' => $this->_mode,
616 'key' => $this->_key,
617 'base64' => TRUE,
618 'hmac' => $this->_mode === 'gcm' ? FALSE : array('digest' => 'sha512', 'key' => NULL)
619 )
620 : FALSE;
621 }
622 elseif ( ! isset($params['cipher'], $params['mode'], $params['key']))
623 {
624 return FALSE;
625 }
626
627 if ($params['mode'] === 'gcm')
628 {
629 $params['hmac'] = FALSE;
630 }
631 elseif ( ! isset($params['hmac']) OR ( ! is_array($params['hmac']) && $params['hmac'] !== FALSE))
632 {
633 $params['hmac'] = array(
634 'digest' => 'sha512',
635 'key' => NULL
636 );
637 }
638 elseif (is_array($params['hmac']))
639 {
640 if (isset($params['hmac']['digest']) && ! isset($this->_digests[$params['hmac']['digest']]))
Andrey Andreev818aedb2014-02-03 11:30:25 +0200641 {
642 return FALSE;
643 }
644
Andrey Andreev29cade42014-02-04 14:05:58 +0200645 $params['hmac'] = array(
646 'digest' => isset($params['hmac']['digest']) ? $params['hmac']['digest'] : 'sha512',
647 'key' => isset($params['hmac']['key']) ? $params['hmac']['key'] : NULL
Andrey Andreev818aedb2014-02-03 11:30:25 +0200648 );
649 }
650
651 $params = array(
652 'handle' => NULL,
653 'cipher' => isset($params['cipher']) ? $params['cipher'] : $this->_cipher,
654 'mode' => isset($params['mode']) ? $params['mode'] : $this->_mode,
655 'key' => isset($params['key']) ? $params['key'] : $this->_key,
656 'iv' => isset($params['iv']) ? $params['iv'] : NULL,
Andrey Andreev29cade42014-02-04 14:05:58 +0200657 'base64' => isset($params['base64']) ? $params['base64'] : TRUE,
658 'hmac' => $params['hmac']
Andrey Andreev818aedb2014-02-03 11:30:25 +0200659 );
660
Andrey Andreev818aedb2014-02-03 11:30:25 +0200661 $this->_cipher_alias($params['cipher']);
Andrey Andreev29cade42014-02-04 14:05:58 +0200662 $params['handle'] = ($params['cipher'] !== $this->_cipher OR $params['mode'] !== $this->_mode)
663 ? $this->{'_'.$this->_driver.'_get_handle'}($params['cipher'], $params['mode'])
664 : $this->_handle;
Andrey Andreev818aedb2014-02-03 11:30:25 +0200665
666 return $params;
667 }
668
669 // --------------------------------------------------------------------
670
671 /**
Andrey Andreev29cade42014-02-04 14:05:58 +0200672 * Get MCrypt handle
Andrey Andreev818aedb2014-02-03 11:30:25 +0200673 *
Andrey Andreev29cade42014-02-04 14:05:58 +0200674 * @param string $cipher Cipher name
675 * @param string $mode Encryption mode
676 * @return resource
Andrey Andreev818aedb2014-02-03 11:30:25 +0200677 */
Andrey Andreev29cade42014-02-04 14:05:58 +0200678 protected function _mcrypt_get_handle($cipher, $mode)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200679 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200680 return mcrypt_module_open($cipher, '', $mode, '');
681 }
Andrey Andreev818aedb2014-02-03 11:30:25 +0200682
Andrey Andreev29cade42014-02-04 14:05:58 +0200683 // --------------------------------------------------------------------
Andrey Andreev818aedb2014-02-03 11:30:25 +0200684
Andrey Andreev29cade42014-02-04 14:05:58 +0200685 /**
686 * Get OpenSSL handle
687 *
688 * @param string $cipher Cipher name
689 * @param string $mode Encryption mode
690 * @return string
691 */
692 protected function _openssl_get_handle($cipher, $mode)
693 {
694 // OpenSSL methods aren't suffixed with '-stream' for this mode
695 return ($mode === 'stream')
696 ? $cipher
697 : $cipher.'-'.$mode;
Andrey Andreev818aedb2014-02-03 11:30:25 +0200698 }
699
700 // --------------------------------------------------------------------
701
702 /**
703 * Cipher alias
704 *
705 * Tries to translate cipher names between MCrypt and OpenSSL's "dialects".
706 *
707 * @param string $cipher Cipher name
708 * @return void
709 */
710 protected function _cipher_alias(&$cipher)
711 {
712 static $dictionary;
713
714 if (empty($dictionary))
715 {
716 $dictionary = array(
717 'mcrypt' => array(
Andrey Andreev50ccc382014-02-04 23:30:06 +0200718 'aes-128' => 'rijndael-128',
719 'aes-192' => 'rijndael-128',
720 'aes-256' => 'rijndael-128',
721 'des3-ede3' => 'tripledes',
722 'rc4-40' => 'arcfour'
Andrey Andreev818aedb2014-02-03 11:30:25 +0200723 ),
724 'openssl' => array(
Andrey Andreev50ccc382014-02-04 23:30:06 +0200725 'rijndael-128' => 'aes-128',
726 'tripledes' => 'des-ede3',
727 'arcfour' => 'rc4-40'
Andrey Andreev818aedb2014-02-03 11:30:25 +0200728 )
729 );
730
731 // Notes regarding other seemingly matching ciphers between
732 // MCrypt and OpenSSL:
733 //
734 // - DES is compatible, but doesn't need an alias
735 // - Blowfish is NOT compatible
736 // mcrypt: 'blowfish', 'blowfish-compat'
737 // openssl: 'bf'
738 // - CAST-128/CAST5 is NOT compatible
739 // mcrypt: 'cast-128'
740 // openssl: 'cast5'
741 // - RC2 is NOT compatible
742 // mcrypt: 'rc2'
743 // openssl: 'rc2', 'rc2-40', 'rc2-64'
744 //
745 // To avoid any other confusion due to a popular (but incorrect)
746 // belief, it should also be noted that Rijndael-192/256 are NOT
747 // the same ciphers as AES-192/256 like Rijndael-128 and AES-256 is.
748 //
749 // All compatibility tests were done in CBC mode.
750 }
751
Andrey Andreev50ccc382014-02-04 23:30:06 +0200752 if (isset($dictionary[$this->_driver][$cipher]))
Andrey Andreev818aedb2014-02-03 11:30:25 +0200753 {
Andrey Andreev50ccc382014-02-04 23:30:06 +0200754 $cipher = $dictionary[$this->_driver][$cipher];
Andrey Andreev818aedb2014-02-03 11:30:25 +0200755 }
756 }
757
758 // --------------------------------------------------------------------
759
760 /**
Andrey Andreev29cade42014-02-04 14:05:58 +0200761 * HKDF
762 *
763 * @link https://tools.ietf.org/rfc/rfc5869.txt
764 * @param $key Input key
765 * @param $digest A SHA-2 hashing algorithm
766 * @param $salt Optional salt
767 * @param $info Optional context/application-specific info
768 * @param $length Output length (defaults to the selected digest size)
769 * @return string A pseudo-random key
770 */
771 public function hkdf($key, $digest = 'sha512', $salt = NULL, $length = NULL, $info = '')
772 {
773 if ( ! isset($this->_digests[$digest]))
774 {
775 return FALSE;
776 }
777
778 if (empty($length) OR ! is_int($length))
779 {
780 $length = $this->_digests[$digest];
781 }
782 elseif ($length > (255 * $this->_digests[$digest]))
783 {
784 return FALSE;
785 }
786
787 isset($salt) OR $salt = str_repeat("\0", $this->_digests[$digest]);
788
789 $prk = hash_hmac($digest, $key, $salt, TRUE);
790 $key = '';
791 for ($key_block = '', $block_index = 1; strlen($key) < $length; $block_index++)
792 {
793 $key_block = hash_hmac($digest, $key_block.$info.chr($block_index), $prk, TRUE);
794 $key .= $key_block;
795 }
796
797 return substr($key, 0, $length);
798 }
799
800 // --------------------------------------------------------------------
801
802 /**
Andrey Andreev818aedb2014-02-03 11:30:25 +0200803 * __get() magic
804 *
805 * @param string $key Property name
806 * @return mixed
807 */
808 public function __get($key)
809 {
Andrey Andreev912831f2014-02-04 17:21:37 +0200810 return in_array($key, array('cipher', 'mode', 'driver', 'drivers', 'digests'), TRUE)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200811 ? $this->{'_'.$key}
812 : NULL;
813 }
814
815}
816
817/* End of file Encryption.php */
818/* Location: ./system/libraries/Encryption.php */