File: DESTest.php

package info (click to toggle)
php-phpseclib 2.0.42-1%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,416 kB
  • sloc: php: 11,867; sh: 66; xml: 49; makefile: 25
file content (78 lines) | stat: -rw-r--r-- 3,241 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
 * @author    Andreas Fischer <bantu@phpbb.com>
 * @copyright MMXIII Andreas Fischer
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
 */

use phpseclib\Crypt\Base;
use phpseclib\Crypt\DES;

// the AES tests establish the correctness of the modes of operation. this test is inteded to establish the consistency of
// key and iv padding between the multiple engines
class Unit_Crypt_DESTest extends PhpseclibTestCase
{
    public function testEncryptPadding()
    {
        $des = new DES(Base::MODE_CBC);
        $des->setKey('d');
        $des->setIV('d');

        $des->setPreferredEngine(Base::ENGINE_INTERNAL);

        $result = pack('H*', '3e7613642049af1e');

        $internal = $des->encrypt('d');
        $this->assertEquals($result, $internal, 'Failed asserting that the internal engine produced the correct result');

        $des->setPreferredEngine(Base::ENGINE_MCRYPT);
        if ($des->getEngine() == Base::ENGINE_MCRYPT) {
            $mcrypt = $des->encrypt('d');
            $this->assertEquals($result, $mcrypt, 'Failed asserting that the mcrypt engine produced the correct result');
        } else {
            self::markTestSkipped('Unable to initialize mcrypt engine');
        }

        $des->setPreferredEngine(Base::ENGINE_OPENSSL);
        if ($des->getEngine() == Base::ENGINE_OPENSSL) {
            $openssl = $des->encrypt('d');
            $this->assertEquals($result, $openssl,  'Failed asserting that the OpenSSL engine produced the correct result');
        } else {
            self::markTestSkipped('Unable to initialize OpenSSL engine');
        }
    }

    // phpseclib null pads ciphertext's if they're not long enough and you're in ecb / cbc mode. this silent failure mode is consistent
    // with mcrypt's behavior. maybe throwing an exception would be better but whatever. this test is more intended to establish consistent
    // behavior between the various engine's
    public function testDecryptPadding()
    {
        $des = new DES(Base::MODE_CBC);
        $des->disablePadding();
        // when the key and iv are not specified they should be null padded
        //$des->setKey();
        //$des->setIV();

        $des->setPreferredEngine(Base::ENGINE_INTERNAL);
        $internal = $des->decrypt('d');

        $result = pack('H*', '79b305d1ce555221');
        $this->assertEquals($result, $internal, 'Failed asserting that the internal engine produced the correct result');

        $des->setPreferredEngine(Base::ENGINE_MCRYPT);
        if ($des->getEngine() == Base::ENGINE_MCRYPT) {
            $mcrypt = $des->decrypt('d');
            $this->assertEquals($result, $mcrypt, 'Failed asserting that the mcrypt engine produced the correct result');
        } else {
            self::markTestSkipped('Unable to initialize mcrypt engine');
        }

        $des->setPreferredEngine(Base::ENGINE_OPENSSL);
        if ($des->getEngine() == Base::ENGINE_OPENSSL) {
            $openssl = $des->decrypt('d');
            $this->assertEquals($result, $openssl,  'Failed asserting that the OpenSSL engine produced the correct result');
        } else {
            self::markTestSkipped('Unable to initialize OpenSSL engine');
        }
    }
}