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