Commit | Line | Data |
---|---|---|
14d4f286 S |
1 | <?php |
2 | /** | |
3 | * The mcrypt block cipher implementation | |
4 | * | |
5 | * This class is used above all other implementations since it uses a core PHP | |
6 | * library if it is available. | |
7 | * | |
8 | * PHP version 5.3 | |
9 | * | |
10 | * @category PHPCryptLib | |
11 | * @package Cipher | |
12 | * @subpackage Block | |
13 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
14 | * @copyright 2011 The Authors | |
15 | * @license http://www.opensource.org/licenses/mit-license.html MIT License | |
16 | * @version Build @@version@@ | |
17 | */ | |
18 | ||
19 | namespace CryptLib\Cipher\Block\Cipher; | |
20 | ||
21 | /** | |
22 | * The mcrypt block cipher implementation | |
23 | * | |
24 | * This class is used above all other implementations since it uses a core PHP | |
25 | * library if it is available. | |
26 | * | |
27 | * @category PHPCryptLib | |
28 | * @package Cipher | |
29 | * @subpackage Block | |
30 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
31 | */ | |
32 | class MCrypt extends \CryptLib\Cipher\Block\AbstractCipher { | |
33 | ||
34 | /** | |
35 | * @var resource The mcrypt resource for cipher operations | |
36 | */ | |
37 | protected $mcrypt = null; | |
38 | ||
39 | /** | |
40 | * Get a list of supported ciphers for this class implementation | |
41 | * | |
42 | * @return array A list of supported ciphers | |
43 | */ | |
44 | public static function getSupportedCiphers() { | |
45 | // @codeCoverageIgnoreStart | |
46 | if (!function_exists('mcrypt_list_algorithms')) { | |
47 | return array(); | |
48 | } | |
49 | // @codeCoverageIgnoreEnd | |
50 | return mcrypt_list_algorithms(); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Construct the instance for the supplied cipher name | |
55 | * | |
56 | * @param string $cipher The cipher to implement | |
57 | * | |
58 | * @return void | |
59 | * @throws InvalidArgumentException if the cipher is not supported | |
60 | */ | |
61 | public function __construct($cipher) { | |
62 | parent::__construct($cipher); | |
63 | $this->keySize = mcrypt_get_key_size($cipher, MCRYPT_MODE_ECB); | |
64 | $this->blockSize = mcrypt_get_block_size($cipher, MCRYPT_MODE_ECB); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Destroy the mcrypt module if it's open | |
69 | * | |
70 | * @return void | |
71 | */ | |
72 | public function __destruct() { | |
73 | if ($this->mcrypt) { | |
74 | mcrypt_module_close($this->mcrypt); | |
75 | } | |
76 | } | |
77 | ||
78 | /** | |
79 | * Set the key to use for the cipher | |
80 | * | |
81 | * @param string $key The key to use | |
82 | * | |
83 | * @throws InvalidArgumentException If the key is not the correct size | |
84 | * @return void | |
85 | */ | |
86 | public function setKey($key) { | |
87 | switch ($this->cipher) { | |
88 | case 'rijndael-128': | |
89 | case 'rijndael-192': | |
90 | case 'rijndael-256': | |
91 | if (!in_array(strlen($key), array(16, 20, 24, 28, 32))) { | |
92 | throw new \InvalidArgumentException( | |
93 | sprintf( | |
94 | 'The supplied key block is in the valid sizes [%d:%s]', | |
95 | strlen($key), | |
96 | '16, 20, 24, 28, 32' | |
97 | ) | |
98 | ); | |
99 | } | |
100 | $this->keySize = strlen($key); | |
101 | default: | |
102 | parent::setKey($key); | |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
107 | * Decrypt a block of data using the supplied string key | |
108 | * | |
109 | * Note that the supplied data should be the same size as the block size of | |
110 | * the cipher being used. | |
111 | * | |
112 | * @param string $data The data to decrypt | |
113 | * | |
114 | * @return string The result decrypted data | |
115 | */ | |
116 | protected function decryptBlockData($data) { | |
117 | return mdecrypt_generic($this->mcrypt, $data); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Encrypt a block of data using the supplied string key | |
122 | * | |
123 | * Note that the supplied data should be the same size as the block size of | |
124 | * the cipher being used. | |
125 | * | |
126 | * @param string $data The data to encrypt | |
127 | * | |
128 | * @return string The result encrypted data | |
129 | */ | |
130 | protected function encryptBlockData($data) { | |
131 | return mcrypt_generic($this->mcrypt, $data); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Initialize the cipher by preparing the key | |
136 | * | |
137 | * @return boolean The status of the initialization | |
138 | * @codeCoverageIgnore | |
139 | */ | |
140 | protected function initialize() { | |
141 | if ($this->mcrypt) { | |
142 | mcrypt_module_close($this->mcrypt); | |
143 | } | |
144 | $this->mcrypt = mcrypt_module_open($this->cipher, '', MCRYPT_MODE_ECB, ''); | |
145 | if ($this->mcrypt) { | |
146 | $initv = str_repeat(chr(0), $this->getBlockSize()); | |
147 | return false !== mcrypt_generic_init($this->mcrypt, $this->key, $initv); | |
148 | } | |
149 | return false; | |
150 | } | |
151 | ||
152 | } |