Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter Encryption Class |
| 42 | * |
Andrey Andreev | fc4db34 | 2014-08-27 14:18:19 +0300 | [diff] [blame] | 43 | * Provides two-way keyed encoding using Mcrypt |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | * |
| 45 | * @package CodeIgniter |
| 46 | * @subpackage Libraries |
| 47 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/libraries/encryption.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
| 51 | class CI_Encrypt { |
| 52 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 53 | /** |
| 54 | * Reference to the user's encryption key |
| 55 | * |
| 56 | * @var string |
| 57 | */ |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 58 | public $encryption_key = ''; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 59 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 60 | /** |
| 61 | * Type of hash operation |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 62 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 63 | * @var string |
| 64 | */ |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 65 | protected $_hash_type = 'sha1'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 66 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 67 | /** |
Calvin Tam | 55bc505 | 2015-07-24 02:27:24 -0700 | [diff] [blame] | 68 | * Flag for the existence of mcrypt |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 69 | * |
| 70 | * @var bool |
| 71 | */ |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 72 | protected $_mcrypt_exists = FALSE; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 73 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 74 | /** |
| 75 | * Current cipher to be used with mcrypt |
| 76 | * |
| 77 | * @var string |
| 78 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 79 | protected $_mcrypt_cipher; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 80 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 81 | /** |
| 82 | * Method for encrypting/decrypting data |
| 83 | * |
| 84 | * @var int |
| 85 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 86 | protected $_mcrypt_mode; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 88 | /** |
| 89 | * Initialize Encryption class |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 90 | * |
| 91 | * @return void |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 92 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 93 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
Andrey Andreev | 6eb77da | 2014-05-31 21:18:17 +0300 | [diff] [blame] | 95 | if (($this->_mcrypt_exists = function_exists('mcrypt_encrypt')) === FALSE) |
| 96 | { |
| 97 | show_error('The Encrypt library requires the Mcrypt extension.'); |
| 98 | } |
| 99 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 100 | log_message('info', 'Encrypt Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // -------------------------------------------------------------------- |
| 104 | |
| 105 | /** |
| 106 | * Fetch the encryption key |
| 107 | * |
| 108 | * Returns it as MD5 in order to have an exact-length 128 bit key. |
| 109 | * Mcrypt is sensitive to keys that are not the correct length |
| 110 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | * @param string |
| 112 | * @return string |
| 113 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 114 | public function get_key($key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 116 | if ($key === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 118 | if ($this->encryption_key !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | { |
| 120 | return $this->encryption_key; |
| 121 | } |
| 122 | |
Joffrey Jaffeux | ba7f50b | 2012-06-06 01:40:01 +0200 | [diff] [blame] | 123 | $key = config_item('encryption_key'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | |
Andrey Andreev | f565212 | 2017-01-19 15:17:00 +0200 | [diff] [blame] | 125 | if ( ! self::strlen($key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | { |
| 127 | show_error('In order to use the encryption class requires that you set an encryption key in your config file.'); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return md5($key); |
| 132 | } |
| 133 | |
| 134 | // -------------------------------------------------------------------- |
| 135 | |
| 136 | /** |
| 137 | * Set the encryption key |
| 138 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 140 | * @return CI_Encrypt |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 142 | public function set_key($key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | { |
| 144 | $this->encryption_key = $key; |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 145 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
| 149 | |
| 150 | /** |
| 151 | * Encode |
| 152 | * |
| 153 | * Encodes the message string using bitwise XOR encoding. |
| 154 | * The key is combined with a random hash, and then it |
| 155 | * too gets converted using XOR. The whole thing is then run |
Andrey Andreev | 6eb77da | 2014-05-31 21:18:17 +0300 | [diff] [blame] | 156 | * through mcrypt using the randomized key. The end result |
| 157 | * is a double-encrypted message string that is randomized |
| 158 | * with each call to this function, even if the supplied |
| 159 | * message and key are the same. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | * @param string the string to encode |
| 162 | * @param string the key |
| 163 | * @return string |
| 164 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 165 | public function encode($string, $key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | { |
Andrey Andreev | 6eb77da | 2014-05-31 21:18:17 +0300 | [diff] [blame] | 167 | return base64_encode($this->mcrypt_encode($string, $this->get_key($key))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
| 172 | /** |
| 173 | * Decode |
| 174 | * |
| 175 | * Reverses the above process |
| 176 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | * @param string |
| 178 | * @param string |
| 179 | * @return string |
| 180 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 181 | public function decode($string, $key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | { |
Bogdan Lysenko | c16b4f4 | 2012-10-11 11:41:01 +0300 | [diff] [blame] | 183 | if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string) OR base64_encode(base64_decode($string)) !== $string) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | { |
| 185 | return FALSE; |
| 186 | } |
| 187 | |
Andrey Andreev | 6eb77da | 2014-05-31 21:18:17 +0300 | [diff] [blame] | 188 | return $this->mcrypt_decode(base64_decode($string), $this->get_key($key)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 192 | |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 193 | /** |
| 194 | * Encode from Legacy |
| 195 | * |
| 196 | * Takes an encoded string from the original Encryption class algorithms and |
| 197 | * returns a newly encoded string using the improved method added in 2.0.0 |
| 198 | * This allows for backwards compatibility and a method to transition to the |
| 199 | * new encryption algorithms. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | * |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 201 | * For more details, see https://codeigniter.com/user_guide/installation/upgrade_200.html#encryption |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 202 | * |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 203 | * @param string |
| 204 | * @param int (mcrypt mode constant) |
| 205 | * @param string |
| 206 | * @return string |
| 207 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 208 | public function encode_from_legacy($string, $legacy_mode = MCRYPT_MODE_ECB, $key = '') |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 209 | { |
Andrey Andreev | 6eb77da | 2014-05-31 21:18:17 +0300 | [diff] [blame] | 210 | if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string)) |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 211 | { |
| 212 | return FALSE; |
| 213 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 214 | |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 215 | // decode it first |
| 216 | // set mode temporarily to what it was when string was encoded with the legacy |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 217 | // algorithm - typically MCRYPT_MODE_ECB |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 218 | $current_mode = $this->_get_mode(); |
| 219 | $this->set_mode($legacy_mode); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 221 | $key = $this->get_key($key); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 222 | $dec = base64_decode($string); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 223 | if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE) |
| 224 | { |
Andrey Andreev | f696c1f | 2012-06-12 12:14:51 +0300 | [diff] [blame] | 225 | $this->set_mode($current_mode); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 226 | return FALSE; |
| 227 | } |
| 228 | |
| 229 | $dec = $this->_xor_decode($dec, $key); |
| 230 | |
| 231 | // set the mcrypt mode back to what it should be, typically MCRYPT_MODE_CBC |
Derek Jones | 092103e | 2010-09-02 11:11:58 -0500 | [diff] [blame] | 232 | $this->set_mode($current_mode); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 233 | |
| 234 | // and re-encode |
| 235 | return base64_encode($this->mcrypt_encode($dec, $key)); |
| 236 | } |
| 237 | |
| 238 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | * XOR Decode |
| 242 | * |
| 243 | * Takes an encoded string and key as input and generates the |
| 244 | * plain-text original message |
| 245 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | * @param string |
| 247 | * @param string |
| 248 | * @return string |
| 249 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 250 | protected function _xor_decode($string, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | { |
| 252 | $string = $this->_xor_merge($string, $key); |
| 253 | |
| 254 | $dec = ''; |
Andrey Andreev | f565212 | 2017-01-19 15:17:00 +0200 | [diff] [blame] | 255 | for ($i = 0, $l = self::strlen($string); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 257 | $dec .= ($string[$i++] ^ $string[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return $dec; |
| 261 | } |
| 262 | |
| 263 | // -------------------------------------------------------------------- |
| 264 | |
| 265 | /** |
| 266 | * XOR key + string Combiner |
| 267 | * |
| 268 | * Takes a string and key as input and computes the difference using XOR |
| 269 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | * @param string |
| 271 | * @param string |
| 272 | * @return string |
| 273 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 274 | protected function _xor_merge($string, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | { |
| 276 | $hash = $this->hash($key); |
| 277 | $str = ''; |
Andrey Andreev | f565212 | 2017-01-19 15:17:00 +0200 | [diff] [blame] | 278 | |
| 279 | for ($i = 0, $ls = self::strlen($string), $lh = self::strlen($hash); $i < $ls; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 281 | $str .= $string[$i] ^ $hash[($i % $lh)]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return $str; |
| 285 | } |
| 286 | |
| 287 | // -------------------------------------------------------------------- |
| 288 | |
| 289 | /** |
| 290 | * Encrypt using Mcrypt |
| 291 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | * @param string |
| 293 | * @param string |
| 294 | * @return string |
| 295 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 296 | public function mcrypt_encode($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | { |
| 298 | $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode()); |
Andrey Andreev | f565212 | 2017-01-19 15:17:00 +0200 | [diff] [blame] | 299 | $init_vect = mcrypt_create_iv($init_size, MCRYPT_DEV_URANDOM); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key); |
| 301 | } |
| 302 | |
| 303 | // -------------------------------------------------------------------- |
| 304 | |
| 305 | /** |
| 306 | * Decrypt using Mcrypt |
| 307 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | * @param string |
| 309 | * @param string |
| 310 | * @return string |
| 311 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 312 | public function mcrypt_decode($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | { |
| 314 | $data = $this->_remove_cipher_noise($data, $key); |
| 315 | $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode()); |
| 316 | |
Andrey Andreev | f565212 | 2017-01-19 15:17:00 +0200 | [diff] [blame] | 317 | if ($init_size > self::strlen($data)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | { |
| 319 | return FALSE; |
| 320 | } |
| 321 | |
Andrey Andreev | f565212 | 2017-01-19 15:17:00 +0200 | [diff] [blame] | 322 | $init_vect = self::substr($data, 0, $init_size); |
| 323 | $data = self::substr($data, $init_size); |
| 324 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0"); |
| 326 | } |
| 327 | |
| 328 | // -------------------------------------------------------------------- |
| 329 | |
| 330 | /** |
| 331 | * Adds permuted noise to the IV + encrypted data to protect |
| 332 | * against Man-in-the-middle attacks on CBC mode ciphers |
| 333 | * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV |
| 334 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | * @param string |
| 336 | * @param string |
| 337 | * @return string |
| 338 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 339 | protected function _add_cipher_noise($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 341 | $key = $this->hash($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | $str = ''; |
| 343 | |
Andrey Andreev | f565212 | 2017-01-19 15:17:00 +0200 | [diff] [blame] | 344 | for ($i = 0, $j = 0, $ld = self::strlen($data), $lk = self::strlen($key); $i < $ld; ++$i, ++$j) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 346 | if ($j >= $lk) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | { |
| 348 | $j = 0; |
| 349 | } |
| 350 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 351 | $str .= chr((ord($data[$i]) + ord($key[$j])) % 256); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | return $str; |
| 355 | } |
| 356 | |
| 357 | // -------------------------------------------------------------------- |
| 358 | |
| 359 | /** |
| 360 | * Removes permuted noise from the IV + encrypted data, reversing |
| 361 | * _add_cipher_noise() |
| 362 | * |
| 363 | * Function description |
| 364 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 365 | * @param string $data |
| 366 | * @param string $key |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 367 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 369 | protected function _remove_cipher_noise($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 371 | $key = $this->hash($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | $str = ''; |
| 373 | |
Andrey Andreev | f565212 | 2017-01-19 15:17:00 +0200 | [diff] [blame] | 374 | for ($i = 0, $j = 0, $ld = self::strlen($data), $lk = self::strlen($key); $i < $ld; ++$i, ++$j) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 376 | if ($j >= $lk) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | { |
| 378 | $j = 0; |
| 379 | } |
| 380 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 381 | $temp = ord($data[$i]) - ord($key[$j]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | |
| 383 | if ($temp < 0) |
| 384 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 385 | $temp += 256; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 387 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | $str .= chr($temp); |
| 389 | } |
| 390 | |
| 391 | return $str; |
| 392 | } |
| 393 | |
| 394 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 395 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | /** |
| 397 | * Set the Mcrypt Cipher |
| 398 | * |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 399 | * @param int |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 400 | * @return CI_Encrypt |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 402 | public function set_cipher($cipher) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | { |
| 404 | $this->_mcrypt_cipher = $cipher; |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 405 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // -------------------------------------------------------------------- |
| 409 | |
| 410 | /** |
| 411 | * Set the Mcrypt Mode |
| 412 | * |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 413 | * @param int |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 414 | * @return CI_Encrypt |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | */ |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 416 | public function set_mode($mode) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | { |
| 418 | $this->_mcrypt_mode = $mode; |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 419 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // -------------------------------------------------------------------- |
| 423 | |
| 424 | /** |
| 425 | * Get Mcrypt cipher Value |
| 426 | * |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 427 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 429 | protected function _get_cipher() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | { |
Andrey Andreev | 79eca3d | 2012-06-04 18:28:50 +0300 | [diff] [blame] | 431 | if ($this->_mcrypt_cipher === NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | { |
Andrey Andreev | d655a99 | 2012-01-10 22:31:29 +0200 | [diff] [blame] | 433 | return $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return $this->_mcrypt_cipher; |
| 437 | } |
| 438 | |
| 439 | // -------------------------------------------------------------------- |
| 440 | |
| 441 | /** |
| 442 | * Get Mcrypt Mode Value |
| 443 | * |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 444 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 446 | protected function _get_mode() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | { |
Andrey Andreev | 79eca3d | 2012-06-04 18:28:50 +0300 | [diff] [blame] | 448 | if ($this->_mcrypt_mode === NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | { |
Andrey Andreev | d655a99 | 2012-01-10 22:31:29 +0200 | [diff] [blame] | 450 | return $this->_mcrypt_mode = MCRYPT_MODE_CBC; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 452 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | return $this->_mcrypt_mode; |
| 454 | } |
| 455 | |
| 456 | // -------------------------------------------------------------------- |
| 457 | |
| 458 | /** |
| 459 | * Set the Hash type |
| 460 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | * @param string |
Andrey Andreev | f4cb94e | 2012-01-19 15:16:55 +0200 | [diff] [blame] | 462 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 464 | public function set_hash($type = 'sha1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | { |
Daniel Morris | ada7775 | 2012-10-04 10:24:16 +0100 | [diff] [blame] | 466 | $this->_hash_type = in_array($type, hash_algos()) ? $type : 'sha1'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // -------------------------------------------------------------------- |
| 470 | |
| 471 | /** |
| 472 | * Hash encode a string |
| 473 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | * @param string |
| 475 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 476 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 477 | public function hash($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | { |
Daniel Morris | a9923f5 | 2012-10-03 19:37:09 +0100 | [diff] [blame] | 479 | return hash($this->_hash_type, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | } |
Andrey Andreev | 38d0e93 | 2012-04-03 19:27:45 +0300 | [diff] [blame] | 481 | |
Andrey Andreev | f565212 | 2017-01-19 15:17:00 +0200 | [diff] [blame] | 482 | // -------------------------------------------------------------------- |
| 483 | |
| 484 | /** |
| 485 | * Byte-safe strlen() |
| 486 | * |
| 487 | * @param string $str |
| 488 | * @return int |
| 489 | */ |
| 490 | protected static function strlen($str) |
| 491 | { |
| 492 | return defined('MB_OVERLOAD_STRING') |
| 493 | ? mb_strlen($str, '8bit') |
| 494 | : strlen($str); |
| 495 | } |
| 496 | |
| 497 | // -------------------------------------------------------------------- |
| 498 | |
| 499 | /** |
| 500 | * Byte-safe substr() |
| 501 | * |
| 502 | * @param string $str |
| 503 | * @param int $start |
| 504 | * @param int $length |
| 505 | * @return string |
| 506 | */ |
| 507 | protected static function substr($str, $start, $length = NULL) |
| 508 | { |
| 509 | if (defined('MB_OVERLOAD_STRING')) |
| 510 | { |
| 511 | // mb_substr($str, $start, null, '8bit') returns an empty |
| 512 | // string on PHP 5.3 |
| 513 | isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start); |
| 514 | return mb_substr($str, $start, $length, '8bit'); |
| 515 | } |
| 516 | |
| 517 | return isset($length) |
| 518 | ? substr($str, $start, $length) |
| 519 | : substr($str, $start); |
| 520 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 521 | } |