@@ -57,8 +57,9 @@ discard block |
||
57 | 57 | function validate_password($password, $correct_hash) |
58 | 58 | { |
59 | 59 | $params = explode(":", $correct_hash); |
60 | - if (count($params) < HASH_SECTIONS) |
|
61 | - return false; |
|
60 | + if (count($params) < HASH_SECTIONS) { |
|
61 | + return false; |
|
62 | + } |
|
62 | 63 | $pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]); |
63 | 64 | return slow_equals( |
64 | 65 | $pbkdf2, |
@@ -108,10 +109,12 @@ discard block |
||
108 | 109 | function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) |
109 | 110 | { |
110 | 111 | $algorithm = strtolower($algorithm); |
111 | - if (!in_array($algorithm, hash_algos(), true)) |
|
112 | - trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); |
|
113 | - if ($count <= 0 || $key_length <= 0) |
|
114 | - trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); |
|
112 | + if (!in_array($algorithm, hash_algos(), true)) { |
|
113 | + trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); |
|
114 | + } |
|
115 | + if ($count <= 0 || $key_length <= 0) { |
|
116 | + trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); |
|
117 | + } |
|
115 | 118 | |
116 | 119 | if (function_exists("hash_pbkdf2")) { |
117 | 120 | // The output length is in NIBBLES (4-bits) if $raw_output is false! |
@@ -137,9 +140,10 @@ discard block |
||
137 | 140 | $output .= $xorsum; |
138 | 141 | } |
139 | 142 | |
140 | - if ($raw_output) |
|
141 | - return substr($output, 0, $key_length); |
|
142 | - else |
|
143 | - return bin2hex(substr($output, 0, $key_length)); |
|
144 | -} |
|
143 | + if ($raw_output) { |
|
144 | + return substr($output, 0, $key_length); |
|
145 | + } else { |
|
146 | + return bin2hex(substr($output, 0, $key_length)); |
|
147 | + } |
|
148 | + } |
|
145 | 149 | ?> |
146 | 150 | \ No newline at end of file |
@@ -39,294 +39,294 @@ |
||
39 | 39 | */ |
40 | 40 | class Padding |
41 | 41 | { |
42 | - /** |
|
43 | - * Constructor |
|
44 | - * |
|
45 | - * @return void |
|
46 | - */ |
|
47 | - public function __construct() { } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Destructor |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function __destruct() { } |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * Returns a string padded using the specified padding scheme |
|
60 | - * |
|
61 | - * @param string $text The string to pad |
|
62 | - * @param integer $bytes The number of bytes to pad |
|
63 | - * @param integer $type One of the predefined padding types |
|
64 | - * @return boolean True on success, false on error |
|
65 | - */ |
|
66 | - public static function pad(&$text, $bytes, $type = PHP_Crypt::ZERO) |
|
67 | - { |
|
68 | - // if the size of padding is not greater than 1 |
|
69 | - // just return true, no padding will be done |
|
70 | - if (!($bytes > 0)) |
|
71 | - return true; |
|
72 | - |
|
73 | - switch ($type) |
|
74 | - { |
|
75 | - case PHP_Crypt::PAD_ZERO: |
|
76 | - return self::zeroPad($text, $bytes); |
|
77 | - break; |
|
78 | - case PHP_Crypt::PAD_ANSI_X923: |
|
79 | - return self::ansiX923Pad($text, $bytes); |
|
80 | - break; |
|
81 | - case PHP_Crypt::PAD_ISO_10126: |
|
82 | - return self::iso10126Pad($text, $bytes); |
|
83 | - break; |
|
84 | - case PHP_Crypt::PAD_PKCS7: |
|
85 | - return self::pkcs7Pad($text, $bytes); |
|
86 | - break; |
|
87 | - case PHP_Crypt::PAD_ISO_7816_4: |
|
88 | - return self::iso7816Pad($text, $bytes); |
|
89 | - break; |
|
90 | - default: |
|
91 | - trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
92 | - return self::zeroPad($text, $bytes); |
|
93 | - } |
|
94 | - |
|
95 | - return true; |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * Strips padding from a string |
|
101 | - * |
|
102 | - * @param string $text The string strip padding from |
|
103 | - * @param integer $type One of the predefined padding types |
|
104 | - * @return boolean True on success, false on error |
|
105 | - */ |
|
106 | - public static function strip(&$text, $type = PHP_Crypt::ZERO) |
|
107 | - { |
|
108 | - switch ($type) |
|
109 | - { |
|
110 | - case PHP_Crypt::PAD_ZERO: |
|
111 | - return self::zeroStrip($text); |
|
112 | - break; |
|
113 | - case PHP_Crypt::PAD_ANSI_X923: |
|
114 | - return self::ansiX923Strip($text); |
|
115 | - break; |
|
116 | - case PHP_Crypt::PAD_ISO_10126: |
|
117 | - return self::iso10126Strip($text); |
|
118 | - break; |
|
119 | - case PHP_Crypt::PAD_PKCS7: |
|
120 | - return self::pkcs7Strip($text); |
|
121 | - break; |
|
122 | - case PHP_Crypt::PAD_ISO_7816_4: |
|
123 | - return self::iso7816Strip($text); |
|
124 | - break; |
|
125 | - default: |
|
126 | - trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
127 | - return self::zeroStrip($text); |
|
128 | - } |
|
129 | - |
|
130 | - return true; |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * Pads a string with null bytes |
|
136 | - * |
|
137 | - * @param string $text The string to be padded |
|
138 | - * @param integer $bytes The number of bytes to pad |
|
139 | - * @return boolean Returns true |
|
140 | - */ |
|
141 | - private static function zeroPad(&$text, $bytes) |
|
142 | - { |
|
143 | - $len = $bytes + strlen($text); |
|
144 | - $text = str_pad($text, $len, chr(0), STR_PAD_RIGHT); |
|
145 | - return true; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * Strips null padding off a string |
|
151 | - * NOTE: This is generally a bad idea as there is no way |
|
152 | - * to distinguish a null byte that is not padding from |
|
153 | - * a null byte that is padding. Stripping null padding should |
|
154 | - * be handled by the developer at the application level. |
|
155 | - * |
|
156 | - * @param string $text The string with null padding to strip |
|
157 | - * @return boolean Returns true |
|
158 | - */ |
|
159 | - private static function zeroStrip(&$text) |
|
160 | - { |
|
161 | - // with NULL byte padding, we should not strip off the |
|
162 | - // null bytes, instead leave this to the developer at the |
|
163 | - // application level |
|
164 | - //$text = preg_replace('/\0+$/', '', $text); |
|
165 | - return true; |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * Pads a string using ANSI X.923 |
|
171 | - * Adds null padding to the string, except for the last byte |
|
172 | - * which indicates the number of bytes padded |
|
173 | - * |
|
174 | - * @param string $text The string to pad |
|
175 | - * @param integer The number of bytes to pad |
|
176 | - * @param integer $bytes |
|
177 | - * @return boolean Returns true |
|
178 | - */ |
|
179 | - private static function ansiX923Pad(&$text, $bytes) |
|
180 | - { |
|
181 | - $len = $bytes + strlen($text); |
|
182 | - $text = str_pad($text, ($len - 1), "\0", STR_PAD_RIGHT); |
|
183 | - $text .= chr($bytes); |
|
184 | - return true; |
|
185 | - } |
|
186 | - |
|
187 | - |
|
188 | - /** |
|
189 | - * Strips ANSI X.923 padding from a string |
|
190 | - * |
|
191 | - * @param string $text The string to strip padding from |
|
192 | - * @return boolean Returns true |
|
193 | - */ |
|
194 | - private static function ansiX923Strip(&$text) |
|
195 | - { |
|
196 | - $pos = strlen($text) - 1; |
|
197 | - $c = ord($text[$pos]); |
|
198 | - |
|
199 | - if ($c == 0) |
|
200 | - return true; |
|
201 | - else if ($c == 1) |
|
202 | - $text = substr($text, 0, -1); |
|
203 | - else |
|
204 | - { |
|
205 | - // the total null bytes are 1 less than the value of the final byte |
|
206 | - $nc = $c - 1; |
|
207 | - $text = preg_replace('/\0{'.$nc.'}'.preg_quote(chr($c)).'$/', "", $text); |
|
208 | - } |
|
209 | - |
|
210 | - return true; |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * Pads a string using ISO 10126 |
|
216 | - * Adds random bytes to the end of the string, except for the last |
|
217 | - * byte which indicates the number of padded bytes |
|
218 | - * |
|
219 | - * @param string $text The string to pad |
|
220 | - * @param integer The number of bytes to pad |
|
221 | - * @param integer $bytes |
|
222 | - * @return boolean Returns true |
|
223 | - */ |
|
224 | - private static function iso10126Pad(&$text, $bytes) |
|
225 | - { |
|
226 | - // create the random pad bytes, we do one less than |
|
227 | - // needed because the last byte is reserved for the |
|
228 | - // number of padded bytes |
|
229 | - for ($i = 0; $i < ($bytes - 1); ++$i) |
|
230 | - $text .= chr(mt_rand(0, 255)); |
|
231 | - |
|
232 | - // add the byte to indicate the padding length |
|
233 | - $text .= chr($bytes); |
|
234 | - return true; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * Strips ISO 10126 padding from a string |
|
240 | - * |
|
241 | - * @param string $text The string to strip padding from |
|
242 | - * @return boolean Returns true |
|
243 | - */ |
|
244 | - private static function iso10126Strip(&$text) |
|
245 | - { |
|
246 | - $pos = strlen($text) - 1; |
|
247 | - $c = ord($text[$pos]) * -1; |
|
248 | - |
|
249 | - // if we got a null byte at the end of the string, |
|
250 | - // just return |
|
251 | - if ($c == 0) |
|
252 | - return true; |
|
253 | - |
|
254 | - $text = substr($text, 0, $c); |
|
255 | - return true; |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Pads a string using PKCS7 |
|
261 | - * Adds padding using the bytes with the value of the |
|
262 | - * number of bytes need for padding |
|
263 | - * |
|
264 | - * @param string $text The string to pad |
|
265 | - * @param integer The number of bytes to pad |
|
266 | - * @param integer $bytes |
|
267 | - * @return boolean Returns true |
|
268 | - */ |
|
269 | - private static function pkcs7Pad(&$text, $bytes) |
|
270 | - { |
|
271 | - $len = $bytes + strlen($text); |
|
272 | - $text = str_pad($text, $len, chr($bytes), STR_PAD_RIGHT); |
|
273 | - return true; |
|
274 | - } |
|
275 | - |
|
276 | - |
|
277 | - /** |
|
278 | - * Strips PKCS7 padding from a string |
|
279 | - * |
|
280 | - * @param string $text The string to strip padding from |
|
281 | - * @return boolean Returns true |
|
282 | - */ |
|
283 | - private static function pkcs7Strip(&$text) |
|
284 | - { |
|
285 | - $pos = strlen($text) - 1; |
|
286 | - $c = ord($text[$pos]); |
|
287 | - |
|
288 | - if ($c == 0) |
|
289 | - return true; |
|
290 | - |
|
291 | - $text = preg_replace('/'.preg_quote(chr($c)).'{'.$c.'}$/', "", $text); |
|
292 | - return true; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Pads a string using ISO/IEC 7816-4 |
|
298 | - * Adds byte 0x80 followed by null bytes to pad a string |
|
299 | - * |
|
300 | - * @param string $text The string to pad |
|
301 | - * @param integer The number of bytes to pad |
|
302 | - * @param integer $bytes |
|
303 | - * @return boolean Returns true |
|
304 | - */ |
|
305 | - private static function iso7816Pad(&$text, $bytes) |
|
306 | - { |
|
307 | - $text .= chr(0x80); |
|
308 | - $len = $bytes + strlen($text); |
|
309 | - |
|
310 | - // if we are only padding one byte, then 0x80 is all we need |
|
311 | - // else we follow up with null bytes |
|
312 | - if ($bytes > 1) |
|
313 | - $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
314 | - |
|
315 | - return true; |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Strips ISO/IEC 7816-4 padding from a string |
|
321 | - * |
|
322 | - * @param string $text The string to strip padding from |
|
323 | - * @return boolean Returns true |
|
324 | - */ |
|
325 | - private static function iso7816Strip(&$text) |
|
326 | - { |
|
327 | - $c = chr(0x80); |
|
328 | - $text = preg_replace('/'.preg_quote($c).'\0*$/', '', $text); |
|
329 | - return true; |
|
330 | - } |
|
42 | + /** |
|
43 | + * Constructor |
|
44 | + * |
|
45 | + * @return void |
|
46 | + */ |
|
47 | + public function __construct() { } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Destructor |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function __destruct() { } |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * Returns a string padded using the specified padding scheme |
|
60 | + * |
|
61 | + * @param string $text The string to pad |
|
62 | + * @param integer $bytes The number of bytes to pad |
|
63 | + * @param integer $type One of the predefined padding types |
|
64 | + * @return boolean True on success, false on error |
|
65 | + */ |
|
66 | + public static function pad(&$text, $bytes, $type = PHP_Crypt::ZERO) |
|
67 | + { |
|
68 | + // if the size of padding is not greater than 1 |
|
69 | + // just return true, no padding will be done |
|
70 | + if (!($bytes > 0)) |
|
71 | + return true; |
|
72 | + |
|
73 | + switch ($type) |
|
74 | + { |
|
75 | + case PHP_Crypt::PAD_ZERO: |
|
76 | + return self::zeroPad($text, $bytes); |
|
77 | + break; |
|
78 | + case PHP_Crypt::PAD_ANSI_X923: |
|
79 | + return self::ansiX923Pad($text, $bytes); |
|
80 | + break; |
|
81 | + case PHP_Crypt::PAD_ISO_10126: |
|
82 | + return self::iso10126Pad($text, $bytes); |
|
83 | + break; |
|
84 | + case PHP_Crypt::PAD_PKCS7: |
|
85 | + return self::pkcs7Pad($text, $bytes); |
|
86 | + break; |
|
87 | + case PHP_Crypt::PAD_ISO_7816_4: |
|
88 | + return self::iso7816Pad($text, $bytes); |
|
89 | + break; |
|
90 | + default: |
|
91 | + trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
92 | + return self::zeroPad($text, $bytes); |
|
93 | + } |
|
94 | + |
|
95 | + return true; |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * Strips padding from a string |
|
101 | + * |
|
102 | + * @param string $text The string strip padding from |
|
103 | + * @param integer $type One of the predefined padding types |
|
104 | + * @return boolean True on success, false on error |
|
105 | + */ |
|
106 | + public static function strip(&$text, $type = PHP_Crypt::ZERO) |
|
107 | + { |
|
108 | + switch ($type) |
|
109 | + { |
|
110 | + case PHP_Crypt::PAD_ZERO: |
|
111 | + return self::zeroStrip($text); |
|
112 | + break; |
|
113 | + case PHP_Crypt::PAD_ANSI_X923: |
|
114 | + return self::ansiX923Strip($text); |
|
115 | + break; |
|
116 | + case PHP_Crypt::PAD_ISO_10126: |
|
117 | + return self::iso10126Strip($text); |
|
118 | + break; |
|
119 | + case PHP_Crypt::PAD_PKCS7: |
|
120 | + return self::pkcs7Strip($text); |
|
121 | + break; |
|
122 | + case PHP_Crypt::PAD_ISO_7816_4: |
|
123 | + return self::iso7816Strip($text); |
|
124 | + break; |
|
125 | + default: |
|
126 | + trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
127 | + return self::zeroStrip($text); |
|
128 | + } |
|
129 | + |
|
130 | + return true; |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * Pads a string with null bytes |
|
136 | + * |
|
137 | + * @param string $text The string to be padded |
|
138 | + * @param integer $bytes The number of bytes to pad |
|
139 | + * @return boolean Returns true |
|
140 | + */ |
|
141 | + private static function zeroPad(&$text, $bytes) |
|
142 | + { |
|
143 | + $len = $bytes + strlen($text); |
|
144 | + $text = str_pad($text, $len, chr(0), STR_PAD_RIGHT); |
|
145 | + return true; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * Strips null padding off a string |
|
151 | + * NOTE: This is generally a bad idea as there is no way |
|
152 | + * to distinguish a null byte that is not padding from |
|
153 | + * a null byte that is padding. Stripping null padding should |
|
154 | + * be handled by the developer at the application level. |
|
155 | + * |
|
156 | + * @param string $text The string with null padding to strip |
|
157 | + * @return boolean Returns true |
|
158 | + */ |
|
159 | + private static function zeroStrip(&$text) |
|
160 | + { |
|
161 | + // with NULL byte padding, we should not strip off the |
|
162 | + // null bytes, instead leave this to the developer at the |
|
163 | + // application level |
|
164 | + //$text = preg_replace('/\0+$/', '', $text); |
|
165 | + return true; |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * Pads a string using ANSI X.923 |
|
171 | + * Adds null padding to the string, except for the last byte |
|
172 | + * which indicates the number of bytes padded |
|
173 | + * |
|
174 | + * @param string $text The string to pad |
|
175 | + * @param integer The number of bytes to pad |
|
176 | + * @param integer $bytes |
|
177 | + * @return boolean Returns true |
|
178 | + */ |
|
179 | + private static function ansiX923Pad(&$text, $bytes) |
|
180 | + { |
|
181 | + $len = $bytes + strlen($text); |
|
182 | + $text = str_pad($text, ($len - 1), "\0", STR_PAD_RIGHT); |
|
183 | + $text .= chr($bytes); |
|
184 | + return true; |
|
185 | + } |
|
186 | + |
|
187 | + |
|
188 | + /** |
|
189 | + * Strips ANSI X.923 padding from a string |
|
190 | + * |
|
191 | + * @param string $text The string to strip padding from |
|
192 | + * @return boolean Returns true |
|
193 | + */ |
|
194 | + private static function ansiX923Strip(&$text) |
|
195 | + { |
|
196 | + $pos = strlen($text) - 1; |
|
197 | + $c = ord($text[$pos]); |
|
198 | + |
|
199 | + if ($c == 0) |
|
200 | + return true; |
|
201 | + else if ($c == 1) |
|
202 | + $text = substr($text, 0, -1); |
|
203 | + else |
|
204 | + { |
|
205 | + // the total null bytes are 1 less than the value of the final byte |
|
206 | + $nc = $c - 1; |
|
207 | + $text = preg_replace('/\0{'.$nc.'}'.preg_quote(chr($c)).'$/', "", $text); |
|
208 | + } |
|
209 | + |
|
210 | + return true; |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * Pads a string using ISO 10126 |
|
216 | + * Adds random bytes to the end of the string, except for the last |
|
217 | + * byte which indicates the number of padded bytes |
|
218 | + * |
|
219 | + * @param string $text The string to pad |
|
220 | + * @param integer The number of bytes to pad |
|
221 | + * @param integer $bytes |
|
222 | + * @return boolean Returns true |
|
223 | + */ |
|
224 | + private static function iso10126Pad(&$text, $bytes) |
|
225 | + { |
|
226 | + // create the random pad bytes, we do one less than |
|
227 | + // needed because the last byte is reserved for the |
|
228 | + // number of padded bytes |
|
229 | + for ($i = 0; $i < ($bytes - 1); ++$i) |
|
230 | + $text .= chr(mt_rand(0, 255)); |
|
231 | + |
|
232 | + // add the byte to indicate the padding length |
|
233 | + $text .= chr($bytes); |
|
234 | + return true; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * Strips ISO 10126 padding from a string |
|
240 | + * |
|
241 | + * @param string $text The string to strip padding from |
|
242 | + * @return boolean Returns true |
|
243 | + */ |
|
244 | + private static function iso10126Strip(&$text) |
|
245 | + { |
|
246 | + $pos = strlen($text) - 1; |
|
247 | + $c = ord($text[$pos]) * -1; |
|
248 | + |
|
249 | + // if we got a null byte at the end of the string, |
|
250 | + // just return |
|
251 | + if ($c == 0) |
|
252 | + return true; |
|
253 | + |
|
254 | + $text = substr($text, 0, $c); |
|
255 | + return true; |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Pads a string using PKCS7 |
|
261 | + * Adds padding using the bytes with the value of the |
|
262 | + * number of bytes need for padding |
|
263 | + * |
|
264 | + * @param string $text The string to pad |
|
265 | + * @param integer The number of bytes to pad |
|
266 | + * @param integer $bytes |
|
267 | + * @return boolean Returns true |
|
268 | + */ |
|
269 | + private static function pkcs7Pad(&$text, $bytes) |
|
270 | + { |
|
271 | + $len = $bytes + strlen($text); |
|
272 | + $text = str_pad($text, $len, chr($bytes), STR_PAD_RIGHT); |
|
273 | + return true; |
|
274 | + } |
|
275 | + |
|
276 | + |
|
277 | + /** |
|
278 | + * Strips PKCS7 padding from a string |
|
279 | + * |
|
280 | + * @param string $text The string to strip padding from |
|
281 | + * @return boolean Returns true |
|
282 | + */ |
|
283 | + private static function pkcs7Strip(&$text) |
|
284 | + { |
|
285 | + $pos = strlen($text) - 1; |
|
286 | + $c = ord($text[$pos]); |
|
287 | + |
|
288 | + if ($c == 0) |
|
289 | + return true; |
|
290 | + |
|
291 | + $text = preg_replace('/'.preg_quote(chr($c)).'{'.$c.'}$/', "", $text); |
|
292 | + return true; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Pads a string using ISO/IEC 7816-4 |
|
298 | + * Adds byte 0x80 followed by null bytes to pad a string |
|
299 | + * |
|
300 | + * @param string $text The string to pad |
|
301 | + * @param integer The number of bytes to pad |
|
302 | + * @param integer $bytes |
|
303 | + * @return boolean Returns true |
|
304 | + */ |
|
305 | + private static function iso7816Pad(&$text, $bytes) |
|
306 | + { |
|
307 | + $text .= chr(0x80); |
|
308 | + $len = $bytes + strlen($text); |
|
309 | + |
|
310 | + // if we are only padding one byte, then 0x80 is all we need |
|
311 | + // else we follow up with null bytes |
|
312 | + if ($bytes > 1) |
|
313 | + $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
314 | + |
|
315 | + return true; |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Strips ISO/IEC 7816-4 padding from a string |
|
321 | + * |
|
322 | + * @param string $text The string to strip padding from |
|
323 | + * @return boolean Returns true |
|
324 | + */ |
|
325 | + private static function iso7816Strip(&$text) |
|
326 | + { |
|
327 | + $c = chr(0x80); |
|
328 | + $text = preg_replace('/'.preg_quote($c).'\0*$/', '', $text); |
|
329 | + return true; |
|
330 | + } |
|
331 | 331 | } |
332 | 332 | ?> |
@@ -67,8 +67,9 @@ discard block |
||
67 | 67 | { |
68 | 68 | // if the size of padding is not greater than 1 |
69 | 69 | // just return true, no padding will be done |
70 | - if (!($bytes > 0)) |
|
71 | - return true; |
|
70 | + if (!($bytes > 0)) { |
|
71 | + return true; |
|
72 | + } |
|
72 | 73 | |
73 | 74 | switch ($type) |
74 | 75 | { |
@@ -196,11 +197,11 @@ discard block |
||
196 | 197 | $pos = strlen($text) - 1; |
197 | 198 | $c = ord($text[$pos]); |
198 | 199 | |
199 | - if ($c == 0) |
|
200 | - return true; |
|
201 | - else if ($c == 1) |
|
202 | - $text = substr($text, 0, -1); |
|
203 | - else |
|
200 | + if ($c == 0) { |
|
201 | + return true; |
|
202 | + } else if ($c == 1) { |
|
203 | + $text = substr($text, 0, -1); |
|
204 | + } else |
|
204 | 205 | { |
205 | 206 | // the total null bytes are 1 less than the value of the final byte |
206 | 207 | $nc = $c - 1; |
@@ -226,8 +227,9 @@ discard block |
||
226 | 227 | // create the random pad bytes, we do one less than |
227 | 228 | // needed because the last byte is reserved for the |
228 | 229 | // number of padded bytes |
229 | - for ($i = 0; $i < ($bytes - 1); ++$i) |
|
230 | - $text .= chr(mt_rand(0, 255)); |
|
230 | + for ($i = 0; $i < ($bytes - 1); ++$i) { |
|
231 | + $text .= chr(mt_rand(0, 255)); |
|
232 | + } |
|
231 | 233 | |
232 | 234 | // add the byte to indicate the padding length |
233 | 235 | $text .= chr($bytes); |
@@ -248,8 +250,9 @@ discard block |
||
248 | 250 | |
249 | 251 | // if we got a null byte at the end of the string, |
250 | 252 | // just return |
251 | - if ($c == 0) |
|
252 | - return true; |
|
253 | + if ($c == 0) { |
|
254 | + return true; |
|
255 | + } |
|
253 | 256 | |
254 | 257 | $text = substr($text, 0, $c); |
255 | 258 | return true; |
@@ -285,8 +288,9 @@ discard block |
||
285 | 288 | $pos = strlen($text) - 1; |
286 | 289 | $c = ord($text[$pos]); |
287 | 290 | |
288 | - if ($c == 0) |
|
289 | - return true; |
|
291 | + if ($c == 0) { |
|
292 | + return true; |
|
293 | + } |
|
290 | 294 | |
291 | 295 | $text = preg_replace('/'.preg_quote(chr($c)).'{'.$c.'}$/', "", $text); |
292 | 296 | return true; |
@@ -309,8 +313,9 @@ discard block |
||
309 | 313 | |
310 | 314 | // if we are only padding one byte, then 0x80 is all we need |
311 | 315 | // else we follow up with null bytes |
312 | - if ($bytes > 1) |
|
313 | - $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
316 | + if ($bytes > 1) { |
|
317 | + $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
318 | + } |
|
314 | 319 | |
315 | 320 | return true; |
316 | 321 | } |
@@ -35,423 +35,423 @@ |
||
35 | 35 | */ |
36 | 36 | class PHP_Crypt |
37 | 37 | { |
38 | - // Ciphers |
|
39 | - const CIPHER_3DES = "3DES"; |
|
40 | - const CIPHER_3WAY = "3-Way"; |
|
41 | - const CIPHER_AES_128 = "AES-128"; |
|
42 | - const CIPHER_AES_192 = "AES-192"; |
|
43 | - const CIPHER_AES_256 = "AES-256"; |
|
44 | - const CIPHER_ARC4 = "ARC4"; // Alternative RC4 |
|
45 | - const CIPHER_BLOWFISH = "Blowfish"; |
|
46 | - const CIPHER_CAST_128 = "CAST-128"; |
|
47 | - const CIPHER_CAST_256 = "CAST-256"; |
|
48 | - const CIPHER_DES = "DES"; |
|
49 | - const CIPHER_ENIGMA = "Enigma"; |
|
50 | - const CIPHER_GOST = "GOST"; |
|
51 | - const CIPHER_RC2 = "RC2"; |
|
52 | - const CIPHER_RIJNDAEL_128 = "Rijndael-128"; |
|
53 | - const CIPHER_RIJNDAEL_192 = "Rijndael-192"; |
|
54 | - const CIPHER_RIJNDAEL_256 = "Rijndael-256"; |
|
55 | - const CIPHER_SKIPJACK = "Skipjack"; |
|
56 | - const CIPHER_SIMPLEXOR = "SimpleXOR"; |
|
57 | - const CIPHER_VIGENERE = "Vigenere"; // historical |
|
58 | - |
|
59 | - // Modes |
|
60 | - const MODE_CBC = "CBC"; |
|
61 | - const MODE_CFB = "CFB"; // 8 bit cfb mode |
|
62 | - const MODE_CTR = "CTR"; |
|
63 | - const MODE_ECB = "ECB"; |
|
64 | - const MODE_NCFB = "NCFB"; // blocksize cfb mode |
|
65 | - const MODE_NOFB = "NOFB"; // blocksize ofb mode |
|
66 | - const MODE_OFB = "OFB"; // 8 bit ofb mode |
|
67 | - const MODE_PCBC = "PCBC"; |
|
68 | - const MODE_RAW = "Raw"; // raw encryption, with no mode |
|
69 | - const MODE_STREAM = "Stream"; // used only for stream ciphers |
|
70 | - |
|
71 | - // The source of random data used to create keys and IV's |
|
72 | - // Used for PHP_Crypt::createKey(), PHP_Crypt::createIV() |
|
73 | - const RAND = "rand"; // uses mt_rand(), windows & unix |
|
74 | - const RAND_DEV_RAND = "/dev/random"; // unix only |
|
75 | - const RAND_DEV_URAND = "/dev/urandom"; // unix only |
|
76 | - const RAND_WIN_COM = "wincom"; // windows only, COM extension |
|
77 | - const RAND_DEFAULT_SZ = 32; // the default number of bytes returned |
|
78 | - |
|
79 | - // Padding types |
|
80 | - const PAD_ZERO = 0; |
|
81 | - const PAD_ANSI_X923 = 1; |
|
82 | - const PAD_ISO_10126 = 2; |
|
83 | - const PAD_PKCS7 = 3; |
|
84 | - const PAD_ISO_7816_4 = 4; |
|
85 | - |
|
86 | - |
|
87 | - /** @type object $cipher An instance of the cipher object selected */ |
|
88 | - private $cipher = null; |
|
89 | - |
|
90 | - /** @type object $mode An instance of the mode object selected */ |
|
91 | - private $mode = null; |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * Constructor |
|
96 | - * |
|
97 | - * @param string $key The key to use for the selected Cipher |
|
98 | - * @param string $cipher The type of cipher to use |
|
99 | - * @param string $mode The encrypt mode to use with the cipher |
|
100 | - * @param integer $padding The padding type to use. Defaults to PAD_ZERO |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
|
104 | - { |
|
105 | - /* |
|
38 | + // Ciphers |
|
39 | + const CIPHER_3DES = "3DES"; |
|
40 | + const CIPHER_3WAY = "3-Way"; |
|
41 | + const CIPHER_AES_128 = "AES-128"; |
|
42 | + const CIPHER_AES_192 = "AES-192"; |
|
43 | + const CIPHER_AES_256 = "AES-256"; |
|
44 | + const CIPHER_ARC4 = "ARC4"; // Alternative RC4 |
|
45 | + const CIPHER_BLOWFISH = "Blowfish"; |
|
46 | + const CIPHER_CAST_128 = "CAST-128"; |
|
47 | + const CIPHER_CAST_256 = "CAST-256"; |
|
48 | + const CIPHER_DES = "DES"; |
|
49 | + const CIPHER_ENIGMA = "Enigma"; |
|
50 | + const CIPHER_GOST = "GOST"; |
|
51 | + const CIPHER_RC2 = "RC2"; |
|
52 | + const CIPHER_RIJNDAEL_128 = "Rijndael-128"; |
|
53 | + const CIPHER_RIJNDAEL_192 = "Rijndael-192"; |
|
54 | + const CIPHER_RIJNDAEL_256 = "Rijndael-256"; |
|
55 | + const CIPHER_SKIPJACK = "Skipjack"; |
|
56 | + const CIPHER_SIMPLEXOR = "SimpleXOR"; |
|
57 | + const CIPHER_VIGENERE = "Vigenere"; // historical |
|
58 | + |
|
59 | + // Modes |
|
60 | + const MODE_CBC = "CBC"; |
|
61 | + const MODE_CFB = "CFB"; // 8 bit cfb mode |
|
62 | + const MODE_CTR = "CTR"; |
|
63 | + const MODE_ECB = "ECB"; |
|
64 | + const MODE_NCFB = "NCFB"; // blocksize cfb mode |
|
65 | + const MODE_NOFB = "NOFB"; // blocksize ofb mode |
|
66 | + const MODE_OFB = "OFB"; // 8 bit ofb mode |
|
67 | + const MODE_PCBC = "PCBC"; |
|
68 | + const MODE_RAW = "Raw"; // raw encryption, with no mode |
|
69 | + const MODE_STREAM = "Stream"; // used only for stream ciphers |
|
70 | + |
|
71 | + // The source of random data used to create keys and IV's |
|
72 | + // Used for PHP_Crypt::createKey(), PHP_Crypt::createIV() |
|
73 | + const RAND = "rand"; // uses mt_rand(), windows & unix |
|
74 | + const RAND_DEV_RAND = "/dev/random"; // unix only |
|
75 | + const RAND_DEV_URAND = "/dev/urandom"; // unix only |
|
76 | + const RAND_WIN_COM = "wincom"; // windows only, COM extension |
|
77 | + const RAND_DEFAULT_SZ = 32; // the default number of bytes returned |
|
78 | + |
|
79 | + // Padding types |
|
80 | + const PAD_ZERO = 0; |
|
81 | + const PAD_ANSI_X923 = 1; |
|
82 | + const PAD_ISO_10126 = 2; |
|
83 | + const PAD_PKCS7 = 3; |
|
84 | + const PAD_ISO_7816_4 = 4; |
|
85 | + |
|
86 | + |
|
87 | + /** @type object $cipher An instance of the cipher object selected */ |
|
88 | + private $cipher = null; |
|
89 | + |
|
90 | + /** @type object $mode An instance of the mode object selected */ |
|
91 | + private $mode = null; |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * Constructor |
|
96 | + * |
|
97 | + * @param string $key The key to use for the selected Cipher |
|
98 | + * @param string $cipher The type of cipher to use |
|
99 | + * @param string $mode The encrypt mode to use with the cipher |
|
100 | + * @param integer $padding The padding type to use. Defaults to PAD_ZERO |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
|
104 | + { |
|
105 | + /* |
|
106 | 106 | * CIPHERS |
107 | 107 | */ |
108 | - switch ($cipher) |
|
109 | - { |
|
110 | - case self::CIPHER_3DES: |
|
111 | - $this->cipher = new Cipher_3DES($key); |
|
112 | - break; |
|
108 | + switch ($cipher) |
|
109 | + { |
|
110 | + case self::CIPHER_3DES: |
|
111 | + $this->cipher = new Cipher_3DES($key); |
|
112 | + break; |
|
113 | 113 | |
114 | - case self::CIPHER_3WAY: |
|
115 | - $this->cipher = new Cipher_3WAY($key); |
|
116 | - break; |
|
114 | + case self::CIPHER_3WAY: |
|
115 | + $this->cipher = new Cipher_3WAY($key); |
|
116 | + break; |
|
117 | 117 | |
118 | - case self::CIPHER_AES_128: |
|
119 | - $this->cipher = new Cipher_AES_128($key); |
|
120 | - break; |
|
118 | + case self::CIPHER_AES_128: |
|
119 | + $this->cipher = new Cipher_AES_128($key); |
|
120 | + break; |
|
121 | 121 | |
122 | - case self::CIPHER_AES_192: |
|
123 | - $this->cipher = new Cipher_AES_192($key); |
|
124 | - break; |
|
122 | + case self::CIPHER_AES_192: |
|
123 | + $this->cipher = new Cipher_AES_192($key); |
|
124 | + break; |
|
125 | 125 | |
126 | - case self::CIPHER_AES_256: |
|
127 | - $this->cipher = new Cipher_AES_256($key); |
|
128 | - break; |
|
126 | + case self::CIPHER_AES_256: |
|
127 | + $this->cipher = new Cipher_AES_256($key); |
|
128 | + break; |
|
129 | 129 | |
130 | - case self::CIPHER_ARC4: // an alternative to RC4 |
|
131 | - $this->cipher = new Cipher_ARC4($key); |
|
132 | - break; |
|
130 | + case self::CIPHER_ARC4: // an alternative to RC4 |
|
131 | + $this->cipher = new Cipher_ARC4($key); |
|
132 | + break; |
|
133 | 133 | |
134 | - case self::CIPHER_BLOWFISH: |
|
135 | - $this->cipher = new Cipher_Blowfish($key); |
|
136 | - break; |
|
134 | + case self::CIPHER_BLOWFISH: |
|
135 | + $this->cipher = new Cipher_Blowfish($key); |
|
136 | + break; |
|
137 | 137 | |
138 | - case self::CIPHER_CAST_128: |
|
139 | - $this->cipher = new Cipher_CAST_128($key); |
|
140 | - break; |
|
138 | + case self::CIPHER_CAST_128: |
|
139 | + $this->cipher = new Cipher_CAST_128($key); |
|
140 | + break; |
|
141 | 141 | |
142 | - case self::CIPHER_CAST_256: |
|
143 | - $this->cipher = new Cipher_CAST_256($key); |
|
144 | - break; |
|
142 | + case self::CIPHER_CAST_256: |
|
143 | + $this->cipher = new Cipher_CAST_256($key); |
|
144 | + break; |
|
145 | 145 | |
146 | - case self::CIPHER_DES: |
|
147 | - $this->cipher = new Cipher_DES($key); |
|
148 | - break; |
|
146 | + case self::CIPHER_DES: |
|
147 | + $this->cipher = new Cipher_DES($key); |
|
148 | + break; |
|
149 | 149 | |
150 | - case self::CIPHER_ENIGMA: |
|
151 | - $this->cipher = new Cipher_Enigma($key); |
|
152 | - break; |
|
150 | + case self::CIPHER_ENIGMA: |
|
151 | + $this->cipher = new Cipher_Enigma($key); |
|
152 | + break; |
|
153 | 153 | |
154 | - case self::CIPHER_GOST: |
|
155 | - $this->cipher = new Cipher_GOST($key); |
|
156 | - break; |
|
154 | + case self::CIPHER_GOST: |
|
155 | + $this->cipher = new Cipher_GOST($key); |
|
156 | + break; |
|
157 | 157 | |
158 | - case self::CIPHER_RC2: |
|
159 | - $this->cipher = new Cipher_RC2($key); |
|
160 | - break; |
|
158 | + case self::CIPHER_RC2: |
|
159 | + $this->cipher = new Cipher_RC2($key); |
|
160 | + break; |
|
161 | 161 | |
162 | - case self::CIPHER_RIJNDAEL_128: |
|
163 | - $this->cipher = new Cipher_Rijndael_128($key); |
|
164 | - break; |
|
162 | + case self::CIPHER_RIJNDAEL_128: |
|
163 | + $this->cipher = new Cipher_Rijndael_128($key); |
|
164 | + break; |
|
165 | 165 | |
166 | - case self::CIPHER_RIJNDAEL_192: |
|
167 | - $this->cipher = new Cipher_Rijndael_192($key); |
|
168 | - break; |
|
166 | + case self::CIPHER_RIJNDAEL_192: |
|
167 | + $this->cipher = new Cipher_Rijndael_192($key); |
|
168 | + break; |
|
169 | 169 | |
170 | - case self::CIPHER_RIJNDAEL_256: |
|
171 | - $this->cipher = new Cipher_Rijndael_256($key); |
|
172 | - break; |
|
170 | + case self::CIPHER_RIJNDAEL_256: |
|
171 | + $this->cipher = new Cipher_Rijndael_256($key); |
|
172 | + break; |
|
173 | 173 | |
174 | - case self::CIPHER_SIMPLEXOR: |
|
175 | - $this->cipher = new Cipher_Simple_XOR($key); |
|
176 | - break; |
|
174 | + case self::CIPHER_SIMPLEXOR: |
|
175 | + $this->cipher = new Cipher_Simple_XOR($key); |
|
176 | + break; |
|
177 | 177 | |
178 | - case self::CIPHER_SKIPJACK: |
|
179 | - $this->cipher = new Cipher_Skipjack($key); |
|
180 | - break; |
|
178 | + case self::CIPHER_SKIPJACK: |
|
179 | + $this->cipher = new Cipher_Skipjack($key); |
|
180 | + break; |
|
181 | 181 | |
182 | - case self::CIPHER_VIGENERE: |
|
183 | - $this->cipher = new Cipher_Vigenere($key); |
|
184 | - break; |
|
182 | + case self::CIPHER_VIGENERE: |
|
183 | + $this->cipher = new Cipher_Vigenere($key); |
|
184 | + break; |
|
185 | 185 | |
186 | - default: |
|
187 | - trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
188 | - } |
|
186 | + default: |
|
187 | + trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
188 | + } |
|
189 | 189 | |
190 | 190 | |
191 | - /* |
|
191 | + /* |
|
192 | 192 | * MODES |
193 | 193 | */ |
194 | - switch ($mode) |
|
195 | - { |
|
196 | - case self::MODE_CBC: |
|
197 | - $this->mode = new Mode_CBC($this->cipher); |
|
198 | - break; |
|
199 | - |
|
200 | - case self::MODE_CFB: |
|
201 | - $this->mode = new Mode_CFB($this->cipher); |
|
202 | - break; |
|
203 | - |
|
204 | - case self::MODE_CTR: |
|
205 | - $this->mode = new Mode_CTR($this->cipher); |
|
206 | - break; |
|
207 | - |
|
208 | - case self::MODE_ECB: |
|
209 | - $this->mode = new Mode_ECB($this->cipher); |
|
210 | - break; |
|
211 | - |
|
212 | - case self::MODE_NCFB: |
|
213 | - $this->mode = new Mode_NCFB($this->cipher); |
|
214 | - break; |
|
215 | - |
|
216 | - case self::MODE_NOFB: |
|
217 | - $this->mode = new Mode_NOFB($this->cipher); |
|
218 | - break; |
|
219 | - |
|
220 | - case self::MODE_OFB: |
|
221 | - $this->mode = new Mode_OFB($this->cipher); |
|
222 | - break; |
|
223 | - |
|
224 | - case self::MODE_PCBC: |
|
225 | - $this->mode = new Mode_PCBC($this->cipher); |
|
226 | - break; |
|
227 | - |
|
228 | - case self::MODE_RAW: |
|
229 | - $this->mode = new Mode_RAW($this->cipher); |
|
230 | - break; |
|
231 | - |
|
232 | - case self::MODE_STREAM: |
|
233 | - $this->mode = new Mode_Stream($this->cipher); |
|
234 | - break; |
|
235 | - |
|
236 | - default: |
|
237 | - trigger_error("$mode is not a valid mode", E_USER_WARNING); |
|
238 | - } |
|
239 | - |
|
240 | - // set the default padding |
|
241 | - $this->padding($padding); |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * Destructor |
|
247 | - * |
|
248 | - * @return void |
|
249 | - */ |
|
250 | - public function __destruct() |
|
251 | - { |
|
252 | - |
|
253 | - } |
|
254 | - |
|
255 | - |
|
256 | - /** |
|
257 | - * Encrypt a plain text message using the Mode and Cipher selected. |
|
258 | - * Some stream modes require this function to be called in a loop |
|
259 | - * which requires the use of $result parameter to retrieve |
|
260 | - * the decrypted data. |
|
261 | - * |
|
262 | - * @param string $text The plain text string |
|
263 | - * @return string The encrypted string |
|
264 | - */ |
|
265 | - public function encrypt($text) |
|
266 | - { |
|
267 | - // check that an iv is set, if required by the mode |
|
268 | - $this->mode->checkIV(); |
|
269 | - |
|
270 | - // the encryption is done inside the mode |
|
271 | - $this->mode->encrypt($text); |
|
272 | - return $text; |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * Decrypt an encrypted message using the Mode and Cipher selected. |
|
278 | - * Some stream modes require this function to be called in a loop |
|
279 | - * which requires the use of $result parameter to retrieve |
|
280 | - * the decrypted data. |
|
281 | - * |
|
282 | - * @param string $text The encrypted string |
|
283 | - * @return string The decrypted string |
|
284 | - */ |
|
285 | - public function decrypt($text) |
|
286 | - { |
|
287 | - // check that an iv is set, if required by the mode |
|
288 | - $this->mode->checkIV(); |
|
289 | - |
|
290 | - // the decryption is done inside the mode |
|
291 | - $this->mode->decrypt($text); |
|
292 | - return $text; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Return the cipher object being used |
|
298 | - * |
|
299 | - * @return object The Cipher object |
|
300 | - */ |
|
301 | - public function cipher() |
|
302 | - { |
|
303 | - return $this->cipher; |
|
304 | - } |
|
305 | - |
|
306 | - |
|
307 | - /** |
|
308 | - * Return the mode object being used |
|
309 | - * |
|
310 | - * @return object The Mode object |
|
311 | - */ |
|
312 | - public function mode() |
|
313 | - { |
|
314 | - return $this->mode; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /** |
|
319 | - * Returns the name of the cipher being used |
|
320 | - * |
|
321 | - * @return string The name of the cipher currently in use, |
|
322 | - * it will be one of the predefined phpCrypt cipher constants |
|
323 | - */ |
|
324 | - public function cipherName() |
|
325 | - { |
|
326 | - return $this->cipher->name(); |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * Return the name of the mode being used |
|
332 | - * |
|
333 | - * @return string The name of the mode in use, it will |
|
334 | - * be one of the predefined phpCrypt mode constants |
|
335 | - */ |
|
336 | - public function modeName() |
|
337 | - { |
|
338 | - return $this->mode->name(); |
|
339 | - } |
|
340 | - |
|
341 | - |
|
342 | - /** |
|
343 | - * Returns Ciphers required block size in bytes |
|
344 | - * |
|
345 | - * @return integer The cipher data block size, in bytes |
|
346 | - */ |
|
347 | - public function cipherBlockSize() |
|
348 | - { |
|
349 | - return $this->cipher->blockSize(); |
|
350 | - } |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * Returns the cipher's required key size, in bytes |
|
355 | - * |
|
356 | - * @return integer The cipher's key size requirement, in bytes |
|
357 | - */ |
|
358 | - public function cipherKeySize() |
|
359 | - { |
|
360 | - return $this->cipher->keySize(); |
|
361 | - } |
|
362 | - |
|
363 | - |
|
364 | - /** |
|
365 | - * Sets and/or returns the key to be used. Normally you set |
|
366 | - * the key in the phpCrypt constructor. This can be usefully |
|
367 | - * if you need to change the key on the fly and don't want |
|
368 | - * to create a new instance of phpCrypt. |
|
369 | - * |
|
370 | - * If the $key parameter is not given, this function will simply |
|
371 | - * return the key currently in use. |
|
372 | - * |
|
373 | - * @param string $key Optional, The key to set |
|
374 | - * @return string The key being used |
|
375 | - */ |
|
376 | - public function cipherKey($key = "") |
|
377 | - { |
|
378 | - return $this->cipher->key($key); |
|
379 | - } |
|
380 | - |
|
381 | - |
|
382 | - /** |
|
383 | - * A helper function which will create a random key. Calls |
|
384 | - * Core::randBytes(). By default it will use PHP_Crypt::RAND for |
|
385 | - * the random source of bytes, and return a PHP_Crypt::RAND_DEFAULT_SZ |
|
386 | - * byte string. There are 4 ways to create a random byte string by |
|
387 | - * setting the $src parameter: |
|
388 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
389 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
390 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
391 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
392 | - * |
|
393 | - * @param string $src Optional, The source to use to create random bytes |
|
394 | - * @param integer $len Optional, The number of random bytes to return |
|
395 | - * @return string A random string of bytes |
|
396 | - */ |
|
397 | - public static function createKey($src = self::RAND, $len = self::RAND_DEFAULT_SZ) |
|
398 | - { |
|
399 | - return Core::randBytes($src, $len); |
|
400 | - } |
|
401 | - |
|
402 | - |
|
403 | - /** |
|
404 | - * Sets the IV to use. Note that you do not need to call |
|
405 | - * this function if creating an IV using createIV(). This |
|
406 | - * function is used when an IV has already been created |
|
407 | - * outside of phpCrypt and needs to be set. Alternatively |
|
408 | - * you can just pass the $iv parameter to the encrypt() |
|
409 | - * or decrypt() functions |
|
410 | - * |
|
411 | - * When the $iv parameter is not given, the function will |
|
412 | - * return the current IV being used. See createIV() if you |
|
413 | - * need to create an IV. |
|
414 | - * |
|
415 | - * @param string $iv Optional, The IV to use during Encryption/Decryption |
|
416 | - * @return void |
|
417 | - */ |
|
418 | - public function IV($iv = "") |
|
419 | - { |
|
420 | - return $this->mode->IV($iv); |
|
421 | - } |
|
422 | - |
|
423 | - |
|
424 | - /** |
|
425 | - * Creates an IV for the the Cipher selected, if one is required. |
|
426 | - * If you already have an IV to use, this function does not need |
|
427 | - * to be called, instead set it with setIV(). If you create an |
|
428 | - * IV with createIV(), you do not need to set it with setIV(), |
|
429 | - * as it is automatically set in this function |
|
430 | - * |
|
431 | - * $src values are: |
|
432 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
433 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
434 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
435 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
436 | - * |
|
437 | - * @param string $src Optional, how the IV is generated |
|
438 | - * @return string The IV that was created, and set for the mode |
|
439 | - */ |
|
440 | - public function createIV($src = self::RAND) |
|
441 | - { |
|
442 | - return $this->mode->createIV($src); |
|
443 | - } |
|
444 | - |
|
445 | - |
|
446 | - /** |
|
447 | - * Sets the type of padding to be used within the specified Mode |
|
448 | - * |
|
449 | - * @param string $type One of the predefined padding types |
|
450 | - * @return void |
|
451 | - */ |
|
452 | - public function padding($type = "") |
|
453 | - { |
|
454 | - return $this->mode->padding($type); |
|
455 | - } |
|
194 | + switch ($mode) |
|
195 | + { |
|
196 | + case self::MODE_CBC: |
|
197 | + $this->mode = new Mode_CBC($this->cipher); |
|
198 | + break; |
|
199 | + |
|
200 | + case self::MODE_CFB: |
|
201 | + $this->mode = new Mode_CFB($this->cipher); |
|
202 | + break; |
|
203 | + |
|
204 | + case self::MODE_CTR: |
|
205 | + $this->mode = new Mode_CTR($this->cipher); |
|
206 | + break; |
|
207 | + |
|
208 | + case self::MODE_ECB: |
|
209 | + $this->mode = new Mode_ECB($this->cipher); |
|
210 | + break; |
|
211 | + |
|
212 | + case self::MODE_NCFB: |
|
213 | + $this->mode = new Mode_NCFB($this->cipher); |
|
214 | + break; |
|
215 | + |
|
216 | + case self::MODE_NOFB: |
|
217 | + $this->mode = new Mode_NOFB($this->cipher); |
|
218 | + break; |
|
219 | + |
|
220 | + case self::MODE_OFB: |
|
221 | + $this->mode = new Mode_OFB($this->cipher); |
|
222 | + break; |
|
223 | + |
|
224 | + case self::MODE_PCBC: |
|
225 | + $this->mode = new Mode_PCBC($this->cipher); |
|
226 | + break; |
|
227 | + |
|
228 | + case self::MODE_RAW: |
|
229 | + $this->mode = new Mode_RAW($this->cipher); |
|
230 | + break; |
|
231 | + |
|
232 | + case self::MODE_STREAM: |
|
233 | + $this->mode = new Mode_Stream($this->cipher); |
|
234 | + break; |
|
235 | + |
|
236 | + default: |
|
237 | + trigger_error("$mode is not a valid mode", E_USER_WARNING); |
|
238 | + } |
|
239 | + |
|
240 | + // set the default padding |
|
241 | + $this->padding($padding); |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * Destructor |
|
247 | + * |
|
248 | + * @return void |
|
249 | + */ |
|
250 | + public function __destruct() |
|
251 | + { |
|
252 | + |
|
253 | + } |
|
254 | + |
|
255 | + |
|
256 | + /** |
|
257 | + * Encrypt a plain text message using the Mode and Cipher selected. |
|
258 | + * Some stream modes require this function to be called in a loop |
|
259 | + * which requires the use of $result parameter to retrieve |
|
260 | + * the decrypted data. |
|
261 | + * |
|
262 | + * @param string $text The plain text string |
|
263 | + * @return string The encrypted string |
|
264 | + */ |
|
265 | + public function encrypt($text) |
|
266 | + { |
|
267 | + // check that an iv is set, if required by the mode |
|
268 | + $this->mode->checkIV(); |
|
269 | + |
|
270 | + // the encryption is done inside the mode |
|
271 | + $this->mode->encrypt($text); |
|
272 | + return $text; |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * Decrypt an encrypted message using the Mode and Cipher selected. |
|
278 | + * Some stream modes require this function to be called in a loop |
|
279 | + * which requires the use of $result parameter to retrieve |
|
280 | + * the decrypted data. |
|
281 | + * |
|
282 | + * @param string $text The encrypted string |
|
283 | + * @return string The decrypted string |
|
284 | + */ |
|
285 | + public function decrypt($text) |
|
286 | + { |
|
287 | + // check that an iv is set, if required by the mode |
|
288 | + $this->mode->checkIV(); |
|
289 | + |
|
290 | + // the decryption is done inside the mode |
|
291 | + $this->mode->decrypt($text); |
|
292 | + return $text; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Return the cipher object being used |
|
298 | + * |
|
299 | + * @return object The Cipher object |
|
300 | + */ |
|
301 | + public function cipher() |
|
302 | + { |
|
303 | + return $this->cipher; |
|
304 | + } |
|
305 | + |
|
306 | + |
|
307 | + /** |
|
308 | + * Return the mode object being used |
|
309 | + * |
|
310 | + * @return object The Mode object |
|
311 | + */ |
|
312 | + public function mode() |
|
313 | + { |
|
314 | + return $this->mode; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /** |
|
319 | + * Returns the name of the cipher being used |
|
320 | + * |
|
321 | + * @return string The name of the cipher currently in use, |
|
322 | + * it will be one of the predefined phpCrypt cipher constants |
|
323 | + */ |
|
324 | + public function cipherName() |
|
325 | + { |
|
326 | + return $this->cipher->name(); |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * Return the name of the mode being used |
|
332 | + * |
|
333 | + * @return string The name of the mode in use, it will |
|
334 | + * be one of the predefined phpCrypt mode constants |
|
335 | + */ |
|
336 | + public function modeName() |
|
337 | + { |
|
338 | + return $this->mode->name(); |
|
339 | + } |
|
340 | + |
|
341 | + |
|
342 | + /** |
|
343 | + * Returns Ciphers required block size in bytes |
|
344 | + * |
|
345 | + * @return integer The cipher data block size, in bytes |
|
346 | + */ |
|
347 | + public function cipherBlockSize() |
|
348 | + { |
|
349 | + return $this->cipher->blockSize(); |
|
350 | + } |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * Returns the cipher's required key size, in bytes |
|
355 | + * |
|
356 | + * @return integer The cipher's key size requirement, in bytes |
|
357 | + */ |
|
358 | + public function cipherKeySize() |
|
359 | + { |
|
360 | + return $this->cipher->keySize(); |
|
361 | + } |
|
362 | + |
|
363 | + |
|
364 | + /** |
|
365 | + * Sets and/or returns the key to be used. Normally you set |
|
366 | + * the key in the phpCrypt constructor. This can be usefully |
|
367 | + * if you need to change the key on the fly and don't want |
|
368 | + * to create a new instance of phpCrypt. |
|
369 | + * |
|
370 | + * If the $key parameter is not given, this function will simply |
|
371 | + * return the key currently in use. |
|
372 | + * |
|
373 | + * @param string $key Optional, The key to set |
|
374 | + * @return string The key being used |
|
375 | + */ |
|
376 | + public function cipherKey($key = "") |
|
377 | + { |
|
378 | + return $this->cipher->key($key); |
|
379 | + } |
|
380 | + |
|
381 | + |
|
382 | + /** |
|
383 | + * A helper function which will create a random key. Calls |
|
384 | + * Core::randBytes(). By default it will use PHP_Crypt::RAND for |
|
385 | + * the random source of bytes, and return a PHP_Crypt::RAND_DEFAULT_SZ |
|
386 | + * byte string. There are 4 ways to create a random byte string by |
|
387 | + * setting the $src parameter: |
|
388 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
389 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
390 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
391 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
392 | + * |
|
393 | + * @param string $src Optional, The source to use to create random bytes |
|
394 | + * @param integer $len Optional, The number of random bytes to return |
|
395 | + * @return string A random string of bytes |
|
396 | + */ |
|
397 | + public static function createKey($src = self::RAND, $len = self::RAND_DEFAULT_SZ) |
|
398 | + { |
|
399 | + return Core::randBytes($src, $len); |
|
400 | + } |
|
401 | + |
|
402 | + |
|
403 | + /** |
|
404 | + * Sets the IV to use. Note that you do not need to call |
|
405 | + * this function if creating an IV using createIV(). This |
|
406 | + * function is used when an IV has already been created |
|
407 | + * outside of phpCrypt and needs to be set. Alternatively |
|
408 | + * you can just pass the $iv parameter to the encrypt() |
|
409 | + * or decrypt() functions |
|
410 | + * |
|
411 | + * When the $iv parameter is not given, the function will |
|
412 | + * return the current IV being used. See createIV() if you |
|
413 | + * need to create an IV. |
|
414 | + * |
|
415 | + * @param string $iv Optional, The IV to use during Encryption/Decryption |
|
416 | + * @return void |
|
417 | + */ |
|
418 | + public function IV($iv = "") |
|
419 | + { |
|
420 | + return $this->mode->IV($iv); |
|
421 | + } |
|
422 | + |
|
423 | + |
|
424 | + /** |
|
425 | + * Creates an IV for the the Cipher selected, if one is required. |
|
426 | + * If you already have an IV to use, this function does not need |
|
427 | + * to be called, instead set it with setIV(). If you create an |
|
428 | + * IV with createIV(), you do not need to set it with setIV(), |
|
429 | + * as it is automatically set in this function |
|
430 | + * |
|
431 | + * $src values are: |
|
432 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
433 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
434 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
435 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
436 | + * |
|
437 | + * @param string $src Optional, how the IV is generated |
|
438 | + * @return string The IV that was created, and set for the mode |
|
439 | + */ |
|
440 | + public function createIV($src = self::RAND) |
|
441 | + { |
|
442 | + return $this->mode->createIV($src); |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + /** |
|
447 | + * Sets the type of padding to be used within the specified Mode |
|
448 | + * |
|
449 | + * @param string $type One of the predefined padding types |
|
450 | + * @return void |
|
451 | + */ |
|
452 | + public function padding($type = "") |
|
453 | + { |
|
454 | + return $this->mode->padding($type); |
|
455 | + } |
|
456 | 456 | } |
457 | 457 | ?> |
@@ -71,7 +71,7 @@ |
||
71 | 71 | // The source of random data used to create keys and IV's |
72 | 72 | // Used for PHP_Crypt::createKey(), PHP_Crypt::createIV() |
73 | 73 | const RAND = "rand"; // uses mt_rand(), windows & unix |
74 | - const RAND_DEV_RAND = "/dev/random"; // unix only |
|
74 | + const RAND_DEV_RAND = "/dev/random"; // unix only |
|
75 | 75 | const RAND_DEV_URAND = "/dev/urandom"; // unix only |
76 | 76 | const RAND_WIN_COM = "wincom"; // windows only, COM extension |
77 | 77 | const RAND_DEFAULT_SZ = 32; // the default number of bytes returned |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | // the key must be between 1 and 128 bytes, keys larger than |
69 | 69 | // 128 bytes are truncated in expandedKey() |
70 | 70 | $keylen = strlen($key); |
71 | - if($keylen < 1) |
|
71 | + if ($keylen < 1) |
|
72 | 72 | { |
73 | 73 | $err = "Key size is $keylen bits, Key size must be between 1 and 128 bytes"; |
74 | 74 | trigger_error($err, E_USER_WARNING); |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | $k = self::splitBytes($this->xkey); |
116 | 116 | $j = 0; // the key index |
117 | 117 | |
118 | - for($i = 0; $i < 16; ++$i) |
|
118 | + for ($i = 0; $i < 16; ++$i) |
|
119 | 119 | { |
120 | 120 | $j = $i * 4; |
121 | 121 | |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | $w[3] = parent::uInt32($w[3]); |
146 | 146 | |
147 | 147 | // rounds 5 and 11 get rc2's Mash |
148 | - if($i == 4 || $i == 10) |
|
148 | + if ($i == 4 || $i == 10) |
|
149 | 149 | { |
150 | 150 | $w[0] += $k[$w[3] & 63]; |
151 | 151 | $w[1] += $k[$w[0] & 63]; |
@@ -160,11 +160,11 @@ discard block |
||
160 | 160 | * correct results in PHP I needed to do this as well |
161 | 161 | */ |
162 | 162 | $max = count($w); |
163 | - for($i = 0; $i < $max; ++$i) |
|
163 | + for ($i = 0; $i < $max; ++$i) |
|
164 | 164 | { |
165 | 165 | $pos = $i * 2; |
166 | 166 | $text[$pos] = chr($w[$i]); |
167 | - $text[$pos+1] = chr($w[$i] >> 8); |
|
167 | + $text[$pos + 1] = chr($w[$i] >> 8); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | return true; |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | $k = self::splitBytes($this->xkey); |
188 | 188 | $j = 0; // the key index |
189 | 189 | |
190 | - for($i = 15; $i >= 0; --$i) |
|
190 | + for ($i = 15; $i >= 0; --$i) |
|
191 | 191 | { |
192 | 192 | $j = $i * 4; |
193 | 193 | |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | $w[0] -= parent::uInt($w[1] & ~$w[3]) + parent::uInt($w[2] & $w[3]) + $k[$j + 0]; |
219 | 219 | $w[0] = parent::uInt32($w[0]); |
220 | 220 | |
221 | - if($i == 5 || $i == 11) |
|
221 | + if ($i == 5 || $i == 11) |
|
222 | 222 | { |
223 | 223 | $w[3] -= $k[$w[2] & 63]; |
224 | 224 | $w[2] -= $k[$w[1] & 63]; |
@@ -234,11 +234,11 @@ discard block |
||
234 | 234 | * correct results in PHP I needed to do this as well |
235 | 235 | */ |
236 | 236 | $max = count($w); |
237 | - for($i = 0; $i < $max; ++$i) |
|
237 | + for ($i = 0; $i < $max; ++$i) |
|
238 | 238 | { |
239 | 239 | $pos = $i * 2; |
240 | 240 | $text[$pos] = chr($w[$i]); |
241 | - $text[$pos+1] = chr($w[$i] >> 8); |
|
241 | + $text[$pos + 1] = chr($w[$i] >> 8); |
|
242 | 242 | } |
243 | 243 | |
244 | 244 | return true; |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | { |
257 | 257 | $arr = str_split($str, 2); |
258 | 258 | |
259 | - return array_map(function($b){ |
|
259 | + return array_map(function($b) { |
|
260 | 260 | return Core::str2Dec($b[1].$b[0]); |
261 | 261 | }, $arr); |
262 | 262 | } |
@@ -276,11 +276,11 @@ discard block |
||
276 | 276 | $len = $this->keySize(); |
277 | 277 | |
278 | 278 | // the max length of the key is 128 bytes |
279 | - if($len > 128) |
|
279 | + if ($len > 128) |
|
280 | 280 | $this->xkey = substr($this->xkey, 0, 128); |
281 | 281 | |
282 | 282 | // now expanded the rest of the key to 128 bytes, using the sbox |
283 | - for($i = $len; $i < 128; ++$i) |
|
283 | + for ($i = $len; $i < 128; ++$i) |
|
284 | 284 | { |
285 | 285 | $byte1 = ord($this->xkey[$i - $len]); |
286 | 286 | $byte2 = ord($this->xkey[$i - 1]); |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | |
289 | 289 | // the sbox is only 255 bytes, so if we extend past that |
290 | 290 | // we need to modulo 256 so we have a valid position |
291 | - if($pos > 255) |
|
291 | + if ($pos > 255) |
|
292 | 292 | $pos -= 256; |
293 | 293 | |
294 | 294 | $this->xkey .= chr(self::$_sbox[$pos]); |
@@ -276,8 +276,9 @@ discard block |
||
276 | 276 | $len = $this->keySize(); |
277 | 277 | |
278 | 278 | // the max length of the key is 128 bytes |
279 | - if($len > 128) |
|
280 | - $this->xkey = substr($this->xkey, 0, 128); |
|
279 | + if($len > 128) { |
|
280 | + $this->xkey = substr($this->xkey, 0, 128); |
|
281 | + } |
|
281 | 282 | |
282 | 283 | // now expanded the rest of the key to 128 bytes, using the sbox |
283 | 284 | for($i = $len; $i < 128; ++$i) |
@@ -288,8 +289,9 @@ discard block |
||
288 | 289 | |
289 | 290 | // the sbox is only 255 bytes, so if we extend past that |
290 | 291 | // we need to modulo 256 so we have a valid position |
291 | - if($pos > 255) |
|
292 | - $pos -= 256; |
|
292 | + if($pos > 255) { |
|
293 | + $pos -= 256; |
|
294 | + } |
|
293 | 295 | |
294 | 296 | $this->xkey .= chr(self::$_sbox[$pos]); |
295 | 297 | } |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | $this->nr2 = 0; |
141 | 141 | |
142 | 142 | $max = strlen($str); |
143 | - for($j = 0; $j < $max; ++$j) |
|
143 | + for ($j = 0; $j < $max; ++$j) |
|
144 | 144 | { |
145 | 145 | $i = ord($str[$j]); |
146 | 146 | $this->nr1 = $this->n1; |
@@ -153,12 +153,12 @@ discard block |
||
153 | 153 | $str[$j] = chr($i); |
154 | 154 | $this->n1++; |
155 | 155 | |
156 | - if($this->n1 == self::ROTORSZ) |
|
156 | + if ($this->n1 == self::ROTORSZ) |
|
157 | 157 | { |
158 | 158 | $this->n1 = 0; |
159 | 159 | $this->n2++; |
160 | 160 | |
161 | - if($this->n2 == self::ROTORSZ) |
|
161 | + if ($this->n2 == self::ROTORSZ) |
|
162 | 162 | $this->n2 = 0; |
163 | 163 | |
164 | 164 | $this->nr2 = $this->n2; |
@@ -189,22 +189,22 @@ discard block |
||
189 | 189 | $klen = $this->keySize(); |
190 | 190 | |
191 | 191 | // get the key to exactly 13 bytes if it's less than 13 |
192 | - if($klen < 13) |
|
192 | + if ($klen < 13) |
|
193 | 193 | $this->xkey = str_pad($this->xkey, 13, chr(0), STR_PAD_RIGHT); |
194 | 194 | |
195 | 195 | $seed = 123; |
196 | - for($i = 0; $i < 13; ++$i) |
|
196 | + for ($i = 0; $i < 13; ++$i) |
|
197 | 197 | $seed = parent::sInt32($seed) * ord($this->xkey[$i]) + $i; |
198 | 198 | |
199 | 199 | // sets $t1 and $deck |
200 | - for($i = 0; $i < self::ROTORSZ; ++$i) |
|
200 | + for ($i = 0; $i < self::ROTORSZ; ++$i) |
|
201 | 201 | { |
202 | 202 | $this->t1[] = $i; |
203 | 203 | $this->deck[] = $i; |
204 | 204 | } |
205 | 205 | |
206 | 206 | // sets $t3 |
207 | - for($i = 0; $i < self::ROTORSZ; ++$i) |
|
207 | + for ($i = 0; $i < self::ROTORSZ; ++$i) |
|
208 | 208 | { |
209 | 209 | // make sure the return values are 32 bit |
210 | 210 | $seed = 5 * parent::sInt32($seed) + ord($this->xkey[$i % 13]); |
@@ -223,11 +223,11 @@ discard block |
||
223 | 223 | $temp = $this->t1[$k]; |
224 | 224 | $this->t1[$k] = $this->t1[$ic]; |
225 | 225 | $this->t1[$ic] = $temp; |
226 | - if($this->t3[$k] != 0) |
|
226 | + if ($this->t3[$k] != 0) |
|
227 | 227 | continue; |
228 | 228 | |
229 | 229 | $ic = ($random & self::MASK) % $k; |
230 | - while($this->t3[$ic] != 0) |
|
230 | + while ($this->t3[$ic] != 0) |
|
231 | 231 | $ic = ($ic + 1) % $k; |
232 | 232 | |
233 | 233 | $this->t3[$k] = $ic; |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | } |
236 | 236 | |
237 | 237 | // sets $t2 |
238 | - for($i = 0; $i < self::ROTORSZ; ++$i) |
|
238 | + for ($i = 0; $i < self::ROTORSZ; ++$i) |
|
239 | 239 | { |
240 | 240 | $pos = $this->t1[$i] & self::MASK; |
241 | 241 | $this->t2[$pos] = $i; |
@@ -158,8 +158,9 @@ discard block |
||
158 | 158 | $this->n1 = 0; |
159 | 159 | $this->n2++; |
160 | 160 | |
161 | - if($this->n2 == self::ROTORSZ) |
|
162 | - $this->n2 = 0; |
|
161 | + if($this->n2 == self::ROTORSZ) { |
|
162 | + $this->n2 = 0; |
|
163 | + } |
|
163 | 164 | |
164 | 165 | $this->nr2 = $this->n2; |
165 | 166 | } |
@@ -189,12 +190,14 @@ discard block |
||
189 | 190 | $klen = $this->keySize(); |
190 | 191 | |
191 | 192 | // get the key to exactly 13 bytes if it's less than 13 |
192 | - if($klen < 13) |
|
193 | - $this->xkey = str_pad($this->xkey, 13, chr(0), STR_PAD_RIGHT); |
|
193 | + if($klen < 13) { |
|
194 | + $this->xkey = str_pad($this->xkey, 13, chr(0), STR_PAD_RIGHT); |
|
195 | + } |
|
194 | 196 | |
195 | 197 | $seed = 123; |
196 | - for($i = 0; $i < 13; ++$i) |
|
197 | - $seed = parent::sInt32($seed) * ord($this->xkey[$i]) + $i; |
|
198 | + for($i = 0; $i < 13; ++$i) { |
|
199 | + $seed = parent::sInt32($seed) * ord($this->xkey[$i]) + $i; |
|
200 | + } |
|
198 | 201 | |
199 | 202 | // sets $t1 and $deck |
200 | 203 | for($i = 0; $i < self::ROTORSZ; ++$i) |
@@ -223,12 +226,14 @@ discard block |
||
223 | 226 | $temp = $this->t1[$k]; |
224 | 227 | $this->t1[$k] = $this->t1[$ic]; |
225 | 228 | $this->t1[$ic] = $temp; |
226 | - if($this->t3[$k] != 0) |
|
227 | - continue; |
|
229 | + if($this->t3[$k] != 0) { |
|
230 | + continue; |
|
231 | + } |
|
228 | 232 | |
229 | 233 | $ic = ($random & self::MASK) % $k; |
230 | - while($this->t3[$ic] != 0) |
|
231 | - $ic = ($ic + 1) % $k; |
|
234 | + while($this->t3[$ic] != 0) { |
|
235 | + $ic = ($ic + 1) % $k; |
|
236 | + } |
|
232 | 237 | |
233 | 238 | $this->t3[$k] = $ic; |
234 | 239 | $this->t3[$ic] = $k; |
@@ -45,101 +45,101 @@ |
||
45 | 45 | */ |
46 | 46 | class Cipher_Simple_XOR extends Cipher |
47 | 47 | { |
48 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
49 | - const BYTES_BLOCK = 1; // 8 bits |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * Constructor |
|
54 | - * Sets the key used for encryption. Also sets the requied block size |
|
55 | - * |
|
56 | - * @param string $key string containing the user supplied encryption key |
|
57 | - * @return void |
|
58 | - */ |
|
59 | - public function __construct($key) |
|
60 | - { |
|
61 | - // SimpleXOR does not have a key size requirement |
|
62 | - parent::__construct(PHP_Crypt::CIPHER_SIMPLEXOR, $key); |
|
63 | - |
|
64 | - // required block size in bits |
|
65 | - $this->blockSize(self::BYTES_BLOCK); |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * Destructor |
|
71 | - * |
|
72 | - * @return void |
|
73 | - */ |
|
74 | - public function __destruct() |
|
75 | - { |
|
76 | - parent::__destruct(); |
|
77 | - } |
|
78 | - |
|
79 | - |
|
80 | - /** |
|
81 | - * Encrypts data using an XOR encryption scheme |
|
82 | - * |
|
83 | - * @param string $text A string to encrypt |
|
84 | - * @return boolean Always returns true |
|
85 | - */ |
|
86 | - public function encrypt(&$text) |
|
87 | - { |
|
88 | - $this->operation(parent::ENCRYPT); |
|
89 | - return $this->simpleXOR($text); |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Decrypts data encrypted with SimpleXOR::Encrypt() |
|
95 | - * |
|
96 | - * @param string $text An encrypted string to decrypt |
|
97 | - * @return boolean Always returns true |
|
98 | - */ |
|
99 | - public function decrypt(&$text) |
|
100 | - { |
|
101 | - $this->operation(parent::DECRYPT); |
|
102 | - return $this->simpleXOR($text); |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * Because XOR Encryption uses the same algorithm to encrypt and decrypt, |
|
108 | - * this function contains the code to do both. The SimpleXOR::Encrypt() |
|
109 | - * and SimpleXOR::Decrypt() function above just call this function |
|
110 | - * |
|
111 | - * @param string $text |
|
112 | - * @return boolean Always returns true |
|
113 | - */ |
|
114 | - private function simpleXOR(&$text) |
|
115 | - { |
|
116 | - $pos = 0; |
|
117 | - $max = strlen($text); |
|
118 | - $key = $this->key(); |
|
119 | - |
|
120 | - for ($i = 0; $i < $max; ++$i) |
|
121 | - { |
|
122 | - // if the current position in the key reaches the end of the key, |
|
123 | - // start over at position 0 of the key |
|
124 | - if ($pos >= $this->keySize()) |
|
125 | - $pos = 0; |
|
126 | - |
|
127 | - $text[$i] = $text[$i] ^ $key[$pos]; |
|
128 | - ++$pos; |
|
129 | - } |
|
130 | - |
|
131 | - return true; |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * Indicates that this is stream cipher |
|
137 | - * |
|
138 | - * @return integer Returns Cipher::STREAM |
|
139 | - */ |
|
140 | - public function type() |
|
141 | - { |
|
142 | - return parent::STREAM; |
|
143 | - } |
|
48 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
49 | + const BYTES_BLOCK = 1; // 8 bits |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * Constructor |
|
54 | + * Sets the key used for encryption. Also sets the requied block size |
|
55 | + * |
|
56 | + * @param string $key string containing the user supplied encryption key |
|
57 | + * @return void |
|
58 | + */ |
|
59 | + public function __construct($key) |
|
60 | + { |
|
61 | + // SimpleXOR does not have a key size requirement |
|
62 | + parent::__construct(PHP_Crypt::CIPHER_SIMPLEXOR, $key); |
|
63 | + |
|
64 | + // required block size in bits |
|
65 | + $this->blockSize(self::BYTES_BLOCK); |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * Destructor |
|
71 | + * |
|
72 | + * @return void |
|
73 | + */ |
|
74 | + public function __destruct() |
|
75 | + { |
|
76 | + parent::__destruct(); |
|
77 | + } |
|
78 | + |
|
79 | + |
|
80 | + /** |
|
81 | + * Encrypts data using an XOR encryption scheme |
|
82 | + * |
|
83 | + * @param string $text A string to encrypt |
|
84 | + * @return boolean Always returns true |
|
85 | + */ |
|
86 | + public function encrypt(&$text) |
|
87 | + { |
|
88 | + $this->operation(parent::ENCRYPT); |
|
89 | + return $this->simpleXOR($text); |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Decrypts data encrypted with SimpleXOR::Encrypt() |
|
95 | + * |
|
96 | + * @param string $text An encrypted string to decrypt |
|
97 | + * @return boolean Always returns true |
|
98 | + */ |
|
99 | + public function decrypt(&$text) |
|
100 | + { |
|
101 | + $this->operation(parent::DECRYPT); |
|
102 | + return $this->simpleXOR($text); |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * Because XOR Encryption uses the same algorithm to encrypt and decrypt, |
|
108 | + * this function contains the code to do both. The SimpleXOR::Encrypt() |
|
109 | + * and SimpleXOR::Decrypt() function above just call this function |
|
110 | + * |
|
111 | + * @param string $text |
|
112 | + * @return boolean Always returns true |
|
113 | + */ |
|
114 | + private function simpleXOR(&$text) |
|
115 | + { |
|
116 | + $pos = 0; |
|
117 | + $max = strlen($text); |
|
118 | + $key = $this->key(); |
|
119 | + |
|
120 | + for ($i = 0; $i < $max; ++$i) |
|
121 | + { |
|
122 | + // if the current position in the key reaches the end of the key, |
|
123 | + // start over at position 0 of the key |
|
124 | + if ($pos >= $this->keySize()) |
|
125 | + $pos = 0; |
|
126 | + |
|
127 | + $text[$i] = $text[$i] ^ $key[$pos]; |
|
128 | + ++$pos; |
|
129 | + } |
|
130 | + |
|
131 | + return true; |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * Indicates that this is stream cipher |
|
137 | + * |
|
138 | + * @return integer Returns Cipher::STREAM |
|
139 | + */ |
|
140 | + public function type() |
|
141 | + { |
|
142 | + return parent::STREAM; |
|
143 | + } |
|
144 | 144 | } |
145 | 145 | ?> |
@@ -121,8 +121,9 @@ |
||
121 | 121 | { |
122 | 122 | // if the current position in the key reaches the end of the key, |
123 | 123 | // start over at position 0 of the key |
124 | - if ($pos >= $this->keySize()) |
|
125 | - $pos = 0; |
|
124 | + if ($pos >= $this->keySize()) { |
|
125 | + $pos = 0; |
|
126 | + } |
|
126 | 127 | |
127 | 128 | $text[$i] = $text[$i] ^ $key[$pos]; |
128 | 129 | ++$pos; |
@@ -75,14 +75,14 @@ discard block |
||
75 | 75 | { |
76 | 76 | // the max length of the key is 448 bits (56 bytes) |
77 | 77 | $keylen = strlen($key); |
78 | - if($keylen > 56) |
|
78 | + if ($keylen > 56) |
|
79 | 79 | { |
80 | 80 | $key = substr($key, 0, 56); |
81 | 81 | $keylen = 56; |
82 | 82 | } |
83 | - else if($keylen < 1) |
|
83 | + else if ($keylen < 1) |
|
84 | 84 | { |
85 | - $msg = "No key given. The key must be between 1 - 56 bytes."; |
|
85 | + $msg = "No key given. The key must be between 1 - 56 bytes."; |
|
86 | 86 | trigger_error($msg, E_USER_WARNING); |
87 | 87 | } |
88 | 88 | |
@@ -146,12 +146,12 @@ discard block |
||
146 | 146 | $xl = parent::str2Dec(substr($data, 0, 4)); |
147 | 147 | $xr = parent::str2Dec(substr($data, 4, 4)); |
148 | 148 | |
149 | - for($i = 0; $i < 16; ++$i) |
|
149 | + for ($i = 0; $i < 16; ++$i) |
|
150 | 150 | { |
151 | - if($this->operation() == parent::ENCRYPT) |
|
151 | + if ($this->operation() == parent::ENCRYPT) |
|
152 | 152 | $xl ^= self::$_p[$i]; |
153 | 153 | else |
154 | - $xl ^= self::$_p[17-$i]; |
|
154 | + $xl ^= self::$_p[17 - $i]; |
|
155 | 155 | |
156 | 156 | // perform F() on the left half, and XOR with the right half |
157 | 157 | $xr = $this->F($xl) ^ $xr; |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | $xr = $tmp; |
169 | 169 | |
170 | 170 | // XOR the final two elements of $_p |
171 | - if($this->operation() == parent::ENCRYPT) |
|
171 | + if ($this->operation() == parent::ENCRYPT) |
|
172 | 172 | { |
173 | 173 | $xr ^= self::$_p[16]; |
174 | 174 | $xl = $xl ^ self::$_p[17]; |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | { |
196 | 196 | // split the 32 bits into four 8 bit parts |
197 | 197 | $x[0] = $i & 0xff; // first byte |
198 | - $x[1] = ($i >> 8) & 0xff; // second byte |
|
198 | + $x[1] = ($i >> 8) & 0xff; // second byte |
|
199 | 199 | $x[2] = ($i >> 16) & 0xff; // third byte |
200 | 200 | $x[3] = ($i >> 24) & 0xff; // fourth byte |
201 | 201 | |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | private function subKeys() |
218 | 218 | { |
219 | 219 | // now xor each element of $_p with 32 bits from the key |
220 | - for($i = 0; $i < 18; ++$i) |
|
220 | + for ($i = 0; $i < 18; ++$i) |
|
221 | 221 | { |
222 | 222 | $c = $this->keyChunk(4); |
223 | 223 | self::$_p[$i] ^= parent::str2Dec($c); |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | |
229 | 229 | // now we loop, each loop replacing elements of $_p, or an $_sbox with the |
230 | 230 | // repeatedly encrypted zero string |
231 | - for($i = 0; $i < 1042; $i += 2) |
|
231 | + for ($i = 0; $i < 1042; $i += 2) |
|
232 | 232 | { |
233 | 233 | // encrypt the 64 bit null string |
234 | 234 | $this->encrypt($zero); |
@@ -239,29 +239,29 @@ discard block |
||
239 | 239 | |
240 | 240 | // now fill the $_p, $_sbox1, $_sbox2, $_sbox3, $_sbox4 |
241 | 241 | // with 4 bytes from the repeatedly encrypted 8 byte null string |
242 | - if($i < 18) |
|
242 | + if ($i < 18) |
|
243 | 243 | { |
244 | 244 | self::$_p[$i] = $z0; |
245 | 245 | self::$_p[$i + 1] = $z1; |
246 | 246 | } |
247 | - else if($i >= 18 && $i < 274) |
|
247 | + else if ($i >= 18 && $i < 274) |
|
248 | 248 | { |
249 | 249 | self::$_sbox1[$i - 18] = $z0; |
250 | 250 | self::$_sbox1[$i - 18 + 1] = $z1; |
251 | 251 | } |
252 | - else if($i >= 274 && $i < 530) |
|
252 | + else if ($i >= 274 && $i < 530) |
|
253 | 253 | { |
254 | 254 | self::$_sbox2[$i - 274] = $z0; |
255 | 255 | self::$_sbox2[$i - 274 + 1] = $z1; |
256 | 256 | } |
257 | - else if($i >= 530 && $i < 786) |
|
257 | + else if ($i >= 530 && $i < 786) |
|
258 | 258 | { |
259 | 259 | self::$_sbox3[$i - 530] = $z0; |
260 | 260 | self::$_sbox3[$i - 530 + 1] = $z1; |
261 | 261 | } |
262 | - else if($i >= 786 && $i < 1042) |
|
262 | + else if ($i >= 786 && $i < 1042) |
|
263 | 263 | { |
264 | - self::$_sbox4[$i -786] = $z0; |
|
264 | + self::$_sbox4[$i - 786] = $z0; |
|
265 | 265 | self::$_sbox4[$i - 786 + 1] = $z1; |
266 | 266 | } |
267 | 267 | } |
@@ -284,12 +284,12 @@ discard block |
||
284 | 284 | */ |
285 | 285 | private function keyChunk($size = 1, $reset = false) |
286 | 286 | { |
287 | - if($reset || $this->key_pos >= $this->keySize()) |
|
287 | + if ($reset || $this->key_pos >= $this->keySize()) |
|
288 | 288 | $this->key_pos = 0; |
289 | 289 | |
290 | 290 | $bytes = substr($this->key(), $this->key_pos, $size); |
291 | 291 | $len = strlen($bytes); |
292 | - if($len < $size) |
|
292 | + if ($len < $size) |
|
293 | 293 | { |
294 | 294 | $bytes .= substr($this->key(), 0, $size - $len); |
295 | 295 | $this->key_pos = $size - $len; |
@@ -79,8 +79,7 @@ discard block |
||
79 | 79 | { |
80 | 80 | $key = substr($key, 0, 56); |
81 | 81 | $keylen = 56; |
82 | - } |
|
83 | - else if($keylen < 1) |
|
82 | + } else if($keylen < 1) |
|
84 | 83 | { |
85 | 84 | $msg = "No key given. The key must be between 1 - 56 bytes."; |
86 | 85 | trigger_error($msg, E_USER_WARNING); |
@@ -148,10 +147,11 @@ discard block |
||
148 | 147 | |
149 | 148 | for($i = 0; $i < 16; ++$i) |
150 | 149 | { |
151 | - if($this->operation() == parent::ENCRYPT) |
|
152 | - $xl ^= self::$_p[$i]; |
|
153 | - else |
|
154 | - $xl ^= self::$_p[17-$i]; |
|
150 | + if($this->operation() == parent::ENCRYPT) { |
|
151 | + $xl ^= self::$_p[$i]; |
|
152 | + } else { |
|
153 | + $xl ^= self::$_p[17-$i]; |
|
154 | + } |
|
155 | 155 | |
156 | 156 | // perform F() on the left half, and XOR with the right half |
157 | 157 | $xr = $this->F($xl) ^ $xr; |
@@ -172,8 +172,7 @@ discard block |
||
172 | 172 | { |
173 | 173 | $xr ^= self::$_p[16]; |
174 | 174 | $xl = $xl ^ self::$_p[17]; |
175 | - } |
|
176 | - else // parent::DECRYPT |
|
175 | + } else // parent::DECRYPT |
|
177 | 176 | { |
178 | 177 | $xr ^= self::$_p[1]; |
179 | 178 | $xl ^= self::$_p[0]; |
@@ -243,23 +242,19 @@ discard block |
||
243 | 242 | { |
244 | 243 | self::$_p[$i] = $z0; |
245 | 244 | self::$_p[$i + 1] = $z1; |
246 | - } |
|
247 | - else if($i >= 18 && $i < 274) |
|
245 | + } else if($i >= 18 && $i < 274) |
|
248 | 246 | { |
249 | 247 | self::$_sbox1[$i - 18] = $z0; |
250 | 248 | self::$_sbox1[$i - 18 + 1] = $z1; |
251 | - } |
|
252 | - else if($i >= 274 && $i < 530) |
|
249 | + } else if($i >= 274 && $i < 530) |
|
253 | 250 | { |
254 | 251 | self::$_sbox2[$i - 274] = $z0; |
255 | 252 | self::$_sbox2[$i - 274 + 1] = $z1; |
256 | - } |
|
257 | - else if($i >= 530 && $i < 786) |
|
253 | + } else if($i >= 530 && $i < 786) |
|
258 | 254 | { |
259 | 255 | self::$_sbox3[$i - 530] = $z0; |
260 | 256 | self::$_sbox3[$i - 530 + 1] = $z1; |
261 | - } |
|
262 | - else if($i >= 786 && $i < 1042) |
|
257 | + } else if($i >= 786 && $i < 1042) |
|
263 | 258 | { |
264 | 259 | self::$_sbox4[$i -786] = $z0; |
265 | 260 | self::$_sbox4[$i - 786 + 1] = $z1; |
@@ -284,8 +279,9 @@ discard block |
||
284 | 279 | */ |
285 | 280 | private function keyChunk($size = 1, $reset = false) |
286 | 281 | { |
287 | - if($reset || $this->key_pos >= $this->keySize()) |
|
288 | - $this->key_pos = 0; |
|
282 | + if($reset || $this->key_pos >= $this->keySize()) { |
|
283 | + $this->key_pos = 0; |
|
284 | + } |
|
289 | 285 | |
290 | 286 | $bytes = substr($this->key(), $this->key_pos, $size); |
291 | 287 | $len = strlen($bytes); |
@@ -293,9 +289,9 @@ discard block |
||
293 | 289 | { |
294 | 290 | $bytes .= substr($this->key(), 0, $size - $len); |
295 | 291 | $this->key_pos = $size - $len; |
292 | + } else { |
|
293 | + $this->key_pos += $size; |
|
296 | 294 | } |
297 | - else |
|
298 | - $this->key_pos += $size; |
|
299 | 295 | |
300 | 296 | return $bytes; |
301 | 297 | } |
@@ -38,619 +38,619 @@ |
||
38 | 38 | */ |
39 | 39 | class Cipher_CAST_256 extends Cipher |
40 | 40 | { |
41 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | - const BYTES_BLOCK = 16; // 128 bits; |
|
43 | - |
|
44 | - //const BYTES_KEY = 0; // between 128, 160, 192, 224, 256 bits |
|
45 | - |
|
46 | - /** @type integer BYTES_KEY_MAX The max key size, in bytes */ |
|
47 | - const BYTES_KEY_MAX = 32; |
|
48 | - |
|
49 | - /** @type array $_s1 An array of 256 unsigned integers */ |
|
50 | - private static $_s1 = array(); |
|
51 | - |
|
52 | - /** @type array $_s2 An array of 256 unsigned integers */ |
|
53 | - private static $_s2 = array(); |
|
54 | - |
|
55 | - /** @type array $_s3 An array of 256 unsigned integers */ |
|
56 | - private static $_s3 = array(); |
|
57 | - |
|
58 | - /** @type array $_s4 An array of 256 unsigned integers */ |
|
59 | - private static $_s4 = array(); |
|
60 | - |
|
61 | - private static $_req_key_sizes = array(16, 20, 24, 28, 32); |
|
62 | - |
|
63 | - /** @type string $_mkey The 16 byte masking subkey */ |
|
64 | - private $_mkey = array(); |
|
65 | - |
|
66 | - /** @type string $_rkey The 16 byte rotate subkey */ |
|
67 | - private $_rkey = array(); |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * Constructor |
|
72 | - * |
|
73 | - * @param string $key The key used for Encryption/Decryption |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - public function __construct($key) |
|
77 | - { |
|
78 | - $keylen = strlen($key); |
|
79 | - |
|
80 | - if ($keylen > self::BYTES_KEY_MAX) |
|
81 | - { |
|
82 | - $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
83 | - $keylen = self::BYTES_KEY_MAX; |
|
84 | - } |
|
85 | - else if (!in_array($keylen, self::$_req_key_sizes)) |
|
86 | - { |
|
87 | - $msg = PHP_Crypt::CIPHER_CAST_256." requires a key size of 16, "; |
|
88 | - $msg .= "20, 24, 28, or 32 bytes."; |
|
89 | - trigger_error($msg, E_USER_WARNING); |
|
90 | - } |
|
91 | - |
|
92 | - // set the key, make sure the required length is set in bytes |
|
93 | - parent::__construct(PHP_Crypt::CIPHER_CAST_256, $key, $keylen); |
|
94 | - |
|
95 | - // set the block size |
|
96 | - $this->blockSize(self::BYTES_BLOCK); |
|
97 | - |
|
98 | - // initialize the sboxes constants |
|
99 | - $this->initTables(); |
|
100 | - |
|
101 | - // create the sub keys using the sboxes |
|
102 | - $this->createSubKeys(); |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * Destructor |
|
108 | - * |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - public function __destruct() |
|
112 | - { |
|
113 | - parent::__destruct(); |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * Encrypt plain text data |
|
119 | - * |
|
120 | - * @param string $data A 128 bit block of plain data |
|
121 | - * @return boolean Returns true |
|
122 | - */ |
|
123 | - public function encrypt(&$data) |
|
124 | - { |
|
125 | - $this->operation(parent::ENCRYPT); |
|
126 | - |
|
127 | - // first split the data into four 32 bit blocks, reverse |
|
128 | - // the string order of each block, convert the blocks of data to integers |
|
129 | - $data = str_split($data, 4); |
|
130 | - $data = array_map("strrev", $data); |
|
131 | - $data = array_map("parent::str2Dec", $data); |
|
132 | - |
|
133 | - // do the first 6 loops |
|
134 | - for ($i = 0; $i < 6; ++$i) |
|
135 | - { |
|
136 | - |
|
137 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
138 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
139 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
140 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
141 | - } |
|
142 | - |
|
143 | - // the second 6 loops are done in a different order |
|
144 | - for ($i = 6; $i < 12; ++$i) |
|
145 | - { |
|
146 | - |
|
147 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
148 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
149 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
150 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
151 | - } |
|
152 | - |
|
153 | - // convert the decimals back to a string, reverse the string so it's |
|
154 | - // in the correct order |
|
155 | - $data = array_map(function($v) { |
|
156 | - $v = Core::dec2Str($v, 4); |
|
157 | - return strrev($v); |
|
158 | - }, $data); |
|
159 | - |
|
160 | - // glue the string back together |
|
161 | - $data = implode("", $data); |
|
162 | - |
|
163 | - return true; |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * Decrypt an encrypted string, it does all the steps of encryption, |
|
169 | - * but in reverse. |
|
170 | - * |
|
171 | - * @param string $data A 128 bit block of encrypted data |
|
172 | - * @return boolean Returns true |
|
173 | - */ |
|
174 | - public function decrypt(&$data) |
|
175 | - { |
|
176 | - $this->operation(parent::DECRYPT); |
|
177 | - |
|
178 | - // first split the data into four 32 bit blocks, reverse |
|
179 | - // the string order of each block, convert the blocks of data to integers |
|
180 | - $data = str_split($data, 4); |
|
181 | - $data = array_map("strrev", $data); |
|
182 | - $data = array_map("parent::str2Dec", $data); |
|
183 | - |
|
184 | - // do the first 6 loops |
|
185 | - for ($i = 11; $i >= 6; --$i) |
|
186 | - { |
|
187 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
188 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
189 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
190 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
191 | - } |
|
192 | - |
|
193 | - // the second 6 loops are done in a different order |
|
194 | - for ($i = 5; $i >= 0; --$i) |
|
195 | - { |
|
196 | - $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
197 | - $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
198 | - $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
199 | - $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
200 | - } |
|
201 | - |
|
202 | - // convert the decimals back to a string, reverse the string so it's |
|
203 | - // in the correct order |
|
204 | - $data = array_map(function($v) { |
|
205 | - $v = Core::dec2Str($v, 4); |
|
206 | - return strrev($v); |
|
207 | - }, $data); |
|
208 | - |
|
209 | - // glue the string back together |
|
210 | - $data = implode("", $data); |
|
211 | - |
|
212 | - return true; |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * CAST-256 F1 function |
|
218 | - * |
|
219 | - * @param $d integer The the data input |
|
220 | - * @param $m integer The 32 bit masking key |
|
221 | - * @param $r integer The round number |
|
222 | - * @return integer The value after the F1 calculation |
|
223 | - */ |
|
224 | - private function f1($d, $m, $r) |
|
225 | - { |
|
226 | - $n = parent::uInt32($m + $d); |
|
227 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
228 | - $n = parent::dec2Str($n, 4); |
|
229 | - |
|
230 | - return parent::uInt32( |
|
231 | - ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
232 | - self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
233 | - ); |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * CAST-256 F2 function |
|
239 | - * |
|
240 | - * @param integer $d integer The the data input |
|
241 | - * @param $m integer The 32 bit masking key |
|
242 | - * @param $r integer The round number |
|
243 | - * @return integer The value after the F2 calculation |
|
244 | - */ |
|
245 | - private function f2($d, $m, $r) |
|
246 | - { |
|
247 | - $n = parent::uInt32($m ^ $d); |
|
248 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
249 | - $n = parent::dec2Str($n, 4); |
|
250 | - |
|
251 | - return parent::uInt32( |
|
252 | - ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
253 | - self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
254 | - ); |
|
255 | - } |
|
256 | - |
|
257 | - |
|
258 | - /** |
|
259 | - * CAST-256 F3 function |
|
260 | - * |
|
261 | - * @param integer $d integer The the data input |
|
262 | - * @param $m integer The 32 bit masking key |
|
263 | - * @param $r integer The round number |
|
264 | - * @return integer The value after the F3 calculation |
|
265 | - */ |
|
266 | - private function f3($d, $m, $r) |
|
267 | - { |
|
268 | - $n = parent::uInt32($m - $d); |
|
269 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
270 | - $n = parent::dec2Str($n, 4); |
|
271 | - |
|
272 | - return parent::uInt32( |
|
273 | - ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
274 | - self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
275 | - ); |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - /** |
|
280 | - * Creates the subkeys $_mkey (the masking key) and |
|
281 | - * $_rkey (the rotate key) which are 16 bytes each. These are |
|
282 | - * created from the original key. The original key is null |
|
283 | - * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
284 | - * split in half to create $_mkey and $_rkey |
|
285 | - * |
|
286 | - * @return void |
|
287 | - */ |
|
288 | - private function createSubKeys() |
|
289 | - { |
|
290 | - $cm = 0x5A827999; |
|
291 | - $mm = 0x6ED9EBA1; |
|
292 | - $cr = 19; |
|
293 | - $mr = 17; |
|
294 | - $tm = array(); |
|
295 | - $tr = array(); |
|
296 | - $xkey = $this->key(); |
|
297 | - $tmpkey = array(); |
|
298 | - |
|
299 | - // if the key is less than 32 bytes, pad it to 32 bytes |
|
300 | - // for the key expansion |
|
301 | - if ($this->keySize() < 32) |
|
302 | - $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT); |
|
303 | - |
|
304 | - // split the key up into 4 byte parts, reverse the string, |
|
305 | - // then convert each part into a 32 bit integer |
|
306 | - $xkey = str_split($xkey, 4); |
|
307 | - $xkey = array_map("strrev", $xkey); |
|
308 | - $xkey = array_map("parent::str2Dec", $xkey); |
|
309 | - |
|
310 | - // set up the values need for creating round and masking keys |
|
311 | - for ($i = 0; $i < 24; ++$i) |
|
312 | - { |
|
313 | - $tm[$i] = array(); |
|
314 | - $tr[$i] = array(); |
|
315 | - |
|
316 | - for ($j = 0; $j < 8; ++$j) |
|
317 | - { |
|
318 | - $tm[$i][$j] = $cm; |
|
319 | - $cm = parent::uInt32($cm + $mm); |
|
320 | - $tr[$i][$j] = $cr; |
|
321 | - $cr = parent::uInt32($cr + $mr); |
|
322 | - } |
|
323 | - } |
|
324 | - |
|
325 | - // now create the round and masking keys |
|
326 | - for ($i = 0; $i < 12; ++$i) |
|
327 | - { |
|
328 | - $j = 2 * $i; |
|
329 | - |
|
330 | - $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
331 | - $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
332 | - $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
333 | - $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
334 | - $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
335 | - $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
336 | - $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
337 | - $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
338 | - |
|
339 | - $j = (2 * $i) + 1; |
|
340 | - $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
341 | - $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
342 | - $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
343 | - $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
344 | - $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
345 | - $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
346 | - $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
347 | - $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
348 | - |
|
349 | - // take the least 5 significant bits of each $xkey byte below and assign it |
|
350 | - // to the round key |
|
351 | - $this->_rkey[$i][0] = $xkey[0] & 31; |
|
352 | - $this->_rkey[$i][1] = $xkey[2] & 31; |
|
353 | - $this->_rkey[$i][2] = $xkey[4] & 31; |
|
354 | - $this->_rkey[$i][3] = $xkey[6] & 31; |
|
355 | - |
|
356 | - // now create 32 byte masking keys |
|
357 | - $this->_mkey[$i][0] = $xkey[7]; |
|
358 | - $this->_mkey[$i][1] = $xkey[5]; |
|
359 | - $this->_mkey[$i][2] = $xkey[3]; |
|
360 | - $this->_mkey[$i][3] = $xkey[1]; |
|
361 | - } |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * Initialize the tables |
|
367 | - * |
|
368 | - * @return void |
|
369 | - */ |
|
370 | - private function initTables() |
|
371 | - { |
|
372 | - // 256 unsigned 32 bit integers |
|
373 | - self::$_s1 = array( |
|
374 | - 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
375 | - 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
376 | - 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
377 | - 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
378 | - 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
379 | - 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
380 | - 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
381 | - 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
382 | - 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
383 | - 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
384 | - 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
385 | - 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
386 | - 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
387 | - 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
388 | - 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
389 | - 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
390 | - 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
391 | - 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
392 | - 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
393 | - 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
394 | - 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
395 | - 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
396 | - 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
397 | - 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
398 | - 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
399 | - 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
400 | - 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
401 | - 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
402 | - 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
403 | - 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
404 | - 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
405 | - 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
406 | - 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
407 | - 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
408 | - 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
409 | - 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
410 | - 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
411 | - 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
412 | - 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
413 | - 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
414 | - 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
415 | - 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
416 | - 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
417 | - 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
418 | - 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
419 | - 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
420 | - 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
421 | - 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
422 | - 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
423 | - 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
424 | - 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
425 | - 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
426 | - 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
427 | - 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
428 | - 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
429 | - 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
430 | - 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
431 | - 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
432 | - 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
433 | - 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
434 | - 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
435 | - 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
436 | - 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
437 | - 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
438 | - ); |
|
439 | - |
|
440 | - // 256 unsigned 32 bit integers |
|
441 | - self::$_s2 = array( |
|
442 | - 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
443 | - 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
444 | - 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
445 | - 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
446 | - 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
447 | - 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
448 | - 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
449 | - 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
450 | - 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
451 | - 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
452 | - 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
453 | - 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
454 | - 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
455 | - 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
456 | - 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
457 | - 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
458 | - 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
459 | - 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
460 | - 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
461 | - 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
462 | - 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
463 | - 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
464 | - 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
465 | - 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
466 | - 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
467 | - 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
468 | - 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
469 | - 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
470 | - 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
471 | - 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
472 | - 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
473 | - 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
474 | - 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
475 | - 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
476 | - 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
477 | - 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
478 | - 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
479 | - 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
480 | - 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
481 | - 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
482 | - 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
483 | - 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
484 | - 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
485 | - 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
486 | - 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
487 | - 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
488 | - 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
489 | - 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
490 | - 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
491 | - 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
492 | - 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
493 | - 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
494 | - 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
495 | - 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
496 | - 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
497 | - 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
498 | - 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
499 | - 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
500 | - 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
501 | - 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
502 | - 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
503 | - 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
504 | - 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
505 | - 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
506 | - ); |
|
507 | - |
|
508 | - // 256 unsigned 32 bit integers |
|
509 | - self::$_s3 = array( |
|
510 | - 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
511 | - 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
512 | - 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
513 | - 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
514 | - 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
515 | - 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
516 | - 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
517 | - 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
518 | - 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
519 | - 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
520 | - 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
521 | - 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
522 | - 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
523 | - 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
524 | - 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
525 | - 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
526 | - 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
527 | - 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
528 | - 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
529 | - 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
530 | - 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
531 | - 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
532 | - 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
533 | - 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
534 | - 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
535 | - 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
536 | - 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
537 | - 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
538 | - 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
539 | - 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
540 | - 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
541 | - 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
542 | - 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
543 | - 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
544 | - 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
545 | - 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
546 | - 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
547 | - 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
548 | - 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
549 | - 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
550 | - 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
551 | - 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
552 | - 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
553 | - 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
554 | - 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
555 | - 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
556 | - 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
557 | - 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
558 | - 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
559 | - 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
560 | - 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
561 | - 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
562 | - 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
563 | - 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
564 | - 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
565 | - 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
566 | - 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
567 | - 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
568 | - 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
569 | - 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
570 | - 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
571 | - 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
572 | - 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
573 | - 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
574 | - ); |
|
575 | - |
|
576 | - // 256 unsigned 32 bit integers |
|
577 | - self::$_s4 = array( |
|
578 | - 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
579 | - 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
580 | - 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
581 | - 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
582 | - 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
583 | - 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
584 | - 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
585 | - 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
586 | - 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
587 | - 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
588 | - 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
589 | - 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
590 | - 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
591 | - 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
592 | - 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
593 | - 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
594 | - 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
595 | - 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
596 | - 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
597 | - 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
598 | - 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
599 | - 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
600 | - 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
601 | - 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
602 | - 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
603 | - 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
604 | - 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
605 | - 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
606 | - 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
607 | - 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
608 | - 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
609 | - 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
610 | - 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
611 | - 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
612 | - 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
613 | - 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
614 | - 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
615 | - 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
616 | - 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
617 | - 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
618 | - 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
619 | - 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
620 | - 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
621 | - 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
622 | - 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
623 | - 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
624 | - 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
625 | - 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
626 | - 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
627 | - 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
628 | - 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
629 | - 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
630 | - 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
631 | - 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
632 | - 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
633 | - 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
634 | - 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
635 | - 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
636 | - 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
637 | - 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
638 | - 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
639 | - 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
640 | - 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
641 | - 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
642 | - ); |
|
643 | - } |
|
644 | - |
|
645 | - |
|
646 | - /** |
|
647 | - * Indicates this is a block cipher |
|
648 | - * |
|
649 | - * @return integer Returns Cipher::BLOCK |
|
650 | - */ |
|
651 | - public function type() |
|
652 | - { |
|
653 | - return parent::BLOCK; |
|
654 | - } |
|
41 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | + const BYTES_BLOCK = 16; // 128 bits; |
|
43 | + |
|
44 | + //const BYTES_KEY = 0; // between 128, 160, 192, 224, 256 bits |
|
45 | + |
|
46 | + /** @type integer BYTES_KEY_MAX The max key size, in bytes */ |
|
47 | + const BYTES_KEY_MAX = 32; |
|
48 | + |
|
49 | + /** @type array $_s1 An array of 256 unsigned integers */ |
|
50 | + private static $_s1 = array(); |
|
51 | + |
|
52 | + /** @type array $_s2 An array of 256 unsigned integers */ |
|
53 | + private static $_s2 = array(); |
|
54 | + |
|
55 | + /** @type array $_s3 An array of 256 unsigned integers */ |
|
56 | + private static $_s3 = array(); |
|
57 | + |
|
58 | + /** @type array $_s4 An array of 256 unsigned integers */ |
|
59 | + private static $_s4 = array(); |
|
60 | + |
|
61 | + private static $_req_key_sizes = array(16, 20, 24, 28, 32); |
|
62 | + |
|
63 | + /** @type string $_mkey The 16 byte masking subkey */ |
|
64 | + private $_mkey = array(); |
|
65 | + |
|
66 | + /** @type string $_rkey The 16 byte rotate subkey */ |
|
67 | + private $_rkey = array(); |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * Constructor |
|
72 | + * |
|
73 | + * @param string $key The key used for Encryption/Decryption |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + public function __construct($key) |
|
77 | + { |
|
78 | + $keylen = strlen($key); |
|
79 | + |
|
80 | + if ($keylen > self::BYTES_KEY_MAX) |
|
81 | + { |
|
82 | + $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
83 | + $keylen = self::BYTES_KEY_MAX; |
|
84 | + } |
|
85 | + else if (!in_array($keylen, self::$_req_key_sizes)) |
|
86 | + { |
|
87 | + $msg = PHP_Crypt::CIPHER_CAST_256." requires a key size of 16, "; |
|
88 | + $msg .= "20, 24, 28, or 32 bytes."; |
|
89 | + trigger_error($msg, E_USER_WARNING); |
|
90 | + } |
|
91 | + |
|
92 | + // set the key, make sure the required length is set in bytes |
|
93 | + parent::__construct(PHP_Crypt::CIPHER_CAST_256, $key, $keylen); |
|
94 | + |
|
95 | + // set the block size |
|
96 | + $this->blockSize(self::BYTES_BLOCK); |
|
97 | + |
|
98 | + // initialize the sboxes constants |
|
99 | + $this->initTables(); |
|
100 | + |
|
101 | + // create the sub keys using the sboxes |
|
102 | + $this->createSubKeys(); |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * Destructor |
|
108 | + * |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + public function __destruct() |
|
112 | + { |
|
113 | + parent::__destruct(); |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * Encrypt plain text data |
|
119 | + * |
|
120 | + * @param string $data A 128 bit block of plain data |
|
121 | + * @return boolean Returns true |
|
122 | + */ |
|
123 | + public function encrypt(&$data) |
|
124 | + { |
|
125 | + $this->operation(parent::ENCRYPT); |
|
126 | + |
|
127 | + // first split the data into four 32 bit blocks, reverse |
|
128 | + // the string order of each block, convert the blocks of data to integers |
|
129 | + $data = str_split($data, 4); |
|
130 | + $data = array_map("strrev", $data); |
|
131 | + $data = array_map("parent::str2Dec", $data); |
|
132 | + |
|
133 | + // do the first 6 loops |
|
134 | + for ($i = 0; $i < 6; ++$i) |
|
135 | + { |
|
136 | + |
|
137 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
138 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
139 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
140 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
141 | + } |
|
142 | + |
|
143 | + // the second 6 loops are done in a different order |
|
144 | + for ($i = 6; $i < 12; ++$i) |
|
145 | + { |
|
146 | + |
|
147 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
148 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
149 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
150 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
151 | + } |
|
152 | + |
|
153 | + // convert the decimals back to a string, reverse the string so it's |
|
154 | + // in the correct order |
|
155 | + $data = array_map(function($v) { |
|
156 | + $v = Core::dec2Str($v, 4); |
|
157 | + return strrev($v); |
|
158 | + }, $data); |
|
159 | + |
|
160 | + // glue the string back together |
|
161 | + $data = implode("", $data); |
|
162 | + |
|
163 | + return true; |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * Decrypt an encrypted string, it does all the steps of encryption, |
|
169 | + * but in reverse. |
|
170 | + * |
|
171 | + * @param string $data A 128 bit block of encrypted data |
|
172 | + * @return boolean Returns true |
|
173 | + */ |
|
174 | + public function decrypt(&$data) |
|
175 | + { |
|
176 | + $this->operation(parent::DECRYPT); |
|
177 | + |
|
178 | + // first split the data into four 32 bit blocks, reverse |
|
179 | + // the string order of each block, convert the blocks of data to integers |
|
180 | + $data = str_split($data, 4); |
|
181 | + $data = array_map("strrev", $data); |
|
182 | + $data = array_map("parent::str2Dec", $data); |
|
183 | + |
|
184 | + // do the first 6 loops |
|
185 | + for ($i = 11; $i >= 6; --$i) |
|
186 | + { |
|
187 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
188 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
189 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
190 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
191 | + } |
|
192 | + |
|
193 | + // the second 6 loops are done in a different order |
|
194 | + for ($i = 5; $i >= 0; --$i) |
|
195 | + { |
|
196 | + $data[3] ^= $this->f1($data[0], $this->_mkey[$i][3], $this->_rkey[$i][3]); |
|
197 | + $data[0] ^= $this->f3($data[1], $this->_mkey[$i][2], $this->_rkey[$i][2]); |
|
198 | + $data[1] ^= $this->f2($data[2], $this->_mkey[$i][1], $this->_rkey[$i][1]); |
|
199 | + $data[2] ^= $this->f1($data[3], $this->_mkey[$i][0], $this->_rkey[$i][0]); |
|
200 | + } |
|
201 | + |
|
202 | + // convert the decimals back to a string, reverse the string so it's |
|
203 | + // in the correct order |
|
204 | + $data = array_map(function($v) { |
|
205 | + $v = Core::dec2Str($v, 4); |
|
206 | + return strrev($v); |
|
207 | + }, $data); |
|
208 | + |
|
209 | + // glue the string back together |
|
210 | + $data = implode("", $data); |
|
211 | + |
|
212 | + return true; |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * CAST-256 F1 function |
|
218 | + * |
|
219 | + * @param $d integer The the data input |
|
220 | + * @param $m integer The 32 bit masking key |
|
221 | + * @param $r integer The round number |
|
222 | + * @return integer The value after the F1 calculation |
|
223 | + */ |
|
224 | + private function f1($d, $m, $r) |
|
225 | + { |
|
226 | + $n = parent::uInt32($m + $d); |
|
227 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
228 | + $n = parent::dec2Str($n, 4); |
|
229 | + |
|
230 | + return parent::uInt32( |
|
231 | + ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
232 | + self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
233 | + ); |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * CAST-256 F2 function |
|
239 | + * |
|
240 | + * @param integer $d integer The the data input |
|
241 | + * @param $m integer The 32 bit masking key |
|
242 | + * @param $r integer The round number |
|
243 | + * @return integer The value after the F2 calculation |
|
244 | + */ |
|
245 | + private function f2($d, $m, $r) |
|
246 | + { |
|
247 | + $n = parent::uInt32($m ^ $d); |
|
248 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
249 | + $n = parent::dec2Str($n, 4); |
|
250 | + |
|
251 | + return parent::uInt32( |
|
252 | + ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
253 | + self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
254 | + ); |
|
255 | + } |
|
256 | + |
|
257 | + |
|
258 | + /** |
|
259 | + * CAST-256 F3 function |
|
260 | + * |
|
261 | + * @param integer $d integer The the data input |
|
262 | + * @param $m integer The 32 bit masking key |
|
263 | + * @param $r integer The round number |
|
264 | + * @return integer The value after the F3 calculation |
|
265 | + */ |
|
266 | + private function f3($d, $m, $r) |
|
267 | + { |
|
268 | + $n = parent::uInt32($m - $d); |
|
269 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $r)); |
|
270 | + $n = parent::dec2Str($n, 4); |
|
271 | + |
|
272 | + return parent::uInt32( |
|
273 | + ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
274 | + self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
275 | + ); |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + /** |
|
280 | + * Creates the subkeys $_mkey (the masking key) and |
|
281 | + * $_rkey (the rotate key) which are 16 bytes each. These are |
|
282 | + * created from the original key. The original key is null |
|
283 | + * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
284 | + * split in half to create $_mkey and $_rkey |
|
285 | + * |
|
286 | + * @return void |
|
287 | + */ |
|
288 | + private function createSubKeys() |
|
289 | + { |
|
290 | + $cm = 0x5A827999; |
|
291 | + $mm = 0x6ED9EBA1; |
|
292 | + $cr = 19; |
|
293 | + $mr = 17; |
|
294 | + $tm = array(); |
|
295 | + $tr = array(); |
|
296 | + $xkey = $this->key(); |
|
297 | + $tmpkey = array(); |
|
298 | + |
|
299 | + // if the key is less than 32 bytes, pad it to 32 bytes |
|
300 | + // for the key expansion |
|
301 | + if ($this->keySize() < 32) |
|
302 | + $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT); |
|
303 | + |
|
304 | + // split the key up into 4 byte parts, reverse the string, |
|
305 | + // then convert each part into a 32 bit integer |
|
306 | + $xkey = str_split($xkey, 4); |
|
307 | + $xkey = array_map("strrev", $xkey); |
|
308 | + $xkey = array_map("parent::str2Dec", $xkey); |
|
309 | + |
|
310 | + // set up the values need for creating round and masking keys |
|
311 | + for ($i = 0; $i < 24; ++$i) |
|
312 | + { |
|
313 | + $tm[$i] = array(); |
|
314 | + $tr[$i] = array(); |
|
315 | + |
|
316 | + for ($j = 0; $j < 8; ++$j) |
|
317 | + { |
|
318 | + $tm[$i][$j] = $cm; |
|
319 | + $cm = parent::uInt32($cm + $mm); |
|
320 | + $tr[$i][$j] = $cr; |
|
321 | + $cr = parent::uInt32($cr + $mr); |
|
322 | + } |
|
323 | + } |
|
324 | + |
|
325 | + // now create the round and masking keys |
|
326 | + for ($i = 0; $i < 12; ++$i) |
|
327 | + { |
|
328 | + $j = 2 * $i; |
|
329 | + |
|
330 | + $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
331 | + $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
332 | + $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
333 | + $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
334 | + $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
335 | + $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
336 | + $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
337 | + $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
338 | + |
|
339 | + $j = (2 * $i) + 1; |
|
340 | + $xkey[6] = parent::uInt32($xkey[6] ^ $this->f1($xkey[7], $tm[$j][0], $tr[$j][0])); |
|
341 | + $xkey[5] = parent::uInt32($xkey[5] ^ $this->f2($xkey[6], $tm[$j][1], $tr[$j][1])); |
|
342 | + $xkey[4] = parent::uInt32($xkey[4] ^ $this->f3($xkey[5], $tm[$j][2], $tr[$j][2])); |
|
343 | + $xkey[3] = parent::uInt32($xkey[3] ^ $this->f1($xkey[4], $tm[$j][3], $tr[$j][3])); |
|
344 | + $xkey[2] = parent::uInt32($xkey[2] ^ $this->f2($xkey[3], $tm[$j][4], $tr[$j][4])); |
|
345 | + $xkey[1] = parent::uInt32($xkey[1] ^ $this->f3($xkey[2], $tm[$j][5], $tr[$j][5])); |
|
346 | + $xkey[0] = parent::uInt32($xkey[0] ^ $this->f1($xkey[1], $tm[$j][6], $tr[$j][6])); |
|
347 | + $xkey[7] = parent::uInt32($xkey[7] ^ $this->f2($xkey[0], $tm[$j][7], $tr[$j][7])); |
|
348 | + |
|
349 | + // take the least 5 significant bits of each $xkey byte below and assign it |
|
350 | + // to the round key |
|
351 | + $this->_rkey[$i][0] = $xkey[0] & 31; |
|
352 | + $this->_rkey[$i][1] = $xkey[2] & 31; |
|
353 | + $this->_rkey[$i][2] = $xkey[4] & 31; |
|
354 | + $this->_rkey[$i][3] = $xkey[6] & 31; |
|
355 | + |
|
356 | + // now create 32 byte masking keys |
|
357 | + $this->_mkey[$i][0] = $xkey[7]; |
|
358 | + $this->_mkey[$i][1] = $xkey[5]; |
|
359 | + $this->_mkey[$i][2] = $xkey[3]; |
|
360 | + $this->_mkey[$i][3] = $xkey[1]; |
|
361 | + } |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * Initialize the tables |
|
367 | + * |
|
368 | + * @return void |
|
369 | + */ |
|
370 | + private function initTables() |
|
371 | + { |
|
372 | + // 256 unsigned 32 bit integers |
|
373 | + self::$_s1 = array( |
|
374 | + 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
375 | + 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
376 | + 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
377 | + 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
378 | + 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
379 | + 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
380 | + 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
381 | + 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
382 | + 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
383 | + 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
384 | + 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
385 | + 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
386 | + 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
387 | + 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
388 | + 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
389 | + 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
390 | + 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
391 | + 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
392 | + 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
393 | + 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
394 | + 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
395 | + 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
396 | + 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
397 | + 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
398 | + 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
399 | + 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
400 | + 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
401 | + 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
402 | + 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
403 | + 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
404 | + 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
405 | + 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
406 | + 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
407 | + 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
408 | + 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
409 | + 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
410 | + 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
411 | + 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
412 | + 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
413 | + 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
414 | + 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
415 | + 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
416 | + 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
417 | + 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
418 | + 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
419 | + 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
420 | + 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
421 | + 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
422 | + 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
423 | + 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
424 | + 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
425 | + 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
426 | + 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
427 | + 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
428 | + 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
429 | + 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
430 | + 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
431 | + 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
432 | + 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
433 | + 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
434 | + 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
435 | + 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
436 | + 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
437 | + 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
438 | + ); |
|
439 | + |
|
440 | + // 256 unsigned 32 bit integers |
|
441 | + self::$_s2 = array( |
|
442 | + 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
443 | + 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
444 | + 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
445 | + 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
446 | + 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
447 | + 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
448 | + 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
449 | + 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
450 | + 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
451 | + 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
452 | + 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
453 | + 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
454 | + 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
455 | + 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
456 | + 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
457 | + 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
458 | + 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
459 | + 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
460 | + 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
461 | + 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
462 | + 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
463 | + 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
464 | + 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
465 | + 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
466 | + 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
467 | + 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
468 | + 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
469 | + 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
470 | + 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
471 | + 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
472 | + 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
473 | + 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
474 | + 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
475 | + 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
476 | + 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
477 | + 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
478 | + 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
479 | + 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
480 | + 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
481 | + 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
482 | + 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
483 | + 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
484 | + 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
485 | + 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
486 | + 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
487 | + 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
488 | + 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
489 | + 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
490 | + 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
491 | + 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
492 | + 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
493 | + 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
494 | + 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
495 | + 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
496 | + 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
497 | + 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
498 | + 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
499 | + 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
500 | + 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
501 | + 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
502 | + 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
503 | + 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
504 | + 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
505 | + 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
506 | + ); |
|
507 | + |
|
508 | + // 256 unsigned 32 bit integers |
|
509 | + self::$_s3 = array( |
|
510 | + 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
511 | + 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
512 | + 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
513 | + 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
514 | + 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
515 | + 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
516 | + 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
517 | + 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
518 | + 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
519 | + 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
520 | + 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
521 | + 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
522 | + 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
523 | + 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
524 | + 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
525 | + 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
526 | + 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
527 | + 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
528 | + 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
529 | + 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
530 | + 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
531 | + 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
532 | + 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
533 | + 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
534 | + 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
535 | + 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
536 | + 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
537 | + 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
538 | + 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
539 | + 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
540 | + 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
541 | + 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
542 | + 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
543 | + 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
544 | + 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
545 | + 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
546 | + 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
547 | + 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
548 | + 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
549 | + 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
550 | + 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
551 | + 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
552 | + 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
553 | + 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
554 | + 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
555 | + 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
556 | + 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
557 | + 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
558 | + 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
559 | + 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
560 | + 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
561 | + 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
562 | + 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
563 | + 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
564 | + 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
565 | + 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
566 | + 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
567 | + 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
568 | + 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
569 | + 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
570 | + 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
571 | + 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
572 | + 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
573 | + 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
574 | + ); |
|
575 | + |
|
576 | + // 256 unsigned 32 bit integers |
|
577 | + self::$_s4 = array( |
|
578 | + 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
579 | + 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
580 | + 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
581 | + 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
582 | + 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
583 | + 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
584 | + 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
585 | + 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
586 | + 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
587 | + 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
588 | + 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
589 | + 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
590 | + 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
591 | + 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
592 | + 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
593 | + 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
594 | + 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
595 | + 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
596 | + 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
597 | + 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
598 | + 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
599 | + 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
600 | + 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
601 | + 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
602 | + 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
603 | + 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
604 | + 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
605 | + 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
606 | + 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
607 | + 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
608 | + 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
609 | + 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
610 | + 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
611 | + 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
612 | + 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
613 | + 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
614 | + 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
615 | + 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
616 | + 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
617 | + 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
618 | + 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
619 | + 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
620 | + 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
621 | + 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
622 | + 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
623 | + 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
624 | + 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
625 | + 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
626 | + 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
627 | + 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
628 | + 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
629 | + 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
630 | + 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
631 | + 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
632 | + 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
633 | + 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
634 | + 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
635 | + 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
636 | + 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
637 | + 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
638 | + 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
639 | + 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
640 | + 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
641 | + 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
642 | + ); |
|
643 | + } |
|
644 | + |
|
645 | + |
|
646 | + /** |
|
647 | + * Indicates this is a block cipher |
|
648 | + * |
|
649 | + * @return integer Returns Cipher::BLOCK |
|
650 | + */ |
|
651 | + public function type() |
|
652 | + { |
|
653 | + return parent::BLOCK; |
|
654 | + } |
|
655 | 655 | } |
656 | 656 | ?> |
@@ -81,8 +81,7 @@ discard block |
||
81 | 81 | { |
82 | 82 | $key = substr($key, 0, self::BYTES_KEY_MAX); |
83 | 83 | $keylen = self::BYTES_KEY_MAX; |
84 | - } |
|
85 | - else if (!in_array($keylen, self::$_req_key_sizes)) |
|
84 | + } else if (!in_array($keylen, self::$_req_key_sizes)) |
|
86 | 85 | { |
87 | 86 | $msg = PHP_Crypt::CIPHER_CAST_256." requires a key size of 16, "; |
88 | 87 | $msg .= "20, 24, 28, or 32 bytes."; |
@@ -298,8 +297,9 @@ discard block |
||
298 | 297 | |
299 | 298 | // if the key is less than 32 bytes, pad it to 32 bytes |
300 | 299 | // for the key expansion |
301 | - if ($this->keySize() < 32) |
|
302 | - $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT); |
|
300 | + if ($this->keySize() < 32) { |
|
301 | + $xkey = str_pad($xkey, 32, "\0", STR_PAD_RIGHT); |
|
302 | + } |
|
303 | 303 | |
304 | 304 | // split the key up into 4 byte parts, reverse the string, |
305 | 305 | // then convert each part into a 32 bit integer |
@@ -239,102 +239,102 @@ |
||
239 | 239 | |
240 | 240 | $d = $tmp; |
241 | 241 | */ |
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * Applies several of 3Way's functions used for encryption and decryption |
|
247 | - * NOTE: Please read the comments in the $this->gamma() function. This |
|
248 | - * function calls the $this->gamma() function which does not do anything. |
|
249 | - * |
|
250 | - * @param integer[] $d A 3 element 32 bit integer array |
|
251 | - * @return void |
|
252 | - */ |
|
253 | - private function rho(&$d) |
|
254 | - { |
|
255 | - $this->theta($d); |
|
256 | - $this->pi1($d); |
|
257 | - $this->gamma($d); |
|
258 | - $this->pi2($d); |
|
259 | - } |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * 3Way's PI_1 function |
|
264 | - * This was taken from mcrypt's 3-way pi_1() function |
|
265 | - * |
|
266 | - * @param array $d A 3 element 32 bit integer array |
|
267 | - * @return void |
|
268 | - */ |
|
269 | - private function pi1(&$d) |
|
270 | - { |
|
271 | - $d[0] = parent::uInt32(($d[0] >> 10) ^ ($d[0] << 22)); |
|
272 | - $d[2] = parent::uInt32(($d[2] << 1) ^ ($d[2] >> 31)); |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * 3Way's PI_2 function |
|
278 | - * This was taken from mcrypt's 3-way pi_2() function |
|
279 | - * |
|
280 | - * @param array $d A 3 element 32 bit integer array |
|
281 | - * @return void |
|
282 | - */ |
|
283 | - private function pi2(&$d) |
|
284 | - { |
|
285 | - $d[0] = parent::uInt32(($d[0] << 1) ^ ($d[0] >> 31)); |
|
286 | - $d[2] = parent::uInt32(($d[2] >> 10) ^ ($d[2] << 22)); |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * Reverse the bits of each element of array $d, and |
|
292 | - * reverses the order of array $d, used only during |
|
293 | - * decryption |
|
294 | - * |
|
295 | - * @param array $d A 3 element array of a 32 bit integers |
|
296 | - * @return void |
|
297 | - */ |
|
298 | - private function invertBits(&$d) |
|
299 | - { |
|
300 | - $d = array_map("parent::dec2Bin", $d); |
|
301 | - $d = array_map("strrev", $d); |
|
302 | - $d = array_map("parent::bin2Dec", $d); |
|
303 | - $d = array_reverse($d); |
|
304 | - } |
|
305 | - |
|
306 | - |
|
307 | - /** |
|
308 | - * Initialize the tables used in 3Way Encryption. |
|
309 | - * |
|
310 | - * @return void |
|
311 | - */ |
|
312 | - private function initTables() |
|
313 | - { |
|
314 | - // round constants for encryption |
|
315 | - self::$_rcon_enc = array( |
|
316 | - 0x0B0B, 0x1616, 0x2C2C, 0x5858, |
|
317 | - 0xB0B0, 0x7171, 0xE2E2, 0xD5D5, |
|
318 | - 0xBBBB, 0x6767, 0xCECE, 0x8D8D |
|
319 | - ); |
|
320 | - |
|
321 | - // round constants for decryption |
|
322 | - self::$_rcon_dec = array( |
|
323 | - 0xB1B1, 0x7373, 0xE6E6, 0xDDDD, |
|
324 | - 0xABAB, 0x4747, 0x8E8E, 0x0D0D, |
|
325 | - 0x1A1A, 0x3434, 0x6868, 0xD0D0 |
|
326 | - ); |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * Indicates this is a block cipher |
|
332 | - * |
|
333 | - * @return integer Returns Cipher::BLOCK |
|
334 | - */ |
|
335 | - public function type() |
|
336 | - { |
|
337 | - return parent::BLOCK; |
|
338 | - } |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * Applies several of 3Way's functions used for encryption and decryption |
|
247 | + * NOTE: Please read the comments in the $this->gamma() function. This |
|
248 | + * function calls the $this->gamma() function which does not do anything. |
|
249 | + * |
|
250 | + * @param integer[] $d A 3 element 32 bit integer array |
|
251 | + * @return void |
|
252 | + */ |
|
253 | + private function rho(&$d) |
|
254 | + { |
|
255 | + $this->theta($d); |
|
256 | + $this->pi1($d); |
|
257 | + $this->gamma($d); |
|
258 | + $this->pi2($d); |
|
259 | + } |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * 3Way's PI_1 function |
|
264 | + * This was taken from mcrypt's 3-way pi_1() function |
|
265 | + * |
|
266 | + * @param array $d A 3 element 32 bit integer array |
|
267 | + * @return void |
|
268 | + */ |
|
269 | + private function pi1(&$d) |
|
270 | + { |
|
271 | + $d[0] = parent::uInt32(($d[0] >> 10) ^ ($d[0] << 22)); |
|
272 | + $d[2] = parent::uInt32(($d[2] << 1) ^ ($d[2] >> 31)); |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * 3Way's PI_2 function |
|
278 | + * This was taken from mcrypt's 3-way pi_2() function |
|
279 | + * |
|
280 | + * @param array $d A 3 element 32 bit integer array |
|
281 | + * @return void |
|
282 | + */ |
|
283 | + private function pi2(&$d) |
|
284 | + { |
|
285 | + $d[0] = parent::uInt32(($d[0] << 1) ^ ($d[0] >> 31)); |
|
286 | + $d[2] = parent::uInt32(($d[2] >> 10) ^ ($d[2] << 22)); |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * Reverse the bits of each element of array $d, and |
|
292 | + * reverses the order of array $d, used only during |
|
293 | + * decryption |
|
294 | + * |
|
295 | + * @param array $d A 3 element array of a 32 bit integers |
|
296 | + * @return void |
|
297 | + */ |
|
298 | + private function invertBits(&$d) |
|
299 | + { |
|
300 | + $d = array_map("parent::dec2Bin", $d); |
|
301 | + $d = array_map("strrev", $d); |
|
302 | + $d = array_map("parent::bin2Dec", $d); |
|
303 | + $d = array_reverse($d); |
|
304 | + } |
|
305 | + |
|
306 | + |
|
307 | + /** |
|
308 | + * Initialize the tables used in 3Way Encryption. |
|
309 | + * |
|
310 | + * @return void |
|
311 | + */ |
|
312 | + private function initTables() |
|
313 | + { |
|
314 | + // round constants for encryption |
|
315 | + self::$_rcon_enc = array( |
|
316 | + 0x0B0B, 0x1616, 0x2C2C, 0x5858, |
|
317 | + 0xB0B0, 0x7171, 0xE2E2, 0xD5D5, |
|
318 | + 0xBBBB, 0x6767, 0xCECE, 0x8D8D |
|
319 | + ); |
|
320 | + |
|
321 | + // round constants for decryption |
|
322 | + self::$_rcon_dec = array( |
|
323 | + 0xB1B1, 0x7373, 0xE6E6, 0xDDDD, |
|
324 | + 0xABAB, 0x4747, 0x8E8E, 0x0D0D, |
|
325 | + 0x1A1A, 0x3434, 0x6868, 0xD0D0 |
|
326 | + ); |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * Indicates this is a block cipher |
|
332 | + * |
|
333 | + * @return integer Returns Cipher::BLOCK |
|
334 | + */ |
|
335 | + public function type() |
|
336 | + { |
|
337 | + return parent::BLOCK; |
|
338 | + } |
|
339 | 339 | } |
340 | 340 | ?> |
@@ -140,12 +140,12 @@ discard block |
||
140 | 140 | $key = array_map("parent::str2Dec", $key); |
141 | 141 | |
142 | 142 | // determine which round constant to use |
143 | - if($this->operation() == parent::ENCRYPT) |
|
143 | + if ($this->operation() == parent::ENCRYPT) |
|
144 | 144 | $rcon = self::$_rcon_enc; |
145 | 145 | else |
146 | 146 | $rcon = self::$_rcon_dec; |
147 | 147 | |
148 | - if($this->operation() == parent::DECRYPT) |
|
148 | + if ($this->operation() == parent::DECRYPT) |
|
149 | 149 | { |
150 | 150 | $this->theta($key); |
151 | 151 | $this->invertBits($key); |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | } |
154 | 154 | |
155 | 155 | // 3Way uses 11 rounds |
156 | - for($i = 0; $i < self::ROUNDS; ++$i) |
|
156 | + for ($i = 0; $i < self::ROUNDS; ++$i) |
|
157 | 157 | { |
158 | 158 | $data[0] = parent::uInt32($data[0] ^ $key[0] ^ ($rcon[$i] << 16)); |
159 | 159 | $data[1] = parent::uInt32($data[1] ^ $key[1]); |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | |
169 | 169 | $this->theta($data); |
170 | 170 | |
171 | - if($this->operation() == parent::DECRYPT) |
|
171 | + if ($this->operation() == parent::DECRYPT) |
|
172 | 172 | $this->invertBits($data); |
173 | 173 | |
174 | 174 | // assemble the three 32 bit parts back to a 96 bit string |
@@ -140,10 +140,11 @@ discard block |
||
140 | 140 | $key = array_map("parent::str2Dec", $key); |
141 | 141 | |
142 | 142 | // determine which round constant to use |
143 | - if($this->operation() == parent::ENCRYPT) |
|
144 | - $rcon = self::$_rcon_enc; |
|
145 | - else |
|
146 | - $rcon = self::$_rcon_dec; |
|
143 | + if($this->operation() == parent::ENCRYPT) { |
|
144 | + $rcon = self::$_rcon_enc; |
|
145 | + } else { |
|
146 | + $rcon = self::$_rcon_dec; |
|
147 | + } |
|
147 | 148 | |
148 | 149 | if($this->operation() == parent::DECRYPT) |
149 | 150 | { |
@@ -168,8 +169,9 @@ discard block |
||
168 | 169 | |
169 | 170 | $this->theta($data); |
170 | 171 | |
171 | - if($this->operation() == parent::DECRYPT) |
|
172 | - $this->invertBits($data); |
|
172 | + if($this->operation() == parent::DECRYPT) { |
|
173 | + $this->invertBits($data); |
|
174 | + } |
|
173 | 175 | |
174 | 176 | // assemble the three 32 bit parts back to a 96 bit string |
175 | 177 | $data = parent::dec2Str($data[0], 4).parent::dec2Str($data[1], 4). |