458
|
1 |
<?php
|
|
2 |
|
|
3 |
/**
|
|
4 |
* Phijndael - an implementation of the AES encryption standard in PHP
|
|
5 |
* Originally written by Fritz Schneider <fritz AT cd DOT ucsd DOT edu>
|
|
6 |
* Ported to PHP by Dan Fuhry <dan AT enano DOT homelinux DOT org>
|
|
7 |
* @package phijndael
|
|
8 |
* @author Fritz Schneider
|
|
9 |
* @author Dan Fuhry
|
|
10 |
* @license BSD-style license
|
|
11 |
*/
|
|
12 |
|
461
|
13 |
define ('ENC_HEX', 201);
|
458
|
14 |
define ('ENC_BASE64', 202);
|
|
15 |
define ('ENC_BINARY', 203);
|
|
16 |
|
|
17 |
$_aes_objcache = array();
|
|
18 |
|
|
19 |
class AESCrypt {
|
|
20 |
|
|
21 |
var $debug = false;
|
|
22 |
var $mcrypt = false;
|
|
23 |
var $decrypt_cache = array();
|
|
24 |
|
|
25 |
// Rijndael parameters -- Valid values are 128, 192, or 256
|
|
26 |
|
|
27 |
var $keySizeInBits = 128;
|
|
28 |
var $blockSizeInBits = 128;
|
|
29 |
|
|
30 |
/////// You shouldn't have to modify anything below this line except for
|
|
31 |
/////// the function getRandomBytes().
|
|
32 |
//
|
|
33 |
// Note: in the following code the two dimensional arrays are indexed as
|
|
34 |
// you would probably expect, as array[row][column]. The state arrays
|
|
35 |
// are 2d arrays of the form state[4][Nb].
|
|
36 |
|
|
37 |
|
|
38 |
// The number of rounds for the cipher, indexed by [Nk][Nb]
|
|
39 |
var $roundsArray = Array(0,0,0,0,Array(0,0,0,0,10,0, 12,0, 14),0,
|
|
40 |
Array(0,0,0,0,12,0, 12,0, 14),0,
|
|
41 |
Array(0,0,0,0,14,0, 14,0, 14) );
|
|
42 |
|
|
43 |
// The number of bytes to shift by in shiftRow, indexed by [Nb][row]
|
|
44 |
var $shiftOffsets = Array(0,0,0,0,Array(0,1, 2, 3),0,Array(0,1, 2, 3),0,Array(0,1, 3, 4) );
|
|
45 |
|
|
46 |
// The round constants used in subkey expansion
|
|
47 |
var $Rcon = Array(
|
|
48 |
0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
|
|
49 |
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
|
|
50 |
0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc,
|
|
51 |
0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4,
|
|
52 |
0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91 );
|
|
53 |
|
|
54 |
// Precomputed lookup table for the SBox
|
|
55 |
var $SBox = Array(
|
|
56 |
99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171,
|
|
57 |
118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164,
|
|
58 |
114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113,
|
|
59 |
216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226,
|
|
60 |
235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214,
|
|
61 |
179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203,
|
|
62 |
190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69,
|
|
63 |
249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245,
|
|
64 |
188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68,
|
|
65 |
23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42,
|
|
66 |
144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73,
|
|
67 |
6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109,
|
|
68 |
141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37,
|
|
69 |
46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62,
|
|
70 |
181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225,
|
|
71 |
248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
|
|
72 |
140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187,
|
|
73 |
22 );
|
|
74 |
|
|
75 |
// Precomputed lookup table for the inverse SBox
|
|
76 |
var $SBoxInverse = Array(
|
|
77 |
82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215,
|
|
78 |
251, 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222,
|
|
79 |
233, 203, 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66,
|
|
80 |
250, 195, 78, 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73,
|
|
81 |
109, 139, 209, 37, 114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92,
|
|
82 |
204, 93, 101, 182, 146, 108, 112, 72, 80, 253, 237, 185, 218, 94, 21,
|
|
83 |
70, 87, 167, 141, 157, 132, 144, 216, 171, 0, 140, 188, 211, 10, 247,
|
|
84 |
228, 88, 5, 184, 179, 69, 6, 208, 44, 30, 143, 202, 63, 15, 2,
|
|
85 |
193, 175, 189, 3, 1, 19, 138, 107, 58, 145, 17, 65, 79, 103, 220,
|
|
86 |
234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116, 34, 231, 173,
|
|
87 |
53, 133, 226, 249, 55, 232, 28, 117, 223, 110, 71, 241, 26, 113, 29,
|
|
88 |
41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27, 252, 86, 62, 75,
|
|
89 |
198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244, 31, 221, 168,
|
|
90 |
51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95, 96, 81,
|
|
91 |
127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239, 160,
|
|
92 |
224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97,
|
|
93 |
23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12,
|
|
94 |
125 );
|
|
95 |
|
|
96 |
function __construct($ks = 128, $bs = 128, $debug = false)
|
|
97 |
{
|
|
98 |
$this->keySizeInBits = $ks;
|
|
99 |
$this->blockSizeInBits = $bs;
|
|
100 |
|
|
101 |
// Use the Mcrypt library? This speeds things up dramatically.
|
|
102 |
if(defined('MCRYPT_RIJNDAEL_' . $ks) && defined('MCRYPT_ACCEL'))
|
|
103 |
{
|
|
104 |
eval('$mcb = MCRYPT_RIJNDAEL_' . $ks.';');
|
|
105 |
$bks = mcrypt_module_get_algo_block_size($mcb);
|
|
106 |
$bks = $bks * 8;
|
|
107 |
if ( $bks != $bs )
|
|
108 |
{
|
|
109 |
$mcb = false;
|
|
110 |
echo (string)$bks;
|
|
111 |
}
|
|
112 |
}
|
|
113 |
else
|
|
114 |
{
|
|
115 |
$mcb = false;
|
|
116 |
}
|
|
117 |
|
|
118 |
$this->mcrypt = $mcb;
|
|
119 |
|
|
120 |
// Cipher parameters ... do not change these
|
|
121 |
$this->Nk = $this->keySizeInBits / 32;
|
|
122 |
$this->Nb = $this->blockSizeInBits / 32;
|
|
123 |
$this->Nr = $this->roundsArray[$this->Nk][$this->Nb];
|
|
124 |
$this->debug = $debug;
|
|
125 |
}
|
|
126 |
|
|
127 |
public static function singleton($key_size, $block_size)
|
|
128 |
{
|
|
129 |
static $_aes_objcache;
|
|
130 |
if ( isset($_aes_objcache["$key_size,$block_size"]) )
|
|
131 |
{
|
|
132 |
return $_aes_objcache["$key_size,$block_size"];
|
|
133 |
}
|
|
134 |
|
|
135 |
$_aes_objcache["$key_size,$block_size"] = new AESCrypt($key_size, $block_size);
|
|
136 |
return $_aes_objcache["$key_size,$block_size"];
|
|
137 |
}
|
|
138 |
|
|
139 |
// Error handler
|
|
140 |
|
|
141 |
function trigger_error($text, $level = E_USER_NOTICE)
|
|
142 |
{
|
|
143 |
$bt = debug_backtrace();
|
|
144 |
$lastfunc =& $bt[1];
|
|
145 |
switch($level)
|
|
146 |
{
|
|
147 |
case E_USER_NOTICE:
|
|
148 |
default:
|
|
149 |
$desc = 'Notice';
|
|
150 |
break;
|
|
151 |
case E_USER_WARNING:
|
|
152 |
$desc = 'Warning';
|
|
153 |
break;
|
|
154 |
case E_USER_ERROR:
|
|
155 |
$desc = 'Fatal';
|
|
156 |
break;
|
|
157 |
}
|
|
158 |
ob_start();
|
|
159 |
if($this->debug || $level == E_USER_ERROR) echo "AES encryption: <b>{$desc}:</b> $text in {$lastfunc['file']} on line {$lastfunc['line']} in function {$lastfunc['function']}<br />";
|
|
160 |
if($this->debug)
|
|
161 |
{
|
|
162 |
//echo '<pre>'.enano_debug_print_backtrace(true).'</pre>';
|
|
163 |
}
|
|
164 |
ob_end_flush();
|
|
165 |
if($level == E_USER_ERROR)
|
|
166 |
{
|
|
167 |
echo '<p><b>This can sometimes happen if you are upgrading Enano to a new version and did not log out first.</b> <a href="'.$_SERVER['PHP_SELF'].'?do=diag&sub=cookie_destroy">Click here</a> to force cookies to clear and try again. You will be logged out.</p>';
|
|
168 |
exit;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
function array_slice_js_compat($array, $start, $finish = 0)
|
|
173 |
{
|
|
174 |
$len = $finish - $start;
|
|
175 |
if($len < 0) $len = 0 - $len;
|
|
176 |
//if($this->debug) echo (string)$len . ' ';
|
|
177 |
//if(count($array) < $start + $len)
|
|
178 |
// $this->trigger_error('Index out of range', E_USER_WARNING);
|
|
179 |
return array_slice($array, $start, $len);
|
|
180 |
}
|
|
181 |
|
|
182 |
function concat($s1, $s2)
|
|
183 |
{
|
|
184 |
if(is_array($s1) && is_array($s2))
|
|
185 |
return array_merge($s1, $s2);
|
|
186 |
elseif( ( is_array($s1) && !is_array($s2) ) || ( !is_array($s1) && is_array($s2) ) )
|
|
187 |
{
|
|
188 |
$this->trigger_error('incompatible types - you can\'t combine a non-array with an array', E_USER_WARNING);
|
|
189 |
return false;
|
|
190 |
}
|
|
191 |
else
|
|
192 |
return $s1 . $s2;
|
|
193 |
}
|
|
194 |
|
|
195 |
// This method circularly shifts the array left by the number of elements
|
|
196 |
// given in its parameter. It returns the resulting array and is used for
|
|
197 |
// the ShiftRow step. Note that shift() and push() could be used for a more
|
|
198 |
// elegant solution, but they require IE5.5+, so I chose to do it manually.
|
|
199 |
|
|
200 |
function cyclicShiftLeft($theArray, $positions) {
|
|
201 |
if(!is_int($positions))
|
|
202 |
{
|
|
203 |
$this->trigger_error('$positions is not an integer! Backtrace:<br /><pre>'.print_r(debug_backtrace(), true).'</pre>', E_USER_WARNING);
|
|
204 |
return false;
|
|
205 |
}
|
|
206 |
$second = array_slice($theArray, 0, $positions);
|
|
207 |
$first = array_slice($theArray, $positions);
|
|
208 |
$theArray = array_merge($first, $second);
|
|
209 |
return $theArray;
|
|
210 |
}
|
|
211 |
|
|
212 |
// Multiplies the element "poly" of GF(2^8) by x. See the Rijndael spec.
|
|
213 |
|
|
214 |
function xtime($poly) {
|
|
215 |
$poly <<= 1;
|
|
216 |
return (($poly & 0x100) ? ($poly ^ 0x11B) : ($poly));
|
|
217 |
}
|
|
218 |
|
|
219 |
// Multiplies the two elements of GF(2^8) together and returns the result.
|
|
220 |
// See the Rijndael spec, but should be straightforward: for each power of
|
|
221 |
// the indeterminant that has a 1 coefficient in x, add y times that power
|
|
222 |
// to the result. x and y should be bytes representing elements of GF(2^8)
|
|
223 |
|
|
224 |
function mult_GF256($x, $y) {
|
|
225 |
$result = 0;
|
|
226 |
|
|
227 |
for ($bit = 1; $bit < 256; $bit *= 2, $y = $this->xtime($y)) {
|
|
228 |
if ($x & $bit)
|
|
229 |
$result ^= $y;
|
|
230 |
}
|
|
231 |
return $result;
|
|
232 |
}
|
|
233 |
|
|
234 |
// Performs the substitution step of the cipher. State is the 2d array of
|
|
235 |
// state information (see spec) and direction is string indicating whether
|
|
236 |
// we are performing the forward substitution ("encrypt") or inverse
|
|
237 |
// substitution (anything else)
|
|
238 |
|
|
239 |
function byteSub(&$state, $direction) {
|
|
240 |
//global $this->SBox, $this->SBoxInverse, $this->Nb;
|
|
241 |
if ($direction == "encrypt") // Point S to the SBox we're using
|
|
242 |
$S =& $this->SBox;
|
|
243 |
else
|
|
244 |
$S =& $this->SBoxInverse;
|
|
245 |
for ($i = 0; $i < 4; $i++) // Substitute for every byte in state
|
|
246 |
for ($j = 0; $j < $this->Nb; $j++)
|
|
247 |
$state[$i][$j] = $S[$state[$i][$j]];
|
|
248 |
}
|
|
249 |
|
|
250 |
// Performs the row shifting step of the cipher.
|
|
251 |
|
|
252 |
function shiftRow(&$state, $direction) {
|
|
253 |
//global $this->Nb, $this->shiftOffsets;
|
|
254 |
for ($i=1; $i<4; $i++) // Row 0 never shifts
|
|
255 |
if ($direction == "encrypt")
|
|
256 |
$state[$i] = $this->cyclicShiftLeft($state[$i], $this->shiftOffsets[$this->Nb][$i]);
|
|
257 |
else
|
|
258 |
$state[$i] = $this->cyclicShiftLeft($state[$i], $this->Nb - $this->shiftOffsets[$this->Nb][$i]);
|
|
259 |
|
|
260 |
}
|
|
261 |
|
|
262 |
// Performs the column mixing step of the cipher. Most of these steps can
|
|
263 |
// be combined into table lookups on 32bit values (at least for encryption)
|
|
264 |
// to greatly increase the speed.
|
|
265 |
|
|
266 |
function mixColumn(&$state, $direction) {
|
|
267 |
//global $this->Nb;
|
|
268 |
$b = Array(); // Result of matrix multiplications
|
|
269 |
for ($j = 0; $j < $this->Nb; $j++) { // Go through each column...
|
|
270 |
for ($i = 0; $i < 4; $i++) { // and for each row in the column...
|
|
271 |
if ($direction == "encrypt")
|
|
272 |
$b[$i] = $this->mult_GF256($state[$i][$j], 2) ^ // perform mixing
|
|
273 |
$this->mult_GF256($state[($i+1)%4][$j], 3) ^
|
|
274 |
$state[($i+2)%4][$j] ^
|
|
275 |
$state[($i+3)%4][$j];
|
|
276 |
else
|
|
277 |
$b[$i] = $this->mult_GF256($state[$i][$j], 0xE) ^
|
|
278 |
$this->mult_GF256($state[($i+1)%4][$j], 0xB) ^
|
|
279 |
$this->mult_GF256($state[($i+2)%4][$j], 0xD) ^
|
|
280 |
$this->mult_GF256($state[($i+3)%4][$j], 9);
|
|
281 |
}
|
|
282 |
for ($i = 0; $i < 4; $i++) // Place result back into column
|
|
283 |
$state[$i][$j] = $b[$i];
|
|
284 |
}
|
|
285 |
}
|
|
286 |
|
|
287 |
// Adds the current round key to the state information. Straightforward.
|
|
288 |
|
|
289 |
function addRoundKey(&$state, $roundKey) {
|
|
290 |
//global $this->Nb;
|
|
291 |
for ($j = 0; $j < $this->Nb; $j++) { // Step through columns...
|
|
292 |
$state[0][$j] ^= ( $roundKey[$j] & 0xFF); // and XOR
|
|
293 |
$state[1][$j] ^= (($roundKey[$j]>>8) & 0xFF);
|
|
294 |
$state[2][$j] ^= (($roundKey[$j]>>16) & 0xFF);
|
|
295 |
$state[3][$j] ^= (($roundKey[$j]>>24) & 0xFF);
|
|
296 |
}
|
|
297 |
}
|
|
298 |
|
|
299 |
// This function creates the expanded key from the input (128/192/256-bit)
|
|
300 |
// key. The parameter key is an array of bytes holding the value of the key.
|
|
301 |
// The returned value is an array whose elements are the 32-bit words that
|
|
302 |
// make up the expanded key.
|
|
303 |
|
|
304 |
function keyExpansion($key) {
|
|
305 |
//global $this->keySizeInBits, $this->blockSizeInBits, $this->roundsArray, $this->Nk, $this->Nb, $this->Nr, $this->Nk, $this->SBox, $this->Rcon;
|
|
306 |
$expandedKey = Array();
|
|
307 |
|
|
308 |
// in case the key size or parameters were changed...
|
|
309 |
$this->Nk = $this->keySizeInBits / 32;
|
|
310 |
$this->Nb = $this->blockSizeInBits / 32;
|
|
311 |
$this->Nr = $this->roundsArray[$this->Nk][$this->Nb];
|
|
312 |
|
|
313 |
for ($j=0; $j < $this->Nk; $j++) // Fill in input key first
|
|
314 |
$expandedKey[$j] =
|
|
315 |
($key[4*$j]) | ($key[4*$j+1]<<8) | ($key[4*$j+2]<<16) | ($key[4*$j+3]<<24);
|
|
316 |
|
|
317 |
// Now walk down the rest of the array filling in expanded key bytes as
|
|
318 |
// per Rijndael's spec
|
|
319 |
for ($j = $this->Nk; $j < $this->Nb * ($this->Nr + 1); $j++) { // For each word of expanded key
|
|
320 |
$temp = $expandedKey[$j - 1];
|
|
321 |
if ($j % $this->Nk == 0)
|
|
322 |
$temp = ( ($this->SBox[($temp>>8) & 0xFF]) |
|
|
323 |
($this->SBox[($temp>>16) & 0xFF]<<8) |
|
|
324 |
($this->SBox[($temp>>24) & 0xFF]<<16) |
|
|
325 |
($this->SBox[$temp & 0xFF]<<24) ) ^ $this->Rcon[floor($j / $this->Nk) - 1];
|
|
326 |
elseif ($this->Nk > 6 && $j % $this->Nk == 4)
|
|
327 |
$temp = ($this->SBox[($temp>>24) & 0xFF]<<24) |
|
|
328 |
($this->SBox[($temp>>16) & 0xFF]<<16) |
|
|
329 |
($this->SBox[($temp>>8) & 0xFF]<<8) |
|
|
330 |
($this->SBox[ $temp & 0xFF]);
|
|
331 |
$expandedKey[$j] = $expandedKey[$j-$this->Nk] ^ $temp;
|
|
332 |
}
|
|
333 |
return $expandedKey;
|
|
334 |
}
|
|
335 |
|
|
336 |
// Rijndael's round functions...
|
|
337 |
|
|
338 |
function RijndaelRound(&$state, $roundKey) {
|
|
339 |
$this->byteSub($state, "encrypt");
|
|
340 |
$this->shiftRow($state, "encrypt");
|
|
341 |
$this->mixColumn($state, "encrypt");
|
|
342 |
$this->addRoundKey($state, $roundKey);
|
|
343 |
}
|
|
344 |
|
|
345 |
function InverseRijndaelRound(&$state, $roundKey) {
|
|
346 |
$this->addRoundKey($state, $roundKey);
|
|
347 |
$this->mixColumn($state, "decrypt");
|
|
348 |
$this->shiftRow($state, "decrypt");
|
|
349 |
$this->byteSub($state, "decrypt");
|
|
350 |
}
|
|
351 |
|
|
352 |
function FinalRijndaelRound(&$state, $roundKey) {
|
|
353 |
$this->byteSub($state, "encrypt");
|
|
354 |
$this->shiftRow($state, "encrypt");
|
|
355 |
$this->addRoundKey($state, $roundKey);
|
|
356 |
}
|
|
357 |
|
|
358 |
function InverseFinalRijndaelRound(&$state, $roundKey){
|
|
359 |
$this->addRoundKey($state, $roundKey);
|
|
360 |
$this->shiftRow($state, "decrypt");
|
|
361 |
$this->byteSub($state, "decrypt");
|
|
362 |
}
|
|
363 |
|
|
364 |
// encrypt is the basic encryption function. It takes parameters
|
|
365 |
// block, an array of bytes representing a plaintext block, and expandedKey,
|
|
366 |
// an array of words representing the expanded key previously returned by
|
|
367 |
// keyExpansion(). The ciphertext block is returned as an array of bytes.
|
|
368 |
|
|
369 |
function cryptBlock($block, $expandedKey) {
|
|
370 |
//global $this->blockSizeInBits, $this->Nb, $this->Nr;
|
|
371 |
$t=count($block)*8;
|
|
372 |
if (!is_array($block) || count($block)*8 != $this->blockSizeInBits)
|
|
373 |
{
|
|
374 |
$this->trigger_error('block is bad or block size is wrong<pre>'.print_r($block, true).'</pre><p>Aiming for size '.$this->blockSizeInBits.', got '.$t.'.', E_USER_WARNING);
|
|
375 |
return false;
|
|
376 |
}
|
|
377 |
if (!$expandedKey)
|
|
378 |
return;
|
|
379 |
|
|
380 |
$block = $this->packBytes($block);
|
|
381 |
$this->addRoundKey($block, $expandedKey);
|
|
382 |
for ($i=1; $i<$this->Nr; $i++)
|
|
383 |
$this->RijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$i, $this->Nb*($i+1)));
|
|
384 |
$this->FinalRijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$this->Nr));
|
|
385 |
$ret = $this->unpackBytes($block);
|
|
386 |
return $ret;
|
|
387 |
}
|
|
388 |
|
|
389 |
// decrypt is the basic decryption function. It takes parameters
|
|
390 |
// block, an array of bytes representing a ciphertext block, and expandedKey,
|
|
391 |
// an array of words representing the expanded key previously returned by
|
|
392 |
// keyExpansion(). The decrypted block is returned as an array of bytes.
|
|
393 |
|
|
394 |
function unCryptBlock($block, $expandedKey) {
|
|
395 |
$t = count($block)*8;
|
|
396 |
if (!is_array($block) || count($block)*8 != $this->blockSizeInBits)
|
|
397 |
{
|
|
398 |
$this->trigger_error('$block is not a valid rijndael-block array: '.$this->byteArrayToHex($block).'<pre>'.print_r($block, true).'</pre><p>Block size is '.$t.', should be '.$this->blockSizeInBits.'</p>', E_USER_WARNING);
|
|
399 |
return false;
|
|
400 |
}
|
|
401 |
if (!$expandedKey)
|
|
402 |
{
|
|
403 |
$this->trigger_error('$expandedKey is invalid', E_USER_WARNING);
|
|
404 |
return false;
|
|
405 |
}
|
|
406 |
|
|
407 |
$block = $this->packBytes($block);
|
|
408 |
$this->InverseFinalRijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$this->Nr));
|
|
409 |
for ($i = $this->Nr - 1; $i>0; $i--)
|
|
410 |
{
|
|
411 |
$this->InverseRijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$i, $this->Nb*($i+1)));
|
|
412 |
}
|
|
413 |
$this->addRoundKey($block, $expandedKey);
|
|
414 |
$ret = $this->unpackBytes($block);
|
|
415 |
if(!is_array($ret))
|
|
416 |
{
|
|
417 |
$this->trigger_error('$ret is not an array', E_USER_WARNING);
|
|
418 |
}
|
|
419 |
return $ret;
|
|
420 |
}
|
|
421 |
|
|
422 |
// This method takes a byte array (byteArray) and converts it to a string by
|
|
423 |
// applying String.fromCharCode() to each value and concatenating the result.
|
|
424 |
// The resulting string is returned. Note that this function SKIPS zero bytes
|
|
425 |
// under the assumption that they are padding added in formatPlaintext().
|
|
426 |
// Obviously, do not invoke this method on raw data that can contain zero
|
|
427 |
// bytes. It is really only appropriate for printable ASCII/Latin-1
|
|
428 |
// values. Roll your own function for more robust functionality :)
|
|
429 |
|
|
430 |
function byteArrayToString($byteArray) {
|
|
431 |
$result = "";
|
|
432 |
for($i=0; $i<count($byteArray); $i++)
|
|
433 |
if ($byteArray[$i] != 0)
|
|
434 |
$result .= chr($byteArray[$i]);
|
|
435 |
return $result;
|
|
436 |
}
|
|
437 |
|
|
438 |
// This function takes an array of bytes (byteArray) and converts them
|
|
439 |
// to a hexadecimal string. Array element 0 is found at the beginning of
|
|
440 |
// the resulting string, high nibble first. Consecutive elements follow
|
|
441 |
// similarly, for example [16, 255] --> "10ff". The function returns a
|
|
442 |
// string.
|
|
443 |
|
|
444 |
/*
|
|
445 |
function byteArrayToHex($byteArray) {
|
|
446 |
$result = "";
|
|
447 |
if (!$byteArray)
|
|
448 |
return;
|
|
449 |
for ($i=0; $i<count($byteArray); $i++)
|
|
450 |
$result .= (($byteArray[$i]<16) ? "0" : "") + toString($byteArray[$i]); // magic number here is 16, not sure how to handle this...
|
|
451 |
|
|
452 |
return $result;
|
|
453 |
}
|
|
454 |
*/
|
|
455 |
function byteArrayToHex($arr)
|
|
456 |
{
|
|
457 |
$ret = '';
|
|
458 |
foreach($arr as $a)
|
|
459 |
{
|
|
460 |
$nibble = (string)dechex(intval($a));
|
|
461 |
if(strlen($nibble) == 1) $nibble = '0' . $nibble;
|
|
462 |
$ret .= $nibble;
|
|
463 |
}
|
|
464 |
return $ret;
|
|
465 |
}
|
|
466 |
|
|
467 |
// PHP equivalent of Javascript's toString()
|
|
468 |
function toString($bool)
|
|
469 |
{
|
|
470 |
if(is_bool($bool))
|
|
471 |
return ($bool) ? 'true' : 'false';
|
|
472 |
elseif(is_array($bool))
|
|
473 |
return implode(',', $bool);
|
|
474 |
else
|
|
475 |
return (string)$bool;
|
|
476 |
}
|
|
477 |
|
|
478 |
// This function converts a string containing hexadecimal digits to an
|
|
479 |
// array of bytes. The resulting byte array is filled in the order the
|
|
480 |
// values occur in the string, for example "10FF" --> [16, 255]. This
|
|
481 |
// function returns an array.
|
|
482 |
|
|
483 |
/*
|
|
484 |
function hexToByteArray($hexString) {
|
|
485 |
$byteArray = Array();
|
|
486 |
if (strlen($hexString) % 2) // must have even length
|
|
487 |
return;
|
|
488 |
if (strstr($hexString, "0x") == $hexString || strstr($hexString, "0X") == $hexString)
|
|
489 |
$hexString = substr($hexString, 2);
|
|
490 |
for ($i = 0; $i<strlen($hexString); $i++,$i++)
|
|
491 |
$byteArray[floor($i/2)] = intval(substr($hexString, $i, 2)); // again, that strange magic number: 16
|
|
492 |
return $byteArray;
|
|
493 |
}
|
|
494 |
*/
|
|
495 |
function hexToByteArray($str)
|
|
496 |
{
|
|
497 |
if(substr($str, 0, 2) == '0x' || substr($str, 0, 2) == '0X')
|
|
498 |
$str = substr($str, 2);
|
|
499 |
$arr = Array();
|
|
500 |
$str = $this->enano_str_split($str, 2);
|
|
501 |
foreach($str as $s)
|
|
502 |
{
|
|
503 |
$arr[] = intval(hexdec($s));
|
|
504 |
}
|
|
505 |
return $arr;
|
|
506 |
}
|
|
507 |
|
|
508 |
// This function packs an array of bytes into the four row form defined by
|
|
509 |
// Rijndael. It assumes the length of the array of bytes is divisible by
|
|
510 |
// four. Bytes are filled in according to the Rijndael spec (starting with
|
|
511 |
// column 0, row 0 to 3). This function returns a 2d array.
|
|
512 |
|
|
513 |
function packBytes($octets) {
|
|
514 |
$state = Array();
|
|
515 |
if (!$octets || count($octets) % 4)
|
|
516 |
return;
|
|
517 |
|
|
518 |
$state[0] = Array(); $state[1] = Array();
|
|
519 |
$state[2] = Array(); $state[3] = Array();
|
|
520 |
for ($j=0; $j<count($octets); $j = $j+4) {
|
|
521 |
$state[0][$j/4] = $octets[$j];
|
|
522 |
$state[1][$j/4] = $octets[$j+1];
|
|
523 |
$state[2][$j/4] = $octets[$j+2];
|
|
524 |
$state[3][$j/4] = $octets[$j+3];
|
|
525 |
}
|
|
526 |
return $state;
|
|
527 |
}
|
|
528 |
|
|
529 |
// This function unpacks an array of bytes from the four row format preferred
|
|
530 |
// by Rijndael into a single 1d array of bytes. It assumes the input "packed"
|
|
531 |
// is a packed array. Bytes are filled in according to the Rijndael spec.
|
|
532 |
// This function returns a 1d array of bytes.
|
|
533 |
|
|
534 |
function unpackBytes($packed) {
|
|
535 |
$result = Array();
|
|
536 |
for ($j=0; $j<count($packed[0]); $j++) {
|
|
537 |
$result[] = $packed[0][$j];
|
|
538 |
$result[] = $packed[1][$j];
|
|
539 |
$result[] = $packed[2][$j];
|
|
540 |
$result[] = $packed[3][$j];
|
|
541 |
}
|
|
542 |
return $result;
|
|
543 |
}
|
|
544 |
|
|
545 |
function charCodeAt($str, $i)
|
|
546 |
{
|
|
547 |
return ord(substr($str, $i, 1));
|
|
548 |
}
|
|
549 |
|
|
550 |
function fromCharCode($str)
|
|
551 |
{
|
|
552 |
return chr($str);
|
|
553 |
}
|
|
554 |
|
|
555 |
// This function takes a prospective plaintext (string or array of bytes)
|
|
556 |
// and pads it with zero bytes if its length is not a multiple of the block
|
|
557 |
// size. If plaintext is a string, it is converted to an array of bytes
|
|
558 |
// in the process. The type checking can be made much nicer using the
|
|
559 |
// instanceof operator, but this operator is not available until IE5.0 so I
|
|
560 |
// chose to use the heuristic below.
|
|
561 |
|
|
562 |
function formatPlaintext($plaintext) {
|
|
563 |
//global $this->blockSizeInBits;
|
|
564 |
$bpb = $this->blockSizeInBits / 8; // bytes per block
|
|
565 |
|
|
566 |
// if primitive string or String instance
|
|
567 |
if (is_string($plaintext)) {
|
|
568 |
$plaintext = $this->enano_str_split($plaintext);
|
|
569 |
// Unicode issues here (ignoring high byte)
|
|
570 |
for ($i=0; $i<sizeof($plaintext); $i++)
|
|
571 |
$plaintext[$i] = $this->charCodeAt($plaintext[$i], 0) & 0xFF;
|
|
572 |
}
|
|
573 |
|
|
574 |
for ($i = $bpb - (sizeof($plaintext) % $bpb); $i > 0 && $i < $bpb; $i--)
|
|
575 |
$plaintext[] = 0;
|
|
576 |
|
|
577 |
return $plaintext;
|
|
578 |
}
|
|
579 |
|
|
580 |
// Returns an array containing "howMany" random bytes. YOU SHOULD CHANGE THIS
|
|
581 |
// TO RETURN HIGHER QUALITY RANDOM BYTES IF YOU ARE USING THIS FOR A "REAL"
|
|
582 |
// APPLICATION. (edit: done, mt_rand() is relatively secure)
|
|
583 |
|
|
584 |
function getRandomBytes($howMany) {
|
|
585 |
$bytes = Array();
|
|
586 |
for ($i=0; $i<$howMany; $i++)
|
|
587 |
$bytes[$i] = mt_rand(0, 255);
|
|
588 |
return $bytes;
|
|
589 |
}
|
|
590 |
|
|
591 |
// rijndaelEncrypt(plaintext, key, mode)
|
|
592 |
// Encrypts the plaintext using the given key and in the given mode.
|
|
593 |
// The parameter "plaintext" can either be a string or an array of bytes.
|
|
594 |
// The parameter "key" must be an array of key bytes. If you have a hex
|
|
595 |
// string representing the key, invoke hexToByteArray() on it to convert it
|
|
596 |
// to an array of bytes. The third parameter "mode" is a string indicating
|
|
597 |
// the encryption mode to use, either "ECB" or "CBC". If the parameter is
|
|
598 |
// omitted, ECB is assumed.
|
|
599 |
//
|
|
600 |
// An array of bytes representing the cihpertext is returned. To convert
|
|
601 |
// this array to hex, invoke byteArrayToHex() on it. If you are using this
|
|
602 |
// "for real" it is a good idea to change the function getRandomBytes() to
|
|
603 |
// something that returns truly random bits.
|
|
604 |
|
|
605 |
function rijndaelEncrypt($plaintext, $key, $mode = 'ECB') {
|
|
606 |
//global $this->blockSizeInBits, $this->keySizeInBits;
|
|
607 |
$bpb = $this->blockSizeInBits / 8; // bytes per block
|
|
608 |
// var ct; // ciphertext
|
|
609 |
|
|
610 |
if($mode == 'CBC')
|
|
611 |
{
|
|
612 |
if (!is_string($plaintext) || !is_array($key))
|
|
613 |
{
|
|
614 |
$this->trigger_error('In CBC mode the first and second parameters should be strings', E_USER_WARNING);
|
|
615 |
return false;
|
|
616 |
}
|
|
617 |
} else {
|
|
618 |
if (!is_array($plaintext) || !is_array($key))
|
|
619 |
{
|
|
620 |
$this->trigger_error('In ECB mode the first and second parameters should be byte arrays', E_USER_WARNING);
|
|
621 |
return false;
|
|
622 |
}
|
|
623 |
}
|
|
624 |
if (sizeof($key)*8 != $this->keySizeInBits)
|
|
625 |
{
|
|
626 |
$this->trigger_error('The key needs to be '. ( $this->keySizeInBits / 8 ) .' bytes in length', E_USER_WARNING);
|
|
627 |
return false;
|
|
628 |
}
|
|
629 |
if ($mode == "CBC")
|
|
630 |
$ct = $this->getRandomBytes($bpb); // get IV
|
|
631 |
else {
|
|
632 |
$mode = "ECB";
|
|
633 |
$ct = Array();
|
|
634 |
}
|
|
635 |
|
|
636 |
// convert plaintext to byte array and pad with zeros if necessary.
|
|
637 |
$plaintext = $this->formatPlaintext($plaintext);
|
|
638 |
|
|
639 |
$expandedKey = $this->keyExpansion($key);
|
|
640 |
|
|
641 |
for ($block=0; $block<sizeof($plaintext) / $bpb; $block++) {
|
|
642 |
$aBlock = $this->array_slice_js_compat($plaintext, $block*$bpb, ($block+1)*$bpb);
|
|
643 |
if ($mode == "CBC")
|
|
644 |
{
|
|
645 |
for ($i=0; $i<$bpb; $i++)
|
|
646 |
{
|
|
647 |
$aBlock[$i] ^= $ct[$block*$bpb + $i];
|
|
648 |
}
|
|
649 |
}
|
|
650 |
$cp = $this->cryptBlock($aBlock, $expandedKey);
|
|
651 |
$ct = $this->concat($ct, $cp);
|
|
652 |
}
|
|
653 |
|
|
654 |
return $ct;
|
|
655 |
}
|
|
656 |
|
|
657 |
// rijndaelDecrypt(ciphertext, key, mode)
|
|
658 |
// Decrypts the using the given key and mode. The parameter "ciphertext"
|
|
659 |
// must be an array of bytes. The parameter "key" must be an array of key
|
|
660 |
// bytes. If you have a hex string representing the ciphertext or key,
|
|
661 |
// invoke hexToByteArray() on it to convert it to an array of bytes. The
|
|
662 |
// parameter "mode" is a string, either "CBC" or "ECB".
|
|
663 |
//
|
|
664 |
// An array of bytes representing the plaintext is returned. To convert
|
|
665 |
// this array to a hex string, invoke byteArrayToHex() on it. To convert it
|
|
666 |
// to a string of characters, you can use byteArrayToString().
|
|
667 |
|
|
668 |
function rijndaelDecrypt($ciphertext, $key, $mode = 'ECB') {
|
|
669 |
//global $this->blockSizeInBits, $this->keySizeInBits;
|
|
670 |
$bpb = $this->blockSizeInBits / 8; // bytes per block
|
|
671 |
$pt = Array(); // plaintext array
|
|
672 |
// $aBlock; // a decrypted block
|
|
673 |
// $block; // current block number
|
|
674 |
|
|
675 |
if (!$ciphertext)
|
|
676 |
{
|
|
677 |
$this->trigger_error('$ciphertext should be a byte array', E_USER_WARNING);
|
|
678 |
return false;
|
|
679 |
}
|
|
680 |
if( !is_array($key) )
|
|
681 |
{
|
|
682 |
$this->trigger_error('$key should be a byte array', E_USER_WARNING);
|
|
683 |
return false;
|
|
684 |
}
|
|
685 |
if( is_string($ciphertext) )
|
|
686 |
{
|
|
687 |
$this->trigger_error('$ciphertext should be a byte array', E_USER_WARNING);
|
|
688 |
return false;
|
|
689 |
}
|
|
690 |
if (sizeof($key)*8 != $this->keySizeInBits)
|
|
691 |
{
|
|
692 |
$this->trigger_error('Encryption key is the wrong length', E_USER_WARNING);
|
|
693 |
return false;
|
|
694 |
}
|
|
695 |
if (!$mode)
|
|
696 |
$mode = "ECB"; // assume ECB if mode omitted
|
|
697 |
|
|
698 |
$expandedKey = $this->keyExpansion($key);
|
|
699 |
|
|
700 |
// work backwards to accomodate CBC mode
|
|
701 |
for ($block=(sizeof($ciphertext) / $bpb)-1; $block>0; $block--)
|
|
702 |
{
|
|
703 |
if( ( $block*$bpb ) + ( ($block+1)*$bpb ) > count($ciphertext) )
|
|
704 |
{
|
|
705 |
//$this->trigger_error('$ciphertext index out of bounds', E_USER_ERROR);
|
|
706 |
}
|
|
707 |
$current_block = $this->array_slice_js_compat($ciphertext, $block*$bpb, ($block+1)*$bpb);
|
|
708 |
if(count($current_block) * 8 != $this->blockSizeInBits)
|
|
709 |
{
|
|
710 |
// $c=count($current_block)*8;
|
|
711 |
// $this->trigger_error('We got a '.$c.'-bit block, instead of '.$this->blockSizeInBits.'', E_USER_ERROR);
|
|
712 |
}
|
|
713 |
$aBlock = $this->uncryptBlock($current_block, $expandedKey);
|
|
714 |
if(!$aBlock)
|
|
715 |
{
|
|
716 |
$this->trigger_error('Shared block decryption routine returned false', E_USER_WARNING);
|
|
717 |
return false;
|
|
718 |
}
|
|
719 |
if ($mode == "CBC")
|
|
720 |
for ($i=0; $i<$bpb; $i++)
|
|
721 |
$pt[($block-1)*$bpb + $i] = $aBlock[$i] ^ $ciphertext[($block-1)*$bpb + $i];
|
|
722 |
else
|
|
723 |
$pt = $this->concat($aBlock, $pt);
|
|
724 |
}
|
|
725 |
|
|
726 |
// do last block if ECB (skips the IV in CBC)
|
|
727 |
if ($mode == "ECB")
|
|
728 |
{
|
|
729 |
$x = $this->uncryptBlock($this->array_slice_js_compat($ciphertext, 0, $bpb), $expandedKey);
|
|
730 |
if(!$x)
|
|
731 |
{
|
|
732 |
$this->trigger_error('ECB block decryption routine returned false', E_USER_WARNING);
|
|
733 |
return false;
|
|
734 |
}
|
|
735 |
$pt = $this->concat($x, $pt);
|
|
736 |
if(!$pt)
|
|
737 |
{
|
|
738 |
$this->trigger_error('ECB concatenation routine returned false', E_USER_WARNING);
|
|
739 |
return false;
|
|
740 |
}
|
|
741 |
}
|
|
742 |
|
|
743 |
return $pt;
|
|
744 |
}
|
|
745 |
|
|
746 |
/**
|
|
747 |
* Wrapper for encryption.
|
|
748 |
* @param string $text the text to encrypt
|
|
749 |
* @param string $key the raw binary key to encrypt with
|
|
750 |
* @param int $return_encoding optional - can be ENC_BINARY, ENC_HEX or ENC_BASE64
|
|
751 |
*/
|
|
752 |
|
|
753 |
function encrypt($text, $key, $return_encoding = ENC_HEX)
|
|
754 |
{
|
|
755 |
if ( $text == '' )
|
|
756 |
return '';
|
|
757 |
if ( $this->mcrypt && $this->blockSizeInBits == mcrypt_module_get_algo_block_size(eval('return MCRYPT_RIJNDAEL_'.$this->keySizeInBits.';')) )
|
|
758 |
{
|
|
759 |
$iv_size = mcrypt_get_iv_size($this->mcrypt, MCRYPT_MODE_ECB);
|
|
760 |
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
|
|
761 |
$cryptext = mcrypt_encrypt($this->mcrypt, $key, $text, MCRYPT_MODE_ECB, $iv);
|
|
762 |
switch($return_encoding)
|
|
763 |
{
|
|
764 |
case ENC_HEX:
|
|
765 |
default:
|
|
766 |
$cryptext = $this->strtohex($cryptext);
|
|
767 |
break;
|
|
768 |
case ENC_BINARY:
|
|
769 |
$cryptext = $cryptext;
|
|
770 |
break;
|
|
771 |
case ENC_BASE64:
|
|
772 |
$cryptext = base64_encode($cryptext);
|
|
773 |
break;
|
|
774 |
}
|
|
775 |
}
|
|
776 |
else
|
|
777 |
{
|
|
778 |
$key = $this->prepare_string($key);
|
|
779 |
$text = $this->prepare_string($text);
|
|
780 |
profiler_log('AES: Started encryption of a string');
|
|
781 |
$cryptext = $this->rijndaelEncrypt($text, $key, 'ECB');
|
|
782 |
profiler_log('AES: Finished encryption of a string');
|
|
783 |
if(!is_array($cryptext))
|
|
784 |
{
|
|
785 |
echo 'Warning: encryption failed for string: '.print_r($text,true).'<br />';
|
|
786 |
return false;
|
|
787 |
}
|
|
788 |
switch($return_encoding)
|
|
789 |
{
|
|
790 |
case ENC_HEX:
|
|
791 |
default:
|
|
792 |
$cryptext = $this->byteArrayToHex($cryptext);
|
|
793 |
break;
|
|
794 |
case ENC_BINARY:
|
|
795 |
$cryptext = $this->byteArrayToString($cryptext);
|
|
796 |
break;
|
|
797 |
case ENC_BASE64:
|
|
798 |
$cryptext = base64_encode($this->byteArrayToString($cryptext));
|
|
799 |
break;
|
|
800 |
}
|
|
801 |
}
|
|
802 |
return $cryptext;
|
|
803 |
}
|
|
804 |
|
|
805 |
/**
|
|
806 |
* Wrapper for decryption.
|
|
807 |
* @param string $text the encrypted text
|
|
808 |
* @param string $key the raw binary key used to encrypt the text
|
|
809 |
* @param int $input_encoding the encoding used for the encrypted string. Can be ENC_BINARY, ENC_HEX, or ENC_BASE64.
|
|
810 |
* @return string
|
|
811 |
*/
|
|
812 |
|
|
813 |
function decrypt($text, $key, $input_encoding = ENC_HEX)
|
|
814 |
{
|
|
815 |
if ( $text == '' )
|
|
816 |
return '';
|
|
817 |
|
|
818 |
switch($input_encoding)
|
|
819 |
{
|
|
820 |
case ENC_BINARY:
|
|
821 |
default:
|
|
822 |
break;
|
|
823 |
case ENC_HEX:
|
|
824 |
$text = $this->hextostring($text);
|
|
825 |
break;
|
|
826 |
case ENC_BASE64:
|
|
827 |
$text = base64_decode($text);
|
|
828 |
break;
|
|
829 |
}
|
|
830 |
|
|
831 |
// Run memory-cache check
|
|
832 |
if ( isset($this->decrypt_cache[$key]) && is_array($this->decrypt_cache[$key]) )
|
|
833 |
{
|
|
834 |
if ( isset($this->decrypt_cache[$key][$text]) )
|
|
835 |
{
|
|
836 |
return $this->decrypt_cache[$key][$text];
|
|
837 |
}
|
|
838 |
}
|
|
839 |
|
|
840 |
// Run disk-cache check
|
|
841 |
$hash = sha1($text . '::' . $key);
|
|
842 |
if ( $dypt = aes_decrypt_cache_fetch($hash) )
|
|
843 |
return $dypt;
|
|
844 |
|
|
845 |
$text_bin = $text;
|
|
846 |
$key_bin = $key;
|
|
847 |
|
|
848 |
if ( $this->mcrypt )
|
|
849 |
{
|
|
850 |
$iv_size = mcrypt_get_iv_size($this->mcrypt, MCRYPT_MODE_ECB);
|
|
851 |
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
|
|
852 |
$dypt = mcrypt_decrypt($this->mcrypt, $key, $text, MCRYPT_MODE_ECB, $iv);
|
|
853 |
}
|
|
854 |
else
|
|
855 |
{
|
|
856 |
$etext = $this->prepare_string($text);
|
|
857 |
$ekey = $this->prepare_string($key);
|
|
858 |
$mod = count($etext) % $this->blockSizeInBits;
|
|
859 |
profiler_log('AES: Started decryption of a string');
|
|
860 |
$dypt = $this->rijndaelDecrypt($etext, $ekey, 'ECB');
|
|
861 |
profiler_log('AES: Finished decryption of a string');
|
|
862 |
if(!$dypt)
|
|
863 |
{
|
|
864 |
echo '<pre>'.print_r($dypt, true).'</pre>';
|
|
865 |
$this->trigger_error('Rijndael main decryption routine failed', E_USER_ERROR);
|
|
866 |
}
|
|
867 |
$dypt = $this->byteArrayToString($dypt);
|
|
868 |
}
|
|
869 |
if ( !isset($this->decrypt_cache[$key_bin]) )
|
|
870 |
$this->decrypt_cache[$key_bin] = array();
|
|
871 |
|
|
872 |
$this->decrypt_cache[$key_bin][$text_bin] = $dypt;
|
|
873 |
|
|
874 |
aes_decrypt_cache_store($text_bin, $dypt, $key_bin);
|
|
875 |
|
|
876 |
return $dypt;
|
|
877 |
}
|
|
878 |
|
|
879 |
/**
|
|
880 |
* Enano-ese equivalent of str_split() which is only found in PHP5
|
|
881 |
* @param $text string the text to split
|
|
882 |
* @param $inc int size of each block
|
|
883 |
* @return array
|
|
884 |
*/
|
|
885 |
|
|
886 |
function enano_str_split($text, $inc = 1)
|
|
887 |
{
|
|
888 |
if($inc < 1) return false;
|
|
889 |
if($inc >= strlen($text)) return Array($text);
|
|
890 |
$len = ceil(strlen($text) / $inc);
|
|
891 |
$ret = Array();
|
|
892 |
for($i=0;$i<strlen($text);$i=$i+$inc)
|
|
893 |
{
|
|
894 |
$ret[] = substr($text, $i, $inc);
|
|
895 |
}
|
|
896 |
return $ret;
|
|
897 |
}
|
|
898 |
|
|
899 |
/**
|
|
900 |
* Generates a random key suitable for encryption
|
|
901 |
* @param int $len the length of the key, in bytes
|
|
902 |
* @return string a BINARY key
|
|
903 |
*/
|
|
904 |
|
|
905 |
function randkey($len = 32)
|
|
906 |
{
|
|
907 |
$key = '';
|
|
908 |
for($i=0;$i<$len;$i++)
|
|
909 |
{
|
|
910 |
$key .= chr(mt_rand(0, 255));
|
|
911 |
}
|
|
912 |
if ( file_exists('/dev/urandom') && is_readable('/dev/urandom') )
|
|
913 |
{
|
|
914 |
// Let's use something a little more secure
|
|
915 |
$ur = @fopen('/dev/urandom', 'r');
|
|
916 |
if ( !$ur )
|
|
917 |
return $key;
|
|
918 |
$ukey = @fread($ur, $len);
|
|
919 |
fclose($ur);
|
|
920 |
if ( strlen($ukey) != $len )
|
|
921 |
return $key;
|
|
922 |
return $ukey;
|
|
923 |
}
|
|
924 |
return $key;
|
|
925 |
}
|
|
926 |
|
|
927 |
/*
|
|
928 |
function byteArrayToString($arr)
|
|
929 |
{
|
|
930 |
if(!is_array($arr))
|
|
931 |
{
|
|
932 |
$this->trigger_error('First parameter should be an array', E_USER_WARNING);
|
|
933 |
return false;
|
|
934 |
}
|
|
935 |
$ret = '';
|
|
936 |
foreach($arr as $a)
|
|
937 |
{
|
|
938 |
if($a != 0) $ret .= chr($a);
|
|
939 |
}
|
|
940 |
return $ret;
|
|
941 |
}
|
|
942 |
*/
|
|
943 |
|
|
944 |
function strtohex($str)
|
|
945 |
{
|
|
946 |
$str = $this->enano_str_split($str);
|
|
947 |
$ret = '';
|
|
948 |
foreach($str as $s)
|
|
949 |
{
|
|
950 |
$chr = dechex(ord($s));
|
|
951 |
if(strlen($chr) < 2) $chr = '0' . $chr;
|
|
952 |
$ret .= $chr;
|
|
953 |
}
|
|
954 |
return $ret;
|
|
955 |
}
|
|
956 |
|
|
957 |
function gen_readymade_key()
|
|
958 |
{
|
|
959 |
$key = $this->strtohex($this->randkey($this->keySizeInBits / 8));
|
|
960 |
return $key;
|
|
961 |
}
|
|
962 |
|
|
963 |
function prepare_string($text)
|
|
964 |
{
|
|
965 |
$ret = $this->hexToByteArray($this->strtohex($text));
|
|
966 |
if(count($ret) != strlen($text))
|
|
967 |
{
|
|
968 |
die('Could not convert string "' . $text . '" to hex byte array for encryption');
|
|
969 |
}
|
|
970 |
return $ret;
|
|
971 |
}
|
|
972 |
|
|
973 |
/**
|
|
974 |
* Decodes a hex string.
|
|
975 |
* @param string $hex The hex code to decode
|
|
976 |
* @return string
|
|
977 |
*/
|
|
978 |
|
|
979 |
function hextostring($hex)
|
|
980 |
{
|
|
981 |
$hex = $this->enano_str_split($hex, 2);
|
|
982 |
$bin_key = '';
|
|
983 |
foreach($hex as $nibble)
|
|
984 |
{
|
|
985 |
$byte = chr(hexdec($nibble));
|
|
986 |
$bin_key .= $byte;
|
|
987 |
}
|
|
988 |
return $bin_key;
|
|
989 |
}
|
|
990 |
}
|
|
991 |
|
|
992 |
function aes_decrypt_cache_store($encrypted, $decrypted, $key)
|
|
993 |
{
|
|
994 |
$cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
|
|
995 |
// only cache if $decrypted is long enough to actually warrant caching
|
|
996 |
if ( strlen($decrypted) < 32 )
|
|
997 |
{
|
|
998 |
profiler_log("AES: Skipped caching a string (probably a password, we dunno) because it's too short");
|
|
999 |
return false;
|
|
1000 |
}
|
|
1001 |
if ( file_exists($cache_file) )
|
|
1002 |
{
|
|
1003 |
require_once($cache_file);
|
|
1004 |
global $aes_decrypt_cache;
|
|
1005 |
$cachekey = sha1($encrypted . '::' . $key);
|
|
1006 |
$aes_decrypt_cache[$cachekey] = $decrypted;
|
|
1007 |
|
|
1008 |
if ( count($aes_decrypt_cache) > 5000 )
|
|
1009 |
{
|
|
1010 |
// we've got a lot of strings in the cache, clear out a few
|
|
1011 |
$keys = array_keys($aes_decrypt_cache);
|
|
1012 |
for ( $i = 0; $i < 2500; $i++ )
|
|
1013 |
{
|
|
1014 |
unset($aes_decrypt_cache[$keys[$i]]);
|
|
1015 |
unset($aes_decrypt_cache[$keys[$i]]);
|
|
1016 |
}
|
|
1017 |
}
|
|
1018 |
}
|
|
1019 |
else
|
|
1020 |
{
|
|
1021 |
$aes_decrypt_cache = array(
|
|
1022 |
sha1($encrypted . '::' . $key) => $decrypted
|
|
1023 |
);
|
|
1024 |
}
|
|
1025 |
// call var_export and collect contents
|
|
1026 |
ob_start();
|
|
1027 |
var_export($aes_decrypt_cache);
|
|
1028 |
$dec_cache_string = ob_get_contents();
|
|
1029 |
ob_end_clean();
|
|
1030 |
$f = @fopen($cache_file, 'w');
|
|
1031 |
if ( !$f )
|
|
1032 |
return false;
|
|
1033 |
fwrite($f, "<?php
|
|
1034 |
\$GLOBALS['aes_decrypt_cache'] = $dec_cache_string;
|
|
1035 |
");
|
|
1036 |
fclose($f);
|
|
1037 |
return true;
|
|
1038 |
}
|
|
1039 |
|
|
1040 |
function aes_decrypt_cache_fetch($hash)
|
|
1041 |
{
|
|
1042 |
$cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
|
|
1043 |
if ( !file_exists($cache_file) )
|
|
1044 |
return false;
|
|
1045 |
|
|
1046 |
require_once($cache_file);
|
|
1047 |
global $aes_decrypt_cache;
|
|
1048 |
if ( isset($aes_decrypt_cache[$hash]) )
|
|
1049 |
{
|
|
1050 |
profiler_log("AES: Loaded cached decrypted string, hash is $hash");
|
|
1051 |
return $aes_decrypt_cache[$hash];
|
|
1052 |
}
|
|
1053 |
|
|
1054 |
return false;
|
|
1055 |
}
|
|
1056 |
|
|
1057 |
function aes_decrypt_cache_destroy($hash)
|
|
1058 |
{
|
|
1059 |
$cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
|
|
1060 |
if ( !file_exists($cache_file) )
|
|
1061 |
return false;
|
|
1062 |
|
|
1063 |
require_once($cache_file);
|
|
1064 |
global $aes_decrypt_cache;
|
|
1065 |
|
|
1066 |
if ( isset($aes_decrypt_cache[$hash]) )
|
|
1067 |
unset($aes_decrypt_cache[$hash]);
|
|
1068 |
|
|
1069 |
// call var_export and collect contents
|
|
1070 |
ob_start();
|
|
1071 |
var_export($aes_decrypt_cache);
|
|
1072 |
$dec_cache_string = ob_get_contents();
|
|
1073 |
ob_end_clean();
|
|
1074 |
$f = @fopen($cache_file, 'w');
|
|
1075 |
if ( !$f )
|
|
1076 |
return false;
|
|
1077 |
fwrite($f, "<?php
|
|
1078 |
\$GLOBALS['aes_decrypt_cache'] = $dec_cache_string;
|
|
1079 |
");
|
|
1080 |
fclose($f);
|
|
1081 |
return true;
|
|
1082 |
}
|
|
1083 |
|
|
1084 |
?>
|