]> git.mxchange.org Git - friendica.git/blob - library/phpsec/Crypt/DES.php
Merge pull request #3277 from annando/issue-3142
[friendica.git] / library / phpsec / Crypt / DES.php
1 <?php\r
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */\r
3 \r
4 /**\r
5  * Pure-PHP implementation of DES.\r
6  *\r
7  * Uses mcrypt, if available, and an internal implementation, otherwise.\r
8  *\r
9  * PHP versions 4 and 5\r
10  *\r
11  * Useful resources are as follows:\r
12  *\r
13  *  - {@link http://en.wikipedia.org/wiki/DES_supplementary_material Wikipedia: DES supplementary material}\r
14  *  - {@link http://www.itl.nist.gov/fipspubs/fip46-2.htm FIPS 46-2 - (DES), Data Encryption Standard}\r
15  *  - {@link http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html JavaScript DES Example}\r
16  *\r
17  * Here's a short example of how to use this library:\r
18  * <code>\r
19  * <?php\r
20  *    include('Crypt/DES.php');\r
21  *\r
22  *    $des = new Crypt_DES();\r
23  *\r
24  *    $des->setKey('abcdefgh');\r
25  *\r
26  *    $size = 10 * 1024;\r
27  *    $plaintext = '';\r
28  *    for ($i = 0; $i < $size; $i++) {\r
29  *        $plaintext.= 'a';\r
30  *    }\r
31  *\r
32  *    echo $des->decrypt($des->encrypt($plaintext));\r
33  * ?>\r
34  * </code>\r
35  *\r
36  * LICENSE: This library is free software; you can redistribute it and/or\r
37  * modify it under the terms of the GNU Lesser General Public\r
38  * License as published by the Free Software Foundation; either\r
39  * version 2.1 of the License, or (at your option) any later version.\r
40  *\r
41  * This library is distributed in the hope that it will be useful,\r
42  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
44  * Lesser General Public License for more details.\r
45  *\r
46  * You should have received a copy of the GNU Lesser General Public\r
47  * License along with this library; if not, write to the Free Software\r
48  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,\r
49  * MA  02111-1307  USA\r
50  *\r
51  * @category   Crypt\r
52  * @package    Crypt_DES\r
53  * @author     Jim Wigginton <terrafrost@php.net>\r
54  * @copyright  MMVII Jim Wigginton\r
55  * @license    http://www.gnu.org/licenses/lgpl.txt\r
56  * @version    $Id: DES.php,v 1.12 2010/02/09 06:10:26 terrafrost Exp $\r
57  * @link       http://phpseclib.sourceforge.net\r
58  */\r
59 \r
60 /**#@+\r
61  * @access private\r
62  * @see Crypt_DES::_prepareKey()\r
63  * @see Crypt_DES::_processBlock()\r
64  */\r
65 /**\r
66  * Contains array_reverse($keys[CRYPT_DES_DECRYPT])\r
67  */\r
68 define('CRYPT_DES_ENCRYPT', 0);\r
69 /**\r
70  * Contains array_reverse($keys[CRYPT_DES_ENCRYPT])\r
71  */\r
72 define('CRYPT_DES_DECRYPT', 1);\r
73 /**#@-*/\r
74 \r
75 /**#@+\r
76  * @access public\r
77  * @see Crypt_DES::encrypt()\r
78  * @see Crypt_DES::decrypt()\r
79  */\r
80 /**\r
81  * Encrypt / decrypt using the Counter mode.\r
82  *\r
83  * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.\r
84  *\r
85  * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29\r
86  */\r
87 define('CRYPT_DES_MODE_CTR', -1);\r
88 /**\r
89  * Encrypt / decrypt using the Electronic Code Book mode.\r
90  *\r
91  * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29\r
92  */\r
93 define('CRYPT_DES_MODE_ECB', 1);\r
94 /**\r
95  * Encrypt / decrypt using the Code Book Chaining mode.\r
96  *\r
97  * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29\r
98  */\r
99 define('CRYPT_DES_MODE_CBC', 2);\r
100 /**#@-*/\r
101 \r
102 /**#@+\r
103  * @access private\r
104  * @see Crypt_DES::Crypt_DES()\r
105  */\r
106 /**\r
107  * Toggles the internal implementation\r
108  */\r
109 define('CRYPT_DES_MODE_INTERNAL', 1);\r
110 /**\r
111  * Toggles the mcrypt implementation\r
112  */\r
113 define('CRYPT_DES_MODE_MCRYPT', 2);\r
114 /**#@-*/\r
115 \r
116 /**\r
117  * Pure-PHP implementation of DES.\r
118  *\r
119  * @author  Jim Wigginton <terrafrost@php.net>\r
120  * @version 0.1.0\r
121  * @access  public\r
122  * @package Crypt_DES\r
123  */\r
124 class Crypt_DES {\r
125     /**\r
126      * The Key Schedule\r
127      *\r
128      * @see Crypt_DES::setKey()\r
129      * @var Array\r
130      * @access private\r
131      */\r
132     var $keys = "\0\0\0\0\0\0\0\0";\r
133 \r
134     /**\r
135      * The Encryption Mode\r
136      *\r
137      * @see Crypt_DES::Crypt_DES()\r
138      * @var Integer\r
139      * @access private\r
140      */\r
141     var $mode;\r
142 \r
143     /**\r
144      * Continuous Buffer status\r
145      *\r
146      * @see Crypt_DES::enableContinuousBuffer()\r
147      * @var Boolean\r
148      * @access private\r
149      */\r
150     var $continuousBuffer = false;\r
151 \r
152     /**\r
153      * Padding status\r
154      *\r
155      * @see Crypt_DES::enablePadding()\r
156      * @var Boolean\r
157      * @access private\r
158      */\r
159     var $padding = true;\r
160 \r
161     /**\r
162      * The Initialization Vector\r
163      *\r
164      * @see Crypt_DES::setIV()\r
165      * @var String\r
166      * @access private\r
167      */\r
168     var $iv = "\0\0\0\0\0\0\0\0";\r
169 \r
170     /**\r
171      * A "sliding" Initialization Vector\r
172      *\r
173      * @see Crypt_DES::enableContinuousBuffer()\r
174      * @var String\r
175      * @access private\r
176      */\r
177     var $encryptIV = "\0\0\0\0\0\0\0\0";\r
178 \r
179     /**\r
180      * A "sliding" Initialization Vector\r
181      *\r
182      * @see Crypt_DES::enableContinuousBuffer()\r
183      * @var String\r
184      * @access private\r
185      */\r
186     var $decryptIV = "\0\0\0\0\0\0\0\0";\r
187 \r
188     /**\r
189      * mcrypt resource for encryption\r
190      *\r
191      * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.\r
192      * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.\r
193      *\r
194      * @see Crypt_AES::encrypt()\r
195      * @var String\r
196      * @access private\r
197      */\r
198     var $enmcrypt;\r
199 \r
200     /**\r
201      * mcrypt resource for decryption\r
202      *\r
203      * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.\r
204      * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.\r
205      *\r
206      * @see Crypt_AES::decrypt()\r
207      * @var String\r
208      * @access private\r
209      */\r
210     var $demcrypt;\r
211 \r
212     /**\r
213      * Does the (en|de)mcrypt resource need to be (re)initialized?\r
214      *\r
215      * @see setKey()\r
216      * @see setIV()\r
217      * @var Boolean\r
218      * @access private\r
219      */\r
220     var $changed = true;\r
221 \r
222     /**\r
223      * Default Constructor.\r
224      *\r
225      * Determines whether or not the mcrypt extension should be used.  $mode should only, at present, be\r
226      * CRYPT_DES_MODE_ECB or CRYPT_DES_MODE_CBC.  If not explictly set, CRYPT_DES_MODE_CBC will be used.\r
227      *\r
228      * @param optional Integer $mode\r
229      * @return Crypt_DES\r
230      * @access public\r
231      */\r
232     function Crypt_DES($mode = CRYPT_MODE_DES_CBC)\r
233     {\r
234         if ( !defined('CRYPT_DES_MODE') ) {\r
235             switch (true) {\r
236                 case extension_loaded('mcrypt'):\r
237                     // i'd check to see if des was supported, by doing in_array('des', mcrypt_list_algorithms('')),\r
238                     // but since that can be changed after the object has been created, there doesn't seem to be\r
239                     // a lot of point...\r
240                     define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT);\r
241                     break;\r
242                 default:\r
243                     define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL);\r
244             }\r
245         }\r
246 \r
247         switch ( CRYPT_DES_MODE ) {\r
248             case CRYPT_DES_MODE_MCRYPT:\r
249                 switch ($mode) {\r
250                     case CRYPT_DES_MODE_ECB:\r
251                         $this->mode = MCRYPT_MODE_ECB;\r
252                         break;\r
253                     case CRYPT_DES_MODE_CTR:\r
254                         $this->mode = 'ctr';\r
255                         //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_DES_MODE_CTR;\r
256                         break;\r
257                     case CRYPT_DES_MODE_CBC:\r
258                     default:\r
259                         $this->mode = MCRYPT_MODE_CBC;\r
260                 }\r
261 \r
262                 break;\r
263             default:\r
264                 switch ($mode) {\r
265                     case CRYPT_DES_MODE_ECB:\r
266                     case CRYPT_DES_MODE_CTR:\r
267                     case CRYPT_DES_MODE_CBC:\r
268                         $this->mode = $mode;\r
269                         break;\r
270                     default:\r
271                         $this->mode = CRYPT_DES_MODE_CBC;\r
272                 }\r
273         }\r
274     }\r
275 \r
276     /**\r
277      * Sets the key.\r
278      *\r
279      * Keys can be of any length.  DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we\r
280      * only use the first eight, if $key has more then eight characters in it, and pad $key with the\r
281      * null byte if it is less then eight characters long.\r
282      *\r
283      * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.\r
284      *\r
285      * If the key is not explicitly set, it'll be assumed to be all zero's.\r
286      *\r
287      * @access public\r
288      * @param String $key\r
289      */\r
290     function setKey($key)\r
291     {\r
292         $this->keys = ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) ? substr($key, 0, 8) : $this->_prepareKey($key);\r
293         $this->changed = true;\r
294     }\r
295 \r
296     /**\r
297      * Sets the initialization vector. (optional)\r
298      *\r
299      * SetIV is not required when CRYPT_DES_MODE_ECB is being used.  If not explictly set, it'll be assumed\r
300      * to be all zero's.\r
301      *\r
302      * @access public\r
303      * @param String $iv\r
304      */\r
305     function setIV($iv)\r
306     {\r
307         $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, 8), 8, chr(0));\r
308         $this->changed = true;\r
309     }\r
310 \r
311     /**\r
312      * Generate CTR XOR encryption key\r
313      *\r
314      * Encrypt the output of this and XOR it against the ciphertext / plaintext to get the\r
315      * plaintext / ciphertext in CTR mode.\r
316      *\r
317      * @see Crypt_DES::decrypt()\r
318      * @see Crypt_DES::encrypt()\r
319      * @access public\r
320      * @param Integer $length\r
321      * @param String $iv\r
322      */\r
323     function _generate_xor($length, &$iv)\r
324     {\r
325         $xor = '';\r
326         $num_blocks = ($length + 7) >> 3;\r
327         for ($i = 0; $i < $num_blocks; $i++) {\r
328             $xor.= $iv;\r
329             for ($j = 4; $j <= 8; $j+=4) {\r
330                 $temp = substr($iv, -$j, 4);\r
331                 switch ($temp) {\r
332                     case "\xFF\xFF\xFF\xFF":\r
333                         $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);\r
334                         break;\r
335                     case "\x7F\xFF\xFF\xFF":\r
336                         $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);\r
337                         break 2;\r
338                     default:\r
339                         extract(unpack('Ncount', $temp));\r
340                         $iv = substr_replace($iv, pack('N', $count + 1), -$j, 4);\r
341                         break 2;\r
342                 }\r
343             }\r
344         }\r
345 \r
346         return $xor;\r
347     }\r
348 \r
349     /**\r
350      * Encrypts a message.\r
351      *\r
352      * $plaintext will be padded with up to 8 additional bytes.  Other DES implementations may or may not pad in the\r
353      * same manner.  Other common approaches to padding and the reasons why it's necessary are discussed in the following\r
354      * URL:\r
355      *\r
356      * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}\r
357      *\r
358      * An alternative to padding is to, separately, send the length of the file.  This is what SSH, in fact, does.\r
359      * strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that\r
360      * length.\r
361      *\r
362      * @see Crypt_DES::decrypt()\r
363      * @access public\r
364      * @param String $plaintext\r
365      */\r
366     function encrypt($plaintext)\r
367     {\r
368         if ($this->mode != CRYPT_DES_MODE_CTR && $this->mode != 'ctr') {\r
369             $plaintext = $this->_pad($plaintext);\r
370         }\r
371 \r
372         if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {\r
373             if ($this->changed) {\r
374                 if (!isset($this->enmcrypt)) {\r
375                     $this->enmcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');\r
376                 }\r
377                 mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);\r
378                 $this->changed = false;\r
379             }\r
380 \r
381             $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);\r
382 \r
383             if (!$this->continuousBuffer) {\r
384                 mcrypt_generic_init($this->enmcrypt, $this->keys, $this->encryptIV);\r
385             }\r
386 \r
387             return $ciphertext;\r
388         }\r
389 \r
390         if (!is_array($this->keys)) {\r
391             $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");\r
392         }\r
393 \r
394         $ciphertext = '';\r
395         switch ($this->mode) {\r
396             case CRYPT_DES_MODE_ECB:\r
397                 for ($i = 0; $i < strlen($plaintext); $i+=8) {\r
398                     $ciphertext.= $this->_processBlock(substr($plaintext, $i, 8), CRYPT_DES_ENCRYPT);\r
399                 }\r
400                 break;\r
401             case CRYPT_DES_MODE_CBC:\r
402                 $xor = $this->encryptIV;\r
403                 for ($i = 0; $i < strlen($plaintext); $i+=8) {\r
404                     $block = substr($plaintext, $i, 8);\r
405                     $block = $this->_processBlock($block ^ $xor, CRYPT_DES_ENCRYPT);\r
406                     $xor = $block;\r
407                     $ciphertext.= $block;\r
408                 }\r
409                 if ($this->continuousBuffer) {\r
410                     $this->encryptIV = $xor;\r
411                 }\r
412                 break;\r
413             case CRYPT_DES_MODE_CTR:\r
414                 $xor = $this->encryptIV;\r
415                 for ($i = 0; $i < strlen($plaintext); $i+=8) {\r
416                     $block = substr($plaintext, $i, 8);\r
417                     $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);\r
418                     $ciphertext.= $block ^ $key;\r
419                 }\r
420                 if ($this->continuousBuffer) {\r
421                     $this->encryptIV = $xor;\r
422                 }\r
423         }\r
424 \r
425         return $ciphertext;\r
426     }\r
427 \r
428     /**\r
429      * Decrypts a message.\r
430      *\r
431      * If strlen($ciphertext) is not a multiple of 8, null bytes will be added to the end of the string until it is.\r
432      *\r
433      * @see Crypt_DES::encrypt()\r
434      * @access public\r
435      * @param String $ciphertext\r
436      */\r
437     function decrypt($ciphertext)\r
438     {\r
439         if ($this->mode != CRYPT_DES_MODE_CTR && $this->mode != 'ctr') {\r
440             // we pad with chr(0) since that's what mcrypt_generic does.  to quote from http://php.net/function.mcrypt-generic :\r
441             // "The data is padded with "\0" to make sure the length of the data is n * blocksize."\r
442             $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 7) & 0xFFFFFFF8, chr(0));\r
443         }\r
444 \r
445         if ( CRYPT_DES_MODE == CRYPT_DES_MODE_MCRYPT ) {\r
446             if ($this->changed) {\r
447                 if (!isset($this->demcrypt)) {\r
448                     $this->demcrypt = mcrypt_module_open(MCRYPT_DES, '', $this->mode, '');\r
449                 }\r
450                 mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);\r
451                 $this->changed = false;\r
452             }\r
453 \r
454             $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);\r
455 \r
456             if (!$this->continuousBuffer) {\r
457                 mcrypt_generic_init($this->demcrypt, $this->keys, $this->decryptIV);\r
458             }\r
459 \r
460             return $this->mode != 'ctr' ? $this->_unpad($plaintext) : $plaintext;\r
461         }\r
462 \r
463         if (!is_array($this->keys)) {\r
464             $this->keys = $this->_prepareKey("\0\0\0\0\0\0\0\0");\r
465         }\r
466 \r
467         $plaintext = '';\r
468         switch ($this->mode) {\r
469             case CRYPT_DES_MODE_ECB:\r
470                 for ($i = 0; $i < strlen($ciphertext); $i+=8) {\r
471                     $plaintext.= $this->_processBlock(substr($ciphertext, $i, 8), CRYPT_DES_DECRYPT);\r
472                 }\r
473                 break;\r
474             case CRYPT_DES_MODE_CBC:\r
475                 $xor = $this->decryptIV;\r
476                 for ($i = 0; $i < strlen($ciphertext); $i+=8) {\r
477                     $block = substr($ciphertext, $i, 8);\r
478                     $plaintext.= $this->_processBlock($block, CRYPT_DES_DECRYPT) ^ $xor;\r
479                     $xor = $block;\r
480                 }\r
481                 if ($this->continuousBuffer) {\r
482                     $this->decryptIV = $xor;\r
483                 }\r
484                 break;\r
485             case CRYPT_DES_MODE_CTR:\r
486                 $xor = $this->decryptIV;\r
487                 for ($i = 0; $i < strlen($ciphertext); $i+=8) {\r
488                     $block = substr($ciphertext, $i, 8);\r
489                     $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT);\r
490                     $plaintext.= $block ^ $key;\r
491                 }\r
492                 if ($this->continuousBuffer) {\r
493                     $this->decryptIV = $xor;\r
494                 }\r
495         }\r
496 \r
497         return $this->mode != CRYPT_DES_MODE_CTR ? $this->_unpad($plaintext) : $plaintext;\r
498     }\r
499 \r
500     /**\r
501      * Treat consecutive "packets" as if they are a continuous buffer.\r
502      *\r
503      * Say you have a 16-byte plaintext $plaintext.  Using the default behavior, the two following code snippets\r
504      * will yield different outputs:\r
505      *\r
506      * <code>\r
507      *    echo $des->encrypt(substr($plaintext, 0, 8));\r
508      *    echo $des->encrypt(substr($plaintext, 8, 8));\r
509      * </code>\r
510      * <code>\r
511      *    echo $des->encrypt($plaintext);\r
512      * </code>\r
513      *\r
514      * The solution is to enable the continuous buffer.  Although this will resolve the above discrepancy, it creates\r
515      * another, as demonstrated with the following:\r
516      *\r
517      * <code>\r
518      *    $des->encrypt(substr($plaintext, 0, 8));\r
519      *    echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));\r
520      * </code>\r
521      * <code>\r
522      *    echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));\r
523      * </code>\r
524      *\r
525      * With the continuous buffer disabled, these would yield the same output.  With it enabled, they yield different\r
526      * outputs.  The reason is due to the fact that the initialization vector's change after every encryption /\r
527      * decryption round when the continuous buffer is enabled.  When it's disabled, they remain constant.\r
528      *\r
529      * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each\r
530      * encryption / decryption round, whereas otherwise, it'd remain constant.  For this reason, it's recommended that\r
531      * continuous buffers not be used.  They do offer better security and are, in fact, sometimes required (SSH uses them),\r
532      * however, they are also less intuitive and more likely to cause you problems.\r
533      *\r
534      * @see Crypt_DES::disableContinuousBuffer()\r
535      * @access public\r
536      */\r
537     function enableContinuousBuffer()\r
538     {\r
539         $this->continuousBuffer = true;\r
540     }\r
541 \r
542     /**\r
543      * Treat consecutive packets as if they are a discontinuous buffer.\r
544      *\r
545      * The default behavior.\r
546      *\r
547      * @see Crypt_DES::enableContinuousBuffer()\r
548      * @access public\r
549      */\r
550     function disableContinuousBuffer()\r
551     {\r
552         $this->continuousBuffer = false;\r
553         $this->encryptIV = $this->iv;\r
554         $this->decryptIV = $this->iv;\r
555     }\r
556 \r
557     /**\r
558      * Pad "packets".\r
559      *\r
560      * DES works by encrypting eight bytes at a time.  If you ever need to encrypt or decrypt something that's not\r
561      * a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.\r
562      *\r
563      * Padding is enabled by default.  Sometimes, however, it is undesirable to pad strings.  Such is the case in SSH1,\r
564      * where "packets" are padded with random bytes before being encrypted.  Unpad these packets and you risk stripping\r
565      * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is\r
566      * transmitted separately)\r
567      *\r
568      * @see Crypt_DES::disablePadding()\r
569      * @access public\r
570      */\r
571     function enablePadding()\r
572     {\r
573         $this->padding = true;\r
574     }\r
575 \r
576     /**\r
577      * Do not pad packets.\r
578      *\r
579      * @see Crypt_DES::enablePadding()\r
580      * @access public\r
581      */\r
582     function disablePadding()\r
583     {\r
584         $this->padding = false;\r
585     }\r
586 \r
587     /**\r
588      * Pads a string\r
589      *\r
590      * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).\r
591      * 8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)\r
592      *\r
593      * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless\r
594      * and padding will, hence forth, be enabled.\r
595      *\r
596      * @see Crypt_DES::_unpad()\r
597      * @access private\r
598      */\r
599     function _pad($text)\r
600     {\r
601         $length = strlen($text);\r
602 \r
603         if (!$this->padding) {\r
604             if (($length & 7) == 0) {\r
605                 return $text;\r
606             } else {\r
607                 user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE);\r
608                 $this->padding = true;\r
609             }\r
610         }\r
611 \r
612         $pad = 8 - ($length & 7);\r
613         return str_pad($text, $length + $pad, chr($pad));\r
614     }\r
615 \r
616     /**\r
617      * Unpads a string\r
618      *\r
619      * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong\r
620      * and false will be returned.\r
621      *\r
622      * @see Crypt_DES::_pad()\r
623      * @access private\r
624      */\r
625     function _unpad($text)\r
626     {\r
627         if (!$this->padding) {\r
628             return $text;\r
629         }\r
630 \r
631         $length = ord($text[strlen($text) - 1]);\r
632 \r
633         if (!$length || $length > 8) {\r
634             return false;\r
635         }\r
636 \r
637         return substr($text, 0, -$length);\r
638     }\r
639 \r
640     /**\r
641      * Encrypts or decrypts a 64-bit block\r
642      *\r
643      * $mode should be either CRYPT_DES_ENCRYPT or CRYPT_DES_DECRYPT.  See\r
644      * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general\r
645      * idea of what this function does.\r
646      *\r
647      * @access private\r
648      * @param String $block\r
649      * @param Integer $mode\r
650      * @return String\r
651      */\r
652     function _processBlock($block, $mode)\r
653     {\r
654         // s-boxes.  in the official DES docs, they're described as being matrices that\r
655         // one accesses by using the first and last bits to determine the row and the\r
656         // middle four bits to determine the column.  in this implementation, they've\r
657         // been converted to vectors\r
658         static $sbox = array(\r
659             array(\r
660                 14,  0,  4, 15, 13,  7,  1,  4,  2, 14, 15,  2, 11, 13,  8,  1,\r
661                  3, 10 ,10,  6,  6, 12, 12, 11,  5,  9,  9,  5,  0,  3,  7,  8,\r
662                  4, 15,  1, 12, 14,  8,  8,  2, 13,  4,  6,  9,  2,  1, 11,  7,\r
663                 15,  5, 12, 11,  9,  3,  7, 14,  3, 10, 10,  0,  5,  6,  0, 13\r
664             ),\r
665             array(\r
666                 15,  3,  1, 13,  8,  4, 14,  7,  6, 15, 11,  2,  3,  8,  4, 14,\r
667                  9, 12,  7,  0,  2,  1, 13, 10, 12,  6,  0,  9,  5, 11, 10,  5,\r
668                  0, 13, 14,  8,  7, 10, 11,  1, 10,  3,  4, 15, 13,  4,  1,  2,\r
669                  5, 11,  8,  6, 12,  7,  6, 12,  9,  0,  3,  5,  2, 14, 15,  9\r
670             ),\r
671             array(\r
672                 10, 13,  0,  7,  9,  0, 14,  9,  6,  3,  3,  4, 15,  6,  5, 10,\r
673                  1,  2, 13,  8, 12,  5,  7, 14, 11, 12,  4, 11,  2, 15,  8,  1,\r
674                 13,  1,  6, 10,  4, 13,  9,  0,  8,  6, 15,  9,  3,  8,  0,  7,\r
675                 11,  4,  1, 15,  2, 14, 12,  3,  5, 11, 10,  5, 14,  2,  7, 12\r
676             ),\r
677             array(\r
678                  7, 13, 13,  8, 14, 11,  3,  5,  0,  6,  6, 15,  9,  0, 10,  3,\r
679                  1,  4,  2,  7,  8,  2,  5, 12, 11,  1, 12, 10,  4, 14, 15,  9,\r
680                 10,  3,  6, 15,  9,  0,  0,  6, 12, 10, 11,  1,  7, 13, 13,  8,\r
681                 15,  9,  1,  4,  3,  5, 14, 11,  5, 12,  2,  7,  8,  2,  4, 14\r
682             ),\r
683             array(\r
684                  2, 14, 12, 11,  4,  2,  1, 12,  7,  4, 10,  7, 11, 13,  6,  1,\r
685                  8,  5,  5,  0,  3, 15, 15, 10, 13,  3,  0,  9, 14,  8,  9,  6,\r
686                  4, 11,  2,  8,  1, 12, 11,  7, 10,  1, 13, 14,  7,  2,  8, 13,\r
687                 15,  6,  9, 15, 12,  0,  5,  9,  6, 10,  3,  4,  0,  5, 14,  3\r
688             ),\r
689             array(\r
690                 12, 10,  1, 15, 10,  4, 15,  2,  9,  7,  2, 12,  6,  9,  8,  5,\r
691                  0,  6, 13,  1,  3, 13,  4, 14, 14,  0,  7, 11,  5,  3, 11,  8,\r
692                  9,  4, 14,  3, 15,  2,  5, 12,  2,  9,  8,  5, 12, 15,  3, 10,\r
693                  7, 11,  0, 14,  4,  1, 10,  7,  1,  6, 13,  0, 11,  8,  6, 13\r
694             ),\r
695             array(\r
696                  4, 13, 11,  0,  2, 11, 14,  7, 15,  4,  0,  9,  8,  1, 13, 10,\r
697                  3, 14, 12,  3,  9,  5,  7, 12,  5,  2, 10, 15,  6,  8,  1,  6,\r
698                  1,  6,  4, 11, 11, 13, 13,  8, 12,  1,  3,  4,  7, 10, 14,  7,\r
699                 10,  9, 15,  5,  6,  0,  8, 15,  0, 14,  5,  2,  9,  3,  2, 12\r
700             ),\r
701             array(\r
702                 13,  1,  2, 15,  8, 13,  4,  8,  6, 10, 15,  3, 11,  7,  1,  4,\r
703                 10, 12,  9,  5,  3,  6, 14, 11,  5,  0,  0, 14, 12,  9,  7,  2,\r
704                  7,  2, 11,  1,  4, 14,  1,  7,  9,  4, 12, 10, 14,  8,  2, 13,\r
705                  0, 15,  6, 12, 10,  9, 13,  0, 15,  3,  3,  5,  5,  6,  8, 11\r
706             )\r
707         );\r
708 \r
709         $keys = $this->keys;\r
710 \r
711         $temp = unpack('Na/Nb', $block);\r
712         $block = array($temp['a'], $temp['b']);\r
713 \r
714         // because php does arithmetic right shifts, if the most significant bits are set, right\r
715         // shifting those into the correct position will add 1's - not 0's.  this will intefere\r
716         // with the | operation unless a second & is done.  so we isolate these bits and left shift\r
717         // them into place.  we then & each block with 0x7FFFFFFF to prevennt 1's from being added\r
718         // for any other shifts.\r
719         $msb = array(\r
720             ($block[0] >> 31) & 1,\r
721             ($block[1] >> 31) & 1\r
722         );\r
723         $block[0] &= 0x7FFFFFFF;\r
724         $block[1] &= 0x7FFFFFFF;\r
725 \r
726         // we isolate the appropriate bit in the appropriate integer and shift as appropriate.  in\r
727         // some cases, there are going to be multiple bits in the same integer that need to be shifted\r
728         // in the same way.  we combine those into one shift operation.\r
729         $block = array(\r
730             (($block[1] & 0x00000040) << 25) | (($block[1] & 0x00004000) << 16) |\r
731             (($block[1] & 0x00400001) <<  7) | (($block[1] & 0x40000100) >>  2) |\r
732             (($block[0] & 0x00000040) << 21) | (($block[0] & 0x00004000) << 12) |\r
733             (($block[0] & 0x00400001) <<  3) | (($block[0] & 0x40000100) >>  6) |\r
734             (($block[1] & 0x00000010) << 19) | (($block[1] & 0x00001000) << 10) |\r
735             (($block[1] & 0x00100000) <<  1) | (($block[1] & 0x10000000) >>  8) |\r
736             (($block[0] & 0x00000010) << 15) | (($block[0] & 0x00001000) <<  6) |\r
737             (($block[0] & 0x00100000) >>  3) | (($block[0] & 0x10000000) >> 12) |\r
738             (($block[1] & 0x00000004) << 13) | (($block[1] & 0x00000400) <<  4) |\r
739             (($block[1] & 0x00040000) >>  5) | (($block[1] & 0x04000000) >> 14) |\r
740             (($block[0] & 0x00000004) <<  9) | ( $block[0] & 0x00000400       ) |\r
741             (($block[0] & 0x00040000) >>  9) | (($block[0] & 0x04000000) >> 18) |\r
742             (($block[1] & 0x00010000) >> 11) | (($block[1] & 0x01000000) >> 20) |\r
743             (($block[0] & 0x00010000) >> 15) | (($block[0] & 0x01000000) >> 24)\r
744         ,\r
745             (($block[1] & 0x00000080) << 24) | (($block[1] & 0x00008000) << 15) |\r
746             (($block[1] & 0x00800002) <<  6) | (($block[0] & 0x00000080) << 20) |\r
747             (($block[0] & 0x00008000) << 11) | (($block[0] & 0x00800002) <<  2) |\r
748             (($block[1] & 0x00000020) << 18) | (($block[1] & 0x00002000) <<  9) |\r
749             ( $block[1] & 0x00200000       ) | (($block[1] & 0x20000000) >>  9) |\r
750             (($block[0] & 0x00000020) << 14) | (($block[0] & 0x00002000) <<  5) |\r
751             (($block[0] & 0x00200000) >>  4) | (($block[0] & 0x20000000) >> 13) |\r
752             (($block[1] & 0x00000008) << 12) | (($block[1] & 0x00000800) <<  3) |\r
753             (($block[1] & 0x00080000) >>  6) | (($block[1] & 0x08000000) >> 15) |\r
754             (($block[0] & 0x00000008) <<  8) | (($block[0] & 0x00000800) >>  1) |\r
755             (($block[0] & 0x00080000) >> 10) | (($block[0] & 0x08000000) >> 19) |\r
756             (($block[1] & 0x00000200) >>  3) | (($block[0] & 0x00000200) >>  7) |\r
757             (($block[1] & 0x00020000) >> 12) | (($block[1] & 0x02000000) >> 21) |\r
758             (($block[0] & 0x00020000) >> 16) | (($block[0] & 0x02000000) >> 25) |\r
759             ($msb[1] << 28) | ($msb[0] << 24)\r
760         );\r
761 \r
762         for ($i = 0; $i < 16; $i++) {\r
763             // start of "the Feistel (F) function" - see the following URL:\r
764             // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png\r
765             $temp = (($sbox[0][((($block[1] >> 27) & 0x1F) | (($block[1] & 1) << 5)) ^ $keys[$mode][$i][0]]) << 28)\r
766                   | (($sbox[1][(($block[1] & 0x1F800000) >> 23) ^ $keys[$mode][$i][1]]) << 24)\r
767                   | (($sbox[2][(($block[1] & 0x01F80000) >> 19) ^ $keys[$mode][$i][2]]) << 20)\r
768                   | (($sbox[3][(($block[1] & 0x001F8000) >> 15) ^ $keys[$mode][$i][3]]) << 16)\r
769                   | (($sbox[4][(($block[1] & 0x0001F800) >> 11) ^ $keys[$mode][$i][4]]) << 12)\r
770                   | (($sbox[5][(($block[1] & 0x00001F80) >>  7) ^ $keys[$mode][$i][5]]) <<  8)\r
771                   | (($sbox[6][(($block[1] & 0x000001F8) >>  3) ^ $keys[$mode][$i][6]]) <<  4)\r
772                   | ( $sbox[7][((($block[1] & 0x1F) << 1) | (($block[1] >> 31) & 1)) ^ $keys[$mode][$i][7]]);\r
773 \r
774             $msb = ($temp >> 31) & 1;\r
775             $temp &= 0x7FFFFFFF;\r
776             $newBlock = (($temp & 0x00010000) << 15) | (($temp & 0x02020120) <<  5)\r
777                       | (($temp & 0x00001800) << 17) | (($temp & 0x01000000) >> 10)\r
778                       | (($temp & 0x00000008) << 24) | (($temp & 0x00100000) <<  6)\r
779                       | (($temp & 0x00000010) << 21) | (($temp & 0x00008000) <<  9)\r
780                       | (($temp & 0x00000200) << 12) | (($temp & 0x10000000) >> 27)\r
781                       | (($temp & 0x00000040) << 14) | (($temp & 0x08000000) >>  8)\r
782                       | (($temp & 0x00004000) <<  4) | (($temp & 0x00000002) << 16)\r
783                       | (($temp & 0x00442000) >>  6) | (($temp & 0x40800000) >> 15)\r
784                       | (($temp & 0x00000001) << 11) | (($temp & 0x20000000) >> 20)\r
785                       | (($temp & 0x00080000) >> 13) | (($temp & 0x00000004) <<  3)\r
786                       | (($temp & 0x04000000) >> 22) | (($temp & 0x00000480) >>  7)\r
787                       | (($temp & 0x00200000) >> 19) | ($msb << 23);\r
788             // end of "the Feistel (F) function" - $newBlock is F's output\r
789 \r
790             $temp = $block[1];\r
791             $block[1] = $block[0] ^ $newBlock;\r
792             $block[0] = $temp;\r
793         }\r
794 \r
795         $msb = array(\r
796             ($block[0] >> 31) & 1,\r
797             ($block[1] >> 31) & 1\r
798         );\r
799         $block[0] &= 0x7FFFFFFF;\r
800         $block[1] &= 0x7FFFFFFF;\r
801 \r
802         $block = array(\r
803             (($block[0] & 0x01000004) <<  7) | (($block[1] & 0x01000004) <<  6) |\r
804             (($block[0] & 0x00010000) << 13) | (($block[1] & 0x00010000) << 12) |\r
805             (($block[0] & 0x00000100) << 19) | (($block[1] & 0x00000100) << 18) |\r
806             (($block[0] & 0x00000001) << 25) | (($block[1] & 0x00000001) << 24) |\r
807             (($block[0] & 0x02000008) >>  2) | (($block[1] & 0x02000008) >>  3) |\r
808             (($block[0] & 0x00020000) <<  4) | (($block[1] & 0x00020000) <<  3) |\r
809             (($block[0] & 0x00000200) << 10) | (($block[1] & 0x00000200) <<  9) |\r
810             (($block[0] & 0x00000002) << 16) | (($block[1] & 0x00000002) << 15) |\r
811             (($block[0] & 0x04000000) >> 11) | (($block[1] & 0x04000000) >> 12) |\r
812             (($block[0] & 0x00040000) >>  5) | (($block[1] & 0x00040000) >>  6) |\r
813             (($block[0] & 0x00000400) <<  1) | ( $block[1] & 0x00000400       ) |\r
814             (($block[0] & 0x08000000) >> 20) | (($block[1] & 0x08000000) >> 21) |\r
815             (($block[0] & 0x00080000) >> 14) | (($block[1] & 0x00080000) >> 15) |\r
816             (($block[0] & 0x00000800) >>  8) | (($block[1] & 0x00000800) >>  9)\r
817         ,\r
818             (($block[0] & 0x10000040) <<  3) | (($block[1] & 0x10000040) <<  2) |\r
819             (($block[0] & 0x00100000) <<  9) | (($block[1] & 0x00100000) <<  8) |\r
820             (($block[0] & 0x00001000) << 15) | (($block[1] & 0x00001000) << 14) |\r
821             (($block[0] & 0x00000010) << 21) | (($block[1] & 0x00000010) << 20) |\r
822             (($block[0] & 0x20000080) >>  6) | (($block[1] & 0x20000080) >>  7) |\r
823             ( $block[0] & 0x00200000       ) | (($block[1] & 0x00200000) >>  1) |\r
824             (($block[0] & 0x00002000) <<  6) | (($block[1] & 0x00002000) <<  5) |\r
825             (($block[0] & 0x00000020) << 12) | (($block[1] & 0x00000020) << 11) |\r
826             (($block[0] & 0x40000000) >> 15) | (($block[1] & 0x40000000) >> 16) |\r
827             (($block[0] & 0x00400000) >>  9) | (($block[1] & 0x00400000) >> 10) |\r
828             (($block[0] & 0x00004000) >>  3) | (($block[1] & 0x00004000) >>  4) |\r
829             (($block[0] & 0x00800000) >> 18) | (($block[1] & 0x00800000) >> 19) |\r
830             (($block[0] & 0x00008000) >> 12) | (($block[1] & 0x00008000) >> 13) |\r
831             ($msb[0] <<  7) | ($msb[1] <<  6)\r
832         );\r
833 \r
834         return pack('NN', $block[0], $block[1]);\r
835     }\r
836 \r
837     /**\r
838      * Creates the key schedule.\r
839      *\r
840      * @access private\r
841      * @param String $key\r
842      * @return Array\r
843      */\r
844     function _prepareKey($key)\r
845     {\r
846         static $shifts = array( // number of key bits shifted per round\r
847             1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1\r
848         );\r
849 \r
850         // pad the key and remove extra characters as appropriate.\r
851         $key = str_pad(substr($key, 0, 8), 8, chr(0));\r
852 \r
853         $temp = unpack('Na/Nb', $key);\r
854         $key = array($temp['a'], $temp['b']);\r
855         $msb = array(\r
856             ($key[0] >> 31) & 1,\r
857             ($key[1] >> 31) & 1\r
858         );\r
859         $key[0] &= 0x7FFFFFFF;\r
860         $key[1] &= 0x7FFFFFFF;\r
861 \r
862         $key = array(\r
863             (($key[1] & 0x00000002) << 26) | (($key[1] & 0x00000204) << 17) |\r
864             (($key[1] & 0x00020408) <<  8) | (($key[1] & 0x02040800) >>  1) |\r
865             (($key[0] & 0x00000002) << 22) | (($key[0] & 0x00000204) << 13) |\r
866             (($key[0] & 0x00020408) <<  4) | (($key[0] & 0x02040800) >>  5) |\r
867             (($key[1] & 0x04080000) >> 10) | (($key[0] & 0x04080000) >> 14) |\r
868             (($key[1] & 0x08000000) >> 19) | (($key[0] & 0x08000000) >> 23) |\r
869             (($key[0] & 0x00000010) >>  1) | (($key[0] & 0x00001000) >> 10) |\r
870             (($key[0] & 0x00100000) >> 19) | (($key[0] & 0x10000000) >> 28)\r
871         ,\r
872             (($key[1] & 0x00000080) << 20) | (($key[1] & 0x00008000) << 11) |\r
873             (($key[1] & 0x00800000) <<  2) | (($key[0] & 0x00000080) << 16) |\r
874             (($key[0] & 0x00008000) <<  7) | (($key[0] & 0x00800000) >>  2) |\r
875             (($key[1] & 0x00000040) << 13) | (($key[1] & 0x00004000) <<  4) |\r
876             (($key[1] & 0x00400000) >>  5) | (($key[1] & 0x40000000) >> 14) |\r
877             (($key[0] & 0x00000040) <<  9) | ( $key[0] & 0x00004000       ) |\r
878             (($key[0] & 0x00400000) >>  9) | (($key[0] & 0x40000000) >> 18) |\r
879             (($key[1] & 0x00000020) <<  6) | (($key[1] & 0x00002000) >>  3) |\r
880             (($key[1] & 0x00200000) >> 12) | (($key[1] & 0x20000000) >> 21) |\r
881             (($key[0] & 0x00000020) <<  2) | (($key[0] & 0x00002000) >>  7) |\r
882             (($key[0] & 0x00200000) >> 16) | (($key[0] & 0x20000000) >> 25) |\r
883             (($key[1] & 0x00000010) >>  1) | (($key[1] & 0x00001000) >> 10) |\r
884             (($key[1] & 0x00100000) >> 19) | (($key[1] & 0x10000000) >> 28) |\r
885             ($msb[1] << 24) | ($msb[0] << 20)\r
886         ); \r
887 \r
888         $keys = array();\r
889         for ($i = 0; $i < 16; $i++) {\r
890             $key[0] <<= $shifts[$i];\r
891             $temp = ($key[0] & 0xF0000000) >> 28;\r
892             $key[0] = ($key[0] | $temp) & 0x0FFFFFFF;\r
893 \r
894             $key[1] <<= $shifts[$i];\r
895             $temp = ($key[1] & 0xF0000000) >> 28;\r
896             $key[1] = ($key[1] | $temp) & 0x0FFFFFFF;\r
897 \r
898             $temp = array(\r
899                 (($key[1] & 0x00004000) >>  9) | (($key[1] & 0x00000800) >>  7) |\r
900                 (($key[1] & 0x00020000) >> 14) | (($key[1] & 0x00000010) >>  2) |\r
901                 (($key[1] & 0x08000000) >> 26) | (($key[1] & 0x00800000) >> 23)\r
902             ,\r
903                 (($key[1] & 0x02400000) >> 20) | (($key[1] & 0x00000001) <<  4) |\r
904                 (($key[1] & 0x00002000) >> 10) | (($key[1] & 0x00040000) >> 18) |\r
905                 (($key[1] & 0x00000080) >>  6)\r
906             ,\r
907                 ( $key[1] & 0x00000020       ) | (($key[1] & 0x00000200) >>  5) |\r
908                 (($key[1] & 0x00010000) >> 13) | (($key[1] & 0x01000000) >> 22) |\r
909                 (($key[1] & 0x00000004) >>  1) | (($key[1] & 0x00100000) >> 20)\r
910             ,\r
911                 (($key[1] & 0x00001000) >>  7) | (($key[1] & 0x00200000) >> 17) |\r
912                 (($key[1] & 0x00000002) <<  2) | (($key[1] & 0x00000100) >>  6) |\r
913                 (($key[1] & 0x00008000) >> 14) | (($key[1] & 0x04000000) >> 26)\r
914             ,\r
915                 (($key[0] & 0x00008000) >> 10) | ( $key[0] & 0x00000010       ) |\r
916                 (($key[0] & 0x02000000) >> 22) | (($key[0] & 0x00080000) >> 17) |\r
917                 (($key[0] & 0x00000200) >>  8) | (($key[0] & 0x00000002) >>  1)\r
918             ,\r
919                 (($key[0] & 0x04000000) >> 21) | (($key[0] & 0x00010000) >> 12) |\r
920                 (($key[0] & 0x00000020) >>  2) | (($key[0] & 0x00000800) >>  9) |\r
921                 (($key[0] & 0x00800000) >> 22) | (($key[0] & 0x00000100) >>  8)\r
922             ,\r
923                 (($key[0] & 0x00001000) >>  7) | (($key[0] & 0x00000088) >>  3) |\r
924                 (($key[0] & 0x00020000) >> 14) | (($key[0] & 0x00000001) <<  2) |\r
925                 (($key[0] & 0x00400000) >> 21)\r
926             ,\r
927                 (($key[0] & 0x00000400) >>  5) | (($key[0] & 0x00004000) >> 10) |\r
928                 (($key[0] & 0x00000040) >>  3) | (($key[0] & 0x00100000) >> 18) |\r
929                 (($key[0] & 0x08000000) >> 26) | (($key[0] & 0x01000000) >> 24)\r
930             );\r
931 \r
932             $keys[] = $temp;\r
933         }\r
934 \r
935         $temp = array(\r
936             CRYPT_DES_ENCRYPT => $keys,\r
937             CRYPT_DES_DECRYPT => array_reverse($keys)\r
938         );\r
939 \r
940         return $temp;\r
941     }\r
942 }\r
943 \r
944 // vim: ts=4:sw=4:et:\r
945 // vim6: fdl=1: