DES.php 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. <?php
  2. /**
  3. * Pure-PHP implementation of DES.
  4. *
  5. * Uses mcrypt, if available, and an internal implementation, otherwise.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * Useful resources are as follows:
  10. *
  11. * - {@link http://en.wikipedia.org/wiki/DES_supplementary_material Wikipedia: DES supplementary material}
  12. * - {@link http://www.itl.nist.gov/fipspubs/fip46-2.htm FIPS 46-2 - (DES), Data Encryption Standard}
  13. * - {@link http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html JavaScript DES Example}
  14. *
  15. * Here's a short example of how to use this library:
  16. * <code>
  17. * <?php
  18. * include 'Crypt/DES.php';
  19. *
  20. * $des = new Crypt_DES();
  21. *
  22. * $des->setKey('abcdefgh');
  23. *
  24. * $size = 10 * 1024;
  25. * $plaintext = '';
  26. * for ($i = 0; $i < $size; $i++) {
  27. * $plaintext.= 'a';
  28. * }
  29. *
  30. * echo $des->decrypt($des->encrypt($plaintext));
  31. * ?>
  32. * </code>
  33. *
  34. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  35. * of this software and associated documentation files (the "Software"), to deal
  36. * in the Software without restriction, including without limitation the rights
  37. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  38. * copies of the Software, and to permit persons to whom the Software is
  39. * furnished to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included in
  42. * all copies or substantial portions of the Software.
  43. *
  44. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  45. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  46. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  47. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  48. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  49. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  50. * THE SOFTWARE.
  51. *
  52. * @category Crypt
  53. * @package Crypt_DES
  54. * @author Jim Wigginton <terrafrost@php.net>
  55. * @copyright 2007 Jim Wigginton
  56. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  57. * @link http://phpseclib.sourceforge.net
  58. */
  59. /**
  60. * Include Crypt_Base
  61. *
  62. * Base cipher class
  63. */
  64. if (!class_exists('Crypt_Base')) {
  65. include_once 'Base.php';
  66. }
  67. /**#@+
  68. * @access private
  69. * @see Crypt_DES::_setupKey()
  70. * @see Crypt_DES::_processBlock()
  71. */
  72. /**
  73. * Contains $keys[CRYPT_DES_ENCRYPT]
  74. */
  75. define('CRYPT_DES_ENCRYPT', 0);
  76. /**
  77. * Contains $keys[CRYPT_DES_DECRYPT]
  78. */
  79. define('CRYPT_DES_DECRYPT', 1);
  80. /**#@-*/
  81. /**#@+
  82. * @access public
  83. * @see Crypt_DES::encrypt()
  84. * @see Crypt_DES::decrypt()
  85. */
  86. /**
  87. * Encrypt / decrypt using the Counter mode.
  88. *
  89. * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
  90. *
  91. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
  92. */
  93. define('CRYPT_DES_MODE_CTR', CRYPT_MODE_CTR);
  94. /**
  95. * Encrypt / decrypt using the Electronic Code Book mode.
  96. *
  97. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
  98. */
  99. define('CRYPT_DES_MODE_ECB', CRYPT_MODE_ECB);
  100. /**
  101. * Encrypt / decrypt using the Code Book Chaining mode.
  102. *
  103. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
  104. */
  105. define('CRYPT_DES_MODE_CBC', CRYPT_MODE_CBC);
  106. /**
  107. * Encrypt / decrypt using the Cipher Feedback mode.
  108. *
  109. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
  110. */
  111. define('CRYPT_DES_MODE_CFB', CRYPT_MODE_CFB);
  112. /**
  113. * Encrypt / decrypt using the Cipher Feedback mode.
  114. *
  115. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
  116. */
  117. define('CRYPT_DES_MODE_OFB', CRYPT_MODE_OFB);
  118. /**#@-*/
  119. /**#@+
  120. * @access private
  121. * @see Crypt_Base::Crypt_Base()
  122. */
  123. /**
  124. * Toggles the internal implementation
  125. */
  126. define('CRYPT_DES_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
  127. /**
  128. * Toggles the mcrypt implementation
  129. */
  130. define('CRYPT_DES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
  131. /**#@-*/
  132. /**
  133. * Pure-PHP implementation of DES.
  134. *
  135. * @package Crypt_DES
  136. * @author Jim Wigginton <terrafrost@php.net>
  137. * @access public
  138. */
  139. class Crypt_DES extends Crypt_Base
  140. {
  141. /**
  142. * Block Length of the cipher
  143. *
  144. * @see Crypt_Base::block_size
  145. * @var Integer
  146. * @access private
  147. */
  148. var $block_size = 8;
  149. /**
  150. * The Key
  151. *
  152. * @see Crypt_Base::key
  153. * @see setKey()
  154. * @var String
  155. * @access private
  156. */
  157. var $key = "\0\0\0\0\0\0\0\0";
  158. /**
  159. * The default password key_size used by setPassword()
  160. *
  161. * @see Crypt_Base::password_key_size
  162. * @see Crypt_Base::setPassword()
  163. * @var Integer
  164. * @access private
  165. */
  166. var $password_key_size = 8;
  167. /**
  168. * The namespace used by the cipher for its constants.
  169. *
  170. * @see Crypt_Base::const_namespace
  171. * @var String
  172. * @access private
  173. */
  174. var $const_namespace = 'DES';
  175. /**
  176. * The mcrypt specific name of the cipher
  177. *
  178. * @see Crypt_Base::cipher_name_mcrypt
  179. * @var String
  180. * @access private
  181. */
  182. var $cipher_name_mcrypt = 'des';
  183. /**
  184. * Optimizing value while CFB-encrypting
  185. *
  186. * @see Crypt_Base::cfb_init_len
  187. * @var Integer
  188. * @access private
  189. */
  190. var $cfb_init_len = 500;
  191. /**
  192. * Switch for DES/3DES encryption
  193. *
  194. * Used only if $engine == CRYPT_DES_MODE_INTERNAL
  195. *
  196. * @see Crypt_DES::_setupKey()
  197. * @see Crypt_DES::_processBlock()
  198. * @var Integer
  199. * @access private
  200. */
  201. var $des_rounds = 1;
  202. /**
  203. * max possible size of $key
  204. *
  205. * @see Crypt_DES::setKey()
  206. * @var String
  207. * @access private
  208. */
  209. var $key_size_max = 8;
  210. /**
  211. * The Key Schedule
  212. *
  213. * @see Crypt_DES::_setupKey()
  214. * @var Array
  215. * @access private
  216. */
  217. var $keys;
  218. /**
  219. * Shuffle table.
  220. *
  221. * For each byte value index, the entry holds an 8-byte string
  222. * with each byte containing all bits in the same state as the
  223. * corresponding bit in the index value.
  224. *
  225. * @see Crypt_DES::_processBlock()
  226. * @see Crypt_DES::_setupKey()
  227. * @var Array
  228. * @access private
  229. */
  230. var $shuffle = array(
  231. "\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\xFF",
  232. "\x00\x00\x00\x00\x00\x00\xFF\x00", "\x00\x00\x00\x00\x00\x00\xFF\xFF",
  233. "\x00\x00\x00\x00\x00\xFF\x00\x00", "\x00\x00\x00\x00\x00\xFF\x00\xFF",
  234. "\x00\x00\x00\x00\x00\xFF\xFF\x00", "\x00\x00\x00\x00\x00\xFF\xFF\xFF",
  235. "\x00\x00\x00\x00\xFF\x00\x00\x00", "\x00\x00\x00\x00\xFF\x00\x00\xFF",
  236. "\x00\x00\x00\x00\xFF\x00\xFF\x00", "\x00\x00\x00\x00\xFF\x00\xFF\xFF",
  237. "\x00\x00\x00\x00\xFF\xFF\x00\x00", "\x00\x00\x00\x00\xFF\xFF\x00\xFF",
  238. "\x00\x00\x00\x00\xFF\xFF\xFF\x00", "\x00\x00\x00\x00\xFF\xFF\xFF\xFF",
  239. "\x00\x00\x00\xFF\x00\x00\x00\x00", "\x00\x00\x00\xFF\x00\x00\x00\xFF",
  240. "\x00\x00\x00\xFF\x00\x00\xFF\x00", "\x00\x00\x00\xFF\x00\x00\xFF\xFF",
  241. "\x00\x00\x00\xFF\x00\xFF\x00\x00", "\x00\x00\x00\xFF\x00\xFF\x00\xFF",
  242. "\x00\x00\x00\xFF\x00\xFF\xFF\x00", "\x00\x00\x00\xFF\x00\xFF\xFF\xFF",
  243. "\x00\x00\x00\xFF\xFF\x00\x00\x00", "\x00\x00\x00\xFF\xFF\x00\x00\xFF",
  244. "\x00\x00\x00\xFF\xFF\x00\xFF\x00", "\x00\x00\x00\xFF\xFF\x00\xFF\xFF",
  245. "\x00\x00\x00\xFF\xFF\xFF\x00\x00", "\x00\x00\x00\xFF\xFF\xFF\x00\xFF",
  246. "\x00\x00\x00\xFF\xFF\xFF\xFF\x00", "\x00\x00\x00\xFF\xFF\xFF\xFF\xFF",
  247. "\x00\x00\xFF\x00\x00\x00\x00\x00", "\x00\x00\xFF\x00\x00\x00\x00\xFF",
  248. "\x00\x00\xFF\x00\x00\x00\xFF\x00", "\x00\x00\xFF\x00\x00\x00\xFF\xFF",
  249. "\x00\x00\xFF\x00\x00\xFF\x00\x00", "\x00\x00\xFF\x00\x00\xFF\x00\xFF",
  250. "\x00\x00\xFF\x00\x00\xFF\xFF\x00", "\x00\x00\xFF\x00\x00\xFF\xFF\xFF",
  251. "\x00\x00\xFF\x00\xFF\x00\x00\x00", "\x00\x00\xFF\x00\xFF\x00\x00\xFF",
  252. "\x00\x00\xFF\x00\xFF\x00\xFF\x00", "\x00\x00\xFF\x00\xFF\x00\xFF\xFF",
  253. "\x00\x00\xFF\x00\xFF\xFF\x00\x00", "\x00\x00\xFF\x00\xFF\xFF\x00\xFF",
  254. "\x00\x00\xFF\x00\xFF\xFF\xFF\x00", "\x00\x00\xFF\x00\xFF\xFF\xFF\xFF",
  255. "\x00\x00\xFF\xFF\x00\x00\x00\x00", "\x00\x00\xFF\xFF\x00\x00\x00\xFF",
  256. "\x00\x00\xFF\xFF\x00\x00\xFF\x00", "\x00\x00\xFF\xFF\x00\x00\xFF\xFF",
  257. "\x00\x00\xFF\xFF\x00\xFF\x00\x00", "\x00\x00\xFF\xFF\x00\xFF\x00\xFF",
  258. "\x00\x00\xFF\xFF\x00\xFF\xFF\x00", "\x00\x00\xFF\xFF\x00\xFF\xFF\xFF",
  259. "\x00\x00\xFF\xFF\xFF\x00\x00\x00", "\x00\x00\xFF\xFF\xFF\x00\x00\xFF",
  260. "\x00\x00\xFF\xFF\xFF\x00\xFF\x00", "\x00\x00\xFF\xFF\xFF\x00\xFF\xFF",
  261. "\x00\x00\xFF\xFF\xFF\xFF\x00\x00", "\x00\x00\xFF\xFF\xFF\xFF\x00\xFF",
  262. "\x00\x00\xFF\xFF\xFF\xFF\xFF\x00", "\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF",
  263. "\x00\xFF\x00\x00\x00\x00\x00\x00", "\x00\xFF\x00\x00\x00\x00\x00\xFF",
  264. "\x00\xFF\x00\x00\x00\x00\xFF\x00", "\x00\xFF\x00\x00\x00\x00\xFF\xFF",
  265. "\x00\xFF\x00\x00\x00\xFF\x00\x00", "\x00\xFF\x00\x00\x00\xFF\x00\xFF",
  266. "\x00\xFF\x00\x00\x00\xFF\xFF\x00", "\x00\xFF\x00\x00\x00\xFF\xFF\xFF",
  267. "\x00\xFF\x00\x00\xFF\x00\x00\x00", "\x00\xFF\x00\x00\xFF\x00\x00\xFF",
  268. "\x00\xFF\x00\x00\xFF\x00\xFF\x00", "\x00\xFF\x00\x00\xFF\x00\xFF\xFF",
  269. "\x00\xFF\x00\x00\xFF\xFF\x00\x00", "\x00\xFF\x00\x00\xFF\xFF\x00\xFF",
  270. "\x00\xFF\x00\x00\xFF\xFF\xFF\x00", "\x00\xFF\x00\x00\xFF\xFF\xFF\xFF",
  271. "\x00\xFF\x00\xFF\x00\x00\x00\x00", "\x00\xFF\x00\xFF\x00\x00\x00\xFF",
  272. "\x00\xFF\x00\xFF\x00\x00\xFF\x00", "\x00\xFF\x00\xFF\x00\x00\xFF\xFF",
  273. "\x00\xFF\x00\xFF\x00\xFF\x00\x00", "\x00\xFF\x00\xFF\x00\xFF\x00\xFF",
  274. "\x00\xFF\x00\xFF\x00\xFF\xFF\x00", "\x00\xFF\x00\xFF\x00\xFF\xFF\xFF",
  275. "\x00\xFF\x00\xFF\xFF\x00\x00\x00", "\x00\xFF\x00\xFF\xFF\x00\x00\xFF",
  276. "\x00\xFF\x00\xFF\xFF\x00\xFF\x00", "\x00\xFF\x00\xFF\xFF\x00\xFF\xFF",
  277. "\x00\xFF\x00\xFF\xFF\xFF\x00\x00", "\x00\xFF\x00\xFF\xFF\xFF\x00\xFF",
  278. "\x00\xFF\x00\xFF\xFF\xFF\xFF\x00", "\x00\xFF\x00\xFF\xFF\xFF\xFF\xFF",
  279. "\x00\xFF\xFF\x00\x00\x00\x00\x00", "\x00\xFF\xFF\x00\x00\x00\x00\xFF",
  280. "\x00\xFF\xFF\x00\x00\x00\xFF\x00", "\x00\xFF\xFF\x00\x00\x00\xFF\xFF",
  281. "\x00\xFF\xFF\x00\x00\xFF\x00\x00", "\x00\xFF\xFF\x00\x00\xFF\x00\xFF",
  282. "\x00\xFF\xFF\x00\x00\xFF\xFF\x00", "\x00\xFF\xFF\x00\x00\xFF\xFF\xFF",
  283. "\x00\xFF\xFF\x00\xFF\x00\x00\x00", "\x00\xFF\xFF\x00\xFF\x00\x00\xFF",
  284. "\x00\xFF\xFF\x00\xFF\x00\xFF\x00", "\x00\xFF\xFF\x00\xFF\x00\xFF\xFF",
  285. "\x00\xFF\xFF\x00\xFF\xFF\x00\x00", "\x00\xFF\xFF\x00\xFF\xFF\x00\xFF",
  286. "\x00\xFF\xFF\x00\xFF\xFF\xFF\x00", "\x00\xFF\xFF\x00\xFF\xFF\xFF\xFF",
  287. "\x00\xFF\xFF\xFF\x00\x00\x00\x00", "\x00\xFF\xFF\xFF\x00\x00\x00\xFF",
  288. "\x00\xFF\xFF\xFF\x00\x00\xFF\x00", "\x00\xFF\xFF\xFF\x00\x00\xFF\xFF",
  289. "\x00\xFF\xFF\xFF\x00\xFF\x00\x00", "\x00\xFF\xFF\xFF\x00\xFF\x00\xFF",
  290. "\x00\xFF\xFF\xFF\x00\xFF\xFF\x00", "\x00\xFF\xFF\xFF\x00\xFF\xFF\xFF",
  291. "\x00\xFF\xFF\xFF\xFF\x00\x00\x00", "\x00\xFF\xFF\xFF\xFF\x00\x00\xFF",
  292. "\x00\xFF\xFF\xFF\xFF\x00\xFF\x00", "\x00\xFF\xFF\xFF\xFF\x00\xFF\xFF",
  293. "\x00\xFF\xFF\xFF\xFF\xFF\x00\x00", "\x00\xFF\xFF\xFF\xFF\xFF\x00\xFF",
  294. "\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00", "\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
  295. "\xFF\x00\x00\x00\x00\x00\x00\x00", "\xFF\x00\x00\x00\x00\x00\x00\xFF",
  296. "\xFF\x00\x00\x00\x00\x00\xFF\x00", "\xFF\x00\x00\x00\x00\x00\xFF\xFF",
  297. "\xFF\x00\x00\x00\x00\xFF\x00\x00", "\xFF\x00\x00\x00\x00\xFF\x00\xFF",
  298. "\xFF\x00\x00\x00\x00\xFF\xFF\x00", "\xFF\x00\x00\x00\x00\xFF\xFF\xFF",
  299. "\xFF\x00\x00\x00\xFF\x00\x00\x00", "\xFF\x00\x00\x00\xFF\x00\x00\xFF",
  300. "\xFF\x00\x00\x00\xFF\x00\xFF\x00", "\xFF\x00\x00\x00\xFF\x00\xFF\xFF",
  301. "\xFF\x00\x00\x00\xFF\xFF\x00\x00", "\xFF\x00\x00\x00\xFF\xFF\x00\xFF",
  302. "\xFF\x00\x00\x00\xFF\xFF\xFF\x00", "\xFF\x00\x00\x00\xFF\xFF\xFF\xFF",
  303. "\xFF\x00\x00\xFF\x00\x00\x00\x00", "\xFF\x00\x00\xFF\x00\x00\x00\xFF",
  304. "\xFF\x00\x00\xFF\x00\x00\xFF\x00", "\xFF\x00\x00\xFF\x00\x00\xFF\xFF",
  305. "\xFF\x00\x00\xFF\x00\xFF\x00\x00", "\xFF\x00\x00\xFF\x00\xFF\x00\xFF",
  306. "\xFF\x00\x00\xFF\x00\xFF\xFF\x00", "\xFF\x00\x00\xFF\x00\xFF\xFF\xFF",
  307. "\xFF\x00\x00\xFF\xFF\x00\x00\x00", "\xFF\x00\x00\xFF\xFF\x00\x00\xFF",
  308. "\xFF\x00\x00\xFF\xFF\x00\xFF\x00", "\xFF\x00\x00\xFF\xFF\x00\xFF\xFF",
  309. "\xFF\x00\x00\xFF\xFF\xFF\x00\x00", "\xFF\x00\x00\xFF\xFF\xFF\x00\xFF",
  310. "\xFF\x00\x00\xFF\xFF\xFF\xFF\x00", "\xFF\x00\x00\xFF\xFF\xFF\xFF\xFF",
  311. "\xFF\x00\xFF\x00\x00\x00\x00\x00", "\xFF\x00\xFF\x00\x00\x00\x00\xFF",
  312. "\xFF\x00\xFF\x00\x00\x00\xFF\x00", "\xFF\x00\xFF\x00\x00\x00\xFF\xFF",
  313. "\xFF\x00\xFF\x00\x00\xFF\x00\x00", "\xFF\x00\xFF\x00\x00\xFF\x00\xFF",
  314. "\xFF\x00\xFF\x00\x00\xFF\xFF\x00", "\xFF\x00\xFF\x00\x00\xFF\xFF\xFF",
  315. "\xFF\x00\xFF\x00\xFF\x00\x00\x00", "\xFF\x00\xFF\x00\xFF\x00\x00\xFF",
  316. "\xFF\x00\xFF\x00\xFF\x00\xFF\x00", "\xFF\x00\xFF\x00\xFF\x00\xFF\xFF",
  317. "\xFF\x00\xFF\x00\xFF\xFF\x00\x00", "\xFF\x00\xFF\x00\xFF\xFF\x00\xFF",
  318. "\xFF\x00\xFF\x00\xFF\xFF\xFF\x00", "\xFF\x00\xFF\x00\xFF\xFF\xFF\xFF",
  319. "\xFF\x00\xFF\xFF\x00\x00\x00\x00", "\xFF\x00\xFF\xFF\x00\x00\x00\xFF",
  320. "\xFF\x00\xFF\xFF\x00\x00\xFF\x00", "\xFF\x00\xFF\xFF\x00\x00\xFF\xFF",
  321. "\xFF\x00\xFF\xFF\x00\xFF\x00\x00", "\xFF\x00\xFF\xFF\x00\xFF\x00\xFF",
  322. "\xFF\x00\xFF\xFF\x00\xFF\xFF\x00", "\xFF\x00\xFF\xFF\x00\xFF\xFF\xFF",
  323. "\xFF\x00\xFF\xFF\xFF\x00\x00\x00", "\xFF\x00\xFF\xFF\xFF\x00\x00\xFF",
  324. "\xFF\x00\xFF\xFF\xFF\x00\xFF\x00", "\xFF\x00\xFF\xFF\xFF\x00\xFF\xFF",
  325. "\xFF\x00\xFF\xFF\xFF\xFF\x00\x00", "\xFF\x00\xFF\xFF\xFF\xFF\x00\xFF",
  326. "\xFF\x00\xFF\xFF\xFF\xFF\xFF\x00", "\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF",
  327. "\xFF\xFF\x00\x00\x00\x00\x00\x00", "\xFF\xFF\x00\x00\x00\x00\x00\xFF",
  328. "\xFF\xFF\x00\x00\x00\x00\xFF\x00", "\xFF\xFF\x00\x00\x00\x00\xFF\xFF",
  329. "\xFF\xFF\x00\x00\x00\xFF\x00\x00", "\xFF\xFF\x00\x00\x00\xFF\x00\xFF",
  330. "\xFF\xFF\x00\x00\x00\xFF\xFF\x00", "\xFF\xFF\x00\x00\x00\xFF\xFF\xFF",
  331. "\xFF\xFF\x00\x00\xFF\x00\x00\x00", "\xFF\xFF\x00\x00\xFF\x00\x00\xFF",
  332. "\xFF\xFF\x00\x00\xFF\x00\xFF\x00", "\xFF\xFF\x00\x00\xFF\x00\xFF\xFF",
  333. "\xFF\xFF\x00\x00\xFF\xFF\x00\x00", "\xFF\xFF\x00\x00\xFF\xFF\x00\xFF",
  334. "\xFF\xFF\x00\x00\xFF\xFF\xFF\x00", "\xFF\xFF\x00\x00\xFF\xFF\xFF\xFF",
  335. "\xFF\xFF\x00\xFF\x00\x00\x00\x00", "\xFF\xFF\x00\xFF\x00\x00\x00\xFF",
  336. "\xFF\xFF\x00\xFF\x00\x00\xFF\x00", "\xFF\xFF\x00\xFF\x00\x00\xFF\xFF",
  337. "\xFF\xFF\x00\xFF\x00\xFF\x00\x00", "\xFF\xFF\x00\xFF\x00\xFF\x00\xFF",
  338. "\xFF\xFF\x00\xFF\x00\xFF\xFF\x00", "\xFF\xFF\x00\xFF\x00\xFF\xFF\xFF",
  339. "\xFF\xFF\x00\xFF\xFF\x00\x00\x00", "\xFF\xFF\x00\xFF\xFF\x00\x00\xFF",
  340. "\xFF\xFF\x00\xFF\xFF\x00\xFF\x00", "\xFF\xFF\x00\xFF\xFF\x00\xFF\xFF",
  341. "\xFF\xFF\x00\xFF\xFF\xFF\x00\x00", "\xFF\xFF\x00\xFF\xFF\xFF\x00\xFF",
  342. "\xFF\xFF\x00\xFF\xFF\xFF\xFF\x00", "\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF",
  343. "\xFF\xFF\xFF\x00\x00\x00\x00\x00", "\xFF\xFF\xFF\x00\x00\x00\x00\xFF",
  344. "\xFF\xFF\xFF\x00\x00\x00\xFF\x00", "\xFF\xFF\xFF\x00\x00\x00\xFF\xFF",
  345. "\xFF\xFF\xFF\x00\x00\xFF\x00\x00", "\xFF\xFF\xFF\x00\x00\xFF\x00\xFF",
  346. "\xFF\xFF\xFF\x00\x00\xFF\xFF\x00", "\xFF\xFF\xFF\x00\x00\xFF\xFF\xFF",
  347. "\xFF\xFF\xFF\x00\xFF\x00\x00\x00", "\xFF\xFF\xFF\x00\xFF\x00\x00\xFF",
  348. "\xFF\xFF\xFF\x00\xFF\x00\xFF\x00", "\xFF\xFF\xFF\x00\xFF\x00\xFF\xFF",
  349. "\xFF\xFF\xFF\x00\xFF\xFF\x00\x00", "\xFF\xFF\xFF\x00\xFF\xFF\x00\xFF",
  350. "\xFF\xFF\xFF\x00\xFF\xFF\xFF\x00", "\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF",
  351. "\xFF\xFF\xFF\xFF\x00\x00\x00\x00", "\xFF\xFF\xFF\xFF\x00\x00\x00\xFF",
  352. "\xFF\xFF\xFF\xFF\x00\x00\xFF\x00", "\xFF\xFF\xFF\xFF\x00\x00\xFF\xFF",
  353. "\xFF\xFF\xFF\xFF\x00\xFF\x00\x00", "\xFF\xFF\xFF\xFF\x00\xFF\x00\xFF",
  354. "\xFF\xFF\xFF\xFF\x00\xFF\xFF\x00", "\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF",
  355. "\xFF\xFF\xFF\xFF\xFF\x00\x00\x00", "\xFF\xFF\xFF\xFF\xFF\x00\x00\xFF",
  356. "\xFF\xFF\xFF\xFF\xFF\x00\xFF\x00", "\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF",
  357. "\xFF\xFF\xFF\xFF\xFF\xFF\x00\x00", "\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF",
  358. "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00", "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
  359. );
  360. /**
  361. * IP mapping helper table.
  362. *
  363. * Indexing this table with each source byte performs the initial bit permutation.
  364. *
  365. * @var Array
  366. * @access private
  367. */
  368. var $ipmap = array(
  369. 0x00, 0x10, 0x01, 0x11, 0x20, 0x30, 0x21, 0x31,
  370. 0x02, 0x12, 0x03, 0x13, 0x22, 0x32, 0x23, 0x33,
  371. 0x40, 0x50, 0x41, 0x51, 0x60, 0x70, 0x61, 0x71,
  372. 0x42, 0x52, 0x43, 0x53, 0x62, 0x72, 0x63, 0x73,
  373. 0x04, 0x14, 0x05, 0x15, 0x24, 0x34, 0x25, 0x35,
  374. 0x06, 0x16, 0x07, 0x17, 0x26, 0x36, 0x27, 0x37,
  375. 0x44, 0x54, 0x45, 0x55, 0x64, 0x74, 0x65, 0x75,
  376. 0x46, 0x56, 0x47, 0x57, 0x66, 0x76, 0x67, 0x77,
  377. 0x80, 0x90, 0x81, 0x91, 0xA0, 0xB0, 0xA1, 0xB1,
  378. 0x82, 0x92, 0x83, 0x93, 0xA2, 0xB2, 0xA3, 0xB3,
  379. 0xC0, 0xD0, 0xC1, 0xD1, 0xE0, 0xF0, 0xE1, 0xF1,
  380. 0xC2, 0xD2, 0xC3, 0xD3, 0xE2, 0xF2, 0xE3, 0xF3,
  381. 0x84, 0x94, 0x85, 0x95, 0xA4, 0xB4, 0xA5, 0xB5,
  382. 0x86, 0x96, 0x87, 0x97, 0xA6, 0xB6, 0xA7, 0xB7,
  383. 0xC4, 0xD4, 0xC5, 0xD5, 0xE4, 0xF4, 0xE5, 0xF5,
  384. 0xC6, 0xD6, 0xC7, 0xD7, 0xE6, 0xF6, 0xE7, 0xF7,
  385. 0x08, 0x18, 0x09, 0x19, 0x28, 0x38, 0x29, 0x39,
  386. 0x0A, 0x1A, 0x0B, 0x1B, 0x2A, 0x3A, 0x2B, 0x3B,
  387. 0x48, 0x58, 0x49, 0x59, 0x68, 0x78, 0x69, 0x79,
  388. 0x4A, 0x5A, 0x4B, 0x5B, 0x6A, 0x7A, 0x6B, 0x7B,
  389. 0x0C, 0x1C, 0x0D, 0x1D, 0x2C, 0x3C, 0x2D, 0x3D,
  390. 0x0E, 0x1E, 0x0F, 0x1F, 0x2E, 0x3E, 0x2F, 0x3F,
  391. 0x4C, 0x5C, 0x4D, 0x5D, 0x6C, 0x7C, 0x6D, 0x7D,
  392. 0x4E, 0x5E, 0x4F, 0x5F, 0x6E, 0x7E, 0x6F, 0x7F,
  393. 0x88, 0x98, 0x89, 0x99, 0xA8, 0xB8, 0xA9, 0xB9,
  394. 0x8A, 0x9A, 0x8B, 0x9B, 0xAA, 0xBA, 0xAB, 0xBB,
  395. 0xC8, 0xD8, 0xC9, 0xD9, 0xE8, 0xF8, 0xE9, 0xF9,
  396. 0xCA, 0xDA, 0xCB, 0xDB, 0xEA, 0xFA, 0xEB, 0xFB,
  397. 0x8C, 0x9C, 0x8D, 0x9D, 0xAC, 0xBC, 0xAD, 0xBD,
  398. 0x8E, 0x9E, 0x8F, 0x9F, 0xAE, 0xBE, 0xAF, 0xBF,
  399. 0xCC, 0xDC, 0xCD, 0xDD, 0xEC, 0xFC, 0xED, 0xFD,
  400. 0xCE, 0xDE, 0xCF, 0xDF, 0xEE, 0xFE, 0xEF, 0xFF
  401. );
  402. /**
  403. * Inverse IP mapping helper table.
  404. * Indexing this table with a byte value reverses the bit order.
  405. *
  406. * @var Array
  407. * @access private
  408. */
  409. var $invipmap = array(
  410. 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
  411. 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  412. 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
  413. 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  414. 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
  415. 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  416. 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
  417. 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
  418. 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
  419. 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
  420. 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
  421. 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
  422. 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
  423. 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
  424. 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
  425. 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
  426. 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
  427. 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
  428. 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
  429. 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
  430. 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
  431. 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
  432. 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
  433. 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
  434. 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
  435. 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
  436. 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
  437. 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
  438. 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
  439. 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  440. 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
  441. 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  442. );
  443. /**
  444. * Pre-permuted S-box1
  445. *
  446. * Each box ($sbox1-$sbox8) has been vectorized, then each value pre-permuted using the
  447. * P table: concatenation can then be replaced by exclusive ORs.
  448. *
  449. * @var Array
  450. * @access private
  451. */
  452. var $sbox1 = array(
  453. 0x00808200, 0x00000000, 0x00008000, 0x00808202,
  454. 0x00808002, 0x00008202, 0x00000002, 0x00008000,
  455. 0x00000200, 0x00808200, 0x00808202, 0x00000200,
  456. 0x00800202, 0x00808002, 0x00800000, 0x00000002,
  457. 0x00000202, 0x00800200, 0x00800200, 0x00008200,
  458. 0x00008200, 0x00808000, 0x00808000, 0x00800202,
  459. 0x00008002, 0x00800002, 0x00800002, 0x00008002,
  460. 0x00000000, 0x00000202, 0x00008202, 0x00800000,
  461. 0x00008000, 0x00808202, 0x00000002, 0x00808000,
  462. 0x00808200, 0x00800000, 0x00800000, 0x00000200,
  463. 0x00808002, 0x00008000, 0x00008200, 0x00800002,
  464. 0x00000200, 0x00000002, 0x00800202, 0x00008202,
  465. 0x00808202, 0x00008002, 0x00808000, 0x00800202,
  466. 0x00800002, 0x00000202, 0x00008202, 0x00808200,
  467. 0x00000202, 0x00800200, 0x00800200, 0x00000000,
  468. 0x00008002, 0x00008200, 0x00000000, 0x00808002
  469. );
  470. /**
  471. * Pre-permuted S-box2
  472. *
  473. * @var Array
  474. * @access private
  475. */
  476. var $sbox2 = array(
  477. 0x40084010, 0x40004000, 0x00004000, 0x00084010,
  478. 0x00080000, 0x00000010, 0x40080010, 0x40004010,
  479. 0x40000010, 0x40084010, 0x40084000, 0x40000000,
  480. 0x40004000, 0x00080000, 0x00000010, 0x40080010,
  481. 0x00084000, 0x00080010, 0x40004010, 0x00000000,
  482. 0x40000000, 0x00004000, 0x00084010, 0x40080000,
  483. 0x00080010, 0x40000010, 0x00000000, 0x00084000,
  484. 0x00004010, 0x40084000, 0x40080000, 0x00004010,
  485. 0x00000000, 0x00084010, 0x40080010, 0x00080000,
  486. 0x40004010, 0x40080000, 0x40084000, 0x00004000,
  487. 0x40080000, 0x40004000, 0x00000010, 0x40084010,
  488. 0x00084010, 0x00000010, 0x00004000, 0x40000000,
  489. 0x00004010, 0x40084000, 0x00080000, 0x40000010,
  490. 0x00080010, 0x40004010, 0x40000010, 0x00080010,
  491. 0x00084000, 0x00000000, 0x40004000, 0x00004010,
  492. 0x40000000, 0x40080010, 0x40084010, 0x00084000
  493. );
  494. /**
  495. * Pre-permuted S-box3
  496. *
  497. * @var Array
  498. * @access private
  499. */
  500. var $sbox3 = array(
  501. 0x00000104, 0x04010100, 0x00000000, 0x04010004,
  502. 0x04000100, 0x00000000, 0x00010104, 0x04000100,
  503. 0x00010004, 0x04000004, 0x04000004, 0x00010000,
  504. 0x04010104, 0x00010004, 0x04010000, 0x00000104,
  505. 0x04000000, 0x00000004, 0x04010100, 0x00000100,
  506. 0x00010100, 0x04010000, 0x04010004, 0x00010104,
  507. 0x04000104, 0x00010100, 0x00010000, 0x04000104,
  508. 0x00000004, 0x04010104, 0x00000100, 0x04000000,
  509. 0x04010100, 0x04000000, 0x00010004, 0x00000104,
  510. 0x00010000, 0x04010100, 0x04000100, 0x00000000,
  511. 0x00000100, 0x00010004, 0x04010104, 0x04000100,
  512. 0x04000004, 0x00000100, 0x00000000, 0x04010004,
  513. 0x04000104, 0x00010000, 0x04000000, 0x04010104,
  514. 0x00000004, 0x00010104, 0x00010100, 0x04000004,
  515. 0x04010000, 0x04000104, 0x00000104, 0x04010000,
  516. 0x00010104, 0x00000004, 0x04010004, 0x00010100
  517. );
  518. /**
  519. * Pre-permuted S-box4
  520. *
  521. * @var Array
  522. * @access private
  523. */
  524. var $sbox4 = array(
  525. 0x80401000, 0x80001040, 0x80001040, 0x00000040,
  526. 0x00401040, 0x80400040, 0x80400000, 0x80001000,
  527. 0x00000000, 0x00401000, 0x00401000, 0x80401040,
  528. 0x80000040, 0x00000000, 0x00400040, 0x80400000,
  529. 0x80000000, 0x00001000, 0x00400000, 0x80401000,
  530. 0x00000040, 0x00400000, 0x80001000, 0x00001040,
  531. 0x80400040, 0x80000000, 0x00001040, 0x00400040,
  532. 0x00001000, 0x00401040, 0x80401040, 0x80000040,
  533. 0x00400040, 0x80400000, 0x00401000, 0x80401040,
  534. 0x80000040, 0x00000000, 0x00000000, 0x00401000,
  535. 0x00001040, 0x00400040, 0x80400040, 0x80000000,
  536. 0x80401000, 0x80001040, 0x80001040, 0x00000040,
  537. 0x80401040, 0x80000040, 0x80000000, 0x00001000,
  538. 0x80400000, 0x80001000, 0x00401040, 0x80400040,
  539. 0x80001000, 0x00001040, 0x00400000, 0x80401000,
  540. 0x00000040, 0x00400000, 0x00001000, 0x00401040
  541. );
  542. /**
  543. * Pre-permuted S-box5
  544. *
  545. * @var Array
  546. * @access private
  547. */
  548. var $sbox5 = array(
  549. 0x00000080, 0x01040080, 0x01040000, 0x21000080,
  550. 0x00040000, 0x00000080, 0x20000000, 0x01040000,
  551. 0x20040080, 0x00040000, 0x01000080, 0x20040080,
  552. 0x21000080, 0x21040000, 0x00040080, 0x20000000,
  553. 0x01000000, 0x20040000, 0x20040000, 0x00000000,
  554. 0x20000080, 0x21040080, 0x21040080, 0x01000080,
  555. 0x21040000, 0x20000080, 0x00000000, 0x21000000,
  556. 0x01040080, 0x01000000, 0x21000000, 0x00040080,
  557. 0x00040000, 0x21000080, 0x00000080, 0x01000000,
  558. 0x20000000, 0x01040000, 0x21000080, 0x20040080,
  559. 0x01000080, 0x20000000, 0x21040000, 0x01040080,
  560. 0x20040080, 0x00000080, 0x01000000, 0x21040000,
  561. 0x21040080, 0x00040080, 0x21000000, 0x21040080,
  562. 0x01040000, 0x00000000, 0x20040000, 0x21000000,
  563. 0x00040080, 0x01000080, 0x20000080, 0x00040000,
  564. 0x00000000, 0x20040000, 0x01040080, 0x20000080
  565. );
  566. /**
  567. * Pre-permuted S-box6
  568. *
  569. * @var Array
  570. * @access private
  571. */
  572. var $sbox6 = array(
  573. 0x10000008, 0x10200000, 0x00002000, 0x10202008,
  574. 0x10200000, 0x00000008, 0x10202008, 0x00200000,
  575. 0x10002000, 0x00202008, 0x00200000, 0x10000008,
  576. 0x00200008, 0x10002000, 0x10000000, 0x00002008,
  577. 0x00000000, 0x00200008, 0x10002008, 0x00002000,
  578. 0x00202000, 0x10002008, 0x00000008, 0x10200008,
  579. 0x10200008, 0x00000000, 0x00202008, 0x10202000,
  580. 0x00002008, 0x00202000, 0x10202000, 0x10000000,
  581. 0x10002000, 0x00000008, 0x10200008, 0x00202000,
  582. 0x10202008, 0x00200000, 0x00002008, 0x10000008,
  583. 0x00200000, 0x10002000, 0x10000000, 0x00002008,
  584. 0x10000008, 0x10202008, 0x00202000, 0x10200000,
  585. 0x00202008, 0x10202000, 0x00000000, 0x10200008,
  586. 0x00000008, 0x00002000, 0x10200000, 0x00202008,
  587. 0x00002000, 0x00200008, 0x10002008, 0x00000000,
  588. 0x10202000, 0x10000000, 0x00200008, 0x10002008
  589. );
  590. /**
  591. * Pre-permuted S-box7
  592. *
  593. * @var Array
  594. * @access private
  595. */
  596. var $sbox7 = array(
  597. 0x00100000, 0x02100001, 0x02000401, 0x00000000,
  598. 0x00000400, 0x02000401, 0x00100401, 0x02100400,
  599. 0x02100401, 0x00100000, 0x00000000, 0x02000001,
  600. 0x00000001, 0x02000000, 0x02100001, 0x00000401,
  601. 0x02000400, 0x00100401, 0x00100001, 0x02000400,
  602. 0x02000001, 0x02100000, 0x02100400, 0x00100001,
  603. 0x02100000, 0x00000400, 0x00000401, 0x02100401,
  604. 0x00100400, 0x00000001, 0x02000000, 0x00100400,
  605. 0x02000000, 0x00100400, 0x00100000, 0x02000401,
  606. 0x02000401, 0x02100001, 0x02100001, 0x00000001,
  607. 0x00100001, 0x02000000, 0x02000400, 0x00100000,
  608. 0x02100400, 0x00000401, 0x00100401, 0x02100400,
  609. 0x00000401, 0x02000001, 0x02100401, 0x02100000,
  610. 0x00100400, 0x00000000, 0x00000001, 0x02100401,
  611. 0x00000000, 0x00100401, 0x02100000, 0x00000400,
  612. 0x02000001, 0x02000400, 0x00000400, 0x00100001
  613. );
  614. /**
  615. * Pre-permuted S-box8
  616. *
  617. * @var Array
  618. * @access private
  619. */
  620. var $sbox8 = array(
  621. 0x08000820, 0x00000800, 0x00020000, 0x08020820,
  622. 0x08000000, 0x08000820, 0x00000020, 0x08000000,
  623. 0x00020020, 0x08020000, 0x08020820, 0x00020800,
  624. 0x08020800, 0x00020820, 0x00000800, 0x00000020,
  625. 0x08020000, 0x08000020, 0x08000800, 0x00000820,
  626. 0x00020800, 0x00020020, 0x08020020, 0x08020800,
  627. 0x00000820, 0x00000000, 0x00000000, 0x08020020,
  628. 0x08000020, 0x08000800, 0x00020820, 0x00020000,
  629. 0x00020820, 0x00020000, 0x08020800, 0x00000800,
  630. 0x00000020, 0x08020020, 0x00000800, 0x00020820,
  631. 0x08000800, 0x00000020, 0x08000020, 0x08020000,
  632. 0x08020020, 0x08000000, 0x00020000, 0x08000820,
  633. 0x00000000, 0x08020820, 0x00020020, 0x08000020,
  634. 0x08020000, 0x08000800, 0x08000820, 0x00000000,
  635. 0x08020820, 0x00020800, 0x00020800, 0x00000820,
  636. 0x00000820, 0x00020020, 0x08000000, 0x08020800
  637. );
  638. /**
  639. * Sets the key.
  640. *
  641. * Keys can be of any length. DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we
  642. * only use the first eight, if $key has more then eight characters in it, and pad $key with the
  643. * null byte if it is less then eight characters long.
  644. *
  645. * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
  646. *
  647. * If the key is not explicitly set, it'll be assumed to be all zero's.
  648. *
  649. * @see Crypt_Base::setKey()
  650. * @access public
  651. * @param String $key
  652. */
  653. function setKey($key)
  654. {
  655. // We check/cut here only up to max length of the key.
  656. // Key padding to the proper length will be done in _setupKey()
  657. if (strlen($key) > $this->key_size_max) {
  658. $key = substr($key, 0, $this->key_size_max);
  659. }
  660. // Sets the key
  661. parent::setKey($key);
  662. }
  663. /**
  664. * Encrypts a block
  665. *
  666. * @see Crypt_Base::_encryptBlock()
  667. * @see Crypt_Base::encrypt()
  668. * @see Crypt_DES::encrypt()
  669. * @access private
  670. * @param String $in
  671. * @return String
  672. */
  673. function _encryptBlock($in)
  674. {
  675. return $this->_processBlock($in, CRYPT_DES_ENCRYPT);
  676. }
  677. /**
  678. * Decrypts a block
  679. *
  680. * @see Crypt_Base::_decryptBlock()
  681. * @see Crypt_Base::decrypt()
  682. * @see Crypt_DES::decrypt()
  683. * @access private
  684. * @param String $in
  685. * @return String
  686. */
  687. function _decryptBlock($in)
  688. {
  689. return $this->_processBlock($in, CRYPT_DES_DECRYPT);
  690. }
  691. /**
  692. * Encrypts or decrypts a 64-bit block
  693. *
  694. * $mode should be either CRYPT_DES_ENCRYPT or CRYPT_DES_DECRYPT. See
  695. * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general
  696. * idea of what this function does.
  697. *
  698. * @see Crypt_DES::_encryptBlock()
  699. * @see Crypt_DES::_decryptBlock()
  700. * @access private
  701. * @param String $block
  702. * @param Integer $mode
  703. * @return String
  704. */
  705. function _processBlock($block, $mode)
  706. {
  707. static $sbox1, $sbox2, $sbox3, $sbox4, $sbox5, $sbox6, $sbox7, $sbox8, $shuffleip, $shuffleinvip;
  708. if (!$sbox1) {
  709. $sbox1 = array_map("intval", $this->sbox1);
  710. $sbox2 = array_map("intval", $this->sbox2);
  711. $sbox3 = array_map("intval", $this->sbox3);
  712. $sbox4 = array_map("intval", $this->sbox4);
  713. $sbox5 = array_map("intval", $this->sbox5);
  714. $sbox6 = array_map("intval", $this->sbox6);
  715. $sbox7 = array_map("intval", $this->sbox7);
  716. $sbox8 = array_map("intval", $this->sbox8);
  717. /* Merge $shuffle with $[inv]ipmap */
  718. for ($i = 0; $i < 256; ++$i) {
  719. $shuffleip[] = $this->shuffle[$this->ipmap[$i]];
  720. $shuffleinvip[] = $this->shuffle[$this->invipmap[$i]];
  721. }
  722. }
  723. $keys = $this->keys[$mode];
  724. $ki = -1;
  725. // Do the initial IP permutation.
  726. $t = unpack('Nl/Nr', $block);
  727. list($l, $r) = array($t['l'], $t['r']);
  728. $block = ($shuffleip[ $r & 0xFF] & "\x80\x80\x80\x80\x80\x80\x80\x80") |
  729. ($shuffleip[($r >> 8) & 0xFF] & "\x40\x40\x40\x40\x40\x40\x40\x40") |
  730. ($shuffleip[($r >> 16) & 0xFF] & "\x20\x20\x20\x20\x20\x20\x20\x20") |
  731. ($shuffleip[($r >> 24) & 0xFF] & "\x10\x10\x10\x10\x10\x10\x10\x10") |
  732. ($shuffleip[ $l & 0xFF] & "\x08\x08\x08\x08\x08\x08\x08\x08") |
  733. ($shuffleip[($l >> 8) & 0xFF] & "\x04\x04\x04\x04\x04\x04\x04\x04") |
  734. ($shuffleip[($l >> 16) & 0xFF] & "\x02\x02\x02\x02\x02\x02\x02\x02") |
  735. ($shuffleip[($l >> 24) & 0xFF] & "\x01\x01\x01\x01\x01\x01\x01\x01");
  736. // Extract L0 and R0.
  737. $t = unpack('Nl/Nr', $block);
  738. list($l, $r) = array($t['l'], $t['r']);
  739. for ($des_round = 0; $des_round < $this->des_rounds; ++$des_round) {
  740. // Perform the 16 steps.
  741. for ($i = 0; $i < 16; $i++) {
  742. // start of "the Feistel (F) function" - see the following URL:
  743. // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png
  744. // Merge key schedule.
  745. $b1 = (($r >> 3) & 0x1FFFFFFF) ^ ($r << 29) ^ $keys[++$ki];
  746. $b2 = (($r >> 31) & 0x00000001) ^ ($r << 1) ^ $keys[++$ki];
  747. // S-box indexing.
  748. $t = $sbox1[($b1 >> 24) & 0x3F] ^ $sbox2[($b2 >> 24) & 0x3F] ^
  749. $sbox3[($b1 >> 16) & 0x3F] ^ $sbox4[($b2 >> 16) & 0x3F] ^
  750. $sbox5[($b1 >> 8) & 0x3F] ^ $sbox6[($b2 >> 8) & 0x3F] ^
  751. $sbox7[ $b1 & 0x3F] ^ $sbox8[ $b2 & 0x3F] ^ $l;
  752. // end of "the Feistel (F) function"
  753. $l = $r;
  754. $r = $t;
  755. }
  756. // Last step should not permute L & R.
  757. $t = $l;
  758. $l = $r;
  759. $r = $t;
  760. }
  761. // Perform the inverse IP permutation.
  762. return ($shuffleinvip[($r >> 24) & 0xFF] & "\x80\x80\x80\x80\x80\x80\x80\x80") |
  763. ($shuffleinvip[($l >> 24) & 0xFF] & "\x40\x40\x40\x40\x40\x40\x40\x40") |
  764. ($shuffleinvip[($r >> 16) & 0xFF] & "\x20\x20\x20\x20\x20\x20\x20\x20") |
  765. ($shuffleinvip[($l >> 16) & 0xFF] & "\x10\x10\x10\x10\x10\x10\x10\x10") |
  766. ($shuffleinvip[($r >> 8) & 0xFF] & "\x08\x08\x08\x08\x08\x08\x08\x08") |
  767. ($shuffleinvip[($l >> 8) & 0xFF] & "\x04\x04\x04\x04\x04\x04\x04\x04") |
  768. ($shuffleinvip[ $r & 0xFF] & "\x02\x02\x02\x02\x02\x02\x02\x02") |
  769. ($shuffleinvip[ $l & 0xFF] & "\x01\x01\x01\x01\x01\x01\x01\x01");
  770. }
  771. /**
  772. * Creates the key schedule
  773. *
  774. * @see Crypt_Base::_setupKey()
  775. * @access private
  776. */
  777. function _setupKey()
  778. {
  779. if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->des_rounds === $this->kl['des_rounds']) {
  780. // already expanded
  781. return;
  782. }
  783. $this->kl = array('key' => $this->key, 'des_rounds' => $this->des_rounds);
  784. static $shifts = array( // number of key bits shifted per round
  785. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  786. );
  787. static $pc1map = array(
  788. 0x00, 0x00, 0x08, 0x08, 0x04, 0x04, 0x0C, 0x0C,
  789. 0x02, 0x02, 0x0A, 0x0A, 0x06, 0x06, 0x0E, 0x0E,
  790. 0x10, 0x10, 0x18, 0x18, 0x14, 0x14, 0x1C, 0x1C,
  791. 0x12, 0x12, 0x1A, 0x1A, 0x16, 0x16, 0x1E, 0x1E,
  792. 0x20, 0x20, 0x28, 0x28, 0x24, 0x24, 0x2C, 0x2C,
  793. 0x22, 0x22, 0x2A, 0x2A, 0x26, 0x26, 0x2E, 0x2E,
  794. 0x30, 0x30, 0x38, 0x38, 0x34, 0x34, 0x3C, 0x3C,
  795. 0x32, 0x32, 0x3A, 0x3A, 0x36, 0x36, 0x3E, 0x3E,
  796. 0x40, 0x40, 0x48, 0x48, 0x44, 0x44, 0x4C, 0x4C,
  797. 0x42, 0x42, 0x4A, 0x4A, 0x46, 0x46, 0x4E, 0x4E,
  798. 0x50, 0x50, 0x58, 0x58, 0x54, 0x54, 0x5C, 0x5C,
  799. 0x52, 0x52, 0x5A, 0x5A, 0x56, 0x56, 0x5E, 0x5E,
  800. 0x60, 0x60, 0x68, 0x68, 0x64, 0x64, 0x6C, 0x6C,
  801. 0x62, 0x62, 0x6A, 0x6A, 0x66, 0x66, 0x6E, 0x6E,
  802. 0x70, 0x70, 0x78, 0x78, 0x74, 0x74, 0x7C, 0x7C,
  803. 0x72, 0x72, 0x7A, 0x7A, 0x76, 0x76, 0x7E, 0x7E,
  804. 0x80, 0x80, 0x88, 0x88, 0x84, 0x84, 0x8C, 0x8C,
  805. 0x82, 0x82, 0x8A, 0x8A, 0x86, 0x86, 0x8E, 0x8E,
  806. 0x90, 0x90, 0x98, 0x98, 0x94, 0x94, 0x9C, 0x9C,
  807. 0x92, 0x92, 0x9A, 0x9A, 0x96, 0x96, 0x9E, 0x9E,
  808. 0xA0, 0xA0, 0xA8, 0xA8, 0xA4, 0xA4, 0xAC, 0xAC,
  809. 0xA2, 0xA2, 0xAA, 0xAA, 0xA6, 0xA6, 0xAE, 0xAE,
  810. 0xB0, 0xB0, 0xB8, 0xB8, 0xB4, 0xB4, 0xBC, 0xBC,
  811. 0xB2, 0xB2, 0xBA, 0xBA, 0xB6, 0xB6, 0xBE, 0xBE,
  812. 0xC0, 0xC0, 0xC8, 0xC8, 0xC4, 0xC4, 0xCC, 0xCC,
  813. 0xC2, 0xC2, 0xCA, 0xCA, 0xC6, 0xC6, 0xCE, 0xCE,
  814. 0xD0, 0xD0, 0xD8, 0xD8, 0xD4, 0xD4, 0xDC, 0xDC,
  815. 0xD2, 0xD2, 0xDA, 0xDA, 0xD6, 0xD6, 0xDE, 0xDE,
  816. 0xE0, 0xE0, 0xE8, 0xE8, 0xE4, 0xE4, 0xEC, 0xEC,
  817. 0xE2, 0xE2, 0xEA, 0xEA, 0xE6, 0xE6, 0xEE, 0xEE,
  818. 0xF0, 0xF0, 0xF8, 0xF8, 0xF4, 0xF4, 0xFC, 0xFC,
  819. 0xF2, 0xF2, 0xFA, 0xFA, 0xF6, 0xF6, 0xFE, 0xFE
  820. );
  821. // Mapping tables for the PC-2 transformation.
  822. static $pc2mapc1 = array(
  823. 0x00000000, 0x00000400, 0x00200000, 0x00200400,
  824. 0x00000001, 0x00000401, 0x00200001, 0x00200401,
  825. 0x02000000, 0x02000400, 0x02200000, 0x02200400,
  826. 0x02000001, 0x02000401, 0x02200001, 0x02200401
  827. );
  828. static $pc2mapc2 = array(
  829. 0x00000000, 0x00000800, 0x08000000, 0x08000800,
  830. 0x00010000, 0x00010800, 0x08010000, 0x08010800,
  831. 0x00000000, 0x00000800, 0x08000000, 0x08000800,
  832. 0x00010000, 0x00010800, 0x08010000, 0x08010800,
  833. 0x00000100, 0x00000900, 0x08000100, 0x08000900,
  834. 0x00010100, 0x00010900, 0x08010100, 0x08010900,
  835. 0x00000100, 0x00000900, 0x08000100, 0x08000900,
  836. 0x00010100, 0x00010900, 0x08010100, 0x08010900,
  837. 0x00000010, 0x00000810, 0x08000010, 0x08000810,
  838. 0x00010010, 0x00010810, 0x08010010, 0x08010810,
  839. 0x00000010, 0x00000810, 0x08000010, 0x08000810,
  840. 0x00010010, 0x00010810, 0x08010010, 0x08010810,
  841. 0x00000110, 0x00000910, 0x08000110, 0x08000910,
  842. 0x00010110, 0x00010910, 0x08010110, 0x08010910,
  843. 0x00000110, 0x00000910, 0x08000110, 0x08000910,
  844. 0x00010110, 0x00010910, 0x08010110, 0x08010910,
  845. 0x00040000, 0x00040800, 0x08040000, 0x08040800,
  846. 0x00050000, 0x00050800, 0x08050000, 0x08050800,
  847. 0x00040000, 0x00040800, 0x08040000, 0x08040800,
  848. 0x00050000, 0x00050800, 0x08050000, 0x08050800,
  849. 0x00040100, 0x00040900, 0x08040100, 0x08040900,
  850. 0x00050100, 0x00050900, 0x08050100, 0x08050900,
  851. 0x00040100, 0x00040900, 0x08040100, 0x08040900,
  852. 0x00050100, 0x00050900, 0x08050100, 0x08050900,
  853. 0x00040010, 0x00040810, 0x08040010, 0x08040810,
  854. 0x00050010, 0x00050810, 0x08050010, 0x08050810,
  855. 0x00040010, 0x00040810, 0x08040010, 0x08040810,
  856. 0x00050010, 0x00050810, 0x08050010, 0x08050810,
  857. 0x00040110, 0x00040910, 0x08040110, 0x08040910,
  858. 0x00050110, 0x00050910, 0x08050110, 0x08050910,
  859. 0x00040110, 0x00040910, 0x08040110, 0x08040910,
  860. 0x00050110, 0x00050910, 0x08050110, 0x08050910,
  861. 0x01000000, 0x01000800, 0x09000000, 0x09000800,
  862. 0x01010000, 0x01010800, 0x09010000, 0x09010800,
  863. 0x01000000, 0x01000800, 0x09000000, 0x09000800,
  864. 0x01010000, 0x01010800, 0x09010000, 0x09010800,
  865. 0x01000100, 0x01000900, 0x09000100, 0x09000900,
  866. 0x01010100, 0x01010900, 0x09010100, 0x09010900,
  867. 0x01000100, 0x01000900, 0x09000100, 0x09000900,
  868. 0x01010100, 0x01010900, 0x09010100, 0x09010900,
  869. 0x01000010, 0x01000810, 0x09000010, 0x09000810,
  870. 0x01010010, 0x01010810, 0x09010010, 0x09010810,
  871. 0x01000010, 0x01000810, 0x09000010, 0x09000810,
  872. 0x01010010, 0x01010810, 0x09010010, 0x09010810,
  873. 0x01000110, 0x01000910, 0x09000110, 0x09000910,
  874. 0x01010110, 0x01010910, 0x09010110, 0x09010910,
  875. 0x01000110, 0x01000910, 0x09000110, 0x09000910,
  876. 0x01010110, 0x01010910, 0x09010110, 0x09010910,
  877. 0x01040000, 0x01040800, 0x09040000, 0x09040800,
  878. 0x01050000, 0x01050800, 0x09050000, 0x09050800,
  879. 0x01040000, 0x01040800, 0x09040000, 0x09040800,
  880. 0x01050000, 0x01050800, 0x09050000, 0x09050800,
  881. 0x01040100, 0x01040900, 0x09040100, 0x09040900,
  882. 0x01050100, 0x01050900, 0x09050100, 0x09050900,
  883. 0x01040100, 0x01040900, 0x09040100, 0x09040900,
  884. 0x01050100, 0x01050900, 0x09050100, 0x09050900,
  885. 0x01040010, 0x01040810, 0x09040010, 0x09040810,
  886. 0x01050010, 0x01050810, 0x09050010, 0x09050810,
  887. 0x01040010, 0x01040810, 0x09040010, 0x09040810,
  888. 0x01050010, 0x01050810, 0x09050010, 0x09050810,
  889. 0x01040110, 0x01040910, 0x09040110, 0x09040910,
  890. 0x01050110, 0x01050910, 0x09050110, 0x09050910,
  891. 0x01040110, 0x01040910, 0x09040110, 0x09040910,
  892. 0x01050110, 0x01050910, 0x09050110, 0x09050910
  893. );
  894. static $pc2mapc3 = array(
  895. 0x00000000, 0x00000004, 0x00001000, 0x00001004,
  896. 0x00000000, 0x00000004, 0x00001000, 0x00001004,
  897. 0x10000000, 0x10000004, 0x10001000, 0x10001004,
  898. 0x10000000, 0x10000004, 0x10001000, 0x10001004,
  899. 0x00000020, 0x00000024, 0x00001020, 0x00001024,
  900. 0x00000020, 0x00000024, 0x00001020, 0x00001024,
  901. 0x10000020, 0x10000024, 0x10001020, 0x10001024,
  902. 0x10000020, 0x10000024, 0x10001020, 0x10001024,
  903. 0x00080000, 0x00080004, 0x00081000, 0x00081004,
  904. 0x00080000, 0x00080004, 0x00081000, 0x00081004,
  905. 0x10080000, 0x10080004, 0x10081000, 0x10081004,
  906. 0x10080000, 0x10080004, 0x10081000, 0x10081004,
  907. 0x00080020, 0x00080024, 0x00081020, 0x00081024,
  908. 0x00080020, 0x00080024, 0x00081020, 0x00081024,
  909. 0x10080020, 0x10080024, 0x10081020, 0x10081024,
  910. 0x10080020, 0x10080024, 0x10081020, 0x10081024,
  911. 0x20000000, 0x20000004, 0x20001000, 0x20001004,
  912. 0x20000000, 0x20000004, 0x20001000, 0x20001004,
  913. 0x30000000, 0x30000004, 0x30001000, 0x30001004,
  914. 0x30000000, 0x30000004, 0x30001000, 0x30001004,
  915. 0x20000020, 0x20000024, 0x20001020, 0x20001024,
  916. 0x20000020, 0x20000024, 0x20001020, 0x20001024,
  917. 0x30000020, 0x30000024, 0x30001020, 0x30001024,
  918. 0x30000020, 0x30000024, 0x30001020, 0x30001024,
  919. 0x20080000, 0x20080004, 0x20081000, 0x20081004,
  920. 0x20080000, 0x20080004, 0x20081000, 0x20081004,
  921. 0x30080000, 0x30080004, 0x30081000, 0x30081004,
  922. 0x30080000, 0x30080004, 0x30081000, 0x30081004,
  923. 0x20080020, 0x20080024, 0x20081020, 0x20081024,
  924. 0x20080020, 0x20080024, 0x20081020, 0x20081024,
  925. 0x30080020, 0x30080024, 0x30081020, 0x30081024,
  926. 0x30080020, 0x30080024, 0x30081020, 0x30081024,
  927. 0x00000002, 0x00000006, 0x00001002, 0x00001006,
  928. 0x00000002, 0x00000006, 0x00001002, 0x00001006,
  929. 0x10000002, 0x10000006, 0x10001002, 0x10001006,
  930. 0x10000002, 0x10000006, 0x10001002, 0x10001006,
  931. 0x00000022, 0x00000026, 0x00001022, 0x00001026,
  932. 0x00000022, 0x00000026, 0x00001022, 0x00001026,
  933. 0x10000022, 0x10000026, 0x10001022, 0x10001026,
  934. 0x10000022, 0x10000026, 0x10001022, 0x10001026,
  935. 0x00080002, 0x00080006, 0x00081002, 0x00081006,
  936. 0x00080002, 0x00080006, 0x00081002, 0x00081006,
  937. 0x10080002, 0x10080006, 0x10081002, 0x10081006,
  938. 0x10080002, 0x10080006, 0x10081002, 0x10081006,
  939. 0x00080022, 0x00080026, 0x00081022, 0x00081026,
  940. 0x00080022, 0x00080026, 0x00081022, 0x00081026,
  941. 0x10080022, 0x10080026, 0x10081022, 0x10081026,
  942. 0x10080022, 0x10080026, 0x10081022, 0x10081026,
  943. 0x20000002, 0x20000006, 0x20001002, 0x20001006,
  944. 0x20000002, 0x20000006, 0x20001002, 0x20001006,
  945. 0x30000002, 0x30000006, 0x30001002, 0x30001006,
  946. 0x30000002, 0x30000006, 0x30001002, 0x30001006,
  947. 0x20000022, 0x20000026, 0x20001022, 0x20001026,
  948. 0x20000022, 0x20000026, 0x20001022, 0x20001026,
  949. 0x30000022, 0x30000026, 0x30001022, 0x30001026,
  950. 0x30000022, 0x30000026, 0x30001022, 0x30001026,
  951. 0x20080002, 0x20080006, 0x20081002, 0x20081006,
  952. 0x20080002, 0x20080006, 0x20081002, 0x20081006,
  953. 0x30080002, 0x30080006, 0x30081002, 0x30081006,
  954. 0x30080002, 0x30080006, 0x30081002, 0x30081006,
  955. 0x20080022, 0x20080026, 0x20081022, 0x20081026,
  956. 0x20080022, 0x20080026, 0x20081022, 0x20081026,
  957. 0x30080022, 0x30080026, 0x30081022, 0x30081026,
  958. 0x30080022, 0x30080026, 0x30081022, 0x30081026
  959. );
  960. static $pc2mapc4 = array(
  961. 0x00000000, 0x00100000, 0x00000008, 0x00100008,
  962. 0x00000200, 0x00100200, 0x00000208, 0x00100208,
  963. 0x00000000, 0x00100000, 0x00000008, 0x00100008,
  964. 0x00000200, 0x00100200, 0x00000208, 0x00100208,
  965. 0x04000000, 0x04100000, 0x04000008, 0x04100008,
  966. 0x04000200, 0x04100200, 0x04000208, 0x04100208,
  967. 0x04000000, 0x04100000, 0x04000008, 0x04100008,
  968. 0x04000200, 0x04100200, 0x04000208, 0x04100208,
  969. 0x00002000, 0x00102000, 0x00002008, 0x00102008,
  970. 0x00002200, 0x00102200, 0x00002208, 0x00102208,
  971. 0x00002000, 0x00102000, 0x00002008, 0x00102008,
  972. 0x00002200, 0x00102200, 0x00002208, 0x00102208,
  973. 0x04002000, 0x04102000, 0x04002008, 0x04102008,
  974. 0x04002200, 0x04102200, 0x04002208, 0x04102208,
  975. 0x04002000, 0x04102000, 0x04002008, 0x04102008,
  976. 0x04002200, 0x04102200, 0x04002208, 0x04102208,
  977. 0x00000000, 0x00100000, 0x00000008, 0x00100008,
  978. 0x00000200, 0x00100200, 0x00000208, 0x00100208,
  979. 0x00000000, 0x00100000, 0x00000008, 0x00100008,
  980. 0x00000200, 0x00100200, 0x00000208, 0x00100208,
  981. 0x04000000, 0x04100000, 0x04000008, 0x04100008,
  982. 0x04000200, 0x04100200, 0x04000208, 0x04100208,
  983. 0x04000000, 0x04100000, 0x04000008, 0x04100008,
  984. 0x04000200, 0x04100200, 0x04000208, 0x04100208,
  985. 0x00002000, 0x00102000, 0x00002008, 0x00102008,
  986. 0x00002200, 0x00102200, 0x00002208, 0x00102208,
  987. 0x00002000, 0x00102000, 0x00002008, 0x00102008,
  988. 0x00002200, 0x00102200, 0x00002208, 0x00102208,
  989. 0x04002000, 0x04102000, 0x04002008, 0x04102008,
  990. 0x04002200, 0x04102200, 0x04002208, 0x04102208,
  991. 0x04002000, 0x04102000, 0x04002008, 0x04102008,
  992. 0x04002200, 0x04102200, 0x04002208, 0x04102208,
  993. 0x00020000, 0x00120000, 0x00020008, 0x00120008,
  994. 0x00020200, 0x00120200, 0x00020208, 0x00120208,
  995. 0x00020000, 0x00120000, 0x00020008, 0x00120008,
  996. 0x00020200, 0x00120200, 0x00020208, 0x00120208,
  997. 0x04020000, 0x04120000, 0x04020008, 0x04120008,
  998. 0x04020200, 0x04120200, 0x04020208, 0x04120208,
  999. 0x04020000, 0x04120000, 0x04020008, 0x04120008,
  1000. 0x04020200, 0x04120200, 0x04020208, 0x04120208,
  1001. 0x00022000, 0x00122000, 0x00022008, 0x00122008,
  1002. 0x00022200, 0x00122200, 0x00022208, 0x00122208,
  1003. 0x00022000, 0x00122000, 0x00022008, 0x00122008,
  1004. 0x00022200, 0x00122200, 0x00022208, 0x00122208,
  1005. 0x04022000, 0x04122000, 0x04022008, 0x04122008,
  1006. 0x04022200, 0x04122200, 0x04022208, 0x04122208,
  1007. 0x04022000, 0x04122000, 0x04022008, 0x04122008,
  1008. 0x04022200, 0x04122200, 0x04022208, 0x04122208,
  1009. 0x00020000, 0x00120000, 0x00020008, 0x00120008,
  1010. 0x00020200, 0x00120200, 0x00020208, 0x00120208,
  1011. 0x00020000, 0x00120000, 0x00020008, 0x00120008,
  1012. 0x00020200, 0x00120200, 0x00020208, 0x00120208,
  1013. 0x04020000, 0x04120000, 0x04020008, 0x04120008,
  1014. 0x04020200, 0x04120200, 0x04020208, 0x04120208,
  1015. 0x04020000, 0x04120000, 0x04020008, 0x04120008,
  1016. 0x04020200, 0x04120200, 0x04020208, 0x04120208,
  1017. 0x00022000, 0x00122000, 0x00022008, 0x00122008,
  1018. 0x00022200, 0x00122200, 0x00022208, 0x00122208,
  1019. 0x00022000, 0x00122000, 0x00022008, 0x00122008,
  1020. 0x00022200, 0x00122200, 0x00022208, 0x00122208,
  1021. 0x04022000, 0x04122000, 0x04022008, 0x04122008,
  1022. 0x04022200, 0x04122200, 0x04022208, 0x04122208,
  1023. 0x04022000, 0x04122000, 0x04022008, 0x04122008,
  1024. 0x04022200, 0x04122200, 0x04022208, 0x04122208
  1025. );
  1026. static $pc2mapd1 = array(
  1027. 0x00000000, 0x00000001, 0x08000000, 0x08000001,
  1028. 0x00200000, 0x00200001, 0x08200000, 0x08200001,
  1029. 0x00000002, 0x00000003, 0x08000002, 0x08000003,
  1030. 0x00200002, 0x00200003, 0x08200002, 0x08200003
  1031. );
  1032. static $pc2mapd2 = array(
  1033. 0x00000000, 0x00100000, 0x00000800, 0x00100800,
  1034. 0x00000000, 0x00100000, 0x00000800, 0x00100800,
  1035. 0x04000000, 0x04100000, 0x04000800, 0x04100800,
  1036. 0x04000000, 0x04100000, 0x04000800, 0x04100800,
  1037. 0x00000004, 0x00100004, 0x00000804, 0x00100804,
  1038. 0x00000004, 0x00100004, 0x00000804, 0x00100804,
  1039. 0x04000004, 0x04100004, 0x04000804, 0x04100804,
  1040. 0x04000004, 0x04100004, 0x04000804, 0x04100804,
  1041. 0x00000000, 0x00100000, 0x00000800, 0x00100800,
  1042. 0x00000000, 0x00100000, 0x00000800, 0x00100800,
  1043. 0x04000000, 0x04100000, 0x04000800, 0x04100800,
  1044. 0x04000000, 0x04100000, 0x04000800, 0x04100800,
  1045. 0x00000004, 0x00100004, 0x00000804, 0x00100804,
  1046. 0x00000004, 0x00100004, 0x00000804, 0x00100804,
  1047. 0x04000004, 0x04100004, 0x04000804, 0x04100804,
  1048. 0x04000004, 0x04100004, 0x04000804, 0x04100804,
  1049. 0x00000200, 0x00100200, 0x00000A00, 0x00100A00,
  1050. 0x00000200, 0x00100200, 0x00000A00, 0x00100A00,
  1051. 0x04000200, 0x04100200, 0x04000A00, 0x04100A00,
  1052. 0x04000200, 0x04100200, 0x04000A00, 0x04100A00,
  1053. 0x00000204, 0x00100204, 0x00000A04, 0x00100A04,
  1054. 0x00000204, 0x00100204, 0x00000A04, 0x00100A04,
  1055. 0x04000204, 0x04100204, 0x04000A04, 0x04100A04,
  1056. 0x04000204, 0x04100204, 0x04000A04, 0x04100A04,
  1057. 0x00000200, 0x00100200, 0x00000A00, 0x00100A00,
  1058. 0x00000200, 0x00100200, 0x00000A00, 0x00100A00,
  1059. 0x04000200, 0x04100200, 0x04000A00, 0x04100A00,
  1060. 0x04000200, 0x04100200, 0x04000A00, 0x04100A00,
  1061. 0x00000204, 0x00100204, 0x00000A04, 0x00100A04,
  1062. 0x00000204, 0x00100204, 0x00000A04, 0x00100A04,
  1063. 0x04000204, 0x04100204, 0x04000A04, 0x04100A04,
  1064. 0x04000204, 0x04100204, 0x04000A04, 0x04100A04,
  1065. 0x00020000, 0x00120000, 0x00020800, 0x00120800,
  1066. 0x00020000, 0x00120000, 0x00020800, 0x00120800,
  1067. 0x04020000, 0x04120000, 0x04020800, 0x04120800,
  1068. 0x04020000, 0x04120000, 0x04020800, 0x04120800,
  1069. 0x00020004, 0x00120004, 0x00020804, 0x00120804,
  1070. 0x00020004, 0x00120004, 0x00020804, 0x00120804,
  1071. 0x04020004, 0x04120004, 0x04020804, 0x04120804,
  1072. 0x04020004, 0x04120004, 0x04020804, 0x04120804,
  1073. 0x00020000, 0x00120000, 0x00020800, 0x00120800,
  1074. 0x00020000, 0x00120000, 0x00020800, 0x00120800,
  1075. 0x04020000, 0x04120000, 0x04020800, 0x04120800,
  1076. 0x04020000, 0x04120000, 0x04020800, 0x04120800,
  1077. 0x00020004, 0x00120004, 0x00020804, 0x00120804,
  1078. 0x00020004, 0x00120004, 0x00020804, 0x00120804,
  1079. 0x04020004, 0x04120004, 0x04020804, 0x04120804,
  1080. 0x04020004, 0x04120004, 0x04020804, 0x04120804,
  1081. 0x00020200, 0x00120200, 0x00020A00, 0x00120A00,
  1082. 0x00020200, 0x00120200, 0x00020A00, 0x00120A00,
  1083. 0x04020200, 0x04120200, 0x04020A00, 0x04120A00,
  1084. 0x04020200, 0x04120200, 0x04020A00, 0x04120A00,
  1085. 0x00020204, 0x00120204, 0x00020A04, 0x00120A04,
  1086. 0x00020204, 0x00120204, 0x00020A04, 0x00120A04,
  1087. 0x04020204, 0x04120204, 0x04020A04, 0x04120A04,
  1088. 0x04020204, 0x04120204, 0x04020A04, 0x04120A04,
  1089. 0x00020200, 0x00120200, 0x00020A00, 0x00120A00,
  1090. 0x00020200, 0x00120200, 0x00020A00, 0x00120A00,
  1091. 0x04020200, 0x04120200, 0x04020A00, 0x04120A00,
  1092. 0x04020200, 0x04120200, 0x04020A00, 0x04120A00,
  1093. 0x00020204, 0x00120204, 0x00020A04, 0x00120A04,
  1094. 0x00020204, 0x00120204, 0x00020A04, 0x00120A04,
  1095. 0x04020204, 0x04120204, 0x04020A04, 0x04120A04,
  1096. 0x04020204, 0x04120204, 0x04020A04, 0x04120A04
  1097. );
  1098. static $pc2mapd3 = array(
  1099. 0x00000000, 0x00010000, 0x02000000, 0x02010000,
  1100. 0x00000020, 0x00010020, 0x02000020, 0x02010020,
  1101. 0x00040000, 0x00050000, 0x02040000, 0x02050000,
  1102. 0x00040020, 0x00050020, 0x02040020, 0x02050020,
  1103. 0x00002000, 0x00012000, 0x02002000, 0x02012000,
  1104. 0x00002020, 0x00012020, 0x02002020, 0x02012020,
  1105. 0x00042000, 0x00052000, 0x02042000, 0x02052000,
  1106. 0x00042020, 0x00052020, 0x02042020, 0x02052020,
  1107. 0x00000000, 0x00010000, 0x02000000, 0x02010000,
  1108. 0x00000020, 0x00010020, 0x02000020, 0x02010020,
  1109. 0x00040000, 0x00050000, 0x02040000, 0x02050000,
  1110. 0x00040020, 0x00050020, 0x02040020, 0x02050020,
  1111. 0x00002000, 0x00012000, 0x02002000, 0x02012000,
  1112. 0x00002020, 0x00012020, 0x02002020, 0x02012020,
  1113. 0x00042000, 0x00052000, 0x02042000, 0x02052000,
  1114. 0x00042020, 0x00052020, 0x02042020, 0x02052020,
  1115. 0x00000010, 0x00010010, 0x02000010, 0x02010010,
  1116. 0x00000030, 0x00010030, 0x02000030, 0x02010030,
  1117. 0x00040010, 0x00050010, 0x02040010, 0x02050010,
  1118. 0x00040030, 0x00050030, 0x02040030, 0x02050030,
  1119. 0x00002010, 0x00012010, 0x02002010, 0x02012010,
  1120. 0x00002030, 0x00012030, 0x02002030, 0x02012030,
  1121. 0x00042010, 0x00052010, 0x02042010, 0x02052010,
  1122. 0x00042030, 0x00052030, 0x02042030, 0x02052030,
  1123. 0x00000010, 0x00010010, 0x02000010, 0x02010010,
  1124. 0x00000030, 0x00010030, 0x02000030, 0x02010030,
  1125. 0x00040010, 0x00050010, 0x02040010, 0x02050010,
  1126. 0x00040030, 0x00050030, 0x02040030, 0x02050030,
  1127. 0x00002010, 0x00012010, 0x02002010, 0x02012010,
  1128. 0x00002030, 0x00012030, 0x02002030, 0x02012030,
  1129. 0x00042010, 0x00052010, 0x02042010, 0x02052010,
  1130. 0x00042030, 0x00052030, 0x02042030, 0x02052030,
  1131. 0x20000000, 0x20010000, 0x22000000, 0x22010000,
  1132. 0x20000020, 0x20010020, 0x22000020, 0x22010020,
  1133. 0x20040000, 0x20050000, 0x22040000, 0x22050000,
  1134. 0x20040020, 0x20050020, 0x22040020, 0x22050020,
  1135. 0x20002000, 0x20012000, 0x22002000, 0x22012000,
  1136. 0x20002020, 0x20012020, 0x22002020, 0x22012020,
  1137. 0x20042000, 0x20052000, 0x22042000, 0x22052000,
  1138. 0x20042020, 0x20052020, 0x22042020, 0x22052020,
  1139. 0x20000000, 0x20010000, 0x22000000, 0x22010000,
  1140. 0x20000020, 0x20010020, 0x22000020, 0x22010020,
  1141. 0x20040000, 0x20050000, 0x22040000, 0x22050000,
  1142. 0x20040020, 0x20050020, 0x22040020, 0x22050020,
  1143. 0x20002000, 0x20012000, 0x22002000, 0x22012000,
  1144. 0x20002020, 0x20012020, 0x22002020, 0x22012020,
  1145. 0x20042000, 0x20052000, 0x22042000, 0x22052000,
  1146. 0x20042020, 0x20052020, 0x22042020, 0x22052020,
  1147. 0x20000010, 0x20010010, 0x22000010, 0x22010010,
  1148. 0x20000030, 0x20010030, 0x22000030, 0x22010030,
  1149. 0x20040010, 0x20050010, 0x22040010, 0x22050010,
  1150. 0x20040030, 0x20050030, 0x22040030, 0x22050030,
  1151. 0x20002010, 0x20012010, 0x22002010, 0x22012010,
  1152. 0x20002030, 0x20012030, 0x22002030, 0x22012030,
  1153. 0x20042010, 0x20052010, 0x22042010, 0x22052010,
  1154. 0x20042030, 0x20052030, 0x22042030, 0x22052030,
  1155. 0x20000010, 0x20010010, 0x22000010, 0x22010010,
  1156. 0x20000030, 0x20010030, 0x22000030, 0x22010030,
  1157. 0x20040010, 0x20050010, 0x22040010, 0x22050010,
  1158. 0x20040030, 0x20050030, 0x22040030, 0x22050030,
  1159. 0x20002010, 0x20012010, 0x22002010, 0x22012010,
  1160. 0x20002030, 0x20012030, 0x22002030, 0x22012030,
  1161. 0x20042010, 0x20052010, 0x22042010, 0x22052010,
  1162. 0x20042030, 0x20052030, 0x22042030, 0x22052030
  1163. );
  1164. static $pc2mapd4 = array(
  1165. 0x00000000, 0x00000400, 0x01000000, 0x01000400,
  1166. 0x00000000, 0x00000400, 0x01000000, 0x01000400,
  1167. 0x00000100, 0x00000500, 0x01000100, 0x01000500,
  1168. 0x00000100, 0x00000500, 0x01000100, 0x01000500,
  1169. 0x10000000, 0x10000400, 0x11000000, 0x11000400,
  1170. 0x10000000, 0x10000400, 0x11000000, 0x11000400,
  1171. 0x10000100, 0x10000500, 0x11000100, 0x11000500,
  1172. 0x10000100, 0x10000500, 0x11000100, 0x11000500,
  1173. 0x00080000, 0x00080400, 0x01080000, 0x01080400,
  1174. 0x00080000, 0x00080400, 0x01080000, 0x01080400,
  1175. 0x00080100, 0x00080500, 0x01080100, 0x01080500,
  1176. 0x00080100, 0x00080500, 0x01080100, 0x01080500,
  1177. 0x10080000, 0x10080400, 0x11080000, 0x11080400,
  1178. 0x10080000, 0x10080400, 0x11080000, 0x11080400,
  1179. 0x10080100, 0x10080500, 0x11080100, 0x11080500,
  1180. 0x10080100, 0x10080500, 0x11080100, 0x11080500,
  1181. 0x00000008, 0x00000408, 0x01000008, 0x01000408,
  1182. 0x00000008, 0x00000408, 0x01000008, 0x01000408,
  1183. 0x00000108, 0x00000508, 0x01000108, 0x01000508,
  1184. 0x00000108, 0x00000508, 0x01000108, 0x01000508,
  1185. 0x10000008, 0x10000408, 0x11000008, 0x11000408,
  1186. 0x10000008, 0x10000408, 0x11000008, 0x11000408,
  1187. 0x10000108, 0x10000508, 0x11000108, 0x11000508,
  1188. 0x10000108, 0x10000508, 0x11000108, 0x11000508,
  1189. 0x00080008, 0x00080408, 0x01080008, 0x01080408,
  1190. 0x00080008, 0x00080408, 0x01080008, 0x01080408,
  1191. 0x00080108, 0x00080508, 0x01080108, 0x01080508,
  1192. 0x00080108, 0x00080508, 0x01080108, 0x01080508,
  1193. 0x10080008, 0x10080408, 0x11080008, 0x11080408,
  1194. 0x10080008, 0x10080408, 0x11080008, 0x11080408,
  1195. 0x10080108, 0x10080508, 0x11080108, 0x11080508,
  1196. 0x10080108, 0x10080508, 0x11080108, 0x11080508,
  1197. 0x00001000, 0x00001400, 0x01001000, 0x01001400,
  1198. 0x00001000, 0x00001400, 0x01001000, 0x01001400,
  1199. 0x00001100, 0x00001500, 0x01001100, 0x01001500,
  1200. 0x00001100, 0x00001500, 0x01001100, 0x01001500,
  1201. 0x10001000, 0x10001400, 0x11001000, 0x11001400,
  1202. 0x10001000, 0x10001400, 0x11001000, 0x11001400,
  1203. 0x10001100, 0x10001500, 0x11001100, 0x11001500,
  1204. 0x10001100, 0x10001500, 0x11001100, 0x11001500,
  1205. 0x00081000, 0x00081400, 0x01081000, 0x01081400,
  1206. 0x00081000, 0x00081400, 0x01081000, 0x01081400,
  1207. 0x00081100, 0x00081500, 0x01081100, 0x01081500,
  1208. 0x00081100, 0x00081500, 0x01081100, 0x01081500,
  1209. 0x10081000, 0x10081400, 0x11081000, 0x11081400,
  1210. 0x10081000, 0x10081400, 0x11081000, 0x11081400,
  1211. 0x10081100, 0x10081500, 0x11081100, 0x11081500,
  1212. 0x10081100, 0x10081500, 0x11081100, 0x11081500,
  1213. 0x00001008, 0x00001408, 0x01001008, 0x01001408,
  1214. 0x00001008, 0x00001408, 0x01001008, 0x01001408,
  1215. 0x00001108, 0x00001508, 0x01001108, 0x01001508,
  1216. 0x00001108, 0x00001508, 0x01001108, 0x01001508,
  1217. 0x10001008, 0x10001408, 0x11001008, 0x11001408,
  1218. 0x10001008, 0x10001408, 0x11001008, 0x11001408,
  1219. 0x10001108, 0x10001508, 0x11001108, 0x11001508,
  1220. 0x10001108, 0x10001508, 0x11001108, 0x11001508,
  1221. 0x00081008, 0x00081408, 0x01081008, 0x01081408,
  1222. 0x00081008, 0x00081408, 0x01081008, 0x01081408,
  1223. 0x00081108, 0x00081508, 0x01081108, 0x01081508,
  1224. 0x00081108, 0x00081508, 0x01081108, 0x01081508,
  1225. 0x10081008, 0x10081408, 0x11081008, 0x11081408,
  1226. 0x10081008, 0x10081408, 0x11081008, 0x11081408,
  1227. 0x10081108, 0x10081508, 0x11081108, 0x11081508,
  1228. 0x10081108, 0x10081508, 0x11081108, 0x11081508
  1229. );
  1230. $keys = array();
  1231. for ($des_round = 0; $des_round < $this->des_rounds; ++$des_round) {
  1232. // pad the key and remove extra characters as appropriate.
  1233. $key = str_pad(substr($this->key, $des_round * 8, 8), 8, "\0");
  1234. // Perform the PC/1 transformation and compute C and D.
  1235. $t = unpack('Nl/Nr', $key);
  1236. list($l, $r) = array($t['l'], $t['r']);
  1237. $key = ($this->shuffle[$pc1map[ $r & 0xFF]] & "\x80\x80\x80\x80\x80\x80\x80\x00") |
  1238. ($this->shuffle[$pc1map[($r >> 8) & 0xFF]] & "\x40\x40\x40\x40\x40\x40\x40\x00") |
  1239. ($this->shuffle[$pc1map[($r >> 16) & 0xFF]] & "\x20\x20\x20\x20\x20\x20\x20\x00") |
  1240. ($this->shuffle[$pc1map[($r >> 24) & 0xFF]] & "\x10\x10\x10\x10\x10\x10\x10\x00") |
  1241. ($this->shuffle[$pc1map[ $l & 0xFF]] & "\x08\x08\x08\x08\x08\x08\x08\x00") |
  1242. ($this->shuffle[$pc1map[($l >> 8) & 0xFF]] & "\x04\x04\x04\x04\x04\x04\x04\x00") |
  1243. ($this->shuffle[$pc1map[($l >> 16) & 0xFF]] & "\x02\x02\x02\x02\x02\x02\x02\x00") |
  1244. ($this->shuffle[$pc1map[($l >> 24) & 0xFF]] & "\x01\x01\x01\x01\x01\x01\x01\x00");
  1245. $key = unpack('Nc/Nd', $key);
  1246. $c = ( $key['c'] >> 4) & 0x0FFFFFFF;
  1247. $d = (($key['d'] >> 4) & 0x0FFFFFF0) | ($key['c'] & 0x0F);
  1248. $keys[$des_round] = array(
  1249. CRYPT_DES_ENCRYPT => array(),
  1250. CRYPT_DES_DECRYPT => array_fill(0, 32, 0)
  1251. );
  1252. for ($i = 0, $ki = 31; $i < 16; ++$i, $ki-= 2) {
  1253. $c <<= $shifts[$i];
  1254. $c = ($c | ($c >> 28)) & 0x0FFFFFFF;
  1255. $d <<= $shifts[$i];
  1256. $d = ($d | ($d >> 28)) & 0x0FFFFFFF;
  1257. // Perform the PC-2 transformation.
  1258. $cp = $pc2mapc1[ $c >> 24 ] | $pc2mapc2[($c >> 16) & 0xFF] |
  1259. $pc2mapc3[($c >> 8) & 0xFF] | $pc2mapc4[ $c & 0xFF];
  1260. $dp = $pc2mapd1[ $d >> 24 ] | $pc2mapd2[($d >> 16) & 0xFF] |
  1261. $pc2mapd3[($d >> 8) & 0xFF] | $pc2mapd4[ $d & 0xFF];
  1262. // Reorder: odd bytes/even bytes. Push the result in key schedule.
  1263. $val1 = ( $cp & 0xFF000000) | (($cp << 8) & 0x00FF0000) |
  1264. (($dp >> 16) & 0x0000FF00) | (($dp >> 8) & 0x000000FF);
  1265. $val2 = (($cp << 8) & 0xFF000000) | (($cp << 16) & 0x00FF0000) |
  1266. (($dp >> 8) & 0x0000FF00) | ( $dp & 0x000000FF);
  1267. $keys[$des_round][CRYPT_DES_ENCRYPT][ ] = $val1;
  1268. $keys[$des_round][CRYPT_DES_DECRYPT][$ki - 1] = $val1;
  1269. $keys[$des_round][CRYPT_DES_ENCRYPT][ ] = $val2;
  1270. $keys[$des_round][CRYPT_DES_DECRYPT][$ki ] = $val2;
  1271. }
  1272. }
  1273. switch ($this->des_rounds) {
  1274. case 3: // 3DES keys
  1275. $this->keys = array(
  1276. CRYPT_DES_ENCRYPT => array_merge(
  1277. $keys[0][CRYPT_DES_ENCRYPT],
  1278. $keys[1][CRYPT_DES_DECRYPT],
  1279. $keys[2][CRYPT_DES_ENCRYPT]
  1280. ),
  1281. CRYPT_DES_DECRYPT => array_merge(
  1282. $keys[2][CRYPT_DES_DECRYPT],
  1283. $keys[1][CRYPT_DES_ENCRYPT],
  1284. $keys[0][CRYPT_DES_DECRYPT]
  1285. )
  1286. );
  1287. break;
  1288. // case 1: // DES keys
  1289. default:
  1290. $this->keys = array(
  1291. CRYPT_DES_ENCRYPT => $keys[0][CRYPT_DES_ENCRYPT],
  1292. CRYPT_DES_DECRYPT => $keys[0][CRYPT_DES_DECRYPT]
  1293. );
  1294. }
  1295. }
  1296. /**
  1297. * Setup the performance-optimized function for de/encrypt()
  1298. *
  1299. * @see Crypt_Base::_setupInlineCrypt()
  1300. * @access private
  1301. */
  1302. function _setupInlineCrypt()
  1303. {
  1304. $lambda_functions =& Crypt_DES::_getLambdaFunctions();
  1305. // Engine configuration for:
  1306. // - DES ($des_rounds == 1) or
  1307. // - 3DES ($des_rounds == 3)
  1308. $des_rounds = $this->des_rounds;
  1309. // We create max. 10 hi-optimized code for memory reason. Means: For each $key one ultra fast inline-crypt function.
  1310. // After that, we'll still create very fast optimized code but not the hi-ultimative code, for each $mode one
  1311. $gen_hi_opt_code = (bool)( count($lambda_functions) < 10 );
  1312. // Generation of a uniqe hash for our generated code
  1313. switch (true) {
  1314. case $gen_hi_opt_code:
  1315. // For hi-optimized code, we create for each combination of
  1316. // $mode, $des_rounds and $this->key its own encrypt/decrypt function.
  1317. $code_hash = md5(str_pad("Crypt_DES, $des_rounds, {$this->mode}, ", 32, "\0") . $this->key);
  1318. break;
  1319. default:
  1320. // After max 10 hi-optimized functions, we create generic
  1321. // (still very fast.. but not ultra) functions for each $mode/$des_rounds
  1322. // Currently 2 * 5 generic functions will be then max. possible.
  1323. $code_hash = "Crypt_DES, $des_rounds, {$this->mode}";
  1324. }
  1325. // Is there a re-usable $lambda_functions in there? If not, we have to create it.
  1326. if (!isset($lambda_functions[$code_hash])) {
  1327. // Init code for both, encrypt and decrypt.
  1328. $init_crypt = 'static $sbox1, $sbox2, $sbox3, $sbox4, $sbox5, $sbox6, $sbox7, $sbox8, $shuffleip, $shuffleinvip;
  1329. if (!$sbox1) {
  1330. $sbox1 = array_map("intval", $self->sbox1);
  1331. $sbox2 = array_map("intval", $self->sbox2);
  1332. $sbox3 = array_map("intval", $self->sbox3);
  1333. $sbox4 = array_map("intval", $self->sbox4);
  1334. $sbox5 = array_map("intval", $self->sbox5);
  1335. $sbox6 = array_map("intval", $self->sbox6);
  1336. $sbox7 = array_map("intval", $self->sbox7);
  1337. $sbox8 = array_map("intval", $self->sbox8);'
  1338. /* Merge $shuffle with $[inv]ipmap */ . '
  1339. for ($i = 0; $i < 256; ++$i) {
  1340. $shuffleip[] = $self->shuffle[$self->ipmap[$i]];
  1341. $shuffleinvip[] = $self->shuffle[$self->invipmap[$i]];
  1342. }
  1343. }
  1344. ';
  1345. switch (true) {
  1346. case $gen_hi_opt_code:
  1347. // In Hi-optimized code mode, we use our [3]DES key schedule as hardcoded integers.
  1348. // No futher initialisation of the $keys schedule is necessary.
  1349. // That is the extra performance boost.
  1350. $k = array(
  1351. CRYPT_DES_ENCRYPT => $this->keys[CRYPT_DES_ENCRYPT],
  1352. CRYPT_DES_DECRYPT => $this->keys[CRYPT_DES_DECRYPT]
  1353. );
  1354. $init_encrypt = '';
  1355. $init_decrypt = '';
  1356. break;
  1357. default:
  1358. // In generic optimized code mode, we have to use, as the best compromise [currently],
  1359. // our key schedule as $ke/$kd arrays. (with hardcoded indexes...)
  1360. $k = array(
  1361. CRYPT_DES_ENCRYPT => array(),
  1362. CRYPT_DES_DECRYPT => array()
  1363. );
  1364. for ($i = 0, $c = count($this->keys[CRYPT_DES_ENCRYPT]); $i < $c; ++$i) {
  1365. $k[CRYPT_DES_ENCRYPT][$i] = '$ke[' . $i . ']';
  1366. $k[CRYPT_DES_DECRYPT][$i] = '$kd[' . $i . ']';
  1367. }
  1368. $init_encrypt = '$ke = $self->keys[CRYPT_DES_ENCRYPT];';
  1369. $init_decrypt = '$kd = $self->keys[CRYPT_DES_DECRYPT];';
  1370. break;
  1371. }
  1372. // Creating code for en- and decryption.
  1373. $crypt_block = array();
  1374. foreach (array(CRYPT_DES_ENCRYPT, CRYPT_DES_DECRYPT) as $c) {
  1375. /* Do the initial IP permutation. */
  1376. $crypt_block[$c] = '
  1377. $in = unpack("N*", $in);
  1378. $l = $in[1];
  1379. $r = $in[2];
  1380. $in = unpack("N*",
  1381. ($shuffleip[ $r & 0xFF] & "\x80\x80\x80\x80\x80\x80\x80\x80") |
  1382. ($shuffleip[($r >> 8) & 0xFF] & "\x40\x40\x40\x40\x40\x40\x40\x40") |
  1383. ($shuffleip[($r >> 16) & 0xFF] & "\x20\x20\x20\x20\x20\x20\x20\x20") |
  1384. ($shuffleip[($r >> 24) & 0xFF] & "\x10\x10\x10\x10\x10\x10\x10\x10") |
  1385. ($shuffleip[ $l & 0xFF] & "\x08\x08\x08\x08\x08\x08\x08\x08") |
  1386. ($shuffleip[($l >> 8) & 0xFF] & "\x04\x04\x04\x04\x04\x04\x04\x04") |
  1387. ($shuffleip[($l >> 16) & 0xFF] & "\x02\x02\x02\x02\x02\x02\x02\x02") |
  1388. ($shuffleip[($l >> 24) & 0xFF] & "\x01\x01\x01\x01\x01\x01\x01\x01")
  1389. );
  1390. ' . /* Extract L0 and R0 */ '
  1391. $l = $in[1];
  1392. $r = $in[2];
  1393. ';
  1394. $l = '$l';
  1395. $r = '$r';
  1396. // Perform DES or 3DES.
  1397. for ($ki = -1, $des_round = 0; $des_round < $des_rounds; ++$des_round) {
  1398. // Perform the 16 steps.
  1399. for ($i = 0; $i < 16; ++$i) {
  1400. // start of "the Feistel (F) function" - see the following URL:
  1401. // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png
  1402. // Merge key schedule.
  1403. $crypt_block[$c].= '
  1404. $b1 = ((' . $r . ' >> 3) & 0x1FFFFFFF) ^ (' . $r . ' << 29) ^ ' . $k[$c][++$ki] . ';
  1405. $b2 = ((' . $r . ' >> 31) & 0x00000001) ^ (' . $r . ' << 1) ^ ' . $k[$c][++$ki] . ';' .
  1406. /* S-box indexing. */
  1407. $l . ' = $sbox1[($b1 >> 24) & 0x3F] ^ $sbox2[($b2 >> 24) & 0x3F] ^
  1408. $sbox3[($b1 >> 16) & 0x3F] ^ $sbox4[($b2 >> 16) & 0x3F] ^
  1409. $sbox5[($b1 >> 8) & 0x3F] ^ $sbox6[($b2 >> 8) & 0x3F] ^
  1410. $sbox7[ $b1 & 0x3F] ^ $sbox8[ $b2 & 0x3F] ^ ' . $l . ';
  1411. ';
  1412. // end of "the Feistel (F) function"
  1413. // swap L & R
  1414. list($l, $r) = array($r, $l);
  1415. }
  1416. list($l, $r) = array($r, $l);
  1417. }
  1418. // Perform the inverse IP permutation.
  1419. $crypt_block[$c].= '$in =
  1420. ($shuffleinvip[($l >> 24) & 0xFF] & "\x80\x80\x80\x80\x80\x80\x80\x80") |
  1421. ($shuffleinvip[($r >> 24) & 0xFF] & "\x40\x40\x40\x40\x40\x40\x40\x40") |
  1422. ($shuffleinvip[($l >> 16) & 0xFF] & "\x20\x20\x20\x20\x20\x20\x20\x20") |
  1423. ($shuffleinvip[($r >> 16) & 0xFF] & "\x10\x10\x10\x10\x10\x10\x10\x10") |
  1424. ($shuffleinvip[($l >> 8) & 0xFF] & "\x08\x08\x08\x08\x08\x08\x08\x08") |
  1425. ($shuffleinvip[($r >> 8) & 0xFF] & "\x04\x04\x04\x04\x04\x04\x04\x04") |
  1426. ($shuffleinvip[ $l & 0xFF] & "\x02\x02\x02\x02\x02\x02\x02\x02") |
  1427. ($shuffleinvip[ $r & 0xFF] & "\x01\x01\x01\x01\x01\x01\x01\x01");
  1428. ';
  1429. }
  1430. // Creates the inline-crypt function
  1431. $lambda_functions[$code_hash] = $this->_createInlineCryptFunction(
  1432. array(
  1433. 'init_crypt' => $init_crypt,
  1434. 'init_encrypt' => $init_encrypt,
  1435. 'init_decrypt' => $init_decrypt,
  1436. 'encrypt_block' => $crypt_block[CRYPT_DES_ENCRYPT],
  1437. 'decrypt_block' => $crypt_block[CRYPT_DES_DECRYPT]
  1438. )
  1439. );
  1440. }
  1441. // Set the inline-crypt function as callback in: $this->inline_crypt
  1442. $this->inline_crypt = $lambda_functions[$code_hash];
  1443. }
  1444. }