Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 84 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Crypt_TripleDES | |
0.00% |
0 / 78 |
|
0.00% |
0 / 10 |
870 | |
0.00% |
0 / 1 |
| Crypt_TripleDES | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| isValidEngine | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| setIV | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| setKey | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| encrypt | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| decrypt | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| enableContinuousBuffer | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| disableContinuousBuffer | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| _setupKey | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| setPreferredEngine | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Pure-PHP implementation of Triple DES. |
| 5 | * |
| 6 | * Uses mcrypt, if available, and an internal implementation, otherwise. Operates in the EDE3 mode (encrypt-decrypt-encrypt). |
| 7 | * |
| 8 | * PHP versions 4 and 5 |
| 9 | * |
| 10 | * Here's a short example of how to use this library: |
| 11 | * <code> |
| 12 | * <?php |
| 13 | * include 'Crypt/TripleDES.php'; |
| 14 | * |
| 15 | * $des = new Crypt_TripleDES(); |
| 16 | * |
| 17 | * $des->setKey('abcdefghijklmnopqrstuvwx'); |
| 18 | * |
| 19 | * $size = 10 * 1024; |
| 20 | * $plaintext = ''; |
| 21 | * for ($i = 0; $i < $size; $i++) { |
| 22 | * $plaintext.= 'a'; |
| 23 | * } |
| 24 | * |
| 25 | * echo $des->decrypt($des->encrypt($plaintext)); |
| 26 | * ?> |
| 27 | * </code> |
| 28 | * |
| 29 | * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy |
| 30 | * of this software and associated documentation files (the "Software"), to deal |
| 31 | * in the Software without restriction, including without limitation the rights |
| 32 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 33 | * copies of the Software, and to permit persons to whom the Software is |
| 34 | * furnished to do so, subject to the following conditions: |
| 35 | * |
| 36 | * The above copyright notice and this permission notice shall be included in |
| 37 | * all copies or substantial portions of the Software. |
| 38 | * |
| 39 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 40 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 41 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 42 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 43 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 44 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 45 | * THE SOFTWARE. |
| 46 | * |
| 47 | * @category Crypt |
| 48 | * @package Crypt_TripleDES |
| 49 | * @author Jim Wigginton <terrafrost@php.net> |
| 50 | * @copyright 2007 Jim Wigginton |
| 51 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 52 | * @link http://phpseclib.sourceforge.net |
| 53 | */ |
| 54 | |
| 55 | /** |
| 56 | * Include Crypt_DES |
| 57 | */ |
| 58 | if (!class_exists('Crypt_DES')) { |
| 59 | include_once 'DES.php'; |
| 60 | } |
| 61 | |
| 62 | /**#@+ |
| 63 | * @access public |
| 64 | * @see Crypt_TripleDES::Crypt_TripleDES() |
| 65 | */ |
| 66 | /** |
| 67 | * Encrypt / decrypt using inner chaining |
| 68 | * |
| 69 | * Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (CRYPT_DES_MODE_CBC3). |
| 70 | */ |
| 71 | define('CRYPT_MODE_3CBC', -2); |
| 72 | /** |
| 73 | * BC version of the above. |
| 74 | */ |
| 75 | define('CRYPT_DES_MODE_3CBC', -2); |
| 76 | /** |
| 77 | * Encrypt / decrypt using outer chaining |
| 78 | * |
| 79 | * Outer chaining is used by SSH-2 and when the mode is set to CRYPT_DES_MODE_CBC. |
| 80 | */ |
| 81 | define('CRYPT_MODE_CBC3', CRYPT_MODE_CBC); |
| 82 | /** |
| 83 | * BC version of the above. |
| 84 | */ |
| 85 | define('CRYPT_DES_MODE_CBC3', CRYPT_MODE_CBC3); |
| 86 | /**#@-*/ |
| 87 | |
| 88 | /** |
| 89 | * Pure-PHP implementation of Triple DES. |
| 90 | * |
| 91 | * @package Crypt_TripleDES |
| 92 | * @author Jim Wigginton <terrafrost@php.net> |
| 93 | * @access public |
| 94 | */ |
| 95 | class Crypt_TripleDES extends Crypt_DES |
| 96 | { |
| 97 | /** |
| 98 | * The default password key_size used by setPassword() |
| 99 | * |
| 100 | * @see Crypt_DES::password_key_size |
| 101 | * @see Crypt_Base::password_key_size |
| 102 | * @see Crypt_Base::setPassword() |
| 103 | * @var Integer |
| 104 | * @access private |
| 105 | */ |
| 106 | var $password_key_size = 24; |
| 107 | |
| 108 | /** |
| 109 | * The default salt used by setPassword() |
| 110 | * |
| 111 | * @see Crypt_Base::password_default_salt |
| 112 | * @see Crypt_Base::setPassword() |
| 113 | * @var String |
| 114 | * @access private |
| 115 | */ |
| 116 | var $password_default_salt = 'phpseclib'; |
| 117 | |
| 118 | /** |
| 119 | * The namespace used by the cipher for its constants. |
| 120 | * |
| 121 | * @see Crypt_DES::const_namespace |
| 122 | * @see Crypt_Base::const_namespace |
| 123 | * @var String |
| 124 | * @access private |
| 125 | */ |
| 126 | var $const_namespace = 'DES'; |
| 127 | |
| 128 | /** |
| 129 | * The mcrypt specific name of the cipher |
| 130 | * |
| 131 | * @see Crypt_DES::cipher_name_mcrypt |
| 132 | * @see Crypt_Base::cipher_name_mcrypt |
| 133 | * @var String |
| 134 | * @access private |
| 135 | */ |
| 136 | var $cipher_name_mcrypt = 'tripledes'; |
| 137 | |
| 138 | /** |
| 139 | * Optimizing value while CFB-encrypting |
| 140 | * |
| 141 | * @see Crypt_Base::cfb_init_len |
| 142 | * @var Integer |
| 143 | * @access private |
| 144 | */ |
| 145 | var $cfb_init_len = 750; |
| 146 | |
| 147 | /** |
| 148 | * max possible size of $key |
| 149 | * |
| 150 | * @see Crypt_TripleDES::setKey() |
| 151 | * @see Crypt_DES::setKey() |
| 152 | * @var String |
| 153 | * @access private |
| 154 | */ |
| 155 | var $key_size_max = 24; |
| 156 | |
| 157 | /** |
| 158 | * Internal flag whether using CRYPT_DES_MODE_3CBC or not |
| 159 | * |
| 160 | * @var Boolean |
| 161 | * @access private |
| 162 | */ |
| 163 | var $mode_3cbc; |
| 164 | |
| 165 | /** |
| 166 | * The Crypt_DES objects |
| 167 | * |
| 168 | * Used only if $mode_3cbc === true |
| 169 | * |
| 170 | * @var Array |
| 171 | * @access private |
| 172 | */ |
| 173 | var $des; |
| 174 | |
| 175 | /** |
| 176 | * Default Constructor. |
| 177 | * |
| 178 | * Determines whether or not the mcrypt extension should be used. |
| 179 | * |
| 180 | * $mode could be: |
| 181 | * |
| 182 | * - CRYPT_DES_MODE_ECB |
| 183 | * |
| 184 | * - CRYPT_DES_MODE_CBC |
| 185 | * |
| 186 | * - CRYPT_DES_MODE_CTR |
| 187 | * |
| 188 | * - CRYPT_DES_MODE_CFB |
| 189 | * |
| 190 | * - CRYPT_DES_MODE_OFB |
| 191 | * |
| 192 | * - CRYPT_DES_MODE_3CBC |
| 193 | * |
| 194 | * If not explicitly set, CRYPT_DES_MODE_CBC will be used. |
| 195 | * |
| 196 | * @see Crypt_DES::Crypt_DES() |
| 197 | * @see Crypt_Base::Crypt_Base() |
| 198 | * @param optional Integer $mode |
| 199 | * @access public |
| 200 | */ |
| 201 | function Crypt_TripleDES($mode = CRYPT_MODE_CBC) |
| 202 | { |
| 203 | switch ($mode) { |
| 204 | // In case of CRYPT_DES_MODE_3CBC, we init as CRYPT_DES_MODE_CBC |
| 205 | // and additional flag us internally as 3CBC |
| 206 | case CRYPT_DES_MODE_3CBC: |
| 207 | parent::Crypt_Base(CRYPT_MODE_CBC); |
| 208 | $this->mode_3cbc = true; |
| 209 | |
| 210 | // This three $des'es will do the 3CBC work (if $key > 64bits) |
| 211 | $this->des = array( |
| 212 | new Crypt_DES(CRYPT_MODE_CBC), |
| 213 | new Crypt_DES(CRYPT_MODE_CBC), |
| 214 | new Crypt_DES(CRYPT_MODE_CBC), |
| 215 | ); |
| 216 | |
| 217 | // we're going to be doing the padding, ourselves, so disable it in the Crypt_DES objects |
| 218 | $this->des[0]->disablePadding(); |
| 219 | $this->des[1]->disablePadding(); |
| 220 | $this->des[2]->disablePadding(); |
| 221 | break; |
| 222 | // If not 3CBC, we init as usual |
| 223 | default: |
| 224 | parent::Crypt_Base($mode); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Test for engine validity |
| 230 | * |
| 231 | * This is mainly just a wrapper to set things up for Crypt_Base::isValidEngine() |
| 232 | * |
| 233 | * @see Crypt_Base::Crypt_Base() |
| 234 | * @param Integer $engine |
| 235 | * @access public |
| 236 | * @return Boolean |
| 237 | */ |
| 238 | function isValidEngine($engine) |
| 239 | { |
| 240 | if ($engine == CRYPT_ENGINE_OPENSSL) { |
| 241 | $this->cipher_name_openssl_ecb = 'des-ede3'; |
| 242 | $mode = $this->_openssl_translate_mode(); |
| 243 | $this->cipher_name_openssl = $mode == 'ecb' ? 'des-ede3' : 'des-ede3-' . $mode; |
| 244 | } |
| 245 | |
| 246 | return parent::isValidEngine($engine); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Sets the initialization vector. (optional) |
| 251 | * |
| 252 | * SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explicitly set, it'll be assumed |
| 253 | * to be all zero's. |
| 254 | * |
| 255 | * @see Crypt_Base::setIV() |
| 256 | * @access public |
| 257 | * @param String $iv |
| 258 | */ |
| 259 | function setIV($iv) |
| 260 | { |
| 261 | parent::setIV($iv); |
| 262 | if ($this->mode_3cbc) { |
| 263 | $this->des[0]->setIV($iv); |
| 264 | $this->des[1]->setIV($iv); |
| 265 | $this->des[2]->setIV($iv); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Sets the key. |
| 271 | * |
| 272 | * Keys can be of any length. Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or |
| 273 | * 192-bit (eg. strlen($key) == 24) keys. This function pads and truncates $key as appropriate. |
| 274 | * |
| 275 | * DES also requires that every eighth bit be a parity bit, however, we'll ignore that. |
| 276 | * |
| 277 | * If the key is not explicitly set, it'll be assumed to be all null bytes. |
| 278 | * |
| 279 | * @access public |
| 280 | * @see Crypt_DES::setKey() |
| 281 | * @see Crypt_Base::setKey() |
| 282 | * @param String $key |
| 283 | */ |
| 284 | function setKey($key) |
| 285 | { |
| 286 | $length = strlen($key); |
| 287 | if ($length > 8) { |
| 288 | $key = str_pad(substr($key, 0, 24), 24, chr(0)); |
| 289 | // if $key is between 64 and 128-bits, use the first 64-bits as the last, per this: |
| 290 | // http://php.net/function.mcrypt-encrypt#47973 |
| 291 | $key = $length <= 16 ? substr_replace($key, substr($key, 0, 8), 16) : substr($key, 0, 24); |
| 292 | } else { |
| 293 | $key = str_pad($key, 8, chr(0)); |
| 294 | } |
| 295 | parent::setKey($key); |
| 296 | |
| 297 | // And in case of CRYPT_DES_MODE_3CBC: |
| 298 | // if key <= 64bits we not need the 3 $des to work, |
| 299 | // because we will then act as regular DES-CBC with just a <= 64bit key. |
| 300 | // So only if the key > 64bits (> 8 bytes) we will call setKey() for the 3 $des. |
| 301 | if ($this->mode_3cbc && $length > 8) { |
| 302 | $this->des[0]->setKey(substr($key, 0, 8)); |
| 303 | $this->des[1]->setKey(substr($key, 8, 8)); |
| 304 | $this->des[2]->setKey(substr($key, 16, 8)); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Encrypts a message. |
| 310 | * |
| 311 | * @see Crypt_Base::encrypt() |
| 312 | * @access public |
| 313 | * @param String $plaintext |
| 314 | * @return String $cipertext |
| 315 | */ |
| 316 | function encrypt($plaintext) |
| 317 | { |
| 318 | // parent::en/decrypt() is able to do all the work for all modes and keylengths, |
| 319 | // except for: CRYPT_MODE_3CBC (inner chaining CBC) with a key > 64bits |
| 320 | |
| 321 | // if the key is smaller then 8, do what we'd normally do |
| 322 | if ($this->mode_3cbc && strlen($this->key) > 8) { |
| 323 | return $this->des[2]->encrypt( |
| 324 | $this->des[1]->decrypt( |
| 325 | $this->des[0]->encrypt( |
| 326 | $this->_pad($plaintext) |
| 327 | ) |
| 328 | ) |
| 329 | ); |
| 330 | } |
| 331 | |
| 332 | return parent::encrypt($plaintext); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Decrypts a message. |
| 337 | * |
| 338 | * @see Crypt_Base::decrypt() |
| 339 | * @access public |
| 340 | * @param String $ciphertext |
| 341 | * @return String $plaintext |
| 342 | */ |
| 343 | function decrypt($ciphertext) |
| 344 | { |
| 345 | if ($this->mode_3cbc && strlen($this->key) > 8) { |
| 346 | return $this->_unpad( |
| 347 | $this->des[0]->decrypt( |
| 348 | $this->des[1]->encrypt( |
| 349 | $this->des[2]->decrypt( |
| 350 | str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, "\0") |
| 351 | ) |
| 352 | ) |
| 353 | ) |
| 354 | ); |
| 355 | } |
| 356 | |
| 357 | return parent::decrypt($ciphertext); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Treat consecutive "packets" as if they are a continuous buffer. |
| 362 | * |
| 363 | * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets |
| 364 | * will yield different outputs: |
| 365 | * |
| 366 | * <code> |
| 367 | * echo $des->encrypt(substr($plaintext, 0, 8)); |
| 368 | * echo $des->encrypt(substr($plaintext, 8, 8)); |
| 369 | * </code> |
| 370 | * <code> |
| 371 | * echo $des->encrypt($plaintext); |
| 372 | * </code> |
| 373 | * |
| 374 | * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates |
| 375 | * another, as demonstrated with the following: |
| 376 | * |
| 377 | * <code> |
| 378 | * $des->encrypt(substr($plaintext, 0, 8)); |
| 379 | * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8))); |
| 380 | * </code> |
| 381 | * <code> |
| 382 | * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8))); |
| 383 | * </code> |
| 384 | * |
| 385 | * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different |
| 386 | * outputs. The reason is due to the fact that the initialization vector's change after every encryption / |
| 387 | * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant. |
| 388 | * |
| 389 | * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each |
| 390 | * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that |
| 391 | * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them), |
| 392 | * however, they are also less intuitive and more likely to cause you problems. |
| 393 | * |
| 394 | * @see Crypt_Base::enableContinuousBuffer() |
| 395 | * @see Crypt_TripleDES::disableContinuousBuffer() |
| 396 | * @access public |
| 397 | */ |
| 398 | function enableContinuousBuffer() |
| 399 | { |
| 400 | parent::enableContinuousBuffer(); |
| 401 | if ($this->mode_3cbc) { |
| 402 | $this->des[0]->enableContinuousBuffer(); |
| 403 | $this->des[1]->enableContinuousBuffer(); |
| 404 | $this->des[2]->enableContinuousBuffer(); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Treat consecutive packets as if they are a discontinuous buffer. |
| 410 | * |
| 411 | * The default behavior. |
| 412 | * |
| 413 | * @see Crypt_Base::disableContinuousBuffer() |
| 414 | * @see Crypt_TripleDES::enableContinuousBuffer() |
| 415 | * @access public |
| 416 | */ |
| 417 | function disableContinuousBuffer() |
| 418 | { |
| 419 | parent::disableContinuousBuffer(); |
| 420 | if ($this->mode_3cbc) { |
| 421 | $this->des[0]->disableContinuousBuffer(); |
| 422 | $this->des[1]->disableContinuousBuffer(); |
| 423 | $this->des[2]->disableContinuousBuffer(); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Creates the key schedule |
| 429 | * |
| 430 | * @see Crypt_DES::_setupKey() |
| 431 | * @see Crypt_Base::_setupKey() |
| 432 | * @access private |
| 433 | */ |
| 434 | function _setupKey() |
| 435 | { |
| 436 | switch (true) { |
| 437 | // if $key <= 64bits we configure our internal pure-php cipher engine |
| 438 | // to act as regular [1]DES, not as 3DES. mcrypt.so::tripledes does the same. |
| 439 | case strlen($this->key) <= 8: |
| 440 | $this->des_rounds = 1; |
| 441 | break; |
| 442 | |
| 443 | // otherwise, if $key > 64bits, we configure our engine to work as 3DES. |
| 444 | default: |
| 445 | $this->des_rounds = 3; |
| 446 | |
| 447 | // (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately. |
| 448 | if ($this->mode_3cbc) { |
| 449 | $this->des[0]->_setupKey(); |
| 450 | $this->des[1]->_setupKey(); |
| 451 | $this->des[2]->_setupKey(); |
| 452 | |
| 453 | // because $des[0-2] will, now, do all the work we can return here |
| 454 | // not need unnecessary stress parent::_setupKey() with our, now unused, $key. |
| 455 | return; |
| 456 | } |
| 457 | } |
| 458 | // setup our key |
| 459 | parent::_setupKey(); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Sets the internal crypt engine |
| 464 | * |
| 465 | * @see Crypt_Base::Crypt_Base() |
| 466 | * @see Crypt_Base::setPreferredEngine() |
| 467 | * @param Integer $engine |
| 468 | * @access public |
| 469 | * @return Integer |
| 470 | */ |
| 471 | function setPreferredEngine($engine) |
| 472 | { |
| 473 | if ($this->mode_3cbc) { |
| 474 | $this->des[0]->setPreferredEngine($engine); |
| 475 | $this->des[1]->setPreferredEngine($engine); |
| 476 | $this->des[2]->setPreferredEngine($engine); |
| 477 | } |
| 478 | |
| 479 | return parent::setPreferredEngine($engine); |
| 480 | } |
| 481 | } |