DES.php 70 KB

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