add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Cipher / Block / AbstractCipher.php
1 <?php
2 /**
3 * An abstract class for simplifing creation of ciphers
4 *
5 * PHP version 5.3
6 *
7 * @category PHPCryptLib
8 * @package Cipher
9 * @subpackage Block
10 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
11 * @copyright 2011 The Authors
12 * @license http://www.opensource.org/licenses/mit-license.html MIT License
13 * @version Build @@version@@
14 */
15
16 namespace CryptLib\Cipher\Block;
17
18 /**
19 * An abstract class for simplifing creation of ciphers
20 *
21 * @category PHPCryptLib
22 * @package Cipher
23 * @subpackage Block
24 */
25 abstract class AbstractCipher implements \CryptLib\Cipher\Block\Cipher {
26
27 /**
28 * @var int The block size for the cipher
29 */
30 protected $blockSize = 0;
31
32 /**
33 * @var string The cipher name for the current instance
34 */
35 protected $cipher = '';
36
37 /**
38 * @var boolean Is the cipher ready to encrypt/decrypt
39 */
40 protected $initialized = false;
41
42 /**
43 * @var string The key to use for encryption/decryption
44 */
45 protected $key = '';
46
47 /**
48 * @var int The size of the key to use
49 */
50 protected $keySize = 0;
51
52 /**
53 * Decrypt a block of data
54 *
55 * @param string $data The ciphertext to decrypt
56 *
57 * @return string The decrypted data
58 */
59 abstract protected function decryptBlockData($data);
60
61 /**
62 * Encrypt a block of data
63 *
64 * @param string $data The plaintext to encrypt
65 *
66 * @return string The encrypted cipher text
67 */
68 abstract protected function encryptBlockData($data);
69
70 /**
71 * Construct the instance for the supplied cipher name
72 *
73 * @param string $cipher The cipher to implement
74 *
75 * @return void
76 * @throws InvalidArgumentException if the cipher is not supported
77 */
78 public function __construct($cipher) {
79 $ciphers = static::getSupportedCiphers();
80 if (in_array($cipher, $ciphers)) {
81 $this->cipher = $cipher;
82 } else {
83 throw new \InvalidArgumentException('Unsupported Cipher Supplied');
84 }
85 }
86
87 /**
88 * Decrypt a block of data using the supplied string key.
89 *
90 * Note that the supplied data should be the same size as the block size of
91 * the cipher being used.
92 *
93 * @param string $data The data to decrypt
94 *
95 * @return string The result decrypted data
96 * @throws InvalidArgumentException If the data size is not the block size
97 * @throws RuntimeException If the cipher is not initialized
98 */
99 public function decryptBlock($data) {
100 $this->enforceInitializedCipher();
101 $this->enforceProperBlockSize($data);
102 return $this->decryptBlockData($data);
103 }
104
105 /**
106 * Encrypt a block of data using the supplied string key.
107 *
108 * Note that the supplied data should be the same size as the block size of
109 * the cipher being used.
110 *
111 * @param string $data The data to encrypt
112 *
113 * @return string The result encrypted data
114 * @throws InvalidArgumentException If the data size is not the block size
115 * @throws RuntimeException If the cipher is not initialized
116 */
117 public function encryptBlock($data) {
118 $this->enforceInitializedCipher();
119 $this->enforceProperBlockSize($data);
120 return $this->encryptBlockData($data);
121 }
122
123 /**
124 * Get the block size for the current initialized cipher
125 *
126 * @param string $key The key the data will be encrypted with
127 *
128 * @return int The block size for the current cipher
129 */
130 public function getBlockSize() {
131 return $this->blockSize;
132 }
133
134 /**
135 * Get the string name of the current cipher instance
136 *
137 * @return string The current instantiated cipher
138 */
139 public function getCipher() {
140 return $this->cipher;
141 }
142
143 /**
144 * Get the key size for the current initialized cipher
145 *
146 * @return int The key size for the current cipher
147 */
148 public function getKeySize() {
149 return $this->keySize;
150 }
151
152 /**
153 * Set the key to use for the cipher
154 *
155 * @param string $key The key to use
156 *
157 * @throws InvalidArgumentException If the key is not the correct size
158 * @return void
159 */
160 public function setKey($key) {
161 if (strlen($key) != $this->getKeySize()) {
162 throw new \InvalidArgumentException(
163 sprintf(
164 'The supplied key block is not the correct size [%d:%d]',
165 strlen($key),
166 $this->getKeySize()
167 )
168 );
169 }
170 $this->key = $key;
171 $this->initialized = $this->initialize();
172 }
173
174 /**
175 * Check to see if the cipher is initialized
176 *
177 * @return void
178 * @throws RuntimeException If the cipher is not initialized
179 */
180 protected function enforceInitializedCipher() {
181 if (!$this->initialized) {
182 throw new \RuntimeException(
183 'The cipher has not been properly initialized'
184 );
185 }
186 }
187
188 /**
189 * Check to see if the data is of the correct block size
190 *
191 * @param string $data The data block to check
192 *
193 * @return void
194 * @throws InvalidArgumentException if the data is not the correct size
195 */
196 protected function enforceProperBlockSize($data) {
197 if (strlen($data) != $this->getBlockSize()) {
198 throw new \InvalidArgumentException(
199 sprintf(
200 'The supplied data block is not the correct size [%d:%d]',
201 strlen($data),
202 $this->getBlockSize()
203 )
204 );
205 }
206 }
207
208 /**
209 * Initialize the function after the key is set
210 *
211 * @return boolean The status of the initialization
212 */
213 protected function initialize() {
214 return true;
215 }
216
217 }