Subversion Repositories oidplus

Rev

Rev 846 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 846 Rev 874
Line 24... Line 24...
24
 *
24
 *
25
 *    echo $twofish->decrypt($twofish->encrypt($plaintext));
25
 *    echo $twofish->decrypt($twofish->encrypt($plaintext));
26
 * ?>
26
 * ?>
27
 * </code>
27
 * </code>
28
 *
28
 *
-
 
29
 * @category  Crypt
-
 
30
 * @package   Twofish
29
 * @author    Jim Wigginton <terrafrost@php.net>
31
 * @author    Jim Wigginton <terrafrost@php.net>
30
 * @author    Hans-Juergen Petrich <petrich@tronic-media.com>
32
 * @author    Hans-Juergen Petrich <petrich@tronic-media.com>
31
 * @copyright 2007 Jim Wigginton
33
 * @copyright 2007 Jim Wigginton
32
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
34
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
33
 * @link      http://phpseclib.sourceforge.net
35
 * @link      http://phpseclib.sourceforge.net
Line 39... Line 41...
39
use phpseclib3\Exception\BadModeException;
41
use phpseclib3\Exception\BadModeException;
40
 
42
 
41
/**
43
/**
42
 * Pure-PHP implementation of Twofish.
44
 * Pure-PHP implementation of Twofish.
43
 *
45
 *
-
 
46
 * @package Twofish
44
 * @author  Jim Wigginton <terrafrost@php.net>
47
 * @author  Jim Wigginton <terrafrost@php.net>
45
 * @author  Hans-Juergen Petrich <petrich@tronic-media.com>
48
 * @author  Hans-Juergen Petrich <petrich@tronic-media.com>
-
 
49
 * @access  public
46
 */
50
 */
47
class Twofish extends BlockCipher
51
class Twofish extends BlockCipher
48
{
52
{
49
    /**
53
    /**
50
     * The mcrypt specific name of the cipher
54
     * The mcrypt specific name of the cipher
51
     *
55
     *
52
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt
56
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt
53
     * @var string
57
     * @var string
-
 
58
     * @access private
54
     */
59
     */
55
    protected $cipher_name_mcrypt = 'twofish';
60
    protected $cipher_name_mcrypt = 'twofish';
56
 
61
 
57
    /**
62
    /**
58
     * Optimizing value while CFB-encrypting
63
     * Optimizing value while CFB-encrypting
59
     *
64
     *
60
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cfb_init_len
65
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cfb_init_len
61
     * @var int
66
     * @var int
-
 
67
     * @access private
62
     */
68
     */
63
    protected $cfb_init_len = 800;
69
    protected $cfb_init_len = 800;
64
 
70
 
65
    /**
71
    /**
66
     * Q-Table
72
     * Q-Table
67
     *
73
     *
68
     * @var array
74
     * @var array
-
 
75
     * @access private
69
     */
76
     */
70
    private static $q0 = [
77
    private static $q0 = [
71
        0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76,
78
        0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76,
72
        0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38,
79
        0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38,
73
        0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C,
80
        0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C,
Line 104... Line 111...
104
 
111
 
105
    /**
112
    /**
106
     * Q-Table
113
     * Q-Table
107
     *
114
     *
108
     * @var array
115
     * @var array
-
 
116
     * @access private
109
     */
117
     */
110
    private static $q1 = [
118
    private static $q1 = [
111
        0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8,
119
        0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8,
112
        0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B,
120
        0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B,
113
        0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1,
121
        0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1,
Line 144... Line 152...
144
 
152
 
145
    /**
153
    /**
146
     * M-Table
154
     * M-Table
147
     *
155
     *
148
     * @var array
156
     * @var array
-
 
157
     * @access private
149
     */
158
     */
150
    private static $m0 = [
159
    private static $m0 = [
151
        0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B, 0xE2E22BFB, 0x9E9EFAC8,
160
        0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B, 0xE2E22BFB, 0x9E9EFAC8,
152
        0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B, 0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B,
161
        0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B, 0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B,
153
        0x3C3C57D6, 0x93938A32, 0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1,
162
        0x3C3C57D6, 0x93938A32, 0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1,
Line 184... Line 193...
184
 
193
 
185
    /**
194
    /**
186
     * M-Table
195
     * M-Table
187
     *
196
     *
188
     * @var array
197
     * @var array
-
 
198
     * @access private
189
     */
199
     */
190
    private static $m1 = [
200
    private static $m1 = [
191
        0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252, 0xA3658080, 0x76DFE4E4,
201
        0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252, 0xA3658080, 0x76DFE4E4,
192
        0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A, 0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A,
202
        0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A, 0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A,
193
        0x0D54E6E6, 0xC6432020, 0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141,
203
        0x0D54E6E6, 0xC6432020, 0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141,
Line 224... Line 234...
224
 
234
 
225
    /**
235
    /**
226
     * M-Table
236
     * M-Table
227
     *
237
     *
228
     * @var array
238
     * @var array
-
 
239
     * @access private
229
     */
240
     */
230
    private static $m2 = [
241
    private static $m2 = [
231
        0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B, 0xE2FBE22B, 0x9EC89EFA,
242
        0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B, 0xE2FBE22B, 0x9EC89EFA,
232
        0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F, 0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7,
243
        0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F, 0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7,
233
        0x3CD63C57, 0x9332938A, 0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783,
244
        0x3CD63C57, 0x9332938A, 0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783,
Line 264... Line 275...
264
 
275
 
265
    /**
276
    /**
266
     * M-Table
277
     * M-Table
267
     *
278
     *
268
     * @var array
279
     * @var array
-
 
280
     * @access private
269
     */
281
     */
270
    private static $m3 = [
282
    private static $m3 = [
271
        0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98, 0x6580A365, 0xDFE476DF,
283
        0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98, 0x6580A365, 0xDFE476DF,
272
        0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866, 0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836,
284
        0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866, 0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836,
273
        0x54E60D54, 0x4320C643, 0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77,
285
        0x54E60D54, 0x4320C643, 0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77,
Line 304... Line 316...
304
 
316
 
305
    /**
317
    /**
306
     * The Key Schedule Array
318
     * The Key Schedule Array
307
     *
319
     *
308
     * @var array
320
     * @var array
-
 
321
     * @access private
309
     */
322
     */
310
    private $K = [];
323
    private $K = [];
311
 
324
 
312
    /**
325
    /**
313
     * The Key depended S-Table 0
326
     * The Key depended S-Table 0
314
     *
327
     *
315
     * @var array
328
     * @var array
-
 
329
     * @access private
316
     */
330
     */
317
    private $S0 = [];
331
    private $S0 = [];
318
 
332
 
319
    /**
333
    /**
320
     * The Key depended S-Table 1
334
     * The Key depended S-Table 1
321
     *
335
     *
322
     * @var array
336
     * @var array
-
 
337
     * @access private
323
     */
338
     */
324
    private $S1 = [];
339
    private $S1 = [];
325
 
340
 
326
    /**
341
    /**
327
     * The Key depended S-Table 2
342
     * The Key depended S-Table 2
328
     *
343
     *
329
     * @var array
344
     * @var array
-
 
345
     * @access private
330
     */
346
     */
331
    private $S2 = [];
347
    private $S2 = [];
332
 
348
 
333
    /**
349
    /**
334
     * The Key depended S-Table 3
350
     * The Key depended S-Table 3
335
     *
351
     *
336
     * @var array
352
     * @var array
-
 
353
     * @access private
337
     */
354
     */
338
    private $S3 = [];
355
    private $S3 = [];
339
 
356
 
340
    /**
357
    /**
341
     * Holds the last used key
358
     * Holds the last used key
342
     *
359
     *
343
     * @var array
360
     * @var array
-
 
361
     * @access private
344
     */
362
     */
345
    private $kl;
363
    private $kl;
346
 
364
 
347
    /**
365
    /**
348
     * The Key Length (in bytes)
366
     * The Key Length (in bytes)
349
     *
367
     *
350
     * @see Crypt_Twofish::setKeyLength()
368
     * @see Crypt_Twofish::setKeyLength()
351
     * @var int
369
     * @var int
-
 
370
     * @access private
352
     */
371
     */
353
    protected $key_length = 16;
372
    protected $key_length = 16;
354
 
373
 
355
    /**
374
    /**
356
     * Default Constructor.
375
     * Default Constructor.
357
     *
376
     *
358
     * @param string $mode
377
     * @param string $mode
-
 
378
     * @access public
359
     * @throws BadModeException if an invalid / unsupported mode is provided
379
     * @throws BadModeException if an invalid / unsupported mode is provided
360
     */
380
     */
361
    public function __construct($mode)
381
    public function __construct($mode)
362
    {
382
    {
363
        parent::__construct($mode);
383
        parent::__construct($mode);
Line 370... Line 390...
370
    /**
390
    /**
371
     * Sets the key length.
391
     * Sets the key length.
372
     *
392
     *
373
     * Valid key lengths are 128, 192 or 256 bits
393
     * Valid key lengths are 128, 192 or 256 bits
374
     *
394
     *
-
 
395
     * @access public
375
     * @param int $length
396
     * @param int $length
376
     */
397
     */
377
    public function setKeyLength($length)
398
    public function setKeyLength($length)
378
    {
399
    {
379
        switch ($length) {
400
        switch ($length) {
Line 392... Line 413...
392
     * Sets the key.
413
     * Sets the key.
393
     *
414
     *
394
     * Rijndael supports five different key lengths
415
     * Rijndael supports five different key lengths
395
     *
416
     *
396
     * @see setKeyLength()
417
     * @see setKeyLength()
-
 
418
     * @access public
397
     * @param string $key
419
     * @param string $key
398
     * @throws \LengthException if the key length isn't supported
420
     * @throws \LengthException if the key length isn't supported
399
     */
421
     */
400
    public function setKey($key)
422
    public function setKey($key)
401
    {
423
    {
Line 413... Line 435...
413
 
435
 
414
    /**
436
    /**
415
     * Setup the key (expansion)
437
     * Setup the key (expansion)
416
     *
438
     *
417
     * @see \phpseclib3\Crypt\Common\SymmetricKey::_setupKey()
439
     * @see \phpseclib3\Crypt\Common\SymmetricKey::_setupKey()
-
 
440
     * @access private
418
     */
441
     */
419
    protected function setupKey()
442
    protected function setupKey()
420
    {
443
    {
421
        if (isset($this->kl['key']) && $this->key === $this->kl['key']) {
444
        if (isset($this->kl['key']) && $this->key === $this->kl['key']) {
422
            // already expanded
445
            // already expanded
Line 524... Line 547...
524
    }
547
    }
525
 
548
 
526
    /**
549
    /**
527
     * _mdsrem function using by the twofish cipher algorithm
550
     * _mdsrem function using by the twofish cipher algorithm
528
     *
551
     *
-
 
552
     * @access private
529
     * @param string $A
553
     * @param string $A
530
     * @param string $B
554
     * @param string $B
531
     * @return array
555
     * @return array
532
     */
556
     */
533
    private function mdsrem($A, $B)
557
    private function mdsrem($A, $B)
Line 571... Line 595...
571
    }
595
    }
572
 
596
 
573
    /**
597
    /**
574
     * Encrypts a block
598
     * Encrypts a block
575
     *
599
     *
-
 
600
     * @access private
576
     * @param string $in
601
     * @param string $in
577
     * @return string
602
     * @return string
578
     */
603
     */
579
    protected function encryptBlock($in)
604
    protected function encryptBlock($in)
580
    {
605
    {
Line 626... Line 651...
626
    }
651
    }
627
 
652
 
628
    /**
653
    /**
629
     * Decrypts a block
654
     * Decrypts a block
630
     *
655
     *
-
 
656
     * @access private
631
     * @param string $in
657
     * @param string $in
632
     * @return string
658
     * @return string
633
     */
659
     */
634
    protected function decryptBlock($in)
660
    protected function decryptBlock($in)
635
    {
661
    {
Line 682... Line 708...
682
 
708
 
683
    /**
709
    /**
684
     * Setup the performance-optimized function for de/encrypt()
710
     * Setup the performance-optimized function for de/encrypt()
685
     *
711
     *
686
     * @see \phpseclib3\Crypt\Common\SymmetricKey::_setupInlineCrypt()
712
     * @see \phpseclib3\Crypt\Common\SymmetricKey::_setupInlineCrypt()
-
 
713
     * @access private
687
     */
714
     */
688
    protected function setupInlineCrypt()
715
    protected function setupInlineCrypt()
689
    {
716
    {
690
        $K = $this->K;
717
        $K = $this->K;
691
        $init_crypt = '
718
        $init_crypt = '