SSH SFTP Updater Support - Version 0.5

Version Description

  • update phpseclib to latest version
Download this release

Release Info

Developer TerraFrost
Plugin Icon wp plugin SSH SFTP Updater Support
Version 0.5
Comparing to
See all releases

Code changes from version 0.4 to 0.5

phpseclib/Crypt/AES.php CHANGED
@@ -1,631 +1,207 @@
1
- <?php
2
- /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
-
4
- /**
5
- * Pure-PHP implementation of AES.
6
- *
7
- * Uses mcrypt, if available, and an internal implementation, otherwise.
8
- *
9
- * PHP versions 4 and 5
10
- *
11
- * If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
12
- * {@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits
13
- * it'll be null-padded to 160-bits and 160 bits will be the key length until {@link Crypt_Rijndael::setKey() setKey()}
14
- * is called, again, at which point, it'll be recalculated.
15
- *
16
- * Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
17
- * make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,
18
- * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
19
- *
20
- * Here's a short example of how to use this library:
21
- * <code>
22
- * <?php
23
- * include('Crypt/AES.php');
24
- *
25
- * $aes = new Crypt_AES();
26
- *
27
- * $aes->setKey('abcdefghijklmnop');
28
- *
29
- * $size = 10 * 1024;
30
- * $plaintext = '';
31
- * for ($i = 0; $i < $size; $i++) {
32
- * $plaintext.= 'a';
33
- * }
34
- *
35
- * echo $aes->decrypt($aes->encrypt($plaintext));
36
- * ?>
37
- * </code>
38
- *
39
- * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
40
- * of this software and associated documentation files (the "Software"), to deal
41
- * in the Software without restriction, including without limitation the rights
42
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43
- * copies of the Software, and to permit persons to whom the Software is
44
- * furnished to do so, subject to the following conditions:
45
- *
46
- * The above copyright notice and this permission notice shall be included in
47
- * all copies or substantial portions of the Software.
48
- *
49
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
55
- * THE SOFTWARE.
56
- *
57
- * @category Crypt
58
- * @package Crypt_AES
59
- * @author Jim Wigginton <terrafrost@php.net>
60
- * @copyright MMVIII Jim Wigginton
61
- * @license http://www.opensource.org/licenses/mit-license.html MIT License
62
- * @version $Id: AES.php,v 1.7 2010/02/09 06:10:25 terrafrost Exp $
63
- * @link http://phpseclib.sourceforge.net
64
- */
65
-
66
- /**
67
- * Include Crypt_Rijndael
68
- */
69
- if (!class_exists('Crypt_Rijndael')) {
70
- require_once 'Rijndael.php';
71
- }
72
-
73
- /**#@+
74
- * @access public
75
- * @see Crypt_AES::encrypt()
76
- * @see Crypt_AES::decrypt()
77
- */
78
- /**
79
- * Encrypt / decrypt using the Counter mode.
80
- *
81
- * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
82
- *
83
- * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
84
- */
85
- define('CRYPT_AES_MODE_CTR', -1);
86
- /**
87
- * Encrypt / decrypt using the Electronic Code Book mode.
88
- *
89
- * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
90
- */
91
- define('CRYPT_AES_MODE_ECB', 1);
92
- /**
93
- * Encrypt / decrypt using the Code Book Chaining mode.
94
- *
95
- * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
96
- */
97
- define('CRYPT_AES_MODE_CBC', 2);
98
- /**
99
- * Encrypt / decrypt using the Cipher Feedback mode.
100
- *
101
- * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
102
- */
103
- define('CRYPT_AES_MODE_CFB', 3);
104
- /**
105
- * Encrypt / decrypt using the Cipher Feedback mode.
106
- *
107
- * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
108
- */
109
- define('CRYPT_AES_MODE_OFB', 4);
110
- /**#@-*/
111
-
112
- /**#@+
113
- * @access private
114
- * @see Crypt_AES::Crypt_AES()
115
- */
116
- /**
117
- * Toggles the internal implementation
118
- */
119
- define('CRYPT_AES_MODE_INTERNAL', 1);
120
- /**
121
- * Toggles the mcrypt implementation
122
- */
123
- define('CRYPT_AES_MODE_MCRYPT', 2);
124
- /**#@-*/
125
-
126
- /**
127
- * Pure-PHP implementation of AES.
128
- *
129
- * @author Jim Wigginton <terrafrost@php.net>
130
- * @version 0.1.0
131
- * @access public
132
- * @package Crypt_AES
133
- */
134
- class Crypt_AES extends Crypt_Rijndael {
135
- /**
136
- * mcrypt resource for encryption
137
- *
138
- * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
139
- * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
140
- *
141
- * @see Crypt_AES::encrypt()
142
- * @var String
143
- * @access private
144
- */
145
- var $enmcrypt;
146
-
147
- /**
148
- * mcrypt resource for decryption
149
- *
150
- * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
151
- * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
152
- *
153
- * @see Crypt_AES::decrypt()
154
- * @var String
155
- * @access private
156
- */
157
- var $demcrypt;
158
-
159
- /**
160
- * mcrypt resource for CFB mode
161
- *
162
- * @see Crypt_AES::encrypt()
163
- * @see Crypt_AES::decrypt()
164
- * @var String
165
- * @access private
166
- */
167
- var $ecb;
168
-
169
- /**
170
- * Default Constructor.
171
- *
172
- * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
173
- * CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC. If not explictly set, CRYPT_AES_MODE_CBC will be used.
174
- *
175
- * @param optional Integer $mode
176
- * @return Crypt_AES
177
- * @access public
178
- */
179
- function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
180
- {
181
- if ( !defined('CRYPT_AES_MODE') ) {
182
- switch (true) {
183
- case extension_loaded('mcrypt') && in_array('rijndael-128', mcrypt_list_algorithms()):
184
- define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);
185
- break;
186
- default:
187
- define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
188
- }
189
- }
190
-
191
- switch ( CRYPT_AES_MODE ) {
192
- case CRYPT_AES_MODE_MCRYPT:
193
- switch ($mode) {
194
- case CRYPT_AES_MODE_ECB:
195
- $this->paddable = true;
196
- $this->mode = MCRYPT_MODE_ECB;
197
- break;
198
- case CRYPT_AES_MODE_CTR:
199
- // ctr doesn't have a constant associated with it even though it appears to be fairly widely
200
- // supported. in lieu of knowing just how widely supported it is, i've, for now, opted not to
201
- // include a compatibility layer. the layer has been implemented but, for now, is commented out.
202
- $this->mode = 'ctr';
203
- //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_AES_MODE_CTR;
204
- break;
205
- case CRYPT_AES_MODE_CFB:
206
- $this->mode = 'ncfb';
207
- break;
208
- case CRYPT_AES_MODE_OFB:
209
- $this->mode = MCRYPT_MODE_NOFB;
210
- break;
211
- case CRYPT_AES_MODE_CBC:
212
- default:
213
- $this->paddable = true;
214
- $this->mode = MCRYPT_MODE_CBC;
215
- }
216
-
217
- break;
218
- default:
219
- switch ($mode) {
220
- case CRYPT_AES_MODE_ECB:
221
- $this->paddable = true;
222
- $this->mode = CRYPT_RIJNDAEL_MODE_ECB;
223
- break;
224
- case CRYPT_AES_MODE_CTR:
225
- $this->mode = CRYPT_RIJNDAEL_MODE_CTR;
226
- break;
227
- case CRYPT_AES_MODE_CFB:
228
- $this->mode = CRYPT_RIJNDAEL_MODE_CFB;
229
- break;
230
- case CRYPT_AES_MODE_OFB:
231
- $this->mode = CRYPT_RIJNDAEL_MODE_OFB;
232
- break;
233
- case CRYPT_AES_MODE_CBC:
234
- default:
235
- $this->paddable = true;
236
- $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
237
- }
238
- }
239
-
240
- if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
241
- parent::Crypt_Rijndael($this->mode);
242
- }
243
- }
244
-
245
- /**
246
- * Dummy function
247
- *
248
- * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
249
- *
250
- * @access public
251
- * @param Integer $length
252
- */
253
- function setBlockLength($length)
254
- {
255
- return;
256
- }
257
-
258
-
259
- /**
260
- * Sets the initialization vector. (optional)
261
- *
262
- * SetIV is not required when CRYPT_RIJNDAEL_MODE_ECB is being used. If not explictly set, it'll be assumed
263
- * to be all zero's.
264
- *
265
- * @access public
266
- * @param String $iv
267
- */
268
- function setIV($iv)
269
- {
270
- parent::setIV($iv);
271
- if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
272
- $this->changed = true;
273
- }
274
- }
275
-
276
- /**
277
- * Encrypts a message.
278
- *
279
- * $plaintext will be padded with up to 16 additional bytes. Other AES implementations may or may not pad in the
280
- * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
281
- * URL:
282
- *
283
- * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
284
- *
285
- * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
286
- * strlen($plaintext) will still need to be a multiple of 16, however, arbitrary values can be added to make it that
287
- * length.
288
- *
289
- * @see Crypt_AES::decrypt()
290
- * @access public
291
- * @param String $plaintext
292
- */
293
- function encrypt($plaintext)
294
- {
295
- if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
296
- $changed = $this->changed;
297
- $this->_mcryptSetup();
298
- /*
299
- if ($this->mode == CRYPT_AES_MODE_CTR) {
300
- $iv = $this->encryptIV;
301
- $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($plaintext), $iv));
302
- $ciphertext = $plaintext ^ $xor;
303
- if ($this->continuousBuffer) {
304
- $this->encryptIV = $iv;
305
- }
306
- return $ciphertext;
307
- }
308
- */
309
- // re: http://phpseclib.sourceforge.net/cfb-demo.phps
310
- // using mcrypt's default handing of CFB the above would output two different things. using phpseclib's
311
- // rewritten CFB implementation the above outputs the same thing twice.
312
- if ($this->mode == 'ncfb' && $this->continuousBuffer) {
313
- if ($changed) {
314
- $this->ecb = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
315
- mcrypt_generic_init($this->ecb, $this->key, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
316
- }
317
-
318
- $buffer = &$this->enbuffer['encrypted'];
319
-
320
- if (strlen($buffer)) {
321
- $ciphertext = $plaintext ^ substr($this->encryptIV, strlen($buffer));
322
- $buffer.= $ciphertext;
323
- if (strlen($buffer) == 16) {
324
- $this->encryptIV = $buffer;
325
- $buffer = '';
326
- mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
327
- }
328
- $plaintext = substr($plaintext, strlen($ciphertext));
329
- } else {
330
- $ciphertext = '';
331
- }
332
-
333
- $last_pos = strlen($plaintext) & 0xFFFFFFF0;
334
- if ($last_pos) {
335
- $ciphertext.= mcrypt_generic($this->enmcrypt, substr($plaintext, 0, $last_pos));
336
- $this->encryptIV = substr($ciphertext, -16);
337
- }
338
-
339
- if (strlen($plaintext) & 0xF) {
340
- $this->encryptIV = mcrypt_generic($this->ecb, $this->encryptIV);
341
- $buffer = substr($plaintext, $last_pos) ^ $this->encryptIV;
342
- $ciphertext.= $buffer;
343
- }
344
-
345
- return $ciphertext;
346
- }
347
-
348
- if ($this->paddable) {
349
- $plaintext = $this->_pad($plaintext);
350
- }
351
-
352
- $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
353
-
354
- if (!$this->continuousBuffer) {
355
- mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
356
- }
357
-
358
- return $ciphertext;
359
- }
360
-
361
- return parent::encrypt($plaintext);
362
- }
363
-
364
- /**
365
- * Decrypts a message.
366
- *
367
- * If strlen($ciphertext) is not a multiple of 16, null bytes will be added to the end of the string until it is.
368
- *
369
- * @see Crypt_AES::encrypt()
370
- * @access public
371
- * @param String $ciphertext
372
- */
373
- function decrypt($ciphertext)
374
- {
375
- if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
376
- $changed = $this->changed;
377
- $this->_mcryptSetup();
378
- /*
379
- if ($this->mode == CRYPT_AES_MODE_CTR) {
380
- $iv = $this->decryptIV;
381
- $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($ciphertext), $iv));
382
- $plaintext = $ciphertext ^ $xor;
383
- if ($this->continuousBuffer) {
384
- $this->decryptIV = $iv;
385
- }
386
- return $plaintext;
387
- }
388
- */
389
- if ($this->mode == 'ncfb' && $this->continuousBuffer) {
390
- if ($changed) {
391
- $this->ecb = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
392
- mcrypt_generic_init($this->ecb, $this->key, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
393
- }
394
-
395
- $buffer = &$this->debuffer['ciphertext'];
396
-
397
- if (strlen($buffer)) {
398
- $plaintext = $ciphertext ^ substr($this->decryptIV, strlen($buffer));
399
-
400
- $buffer.= substr($ciphertext, 0, strlen($plaintext));
401
- if (strlen($buffer) == 16) {
402
- $this->decryptIV = $buffer;
403
- $buffer = '';
404
- mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
405
- }
406
- $ciphertext = substr($ciphertext, strlen($plaintext));
407
- } else {
408
- $plaintext = '';
409
- }
410
-
411
- $last_pos = strlen($ciphertext) & 0xFFFFFFF0;
412
- if ($last_pos) {
413
- $plaintext = mdecrypt_generic($this->demcrypt, substr($ciphertext, 0, $last_pos));
414
- $this->decryptIV = substr($ciphertext, $last_pos - 16, 16);
415
- $this->decryptIV = mcrypt_generic($this->ecb, $this->decryptIV);
416
- }
417
-
418
- if (strlen($ciphertext) & 0xF) {
419
- $buffer = substr($ciphertext, $last_pos);
420
- $plaintext.= $buffer ^ $this->decryptIV;
421
- }
422
-
423
- return $plaintext;
424
- }
425
-
426
- if ($this->paddable) {
427
- // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
428
- // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
429
- $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 15) & 0xFFFFFFF0, chr(0));
430
- }
431
-
432
- $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
433
-
434
- if (!$this->continuousBuffer) {
435
- mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
436
- }
437
-
438
- return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
439
- }
440
-
441
- return parent::decrypt($ciphertext);
442
- }
443
-
444
- /**
445
- * Setup mcrypt
446
- *
447
- * Validates all the variables.
448
- *
449
- * @access private
450
- */
451
- function _mcryptSetup()
452
- {
453
- if (!$this->changed) {
454
- return;
455
- }
456
-
457
- if (!$this->explicit_key_length) {
458
- // this just copied from Crypt_Rijndael::_setup()
459
- $length = strlen($this->key) >> 2;
460
- if ($length > 8) {
461
- $length = 8;
462
- } else if ($length < 4) {
463
- $length = 4;
464
- }
465
- $this->Nk = $length;
466
- $this->key_size = $length << 2;
467
- }
468
-
469
- switch ($this->Nk) {
470
- case 4: // 128
471
- $this->key_size = 16;
472
- break;
473
- case 5: // 160
474
- case 6: // 192
475
- $this->key_size = 24;
476
- break;
477
- case 7: // 224
478
- case 8: // 256
479
- $this->key_size = 32;
480
- }
481
-
482
- $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, chr(0));
483
- $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, 16), 16, chr(0));
484
-
485
- if (!isset($this->enmcrypt)) {
486
- $mode = $this->mode;
487
- //$mode = $this->mode == CRYPT_AES_MODE_CTR ? MCRYPT_MODE_ECB : $this->mode;
488
-
489
- $this->demcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
490
- $this->enmcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
491
- } // else should mcrypt_generic_deinit be called?
492
-
493
- mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
494
- mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
495
-
496
- $this->changed = false;
497
- }
498
-
499
- /**
500
- * Encrypts a block
501
- *
502
- * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
503
- *
504
- * @see Crypt_Rijndael::_encryptBlock()
505
- * @access private
506
- * @param String $in
507
- * @return String
508
- */
509
- function _encryptBlock($in)
510
- {
511
- $state = unpack('N*word', $in);
512
-
513
- $Nr = $this->Nr;
514
- $w = $this->w;
515
- $t0 = $this->t0;
516
- $t1 = $this->t1;
517
- $t2 = $this->t2;
518
- $t3 = $this->t3;
519
-
520
- // addRoundKey and reindex $state
521
- $state = array(
522
- $state['word1'] ^ $w[0][0],
523
- $state['word2'] ^ $w[0][1],
524
- $state['word3'] ^ $w[0][2],
525
- $state['word4'] ^ $w[0][3]
526
- );
527
-
528
- // shiftRows + subWord + mixColumns + addRoundKey
529
- // we could loop unroll this and use if statements to do more rounds as necessary, but, in my tests, that yields
530
- // only a marginal improvement. since that also, imho, hinders the readability of the code, i've opted not to do it.
531
- for ($round = 1; $round < $Nr; $round++) {
532
- $state = array(
533
- $t0[$state[0] & 0xFF000000] ^ $t1[$state[1] & 0x00FF0000] ^ $t2[$state[2] & 0x0000FF00] ^ $t3[$state[3] & 0x000000FF] ^ $w[$round][0],
534
- $t0[$state[1] & 0xFF000000] ^ $t1[$state[2] & 0x00FF0000] ^ $t2[$state[3] & 0x0000FF00] ^ $t3[$state[0] & 0x000000FF] ^ $w[$round][1],
535
- $t0[$state[2] & 0xFF000000] ^ $t1[$state[3] & 0x00FF0000] ^ $t2[$state[0] & 0x0000FF00] ^ $t3[$state[1] & 0x000000FF] ^ $w[$round][2],
536
- $t0[$state[3] & 0xFF000000] ^ $t1[$state[0] & 0x00FF0000] ^ $t2[$state[1] & 0x0000FF00] ^ $t3[$state[2] & 0x000000FF] ^ $w[$round][3]
537
- );
538
-
539
- }
540
-
541
- // subWord
542
- $state = array(
543
- $this->_subWord($state[0]),
544
- $this->_subWord($state[1]),
545
- $this->_subWord($state[2]),
546
- $this->_subWord($state[3])
547
- );
548
-
549
- // shiftRows + addRoundKey
550
- $state = array(
551
- ($state[0] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[3] & 0x000000FF) ^ $this->w[$this->Nr][0],
552
- ($state[1] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[0] & 0x000000FF) ^ $this->w[$this->Nr][1],
553
- ($state[2] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[1] & 0x000000FF) ^ $this->w[$this->Nr][2],
554
- ($state[3] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[2] & 0x000000FF) ^ $this->w[$this->Nr][3]
555
- );
556
-
557
- return pack('N*', $state[0], $state[1], $state[2], $state[3]);
558
- }
559
-
560
- /**
561
- * Decrypts a block
562
- *
563
- * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
564
- *
565
- * @see Crypt_Rijndael::_decryptBlock()
566
- * @access private
567
- * @param String $in
568
- * @return String
569
- */
570
- function _decryptBlock($in)
571
- {
572
- $state = unpack('N*word', $in);
573
-
574
- $Nr = $this->Nr;
575
- $dw = $this->dw;
576
- $dt0 = $this->dt0;
577
- $dt1 = $this->dt1;
578
- $dt2 = $this->dt2;
579
- $dt3 = $this->dt3;
580
-
581
- // addRoundKey and reindex $state
582
- $state = array(
583
- $state['word1'] ^ $dw[$this->Nr][0],
584
- $state['word2'] ^ $dw[$this->Nr][1],
585
- $state['word3'] ^ $dw[$this->Nr][2],
586
- $state['word4'] ^ $dw[$this->Nr][3]
587
- );
588
-
589
-
590
- // invShiftRows + invSubBytes + invMixColumns + addRoundKey
591
- for ($round = $Nr - 1; $round > 0; $round--) {
592
- $state = array(
593
- $dt0[$state[0] & 0xFF000000] ^ $dt1[$state[3] & 0x00FF0000] ^ $dt2[$state[2] & 0x0000FF00] ^ $dt3[$state[1] & 0x000000FF] ^ $dw[$round][0],
594
- $dt0[$state[1] & 0xFF000000] ^ $dt1[$state[0] & 0x00FF0000] ^ $dt2[$state[3] & 0x0000FF00] ^ $dt3[$state[2] & 0x000000FF] ^ $dw[$round][1],
595
- $dt0[$state[2] & 0xFF000000] ^ $dt1[$state[1] & 0x00FF0000] ^ $dt2[$state[0] & 0x0000FF00] ^ $dt3[$state[3] & 0x000000FF] ^ $dw[$round][2],
596
- $dt0[$state[3] & 0xFF000000] ^ $dt1[$state[2] & 0x00FF0000] ^ $dt2[$state[1] & 0x0000FF00] ^ $dt3[$state[0] & 0x000000FF] ^ $dw[$round][3]
597
- );
598
- }
599
-
600
- // invShiftRows + invSubWord + addRoundKey
601
- $state = array(
602
- $this->_invSubWord(($state[0] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[1] & 0x000000FF)) ^ $dw[0][0],
603
- $this->_invSubWord(($state[1] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[2] & 0x000000FF)) ^ $dw[0][1],
604
- $this->_invSubWord(($state[2] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[3] & 0x000000FF)) ^ $dw[0][2],
605
- $this->_invSubWord(($state[3] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[0] & 0x000000FF)) ^ $dw[0][3]
606
- );
607
-
608
- return pack('N*', $state[0], $state[1], $state[2], $state[3]);
609
- }
610
-
611
- /**
612
- * Treat consecutive packets as if they are a discontinuous buffer.
613
- *
614
- * The default behavior.
615
- *
616
- * @see Crypt_Rijndael::enableContinuousBuffer()
617
- * @access public
618
- */
619
- function disableContinuousBuffer()
620
- {
621
- parent::disableContinuousBuffer();
622
-
623
- if (CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT) {
624
- mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
625
- mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
626
- }
627
- }
628
- }
629
-
630
- // vim: ts=4:sw=4:et:
631
- // vim6: fdl=1:
1
+ <?php
2
+
3
+ /**
4
+ * Pure-PHP implementation of AES.
5
+ *
6
+ * Uses mcrypt, if available/possible, and an internal implementation, otherwise.
7
+ *
8
+ * PHP versions 4 and 5
9
+ *
10
+ * If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
11
+ * {@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits
12
+ * it'll be null-padded to 192-bits and 192 bits will be the key length until {@link Crypt_AES::setKey() setKey()}
13
+ * is called, again, at which point, it'll be recalculated.
14
+ *
15
+ * Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
16
+ * make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,
17
+ * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
18
+ *
19
+ * Here's a short example of how to use this library:
20
+ * <code>
21
+ * <?php
22
+ * include 'Crypt/AES.php';
23
+ *
24
+ * $aes = new Crypt_AES();
25
+ *
26
+ * $aes->setKey('abcdefghijklmnop');
27
+ *
28
+ * $size = 10 * 1024;
29
+ * $plaintext = '';
30
+ * for ($i = 0; $i < $size; $i++) {
31
+ * $plaintext.= 'a';
32
+ * }
33
+ *
34
+ * echo $aes->decrypt($aes->encrypt($plaintext));
35
+ * ?>
36
+ * </code>
37
+ *
38
+ * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
39
+ * of this software and associated documentation files (the "Software"), to deal
40
+ * in the Software without restriction, including without limitation the rights
41
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
42
+ * copies of the Software, and to permit persons to whom the Software is
43
+ * furnished to do so, subject to the following conditions:
44
+ *
45
+ * The above copyright notice and this permission notice shall be included in
46
+ * all copies or substantial portions of the Software.
47
+ *
48
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
53
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
54
+ * THE SOFTWARE.
55
+ *
56
+ * @category Crypt
57
+ * @package Crypt_AES
58
+ * @author Jim Wigginton <terrafrost@php.net>
59
+ * @copyright MMVIII Jim Wigginton
60
+ * @license http://www.opensource.org/licenses/mit-license.html MIT License
61
+ * @link http://phpseclib.sourceforge.net
62
+ */
63
+
64
+ /**
65
+ * Include Crypt_Rijndael
66
+ */
67
+ if (!class_exists('Crypt_Rijndael')) {
68
+ include_once 'Rijndael.php';
69
+ }
70
+
71
+ /**#@+
72
+ * @access public
73
+ * @see Crypt_AES::encrypt()
74
+ * @see Crypt_AES::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_AES_MODE_CTR', CRYPT_MODE_CTR);
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_AES_MODE_ECB', CRYPT_MODE_ECB);
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_AES_MODE_CBC', CRYPT_MODE_CBC);
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_AES_MODE_CFB', CRYPT_MODE_CFB);
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_AES_MODE_OFB', CRYPT_MODE_OFB);
108
+ /**#@-*/
109
+
110
+ /**#@+
111
+ * @access private
112
+ * @see Crypt_Base::Crypt_Base()
113
+ */
114
+ /**
115
+ * Toggles the internal implementation
116
+ */
117
+ define('CRYPT_AES_MODE_INTERNAL', CRYPT_MODE_INTERNAL);
118
+ /**
119
+ * Toggles the mcrypt implementation
120
+ */
121
+ define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
122
+ /**#@-*/
123
+
124
+ /**
125
+ * Pure-PHP implementation of AES.
126
+ *
127
+ * @package Crypt_AES
128
+ * @author Jim Wigginton <terrafrost@php.net>
129
+ * @access public
130
+ */
131
+ class Crypt_AES extends Crypt_Rijndael
132
+ {
133
+ /**
134
+ * The namespace used by the cipher for its constants.
135
+ *
136
+ * @see Crypt_Base::const_namespace
137
+ * @var String
138
+ * @access private
139
+ */
140
+ var $const_namespace = 'AES';
141
+
142
+ /**
143
+ * Dummy function
144
+ *
145
+ * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
146
+ *
147
+ * @see Crypt_Rijndael::setBlockLength()
148
+ * @access public
149
+ * @param Integer $length
150
+ */
151
+ function setBlockLength($length)
152
+ {
153
+ return;
154
+ }
155
+
156
+ /**
157
+ * Sets the key length
158
+ *
159
+ * Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to
160
+ * 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
161
+ *
162
+ * @see Crypt_Rijndael:setKeyLength()
163
+ * @access public
164
+ * @param Integer $length
165
+ */
166
+ function setKeyLength($length)
167
+ {
168
+ switch ($length) {
169
+ case 160:
170
+ $length = 192;
171
+ break;
172
+ case 224:
173
+ $length = 256;
174
+ }
175
+ parent::setKeyLength($length);
176
+ }
177
+
178
+ /**
179
+ * Sets the key.
180
+ *
181
+ * Rijndael supports five different key lengths, AES only supports three.
182
+ *
183
+ * @see Crypt_Rijndael:setKey()
184
+ * @see setKeyLength()
185
+ * @access public
186
+ * @param String $key
187
+ */
188
+ function setKey($key)
189
+ {
190
+ parent::setKey($key);
191
+
192
+ if (!$this->explicit_key_length) {
193
+ $length = strlen($key);
194
+ switch (true) {
195
+ case $length <= 16:
196
+ $this->key_size = 16;
197
+ break;
198
+ case $length <= 24:
199
+ $this->key_size = 24;
200
+ break;
201
+ default:
202
+ $this->key_size = 32;
203
+ }
204
+ $this->_setupEngine();
205
+ }
206
+ }
207
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
phpseclib/Crypt/Base.php ADDED
@@ -0,0 +1,2011 @@