| 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 MMVII 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 |
require_once dirname(__FILE__).'/DES.php'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Encrypt / decrypt using inner chaining |
| 64 |
* |
| 65 |
* Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (CRYPT_DES_MODE_CBC3). |
| 66 |
*/ |
| 67 |
define('CRYPT_DES_MODE_3CBC', -2); |
| 68 |
|
| 69 |
/** |
| 70 |
* Encrypt / decrypt using outer chaining |
| 71 |
* |
| 72 |
* Outer chaining is used by SSH-2 and when the mode is set to CRYPT_DES_MODE_CBC. |
| 73 |
*/ |
| 74 |
define('CRYPT_DES_MODE_CBC3', CRYPT_DES_MODE_CBC); |
| 75 |
|
| 76 |
/** |
| 77 |
* Pure-PHP implementation of Triple DES. |
| 78 |
* |
| 79 |
* @package Crypt_TripleDES |
| 80 |
* @author Jim Wigginton <terrafrost@php.net> |
| 81 |
* @access public |
| 82 |
*/ |
| 83 |
class Crypt_TripleDES extends Crypt_DES |
| 84 |
{ |
| 85 |
/** |
| 86 |
* The default password key_size used by setPassword() |
| 87 |
* |
| 88 |
* @see Crypt_DES::password_key_size |
| 89 |
* @see Crypt_Base::password_key_size |
| 90 |
* @see Crypt_Base::setPassword() |
| 91 |
* @var Integer |
| 92 |
* @access private |
| 93 |
*/ |
| 94 |
public $password_key_size = 24; |
| 95 |
|
| 96 |
/** |
| 97 |
* The default salt used by setPassword() |
| 98 |
* |
| 99 |
* @see Crypt_Base::password_default_salt |
| 100 |
* @see Crypt_Base::setPassword() |
| 101 |
* @var String |
| 102 |
* @access private |
| 103 |
*/ |
| 104 |
public $password_default_salt = 'phpseclib'; |
| 105 |
|
| 106 |
/** |
| 107 |
* The namespace used by the cipher for its constants. |
| 108 |
* |
| 109 |
* @see Crypt_DES::const_namespace |
| 110 |
* @see Crypt_Base::const_namespace |
| 111 |
* @var String |
| 112 |
* @access private |
| 113 |
*/ |
| 114 |
public $const_namespace = 'DES'; |
| 115 |
|
| 116 |
/** |
| 117 |
* The mcrypt specific name of the cipher |
| 118 |
* |
| 119 |
* @see Crypt_DES::cipher_name_mcrypt |
| 120 |
* @see Crypt_Base::cipher_name_mcrypt |
| 121 |
* @var String |
| 122 |
* @access private |
| 123 |
*/ |
| 124 |
public $cipher_name_mcrypt = 'tripledes'; |
| 125 |
|
| 126 |
/** |
| 127 |
* Optimizing value while CFB-encrypting |
| 128 |
* |
| 129 |
* @see Crypt_Base::cfb_init_len |
| 130 |
* @var Integer |
| 131 |
* @access private |
| 132 |
*/ |
| 133 |
public $cfb_init_len = 750; |
| 134 |
|
| 135 |
/** |
| 136 |
* max possible size of $key |
| 137 |
* |
| 138 |
* @see Crypt_TripleDES::setKey() |
| 139 |
* @see Crypt_DES::setKey() |
| 140 |
* @var String |
| 141 |
* @access private |
| 142 |
*/ |
| 143 |
public $key_size_max = 24; |
| 144 |
|
| 145 |
/** |
| 146 |
* Internal flag whether using CRYPT_DES_MODE_3CBC or not |
| 147 |
* |
| 148 |
* @var Boolean |
| 149 |
* @access private |
| 150 |
*/ |
| 151 |
public $mode_3cbc; |
| 152 |
|
| 153 |
/** |
| 154 |
* The Crypt_DES objects |
| 155 |
* |
| 156 |
* Used only if $mode_3cbc === true |
| 157 |
* |
| 158 |
* @var Array |
| 159 |
* @access private |
| 160 |
*/ |
| 161 |
public $des; |
| 162 |
|
| 163 |
/** |
| 164 |
* Default Constructor. |
| 165 |
* |
| 166 |
* Determines whether or not the mcrypt extension should be used. |
| 167 |
* |
| 168 |
* $mode could be: |
| 169 |
* |
| 170 |
* - CRYPT_DES_MODE_ECB |
| 171 |
* |
| 172 |
* - CRYPT_DES_MODE_CBC |
| 173 |
* |
| 174 |
* - CRYPT_DES_MODE_CTR |
| 175 |
* |
| 176 |
* - CRYPT_DES_MODE_CFB |
| 177 |
* |
| 178 |
* - CRYPT_DES_MODE_OFB |
| 179 |
* |
| 180 |
* - CRYPT_DES_MODE_3CBC |
| 181 |
* |
| 182 |
* If not explicitly set, CRYPT_DES_MODE_CBC will be used. |
| 183 |
* |
| 184 |
* @see Crypt_DES::Crypt_DES() |
| 185 |
* @see Crypt_Base::Crypt_Base() |
| 186 |
* |
| 187 |
* @param optional Integer $mode |
| 188 |
* |
| 189 |
* @access public |
| 190 |
*/ |
| 191 |
public function __construct($mode = CRYPT_DES_MODE_CBC) |
| 192 |
{ |
| 193 |
switch ($mode) { |
| 194 |
// In case of CRYPT_DES_MODE_3CBC, we init as CRYPT_DES_MODE_CBC |
| 195 |
// and additional flag us internally as 3CBC |
| 196 |
case CRYPT_DES_MODE_3CBC: |
| 197 |
parent::__construct(CRYPT_DES_MODE_CBC); |
| 198 |
$this->mode_3cbc = true; |
| 199 |
|
| 200 |
// This three $des'es will do the 3CBC work (if $key > 64bits) |
| 201 |
$this->des = array( |
| 202 |
new Crypt_DES(CRYPT_DES_MODE_CBC), |
| 203 |
new Crypt_DES(CRYPT_DES_MODE_CBC), |
| 204 |
new Crypt_DES(CRYPT_DES_MODE_CBC), |
| 205 |
); |
| 206 |
|
| 207 |
// we're going to be doing the padding, ourselves, so disable it in the Crypt_DES objects |
| 208 |
$this->des[0]->disablePadding(); |
| 209 |
$this->des[1]->disablePadding(); |
| 210 |
$this->des[2]->disablePadding(); |
| 211 |
break; |
| 212 |
// If not 3CBC, we init as usual |
| 213 |
default: |
| 214 |
parent::__construct($mode); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Sets the initialization vector. (optional) |
| 220 |
* |
| 221 |
* SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explicitly set, it'll be assumed |
| 222 |
* to be all zero's. |
| 223 |
* |
| 224 |
* @see Crypt_Base::setIV() |
| 225 |
* @access public |
| 226 |
* |
| 227 |
* @param String $iv |
| 228 |
*/ |
| 229 |
public function setIV($iv) |
| 230 |
{ |
| 231 |
parent::setIV($iv); |
| 232 |
if ($this->mode_3cbc) { |
| 233 |
$this->des[0]->setIV($iv); |
| 234 |
$this->des[1]->setIV($iv); |
| 235 |
$this->des[2]->setIV($iv); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Sets the key. |
| 241 |
* |
| 242 |
* Keys can be of any length. Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or |
| 243 |
* 192-bit (eg. strlen($key) == 24) keys. This function pads and truncates $key as appropriate. |
| 244 |
* |
| 245 |
* DES also requires that every eighth bit be a parity bit, however, we'll ignore that. |
| 246 |
* |
| 247 |
* If the key is not explicitly set, it'll be assumed to be all null bytes. |
| 248 |
* |
| 249 |
* @access public |
| 250 |
* @see Crypt_DES::setKey() |
| 251 |
* @see Crypt_Base::setKey() |
| 252 |
* |
| 253 |
* @param String $key |
| 254 |
*/ |
| 255 |
public function setKey($key) |
| 256 |
{ |
| 257 |
$length = strlen($key); |
| 258 |
if ($length > 8) { |
| 259 |
$key = str_pad(substr($key, 0, 24), 24, chr(0)); |
| 260 |
// if $key is between 64 and 128-bits, use the first 64-bits as the last, per this: |
| 261 |
// http://php.net/function.mcrypt-encrypt#47973 |
| 262 |
//$key = $length <= 16 ? substr_replace($key, substr($key, 0, 8), 16) : substr($key, 0, 24); |
| 263 |
} else { |
| 264 |
$key = str_pad($key, 8, chr(0)); |
| 265 |
} |
| 266 |
parent::setKey($key); |
| 267 |
|
| 268 |
// And in case of CRYPT_DES_MODE_3CBC: |
| 269 |
// if key <= 64bits we not need the 3 $des to work, |
| 270 |
// because we will then act as regular DES-CBC with just a <= 64bit key. |
| 271 |
// So only if the key > 64bits (> 8 bytes) we will call setKey() for the 3 $des. |
| 272 |
if ($this->mode_3cbc && $length > 8) { |
| 273 |
$this->des[0]->setKey(substr($key, 0, 8)); |
| 274 |
$this->des[1]->setKey(substr($key, 8, 8)); |
| 275 |
$this->des[2]->setKey(substr($key, 16, 8)); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Encrypts a message. |
| 281 |
* |
| 282 |
* @see Crypt_Base::encrypt() |
| 283 |
* @access public |
| 284 |
* |
| 285 |
* @param String $plaintext |
| 286 |
* |
| 287 |
* @return String $cipertext |
| 288 |
*/ |
| 289 |
public function encrypt($plaintext) |
| 290 |
{ |
| 291 |
// parent::en/decrypt() is able to do all the work for all modes and keylengths, |
| 292 |
// except for: CRYPT_DES_MODE_3CBC (inner chaining CBC) with a key > 64bits |
| 293 |
|
| 294 |
// if the key is smaller then 8, do what we'd normally do |
| 295 |
if ($this->mode_3cbc && strlen($this->key) > 8) { |
| 296 |
return $this->des[2]->encrypt( |
| 297 |
$this->des[1]->decrypt( |
| 298 |
$this->des[0]->encrypt( |
| 299 |
$this->_pad($plaintext) |
| 300 |
) |
| 301 |
) |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
return parent::encrypt($plaintext); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Decrypts a message. |
| 310 |
* |
| 311 |
* @see Crypt_Base::decrypt() |
| 312 |
* @access public |
| 313 |
* |
| 314 |
* @param String $ciphertext |
| 315 |
* |
| 316 |
* @return String $plaintext |
| 317 |
*/ |
| 318 |
public function decrypt($ciphertext) |
| 319 |
{ |
| 320 |
if ($this->mode_3cbc && strlen($this->key) > 8) { |
| 321 |
return $this->_unpad( |
| 322 |
$this->des[0]->decrypt( |
| 323 |
$this->des[1]->encrypt( |
| 324 |
$this->des[2]->decrypt( |
| 325 |
str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, "\0") |
| 326 |
) |
| 327 |
) |
| 328 |
) |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
return parent::decrypt($ciphertext); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Treat consecutive "packets" as if they are a continuous buffer. |
| 337 |
* |
| 338 |
* Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets |
| 339 |
* will yield different outputs: |
| 340 |
* |
| 341 |
* <code> |
| 342 |
* echo $des->encrypt(substr($plaintext, 0, 8)); |
| 343 |
* echo $des->encrypt(substr($plaintext, 8, 8)); |
| 344 |
* </code> |
| 345 |
* <code> |
| 346 |
* echo $des->encrypt($plaintext); |
| 347 |
* </code> |
| 348 |
* |
| 349 |
* The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates |
| 350 |
* another, as demonstrated with the following: |
| 351 |
* |
| 352 |
* <code> |
| 353 |
* $des->encrypt(substr($plaintext, 0, 8)); |
| 354 |
* echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8))); |
| 355 |
* </code> |
| 356 |
* <code> |
| 357 |
* echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8))); |
| 358 |
* </code> |
| 359 |
* |
| 360 |
* With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different |
| 361 |
* outputs. The reason is due to the fact that the initialization vector's change after every encryption / |
| 362 |
* decryption round when the continuous buffer is enabled. When it's disabled, they remain constant. |
| 363 |
* |
| 364 |
* Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each |
| 365 |
* encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that |
| 366 |
* continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them), |
| 367 |
* however, they are also less intuitive and more likely to cause you problems. |
| 368 |
* |
| 369 |
* @see Crypt_Base::enableContinuousBuffer() |
| 370 |
* @see Crypt_TripleDES::disableContinuousBuffer() |
| 371 |
* @access public |
| 372 |
*/ |
| 373 |
public function enableContinuousBuffer() |
| 374 |
{ |
| 375 |
parent::enableContinuousBuffer(); |
| 376 |
if ($this->mode_3cbc) { |
| 377 |
$this->des[0]->enableContinuousBuffer(); |
| 378 |
$this->des[1]->enableContinuousBuffer(); |
| 379 |
$this->des[2]->enableContinuousBuffer(); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Treat consecutive packets as if they are a discontinuous buffer. |
| 385 |
* |
| 386 |
* The default behavior. |
| 387 |
* |
| 388 |
* @see Crypt_Base::disableContinuousBuffer() |
| 389 |
* @see Crypt_TripleDES::enableContinuousBuffer() |
| 390 |
* @access public |
| 391 |
*/ |
| 392 |
public function disableContinuousBuffer() |
| 393 |
{ |
| 394 |
parent::disableContinuousBuffer(); |
| 395 |
if ($this->mode_3cbc) { |
| 396 |
$this->des[0]->disableContinuousBuffer(); |
| 397 |
$this->des[1]->disableContinuousBuffer(); |
| 398 |
$this->des[2]->disableContinuousBuffer(); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Creates the key schedule |
| 404 |
* |
| 405 |
* @see Crypt_DES::_setupKey() |
| 406 |
* @see Crypt_Base::_setupKey() |
| 407 |
* @access private |
| 408 |
*/ |
| 409 |
public function _setupKey() |
| 410 |
{ |
| 411 |
switch (true) { |
| 412 |
// if $key <= 64bits we configure our internal pure-php cipher engine |
| 413 |
// to act as regular [1]DES, not as 3DES. mcrypt.so::tripledes does the same. |
| 414 |
case strlen($this->key) <= 8: |
| 415 |
$this->des_rounds = 1; |
| 416 |
break; |
| 417 |
|
| 418 |
// otherwise, if $key > 64bits, we configure our engine to work as 3DES. |
| 419 |
default: |
| 420 |
$this->des_rounds = 3; |
| 421 |
|
| 422 |
// (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately. |
| 423 |
if ($this->mode_3cbc) { |
| 424 |
$this->des[0]->_setupKey(); |
| 425 |
$this->des[1]->_setupKey(); |
| 426 |
$this->des[2]->_setupKey(); |
| 427 |
|
| 428 |
// because $des[0-2] will, now, do all the work we can return here |
| 429 |
// not need unnecessary stress parent::_setupKey() with our, now unused, $key. |
| 430 |
return; |
| 431 |
} |
| 432 |
} |
| 433 |
// setup our key |
| 434 |
parent::_setupKey(); |
| 435 |
} |
| 436 |
} |
| 437 |
|