DES.php 72 KB

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