Subversion Repositories oidplus

Rev

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

Rev 846 Rev 874
Line 30... Line 30...
30
 *
30
 *
31
 *    echo $des->decrypt($des->encrypt($plaintext));
31
 *    echo $des->decrypt($des->encrypt($plaintext));
32
 * ?>
32
 * ?>
33
 * </code>
33
 * </code>
34
 *
34
 *
-
 
35
 * @category  Crypt
-
 
36
 * @package   DES
35
 * @author    Jim Wigginton <terrafrost@php.net>
37
 * @author    Jim Wigginton <terrafrost@php.net>
36
 * @copyright 2007 Jim Wigginton
38
 * @copyright 2007 Jim Wigginton
37
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
39
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
38
 * @link      http://phpseclib.sourceforge.net
40
 * @link      http://phpseclib.sourceforge.net
39
 */
41
 */
Line 44... Line 46...
44
use phpseclib3\Exception\BadModeException;
46
use phpseclib3\Exception\BadModeException;
45
 
47
 
46
/**
48
/**
47
 * Pure-PHP implementation of DES.
49
 * Pure-PHP implementation of DES.
48
 *
50
 *
-
 
51
 * @package DES
49
 * @author  Jim Wigginton <terrafrost@php.net>
52
 * @author  Jim Wigginton <terrafrost@php.net>
-
 
53
 * @access  public
50
 */
54
 */
51
class DES extends BlockCipher
55
class DES extends BlockCipher
52
{
56
{
53
    /**
57
    /**
54
     * Contains $keys[self::ENCRYPT]
58
     * Contains $keys[self::ENCRYPT]
55
     *
59
     *
-
 
60
     * @access private
56
     * @see \phpseclib3\Crypt\DES::setupKey()
61
     * @see \phpseclib3\Crypt\DES::setupKey()
57
     * @see \phpseclib3\Crypt\DES::processBlock()
62
     * @see \phpseclib3\Crypt\DES::processBlock()
58
     */
63
     */
59
    const ENCRYPT = 0;
64
    const ENCRYPT = 0;
60
    /**
65
    /**
61
     * Contains $keys[self::DECRYPT]
66
     * Contains $keys[self::DECRYPT]
62
     *
67
     *
-
 
68
     * @access private
63
     * @see \phpseclib3\Crypt\DES::setupKey()
69
     * @see \phpseclib3\Crypt\DES::setupKey()
64
     * @see \phpseclib3\Crypt\DES::processBlock()
70
     * @see \phpseclib3\Crypt\DES::processBlock()
65
     */
71
     */
66
    const DECRYPT = 1;
72
    const DECRYPT = 1;
67
 
73
 
68
    /**
74
    /**
69
     * Block Length of the cipher
75
     * Block Length of the cipher
70
     *
76
     *
71
     * @see \phpseclib3\Crypt\Common\SymmetricKey::block_size
77
     * @see \phpseclib3\Crypt\Common\SymmetricKey::block_size
72
     * @var int
78
     * @var int
-
 
79
     * @access private
73
     */
80
     */
74
    protected $block_size = 8;
81
    protected $block_size = 8;
75
 
82
 
76
    /**
83
    /**
77
     * Key Length (in bytes)
84
     * Key Length (in bytes)
78
     *
85
     *
79
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setKeyLength()
86
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setKeyLength()
80
     * @var int
87
     * @var int
-
 
88
     * @access private
81
     */
89
     */
82
    protected $key_length = 8;
90
    protected $key_length = 8;
83
 
91
 
84
    /**
92
    /**
85
     * The mcrypt specific name of the cipher
93
     * The mcrypt specific name of the cipher
86
     *
94
     *
87
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt
95
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cipher_name_mcrypt
88
     * @var string
96
     * @var string
-
 
97
     * @access private
89
     */
98
     */
90
    protected $cipher_name_mcrypt = 'des';
99
    protected $cipher_name_mcrypt = 'des';
91
 
100
 
92
    /**
101
    /**
93
     * The OpenSSL names of the cipher / modes
102
     * The OpenSSL names of the cipher / modes
94
     *
103
     *
95
     * @see \phpseclib3\Crypt\Common\SymmetricKey::openssl_mode_names
104
     * @see \phpseclib3\Crypt\Common\SymmetricKey::openssl_mode_names
96
     * @var array
105
     * @var array
-
 
106
     * @access private
97
     */
107
     */
98
    protected $openssl_mode_names = [
108
    protected $openssl_mode_names = [
99
        self::MODE_ECB => 'des-ecb',
109
        self::MODE_ECB => 'des-ecb',
100
        self::MODE_CBC => 'des-cbc',
110
        self::MODE_CBC => 'des-cbc',
101
        self::MODE_CFB => 'des-cfb',
111
        self::MODE_CFB => 'des-cfb',
Line 106... Line 116...
106
    /**
116
    /**
107
     * Optimizing value while CFB-encrypting
117
     * Optimizing value while CFB-encrypting
108
     *
118
     *
109
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cfb_init_len
119
     * @see \phpseclib3\Crypt\Common\SymmetricKey::cfb_init_len
110
     * @var int
120
     * @var int
-
 
121
     * @access private
111
     */
122
     */
112
    protected $cfb_init_len = 500;
123
    protected $cfb_init_len = 500;
113
 
124
 
114
    /**
125
    /**
115
     * Switch for DES/3DES encryption
126
     * Switch for DES/3DES encryption
Line 117... Line 128...
117
     * Used only if $engine == self::ENGINE_INTERNAL
128
     * Used only if $engine == self::ENGINE_INTERNAL
118
     *
129
     *
119
     * @see self::setupKey()
130
     * @see self::setupKey()
120
     * @see self::processBlock()
131
     * @see self::processBlock()
121
     * @var int
132
     * @var int
-
 
133
     * @access private
122
     */
134
     */
123
    protected $des_rounds = 1;
135
    protected $des_rounds = 1;
124
 
136
 
125
    /**
137
    /**
126
     * max possible size of $key
138
     * max possible size of $key
127
     *
139
     *
128
     * @see self::setKey()
140
     * @see self::setKey()
129
     * @var string
141
     * @var string
-
 
142
     * @access private
130
     */
143
     */
131
    protected $key_length_max = 8;
144
    protected $key_length_max = 8;
132
 
145
 
133
    /**
146
    /**
134
     * The Key Schedule
147
     * The Key Schedule
135
     *
148
     *
136
     * @see self::setupKey()
149
     * @see self::setupKey()
137
     * @var array
150
     * @var array
-
 
151
     * @access private
138
     */
152
     */
139
    private $keys;
153
    private $keys;
140
 
154
 
141
    /**
155
    /**
142
     * Shuffle table.
156
     * Shuffle table.
Line 146... Line 160...
146
     * corresponding bit in the index value.
160
     * corresponding bit in the index value.
147
     *
161
     *
148
     * @see self::processBlock()
162
     * @see self::processBlock()
149
     * @see self::setupKey()
163
     * @see self::setupKey()
150
     * @var array
164
     * @var array
-
 
165
     * @access private
151
     */
166
     */
152
    protected static $shuffle = [
167
    protected static $shuffle = [
153
        "\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\xFF",
168
        "\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\xFF",
154
        "\x00\x00\x00\x00\x00\x00\xFF\x00", "\x00\x00\x00\x00\x00\x00\xFF\xFF",
169
        "\x00\x00\x00\x00\x00\x00\xFF\x00", "\x00\x00\x00\x00\x00\x00\xFF\xFF",
155
        "\x00\x00\x00\x00\x00\xFF\x00\x00", "\x00\x00\x00\x00\x00\xFF\x00\xFF",
170
        "\x00\x00\x00\x00\x00\xFF\x00\x00", "\x00\x00\x00\x00\x00\xFF\x00\xFF",
Line 284... Line 299...
284
     * IP mapping helper table.
299
     * IP mapping helper table.
285
     *
300
     *
286
     * Indexing this table with each source byte performs the initial bit permutation.
301
     * Indexing this table with each source byte performs the initial bit permutation.
287
     *
302
     *
288
     * @var array
303
     * @var array
-
 
304
     * @access private
289
     */
305
     */
290
    protected static $ipmap = [
306
    protected static $ipmap = [
291
        0x00, 0x10, 0x01, 0x11, 0x20, 0x30, 0x21, 0x31,
307
        0x00, 0x10, 0x01, 0x11, 0x20, 0x30, 0x21, 0x31,
292
        0x02, 0x12, 0x03, 0x13, 0x22, 0x32, 0x23, 0x33,
308
        0x02, 0x12, 0x03, 0x13, 0x22, 0x32, 0x23, 0x33,
293
        0x40, 0x50, 0x41, 0x51, 0x60, 0x70, 0x61, 0x71,
309
        0x40, 0x50, 0x41, 0x51, 0x60, 0x70, 0x61, 0x71,
Line 325... Line 341...
325
    /**
341
    /**
326
     * Inverse IP mapping helper table.
342
     * Inverse IP mapping helper table.
327
     * Indexing this table with a byte value reverses the bit order.
343
     * Indexing this table with a byte value reverses the bit order.
328
     *
344
     *
329
     * @var array
345
     * @var array
-
 
346
     * @access private
330
     */
347
     */
331
    protected static $invipmap = [
348
    protected static $invipmap = [
332
        0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
349
        0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
333
        0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
350
        0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
334
        0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
351
        0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
Line 368... Line 385...
368
     *
385
     *
369
     * Each box ($sbox1-$sbox8) has been vectorized, then each value pre-permuted using the
386
     * Each box ($sbox1-$sbox8) has been vectorized, then each value pre-permuted using the
370
     * P table: concatenation can then be replaced by exclusive ORs.
387
     * P table: concatenation can then be replaced by exclusive ORs.
371
     *
388
     *
372
     * @var array
389
     * @var array
-
 
390
     * @access private
373
     */
391
     */
374
    protected static $sbox1 = [
392
    protected static $sbox1 = [
375
        0x00808200, 0x00000000, 0x00008000, 0x00808202,
393
        0x00808200, 0x00000000, 0x00008000, 0x00808202,
376
        0x00808002, 0x00008202, 0x00000002, 0x00008000,
394
        0x00808002, 0x00008202, 0x00000002, 0x00008000,
377
        0x00000200, 0x00808200, 0x00808202, 0x00000200,
395
        0x00000200, 0x00808200, 0x00808202, 0x00000200,
Line 392... Line 410...
392
 
410
 
393
    /**
411
    /**
394
     * Pre-permuted S-box2
412
     * Pre-permuted S-box2
395
     *
413
     *
396
     * @var array
414
     * @var array
-
 
415
     * @access private
397
     */
416
     */
398
    protected static $sbox2 = [
417
    protected static $sbox2 = [
399
        0x40084010, 0x40004000, 0x00004000, 0x00084010,
418
        0x40084010, 0x40004000, 0x00004000, 0x00084010,
400
        0x00080000, 0x00000010, 0x40080010, 0x40004010,
419
        0x00080000, 0x00000010, 0x40080010, 0x40004010,
401
        0x40000010, 0x40084010, 0x40084000, 0x40000000,
420
        0x40000010, 0x40084010, 0x40084000, 0x40000000,
Line 416... Line 435...
416
 
435
 
417
    /**
436
    /**
418
     * Pre-permuted S-box3
437
     * Pre-permuted S-box3
419
     *
438
     *
420
     * @var array
439
     * @var array
-
 
440
     * @access private
421
     */
441
     */
422
    protected static $sbox3 = [
442
    protected static $sbox3 = [
423
        0x00000104, 0x04010100, 0x00000000, 0x04010004,
443
        0x00000104, 0x04010100, 0x00000000, 0x04010004,
424
        0x04000100, 0x00000000, 0x00010104, 0x04000100,
444
        0x04000100, 0x00000000, 0x00010104, 0x04000100,
425
        0x00010004, 0x04000004, 0x04000004, 0x00010000,
445
        0x00010004, 0x04000004, 0x04000004, 0x00010000,
Line 440... Line 460...
440
 
460
 
441
    /**
461
    /**
442
     * Pre-permuted S-box4
462
     * Pre-permuted S-box4
443
     *
463
     *
444
     * @var array
464
     * @var array
-
 
465
     * @access private
445
     */
466
     */
446
    protected static $sbox4 = [
467
    protected static $sbox4 = [
447
        0x80401000, 0x80001040, 0x80001040, 0x00000040,
468
        0x80401000, 0x80001040, 0x80001040, 0x00000040,
448
        0x00401040, 0x80400040, 0x80400000, 0x80001000,
469
        0x00401040, 0x80400040, 0x80400000, 0x80001000,
449
        0x00000000, 0x00401000, 0x00401000, 0x80401040,
470
        0x00000000, 0x00401000, 0x00401000, 0x80401040,
Line 464... Line 485...
464
 
485
 
465
    /**
486
    /**
466
     * Pre-permuted S-box5
487
     * Pre-permuted S-box5
467
     *
488
     *
468
     * @var array
489
     * @var array
-
 
490
     * @access private
469
     */
491
     */
470
    protected static $sbox5 = [
492
    protected static $sbox5 = [
471
        0x00000080, 0x01040080, 0x01040000, 0x21000080,
493
        0x00000080, 0x01040080, 0x01040000, 0x21000080,
472
        0x00040000, 0x00000080, 0x20000000, 0x01040000,
494
        0x00040000, 0x00000080, 0x20000000, 0x01040000,
473
        0x20040080, 0x00040000, 0x01000080, 0x20040080,
495
        0x20040080, 0x00040000, 0x01000080, 0x20040080,
Line 488... Line 510...
488
 
510
 
489
    /**
511
    /**
490
     * Pre-permuted S-box6
512
     * Pre-permuted S-box6
491
     *
513
     *
492
     * @var array
514
     * @var array
-
 
515
     * @access private
493
     */
516
     */
494
    protected static $sbox6 = [
517
    protected static $sbox6 = [
495
        0x10000008, 0x10200000, 0x00002000, 0x10202008,
518
        0x10000008, 0x10200000, 0x00002000, 0x10202008,
496
        0x10200000, 0x00000008, 0x10202008, 0x00200000,
519
        0x10200000, 0x00000008, 0x10202008, 0x00200000,
497
        0x10002000, 0x00202008, 0x00200000, 0x10000008,
520
        0x10002000, 0x00202008, 0x00200000, 0x10000008,
Line 512... Line 535...
512
 
535
 
513
    /**
536
    /**
514
     * Pre-permuted S-box7
537
     * Pre-permuted S-box7
515
     *
538
     *
516
     * @var array
539
     * @var array
-
 
540
     * @access private
517
     */
541
     */
518
    protected static $sbox7 = [
542
    protected static $sbox7 = [
519
        0x00100000, 0x02100001, 0x02000401, 0x00000000,
543
        0x00100000, 0x02100001, 0x02000401, 0x00000000,
520
        0x00000400, 0x02000401, 0x00100401, 0x02100400,
544
        0x00000400, 0x02000401, 0x00100401, 0x02100400,
521
        0x02100401, 0x00100000, 0x00000000, 0x02000001,
545
        0x02100401, 0x00100000, 0x00000000, 0x02000001,
Line 536... Line 560...
536
 
560
 
537
    /**
561
    /**
538
     * Pre-permuted S-box8
562
     * Pre-permuted S-box8
539
     *
563
     *
540
     * @var array
564
     * @var array
-
 
565
     * @access private
541
     */
566
     */
542
    protected static $sbox8 = [
567
    protected static $sbox8 = [
543
        0x08000820, 0x00000800, 0x00020000, 0x08020820,
568
        0x08000820, 0x00000800, 0x00020000, 0x08020820,
544
        0x08000000, 0x08000820, 0x00000020, 0x08000000,
569
        0x08000000, 0x08000820, 0x00000020, 0x08000000,
545
        0x00020020, 0x08020000, 0x08020820, 0x00020800,
570
        0x00020020, 0x08020000, 0x08020820, 0x00020800,
Line 560... Line 585...
560
 
585
 
561
    /**
586
    /**
562
     * Default Constructor.
587
     * Default Constructor.
563
     *
588
     *
564
     * @param string $mode
589
     * @param string $mode
-
 
590
     * @access public
565
     * @throws BadModeException if an invalid / unsupported mode is provided
591
     * @throws BadModeException if an invalid / unsupported mode is provided
566
     */
592
     */
567
    public function __construct($mode)
593
    public function __construct($mode)
568
    {
594
    {
569
        parent::__construct($mode);
595
        parent::__construct($mode);
Line 578... Line 604...
578
     *
604
     *
579
     * This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine()
605
     * This is mainly just a wrapper to set things up for \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine()
580
     *
606
     *
581
     * @see \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine()
607
     * @see \phpseclib3\Crypt\Common\SymmetricKey::isValidEngine()
582
     * @param int $engine
608
     * @param int $engine
-
 
609
     * @access protected
583
     * @return bool
610
     * @return bool
584
     */
611
     */
585
    protected function isValidEngineHelper($engine)
612
    protected function isValidEngineHelper($engine)
586
    {
613
    {
587
        if ($this->key_length_max == 8) {
614
        if ($this->key_length_max == 8) {
Line 600... Line 627...
600
     * Keys must be 64-bits long or 8 bytes long.
627
     * Keys must be 64-bits long or 8 bytes long.
601
     *
628
     *
602
     * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
629
     * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
603
     *
630
     *
604
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setKey()
631
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setKey()
-
 
632
     * @access public
605
     * @param string $key
633
     * @param string $key
606
     */
634
     */
607
    public function setKey($key)
635
    public function setKey($key)
608
    {
636
    {
609
        if (!($this instanceof TripleDES) && strlen($key) != 8) {
637
        if (!($this instanceof TripleDES) && strlen($key) != 8) {
Line 618... Line 646...
618
     * Encrypts a block
646
     * Encrypts a block
619
     *
647
     *
620
     * @see \phpseclib3\Crypt\Common\SymmetricKey::encryptBlock()
648
     * @see \phpseclib3\Crypt\Common\SymmetricKey::encryptBlock()
621
     * @see \phpseclib3\Crypt\Common\SymmetricKey::encrypt()
649
     * @see \phpseclib3\Crypt\Common\SymmetricKey::encrypt()
622
     * @see self::encrypt()
650
     * @see self::encrypt()
-
 
651
     * @access private
623
     * @param string $in
652
     * @param string $in
624
     * @return string
653
     * @return string
625
     */
654
     */
626
    protected function encryptBlock($in)
655
    protected function encryptBlock($in)
627
    {
656
    {
Line 632... Line 661...
632
     * Decrypts a block
661
     * Decrypts a block
633
     *
662
     *
634
     * @see \phpseclib3\Crypt\Common\SymmetricKey::decryptBlock()
663
     * @see \phpseclib3\Crypt\Common\SymmetricKey::decryptBlock()
635
     * @see \phpseclib3\Crypt\Common\SymmetricKey::decrypt()
664
     * @see \phpseclib3\Crypt\Common\SymmetricKey::decrypt()
636
     * @see self::decrypt()
665
     * @see self::decrypt()
-
 
666
     * @access private
637
     * @param string $in
667
     * @param string $in
638
     * @return string
668
     * @return string
639
     */
669
     */
640
    protected function decryptBlock($in)
670
    protected function decryptBlock($in)
641
    {
671
    {
Line 649... Line 679...
649
     * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general
679
     * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general
650
     * idea of what this function does.
680
     * idea of what this function does.
651
     *
681
     *
652
     * @see self::encryptBlock()
682
     * @see self::encryptBlock()
653
     * @see self::decryptBlock()
683
     * @see self::decryptBlock()
-
 
684
     * @access private
654
     * @param string $block
685
     * @param string $block
655
     * @param int $mode
686
     * @param int $mode
656
     * @return string
687
     * @return string
657
     */
688
     */
658
    private function processBlock($block, $mode)
689
    private function processBlock($block, $mode)
Line 732... Line 763...
732
 
763
 
733
    /**
764
    /**
734
     * Creates the key schedule
765
     * Creates the key schedule
735
     *
766
     *
736
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setupKey()
767
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setupKey()
-
 
768
     * @access private
737
     */
769
     */
738
    protected function setupKey()
770
    protected function setupKey()
739
    {
771
    {
740
        if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->des_rounds === $this->kl['des_rounds']) {
772
        if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->des_rounds === $this->kl['des_rounds']) {
741
            // already expanded
773
            // already expanded
Line 1266... Line 1298...
1266
 
1298
 
1267
    /**
1299
    /**
1268
     * Setup the performance-optimized function for de/encrypt()
1300
     * Setup the performance-optimized function for de/encrypt()
1269
     *
1301
     *
1270
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setupInlineCrypt()
1302
     * @see \phpseclib3\Crypt\Common\SymmetricKey::setupInlineCrypt()
-
 
1303
     * @access private
1271
     */
1304
     */
1272
    protected function setupInlineCrypt()
1305
    protected function setupInlineCrypt()
1273
    {
1306
    {
1274
        // Engine configuration for:
1307
        // Engine configuration for:
1275
        // -  DES ($des_rounds == 1) or
1308
        // -  DES ($des_rounds == 1) or