RC2.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. /**
  3. * Pure-PHP implementation of RC2.
  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://tools.ietf.org/html/rfc2268}
  12. *
  13. * Here's a short example of how to use this library:
  14. * <code>
  15. * <?php
  16. * include 'vendor/autoload.php';
  17. *
  18. * $rc2 = new \phpseclib\Crypt\RC2();
  19. *
  20. * $rc2->setKey('abcdefgh');
  21. *
  22. * $plaintext = str_repeat('a', 1024);
  23. *
  24. * echo $rc2->decrypt($rc2->encrypt($plaintext));
  25. * ?>
  26. * </code>
  27. *
  28. * @category Crypt
  29. * @package RC2
  30. * @author Patrick Monnerat <pm@datasphere.ch>
  31. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  32. * @link http://phpseclib.sourceforge.net
  33. */
  34. namespace phpseclib\Crypt;
  35. /**
  36. * Pure-PHP implementation of RC2.
  37. *
  38. * @package RC2
  39. * @access public
  40. */
  41. class RC2 extends Base
  42. {
  43. /**
  44. * Block Length of the cipher
  45. *
  46. * @see \phpseclib\Crypt\Base::block_size
  47. * @var int
  48. * @access private
  49. */
  50. var $block_size = 8;
  51. /**
  52. * The Key
  53. *
  54. * @see \phpseclib\Crypt\Base::key
  55. * @see self::setKey()
  56. * @var string
  57. * @access private
  58. */
  59. var $key;
  60. /**
  61. * The Original (unpadded) Key
  62. *
  63. * @see \phpseclib\Crypt\Base::key
  64. * @see self::setKey()
  65. * @see self::encrypt()
  66. * @see self::decrypt()
  67. * @var string
  68. * @access private
  69. */
  70. var $orig_key;
  71. /**
  72. * Don't truncate / null pad key
  73. *
  74. * @see \phpseclib\Crypt\Base::_clearBuffers()
  75. * @var bool
  76. * @access private
  77. */
  78. var $skip_key_adjustment = true;
  79. /**
  80. * Key Length (in bytes)
  81. *
  82. * @see \phpseclib\Crypt\RC2::setKeyLength()
  83. * @var int
  84. * @access private
  85. */
  86. var $key_length = 16; // = 128 bits
  87. /**
  88. * The mcrypt specific name of the cipher
  89. *
  90. * @see \phpseclib\Crypt\Base::cipher_name_mcrypt
  91. * @var string
  92. * @access private
  93. */
  94. var $cipher_name_mcrypt = 'rc2';
  95. /**
  96. * Optimizing value while CFB-encrypting
  97. *
  98. * @see \phpseclib\Crypt\Base::cfb_init_len
  99. * @var int
  100. * @access private
  101. */
  102. var $cfb_init_len = 500;
  103. /**
  104. * The key length in bits.
  105. *
  106. * @see self::setKeyLength()
  107. * @see self::setKey()
  108. * @var int
  109. * @access private
  110. * @internal Should be in range [1..1024].
  111. * @internal Changing this value after setting the key has no effect.
  112. */
  113. var $default_key_length = 1024;
  114. /**
  115. * The key length in bits.
  116. *
  117. * @see self::isValidEnine()
  118. * @see self::setKey()
  119. * @var int
  120. * @access private
  121. * @internal Should be in range [1..1024].
  122. */
  123. var $current_key_length;
  124. /**
  125. * The Key Schedule
  126. *
  127. * @see self::_setupKey()
  128. * @var array
  129. * @access private
  130. */
  131. var $keys;
  132. /**
  133. * Key expansion randomization table.
  134. * Twice the same 256-value sequence to save a modulus in key expansion.
  135. *
  136. * @see self::setKey()
  137. * @var array
  138. * @access private
  139. */
  140. var $pitable = array(
  141. 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED,
  142. 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D,
  143. 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E,
  144. 0x62, 0x4C, 0x64, 0x88, 0x44, 0x8B, 0xFB, 0xA2,
  145. 0x17, 0x9A, 0x59, 0xF5, 0x87, 0xB3, 0x4F, 0x13,
  146. 0x61, 0x45, 0x6D, 0x8D, 0x09, 0x81, 0x7D, 0x32,
  147. 0xBD, 0x8F, 0x40, 0xEB, 0x86, 0xB7, 0x7B, 0x0B,
  148. 0xF0, 0x95, 0x21, 0x22, 0x5C, 0x6B, 0x4E, 0x82,
  149. 0x54, 0xD6, 0x65, 0x93, 0xCE, 0x60, 0xB2, 0x1C,
  150. 0x73, 0x56, 0xC0, 0x14, 0xA7, 0x8C, 0xF1, 0xDC,
  151. 0x12, 0x75, 0xCA, 0x1F, 0x3B, 0xBE, 0xE4, 0xD1,
  152. 0x42, 0x3D, 0xD4, 0x30, 0xA3, 0x3C, 0xB6, 0x26,
  153. 0x6F, 0xBF, 0x0E, 0xDA, 0x46, 0x69, 0x07, 0x57,
  154. 0x27, 0xF2, 0x1D, 0x9B, 0xBC, 0x94, 0x43, 0x03,
  155. 0xF8, 0x11, 0xC7, 0xF6, 0x90, 0xEF, 0x3E, 0xE7,
  156. 0x06, 0xC3, 0xD5, 0x2F, 0xC8, 0x66, 0x1E, 0xD7,
  157. 0x08, 0xE8, 0xEA, 0xDE, 0x80, 0x52, 0xEE, 0xF7,
  158. 0x84, 0xAA, 0x72, 0xAC, 0x35, 0x4D, 0x6A, 0x2A,
  159. 0x96, 0x1A, 0xD2, 0x71, 0x5A, 0x15, 0x49, 0x74,
  160. 0x4B, 0x9F, 0xD0, 0x5E, 0x04, 0x18, 0xA4, 0xEC,
  161. 0xC2, 0xE0, 0x41, 0x6E, 0x0F, 0x51, 0xCB, 0xCC,
  162. 0x24, 0x91, 0xAF, 0x50, 0xA1, 0xF4, 0x70, 0x39,
  163. 0x99, 0x7C, 0x3A, 0x85, 0x23, 0xB8, 0xB4, 0x7A,
  164. 0xFC, 0x02, 0x36, 0x5B, 0x25, 0x55, 0x97, 0x31,
  165. 0x2D, 0x5D, 0xFA, 0x98, 0xE3, 0x8A, 0x92, 0xAE,
  166. 0x05, 0xDF, 0x29, 0x10, 0x67, 0x6C, 0xBA, 0xC9,
  167. 0xD3, 0x00, 0xE6, 0xCF, 0xE1, 0x9E, 0xA8, 0x2C,
  168. 0x63, 0x16, 0x01, 0x3F, 0x58, 0xE2, 0x89, 0xA9,
  169. 0x0D, 0x38, 0x34, 0x1B, 0xAB, 0x33, 0xFF, 0xB0,
  170. 0xBB, 0x48, 0x0C, 0x5F, 0xB9, 0xB1, 0xCD, 0x2E,
  171. 0xC5, 0xF3, 0xDB, 0x47, 0xE5, 0xA5, 0x9C, 0x77,
  172. 0x0A, 0xA6, 0x20, 0x68, 0xFE, 0x7F, 0xC1, 0xAD,
  173. 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED,
  174. 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D,
  175. 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E,
  176. 0x62, 0x4C, 0x64, 0x88, 0x44, 0x8B, 0xFB, 0xA2,
  177. 0x17, 0x9A, 0x59, 0xF5, 0x87, 0xB3, 0x4F, 0x13,
  178. 0x61, 0x45, 0x6D, 0x8D, 0x09, 0x81, 0x7D, 0x32,
  179. 0xBD, 0x8F, 0x40, 0xEB, 0x86, 0xB7, 0x7B, 0x0B,
  180. 0xF0, 0x95, 0x21, 0x22, 0x5C, 0x6B, 0x4E, 0x82,
  181. 0x54, 0xD6, 0x65, 0x93, 0xCE, 0x60, 0xB2, 0x1C,
  182. 0x73, 0x56, 0xC0, 0x14, 0xA7, 0x8C, 0xF1, 0xDC,
  183. 0x12, 0x75, 0xCA, 0x1F, 0x3B, 0xBE, 0xE4, 0xD1,
  184. 0x42, 0x3D, 0xD4, 0x30, 0xA3, 0x3C, 0xB6, 0x26,
  185. 0x6F, 0xBF, 0x0E, 0xDA, 0x46, 0x69, 0x07, 0x57,
  186. 0x27, 0xF2, 0x1D, 0x9B, 0xBC, 0x94, 0x43, 0x03,
  187. 0xF8, 0x11, 0xC7, 0xF6, 0x90, 0xEF, 0x3E, 0xE7,
  188. 0x06, 0xC3, 0xD5, 0x2F, 0xC8, 0x66, 0x1E, 0xD7,
  189. 0x08, 0xE8, 0xEA, 0xDE, 0x80, 0x52, 0xEE, 0xF7,
  190. 0x84, 0xAA, 0x72, 0xAC, 0x35, 0x4D, 0x6A, 0x2A,
  191. 0x96, 0x1A, 0xD2, 0x71, 0x5A, 0x15, 0x49, 0x74,
  192. 0x4B, 0x9F, 0xD0, 0x5E, 0x04, 0x18, 0xA4, 0xEC,
  193. 0xC2, 0xE0, 0x41, 0x6E, 0x0F, 0x51, 0xCB, 0xCC,
  194. 0x24, 0x91, 0xAF, 0x50, 0xA1, 0xF4, 0x70, 0x39,
  195. 0x99, 0x7C, 0x3A, 0x85, 0x23, 0xB8, 0xB4, 0x7A,
  196. 0xFC, 0x02, 0x36, 0x5B, 0x25, 0x55, 0x97, 0x31,
  197. 0x2D, 0x5D, 0xFA, 0x98, 0xE3, 0x8A, 0x92, 0xAE,
  198. 0x05, 0xDF, 0x29, 0x10, 0x67, 0x6C, 0xBA, 0xC9,
  199. 0xD3, 0x00, 0xE6, 0xCF, 0xE1, 0x9E, 0xA8, 0x2C,
  200. 0x63, 0x16, 0x01, 0x3F, 0x58, 0xE2, 0x89, 0xA9,
  201. 0x0D, 0x38, 0x34, 0x1B, 0xAB, 0x33, 0xFF, 0xB0,
  202. 0xBB, 0x48, 0x0C, 0x5F, 0xB9, 0xB1, 0xCD, 0x2E,
  203. 0xC5, 0xF3, 0xDB, 0x47, 0xE5, 0xA5, 0x9C, 0x77,
  204. 0x0A, 0xA6, 0x20, 0x68, 0xFE, 0x7F, 0xC1, 0xAD
  205. );
  206. /**
  207. * Inverse key expansion randomization table.
  208. *
  209. * @see self::setKey()
  210. * @var array
  211. * @access private
  212. */
  213. var $invpitable = array(
  214. 0xD1, 0xDA, 0xB9, 0x6F, 0x9C, 0xC8, 0x78, 0x66,
  215. 0x80, 0x2C, 0xF8, 0x37, 0xEA, 0xE0, 0x62, 0xA4,
  216. 0xCB, 0x71, 0x50, 0x27, 0x4B, 0x95, 0xD9, 0x20,
  217. 0x9D, 0x04, 0x91, 0xE3, 0x47, 0x6A, 0x7E, 0x53,
  218. 0xFA, 0x3A, 0x3B, 0xB4, 0xA8, 0xBC, 0x5F, 0x68,
  219. 0x08, 0xCA, 0x8F, 0x14, 0xD7, 0xC0, 0xEF, 0x7B,
  220. 0x5B, 0xBF, 0x2F, 0xE5, 0xE2, 0x8C, 0xBA, 0x12,
  221. 0xE1, 0xAF, 0xB2, 0x54, 0x5D, 0x59, 0x76, 0xDB,
  222. 0x32, 0xA2, 0x58, 0x6E, 0x1C, 0x29, 0x64, 0xF3,
  223. 0xE9, 0x96, 0x0C, 0x98, 0x19, 0x8D, 0x3E, 0x26,
  224. 0xAB, 0xA5, 0x85, 0x16, 0x40, 0xBD, 0x49, 0x67,
  225. 0xDC, 0x22, 0x94, 0xBB, 0x3C, 0xC1, 0x9B, 0xEB,
  226. 0x45, 0x28, 0x18, 0xD8, 0x1A, 0x42, 0x7D, 0xCC,
  227. 0xFB, 0x65, 0x8E, 0x3D, 0xCD, 0x2A, 0xA3, 0x60,
  228. 0xAE, 0x93, 0x8A, 0x48, 0x97, 0x51, 0x15, 0xF7,
  229. 0x01, 0x0B, 0xB7, 0x36, 0xB1, 0x2E, 0x11, 0xFD,
  230. 0x84, 0x2D, 0x3F, 0x13, 0x88, 0xB3, 0x34, 0x24,
  231. 0x1B, 0xDE, 0xC5, 0x1D, 0x4D, 0x2B, 0x17, 0x31,
  232. 0x74, 0xA9, 0xC6, 0x43, 0x6D, 0x39, 0x90, 0xBE,
  233. 0xC3, 0xB0, 0x21, 0x6B, 0xF6, 0x0F, 0xD5, 0x99,
  234. 0x0D, 0xAC, 0x1F, 0x5C, 0x9E, 0xF5, 0xF9, 0x4C,
  235. 0xD6, 0xDF, 0x89, 0xE4, 0x8B, 0xFF, 0xC7, 0xAA,
  236. 0xE7, 0xED, 0x46, 0x25, 0xB6, 0x06, 0x5E, 0x35,
  237. 0xB5, 0xEC, 0xCE, 0xE8, 0x6C, 0x30, 0x55, 0x61,
  238. 0x4A, 0xFE, 0xA0, 0x79, 0x03, 0xF0, 0x10, 0x72,
  239. 0x7C, 0xCF, 0x52, 0xA6, 0xA7, 0xEE, 0x44, 0xD3,
  240. 0x9A, 0x57, 0x92, 0xD0, 0x5A, 0x7A, 0x41, 0x7F,
  241. 0x0E, 0x00, 0x63, 0xF2, 0x4F, 0x05, 0x83, 0xC9,
  242. 0xA1, 0xD4, 0xDD, 0xC4, 0x56, 0xF4, 0xD2, 0x77,
  243. 0x81, 0x09, 0x82, 0x33, 0x9F, 0x07, 0x86, 0x75,
  244. 0x38, 0x4E, 0x69, 0xF1, 0xAD, 0x23, 0x73, 0x87,
  245. 0x70, 0x02, 0xC2, 0x1E, 0xB8, 0x0A, 0xFC, 0xE6
  246. );
  247. /**
  248. * Default Constructor.
  249. *
  250. * @param int $mode
  251. * @access public
  252. * @throws \InvalidArgumentException if an invalid / unsupported mode is provided
  253. */
  254. function __construct($mode)
  255. {
  256. if ($mode == self::MODE_STREAM) {
  257. throw new \InvalidArgumentException('Block ciphers cannot be ran in stream mode');
  258. }
  259. parent::__construct($mode);
  260. }
  261. /**
  262. * Test for engine validity
  263. *
  264. * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine()
  265. *
  266. * @see \phpseclib\Crypt\Base::__construct()
  267. * @param int $engine
  268. * @access public
  269. * @return bool
  270. */
  271. function isValidEngine($engine)
  272. {
  273. switch ($engine) {
  274. case self::ENGINE_OPENSSL:
  275. if ($this->current_key_length != 128 || strlen($this->orig_key) < 16) {
  276. return false;
  277. }
  278. $this->cipher_name_openssl_ecb = 'rc2-ecb';
  279. $this->cipher_name_openssl = 'rc2-' . $this->_openssl_translate_mode();
  280. }
  281. return parent::isValidEngine($engine);
  282. }
  283. /**
  284. * Sets the key length.
  285. *
  286. * Valid key lengths are 8 to 1024.
  287. * Calling this function after setting the key has no effect until the next
  288. * \phpseclib\Crypt\RC2::setKey() call.
  289. *
  290. * @access public
  291. * @param int $length in bits
  292. * @throws \LengthException if the key length isn't supported
  293. */
  294. function setKeyLength($length)
  295. {
  296. if ($length < 8 || $length > 1024) {
  297. throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 1024 bits, inclusive, are supported');
  298. }
  299. $this->default_key_length = $this->current_key_length = $length;
  300. }
  301. /**
  302. * Returns the current key length
  303. *
  304. * @access public
  305. * @return int
  306. */
  307. function getKeyLength()
  308. {
  309. return $this->current_key_length;
  310. }
  311. /**
  312. * Sets the key.
  313. *
  314. * Keys can be of any length. RC2, itself, uses 8 to 1024 bit keys (eg.
  315. * strlen($key) <= 128), however, we only use the first 128 bytes if $key
  316. * has more then 128 bytes in it, and set $key to a single null byte if
  317. * it is empty.
  318. *
  319. * If the key is not explicitly set, it'll be assumed to be a single
  320. * null byte.
  321. *
  322. * @see \phpseclib\Crypt\Base::setKey()
  323. * @access public
  324. * @param string $key
  325. * @param int $t1 optional Effective key length in bits.
  326. * @throws \LengthException if the key length isn't supported
  327. */
  328. function setKey($key, $t1 = false)
  329. {
  330. $this->orig_key = $key;
  331. if ($t1 === false) {
  332. $t1 = $this->default_key_length;
  333. }
  334. if ($t1 < 1 || $t1 > 1024) {
  335. throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys between 1 and 1024 bits, inclusive, are supported');
  336. }
  337. $this->current_key_length = $t1;
  338. // Key byte count should be 1..128.
  339. $key = strlen($key) ? substr($key, 0, 128) : "\x00";
  340. $t = strlen($key);
  341. // The mcrypt RC2 implementation only supports effective key length
  342. // of 1024 bits. It is however possible to handle effective key
  343. // lengths in range 1..1024 by expanding the key and applying
  344. // inverse pitable mapping to the first byte before submitting it
  345. // to mcrypt.
  346. // Key expansion.
  347. $l = array_values(unpack('C*', $key));
  348. $t8 = ($t1 + 7) >> 3;
  349. $tm = 0xFF >> (8 * $t8 - $t1);
  350. // Expand key.
  351. $pitable = $this->pitable;
  352. for ($i = $t; $i < 128; $i++) {
  353. $l[$i] = $pitable[$l[$i - 1] + $l[$i - $t]];
  354. }
  355. $i = 128 - $t8;
  356. $l[$i] = $pitable[$l[$i] & $tm];
  357. while ($i--) {
  358. $l[$i] = $pitable[$l[$i + 1] ^ $l[$i + $t8]];
  359. }
  360. // Prepare the key for mcrypt.
  361. $l[0] = $this->invpitable[$l[0]];
  362. array_unshift($l, 'C*');
  363. parent::setKey(call_user_func_array('pack', $l));
  364. }
  365. /**
  366. * Encrypts a message.
  367. *
  368. * Mostly a wrapper for \phpseclib\Crypt\Base::encrypt, with some additional OpenSSL handling code
  369. *
  370. * @see self::decrypt()
  371. * @access public
  372. * @param string $plaintext
  373. * @return string $ciphertext
  374. */
  375. function encrypt($plaintext)
  376. {
  377. if ($this->engine == self::ENGINE_OPENSSL) {
  378. $temp = $this->key;
  379. $this->key = $this->orig_key;
  380. $result = parent::encrypt($plaintext);
  381. $this->key = $temp;
  382. return $result;
  383. }
  384. return parent::encrypt($plaintext);
  385. }
  386. /**
  387. * Decrypts a message.
  388. *
  389. * Mostly a wrapper for \phpseclib\Crypt\Base::decrypt, with some additional OpenSSL handling code
  390. *
  391. * @see self::encrypt()
  392. * @access public
  393. * @param string $ciphertext
  394. * @return string $plaintext
  395. */
  396. function decrypt($ciphertext)
  397. {
  398. if ($this->engine == self::ENGINE_OPENSSL) {
  399. $temp = $this->key;
  400. $this->key = $this->orig_key;
  401. $result = parent::decrypt($ciphertext);
  402. $this->key = $temp;
  403. return $result;
  404. }
  405. return parent::decrypt($ciphertext);
  406. }
  407. /**
  408. * Encrypts a block
  409. *
  410. * @see \phpseclib\Crypt\Base::_encryptBlock()
  411. * @see \phpseclib\Crypt\Base::encrypt()
  412. * @access private
  413. * @param string $in
  414. * @return string
  415. */
  416. function _encryptBlock($in)
  417. {
  418. list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in));
  419. $keys = $this->keys;
  420. $limit = 20;
  421. $actions = array($limit => 44, 44 => 64);
  422. $j = 0;
  423. for (;;) {
  424. // Mixing round.
  425. $r0 = (($r0 + $keys[$j++] + ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF) << 1;
  426. $r0 |= $r0 >> 16;
  427. $r1 = (($r1 + $keys[$j++] + ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF) << 2;
  428. $r1 |= $r1 >> 16;
  429. $r2 = (($r2 + $keys[$j++] + ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF) << 3;
  430. $r2 |= $r2 >> 16;
  431. $r3 = (($r3 + $keys[$j++] + ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF) << 5;
  432. $r3 |= $r3 >> 16;
  433. if ($j === $limit) {
  434. if ($limit === 64) {
  435. break;
  436. }
  437. // Mashing round.
  438. $r0 += $keys[$r3 & 0x3F];
  439. $r1 += $keys[$r0 & 0x3F];
  440. $r2 += $keys[$r1 & 0x3F];
  441. $r3 += $keys[$r2 & 0x3F];
  442. $limit = $actions[$limit];
  443. }
  444. }
  445. return pack('vvvv', $r0, $r1, $r2, $r3);
  446. }
  447. /**
  448. * Decrypts a block
  449. *
  450. * @see \phpseclib\Crypt\Base::_decryptBlock()
  451. * @see \phpseclib\Crypt\Base::decrypt()
  452. * @access private
  453. * @param string $in
  454. * @return string
  455. */
  456. function _decryptBlock($in)
  457. {
  458. list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in));
  459. $keys = $this->keys;
  460. $limit = 44;
  461. $actions = array($limit => 20, 20 => 0);
  462. $j = 64;
  463. for (;;) {
  464. // R-mixing round.
  465. $r3 = ($r3 | ($r3 << 16)) >> 5;
  466. $r3 = ($r3 - $keys[--$j] - ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF;
  467. $r2 = ($r2 | ($r2 << 16)) >> 3;
  468. $r2 = ($r2 - $keys[--$j] - ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF;
  469. $r1 = ($r1 | ($r1 << 16)) >> 2;
  470. $r1 = ($r1 - $keys[--$j] - ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF;
  471. $r0 = ($r0 | ($r0 << 16)) >> 1;
  472. $r0 = ($r0 - $keys[--$j] - ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF;
  473. if ($j === $limit) {
  474. if ($limit === 0) {
  475. break;
  476. }
  477. // R-mashing round.
  478. $r3 = ($r3 - $keys[$r2 & 0x3F]) & 0xFFFF;
  479. $r2 = ($r2 - $keys[$r1 & 0x3F]) & 0xFFFF;
  480. $r1 = ($r1 - $keys[$r0 & 0x3F]) & 0xFFFF;
  481. $r0 = ($r0 - $keys[$r3 & 0x3F]) & 0xFFFF;
  482. $limit = $actions[$limit];
  483. }
  484. }
  485. return pack('vvvv', $r0, $r1, $r2, $r3);
  486. }
  487. /**
  488. * Setup the \phpseclib\Crypt\Base::ENGINE_MCRYPT $engine
  489. *
  490. * @see \phpseclib\Crypt\Base::_setupMcrypt()
  491. * @access private
  492. */
  493. function _setupMcrypt()
  494. {
  495. if (!isset($this->key)) {
  496. $this->setKey('');
  497. }
  498. parent::_setupMcrypt();
  499. }
  500. /**
  501. * Creates the key schedule
  502. *
  503. * @see \phpseclib\Crypt\Base::_setupKey()
  504. * @access private
  505. */
  506. function _setupKey()
  507. {
  508. if (!isset($this->key)) {
  509. $this->setKey('');
  510. }
  511. // Key has already been expanded in \phpseclib\Crypt\RC2::setKey():
  512. // Only the first value must be altered.
  513. $l = unpack('Ca/Cb/v*', $this->key);
  514. array_unshift($l, $this->pitable[$l['a']] | ($l['b'] << 8));
  515. unset($l['a']);
  516. unset($l['b']);
  517. $this->keys = $l;
  518. }
  519. /**
  520. * Setup the performance-optimized function for de/encrypt()
  521. *
  522. * @see \phpseclib\Crypt\Base::_setupInlineCrypt()
  523. * @access private
  524. */
  525. function _setupInlineCrypt()
  526. {
  527. $lambda_functions =& self::_getLambdaFunctions();
  528. // The first 10 generated $lambda_functions will use the $keys hardcoded as integers
  529. // for the mixing rounds, for better inline crypt performance [~20% faster].
  530. // But for memory reason we have to limit those ultra-optimized $lambda_functions to an amount of 10.
  531. // (Currently, for Crypt_RC2, one generated $lambda_function cost on php5.5@32bit ~60kb unfreeable mem and ~100kb on php5.5@64bit)
  532. $gen_hi_opt_code = (bool)(count($lambda_functions) < 10);
  533. // Generation of a uniqe hash for our generated code
  534. $code_hash = "Crypt_RC2, {$this->mode}";
  535. if ($gen_hi_opt_code) {
  536. $code_hash = str_pad($code_hash, 32) . $this->_hashInlineCryptFunction($this->key);
  537. }
  538. // Is there a re-usable $lambda_functions in there?
  539. // If not, we have to create it.
  540. if (!isset($lambda_functions[$code_hash])) {
  541. // Init code for both, encrypt and decrypt.
  542. $init_crypt = '$keys = $self->keys;';
  543. switch (true) {
  544. case $gen_hi_opt_code:
  545. $keys = $this->keys;
  546. default:
  547. $keys = array();
  548. foreach ($this->keys as $k => $v) {
  549. $keys[$k] = '$keys[' . $k . ']';
  550. }
  551. }
  552. // $in is the current 8 bytes block which has to be en/decrypt
  553. $encrypt_block = $decrypt_block = '
  554. $in = unpack("v4", $in);
  555. $r0 = $in[1];
  556. $r1 = $in[2];
  557. $r2 = $in[3];
  558. $r3 = $in[4];
  559. ';
  560. // Create code for encryption.
  561. $limit = 20;
  562. $actions = array($limit => 44, 44 => 64);
  563. $j = 0;
  564. for (;;) {
  565. // Mixing round.
  566. $encrypt_block .= '
  567. $r0 = (($r0 + ' . $keys[$j++] . ' +
  568. ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF) << 1;
  569. $r0 |= $r0 >> 16;
  570. $r1 = (($r1 + ' . $keys[$j++] . ' +
  571. ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF) << 2;
  572. $r1 |= $r1 >> 16;
  573. $r2 = (($r2 + ' . $keys[$j++] . ' +
  574. ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF) << 3;
  575. $r2 |= $r2 >> 16;
  576. $r3 = (($r3 + ' . $keys[$j++] . ' +
  577. ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF) << 5;
  578. $r3 |= $r3 >> 16;';
  579. if ($j === $limit) {
  580. if ($limit === 64) {
  581. break;
  582. }
  583. // Mashing round.
  584. $encrypt_block .= '
  585. $r0 += $keys[$r3 & 0x3F];
  586. $r1 += $keys[$r0 & 0x3F];
  587. $r2 += $keys[$r1 & 0x3F];
  588. $r3 += $keys[$r2 & 0x3F];';
  589. $limit = $actions[$limit];
  590. }
  591. }
  592. $encrypt_block .= '$in = pack("v4", $r0, $r1, $r2, $r3);';
  593. // Create code for decryption.
  594. $limit = 44;
  595. $actions = array($limit => 20, 20 => 0);
  596. $j = 64;
  597. for (;;) {
  598. // R-mixing round.
  599. $decrypt_block .= '
  600. $r3 = ($r3 | ($r3 << 16)) >> 5;
  601. $r3 = ($r3 - ' . $keys[--$j] . ' -
  602. ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF;
  603. $r2 = ($r2 | ($r2 << 16)) >> 3;
  604. $r2 = ($r2 - ' . $keys[--$j] . ' -
  605. ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF;
  606. $r1 = ($r1 | ($r1 << 16)) >> 2;
  607. $r1 = ($r1 - ' . $keys[--$j] . ' -
  608. ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF;
  609. $r0 = ($r0 | ($r0 << 16)) >> 1;
  610. $r0 = ($r0 - ' . $keys[--$j] . ' -
  611. ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF;';
  612. if ($j === $limit) {
  613. if ($limit === 0) {
  614. break;
  615. }
  616. // R-mashing round.
  617. $decrypt_block .= '
  618. $r3 = ($r3 - $keys[$r2 & 0x3F]) & 0xFFFF;
  619. $r2 = ($r2 - $keys[$r1 & 0x3F]) & 0xFFFF;
  620. $r1 = ($r1 - $keys[$r0 & 0x3F]) & 0xFFFF;
  621. $r0 = ($r0 - $keys[$r3 & 0x3F]) & 0xFFFF;';
  622. $limit = $actions[$limit];
  623. }
  624. }
  625. $decrypt_block .= '$in = pack("v4", $r0, $r1, $r2, $r3);';
  626. // Creates the inline-crypt function
  627. $lambda_functions[$code_hash] = $this->_createInlineCryptFunction(
  628. array(
  629. 'init_crypt' => $init_crypt,
  630. 'encrypt_block' => $encrypt_block,
  631. 'decrypt_block' => $decrypt_block
  632. )
  633. );
  634. }
  635. // Set the inline-crypt function as callback in: $this->inline_crypt
  636. $this->inline_crypt = $lambda_functions[$code_hash];
  637. }
  638. }