@@ -36,299 +36,299 @@ |
||
36 | 36 | */ |
37 | 37 | abstract class Mode |
38 | 38 | { |
39 | - /** |
|
40 | - * @type object $cipher The cipher object used within the mode |
|
41 | - */ |
|
42 | - protected $cipher = null; |
|
43 | - |
|
44 | - /** |
|
45 | - * @type string $iv The IV used for the mode, not all Modes |
|
46 | - * use an IV so this may be empty |
|
47 | - */ |
|
48 | - protected $iv = ""; |
|
49 | - |
|
50 | - /** |
|
51 | - * @type string $register For modes that use a register to do |
|
52 | - * encryption/decryption. This stores the unencrypted register. |
|
53 | - */ |
|
54 | - protected $register = ""; |
|
55 | - |
|
56 | - /** |
|
57 | - * @type string $enc_register For modes that use a register to do |
|
58 | - * encryption/decryption. This stores the encrypted register |
|
59 | - */ |
|
60 | - protected $enc_register = ""; |
|
61 | - |
|
62 | - /** |
|
63 | - * @type integer $block_size The byte size of the block to |
|
64 | - * encrypt/decrypt for the Mode |
|
65 | - */ |
|
66 | - private $block_size = 0; |
|
67 | - |
|
68 | - /** @type string $mode_name The name of mode currently used */ |
|
69 | - private $mode_name = ""; |
|
70 | - |
|
71 | - /** |
|
72 | - * @type string $padding The type of padding to use when required. |
|
73 | - * Padding types are defined in phpCrypt class. Defaults to |
|
74 | - * PHP_Crypt::PAD_ZERO |
|
75 | - */ |
|
76 | - private $padding = PHP_Crypt::PAD_ZERO; |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * Constructor |
|
81 | - * Sets the cipher object that will be used for encryption |
|
82 | - * |
|
83 | - * @param object $cipher One of the phpCrypt encryption cipher objects |
|
84 | - * @param string $mode_name The name of phpCrypt's modes, as defined in the mode |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - protected function __construct($mode_name, $cipher) |
|
88 | - { |
|
89 | - $this->name($mode_name); |
|
90 | - $this->cipher($cipher); |
|
91 | - $this->block_size = $this->cipher->blockSize(); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * Destructor |
|
97 | - * |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - protected function __destruct() |
|
101 | - { |
|
102 | - |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - |
|
107 | - /********************************************************************** |
|
39 | + /** |
|
40 | + * @type object $cipher The cipher object used within the mode |
|
41 | + */ |
|
42 | + protected $cipher = null; |
|
43 | + |
|
44 | + /** |
|
45 | + * @type string $iv The IV used for the mode, not all Modes |
|
46 | + * use an IV so this may be empty |
|
47 | + */ |
|
48 | + protected $iv = ""; |
|
49 | + |
|
50 | + /** |
|
51 | + * @type string $register For modes that use a register to do |
|
52 | + * encryption/decryption. This stores the unencrypted register. |
|
53 | + */ |
|
54 | + protected $register = ""; |
|
55 | + |
|
56 | + /** |
|
57 | + * @type string $enc_register For modes that use a register to do |
|
58 | + * encryption/decryption. This stores the encrypted register |
|
59 | + */ |
|
60 | + protected $enc_register = ""; |
|
61 | + |
|
62 | + /** |
|
63 | + * @type integer $block_size The byte size of the block to |
|
64 | + * encrypt/decrypt for the Mode |
|
65 | + */ |
|
66 | + private $block_size = 0; |
|
67 | + |
|
68 | + /** @type string $mode_name The name of mode currently used */ |
|
69 | + private $mode_name = ""; |
|
70 | + |
|
71 | + /** |
|
72 | + * @type string $padding The type of padding to use when required. |
|
73 | + * Padding types are defined in phpCrypt class. Defaults to |
|
74 | + * PHP_Crypt::PAD_ZERO |
|
75 | + */ |
|
76 | + private $padding = PHP_Crypt::PAD_ZERO; |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * Constructor |
|
81 | + * Sets the cipher object that will be used for encryption |
|
82 | + * |
|
83 | + * @param object $cipher One of the phpCrypt encryption cipher objects |
|
84 | + * @param string $mode_name The name of phpCrypt's modes, as defined in the mode |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + protected function __construct($mode_name, $cipher) |
|
88 | + { |
|
89 | + $this->name($mode_name); |
|
90 | + $this->cipher($cipher); |
|
91 | + $this->block_size = $this->cipher->blockSize(); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * Destructor |
|
97 | + * |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + protected function __destruct() |
|
101 | + { |
|
102 | + |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + |
|
107 | + /********************************************************************** |
|
108 | 108 | * ABSTRACT METHODS |
109 | 109 | * |
110 | 110 | * The abstract methods required by inheriting classes to implement |
111 | 111 | **********************************************************************/ |
112 | 112 | |
113 | - /** |
|
114 | - * All modes must have encrypt(), which implements |
|
115 | - * the mode using the cipher's encrypiton algorithm |
|
116 | - * |
|
117 | - * @param string $text A String to encrypt |
|
118 | - * @return boolean Always returns true |
|
119 | - */ |
|
120 | - abstract public function encrypt(&$text); |
|
113 | + /** |
|
114 | + * All modes must have encrypt(), which implements |
|
115 | + * the mode using the cipher's encrypiton algorithm |
|
116 | + * |
|
117 | + * @param string $text A String to encrypt |
|
118 | + * @return boolean Always returns true |
|
119 | + */ |
|
120 | + abstract public function encrypt(&$text); |
|
121 | 121 | |
122 | 122 | |
123 | - /** |
|
124 | - * All modes must have decrypt(), which implements |
|
125 | - * the mode using the cipher's encryption algorithm |
|
126 | - * |
|
127 | - * @param string $text A String to decrypt |
|
128 | - * @return boolean Always returns true |
|
129 | - */ |
|
130 | - abstract public function decrypt(&$text); |
|
123 | + /** |
|
124 | + * All modes must have decrypt(), which implements |
|
125 | + * the mode using the cipher's encryption algorithm |
|
126 | + * |
|
127 | + * @param string $text A String to decrypt |
|
128 | + * @return boolean Always returns true |
|
129 | + */ |
|
130 | + abstract public function decrypt(&$text); |
|
131 | 131 | |
132 | 132 | |
133 | - /** |
|
134 | - * Indicates whether or not a mode requires an IV |
|
135 | - * |
|
136 | - * @return boolean Always returns true or false |
|
137 | - */ |
|
138 | - abstract public function requiresIV(); |
|
133 | + /** |
|
134 | + * Indicates whether or not a mode requires an IV |
|
135 | + * |
|
136 | + * @return boolean Always returns true or false |
|
137 | + */ |
|
138 | + abstract public function requiresIV(); |
|
139 | 139 | |
140 | 140 | |
141 | 141 | |
142 | 142 | |
143 | - /********************************************************************** |
|
143 | + /********************************************************************** |
|
144 | 144 | * PUBLIC METHODS |
145 | 145 | * |
146 | 146 | **********************************************************************/ |
147 | 147 | |
148 | - /** |
|
149 | - * Create an IV if the Mode used requires an IV. |
|
150 | - * The IV should be saved and used for Encryption/Decryption |
|
151 | - * of the same blocks of data. |
|
152 | - * There are 3 ways to auto generate an IV by setting $src parameter |
|
153 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
154 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
155 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
156 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
157 | - * |
|
158 | - * @param string $src Optional, Sets how the IV is generated, must be |
|
159 | - * one of the predefined PHP_Crypt RAND constants. Defaults to |
|
160 | - * PHP_Crypt::RAND if none is given. |
|
161 | - * @return string The IV that is being used by the mode |
|
162 | - */ |
|
163 | - public function createIV($src = PHP_Crypt::RAND) |
|
164 | - { |
|
165 | - // if the mode does not use an IV, lets not waste time |
|
166 | - if (!$this->requiresIV()) |
|
167 | - return false; |
|
168 | - |
|
169 | - $iv = Core::randBytes($src, $this->block_size); |
|
170 | - return $this->IV($iv); |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * Sets or Returns an IV for the mode to use. If the $iv parameter |
|
176 | - * is not given, this function only returns the current IV in use. |
|
177 | - * |
|
178 | - * @param string $iv Optional, An IV to use for the mode and cipher selected |
|
179 | - * @return string The current IV being used |
|
180 | - */ |
|
181 | - public function IV($iv = null) |
|
182 | - { |
|
183 | - if ($iv != null) |
|
184 | - { |
|
185 | - // check that the iv is the correct length, |
|
186 | - $len = strlen($iv); |
|
187 | - if ($len != $this->block_size) |
|
188 | - { |
|
189 | - $msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes"; |
|
190 | - trigger_error($msg, E_USER_WARNING); |
|
191 | - } |
|
192 | - |
|
193 | - $this->clearRegisters(); |
|
194 | - $this->register = $iv; |
|
195 | - $this->iv = $iv; |
|
196 | - } |
|
197 | - |
|
198 | - return $this->iv; |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - /** |
|
203 | - * Checks to see if the current mode requires an IV and that it is set |
|
204 | - * if it is required. Triggers E_USER_WARNING an IV is required and not set |
|
205 | - * |
|
206 | - * @return void |
|
207 | - */ |
|
208 | - public function checkIV() |
|
209 | - { |
|
210 | - if ($this->requiresIV() && strlen($this->register) == 0) |
|
211 | - { |
|
212 | - $msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty"; |
|
213 | - trigger_error($msg, E_USER_WARNING); |
|
214 | - } |
|
215 | - } |
|
216 | - |
|
217 | - |
|
218 | - /** |
|
219 | - * Sets and returns the name of the mode being used |
|
220 | - * If $name parameter is set, sets the mode. If |
|
221 | - * $name is not set, returns the current mode in use |
|
222 | - * |
|
223 | - * @param string $name Optional, One of the predefined |
|
224 | - * phpCrypt mode constant names |
|
225 | - * @return string One of the predefined phpCrypt mode |
|
226 | - * constant mode names |
|
227 | - */ |
|
228 | - public function name($name = "") |
|
229 | - { |
|
230 | - if ($name != "") |
|
231 | - $this->mode_name = $name; |
|
232 | - |
|
233 | - return $this->mode_name; |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * Sets or Returns the padding type used with the mode |
|
239 | - * If the $type parameter is not given, this function |
|
240 | - * returns the the padding type only. |
|
241 | - * |
|
242 | - * @param string $type One of the predefined padding types |
|
243 | - * @return string |
|
244 | - */ |
|
245 | - public function padding($type = "") |
|
246 | - { |
|
247 | - if ($type != "") |
|
248 | - $this->padding = $type; |
|
249 | - |
|
250 | - return $this->padding; |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * Returns or Sets the cipher object being used |
|
256 | - * If the $cipher parameter is set with a cipher object, |
|
257 | - * the cipher current cipher will be set to this cipher. |
|
258 | - * If the $cipher parameter is not set, returns the |
|
259 | - * current cipher object being used. |
|
260 | - * |
|
261 | - * @param object $cipher Optional, An object of type Cipher |
|
262 | - * @return object An object of type Cipher |
|
263 | - */ |
|
264 | - public function cipher($cipher = null) |
|
265 | - { |
|
266 | - if (is_object($cipher)) |
|
267 | - $this->cipher = $cipher; |
|
268 | - |
|
269 | - return $this->cipher; |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - |
|
274 | - |
|
275 | - /********************************************************************** |
|
148 | + /** |
|
149 | + * Create an IV if the Mode used requires an IV. |
|
150 | + * The IV should be saved and used for Encryption/Decryption |
|
151 | + * of the same blocks of data. |
|
152 | + * There are 3 ways to auto generate an IV by setting $src parameter |
|
153 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
154 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
155 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
156 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
157 | + * |
|
158 | + * @param string $src Optional, Sets how the IV is generated, must be |
|
159 | + * one of the predefined PHP_Crypt RAND constants. Defaults to |
|
160 | + * PHP_Crypt::RAND if none is given. |
|
161 | + * @return string The IV that is being used by the mode |
|
162 | + */ |
|
163 | + public function createIV($src = PHP_Crypt::RAND) |
|
164 | + { |
|
165 | + // if the mode does not use an IV, lets not waste time |
|
166 | + if (!$this->requiresIV()) |
|
167 | + return false; |
|
168 | + |
|
169 | + $iv = Core::randBytes($src, $this->block_size); |
|
170 | + return $this->IV($iv); |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * Sets or Returns an IV for the mode to use. If the $iv parameter |
|
176 | + * is not given, this function only returns the current IV in use. |
|
177 | + * |
|
178 | + * @param string $iv Optional, An IV to use for the mode and cipher selected |
|
179 | + * @return string The current IV being used |
|
180 | + */ |
|
181 | + public function IV($iv = null) |
|
182 | + { |
|
183 | + if ($iv != null) |
|
184 | + { |
|
185 | + // check that the iv is the correct length, |
|
186 | + $len = strlen($iv); |
|
187 | + if ($len != $this->block_size) |
|
188 | + { |
|
189 | + $msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes"; |
|
190 | + trigger_error($msg, E_USER_WARNING); |
|
191 | + } |
|
192 | + |
|
193 | + $this->clearRegisters(); |
|
194 | + $this->register = $iv; |
|
195 | + $this->iv = $iv; |
|
196 | + } |
|
197 | + |
|
198 | + return $this->iv; |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + /** |
|
203 | + * Checks to see if the current mode requires an IV and that it is set |
|
204 | + * if it is required. Triggers E_USER_WARNING an IV is required and not set |
|
205 | + * |
|
206 | + * @return void |
|
207 | + */ |
|
208 | + public function checkIV() |
|
209 | + { |
|
210 | + if ($this->requiresIV() && strlen($this->register) == 0) |
|
211 | + { |
|
212 | + $msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty"; |
|
213 | + trigger_error($msg, E_USER_WARNING); |
|
214 | + } |
|
215 | + } |
|
216 | + |
|
217 | + |
|
218 | + /** |
|
219 | + * Sets and returns the name of the mode being used |
|
220 | + * If $name parameter is set, sets the mode. If |
|
221 | + * $name is not set, returns the current mode in use |
|
222 | + * |
|
223 | + * @param string $name Optional, One of the predefined |
|
224 | + * phpCrypt mode constant names |
|
225 | + * @return string One of the predefined phpCrypt mode |
|
226 | + * constant mode names |
|
227 | + */ |
|
228 | + public function name($name = "") |
|
229 | + { |
|
230 | + if ($name != "") |
|
231 | + $this->mode_name = $name; |
|
232 | + |
|
233 | + return $this->mode_name; |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * Sets or Returns the padding type used with the mode |
|
239 | + * If the $type parameter is not given, this function |
|
240 | + * returns the the padding type only. |
|
241 | + * |
|
242 | + * @param string $type One of the predefined padding types |
|
243 | + * @return string |
|
244 | + */ |
|
245 | + public function padding($type = "") |
|
246 | + { |
|
247 | + if ($type != "") |
|
248 | + $this->padding = $type; |
|
249 | + |
|
250 | + return $this->padding; |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * Returns or Sets the cipher object being used |
|
256 | + * If the $cipher parameter is set with a cipher object, |
|
257 | + * the cipher current cipher will be set to this cipher. |
|
258 | + * If the $cipher parameter is not set, returns the |
|
259 | + * current cipher object being used. |
|
260 | + * |
|
261 | + * @param object $cipher Optional, An object of type Cipher |
|
262 | + * @return object An object of type Cipher |
|
263 | + */ |
|
264 | + public function cipher($cipher = null) |
|
265 | + { |
|
266 | + if (is_object($cipher)) |
|
267 | + $this->cipher = $cipher; |
|
268 | + |
|
269 | + return $this->cipher; |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + |
|
274 | + |
|
275 | + /********************************************************************** |
|
276 | 276 | * PROTECTED METHODS |
277 | 277 | * |
278 | 278 | **********************************************************************/ |
279 | 279 | |
280 | - /** |
|
281 | - * Pads str so that final block is $block_bits in size, if the final block |
|
282 | - * is $block_bits, then an additional block is added that is $block_bits in size |
|
283 | - * The padding should be set by phpCrypt::setPadding() |
|
284 | - * |
|
285 | - * @param string $str the string to be padded |
|
286 | - * @return boolean Returns true |
|
287 | - */ |
|
288 | - protected function pad(&$str) |
|
289 | - { |
|
290 | - $len = strlen($str); |
|
291 | - $bytes = $this->cipher->blockSize(); // returns bytes |
|
292 | - |
|
293 | - // now determine the next multiple of blockSize(), then find |
|
294 | - // the difference between that and the length of $str, |
|
295 | - // this is how many padding bytes we will need |
|
296 | - $num = ceil($len / $bytes) * $bytes; |
|
297 | - $num = $num - $len; |
|
298 | - |
|
299 | - Padding::pad($str, $num, $this->padding); |
|
300 | - return true; |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * Strip out the padded blocks created from Pad(). |
|
306 | - * Padding type should be set by phpCrypt::setPadding() |
|
307 | - * |
|
308 | - * @param string $str the string to strip padding from |
|
309 | - * @return boolean Returns True |
|
310 | - */ |
|
311 | - protected function strip(&$str) |
|
312 | - { |
|
313 | - Padding::strip($str, $this->padding); |
|
314 | - return true; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /********************************************************************** |
|
280 | + /** |
|
281 | + * Pads str so that final block is $block_bits in size, if the final block |
|
282 | + * is $block_bits, then an additional block is added that is $block_bits in size |
|
283 | + * The padding should be set by phpCrypt::setPadding() |
|
284 | + * |
|
285 | + * @param string $str the string to be padded |
|
286 | + * @return boolean Returns true |
|
287 | + */ |
|
288 | + protected function pad(&$str) |
|
289 | + { |
|
290 | + $len = strlen($str); |
|
291 | + $bytes = $this->cipher->blockSize(); // returns bytes |
|
292 | + |
|
293 | + // now determine the next multiple of blockSize(), then find |
|
294 | + // the difference between that and the length of $str, |
|
295 | + // this is how many padding bytes we will need |
|
296 | + $num = ceil($len / $bytes) * $bytes; |
|
297 | + $num = $num - $len; |
|
298 | + |
|
299 | + Padding::pad($str, $num, $this->padding); |
|
300 | + return true; |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * Strip out the padded blocks created from Pad(). |
|
306 | + * Padding type should be set by phpCrypt::setPadding() |
|
307 | + * |
|
308 | + * @param string $str the string to strip padding from |
|
309 | + * @return boolean Returns True |
|
310 | + */ |
|
311 | + protected function strip(&$str) |
|
312 | + { |
|
313 | + Padding::strip($str, $this->padding); |
|
314 | + return true; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /********************************************************************** |
|
319 | 319 | * PRIVATE METHODS |
320 | 320 | * |
321 | 321 | **********************************************************************/ |
322 | 322 | |
323 | - /** |
|
324 | - * Clears the registers used for some modes |
|
325 | - * |
|
326 | - * @return void |
|
327 | - */ |
|
328 | - private function clearRegisters() |
|
329 | - { |
|
330 | - $this->register = ""; |
|
331 | - $this->enc_register = ""; |
|
332 | - } |
|
323 | + /** |
|
324 | + * Clears the registers used for some modes |
|
325 | + * |
|
326 | + * @return void |
|
327 | + */ |
|
328 | + private function clearRegisters() |
|
329 | + { |
|
330 | + $this->register = ""; |
|
331 | + $this->enc_register = ""; |
|
332 | + } |
|
333 | 333 | } |
334 | 334 | ?> |
@@ -39,76 +39,76 @@ |
||
39 | 39 | */ |
40 | 40 | class Mode_Raw extends Mode |
41 | 41 | { |
42 | - /** |
|
43 | - * Constructor |
|
44 | - * Sets the cipher object that will be used for encryption |
|
45 | - * |
|
46 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function __construct($cipher) |
|
50 | - { |
|
51 | - parent::__construct(PHP_Crypt::MODE_RAW, $cipher); |
|
52 | - } |
|
42 | + /** |
|
43 | + * Constructor |
|
44 | + * Sets the cipher object that will be used for encryption |
|
45 | + * |
|
46 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function __construct($cipher) |
|
50 | + { |
|
51 | + parent::__construct(PHP_Crypt::MODE_RAW, $cipher); |
|
52 | + } |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Constructor used by classes that extend this class |
|
57 | - * Used by Mode_Stream, which extends this class |
|
58 | - * |
|
59 | - * @param object $cipher One of phpCrypts cipher objects |
|
60 | - * @param integer $mode The mode constant identifier |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - protected function __construct1($mode, $cipher) |
|
64 | - { |
|
65 | - parent::__construct($mode, $cipher); |
|
66 | - } |
|
55 | + /** |
|
56 | + * Constructor used by classes that extend this class |
|
57 | + * Used by Mode_Stream, which extends this class |
|
58 | + * |
|
59 | + * @param object $cipher One of phpCrypts cipher objects |
|
60 | + * @param integer $mode The mode constant identifier |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + protected function __construct1($mode, $cipher) |
|
64 | + { |
|
65 | + parent::__construct($mode, $cipher); |
|
66 | + } |
|
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * Destructor |
|
71 | - */ |
|
72 | - public function __destruct() |
|
73 | - { |
|
74 | - parent::__destruct(); |
|
75 | - } |
|
69 | + /** |
|
70 | + * Destructor |
|
71 | + */ |
|
72 | + public function __destruct() |
|
73 | + { |
|
74 | + parent::__destruct(); |
|
75 | + } |
|
76 | 76 | |
77 | 77 | |
78 | - /** |
|
79 | - * Encrypts an the string using the Cipher with no Mode |
|
80 | - * NOTE: The data in $text must be the exact length required by the Cipher |
|
81 | - * |
|
82 | - * @return boolean Always returns false |
|
83 | - */ |
|
84 | - public function encrypt(&$text) |
|
85 | - { |
|
86 | - $this->cipher->encrypt($text); |
|
87 | - return true; |
|
88 | - } |
|
78 | + /** |
|
79 | + * Encrypts an the string using the Cipher with no Mode |
|
80 | + * NOTE: The data in $text must be the exact length required by the Cipher |
|
81 | + * |
|
82 | + * @return boolean Always returns false |
|
83 | + */ |
|
84 | + public function encrypt(&$text) |
|
85 | + { |
|
86 | + $this->cipher->encrypt($text); |
|
87 | + return true; |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * Decrypts one block of cipher text, not using any mode. |
|
93 | - * NOTE: The data in $text must be the exact length required by the Cipher |
|
94 | - * |
|
95 | - * @return boolean Always returns false |
|
96 | - */ |
|
97 | - public function decrypt(&$text) |
|
98 | - { |
|
99 | - $this->cipher->decrypt($text); |
|
100 | - return true; |
|
101 | - } |
|
91 | + /** |
|
92 | + * Decrypts one block of cipher text, not using any mode. |
|
93 | + * NOTE: The data in $text must be the exact length required by the Cipher |
|
94 | + * |
|
95 | + * @return boolean Always returns false |
|
96 | + */ |
|
97 | + public function decrypt(&$text) |
|
98 | + { |
|
99 | + $this->cipher->decrypt($text); |
|
100 | + return true; |
|
101 | + } |
|
102 | 102 | |
103 | 103 | |
104 | - /** |
|
105 | - * This mode does not require an IV |
|
106 | - * |
|
107 | - * @return boolean false |
|
108 | - */ |
|
109 | - public function requiresIV() |
|
110 | - { |
|
111 | - return false; |
|
112 | - } |
|
104 | + /** |
|
105 | + * This mode does not require an IV |
|
106 | + * |
|
107 | + * @return boolean false |
|
108 | + */ |
|
109 | + public function requiresIV() |
|
110 | + { |
|
111 | + return false; |
|
112 | + } |
|
113 | 113 | } |
114 | 114 | ?> |
@@ -36,106 +36,106 @@ |
||
36 | 36 | */ |
37 | 37 | class Mode_ECB extends Mode |
38 | 38 | { |
39 | - /** |
|
40 | - * Constructor |
|
41 | - * Sets the cipher object that will be used for encryption |
|
42 | - * |
|
43 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | - * @return void |
|
45 | - */ |
|
46 | - public function __construct($cipher) |
|
47 | - { |
|
48 | - parent::__construct(PHP_Crypt::MODE_ECB, $cipher); |
|
49 | - |
|
50 | - // this works with only block Ciphers |
|
51 | - if ($cipher->type() != Cipher::BLOCK) |
|
52 | - trigger_error("ECB mode requires a block cipher", E_USER_WARNING); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * Destructor |
|
58 | - * |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - public function __destruct() |
|
62 | - { |
|
63 | - parent::__destruct(); |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Encrypts an the entire string $plain_text using the cipher passed |
|
69 | - * to the constructor in ECB mode |
|
70 | - * |
|
71 | - * @param string $text The string to be encrypted in ECB mode |
|
72 | - * @return boolean Returns true |
|
73 | - */ |
|
74 | - public function encrypt(&$text) |
|
75 | - { |
|
76 | - $this->pad($text); |
|
77 | - $blocksz = $this->cipher->blockSize(); |
|
78 | - |
|
79 | - $max = strlen($text) / $blocksz; |
|
80 | - for ($i = 0; $i < $max; ++$i) |
|
81 | - { |
|
82 | - // get the current position within $text |
|
83 | - $pos = $i * $blocksz; |
|
84 | - |
|
85 | - // grab a block of text |
|
86 | - $block = substr($text, $pos, $blocksz); |
|
87 | - |
|
88 | - // encrypt the block |
|
89 | - $this->cipher->encrypt($block); |
|
90 | - |
|
91 | - // replace the plain text with the cipher text |
|
92 | - $text = substr_replace($text, $block, $pos, $blocksz); |
|
93 | - } |
|
94 | - |
|
95 | - return true; |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * Decrypts an the entire string $plain_text using the cipher passed |
|
101 | - * to the constructor in ECB mode |
|
102 | - * |
|
103 | - * @param string $text The string to be decrypted in ECB mode |
|
104 | - * @return boolean Returns true |
|
105 | - */ |
|
106 | - public function decrypt(&$text) |
|
107 | - { |
|
108 | - $blocksz = $this->cipher->blockSize(); |
|
109 | - |
|
110 | - $max = strlen($text) / $blocksz; |
|
111 | - for ($i = 0; $i < $max; ++$i) |
|
112 | - { |
|
113 | - // get the current position within $text |
|
114 | - $pos = $i * $blocksz; |
|
115 | - |
|
116 | - // get a block of cipher text |
|
117 | - $block = substr($text, $pos, $blocksz); |
|
118 | - |
|
119 | - // decrypt the block |
|
120 | - $this->cipher->decrypt($block); |
|
121 | - |
|
122 | - // replace the block of cipher text with plain text |
|
123 | - $text = substr_replace($text, $block, $pos, $blocksz); |
|
124 | - } |
|
125 | - |
|
126 | - $this->strip($text); |
|
127 | - return true; |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * This mode does not require an IV |
|
133 | - * |
|
134 | - * @return boolean Returns false |
|
135 | - */ |
|
136 | - public function requiresIV() |
|
137 | - { |
|
138 | - return false; |
|
139 | - } |
|
39 | + /** |
|
40 | + * Constructor |
|
41 | + * Sets the cipher object that will be used for encryption |
|
42 | + * |
|
43 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
44 | + * @return void |
|
45 | + */ |
|
46 | + public function __construct($cipher) |
|
47 | + { |
|
48 | + parent::__construct(PHP_Crypt::MODE_ECB, $cipher); |
|
49 | + |
|
50 | + // this works with only block Ciphers |
|
51 | + if ($cipher->type() != Cipher::BLOCK) |
|
52 | + trigger_error("ECB mode requires a block cipher", E_USER_WARNING); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * Destructor |
|
58 | + * |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + public function __destruct() |
|
62 | + { |
|
63 | + parent::__destruct(); |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Encrypts an the entire string $plain_text using the cipher passed |
|
69 | + * to the constructor in ECB mode |
|
70 | + * |
|
71 | + * @param string $text The string to be encrypted in ECB mode |
|
72 | + * @return boolean Returns true |
|
73 | + */ |
|
74 | + public function encrypt(&$text) |
|
75 | + { |
|
76 | + $this->pad($text); |
|
77 | + $blocksz = $this->cipher->blockSize(); |
|
78 | + |
|
79 | + $max = strlen($text) / $blocksz; |
|
80 | + for ($i = 0; $i < $max; ++$i) |
|
81 | + { |
|
82 | + // get the current position within $text |
|
83 | + $pos = $i * $blocksz; |
|
84 | + |
|
85 | + // grab a block of text |
|
86 | + $block = substr($text, $pos, $blocksz); |
|
87 | + |
|
88 | + // encrypt the block |
|
89 | + $this->cipher->encrypt($block); |
|
90 | + |
|
91 | + // replace the plain text with the cipher text |
|
92 | + $text = substr_replace($text, $block, $pos, $blocksz); |
|
93 | + } |
|
94 | + |
|
95 | + return true; |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * Decrypts an the entire string $plain_text using the cipher passed |
|
101 | + * to the constructor in ECB mode |
|
102 | + * |
|
103 | + * @param string $text The string to be decrypted in ECB mode |
|
104 | + * @return boolean Returns true |
|
105 | + */ |
|
106 | + public function decrypt(&$text) |
|
107 | + { |
|
108 | + $blocksz = $this->cipher->blockSize(); |
|
109 | + |
|
110 | + $max = strlen($text) / $blocksz; |
|
111 | + for ($i = 0; $i < $max; ++$i) |
|
112 | + { |
|
113 | + // get the current position within $text |
|
114 | + $pos = $i * $blocksz; |
|
115 | + |
|
116 | + // get a block of cipher text |
|
117 | + $block = substr($text, $pos, $blocksz); |
|
118 | + |
|
119 | + // decrypt the block |
|
120 | + $this->cipher->decrypt($block); |
|
121 | + |
|
122 | + // replace the block of cipher text with plain text |
|
123 | + $text = substr_replace($text, $block, $pos, $blocksz); |
|
124 | + } |
|
125 | + |
|
126 | + $this->strip($text); |
|
127 | + return true; |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * This mode does not require an IV |
|
133 | + * |
|
134 | + * @return boolean Returns false |
|
135 | + */ |
|
136 | + public function requiresIV() |
|
137 | + { |
|
138 | + return false; |
|
139 | + } |
|
140 | 140 | } |
141 | 141 | ?> |
@@ -40,80 +40,80 @@ |
||
40 | 40 | for ($i=0; $i<4*$Nb; $i++) $output[$i] = $state[$i%4][floor($i/4)]; |
41 | 41 | |
42 | 42 | return $output; |
43 | - } |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param integer $rnd |
|
47 | - * @param integer $Nb |
|
48 | - */ |
|
49 | - private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
|
45 | + /** |
|
46 | + * @param integer $rnd |
|
47 | + * @param integer $Nb |
|
48 | + */ |
|
49 | + private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
|
50 | 50 | for ($r=0; $r<4; $r++) { |
51 | - for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
51 | + for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | return $state; |
55 | - } |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param integer $Nb |
|
59 | - */ |
|
60 | - private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
|
57 | + /** |
|
58 | + * @param integer $Nb |
|
59 | + */ |
|
60 | + private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
|
61 | 61 | for ($r=0; $r<4; $r++) { |
62 | - for ($c=0; $c<$Nb; $c++) { |
|
63 | - $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
|
64 | - } |
|
62 | + for ($c=0; $c<$Nb; $c++) { |
|
63 | + $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
|
64 | + } |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | return $s; |
68 | - } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * @param integer $Nb |
|
72 | - */ |
|
73 | - private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
|
70 | + /** |
|
71 | + * @param integer $Nb |
|
72 | + */ |
|
73 | + private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
|
74 | 74 | $t = array(4); |
75 | 75 | for ($r=1; $r<4; $r++) { |
76 | - for ($c=0; $c<4; $c++) { |
|
77 | - $t[$c] = $s[$r][($c+$r)%$Nb]; |
|
78 | - } |
|
79 | - // shift into temp copy |
|
80 | - for ($c=0; $c<4; $c++) { |
|
81 | - $s[$r][$c] = $t[$c]; |
|
82 | - } |
|
83 | - // and copy back |
|
76 | + for ($c=0; $c<4; $c++) { |
|
77 | + $t[$c] = $s[$r][($c+$r)%$Nb]; |
|
78 | + } |
|
79 | + // shift into temp copy |
|
80 | + for ($c=0; $c<4; $c++) { |
|
81 | + $s[$r][$c] = $t[$c]; |
|
82 | + } |
|
83 | + // and copy back |
|
84 | 84 | } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): |
85 | 85 | return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
86 | - } |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @param integer $Nb |
|
90 | - */ |
|
91 | - private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [é5.1.3] |
|
88 | + /** |
|
89 | + * @param integer $Nb |
|
90 | + */ |
|
91 | + private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [é5.1.3] |
|
92 | 92 | for ($c=0; $c<4; $c++) { |
93 | - $a = array(4); // 'a' is a copy of the current column from 's' |
|
94 | - $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
95 | - for ($i=0; $i<4; $i++) { |
|
93 | + $a = array(4); // 'a' is a copy of the current column from 's' |
|
94 | + $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
95 | + for ($i=0; $i<4; $i++) { |
|
96 | 96 | $a[$i] = $s[$i][$c]; |
97 | 97 | $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1; |
98 | - } |
|
99 | - // a[n] ^ b[n] is aé{03} in GF(2^8) |
|
100 | - $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
|
101 | - $s[1][$c] = $a[0] ^ $b[1] ^ $a[2] ^ $b[2] ^ $a[3]; // a0 * 2*a1 + 3*a2 + a3 |
|
102 | - $s[2][$c] = $a[0] ^ $a[1] ^ $b[2] ^ $a[3] ^ $b[3]; // a0 + a1 + 2*a2 + 3*a3 |
|
103 | - $s[3][$c] = $a[0] ^ $b[0] ^ $a[1] ^ $a[2] ^ $b[3]; // 3*a0 + a1 + a2 + 2*a3 |
|
98 | + } |
|
99 | + // a[n] ^ b[n] is aé{03} in GF(2^8) |
|
100 | + $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
|
101 | + $s[1][$c] = $a[0] ^ $b[1] ^ $a[2] ^ $b[2] ^ $a[3]; // a0 * 2*a1 + 3*a2 + a3 |
|
102 | + $s[2][$c] = $a[0] ^ $a[1] ^ $b[2] ^ $a[3] ^ $b[3]; // a0 + a1 + 2*a2 + 3*a3 |
|
103 | + $s[3][$c] = $a[0] ^ $b[0] ^ $a[1] ^ $a[2] ^ $b[3]; // 3*a0 + a1 + a2 + 2*a3 |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | return $s; |
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Key expansion for Rijndael cipher(): performs key expansion on cipher key |
|
111 | - * to generate a key schedule |
|
112 | - * |
|
113 | - * @param key cipher key byte-array (16 bytes) |
|
114 | - * @return key schedule as 2D byte-array (Nr+1 x Nb bytes) |
|
115 | - */ |
|
116 | - public static function keyExpansion($key) { // generate Key Schedule from Cipher Key [é5.2] |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Key expansion for Rijndael cipher(): performs key expansion on cipher key |
|
111 | + * to generate a key schedule |
|
112 | + * |
|
113 | + * @param key cipher key byte-array (16 bytes) |
|
114 | + * @return key schedule as 2D byte-array (Nr+1 x Nb bytes) |
|
115 | + */ |
|
116 | + public static function keyExpansion($key) { // generate Key Schedule from Cipher Key [é5.2] |
|
117 | 117 | $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
118 | 118 | $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
119 | 119 | $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
@@ -7,19 +7,19 @@ discard block |
||
7 | 7 | |
8 | 8 | class AesCtr extends Aes |
9 | 9 | { |
10 | - /** |
|
11 | - * Encrypt a text using AES encryption in Counter mode of operation |
|
12 | - * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
|
13 | - * |
|
14 | - * Unicode multi-byte character safe |
|
15 | - * |
|
16 | - * @param plaintext source text to be encrypted |
|
17 | - * @param password the password to use to generate a key |
|
18 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
19 | - * @return string text |
|
20 | - */ |
|
21 | - public static function encrypt($plaintext, $password, $nBits) |
|
22 | - { |
|
10 | + /** |
|
11 | + * Encrypt a text using AES encryption in Counter mode of operation |
|
12 | + * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
|
13 | + * |
|
14 | + * Unicode multi-byte character safe |
|
15 | + * |
|
16 | + * @param plaintext source text to be encrypted |
|
17 | + * @param password the password to use to generate a key |
|
18 | + * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
19 | + * @return string text |
|
20 | + */ |
|
21 | + public static function encrypt($plaintext, $password, $nBits) |
|
22 | + { |
|
23 | 23 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
24 | 24 | if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
25 | 25 | return ''; |
@@ -87,18 +87,18 @@ discard block |
||
87 | 87 | $ciphertext = base64_encode($ciphertext); |
88 | 88 | |
89 | 89 | return $ciphertext; |
90 | - } |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Decrypt a text encrypted by AES in counter mode of operation |
|
94 | - * |
|
95 | - * @param ciphertext source text to be decrypted |
|
96 | - * @param password the password to use to generate a key |
|
97 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
98 | - * @return string text |
|
99 | - */ |
|
100 | - public static function decrypt($ciphertext, $password, $nBits) |
|
101 | - { |
|
92 | + /** |
|
93 | + * Decrypt a text encrypted by AES in counter mode of operation |
|
94 | + * |
|
95 | + * @param ciphertext source text to be decrypted |
|
96 | + * @param password the password to use to generate a key |
|
97 | + * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
98 | + * @return string text |
|
99 | + */ |
|
100 | + public static function decrypt($ciphertext, $password, $nBits) |
|
101 | + { |
|
102 | 102 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
103 | 103 | if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
104 | 104 | return ''; |