RC2.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP implementation of RC2.
  5. *
  6. * Uses mcrypt, if available, and an internal implementation, otherwise.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * Useful resources are as follows:
  11. *
  12. * - {@link http://tools.ietf.org/html/rfc2268}
  13. *
  14. * Here's a short example of how to use this library:
  15. * <code>
  16. * <?php
  17. * include('Crypt/RC2.php');
  18. *
  19. * $rc2 = new Crypt_RC2();
  20. *
  21. * $rc2->setKey('abcdefgh');
  22. *
  23. * $plaintext = str_repeat('a', 1024);
  24. *
  25. * echo $rc2->decrypt($rc2->encrypt($plaintext));
  26. * ?>
  27. * </code>
  28. *
  29. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  30. * of this software and associated documentation files (the "Software"), to deal
  31. * in the Software without restriction, including without limitation the rights
  32. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  33. * copies of the Software, and to permit persons to whom the Software is
  34. * furnished to do so, subject to the following conditions:
  35. *
  36. * The above copyright notice and this permission notice shall be included in
  37. * all copies or substantial portions of the Software.
  38. *
  39. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  40. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  41. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  42. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  43. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  44. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  45. * THE SOFTWARE.
  46. *
  47. * @category Crypt
  48. * @package Crypt_RC2
  49. * @author Patrick Monnerat <pm@datasphere.ch>
  50. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  51. * @link http://phpseclib.sourceforge.net
  52. */
  53. /**
  54. * Include Crypt_Base
  55. *
  56. * Base cipher class
  57. */
  58. if (!class_exists('Crypt_Base')) {
  59. require_once('Base.php');
  60. }
  61. /**#@+
  62. * @access public
  63. * @see Crypt_RC2::encrypt()
  64. * @see Crypt_RC2::decrypt()
  65. */
  66. /**
  67. * Encrypt / decrypt using the Counter mode.
  68. *
  69. * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
  70. *
  71. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
  72. */
  73. define('CRYPT_RC2_MODE_CTR', CRYPT_MODE_CTR);
  74. /**
  75. * Encrypt / decrypt using the Electronic Code Book mode.
  76. *
  77. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
  78. */
  79. define('CRYPT_RC2_MODE_ECB', CRYPT_MODE_ECB);
  80. /**
  81. * Encrypt / decrypt using the Code Book Chaining mode.
  82. *
  83. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
  84. */
  85. define('CRYPT_RC2_MODE_CBC', CRYPT_MODE_CBC);
  86. /**
  87. * Encrypt / decrypt using the Cipher Feedback mode.
  88. *
  89. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
  90. */
  91. define('CRYPT_RC2_MODE_CFB', CRYPT_MODE_CFB);
  92. /**
  93. * Encrypt / decrypt using the Cipher Feedback mode.
  94. *
  95. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
  96. */
  97. define('CRYPT_RC2_MODE_OFB', CRYPT_MODE_OFB);
  98. /**#@-*/
  99. /**#@+
  100. * @access private
  101. * @see Crypt_RC2::Crypt_RC2()
  102. */
  103. /**
  104. * Toggles the internal implementation
  105. */
  106. define('CRYPT_RC2_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
  107. /**
  108. * Toggles the mcrypt implementation
  109. */
  110. define('CRYPT_RC2_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
  111. /**#@-*/
  112. /**
  113. * Pure-PHP implementation of RC2.
  114. *
  115. * @version 0.1.1
  116. * @access public
  117. * @package Crypt_RC2
  118. */
  119. class Crypt_RC2 extends Crypt_Base {
  120. /**
  121. * Block Length of the cipher
  122. *
  123. * @see Crypt_Base::block_size
  124. * @var Integer
  125. * @access private
  126. */
  127. var $block_size = 8;
  128. /**
  129. * The Key
  130. *
  131. * @see Crypt_Base::key
  132. * @see setKey()
  133. * @var String
  134. * @access private
  135. */
  136. var $key = "\0";
  137. /**
  138. * The default password key_size used by setPassword()
  139. *
  140. * @see Crypt_Base::password_key_size
  141. * @see Crypt_Base::setPassword()
  142. * @var Integer
  143. * @access private
  144. */
  145. var $password_key_size = 16; // = 128 bits
  146. /**
  147. * The namespace used by the cipher for its constants.
  148. *
  149. * @see Crypt_Base::const_namespace
  150. * @var String
  151. * @access private
  152. */
  153. var $const_namespace = 'RC2';
  154. /**
  155. * The mcrypt specific name of the cipher
  156. *
  157. * @see Crypt_Base::cipher_name_mcrypt
  158. * @var String
  159. * @access private
  160. */
  161. var $cipher_name_mcrypt = 'rc2';
  162. /**
  163. * Optimizing value while CFB-encrypting
  164. *
  165. * @see Crypt_Base::cfb_init_len
  166. * @var Integer
  167. * @access private
  168. */
  169. var $cfb_init_len = 500;
  170. /**
  171. * The key length in bits.
  172. *
  173. * @see Crypt_RC2::setKeyLength()
  174. * @see Crypt_RC2::setKey()
  175. * @var Integer
  176. * @access private
  177. * @internal Should be in range [1..1024].
  178. * @internal Changing this value after setting the key has no effect.
  179. */
  180. var $default_key_length = 1024;
  181. /**
  182. * The Key Schedule
  183. *
  184. * @see Crypt_RC2::_setupKey()
  185. * @var Array
  186. * @access private
  187. */
  188. var $keys;
  189. /**
  190. * Key expansion randomization table.
  191. * Twice the same 256-value sequence to save a modulus in key expansion.
  192. *
  193. * @see Crypt_RC2::setKey()
  194. * @var Array
  195. * @access private
  196. */
  197. var $pitable = array(
  198. 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED,
  199. 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D,
  200. 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E,
  201. 0x62, 0x4C, 0x64, 0x88, 0x44, 0x8B, 0xFB, 0xA2,
  202. 0x17, 0x9A, 0x59, 0xF5, 0x87, 0xB3, 0x4F, 0x13,
  203. 0x61, 0x45, 0x6D, 0x8D, 0x09, 0x81, 0x7D, 0x32,
  204. 0xBD, 0x8F, 0x40, 0xEB, 0x86, 0xB7, 0x7B, 0x0B,
  205. 0xF0, 0x95, 0x21, 0x22, 0x5C, 0x6B, 0x4E, 0x82,
  206. 0x54, 0xD6, 0x65, 0x93, 0xCE, 0x60, 0xB2, 0x1C,
  207. 0x73, 0x56, 0xC0, 0x14, 0xA7, 0x8C, 0xF1, 0xDC,
  208. 0x12, 0x75, 0xCA, 0x1F, 0x3B, 0xBE, 0xE4, 0xD1,
  209. 0x42, 0x3D, 0xD4, 0x30, 0xA3, 0x3C, 0xB6, 0x26,
  210. 0x6F, 0xBF, 0x0E, 0xDA, 0x46, 0x69, 0x07, 0x57,
  211. 0x27, 0xF2, 0x1D, 0x9B, 0xBC, 0x94, 0x43, 0x03,
  212. 0xF8, 0x11, 0xC7, 0xF6, 0x90, 0xEF, 0x3E, 0xE7,
  213. 0x06, 0xC3, 0xD5, 0x2F, 0xC8, 0x66, 0x1E, 0xD7,
  214. 0x08, 0xE8, 0xEA, 0xDE, 0x80, 0x52, 0xEE, 0xF7,
  215. 0x84, 0xAA, 0x72, 0xAC, 0x35, 0x4D, 0x6A, 0x2A,
  216. 0x96, 0x1A, 0xD2, 0x71, 0x5A, 0x15, 0x49, 0x74,
  217. 0x4B, 0x9F, 0xD0, 0x5E, 0x04, 0x18, 0xA4, 0xEC,
  218. 0xC2, 0xE0, 0x41, 0x6E, 0x0F, 0x51, 0xCB, 0xCC,
  219. 0x24, 0x91, 0xAF, 0x50, 0xA1, 0xF4, 0x70, 0x39,
  220. 0x99, 0x7C, 0x3A, 0x85, 0x23, 0xB8, 0xB4, 0x7A,
  221. 0xFC, 0x02, 0x36, 0x5B, 0x25, 0x55, 0x97, 0x31,
  222. 0x2D, 0x5D, 0xFA, 0x98, 0xE3, 0x8A, 0x92, 0xAE,
  223. 0x05, 0xDF, 0x29, 0x10, 0x67, 0x6C, 0xBA, 0xC9,
  224. 0xD3, 0x00, 0xE6, 0xCF, 0xE1, 0x9E, 0xA8, 0x2C,
  225. 0x63, 0x16, 0x01, 0x3F, 0x58, 0xE2, 0x89, 0xA9,
  226. 0x0D, 0x38, 0x34, 0x1B, 0xAB, 0x33, 0xFF, 0xB0,
  227. 0xBB, 0x48, 0x0C, 0x5F, 0xB9, 0xB1, 0xCD, 0x2E,
  228. 0xC5, 0xF3, 0xDB, 0x47, 0xE5, 0xA5, 0x9C, 0x77,
  229. 0x0A, 0xA6, 0x20, 0x68, 0xFE, 0x7F, 0xC1, 0xAD,
  230. 0xD9, 0x78, 0xF9, 0xC4, 0x19, 0xDD, 0xB5, 0xED,
  231. 0x28, 0xE9, 0xFD, 0x79, 0x4A, 0xA0, 0xD8, 0x9D,
  232. 0xC6, 0x7E, 0x37, 0x83, 0x2B, 0x76, 0x53, 0x8E,
  233. 0x62, 0x4C, 0x64, 0x88, 0x44, 0x8B, 0xFB, 0xA2,
  234. 0x17, 0x9A, 0x59, 0xF5, 0x87, 0xB3, 0x4F, 0x13,
  235. 0x61, 0x45, 0x6D, 0x8D, 0x09, 0x81, 0x7D, 0x32,
  236. 0xBD, 0x8F, 0x40, 0xEB, 0x86, 0xB7, 0x7B, 0x0B,
  237. 0xF0, 0x95, 0x21, 0x22, 0x5C, 0x6B, 0x4E, 0x82,
  238. 0x54, 0xD6, 0x65, 0x93, 0xCE, 0x60, 0xB2, 0x1C,
  239. 0x73, 0x56, 0xC0, 0x14, 0xA7, 0x8C, 0xF1, 0xDC,
  240. 0x12, 0x75, 0xCA, 0x1F, 0x3B, 0xBE, 0xE4, 0xD1,
  241. 0x42, 0x3D, 0xD4, 0x30, 0xA3, 0x3C, 0xB6, 0x26,
  242. 0x6F, 0xBF, 0x0E, 0xDA, 0x46, 0x69, 0x07, 0x57,
  243. 0x27, 0xF2, 0x1D, 0x9B, 0xBC, 0x94, 0x43, 0x03,
  244. 0xF8, 0x11, 0xC7, 0xF6, 0x90, 0xEF, 0x3E, 0xE7,
  245. 0x06, 0xC3, 0xD5, 0x2F, 0xC8, 0x66, 0x1E, 0xD7,
  246. 0x08, 0xE8, 0xEA, 0xDE, 0x80, 0x52, 0xEE, 0xF7,
  247. 0x84, 0xAA, 0x72, 0xAC, 0x35, 0x4D, 0x6A, 0x2A,
  248. 0x96, 0x1A, 0xD2, 0x71, 0x5A, 0x15, 0x49, 0x74,
  249. 0x4B, 0x9F, 0xD0, 0x5E, 0x04, 0x18, 0xA4, 0xEC,
  250. 0xC2, 0xE0, 0x41, 0x6E, 0x0F, 0x51, 0xCB, 0xCC,
  251. 0x24, 0x91, 0xAF, 0x50, 0xA1, 0xF4, 0x70, 0x39,
  252. 0x99, 0x7C, 0x3A, 0x85, 0x23, 0xB8, 0xB4, 0x7A,
  253. 0xFC, 0x02, 0x36, 0x5B, 0x25, 0x55, 0x97, 0x31,
  254. 0x2D, 0x5D, 0xFA, 0x98, 0xE3, 0x8A, 0x92, 0xAE,
  255. 0x05, 0xDF, 0x29, 0x10, 0x67, 0x6C, 0xBA, 0xC9,
  256. 0xD3, 0x00, 0xE6, 0xCF, 0xE1, 0x9E, 0xA8, 0x2C,
  257. 0x63, 0x16, 0x01, 0x3F, 0x58, 0xE2, 0x89, 0xA9,
  258. 0x0D, 0x38, 0x34, 0x1B, 0xAB, 0x33, 0xFF, 0xB0,
  259. 0xBB, 0x48, 0x0C, 0x5F, 0xB9, 0xB1, 0xCD, 0x2E,
  260. 0xC5, 0xF3, 0xDB, 0x47, 0xE5, 0xA5, 0x9C, 0x77,
  261. 0x0A, 0xA6, 0x20, 0x68, 0xFE, 0x7F, 0xC1, 0xAD
  262. );
  263. /**
  264. * Inverse key expansion randomization table.
  265. *
  266. * @see Crypt_RC2::setKey()
  267. * @var Array
  268. * @access private
  269. */
  270. var $invpitable = array(
  271. 0xD1, 0xDA, 0xB9, 0x6F, 0x9C, 0xC8, 0x78, 0x66,
  272. 0x80, 0x2C, 0xF8, 0x37, 0xEA, 0xE0, 0x62, 0xA4,
  273. 0xCB, 0x71, 0x50, 0x27, 0x4B, 0x95, 0xD9, 0x20,
  274. 0x9D, 0x04, 0x91, 0xE3, 0x47, 0x6A, 0x7E, 0x53,
  275. 0xFA, 0x3A, 0x3B, 0xB4, 0xA8, 0xBC, 0x5F, 0x68,
  276. 0x08, 0xCA, 0x8F, 0x14, 0xD7, 0xC0, 0xEF, 0x7B,
  277. 0x5B, 0xBF, 0x2F, 0xE5, 0xE2, 0x8C, 0xBA, 0x12,
  278. 0xE1, 0xAF, 0xB2, 0x54, 0x5D, 0x59, 0x76, 0xDB,
  279. 0x32, 0xA2, 0x58, 0x6E, 0x1C, 0x29, 0x64, 0xF3,
  280. 0xE9, 0x96, 0x0C, 0x98, 0x19, 0x8D, 0x3E, 0x26,
  281. 0xAB, 0xA5, 0x85, 0x16, 0x40, 0xBD, 0x49, 0x67,
  282. 0xDC, 0x22, 0x94, 0xBB, 0x3C, 0xC1, 0x9B, 0xEB,
  283. 0x45, 0x28, 0x18, 0xD8, 0x1A, 0x42, 0x7D, 0xCC,
  284. 0xFB, 0x65, 0x8E, 0x3D, 0xCD, 0x2A, 0xA3, 0x60,
  285. 0xAE, 0x93, 0x8A, 0x48, 0x97, 0x51, 0x15, 0xF7,
  286. 0x01, 0x0B, 0xB7, 0x36, 0xB1, 0x2E, 0x11, 0xFD,
  287. 0x84, 0x2D, 0x3F, 0x13, 0x88, 0xB3, 0x34, 0x24,
  288. 0x1B, 0xDE, 0xC5, 0x1D, 0x4D, 0x2B, 0x17, 0x31,
  289. 0x74, 0xA9, 0xC6, 0x43, 0x6D, 0x39, 0x90, 0xBE,
  290. 0xC3, 0xB0, 0x21, 0x6B, 0xF6, 0x0F, 0xD5, 0x99,
  291. 0x0D, 0xAC, 0x1F, 0x5C, 0x9E, 0xF5, 0xF9, 0x4C,
  292. 0xD6, 0xDF, 0x89, 0xE4, 0x8B, 0xFF, 0xC7, 0xAA,
  293. 0xE7, 0xED, 0x46, 0x25, 0xB6, 0x06, 0x5E, 0x35,
  294. 0xB5, 0xEC, 0xCE, 0xE8, 0x6C, 0x30, 0x55, 0x61,
  295. 0x4A, 0xFE, 0xA0, 0x79, 0x03, 0xF0, 0x10, 0x72,
  296. 0x7C, 0xCF, 0x52, 0xA6, 0xA7, 0xEE, 0x44, 0xD3,
  297. 0x9A, 0x57, 0x92, 0xD0, 0x5A, 0x7A, 0x41, 0x7F,
  298. 0x0E, 0x00, 0x63, 0xF2, 0x4F, 0x05, 0x83, 0xC9,
  299. 0xA1, 0xD4, 0xDD, 0xC4, 0x56, 0xF4, 0xD2, 0x77,
  300. 0x81, 0x09, 0x82, 0x33, 0x9F, 0x07, 0x86, 0x75,
  301. 0x38, 0x4E, 0x69, 0xF1, 0xAD, 0x23, 0x73, 0x87,
  302. 0x70, 0x02, 0xC2, 0x1E, 0xB8, 0x0A, 0xFC, 0xE6
  303. );
  304. /**
  305. * Default Constructor.
  306. *
  307. * Determines whether or not the mcrypt extension should be used.
  308. *
  309. * $mode could be:
  310. *
  311. * - CRYPT_RC2_MODE_ECB
  312. *
  313. * - CRYPT_RC2_MODE_CBC
  314. *
  315. * - CRYPT_RC2_MODE_CTR
  316. *
  317. * - CRYPT_RC2_MODE_CFB
  318. *
  319. * - CRYPT_RC2_MODE_OFB
  320. *
  321. * If not explictly set, CRYPT_RC2_MODE_CBC will be used.
  322. *
  323. * @see Crypt_Base::Crypt_Base()
  324. * @param optional Integer $mode
  325. * @access public
  326. */
  327. function Crypt_RC2($mode = CRYPT_RC2_MODE_CBC)
  328. {
  329. parent::Crypt_Base($mode);
  330. $this->setKey('');
  331. }
  332. /**
  333. * Sets the key length
  334. *
  335. * Valid key lengths are 1 to 1024.
  336. * Calling this function after setting the key has no effect until the next
  337. * Crypt_RC2::setKey() call.
  338. *
  339. * @access public
  340. * @param Integer $length in bits
  341. */
  342. function setKeyLength($length)
  343. {
  344. if ($length >= 1 && $length <= 1024) {
  345. $this->default_key_length = $length;
  346. }
  347. }
  348. /**
  349. * Sets the key.
  350. *
  351. * Keys can be of any length. RC2, itself, uses 1 to 1024 bit keys (eg.
  352. * strlen($key) <= 128), however, we only use the first 128 bytes if $key
  353. * has more then 128 bytes in it, and set $key to a single null byte if
  354. * it is empty.
  355. *
  356. * If the key is not explicitly set, it'll be assumed to be a single
  357. * null byte.
  358. *
  359. * @see Crypt_Base::setKey()
  360. * @access public
  361. * @param String $key
  362. * @param Integer $t1 optional Effective key length in bits.
  363. */
  364. function setKey($key, $t1 = 0)
  365. {
  366. if ($t1 <= 0) {
  367. $t1 = $this->default_key_length;
  368. } else if ($t1 > 1024) {
  369. $t1 = 1024;
  370. }
  371. // Key byte count should be 1..128.
  372. $key = strlen($key) ? substr($key, 0, 128): "\x00";
  373. $t = strlen($key);
  374. // The mcrypt RC2 implementation only supports effective key length
  375. // of 1024 bits. It is however possible to handle effective key
  376. // lengths in range 1..1024 by expanding the key and applying
  377. // inverse pitable mapping to the first byte before submitting it
  378. // to mcrypt.
  379. // Key expansion.
  380. $l = array_values(unpack('C*', $key));
  381. $t8 = ($t1 + 7) >> 3;
  382. $tm = 0xFF >> (8 * $t8 - $t1);
  383. // Expand key.
  384. $pitable = $this->pitable;
  385. for ($i = $t; $i < 128; $i++) {
  386. $l[$i] = $pitable[$l[$i - 1] + $l[$i - $t]];
  387. }
  388. $i = 128 - $t8;
  389. $l[$i] = $pitable[$l[$i] & $tm];
  390. while ($i--) {
  391. $l[$i] = $pitable[$l[$i + 1] ^ $l[$i + $t8]];
  392. }
  393. // Prepare the key for mcrypt.
  394. $l[0] = $this->invpitable[$l[0]];
  395. array_unshift($l, 'C*');
  396. parent::setKey(call_user_func_array('pack', $l));
  397. }
  398. /**
  399. * Encrypts a block
  400. *
  401. * @see Crypt_Base::_encryptBlock()
  402. * @see Crypt_Base::encrypt()
  403. * @access private
  404. * @param String $in
  405. * @return String
  406. */
  407. function _encryptBlock($in)
  408. {
  409. list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in));
  410. $keys = $this->keys;
  411. $limit = 20;
  412. $actions = array($limit => 44, 44 => 64);
  413. $j = 0;
  414. for (;;) {
  415. // Mixing round.
  416. $r0 = (($r0 + $keys[$j++] + ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF) << 1;
  417. $r0 |= $r0 >> 16;
  418. $r1 = (($r1 + $keys[$j++] + ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF) << 2;
  419. $r1 |= $r1 >> 16;
  420. $r2 = (($r2 + $keys[$j++] + ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF) << 3;
  421. $r2 |= $r2 >> 16;
  422. $r3 = (($r3 + $keys[$j++] + ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF) << 5;
  423. $r3 |= $r3 >> 16;
  424. if ($j == $limit) {
  425. if ($limit == 64) {
  426. break;
  427. }
  428. // Mashing round.
  429. $r0 += $keys[$r3 & 0x3F];
  430. $r1 += $keys[$r0 & 0x3F];
  431. $r2 += $keys[$r1 & 0x3F];
  432. $r3 += $keys[$r2 & 0x3F];
  433. $limit = $actions[$limit];
  434. }
  435. }
  436. return pack('vvvv', $r0, $r1, $r2, $r3);
  437. }
  438. /**
  439. * Decrypts a block
  440. *
  441. * @see Crypt_Base::_decryptBlock()
  442. * @see Crypt_Base::decrypt()
  443. * @access private
  444. * @param String $in
  445. * @return String
  446. */
  447. function _decryptBlock($in)
  448. {
  449. list($r0, $r1, $r2, $r3) = array_values(unpack('v*', $in));
  450. $keys = $this->keys;
  451. $limit = 44;
  452. $actions = array($limit => 20, 20 => 0);
  453. $j = 64;
  454. for (;;) {
  455. // R-mixing round.
  456. $r3 = ($r3 | ($r3 << 16)) >> 5;
  457. $r3 = ($r3 - $keys[--$j] - ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF;
  458. $r2 = ($r2 | ($r2 << 16)) >> 3;
  459. $r2 = ($r2 - $keys[--$j] - ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF;
  460. $r1 = ($r1 | ($r1 << 16)) >> 2;
  461. $r1 = ($r1 - $keys[--$j] - ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF;
  462. $r0 = ($r0 | ($r0 << 16)) >> 1;
  463. $r0 = ($r0 - $keys[--$j] - ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF;
  464. if ($j == $limit) {
  465. if (!$limit) {
  466. break;
  467. }
  468. // R-mashing round.
  469. $r3 = ($r3 - $keys[$r2 & 0x3F]) & 0xFFFF;
  470. $r2 = ($r2 - $keys[$r1 & 0x3F]) & 0xFFFF;
  471. $r1 = ($r1 - $keys[$r0 & 0x3F]) & 0xFFFF;
  472. $r0 = ($r0 - $keys[$r3 & 0x3F]) & 0xFFFF;
  473. $limit = $actions[$limit];
  474. }
  475. }
  476. return pack('vvvv', $r0, $r1, $r2, $r3);
  477. }
  478. /**
  479. * Creates the key schedule
  480. *
  481. * @see Crypt_Base::_setupKey()
  482. * @access private
  483. */
  484. function _setupKey()
  485. {
  486. // Key has already been expanded in Crypt_RC2::setKey():
  487. // Only the first value must be altered.
  488. $l = unpack('Ca/Cb/v*', $this->key);
  489. array_unshift($l, $this->pitable[$l['a']] | ($l['b'] << 8));
  490. unset($l['a']);
  491. unset($l['b']);
  492. $this->keys = $l;
  493. }
  494. /**
  495. * Setup the performance-optimized function for de/encrypt()
  496. *
  497. * @see Crypt_Base::_setupInlineCrypt()
  498. * @access private
  499. */
  500. function _setupInlineCrypt()
  501. {
  502. $lambda_functions = &Crypt_RC2::_getLambdaFunctions();
  503. // The first 10 generated $lambda_functions will use the $keys hardcoded as integers
  504. // for the mixing rounds, for better inline crypt performance [~20% faster].
  505. // But for memory reason we have to limit those ultra-optimized $lambda_functions to an amount of 10.
  506. $keys = $this->keys;
  507. if (count($lambda_functions) >= 10) {
  508. foreach ($this->keys as $k => $v) {
  509. $keys[$k] = '$keys[' . $k . ']';
  510. }
  511. }
  512. $code_hash = md5(str_pad("Crypt_RC2, {$this->mode}, ", 32, "\0") . implode(',', $keys));
  513. // Is there a re-usable $lambda_functions in there?
  514. // If not, we have to create it.
  515. if (!isset($lambda_functions[$code_hash])) {
  516. // Init code for both, encrypt and decrypt.
  517. $init_crypt = '$keys = $self->keys;';
  518. // $in is the current 8 bytes block which has to be en/decrypt
  519. $encrypt_block = $decrypt_block = '
  520. $in = unpack("v4", $in);
  521. $r0 = $in[1];
  522. $r1 = $in[2];
  523. $r2 = $in[3];
  524. $r3 = $in[4];
  525. ';
  526. // Create code for encryption.
  527. $limit = 20;
  528. $actions = array($limit => 44, 44 => 64);
  529. $j = 0;
  530. for (;;) {
  531. // Mixing round.
  532. $encrypt_block .= '
  533. $r0 = (($r0 + ' . $keys[$j++] . ' +
  534. ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF) << 1;
  535. $r0 |= $r0 >> 16;
  536. $r1 = (($r1 + ' . $keys[$j++] . ' +
  537. ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF) << 2;
  538. $r1 |= $r1 >> 16;
  539. $r2 = (($r2 + ' . $keys[$j++] . ' +
  540. ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF) << 3;
  541. $r2 |= $r2 >> 16;
  542. $r3 = (($r3 + ' . $keys[$j++] . ' +
  543. ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF) << 5;
  544. $r3 |= $r3 >> 16;';
  545. if ($j == $limit) {
  546. if ($limit == 64) {
  547. break;
  548. }
  549. // Mashing round.
  550. $encrypt_block .= '
  551. $r0 += $keys[$r3 & 0x3F];
  552. $r1 += $keys[$r0 & 0x3F];
  553. $r2 += $keys[$r1 & 0x3F];
  554. $r3 += $keys[$r2 & 0x3F];';
  555. $limit = $actions[$limit];
  556. }
  557. }
  558. $encrypt_block .= '$in = pack("v4", $r0, $r1, $r2, $r3);';
  559. // Create code for decryption.
  560. $limit = 44;
  561. $actions = array($limit => 20, 20 => 0);
  562. $j = 64;
  563. for (;;) {
  564. // R-mixing round.
  565. $decrypt_block .= '
  566. $r3 = ($r3 | ($r3 << 16)) >> 5;
  567. $r3 = ($r3 - ' . $keys[--$j] . ' -
  568. ((($r0 ^ $r1) & $r2) ^ $r0)) & 0xFFFF;
  569. $r2 = ($r2 | ($r2 << 16)) >> 3;
  570. $r2 = ($r2 - ' . $keys[--$j] . ' -
  571. ((($r3 ^ $r0) & $r1) ^ $r3)) & 0xFFFF;
  572. $r1 = ($r1 | ($r1 << 16)) >> 2;
  573. $r1 = ($r1 - ' . $keys[--$j] . ' -
  574. ((($r2 ^ $r3) & $r0) ^ $r2)) & 0xFFFF;
  575. $r0 = ($r0 | ($r0 << 16)) >> 1;
  576. $r0 = ($r0 - ' . $keys[--$j] . ' -
  577. ((($r1 ^ $r2) & $r3) ^ $r1)) & 0xFFFF;';
  578. if ($j == $limit) {
  579. if (!$limit) {
  580. break;
  581. }
  582. // R-mashing round.
  583. $decrypt_block .= '
  584. $r3 = ($r3 - $keys[$r2 & 0x3F]) & 0xFFFF;
  585. $r2 = ($r2 - $keys[$r1 & 0x3F]) & 0xFFFF;
  586. $r1 = ($r1 - $keys[$r0 & 0x3F]) & 0xFFFF;
  587. $r0 = ($r0 - $keys[$r3 & 0x3F]) & 0xFFFF;';
  588. $limit = $actions[$limit];
  589. }
  590. }
  591. $decrypt_block .= '$in = pack("v4", $r0, $r1, $r2, $r3);';
  592. // Creates the inline-crypt function
  593. $lambda_functions[$code_hash] = $this->_createInlineCryptFunction(
  594. array(
  595. 'init_crypt' => $init_crypt,
  596. 'encrypt_block' => $encrypt_block,
  597. 'decrypt_block' => $decrypt_block
  598. )
  599. );
  600. }
  601. // Set the inline-crypt function as callback in: $this->inline_crypt
  602. $this->inline_crypt = $lambda_functions[$code_hash];
  603. }
  604. }
  605. // vim: ts=4:sw=4:et:
  606. // vim6: fdl=1: