RC2.php 24 KB

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