-
Notifications
You must be signed in to change notification settings - Fork 18
/
Mode.php
372 lines (309 loc) · 9.3 KB
/
Mode.php
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/*
* Author: Ryan Gilfether
* URL: http://www.gilfether.com/phpCrypt
* Date: Sep 4, 2005
* Copyright (C) 2005 Ryan Gilfether
*
* This file is part of phpCrypt
*
* phpCrypt is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
namespace PHP_Crypt;
require_once(dirname(__FILE__)."/Padding.php");
include_once(dirname(__FILE__)."/phpCrypt.php");
/**
* A base class that should not be used directly. Instead, all mode classes should
* extend this one. It provides tools that may be used for any type of Mode.
*
* @author Ryan Gilfether
* @link http://www.gilfether.com/phpcrypt
* @copyright 2005 Ryan Gilfether
*/
abstract class Mode
{
/**
* @type object $cipher The cipher object used within the mode
*/
protected $cipher = null;
/**
* @type string $iv The IV used for the mode, not all Modes
* use an IV so this may be empty
*/
protected $iv = "";
/**
* @type string $register For modes that use a register to do
* encryption/decryption. This stores the unencrypted register.
*/
protected $register = "";
/**
* @type string $enc_register For modes that use a register to do
* encryption/decryption. This stores the encrypted register
*/
protected $enc_register = "";
/**
* @type integer $block_size The byte size of the block to
* encrypt/decrypt for the Mode
*/
private $block_size = 0;
/** @type string $mode_name The name of mode currently used */
private $mode_name = "";
/**
* @type string $padding The type of padding to use when required.
* Padding types are defined in phpCrypt class. Defaults to
* PHP_Crypt::PAD_ZERO
*/
private $padding = PHP_Crypt::PAD_ZERO;
/**
* Constructor
* Sets the cipher object that will be used for encryption
*
* @param object $cipher One of the phpCrypt encryption cipher objects
* @param string $mode_name The name of phpCrypt's modes, as defined in the mode
* @return void
*/
protected function __construct($mode_name, $cipher)
{
$this->name($mode_name);
$this->cipher($cipher);
}
/**
* Destructor
*
* @return void
*/
protected function __destruct()
{
}
/**********************************************************************
* ABSTRACT METHODS
*
* The abstract methods required by inheriting classes to implement
**********************************************************************/
/**
* All modes must have encrypt(), which implements
* the mode using the cipher's encrypiton algorithm
*
* @param string $text A String to encrypt
* @return boolean Always returns true
*/
abstract public function encrypt(&$text);
/**
* All modes must have decrypt(), which implements
* the mode using the cipher's encryption algorithm
*
* @param string $text A String to decrypt
* @return boolean Always returns true
*/
abstract public function decrypt(&$text);
/**
* Indicates whether or not a mode requires an IV
*
* @return boolean Always returns true or false
*/
abstract public function requiresIV();
/**********************************************************************
* PUBLIC METHODS
*
**********************************************************************/
/**
* Create an IV if the Mode used requires an IV.
* The IV should be saved and used for Encryption/Decryption
* of the same blocks of data.
* There are 3 ways to auto generate an IV by setting $src parameter
* PHP_Crypt::IV_RAND (default) - uses mt_rand
* PHP_Crypt::IV_DEV_RAND - uses /dev/random
* PHP_Crypt::IV_DEV_URAND - uses /dev/urandom
*
* @param string $src Optional, how the IV is generated, can be one of
* PHP_Crypt::IV_RAND, PHP_Crypt::IV_DEV_RAND, PHP_Crypt::IV_DEV_URAND
* @return string The IV that is being used by the mode
*/
public function createIV($src = null)
{
// if the mode does not use an IV, lets not waste time
if(!$this->requiresIV())
return false;
$iv = "";
if($src == PHP_Crypt::IV_DEV_RAND)
{
if(file_exists(PHP_Crypt::IV_DEV_RAND))
$iv = file_get_contents(PHP_CRYPT::IV_DEV_RAND, false, null, 0, $this->block_size);
else
trigger_error(PHP_Crypt::IV_DEV_RAND." not found. Try using PHP_CRYPT::IV_RAND", E_USER_WARNING);
}
else if($src == PHP_Crypt::IV_DEV_URAND)
{
if(file_exists(PHP_Crypt::IV_DEV_URAND))
$iv = file_get_contents(PHP_CRYPT::IV_DEV_URAND, false, null, 0, $this->block_size);
else
trigger_error(PHP_Crypt::IV_DEV_URAND." not found. Try using PHP_CRYPT::IV_RAND", E_USER_WARNING);
}
else // $src == PHP_Crypt::IV_RAND
{
for($i = 0; $i < $this->block_size; ++$i)
$iv .= chr(mt_rand(0, 255));
}
// now store the IV we created
return $this->IV($iv);
}
/**
* Sets or Returns an IV for the mode to use
*
* @param string $iv Optional, An IV to use for the mode and cipher selected
* @return void
*/
public function IV($iv = null)
{
if($iv != null)
{
// check that the iv is the correct length,
$len = strlen($iv);
if($len != $this->block_size)
{
$msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes";
trigger_error($msg, E_USER_WARNING);
}
$this->clearRegisters();
$this->register = $iv;
$this->iv = $iv;
}
return $this->iv;
}
/**
* Checks to see if the current mode requires an IV and that it is set
* if it is required. Triggers E_USER_WARNING an IV is required and not set
*
* @return void
*/
public function checkIV()
{
if($this->requiresIV() && strlen($this->register) == 0)
{
$msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty";
trigger_error($msg, E_USER_WARNING);
}
}
/**
* Sets and returns the name of the mode being used
* If $name parameter is set, sets the mode. If
* $name is not set, returns the current mode in use
*
* @param string $name Optional, One of the predefined
* phpCrypt mode constant names
* @return string One of the predefined phpCrypt mode
* constant mode names
*/
public function name($name = "")
{
if($name != "")
$this->mode_name = $name;
return $this->mode_name;
}
/**
* Sets or Returns the padding type used with the mode
* If the $type parameter is not given, this function
* returns the the padding type only.
*
* @param string $type One of the predefined padding types
* @return void
*/
public function padding($type = "")
{
if($type != "")
$this->padding = $type;
return $this->padding;
}
/**
* Returns or Sets the cipher object being used
* If the $cipher parameter is set with a cipher object,
* the cipher current cipher will be set to this cipher.
* If the $cipher parameter is not set, returns the
* current cipher object being used.
*
* @param object $cipher Optional, An object of type Cipher
* @return object An object of type Cipher
*/
public function cipher($cipher = null)
{
if(is_object($cipher))
$this->cipher = $cipher;
return $this->cipher;
}
/**********************************************************************
* PROTECTED METHODS
*
**********************************************************************/
/**
* Pads str so that final block is $block_bits in size, if the final block
* is $block_bits, then an additional block is added that is $block_bits in size
* The padding should be set by phpCrypt::setPadding()
*
* @param string $str the string to be padded
* @return boolean Returns true
*/
protected function pad(&$str)
{
$len = strlen($str);
$bytes = $this->blockSize(); // returns bytes
// now determine the next multiple of blockSize(), then find
// the difference between that and the length of $str,
// this is how many padding bytes we will need
$num = ceil($len / $bytes) * $bytes;
$num = $num - $len;
Padding::pad($str, $num, $this->padding);
return true;
}
/**
* Strip out the padded blocks created from Pad().
* Padding type should be set by phpCrypt::setPadding()
*
* @param string $str the string to strip padding from
* @return boolean Returns True
*/
protected function strip(&$str)
{
Padding::strip($str, $this->padding);
return true;
}
/**
* Sets or Returns the size of block (in bytes) the Mode should use
* for the Cipher's Encryption, for example DES uses 64bit data, which
* means our BlockSize will be 8 bytes
*
* @param int $bytes Options, The size of the block in bytes
* @return int The currently set block size, in bytes
*/
protected function blockSize($bytes = 0)
{
if($bytes > 0)
$this->block_size = $bytes;
return $this->block_size;
}
/**********************************************************************
* PRIVATE METHODS
*
**********************************************************************/
/**
* Clears the registers used for some modes
*
* @return void
*/
private function clearRegisters()
{
$this->register = "";
$this->enc_register = "";
}
}
?>