Crypt_Rijndael.php 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP implementation of Rijndael.
  5. *
  6. * Does not use mcrypt, even when available, for reasons that are explained below.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * If {@link Crypt_Rijndael::setBlockLength() setBlockLength()} isn't called, it'll be assumed to be 128 bits. If
  11. * {@link Crypt_Rijndael::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
  12. * {@link Crypt_Rijndael::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's
  13. * 136-bits it'll be null-padded to 160-bits and 160 bits will be the key length until
  14. * {@link Crypt_Rijndael::setKey() setKey()} is called, again, at which point, it'll be recalculated.
  15. *
  16. * Not all Rijndael implementations may support 160-bits or 224-bits as the block length / key length. mcrypt, for example,
  17. * does not. AES, itself, only supports block lengths of 128 and key lengths of 128, 192, and 256.
  18. * {@link http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=10 Rijndael-ammended.pdf#page=10} defines the
  19. * algorithm for block lengths of 192 and 256 but not for block lengths / key lengths of 160 and 224. Indeed, 160 and 224
  20. * are first defined as valid key / block lengths in
  21. * {@link http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=44 Rijndael-ammended.pdf#page=44}:
  22. * Extensions: Other block and Cipher Key lengths.
  23. *
  24. * {@internal The variable names are the same as those in
  25. * {@link http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf#page=10 fips-197.pdf#page=10}.}}
  26. *
  27. * Here's a short example of how to use this library:
  28. * <code>
  29. * <?php
  30. * include('Crypt/Rijndael.php');
  31. *
  32. * $rijndael = new Crypt_Rijndael();
  33. *
  34. * $rijndael->setKey('abcdefghijklmnop');
  35. *
  36. * $size = 10 * 1024;
  37. * $plaintext = '';
  38. * for ($i = 0; $i < $size; $i++) {
  39. * $plaintext.= 'a';
  40. * }
  41. *
  42. * echo $rijndael->decrypt($rijndael->encrypt($plaintext));
  43. * ?>
  44. * </code>
  45. *
  46. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  47. * of this software and associated documentation files (the "Software"), to deal
  48. * in the Software without restriction, including without limitation the rights
  49. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  50. * copies of the Software, and to permit persons to whom the Software is
  51. * furnished to do so, subject to the following conditions:
  52. *
  53. * The above copyright notice and this permission notice shall be included in
  54. * all copies or substantial portions of the Software.
  55. *
  56. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  57. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  58. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  59. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  60. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  61. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  62. * THE SOFTWARE.
  63. *
  64. * @category Crypt
  65. * @package Crypt_Rijndael
  66. * @author Jim Wigginton <terrafrost@php.net>
  67. * @copyright MMVIII Jim Wigginton
  68. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  69. * @link http://phpseclib.sourceforge.net
  70. */
  71. /**#@+
  72. * @access public
  73. * @see Crypt_Rijndael::encrypt()
  74. * @see Crypt_Rijndael::decrypt()
  75. */
  76. /**
  77. * Encrypt / decrypt using the Counter mode.
  78. *
  79. * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
  80. *
  81. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
  82. */
  83. define('CRYPT_RIJNDAEL_MODE_CTR', -1);
  84. /**
  85. * Encrypt / decrypt using the Electronic Code Book mode.
  86. *
  87. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
  88. */
  89. define('CRYPT_RIJNDAEL_MODE_ECB', 1);
  90. /**
  91. * Encrypt / decrypt using the Code Book Chaining mode.
  92. *
  93. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
  94. */
  95. define('CRYPT_RIJNDAEL_MODE_CBC', 2);
  96. /**
  97. * Encrypt / decrypt using the Cipher Feedback mode.
  98. *
  99. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
  100. */
  101. define('CRYPT_RIJNDAEL_MODE_CFB', 3);
  102. /**
  103. * Encrypt / decrypt using the Cipher Feedback mode.
  104. *
  105. * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
  106. */
  107. define('CRYPT_RIJNDAEL_MODE_OFB', 4);
  108. /**#@-*/
  109. /**#@+
  110. * @access private
  111. * @see Crypt_Rijndael::Crypt_Rijndael()
  112. */
  113. /**
  114. * Toggles the internal implementation
  115. */
  116. define('CRYPT_RIJNDAEL_MODE_INTERNAL', 1);
  117. /**
  118. * Toggles the mcrypt implementation
  119. */
  120. define('CRYPT_RIJNDAEL_MODE_MCRYPT', 2);
  121. /**#@-*/
  122. /**
  123. * Pure-PHP implementation of Rijndael.
  124. *
  125. * @author Jim Wigginton <terrafrost@php.net>
  126. * @version 0.1.0
  127. * @access public
  128. * @package Crypt_Rijndael
  129. */
  130. class Crypt_Rijndael {
  131. /**
  132. * The Encryption Mode
  133. *
  134. * @see Crypt_Rijndael::Crypt_Rijndael()
  135. * @var Integer
  136. * @access private
  137. */
  138. var $mode;
  139. /**
  140. * The Key
  141. *
  142. * @see Crypt_Rijndael::setKey()
  143. * @var String
  144. * @access private
  145. */
  146. var $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  147. /**
  148. * The Initialization Vector
  149. *
  150. * @see Crypt_Rijndael::setIV()
  151. * @var String
  152. * @access private
  153. */
  154. var $iv = '';
  155. /**
  156. * A "sliding" Initialization Vector
  157. *
  158. * @see Crypt_Rijndael::enableContinuousBuffer()
  159. * @var String
  160. * @access private
  161. */
  162. var $encryptIV = '';
  163. /**
  164. * A "sliding" Initialization Vector
  165. *
  166. * @see Crypt_Rijndael::enableContinuousBuffer()
  167. * @var String
  168. * @access private
  169. */
  170. var $decryptIV = '';
  171. /**
  172. * Continuous Buffer status
  173. *
  174. * @see Crypt_Rijndael::enableContinuousBuffer()
  175. * @var Boolean
  176. * @access private
  177. */
  178. var $continuousBuffer = false;
  179. /**
  180. * Padding status
  181. *
  182. * @see Crypt_Rijndael::enablePadding()
  183. * @var Boolean
  184. * @access private
  185. */
  186. var $padding = true;
  187. /**
  188. * Does the key schedule need to be (re)calculated?
  189. *
  190. * @see setKey()
  191. * @see setBlockLength()
  192. * @see setKeyLength()
  193. * @var Boolean
  194. * @access private
  195. */
  196. var $changed = true;
  197. /**
  198. * Has the key length explicitly been set or should it be derived from the key, itself?
  199. *
  200. * @see setKeyLength()
  201. * @var Boolean
  202. * @access private
  203. */
  204. var $explicit_key_length = false;
  205. /**
  206. * The Key Schedule
  207. *
  208. * @see _setup()
  209. * @var Array
  210. * @access private
  211. */
  212. var $w;
  213. /**
  214. * The Inverse Key Schedule
  215. *
  216. * @see _setup()
  217. * @var Array
  218. * @access private
  219. */
  220. var $dw;
  221. /**
  222. * The Block Length
  223. *
  224. * @see setBlockLength()
  225. * @var Integer
  226. * @access private
  227. * @internal The max value is 32, the min value is 16. All valid values are multiples of 4. Exists in conjunction with
  228. * $Nb because we need this value and not $Nb to pad strings appropriately.
  229. */
  230. var $block_size = 16;
  231. /**
  232. * The Block Length divided by 32
  233. *
  234. * @see setBlockLength()
  235. * @var Integer
  236. * @access private
  237. * @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4. Exists in conjunction with $block_size
  238. * because the encryption / decryption / key schedule creation requires this number and not $block_size. We could
  239. * derive this from $block_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu
  240. * of that, we'll just precompute it once.
  241. *
  242. */
  243. var $Nb = 4;
  244. /**
  245. * The Key Length
  246. *
  247. * @see setKeyLength()
  248. * @var Integer
  249. * @access private
  250. * @internal The max value is 256 / 8 = 32, the min value is 128 / 8 = 16. Exists in conjunction with $key_size
  251. * because the encryption / decryption / key schedule creation requires this number and not $key_size. We could
  252. * derive this from $key_size or vice versa, but that'd mean we'd have to do multiple shift operations, so in lieu
  253. * of that, we'll just precompute it once.
  254. */
  255. var $key_size = 16;
  256. /**
  257. * The Key Length divided by 32
  258. *
  259. * @see setKeyLength()
  260. * @var Integer
  261. * @access private
  262. * @internal The max value is 256 / 32 = 8, the min value is 128 / 32 = 4
  263. */
  264. var $Nk = 4;
  265. /**
  266. * The Number of Rounds
  267. *
  268. * @var Integer
  269. * @access private
  270. * @internal The max value is 14, the min value is 10.
  271. */
  272. var $Nr;
  273. /**
  274. * Shift offsets
  275. *
  276. * @var Array
  277. * @access private
  278. */
  279. var $c;
  280. /**
  281. * Precomputed mixColumns table
  282. *
  283. * @see Crypt_Rijndael()
  284. * @var Array
  285. * @access private
  286. */
  287. var $t0;
  288. /**
  289. * Precomputed mixColumns table
  290. *
  291. * @see Crypt_Rijndael()
  292. * @var Array
  293. * @access private
  294. */
  295. var $t1;
  296. /**
  297. * Precomputed mixColumns table
  298. *
  299. * @see Crypt_Rijndael()
  300. * @var Array
  301. * @access private
  302. */
  303. var $t2;
  304. /**
  305. * Precomputed mixColumns table
  306. *
  307. * @see Crypt_Rijndael()
  308. * @var Array
  309. * @access private
  310. */
  311. var $t3;
  312. /**
  313. * Precomputed invMixColumns table
  314. *
  315. * @see Crypt_Rijndael()
  316. * @var Array
  317. * @access private
  318. */
  319. var $dt0;
  320. /**
  321. * Precomputed invMixColumns table
  322. *
  323. * @see Crypt_Rijndael()
  324. * @var Array
  325. * @access private
  326. */
  327. var $dt1;
  328. /**
  329. * Precomputed invMixColumns table
  330. *
  331. * @see Crypt_Rijndael()
  332. * @var Array
  333. * @access private
  334. */
  335. var $dt2;
  336. /**
  337. * Precomputed invMixColumns table
  338. *
  339. * @see Crypt_Rijndael()
  340. * @var Array
  341. * @access private
  342. */
  343. var $dt3;
  344. /**
  345. * The SubByte S-Box
  346. *
  347. * @see Crypt_Rijndael::_encryptBlock()
  348. * @var Array
  349. * @access private
  350. */
  351. var $sbox;
  352. /**
  353. * The inverse SubByte S-Box
  354. *
  355. * @see Crypt_Rijndael::_decryptBlock()
  356. * @var Array
  357. * @access private
  358. */
  359. var $isbox;
  360. /**
  361. * Performance-optimized callback function for en/decrypt()
  362. *
  363. * @see Crypt_Rijndael::encrypt()
  364. * @see Crypt_Rijndael::decrypt()
  365. * @see Crypt_Rijndael::inline_crypt_setup()
  366. * @see Crypt_Rijndael::$use_inline_crypt
  367. * @var Callback
  368. * @access private
  369. */
  370. var $inline_crypt;
  371. /**
  372. * Holds whether performance-optimized $inline_crypt should be used or not.
  373. *
  374. * @see Crypt_Rijndael::Crypt_Rijndael()
  375. * @see Crypt_Rijndael::inline_crypt_setup()
  376. * @see Crypt_Rijndael::$inline_crypt
  377. * @var Boolean
  378. * @access private
  379. */
  380. var $use_inline_crypt = true;
  381. /**
  382. * Is the mode one that is paddable?
  383. *
  384. * @see Crypt_Rijndael::Crypt_Rijndael()
  385. * @var Boolean
  386. * @access private
  387. */
  388. var $paddable = false;
  389. /**
  390. * Encryption buffer for CTR, OFB and CFB modes
  391. *
  392. * @see Crypt_Rijndael::encrypt()
  393. * @var String
  394. * @access private
  395. */
  396. var $enbuffer = array('encrypted' => '', 'xor' => '', 'pos' => 0);
  397. /**
  398. * Decryption buffer for CTR, OFB and CFB modes
  399. *
  400. * @see Crypt_Rijndael::decrypt()
  401. * @var String
  402. * @access private
  403. */
  404. var $debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0);
  405. /**
  406. * Default Constructor.
  407. *
  408. * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
  409. * CRYPT_RIJNDAEL_MODE_ECB or CRYPT_RIJNDAEL_MODE_CBC. If not explictly set, CRYPT_RIJNDAEL_MODE_CBC will be used.
  410. *
  411. * @param optional Integer $mode
  412. * @return Crypt_Rijndael
  413. * @access public
  414. */
  415. function Crypt_Rijndael($mode = CRYPT_RIJNDAEL_MODE_CBC)
  416. {
  417. switch ($mode) {
  418. case CRYPT_RIJNDAEL_MODE_ECB:
  419. case CRYPT_RIJNDAEL_MODE_CBC:
  420. $this->paddable = true;
  421. $this->mode = $mode;
  422. break;
  423. case CRYPT_RIJNDAEL_MODE_CTR:
  424. case CRYPT_RIJNDAEL_MODE_CFB:
  425. case CRYPT_RIJNDAEL_MODE_OFB:
  426. $this->mode = $mode;
  427. break;
  428. default:
  429. $this->paddable = true;
  430. $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
  431. }
  432. $t3 = &$this->t3;
  433. $t2 = &$this->t2;
  434. $t1 = &$this->t1;
  435. $t0 = &$this->t0;
  436. $dt3 = &$this->dt3;
  437. $dt2 = &$this->dt2;
  438. $dt1 = &$this->dt1;
  439. $dt0 = &$this->dt0;
  440. // according to <http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=19> (section 5.2.1),
  441. // precomputed tables can be used in the mixColumns phase. in that example, they're assigned t0...t3, so
  442. // those are the names we'll use.
  443. $t3 = array(
  444. 0x6363A5C6, 0x7C7C84F8, 0x777799EE, 0x7B7B8DF6, 0xF2F20DFF, 0x6B6BBDD6, 0x6F6FB1DE, 0xC5C55491,
  445. 0x30305060, 0x01010302, 0x6767A9CE, 0x2B2B7D56, 0xFEFE19E7, 0xD7D762B5, 0xABABE64D, 0x76769AEC,
  446. 0xCACA458F, 0x82829D1F, 0xC9C94089, 0x7D7D87FA, 0xFAFA15EF, 0x5959EBB2, 0x4747C98E, 0xF0F00BFB,
  447. 0xADADEC41, 0xD4D467B3, 0xA2A2FD5F, 0xAFAFEA45, 0x9C9CBF23, 0xA4A4F753, 0x727296E4, 0xC0C05B9B,
  448. 0xB7B7C275, 0xFDFD1CE1, 0x9393AE3D, 0x26266A4C, 0x36365A6C, 0x3F3F417E, 0xF7F702F5, 0xCCCC4F83,
  449. 0x34345C68, 0xA5A5F451, 0xE5E534D1, 0xF1F108F9, 0x717193E2, 0xD8D873AB, 0x31315362, 0x15153F2A,
  450. 0x04040C08, 0xC7C75295, 0x23236546, 0xC3C35E9D, 0x18182830, 0x9696A137, 0x05050F0A, 0x9A9AB52F,
  451. 0x0707090E, 0x12123624, 0x80809B1B, 0xE2E23DDF, 0xEBEB26CD, 0x2727694E, 0xB2B2CD7F, 0x75759FEA,
  452. 0x09091B12, 0x83839E1D, 0x2C2C7458, 0x1A1A2E34, 0x1B1B2D36, 0x6E6EB2DC, 0x5A5AEEB4, 0xA0A0FB5B,
  453. 0x5252F6A4, 0x3B3B4D76, 0xD6D661B7, 0xB3B3CE7D, 0x29297B52, 0xE3E33EDD, 0x2F2F715E, 0x84849713,
  454. 0x5353F5A6, 0xD1D168B9, 0x00000000, 0xEDED2CC1, 0x20206040, 0xFCFC1FE3, 0xB1B1C879, 0x5B5BEDB6,
  455. 0x6A6ABED4, 0xCBCB468D, 0xBEBED967, 0x39394B72, 0x4A4ADE94, 0x4C4CD498, 0x5858E8B0, 0xCFCF4A85,
  456. 0xD0D06BBB, 0xEFEF2AC5, 0xAAAAE54F, 0xFBFB16ED, 0x4343C586, 0x4D4DD79A, 0x33335566, 0x85859411,
  457. 0x4545CF8A, 0xF9F910E9, 0x02020604, 0x7F7F81FE, 0x5050F0A0, 0x3C3C4478, 0x9F9FBA25, 0xA8A8E34B,
  458. 0x5151F3A2, 0xA3A3FE5D, 0x4040C080, 0x8F8F8A05, 0x9292AD3F, 0x9D9DBC21, 0x38384870, 0xF5F504F1,
  459. 0xBCBCDF63, 0xB6B6C177, 0xDADA75AF, 0x21216342, 0x10103020, 0xFFFF1AE5, 0xF3F30EFD, 0xD2D26DBF,
  460. 0xCDCD4C81, 0x0C0C1418, 0x13133526, 0xECEC2FC3, 0x5F5FE1BE, 0x9797A235, 0x4444CC88, 0x1717392E,
  461. 0xC4C45793, 0xA7A7F255, 0x7E7E82FC, 0x3D3D477A, 0x6464ACC8, 0x5D5DE7BA, 0x19192B32, 0x737395E6,
  462. 0x6060A0C0, 0x81819819, 0x4F4FD19E, 0xDCDC7FA3, 0x22226644, 0x2A2A7E54, 0x9090AB3B, 0x8888830B,
  463. 0x4646CA8C, 0xEEEE29C7, 0xB8B8D36B, 0x14143C28, 0xDEDE79A7, 0x5E5EE2BC, 0x0B0B1D16, 0xDBDB76AD,
  464. 0xE0E03BDB, 0x32325664, 0x3A3A4E74, 0x0A0A1E14, 0x4949DB92, 0x06060A0C, 0x24246C48, 0x5C5CE4B8,
  465. 0xC2C25D9F, 0xD3D36EBD, 0xACACEF43, 0x6262A6C4, 0x9191A839, 0x9595A431, 0xE4E437D3, 0x79798BF2,
  466. 0xE7E732D5, 0xC8C8438B, 0x3737596E, 0x6D6DB7DA, 0x8D8D8C01, 0xD5D564B1, 0x4E4ED29C, 0xA9A9E049,
  467. 0x6C6CB4D8, 0x5656FAAC, 0xF4F407F3, 0xEAEA25CF, 0x6565AFCA, 0x7A7A8EF4, 0xAEAEE947, 0x08081810,
  468. 0xBABAD56F, 0x787888F0, 0x25256F4A, 0x2E2E725C, 0x1C1C2438, 0xA6A6F157, 0xB4B4C773, 0xC6C65197,
  469. 0xE8E823CB, 0xDDDD7CA1, 0x74749CE8, 0x1F1F213E, 0x4B4BDD96, 0xBDBDDC61, 0x8B8B860D, 0x8A8A850F,
  470. 0x707090E0, 0x3E3E427C, 0xB5B5C471, 0x6666AACC, 0x4848D890, 0x03030506, 0xF6F601F7, 0x0E0E121C,
  471. 0x6161A3C2, 0x35355F6A, 0x5757F9AE, 0xB9B9D069, 0x86869117, 0xC1C15899, 0x1D1D273A, 0x9E9EB927,
  472. 0xE1E138D9, 0xF8F813EB, 0x9898B32B, 0x11113322, 0x6969BBD2, 0xD9D970A9, 0x8E8E8907, 0x9494A733,
  473. 0x9B9BB62D, 0x1E1E223C, 0x87879215, 0xE9E920C9, 0xCECE4987, 0x5555FFAA, 0x28287850, 0xDFDF7AA5,
  474. 0x8C8C8F03, 0xA1A1F859, 0x89898009, 0x0D0D171A, 0xBFBFDA65, 0xE6E631D7, 0x4242C684, 0x6868B8D0,
  475. 0x4141C382, 0x9999B029, 0x2D2D775A, 0x0F0F111E, 0xB0B0CB7B, 0x5454FCA8, 0xBBBBD66D, 0x16163A2C
  476. );
  477. $dt3 = array(
  478. 0xF4A75051, 0x4165537E, 0x17A4C31A, 0x275E963A, 0xAB6BCB3B, 0x9D45F11F, 0xFA58ABAC, 0xE303934B,
  479. 0x30FA5520, 0x766DF6AD, 0xCC769188, 0x024C25F5, 0xE5D7FC4F, 0x2ACBD7C5, 0x35448026, 0x62A38FB5,
  480. 0xB15A49DE, 0xBA1B6725, 0xEA0E9845, 0xFEC0E15D, 0x2F7502C3, 0x4CF01281, 0x4697A38D, 0xD3F9C66B,
  481. 0x8F5FE703, 0x929C9515, 0x6D7AEBBF, 0x5259DA95, 0xBE832DD4, 0x7421D358, 0xE0692949, 0xC9C8448E,
  482. 0xC2896A75, 0x8E7978F4, 0x583E6B99, 0xB971DD27, 0xE14FB6BE, 0x88AD17F0, 0x20AC66C9, 0xCE3AB47D,
  483. 0xDF4A1863, 0x1A3182E5, 0x51336097, 0x537F4562, 0x6477E0B1, 0x6BAE84BB, 0x81A01CFE, 0x082B94F9,
  484. 0x48685870, 0x45FD198F, 0xDE6C8794, 0x7BF8B752, 0x73D323AB, 0x4B02E272, 0x1F8F57E3, 0x55AB2A66,
  485. 0xEB2807B2, 0xB5C2032F, 0xC57B9A86, 0x3708A5D3, 0x2887F230, 0xBFA5B223, 0x036ABA02, 0x16825CED,
  486. 0xCF1C2B8A, 0x79B492A7, 0x07F2F0F3, 0x69E2A14E, 0xDAF4CD65, 0x05BED506, 0x34621FD1, 0xA6FE8AC4,
  487. 0x2E539D34, 0xF355A0A2, 0x8AE13205, 0xF6EB75A4, 0x83EC390B, 0x60EFAA40, 0x719F065E, 0x6E1051BD,
  488. 0x218AF93E, 0xDD063D96, 0x3E05AEDD, 0xE6BD464D, 0x548DB591, 0xC45D0571, 0x06D46F04, 0x5015FF60,
  489. 0x98FB2419, 0xBDE997D6, 0x4043CC89, 0xD99E7767, 0xE842BDB0, 0x898B8807, 0x195B38E7, 0xC8EEDB79,
  490. 0x7C0A47A1, 0x420FE97C, 0x841EC9F8, 0x00000000, 0x80868309, 0x2BED4832, 0x1170AC1E, 0x5A724E6C,
  491. 0x0EFFFBFD, 0x8538560F, 0xAED51E3D, 0x2D392736, 0x0FD9640A, 0x5CA62168, 0x5B54D19B, 0x362E3A24,
  492. 0x0A67B10C, 0x57E70F93, 0xEE96D2B4, 0x9B919E1B, 0xC0C54F80, 0xDC20A261, 0x774B695A, 0x121A161C,
  493. 0x93BA0AE2, 0xA02AE5C0, 0x22E0433C, 0x1B171D12, 0x090D0B0E, 0x8BC7ADF2, 0xB6A8B92D, 0x1EA9C814,
  494. 0xF1198557, 0x75074CAF, 0x99DDBBEE, 0x7F60FDA3, 0x01269FF7, 0x72F5BC5C, 0x663BC544, 0xFB7E345B,
  495. 0x4329768B, 0x23C6DCCB, 0xEDFC68B6, 0xE4F163B8, 0x31DCCAD7, 0x63851042, 0x97224013, 0xC6112084,
  496. 0x4A247D85, 0xBB3DF8D2, 0xF93211AE, 0x29A16DC7, 0x9E2F4B1D, 0xB230F3DC, 0x8652EC0D, 0xC1E3D077,
  497. 0xB3166C2B, 0x70B999A9, 0x9448FA11, 0xE9642247, 0xFC8CC4A8, 0xF03F1AA0, 0x7D2CD856, 0x3390EF22,
  498. 0x494EC787, 0x38D1C1D9, 0xCAA2FE8C, 0xD40B3698, 0xF581CFA6, 0x7ADE28A5, 0xB78E26DA, 0xADBFA43F,
  499. 0x3A9DE42C, 0x78920D50, 0x5FCC9B6A, 0x7E466254, 0x8D13C2F6, 0xD8B8E890, 0x39F75E2E, 0xC3AFF582,
  500. 0x5D80BE9F, 0xD0937C69, 0xD52DA96F, 0x2512B3CF, 0xAC993BC8, 0x187DA710, 0x9C636EE8, 0x3BBB7BDB,
  501. 0x267809CD, 0x5918F46E, 0x9AB701EC, 0x4F9AA883, 0x956E65E6, 0xFFE67EAA, 0xBCCF0821, 0x15E8E6EF,
  502. 0xE79BD9BA, 0x6F36CE4A, 0x9F09D4EA, 0xB07CD629, 0xA4B2AF31, 0x3F23312A, 0xA59430C6, 0xA266C035,
  503. 0x4EBC3774, 0x82CAA6FC, 0x90D0B0E0, 0xA7D81533, 0x04984AF1, 0xECDAF741, 0xCD500E7F, 0x91F62F17,
  504. 0x4DD68D76, 0xEFB04D43, 0xAA4D54CC, 0x9604DFE4, 0xD1B5E39E, 0x6A881B4C, 0x2C1FB8C1, 0x65517F46,
  505. 0x5EEA049D, 0x8C355D01, 0x877473FA, 0x0B412EFB, 0x671D5AB3, 0xDBD25292, 0x105633E9, 0xD647136D,
  506. 0xD7618C9A, 0xA10C7A37, 0xF8148E59, 0x133C89EB, 0xA927EECE, 0x61C935B7, 0x1CE5EDE1, 0x47B13C7A,
  507. 0xD2DF599C, 0xF2733F55, 0x14CE7918, 0xC737BF73, 0xF7CDEA53, 0xFDAA5B5F, 0x3D6F14DF, 0x44DB8678,
  508. 0xAFF381CA, 0x68C43EB9, 0x24342C38, 0xA3405FC2, 0x1DC37216, 0xE2250CBC, 0x3C498B28, 0x0D9541FF,
  509. 0xA8017139, 0x0CB3DE08, 0xB4E49CD8, 0x56C19064, 0xCB84617B, 0x32B670D5, 0x6C5C7448, 0xB85742D0
  510. );
  511. for ($i = 0; $i < 256; $i++) {
  512. $t2[] = (($t3[$i] << 8) & 0xFFFFFF00) | (($t3[$i] >> 24) & 0x000000FF);
  513. $t1[] = (($t3[$i] << 16) & 0xFFFF0000) | (($t3[$i] >> 16) & 0x0000FFFF);
  514. $t0[] = (($t3[$i] << 24) & 0xFF000000) | (($t3[$i] >> 8) & 0x00FFFFFF);
  515. $dt2[] = (($dt3[$i] << 8) & 0xFFFFFF00) | (($dt3[$i] >> 24) & 0x000000FF);
  516. $dt1[] = (($dt3[$i] << 16) & 0xFFFF0000) | (($dt3[$i] >> 16) & 0x0000FFFF);
  517. $dt0[] = (($dt3[$i] << 24) & 0xFF000000) | (($dt3[$i] >> 8) & 0x00FFFFFF);
  518. }
  519. // sbox for the S-Box substitution
  520. $this->sbox = array(
  521. 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
  522. 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
  523. 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  524. 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
  525. 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
  526. 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  527. 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
  528. 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  529. 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  530. 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
  531. 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
  532. 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  533. 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
  534. 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
  535. 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  536. 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
  537. );
  538. // sbox for the inverse S-Box substitution
  539. $this->isbox = array(
  540. 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
  541. 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
  542. 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
  543. 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
  544. 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
  545. 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
  546. 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
  547. 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
  548. 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
  549. 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
  550. 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
  551. 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
  552. 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
  553. 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
  554. 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
  555. 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
  556. );
  557. if (!function_exists('create_function') || !is_callable('create_function')) {
  558. $this->use_inline_crypt = false;
  559. }
  560. }
  561. /**
  562. * Sets the key.
  563. *
  564. * Keys can be of any length. Rijndael, itself, requires the use of a key that's between 128-bits and 256-bits long and
  565. * whose length is a multiple of 32. If the key is less than 256-bits and the key length isn't set, we round the length
  566. * up to the closest valid key length, padding $key with null bytes. If the key is more than 256-bits, we trim the
  567. * excess bits.
  568. *
  569. * If the key is not explicitly set, it'll be assumed to be all null bytes.
  570. *
  571. * @access public
  572. * @param String $key
  573. */
  574. function setKey($key)
  575. {
  576. $this->key = $key;
  577. $this->changed = true;
  578. }
  579. /**
  580. * Sets the initialization vector. (optional)
  581. *
  582. * SetIV is not required when CRYPT_RIJNDAEL_MODE_ECB is being used. If not explictly set, it'll be assumed
  583. * to be all zero's.
  584. *
  585. * @access public
  586. * @param String $iv
  587. */
  588. function setIV($iv)
  589. {
  590. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, $this->block_size), $this->block_size, chr(0));
  591. }
  592. /**
  593. * Sets the key length
  594. *
  595. * Valid key lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to
  596. * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
  597. *
  598. * @access public
  599. * @param Integer $length
  600. */
  601. function setKeyLength($length)
  602. {
  603. $length >>= 5;
  604. if ($length > 8) {
  605. $length = 8;
  606. } else if ($length < 4) {
  607. $length = 4;
  608. }
  609. $this->Nk = $length;
  610. $this->key_size = $length << 2;
  611. $this->explicit_key_length = true;
  612. $this->changed = true;
  613. }
  614. /**
  615. * Sets the password.
  616. *
  617. * Depending on what $method is set to, setPassword()'s (optional) parameters are as follows:
  618. * {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2}:
  619. * $hash, $salt, $method
  620. * Set $dkLen by calling setKeyLength()
  621. *
  622. * @param String $password
  623. * @param optional String $method
  624. * @access public
  625. */
  626. function setPassword($password, $method = 'pbkdf2')
  627. {
  628. $key = '';
  629. switch ($method) {
  630. default: // 'pbkdf2'
  631. list(, , $hash, $salt, $count) = func_get_args();
  632. if (!isset($hash)) {
  633. $hash = 'sha1';
  634. }
  635. // WPA and WPA2 use the SSID as the salt
  636. if (!isset($salt)) {
  637. $salt = 'phpseclib';
  638. }
  639. // RFC2898#section-4.2 uses 1,000 iterations by default
  640. // WPA and WPA2 use 4,096.
  641. if (!isset($count)) {
  642. $count = 1000;
  643. }
  644. if (!class_exists('Crypt_Hash')) {
  645. require_once('Crypt/Hash.php');
  646. }
  647. $i = 1;
  648. while (strlen($key) < $this->key_size) { // $dkLen == $this->key_size
  649. //$dk.= $this->_pbkdf($password, $salt, $count, $i++);
  650. $hmac = new Crypt_Hash();
  651. $hmac->setHash($hash);
  652. $hmac->setKey($password);
  653. $f = $u = $hmac->hash($salt . pack('N', $i++));
  654. for ($j = 2; $j <= $count; $j++) {
  655. $u = $hmac->hash($u);
  656. $f^= $u;
  657. }
  658. $key.= $f;
  659. }
  660. }
  661. $this->setKey(substr($key, 0, $this->key_size));
  662. }
  663. /**
  664. * Sets the block length
  665. *
  666. * Valid block lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to
  667. * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
  668. *
  669. * @access public
  670. * @param Integer $length
  671. */
  672. function setBlockLength($length)
  673. {
  674. $length >>= 5;
  675. if ($length > 8) {
  676. $length = 8;
  677. } else if ($length < 4) {
  678. $length = 4;
  679. }
  680. $this->Nb = $length;
  681. $this->block_size = $length << 2;
  682. $this->changed = true;
  683. }
  684. /**
  685. * Generate CTR XOR encryption key
  686. *
  687. * Encrypt the output of this and XOR it against the ciphertext / plaintext to get the
  688. * plaintext / ciphertext in CTR mode.
  689. *
  690. * @see Crypt_Rijndael::decrypt()
  691. * @see Crypt_Rijndael::encrypt()
  692. * @access public
  693. * @param Integer $length
  694. * @param String $iv
  695. */
  696. function _generate_xor($length, &$iv)
  697. {
  698. $xor = '';
  699. $block_size = $this->block_size;
  700. $num_blocks = floor(($length + ($block_size - 1)) / $block_size);
  701. for ($i = 0; $i < $num_blocks; $i++) {
  702. $xor.= $iv;
  703. for ($j = 4; $j <= $block_size; $j+=4) {
  704. $temp = substr($iv, -$j, 4);
  705. switch ($temp) {
  706. case "\xFF\xFF\xFF\xFF":
  707. $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);
  708. break;
  709. case "\x7F\xFF\xFF\xFF":
  710. $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);
  711. break 2;
  712. default:
  713. // Edity by yeepayMPay
  714. // extract(unpack('Ncount', $temp));
  715. // $iv = substr_replace($iv, pack('N', $count + 1), -$j, 4);
  716. $_unpacked = unpack('Ncount', $temp);
  717. $iv = substr_replace($iv, pack('N', $_unpacked['count'] + 1), -$j, 4);
  718. break 2;
  719. }
  720. }
  721. }
  722. return $xor;
  723. }
  724. /**
  725. * Encrypts a message.
  726. *
  727. * $plaintext will be padded with additional bytes such that it's length is a multiple of the block size. Other Rjindael
  728. * implementations may or may not pad in the same manner. Other common approaches to padding and the reasons why it's
  729. * necessary are discussed in the following
  730. * URL:
  731. *
  732. * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
  733. *
  734. * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
  735. * strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that
  736. * length.
  737. *
  738. * @see Crypt_Rijndael::decrypt()
  739. * @access public
  740. * @param String $plaintext
  741. */
  742. function encrypt($plaintext)
  743. {
  744. if ($this->changed) {
  745. $this->_setup();
  746. }
  747. if ($this->use_inline_crypt) {
  748. $inline = $this->inline_crypt;
  749. return $inline('encrypt', $this, $plaintext);
  750. }
  751. if ($this->paddable) {
  752. $plaintext = $this->_pad($plaintext);
  753. }
  754. $block_size = $this->block_size;
  755. $buffer = &$this->enbuffer;
  756. $ciphertext = '';
  757. switch ($this->mode) {
  758. case CRYPT_RIJNDAEL_MODE_ECB:
  759. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  760. $ciphertext.= $this->_encryptBlock(substr($plaintext, $i, $block_size));
  761. }
  762. break;
  763. case CRYPT_RIJNDAEL_MODE_CBC:
  764. $xor = $this->encryptIV;
  765. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  766. $block = substr($plaintext, $i, $block_size);
  767. $block = $this->_encryptBlock($block ^ $xor);
  768. $xor = $block;
  769. $ciphertext.= $block;
  770. }
  771. if ($this->continuousBuffer) {
  772. $this->encryptIV = $xor;
  773. }
  774. break;
  775. case CRYPT_RIJNDAEL_MODE_CTR:
  776. $xor = $this->encryptIV;
  777. if (strlen($buffer['encrypted'])) {
  778. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  779. $block = substr($plaintext, $i, $block_size);
  780. if (strlen($block) > strlen($buffer['encrypted'])) {
  781. $buffer['encrypted'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
  782. }
  783. $key = $this->_string_shift($buffer['encrypted'], $block_size);
  784. $ciphertext.= $block ^ $key;
  785. }
  786. } else {
  787. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  788. $block = substr($plaintext, $i, $block_size);
  789. $key = $this->_encryptBlock($this->_generate_xor($block_size, $xor));
  790. $ciphertext.= $block ^ $key;
  791. }
  792. }
  793. if ($this->continuousBuffer) {
  794. $this->encryptIV = $xor;
  795. // Edity By yeepayMPay
  796. // if ($start = strlen($plaintext) % $block_size) {
  797. $start = strlen($plaintext) % $block_size;
  798. if ($start) {
  799. $buffer['encrypted'] = substr($key, $start) . $buffer['encrypted'];
  800. }
  801. }
  802. break;
  803. case CRYPT_RIJNDAEL_MODE_CFB:
  804. // cfb loosely routines inspired by openssl's:
  805. // http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1
  806. if ($this->continuousBuffer) {
  807. $iv = &$this->encryptIV;
  808. $pos = &$buffer['pos'];
  809. } else {
  810. $iv = $this->encryptIV;
  811. $pos = 0;
  812. }
  813. $len = strlen($plaintext);
  814. $i = 0;
  815. if ($pos) {
  816. $orig_pos = $pos;
  817. $max = $block_size - $pos;
  818. if ($len >= $max) {
  819. $i = $max;
  820. $len-= $max;
  821. $pos = 0;
  822. } else {
  823. $i = $len;
  824. $pos+= $len;
  825. $len = 0;
  826. }
  827. // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
  828. $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
  829. $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
  830. }
  831. while ($len >= $block_size) {
  832. $iv = $this->_encryptBlock($iv) ^ substr($plaintext, $i, $block_size);
  833. $ciphertext.= $iv;
  834. $len-= $block_size;
  835. $i+= $block_size;
  836. }
  837. if ($len) {
  838. $iv = $this->_encryptBlock($iv);
  839. $block = $iv ^ substr($plaintext, $i);
  840. $iv = substr_replace($iv, $block, 0, $len);
  841. $ciphertext.= $block;
  842. $pos = $len;
  843. }
  844. break;
  845. case CRYPT_RIJNDAEL_MODE_OFB:
  846. $xor = $this->encryptIV;
  847. if (strlen($buffer['xor'])) {
  848. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  849. $block = substr($plaintext, $i, $block_size);
  850. if (strlen($block) > strlen($buffer['xor'])) {
  851. $xor = $this->_encryptBlock($xor);
  852. $buffer['xor'].= $xor;
  853. }
  854. $key = $this->_string_shift($buffer['xor'], $block_size);
  855. $ciphertext.= $block ^ $key;
  856. }
  857. } else {
  858. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  859. $xor = $this->_encryptBlock($xor);
  860. $ciphertext.= substr($plaintext, $i, $block_size) ^ $xor;
  861. }
  862. $key = $xor;
  863. }
  864. if ($this->continuousBuffer) {
  865. $this->encryptIV = $xor;
  866. // Edit By yeepayMPay
  867. // if ($start = strlen($plaintext) % $block_size) {
  868. $start = strlen($plaintext) % $block_size;
  869. if ($start) {
  870. $buffer['xor'] = substr($key, $start) . $buffer['xor'];
  871. }
  872. }
  873. }
  874. return $ciphertext;
  875. }
  876. /**
  877. * Decrypts a message.
  878. *
  879. * If strlen($ciphertext) is not a multiple of the block size, null bytes will be added to the end of the string until
  880. * it is.
  881. *
  882. * @see Crypt_Rijndael::encrypt()
  883. * @access public
  884. * @param String $ciphertext
  885. */
  886. function decrypt($ciphertext)
  887. {
  888. if ($this->changed) {
  889. $this->_setup();
  890. }
  891. if ($this->use_inline_crypt) {
  892. $inline = $this->inline_crypt;
  893. return $inline('decrypt', $this, $ciphertext);
  894. }
  895. if ($this->paddable) {
  896. // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
  897. // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
  898. $ciphertext = str_pad($ciphertext, strlen($ciphertext) + ($this->block_size - strlen($ciphertext) % $this->block_size) % $this->block_size, chr(0));
  899. }
  900. $block_size = $this->block_size;
  901. $buffer = &$this->debuffer;
  902. $plaintext = '';
  903. switch ($this->mode) {
  904. case CRYPT_RIJNDAEL_MODE_ECB:
  905. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  906. $plaintext.= $this->_decryptBlock(substr($ciphertext, $i, $block_size));
  907. }
  908. break;
  909. case CRYPT_RIJNDAEL_MODE_CBC:
  910. $xor = $this->decryptIV;
  911. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  912. $block = substr($ciphertext, $i, $block_size);
  913. $plaintext.= $this->_decryptBlock($block) ^ $xor;
  914. $xor = $block;
  915. }
  916. if ($this->continuousBuffer) {
  917. $this->decryptIV = $xor;
  918. }
  919. break;
  920. case CRYPT_RIJNDAEL_MODE_CTR:
  921. $xor = $this->decryptIV;
  922. if (strlen($buffer['ciphertext'])) {
  923. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  924. $block = substr($ciphertext, $i, $block_size);
  925. if (strlen($block) > strlen($buffer['ciphertext'])) {
  926. $buffer['ciphertext'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
  927. }
  928. $key = $this->_string_shift($buffer['ciphertext'], $block_size);
  929. $plaintext.= $block ^ $key;
  930. }
  931. } else {
  932. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  933. $block = substr($ciphertext, $i, $block_size);
  934. $key = $this->_encryptBlock($this->_generate_xor($block_size, $xor));
  935. $plaintext.= $block ^ $key;
  936. }
  937. }
  938. if ($this->continuousBuffer) {
  939. $this->decryptIV = $xor;
  940. // edity By yeepayMPay
  941. // if ($start = strlen($ciphertext) % $block_size) {
  942. $start = strlen($ciphertext) % $block_size;
  943. if ($start) {
  944. $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
  945. }
  946. }
  947. break;
  948. case CRYPT_RIJNDAEL_MODE_CFB:
  949. if ($this->continuousBuffer) {
  950. $iv = &$this->decryptIV;
  951. $pos = &$buffer['pos'];
  952. } else {
  953. $iv = $this->decryptIV;
  954. $pos = 0;
  955. }
  956. $len = strlen($ciphertext);
  957. $i = 0;
  958. if ($pos) {
  959. $orig_pos = $pos;
  960. $max = $block_size - $pos;
  961. if ($len >= $max) {
  962. $i = $max;
  963. $len-= $max;
  964. $pos = 0;
  965. } else {
  966. $i = $len;
  967. $pos+= $len;
  968. $len = 0;
  969. }
  970. // ie. $i = min($max, $len), $len-= $i, $pos+= $i, $pos%= $blocksize
  971. $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
  972. $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
  973. }
  974. while ($len >= $block_size) {
  975. $iv = $this->_encryptBlock($iv);
  976. $cb = substr($ciphertext, $i, $block_size);
  977. $plaintext.= $iv ^ $cb;
  978. $iv = $cb;
  979. $len-= $block_size;
  980. $i+= $block_size;
  981. }
  982. if ($len) {
  983. $iv = $this->_encryptBlock($iv);
  984. $plaintext.= $iv ^ substr($ciphertext, $i);
  985. $iv = substr_replace($iv, substr($ciphertext, $i), 0, $len);
  986. $pos = $len;
  987. }
  988. break;
  989. case CRYPT_RIJNDAEL_MODE_OFB:
  990. $xor = $this->decryptIV;
  991. if (strlen($buffer['xor'])) {
  992. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  993. $block = substr($ciphertext, $i, $block_size);
  994. if (strlen($block) > strlen($buffer['xor'])) {
  995. $xor = $this->_encryptBlock($xor);
  996. $buffer['xor'].= $xor;
  997. }
  998. $key = $this->_string_shift($buffer['xor'], $block_size);
  999. $plaintext.= $block ^ $key;
  1000. }
  1001. } else {
  1002. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  1003. $xor = $this->_encryptBlock($xor);
  1004. $plaintext.= substr($ciphertext, $i, $block_size) ^ $xor;
  1005. }
  1006. $key = $xor;
  1007. }
  1008. if ($this->continuousBuffer) {
  1009. $this->decryptIV = $xor;
  1010. // Edity yeepayMPay
  1011. // if ($start = strlen($ciphertext) % $block_size) {
  1012. $start = strlen($ciphertext) % $block_size;
  1013. if ($start) {
  1014. $buffer['xor'] = substr($key, $start) . $buffer['xor'];
  1015. }
  1016. }
  1017. }
  1018. return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
  1019. }
  1020. /**
  1021. * Encrypts a block
  1022. *
  1023. * @access private
  1024. * @param String $in
  1025. * @return String
  1026. */
  1027. function _encryptBlock($in)
  1028. {
  1029. $state = array();
  1030. $words = unpack('N*word', $in);
  1031. $w = $this->w;
  1032. $t0 = $this->t0;
  1033. $t1 = $this->t1;
  1034. $t2 = $this->t2;
  1035. $t3 = $this->t3;
  1036. $Nb = $this->Nb;
  1037. $Nr = $this->Nr;
  1038. $c = $this->c;
  1039. // addRoundKey
  1040. $i = -1;
  1041. foreach ($words as $word) {
  1042. $state[] = $word ^ $w[0][++$i];
  1043. }
  1044. // fips-197.pdf#page=19, "Figure 5. Pseudo Code for the Cipher", states that this loop has four components -
  1045. // subBytes, shiftRows, mixColumns, and addRoundKey. fips-197.pdf#page=30, "Implementation Suggestions Regarding
  1046. // Various Platforms" suggests that performs enhanced implementations are described in Rijndael-ammended.pdf.
  1047. // Rijndael-ammended.pdf#page=20, "Implementation aspects / 32-bit processor", discusses such an optimization.
  1048. // Unfortunately, the description given there is not quite correct. Per aes.spec.v316.pdf#page=19 [1],
  1049. // equation (7.4.7) is supposed to use addition instead of subtraction, so we'll do that here, as well.
  1050. // [1] http://fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.v316.pdf
  1051. $temp = array();
  1052. for ($round = 1; $round < $Nr; ++$round) {
  1053. $i = 0; // $c[0] == 0
  1054. $j = $c[1];
  1055. $k = $c[2];
  1056. $l = $c[3];
  1057. while ($i < $Nb) {
  1058. $temp[$i] = $t0[$state[$i] >> 24 & 0x000000FF] ^
  1059. $t1[$state[$j] >> 16 & 0x000000FF] ^
  1060. $t2[$state[$k] >> 8 & 0x000000FF] ^
  1061. $t3[$state[$l] & 0x000000FF] ^
  1062. $w[$round][$i];
  1063. ++$i;
  1064. $j = ($j + 1) % $Nb;
  1065. $k = ($k + 1) % $Nb;
  1066. $l = ($l + 1) % $Nb;
  1067. }
  1068. $state = $temp;
  1069. }
  1070. // subWord
  1071. for ($i = 0; $i < $Nb; ++$i) {
  1072. $state[$i] = $this->_subWord($state[$i]);
  1073. }
  1074. // shiftRows + addRoundKey
  1075. $i = 0; // $c[0] == 0
  1076. $j = $c[1];
  1077. $k = $c[2];
  1078. $l = $c[3];
  1079. while ($i < $Nb) {
  1080. $temp[$i] = ($state[$i] & 0xFF000000) ^
  1081. ($state[$j] & 0x00FF0000) ^
  1082. ($state[$k] & 0x0000FF00) ^
  1083. ($state[$l] & 0x000000FF) ^
  1084. $w[$Nr][$i];
  1085. ++$i;
  1086. $j = ($j + 1) % $Nb;
  1087. $k = ($k + 1) % $Nb;
  1088. $l = ($l + 1) % $Nb;
  1089. }
  1090. // 100% ugly switch/case code... but ~5% faster ("smart code" below commented out)
  1091. switch ($Nb) {
  1092. case 8:
  1093. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6], $temp[7]);
  1094. case 7:
  1095. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6]);
  1096. case 6:
  1097. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5]);
  1098. case 5:
  1099. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4]);
  1100. default:
  1101. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3]);
  1102. }
  1103. /*
  1104. $state = $temp;
  1105. array_unshift($state, 'N*');
  1106. return call_user_func_array('pack', $state);
  1107. */
  1108. }
  1109. /**
  1110. * Decrypts a block
  1111. *
  1112. * @access private
  1113. * @param String $in
  1114. * @return String
  1115. */
  1116. function _decryptBlock($in)
  1117. {
  1118. $state = array();
  1119. $words = unpack('N*word', $in);
  1120. $dw = $this->dw;
  1121. $dt0 = $this->dt0;
  1122. $dt1 = $this->dt1;
  1123. $dt2 = $this->dt2;
  1124. $dt3 = $this->dt3;
  1125. $Nb = $this->Nb;
  1126. $Nr = $this->Nr;
  1127. $c = $this->c;
  1128. // addRoundKey
  1129. $i = -1;
  1130. foreach ($words as $word) {
  1131. $state[] = $word ^ $dw[$Nr][++$i];
  1132. }
  1133. $temp = array();
  1134. for ($round = $Nr - 1; $round > 0; --$round) {
  1135. $i = 0; // $c[0] == 0
  1136. $j = $Nb - $c[1];
  1137. $k = $Nb - $c[2];
  1138. $l = $Nb - $c[3];
  1139. while ($i < $Nb) {
  1140. $temp[$i] = $dt0[$state[$i] >> 24 & 0x000000FF] ^
  1141. $dt1[$state[$j] >> 16 & 0x000000FF] ^
  1142. $dt2[$state[$k] >> 8 & 0x000000FF] ^
  1143. $dt3[$state[$l] & 0x000000FF] ^
  1144. $dw[$round][$i];
  1145. ++$i;
  1146. $j = ($j + 1) % $Nb;
  1147. $k = ($k + 1) % $Nb;
  1148. $l = ($l + 1) % $Nb;
  1149. }
  1150. $state = $temp;
  1151. }
  1152. // invShiftRows + invSubWord + addRoundKey
  1153. $i = 0; // $c[0] == 0
  1154. $j = $Nb - $c[1];
  1155. $k = $Nb - $c[2];
  1156. $l = $Nb - $c[3];
  1157. while ($i < $Nb) {
  1158. $temp[$i] = $dw[0][$i] ^
  1159. $this->_invSubWord(($state[$i] & 0xFF000000) |
  1160. ($state[$j] & 0x00FF0000) |
  1161. ($state[$k] & 0x0000FF00) |
  1162. ($state[$l] & 0x000000FF));
  1163. ++$i;
  1164. $j = ($j + 1) % $Nb;
  1165. $k = ($k + 1) % $Nb;
  1166. $l = ($l + 1) % $Nb;
  1167. }
  1168. switch ($Nb) {
  1169. case 8:
  1170. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6], $temp[7]);
  1171. case 7:
  1172. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5], $temp[6]);
  1173. case 6:
  1174. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4], $temp[5]);
  1175. case 5:
  1176. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3], $temp[4]);
  1177. default:
  1178. return pack('N*', $temp[0], $temp[1], $temp[2], $temp[3]);
  1179. }
  1180. /*
  1181. $state = $temp;
  1182. array_unshift($state, 'N*');
  1183. return call_user_func_array('pack', $state);
  1184. */
  1185. }
  1186. /**
  1187. * Setup Rijndael
  1188. *
  1189. * Validates all the variables and calculates $Nr - the number of rounds that need to be performed - and $w - the key
  1190. * key schedule.
  1191. *
  1192. * @access private
  1193. */
  1194. function _setup()
  1195. {
  1196. // Each number in $rcon is equal to the previous number multiplied by two in Rijndael's finite field.
  1197. // See http://en.wikipedia.org/wiki/Finite_field_arithmetic#Multiplicative_inverse
  1198. static $rcon = array(0,
  1199. 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
  1200. 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000,
  1201. 0x6C000000, 0xD8000000, 0xAB000000, 0x4D000000, 0x9A000000,
  1202. 0x2F000000, 0x5E000000, 0xBC000000, 0x63000000, 0xC6000000,
  1203. 0x97000000, 0x35000000, 0x6A000000, 0xD4000000, 0xB3000000,
  1204. 0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000
  1205. );
  1206. if (!$this->explicit_key_length) {
  1207. // we do >> 2, here, and not >> 5, as we do above, since strlen($this->key) tells us the number of bytes - not bits
  1208. $length = strlen($this->key) >> 2;
  1209. if ($length > 8) {
  1210. $length = 8;
  1211. } else if ($length < 4) {
  1212. $length = 4;
  1213. }
  1214. $this->Nk = $length;
  1215. $this->key_size = $length << 2;
  1216. }
  1217. $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, chr(0));
  1218. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, $this->block_size), $this->block_size, chr(0));
  1219. // see Rijndael-ammended.pdf#page=44
  1220. $this->Nr = max($this->Nk, $this->Nb) + 6;
  1221. // shift offsets for Nb = 5, 7 are defined in Rijndael-ammended.pdf#page=44,
  1222. // "Table 8: Shift offsets in Shiftrow for the alternative block lengths"
  1223. // shift offsets for Nb = 4, 6, 8 are defined in Rijndael-ammended.pdf#page=14,
  1224. // "Table 2: Shift offsets for different block lengths"
  1225. switch ($this->Nb) {
  1226. case 4:
  1227. case 5:
  1228. case 6:
  1229. $this->c = array(0, 1, 2, 3);
  1230. break;
  1231. case 7:
  1232. $this->c = array(0, 1, 2, 4);
  1233. break;
  1234. case 8:
  1235. $this->c = array(0, 1, 3, 4);
  1236. }
  1237. $key = $this->key;
  1238. $w = array_values(unpack('N*words', $key));
  1239. $length = $this->Nb * ($this->Nr + 1);
  1240. for ($i = $this->Nk; $i < $length; $i++) {
  1241. $temp = $w[$i - 1];
  1242. if ($i % $this->Nk == 0) {
  1243. // according to <http://php.net/language.types.integer>, "the size of an integer is platform-dependent".
  1244. // on a 32-bit machine, it's 32-bits, and on a 64-bit machine, it's 64-bits. on a 32-bit machine,
  1245. // 0xFFFFFFFF << 8 == 0xFFFFFF00, but on a 64-bit machine, it equals 0xFFFFFFFF00. as such, doing 'and'
  1246. // with 0xFFFFFFFF (or 0xFFFFFF00) on a 32-bit machine is unnecessary, but on a 64-bit machine, it is.
  1247. $temp = (($temp << 8) & 0xFFFFFF00) | (($temp >> 24) & 0x000000FF); // rotWord
  1248. $temp = $this->_subWord($temp) ^ $rcon[$i / $this->Nk];
  1249. } else if ($this->Nk > 6 && $i % $this->Nk == 4) {
  1250. $temp = $this->_subWord($temp);
  1251. }
  1252. $w[$i] = $w[$i - $this->Nk] ^ $temp;
  1253. }
  1254. // convert the key schedule from a vector of $Nb * ($Nr + 1) length to a matrix with $Nr + 1 rows and $Nb columns
  1255. // and generate the inverse key schedule. more specifically,
  1256. // according to <http://csrc.nist.gov/archive/aes/rijndael/Rijndael-ammended.pdf#page=23> (section 5.3.3),
  1257. // "The key expansion for the Inverse Cipher is defined as follows:
  1258. // 1. Apply the Key Expansion.
  1259. // 2. Apply InvMixColumn to all Round Keys except the first and the last one."
  1260. // also, see fips-197.pdf#page=27, "5.3.5 Equivalent Inverse Cipher"
  1261. $temp = $this->w = $this->dw = array();
  1262. for ($i = $row = $col = 0; $i < $length; $i++, $col++) {
  1263. if ($col == $this->Nb) {
  1264. if ($row == 0) {
  1265. $this->dw[0] = $this->w[0];
  1266. } else {
  1267. // subWord + invMixColumn + invSubWord = invMixColumn
  1268. $j = 0;
  1269. while ($j < $this->Nb) {
  1270. $dw = $this->_subWord($this->w[$row][$j]);
  1271. $temp[$j] = $this->dt0[$dw >> 24 & 0x000000FF] ^
  1272. $this->dt1[$dw >> 16 & 0x000000FF] ^
  1273. $this->dt2[$dw >> 8 & 0x000000FF] ^
  1274. $this->dt3[$dw & 0x000000FF];
  1275. $j++;
  1276. }
  1277. $this->dw[$row] = $temp;
  1278. }
  1279. $col = 0;
  1280. $row++;
  1281. }
  1282. $this->w[$row][$col] = $w[$i];
  1283. }
  1284. $this->dw[$row] = $this->w[$row];
  1285. // In case of $this->use_inline_crypt === true we have to use 1-dim key arrays (both ascending)
  1286. if ($this->use_inline_crypt) {
  1287. $this->dw = array_reverse($this->dw);
  1288. $w = array_pop($this->w);
  1289. $dw = array_pop($this->dw);
  1290. foreach ($this->w as $r => $wr) {
  1291. foreach ($wr as $c => $wc) {
  1292. $w[] = $wc;
  1293. $dw[] = $this->dw[$r][$c];
  1294. }
  1295. }
  1296. $this->w = $w;
  1297. $this->dw = $dw;
  1298. $this->inline_crypt_setup();
  1299. }
  1300. $this->changed = false;
  1301. }
  1302. /**
  1303. * Performs S-Box substitutions
  1304. *
  1305. * @access private
  1306. */
  1307. function _subWord($word)
  1308. {
  1309. $sbox = $this->sbox;
  1310. return $sbox[$word & 0x000000FF] |
  1311. ($sbox[$word >> 8 & 0x000000FF] << 8) |
  1312. ($sbox[$word >> 16 & 0x000000FF] << 16) |
  1313. ($sbox[$word >> 24 & 0x000000FF] << 24);
  1314. }
  1315. /**
  1316. * Performs inverse S-Box substitutions
  1317. *
  1318. * @access private
  1319. */
  1320. function _invSubWord($word)
  1321. {
  1322. $isbox = $this->isbox;
  1323. return $isbox[$word & 0x000000FF] |
  1324. ($isbox[$word >> 8 & 0x000000FF] << 8) |
  1325. ($isbox[$word >> 16 & 0x000000FF] << 16) |
  1326. ($isbox[$word >> 24 & 0x000000FF] << 24);
  1327. }
  1328. /**
  1329. * Pad "packets".
  1330. *
  1331. * Rijndael works by encrypting between sixteen and thirty-two bytes at a time, provided that number is also a multiple
  1332. * of four. If you ever need to encrypt or decrypt something that isn't of the proper length, it becomes necessary to
  1333. * pad the input so that it is of the proper length.
  1334. *
  1335. * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH,
  1336. * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
  1337. * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
  1338. * transmitted separately)
  1339. *
  1340. * @see Crypt_Rijndael::disablePadding()
  1341. * @access public
  1342. */
  1343. function enablePadding()
  1344. {
  1345. $this->padding = true;
  1346. }
  1347. /**
  1348. * Do not pad packets.
  1349. *
  1350. * @see Crypt_Rijndael::enablePadding()
  1351. * @access public
  1352. */
  1353. function disablePadding()
  1354. {
  1355. $this->padding = false;
  1356. }
  1357. /**
  1358. * Pads a string
  1359. *
  1360. * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize.
  1361. * $block_size - (strlen($text) % $block_size) bytes are added, each of which is equal to
  1362. * chr($block_size - (strlen($text) % $block_size)
  1363. *
  1364. * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
  1365. * and padding will, hence forth, be enabled.
  1366. *
  1367. * @see Crypt_Rijndael::_unpad()
  1368. * @access private
  1369. */
  1370. function _pad($text)
  1371. {
  1372. $length = strlen($text);
  1373. if (!$this->padding) {
  1374. if ($length % $this->block_size == 0) {
  1375. return $text;
  1376. } else {
  1377. user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})");
  1378. $this->padding = true;
  1379. }
  1380. }
  1381. $pad = $this->block_size - ($length % $this->block_size);
  1382. return str_pad($text, $length + $pad, chr($pad));
  1383. }
  1384. /**
  1385. * Unpads a string.
  1386. *
  1387. * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
  1388. * and false will be returned.
  1389. *
  1390. * @see Crypt_Rijndael::_pad()
  1391. * @access private
  1392. */
  1393. function _unpad($text)
  1394. {
  1395. if (!$this->padding) {
  1396. return $text;
  1397. }
  1398. $length = ord($text[strlen($text) - 1]);
  1399. if (!$length || $length > $this->block_size) {
  1400. return false;
  1401. }
  1402. return substr($text, 0, -$length);
  1403. }
  1404. /**
  1405. * Treat consecutive "packets" as if they are a continuous buffer.
  1406. *
  1407. * Say you have a 32-byte plaintext $plaintext. Using the default behavior, the two following code snippets
  1408. * will yield different outputs:
  1409. *
  1410. * <code>
  1411. * echo $rijndael->encrypt(substr($plaintext, 0, 16));
  1412. * echo $rijndael->encrypt(substr($plaintext, 16, 16));
  1413. * </code>
  1414. * <code>
  1415. * echo $rijndael->encrypt($plaintext);
  1416. * </code>
  1417. *
  1418. * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
  1419. * another, as demonstrated with the following:
  1420. *
  1421. * <code>
  1422. * $rijndael->encrypt(substr($plaintext, 0, 16));
  1423. * echo $rijndael->decrypt($des->encrypt(substr($plaintext, 16, 16)));
  1424. * </code>
  1425. * <code>
  1426. * echo $rijndael->decrypt($des->encrypt(substr($plaintext, 16, 16)));
  1427. * </code>
  1428. *
  1429. * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
  1430. * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
  1431. * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
  1432. *
  1433. * Put another way, when the continuous buffer is enabled, the state of the Crypt_Rijndael() object changes after each
  1434. * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
  1435. * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
  1436. * however, they are also less intuitive and more likely to cause you problems.
  1437. *
  1438. * @see Crypt_Rijndael::disableContinuousBuffer()
  1439. * @access public
  1440. */
  1441. function enableContinuousBuffer()
  1442. {
  1443. $this->continuousBuffer = true;
  1444. }
  1445. /**
  1446. * Treat consecutive packets as if they are a discontinuous buffer.
  1447. *
  1448. * The default behavior.
  1449. *
  1450. * @see Crypt_Rijndael::enableContinuousBuffer()
  1451. * @access public
  1452. */
  1453. function disableContinuousBuffer()
  1454. {
  1455. $this->continuousBuffer = false;
  1456. $this->encryptIV = $this->iv;
  1457. $this->decryptIV = $this->iv;
  1458. $this->enbuffer = array('encrypted' => '', 'xor' => '', 'pos' => 0);
  1459. $this->debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0);
  1460. }
  1461. /**
  1462. * String Shift
  1463. *
  1464. * Inspired by array_shift
  1465. *
  1466. * @param String $string
  1467. * @param optional Integer $index
  1468. * @return String
  1469. * @access private
  1470. */
  1471. function _string_shift(&$string, $index = 1)
  1472. {
  1473. $substr = substr($string, 0, $index);
  1474. $string = substr($string, $index);
  1475. return $substr;
  1476. }
  1477. /**
  1478. * Creates performance-optimized function for de/encrypt(), storing it in $this->inline_crypt
  1479. *
  1480. * @see Crypt_Rijndael::encrypt()
  1481. * @see Crypt_Rijndael::decrypt()
  1482. * @access private
  1483. */
  1484. function inline_crypt_setup()
  1485. {
  1486. // Note: inline_crypt_setup() will be called only if $this->changed === true
  1487. // So here we are'nt under the same heavy timing-stress as we are in _de/encryptBlock() or de/encrypt().
  1488. // However...the here generated function- $code, stored as php callback in $this->inline_crypt, must work as fast as even possible.
  1489. $lambda_functions =& Crypt_Rijndael::get_lambda_functions();
  1490. $block_size = $this->block_size;
  1491. $mode = $this->mode;
  1492. // The first 5 generated $lambda_functions will use the key-words hardcoded for better performance.
  1493. // For memory reason we limit those ultra-optimized function code to 5.
  1494. // After that, we use pure (extracted) integer vars for the key-words which is faster than accessing them via array.
  1495. if (count($lambda_functions) < 5) {
  1496. $w = $this->w;
  1497. $dw = $this->dw;
  1498. $init_encryptBlock = '';
  1499. $init_decryptBlock = '';
  1500. } else {
  1501. for ($i = 0, $cw = count($this->w); $i < $cw; ++$i) {
  1502. $w[] = '$w_'.$i;
  1503. $dw[] = '$dw_'.$i;
  1504. }
  1505. $init_encryptBlock = 'extract($self->w, EXTR_PREFIX_ALL, "w");';
  1506. $init_decryptBlock = 'extract($self->dw, EXTR_PREFIX_ALL, "dw");';
  1507. }
  1508. $code_hash = md5("$mode, $block_size, " . implode(',', $w));
  1509. if (!isset($lambda_functions[$code_hash])) {
  1510. $Nr = $this->Nr;
  1511. $Nb = $this->Nb;
  1512. $c = $this->c;
  1513. // Generating encrypt code:
  1514. $init_encryptBlock.= '
  1515. $t0 = $self->t0;
  1516. $t1 = $self->t1;
  1517. $t2 = $self->t2;
  1518. $t3 = $self->t3;
  1519. $sbox = $self->sbox;';
  1520. $s = 'e';
  1521. $e = 's';
  1522. $wc = $Nb - 1;
  1523. // Preround: addRoundKey
  1524. $_encryptBlock = '$in = unpack("N*", $in);'."\n";
  1525. for ($i = 0; $i < $Nb; ++$i) {
  1526. $_encryptBlock .= '$s'.$i.' = $in['.($i + 1).'] ^ '.$w[++$wc].";\n";
  1527. }
  1528. // Mainrounds: shiftRows + subWord + mixColumns + addRoundKey
  1529. for ($round = 1; $round < $Nr; ++$round) {
  1530. list($s, $e) = array($e, $s);
  1531. for ($i = 0; $i < $Nb; ++$i) {
  1532. $_encryptBlock.=
  1533. '$'.$e.$i.' =
  1534. $t0[($'.$s.$i .' >> 24) & 0xff] ^
  1535. $t1[($'.$s.(($i + $c[1]) % $Nb).' >> 16) & 0xff] ^
  1536. $t2[($'.$s.(($i + $c[2]) % $Nb).' >> 8) & 0xff] ^
  1537. $t3[ $'.$s.(($i + $c[3]) % $Nb).' & 0xff] ^
  1538. '.$w[++$wc].";\n";
  1539. }
  1540. }
  1541. // Finalround: subWord + shiftRows + addRoundKey
  1542. for ($i = 0; $i < $Nb; ++$i) {
  1543. $_encryptBlock.=
  1544. '$'.$e.$i.' =
  1545. $sbox[ $'.$e.$i.' & 0xff] |
  1546. ($sbox[($'.$e.$i.' >> 8) & 0xff] << 8) |
  1547. ($sbox[($'.$e.$i.' >> 16) & 0xff] << 16) |
  1548. ($sbox[($'.$e.$i.' >> 24) & 0xff] << 24);'."\n";
  1549. }
  1550. $_encryptBlock .= '$in = pack("N*"'."\n";
  1551. for ($i = 0; $i < $Nb; ++$i) {
  1552. $_encryptBlock.= ',
  1553. ($'.$e.$i .' & 0xFF000000) ^
  1554. ($'.$e.(($i + $c[1]) % $Nb).' & 0x00FF0000) ^
  1555. ($'.$e.(($i + $c[2]) % $Nb).' & 0x0000FF00) ^
  1556. ($'.$e.(($i + $c[3]) % $Nb).' & 0x000000FF) ^
  1557. '.$w[$i]."\n";
  1558. }
  1559. $_encryptBlock .= ');';
  1560. // Generating decrypt code:
  1561. $init_decryptBlock.= '
  1562. $dt0 = $self->dt0;
  1563. $dt1 = $self->dt1;
  1564. $dt2 = $self->dt2;
  1565. $dt3 = $self->dt3;
  1566. $isbox = $self->isbox;';
  1567. $s = 'e';
  1568. $e = 's';
  1569. $wc = $Nb - 1;
  1570. // Preround: addRoundKey
  1571. $_decryptBlock = '$in = unpack("N*", $in);'."\n";
  1572. for ($i = 0; $i < $Nb; ++$i) {
  1573. $_decryptBlock .= '$s'.$i.' = $in['.($i + 1).'] ^ '.$dw[++$wc].';'."\n";
  1574. }
  1575. // Mainrounds: shiftRows + subWord + mixColumns + addRoundKey
  1576. for ($round = 1; $round < $Nr; ++$round) {
  1577. list($s, $e) = array($e, $s);
  1578. for ($i = 0; $i < $Nb; ++$i) {
  1579. $_decryptBlock.=
  1580. '$'.$e.$i.' =
  1581. $dt0[($'.$s.$i .' >> 24) & 0xff] ^
  1582. $dt1[($'.$s.(($Nb + $i - $c[1]) % $Nb).' >> 16) & 0xff] ^
  1583. $dt2[($'.$s.(($Nb + $i - $c[2]) % $Nb).' >> 8) & 0xff] ^
  1584. $dt3[ $'.$s.(($Nb + $i - $c[3]) % $Nb).' & 0xff] ^
  1585. '.$dw[++$wc].";\n";
  1586. }
  1587. }
  1588. // Finalround: subWord + shiftRows + addRoundKey
  1589. for ($i = 0; $i < $Nb; ++$i) {
  1590. $_decryptBlock.=
  1591. '$'.$e.$i.' =
  1592. $isbox[ $'.$e.$i.' & 0xff] |
  1593. ($isbox[($'.$e.$i.' >> 8) & 0xff] << 8) |
  1594. ($isbox[($'.$e.$i.' >> 16) & 0xff] << 16) |
  1595. ($isbox[($'.$e.$i.' >> 24) & 0xff] << 24);'."\n";
  1596. }
  1597. $_decryptBlock .= '$in = pack("N*"'."\n";
  1598. for ($i = 0; $i < $Nb; ++$i) {
  1599. $_decryptBlock.= ',
  1600. ($'.$e.$i. ' & 0xFF000000) ^
  1601. ($'.$e.(($Nb + $i - $c[1]) % $Nb).' & 0x00FF0000) ^
  1602. ($'.$e.(($Nb + $i - $c[2]) % $Nb).' & 0x0000FF00) ^
  1603. ($'.$e.(($Nb + $i - $c[3]) % $Nb).' & 0x000000FF) ^
  1604. '.$dw[$i]."\n";
  1605. }
  1606. $_decryptBlock .= ');';
  1607. // Generating mode of operation code:
  1608. switch ($mode) {
  1609. case CRYPT_RIJNDAEL_MODE_ECB:
  1610. $encrypt = $init_encryptBlock . '
  1611. $ciphertext = "";
  1612. $text = $self->_pad($text);
  1613. $plaintext_len = strlen($text);
  1614. for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
  1615. $in = substr($text, $i, '.$block_size.');
  1616. '.$_encryptBlock.'
  1617. $ciphertext.= $in;
  1618. }
  1619. return $ciphertext;
  1620. ';
  1621. $decrypt = $init_decryptBlock . '
  1622. $plaintext = "";
  1623. $text = str_pad($text, strlen($text) + ('.$block_size.' - strlen($text) % '.$block_size.') % '.$block_size.', chr(0));
  1624. $ciphertext_len = strlen($text);
  1625. for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') {
  1626. $in = substr($text, $i, '.$block_size.');
  1627. '.$_decryptBlock.'
  1628. $plaintext.= $in;
  1629. }
  1630. return $self->_unpad($plaintext);
  1631. ';
  1632. break;
  1633. case CRYPT_RIJNDAEL_MODE_CBC:
  1634. $encrypt = $init_encryptBlock . '
  1635. $ciphertext = "";
  1636. $text = $self->_pad($text);
  1637. $plaintext_len = strlen($text);
  1638. $in = $self->encryptIV;
  1639. for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
  1640. $in = substr($text, $i, '.$block_size.') ^ $in;
  1641. '.$_encryptBlock.'
  1642. $ciphertext.= $in;
  1643. }
  1644. if ($self->continuousBuffer) {
  1645. $self->encryptIV = $in;
  1646. }
  1647. return $ciphertext;
  1648. ';
  1649. $decrypt = $init_decryptBlock . '
  1650. $plaintext = "";
  1651. $text = str_pad($text, strlen($text) + ('.$block_size.' - strlen($text) % '.$block_size.') % '.$block_size.', chr(0));
  1652. $ciphertext_len = strlen($text);
  1653. $iv = $self->decryptIV;
  1654. for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') {
  1655. $in = $block = substr($text, $i, '.$block_size.');
  1656. '.$_decryptBlock.'
  1657. $plaintext.= $in ^ $iv;
  1658. $iv = $block;
  1659. }
  1660. if ($self->continuousBuffer) {
  1661. $self->decryptIV = $iv;
  1662. }
  1663. return $self->_unpad($plaintext);
  1664. ';
  1665. break;
  1666. case CRYPT_RIJNDAEL_MODE_CTR:
  1667. $encrypt = $init_encryptBlock . '
  1668. $ciphertext = "";
  1669. $plaintext_len = strlen($text);
  1670. $xor = $self->encryptIV;
  1671. $buffer = &$self->enbuffer;
  1672. if (strlen($buffer["encrypted"])) {
  1673. for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
  1674. $block = substr($text, $i, '.$block_size.');
  1675. if (strlen($block) > strlen($buffer["encrypted"])) {
  1676. $in = $self->_generate_xor('.$block_size.', $xor);
  1677. '.$_encryptBlock.'
  1678. $buffer["encrypted"].= $in;
  1679. }
  1680. $key = $self->_string_shift($buffer["encrypted"], '.$block_size.');
  1681. $ciphertext.= $block ^ $key;
  1682. }
  1683. } else {
  1684. for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
  1685. $block = substr($text, $i, '.$block_size.');
  1686. $in = $self->_generate_xor('.$block_size.', $xor);
  1687. '.$_encryptBlock.'
  1688. $key = $in;
  1689. $ciphertext.= $block ^ $key;
  1690. }
  1691. }
  1692. if ($self->continuousBuffer) {
  1693. $self->encryptIV = $xor;
  1694. if ($start = $plaintext_len % '.$block_size.') {
  1695. $buffer["encrypted"] = substr($key, $start) . $buffer["encrypted"];
  1696. }
  1697. }
  1698. return $ciphertext;
  1699. ';
  1700. $decrypt = $init_encryptBlock . '
  1701. $plaintext = "";
  1702. $ciphertext_len = strlen($text);
  1703. $xor = $self->decryptIV;
  1704. $buffer = &$self->debuffer;
  1705. if (strlen($buffer["ciphertext"])) {
  1706. for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') {
  1707. $block = substr($text, $i, '.$block_size.');
  1708. if (strlen($block) > strlen($buffer["ciphertext"])) {
  1709. $in = $self->_generate_xor('.$block_size.', $xor);
  1710. '.$_encryptBlock.'
  1711. $buffer["ciphertext"].= $in;
  1712. }
  1713. $key = $self->_string_shift($buffer["ciphertext"], '.$block_size.');
  1714. $plaintext.= $block ^ $key;
  1715. }
  1716. } else {
  1717. for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') {
  1718. $block = substr($text, $i, '.$block_size.');
  1719. $in = $self->_generate_xor('.$block_size.', $xor);
  1720. '.$_encryptBlock.'
  1721. $key = $in;
  1722. $plaintext.= $block ^ $key;
  1723. }
  1724. }
  1725. if ($self->continuousBuffer) {
  1726. $self->decryptIV = $xor;
  1727. if ($start = $ciphertext_len % '.$block_size.') {
  1728. $buffer["ciphertext"] = substr($key, $start) . $buffer["ciphertext"];
  1729. }
  1730. }
  1731. return $plaintext;
  1732. ';
  1733. break;
  1734. case CRYPT_RIJNDAEL_MODE_CFB:
  1735. $encrypt = $init_encryptBlock . '
  1736. $ciphertext = "";
  1737. $buffer = &$self->enbuffer;
  1738. if ($self->continuousBuffer) {
  1739. $iv = &$self->encryptIV;
  1740. $pos = &$buffer["pos"];
  1741. } else {
  1742. $iv = $self->encryptIV;
  1743. $pos = 0;
  1744. }
  1745. $len = strlen($text);
  1746. $i = 0;
  1747. if ($pos) {
  1748. $orig_pos = $pos;
  1749. $max = '.$block_size.' - $pos;
  1750. if ($len >= $max) {
  1751. $i = $max;
  1752. $len-= $max;
  1753. $pos = 0;
  1754. } else {
  1755. $i = $len;
  1756. $pos+= $len;
  1757. $len = 0;
  1758. }
  1759. $ciphertext = substr($iv, $orig_pos) ^ $text;
  1760. $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
  1761. }
  1762. while ($len >= '.$block_size.') {
  1763. $in = $iv;
  1764. '.$_encryptBlock.';
  1765. $iv = $in ^ substr($text, $i, '.$block_size.');
  1766. $ciphertext.= $iv;
  1767. $len-= '.$block_size.';
  1768. $i+= '.$block_size.';
  1769. }
  1770. if ($len) {
  1771. $in = $iv;
  1772. '.$_encryptBlock.'
  1773. $iv = $in;
  1774. $block = $iv ^ substr($text, $i);
  1775. $iv = substr_replace($iv, $block, 0, $len);
  1776. $ciphertext.= $block;
  1777. $pos = $len;
  1778. }
  1779. return $ciphertext;
  1780. ';
  1781. $decrypt = $init_encryptBlock . '
  1782. $plaintext = "";
  1783. $buffer = &$self->debuffer;
  1784. if ($self->continuousBuffer) {
  1785. $iv = &$self->decryptIV;
  1786. $pos = &$buffer["pos"];
  1787. } else {
  1788. $iv = $self->decryptIV;
  1789. $pos = 0;
  1790. }
  1791. $len = strlen($text);
  1792. $i = 0;
  1793. if ($pos) {
  1794. $orig_pos = $pos;
  1795. $max = '.$block_size.' - $pos;
  1796. if ($len >= $max) {
  1797. $i = $max;
  1798. $len-= $max;
  1799. $pos = 0;
  1800. } else {
  1801. $i = $len;
  1802. $pos+= $len;
  1803. $len = 0;
  1804. }
  1805. $plaintext = substr($iv, $orig_pos) ^ $text;
  1806. $iv = substr_replace($iv, substr($text, 0, $i), $orig_pos, $i);
  1807. }
  1808. while ($len >= '.$block_size.') {
  1809. $in = $iv;
  1810. '.$_encryptBlock.'
  1811. $iv = $in;
  1812. $cb = substr($text, $i, '.$block_size.');
  1813. $plaintext.= $iv ^ $cb;
  1814. $iv = $cb;
  1815. $len-= '.$block_size.';
  1816. $i+= '.$block_size.';
  1817. }
  1818. if ($len) {
  1819. $in = $iv;
  1820. '.$_encryptBlock.'
  1821. $iv = $in;
  1822. $plaintext.= $iv ^ substr($text, $i);
  1823. $iv = substr_replace($iv, substr($text, $i), 0, $len);
  1824. $pos = $len;
  1825. }
  1826. return $plaintext;
  1827. ';
  1828. break;
  1829. case CRYPT_RIJNDAEL_MODE_OFB:
  1830. $encrypt = $init_encryptBlock . '
  1831. $ciphertext = "";
  1832. $plaintext_len = strlen($text);
  1833. $xor = $self->encryptIV;
  1834. $buffer = &$self->enbuffer;
  1835. if (strlen($buffer["xor"])) {
  1836. for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
  1837. $block = substr($text, $i, '.$block_size.');
  1838. if (strlen($block) > strlen($buffer["xor"])) {
  1839. $in = $xor;
  1840. '.$_encryptBlock.'
  1841. $xor = $in;
  1842. $buffer["xor"].= $xor;
  1843. }
  1844. $key = $self->_string_shift($buffer["xor"], '.$block_size.');
  1845. $ciphertext.= $block ^ $key;
  1846. }
  1847. } else {
  1848. for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
  1849. $in = $xor;
  1850. '.$_encryptBlock.'
  1851. $xor = $in;
  1852. $ciphertext.= substr($text, $i, '.$block_size.') ^ $xor;
  1853. }
  1854. $key = $xor;
  1855. }
  1856. if ($self->continuousBuffer) {
  1857. $self->encryptIV = $xor;
  1858. if ($start = $plaintext_len % '.$block_size.') {
  1859. $buffer["xor"] = substr($key, $start) . $buffer["xor"];
  1860. }
  1861. }
  1862. return $ciphertext;
  1863. ';
  1864. $decrypt = $init_encryptBlock . '
  1865. $plaintext = "";
  1866. $ciphertext_len = strlen($text);
  1867. $xor = $self->decryptIV;
  1868. $buffer = &$self->debuffer;
  1869. if (strlen($buffer["xor"])) {
  1870. for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') {
  1871. $block = substr($text, $i, '.$block_size.');
  1872. if (strlen($block) > strlen($buffer["xor"])) {
  1873. $in = $xor;
  1874. '.$_encryptBlock.'
  1875. $xor = $in;
  1876. $buffer["xor"].= $xor;
  1877. }
  1878. $key = $self->_string_shift($buffer["xor"], '.$block_size.');
  1879. $plaintext.= $block ^ $key;
  1880. }
  1881. } else {
  1882. for ($i = 0; $i < $ciphertext_len; $i+= '.$block_size.') {
  1883. $in = $xor;
  1884. '.$_encryptBlock.'
  1885. $xor = $in;
  1886. $plaintext.= substr($text, $i, '.$block_size.') ^ $xor;
  1887. }
  1888. $key = $xor;
  1889. }
  1890. if ($self->continuousBuffer) {
  1891. $self->decryptIV = $xor;
  1892. if ($start = $ciphertext_len % '.$block_size.') {
  1893. $buffer["xor"] = substr($key, $start) . $buffer["xor"];
  1894. }
  1895. }
  1896. return $plaintext;
  1897. ';
  1898. break;
  1899. }
  1900. $lambda_functions[$code_hash] = create_function('$action, &$self, $text', 'if ($action == "encrypt") { '.$encrypt.' } else { '.$decrypt.' }');
  1901. }
  1902. $this->inline_crypt = $lambda_functions[$code_hash];
  1903. }
  1904. /**
  1905. * Holds the lambda_functions table (classwide)
  1906. *
  1907. * @see Crypt_Rijndael::inline_crypt_setup()
  1908. * @return Array
  1909. * @access private
  1910. */
  1911. function &get_lambda_functions()
  1912. {
  1913. static $functions = array();
  1914. return $functions;
  1915. }
  1916. }
  1917. // vim: ts=4:sw=4:et:
  1918. // vim6: fdl=1:
  1919. ?>