File: DESTest.php

package info (click to toggle)
phpseclib 1.0.20-1%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,624 kB
  • sloc: php: 11,392; sh: 66; xml: 50; makefile: 23
file content (77 lines) | stat: -rw-r--r-- 3,230 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
<?php
/**
 * @author    Andreas Fischer <bantu@phpbb.com>
 * @copyright MMXIII Andreas Fischer
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
 */

require_once 'Crypt/DES.php';

// 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 Crypt_DES(CRYPT_MODE_CBC);
        $des->setKey('d');
        $des->setIV('d');

        $des->setPreferredEngine(CRYPT_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(CRYPT_ENGINE_MCRYPT);
        if ($des->getEngine() == CRYPT_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(CRYPT_ENGINE_OPENSSL);
        if ($des->getEngine() == CRYPT_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 Crypt_DES(CRYPT_MODE_CBC);
        $des->disablePadding();
        // when the key and iv are not specified they should be null padded
        //$des->setKey();
        //$des->setIV();

        $des->setPreferredEngine(CRYPT_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(CRYPT_ENGINE_MCRYPT);
        if ($des->getEngine() == CRYPT_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(CRYPT_ENGINE_OPENSSL);
        if ($des->getEngine() == CRYPT_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');
        }
    }
}