blob: 3c1347523f7ac08192c80633157ad2b1d36353bf [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 {
151 if ($this->_driver[$params['driver']])
152 {
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
267 if ( ! in_array($handle, openssl_get_cipher_methods, TRUE))
268 {
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 {
367 mcrypt_module_close($handle);
368 }
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 {
455 if ($iv_size)
456 {
457 $params['iv'] = substr($data, 0, $iv_size);
458 $data = substr($data, $iv_size);
459 }
460 else
461 {
462 $params['iv'] = NULL;
463 }
464 }
465 elseif (strncmp($params['iv'], $data, $iv_size = strlen($params['iv'])) === 0)
466 {
467 $data = substr($data, $iv_size);
468 }
469
470 return $this->{'_'.$this->_driver.'_decrypt'}($data, $params);
471 }
472
473 // --------------------------------------------------------------------
474
475 /**
476 * Decrypt via MCrypt
477 *
478 * @param string $data Encrypted data
479 * @param array $params Input parameters
480 * @return string
481 */
482 protected function _mcrypt_decrypt($data, $params)
483 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200484 if ( ! is_resource($params['handle']))
485 {
486 return FALSE;
487 }
488 elseif (mcrypt_generic_init($params['handle'], $params['key'], $params['iv']) < 0)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200489 {
490 if ($params['handle'] !== $this->_handle)
491 {
492 mcrypt_module_close($params['handle']);
493 }
494
495 return FALSE;
496 }
497
498 $data = mdecrypt_generic($params['handle'], $data);
499
500 mcrypt_generic_deinit($params['handle']);
501 if ($params['handle'] !== $this->_handle)
502 {
503 mcrypt_module_close($handle);
504 }
505
506 // Remove PKCS#7 padding
507 return substr($data, 0, -ord($data[strlen($data)-1]));
508 }
509
510 // --------------------------------------------------------------------
511
512 /**
513 * Decrypt via OpenSSL
514 *
515 * @param string $data Encrypted data
516 * @param array $params Input parameters
517 * @return string
518 */
519 protected function _openssl_decrypt($data, $params)
520 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200521 return empty($params['handle'])
522 ? FALSE
523 : openssl_decrypt(
524 $data,
525 $params['handle'],
526 $params['key'],
527 1, // DO NOT TOUCH!
528 $params['iv']
529 );
Andrey Andreev818aedb2014-02-03 11:30:25 +0200530 }
531
532 // --------------------------------------------------------------------
533
534 /**
535 * Get IV size via MCrypt
536 *
537 * @param resource $handle MCrypt module resource
538 * @return int
539 */
540 protected function _mcrypt_get_iv_size($handle)
541 {
542 return mcrypt_enc_get_iv_size($handle);
543 }
544
545 // --------------------------------------------------------------------
546
547 /**
548 * Get IV size via OpenSSL
549 *
550 * @param string $handle OpenSSL cipher method
551 * @return int
552 */
553 protected function _openssl_get_iv_size($handle)
554 {
555 return openssl_cipher_iv_length($handle);
556 }
557
558 // --------------------------------------------------------------------
559
560 /**
561 * Get IV via MCrypt
562 *
563 * @param int $size
564 * @return int
565 */
566 protected function _mcrypt_get_iv($size)
567 {
568 // If /dev/urandom is available - use it, otherwise there's
569 // also /dev/random, but it is highly unlikely that it would
570 // be available while /dev/urandom is not and it is known to be
571 // blocking anyway.
572 if (defined(MCRYPT_DEV_URANDOM))
573 {
574 $source = MCRYPT_DEV_URANDOM;
575 }
576 else
577 {
578 $source = MCRYPT_RAND;
579 is_php('5.3') OR srand(microtime(TRUE));
580 }
581
582 return mcrypt_create_iv($size, $source);
583 }
584
585 // --------------------------------------------------------------------
586
587 /**
588 * Get IV via OpenSSL
589 *
590 * @param int $size IV size
591 * @return int
592 */
593 protected function _openssl_get_iv($size)
594 {
595 return openssl_random_pseudo_bytes($size);
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
Andrey Andreev29cade42014-02-04 14:05:58 +0200601 * Get params
Andrey Andreev818aedb2014-02-03 11:30:25 +0200602 *
603 * @param array $params Input parameters
604 * @return array
605 */
Andrey Andreev29cade42014-02-04 14:05:58 +0200606 protected function _get_params($params)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200607 {
608 if (empty($params))
609 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200610 return isset($this->_cipher, $this->_mode, $params->_key, $this->_handle)
611 ? array(
612 'handle' => $this->_handle,
613 'cipher' => $this->_cipher,
614 'mode' => $this->_mode,
615 'key' => $this->_key,
616 'base64' => TRUE,
617 'hmac' => $this->_mode === 'gcm' ? FALSE : array('digest' => 'sha512', 'key' => NULL)
618 )
619 : FALSE;
620 }
621 elseif ( ! isset($params['cipher'], $params['mode'], $params['key']))
622 {
623 return FALSE;
624 }
625
626 if ($params['mode'] === 'gcm')
627 {
628 $params['hmac'] = FALSE;
629 }
630 elseif ( ! isset($params['hmac']) OR ( ! is_array($params['hmac']) && $params['hmac'] !== FALSE))
631 {
632 $params['hmac'] = array(
633 'digest' => 'sha512',
634 'key' => NULL
635 );
636 }
637 elseif (is_array($params['hmac']))
638 {
639 if (isset($params['hmac']['digest']) && ! isset($this->_digests[$params['hmac']['digest']]))
Andrey Andreev818aedb2014-02-03 11:30:25 +0200640 {
641 return FALSE;
642 }
643
Andrey Andreev29cade42014-02-04 14:05:58 +0200644 $params['hmac'] = array(
645 'digest' => isset($params['hmac']['digest']) ? $params['hmac']['digest'] : 'sha512',
646 'key' => isset($params['hmac']['key']) ? $params['hmac']['key'] : NULL
Andrey Andreev818aedb2014-02-03 11:30:25 +0200647 );
648 }
649
650 $params = array(
651 'handle' => NULL,
652 'cipher' => isset($params['cipher']) ? $params['cipher'] : $this->_cipher,
653 'mode' => isset($params['mode']) ? $params['mode'] : $this->_mode,
654 'key' => isset($params['key']) ? $params['key'] : $this->_key,
655 'iv' => isset($params['iv']) ? $params['iv'] : NULL,
Andrey Andreev29cade42014-02-04 14:05:58 +0200656 'base64' => isset($params['base64']) ? $params['base64'] : TRUE,
657 'hmac' => $params['hmac']
Andrey Andreev818aedb2014-02-03 11:30:25 +0200658 );
659
Andrey Andreev818aedb2014-02-03 11:30:25 +0200660 $this->_cipher_alias($params['cipher']);
Andrey Andreev29cade42014-02-04 14:05:58 +0200661 $params['handle'] = ($params['cipher'] !== $this->_cipher OR $params['mode'] !== $this->_mode)
662 ? $this->{'_'.$this->_driver.'_get_handle'}($params['cipher'], $params['mode'])
663 : $this->_handle;
Andrey Andreev818aedb2014-02-03 11:30:25 +0200664
665 return $params;
666 }
667
668 // --------------------------------------------------------------------
669
670 /**
Andrey Andreev29cade42014-02-04 14:05:58 +0200671 * Get MCrypt handle
Andrey Andreev818aedb2014-02-03 11:30:25 +0200672 *
Andrey Andreev29cade42014-02-04 14:05:58 +0200673 * @param string $cipher Cipher name
674 * @param string $mode Encryption mode
675 * @return resource
Andrey Andreev818aedb2014-02-03 11:30:25 +0200676 */
Andrey Andreev29cade42014-02-04 14:05:58 +0200677 protected function _mcrypt_get_handle($cipher, $mode)
Andrey Andreev818aedb2014-02-03 11:30:25 +0200678 {
Andrey Andreev29cade42014-02-04 14:05:58 +0200679 return mcrypt_module_open($cipher, '', $mode, '');
680 }
Andrey Andreev818aedb2014-02-03 11:30:25 +0200681
Andrey Andreev29cade42014-02-04 14:05:58 +0200682 // --------------------------------------------------------------------
Andrey Andreev818aedb2014-02-03 11:30:25 +0200683
Andrey Andreev29cade42014-02-04 14:05:58 +0200684 /**
685 * Get OpenSSL handle
686 *
687 * @param string $cipher Cipher name
688 * @param string $mode Encryption mode
689 * @return string
690 */
691 protected function _openssl_get_handle($cipher, $mode)
692 {
693 // OpenSSL methods aren't suffixed with '-stream' for this mode
694 return ($mode === 'stream')
695 ? $cipher
696 : $cipher.'-'.$mode;
Andrey Andreev818aedb2014-02-03 11:30:25 +0200697 }
698
699 // --------------------------------------------------------------------
700
701 /**
702 * Cipher alias
703 *
704 * Tries to translate cipher names between MCrypt and OpenSSL's "dialects".
705 *
706 * @param string $cipher Cipher name
707 * @return void
708 */
709 protected function _cipher_alias(&$cipher)
710 {
711 static $dictionary;
712
713 if (empty($dictionary))
714 {
715 $dictionary = array(
716 'mcrypt' => array(
717 'rijndael-128',
718 'tripledes',
719 'arcfour'
720 ),
721 'openssl' => array(
722 'aes-128',
723 'des-ede3',
724 'rc4-40'
725 )
726 );
727
728 // Notes regarding other seemingly matching ciphers between
729 // MCrypt and OpenSSL:
730 //
731 // - DES is compatible, but doesn't need an alias
732 // - Blowfish is NOT compatible
733 // mcrypt: 'blowfish', 'blowfish-compat'
734 // openssl: 'bf'
735 // - CAST-128/CAST5 is NOT compatible
736 // mcrypt: 'cast-128'
737 // openssl: 'cast5'
738 // - RC2 is NOT compatible
739 // mcrypt: 'rc2'
740 // openssl: 'rc2', 'rc2-40', 'rc2-64'
741 //
742 // To avoid any other confusion due to a popular (but incorrect)
743 // belief, it should also be noted that Rijndael-192/256 are NOT
744 // the same ciphers as AES-192/256 like Rijndael-128 and AES-256 is.
745 //
746 // All compatibility tests were done in CBC mode.
747 }
748
749 $dialect = ($this->_driver === 'mcrypt')
750 ? 'openssl'
751 : 'mcrypt';
752 if (($index = array_search($cipher, $dictionary[$dialect], TRUE)) !== FALSE)
753 {
754 $cipher = $dictionary[$this->_driver][$index];
755 }
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 */