-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCipher.php
366 lines (332 loc) · 10.4 KB
/
Cipher.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
<?php
/**
* Cipher allows to do the following operations:
* - encrypt a text using one of the algorithms delivered by CipherSuite class
* - decrypt a text using one of the algorithms delivered by CiphreSuite class
*
* PHP version 5
*
* @category Encryption
* @package PageProtectionPlus
* @author Pawel Wilk <[email protected]>
* @copyright 2006, 2007 Pawel Wilk
* @license http://www.gnu.org/licenses/gpl.html General Public License version 2 or higher
* @version 2.3b
* @link http://www.mediawiki.org/wiki/Extension:PPP
*/
require_once("CipherSuite.php");
require_once("engines/RandKeyGen.php");
/**
* Handles encryption and decryption.
*/
class Cipher extends CipherSuite {
private $mCipher;
private $mKey;
private $mIV;
public $mLastKey;
public $mLastIV;
public $mLastKeySize;
public $mLastIVSize;
public $mLastCipher;
/**
* Constructor.
*/
function Cipher($cipher='', $key='', $iv='')
{
$this->mErrorText = false;
$this->mCipher = '';
$this->mKey = '';
$this->mIV = '';
$this->mLastKey = '';
$this->mLastIV = '';
$this->mLastCipher = '';
$this->mLastKeySize = 0;
$this->mLastIVSize = 0;
$this->CipherSuite();
if ($cipher !== '') {
if (!$this->SetCipher($cipher)) {
return $this;
}
}
if ($key !== '') {
if (!$this->SetKey($key)) {
return $this;
}
}
if ($iv !== '') {
if (!$this->SetIV($iv)) {
return $this;
}
}
}
/**
* Verifies cipher's identifier used to encrypt/decrypt data.
* @param Cipher cipher's identifier.
* @param For_encrypt set it to true to check the give cipher name against only encryption ciphers (default is false).
* @return Returns true on success or false when the given cipher is unknown or not cupported in encryption mode.
*/
protected function VerifyCipher($cipher,$for_encrypt=false)
{
if ($for_encrypt === true && $this->have_cipher($cipher)) {
if (!$this->have_enc_cipher($cipher) ) {
$this->mErrorText = 'cipher ' . $cipher . ' is not supported for the encryption';
throw new Exception($this->mErrorText);
return false;
} else {
return true;
}
}
if ( !$this->have_cipher($cipher) ) {
$this->mErrorText = 'unknown cipher: ' . $cipher;
throw new Exception($this->mErrorText);
return false;
}
return true;
}
/**
* Sets cipher used to encrypt/decrypt data. Additionaly it checks compliance with key and IV sizes.
* @param Cipher sets the default cipher by its name or sets most preferred if not given.
* @param For_encrypt set it to true to check the give cipher name against only encryption ciphers (default is false).
* @return Returns true on success or false when the given cipher is unknown.
*/
public function SetCipher($cipher='', $for_encrypt=false)
{
if ($cipher === '') {
$cipher = $this->get_preferred_cipher();
} else {
$cipher = $this->correct_cipher_name($cipher);
if ( !$this->VerifyCipher($cipher, $for_encrypt) ) {
$this->mErrorText = 'Cipher::SetCipher(): ' . $this->mErrorText;
throw new Exception($this->mErrorText);
return false;
}
}
if ($key !== false && $key !== '' &&
$iv !== false && $iv !== '') {
$key_size = 0;
$iv_size = 0;
$this->validate_sizes('SetCipher', $cipher, $key, $iv, $key_size, $iv_size);
}
$this->mCipher = $cipher;
return true;
}
/**
* Sets key used to encrypt/decrypt data.
* @param Key key or empty string (which means to generate random key).
* @return Returns true on success or false when key size is wrong.
*/
public function SetKey($key='')
{
if ($this->mCipher === '') {
$cipher = $this->get_preferred_cipher();
} else {
$cipher = $this->mCipher;
}
$key_size = 0;
$iv_size = 0;
if ($key !== '') {
if ($this->mCipher !== '') {
$key_size = 0;
$iv_size = 0;
$this->validate_sizes('SetCipher', $cipher, $key, $iv, $key_size, $iv_size);
}
$this->mKey = $key;
return true;
} else {
$this->mKey = RandKeyGen($key_size);
return true;
}
}
/**
* Sets IV used to encrypt/decrypt data.
* @param IV initialization vactor or empty string (which means to generate random IV).
* @return Returns true on success or false when IV size is wrong.
*/
public function SetIV($iv='')
{
if ($this->mCipher === '') {
$cipher = $this->get_preferred_cipher();
} else {
$cipher = $this->mCipher;
}
if ($key !== '') {
if ($this->mCipher !== '') {
$key_size = 0;
$iv_size = 0;
$this->validate_sizes('SetIV', $cipher, $key, $iv, $key_size, $iv_size);
}
$this->mIV = $iv;
return true;
} else {
$this->mKey = RandKeyGen($liked_iv_size);
return true;
}
}
/**
* Encrypts portion of a data.
* @param Text data to be encrypted.
* @param Cipher cipher to use (if empty, then the already set default will be used or default)
* @param Key key for the cipher (if empty, the random key will be generated and stored into Cipher::mLastKey)
* @param IV initialization vector to use (if empty, the random will be picked up and stored into Cipher::mLastIV)
* @return encoded text or false - see also Cipher::last_error()
*/
public function Encipher($text, $cipher='', $key='', $iv='')
{
if ($cipher === '') {
if ($this->mCipher !== '') {
$cipher = $this->mCipher;
} else {
$cipher = $this->get_preferred_cipher();
}
}
$cipher = $this->correct_cipher_name($cipher);
if ( !$this->VerifyCipher($cipher, true) ) {
$this->mErrorText = 'Cipher::Encipher(): ' . $this->mErrorText;
throw new Exception($this->mErrorText);
return false;
}
/* caution: key and IV sizes may be smaller! */
$key_size = 0;
$iv_size = 0;
$this->validate_sizes('Encipher', $cipher, $key, $iv, $key_size, $iv_size);
if ($this->type($cipher) === 'symmetric') {
if ($key === '' && $this->mKey !== '') {
$key = $this->mKey;
} else {
$key = RandKeyGen($key_size);
}
if ($iv === '' && $this->mIV !== '') {
$iv = $this->mIV;
} else {
$iv = RandKeyGen($iv_size);
}
$this->validate_sizes('Encipher', $cipher, $key, $iv, $key_size, $iv_size);
}
$this->mLastKey = $key;
$this->mLastIV = $iv;
$this->mLastKeySize = $key_size;
$this->mLastIVSize = $iv_size;
$this->mLastCipher = $cipher;
/* $ret = $this->call_cipher_func($cipher, 'init');
if ($ret === false) {
if ($this->mErrorText !== false && $this->mErrorText != '') {
$this->mErrorText = 'Cipher: error while initializing: ' . $this->mErrorText;
throw new Exception($this->mErrorText);
}
throw new Exception("Cipher: internal error while initializing");
return false;
}
*/
$text = $this->call_cipher_func($cipher, 'encrypt', $cipher, $key, $text, $iv);
if ($text === false) {
if ($this->mErrorText !== false && $this->mErrorText != '') {
$this->mErrorText = 'Cipher: error while encrypting: ' . $this->mErrorText;
throw new Exception($this->mErrorText);
}
throw new Exception("Cipher: internal error while encrypting");
}
return $text;
}
/**
* Decrypts portion of a data.
* @param Text data to be encrypted.
* @param Cipher cipher to use (if undefined the previously set cipher will be used).
* @param Key key for the cipher (if undefined the previously set key will be used).
* @param IV IV to use; if undefined (ok for some ciphers) the empty string will be passed to the engine, but only if the key was empty too.
* @return decoded text or false - see also Cipher::last_error()
*/
public function Decipher($text, $cipher='', $key='', $iv='')
{
if ($cipher === '' || $cipher === false) {
if ($this->mCipher === '') {
$this->mErrorText = 'Cipher::Decipher(): error while decrypting: cipher not specified';
throw new Exception($this->mErrorText);
return false;
} else {
$cipher = $this->mCipher;
}
}
if ($key === '' || $key === false) {
if ($this->mKey === false) {
$this->mErrorText = 'Cipher::Decipher(): error while decrypting: key not specified';
throw new Exception($this->mErrorText);
return false;
} else {
$key = $this->mKey;
if ($iv === '' || $iv === false) {
$iv = $this->mIV;
}
}
}
$cipher = $this->correct_cipher_name($cipher);
if ( !$this->VerifyCipher($cipher, true) ) {
$this->mErrorText = 'Cipher::Decipher(): ' . $this->mErrorText;
throw new Exception($this->mErrorText);
return false;
}
$this->validate_sizes('Decipher', $cipher, $key, $iv, $key_size, $iv_size);
$text = $this->call_cipher_func($cipher, 'decrypt', $cipher, $key, $text, $iv);
if ($text === false) {
if ($this->mErrorText !== false && $this->mErrorText != '') {
$this->mErrorText = 'Cipher: error while decrypting: ' . $this->mErrorText;
throw new Exception($this->mErrorText);
}
}
return $text;
}
protected function validate_sizes($name, $cipher, $key, $iv, &$key_size, &$iv_size)
{
$iv_size = strlen($iv);
if ($cipher === false || $cipher === '') {
$cipher = $this->get_preferred_cipher();
}
$liked_iv_size = $this->iv_size($cipher);
if ($iv === false || $iv === '') {
$iv_size = $liked_iv_size;
}
if ($this->type($cipher) === 'asymmetric') {
$key_size = $this->asymmetric_key_size($key);
$liked_key_size = $this->asymmetric_key_size();
} else {
$key_size = strlen($key);
$liked_key_size = $this->key_size($cipher);
}
if ($key === false || $key === '') {
$key_size = $liked_key_size;
}
if ($key !== '' && $key_size > $liked_key_size) {
$this->mErrorText = 'Cipher::' . $name .
' given key size ' .
$key_size .
' is bigger than ' .
$liked_key_size .
' for the alghoritm ' .
$cipher;
throw new Exception($this->mErrorText);
return false;
}
if ($iv !== '' && $iv_size > $liked_iv_size) {
$this->mErrorText = 'Cipher::' . $name .
' given IV size ' .
$iv_size .
' is bigger than ' .
$liked_iv_size .
' for the alghoritm ' .
$cipher;
throw new Exception($this->mErrorText);
return false;
}
}
/**
* Checks whether last operation returned a text containing error message.
* @return True when error message was generated or false if not.
*/
public function HasError()
{
if ($this->mErrorText !== false) {
return true;
}
return false;
}
}
?>