2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
\r
5 * Pure-PHP implementation of Triple DES.
\r
7 * Uses mcrypt, if available, and an internal implementation, otherwise. Operates in the EDE3 mode (encrypt-decrypt-encrypt).
\r
9 * PHP versions 4 and 5
\r
11 * Here's a short example of how to use this library:
\r
14 * include('Crypt/TripleDES.php');
\r
16 * $des = new Crypt_TripleDES();
\r
18 * $des->setKey('abcdefghijklmnopqrstuvwx');
\r
20 * $size = 10 * 1024;
\r
22 * for ($i = 0; $i < $size; $i++) {
\r
26 * echo $des->decrypt($des->encrypt($plaintext));
\r
30 * LICENSE: This library is free software; you can redistribute it and/or
\r
31 * modify it under the terms of the GNU Lesser General Public
\r
32 * License as published by the Free Software Foundation; either
\r
33 * version 2.1 of the License, or (at your option) any later version.
\r
35 * This library is distributed in the hope that it will be useful,
\r
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
38 * Lesser General Public License for more details.
\r
40 * You should have received a copy of the GNU Lesser General Public
\r
41 * License along with this library; if not, write to the Free Software
\r
42 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
\r
46 * @package Crypt_TripleDES
\r
47 * @author Jim Wigginton <terrafrost@php.net>
\r
48 * @copyright MMVII Jim Wigginton
\r
49 * @license http://www.gnu.org/licenses/lgpl.txt
\r
50 * @version $Id: TripleDES.php,v 1.9 2009/11/23 19:06:07 terrafrost Exp $
\r
51 * @link http://phpseclib.sourceforge.net
\r
57 require_once 'DES.php';
\r
60 * Encrypt / decrypt using inner chaining
\r
62 * Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (CRYPT_DES_MODE_CBC3).
\r
64 define('CRYPT_DES_MODE_3CBC', 3);
\r
67 * Encrypt / decrypt using outer chaining
\r
69 * Outer chaining is used by SSH-2 and when the mode is set to CRYPT_DES_MODE_CBC.
\r
71 define('CRYPT_DES_MODE_CBC3', CRYPT_DES_MODE_CBC);
\r
74 * Pure-PHP implementation of Triple DES.
\r
76 * @author Jim Wigginton <terrafrost@php.net>
\r
79 * @package Crypt_TerraDES
\r
81 class Crypt_TripleDES {
\r
85 * @see Crypt_TripleDES::setKey()
\r
89 var $key = "\0\0\0\0\0\0\0\0";
\r
92 * The Encryption Mode
\r
94 * @see Crypt_TripleDES::Crypt_TripleDES()
\r
98 var $mode = CRYPT_DES_MODE_CBC;
\r
101 * Continuous Buffer status
\r
103 * @see Crypt_TripleDES::enableContinuousBuffer()
\r
107 var $continuousBuffer = false;
\r
112 * @see Crypt_TripleDES::enablePadding()
\r
116 var $padding = true;
\r
119 * The Initialization Vector
\r
121 * @see Crypt_TripleDES::setIV()
\r
125 var $iv = "\0\0\0\0\0\0\0\0";
\r
128 * A "sliding" Initialization Vector
\r
130 * @see Crypt_TripleDES::enableContinuousBuffer()
\r
134 var $encryptIV = "\0\0\0\0\0\0\0\0";
\r
137 * A "sliding" Initialization Vector
\r
139 * @see Crypt_TripleDES::enableContinuousBuffer()
\r
143 var $decryptIV = "\0\0\0\0\0\0\0\0";
\r
146 * MCrypt parameters
\r
148 * @see Crypt_TripleDES::setMCrypt()
\r
152 var $mcrypt = array('', '');
\r
155 * The Crypt_DES objects
\r
163 * Default Constructor.
\r
165 * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
\r
166 * CRYPT_DES_MODE_ECB or CRYPT_DES_MODE_CBC. If not explictly set, CRYPT_DES_MODE_CBC will be used.
\r
168 * @param optional Integer $mode
\r
169 * @return Crypt_TripleDES
\r
172 function Crypt_TripleDES($mode = CRYPT_DES_MODE_CBC)
\r
174 if ( !defined('CRYPT_DES_MODE') ) {
\r
176 case extension_loaded('mcrypt'):
\r
177 // i'd check to see if des was supported, by doing in_array('des', mcrypt_list_algorithms('')),
\r
178 // but since that can be changed after the object has been created, there doesn't seem to be
\r
179 // a lot of point...
\r
180 define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT);
\r
183 define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL);
\r
187 if ( $mode == CRYPT_DES_MODE_3CBC ) {
\r
188 $this->mode = CRYPT_DES_MODE_3CBC;
\r
189 $this->des = array(
\r
190 new Crypt_DES(CRYPT_DES_MODE_CBC),
\r
191 new Crypt_DES(CRYPT_DES_MODE_CBC),
\r
192 new Crypt_DES(CRYPT_DES_MODE_CBC)
\r
195 // we're going to be doing the padding, ourselves, so disable it in the Crypt_DES objects
\r
196 $this->des[0]->disablePadding();
\r
197 $this->des[1]->disablePadding();
\r
198 $this->des[2]->disablePadding();
\r
203 switch ( CRYPT_DES_MODE ) {
\r
204 case CRYPT_DES_MODE_MCRYPT:
\r
206 case CRYPT_DES_MODE_ECB:
\r
207 $this->mode = MCRYPT_MODE_ECB; break;
\r
208 case CRYPT_DES_MODE_CBC:
\r
210 $this->mode = MCRYPT_MODE_CBC;
\r
215 $this->des = array(
\r
216 new Crypt_DES(CRYPT_DES_MODE_ECB),
\r
217 new Crypt_DES(CRYPT_DES_MODE_ECB),
\r
218 new Crypt_DES(CRYPT_DES_MODE_ECB)
\r
221 // we're going to be doing the padding, ourselves, so disable it in the Crypt_DES objects
\r
222 $this->des[0]->disablePadding();
\r
223 $this->des[1]->disablePadding();
\r
224 $this->des[2]->disablePadding();
\r
227 case CRYPT_DES_MODE_ECB:
\r
228 case CRYPT_DES_MODE_CBC:
\r
229 $this->mode = $mode;
\r
232 $this->mode = CRYPT_DES_MODE_CBC;
\r
240 * Keys can be of any length. Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or
\r
241 * 192-bit (eg. strlen($key) == 24) keys. This function pads and truncates $key as appropriate.
\r
243 * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
\r
245 * If the key is not explicitly set, it'll be assumed to be all zero's.
\r
248 * @param String $key
\r
250 function setKey($key)
\r
252 $length = strlen($key);
\r
254 $key = str_pad($key, 24, chr(0));
\r
255 // if $key is between 64 and 128-bits, use the first 64-bits as the last, per this:
\r
256 // http://php.net/function.mcrypt-encrypt#47973
\r
257 $key = $length <= 16 ? substr_replace($key, substr($key, 0, 8), 16) : substr($key, 0, 24);
\r
261 case CRYPT_DES_MODE == CRYPT_DES_MODE_INTERNAL:
\r
262 case $this->mode == CRYPT_DES_MODE_3CBC:
\r
263 $this->des[0]->setKey(substr($key, 0, 8));
\r
264 $this->des[1]->setKey(substr($key, 8, 8));
\r
265 $this->des[2]->setKey(substr($key, 16, 8));
\r
270 * Sets the initialization vector. (optional)
\r
272 * SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explictly set, it'll be assumed
\r
273 * to be all zero's.
\r
276 * @param String $iv
\r
278 function setIV($iv)
\r
280 $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0));
\r
281 if ($this->mode == CRYPT_DES_MODE_3CBC) {
\r
282 $this->des[0]->setIV($iv);
\r
283 $this->des[1]->setIV($iv);
\r
284 $this->des[2]->setIV($iv);
\r
289 * Sets MCrypt parameters. (optional)
\r
291 * If MCrypt is being used, empty strings will be used, unless otherwise specified.
\r
293 * @link http://php.net/function.mcrypt-module-open#function.mcrypt-module-open
\r
295 * @param optional Integer $algorithm_directory
\r
296 * @param optional Integer $mode_directory
\r
298 function setMCrypt($algorithm_directory = '', $mode_directory = '')
\r
300 $this->mcrypt = array($algorithm_directory, $mode_directory);
\r
301 if ( $this->mode == CRYPT_DES_MODE_3CBC ) {
\r
302 $this->des[0]->setMCrypt($algorithm_directory, $mode_directory);
\r
303 $this->des[1]->setMCrypt($algorithm_directory, $mode_directory);
\r
304 $this->des[2]->setMCrypt($algorithm_directory, $mode_directory);
\r
309 * Encrypts a message.
\r
312 * @param String $plaintext
\r
314 function encrypt($plaintext)
\r
316 $plaintext = $this->_pad($plaintext);
\r
318 // if the key is smaller then 8, do what we'd normally do
\r
319 if ($this->mode == CRYPT_DES_MODE_3CBC && strlen($this->key) > 8) {
\r
320 $ciphertext = $this->des[2]->encrypt($this->des[1]->decrypt($this->des[0]->encrypt($plaintext)));
\r
322 return $ciphertext;
\r
325 if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
\r
326 $td = mcrypt_module_open(MCRYPT_3DES, $this->mcrypt[0], $this->mode, $this->mcrypt[1]);
\r
327 mcrypt_generic_init($td, $this->key, $this->encryptIV);
\r
329 $ciphertext = mcrypt_generic($td, $plaintext);
\r
331 mcrypt_generic_deinit($td);
\r
332 mcrypt_module_close($td);
\r
334 if ($this->continuousBuffer) {
\r
335 $this->encryptIV = substr($ciphertext, -8);
\r
338 return $ciphertext;
\r
341 if (strlen($this->key) <= 8) {
\r
342 $this->des[0]->mode = $this->mode;
\r
344 return $this->des[0]->encrypt($plaintext);
\r
347 // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
\r
348 // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
\r
349 $plaintext = str_pad($plaintext, ceil(strlen($plaintext) / 8) * 8, chr(0));
\r
352 switch ($this->mode) {
\r
353 case CRYPT_DES_MODE_ECB:
\r
354 for ($i = 0; $i < strlen($plaintext); $i+=8) {
\r
355 $block = substr($plaintext, $i, 8);
\r
356 $block = $this->des[0]->_processBlock($block, CRYPT_DES_ENCRYPT);
\r
357 $block = $this->des[1]->_processBlock($block, CRYPT_DES_DECRYPT);
\r
358 $block = $this->des[2]->_processBlock($block, CRYPT_DES_ENCRYPT);
\r
359 $ciphertext.= $block;
\r
362 case CRYPT_DES_MODE_CBC:
\r
363 $xor = $this->encryptIV;
\r
364 for ($i = 0; $i < strlen($plaintext); $i+=8) {
\r
365 $block = substr($plaintext, $i, 8) ^ $xor;
\r
366 $block = $this->des[0]->_processBlock($block, CRYPT_DES_ENCRYPT);
\r
367 $block = $this->des[1]->_processBlock($block, CRYPT_DES_DECRYPT);
\r
368 $block = $this->des[2]->_processBlock($block, CRYPT_DES_ENCRYPT);
\r
370 $ciphertext.= $block;
\r
372 if ($this->continuousBuffer) {
\r
373 $this->encryptIV = $xor;
\r
377 return $ciphertext;
\r
381 * Decrypts a message.
\r
384 * @param String $ciphertext
\r
386 function decrypt($ciphertext)
\r
388 if ($this->mode == CRYPT_DES_MODE_3CBC && strlen($this->key) > 8) {
\r
389 $plaintext = $this->des[0]->decrypt($this->des[1]->encrypt($this->des[2]->decrypt($ciphertext)));
\r
391 return $this->_unpad($plaintext);
\r
394 // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
\r
395 // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
\r
396 $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, chr(0));
\r
398 if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {
\r
399 $td = mcrypt_module_open(MCRYPT_3DES, $this->mcrypt[0], $this->mode, $this->mcrypt[1]);
\r
400 mcrypt_generic_init($td, $this->key, $this->decryptIV);
\r
402 $plaintext = mdecrypt_generic($td, $ciphertext);
\r
404 mcrypt_generic_deinit($td);
\r
405 mcrypt_module_close($td);
\r
407 if ($this->continuousBuffer) {
\r
408 $this->decryptIV = substr($ciphertext, -8);
\r
411 return $this->_unpad($plaintext);
\r
414 if (strlen($this->key) <= 8) {
\r
415 $this->des[0]->mode = $this->mode;
\r
417 return $this->_unpad($this->des[0]->decrypt($plaintext));
\r
421 switch ($this->mode) {
\r
422 case CRYPT_DES_MODE_ECB:
\r
423 for ($i = 0; $i < strlen($ciphertext); $i+=8) {
\r
424 $block = substr($ciphertext, $i, 8);
\r
425 $block = $this->des[2]->_processBlock($block, CRYPT_DES_DECRYPT);
\r
426 $block = $this->des[1]->_processBlock($block, CRYPT_DES_ENCRYPT);
\r
427 $block = $this->des[0]->_processBlock($block, CRYPT_DES_DECRYPT);
\r
428 $plaintext.= $block;
\r
431 case CRYPT_DES_MODE_CBC:
\r
432 $xor = $this->decryptIV;
\r
433 for ($i = 0; $i < strlen($ciphertext); $i+=8) {
\r
434 $orig = $block = substr($ciphertext, $i, 8);
\r
435 $block = $this->des[2]->_processBlock($block, CRYPT_DES_DECRYPT);
\r
436 $block = $this->des[1]->_processBlock($block, CRYPT_DES_ENCRYPT);
\r
437 $block = $this->des[0]->_processBlock($block, CRYPT_DES_DECRYPT);
\r
438 $plaintext.= $block ^ $xor;
\r
441 if ($this->continuousBuffer) {
\r
442 $this->decryptIV = $xor;
\r
446 return $this->_unpad($plaintext);
\r
450 * Treat consecutive "packets" as if they are a continuous buffer.
\r
452 * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
\r
453 * will yield different outputs:
\r
456 * echo $des->encrypt(substr($plaintext, 0, 8));
\r
457 * echo $des->encrypt(substr($plaintext, 8, 8));
\r
460 * echo $des->encrypt($plaintext);
\r
463 * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
\r
464 * another, as demonstrated with the following:
\r
467 * $des->encrypt(substr($plaintext, 0, 8));
\r
468 * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
\r
471 * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
\r
474 * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
\r
475 * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
\r
476 * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
\r
478 * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
\r
479 * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
\r
480 * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
\r
481 * however, they are also less intuitive and more likely to cause you problems.
\r
483 * @see Crypt_TripleDES::disableContinuousBuffer()
\r
486 function enableContinuousBuffer()
\r
488 $this->continuousBuffer = true;
\r
489 if ($this->mode == CRYPT_DES_MODE_3CBC) {
\r
490 $this->des[0]->enableContinuousBuffer();
\r
491 $this->des[1]->enableContinuousBuffer();
\r
492 $this->des[2]->enableContinuousBuffer();
\r
497 * Treat consecutive packets as if they are a discontinuous buffer.
\r
499 * The default behavior.
\r
501 * @see Crypt_TripleDES::enableContinuousBuffer()
\r
504 function disableContinuousBuffer()
\r
506 $this->continuousBuffer = false;
\r
507 $this->encryptIV = $this->iv;
\r
508 $this->decryptIV = $this->iv;
\r
510 if ($this->mode == CRYPT_DES_MODE_3CBC) {
\r
511 $this->des[0]->disableContinuousBuffer();
\r
512 $this->des[1]->disableContinuousBuffer();
\r
513 $this->des[2]->disableContinuousBuffer();
\r
520 * DES works by encrypting eight bytes at a time. If you ever need to encrypt or decrypt something that's not
\r
521 * a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.
\r
523 * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH1,
\r
524 * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
\r
525 * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
\r
526 * transmitted separately)
\r
528 * @see Crypt_TripleDES::disablePadding()
\r
531 function enablePadding()
\r
533 $this->padding = true;
\r
537 * Do not pad packets.
\r
539 * @see Crypt_TripleDES::enablePadding()
\r
542 function disablePadding()
\r
544 $this->padding = false;
\r
550 * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).
\r
551 * 8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)
\r
553 * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
\r
554 * and padding will, hence forth, be enabled.
\r
556 * @see Crypt_TripleDES::_unpad()
\r
559 function _pad($text)
\r
561 $length = strlen($text);
\r
563 if (!$this->padding) {
\r
564 if (($length & 7) == 0) {
\r
567 user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE);
\r
568 $this->padding = true;
\r
572 $pad = 8 - ($length & 7);
\r
573 return str_pad($text, $length + $pad, chr($pad));
\r
579 * If padding is enabled and the reported padding length is invalid, padding will be, hence forth, disabled.
\r
581 * @see Crypt_TripleDES::_pad()
\r
584 function _unpad($text)
\r
586 if (!$this->padding) {
\r
590 $length = ord($text[strlen($text) - 1]);
\r
592 if (!$length || $length > 8) {
\r
593 user_error("The number of bytes reported as being padded ($length) is invalid (block size = 8)", E_USER_NOTICE);
\r
594 $this->padding = false;
\r
598 return substr($text, 0, -$length);
\r
602 // vim: ts=4:sw=4:et:
\r