@@ -39,199 +39,199 @@ |
||
39 | 39 | */ |
40 | 40 | class Cipher_Skipjack extends Cipher |
41 | 41 | { |
42 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
43 | - const BYTES_BLOCK = 8; // 64 bits |
|
42 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
43 | + const BYTES_BLOCK = 8; // 64 bits |
|
44 | 44 | |
45 | - /** @type integer BYTES_KEY The size of the key, in bytes */ |
|
46 | - const BYTES_KEY = 10; // 80 bits |
|
45 | + /** @type integer BYTES_KEY The size of the key, in bytes */ |
|
46 | + const BYTES_KEY = 10; // 80 bits |
|
47 | 47 | |
48 | - /** @type string $expanded_key The expanded key */ |
|
49 | - private $expanded_key = ""; |
|
48 | + /** @type string $expanded_key The expanded key */ |
|
49 | + private $expanded_key = ""; |
|
50 | 50 | |
51 | - /** @type array $_f The Skipjack F-Table, this is a constant */ |
|
52 | - private static $_f = array(); |
|
51 | + /** @type array $_f The Skipjack F-Table, this is a constant */ |
|
52 | + private static $_f = array(); |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Constructor |
|
57 | - * |
|
58 | - * @param string $key The key used for Encryption/Decryption |
|
59 | - * @return void |
|
60 | - */ |
|
61 | - public function __construct($key) |
|
62 | - { |
|
63 | - // set the Skipjack key |
|
64 | - parent::__construct(PHP_Crypt::CIPHER_SKIPJACK, $key, self::BYTES_KEY); |
|
65 | - |
|
66 | - // initialize variables |
|
67 | - $this->initTables(); |
|
68 | - |
|
69 | - // set the block size used |
|
70 | - $this->blockSize(self::BYTES_BLOCK); |
|
71 | - |
|
72 | - // expand the key from 10 bytes to 128 bytes |
|
73 | - $this->expandKey(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Destructor |
|
79 | - * |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - public function __destruct() |
|
83 | - { |
|
84 | - parent::__destruct(); |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * Encrypt plain text data using Skipjack |
|
90 | - * |
|
91 | - * @return boolean Returns true |
|
92 | - */ |
|
93 | - public function encrypt(&$text) |
|
94 | - { |
|
95 | - $this->operation(parent::ENCRYPT); |
|
96 | - |
|
97 | - for ($i = 1; $i <= 32; ++$i) |
|
98 | - { |
|
99 | - $pos = (4 * $i) - 4; |
|
100 | - $subkey = substr($this->expanded_key, $pos, 4); |
|
101 | - |
|
102 | - if ($i >= 1 && $i <= 8) |
|
103 | - $this->ruleA($text, $subkey, $i); |
|
104 | - |
|
105 | - if ($i >= 9 && $i <= 16) |
|
106 | - $this->ruleB($text, $subkey, $i); |
|
107 | - |
|
108 | - if ($i >= 17 && $i <= 24) |
|
109 | - $this->ruleA($text, $subkey, $i); |
|
110 | - |
|
111 | - if ($i >= 25 && $i <= 32) |
|
112 | - $this->ruleB($text, $subkey, $i); |
|
113 | - } |
|
114 | - |
|
115 | - return true; |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * Decrypt a Skipjack encrypted string |
|
121 | - * |
|
122 | - * @return boolean Returns true |
|
123 | - */ |
|
124 | - public function decrypt(&$text) |
|
125 | - { |
|
126 | - $this->operation(parent::DECRYPT); |
|
127 | - |
|
128 | - for ($i = 32; $i >= 1; --$i) |
|
129 | - { |
|
130 | - $pos = ($i - 1) * 4; |
|
131 | - $subkey = substr($this->expanded_key, $pos, 4); |
|
132 | - |
|
133 | - if ($i <= 32 && $i >= 25) |
|
134 | - $this->ruleB($text, $subkey, $i); |
|
135 | - |
|
136 | - if ($i <= 24 && $i >= 17) |
|
137 | - $this->ruleA($text, $subkey, $i); |
|
138 | - |
|
139 | - if ($i <= 16 && $i >= 9) |
|
140 | - $this->ruleB($text, $subkey, $i); |
|
141 | - |
|
142 | - if ($i <= 8 && $i >= 1) |
|
143 | - $this->ruleA($text, $subkey, $i); |
|
144 | - } |
|
145 | - |
|
146 | - return true; |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * For the G Permutations, the input data is 2 Bytes The first byte is |
|
152 | - * the left side and the second is the right side.The round key is 4 bytes |
|
153 | - * long (Indices 8*i-8 to 8*i), which is split as 4 pieces: K0, K1, K2, K3 |
|
154 | - * |
|
155 | - * @param string $bytes A 2 byte string |
|
156 | - * @param string $key 4 bytes of $this->expanded_key |
|
157 | - * @return string A 2 byte string, the G Permutation of $bytes |
|
158 | - */ |
|
159 | - private function gPermutation($bytes, $key) |
|
160 | - { |
|
161 | - $left = ord($bytes[0]); |
|
162 | - $right = ord($bytes[1]); |
|
163 | - |
|
164 | - if ($this->operation() == parent::ENCRYPT) |
|
165 | - { |
|
166 | - for ($i = 0; $i < 4; ++$i) |
|
167 | - { |
|
168 | - if ($i == 0 || $i == 2) |
|
169 | - { |
|
170 | - $pos = $right ^ $this->str2Dec($key[$i]); |
|
171 | - $left = $left ^ self::$_f[$pos]; |
|
172 | - } else |
|
173 | - { |
|
174 | - $pos = $left ^ $this->str2Dec($key[$i]); |
|
175 | - $right = $right ^ self::$_f[$pos]; |
|
176 | - } |
|
177 | - } |
|
178 | - } else // parent::DECRYPT |
|
179 | - { |
|
180 | - // we do the same as in encryption, but apply the key backwards, |
|
181 | - // from key[3] to key[0] |
|
182 | - for ($i = 3; $i >= 0; --$i) |
|
183 | - { |
|
184 | - if ($i == 0 || $i == 2) |
|
185 | - { |
|
186 | - $pos = $right ^ $this->str2Dec($key[$i]); |
|
187 | - $left = $left ^ self::$_f[$pos]; |
|
188 | - } else |
|
189 | - { |
|
190 | - $pos = $left ^ $this->str2Dec($key[$i]); |
|
191 | - $right = $right ^ self::$_f[$pos]; |
|
192 | - } |
|
193 | - } |
|
194 | - } |
|
195 | - |
|
196 | - return $this->dec2Str($left).$this->dec2Str($right); |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - /** |
|
201 | - * Perform SkipJacks RuleA function. Split the data into 4 parts, |
|
202 | - * 2 bytes each: W0, W1, W2, W3. |
|
203 | - * |
|
204 | - * @param string $bytes An 8 byte string |
|
205 | - * @param string $key 4 bytes of $this->expanded_key |
|
206 | - * @param integer $i The round number |
|
207 | - * @return void |
|
208 | - */ |
|
209 | - private function ruleA(&$bytes, $key, $i) |
|
210 | - { |
|
211 | - $w = str_split($bytes, 2); |
|
212 | - |
|
213 | - if ($this->operation() == parent::ENCRYPT) |
|
214 | - { |
|
215 | - /* |
|
55 | + /** |
|
56 | + * Constructor |
|
57 | + * |
|
58 | + * @param string $key The key used for Encryption/Decryption |
|
59 | + * @return void |
|
60 | + */ |
|
61 | + public function __construct($key) |
|
62 | + { |
|
63 | + // set the Skipjack key |
|
64 | + parent::__construct(PHP_Crypt::CIPHER_SKIPJACK, $key, self::BYTES_KEY); |
|
65 | + |
|
66 | + // initialize variables |
|
67 | + $this->initTables(); |
|
68 | + |
|
69 | + // set the block size used |
|
70 | + $this->blockSize(self::BYTES_BLOCK); |
|
71 | + |
|
72 | + // expand the key from 10 bytes to 128 bytes |
|
73 | + $this->expandKey(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Destructor |
|
79 | + * |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + public function __destruct() |
|
83 | + { |
|
84 | + parent::__destruct(); |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * Encrypt plain text data using Skipjack |
|
90 | + * |
|
91 | + * @return boolean Returns true |
|
92 | + */ |
|
93 | + public function encrypt(&$text) |
|
94 | + { |
|
95 | + $this->operation(parent::ENCRYPT); |
|
96 | + |
|
97 | + for ($i = 1; $i <= 32; ++$i) |
|
98 | + { |
|
99 | + $pos = (4 * $i) - 4; |
|
100 | + $subkey = substr($this->expanded_key, $pos, 4); |
|
101 | + |
|
102 | + if ($i >= 1 && $i <= 8) |
|
103 | + $this->ruleA($text, $subkey, $i); |
|
104 | + |
|
105 | + if ($i >= 9 && $i <= 16) |
|
106 | + $this->ruleB($text, $subkey, $i); |
|
107 | + |
|
108 | + if ($i >= 17 && $i <= 24) |
|
109 | + $this->ruleA($text, $subkey, $i); |
|
110 | + |
|
111 | + if ($i >= 25 && $i <= 32) |
|
112 | + $this->ruleB($text, $subkey, $i); |
|
113 | + } |
|
114 | + |
|
115 | + return true; |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * Decrypt a Skipjack encrypted string |
|
121 | + * |
|
122 | + * @return boolean Returns true |
|
123 | + */ |
|
124 | + public function decrypt(&$text) |
|
125 | + { |
|
126 | + $this->operation(parent::DECRYPT); |
|
127 | + |
|
128 | + for ($i = 32; $i >= 1; --$i) |
|
129 | + { |
|
130 | + $pos = ($i - 1) * 4; |
|
131 | + $subkey = substr($this->expanded_key, $pos, 4); |
|
132 | + |
|
133 | + if ($i <= 32 && $i >= 25) |
|
134 | + $this->ruleB($text, $subkey, $i); |
|
135 | + |
|
136 | + if ($i <= 24 && $i >= 17) |
|
137 | + $this->ruleA($text, $subkey, $i); |
|
138 | + |
|
139 | + if ($i <= 16 && $i >= 9) |
|
140 | + $this->ruleB($text, $subkey, $i); |
|
141 | + |
|
142 | + if ($i <= 8 && $i >= 1) |
|
143 | + $this->ruleA($text, $subkey, $i); |
|
144 | + } |
|
145 | + |
|
146 | + return true; |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * For the G Permutations, the input data is 2 Bytes The first byte is |
|
152 | + * the left side and the second is the right side.The round key is 4 bytes |
|
153 | + * long (Indices 8*i-8 to 8*i), which is split as 4 pieces: K0, K1, K2, K3 |
|
154 | + * |
|
155 | + * @param string $bytes A 2 byte string |
|
156 | + * @param string $key 4 bytes of $this->expanded_key |
|
157 | + * @return string A 2 byte string, the G Permutation of $bytes |
|
158 | + */ |
|
159 | + private function gPermutation($bytes, $key) |
|
160 | + { |
|
161 | + $left = ord($bytes[0]); |
|
162 | + $right = ord($bytes[1]); |
|
163 | + |
|
164 | + if ($this->operation() == parent::ENCRYPT) |
|
165 | + { |
|
166 | + for ($i = 0; $i < 4; ++$i) |
|
167 | + { |
|
168 | + if ($i == 0 || $i == 2) |
|
169 | + { |
|
170 | + $pos = $right ^ $this->str2Dec($key[$i]); |
|
171 | + $left = $left ^ self::$_f[$pos]; |
|
172 | + } else |
|
173 | + { |
|
174 | + $pos = $left ^ $this->str2Dec($key[$i]); |
|
175 | + $right = $right ^ self::$_f[$pos]; |
|
176 | + } |
|
177 | + } |
|
178 | + } else // parent::DECRYPT |
|
179 | + { |
|
180 | + // we do the same as in encryption, but apply the key backwards, |
|
181 | + // from key[3] to key[0] |
|
182 | + for ($i = 3; $i >= 0; --$i) |
|
183 | + { |
|
184 | + if ($i == 0 || $i == 2) |
|
185 | + { |
|
186 | + $pos = $right ^ $this->str2Dec($key[$i]); |
|
187 | + $left = $left ^ self::$_f[$pos]; |
|
188 | + } else |
|
189 | + { |
|
190 | + $pos = $left ^ $this->str2Dec($key[$i]); |
|
191 | + $right = $right ^ self::$_f[$pos]; |
|
192 | + } |
|
193 | + } |
|
194 | + } |
|
195 | + |
|
196 | + return $this->dec2Str($left).$this->dec2Str($right); |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + /** |
|
201 | + * Perform SkipJacks RuleA function. Split the data into 4 parts, |
|
202 | + * 2 bytes each: W0, W1, W2, W3. |
|
203 | + * |
|
204 | + * @param string $bytes An 8 byte string |
|
205 | + * @param string $key 4 bytes of $this->expanded_key |
|
206 | + * @param integer $i The round number |
|
207 | + * @return void |
|
208 | + */ |
|
209 | + private function ruleA(&$bytes, $key, $i) |
|
210 | + { |
|
211 | + $w = str_split($bytes, 2); |
|
212 | + |
|
213 | + if ($this->operation() == parent::ENCRYPT) |
|
214 | + { |
|
215 | + /* |
|
216 | 216 | * Set the W3 as the old W2 |
217 | 217 | * Set the W2 as the old W1 |
218 | 218 | * Set the W1 as the G(W0) |
219 | 219 | * Set the W0 as the W1 xor W4 xor i |
220 | 220 | */ |
221 | 221 | |
222 | - $w[4] = $w[3]; |
|
223 | - $w[3] = $w[2]; |
|
224 | - $w[2] = $w[1]; |
|
225 | - $w[1] = $this->gPermutation($w[0], $key); |
|
226 | - |
|
227 | - $hex1 = $this->str2Hex($w[1]); |
|
228 | - $hex4 = $this->str2Hex($w[4]); |
|
229 | - $hexi = $this->dec2Hex($i); |
|
230 | - $w[0] = $this->xorHex($hex1, $hex4, $hexi); |
|
231 | - $w[0] = $this->hex2Str($w[0]); |
|
232 | - } else // parent::DECRYPT |
|
233 | - { |
|
234 | - /* |
|
222 | + $w[4] = $w[3]; |
|
223 | + $w[3] = $w[2]; |
|
224 | + $w[2] = $w[1]; |
|
225 | + $w[1] = $this->gPermutation($w[0], $key); |
|
226 | + |
|
227 | + $hex1 = $this->str2Hex($w[1]); |
|
228 | + $hex4 = $this->str2Hex($w[4]); |
|
229 | + $hexi = $this->dec2Hex($i); |
|
230 | + $w[0] = $this->xorHex($hex1, $hex4, $hexi); |
|
231 | + $w[0] = $this->hex2Str($w[0]); |
|
232 | + } else // parent::DECRYPT |
|
233 | + { |
|
234 | + /* |
|
235 | 235 | * Set W4 as W0 xor W1 xor i |
236 | 236 | * Set W0 as Inverse G(W1) |
237 | 237 | * Set W1 as the old W2 |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | { |
270 | 270 | $w = str_split($bytes, 2); |
271 | 271 | |
272 | - if($this->operation() == parent::ENCRYPT) |
|
272 | + if ($this->operation() == parent::ENCRYPT) |
|
273 | 273 | { |
274 | 274 | /* |
275 | 275 | * Set the new W3 as the old W2 |
@@ -333,9 +333,9 @@ discard block |
||
333 | 333 | $key = $this->key(); |
334 | 334 | $pos = 0; |
335 | 335 | |
336 | - for($i = 0; $i < 128; ++$i) |
|
336 | + for ($i = 0; $i < 128; ++$i) |
|
337 | 337 | { |
338 | - if($pos == $key_bytes) |
|
338 | + if ($pos == $key_bytes) |
|
339 | 339 | $pos = 0; |
340 | 340 | |
341 | 341 | $this->expanded_key .= $key[$pos]; |
@@ -99,17 +99,21 @@ discard block |
||
99 | 99 | $pos = (4 * $i) - 4; |
100 | 100 | $subkey = substr($this->expanded_key, $pos, 4); |
101 | 101 | |
102 | - if ($i >= 1 && $i <= 8) |
|
103 | - $this->ruleA($text, $subkey, $i); |
|
102 | + if ($i >= 1 && $i <= 8) { |
|
103 | + $this->ruleA($text, $subkey, $i); |
|
104 | + } |
|
104 | 105 | |
105 | - if ($i >= 9 && $i <= 16) |
|
106 | - $this->ruleB($text, $subkey, $i); |
|
106 | + if ($i >= 9 && $i <= 16) { |
|
107 | + $this->ruleB($text, $subkey, $i); |
|
108 | + } |
|
107 | 109 | |
108 | - if ($i >= 17 && $i <= 24) |
|
109 | - $this->ruleA($text, $subkey, $i); |
|
110 | + if ($i >= 17 && $i <= 24) { |
|
111 | + $this->ruleA($text, $subkey, $i); |
|
112 | + } |
|
110 | 113 | |
111 | - if ($i >= 25 && $i <= 32) |
|
112 | - $this->ruleB($text, $subkey, $i); |
|
114 | + if ($i >= 25 && $i <= 32) { |
|
115 | + $this->ruleB($text, $subkey, $i); |
|
116 | + } |
|
113 | 117 | } |
114 | 118 | |
115 | 119 | return true; |
@@ -130,17 +134,21 @@ discard block |
||
130 | 134 | $pos = ($i - 1) * 4; |
131 | 135 | $subkey = substr($this->expanded_key, $pos, 4); |
132 | 136 | |
133 | - if ($i <= 32 && $i >= 25) |
|
134 | - $this->ruleB($text, $subkey, $i); |
|
137 | + if ($i <= 32 && $i >= 25) { |
|
138 | + $this->ruleB($text, $subkey, $i); |
|
139 | + } |
|
135 | 140 | |
136 | - if ($i <= 24 && $i >= 17) |
|
137 | - $this->ruleA($text, $subkey, $i); |
|
141 | + if ($i <= 24 && $i >= 17) { |
|
142 | + $this->ruleA($text, $subkey, $i); |
|
143 | + } |
|
138 | 144 | |
139 | - if ($i <= 16 && $i >= 9) |
|
140 | - $this->ruleB($text, $subkey, $i); |
|
145 | + if ($i <= 16 && $i >= 9) { |
|
146 | + $this->ruleB($text, $subkey, $i); |
|
147 | + } |
|
141 | 148 | |
142 | - if ($i <= 8 && $i >= 1) |
|
143 | - $this->ruleA($text, $subkey, $i); |
|
149 | + if ($i <= 8 && $i >= 1) { |
|
150 | + $this->ruleA($text, $subkey, $i); |
|
151 | + } |
|
144 | 152 | } |
145 | 153 | |
146 | 154 | return true; |
@@ -289,8 +297,7 @@ discard block |
||
289 | 297 | |
290 | 298 | $w[1] = $this->gPermutation($w[0], $key); |
291 | 299 | $w[0] = $w[4]; |
292 | - } |
|
293 | - else // parent::DECRYPT |
|
300 | + } else // parent::DECRYPT |
|
294 | 301 | { |
295 | 302 | /* |
296 | 303 | * Set W4 as the old W0 |
@@ -335,8 +342,9 @@ discard block |
||
335 | 342 | |
336 | 343 | for($i = 0; $i < 128; ++$i) |
337 | 344 | { |
338 | - if($pos == $key_bytes) |
|
339 | - $pos = 0; |
|
345 | + if($pos == $key_bytes) { |
|
346 | + $pos = 0; |
|
347 | + } |
|
340 | 348 | |
341 | 349 | $this->expanded_key .= $key[$pos]; |
342 | 350 | ++$pos; |
@@ -44,204 +44,204 @@ |
||
44 | 44 | */ |
45 | 45 | class Cipher_Vigenere extends Cipher |
46 | 46 | { |
47 | - /** @type array $_vtable The Vigenere table */ |
|
48 | - private static $_vtable = null; |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * Constructor |
|
53 | - * |
|
54 | - * @param string $key The key used for Encryption/Decryption |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function __construct($key) |
|
58 | - { |
|
59 | - // set the key |
|
60 | - parent::__construct(PHP_Crypt::CIPHER_VIGENERE, $key); |
|
61 | - |
|
62 | - $this->initTables(); |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * Destructor |
|
68 | - * |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public function __destruct() |
|
72 | - { |
|
73 | - parent::__destruct(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Encrypt plain text data using Vigenere cipher |
|
79 | - * |
|
80 | - * @return boolean Returns true |
|
81 | - */ |
|
82 | - public function encrypt(&$text) |
|
83 | - { |
|
84 | - $this->operation(parent::ENCRYPT); |
|
85 | - |
|
86 | - // convert to uppercase, and remove any non alphabetic characters |
|
87 | - $text = strtoupper($text); |
|
88 | - $text = preg_replace("/[^A-Z]/", "", $text); |
|
89 | - $len = strlen($text); |
|
90 | - |
|
91 | - // prepare the key for the cipher |
|
92 | - $this->keyPrep($len); |
|
93 | - |
|
94 | - // loop through each letter of the message |
|
95 | - for ($i = 0; $i < $len; ++$i) |
|
96 | - { |
|
97 | - // get the Cipher letter from the Vigenere table, using the |
|
98 | - // current letter from the key as the row, and the current letter |
|
99 | - // from the text, as the column, subtract 65 because ascii upper case |
|
100 | - // letters start at 65 |
|
101 | - $row = ord($this->expanded_key[$i]) - 65; |
|
102 | - $col = ord($text[$i]) - 65; |
|
103 | - $pos = ($row * 26) + $col; |
|
104 | - |
|
105 | - // convert the plain text to cipher text |
|
106 | - $text[$i] = self::$_vtable[$pos]; |
|
107 | - } |
|
108 | - |
|
109 | - return true; |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * Decrypt a Vigenere encrypted string |
|
115 | - * |
|
116 | - * @return boolean Returns true |
|
117 | - */ |
|
118 | - public function decrypt(&$text) |
|
119 | - { |
|
120 | - $this->operation(parent::DECRYPT); |
|
121 | - |
|
122 | - // ensure the cipher text is all uppercase |
|
123 | - $text = strtoupper($text); |
|
124 | - $len = strlen($text); |
|
125 | - |
|
126 | - // prepare the key for the cipher |
|
127 | - $this->keyPrep($len); |
|
128 | - |
|
129 | - // go to the row corresponding to the letter from the key |
|
130 | - for ($i = 0; $i < $len; ++$i) |
|
131 | - { |
|
132 | - // find the row from the current character of the key, we subtract 65 |
|
133 | - // because uppercase letters start at ASCII 65 |
|
134 | - $row = (ord($this->expanded_key[$i]) - 65) * 26; |
|
135 | - |
|
136 | - // loop throw the entire row in the table until we find the letter |
|
137 | - // that matches the letter from the encrypted text |
|
138 | - for ($j = 0; $j < 26; ++$j) |
|
139 | - { |
|
140 | - // save the position we are in in the row |
|
141 | - $pos = $row + $j; |
|
142 | - |
|
143 | - // compare the letter from the table to the letter in the cipher text |
|
144 | - if (self::$_vtable[$pos] == $text[$i]) |
|
145 | - { |
|
146 | - // bingo, we found it. The plain text is the letter associated with |
|
147 | - // with the column position, again add 65 because ascii capital |
|
148 | - // letters start at 65 |
|
149 | - $text[$i] = chr($j + 65); |
|
150 | - break; |
|
151 | - } |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - return true; |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * Prepare the key. The key can only contain uppercase letters. |
|
161 | - * All other characters are stripped out. The key length must match |
|
162 | - * The length of the message |
|
163 | - * |
|
164 | - * @param integer $len The length of message |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - private function keyPrep($len) |
|
168 | - { |
|
169 | - // we never modify the actual key, so we save it into another variable |
|
170 | - $this->expanded_key = $this->key(); |
|
171 | - $this->expanded_key = strtoupper($this->expanded_key); |
|
172 | - $this->expanded_key = preg_replace("/[^A-Z]/", "", $this->expanded_key); |
|
173 | - $keylen = strlen($this->expanded_key); |
|
174 | - |
|
175 | - // The key must be prepared so that it is the same length as the |
|
176 | - // message. If it is longer or shorter we need to modify it |
|
177 | - // to make it the correct length |
|
178 | - if ($keylen > $len) |
|
179 | - $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
180 | - else if ($len > $keylen) |
|
181 | - { |
|
182 | - // if the key is shorter than the message, then we need pad the key |
|
183 | - // by repeating it until it is the correct length |
|
184 | - $diff = $len - $keylen; |
|
185 | - $pos = 0; |
|
186 | - |
|
187 | - for ($i = 0; $i < $diff; ++$i) |
|
188 | - { |
|
189 | - if ($pos >= $keylen) |
|
190 | - $pos = 0; |
|
191 | - |
|
192 | - $this->expanded_key .= $this->expanded_key[$pos]; |
|
193 | - ++$pos; |
|
194 | - } |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - |
|
199 | - /** |
|
200 | - * Initialize the Vigenere table used for encryption & decryption |
|
201 | - * |
|
202 | - * @return void |
|
203 | - */ |
|
204 | - private function initTables() |
|
205 | - { |
|
206 | - self::$_vtable = array( |
|
207 | - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
|
208 | - 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', |
|
209 | - 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', |
|
210 | - 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', |
|
211 | - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', |
|
212 | - 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', |
|
213 | - 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', |
|
214 | - 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
|
215 | - 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
|
216 | - 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', |
|
217 | - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
|
218 | - 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
|
219 | - 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
|
220 | - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
|
221 | - 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', |
|
222 | - 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
|
223 | - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
|
224 | - 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', |
|
225 | - 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
|
226 | - 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', |
|
227 | - 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
|
228 | - 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', |
|
229 | - 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
|
230 | - 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
|
231 | - 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
|
232 | - 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y' |
|
233 | - ); |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * Indicates that this is a stream cipher |
|
239 | - * |
|
240 | - * @return integer Returns Cipher::STREAM |
|
241 | - */ |
|
242 | - public function type() |
|
243 | - { |
|
244 | - return parent::STREAM; |
|
245 | - } |
|
47 | + /** @type array $_vtable The Vigenere table */ |
|
48 | + private static $_vtable = null; |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * Constructor |
|
53 | + * |
|
54 | + * @param string $key The key used for Encryption/Decryption |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function __construct($key) |
|
58 | + { |
|
59 | + // set the key |
|
60 | + parent::__construct(PHP_Crypt::CIPHER_VIGENERE, $key); |
|
61 | + |
|
62 | + $this->initTables(); |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * Destructor |
|
68 | + * |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public function __destruct() |
|
72 | + { |
|
73 | + parent::__destruct(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Encrypt plain text data using Vigenere cipher |
|
79 | + * |
|
80 | + * @return boolean Returns true |
|
81 | + */ |
|
82 | + public function encrypt(&$text) |
|
83 | + { |
|
84 | + $this->operation(parent::ENCRYPT); |
|
85 | + |
|
86 | + // convert to uppercase, and remove any non alphabetic characters |
|
87 | + $text = strtoupper($text); |
|
88 | + $text = preg_replace("/[^A-Z]/", "", $text); |
|
89 | + $len = strlen($text); |
|
90 | + |
|
91 | + // prepare the key for the cipher |
|
92 | + $this->keyPrep($len); |
|
93 | + |
|
94 | + // loop through each letter of the message |
|
95 | + for ($i = 0; $i < $len; ++$i) |
|
96 | + { |
|
97 | + // get the Cipher letter from the Vigenere table, using the |
|
98 | + // current letter from the key as the row, and the current letter |
|
99 | + // from the text, as the column, subtract 65 because ascii upper case |
|
100 | + // letters start at 65 |
|
101 | + $row = ord($this->expanded_key[$i]) - 65; |
|
102 | + $col = ord($text[$i]) - 65; |
|
103 | + $pos = ($row * 26) + $col; |
|
104 | + |
|
105 | + // convert the plain text to cipher text |
|
106 | + $text[$i] = self::$_vtable[$pos]; |
|
107 | + } |
|
108 | + |
|
109 | + return true; |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * Decrypt a Vigenere encrypted string |
|
115 | + * |
|
116 | + * @return boolean Returns true |
|
117 | + */ |
|
118 | + public function decrypt(&$text) |
|
119 | + { |
|
120 | + $this->operation(parent::DECRYPT); |
|
121 | + |
|
122 | + // ensure the cipher text is all uppercase |
|
123 | + $text = strtoupper($text); |
|
124 | + $len = strlen($text); |
|
125 | + |
|
126 | + // prepare the key for the cipher |
|
127 | + $this->keyPrep($len); |
|
128 | + |
|
129 | + // go to the row corresponding to the letter from the key |
|
130 | + for ($i = 0; $i < $len; ++$i) |
|
131 | + { |
|
132 | + // find the row from the current character of the key, we subtract 65 |
|
133 | + // because uppercase letters start at ASCII 65 |
|
134 | + $row = (ord($this->expanded_key[$i]) - 65) * 26; |
|
135 | + |
|
136 | + // loop throw the entire row in the table until we find the letter |
|
137 | + // that matches the letter from the encrypted text |
|
138 | + for ($j = 0; $j < 26; ++$j) |
|
139 | + { |
|
140 | + // save the position we are in in the row |
|
141 | + $pos = $row + $j; |
|
142 | + |
|
143 | + // compare the letter from the table to the letter in the cipher text |
|
144 | + if (self::$_vtable[$pos] == $text[$i]) |
|
145 | + { |
|
146 | + // bingo, we found it. The plain text is the letter associated with |
|
147 | + // with the column position, again add 65 because ascii capital |
|
148 | + // letters start at 65 |
|
149 | + $text[$i] = chr($j + 65); |
|
150 | + break; |
|
151 | + } |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + return true; |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * Prepare the key. The key can only contain uppercase letters. |
|
161 | + * All other characters are stripped out. The key length must match |
|
162 | + * The length of the message |
|
163 | + * |
|
164 | + * @param integer $len The length of message |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + private function keyPrep($len) |
|
168 | + { |
|
169 | + // we never modify the actual key, so we save it into another variable |
|
170 | + $this->expanded_key = $this->key(); |
|
171 | + $this->expanded_key = strtoupper($this->expanded_key); |
|
172 | + $this->expanded_key = preg_replace("/[^A-Z]/", "", $this->expanded_key); |
|
173 | + $keylen = strlen($this->expanded_key); |
|
174 | + |
|
175 | + // The key must be prepared so that it is the same length as the |
|
176 | + // message. If it is longer or shorter we need to modify it |
|
177 | + // to make it the correct length |
|
178 | + if ($keylen > $len) |
|
179 | + $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
180 | + else if ($len > $keylen) |
|
181 | + { |
|
182 | + // if the key is shorter than the message, then we need pad the key |
|
183 | + // by repeating it until it is the correct length |
|
184 | + $diff = $len - $keylen; |
|
185 | + $pos = 0; |
|
186 | + |
|
187 | + for ($i = 0; $i < $diff; ++$i) |
|
188 | + { |
|
189 | + if ($pos >= $keylen) |
|
190 | + $pos = 0; |
|
191 | + |
|
192 | + $this->expanded_key .= $this->expanded_key[$pos]; |
|
193 | + ++$pos; |
|
194 | + } |
|
195 | + } |
|
196 | + } |
|
197 | + |
|
198 | + |
|
199 | + /** |
|
200 | + * Initialize the Vigenere table used for encryption & decryption |
|
201 | + * |
|
202 | + * @return void |
|
203 | + */ |
|
204 | + private function initTables() |
|
205 | + { |
|
206 | + self::$_vtable = array( |
|
207 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
|
208 | + 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', |
|
209 | + 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', |
|
210 | + 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', |
|
211 | + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', |
|
212 | + 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', |
|
213 | + 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', |
|
214 | + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
|
215 | + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
|
216 | + 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', |
|
217 | + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
|
218 | + 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', |
|
219 | + 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
|
220 | + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
|
221 | + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', |
|
222 | + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
|
223 | + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
|
224 | + 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', |
|
225 | + 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
|
226 | + 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', |
|
227 | + 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
|
228 | + 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', |
|
229 | + 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
|
230 | + 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
|
231 | + 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
|
232 | + 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y' |
|
233 | + ); |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * Indicates that this is a stream cipher |
|
239 | + * |
|
240 | + * @return integer Returns Cipher::STREAM |
|
241 | + */ |
|
242 | + public function type() |
|
243 | + { |
|
244 | + return parent::STREAM; |
|
245 | + } |
|
246 | 246 | } |
247 | 247 | ?> |
@@ -175,9 +175,9 @@ discard block |
||
175 | 175 | // The key must be prepared so that it is the same length as the |
176 | 176 | // message. If it is longer or shorter we need to modify it |
177 | 177 | // to make it the correct length |
178 | - if ($keylen > $len) |
|
179 | - $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
180 | - else if ($len > $keylen) |
|
178 | + if ($keylen > $len) { |
|
179 | + $this->expanded_key = substr($this->expanded_key, 0, $len); |
|
180 | + } else if ($len > $keylen) |
|
181 | 181 | { |
182 | 182 | // if the key is shorter than the message, then we need pad the key |
183 | 183 | // by repeating it until it is the correct length |
@@ -186,8 +186,9 @@ discard block |
||
186 | 186 | |
187 | 187 | for ($i = 0; $i < $diff; ++$i) |
188 | 188 | { |
189 | - if ($pos >= $keylen) |
|
190 | - $pos = 0; |
|
189 | + if ($pos >= $keylen) { |
|
190 | + $pos = 0; |
|
191 | + } |
|
191 | 192 | |
192 | 193 | $this->expanded_key .= $this->expanded_key[$pos]; |
193 | 194 | ++$pos; |
@@ -43,208 +43,208 @@ |
||
43 | 43 | */ |
44 | 44 | class Cipher_3DES extends Cipher_DES |
45 | 45 | { |
46 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
47 | - const BYTES_BLOCK = 8; // 64 bits |
|
46 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
47 | + const BYTES_BLOCK = 8; // 64 bits |
|
48 | 48 | |
49 | - /** @type integer BYTES_KEY The size of the key. The key can be |
|
49 | + /** @type integer BYTES_KEY The size of the key. The key can be |
|
50 | 50 | 8, 16, or 24 bytes but gets expanded to 24 bytes before used */ |
51 | - const BYTES_KEY = 24; |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * Constructor |
|
56 | - * |
|
57 | - * @param string $key The key used for Encryption/Decryption |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function __construct($key) |
|
61 | - { |
|
62 | - $key_len = strlen($key); |
|
63 | - |
|
64 | - if ($key_len == 8 || $key_len == 16) |
|
65 | - $key = self::expandKey($key, $key_len); |
|
66 | - else if ($key_len < self::BYTES_KEY) |
|
67 | - { |
|
68 | - $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
|
69 | - $msg .= "$key_len bytes received."; |
|
70 | - trigger_error($msg, E_USER_WARNING); |
|
71 | - } |
|
72 | - // else if the key is longer than 24 bytes, phpCrypt will shorten it |
|
73 | - |
|
74 | - // set the 3DES key, note that we call __construct1() not __construct() |
|
75 | - // this is a second contructor we created for classes that extend the |
|
76 | - // DES class |
|
77 | - parent::__construct1(PHP_Crypt::CIPHER_3DES, $key, self::BYTES_KEY); |
|
78 | - |
|
79 | - // 3DES requires that data is 64 bits |
|
80 | - $this->blockSize(self::BYTES_BLOCK); |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Destructor |
|
86 | - * |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - public function __destruct() |
|
90 | - { |
|
91 | - parent::__destruct(); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * Encrypt plain text data using TripleDES |
|
97 | - * For encryption 3DES takes a 192 bit key, splits |
|
98 | - * it into 3 64 bit parts, and then does the following |
|
99 | - * DES ENCRYPT(key1) -> DES DECRYPT(key2) -> DES ENCRYPT(key3) |
|
100 | - * |
|
101 | - * @return boolean Returns true |
|
102 | - */ |
|
103 | - public function encrypt(&$text) |
|
104 | - { |
|
105 | - $blocksz = $this->blockSize(); |
|
106 | - |
|
107 | - for ($i = 0; $i < 3; ++$i) |
|
108 | - { |
|
109 | - $key = substr($this->key(), ($i * 8), $blocksz); |
|
110 | - $this->keyPermutation($key); |
|
111 | - |
|
112 | - if ($i % 2) // round 1 |
|
113 | - $this->operation(parent::DECRYPT); |
|
114 | - else // rounds 0 and 2 |
|
115 | - $this->operation(parent::ENCRYPT); |
|
116 | - |
|
117 | - $this->des($text); |
|
118 | - } |
|
119 | - |
|
120 | - return true; |
|
121 | - } |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * Decrypt a TripleDES encrypted string |
|
126 | - * For decryption 3DES takes a 192 bit key, splits |
|
127 | - * it into 3 64 bit parts, and then does the following |
|
128 | - * DES DECRYPT(key1) -> DES ENCRYPT(key2) -> DES DECRYPT(key3) |
|
129 | - * |
|
130 | - * @return boolean Returns true |
|
131 | - */ |
|
132 | - public function decrypt(&$text) |
|
133 | - { |
|
134 | - $blocksz = $this->blockSize(); |
|
135 | - |
|
136 | - for ($i = 2; $i >= 0; --$i) |
|
137 | - { |
|
138 | - $key = substr($this->key(), ($i * 8), $blocksz); |
|
139 | - $this->keyPermutation($key); |
|
140 | - |
|
141 | - if ($i % 2) // round 1 |
|
142 | - $this->operation(parent::ENCRYPT); |
|
143 | - else // round 0 and 2 |
|
144 | - $this->operation(parent::DECRYPT); |
|
145 | - |
|
146 | - $this->des($text); |
|
147 | - } |
|
148 | - |
|
149 | - return true; |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * We overload the DES::keyPermutation() function so it takes |
|
155 | - * a 64 bit key as a parameter. This is because 3DES uses a 192 bit |
|
156 | - * key which is split into 3 64 bit parts, each of which must be |
|
157 | - * passed through the keyPermutation() function |
|
158 | - * Key permutation, based on tables $_pc1 and $_pc2 |
|
159 | - * Create 16 subkeys, each of which is 48-bits long. |
|
160 | - * NOTE: The $key parameter is required, however because PHP expects |
|
161 | - * an overloaded method to contain the same number of parameters |
|
162 | - * (when E_STRICT is set) as the parent class, we make it optional |
|
163 | - * to shut the 'error' up |
|
164 | - * |
|
165 | - * @param string $key A 64 bit key |
|
166 | - * @return void |
|
167 | - */ |
|
168 | - private function keyPermutation($key = "") |
|
169 | - { |
|
170 | - $this->sub_keys = array(); |
|
171 | - $pc1m = array(); |
|
172 | - $pcr = array(); |
|
173 | - $c = array(); |
|
174 | - $d = array(); |
|
175 | - |
|
176 | - // convert the key to binary |
|
177 | - $binkey = parent::str2Bin($key); |
|
178 | - |
|
179 | - // reduce the key down to 56bits based on table $_pc1 |
|
180 | - for ($i = 0; $i < 56; ++$i) |
|
181 | - { |
|
182 | - $pos = parent::$_pc1[$i] - 1; |
|
51 | + const BYTES_KEY = 24; |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * Constructor |
|
56 | + * |
|
57 | + * @param string $key The key used for Encryption/Decryption |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function __construct($key) |
|
61 | + { |
|
62 | + $key_len = strlen($key); |
|
63 | + |
|
64 | + if ($key_len == 8 || $key_len == 16) |
|
65 | + $key = self::expandKey($key, $key_len); |
|
66 | + else if ($key_len < self::BYTES_KEY) |
|
67 | + { |
|
68 | + $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
|
69 | + $msg .= "$key_len bytes received."; |
|
70 | + trigger_error($msg, E_USER_WARNING); |
|
71 | + } |
|
72 | + // else if the key is longer than 24 bytes, phpCrypt will shorten it |
|
73 | + |
|
74 | + // set the 3DES key, note that we call __construct1() not __construct() |
|
75 | + // this is a second contructor we created for classes that extend the |
|
76 | + // DES class |
|
77 | + parent::__construct1(PHP_Crypt::CIPHER_3DES, $key, self::BYTES_KEY); |
|
78 | + |
|
79 | + // 3DES requires that data is 64 bits |
|
80 | + $this->blockSize(self::BYTES_BLOCK); |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Destructor |
|
86 | + * |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + public function __destruct() |
|
90 | + { |
|
91 | + parent::__destruct(); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * Encrypt plain text data using TripleDES |
|
97 | + * For encryption 3DES takes a 192 bit key, splits |
|
98 | + * it into 3 64 bit parts, and then does the following |
|
99 | + * DES ENCRYPT(key1) -> DES DECRYPT(key2) -> DES ENCRYPT(key3) |
|
100 | + * |
|
101 | + * @return boolean Returns true |
|
102 | + */ |
|
103 | + public function encrypt(&$text) |
|
104 | + { |
|
105 | + $blocksz = $this->blockSize(); |
|
106 | + |
|
107 | + for ($i = 0; $i < 3; ++$i) |
|
108 | + { |
|
109 | + $key = substr($this->key(), ($i * 8), $blocksz); |
|
110 | + $this->keyPermutation($key); |
|
111 | + |
|
112 | + if ($i % 2) // round 1 |
|
113 | + $this->operation(parent::DECRYPT); |
|
114 | + else // rounds 0 and 2 |
|
115 | + $this->operation(parent::ENCRYPT); |
|
116 | + |
|
117 | + $this->des($text); |
|
118 | + } |
|
119 | + |
|
120 | + return true; |
|
121 | + } |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * Decrypt a TripleDES encrypted string |
|
126 | + * For decryption 3DES takes a 192 bit key, splits |
|
127 | + * it into 3 64 bit parts, and then does the following |
|
128 | + * DES DECRYPT(key1) -> DES ENCRYPT(key2) -> DES DECRYPT(key3) |
|
129 | + * |
|
130 | + * @return boolean Returns true |
|
131 | + */ |
|
132 | + public function decrypt(&$text) |
|
133 | + { |
|
134 | + $blocksz = $this->blockSize(); |
|
135 | + |
|
136 | + for ($i = 2; $i >= 0; --$i) |
|
137 | + { |
|
138 | + $key = substr($this->key(), ($i * 8), $blocksz); |
|
139 | + $this->keyPermutation($key); |
|
140 | + |
|
141 | + if ($i % 2) // round 1 |
|
142 | + $this->operation(parent::ENCRYPT); |
|
143 | + else // round 0 and 2 |
|
144 | + $this->operation(parent::DECRYPT); |
|
145 | + |
|
146 | + $this->des($text); |
|
147 | + } |
|
148 | + |
|
149 | + return true; |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * We overload the DES::keyPermutation() function so it takes |
|
155 | + * a 64 bit key as a parameter. This is because 3DES uses a 192 bit |
|
156 | + * key which is split into 3 64 bit parts, each of which must be |
|
157 | + * passed through the keyPermutation() function |
|
158 | + * Key permutation, based on tables $_pc1 and $_pc2 |
|
159 | + * Create 16 subkeys, each of which is 48-bits long. |
|
160 | + * NOTE: The $key parameter is required, however because PHP expects |
|
161 | + * an overloaded method to contain the same number of parameters |
|
162 | + * (when E_STRICT is set) as the parent class, we make it optional |
|
163 | + * to shut the 'error' up |
|
164 | + * |
|
165 | + * @param string $key A 64 bit key |
|
166 | + * @return void |
|
167 | + */ |
|
168 | + private function keyPermutation($key = "") |
|
169 | + { |
|
170 | + $this->sub_keys = array(); |
|
171 | + $pc1m = array(); |
|
172 | + $pcr = array(); |
|
173 | + $c = array(); |
|
174 | + $d = array(); |
|
175 | + |
|
176 | + // convert the key to binary |
|
177 | + $binkey = parent::str2Bin($key); |
|
178 | + |
|
179 | + // reduce the key down to 56bits based on table $_pc1 |
|
180 | + for ($i = 0; $i < 56; ++$i) |
|
181 | + { |
|
182 | + $pos = parent::$_pc1[$i] - 1; |
|
183 | 183 | $pc1m[$i] = $binkey[$pos]; |
184 | - } |
|
185 | - |
|
186 | - // split $pc1m in half (C0 and D0) |
|
187 | - $c[0] = array_slice($pc1m, 0, 28); |
|
188 | - $d[0] = array_slice($pc1m, 28, 28); |
|
189 | - |
|
190 | - // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
191 | - // where 1 <= n <= 16 |
|
192 | - for ($i = 1; $i <= 16; ++$i) |
|
193 | - { |
|
194 | - // now set the next Cn and Dn as the previous Cn and Dn |
|
195 | - $c[$i] = $c[$i - 1]; |
|
196 | - $d[$i] = $d[$i - 1]; |
|
197 | - |
|
198 | - for ($j = 0; $j < parent::$_key_sched[$i - 1]; ++$j) |
|
199 | - { |
|
200 | - // do a left shift, move each bit one place to the left, |
|
201 | - // except for the first bit, which is cycled to the end |
|
202 | - // of the block. |
|
203 | - $c[$i][] = array_shift($c[$i]); |
|
204 | - $d[$i][] = array_shift($d[$i]); |
|
205 | - } |
|
206 | - |
|
207 | - // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
208 | - // following permutation table to each of the concatenated |
|
209 | - // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
210 | - // of these. |
|
211 | - $CnDn = array_merge($c[$i], $d[$i]); |
|
212 | - $this->sub_keys[$i - 1] = ""; |
|
213 | - for ($j = 0; $j < 48; ++$j) |
|
214 | - $this->sub_keys[$i - 1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
215 | - } |
|
216 | - |
|
217 | - // the sub_keys are created, we are done with the key permutation |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * Triple DES accepts an 8, 16, or 24 bytes key. If the key is |
|
223 | - * 8 or 16 bytes, it is expanded to 24 bytes |
|
224 | - * An 8 byte key is expanded by repeating it 3 times to make a 24 |
|
225 | - * byte key |
|
226 | - * A 16 byte key add the first 8 bytes to the end of the string |
|
227 | - * to make a 24 byte key |
|
228 | - * |
|
229 | - * @param string $key The 8 or 16 byte key to expand |
|
230 | - * @param integer $len |
|
231 | - * @return string If the key given is 8 or 16 bytes it returns the |
|
232 | - * expanded 24 byte key, else it returns the original key unexpanded |
|
233 | - */ |
|
234 | - private static function expandKey($key, $len) |
|
235 | - { |
|
236 | - // if we were given an 8 byte key, repeat it |
|
237 | - // 3 times to produce a 24 byte key |
|
238 | - if ($len == 8) |
|
239 | - $key = str_repeat($key, 3); |
|
240 | - |
|
241 | - // if we were given a 16 byte key, add the first |
|
242 | - // 8 bytes to the end of the key to produce 24 bytes |
|
243 | - if ($len == 16) |
|
244 | - $key .= substr($key, 0, 8); |
|
245 | - |
|
246 | - // return the key |
|
247 | - return $key; |
|
248 | - } |
|
184 | + } |
|
185 | + |
|
186 | + // split $pc1m in half (C0 and D0) |
|
187 | + $c[0] = array_slice($pc1m, 0, 28); |
|
188 | + $d[0] = array_slice($pc1m, 28, 28); |
|
189 | + |
|
190 | + // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
191 | + // where 1 <= n <= 16 |
|
192 | + for ($i = 1; $i <= 16; ++$i) |
|
193 | + { |
|
194 | + // now set the next Cn and Dn as the previous Cn and Dn |
|
195 | + $c[$i] = $c[$i - 1]; |
|
196 | + $d[$i] = $d[$i - 1]; |
|
197 | + |
|
198 | + for ($j = 0; $j < parent::$_key_sched[$i - 1]; ++$j) |
|
199 | + { |
|
200 | + // do a left shift, move each bit one place to the left, |
|
201 | + // except for the first bit, which is cycled to the end |
|
202 | + // of the block. |
|
203 | + $c[$i][] = array_shift($c[$i]); |
|
204 | + $d[$i][] = array_shift($d[$i]); |
|
205 | + } |
|
206 | + |
|
207 | + // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
208 | + // following permutation table to each of the concatenated |
|
209 | + // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
210 | + // of these. |
|
211 | + $CnDn = array_merge($c[$i], $d[$i]); |
|
212 | + $this->sub_keys[$i - 1] = ""; |
|
213 | + for ($j = 0; $j < 48; ++$j) |
|
214 | + $this->sub_keys[$i - 1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
215 | + } |
|
216 | + |
|
217 | + // the sub_keys are created, we are done with the key permutation |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * Triple DES accepts an 8, 16, or 24 bytes key. If the key is |
|
223 | + * 8 or 16 bytes, it is expanded to 24 bytes |
|
224 | + * An 8 byte key is expanded by repeating it 3 times to make a 24 |
|
225 | + * byte key |
|
226 | + * A 16 byte key add the first 8 bytes to the end of the string |
|
227 | + * to make a 24 byte key |
|
228 | + * |
|
229 | + * @param string $key The 8 or 16 byte key to expand |
|
230 | + * @param integer $len |
|
231 | + * @return string If the key given is 8 or 16 bytes it returns the |
|
232 | + * expanded 24 byte key, else it returns the original key unexpanded |
|
233 | + */ |
|
234 | + private static function expandKey($key, $len) |
|
235 | + { |
|
236 | + // if we were given an 8 byte key, repeat it |
|
237 | + // 3 times to produce a 24 byte key |
|
238 | + if ($len == 8) |
|
239 | + $key = str_repeat($key, 3); |
|
240 | + |
|
241 | + // if we were given a 16 byte key, add the first |
|
242 | + // 8 bytes to the end of the key to produce 24 bytes |
|
243 | + if ($len == 16) |
|
244 | + $key .= substr($key, 0, 8); |
|
245 | + |
|
246 | + // return the key |
|
247 | + return $key; |
|
248 | + } |
|
249 | 249 | } |
250 | 250 | ?> |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | { |
62 | 62 | $key_len = strlen($key); |
63 | 63 | |
64 | - if ($key_len == 8 || $key_len == 16) |
|
65 | - $key = self::expandKey($key, $key_len); |
|
66 | - else if ($key_len < self::BYTES_KEY) |
|
64 | + if ($key_len == 8 || $key_len == 16) { |
|
65 | + $key = self::expandKey($key, $key_len); |
|
66 | + } else if ($key_len < self::BYTES_KEY) |
|
67 | 67 | { |
68 | 68 | $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
69 | 69 | $msg .= "$key_len bytes received."; |
@@ -109,10 +109,13 @@ discard block |
||
109 | 109 | $key = substr($this->key(), ($i * 8), $blocksz); |
110 | 110 | $this->keyPermutation($key); |
111 | 111 | |
112 | - if ($i % 2) // round 1 |
|
112 | + if ($i % 2) { |
|
113 | + // round 1 |
|
113 | 114 | $this->operation(parent::DECRYPT); |
114 | - else // rounds 0 and 2 |
|
115 | + } else { |
|
116 | + // rounds 0 and 2 |
|
115 | 117 | $this->operation(parent::ENCRYPT); |
118 | + } |
|
116 | 119 | |
117 | 120 | $this->des($text); |
118 | 121 | } |
@@ -138,10 +141,13 @@ discard block |
||
138 | 141 | $key = substr($this->key(), ($i * 8), $blocksz); |
139 | 142 | $this->keyPermutation($key); |
140 | 143 | |
141 | - if ($i % 2) // round 1 |
|
144 | + if ($i % 2) { |
|
145 | + // round 1 |
|
142 | 146 | $this->operation(parent::ENCRYPT); |
143 | - else // round 0 and 2 |
|
147 | + } else { |
|
148 | + // round 0 and 2 |
|
144 | 149 | $this->operation(parent::DECRYPT); |
150 | + } |
|
145 | 151 | |
146 | 152 | $this->des($text); |
147 | 153 | } |
@@ -210,8 +216,9 @@ discard block |
||
210 | 216 | // of these. |
211 | 217 | $CnDn = array_merge($c[$i], $d[$i]); |
212 | 218 | $this->sub_keys[$i - 1] = ""; |
213 | - for ($j = 0; $j < 48; ++$j) |
|
214 | - $this->sub_keys[$i - 1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
219 | + for ($j = 0; $j < 48; ++$j) { |
|
220 | + $this->sub_keys[$i - 1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
221 | + } |
|
215 | 222 | } |
216 | 223 | |
217 | 224 | // the sub_keys are created, we are done with the key permutation |
@@ -235,13 +242,15 @@ discard block |
||
235 | 242 | { |
236 | 243 | // if we were given an 8 byte key, repeat it |
237 | 244 | // 3 times to produce a 24 byte key |
238 | - if ($len == 8) |
|
239 | - $key = str_repeat($key, 3); |
|
245 | + if ($len == 8) { |
|
246 | + $key = str_repeat($key, 3); |
|
247 | + } |
|
240 | 248 | |
241 | 249 | // if we were given a 16 byte key, add the first |
242 | 250 | // 8 bytes to the end of the key to produce 24 bytes |
243 | - if ($len == 16) |
|
244 | - $key .= substr($key, 0, 8); |
|
251 | + if ($len == 16) { |
|
252 | + $key .= substr($key, 0, 8); |
|
253 | + } |
|
245 | 254 | |
246 | 255 | // return the key |
247 | 256 | return $key; |
@@ -42,994 +42,994 @@ |
||
42 | 42 | */ |
43 | 43 | abstract class Cipher_Rijndael extends Cipher |
44 | 44 | { |
45 | - /** @type string $xkey The expanded key */ |
|
46 | - private $xkey = ""; |
|
47 | - |
|
48 | - /** |
|
49 | - * @type array $_key_sizes The accepted key sizes in bytes, |
|
50 | - * this should be considered a constant |
|
51 | - */ |
|
52 | - private static $_key_sizes = array(16, 24, 32); |
|
53 | - |
|
54 | - |
|
55 | - // THE FOLLOWING TABLES ARE INITIALIZED IN initTables() |
|
56 | - |
|
57 | - /** |
|
58 | - * @type array $_s The sbox, |
|
59 | - * this should be considered a constant |
|
60 | - */ |
|
61 | - private static $_s = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * @type array $_s_inv The inverse sbox |
|
65 | - * this should be considered a constant |
|
66 | - */ |
|
67 | - private static $_s_inv = array(); |
|
68 | - |
|
69 | - /** |
|
70 | - * @type array $_rcon The round constant, |
|
71 | - * this should be considered a constant |
|
72 | - */ |
|
73 | - private static $_rcon = array(); |
|
74 | - |
|
75 | - /** |
|
76 | - * @type array $s_matrix_mult The matrix multiplication table, |
|
77 | - * this should be considered a constant |
|
78 | - */ |
|
79 | - private static $_matrix_mult = array(); |
|
80 | - |
|
81 | - /** |
|
82 | - * @type array $_matrix_mult_inv The matrix multiplication |
|
83 | - * inverse table, this should be considered a constant |
|
84 | - */ |
|
85 | - private static $_matrix_mult_inv = array(); |
|
86 | - |
|
87 | - /* |
|
45 | + /** @type string $xkey The expanded key */ |
|
46 | + private $xkey = ""; |
|
47 | + |
|
48 | + /** |
|
49 | + * @type array $_key_sizes The accepted key sizes in bytes, |
|
50 | + * this should be considered a constant |
|
51 | + */ |
|
52 | + private static $_key_sizes = array(16, 24, 32); |
|
53 | + |
|
54 | + |
|
55 | + // THE FOLLOWING TABLES ARE INITIALIZED IN initTables() |
|
56 | + |
|
57 | + /** |
|
58 | + * @type array $_s The sbox, |
|
59 | + * this should be considered a constant |
|
60 | + */ |
|
61 | + private static $_s = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * @type array $_s_inv The inverse sbox |
|
65 | + * this should be considered a constant |
|
66 | + */ |
|
67 | + private static $_s_inv = array(); |
|
68 | + |
|
69 | + /** |
|
70 | + * @type array $_rcon The round constant, |
|
71 | + * this should be considered a constant |
|
72 | + */ |
|
73 | + private static $_rcon = array(); |
|
74 | + |
|
75 | + /** |
|
76 | + * @type array $s_matrix_mult The matrix multiplication table, |
|
77 | + * this should be considered a constant |
|
78 | + */ |
|
79 | + private static $_matrix_mult = array(); |
|
80 | + |
|
81 | + /** |
|
82 | + * @type array $_matrix_mult_inv The matrix multiplication |
|
83 | + * inverse table, this should be considered a constant |
|
84 | + */ |
|
85 | + private static $_matrix_mult_inv = array(); |
|
86 | + |
|
87 | + /* |
|
88 | 88 | * Galois Multiplication lookup tables, |
89 | 89 | * initialized in initTables() |
90 | 90 | */ |
91 | - /** |
|
92 | - * @type array $_gm2 The Galois Multiplication table |
|
93 | - * for muliplying by 2, this should be considered a constant |
|
94 | - */ |
|
95 | - private static $_gm2 = array(); |
|
96 | - |
|
97 | - /** |
|
98 | - * @type array $_gm3 The Galois Multiplication table |
|
99 | - * for muliplying by 3, this should be considered a constant |
|
100 | - */ |
|
101 | - private static $_gm3 = array(); |
|
102 | - |
|
103 | - /** |
|
104 | - * @type array $_gm9 The Galois Multiplication table |
|
105 | - * for muliplying by 9, this should be considered a constant |
|
106 | - */ |
|
107 | - private static $_gm9 = array(); |
|
108 | - |
|
109 | - /** |
|
110 | - * @type array $_gm11 The Galois Multiplication table |
|
111 | - * for muliplying by 11, this should be considered a constant |
|
112 | - */ |
|
113 | - private static $_gm11 = array(); |
|
114 | - |
|
115 | - /** |
|
116 | - * @type array $_gm13 The Galois Multiplication table |
|
117 | - * for muliplying by 13, this should be considered a constant |
|
118 | - */ |
|
119 | - private static $_gm13 = array(); |
|
120 | - |
|
121 | - /** |
|
122 | - * @type array $_gm14 The Galois Multiplication table |
|
123 | - * for muliplying by 14, this should be considered a constant |
|
124 | - */ |
|
125 | - private static $_gm14 = array(); |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Constructor |
|
130 | - * Sets the key used for encryption. Also sets the requied block size |
|
131 | - * |
|
132 | - * @param string The cipher name as set in a constant in the child class |
|
133 | - * @param string $key string containing the user supplied encryption key |
|
134 | - * @param integer $len Optional, the key size in bytes - used only by the AES child classes |
|
135 | - * @return void |
|
136 | - */ |
|
137 | - public function __construct($cipher_name, $key, $len = 0) |
|
138 | - { |
|
139 | - // AES will pass in a $len, since it has a fixed key size, other |
|
140 | - // rijndael implementations can use variable key sizes, supported |
|
141 | - // sizes are stored in self::$_key_sizes |
|
142 | - if ($len == 0) |
|
143 | - { |
|
144 | - // the key must be one of the following lengths: 16, 24, 32 bytes |
|
145 | - $len = strlen($key); |
|
146 | - if (!in_array($len, self::$_key_sizes)) |
|
147 | - { |
|
148 | - $msg = "Incorrect key length for ".strtoupper($cipher_name).". "; |
|
149 | - $msg .= "Received $len bytes."; |
|
150 | - trigger_error($msg, E_USER_WARNING); |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - // Setup the key |
|
155 | - parent::__construct($cipher_name, $key, $len); |
|
156 | - |
|
157 | - // initialize the tables used for rijndael/aes |
|
158 | - $this->initTables(); |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * Destructor |
|
164 | - * |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - public function __destruct() |
|
168 | - { |
|
169 | - parent::__destruct(); |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * Performs Rijndael encryption |
|
175 | - * |
|
176 | - * @param string @text The string to encrypt |
|
177 | - * @return boolean Returns true |
|
178 | - */ |
|
179 | - public function encrypt(&$text) |
|
180 | - { |
|
181 | - // set the operation to decryption |
|
182 | - $this->operation(parent::ENCRYPT); |
|
183 | - |
|
184 | - $loops = 0; |
|
185 | - $key_sz = $this->keySize(); |
|
186 | - $blk_sz = $this->blockSize(); |
|
187 | - |
|
188 | - // if the key and block size is 16, do 10 rounds |
|
189 | - // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
190 | - // if either key or block size is 32, do 14 rounds |
|
191 | - if ($key_sz == 16 && $blk_sz == 16) |
|
192 | - $loops = 10; |
|
193 | - else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
194 | - $loops = 12; |
|
195 | - else if ($key_sz == 32 || $blk_sz == 32) |
|
196 | - $loops = 14; |
|
197 | - |
|
198 | - // now begin the encryption |
|
199 | - $this->addRoundKey($text, 0); |
|
200 | - |
|
201 | - for ($i = 1; $i <= $loops; ++$i) |
|
202 | - { |
|
203 | - $this->byteSub($text); |
|
204 | - $this->shiftRow($text); |
|
205 | - |
|
206 | - // the last iteration does not use mixColumn |
|
207 | - if ($i < $loops) |
|
208 | - $this->mixColumn($text); |
|
209 | - |
|
210 | - $this->addRoundKey($text, $i); |
|
211 | - } |
|
212 | - |
|
213 | - return true; |
|
214 | - } |
|
215 | - |
|
216 | - |
|
217 | - /** |
|
218 | - * Performs Rijndael decryption |
|
219 | - * |
|
220 | - * @param string @text The string to decrypt |
|
221 | - * @return boolean Returns true |
|
222 | - */ |
|
223 | - public function decrypt(&$text) |
|
224 | - { |
|
225 | - // set the operation to decryption |
|
226 | - $this->operation(parent::DECRYPT); |
|
227 | - |
|
228 | - $loops = 0; |
|
229 | - $key_sz = $this->keySize(); |
|
230 | - $blk_sz = $this->blockSize(); |
|
231 | - |
|
232 | - // if the key and block size is 16, do 10 rounds |
|
233 | - // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
234 | - // if either key or block size is 32, do 14 rounds |
|
235 | - if ($key_sz == 16 && $blk_sz == 16) |
|
236 | - $loops = 10; |
|
237 | - else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
238 | - $loops = 12; |
|
239 | - else if ($key_sz == 32 || $blk_sz == 32) |
|
240 | - $loops = 14; |
|
241 | - |
|
242 | - // now begin the decryption |
|
243 | - $this->addRoundKey($text, 0); |
|
244 | - |
|
245 | - for ($i = 1; $i <= $loops; ++$i) |
|
246 | - { |
|
247 | - $this->shiftRow($text); |
|
248 | - $this->byteSub($text); |
|
249 | - $this->addRoundKey($text, $i); |
|
250 | - |
|
251 | - // the last iteration does not use mixColumn |
|
252 | - if ($i < $loops) |
|
253 | - $this->mixColumn($text); |
|
254 | - } |
|
255 | - |
|
256 | - return true; |
|
257 | - } |
|
258 | - |
|
259 | - |
|
260 | - /** |
|
261 | - * Indicates that this is a block cipher |
|
262 | - * |
|
263 | - * @return integer Returns Cipher::BLOCK |
|
264 | - */ |
|
265 | - public function type() |
|
266 | - { |
|
267 | - return parent::BLOCK; |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * Do the multiplication required in mixColumn() |
|
273 | - * We follow the description the multiplication from Wikipedia: |
|
274 | - * http://en.wikipedia.org/wiki/Advanced_Encryption_Standard#The_MixColumns_step |
|
275 | - * |
|
276 | - * @param integer $m A value from self::$_matrix_mult or self::$_matrix_mult_inv |
|
277 | - * @param integer $byte The value to multipy by $m |
|
278 | - * @return integer The result of the multiplication |
|
279 | - */ |
|
280 | - protected function mixColumnMultiply($m, $byte) |
|
281 | - { |
|
282 | - // if multiplying by 1, then we just return the same number |
|
283 | - if ($m == 0x01) |
|
284 | - return $byte; |
|
285 | - |
|
286 | - $hex = parent::dec2Hex($byte); |
|
287 | - $row = parent::hex2Dec($hex[0]); |
|
288 | - $col = parent::hex2Dec($hex[1]); |
|
289 | - $pos = ($row * 16) + $col; |
|
290 | - |
|
291 | - // multiply by 2 (comes from self::$_matrix_mult during encryption) |
|
292 | - if ($m == 0x02) |
|
293 | - return self::$_gm2[$pos]; |
|
294 | - |
|
295 | - // multiply by 3 (comes from self::$_matrix_mult during encryption) |
|
296 | - if ($m == 0x03) |
|
297 | - return self::$_gm3[$pos]; |
|
298 | - |
|
299 | - // multiply by 9 (comes from self::$_matrix_mult_inv during decryption) |
|
300 | - if ($m == 0x09) |
|
301 | - return self::$_gm9[$pos]; |
|
302 | - |
|
303 | - // multiply by 11 (comes from self::$_matrix_mult_inv during decryption) |
|
304 | - if ($m == 0x0b) |
|
305 | - return self::$_gm11[$pos]; |
|
306 | - |
|
307 | - // multiply by 13 (comes from self::$_matrix_mult_inv during decryption) |
|
308 | - if ($m == 0x0d) |
|
309 | - return self::$_gm13[$pos]; |
|
310 | - |
|
311 | - // multiply by 14 (comes from self::$_matrix_mult_inv during decryption) |
|
312 | - if ($m == 0x0e) |
|
313 | - return self::$_gm14[$pos]; |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * Each time this function is called, it XORs the 16 bytes of $text |
|
319 | - * with the next 16 bytes from the $this->xkey. The expanded key |
|
320 | - * never has the same bytes used twice. Note that the first time |
|
321 | - * addRoundKey() is called, it will be outside of the rounds, so no |
|
322 | - * $round is given. Every call after that will be inside the rounds. |
|
323 | - * |
|
324 | - * @param string $text The text to encrypt/decrypt |
|
325 | - * @param integer $round The round we are on inside of rijndael() |
|
326 | - * @return void |
|
327 | - */ |
|
328 | - private function addRoundKey(&$text, $round) |
|
329 | - { |
|
330 | - // length of the xkey |
|
331 | - $ek_len = strlen($this->xkey); |
|
332 | - $len = $this->blockSize(); |
|
333 | - |
|
334 | - if ($this->operation() == parent::ENCRYPT) |
|
335 | - $offset = $round * $len; |
|
336 | - else |
|
337 | - $offset = ($ek_len - ($round * $len)) - $len; |
|
338 | - |
|
339 | - for ($i = 0; $i < $len; ++$i) |
|
340 | - $text[$i] = $text[$i] ^ $this->xkey[$offset + $i]; |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * Applies the Sbox to each byte of the string passed in |
|
346 | - * This is used in key expansione |
|
347 | - * |
|
348 | - * @param string $text The string to peform the byte subsitution |
|
349 | - * @return void |
|
350 | - */ |
|
351 | - private function byteSub(&$text) |
|
352 | - { |
|
353 | - $max = strlen($text); |
|
354 | - for ($i = 0; $i < $max; ++$i) |
|
355 | - { |
|
356 | - // the sbox is arrange in a 16 x 16 grid, where each row |
|
357 | - // and column is numbered in hex (from 0 - f) |
|
358 | - $hex = parent::str2Hex($text[$i]); |
|
359 | - $row = parent::hex2Dec($hex[0]); |
|
360 | - $col = parent::hex2Dec($hex[1]); |
|
361 | - $pos = ($row * 16) + $col; |
|
362 | - |
|
363 | - // return the corresponding value from the sbox |
|
364 | - if ($this->operation() == parent::ENCRYPT) |
|
365 | - $text[$i] = chr(self::$_s[$pos]); |
|
366 | - else // parent::DECRYPT uses the inverse sbox |
|
367 | - $text[$i] = chr(self::$_s_inv[$pos]); |
|
368 | - } |
|
369 | - } |
|
370 | - |
|
371 | - |
|
372 | - /** |
|
373 | - * This function is hard to explain, the easiest way to understand it is to read |
|
374 | - * http://www.net-security.org/dl/articles/AESbyExample.pdf, Section 5.4 |
|
375 | - * |
|
376 | - * @param string $t The string to multiply bytes by the Galois Multiplication lookup tables |
|
377 | - * @return void |
|
378 | - */ |
|
379 | - private function mixColumn(&$t) |
|
380 | - { |
|
381 | - $tmp = $t; |
|
382 | - |
|
383 | - // the matrix we use depends on if we are encrypting or decrypting |
|
384 | - if ($this->operation() == parent::ENCRYPT) |
|
385 | - $m = self::$_matrix_mult; |
|
386 | - else // parent::DECRYPT |
|
387 | - $m = self::$_matrix_mult_inv; |
|
388 | - |
|
389 | - // the number of rounds we make depends on the block size of the text |
|
390 | - // used during encryption/decryption |
|
391 | - // 128 has a 4x4 matrix, loop 4 times |
|
392 | - // 192 has a 4x6 matrix, loop 6 times |
|
393 | - // 256 has a 4x8 matrix, loop 8 times |
|
394 | - $max_col = ($this->blockSize() * 8) / 32; |
|
395 | - |
|
396 | - // loop through each column of the matrix |
|
397 | - for ($col = 0; $col < $max_col; ++$col) |
|
398 | - { |
|
399 | - $pos = $col * 4; |
|
400 | - |
|
401 | - $a = $this->mixColumnMultiply($m[0], ord($tmp[$pos + 0])); |
|
402 | - $b = $this->mixColumnMultiply($m[4], ord($tmp[$pos + 1])); |
|
403 | - $c = $this->mixColumnMultiply($m[8], ord($tmp[$pos + 2])); |
|
404 | - $d = $this->mixColumnMultiply($m[12], ord($tmp[$pos + 3])); |
|
405 | - $t[$pos + 0] = chr($a ^ $b ^ $c ^ $d); |
|
406 | - |
|
407 | - $a = $this->mixColumnMultiply($m[1], ord($tmp[$pos + 0])); |
|
408 | - $b = $this->mixColumnMultiply($m[5], ord($tmp[$pos + 1])); |
|
409 | - $c = $this->mixColumnMultiply($m[9], ord($tmp[$pos + 2])); |
|
410 | - $d = $this->mixColumnMultiply($m[13], ord($tmp[$pos + 3])); |
|
411 | - $t[$pos + 1] = chr($a ^ $b ^ $c ^ $d); |
|
412 | - |
|
413 | - $a = $this->mixColumnMultiply($m[2], ord($tmp[$pos + 0])); |
|
414 | - $b = $this->mixColumnMultiply($m[6], ord($tmp[$pos + 1])); |
|
415 | - $c = $this->mixColumnMultiply($m[10], ord($tmp[$pos + 2])); |
|
416 | - $d = $this->mixColumnMultiply($m[14], ord($tmp[$pos + 3])); |
|
417 | - $t[$pos + 2] = chr($a ^ $b ^ $c ^ $d); |
|
418 | - |
|
419 | - $a = $this->mixColumnMultiply($m[3], ord($tmp[$pos + 0])); |
|
420 | - $b = $this->mixColumnMultiply($m[7], ord($tmp[$pos + 1])); |
|
421 | - $c = $this->mixColumnMultiply($m[11], ord($tmp[$pos + 2])); |
|
422 | - $d = $this->mixColumnMultiply($m[15], ord($tmp[$pos + 3])); |
|
423 | - $t[$pos + 3] = chr($a ^ $b ^ $c ^ $d); |
|
424 | - } |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * Convert the 16, 24, or 32 bytes of $text into a matrix, and shift each row |
|
430 | - * n-bytes left for encryptiong, n-bytes right if we are decrypting. |
|
431 | - * Row shifts depend on the bit size of the block $text. |
|
432 | - * Rijndael-128/AES: 4x4 matrix |
|
433 | - * Rijndael-192: 6x4 matrix |
|
434 | - * Rijndael-256: 8x4 matrix |
|
435 | - * |
|
436 | - * @param string $text A 16, 24, or 32 byte string |
|
437 | - * @return void |
|
438 | - */ |
|
439 | - private function shiftRow(&$text) |
|
440 | - { |
|
441 | - /* |
|
91 | + /** |
|
92 | + * @type array $_gm2 The Galois Multiplication table |
|
93 | + * for muliplying by 2, this should be considered a constant |
|
94 | + */ |
|
95 | + private static $_gm2 = array(); |
|
96 | + |
|
97 | + /** |
|
98 | + * @type array $_gm3 The Galois Multiplication table |
|
99 | + * for muliplying by 3, this should be considered a constant |
|
100 | + */ |
|
101 | + private static $_gm3 = array(); |
|
102 | + |
|
103 | + /** |
|
104 | + * @type array $_gm9 The Galois Multiplication table |
|
105 | + * for muliplying by 9, this should be considered a constant |
|
106 | + */ |
|
107 | + private static $_gm9 = array(); |
|
108 | + |
|
109 | + /** |
|
110 | + * @type array $_gm11 The Galois Multiplication table |
|
111 | + * for muliplying by 11, this should be considered a constant |
|
112 | + */ |
|
113 | + private static $_gm11 = array(); |
|
114 | + |
|
115 | + /** |
|
116 | + * @type array $_gm13 The Galois Multiplication table |
|
117 | + * for muliplying by 13, this should be considered a constant |
|
118 | + */ |
|
119 | + private static $_gm13 = array(); |
|
120 | + |
|
121 | + /** |
|
122 | + * @type array $_gm14 The Galois Multiplication table |
|
123 | + * for muliplying by 14, this should be considered a constant |
|
124 | + */ |
|
125 | + private static $_gm14 = array(); |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Constructor |
|
130 | + * Sets the key used for encryption. Also sets the requied block size |
|
131 | + * |
|
132 | + * @param string The cipher name as set in a constant in the child class |
|
133 | + * @param string $key string containing the user supplied encryption key |
|
134 | + * @param integer $len Optional, the key size in bytes - used only by the AES child classes |
|
135 | + * @return void |
|
136 | + */ |
|
137 | + public function __construct($cipher_name, $key, $len = 0) |
|
138 | + { |
|
139 | + // AES will pass in a $len, since it has a fixed key size, other |
|
140 | + // rijndael implementations can use variable key sizes, supported |
|
141 | + // sizes are stored in self::$_key_sizes |
|
142 | + if ($len == 0) |
|
143 | + { |
|
144 | + // the key must be one of the following lengths: 16, 24, 32 bytes |
|
145 | + $len = strlen($key); |
|
146 | + if (!in_array($len, self::$_key_sizes)) |
|
147 | + { |
|
148 | + $msg = "Incorrect key length for ".strtoupper($cipher_name).". "; |
|
149 | + $msg .= "Received $len bytes."; |
|
150 | + trigger_error($msg, E_USER_WARNING); |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + // Setup the key |
|
155 | + parent::__construct($cipher_name, $key, $len); |
|
156 | + |
|
157 | + // initialize the tables used for rijndael/aes |
|
158 | + $this->initTables(); |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * Destructor |
|
164 | + * |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + public function __destruct() |
|
168 | + { |
|
169 | + parent::__destruct(); |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * Performs Rijndael encryption |
|
175 | + * |
|
176 | + * @param string @text The string to encrypt |
|
177 | + * @return boolean Returns true |
|
178 | + */ |
|
179 | + public function encrypt(&$text) |
|
180 | + { |
|
181 | + // set the operation to decryption |
|
182 | + $this->operation(parent::ENCRYPT); |
|
183 | + |
|
184 | + $loops = 0; |
|
185 | + $key_sz = $this->keySize(); |
|
186 | + $blk_sz = $this->blockSize(); |
|
187 | + |
|
188 | + // if the key and block size is 16, do 10 rounds |
|
189 | + // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
190 | + // if either key or block size is 32, do 14 rounds |
|
191 | + if ($key_sz == 16 && $blk_sz == 16) |
|
192 | + $loops = 10; |
|
193 | + else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
194 | + $loops = 12; |
|
195 | + else if ($key_sz == 32 || $blk_sz == 32) |
|
196 | + $loops = 14; |
|
197 | + |
|
198 | + // now begin the encryption |
|
199 | + $this->addRoundKey($text, 0); |
|
200 | + |
|
201 | + for ($i = 1; $i <= $loops; ++$i) |
|
202 | + { |
|
203 | + $this->byteSub($text); |
|
204 | + $this->shiftRow($text); |
|
205 | + |
|
206 | + // the last iteration does not use mixColumn |
|
207 | + if ($i < $loops) |
|
208 | + $this->mixColumn($text); |
|
209 | + |
|
210 | + $this->addRoundKey($text, $i); |
|
211 | + } |
|
212 | + |
|
213 | + return true; |
|
214 | + } |
|
215 | + |
|
216 | + |
|
217 | + /** |
|
218 | + * Performs Rijndael decryption |
|
219 | + * |
|
220 | + * @param string @text The string to decrypt |
|
221 | + * @return boolean Returns true |
|
222 | + */ |
|
223 | + public function decrypt(&$text) |
|
224 | + { |
|
225 | + // set the operation to decryption |
|
226 | + $this->operation(parent::DECRYPT); |
|
227 | + |
|
228 | + $loops = 0; |
|
229 | + $key_sz = $this->keySize(); |
|
230 | + $blk_sz = $this->blockSize(); |
|
231 | + |
|
232 | + // if the key and block size is 16, do 10 rounds |
|
233 | + // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
|
234 | + // if either key or block size is 32, do 14 rounds |
|
235 | + if ($key_sz == 16 && $blk_sz == 16) |
|
236 | + $loops = 10; |
|
237 | + else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
238 | + $loops = 12; |
|
239 | + else if ($key_sz == 32 || $blk_sz == 32) |
|
240 | + $loops = 14; |
|
241 | + |
|
242 | + // now begin the decryption |
|
243 | + $this->addRoundKey($text, 0); |
|
244 | + |
|
245 | + for ($i = 1; $i <= $loops; ++$i) |
|
246 | + { |
|
247 | + $this->shiftRow($text); |
|
248 | + $this->byteSub($text); |
|
249 | + $this->addRoundKey($text, $i); |
|
250 | + |
|
251 | + // the last iteration does not use mixColumn |
|
252 | + if ($i < $loops) |
|
253 | + $this->mixColumn($text); |
|
254 | + } |
|
255 | + |
|
256 | + return true; |
|
257 | + } |
|
258 | + |
|
259 | + |
|
260 | + /** |
|
261 | + * Indicates that this is a block cipher |
|
262 | + * |
|
263 | + * @return integer Returns Cipher::BLOCK |
|
264 | + */ |
|
265 | + public function type() |
|
266 | + { |
|
267 | + return parent::BLOCK; |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * Do the multiplication required in mixColumn() |
|
273 | + * We follow the description the multiplication from Wikipedia: |
|
274 | + * http://en.wikipedia.org/wiki/Advanced_Encryption_Standard#The_MixColumns_step |
|
275 | + * |
|
276 | + * @param integer $m A value from self::$_matrix_mult or self::$_matrix_mult_inv |
|
277 | + * @param integer $byte The value to multipy by $m |
|
278 | + * @return integer The result of the multiplication |
|
279 | + */ |
|
280 | + protected function mixColumnMultiply($m, $byte) |
|
281 | + { |
|
282 | + // if multiplying by 1, then we just return the same number |
|
283 | + if ($m == 0x01) |
|
284 | + return $byte; |
|
285 | + |
|
286 | + $hex = parent::dec2Hex($byte); |
|
287 | + $row = parent::hex2Dec($hex[0]); |
|
288 | + $col = parent::hex2Dec($hex[1]); |
|
289 | + $pos = ($row * 16) + $col; |
|
290 | + |
|
291 | + // multiply by 2 (comes from self::$_matrix_mult during encryption) |
|
292 | + if ($m == 0x02) |
|
293 | + return self::$_gm2[$pos]; |
|
294 | + |
|
295 | + // multiply by 3 (comes from self::$_matrix_mult during encryption) |
|
296 | + if ($m == 0x03) |
|
297 | + return self::$_gm3[$pos]; |
|
298 | + |
|
299 | + // multiply by 9 (comes from self::$_matrix_mult_inv during decryption) |
|
300 | + if ($m == 0x09) |
|
301 | + return self::$_gm9[$pos]; |
|
302 | + |
|
303 | + // multiply by 11 (comes from self::$_matrix_mult_inv during decryption) |
|
304 | + if ($m == 0x0b) |
|
305 | + return self::$_gm11[$pos]; |
|
306 | + |
|
307 | + // multiply by 13 (comes from self::$_matrix_mult_inv during decryption) |
|
308 | + if ($m == 0x0d) |
|
309 | + return self::$_gm13[$pos]; |
|
310 | + |
|
311 | + // multiply by 14 (comes from self::$_matrix_mult_inv during decryption) |
|
312 | + if ($m == 0x0e) |
|
313 | + return self::$_gm14[$pos]; |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * Each time this function is called, it XORs the 16 bytes of $text |
|
319 | + * with the next 16 bytes from the $this->xkey. The expanded key |
|
320 | + * never has the same bytes used twice. Note that the first time |
|
321 | + * addRoundKey() is called, it will be outside of the rounds, so no |
|
322 | + * $round is given. Every call after that will be inside the rounds. |
|
323 | + * |
|
324 | + * @param string $text The text to encrypt/decrypt |
|
325 | + * @param integer $round The round we are on inside of rijndael() |
|
326 | + * @return void |
|
327 | + */ |
|
328 | + private function addRoundKey(&$text, $round) |
|
329 | + { |
|
330 | + // length of the xkey |
|
331 | + $ek_len = strlen($this->xkey); |
|
332 | + $len = $this->blockSize(); |
|
333 | + |
|
334 | + if ($this->operation() == parent::ENCRYPT) |
|
335 | + $offset = $round * $len; |
|
336 | + else |
|
337 | + $offset = ($ek_len - ($round * $len)) - $len; |
|
338 | + |
|
339 | + for ($i = 0; $i < $len; ++$i) |
|
340 | + $text[$i] = $text[$i] ^ $this->xkey[$offset + $i]; |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * Applies the Sbox to each byte of the string passed in |
|
346 | + * This is used in key expansione |
|
347 | + * |
|
348 | + * @param string $text The string to peform the byte subsitution |
|
349 | + * @return void |
|
350 | + */ |
|
351 | + private function byteSub(&$text) |
|
352 | + { |
|
353 | + $max = strlen($text); |
|
354 | + for ($i = 0; $i < $max; ++$i) |
|
355 | + { |
|
356 | + // the sbox is arrange in a 16 x 16 grid, where each row |
|
357 | + // and column is numbered in hex (from 0 - f) |
|
358 | + $hex = parent::str2Hex($text[$i]); |
|
359 | + $row = parent::hex2Dec($hex[0]); |
|
360 | + $col = parent::hex2Dec($hex[1]); |
|
361 | + $pos = ($row * 16) + $col; |
|
362 | + |
|
363 | + // return the corresponding value from the sbox |
|
364 | + if ($this->operation() == parent::ENCRYPT) |
|
365 | + $text[$i] = chr(self::$_s[$pos]); |
|
366 | + else // parent::DECRYPT uses the inverse sbox |
|
367 | + $text[$i] = chr(self::$_s_inv[$pos]); |
|
368 | + } |
|
369 | + } |
|
370 | + |
|
371 | + |
|
372 | + /** |
|
373 | + * This function is hard to explain, the easiest way to understand it is to read |
|
374 | + * http://www.net-security.org/dl/articles/AESbyExample.pdf, Section 5.4 |
|
375 | + * |
|
376 | + * @param string $t The string to multiply bytes by the Galois Multiplication lookup tables |
|
377 | + * @return void |
|
378 | + */ |
|
379 | + private function mixColumn(&$t) |
|
380 | + { |
|
381 | + $tmp = $t; |
|
382 | + |
|
383 | + // the matrix we use depends on if we are encrypting or decrypting |
|
384 | + if ($this->operation() == parent::ENCRYPT) |
|
385 | + $m = self::$_matrix_mult; |
|
386 | + else // parent::DECRYPT |
|
387 | + $m = self::$_matrix_mult_inv; |
|
388 | + |
|
389 | + // the number of rounds we make depends on the block size of the text |
|
390 | + // used during encryption/decryption |
|
391 | + // 128 has a 4x4 matrix, loop 4 times |
|
392 | + // 192 has a 4x6 matrix, loop 6 times |
|
393 | + // 256 has a 4x8 matrix, loop 8 times |
|
394 | + $max_col = ($this->blockSize() * 8) / 32; |
|
395 | + |
|
396 | + // loop through each column of the matrix |
|
397 | + for ($col = 0; $col < $max_col; ++$col) |
|
398 | + { |
|
399 | + $pos = $col * 4; |
|
400 | + |
|
401 | + $a = $this->mixColumnMultiply($m[0], ord($tmp[$pos + 0])); |
|
402 | + $b = $this->mixColumnMultiply($m[4], ord($tmp[$pos + 1])); |
|
403 | + $c = $this->mixColumnMultiply($m[8], ord($tmp[$pos + 2])); |
|
404 | + $d = $this->mixColumnMultiply($m[12], ord($tmp[$pos + 3])); |
|
405 | + $t[$pos + 0] = chr($a ^ $b ^ $c ^ $d); |
|
406 | + |
|
407 | + $a = $this->mixColumnMultiply($m[1], ord($tmp[$pos + 0])); |
|
408 | + $b = $this->mixColumnMultiply($m[5], ord($tmp[$pos + 1])); |
|
409 | + $c = $this->mixColumnMultiply($m[9], ord($tmp[$pos + 2])); |
|
410 | + $d = $this->mixColumnMultiply($m[13], ord($tmp[$pos + 3])); |
|
411 | + $t[$pos + 1] = chr($a ^ $b ^ $c ^ $d); |
|
412 | + |
|
413 | + $a = $this->mixColumnMultiply($m[2], ord($tmp[$pos + 0])); |
|
414 | + $b = $this->mixColumnMultiply($m[6], ord($tmp[$pos + 1])); |
|
415 | + $c = $this->mixColumnMultiply($m[10], ord($tmp[$pos + 2])); |
|
416 | + $d = $this->mixColumnMultiply($m[14], ord($tmp[$pos + 3])); |
|
417 | + $t[$pos + 2] = chr($a ^ $b ^ $c ^ $d); |
|
418 | + |
|
419 | + $a = $this->mixColumnMultiply($m[3], ord($tmp[$pos + 0])); |
|
420 | + $b = $this->mixColumnMultiply($m[7], ord($tmp[$pos + 1])); |
|
421 | + $c = $this->mixColumnMultiply($m[11], ord($tmp[$pos + 2])); |
|
422 | + $d = $this->mixColumnMultiply($m[15], ord($tmp[$pos + 3])); |
|
423 | + $t[$pos + 3] = chr($a ^ $b ^ $c ^ $d); |
|
424 | + } |
|
425 | + } |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * Convert the 16, 24, or 32 bytes of $text into a matrix, and shift each row |
|
430 | + * n-bytes left for encryptiong, n-bytes right if we are decrypting. |
|
431 | + * Row shifts depend on the bit size of the block $text. |
|
432 | + * Rijndael-128/AES: 4x4 matrix |
|
433 | + * Rijndael-192: 6x4 matrix |
|
434 | + * Rijndael-256: 8x4 matrix |
|
435 | + * |
|
436 | + * @param string $text A 16, 24, or 32 byte string |
|
437 | + * @return void |
|
438 | + */ |
|
439 | + private function shiftRow(&$text) |
|
440 | + { |
|
441 | + /* |
|
442 | 442 | * Rijndael-128 / AES |
443 | 443 | */ |
444 | - if ($this->blockSize() == 16) |
|
445 | - { |
|
446 | - if ($this->operation() == parent::ENCRYPT) |
|
447 | - { |
|
448 | - // create a 4x4 matrix |
|
449 | - // row 0 is unchanged, |
|
450 | - // shift row 1 left 1 byte |
|
451 | - // shift row 2 left 2 bytes |
|
452 | - // shift row 3 left 3 bytes |
|
453 | - $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
454 | - $text[14].$text[3].$text[8].$text[13].$text[2].$text[7]. |
|
455 | - $text[12].$text[1].$text[6].$text[11]; |
|
456 | - } else // parent::DECRYPT |
|
457 | - { |
|
458 | - // create a 4x4 matrix |
|
459 | - // row 0 is unchanged, |
|
460 | - // shift row 1 right 1 byte |
|
461 | - // shift row 2 right 2 bytes |
|
462 | - // shift row 3 right 3 bytes |
|
463 | - $text = $text[0].$text[13].$text[10].$text[7].$text[4].$text[1]. |
|
464 | - $text[14].$text[11].$text[8].$text[5].$text[2].$text[15]. |
|
465 | - $text[12].$text[9].$text[6].$text[3]; |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - /* |
|
444 | + if ($this->blockSize() == 16) |
|
445 | + { |
|
446 | + if ($this->operation() == parent::ENCRYPT) |
|
447 | + { |
|
448 | + // create a 4x4 matrix |
|
449 | + // row 0 is unchanged, |
|
450 | + // shift row 1 left 1 byte |
|
451 | + // shift row 2 left 2 bytes |
|
452 | + // shift row 3 left 3 bytes |
|
453 | + $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
454 | + $text[14].$text[3].$text[8].$text[13].$text[2].$text[7]. |
|
455 | + $text[12].$text[1].$text[6].$text[11]; |
|
456 | + } else // parent::DECRYPT |
|
457 | + { |
|
458 | + // create a 4x4 matrix |
|
459 | + // row 0 is unchanged, |
|
460 | + // shift row 1 right 1 byte |
|
461 | + // shift row 2 right 2 bytes |
|
462 | + // shift row 3 right 3 bytes |
|
463 | + $text = $text[0].$text[13].$text[10].$text[7].$text[4].$text[1]. |
|
464 | + $text[14].$text[11].$text[8].$text[5].$text[2].$text[15]. |
|
465 | + $text[12].$text[9].$text[6].$text[3]; |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + /* |
|
470 | 470 | * Rijndael-192 |
471 | 471 | */ |
472 | - if ($this->blockSize() == 24) |
|
473 | - { |
|
474 | - if ($this->operation() == parent::ENCRYPT) |
|
475 | - { |
|
476 | - // create a 6x4 matrix |
|
477 | - // row 0 is unchanged |
|
478 | - // shift row 1 left 1 byte |
|
479 | - // shift row 2 left 2 bytes |
|
480 | - // shift row 3 left 3 bytes |
|
481 | - $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
482 | - $text[14].$text[19].$text[8].$text[13].$text[18].$text[23]. |
|
483 | - $text[12].$text[17].$text[22].$text[3].$text[16].$text[21]. |
|
484 | - $text[2].$text[7].$text[20].$text[1].$text[6].$text[11]; |
|
485 | - |
|
486 | - } else // parent::DECRYPT |
|
487 | - { |
|
488 | - // create a 6x4 matrix |
|
489 | - // row 0 is unchanged |
|
490 | - // shift row 1 right 1 byte |
|
491 | - // shift row 2 right 2 bytes |
|
492 | - // shift row 3 right 3 bytes |
|
493 | - $text = $text[0].$text[21].$text[18].$text[15].$text[4].$text[1]. |
|
494 | - $text[22].$text[19].$text[8].$text[5].$text[2].$text[23]. |
|
495 | - $text[12].$text[9].$text[6].$text[3].$text[16].$text[13]. |
|
496 | - $text[10].$text[7].$text[20].$text[17].$text[14].$text[11]; |
|
497 | - } |
|
498 | - } |
|
499 | - |
|
500 | - /* |
|
472 | + if ($this->blockSize() == 24) |
|
473 | + { |
|
474 | + if ($this->operation() == parent::ENCRYPT) |
|
475 | + { |
|
476 | + // create a 6x4 matrix |
|
477 | + // row 0 is unchanged |
|
478 | + // shift row 1 left 1 byte |
|
479 | + // shift row 2 left 2 bytes |
|
480 | + // shift row 3 left 3 bytes |
|
481 | + $text = $text[0].$text[5].$text[10].$text[15].$text[4].$text[9]. |
|
482 | + $text[14].$text[19].$text[8].$text[13].$text[18].$text[23]. |
|
483 | + $text[12].$text[17].$text[22].$text[3].$text[16].$text[21]. |
|
484 | + $text[2].$text[7].$text[20].$text[1].$text[6].$text[11]; |
|
485 | + |
|
486 | + } else // parent::DECRYPT |
|
487 | + { |
|
488 | + // create a 6x4 matrix |
|
489 | + // row 0 is unchanged |
|
490 | + // shift row 1 right 1 byte |
|
491 | + // shift row 2 right 2 bytes |
|
492 | + // shift row 3 right 3 bytes |
|
493 | + $text = $text[0].$text[21].$text[18].$text[15].$text[4].$text[1]. |
|
494 | + $text[22].$text[19].$text[8].$text[5].$text[2].$text[23]. |
|
495 | + $text[12].$text[9].$text[6].$text[3].$text[16].$text[13]. |
|
496 | + $text[10].$text[7].$text[20].$text[17].$text[14].$text[11]; |
|
497 | + } |
|
498 | + } |
|
499 | + |
|
500 | + /* |
|
501 | 501 | * Rijndael-256 |
502 | 502 | */ |
503 | - if ($this->blockSize() == 32) |
|
504 | - { |
|
505 | - if ($this->operation() == parent::ENCRYPT) |
|
506 | - { |
|
507 | - // create an 8x4 matrix |
|
508 | - // row 0 is unchanged |
|
509 | - // shift row 1 left 1 byte |
|
510 | - // shift row 2 left 3 bytes |
|
511 | - // shift row 3 left 4 bytes |
|
512 | - $text = $text[0].$text[5].$text[14].$text[19].$text[4].$text[9].$text[18]. |
|
513 | - $text[23].$text[8].$text[13].$text[22].$text[27].$text[12].$text[17]. |
|
514 | - $text[26].$text[31].$text[16].$text[21].$text[30].$text[3].$text[20]. |
|
515 | - $text[25].$text[2].$text[7].$text[24].$text[29].$text[6].$text[11]. |
|
516 | - $text[28].$text[1].$text[10].$text[15]; |
|
517 | - } else // parent::DECRYPT |
|
518 | - { |
|
519 | - // create an 8x4 matrix |
|
520 | - // row 0 is unchanged |
|
521 | - // shift row 1 right 1 byte |
|
522 | - // shift row 2 right 3 bytes |
|
523 | - // shift row 3 right 4 bytes |
|
524 | - $text = $text[0].$text[29].$text[22].$text[19].$text[4].$text[1].$text[26]. |
|
525 | - $text[23].$text[8].$text[5].$text[30].$text[27].$text[12].$text[9]. |
|
526 | - $text[2].$text[31].$text[16].$text[13].$text[6].$text[3].$text[20]. |
|
527 | - $text[17].$text[10].$text[7].$text[24].$text[21].$text[14].$text[11]. |
|
528 | - $text[28].$text[25].$text[18].$text[15]; |
|
529 | - } |
|
530 | - } |
|
531 | - } |
|
532 | - |
|
533 | - |
|
534 | - /** |
|
535 | - * Applies the Sbox to each byte of the string passed in. |
|
536 | - * This is similar to subByte(), but Unlike subByte() we do not use |
|
537 | - * the _s_inv[] table. This function is only used in expandKey(), |
|
538 | - * which is implemented by the class that inherits this class |
|
539 | - * |
|
540 | - * @param string $text The string to peform the byte subsitution |
|
541 | - * @return string The string with the subsituted bytes |
|
542 | - */ |
|
543 | - private function subWord(&$text) |
|
544 | - { |
|
545 | - $max = strlen($text); |
|
546 | - for ($i = 0; $i < $max; ++$i) |
|
547 | - { |
|
548 | - // the sbox is arrange in a 16 x 16 grid, where each row |
|
549 | - // and column is numbered in hex (from 0 - f) |
|
550 | - $hex = parent::str2Hex($text[$i]); |
|
551 | - $row = parent::hex2Dec($hex[0]); |
|
552 | - $col = parent::hex2Dec($hex[1]); |
|
553 | - $pos = ($row * 16) + $col; |
|
554 | - |
|
555 | - $text[$i] = chr(self::$_s[$pos]); |
|
556 | - } |
|
557 | - } |
|
558 | - |
|
559 | - |
|
560 | - /** |
|
561 | - * Rotate a 4 byte block of the key, moving the first byte to |
|
562 | - * to the end, and shifting everything left |
|
563 | - * Used in key Expandsion |
|
564 | - * |
|
565 | - * @param string $key_block A 4 byte string |
|
566 | - * @return string The shifted 4 byte string |
|
567 | - */ |
|
568 | - private function rotWord($key_block) |
|
569 | - { |
|
570 | - return substr($key_block, 1, 3).$key_block[0]; |
|
571 | - } |
|
572 | - |
|
573 | - |
|
574 | - /** |
|
575 | - * Returns 4 bytes from the expanded key starting at the given offset |
|
576 | - * Used during expandKey() |
|
577 | - * |
|
578 | - * @param integer $offset The offset within $this->xkey to grab the 4 bytes |
|
579 | - * @return string A 4 byte string from the key |
|
580 | - */ |
|
581 | - private function ek($offset) |
|
582 | - { |
|
583 | - return substr($this->xkey, $offset, 4); |
|
584 | - } |
|
585 | - |
|
586 | - |
|
587 | - /** |
|
588 | - * Returns 4 bytes of the original key from the given offset |
|
589 | - * Used during expandKey() |
|
590 | - * |
|
591 | - * @param integer $offset The offset within $this->key to grab the 4 bytes |
|
592 | - * @return string A 4 byte string from the key |
|
593 | - */ |
|
594 | - private function k($offset) |
|
595 | - { |
|
596 | - return substr($this->key(), $offset, 4); |
|
597 | - } |
|
598 | - |
|
599 | - |
|
600 | - /** |
|
601 | - * Return the 4 byte round constant used during expandKey(). |
|
602 | - * Gets the 1 byte value from self::$_rcon and multiplies it by |
|
603 | - * 0x01000000 to create a 4 byte value |
|
604 | - * |
|
605 | - * @param integer $pos The position in self::$_rcon array to grab 1 byte |
|
606 | - * @return integer A 4 byte value |
|
607 | - */ |
|
608 | - private function rcon($pos) |
|
609 | - { |
|
610 | - return (self::$_rcon[$pos] * 0x01000000); |
|
611 | - } |
|
612 | - |
|
613 | - |
|
614 | - /** |
|
615 | - * Expands the key |
|
616 | - * The key expands based on the block size as well as the key size |
|
617 | - * |
|
618 | - * @return boolean|null |
|
619 | - */ |
|
620 | - protected function expandKey() |
|
621 | - { |
|
622 | - if ($this->keySize() == 16) |
|
623 | - return $this->expandKey128(); |
|
624 | - else if ($this->keySize() == 24) |
|
625 | - return $this->expandKey192(); |
|
626 | - else if ($this->keySize() == 32) |
|
627 | - return $this->expandKey256(); |
|
628 | - } |
|
629 | - |
|
630 | - |
|
631 | - /** |
|
632 | - * Expand a 16 byte key, the size it is expanded to varies |
|
633 | - * based on the block size of the Rijndael implementation chosen |
|
634 | - * |
|
635 | - * @return boolean |
|
636 | - */ |
|
637 | - private function expandKey128() |
|
638 | - { |
|
639 | - // clear the xkey, we're creating a new one |
|
640 | - $this->xkey = ""; |
|
641 | - $max = 0; |
|
642 | - |
|
643 | - // the number of rounds we make depends on the block size of the text |
|
644 | - // used during encryption/decryption |
|
645 | - if ($this->blockSize() == 16) |
|
646 | - $max = 44; |
|
647 | - if ($this->blockSize() == 24) |
|
648 | - $max = 78; |
|
649 | - if ($this->blockSize() == 32) |
|
650 | - $max = 120; |
|
651 | - |
|
652 | - // 16 byte key expands to 176 bytes |
|
653 | - for ($i = 0; $i < $max; ++$i) |
|
654 | - { |
|
655 | - if ($i >= 0 && $i <= 3) |
|
656 | - $this->xkey .= $this->k($i * 4); |
|
657 | - else if (($i % 4) == 0) |
|
658 | - { |
|
659 | - // rotate the 4 bytes |
|
660 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
661 | - |
|
662 | - // apply the sbox |
|
663 | - $this->subWord($subword); |
|
664 | - |
|
665 | - // return 4 byte value based on self::$_rcon table |
|
666 | - //$rcon = $this->rcon(($i / 4) - 1); |
|
667 | - $rcon = $this->rcon(($i / 4)); |
|
668 | - |
|
669 | - // grab 4 bytes from $this->extended_key |
|
670 | - $ek = $this->ek(($i - 4) * 4); |
|
671 | - |
|
672 | - $h1 = parent::str2Hex($subword); |
|
673 | - $h2 = parent::dec2Hex($rcon); |
|
674 | - $h3 = parent::str2Hex($ek); |
|
675 | - $res = parent::xorHex($h1, $h2, $h3); |
|
676 | - $this->xkey .= parent::hex2Str($res); |
|
677 | - } else |
|
678 | - { |
|
679 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
680 | - $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
|
681 | - $res = parent::xorHex($h1, $h2); |
|
682 | - $this->xkey .= parent::hex2Str($res); |
|
683 | - } |
|
684 | - } |
|
685 | - |
|
686 | - return true; |
|
687 | - } |
|
688 | - |
|
689 | - |
|
690 | - /** |
|
691 | - * Expand a 24 byte key, the size it is expanded to varies |
|
692 | - * based on the block size of the Rijndael implementation chosen |
|
693 | - * |
|
694 | - * @return boolean |
|
695 | - */ |
|
696 | - private function expandKey192() |
|
697 | - { |
|
698 | - // clear the xkey, we're creating a new one |
|
699 | - $this->xkey = ""; |
|
700 | - $max = 0; |
|
701 | - |
|
702 | - // the number of rounds we make depends on the block size of the text |
|
703 | - // used during encryption/decryption |
|
704 | - if ($this->blockSize() == 16) |
|
705 | - $max = 52; |
|
706 | - if ($this->blockSize() == 24) |
|
707 | - $max = 78; |
|
708 | - if ($this->blockSize() == 32) |
|
709 | - $max = 120; |
|
710 | - |
|
711 | - // 24 byte key expands to 208 bytes |
|
712 | - for ($i = 0; $i < $max; ++$i) |
|
713 | - { |
|
714 | - if ($i >= 0 && $i <= 5) |
|
715 | - $this->xkey .= $this->k($i * 4); |
|
716 | - else if (($i % 6) == 0) |
|
717 | - { |
|
718 | - // rotate the 4 bytes |
|
719 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
720 | - |
|
721 | - // apply the sbox |
|
722 | - $this->subWord($subword); |
|
723 | - |
|
724 | - // return 4 byte value based on self::$_rcon table |
|
725 | - //$rcon = $this->rcon(($i / 6) - 1); |
|
726 | - $rcon = $this->rcon(($i / 6)); |
|
727 | - |
|
728 | - // grab 4 bytes from $this->extended_key |
|
729 | - $ek = $this->ek(($i - 6) * 4); |
|
730 | - |
|
731 | - $h1 = parent::str2Hex($subword); |
|
732 | - $h2 = parent::dec2Hex($rcon); |
|
733 | - $h3 = parent::str2Hex($ek); |
|
734 | - $res = parent::xorHex($h1, $h2, $h3); |
|
735 | - $this->xkey .= parent::hex2Str($res); |
|
736 | - } else |
|
737 | - { |
|
738 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
739 | - $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
|
740 | - $res = parent::xorHex($h1, $h2); |
|
741 | - $this->xkey .= parent::hex2Str($res); |
|
742 | - } |
|
743 | - } |
|
744 | - |
|
745 | - return true; |
|
746 | - } |
|
747 | - |
|
748 | - |
|
749 | - /** |
|
750 | - * Expand a 32 byte key, the size it is expanded to varies |
|
751 | - * based on the block size of the Rijndael implementation chosen |
|
752 | - * |
|
753 | - * @return boolean |
|
754 | - */ |
|
755 | - private function expandKey256() |
|
756 | - { |
|
757 | - // clear the xkey, we're creating a new one |
|
758 | - $this->xkey = ""; |
|
759 | - $max = 0; |
|
760 | - |
|
761 | - // the number of rounds we make depends on the block size of the text |
|
762 | - // used during encryption/decryption |
|
763 | - if ($this->blockSize() == 16) |
|
764 | - $max = 60; |
|
765 | - if ($this->blockSize() == 24) |
|
766 | - $max = 90; |
|
767 | - if ($this->blockSize() == 32) |
|
768 | - $max = 120; |
|
769 | - |
|
770 | - // 32 byte key expands to 240 bytes |
|
771 | - for ($i = 0; $i < $max; ++$i) |
|
772 | - { |
|
773 | - if ($i >= 0 && $i <= 7) |
|
774 | - $this->xkey .= $this->k($i * 4); |
|
775 | - else if ($i % 8 == 0) |
|
776 | - { |
|
777 | - // rotate the 4 bytes |
|
778 | - $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
779 | - |
|
780 | - // apply the sbox |
|
781 | - $this->subWord($subword); |
|
782 | - |
|
783 | - // return 4 byte value based on self::$_rcon table |
|
784 | - $rcon = $this->rcon(($i / 8)); |
|
785 | - |
|
786 | - // grab 4 bytes from $this->extended_key |
|
787 | - $ek = $this->ek(($i - 8) * 4); |
|
788 | - |
|
789 | - $h1 = parent::str2Hex($subword); |
|
790 | - $h2 = parent::dec2Hex($rcon); |
|
791 | - $h3 = parent::str2Hex($ek); |
|
792 | - $res = parent::xorHex($h1, $h2, $h3); |
|
793 | - $this->xkey .= parent::hex2Str($res); |
|
794 | - } |
|
795 | - else if ($i % 4 == 0) |
|
796 | - { |
|
797 | - // get the subsitution from the s-box |
|
798 | - $subword = $this->ek(($i - 1) * 4); |
|
799 | - $this->subWord($subword); |
|
800 | - |
|
801 | - // get the extended key part |
|
802 | - $ek = $this->ek(($i - 8) * 4); |
|
803 | - |
|
804 | - // xor the two parts |
|
805 | - $h1 = parent::str2Hex($subword); |
|
806 | - $h2 = parent::str2Hex($ek); |
|
807 | - $res = parent::xorHex($h1, $h2); |
|
808 | - $this->xkey .= parent::hex2Str($res); |
|
809 | - } else |
|
810 | - { |
|
811 | - $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
812 | - $h2 = parent::str2Hex($this->ek(($i - 8) * 4)); |
|
813 | - $res = parent::xorHex($h1, $h2); |
|
814 | - $this->xkey .= parent::hex2Str($res); |
|
815 | - } |
|
816 | - } |
|
817 | - |
|
818 | - return true; |
|
819 | - } |
|
820 | - |
|
821 | - |
|
822 | - /** |
|
823 | - * Initalizes the tables used for Rijndael/AES encryption |
|
824 | - * |
|
825 | - * @return void |
|
826 | - */ |
|
827 | - private function initTables() |
|
828 | - { |
|
829 | - // the sbox used for encryption |
|
830 | - self::$_s = array( |
|
831 | - 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
832 | - 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
833 | - 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
834 | - 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
835 | - 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
836 | - 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
837 | - 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
838 | - 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
839 | - 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
840 | - 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
841 | - 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
842 | - 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
843 | - 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
844 | - 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
845 | - 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
846 | - 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
847 | - ); |
|
848 | - |
|
849 | - // the inverse sbox used for decryption |
|
850 | - self::$_s_inv = array( |
|
851 | - 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
|
852 | - 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
|
853 | - 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
|
854 | - 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
|
855 | - 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
|
856 | - 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
|
857 | - 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
|
858 | - 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
|
859 | - 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
|
860 | - 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
|
861 | - 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
|
862 | - 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
|
863 | - 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
|
864 | - 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
|
865 | - 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
|
866 | - 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d |
|
867 | - ); |
|
868 | - |
|
869 | - // used in mixColumn() during encryption |
|
870 | - self::$_matrix_mult = array( |
|
871 | - 0x02, 0x01, 0x01, 0x03, |
|
872 | - 0x03, 0x02, 0x01, 0x01, |
|
873 | - 0x01, 0x03, 0x02, 0x01, |
|
874 | - 0x01, 0x01, 0x03, 0x02 |
|
875 | - ); |
|
876 | - |
|
877 | - // used in mixColumn() during decryption |
|
878 | - self::$_matrix_mult_inv = array( |
|
879 | - 0x0e, 0x09, 0x0d, 0x0b, |
|
880 | - 0x0b, 0x0e, 0x09, 0x0d, |
|
881 | - 0x0d, 0x0b, 0x0e, 0x09, |
|
882 | - 0x09, 0x0d, 0x0b, 0x0e |
|
883 | - ); |
|
884 | - |
|
885 | - // The round constants, each round is a 1 byte value which should be multiplied by 0x01000000 |
|
886 | - // to create a 4 byte value before being used in expandKey(). This is done in rcon() |
|
887 | - // NOTE: AES only needs the first row of values, since AES only uses 16 byte blocks, |
|
888 | - // the other values are used for larger block/key combinations supported by Rijndael |
|
889 | - // NOTE: self::$_rcon[0] is never used |
|
890 | - self::$_rcon = array( |
|
891 | - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, |
|
892 | - 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, |
|
893 | - 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, |
|
894 | - 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
|
895 | - 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, |
|
896 | - 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, |
|
897 | - 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, |
|
898 | - 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, |
|
899 | - 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, |
|
900 | - 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
|
901 | - 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, |
|
902 | - 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, |
|
903 | - 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, |
|
904 | - 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, |
|
905 | - 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, |
|
906 | - 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d |
|
907 | - ); |
|
908 | - |
|
909 | - /* |
|
503 | + if ($this->blockSize() == 32) |
|
504 | + { |
|
505 | + if ($this->operation() == parent::ENCRYPT) |
|
506 | + { |
|
507 | + // create an 8x4 matrix |
|
508 | + // row 0 is unchanged |
|
509 | + // shift row 1 left 1 byte |
|
510 | + // shift row 2 left 3 bytes |
|
511 | + // shift row 3 left 4 bytes |
|
512 | + $text = $text[0].$text[5].$text[14].$text[19].$text[4].$text[9].$text[18]. |
|
513 | + $text[23].$text[8].$text[13].$text[22].$text[27].$text[12].$text[17]. |
|
514 | + $text[26].$text[31].$text[16].$text[21].$text[30].$text[3].$text[20]. |
|
515 | + $text[25].$text[2].$text[7].$text[24].$text[29].$text[6].$text[11]. |
|
516 | + $text[28].$text[1].$text[10].$text[15]; |
|
517 | + } else // parent::DECRYPT |
|
518 | + { |
|
519 | + // create an 8x4 matrix |
|
520 | + // row 0 is unchanged |
|
521 | + // shift row 1 right 1 byte |
|
522 | + // shift row 2 right 3 bytes |
|
523 | + // shift row 3 right 4 bytes |
|
524 | + $text = $text[0].$text[29].$text[22].$text[19].$text[4].$text[1].$text[26]. |
|
525 | + $text[23].$text[8].$text[5].$text[30].$text[27].$text[12].$text[9]. |
|
526 | + $text[2].$text[31].$text[16].$text[13].$text[6].$text[3].$text[20]. |
|
527 | + $text[17].$text[10].$text[7].$text[24].$text[21].$text[14].$text[11]. |
|
528 | + $text[28].$text[25].$text[18].$text[15]; |
|
529 | + } |
|
530 | + } |
|
531 | + } |
|
532 | + |
|
533 | + |
|
534 | + /** |
|
535 | + * Applies the Sbox to each byte of the string passed in. |
|
536 | + * This is similar to subByte(), but Unlike subByte() we do not use |
|
537 | + * the _s_inv[] table. This function is only used in expandKey(), |
|
538 | + * which is implemented by the class that inherits this class |
|
539 | + * |
|
540 | + * @param string $text The string to peform the byte subsitution |
|
541 | + * @return string The string with the subsituted bytes |
|
542 | + */ |
|
543 | + private function subWord(&$text) |
|
544 | + { |
|
545 | + $max = strlen($text); |
|
546 | + for ($i = 0; $i < $max; ++$i) |
|
547 | + { |
|
548 | + // the sbox is arrange in a 16 x 16 grid, where each row |
|
549 | + // and column is numbered in hex (from 0 - f) |
|
550 | + $hex = parent::str2Hex($text[$i]); |
|
551 | + $row = parent::hex2Dec($hex[0]); |
|
552 | + $col = parent::hex2Dec($hex[1]); |
|
553 | + $pos = ($row * 16) + $col; |
|
554 | + |
|
555 | + $text[$i] = chr(self::$_s[$pos]); |
|
556 | + } |
|
557 | + } |
|
558 | + |
|
559 | + |
|
560 | + /** |
|
561 | + * Rotate a 4 byte block of the key, moving the first byte to |
|
562 | + * to the end, and shifting everything left |
|
563 | + * Used in key Expandsion |
|
564 | + * |
|
565 | + * @param string $key_block A 4 byte string |
|
566 | + * @return string The shifted 4 byte string |
|
567 | + */ |
|
568 | + private function rotWord($key_block) |
|
569 | + { |
|
570 | + return substr($key_block, 1, 3).$key_block[0]; |
|
571 | + } |
|
572 | + |
|
573 | + |
|
574 | + /** |
|
575 | + * Returns 4 bytes from the expanded key starting at the given offset |
|
576 | + * Used during expandKey() |
|
577 | + * |
|
578 | + * @param integer $offset The offset within $this->xkey to grab the 4 bytes |
|
579 | + * @return string A 4 byte string from the key |
|
580 | + */ |
|
581 | + private function ek($offset) |
|
582 | + { |
|
583 | + return substr($this->xkey, $offset, 4); |
|
584 | + } |
|
585 | + |
|
586 | + |
|
587 | + /** |
|
588 | + * Returns 4 bytes of the original key from the given offset |
|
589 | + * Used during expandKey() |
|
590 | + * |
|
591 | + * @param integer $offset The offset within $this->key to grab the 4 bytes |
|
592 | + * @return string A 4 byte string from the key |
|
593 | + */ |
|
594 | + private function k($offset) |
|
595 | + { |
|
596 | + return substr($this->key(), $offset, 4); |
|
597 | + } |
|
598 | + |
|
599 | + |
|
600 | + /** |
|
601 | + * Return the 4 byte round constant used during expandKey(). |
|
602 | + * Gets the 1 byte value from self::$_rcon and multiplies it by |
|
603 | + * 0x01000000 to create a 4 byte value |
|
604 | + * |
|
605 | + * @param integer $pos The position in self::$_rcon array to grab 1 byte |
|
606 | + * @return integer A 4 byte value |
|
607 | + */ |
|
608 | + private function rcon($pos) |
|
609 | + { |
|
610 | + return (self::$_rcon[$pos] * 0x01000000); |
|
611 | + } |
|
612 | + |
|
613 | + |
|
614 | + /** |
|
615 | + * Expands the key |
|
616 | + * The key expands based on the block size as well as the key size |
|
617 | + * |
|
618 | + * @return boolean|null |
|
619 | + */ |
|
620 | + protected function expandKey() |
|
621 | + { |
|
622 | + if ($this->keySize() == 16) |
|
623 | + return $this->expandKey128(); |
|
624 | + else if ($this->keySize() == 24) |
|
625 | + return $this->expandKey192(); |
|
626 | + else if ($this->keySize() == 32) |
|
627 | + return $this->expandKey256(); |
|
628 | + } |
|
629 | + |
|
630 | + |
|
631 | + /** |
|
632 | + * Expand a 16 byte key, the size it is expanded to varies |
|
633 | + * based on the block size of the Rijndael implementation chosen |
|
634 | + * |
|
635 | + * @return boolean |
|
636 | + */ |
|
637 | + private function expandKey128() |
|
638 | + { |
|
639 | + // clear the xkey, we're creating a new one |
|
640 | + $this->xkey = ""; |
|
641 | + $max = 0; |
|
642 | + |
|
643 | + // the number of rounds we make depends on the block size of the text |
|
644 | + // used during encryption/decryption |
|
645 | + if ($this->blockSize() == 16) |
|
646 | + $max = 44; |
|
647 | + if ($this->blockSize() == 24) |
|
648 | + $max = 78; |
|
649 | + if ($this->blockSize() == 32) |
|
650 | + $max = 120; |
|
651 | + |
|
652 | + // 16 byte key expands to 176 bytes |
|
653 | + for ($i = 0; $i < $max; ++$i) |
|
654 | + { |
|
655 | + if ($i >= 0 && $i <= 3) |
|
656 | + $this->xkey .= $this->k($i * 4); |
|
657 | + else if (($i % 4) == 0) |
|
658 | + { |
|
659 | + // rotate the 4 bytes |
|
660 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
661 | + |
|
662 | + // apply the sbox |
|
663 | + $this->subWord($subword); |
|
664 | + |
|
665 | + // return 4 byte value based on self::$_rcon table |
|
666 | + //$rcon = $this->rcon(($i / 4) - 1); |
|
667 | + $rcon = $this->rcon(($i / 4)); |
|
668 | + |
|
669 | + // grab 4 bytes from $this->extended_key |
|
670 | + $ek = $this->ek(($i - 4) * 4); |
|
671 | + |
|
672 | + $h1 = parent::str2Hex($subword); |
|
673 | + $h2 = parent::dec2Hex($rcon); |
|
674 | + $h3 = parent::str2Hex($ek); |
|
675 | + $res = parent::xorHex($h1, $h2, $h3); |
|
676 | + $this->xkey .= parent::hex2Str($res); |
|
677 | + } else |
|
678 | + { |
|
679 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
680 | + $h2 = parent::str2Hex($this->ek(($i - 4) * 4)); |
|
681 | + $res = parent::xorHex($h1, $h2); |
|
682 | + $this->xkey .= parent::hex2Str($res); |
|
683 | + } |
|
684 | + } |
|
685 | + |
|
686 | + return true; |
|
687 | + } |
|
688 | + |
|
689 | + |
|
690 | + /** |
|
691 | + * Expand a 24 byte key, the size it is expanded to varies |
|
692 | + * based on the block size of the Rijndael implementation chosen |
|
693 | + * |
|
694 | + * @return boolean |
|
695 | + */ |
|
696 | + private function expandKey192() |
|
697 | + { |
|
698 | + // clear the xkey, we're creating a new one |
|
699 | + $this->xkey = ""; |
|
700 | + $max = 0; |
|
701 | + |
|
702 | + // the number of rounds we make depends on the block size of the text |
|
703 | + // used during encryption/decryption |
|
704 | + if ($this->blockSize() == 16) |
|
705 | + $max = 52; |
|
706 | + if ($this->blockSize() == 24) |
|
707 | + $max = 78; |
|
708 | + if ($this->blockSize() == 32) |
|
709 | + $max = 120; |
|
710 | + |
|
711 | + // 24 byte key expands to 208 bytes |
|
712 | + for ($i = 0; $i < $max; ++$i) |
|
713 | + { |
|
714 | + if ($i >= 0 && $i <= 5) |
|
715 | + $this->xkey .= $this->k($i * 4); |
|
716 | + else if (($i % 6) == 0) |
|
717 | + { |
|
718 | + // rotate the 4 bytes |
|
719 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
720 | + |
|
721 | + // apply the sbox |
|
722 | + $this->subWord($subword); |
|
723 | + |
|
724 | + // return 4 byte value based on self::$_rcon table |
|
725 | + //$rcon = $this->rcon(($i / 6) - 1); |
|
726 | + $rcon = $this->rcon(($i / 6)); |
|
727 | + |
|
728 | + // grab 4 bytes from $this->extended_key |
|
729 | + $ek = $this->ek(($i - 6) * 4); |
|
730 | + |
|
731 | + $h1 = parent::str2Hex($subword); |
|
732 | + $h2 = parent::dec2Hex($rcon); |
|
733 | + $h3 = parent::str2Hex($ek); |
|
734 | + $res = parent::xorHex($h1, $h2, $h3); |
|
735 | + $this->xkey .= parent::hex2Str($res); |
|
736 | + } else |
|
737 | + { |
|
738 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
739 | + $h2 = parent::str2Hex($this->ek(($i - 6) * 4)); |
|
740 | + $res = parent::xorHex($h1, $h2); |
|
741 | + $this->xkey .= parent::hex2Str($res); |
|
742 | + } |
|
743 | + } |
|
744 | + |
|
745 | + return true; |
|
746 | + } |
|
747 | + |
|
748 | + |
|
749 | + /** |
|
750 | + * Expand a 32 byte key, the size it is expanded to varies |
|
751 | + * based on the block size of the Rijndael implementation chosen |
|
752 | + * |
|
753 | + * @return boolean |
|
754 | + */ |
|
755 | + private function expandKey256() |
|
756 | + { |
|
757 | + // clear the xkey, we're creating a new one |
|
758 | + $this->xkey = ""; |
|
759 | + $max = 0; |
|
760 | + |
|
761 | + // the number of rounds we make depends on the block size of the text |
|
762 | + // used during encryption/decryption |
|
763 | + if ($this->blockSize() == 16) |
|
764 | + $max = 60; |
|
765 | + if ($this->blockSize() == 24) |
|
766 | + $max = 90; |
|
767 | + if ($this->blockSize() == 32) |
|
768 | + $max = 120; |
|
769 | + |
|
770 | + // 32 byte key expands to 240 bytes |
|
771 | + for ($i = 0; $i < $max; ++$i) |
|
772 | + { |
|
773 | + if ($i >= 0 && $i <= 7) |
|
774 | + $this->xkey .= $this->k($i * 4); |
|
775 | + else if ($i % 8 == 0) |
|
776 | + { |
|
777 | + // rotate the 4 bytes |
|
778 | + $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
|
779 | + |
|
780 | + // apply the sbox |
|
781 | + $this->subWord($subword); |
|
782 | + |
|
783 | + // return 4 byte value based on self::$_rcon table |
|
784 | + $rcon = $this->rcon(($i / 8)); |
|
785 | + |
|
786 | + // grab 4 bytes from $this->extended_key |
|
787 | + $ek = $this->ek(($i - 8) * 4); |
|
788 | + |
|
789 | + $h1 = parent::str2Hex($subword); |
|
790 | + $h2 = parent::dec2Hex($rcon); |
|
791 | + $h3 = parent::str2Hex($ek); |
|
792 | + $res = parent::xorHex($h1, $h2, $h3); |
|
793 | + $this->xkey .= parent::hex2Str($res); |
|
794 | + } |
|
795 | + else if ($i % 4 == 0) |
|
796 | + { |
|
797 | + // get the subsitution from the s-box |
|
798 | + $subword = $this->ek(($i - 1) * 4); |
|
799 | + $this->subWord($subword); |
|
800 | + |
|
801 | + // get the extended key part |
|
802 | + $ek = $this->ek(($i - 8) * 4); |
|
803 | + |
|
804 | + // xor the two parts |
|
805 | + $h1 = parent::str2Hex($subword); |
|
806 | + $h2 = parent::str2Hex($ek); |
|
807 | + $res = parent::xorHex($h1, $h2); |
|
808 | + $this->xkey .= parent::hex2Str($res); |
|
809 | + } else |
|
810 | + { |
|
811 | + $h1 = parent::str2Hex($this->ek(($i - 1) * 4)); |
|
812 | + $h2 = parent::str2Hex($this->ek(($i - 8) * 4)); |
|
813 | + $res = parent::xorHex($h1, $h2); |
|
814 | + $this->xkey .= parent::hex2Str($res); |
|
815 | + } |
|
816 | + } |
|
817 | + |
|
818 | + return true; |
|
819 | + } |
|
820 | + |
|
821 | + |
|
822 | + /** |
|
823 | + * Initalizes the tables used for Rijndael/AES encryption |
|
824 | + * |
|
825 | + * @return void |
|
826 | + */ |
|
827 | + private function initTables() |
|
828 | + { |
|
829 | + // the sbox used for encryption |
|
830 | + self::$_s = array( |
|
831 | + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
832 | + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
833 | + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
834 | + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
835 | + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
836 | + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
837 | + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
838 | + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
839 | + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
840 | + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
841 | + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
842 | + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
843 | + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
844 | + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
845 | + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
846 | + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 |
|
847 | + ); |
|
848 | + |
|
849 | + // the inverse sbox used for decryption |
|
850 | + self::$_s_inv = array( |
|
851 | + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
|
852 | + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
|
853 | + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
|
854 | + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
|
855 | + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
|
856 | + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
|
857 | + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
|
858 | + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
|
859 | + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
|
860 | + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
|
861 | + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
|
862 | + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
|
863 | + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
|
864 | + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
|
865 | + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
|
866 | + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d |
|
867 | + ); |
|
868 | + |
|
869 | + // used in mixColumn() during encryption |
|
870 | + self::$_matrix_mult = array( |
|
871 | + 0x02, 0x01, 0x01, 0x03, |
|
872 | + 0x03, 0x02, 0x01, 0x01, |
|
873 | + 0x01, 0x03, 0x02, 0x01, |
|
874 | + 0x01, 0x01, 0x03, 0x02 |
|
875 | + ); |
|
876 | + |
|
877 | + // used in mixColumn() during decryption |
|
878 | + self::$_matrix_mult_inv = array( |
|
879 | + 0x0e, 0x09, 0x0d, 0x0b, |
|
880 | + 0x0b, 0x0e, 0x09, 0x0d, |
|
881 | + 0x0d, 0x0b, 0x0e, 0x09, |
|
882 | + 0x09, 0x0d, 0x0b, 0x0e |
|
883 | + ); |
|
884 | + |
|
885 | + // The round constants, each round is a 1 byte value which should be multiplied by 0x01000000 |
|
886 | + // to create a 4 byte value before being used in expandKey(). This is done in rcon() |
|
887 | + // NOTE: AES only needs the first row of values, since AES only uses 16 byte blocks, |
|
888 | + // the other values are used for larger block/key combinations supported by Rijndael |
|
889 | + // NOTE: self::$_rcon[0] is never used |
|
890 | + self::$_rcon = array( |
|
891 | + 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, |
|
892 | + 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, |
|
893 | + 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, |
|
894 | + 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, |
|
895 | + 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, |
|
896 | + 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, |
|
897 | + 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, |
|
898 | + 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, |
|
899 | + 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, |
|
900 | + 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, |
|
901 | + 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, |
|
902 | + 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, |
|
903 | + 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, |
|
904 | + 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, |
|
905 | + 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, |
|
906 | + 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d |
|
907 | + ); |
|
908 | + |
|
909 | + /* |
|
910 | 910 | * Galois Multiplication lookup tables |
911 | 911 | * See http://en.wikipedia.org/wiki/Rijndael_mix_columns#InverseMixColumns |
912 | 912 | */ |
913 | 913 | |
914 | - // multiply a byte by 2 (the value 2 will come from self::$_matrix_mult) |
|
915 | - self::$_gm2 = array( |
|
916 | - 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e, |
|
917 | - 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, |
|
918 | - 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, |
|
919 | - 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, |
|
920 | - 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, |
|
921 | - 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, |
|
922 | - 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde, |
|
923 | - 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, |
|
924 | - 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, |
|
925 | - 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25, |
|
926 | - 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, |
|
927 | - 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, |
|
928 | - 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85, |
|
929 | - 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, |
|
930 | - 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5, |
|
931 | - 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5 |
|
932 | - ); |
|
933 | - |
|
934 | - // multiply a byte by 3 (the value 3 will come from self::$_matrix_mult) |
|
935 | - self::$_gm3 = array( |
|
936 | - 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11, |
|
937 | - 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, |
|
938 | - 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71, |
|
939 | - 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41, |
|
940 | - 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, |
|
941 | - 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1, |
|
942 | - 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1, |
|
943 | - 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, |
|
944 | - 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, |
|
945 | - 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba, |
|
946 | - 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, |
|
947 | - 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, |
|
948 | - 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a, |
|
949 | - 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, |
|
950 | - 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, |
|
951 | - 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a |
|
952 | - ); |
|
953 | - |
|
954 | - // multiply a byte by 9 (the value 9 will come from self::$_matrix_mult_inv) |
|
955 | - self::$_gm9 = array( |
|
956 | - 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, |
|
957 | - 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, |
|
958 | - 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, |
|
959 | - 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc, |
|
960 | - 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, |
|
961 | - 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91, |
|
962 | - 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a, |
|
963 | - 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, |
|
964 | - 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b, |
|
965 | - 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b, |
|
966 | - 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, |
|
967 | - 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30, |
|
968 | - 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed, |
|
969 | - 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, |
|
970 | - 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6, |
|
971 | - 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46 |
|
972 | - ); |
|
973 | - |
|
974 | - // multiply a byte by 11 (the value 11 will come from self::$_matrix_mult_inv) |
|
975 | - self::$_gm11 = array( |
|
976 | - 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69, |
|
977 | - 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, |
|
978 | - 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12, |
|
979 | - 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2, |
|
980 | - 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, |
|
981 | - 0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f, |
|
982 | - 0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4, |
|
983 | - 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, |
|
984 | - 0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e, |
|
985 | - 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e, |
|
986 | - 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, |
|
987 | - 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, |
|
988 | - 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68, |
|
989 | - 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, |
|
990 | - 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, |
|
991 | - 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3 |
|
992 | - ); |
|
993 | - |
|
994 | - // multiply a byte by 13 (the value 13 will come from self::$_matrix_mult_inv) |
|
995 | - self::$_gm13 = array( |
|
996 | - 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b, |
|
997 | - 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, |
|
998 | - 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, |
|
999 | - 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20, |
|
1000 | - 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, |
|
1001 | - 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6, |
|
1002 | - 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d, |
|
1003 | - 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, |
|
1004 | - 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91, |
|
1005 | - 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41, |
|
1006 | - 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, |
|
1007 | - 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa, |
|
1008 | - 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc, |
|
1009 | - 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, |
|
1010 | - 0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47, |
|
1011 | - 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97 |
|
1012 | - ); |
|
1013 | - |
|
1014 | - // multiply a byte by 14 (the value 14 will come from self::$_matrix_mult_inv) |
|
1015 | - self::$_gm14 = array( |
|
1016 | - 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a, |
|
1017 | - 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, |
|
1018 | - 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81, |
|
1019 | - 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61, |
|
1020 | - 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, |
|
1021 | - 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17, |
|
1022 | - 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c, |
|
1023 | - 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, |
|
1024 | - 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b, |
|
1025 | - 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb, |
|
1026 | - 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, |
|
1027 | - 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, |
|
1028 | - 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6, |
|
1029 | - 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, |
|
1030 | - 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, |
|
1031 | - 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d |
|
1032 | - ); |
|
1033 | - } |
|
914 | + // multiply a byte by 2 (the value 2 will come from self::$_matrix_mult) |
|
915 | + self::$_gm2 = array( |
|
916 | + 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e, |
|
917 | + 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e, |
|
918 | + 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, |
|
919 | + 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e, |
|
920 | + 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e, |
|
921 | + 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe, |
|
922 | + 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde, |
|
923 | + 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, |
|
924 | + 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05, |
|
925 | + 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25, |
|
926 | + 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45, |
|
927 | + 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65, |
|
928 | + 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85, |
|
929 | + 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5, |
|
930 | + 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5, |
|
931 | + 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5 |
|
932 | + ); |
|
933 | + |
|
934 | + // multiply a byte by 3 (the value 3 will come from self::$_matrix_mult) |
|
935 | + self::$_gm3 = array( |
|
936 | + 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11, |
|
937 | + 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21, |
|
938 | + 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71, |
|
939 | + 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41, |
|
940 | + 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1, |
|
941 | + 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1, |
|
942 | + 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1, |
|
943 | + 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81, |
|
944 | + 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a, |
|
945 | + 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba, |
|
946 | + 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea, |
|
947 | + 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda, |
|
948 | + 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a, |
|
949 | + 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, |
|
950 | + 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a, |
|
951 | + 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a |
|
952 | + ); |
|
953 | + |
|
954 | + // multiply a byte by 9 (the value 9 will come from self::$_matrix_mult_inv) |
|
955 | + self::$_gm9 = array( |
|
956 | + 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77, |
|
957 | + 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, |
|
958 | + 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, |
|
959 | + 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc, |
|
960 | + 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, |
|
961 | + 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91, |
|
962 | + 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a, |
|
963 | + 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, |
|
964 | + 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b, |
|
965 | + 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b, |
|
966 | + 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, |
|
967 | + 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30, |
|
968 | + 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed, |
|
969 | + 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, |
|
970 | + 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6, |
|
971 | + 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46 |
|
972 | + ); |
|
973 | + |
|
974 | + // multiply a byte by 11 (the value 11 will come from self::$_matrix_mult_inv) |
|
975 | + self::$_gm11 = array( |
|
976 | + 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69, |
|
977 | + 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9, |
|
978 | + 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12, |
|
979 | + 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2, |
|
980 | + 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f, |
|
981 | + 0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f, |
|
982 | + 0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4, |
|
983 | + 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54, |
|
984 | + 0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e, |
|
985 | + 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e, |
|
986 | + 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5, |
|
987 | + 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55, |
|
988 | + 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68, |
|
989 | + 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, |
|
990 | + 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13, |
|
991 | + 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3 |
|
992 | + ); |
|
993 | + |
|
994 | + // multiply a byte by 13 (the value 13 will come from self::$_matrix_mult_inv) |
|
995 | + self::$_gm13 = array( |
|
996 | + 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b, |
|
997 | + 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, |
|
998 | + 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0, |
|
999 | + 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20, |
|
1000 | + 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26, |
|
1001 | + 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6, |
|
1002 | + 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d, |
|
1003 | + 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d, |
|
1004 | + 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91, |
|
1005 | + 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41, |
|
1006 | + 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a, |
|
1007 | + 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa, |
|
1008 | + 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc, |
|
1009 | + 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c, |
|
1010 | + 0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47, |
|
1011 | + 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97 |
|
1012 | + ); |
|
1013 | + |
|
1014 | + // multiply a byte by 14 (the value 14 will come from self::$_matrix_mult_inv) |
|
1015 | + self::$_gm14 = array( |
|
1016 | + 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a, |
|
1017 | + 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, |
|
1018 | + 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81, |
|
1019 | + 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61, |
|
1020 | + 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, |
|
1021 | + 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17, |
|
1022 | + 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c, |
|
1023 | + 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, |
|
1024 | + 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b, |
|
1025 | + 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb, |
|
1026 | + 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, |
|
1027 | + 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20, |
|
1028 | + 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6, |
|
1029 | + 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, |
|
1030 | + 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d, |
|
1031 | + 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d |
|
1032 | + ); |
|
1033 | + } |
|
1034 | 1034 | } |
1035 | 1035 | ?> |
@@ -188,12 +188,13 @@ discard block |
||
188 | 188 | // if the key and block size is 16, do 10 rounds |
189 | 189 | // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
190 | 190 | // if either key or block size is 32, do 14 rounds |
191 | - if ($key_sz == 16 && $blk_sz == 16) |
|
192 | - $loops = 10; |
|
193 | - else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
194 | - $loops = 12; |
|
195 | - else if ($key_sz == 32 || $blk_sz == 32) |
|
196 | - $loops = 14; |
|
191 | + if ($key_sz == 16 && $blk_sz == 16) { |
|
192 | + $loops = 10; |
|
193 | + } else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) { |
|
194 | + $loops = 12; |
|
195 | + } else if ($key_sz == 32 || $blk_sz == 32) { |
|
196 | + $loops = 14; |
|
197 | + } |
|
197 | 198 | |
198 | 199 | // now begin the encryption |
199 | 200 | $this->addRoundKey($text, 0); |
@@ -204,8 +205,9 @@ discard block |
||
204 | 205 | $this->shiftRow($text); |
205 | 206 | |
206 | 207 | // the last iteration does not use mixColumn |
207 | - if ($i < $loops) |
|
208 | - $this->mixColumn($text); |
|
208 | + if ($i < $loops) { |
|
209 | + $this->mixColumn($text); |
|
210 | + } |
|
209 | 211 | |
210 | 212 | $this->addRoundKey($text, $i); |
211 | 213 | } |
@@ -232,12 +234,13 @@ discard block |
||
232 | 234 | // if the key and block size is 16, do 10 rounds |
233 | 235 | // if the key or block size is 24, and neither is longer than 24, do 12 rounds |
234 | 236 | // if either key or block size is 32, do 14 rounds |
235 | - if ($key_sz == 16 && $blk_sz == 16) |
|
236 | - $loops = 10; |
|
237 | - else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) |
|
238 | - $loops = 12; |
|
239 | - else if ($key_sz == 32 || $blk_sz == 32) |
|
240 | - $loops = 14; |
|
237 | + if ($key_sz == 16 && $blk_sz == 16) { |
|
238 | + $loops = 10; |
|
239 | + } else if (($key_sz == 24 || $blk_sz == 24) && $key_sz <= 24 && $blk_sz <= 24) { |
|
240 | + $loops = 12; |
|
241 | + } else if ($key_sz == 32 || $blk_sz == 32) { |
|
242 | + $loops = 14; |
|
243 | + } |
|
241 | 244 | |
242 | 245 | // now begin the decryption |
243 | 246 | $this->addRoundKey($text, 0); |
@@ -249,8 +252,9 @@ discard block |
||
249 | 252 | $this->addRoundKey($text, $i); |
250 | 253 | |
251 | 254 | // the last iteration does not use mixColumn |
252 | - if ($i < $loops) |
|
253 | - $this->mixColumn($text); |
|
255 | + if ($i < $loops) { |
|
256 | + $this->mixColumn($text); |
|
257 | + } |
|
254 | 258 | } |
255 | 259 | |
256 | 260 | return true; |
@@ -280,8 +284,9 @@ discard block |
||
280 | 284 | protected function mixColumnMultiply($m, $byte) |
281 | 285 | { |
282 | 286 | // if multiplying by 1, then we just return the same number |
283 | - if ($m == 0x01) |
|
284 | - return $byte; |
|
287 | + if ($m == 0x01) { |
|
288 | + return $byte; |
|
289 | + } |
|
285 | 290 | |
286 | 291 | $hex = parent::dec2Hex($byte); |
287 | 292 | $row = parent::hex2Dec($hex[0]); |
@@ -289,28 +294,34 @@ discard block |
||
289 | 294 | $pos = ($row * 16) + $col; |
290 | 295 | |
291 | 296 | // multiply by 2 (comes from self::$_matrix_mult during encryption) |
292 | - if ($m == 0x02) |
|
293 | - return self::$_gm2[$pos]; |
|
297 | + if ($m == 0x02) { |
|
298 | + return self::$_gm2[$pos]; |
|
299 | + } |
|
294 | 300 | |
295 | 301 | // multiply by 3 (comes from self::$_matrix_mult during encryption) |
296 | - if ($m == 0x03) |
|
297 | - return self::$_gm3[$pos]; |
|
302 | + if ($m == 0x03) { |
|
303 | + return self::$_gm3[$pos]; |
|
304 | + } |
|
298 | 305 | |
299 | 306 | // multiply by 9 (comes from self::$_matrix_mult_inv during decryption) |
300 | - if ($m == 0x09) |
|
301 | - return self::$_gm9[$pos]; |
|
307 | + if ($m == 0x09) { |
|
308 | + return self::$_gm9[$pos]; |
|
309 | + } |
|
302 | 310 | |
303 | 311 | // multiply by 11 (comes from self::$_matrix_mult_inv during decryption) |
304 | - if ($m == 0x0b) |
|
305 | - return self::$_gm11[$pos]; |
|
312 | + if ($m == 0x0b) { |
|
313 | + return self::$_gm11[$pos]; |
|
314 | + } |
|
306 | 315 | |
307 | 316 | // multiply by 13 (comes from self::$_matrix_mult_inv during decryption) |
308 | - if ($m == 0x0d) |
|
309 | - return self::$_gm13[$pos]; |
|
317 | + if ($m == 0x0d) { |
|
318 | + return self::$_gm13[$pos]; |
|
319 | + } |
|
310 | 320 | |
311 | 321 | // multiply by 14 (comes from self::$_matrix_mult_inv during decryption) |
312 | - if ($m == 0x0e) |
|
313 | - return self::$_gm14[$pos]; |
|
322 | + if ($m == 0x0e) { |
|
323 | + return self::$_gm14[$pos]; |
|
324 | + } |
|
314 | 325 | } |
315 | 326 | |
316 | 327 | |
@@ -331,13 +342,15 @@ discard block |
||
331 | 342 | $ek_len = strlen($this->xkey); |
332 | 343 | $len = $this->blockSize(); |
333 | 344 | |
334 | - if ($this->operation() == parent::ENCRYPT) |
|
335 | - $offset = $round * $len; |
|
336 | - else |
|
337 | - $offset = ($ek_len - ($round * $len)) - $len; |
|
345 | + if ($this->operation() == parent::ENCRYPT) { |
|
346 | + $offset = $round * $len; |
|
347 | + } else { |
|
348 | + $offset = ($ek_len - ($round * $len)) - $len; |
|
349 | + } |
|
338 | 350 | |
339 | - for ($i = 0; $i < $len; ++$i) |
|
340 | - $text[$i] = $text[$i] ^ $this->xkey[$offset + $i]; |
|
351 | + for ($i = 0; $i < $len; ++$i) { |
|
352 | + $text[$i] = $text[$i] ^ $this->xkey[$offset + $i]; |
|
353 | + } |
|
341 | 354 | } |
342 | 355 | |
343 | 356 | |
@@ -361,10 +374,12 @@ discard block |
||
361 | 374 | $pos = ($row * 16) + $col; |
362 | 375 | |
363 | 376 | // return the corresponding value from the sbox |
364 | - if ($this->operation() == parent::ENCRYPT) |
|
365 | - $text[$i] = chr(self::$_s[$pos]); |
|
366 | - else // parent::DECRYPT uses the inverse sbox |
|
377 | + if ($this->operation() == parent::ENCRYPT) { |
|
378 | + $text[$i] = chr(self::$_s[$pos]); |
|
379 | + } else { |
|
380 | + // parent::DECRYPT uses the inverse sbox |
|
367 | 381 | $text[$i] = chr(self::$_s_inv[$pos]); |
382 | + } |
|
368 | 383 | } |
369 | 384 | } |
370 | 385 | |
@@ -381,10 +396,12 @@ discard block |
||
381 | 396 | $tmp = $t; |
382 | 397 | |
383 | 398 | // the matrix we use depends on if we are encrypting or decrypting |
384 | - if ($this->operation() == parent::ENCRYPT) |
|
385 | - $m = self::$_matrix_mult; |
|
386 | - else // parent::DECRYPT |
|
399 | + if ($this->operation() == parent::ENCRYPT) { |
|
400 | + $m = self::$_matrix_mult; |
|
401 | + } else { |
|
402 | + // parent::DECRYPT |
|
387 | 403 | $m = self::$_matrix_mult_inv; |
404 | + } |
|
388 | 405 | |
389 | 406 | // the number of rounds we make depends on the block size of the text |
390 | 407 | // used during encryption/decryption |
@@ -619,12 +636,13 @@ discard block |
||
619 | 636 | */ |
620 | 637 | protected function expandKey() |
621 | 638 | { |
622 | - if ($this->keySize() == 16) |
|
623 | - return $this->expandKey128(); |
|
624 | - else if ($this->keySize() == 24) |
|
625 | - return $this->expandKey192(); |
|
626 | - else if ($this->keySize() == 32) |
|
627 | - return $this->expandKey256(); |
|
639 | + if ($this->keySize() == 16) { |
|
640 | + return $this->expandKey128(); |
|
641 | + } else if ($this->keySize() == 24) { |
|
642 | + return $this->expandKey192(); |
|
643 | + } else if ($this->keySize() == 32) { |
|
644 | + return $this->expandKey256(); |
|
645 | + } |
|
628 | 646 | } |
629 | 647 | |
630 | 648 | |
@@ -642,19 +660,22 @@ discard block |
||
642 | 660 | |
643 | 661 | // the number of rounds we make depends on the block size of the text |
644 | 662 | // used during encryption/decryption |
645 | - if ($this->blockSize() == 16) |
|
646 | - $max = 44; |
|
647 | - if ($this->blockSize() == 24) |
|
648 | - $max = 78; |
|
649 | - if ($this->blockSize() == 32) |
|
650 | - $max = 120; |
|
663 | + if ($this->blockSize() == 16) { |
|
664 | + $max = 44; |
|
665 | + } |
|
666 | + if ($this->blockSize() == 24) { |
|
667 | + $max = 78; |
|
668 | + } |
|
669 | + if ($this->blockSize() == 32) { |
|
670 | + $max = 120; |
|
671 | + } |
|
651 | 672 | |
652 | 673 | // 16 byte key expands to 176 bytes |
653 | 674 | for ($i = 0; $i < $max; ++$i) |
654 | 675 | { |
655 | - if ($i >= 0 && $i <= 3) |
|
656 | - $this->xkey .= $this->k($i * 4); |
|
657 | - else if (($i % 4) == 0) |
|
676 | + if ($i >= 0 && $i <= 3) { |
|
677 | + $this->xkey .= $this->k($i * 4); |
|
678 | + } else if (($i % 4) == 0) |
|
658 | 679 | { |
659 | 680 | // rotate the 4 bytes |
660 | 681 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
@@ -701,19 +722,22 @@ discard block |
||
701 | 722 | |
702 | 723 | // the number of rounds we make depends on the block size of the text |
703 | 724 | // used during encryption/decryption |
704 | - if ($this->blockSize() == 16) |
|
705 | - $max = 52; |
|
706 | - if ($this->blockSize() == 24) |
|
707 | - $max = 78; |
|
708 | - if ($this->blockSize() == 32) |
|
709 | - $max = 120; |
|
725 | + if ($this->blockSize() == 16) { |
|
726 | + $max = 52; |
|
727 | + } |
|
728 | + if ($this->blockSize() == 24) { |
|
729 | + $max = 78; |
|
730 | + } |
|
731 | + if ($this->blockSize() == 32) { |
|
732 | + $max = 120; |
|
733 | + } |
|
710 | 734 | |
711 | 735 | // 24 byte key expands to 208 bytes |
712 | 736 | for ($i = 0; $i < $max; ++$i) |
713 | 737 | { |
714 | - if ($i >= 0 && $i <= 5) |
|
715 | - $this->xkey .= $this->k($i * 4); |
|
716 | - else if (($i % 6) == 0) |
|
738 | + if ($i >= 0 && $i <= 5) { |
|
739 | + $this->xkey .= $this->k($i * 4); |
|
740 | + } else if (($i % 6) == 0) |
|
717 | 741 | { |
718 | 742 | // rotate the 4 bytes |
719 | 743 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
@@ -760,19 +784,22 @@ discard block |
||
760 | 784 | |
761 | 785 | // the number of rounds we make depends on the block size of the text |
762 | 786 | // used during encryption/decryption |
763 | - if ($this->blockSize() == 16) |
|
764 | - $max = 60; |
|
765 | - if ($this->blockSize() == 24) |
|
766 | - $max = 90; |
|
767 | - if ($this->blockSize() == 32) |
|
768 | - $max = 120; |
|
787 | + if ($this->blockSize() == 16) { |
|
788 | + $max = 60; |
|
789 | + } |
|
790 | + if ($this->blockSize() == 24) { |
|
791 | + $max = 90; |
|
792 | + } |
|
793 | + if ($this->blockSize() == 32) { |
|
794 | + $max = 120; |
|
795 | + } |
|
769 | 796 | |
770 | 797 | // 32 byte key expands to 240 bytes |
771 | 798 | for ($i = 0; $i < $max; ++$i) |
772 | 799 | { |
773 | - if ($i >= 0 && $i <= 7) |
|
774 | - $this->xkey .= $this->k($i * 4); |
|
775 | - else if ($i % 8 == 0) |
|
800 | + if ($i >= 0 && $i <= 7) { |
|
801 | + $this->xkey .= $this->k($i * 4); |
|
802 | + } else if ($i % 8 == 0) |
|
776 | 803 | { |
777 | 804 | // rotate the 4 bytes |
778 | 805 | $subword = $this->rotWord($this->ek(($i - 1) * 4)); |
@@ -791,8 +818,7 @@ discard block |
||
791 | 818 | $h3 = parent::str2Hex($ek); |
792 | 819 | $res = parent::xorHex($h1, $h2, $h3); |
793 | 820 | $this->xkey .= parent::hex2Str($res); |
794 | - } |
|
795 | - else if ($i % 4 == 0) |
|
821 | + } else if ($i % 4 == 0) |
|
796 | 822 | { |
797 | 823 | // get the subsitution from the s-box |
798 | 824 | $subword = $this->ek(($i - 1) * 4); |
@@ -38,1173 +38,1173 @@ |
||
38 | 38 | */ |
39 | 39 | class Cipher_CAST_128 extends Cipher |
40 | 40 | { |
41 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | - const BYTES_BLOCK = 8; // 64 bits; |
|
43 | - |
|
44 | - //const BYTES_KEY = 0; // between 40 - 128 bits |
|
45 | - |
|
46 | - const BYTES_KEY_SMALL = 10; |
|
47 | - const BYTES_KEY_MAX = 16; |
|
48 | - const BYTES_KEY_MIN = 5; |
|
49 | - |
|
50 | - /** @type array $_s1 An array of 256 unsigned integers */ |
|
51 | - private static $_s1 = array(); |
|
52 | - |
|
53 | - /** @type array $_s2 An array of 256 unsigned integers */ |
|
54 | - private static $_s2 = array(); |
|
55 | - |
|
56 | - /** @type array $_s3 An array of 256 unsigned integers */ |
|
57 | - private static $_s3 = array(); |
|
58 | - |
|
59 | - /** @type array $_s4 An array of 256 unsigned integers */ |
|
60 | - private static $_s4 = array(); |
|
61 | - |
|
62 | - /** @type array $_s5 An array of 256 unsigned integers */ |
|
63 | - private static $_s5 = array(); |
|
64 | - |
|
65 | - /** @type array $_s6 An array of 256 unsigned integers */ |
|
66 | - private static $_s6 = array(); |
|
67 | - |
|
68 | - /** @type array $_s7 An array of 256 unsigned integers */ |
|
69 | - private static $_s7 = array(); |
|
70 | - |
|
71 | - /** @type array $_s8 An array of 256 unsigned integers */ |
|
72 | - private static $_s8 = array(); |
|
73 | - |
|
74 | - /** @type string $_mkey The 16 byte masking subkey */ |
|
75 | - private $_mkey = ""; |
|
76 | - |
|
77 | - /** @type string $_rkey The 16 byte rotate subkey */ |
|
78 | - private $_rkey = ""; |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * Constructor |
|
83 | - * |
|
84 | - * @param string $key The key used for Encryption/Decryption |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - public function __construct($key) |
|
88 | - { |
|
89 | - // the length of the key is is between 5 - 16 bytes (40 - 128 bits) |
|
90 | - $keylen = strlen($key); |
|
91 | - if ($keylen > self::BYTES_KEY_MAX) |
|
92 | - { |
|
93 | - $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
94 | - $keylen = self::BYTES_KEY_MAX; |
|
95 | - } |
|
96 | - else if ($keylen < self::BYTES_KEY_MIN) |
|
97 | - { |
|
98 | - $msg = PHP_Crypt::CIPHER_CAST_128." requires a key size between "; |
|
99 | - $msg .= "5 - 16 bytes."; |
|
100 | - trigger_error($msg, E_USER_WARNING); |
|
101 | - } |
|
102 | - |
|
103 | - // set the key, make sure the required length is set in bytes |
|
104 | - parent::__construct(PHP_Crypt::CIPHER_CAST_128, $key, $keylen); |
|
105 | - |
|
106 | - // set the block size |
|
107 | - $this->blockSize(self::BYTES_BLOCK); |
|
108 | - |
|
109 | - // initialize the sboxes constants |
|
110 | - $this->initTables(); |
|
111 | - |
|
112 | - // create the sub keys using the sboxes |
|
113 | - $this->createSubKeys(); |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * Destructor |
|
119 | - * |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function __destruct() |
|
123 | - { |
|
124 | - parent::__destruct(); |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Encrypt plain text data |
|
130 | - * |
|
131 | - * @param string $data A block of plain data |
|
132 | - * @return boolean Returns true |
|
133 | - */ |
|
134 | - public function encrypt(&$data) |
|
135 | - { |
|
136 | - $this->operation(parent::ENCRYPT); |
|
137 | - |
|
138 | - // split the block in half, left and right |
|
139 | - $l = parent::str2Dec(substr($data, 0, 4)); |
|
140 | - $r = parent::str2Dec(substr($data, 4, 4)); |
|
141 | - |
|
142 | - // We do only 12 rounds if we have a key 10 bytes or less. |
|
143 | - // If we have a key greater than 10 bytes, we do the 12 rounds |
|
144 | - // then proceed to the additional 4 rounds for a total of 16 |
|
145 | - for ($i = 0; $i < 12; $i += 3) |
|
146 | - { |
|
147 | - // f1 |
|
148 | - $tmp = $r; |
|
149 | - $r = $l ^ $this->f1($r, $i); |
|
150 | - $l = $tmp; |
|
151 | - |
|
152 | - // f2 |
|
153 | - $tmp = $r; |
|
154 | - $r = $l ^ $this->f2($r, $i + 1); |
|
155 | - $l = $tmp; |
|
156 | - |
|
157 | - // f3 |
|
158 | - $tmp = $r; |
|
159 | - $r = $l ^ $this->f3($r, $i + 2); |
|
160 | - $l = $tmp; |
|
161 | - } |
|
162 | - |
|
163 | - // only do the full 16 rounds if the key is longer than |
|
164 | - // 10 bytes (80 bits) |
|
165 | - if ($this->keySize() > self::BYTES_KEY_SMALL) |
|
166 | - { |
|
167 | - // f1 |
|
168 | - $tmp = $r; |
|
169 | - $r = $l ^ $this->f1($r, 12); |
|
170 | - $l = $tmp; |
|
171 | - |
|
172 | - // f2 |
|
173 | - $tmp = $r; |
|
174 | - $r = $l ^ $this->f2($r, 13); |
|
175 | - $l = $tmp; |
|
176 | - |
|
177 | - // f3 |
|
178 | - $tmp = $r; |
|
179 | - $r = $l ^ $this->f3($r, 14); |
|
180 | - $l = $tmp; |
|
181 | - |
|
182 | - // f1 |
|
183 | - $tmp = $r; |
|
184 | - $r = $l ^ $this->f1($r, 15); |
|
185 | - $l = $tmp; |
|
186 | - } |
|
187 | - |
|
188 | - // swap the two halfs |
|
189 | - $data = parent::dec2Str($r, 4).parent::dec2Str($l, 4); |
|
190 | - |
|
191 | - return true; |
|
192 | - } |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * Decrypt an encrypted string, it does all the steps of encryption, |
|
197 | - * but in reverse. |
|
198 | - * |
|
199 | - * @param string $data A block of encrypted data |
|
200 | - * @return boolean Returns true |
|
201 | - */ |
|
202 | - public function decrypt(&$data) |
|
203 | - { |
|
204 | - $this->operation(parent::DECRYPT); |
|
205 | - |
|
206 | - // split the block in half, left and right |
|
207 | - $l = parent::str2Dec(substr($data, 0, 4)); |
|
208 | - $r = parent::str2Dec(substr($data, 4, 4)); |
|
209 | - |
|
210 | - // only do the full 16 rounds if the key is longer than |
|
211 | - // 10 bytes (80 bits) |
|
212 | - if ($this->keySize() > self::BYTES_KEY_SMALL) |
|
213 | - { |
|
214 | - // f1 |
|
215 | - $tmp = $r; |
|
216 | - $r = $l ^ $this->f1($r, 15); |
|
217 | - $l = $tmp; |
|
218 | - |
|
219 | - // f3 |
|
220 | - $tmp = $r; |
|
221 | - $r = $l ^ $this->f3($r, 14); |
|
222 | - $l = $tmp; |
|
223 | - |
|
224 | - // f2 |
|
225 | - $tmp = $r; |
|
226 | - $r = $l ^ $this->f2($r, 13); |
|
227 | - $l = $tmp; |
|
228 | - |
|
229 | - // f1 |
|
230 | - $tmp = $r; |
|
231 | - $r = $l ^ $this->f1($r, 12); |
|
232 | - $l = $tmp; |
|
233 | - } |
|
234 | - |
|
235 | - // We do only 12 rounds if we have a key 10 bytes or less. |
|
236 | - for ($i = 11; $i >= 2; $i -= 3) |
|
237 | - { |
|
238 | - // f3 |
|
239 | - $tmp = $r; |
|
240 | - $r = $l ^ $this->f3($r, $i); |
|
241 | - $l = $tmp; |
|
242 | - |
|
243 | - // f2 |
|
244 | - $tmp = $r; |
|
245 | - $r = $l ^ $this->f2($r, $i - 1); |
|
246 | - $l = $tmp; |
|
247 | - |
|
248 | - // f1 |
|
249 | - $tmp = $r; |
|
250 | - $r = $l ^ $this->f1($r, $i - 2); |
|
251 | - $l = $tmp; |
|
252 | - } |
|
253 | - |
|
254 | - // swap the two halfs |
|
255 | - $data = parent::dec2Str($r, 4).parent::dec2Str($l, 4); |
|
256 | - |
|
257 | - return true; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * Cast 128 F1 function |
|
263 | - * |
|
264 | - * @param $r integer The right half of the data being encrypted in cast_128() |
|
265 | - * @param integer $i integer The round number (1-16) in cast_128() |
|
266 | - * @return integer The value after the F1 transformation |
|
267 | - */ |
|
268 | - private function f1($r, $i) |
|
269 | - { |
|
270 | - $n = $this->_mkey[$i] + $r; |
|
271 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
272 | - $n = parent::dec2Str($n, 4); |
|
273 | - |
|
274 | - $f = parent::uInt32( |
|
275 | - ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
276 | - self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
277 | - ); |
|
278 | - |
|
279 | - return $f; |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - /** |
|
284 | - * Cast 128 F2 function |
|
285 | - * |
|
286 | - * @param $r integer The right half of the data being encrypted in cast_128() |
|
287 | - * @param integer $i integer The round number (1-16) in cast_128() |
|
288 | - * @return integer The value after the F2 transformation |
|
289 | - */ |
|
290 | - private function f2($r, $i) |
|
291 | - { |
|
292 | - $n = $this->_mkey[$i] ^ $r; |
|
293 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
294 | - $n = parent::dec2Str($n, 4); |
|
295 | - |
|
296 | - $f = parent::uInt32( |
|
297 | - ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
298 | - self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
299 | - ); |
|
300 | - |
|
301 | - return $f; |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * Cast 128 F3 function |
|
307 | - * |
|
308 | - * @param $r integer The right half of the data being encrypted in cast_128() |
|
309 | - * @param integer $i integer The round number (1-16) in cast_128() |
|
310 | - * @return integer The value after the F3 transformation |
|
311 | - */ |
|
312 | - private function f3($r, $i) |
|
313 | - { |
|
314 | - $n = $this->_mkey[$i] - $r; |
|
315 | - $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
316 | - $n = parent::dec2Str($n, 4); |
|
317 | - |
|
318 | - $f = parent::uInt32( |
|
319 | - ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
320 | - self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
321 | - ); |
|
322 | - |
|
323 | - return $f; |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * Creates the subkeys $_mkey (the masking key) and |
|
329 | - * $_rkey (the rotate key) which are 16 bytes each. These are |
|
330 | - * created from the original key. The original key is null |
|
331 | - * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
332 | - * split in half to create $_mkey and $_rkey |
|
333 | - * |
|
334 | - * @return void |
|
335 | - */ |
|
336 | - private function createSubKeys() |
|
337 | - { |
|
338 | - $x = $this->key(); |
|
339 | - $z = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; // init to 16 bytes |
|
340 | - $skey = array(); |
|
341 | - |
|
342 | - // the max length of the key is 16 bytes, however if it is |
|
343 | - // less, pad it with null to get ito to 16 bytes |
|
344 | - if ($this->keySize() < self::BYTES_KEY_MAX) |
|
345 | - $x = str_pad($x, self::BYTES_KEY_MAX, "\0", STR_PAD_RIGHT); |
|
346 | - |
|
347 | - /* |
|
41 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
42 | + const BYTES_BLOCK = 8; // 64 bits; |
|
43 | + |
|
44 | + //const BYTES_KEY = 0; // between 40 - 128 bits |
|
45 | + |
|
46 | + const BYTES_KEY_SMALL = 10; |
|
47 | + const BYTES_KEY_MAX = 16; |
|
48 | + const BYTES_KEY_MIN = 5; |
|
49 | + |
|
50 | + /** @type array $_s1 An array of 256 unsigned integers */ |
|
51 | + private static $_s1 = array(); |
|
52 | + |
|
53 | + /** @type array $_s2 An array of 256 unsigned integers */ |
|
54 | + private static $_s2 = array(); |
|
55 | + |
|
56 | + /** @type array $_s3 An array of 256 unsigned integers */ |
|
57 | + private static $_s3 = array(); |
|
58 | + |
|
59 | + /** @type array $_s4 An array of 256 unsigned integers */ |
|
60 | + private static $_s4 = array(); |
|
61 | + |
|
62 | + /** @type array $_s5 An array of 256 unsigned integers */ |
|
63 | + private static $_s5 = array(); |
|
64 | + |
|
65 | + /** @type array $_s6 An array of 256 unsigned integers */ |
|
66 | + private static $_s6 = array(); |
|
67 | + |
|
68 | + /** @type array $_s7 An array of 256 unsigned integers */ |
|
69 | + private static $_s7 = array(); |
|
70 | + |
|
71 | + /** @type array $_s8 An array of 256 unsigned integers */ |
|
72 | + private static $_s8 = array(); |
|
73 | + |
|
74 | + /** @type string $_mkey The 16 byte masking subkey */ |
|
75 | + private $_mkey = ""; |
|
76 | + |
|
77 | + /** @type string $_rkey The 16 byte rotate subkey */ |
|
78 | + private $_rkey = ""; |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * Constructor |
|
83 | + * |
|
84 | + * @param string $key The key used for Encryption/Decryption |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + public function __construct($key) |
|
88 | + { |
|
89 | + // the length of the key is is between 5 - 16 bytes (40 - 128 bits) |
|
90 | + $keylen = strlen($key); |
|
91 | + if ($keylen > self::BYTES_KEY_MAX) |
|
92 | + { |
|
93 | + $key = substr($key, 0, self::BYTES_KEY_MAX); |
|
94 | + $keylen = self::BYTES_KEY_MAX; |
|
95 | + } |
|
96 | + else if ($keylen < self::BYTES_KEY_MIN) |
|
97 | + { |
|
98 | + $msg = PHP_Crypt::CIPHER_CAST_128." requires a key size between "; |
|
99 | + $msg .= "5 - 16 bytes."; |
|
100 | + trigger_error($msg, E_USER_WARNING); |
|
101 | + } |
|
102 | + |
|
103 | + // set the key, make sure the required length is set in bytes |
|
104 | + parent::__construct(PHP_Crypt::CIPHER_CAST_128, $key, $keylen); |
|
105 | + |
|
106 | + // set the block size |
|
107 | + $this->blockSize(self::BYTES_BLOCK); |
|
108 | + |
|
109 | + // initialize the sboxes constants |
|
110 | + $this->initTables(); |
|
111 | + |
|
112 | + // create the sub keys using the sboxes |
|
113 | + $this->createSubKeys(); |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * Destructor |
|
119 | + * |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function __destruct() |
|
123 | + { |
|
124 | + parent::__destruct(); |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Encrypt plain text data |
|
130 | + * |
|
131 | + * @param string $data A block of plain data |
|
132 | + * @return boolean Returns true |
|
133 | + */ |
|
134 | + public function encrypt(&$data) |
|
135 | + { |
|
136 | + $this->operation(parent::ENCRYPT); |
|
137 | + |
|
138 | + // split the block in half, left and right |
|
139 | + $l = parent::str2Dec(substr($data, 0, 4)); |
|
140 | + $r = parent::str2Dec(substr($data, 4, 4)); |
|
141 | + |
|
142 | + // We do only 12 rounds if we have a key 10 bytes or less. |
|
143 | + // If we have a key greater than 10 bytes, we do the 12 rounds |
|
144 | + // then proceed to the additional 4 rounds for a total of 16 |
|
145 | + for ($i = 0; $i < 12; $i += 3) |
|
146 | + { |
|
147 | + // f1 |
|
148 | + $tmp = $r; |
|
149 | + $r = $l ^ $this->f1($r, $i); |
|
150 | + $l = $tmp; |
|
151 | + |
|
152 | + // f2 |
|
153 | + $tmp = $r; |
|
154 | + $r = $l ^ $this->f2($r, $i + 1); |
|
155 | + $l = $tmp; |
|
156 | + |
|
157 | + // f3 |
|
158 | + $tmp = $r; |
|
159 | + $r = $l ^ $this->f3($r, $i + 2); |
|
160 | + $l = $tmp; |
|
161 | + } |
|
162 | + |
|
163 | + // only do the full 16 rounds if the key is longer than |
|
164 | + // 10 bytes (80 bits) |
|
165 | + if ($this->keySize() > self::BYTES_KEY_SMALL) |
|
166 | + { |
|
167 | + // f1 |
|
168 | + $tmp = $r; |
|
169 | + $r = $l ^ $this->f1($r, 12); |
|
170 | + $l = $tmp; |
|
171 | + |
|
172 | + // f2 |
|
173 | + $tmp = $r; |
|
174 | + $r = $l ^ $this->f2($r, 13); |
|
175 | + $l = $tmp; |
|
176 | + |
|
177 | + // f3 |
|
178 | + $tmp = $r; |
|
179 | + $r = $l ^ $this->f3($r, 14); |
|
180 | + $l = $tmp; |
|
181 | + |
|
182 | + // f1 |
|
183 | + $tmp = $r; |
|
184 | + $r = $l ^ $this->f1($r, 15); |
|
185 | + $l = $tmp; |
|
186 | + } |
|
187 | + |
|
188 | + // swap the two halfs |
|
189 | + $data = parent::dec2Str($r, 4).parent::dec2Str($l, 4); |
|
190 | + |
|
191 | + return true; |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * Decrypt an encrypted string, it does all the steps of encryption, |
|
197 | + * but in reverse. |
|
198 | + * |
|
199 | + * @param string $data A block of encrypted data |
|
200 | + * @return boolean Returns true |
|
201 | + */ |
|
202 | + public function decrypt(&$data) |
|
203 | + { |
|
204 | + $this->operation(parent::DECRYPT); |
|
205 | + |
|
206 | + // split the block in half, left and right |
|
207 | + $l = parent::str2Dec(substr($data, 0, 4)); |
|
208 | + $r = parent::str2Dec(substr($data, 4, 4)); |
|
209 | + |
|
210 | + // only do the full 16 rounds if the key is longer than |
|
211 | + // 10 bytes (80 bits) |
|
212 | + if ($this->keySize() > self::BYTES_KEY_SMALL) |
|
213 | + { |
|
214 | + // f1 |
|
215 | + $tmp = $r; |
|
216 | + $r = $l ^ $this->f1($r, 15); |
|
217 | + $l = $tmp; |
|
218 | + |
|
219 | + // f3 |
|
220 | + $tmp = $r; |
|
221 | + $r = $l ^ $this->f3($r, 14); |
|
222 | + $l = $tmp; |
|
223 | + |
|
224 | + // f2 |
|
225 | + $tmp = $r; |
|
226 | + $r = $l ^ $this->f2($r, 13); |
|
227 | + $l = $tmp; |
|
228 | + |
|
229 | + // f1 |
|
230 | + $tmp = $r; |
|
231 | + $r = $l ^ $this->f1($r, 12); |
|
232 | + $l = $tmp; |
|
233 | + } |
|
234 | + |
|
235 | + // We do only 12 rounds if we have a key 10 bytes or less. |
|
236 | + for ($i = 11; $i >= 2; $i -= 3) |
|
237 | + { |
|
238 | + // f3 |
|
239 | + $tmp = $r; |
|
240 | + $r = $l ^ $this->f3($r, $i); |
|
241 | + $l = $tmp; |
|
242 | + |
|
243 | + // f2 |
|
244 | + $tmp = $r; |
|
245 | + $r = $l ^ $this->f2($r, $i - 1); |
|
246 | + $l = $tmp; |
|
247 | + |
|
248 | + // f1 |
|
249 | + $tmp = $r; |
|
250 | + $r = $l ^ $this->f1($r, $i - 2); |
|
251 | + $l = $tmp; |
|
252 | + } |
|
253 | + |
|
254 | + // swap the two halfs |
|
255 | + $data = parent::dec2Str($r, 4).parent::dec2Str($l, 4); |
|
256 | + |
|
257 | + return true; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * Cast 128 F1 function |
|
263 | + * |
|
264 | + * @param $r integer The right half of the data being encrypted in cast_128() |
|
265 | + * @param integer $i integer The round number (1-16) in cast_128() |
|
266 | + * @return integer The value after the F1 transformation |
|
267 | + */ |
|
268 | + private function f1($r, $i) |
|
269 | + { |
|
270 | + $n = $this->_mkey[$i] + $r; |
|
271 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
272 | + $n = parent::dec2Str($n, 4); |
|
273 | + |
|
274 | + $f = parent::uInt32( |
|
275 | + ((self::$_s1[ord($n[0])] ^ self::$_s2[ord($n[1])]) - |
|
276 | + self::$_s3[ord($n[2])]) + self::$_s4[ord($n[3])] |
|
277 | + ); |
|
278 | + |
|
279 | + return $f; |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + /** |
|
284 | + * Cast 128 F2 function |
|
285 | + * |
|
286 | + * @param $r integer The right half of the data being encrypted in cast_128() |
|
287 | + * @param integer $i integer The round number (1-16) in cast_128() |
|
288 | + * @return integer The value after the F2 transformation |
|
289 | + */ |
|
290 | + private function f2($r, $i) |
|
291 | + { |
|
292 | + $n = $this->_mkey[$i] ^ $r; |
|
293 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
294 | + $n = parent::dec2Str($n, 4); |
|
295 | + |
|
296 | + $f = parent::uInt32( |
|
297 | + ((self::$_s1[ord($n[0])] - self::$_s2[ord($n[1])]) + |
|
298 | + self::$_s3[ord($n[2])]) ^ self::$_s4[ord($n[3])] |
|
299 | + ); |
|
300 | + |
|
301 | + return $f; |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * Cast 128 F3 function |
|
307 | + * |
|
308 | + * @param $r integer The right half of the data being encrypted in cast_128() |
|
309 | + * @param integer $i integer The round number (1-16) in cast_128() |
|
310 | + * @return integer The value after the F3 transformation |
|
311 | + */ |
|
312 | + private function f3($r, $i) |
|
313 | + { |
|
314 | + $n = $this->_mkey[$i] - $r; |
|
315 | + $n = parent::uInt32(parent::rotBitsLeft32($n, $this->_rkey[$i])); |
|
316 | + $n = parent::dec2Str($n, 4); |
|
317 | + |
|
318 | + $f = parent::uInt32( |
|
319 | + ((self::$_s1[ord($n[0])] + self::$_s2[ord($n[1])]) ^ |
|
320 | + self::$_s3[ord($n[2])]) - self::$_s4[ord($n[3])] |
|
321 | + ); |
|
322 | + |
|
323 | + return $f; |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * Creates the subkeys $_mkey (the masking key) and |
|
329 | + * $_rkey (the rotate key) which are 16 bytes each. These are |
|
330 | + * created from the original key. The original key is null |
|
331 | + * padded up to 16 bytes and expanded to 32 bytes. It is then |
|
332 | + * split in half to create $_mkey and $_rkey |
|
333 | + * |
|
334 | + * @return void |
|
335 | + */ |
|
336 | + private function createSubKeys() |
|
337 | + { |
|
338 | + $x = $this->key(); |
|
339 | + $z = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; // init to 16 bytes |
|
340 | + $skey = array(); |
|
341 | + |
|
342 | + // the max length of the key is 16 bytes, however if it is |
|
343 | + // less, pad it with null to get ito to 16 bytes |
|
344 | + if ($this->keySize() < self::BYTES_KEY_MAX) |
|
345 | + $x = str_pad($x, self::BYTES_KEY_MAX, "\0", STR_PAD_RIGHT); |
|
346 | + |
|
347 | + /* |
|
348 | 348 | * NOW FOR THE UGLY PART, THIS IS TAKEN FROM PAGE 3-4 OF |
349 | 349 | * http://tools.ietf.org/html/rfc2144 |
350 | 350 | */ |
351 | 351 | |
352 | - // two loops, each loop does 16 bytes for a total of 32 bytes |
|
353 | - for ($i = 0; $i < 2; ++$i) |
|
354 | - { |
|
355 | - // z0z1z2z3 = x0x1x2x3 ^ S5[xD] ^ S6[xF] ^ S7[xC] ^ S8[xE] ^ S7[x8] |
|
356 | - $tmp = substr($x, 0x00, 4); |
|
357 | - $tmp = parent::dec2Str( |
|
358 | - parent::uInt32( |
|
359 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0d])] ^ |
|
360 | - self::$_s6[ord($x[0x0f])] ^ self::$_s7[ord($x[0x0c])] ^ |
|
361 | - self::$_s8[ord($x[0x0e])] ^ self::$_s7[ord($x[0x08])] |
|
362 | - ), 4); |
|
363 | - $z = substr_replace($z, $tmp, 0x00, 4); |
|
364 | - |
|
365 | - //print "Z0: ".parent::str2Hex($z)." (".strlen($z).")\n"; |
|
366 | - |
|
367 | - // z4z5z6z7 = x8x9xAxB ^ S5[z0] ^ S6[z2] ^ S7[z1] ^ S8[z3] ^ S8[xA] |
|
368 | - $tmp = substr($x, 0x08, 4); |
|
369 | - $tmp = parent::dec2Str( |
|
370 | - parent::uInt32( |
|
371 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x00])] ^ |
|
372 | - self::$_s6[ord($z[0x02])] ^ self::$_s7[ord($z[0x01])] ^ |
|
373 | - self::$_s8[ord($z[0x03])] ^ self::$_s8[ord($x[0x0a])] |
|
374 | - ), 4); |
|
375 | - $z = substr_replace($z, $tmp, 0x04, 4); |
|
376 | - |
|
377 | - // z8z9zAzB = xCxDxExF ^ S5[z7] ^ S6[z6] ^ S7[z5] ^ S8[z4] ^ S5[x9] |
|
378 | - $tmp = substr($x, 0x0c, 4); |
|
379 | - $tmp = parent::dec2Str( |
|
380 | - parent::uInt32( |
|
381 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x07])] ^ |
|
382 | - self::$_s6[ord($z[0x06])] ^ self::$_s7[ord($z[0x05])] ^ |
|
383 | - self::$_s8[ord($z[0x04])] ^ self::$_s5[ord($x[0x09])] |
|
384 | - ), 4); |
|
385 | - $z = substr_replace($z, $tmp, 0x08, 4); |
|
386 | - |
|
387 | - // zCzDzEzF = x4x5x6x7 ^ S5[zA] ^ S6[z9] ^ S7[zB] ^ S8[z8] ^ S6[xB] |
|
388 | - $tmp = substr($x, 0x04, 4); |
|
389 | - $tmp = parent::dec2Str( |
|
390 | - parent::uInt32( |
|
391 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x0a])] ^ |
|
392 | - self::$_s6[ord($z[0x09])] ^ self::$_s7[ord($z[0x0b])] ^ |
|
393 | - self::$_s8[ord($z[0x08])] ^ self::$_s6[ord($x[0x0b])] |
|
394 | - ), 4); |
|
395 | - $z = substr_replace($z, $tmp, 0x0c, 4); |
|
396 | - |
|
397 | - //print "Z: ".parent::str2Hex($z)." (".strlen($z).")\n"; |
|
398 | - |
|
399 | - // K1 = S5[z8] ^ S6[z9] ^ S7[z7] ^ S8[z6] ^ S5[z2] |
|
400 | - $skey[] = parent::uInt32(self::$_s5[ord($z[0x08])] ^ self::$_s6[ord($z[0x09])] ^ |
|
401 | - self::$_s7[ord($z[0x07])] ^ self::$_s8[ord($z[0x06])] ^ |
|
402 | - self::$_s5[ord($z[0x02])] |
|
403 | - ); |
|
404 | - |
|
405 | - // K2 = S5[zA] ^ S6[zB] ^ S7[z5] ^ S8[z4] ^ S6[z6] |
|
406 | - $skey[] = parent::uInt32( |
|
407 | - self::$_s5[ord($z[0x0a])] ^ self::$_s6[ord($z[0x0b])] ^ |
|
408 | - self::$_s7[ord($z[0x05])] ^ self::$_s8[ord($z[0x04])] ^ |
|
409 | - self::$_s6[ord($z[0x06])] |
|
410 | - ); |
|
411 | - |
|
412 | - // K3 = S5[zC] ^ S6[zD] ^ S7[z3] ^ S8[z2] ^ S7[z9] |
|
413 | - $skey[] = parent::uInt32( |
|
414 | - self::$_s5[ord($z[0x0c])] ^ self::$_s6[ord($z[0x0d])] ^ |
|
415 | - self::$_s7[ord($z[0x03])] ^ self::$_s8[ord($z[0x02])] ^ |
|
416 | - self::$_s7[ord($z[0x09])] |
|
417 | - ); |
|
418 | - |
|
419 | - // K4 = S5[zE] ^ S6[zF] ^ S7[z1] ^ S8[z0] ^ S8[zC] |
|
420 | - $skey[] = parent::uInt32( |
|
421 | - self::$_s5[ord($z[0x0e])] ^ self::$_s6[ord($z[0x0f])] ^ |
|
422 | - self::$_s7[ord($z[0x01])] ^ self::$_s8[ord($z[0x00])] ^ |
|
423 | - self::$_s8[ord($z[0x0c])] |
|
424 | - ); |
|
425 | - |
|
426 | - // x0x1x2x3 = z8z9zAzB ^ S5[z5] ^ S6[z7] ^ S7[z4] ^ S8[z6] ^ S7[z0] |
|
427 | - $tmp = substr($z, 0x08, 4); |
|
428 | - $tmp = parent::dec2Str( |
|
429 | - parent::uInt32( |
|
430 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x05])] ^ |
|
431 | - self::$_s6[ord($z[0x07])] ^ self::$_s7[ord($z[0x04])] ^ |
|
432 | - self::$_s8[ord($z[0x06])] ^ self::$_s7[ord($z[0x00])] |
|
433 | - ), 4); |
|
434 | - $x = substr_replace($x, $tmp, 0x00, 4); |
|
435 | - |
|
436 | - // x4x5x6x7 = z0z1z2z3 ^ S5[x0] ^ S6[x2] ^ S7[x1] ^ S8[x3] ^ S8[z2] |
|
437 | - $tmp = substr($z, 0x00, 4); |
|
438 | - $tmp = parent::dec2Str( |
|
439 | - parent::uInt32( |
|
440 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x00])] ^ |
|
441 | - self::$_s6[ord($x[0x02])] ^ self::$_s7[ord($x[0x01])] ^ |
|
442 | - self::$_s8[ord($x[0x03])] ^ self::$_s8[ord($z[0x02])] |
|
443 | - ), 4); |
|
444 | - $x = substr_replace($x, $tmp, 0x04, 4); |
|
445 | - |
|
446 | - // x8x9xAxB = z4z5z6z7 ^ S5[x7] ^ S6[x6] ^ S7[x5] ^ S8[x4] ^ S5[z1] |
|
447 | - $tmp = substr($z, 0x04, 4); |
|
448 | - $tmp = parent::dec2Str( |
|
449 | - parent::uInt32( |
|
450 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x07])] ^ |
|
451 | - self::$_s6[ord($x[0x06])] ^ self::$_s7[ord($x[0x05])] ^ |
|
452 | - self::$_s8[ord($x[0x04])] ^ self::$_s5[ord($z[0x01])] |
|
453 | - ), 4); |
|
454 | - $x = substr_replace($x, $tmp, 0x08, 4); |
|
455 | - |
|
456 | - // xCxDxExF = zCzDzEzF ^ S5[xA] ^ S6[x9] ^ S7[xB] ^ S8[x8] ^ S6[z3] |
|
457 | - $tmp = substr($z, 0x0c, 4); |
|
458 | - $tmp = parent::dec2Str( |
|
459 | - parent::uInt32( |
|
460 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0a])] ^ |
|
461 | - self::$_s6[ord($x[0x09])] ^ self::$_s7[ord($x[0x0b])] ^ |
|
462 | - self::$_s8[ord($x[0x08])] ^ self::$_s6[ord($z[0x03])] |
|
463 | - ), 4); |
|
464 | - $x = substr_replace($x, $tmp, 0x0c, 4); |
|
465 | - |
|
466 | - // K5 = S5[x3] ^ S6[x2] ^ S7[xC] ^ S8[xD] ^ S5[x8] |
|
467 | - $skey[] = parent::uInt32( |
|
468 | - self::$_s5[ord($x[0x03])] ^ self::$_s6[ord($x[0x02])] ^ |
|
469 | - self::$_s7[ord($x[0x0c])] ^ self::$_s8[ord($x[0x0d])] ^ |
|
470 | - self::$_s5[ord($x[0x08])] |
|
471 | - ); |
|
472 | - |
|
473 | - // K6 = S5[x1] ^ S6[x0] ^ S7[xE] ^ S8[xF] ^ S6[xD] |
|
474 | - $skey[] = parent::uInt32( |
|
475 | - self::$_s5[ord($x[0x01])] ^ self::$_s6[ord($x[0x00])] ^ |
|
476 | - self::$_s7[ord($x[0x0e])] ^ self::$_s8[ord($x[0x0f])] ^ |
|
477 | - self::$_s6[ord($x[0x0d])] |
|
478 | - ); |
|
479 | - |
|
480 | - // K7 = S5[x7] ^ S6[x6] ^ S7[x8] ^ S8[x9] ^ S7[x3] |
|
481 | - $skey[] = parent::uInt32( |
|
482 | - self::$_s5[ord($x[0x07])] ^ self::$_s6[ord($x[0x06])] ^ |
|
483 | - self::$_s7[ord($x[0x08])] ^ self::$_s8[ord($x[0x09])] ^ |
|
484 | - self::$_s7[ord($x[0x03])] |
|
485 | - ); |
|
486 | - |
|
487 | - // K8 = S5[x5] ^ S6[x4] ^ S7[xA] ^ S8[xB] ^ S8[x7] |
|
488 | - $skey[] = parent::uInt32( |
|
489 | - self::$_s5[ord($x[0x05])] ^ self::$_s6[ord($x[0x04])] ^ |
|
490 | - self::$_s7[ord($x[0x0a])] ^ self::$_s8[ord($x[0x0b])] ^ |
|
491 | - self::$_s8[ord($x[0x07])] |
|
492 | - ); |
|
493 | - |
|
494 | - // z0z1z2z3 = x0x1x2x3 ^ S5[xD] ^ S6[xF] ^ S7[xC] ^ S8[xE] ^ S7[x8] |
|
495 | - $tmp = substr($x, 0x00, 4); |
|
496 | - $tmp = parent::dec2Str( |
|
497 | - parent::uInt32( |
|
498 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0d])] ^ |
|
499 | - self::$_s6[ord($x[0x0f])] ^ self::$_s7[ord($x[0x0c])] ^ |
|
500 | - self::$_s8[ord($x[0x0e])] ^ self::$_s7[ord($x[0x08])] |
|
501 | - ), 4); |
|
502 | - $z = substr_replace($z, $tmp, 0x00, 4); |
|
503 | - |
|
504 | - // z4z5z6z7 = x8x9xAxB ^ S5[z0] ^ S6[z2] ^ S7[z1] ^ S8[z3] ^ S8[xA] |
|
505 | - $tmp = substr($x, 0x08, 4); |
|
506 | - $tmp = parent::dec2Str( |
|
507 | - parent::uInt32( |
|
508 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x00])] ^ |
|
509 | - self::$_s6[ord($z[0x02])] ^ self::$_s7[ord($z[0x01])] ^ |
|
510 | - self::$_s8[ord($z[0x03])] ^ self::$_s8[ord($x[0x0a])] |
|
511 | - ), 4); |
|
512 | - $z = substr_replace($z, $tmp, 0x04, 4); |
|
513 | - |
|
514 | - // z8z9zAzB = xCxDxExF ^ S5[z7] ^ S6[z6] ^ S7[z5] ^ S8[z4] ^ S5[x9] |
|
515 | - $tmp = substr($x, 0x0c, 4); |
|
516 | - $tmp = parent::dec2Str( |
|
517 | - parent::uInt32( |
|
518 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x07])] ^ |
|
519 | - self::$_s6[ord($z[0x06])] ^ self::$_s7[ord($z[0x05])] ^ |
|
520 | - self::$_s8[ord($z[0x04])] ^ self::$_s5[ord($x[0x09])] |
|
521 | - ), 4); |
|
522 | - $z = substr_replace($z, $tmp, 0x08, 4); |
|
523 | - |
|
524 | - // zCzDzEzF = x4x5x6x7 ^ S5[zA] ^ S6[z9] ^ S7[zB] ^ S8[z8] ^ S6[xB] |
|
525 | - $tmp = substr($x, 0x04, 4); |
|
526 | - $tmp = parent::dec2Str( |
|
527 | - parent::uInt32( |
|
528 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x0a])] ^ |
|
529 | - self::$_s6[ord($z[0x09])] ^ self::$_s7[ord($z[0x0b])] ^ |
|
530 | - self::$_s8[ord($z[0x08])] ^ self::$_s6[ord($x[0x0b])] |
|
531 | - ), 4); |
|
532 | - $z = substr_replace($z, $tmp, 0x0c, 4); |
|
533 | - |
|
534 | - // K9 = S5[z3] ^ S6[z2] ^ S7[zC] ^ S8[zD] ^ S5[z9] |
|
535 | - $skey[] = parent::uInt32( |
|
536 | - self::$_s5[ord($z[0x03])] ^ self::$_s6[ord($z[0x02])] ^ |
|
537 | - self::$_s7[ord($z[0x0c])] ^ self::$_s8[ord($z[0x0d])] ^ |
|
538 | - self::$_s5[ord($z[0x09])] |
|
539 | - ); |
|
540 | - |
|
541 | - // K10 = S5[z1] ^ S6[z0] ^ S7[zE] ^ S8[zF] ^ S6[zC] |
|
542 | - $skey[] = parent::uInt32( |
|
543 | - self::$_s5[ord($z[0x01])] ^ self::$_s6[ord($z[0x00])] ^ |
|
544 | - self::$_s7[ord($z[0x0e])] ^ self::$_s8[ord($z[0x0f])] ^ |
|
545 | - self::$_s6[ord($z[0x0c])] |
|
546 | - ); |
|
547 | - |
|
548 | - // K11 = S5[z7] ^ S6[z6] ^ S7[z8] ^ S8[z9] ^ S7[z2] |
|
549 | - $skey[] = parent::uInt32( |
|
550 | - self::$_s5[ord($z[0x07])] ^ self::$_s6[ord($z[0x06])] ^ |
|
551 | - self::$_s7[ord($z[0x08])] ^ self::$_s8[ord($z[0x09])] ^ |
|
552 | - self::$_s7[ord($z[0x02])] |
|
553 | - ); |
|
554 | - |
|
555 | - // K12 = S5[z5] ^ S6[z4] ^ S7[zA] ^ S8[zB] ^ S8[z6] |
|
556 | - $skey[] = parent::uInt32( |
|
557 | - self::$_s5[ord($z[0x05])] ^ self::$_s6[ord($z[0x04])] ^ |
|
558 | - self::$_s7[ord($z[0x0a])] ^ self::$_s8[ord($z[0x0b])] ^ |
|
559 | - self::$_s8[ord($z[0x06])] |
|
560 | - ); |
|
561 | - |
|
562 | - // x0x1x2x3 = z8z9zAzB ^ S5[z5] ^ S6[z7] ^ S7[z4] ^ S8[z6] ^ S7[z0] |
|
563 | - $tmp = substr($z, 0x08, 4); |
|
564 | - $tmp = parent::dec2Str( |
|
565 | - parent::uInt32( |
|
566 | - parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x05])] ^ |
|
567 | - self::$_s6[ord($z[0x07])] ^ self::$_s7[ord($z[0x04])] ^ |
|
568 | - self::$_s8[ord($z[0x06])] ^ self::$_s7[ord($z[0x00])] |
|
569 | - ), 4); |
|
570 | - $x = substr_replace($x, $tmp, 0x00, 4); |
|
571 | - |
|
572 | - // x4x5x6x7 = z0z1z2z3 ^ S5[x0] ^ S6[x2] ^ S7[x1] ^ S8[x3] ^ S8[z2] |
|
573 | - $tmp = substr($z, 0x00, 4); |
|
574 | - $tmp = parent::dec2Str( |
|
575 | - parent::uInt32( |
|
576 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x00])] ^ |
|
577 | - self::$_s6[ord($x[0x02])] ^ self::$_s7[ord($x[0x01])] ^ |
|
578 | - self::$_s8[ord($x[0x03])] ^ self::$_s8[ord($z[0x02])] |
|
579 | - ), 4); |
|
580 | - $x = substr_replace($x, $tmp, 0x04, 4); |
|
581 | - |
|
582 | - // x8x9xAxB = z4z5z6z7 ^ S5[x7] ^ S6[x6] ^ S7[x5] ^ S8[x4] ^ S5[z1] |
|
583 | - $tmp = substr($z, 0x04, 4); |
|
584 | - $tmp = parent::dec2Str( |
|
585 | - parent::uInt32( |
|
586 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x07])] ^ |
|
587 | - self::$_s6[ord($x[0x06])] ^ self::$_s7[ord($x[0x05])] ^ |
|
588 | - self::$_s8[ord($x[0x04])] ^ self::$_s5[ord($z[0x01])] |
|
589 | - ), 4); |
|
590 | - $x = substr_replace($x, $tmp, 0x08, 4); |
|
591 | - |
|
592 | - // xCxDxExF = zCzDzEzF ^ S5[xA] ^ S6[x9] ^ S7[xB] ^ S8[x8] ^ S6[z3] |
|
593 | - $tmp = substr($z, 0x0c, 4); |
|
594 | - $tmp = parent::dec2Str( |
|
595 | - parent::uInt32( |
|
596 | - parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0a])] ^ |
|
597 | - self::$_s6[ord($x[0x09])] ^ self::$_s7[ord($x[0x0b])] ^ |
|
598 | - self::$_s8[ord($x[0x08])] ^ self::$_s6[ord($z[0x03])] |
|
599 | - ), 4); |
|
600 | - $x = substr_replace($x, $tmp, 0x0c, 4); |
|
601 | - |
|
602 | - // K13 = S5[x8] ^ S6[x9] ^ S7[x7] ^ S8[x6] ^ S5[x3] |
|
603 | - $skey[] = parent::uInt32( |
|
604 | - self::$_s5[ord($x[0x08])] ^ self::$_s6[ord($x[0x09])] ^ |
|
605 | - self::$_s7[ord($x[0x07])] ^ self::$_s8[ord($x[0x06])] ^ |
|
606 | - self::$_s5[ord($x[0x03])] |
|
607 | - ); |
|
608 | - |
|
609 | - // K14 = S5[xA] ^ S6[xB] ^ S7[x5] ^ S8[x4] ^ S6[x7] |
|
610 | - $skey[] = parent::uInt32( |
|
611 | - self::$_s5[ord($x[0x0a])] ^ self::$_s6[ord($x[0x0b])] ^ |
|
612 | - self::$_s7[ord($x[0x05])] ^ self::$_s8[ord($x[0x04])] ^ |
|
613 | - self::$_s6[ord($x[0x07])] |
|
614 | - ); |
|
615 | - |
|
616 | - // K15 = S5[xC] ^ S6[xD] ^ S7[x3] ^ S8[x2] ^ S7[x8] |
|
617 | - $skey[] = parent::uInt32( |
|
618 | - self::$_s5[ord($x[0x0c])] ^ self::$_s6[ord($x[0x0d])] ^ |
|
619 | - self::$_s7[ord($x[0x03])] ^ self::$_s8[ord($x[0x02])] ^ |
|
620 | - self::$_s7[ord($x[0x08])] |
|
621 | - ); |
|
622 | - |
|
623 | - // K16 = S5[xE] ^ S6[xF] ^ S7[x1] ^ S8[x0] ^ S8[xD] |
|
624 | - $skey[] = parent::uInt32( |
|
625 | - self::$_s5[ord($x[0x0e])] ^ self::$_s6[ord($x[0x0f])] ^ |
|
626 | - self::$_s7[ord($x[0x01])] ^ self::$_s8[ord($x[0x00])] ^ |
|
627 | - self::$_s8[ord($x[0x0d])] |
|
628 | - ); |
|
629 | - } |
|
630 | - |
|
631 | - // create the 16 byte masking and rotate subkeys |
|
632 | - $this->_mkey = array_slice($skey, 0, 16); |
|
633 | - $this->_rkey = array_slice($skey, 16, 16); |
|
634 | - |
|
635 | - // $_rkey only uses the least significant 5 bits |
|
636 | - $this->_rkey = array_map(function($v) { |
|
637 | - return $v &= 31; |
|
638 | - }, $this->_rkey); |
|
639 | - |
|
640 | - // there is 4kb in the s5 - s8 sboxes, which are not needed after we |
|
641 | - // create the subkeys, so free up the memory. unset() doesn't work here |
|
642 | - for ($i = 5; $i <= 8; ++$i) |
|
643 | - self::${"_s$i"} = null; |
|
644 | - } |
|
645 | - |
|
646 | - |
|
647 | - /** |
|
648 | - * Initialize the tables. |
|
649 | - * |
|
650 | - * @return void |
|
651 | - */ |
|
652 | - private function initTables() |
|
653 | - { |
|
654 | - // 256 unsigned 32 bit integers |
|
655 | - self::$_s1 = array( |
|
656 | - 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
657 | - 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
658 | - 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
659 | - 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
660 | - 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
661 | - 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
662 | - 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
663 | - 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
664 | - 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
665 | - 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
666 | - 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
667 | - 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
668 | - 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
669 | - 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
670 | - 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
671 | - 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
672 | - 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
673 | - 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
674 | - 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
675 | - 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
676 | - 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
677 | - 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
678 | - 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
679 | - 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
680 | - 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
681 | - 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
682 | - 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
683 | - 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
684 | - 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
685 | - 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
686 | - 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
687 | - 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
688 | - 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
689 | - 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
690 | - 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
691 | - 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
692 | - 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
693 | - 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
694 | - 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
695 | - 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
696 | - 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
697 | - 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
698 | - 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
699 | - 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
700 | - 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
701 | - 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
702 | - 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
703 | - 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
704 | - 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
705 | - 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
706 | - 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
707 | - 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
708 | - 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
709 | - 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
710 | - 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
711 | - 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
712 | - 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
713 | - 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
714 | - 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
715 | - 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
716 | - 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
717 | - 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
718 | - 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
719 | - 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
720 | - ); |
|
721 | - |
|
722 | - // 256 unsigned 32 bit integers |
|
723 | - self::$_s2 = array( |
|
724 | - 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
725 | - 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
726 | - 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
727 | - 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
728 | - 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
729 | - 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
730 | - 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
731 | - 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
732 | - 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
733 | - 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
734 | - 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
735 | - 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
736 | - 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
737 | - 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
738 | - 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
739 | - 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
740 | - 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
741 | - 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
742 | - 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
743 | - 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
744 | - 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
745 | - 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
746 | - 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
747 | - 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
748 | - 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
749 | - 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
750 | - 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
751 | - 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
752 | - 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
753 | - 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
754 | - 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
755 | - 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
756 | - 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
757 | - 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
758 | - 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
759 | - 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
760 | - 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
761 | - 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
762 | - 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
763 | - 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
764 | - 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
765 | - 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
766 | - 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
767 | - 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
768 | - 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
769 | - 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
770 | - 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
771 | - 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
772 | - 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
773 | - 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
774 | - 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
775 | - 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
776 | - 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
777 | - 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
778 | - 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
779 | - 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
780 | - 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
781 | - 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
782 | - 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
783 | - 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
784 | - 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
785 | - 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
786 | - 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
787 | - 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
788 | - ); |
|
789 | - |
|
790 | - // 256 unsigned 32 bit integers |
|
791 | - self::$_s3 = array( |
|
792 | - 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
793 | - 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
794 | - 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
795 | - 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
796 | - 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
797 | - 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
798 | - 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
799 | - 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
800 | - 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
801 | - 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
802 | - 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
803 | - 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
804 | - 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
805 | - 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
806 | - 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
807 | - 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
808 | - 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
809 | - 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
810 | - 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
811 | - 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
812 | - 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
813 | - 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
814 | - 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
815 | - 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
816 | - 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
817 | - 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
818 | - 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
819 | - 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
820 | - 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
821 | - 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
822 | - 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
823 | - 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
824 | - 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
825 | - 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
826 | - 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
827 | - 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
828 | - 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
829 | - 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
830 | - 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
831 | - 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
832 | - 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
833 | - 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
834 | - 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
835 | - 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
836 | - 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
837 | - 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
838 | - 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
839 | - 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
840 | - 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
841 | - 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
842 | - 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
843 | - 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
844 | - 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
845 | - 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
846 | - 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
847 | - 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
848 | - 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
849 | - 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
850 | - 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
851 | - 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
852 | - 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
853 | - 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
854 | - 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
855 | - 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
856 | - ); |
|
857 | - |
|
858 | - // 256 unsigned 32 bit integers |
|
859 | - self::$_s4 = array( |
|
860 | - 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
861 | - 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
862 | - 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
863 | - 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
864 | - 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
865 | - 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
866 | - 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
867 | - 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
868 | - 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
869 | - 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
870 | - 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
871 | - 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
872 | - 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
873 | - 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
874 | - 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
875 | - 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
876 | - 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
877 | - 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
878 | - 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
879 | - 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
880 | - 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
881 | - 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
882 | - 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
883 | - 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
884 | - 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
885 | - 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
886 | - 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
887 | - 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
888 | - 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
889 | - 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
890 | - 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
891 | - 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
892 | - 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
893 | - 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
894 | - 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
895 | - 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
896 | - 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
897 | - 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
898 | - 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
899 | - 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
900 | - 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
901 | - 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
902 | - 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
903 | - 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
904 | - 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
905 | - 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
906 | - 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
907 | - 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
908 | - 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
909 | - 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
910 | - 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
911 | - 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
912 | - 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
913 | - 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
914 | - 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
915 | - 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
916 | - 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
917 | - 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
918 | - 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
919 | - 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
920 | - 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
921 | - 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
922 | - 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
923 | - 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
924 | - ); |
|
925 | - |
|
926 | - // 256 unsigned 32 bit integers |
|
927 | - self::$_s5 = array( |
|
928 | - 0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911, |
|
929 | - 0xB86A7FFF, 0x1DD358F5, 0x44DD9D44, 0x1731167F, |
|
930 | - 0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00, |
|
931 | - 0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A, |
|
932 | - 0xE6A2E77F, 0xF0C720CD, 0xC4494816, 0xCCF5C180, |
|
933 | - 0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF, |
|
934 | - 0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2, |
|
935 | - 0x4E40B48D, 0x248EB6FB, 0x8DBA1CFE, 0x41A99B02, |
|
936 | - 0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725, |
|
937 | - 0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A, |
|
938 | - 0xF2F3F763, 0x68AF8040, 0xED0C9E56, 0x11B4958B, |
|
939 | - 0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7, |
|
940 | - 0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571, |
|
941 | - 0x0C05372A, 0x578535F2, 0x2261BE02, 0xD642A0C9, |
|
942 | - 0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC, |
|
943 | - 0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981, |
|
944 | - 0x5C1FF900, 0xFE38D399, 0x0C4EFF0B, 0x062407EA, |
|
945 | - 0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774, |
|
946 | - 0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263, |
|
947 | - 0xDF65001F, 0x0EC50966, 0xDFDD55BC, 0x29DE0655, |
|
948 | - 0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468, |
|
949 | - 0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2, |
|
950 | - 0xBCF3F0AA, 0x87AC36E9, 0xE53A7426, 0x01B3D82B, |
|
951 | - 0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910, |
|
952 | - 0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284, |
|
953 | - 0x636737B6, 0x50F5B616, 0xF24766E3, 0x8ECA36C1, |
|
954 | - 0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4, |
|
955 | - 0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA, |
|
956 | - 0x26E46695, 0xB7566419, 0xF654EFC5, 0xD08D58B7, |
|
957 | - 0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049, |
|
958 | - 0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE, |
|
959 | - 0xC62BF3CD, 0x9E0885F9, 0x68CB3E47, 0x086C010F, |
|
960 | - 0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6, |
|
961 | - 0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA, |
|
962 | - 0x0AB378D5, 0xD951FB0C, 0xDED7DA56, 0x4124BBE4, |
|
963 | - 0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE, |
|
964 | - 0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561, |
|
965 | - 0xC3DC0280, 0x05687715, 0x646C6BD7, 0x44904DB3, |
|
966 | - 0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6, |
|
967 | - 0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840, |
|
968 | - 0x76F0AE02, 0x083BE84D, 0x28421C9A, 0x44489406, |
|
969 | - 0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4, |
|
970 | - 0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472, |
|
971 | - 0x3CA5D717, 0x7D161BBA, 0x9CAD9010, 0xAF462BA2, |
|
972 | - 0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487, |
|
973 | - 0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7, |
|
974 | - 0x445F7382, 0x175683F4, 0xCDC66A97, 0x70BE0288, |
|
975 | - 0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5, |
|
976 | - 0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2, |
|
977 | - 0x1C5C1572, 0xF6721B2C, 0x1AD2FFF3, 0x8C25404E, |
|
978 | - 0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78, |
|
979 | - 0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E, |
|
980 | - 0x44094F85, 0x3F481D87, 0xFCFEAE7B, 0x77B5FF76, |
|
981 | - 0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801, |
|
982 | - 0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0, |
|
983 | - 0xDF3B0874, 0x95055110, 0x1B5AD7A8, 0xF61ED5AD, |
|
984 | - 0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58, |
|
985 | - 0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0, |
|
986 | - 0x5CE96C28, 0xE176EDA3, 0x6BAC307F, 0x376829D2, |
|
987 | - 0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20, |
|
988 | - 0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE, |
|
989 | - 0xF9E0659A, 0xEEB9491D, 0x34010718, 0xBB30CAB8, |
|
990 | - 0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55, |
|
991 | - 0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4 |
|
992 | - ); |
|
993 | - |
|
994 | - // 256 unsigned 32 bit integers |
|
995 | - self::$_s6 = array( |
|
996 | - 0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C, |
|
997 | - 0x95DB08E7, 0x016843B4, 0xECED5CBC, 0x325553AC, |
|
998 | - 0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9, |
|
999 | - 0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138, |
|
1000 | - 0x33F14961, 0xC01937BD, 0xF506C6DA, 0xE4625E7E, |
|
1001 | - 0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367, |
|
1002 | - 0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866, |
|
1003 | - 0xA084DB2D, 0x09A8486F, 0xA888614A, 0x2900AF98, |
|
1004 | - 0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C, |
|
1005 | - 0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072, |
|
1006 | - 0xFD41197E, 0x9305A6B0, 0xE86BE3DA, 0x74BED3CD, |
|
1007 | - 0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3, |
|
1008 | - 0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53, |
|
1009 | - 0x5C3D9C01, 0x64BDB941, 0x2C0E636A, 0xBA7DD9CD, |
|
1010 | - 0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D, |
|
1011 | - 0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8, |
|
1012 | - 0x284CAF89, 0xAA928223, 0x9334BE53, 0x3B3A21BF, |
|
1013 | - 0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9, |
|
1014 | - 0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807, |
|
1015 | - 0x5B7C5ECC, 0x221DB3A6, 0x9A69A02F, 0x68818A54, |
|
1016 | - 0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A, |
|
1017 | - 0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387, |
|
1018 | - 0x53BDDB65, 0xE76FFBE7, 0xE967FD78, 0x0BA93563, |
|
1019 | - 0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC, |
|
1020 | - 0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0, |
|
1021 | - 0xE81F994F, 0x9528CD89, 0xFD339FED, 0xB87834BF, |
|
1022 | - 0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE, |
|
1023 | - 0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF, |
|
1024 | - 0x4EC75B95, 0x24F2C3C0, 0x42D15D99, 0xCD0D7FA0, |
|
1025 | - 0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F, |
|
1026 | - 0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2, |
|
1027 | - 0x372B74AF, 0x692573E4, 0xE9A9D848, 0xF3160289, |
|
1028 | - 0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853, |
|
1029 | - 0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950, |
|
1030 | - 0x36F73523, 0x4CFB6E87, 0x7DA4CEC0, 0x6C152DAA, |
|
1031 | - 0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F, |
|
1032 | - 0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9, |
|
1033 | - 0x2B05D08D, 0x48B9D585, 0xDC049441, 0xC8098F9B, |
|
1034 | - 0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751, |
|
1035 | - 0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE, |
|
1036 | - 0xBF32679D, 0xD45B5B75, 0xB353FD00, 0xCBB0E358, |
|
1037 | - 0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13, |
|
1038 | - 0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397, |
|
1039 | - 0x47CF8E7A, 0xB6C85283, 0x3CC2ACFB, 0x3FC06976, |
|
1040 | - 0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459, |
|
1041 | - 0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0, |
|
1042 | - 0x3007CD3E, 0x74719EEF, 0xDC872681, 0x073340D4, |
|
1043 | - 0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891, |
|
1044 | - 0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F, |
|
1045 | - 0x6FE4AC98, 0xB173ECC0, 0xBC60B42A, 0x953498DA, |
|
1046 | - 0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB, |
|
1047 | - 0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC, |
|
1048 | - 0xE8816F4A, 0x3814F200, 0xA3F94043, 0x9C7A54C2, |
|
1049 | - 0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084, |
|
1050 | - 0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB, |
|
1051 | - 0xBA5AC7B5, 0xB6F6DEAF, 0x3A479C3A, 0x5302DA25, |
|
1052 | - 0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B, |
|
1053 | - 0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121, |
|
1054 | - 0xB81A928A, 0x60ED5869, 0x97C55B96, 0xEAEC991B, |
|
1055 | - 0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5, |
|
1056 | - 0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855, |
|
1057 | - 0xD36B4CF1, 0xF544EDEB, 0xB0E93524, 0xBEBB8FBD, |
|
1058 | - 0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454, |
|
1059 | - 0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F |
|
1060 | - ); |
|
1061 | - |
|
1062 | - // 256 unsigned 32 bit integers |
|
1063 | - self::$_s7 = array( |
|
1064 | - 0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693, |
|
1065 | - 0x2A8D7F6F, 0xAB9BC912, 0xDE6008A1, 0x2028DA1F, |
|
1066 | - 0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82, |
|
1067 | - 0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE, |
|
1068 | - 0xA05FBCF6, 0xCD4181E9, 0xE150210C, 0xE24EF1BD, |
|
1069 | - 0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43, |
|
1070 | - 0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F, |
|
1071 | - 0x089766BE, 0xBAEEADF4, 0x1286BECF, 0xB6EACB19, |
|
1072 | - 0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9, |
|
1073 | - 0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2, |
|
1074 | - 0x107789BE, 0xB3B2E9CE, 0x0502AA8F, 0x0BC0351E, |
|
1075 | - 0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516, |
|
1076 | - 0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83, |
|
1077 | - 0x334266CE, 0x8C9341B7, 0xD0D854C0, 0xCB3A6C88, |
|
1078 | - 0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E, |
|
1079 | - 0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816, |
|
1080 | - 0x0A961288, 0xE1A5C06E, 0x13749E67, 0x72FC081A, |
|
1081 | - 0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756, |
|
1082 | - 0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F, |
|
1083 | - 0xBC8EC511, 0x38BC46E9, 0xC6E6FA14, 0xBAE8584A, |
|
1084 | - 0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B, |
|
1085 | - 0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264, |
|
1086 | - 0x92544A8B, 0x009B4FC3, 0xABA68CED, 0x9AC96F78, |
|
1087 | - 0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688, |
|
1088 | - 0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D, |
|
1089 | - 0xF7DEBB85, 0x61FE033C, 0x16746233, 0x3C034C28, |
|
1090 | - 0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802, |
|
1091 | - 0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3, |
|
1092 | - 0x0C4FB99A, 0xBB325778, 0x3EC6D97B, 0x6E77A6A9, |
|
1093 | - 0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7, |
|
1094 | - 0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302, |
|
1095 | - 0xB96D8C32, 0xEBD4E7BE, 0xBE8B9D2D, 0x7979FB06, |
|
1096 | - 0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858, |
|
1097 | - 0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033, |
|
1098 | - 0xF28EBFB0, 0xF5B9C310, 0xA0EAC280, 0x08B9767A, |
|
1099 | - 0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A, |
|
1100 | - 0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4, |
|
1101 | - 0x826D2BEF, 0x4EEB8476, 0x488DCF25, 0x36C9D566, |
|
1102 | - 0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF, |
|
1103 | - 0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509, |
|
1104 | - 0xF22B017D, 0xA4173F70, 0xDD1E16C3, 0x15E0D7F9, |
|
1105 | - 0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962, |
|
1106 | - 0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C, |
|
1107 | - 0x93D29A22, 0xE32DBF9A, 0x058745B9, 0x3453DC1E, |
|
1108 | - 0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07, |
|
1109 | - 0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C, |
|
1110 | - 0x66626C1C, 0x7154C24C, 0xEA082B2A, 0x93EB2939, |
|
1111 | - 0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C, |
|
1112 | - 0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E, |
|
1113 | - 0xD3A0C108, 0xA1E7160E, 0xE4F2DFA6, 0x693ED285, |
|
1114 | - 0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378, |
|
1115 | - 0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301, |
|
1116 | - 0xC79F022F, 0x3C997E7E, 0x5E4F9504, 0x3FFAFBBD, |
|
1117 | - 0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE, |
|
1118 | - 0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567, |
|
1119 | - 0x5592A33D, 0xB5229301, 0xCFD2A87F, 0x60AEB767, |
|
1120 | - 0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2, |
|
1121 | - 0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647, |
|
1122 | - 0x97FD61A9, 0xEA7759F4, 0x2D57539D, 0x569A58CF, |
|
1123 | - 0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914, |
|
1124 | - 0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2, |
|
1125 | - 0x3FFA50BC, 0x3D40F021, 0xC3C0BDAE, 0x4958C24C, |
|
1126 | - 0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA, |
|
1127 | - 0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3 |
|
1128 | - ); |
|
1129 | - |
|
1130 | - // 256 unsigned 32 bit integers |
|
1131 | - self::$_s8 = array( |
|
1132 | - 0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095, |
|
1133 | - 0x7789F8B7, 0xE6C1121B, 0x0E241600, 0x052CE8B5, |
|
1134 | - 0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174, |
|
1135 | - 0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC, |
|
1136 | - 0xDE9ADEB1, 0x0A0CC32C, 0xBE197029, 0x84A00940, |
|
1137 | - 0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD, |
|
1138 | - 0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42, |
|
1139 | - 0xC7ECE831, 0x3F8F95E7, 0x72DF191B, 0x7580330D, |
|
1140 | - 0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164, |
|
1141 | - 0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2, |
|
1142 | - 0x12A8DDEC, 0xFDAA335D, 0x176F43E8, 0x71FB46D4, |
|
1143 | - 0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862, |
|
1144 | - 0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0, |
|
1145 | - 0x4CFDE06F, 0xC28EC4B8, 0x57E8726E, 0x647A78FC, |
|
1146 | - 0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6, |
|
1147 | - 0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C, |
|
1148 | - 0xBBD35049, 0x2998DF04, 0x980CF42A, 0x9B6DF491, |
|
1149 | - 0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E, |
|
1150 | - 0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B, |
|
1151 | - 0x3CF1D2E2, 0x19B47A38, 0x424F7618, 0x35856039, |
|
1152 | - 0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8, |
|
1153 | - 0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8, |
|
1154 | - 0x7170C608, 0x2D5E3354, 0xD4DE495A, 0x64C6D006, |
|
1155 | - 0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42, |
|
1156 | - 0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564, |
|
1157 | - 0xF8D7E54E, 0x3E378160, 0x7895CDA5, 0x859C15A5, |
|
1158 | - 0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB, |
|
1159 | - 0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472, |
|
1160 | - 0x835FFCB8, 0x6DF4C1F2, 0x96F5B195, 0xFD0AF0FC, |
|
1161 | - 0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225, |
|
1162 | - 0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8, |
|
1163 | - 0xC4618187, 0xEA7A6E98, 0x7CD16EFC, 0x1436876C, |
|
1164 | - 0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441, |
|
1165 | - 0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB, |
|
1166 | - 0xA842EEDF, 0xFDBA60B4, 0xF1907B75, 0x20E3030F, |
|
1167 | - 0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054, |
|
1168 | - 0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504, |
|
1169 | - 0xF1E47D8D, 0x844A1BE5, 0xBAE7DFDC, 0x42CBDA70, |
|
1170 | - 0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C, |
|
1171 | - 0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC, |
|
1172 | - 0x77853B53, 0x37EFFCB5, 0xC5068778, 0xE580B3E6, |
|
1173 | - 0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C, |
|
1174 | - 0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD, |
|
1175 | - 0xDD06CAA2, 0x37DF932B, 0xC4248289, 0xACF3EBC3, |
|
1176 | - 0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4, |
|
1177 | - 0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4, |
|
1178 | - 0xE87B40E4, 0xE98EA084, 0x5889E9E1, 0xEFD390FC, |
|
1179 | - 0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101, |
|
1180 | - 0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA, |
|
1181 | - 0x646F1282, 0x7523D24A, 0xE0779695, 0xF9C17A8F, |
|
1182 | - 0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF, |
|
1183 | - 0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E, |
|
1184 | - 0x11403092, 0x00DA6D77, 0x4A0CDD61, 0xAD1F4603, |
|
1185 | - 0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A, |
|
1186 | - 0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37, |
|
1187 | - 0x2DE705CA, 0x8951570F, 0xDF09822B, 0xBD691A6C, |
|
1188 | - 0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819, |
|
1189 | - 0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384, |
|
1190 | - 0x5938FA0F, 0x42399EF3, 0x36997B07, 0x0E84093D, |
|
1191 | - 0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C, |
|
1192 | - 0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347, |
|
1193 | - 0x589E8D82, 0x0D2059D1, 0xA466BB1E, 0xF8DA0A82, |
|
1194 | - 0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D, |
|
1195 | - 0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E |
|
1196 | - ); |
|
1197 | - } |
|
1198 | - |
|
1199 | - |
|
1200 | - /** |
|
1201 | - * Indicates this is a block cipher |
|
1202 | - * |
|
1203 | - * @return integer Returns Cipher::BLOCK |
|
1204 | - */ |
|
1205 | - public function type() |
|
1206 | - { |
|
1207 | - return parent::BLOCK; |
|
1208 | - } |
|
352 | + // two loops, each loop does 16 bytes for a total of 32 bytes |
|
353 | + for ($i = 0; $i < 2; ++$i) |
|
354 | + { |
|
355 | + // z0z1z2z3 = x0x1x2x3 ^ S5[xD] ^ S6[xF] ^ S7[xC] ^ S8[xE] ^ S7[x8] |
|
356 | + $tmp = substr($x, 0x00, 4); |
|
357 | + $tmp = parent::dec2Str( |
|
358 | + parent::uInt32( |
|
359 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0d])] ^ |
|
360 | + self::$_s6[ord($x[0x0f])] ^ self::$_s7[ord($x[0x0c])] ^ |
|
361 | + self::$_s8[ord($x[0x0e])] ^ self::$_s7[ord($x[0x08])] |
|
362 | + ), 4); |
|
363 | + $z = substr_replace($z, $tmp, 0x00, 4); |
|
364 | + |
|
365 | + //print "Z0: ".parent::str2Hex($z)." (".strlen($z).")\n"; |
|
366 | + |
|
367 | + // z4z5z6z7 = x8x9xAxB ^ S5[z0] ^ S6[z2] ^ S7[z1] ^ S8[z3] ^ S8[xA] |
|
368 | + $tmp = substr($x, 0x08, 4); |
|
369 | + $tmp = parent::dec2Str( |
|
370 | + parent::uInt32( |
|
371 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x00])] ^ |
|
372 | + self::$_s6[ord($z[0x02])] ^ self::$_s7[ord($z[0x01])] ^ |
|
373 | + self::$_s8[ord($z[0x03])] ^ self::$_s8[ord($x[0x0a])] |
|
374 | + ), 4); |
|
375 | + $z = substr_replace($z, $tmp, 0x04, 4); |
|
376 | + |
|
377 | + // z8z9zAzB = xCxDxExF ^ S5[z7] ^ S6[z6] ^ S7[z5] ^ S8[z4] ^ S5[x9] |
|
378 | + $tmp = substr($x, 0x0c, 4); |
|
379 | + $tmp = parent::dec2Str( |
|
380 | + parent::uInt32( |
|
381 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x07])] ^ |
|
382 | + self::$_s6[ord($z[0x06])] ^ self::$_s7[ord($z[0x05])] ^ |
|
383 | + self::$_s8[ord($z[0x04])] ^ self::$_s5[ord($x[0x09])] |
|
384 | + ), 4); |
|
385 | + $z = substr_replace($z, $tmp, 0x08, 4); |
|
386 | + |
|
387 | + // zCzDzEzF = x4x5x6x7 ^ S5[zA] ^ S6[z9] ^ S7[zB] ^ S8[z8] ^ S6[xB] |
|
388 | + $tmp = substr($x, 0x04, 4); |
|
389 | + $tmp = parent::dec2Str( |
|
390 | + parent::uInt32( |
|
391 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x0a])] ^ |
|
392 | + self::$_s6[ord($z[0x09])] ^ self::$_s7[ord($z[0x0b])] ^ |
|
393 | + self::$_s8[ord($z[0x08])] ^ self::$_s6[ord($x[0x0b])] |
|
394 | + ), 4); |
|
395 | + $z = substr_replace($z, $tmp, 0x0c, 4); |
|
396 | + |
|
397 | + //print "Z: ".parent::str2Hex($z)." (".strlen($z).")\n"; |
|
398 | + |
|
399 | + // K1 = S5[z8] ^ S6[z9] ^ S7[z7] ^ S8[z6] ^ S5[z2] |
|
400 | + $skey[] = parent::uInt32(self::$_s5[ord($z[0x08])] ^ self::$_s6[ord($z[0x09])] ^ |
|
401 | + self::$_s7[ord($z[0x07])] ^ self::$_s8[ord($z[0x06])] ^ |
|
402 | + self::$_s5[ord($z[0x02])] |
|
403 | + ); |
|
404 | + |
|
405 | + // K2 = S5[zA] ^ S6[zB] ^ S7[z5] ^ S8[z4] ^ S6[z6] |
|
406 | + $skey[] = parent::uInt32( |
|
407 | + self::$_s5[ord($z[0x0a])] ^ self::$_s6[ord($z[0x0b])] ^ |
|
408 | + self::$_s7[ord($z[0x05])] ^ self::$_s8[ord($z[0x04])] ^ |
|
409 | + self::$_s6[ord($z[0x06])] |
|
410 | + ); |
|
411 | + |
|
412 | + // K3 = S5[zC] ^ S6[zD] ^ S7[z3] ^ S8[z2] ^ S7[z9] |
|
413 | + $skey[] = parent::uInt32( |
|
414 | + self::$_s5[ord($z[0x0c])] ^ self::$_s6[ord($z[0x0d])] ^ |
|
415 | + self::$_s7[ord($z[0x03])] ^ self::$_s8[ord($z[0x02])] ^ |
|
416 | + self::$_s7[ord($z[0x09])] |
|
417 | + ); |
|
418 | + |
|
419 | + // K4 = S5[zE] ^ S6[zF] ^ S7[z1] ^ S8[z0] ^ S8[zC] |
|
420 | + $skey[] = parent::uInt32( |
|
421 | + self::$_s5[ord($z[0x0e])] ^ self::$_s6[ord($z[0x0f])] ^ |
|
422 | + self::$_s7[ord($z[0x01])] ^ self::$_s8[ord($z[0x00])] ^ |
|
423 | + self::$_s8[ord($z[0x0c])] |
|
424 | + ); |
|
425 | + |
|
426 | + // x0x1x2x3 = z8z9zAzB ^ S5[z5] ^ S6[z7] ^ S7[z4] ^ S8[z6] ^ S7[z0] |
|
427 | + $tmp = substr($z, 0x08, 4); |
|
428 | + $tmp = parent::dec2Str( |
|
429 | + parent::uInt32( |
|
430 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x05])] ^ |
|
431 | + self::$_s6[ord($z[0x07])] ^ self::$_s7[ord($z[0x04])] ^ |
|
432 | + self::$_s8[ord($z[0x06])] ^ self::$_s7[ord($z[0x00])] |
|
433 | + ), 4); |
|
434 | + $x = substr_replace($x, $tmp, 0x00, 4); |
|
435 | + |
|
436 | + // x4x5x6x7 = z0z1z2z3 ^ S5[x0] ^ S6[x2] ^ S7[x1] ^ S8[x3] ^ S8[z2] |
|
437 | + $tmp = substr($z, 0x00, 4); |
|
438 | + $tmp = parent::dec2Str( |
|
439 | + parent::uInt32( |
|
440 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x00])] ^ |
|
441 | + self::$_s6[ord($x[0x02])] ^ self::$_s7[ord($x[0x01])] ^ |
|
442 | + self::$_s8[ord($x[0x03])] ^ self::$_s8[ord($z[0x02])] |
|
443 | + ), 4); |
|
444 | + $x = substr_replace($x, $tmp, 0x04, 4); |
|
445 | + |
|
446 | + // x8x9xAxB = z4z5z6z7 ^ S5[x7] ^ S6[x6] ^ S7[x5] ^ S8[x4] ^ S5[z1] |
|
447 | + $tmp = substr($z, 0x04, 4); |
|
448 | + $tmp = parent::dec2Str( |
|
449 | + parent::uInt32( |
|
450 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x07])] ^ |
|
451 | + self::$_s6[ord($x[0x06])] ^ self::$_s7[ord($x[0x05])] ^ |
|
452 | + self::$_s8[ord($x[0x04])] ^ self::$_s5[ord($z[0x01])] |
|
453 | + ), 4); |
|
454 | + $x = substr_replace($x, $tmp, 0x08, 4); |
|
455 | + |
|
456 | + // xCxDxExF = zCzDzEzF ^ S5[xA] ^ S6[x9] ^ S7[xB] ^ S8[x8] ^ S6[z3] |
|
457 | + $tmp = substr($z, 0x0c, 4); |
|
458 | + $tmp = parent::dec2Str( |
|
459 | + parent::uInt32( |
|
460 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0a])] ^ |
|
461 | + self::$_s6[ord($x[0x09])] ^ self::$_s7[ord($x[0x0b])] ^ |
|
462 | + self::$_s8[ord($x[0x08])] ^ self::$_s6[ord($z[0x03])] |
|
463 | + ), 4); |
|
464 | + $x = substr_replace($x, $tmp, 0x0c, 4); |
|
465 | + |
|
466 | + // K5 = S5[x3] ^ S6[x2] ^ S7[xC] ^ S8[xD] ^ S5[x8] |
|
467 | + $skey[] = parent::uInt32( |
|
468 | + self::$_s5[ord($x[0x03])] ^ self::$_s6[ord($x[0x02])] ^ |
|
469 | + self::$_s7[ord($x[0x0c])] ^ self::$_s8[ord($x[0x0d])] ^ |
|
470 | + self::$_s5[ord($x[0x08])] |
|
471 | + ); |
|
472 | + |
|
473 | + // K6 = S5[x1] ^ S6[x0] ^ S7[xE] ^ S8[xF] ^ S6[xD] |
|
474 | + $skey[] = parent::uInt32( |
|
475 | + self::$_s5[ord($x[0x01])] ^ self::$_s6[ord($x[0x00])] ^ |
|
476 | + self::$_s7[ord($x[0x0e])] ^ self::$_s8[ord($x[0x0f])] ^ |
|
477 | + self::$_s6[ord($x[0x0d])] |
|
478 | + ); |
|
479 | + |
|
480 | + // K7 = S5[x7] ^ S6[x6] ^ S7[x8] ^ S8[x9] ^ S7[x3] |
|
481 | + $skey[] = parent::uInt32( |
|
482 | + self::$_s5[ord($x[0x07])] ^ self::$_s6[ord($x[0x06])] ^ |
|
483 | + self::$_s7[ord($x[0x08])] ^ self::$_s8[ord($x[0x09])] ^ |
|
484 | + self::$_s7[ord($x[0x03])] |
|
485 | + ); |
|
486 | + |
|
487 | + // K8 = S5[x5] ^ S6[x4] ^ S7[xA] ^ S8[xB] ^ S8[x7] |
|
488 | + $skey[] = parent::uInt32( |
|
489 | + self::$_s5[ord($x[0x05])] ^ self::$_s6[ord($x[0x04])] ^ |
|
490 | + self::$_s7[ord($x[0x0a])] ^ self::$_s8[ord($x[0x0b])] ^ |
|
491 | + self::$_s8[ord($x[0x07])] |
|
492 | + ); |
|
493 | + |
|
494 | + // z0z1z2z3 = x0x1x2x3 ^ S5[xD] ^ S6[xF] ^ S7[xC] ^ S8[xE] ^ S7[x8] |
|
495 | + $tmp = substr($x, 0x00, 4); |
|
496 | + $tmp = parent::dec2Str( |
|
497 | + parent::uInt32( |
|
498 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0d])] ^ |
|
499 | + self::$_s6[ord($x[0x0f])] ^ self::$_s7[ord($x[0x0c])] ^ |
|
500 | + self::$_s8[ord($x[0x0e])] ^ self::$_s7[ord($x[0x08])] |
|
501 | + ), 4); |
|
502 | + $z = substr_replace($z, $tmp, 0x00, 4); |
|
503 | + |
|
504 | + // z4z5z6z7 = x8x9xAxB ^ S5[z0] ^ S6[z2] ^ S7[z1] ^ S8[z3] ^ S8[xA] |
|
505 | + $tmp = substr($x, 0x08, 4); |
|
506 | + $tmp = parent::dec2Str( |
|
507 | + parent::uInt32( |
|
508 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x00])] ^ |
|
509 | + self::$_s6[ord($z[0x02])] ^ self::$_s7[ord($z[0x01])] ^ |
|
510 | + self::$_s8[ord($z[0x03])] ^ self::$_s8[ord($x[0x0a])] |
|
511 | + ), 4); |
|
512 | + $z = substr_replace($z, $tmp, 0x04, 4); |
|
513 | + |
|
514 | + // z8z9zAzB = xCxDxExF ^ S5[z7] ^ S6[z6] ^ S7[z5] ^ S8[z4] ^ S5[x9] |
|
515 | + $tmp = substr($x, 0x0c, 4); |
|
516 | + $tmp = parent::dec2Str( |
|
517 | + parent::uInt32( |
|
518 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x07])] ^ |
|
519 | + self::$_s6[ord($z[0x06])] ^ self::$_s7[ord($z[0x05])] ^ |
|
520 | + self::$_s8[ord($z[0x04])] ^ self::$_s5[ord($x[0x09])] |
|
521 | + ), 4); |
|
522 | + $z = substr_replace($z, $tmp, 0x08, 4); |
|
523 | + |
|
524 | + // zCzDzEzF = x4x5x6x7 ^ S5[zA] ^ S6[z9] ^ S7[zB] ^ S8[z8] ^ S6[xB] |
|
525 | + $tmp = substr($x, 0x04, 4); |
|
526 | + $tmp = parent::dec2Str( |
|
527 | + parent::uInt32( |
|
528 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x0a])] ^ |
|
529 | + self::$_s6[ord($z[0x09])] ^ self::$_s7[ord($z[0x0b])] ^ |
|
530 | + self::$_s8[ord($z[0x08])] ^ self::$_s6[ord($x[0x0b])] |
|
531 | + ), 4); |
|
532 | + $z = substr_replace($z, $tmp, 0x0c, 4); |
|
533 | + |
|
534 | + // K9 = S5[z3] ^ S6[z2] ^ S7[zC] ^ S8[zD] ^ S5[z9] |
|
535 | + $skey[] = parent::uInt32( |
|
536 | + self::$_s5[ord($z[0x03])] ^ self::$_s6[ord($z[0x02])] ^ |
|
537 | + self::$_s7[ord($z[0x0c])] ^ self::$_s8[ord($z[0x0d])] ^ |
|
538 | + self::$_s5[ord($z[0x09])] |
|
539 | + ); |
|
540 | + |
|
541 | + // K10 = S5[z1] ^ S6[z0] ^ S7[zE] ^ S8[zF] ^ S6[zC] |
|
542 | + $skey[] = parent::uInt32( |
|
543 | + self::$_s5[ord($z[0x01])] ^ self::$_s6[ord($z[0x00])] ^ |
|
544 | + self::$_s7[ord($z[0x0e])] ^ self::$_s8[ord($z[0x0f])] ^ |
|
545 | + self::$_s6[ord($z[0x0c])] |
|
546 | + ); |
|
547 | + |
|
548 | + // K11 = S5[z7] ^ S6[z6] ^ S7[z8] ^ S8[z9] ^ S7[z2] |
|
549 | + $skey[] = parent::uInt32( |
|
550 | + self::$_s5[ord($z[0x07])] ^ self::$_s6[ord($z[0x06])] ^ |
|
551 | + self::$_s7[ord($z[0x08])] ^ self::$_s8[ord($z[0x09])] ^ |
|
552 | + self::$_s7[ord($z[0x02])] |
|
553 | + ); |
|
554 | + |
|
555 | + // K12 = S5[z5] ^ S6[z4] ^ S7[zA] ^ S8[zB] ^ S8[z6] |
|
556 | + $skey[] = parent::uInt32( |
|
557 | + self::$_s5[ord($z[0x05])] ^ self::$_s6[ord($z[0x04])] ^ |
|
558 | + self::$_s7[ord($z[0x0a])] ^ self::$_s8[ord($z[0x0b])] ^ |
|
559 | + self::$_s8[ord($z[0x06])] |
|
560 | + ); |
|
561 | + |
|
562 | + // x0x1x2x3 = z8z9zAzB ^ S5[z5] ^ S6[z7] ^ S7[z4] ^ S8[z6] ^ S7[z0] |
|
563 | + $tmp = substr($z, 0x08, 4); |
|
564 | + $tmp = parent::dec2Str( |
|
565 | + parent::uInt32( |
|
566 | + parent::str2Dec($tmp) ^ self::$_s5[ord($z[0x05])] ^ |
|
567 | + self::$_s6[ord($z[0x07])] ^ self::$_s7[ord($z[0x04])] ^ |
|
568 | + self::$_s8[ord($z[0x06])] ^ self::$_s7[ord($z[0x00])] |
|
569 | + ), 4); |
|
570 | + $x = substr_replace($x, $tmp, 0x00, 4); |
|
571 | + |
|
572 | + // x4x5x6x7 = z0z1z2z3 ^ S5[x0] ^ S6[x2] ^ S7[x1] ^ S8[x3] ^ S8[z2] |
|
573 | + $tmp = substr($z, 0x00, 4); |
|
574 | + $tmp = parent::dec2Str( |
|
575 | + parent::uInt32( |
|
576 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x00])] ^ |
|
577 | + self::$_s6[ord($x[0x02])] ^ self::$_s7[ord($x[0x01])] ^ |
|
578 | + self::$_s8[ord($x[0x03])] ^ self::$_s8[ord($z[0x02])] |
|
579 | + ), 4); |
|
580 | + $x = substr_replace($x, $tmp, 0x04, 4); |
|
581 | + |
|
582 | + // x8x9xAxB = z4z5z6z7 ^ S5[x7] ^ S6[x6] ^ S7[x5] ^ S8[x4] ^ S5[z1] |
|
583 | + $tmp = substr($z, 0x04, 4); |
|
584 | + $tmp = parent::dec2Str( |
|
585 | + parent::uInt32( |
|
586 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x07])] ^ |
|
587 | + self::$_s6[ord($x[0x06])] ^ self::$_s7[ord($x[0x05])] ^ |
|
588 | + self::$_s8[ord($x[0x04])] ^ self::$_s5[ord($z[0x01])] |
|
589 | + ), 4); |
|
590 | + $x = substr_replace($x, $tmp, 0x08, 4); |
|
591 | + |
|
592 | + // xCxDxExF = zCzDzEzF ^ S5[xA] ^ S6[x9] ^ S7[xB] ^ S8[x8] ^ S6[z3] |
|
593 | + $tmp = substr($z, 0x0c, 4); |
|
594 | + $tmp = parent::dec2Str( |
|
595 | + parent::uInt32( |
|
596 | + parent::str2Dec($tmp) ^ self::$_s5[ord($x[0x0a])] ^ |
|
597 | + self::$_s6[ord($x[0x09])] ^ self::$_s7[ord($x[0x0b])] ^ |
|
598 | + self::$_s8[ord($x[0x08])] ^ self::$_s6[ord($z[0x03])] |
|
599 | + ), 4); |
|
600 | + $x = substr_replace($x, $tmp, 0x0c, 4); |
|
601 | + |
|
602 | + // K13 = S5[x8] ^ S6[x9] ^ S7[x7] ^ S8[x6] ^ S5[x3] |
|
603 | + $skey[] = parent::uInt32( |
|
604 | + self::$_s5[ord($x[0x08])] ^ self::$_s6[ord($x[0x09])] ^ |
|
605 | + self::$_s7[ord($x[0x07])] ^ self::$_s8[ord($x[0x06])] ^ |
|
606 | + self::$_s5[ord($x[0x03])] |
|
607 | + ); |
|
608 | + |
|
609 | + // K14 = S5[xA] ^ S6[xB] ^ S7[x5] ^ S8[x4] ^ S6[x7] |
|
610 | + $skey[] = parent::uInt32( |
|
611 | + self::$_s5[ord($x[0x0a])] ^ self::$_s6[ord($x[0x0b])] ^ |
|
612 | + self::$_s7[ord($x[0x05])] ^ self::$_s8[ord($x[0x04])] ^ |
|
613 | + self::$_s6[ord($x[0x07])] |
|
614 | + ); |
|
615 | + |
|
616 | + // K15 = S5[xC] ^ S6[xD] ^ S7[x3] ^ S8[x2] ^ S7[x8] |
|
617 | + $skey[] = parent::uInt32( |
|
618 | + self::$_s5[ord($x[0x0c])] ^ self::$_s6[ord($x[0x0d])] ^ |
|
619 | + self::$_s7[ord($x[0x03])] ^ self::$_s8[ord($x[0x02])] ^ |
|
620 | + self::$_s7[ord($x[0x08])] |
|
621 | + ); |
|
622 | + |
|
623 | + // K16 = S5[xE] ^ S6[xF] ^ S7[x1] ^ S8[x0] ^ S8[xD] |
|
624 | + $skey[] = parent::uInt32( |
|
625 | + self::$_s5[ord($x[0x0e])] ^ self::$_s6[ord($x[0x0f])] ^ |
|
626 | + self::$_s7[ord($x[0x01])] ^ self::$_s8[ord($x[0x00])] ^ |
|
627 | + self::$_s8[ord($x[0x0d])] |
|
628 | + ); |
|
629 | + } |
|
630 | + |
|
631 | + // create the 16 byte masking and rotate subkeys |
|
632 | + $this->_mkey = array_slice($skey, 0, 16); |
|
633 | + $this->_rkey = array_slice($skey, 16, 16); |
|
634 | + |
|
635 | + // $_rkey only uses the least significant 5 bits |
|
636 | + $this->_rkey = array_map(function($v) { |
|
637 | + return $v &= 31; |
|
638 | + }, $this->_rkey); |
|
639 | + |
|
640 | + // there is 4kb in the s5 - s8 sboxes, which are not needed after we |
|
641 | + // create the subkeys, so free up the memory. unset() doesn't work here |
|
642 | + for ($i = 5; $i <= 8; ++$i) |
|
643 | + self::${"_s$i"} = null; |
|
644 | + } |
|
645 | + |
|
646 | + |
|
647 | + /** |
|
648 | + * Initialize the tables. |
|
649 | + * |
|
650 | + * @return void |
|
651 | + */ |
|
652 | + private function initTables() |
|
653 | + { |
|
654 | + // 256 unsigned 32 bit integers |
|
655 | + self::$_s1 = array( |
|
656 | + 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, |
|
657 | + 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, |
|
658 | + 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, |
|
659 | + 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, |
|
660 | + 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, |
|
661 | + 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, |
|
662 | + 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, |
|
663 | + 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, |
|
664 | + 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, |
|
665 | + 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, |
|
666 | + 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, |
|
667 | + 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, |
|
668 | + 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, |
|
669 | + 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, |
|
670 | + 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, |
|
671 | + 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, |
|
672 | + 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, |
|
673 | + 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, |
|
674 | + 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, |
|
675 | + 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, |
|
676 | + 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, |
|
677 | + 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, |
|
678 | + 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, |
|
679 | + 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, |
|
680 | + 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, |
|
681 | + 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, |
|
682 | + 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, |
|
683 | + 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, |
|
684 | + 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, |
|
685 | + 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, |
|
686 | + 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, |
|
687 | + 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, |
|
688 | + 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, |
|
689 | + 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, |
|
690 | + 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, |
|
691 | + 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, |
|
692 | + 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, |
|
693 | + 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, |
|
694 | + 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, |
|
695 | + 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, |
|
696 | + 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, |
|
697 | + 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, |
|
698 | + 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, |
|
699 | + 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, |
|
700 | + 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, |
|
701 | + 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, |
|
702 | + 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, |
|
703 | + 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, |
|
704 | + 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, |
|
705 | + 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, |
|
706 | + 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, |
|
707 | + 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, |
|
708 | + 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, |
|
709 | + 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, |
|
710 | + 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, |
|
711 | + 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, |
|
712 | + 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, |
|
713 | + 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, |
|
714 | + 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, |
|
715 | + 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, |
|
716 | + 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, |
|
717 | + 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, |
|
718 | + 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, |
|
719 | + 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF |
|
720 | + ); |
|
721 | + |
|
722 | + // 256 unsigned 32 bit integers |
|
723 | + self::$_s2 = array( |
|
724 | + 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, |
|
725 | + 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, |
|
726 | + 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, |
|
727 | + 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, |
|
728 | + 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, |
|
729 | + 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, |
|
730 | + 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, |
|
731 | + 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, |
|
732 | + 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, |
|
733 | + 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, |
|
734 | + 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, |
|
735 | + 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, |
|
736 | + 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, |
|
737 | + 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, |
|
738 | + 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, |
|
739 | + 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, |
|
740 | + 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, |
|
741 | + 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, |
|
742 | + 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, |
|
743 | + 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, |
|
744 | + 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, |
|
745 | + 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, |
|
746 | + 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, |
|
747 | + 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, |
|
748 | + 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, |
|
749 | + 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, |
|
750 | + 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, |
|
751 | + 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, |
|
752 | + 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, |
|
753 | + 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, |
|
754 | + 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, |
|
755 | + 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, |
|
756 | + 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, |
|
757 | + 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, |
|
758 | + 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, |
|
759 | + 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, |
|
760 | + 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, |
|
761 | + 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, |
|
762 | + 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, |
|
763 | + 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, |
|
764 | + 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, |
|
765 | + 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, |
|
766 | + 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, |
|
767 | + 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, |
|
768 | + 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, |
|
769 | + 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, |
|
770 | + 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, |
|
771 | + 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, |
|
772 | + 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, |
|
773 | + 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, |
|
774 | + 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, |
|
775 | + 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, |
|
776 | + 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, |
|
777 | + 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, |
|
778 | + 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, |
|
779 | + 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, |
|
780 | + 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, |
|
781 | + 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, |
|
782 | + 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, |
|
783 | + 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, |
|
784 | + 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, |
|
785 | + 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, |
|
786 | + 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, |
|
787 | + 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 |
|
788 | + ); |
|
789 | + |
|
790 | + // 256 unsigned 32 bit integers |
|
791 | + self::$_s3 = array( |
|
792 | + 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, |
|
793 | + 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, |
|
794 | + 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, |
|
795 | + 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, |
|
796 | + 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, |
|
797 | + 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, |
|
798 | + 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, |
|
799 | + 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, |
|
800 | + 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, |
|
801 | + 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, |
|
802 | + 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, |
|
803 | + 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, |
|
804 | + 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, |
|
805 | + 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, |
|
806 | + 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, |
|
807 | + 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, |
|
808 | + 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, |
|
809 | + 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, |
|
810 | + 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, |
|
811 | + 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, |
|
812 | + 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, |
|
813 | + 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, |
|
814 | + 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, |
|
815 | + 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, |
|
816 | + 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, |
|
817 | + 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, |
|
818 | + 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, |
|
819 | + 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, |
|
820 | + 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, |
|
821 | + 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, |
|
822 | + 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, |
|
823 | + 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, |
|
824 | + 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, |
|
825 | + 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, |
|
826 | + 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, |
|
827 | + 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, |
|
828 | + 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, |
|
829 | + 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, |
|
830 | + 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, |
|
831 | + 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, |
|
832 | + 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, |
|
833 | + 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, |
|
834 | + 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, |
|
835 | + 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, |
|
836 | + 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, |
|
837 | + 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, |
|
838 | + 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, |
|
839 | + 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, |
|
840 | + 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, |
|
841 | + 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, |
|
842 | + 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, |
|
843 | + 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, |
|
844 | + 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, |
|
845 | + 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, |
|
846 | + 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, |
|
847 | + 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, |
|
848 | + 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, |
|
849 | + 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, |
|
850 | + 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, |
|
851 | + 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, |
|
852 | + 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, |
|
853 | + 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, |
|
854 | + 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, |
|
855 | + 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 |
|
856 | + ); |
|
857 | + |
|
858 | + // 256 unsigned 32 bit integers |
|
859 | + self::$_s4 = array( |
|
860 | + 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, |
|
861 | + 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, |
|
862 | + 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, |
|
863 | + 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, |
|
864 | + 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, |
|
865 | + 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, |
|
866 | + 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, |
|
867 | + 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, |
|
868 | + 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, |
|
869 | + 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, |
|
870 | + 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, |
|
871 | + 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, |
|
872 | + 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, |
|
873 | + 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, |
|
874 | + 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, |
|
875 | + 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, |
|
876 | + 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, |
|
877 | + 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, |
|
878 | + 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, |
|
879 | + 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, |
|
880 | + 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, |
|
881 | + 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, |
|
882 | + 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, |
|
883 | + 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, |
|
884 | + 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, |
|
885 | + 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, |
|
886 | + 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, |
|
887 | + 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, |
|
888 | + 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, |
|
889 | + 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, |
|
890 | + 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, |
|
891 | + 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, |
|
892 | + 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, |
|
893 | + 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, |
|
894 | + 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, |
|
895 | + 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, |
|
896 | + 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, |
|
897 | + 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, |
|
898 | + 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, |
|
899 | + 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, |
|
900 | + 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, |
|
901 | + 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, |
|
902 | + 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, |
|
903 | + 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, |
|
904 | + 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, |
|
905 | + 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, |
|
906 | + 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, |
|
907 | + 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, |
|
908 | + 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, |
|
909 | + 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, |
|
910 | + 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, |
|
911 | + 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, |
|
912 | + 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, |
|
913 | + 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, |
|
914 | + 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, |
|
915 | + 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, |
|
916 | + 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, |
|
917 | + 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, |
|
918 | + 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, |
|
919 | + 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, |
|
920 | + 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, |
|
921 | + 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, |
|
922 | + 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, |
|
923 | + 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 |
|
924 | + ); |
|
925 | + |
|
926 | + // 256 unsigned 32 bit integers |
|
927 | + self::$_s5 = array( |
|
928 | + 0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911, |
|
929 | + 0xB86A7FFF, 0x1DD358F5, 0x44DD9D44, 0x1731167F, |
|
930 | + 0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00, |
|
931 | + 0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A, |
|
932 | + 0xE6A2E77F, 0xF0C720CD, 0xC4494816, 0xCCF5C180, |
|
933 | + 0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF, |
|
934 | + 0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2, |
|
935 | + 0x4E40B48D, 0x248EB6FB, 0x8DBA1CFE, 0x41A99B02, |
|
936 | + 0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725, |
|
937 | + 0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A, |
|
938 | + 0xF2F3F763, 0x68AF8040, 0xED0C9E56, 0x11B4958B, |
|
939 | + 0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7, |
|
940 | + 0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571, |
|
941 | + 0x0C05372A, 0x578535F2, 0x2261BE02, 0xD642A0C9, |
|
942 | + 0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC, |
|
943 | + 0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981, |
|
944 | + 0x5C1FF900, 0xFE38D399, 0x0C4EFF0B, 0x062407EA, |
|
945 | + 0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774, |
|
946 | + 0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263, |
|
947 | + 0xDF65001F, 0x0EC50966, 0xDFDD55BC, 0x29DE0655, |
|
948 | + 0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468, |
|
949 | + 0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2, |
|
950 | + 0xBCF3F0AA, 0x87AC36E9, 0xE53A7426, 0x01B3D82B, |
|
951 | + 0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910, |
|
952 | + 0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284, |
|
953 | + 0x636737B6, 0x50F5B616, 0xF24766E3, 0x8ECA36C1, |
|
954 | + 0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4, |
|
955 | + 0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA, |
|
956 | + 0x26E46695, 0xB7566419, 0xF654EFC5, 0xD08D58B7, |
|
957 | + 0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049, |
|
958 | + 0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE, |
|
959 | + 0xC62BF3CD, 0x9E0885F9, 0x68CB3E47, 0x086C010F, |
|
960 | + 0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6, |
|
961 | + 0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA, |
|
962 | + 0x0AB378D5, 0xD951FB0C, 0xDED7DA56, 0x4124BBE4, |
|
963 | + 0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE, |
|
964 | + 0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561, |
|
965 | + 0xC3DC0280, 0x05687715, 0x646C6BD7, 0x44904DB3, |
|
966 | + 0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6, |
|
967 | + 0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840, |
|
968 | + 0x76F0AE02, 0x083BE84D, 0x28421C9A, 0x44489406, |
|
969 | + 0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4, |
|
970 | + 0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472, |
|
971 | + 0x3CA5D717, 0x7D161BBA, 0x9CAD9010, 0xAF462BA2, |
|
972 | + 0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487, |
|
973 | + 0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7, |
|
974 | + 0x445F7382, 0x175683F4, 0xCDC66A97, 0x70BE0288, |
|
975 | + 0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5, |
|
976 | + 0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2, |
|
977 | + 0x1C5C1572, 0xF6721B2C, 0x1AD2FFF3, 0x8C25404E, |
|
978 | + 0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78, |
|
979 | + 0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E, |
|
980 | + 0x44094F85, 0x3F481D87, 0xFCFEAE7B, 0x77B5FF76, |
|
981 | + 0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801, |
|
982 | + 0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0, |
|
983 | + 0xDF3B0874, 0x95055110, 0x1B5AD7A8, 0xF61ED5AD, |
|
984 | + 0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58, |
|
985 | + 0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0, |
|
986 | + 0x5CE96C28, 0xE176EDA3, 0x6BAC307F, 0x376829D2, |
|
987 | + 0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20, |
|
988 | + 0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE, |
|
989 | + 0xF9E0659A, 0xEEB9491D, 0x34010718, 0xBB30CAB8, |
|
990 | + 0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55, |
|
991 | + 0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4 |
|
992 | + ); |
|
993 | + |
|
994 | + // 256 unsigned 32 bit integers |
|
995 | + self::$_s6 = array( |
|
996 | + 0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C, |
|
997 | + 0x95DB08E7, 0x016843B4, 0xECED5CBC, 0x325553AC, |
|
998 | + 0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9, |
|
999 | + 0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138, |
|
1000 | + 0x33F14961, 0xC01937BD, 0xF506C6DA, 0xE4625E7E, |
|
1001 | + 0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367, |
|
1002 | + 0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866, |
|
1003 | + 0xA084DB2D, 0x09A8486F, 0xA888614A, 0x2900AF98, |
|
1004 | + 0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C, |
|
1005 | + 0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072, |
|
1006 | + 0xFD41197E, 0x9305A6B0, 0xE86BE3DA, 0x74BED3CD, |
|
1007 | + 0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3, |
|
1008 | + 0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53, |
|
1009 | + 0x5C3D9C01, 0x64BDB941, 0x2C0E636A, 0xBA7DD9CD, |
|
1010 | + 0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D, |
|
1011 | + 0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8, |
|
1012 | + 0x284CAF89, 0xAA928223, 0x9334BE53, 0x3B3A21BF, |
|
1013 | + 0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9, |
|
1014 | + 0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807, |
|
1015 | + 0x5B7C5ECC, 0x221DB3A6, 0x9A69A02F, 0x68818A54, |
|
1016 | + 0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A, |
|
1017 | + 0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387, |
|
1018 | + 0x53BDDB65, 0xE76FFBE7, 0xE967FD78, 0x0BA93563, |
|
1019 | + 0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC, |
|
1020 | + 0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0, |
|
1021 | + 0xE81F994F, 0x9528CD89, 0xFD339FED, 0xB87834BF, |
|
1022 | + 0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE, |
|
1023 | + 0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF, |
|
1024 | + 0x4EC75B95, 0x24F2C3C0, 0x42D15D99, 0xCD0D7FA0, |
|
1025 | + 0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F, |
|
1026 | + 0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2, |
|
1027 | + 0x372B74AF, 0x692573E4, 0xE9A9D848, 0xF3160289, |
|
1028 | + 0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853, |
|
1029 | + 0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950, |
|
1030 | + 0x36F73523, 0x4CFB6E87, 0x7DA4CEC0, 0x6C152DAA, |
|
1031 | + 0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F, |
|
1032 | + 0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9, |
|
1033 | + 0x2B05D08D, 0x48B9D585, 0xDC049441, 0xC8098F9B, |
|
1034 | + 0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751, |
|
1035 | + 0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE, |
|
1036 | + 0xBF32679D, 0xD45B5B75, 0xB353FD00, 0xCBB0E358, |
|
1037 | + 0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13, |
|
1038 | + 0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397, |
|
1039 | + 0x47CF8E7A, 0xB6C85283, 0x3CC2ACFB, 0x3FC06976, |
|
1040 | + 0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459, |
|
1041 | + 0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0, |
|
1042 | + 0x3007CD3E, 0x74719EEF, 0xDC872681, 0x073340D4, |
|
1043 | + 0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891, |
|
1044 | + 0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F, |
|
1045 | + 0x6FE4AC98, 0xB173ECC0, 0xBC60B42A, 0x953498DA, |
|
1046 | + 0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB, |
|
1047 | + 0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC, |
|
1048 | + 0xE8816F4A, 0x3814F200, 0xA3F94043, 0x9C7A54C2, |
|
1049 | + 0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084, |
|
1050 | + 0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB, |
|
1051 | + 0xBA5AC7B5, 0xB6F6DEAF, 0x3A479C3A, 0x5302DA25, |
|
1052 | + 0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B, |
|
1053 | + 0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121, |
|
1054 | + 0xB81A928A, 0x60ED5869, 0x97C55B96, 0xEAEC991B, |
|
1055 | + 0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5, |
|
1056 | + 0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855, |
|
1057 | + 0xD36B4CF1, 0xF544EDEB, 0xB0E93524, 0xBEBB8FBD, |
|
1058 | + 0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454, |
|
1059 | + 0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F |
|
1060 | + ); |
|
1061 | + |
|
1062 | + // 256 unsigned 32 bit integers |
|
1063 | + self::$_s7 = array( |
|
1064 | + 0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693, |
|
1065 | + 0x2A8D7F6F, 0xAB9BC912, 0xDE6008A1, 0x2028DA1F, |
|
1066 | + 0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82, |
|
1067 | + 0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE, |
|
1068 | + 0xA05FBCF6, 0xCD4181E9, 0xE150210C, 0xE24EF1BD, |
|
1069 | + 0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43, |
|
1070 | + 0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F, |
|
1071 | + 0x089766BE, 0xBAEEADF4, 0x1286BECF, 0xB6EACB19, |
|
1072 | + 0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9, |
|
1073 | + 0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2, |
|
1074 | + 0x107789BE, 0xB3B2E9CE, 0x0502AA8F, 0x0BC0351E, |
|
1075 | + 0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516, |
|
1076 | + 0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83, |
|
1077 | + 0x334266CE, 0x8C9341B7, 0xD0D854C0, 0xCB3A6C88, |
|
1078 | + 0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E, |
|
1079 | + 0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816, |
|
1080 | + 0x0A961288, 0xE1A5C06E, 0x13749E67, 0x72FC081A, |
|
1081 | + 0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756, |
|
1082 | + 0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F, |
|
1083 | + 0xBC8EC511, 0x38BC46E9, 0xC6E6FA14, 0xBAE8584A, |
|
1084 | + 0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B, |
|
1085 | + 0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264, |
|
1086 | + 0x92544A8B, 0x009B4FC3, 0xABA68CED, 0x9AC96F78, |
|
1087 | + 0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688, |
|
1088 | + 0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D, |
|
1089 | + 0xF7DEBB85, 0x61FE033C, 0x16746233, 0x3C034C28, |
|
1090 | + 0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802, |
|
1091 | + 0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3, |
|
1092 | + 0x0C4FB99A, 0xBB325778, 0x3EC6D97B, 0x6E77A6A9, |
|
1093 | + 0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7, |
|
1094 | + 0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302, |
|
1095 | + 0xB96D8C32, 0xEBD4E7BE, 0xBE8B9D2D, 0x7979FB06, |
|
1096 | + 0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858, |
|
1097 | + 0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033, |
|
1098 | + 0xF28EBFB0, 0xF5B9C310, 0xA0EAC280, 0x08B9767A, |
|
1099 | + 0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A, |
|
1100 | + 0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4, |
|
1101 | + 0x826D2BEF, 0x4EEB8476, 0x488DCF25, 0x36C9D566, |
|
1102 | + 0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF, |
|
1103 | + 0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509, |
|
1104 | + 0xF22B017D, 0xA4173F70, 0xDD1E16C3, 0x15E0D7F9, |
|
1105 | + 0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962, |
|
1106 | + 0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C, |
|
1107 | + 0x93D29A22, 0xE32DBF9A, 0x058745B9, 0x3453DC1E, |
|
1108 | + 0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07, |
|
1109 | + 0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C, |
|
1110 | + 0x66626C1C, 0x7154C24C, 0xEA082B2A, 0x93EB2939, |
|
1111 | + 0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C, |
|
1112 | + 0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E, |
|
1113 | + 0xD3A0C108, 0xA1E7160E, 0xE4F2DFA6, 0x693ED285, |
|
1114 | + 0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378, |
|
1115 | + 0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301, |
|
1116 | + 0xC79F022F, 0x3C997E7E, 0x5E4F9504, 0x3FFAFBBD, |
|
1117 | + 0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE, |
|
1118 | + 0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567, |
|
1119 | + 0x5592A33D, 0xB5229301, 0xCFD2A87F, 0x60AEB767, |
|
1120 | + 0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2, |
|
1121 | + 0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647, |
|
1122 | + 0x97FD61A9, 0xEA7759F4, 0x2D57539D, 0x569A58CF, |
|
1123 | + 0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914, |
|
1124 | + 0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2, |
|
1125 | + 0x3FFA50BC, 0x3D40F021, 0xC3C0BDAE, 0x4958C24C, |
|
1126 | + 0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA, |
|
1127 | + 0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3 |
|
1128 | + ); |
|
1129 | + |
|
1130 | + // 256 unsigned 32 bit integers |
|
1131 | + self::$_s8 = array( |
|
1132 | + 0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095, |
|
1133 | + 0x7789F8B7, 0xE6C1121B, 0x0E241600, 0x052CE8B5, |
|
1134 | + 0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174, |
|
1135 | + 0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC, |
|
1136 | + 0xDE9ADEB1, 0x0A0CC32C, 0xBE197029, 0x84A00940, |
|
1137 | + 0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD, |
|
1138 | + 0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42, |
|
1139 | + 0xC7ECE831, 0x3F8F95E7, 0x72DF191B, 0x7580330D, |
|
1140 | + 0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164, |
|
1141 | + 0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2, |
|
1142 | + 0x12A8DDEC, 0xFDAA335D, 0x176F43E8, 0x71FB46D4, |
|
1143 | + 0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862, |
|
1144 | + 0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0, |
|
1145 | + 0x4CFDE06F, 0xC28EC4B8, 0x57E8726E, 0x647A78FC, |
|
1146 | + 0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6, |
|
1147 | + 0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C, |
|
1148 | + 0xBBD35049, 0x2998DF04, 0x980CF42A, 0x9B6DF491, |
|
1149 | + 0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E, |
|
1150 | + 0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B, |
|
1151 | + 0x3CF1D2E2, 0x19B47A38, 0x424F7618, 0x35856039, |
|
1152 | + 0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8, |
|
1153 | + 0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8, |
|
1154 | + 0x7170C608, 0x2D5E3354, 0xD4DE495A, 0x64C6D006, |
|
1155 | + 0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42, |
|
1156 | + 0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564, |
|
1157 | + 0xF8D7E54E, 0x3E378160, 0x7895CDA5, 0x859C15A5, |
|
1158 | + 0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB, |
|
1159 | + 0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472, |
|
1160 | + 0x835FFCB8, 0x6DF4C1F2, 0x96F5B195, 0xFD0AF0FC, |
|
1161 | + 0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225, |
|
1162 | + 0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8, |
|
1163 | + 0xC4618187, 0xEA7A6E98, 0x7CD16EFC, 0x1436876C, |
|
1164 | + 0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441, |
|
1165 | + 0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB, |
|
1166 | + 0xA842EEDF, 0xFDBA60B4, 0xF1907B75, 0x20E3030F, |
|
1167 | + 0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054, |
|
1168 | + 0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504, |
|
1169 | + 0xF1E47D8D, 0x844A1BE5, 0xBAE7DFDC, 0x42CBDA70, |
|
1170 | + 0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C, |
|
1171 | + 0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC, |
|
1172 | + 0x77853B53, 0x37EFFCB5, 0xC5068778, 0xE580B3E6, |
|
1173 | + 0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C, |
|
1174 | + 0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD, |
|
1175 | + 0xDD06CAA2, 0x37DF932B, 0xC4248289, 0xACF3EBC3, |
|
1176 | + 0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4, |
|
1177 | + 0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4, |
|
1178 | + 0xE87B40E4, 0xE98EA084, 0x5889E9E1, 0xEFD390FC, |
|
1179 | + 0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101, |
|
1180 | + 0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA, |
|
1181 | + 0x646F1282, 0x7523D24A, 0xE0779695, 0xF9C17A8F, |
|
1182 | + 0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF, |
|
1183 | + 0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E, |
|
1184 | + 0x11403092, 0x00DA6D77, 0x4A0CDD61, 0xAD1F4603, |
|
1185 | + 0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A, |
|
1186 | + 0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37, |
|
1187 | + 0x2DE705CA, 0x8951570F, 0xDF09822B, 0xBD691A6C, |
|
1188 | + 0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819, |
|
1189 | + 0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384, |
|
1190 | + 0x5938FA0F, 0x42399EF3, 0x36997B07, 0x0E84093D, |
|
1191 | + 0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C, |
|
1192 | + 0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347, |
|
1193 | + 0x589E8D82, 0x0D2059D1, 0xA466BB1E, 0xF8DA0A82, |
|
1194 | + 0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D, |
|
1195 | + 0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E |
|
1196 | + ); |
|
1197 | + } |
|
1198 | + |
|
1199 | + |
|
1200 | + /** |
|
1201 | + * Indicates this is a block cipher |
|
1202 | + * |
|
1203 | + * @return integer Returns Cipher::BLOCK |
|
1204 | + */ |
|
1205 | + public function type() |
|
1206 | + { |
|
1207 | + return parent::BLOCK; |
|
1208 | + } |
|
1209 | 1209 | } |
1210 | 1210 | ?> |
@@ -92,8 +92,7 @@ discard block |
||
92 | 92 | { |
93 | 93 | $key = substr($key, 0, self::BYTES_KEY_MAX); |
94 | 94 | $keylen = self::BYTES_KEY_MAX; |
95 | - } |
|
96 | - else if ($keylen < self::BYTES_KEY_MIN) |
|
95 | + } else if ($keylen < self::BYTES_KEY_MIN) |
|
97 | 96 | { |
98 | 97 | $msg = PHP_Crypt::CIPHER_CAST_128." requires a key size between "; |
99 | 98 | $msg .= "5 - 16 bytes."; |
@@ -341,8 +340,9 @@ discard block |
||
341 | 340 | |
342 | 341 | // the max length of the key is 16 bytes, however if it is |
343 | 342 | // less, pad it with null to get ito to 16 bytes |
344 | - if ($this->keySize() < self::BYTES_KEY_MAX) |
|
345 | - $x = str_pad($x, self::BYTES_KEY_MAX, "\0", STR_PAD_RIGHT); |
|
343 | + if ($this->keySize() < self::BYTES_KEY_MAX) { |
|
344 | + $x = str_pad($x, self::BYTES_KEY_MAX, "\0", STR_PAD_RIGHT); |
|
345 | + } |
|
346 | 346 | |
347 | 347 | /* |
348 | 348 | * NOW FOR THE UGLY PART, THIS IS TAKEN FROM PAGE 3-4 OF |
@@ -639,8 +639,9 @@ discard block |
||
639 | 639 | |
640 | 640 | // there is 4kb in the s5 - s8 sboxes, which are not needed after we |
641 | 641 | // create the subkeys, so free up the memory. unset() doesn't work here |
642 | - for ($i = 5; $i <= 8; ++$i) |
|
643 | - self::${"_s$i"} = null; |
|
642 | + for ($i = 5; $i <= 8; ++$i) { |
|
643 | + self::${"_s$i"} = null; |
|
644 | + } |
|
644 | 645 | } |
645 | 646 | |
646 | 647 |
@@ -33,589 +33,589 @@ |
||
33 | 33 | */ |
34 | 34 | class Core |
35 | 35 | { |
36 | - /** @type integer HASH_LEN The length of md5() hash string */ |
|
37 | - const HASH_LEN = 16; |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * Constructor |
|
42 | - * |
|
43 | - */ |
|
44 | - protected function __construct() |
|
45 | - { |
|
46 | - |
|
47 | - } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Destructor |
|
52 | - * |
|
53 | - */ |
|
54 | - protected function __destruct() |
|
55 | - { |
|
56 | - |
|
57 | - } |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * Convert hexidecimal to a binary string (ex: "00110110") |
|
62 | - * |
|
63 | - * @param string $hex A string containing a hexidecimal number |
|
64 | - * @return string A string representation of a binary |
|
65 | - */ |
|
66 | - public static function hex2Bin($hex) |
|
67 | - { |
|
68 | - // if we do not have an even number of hex characters |
|
69 | - // append a 0 to the beginning to make it even |
|
70 | - if (strlen($hex) % 2) |
|
71 | - $hex = "0$hex"; |
|
72 | - |
|
73 | - $parts = str_split($hex, 2); |
|
74 | - $parts = array_map(function($v) { |
|
75 | - $v = base_convert($v, 16, 2); |
|
76 | - return str_pad($v, 8, "0", STR_PAD_LEFT); |
|
77 | - }, $parts); |
|
78 | - |
|
79 | - return implode("", $parts); |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * Convert hex to a string |
|
85 | - * |
|
86 | - * @param string $hex A string representation of Hex (IE: "1a2b3c" not 0x1a2b3c) |
|
87 | - * @return string a string |
|
88 | - */ |
|
89 | - public static function hex2Str($hex) |
|
90 | - { |
|
91 | - // php version >= 5.4 have a hex2bin function, use it |
|
92 | - // if it exists |
|
93 | - if (function_exists("hex2bin")) |
|
94 | - return hex2bin($hex); |
|
95 | - |
|
96 | - $parts = str_split($hex, 2); |
|
97 | - $parts = array_map(function($v) { |
|
98 | - return chr(Core::hex2Dec($v)); |
|
99 | - }, $parts); |
|
100 | - |
|
101 | - return implode("", $parts); |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * Converts Hex to Decimal |
|
107 | - * This function just calls php's hexdec() function, but I |
|
108 | - * encapsulated it in this function to keep things uniform |
|
109 | - * and have all possible conversion function available in |
|
110 | - * the Cipher class |
|
111 | - * |
|
112 | - * @param string $hex A hex number to convert to decimal |
|
113 | - * @return integer A decimal number |
|
114 | - */ |
|
115 | - public static function hex2Dec($hex) |
|
116 | - { |
|
117 | - return hexdec($hex); |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * Convert binary string (ie 00110110) to hex |
|
123 | - * |
|
124 | - * @param string $bin A binary string |
|
125 | - * @return string A string representation of hexidecimal number |
|
126 | - */ |
|
127 | - public static function bin2Hex($bin) |
|
128 | - { |
|
129 | - $parts = str_split($bin, 8); |
|
130 | - |
|
131 | - $parts = array_map(function($v) { |
|
132 | - $v = str_pad($v, 8, "0", STR_PAD_LEFT); |
|
133 | - $v = dechex(bindec($v)); |
|
134 | - return str_pad($v, 2, "0", STR_PAD_LEFT); |
|
135 | - }, $parts); |
|
136 | - |
|
137 | - return implode("", $parts); |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * Converts a binary representation (ie 01101011) back to a string |
|
143 | - * |
|
144 | - * @param string $bin a binary representation string |
|
145 | - * @return string A string of characters representing the binary |
|
146 | - */ |
|
147 | - public static function bin2Str($bin) |
|
148 | - { |
|
149 | - $hex = self::bin2Hex($bin); |
|
150 | - return self::hex2Str($hex); |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * Convert a binary string (ie: 01101011) to a decimal number |
|
156 | - * |
|
157 | - * @param string A string representation of a binary number |
|
158 | - * @param string $bin |
|
159 | - * @return integer The number converted from the binary string |
|
160 | - */ |
|
161 | - public static function bin2Dec($bin) |
|
162 | - { |
|
163 | - return bindec($bin); |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * Convert a string to hex |
|
169 | - * This function calls the PHP bin2hex(), and is here |
|
170 | - * for consistency with the other string functions |
|
171 | - * |
|
172 | - * @param string $str A string |
|
173 | - * @return string A string representation of hexidecimal number |
|
174 | - */ |
|
175 | - public static function str2Hex($str) |
|
176 | - { |
|
177 | - return bin2hex($str); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * Convert a string of characters to a decimal number |
|
183 | - * |
|
184 | - * @param string $str The string to convert to decimal |
|
185 | - * @return integer The integer converted from the string |
|
186 | - */ |
|
187 | - public static function str2Dec($str) |
|
188 | - { |
|
189 | - $hex = self::str2Hex($str); |
|
190 | - return self::hex2Dec($hex); |
|
191 | - } |
|
192 | - |
|
193 | - |
|
194 | - /** |
|
195 | - * Converts a string to binary representation (ie 01101011) |
|
196 | - * |
|
197 | - * @param string $str A string |
|
198 | - * @return string A binary representation of the the string |
|
199 | - */ |
|
200 | - public static function str2Bin($str) |
|
201 | - { |
|
202 | - $hex = self::str2Hex($str); |
|
203 | - $parts = str_split($hex, 2); |
|
204 | - |
|
205 | - $parts = array_map(function($v) { |
|
206 | - return Core::hex2Bin($v); |
|
207 | - }, $parts); |
|
208 | - |
|
209 | - return implode("", $parts); |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - /** |
|
214 | - * Converts Decimal to Hex |
|
215 | - * This function just calls php's dechex() function, but I |
|
216 | - * encapsulated it in this function to keep things uniform |
|
217 | - * and have all possible conversion function available in |
|
218 | - * the Cipher class |
|
219 | - * |
|
220 | - * The parameter $req_bytes will pad the return hex with NULL (00) |
|
221 | - * until the hex represents the number of bytes given to $req_bytes |
|
222 | - * This is because dechex() drops null bytes from the Hex, which may |
|
223 | - * be needed in some cases |
|
224 | - * |
|
225 | - * @param integer $dec A decimal number to convert |
|
226 | - * @param integer $req_bytes Optional, forces the string to be at least |
|
227 | - * $req_bytes in size, this is needed because on occasion left most null bytes |
|
228 | - * are dropped in dechex(), causing the string to have a shorter byte |
|
229 | - * size than the initial integer. |
|
230 | - * @return string A hexidecimal representation of the decimal number |
|
231 | - */ |
|
232 | - public static function dec2Hex($dec, $req_bytes = 0) |
|
233 | - { |
|
234 | - $hex = dechex($dec); |
|
235 | - |
|
236 | - // if we do not have an even number of hex characters |
|
237 | - // append a 0 to the beginning. dechex() drops leading 0's |
|
238 | - if (strlen($hex) % 2) |
|
239 | - $hex = "0$hex"; |
|
240 | - |
|
241 | - // if the number of bytes in the hex is less than |
|
242 | - // what we need it to be, add null bytes to the |
|
243 | - // front of the hex to padd it to the required size |
|
244 | - if (($req_bytes * 2) > strlen($hex)) |
|
245 | - $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
246 | - |
|
247 | - return $hex; |
|
248 | - } |
|
249 | - |
|
250 | - |
|
251 | - /** |
|
252 | - * Converts Decimal to Binary |
|
253 | - * This function just calls php's decbin() function, but I |
|
254 | - * encapsulated it in this function to keep things uniform |
|
255 | - * and have all possible conversion function available in |
|
256 | - * the Cipher class |
|
257 | - * |
|
258 | - * @param integer $dec A decimal number to convert |
|
259 | - * @param integer $req_bytes Optional, forces the string to be at least |
|
260 | - * $req_bytes in size, this is needed because on occasion left most null bytes |
|
261 | - * are dropped in dechex(), causing the string to have a shorter byte |
|
262 | - * size than the initial integer. |
|
263 | - * @return string A binary representation of the decimal number |
|
264 | - */ |
|
265 | - public static function dec2Bin($dec, $req_bytes = 0) |
|
266 | - { |
|
267 | - $hex = self::dec2Hex($dec, $req_bytes); |
|
268 | - return self::hex2Bin($hex); |
|
269 | - } |
|
270 | - |
|
271 | - |
|
272 | - /** |
|
273 | - * Convert a decimal to a string of bytes |
|
274 | - * |
|
275 | - * @param integer $dec A decimal number |
|
276 | - * @param integer $req_bytes Optional, forces the string to be at least |
|
277 | - * $req_bytes in size, this is needed because on occasion left most null bytes |
|
278 | - * are dropped in dechex(), causing the string to have a shorter byte |
|
279 | - * size than the initial integer. |
|
280 | - * @return string A string with the number of bytes equal to $dec |
|
281 | - */ |
|
282 | - public static function dec2Str($dec, $req_bytes = 0) |
|
283 | - { |
|
284 | - $hex = self::dec2Hex($dec, $req_bytes); |
|
285 | - return self::hex2Str($hex); |
|
286 | - } |
|
287 | - |
|
288 | - |
|
289 | - /** |
|
290 | - * XORs two binary strings (representation of binary, ie 01101011), |
|
291 | - * assumed to be equal length |
|
292 | - * |
|
293 | - * @param string $a A string that represents binary |
|
294 | - * @param string $b A string that represents binary |
|
295 | - * @return string A representation of binary |
|
296 | - */ |
|
297 | - public static function xorBin($a, $b) |
|
298 | - { |
|
299 | - $len_a = strlen($a); |
|
300 | - $len_b = strlen($b); |
|
301 | - $width = $len_a; |
|
302 | - |
|
303 | - // first determine if the two binary strings are the same length, |
|
304 | - // and if not get them to the same length |
|
305 | - if ($len_a > $len_b) |
|
306 | - { |
|
307 | - $width = $len_a; |
|
308 | - $b = str_pad($b, $width, "0", STR_PAD_LEFT); |
|
309 | - } |
|
310 | - else if ($len_a < $len_b) |
|
311 | - { |
|
312 | - $width = $len_b; |
|
313 | - $a = str_pad($a, $width, "0", STR_PAD_LEFT); |
|
314 | - } |
|
315 | - |
|
316 | - // fortunately PHP knows how to XOR each byte in a string |
|
317 | - // so we don't have to loop to do it |
|
318 | - $bin = self::bin2Str($a) ^ self::bin2Str($b); |
|
319 | - return self::str2Bin($bin); |
|
320 | - } |
|
321 | - |
|
322 | - |
|
323 | - /** |
|
324 | - * ExclusiveOR hex values. Supports an unlimited number of parameters. |
|
325 | - * The values are string representations of hex values |
|
326 | - * IE: "0a1b2c3d" not 0x0a1b2c3d |
|
327 | - * |
|
328 | - * @param string Unlimited number parameters, each a string representation of hex |
|
329 | - * @return string A string representation of the result in Hex |
|
330 | - */ |
|
331 | - public static function xorHex() |
|
332 | - { |
|
333 | - $hex = func_get_args(); |
|
334 | - $count = func_num_args(); |
|
335 | - |
|
336 | - // we need a minimum of 2 values |
|
337 | - if ($count < 2) |
|
338 | - return false; |
|
339 | - |
|
340 | - // first get all hex values to an even number |
|
341 | - array_walk($hex, function(&$val, $i) { |
|
342 | - if (strlen($val) % 2) |
|
343 | - $val = "0".$val; |
|
344 | - }); |
|
345 | - |
|
346 | - $res = 0; |
|
347 | - for ($i = 0; $i < $count; ++$i) |
|
348 | - { |
|
349 | - // if this is the first loop, set the 'result' to the first |
|
350 | - // hex value |
|
351 | - if ($i == 0) |
|
352 | - $res = $hex[0]; |
|
353 | - else |
|
354 | - { |
|
355 | - // to make the code easier to follow |
|
356 | - $h1 = $res; |
|
357 | - $h2 = $hex[$i]; |
|
358 | - |
|
359 | - // get lengths |
|
360 | - $len1 = strlen($h1); |
|
361 | - $len2 = strlen($h2); |
|
362 | - |
|
363 | - // now check that both hex values are the same length, |
|
364 | - // if not pad them with 0's until they are |
|
365 | - if ($len1 > $len2) |
|
366 | - $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
367 | - else if ($len1 < $len2) |
|
368 | - $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
369 | - |
|
370 | - // PHP knows how to XOR each byte in a string, so convert the |
|
371 | - // hex to a string, XOR, and convert back |
|
372 | - $res = self::hex2Str($h1) ^ self::hex2Str($h2); |
|
373 | - $res = self::str2Hex($res); |
|
374 | - } |
|
375 | - } |
|
376 | - |
|
377 | - return $res; |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - /** |
|
382 | - * Forces an integer to be signed |
|
383 | - * |
|
384 | - * @param integer $int An integer |
|
385 | - * @return integer An signed integer |
|
386 | - */ |
|
387 | - public static function sInt($int) |
|
388 | - { |
|
389 | - $arr = unpack("i", pack("i", $int)); |
|
390 | - return $arr[1]; |
|
391 | - } |
|
392 | - |
|
393 | - |
|
394 | - /** |
|
395 | - * Forces an integer to be unsigned |
|
396 | - * |
|
397 | - * @param integer $int A signed integer |
|
398 | - * @return integer An unsigned integer |
|
399 | - */ |
|
400 | - public static function uInt($int) |
|
401 | - { |
|
402 | - $arr = unpack("I", pack("I", $int)); |
|
403 | - $ret = $arr[1]; |
|
404 | - |
|
405 | - // On 32 bit platforms unpack() and pack() do not convert |
|
406 | - // from signed to unsigned properly all the time, it will return |
|
407 | - // the same negative number given to it, the work around is |
|
408 | - // to use sprintf(). |
|
409 | - // Tested with php 5.3.x on Windows XP & Linux 32bit |
|
410 | - if ($ret < 0) |
|
411 | - $ret = sprintf("%u", $ret) + 0; // convert from string to int |
|
412 | - |
|
413 | - return $ret; |
|
414 | - } |
|
415 | - |
|
416 | - |
|
417 | - /** |
|
418 | - * Forces an integer to be a 32 bit signed integer |
|
419 | - * |
|
420 | - * @param integer $int An integer |
|
421 | - * @return integer An signed 32 bit integer |
|
422 | - */ |
|
423 | - public static function sInt32($int) |
|
424 | - { |
|
425 | - if (PHP_INT_SIZE === 4) // 32 bit |
|
426 | - return self::sInt($int); |
|
427 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
428 | - { |
|
429 | - $arr = unpack("l", pack("l", $int)); |
|
430 | - return $arr[1]; |
|
431 | - } |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - /** |
|
436 | - * Force an integer to be a 32 bit unsigned integer |
|
437 | - * |
|
438 | - * @param integer $int An integer |
|
439 | - * @return integer An unsigned 32 bit integer |
|
440 | - */ |
|
441 | - public static function uInt32($int) |
|
442 | - { |
|
443 | - if (PHP_INT_SIZE === 4) // 32 bit |
|
444 | - return self::uInt($int); |
|
445 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
446 | - { |
|
447 | - $arr = unpack("L", pack("L", $int)); |
|
448 | - return $arr[1]; |
|
449 | - } |
|
450 | - } |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * Converts an integer to the value for an signed char |
|
455 | - * |
|
456 | - * @param integer $int The integer to convert to a signed char |
|
457 | - * @return integer A signed integer, representing a signed char |
|
458 | - */ |
|
459 | - public static function sChar($int) |
|
460 | - { |
|
461 | - $arr = unpack("c", pack("c", $int)); |
|
462 | - return $arr[1]; |
|
463 | - } |
|
464 | - |
|
465 | - |
|
466 | - /** |
|
467 | - * Converts an integer to the value for an unsigned char |
|
468 | - * |
|
469 | - * @param integer $int The integer to convert to a unsigned char |
|
470 | - * @return integer An unsigned integer, representing a unsigned char |
|
471 | - */ |
|
472 | - public static function uChar($int) |
|
473 | - { |
|
474 | - $arr = unpack("C", pack("C", $int)); |
|
475 | - return $arr[1]; |
|
476 | - } |
|
477 | - |
|
478 | - |
|
479 | - /** |
|
480 | - * Rotates bits Left, appending the bits pushed off the left onto the right |
|
481 | - * |
|
482 | - * @param integer $shifts The number of shifts left to make |
|
483 | - * @return integer The resulting value from the rotation |
|
484 | - */ |
|
485 | - public static function rotBitsLeft32($i, $shifts) |
|
486 | - { |
|
487 | - if ($shifts <= 0) |
|
488 | - return $i; |
|
489 | - |
|
490 | - $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
491 | - |
|
492 | - // this is causing problems on 32 bit platform |
|
493 | - //return self::uInt32(($i << $shifts) | ($i >> (32 - $shifts))); |
|
494 | - |
|
495 | - // so lets cheat: convert to binary string, rotate left, and |
|
496 | - // convert back to decimal |
|
497 | - $i = self::dec2Bin(self::uInt32($i), 4); |
|
498 | - $i = substr($i, $shifts).substr($i, 0, $shifts); |
|
499 | - return self::bin2Dec($i); |
|
500 | - |
|
501 | - } |
|
502 | - |
|
503 | - |
|
504 | - /** |
|
505 | - * Rotates bits right, appending the bits pushed off the right onto the left |
|
506 | - * |
|
507 | - * @param integer $shifts The number of shifts right to make |
|
508 | - * @return integer The resulting value from the rotation |
|
509 | - */ |
|
510 | - public static function rotBitsRight32($i, $shifts) |
|
511 | - { |
|
512 | - if ($shifts <= 0) |
|
513 | - return $i; |
|
514 | - |
|
515 | - $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
516 | - |
|
517 | - // this might cause problems on 32 bit platforms since rotBitsLeft32 was |
|
518 | - // having a problem with some bit shifts on 32 bits |
|
519 | - // return self::uInt32(($i >> $shifts) | ($i << (32 - $shifts))); |
|
520 | - |
|
521 | - // so lets cheat: convert to binary string, rotate right, |
|
522 | - // and convert back to decimal |
|
523 | - $i = self::dec2Bin($i, 4); |
|
524 | - $i = substr($i, (-1 * $shifts)).substr($i, 0, (-1 * $shifts)); |
|
525 | - return self::bin2Dec($i); |
|
526 | - } |
|
527 | - |
|
528 | - |
|
529 | - /** |
|
530 | - * Create a string of random bytes, used for creating an IV |
|
531 | - * and a random key. See PHP_Crypt::createKey() and PHP_Crypt::createIV() |
|
532 | - * There are 4 ways to auto generate random bytes by setting $src parameter |
|
533 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
534 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
535 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
536 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
537 | - * |
|
538 | - * @param string $src Optional, Use the $src to create the random bytes |
|
539 | - * by default PHP_Crypt::RAND is used when $src is not specified |
|
540 | - * @param integer $byte_len The length of the byte string to create |
|
541 | - * @return string A random string of bytes |
|
542 | - */ |
|
543 | - public static function randBytes($src = PHP_Crypt::RAND, $byte_len = PHP_Crypt::RAND_DEFAULT_SZ) |
|
544 | - { |
|
545 | - $bytes = ""; |
|
546 | - $err_msg = ""; |
|
547 | - |
|
548 | - if ($src == PHP_Crypt::RAND_DEV_RAND) |
|
549 | - { |
|
550 | - if (file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
551 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
552 | - else |
|
553 | - $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
554 | - } |
|
555 | - else if ($src == PHP_Crypt::RAND_DEV_URAND) |
|
556 | - { |
|
557 | - if (file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
558 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
559 | - else |
|
560 | - $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
561 | - } |
|
562 | - else if ($src == PHP_Crypt::RAND_WIN_COM) |
|
563 | - { |
|
564 | - if (extension_loaded('com_dotnet')) |
|
565 | - { |
|
566 | - // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx |
|
567 | - try |
|
568 | - { |
|
569 | - // request a random number in $byte_len bytes, returned |
|
570 | - // as base_64 encoded string. This is because PHP munges the |
|
571 | - // binary data on Windows |
|
572 | - $com = @new \COM("CAPICOM.Utilities.1"); |
|
573 | - $bytes = $com->GetRandom($byte_len, 0); |
|
574 | - } |
|
575 | - catch (Exception $e) |
|
576 | - { |
|
577 | - $err_msg = "Windows COM exception: ".$e->getMessage(); |
|
578 | - } |
|
579 | - |
|
580 | - if (!$bytes) |
|
581 | - $err_msg = "Windows COM failed to create random string of bytes"; |
|
582 | - } |
|
583 | - else |
|
584 | - $err_msg = "The COM_DOTNET extension is not loaded"; |
|
585 | - } |
|
586 | - |
|
587 | - // trigger a warning if something went wrong |
|
588 | - if ($err_msg != "") |
|
589 | - trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
590 | - |
|
591 | - // if the random bytes where not created properly or PHP_Crypt::RAND was |
|
592 | - // passed as the $src param, create the bytes using mt_rand(). It's not |
|
593 | - // the most secure option but we have no other choice |
|
594 | - if (strlen($bytes) < $byte_len) |
|
595 | - { |
|
596 | - $bytes = ""; |
|
597 | - |
|
598 | - // md5() hash a random number to get a 16 byte string, keep looping |
|
599 | - // until we have a string as long or longer than the ciphers block size |
|
600 | - for ($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
601 | - $bytes .= md5(mt_rand(), true); |
|
602 | - } |
|
603 | - |
|
604 | - // because $bytes may have come from mt_rand() or /dev/urandom which are not |
|
605 | - // cryptographically secure, lets add another layer of 'randomness' before |
|
606 | - // the final md5() below |
|
607 | - $bytes = str_shuffle($bytes); |
|
608 | - |
|
609 | - // md5() the $bytes to add extra randomness. Since md5() only returns |
|
610 | - // 16 bytes, we may need to loop to generate a string of $bytes big enough for |
|
611 | - // some ciphers which have a block size larger than 16 bytes |
|
612 | - $tmp = ""; |
|
613 | - $loop = ceil(strlen($bytes) / self::HASH_LEN); |
|
614 | - for ($i = 0; $i < $loop; ++$i) |
|
615 | - $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
616 | - |
|
617 | - // grab the number of bytes equal to the requested $byte_len |
|
618 | - return substr($tmp, 0, $byte_len); |
|
619 | - } |
|
36 | + /** @type integer HASH_LEN The length of md5() hash string */ |
|
37 | + const HASH_LEN = 16; |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * Constructor |
|
42 | + * |
|
43 | + */ |
|
44 | + protected function __construct() |
|
45 | + { |
|
46 | + |
|
47 | + } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Destructor |
|
52 | + * |
|
53 | + */ |
|
54 | + protected function __destruct() |
|
55 | + { |
|
56 | + |
|
57 | + } |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * Convert hexidecimal to a binary string (ex: "00110110") |
|
62 | + * |
|
63 | + * @param string $hex A string containing a hexidecimal number |
|
64 | + * @return string A string representation of a binary |
|
65 | + */ |
|
66 | + public static function hex2Bin($hex) |
|
67 | + { |
|
68 | + // if we do not have an even number of hex characters |
|
69 | + // append a 0 to the beginning to make it even |
|
70 | + if (strlen($hex) % 2) |
|
71 | + $hex = "0$hex"; |
|
72 | + |
|
73 | + $parts = str_split($hex, 2); |
|
74 | + $parts = array_map(function($v) { |
|
75 | + $v = base_convert($v, 16, 2); |
|
76 | + return str_pad($v, 8, "0", STR_PAD_LEFT); |
|
77 | + }, $parts); |
|
78 | + |
|
79 | + return implode("", $parts); |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * Convert hex to a string |
|
85 | + * |
|
86 | + * @param string $hex A string representation of Hex (IE: "1a2b3c" not 0x1a2b3c) |
|
87 | + * @return string a string |
|
88 | + */ |
|
89 | + public static function hex2Str($hex) |
|
90 | + { |
|
91 | + // php version >= 5.4 have a hex2bin function, use it |
|
92 | + // if it exists |
|
93 | + if (function_exists("hex2bin")) |
|
94 | + return hex2bin($hex); |
|
95 | + |
|
96 | + $parts = str_split($hex, 2); |
|
97 | + $parts = array_map(function($v) { |
|
98 | + return chr(Core::hex2Dec($v)); |
|
99 | + }, $parts); |
|
100 | + |
|
101 | + return implode("", $parts); |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * Converts Hex to Decimal |
|
107 | + * This function just calls php's hexdec() function, but I |
|
108 | + * encapsulated it in this function to keep things uniform |
|
109 | + * and have all possible conversion function available in |
|
110 | + * the Cipher class |
|
111 | + * |
|
112 | + * @param string $hex A hex number to convert to decimal |
|
113 | + * @return integer A decimal number |
|
114 | + */ |
|
115 | + public static function hex2Dec($hex) |
|
116 | + { |
|
117 | + return hexdec($hex); |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * Convert binary string (ie 00110110) to hex |
|
123 | + * |
|
124 | + * @param string $bin A binary string |
|
125 | + * @return string A string representation of hexidecimal number |
|
126 | + */ |
|
127 | + public static function bin2Hex($bin) |
|
128 | + { |
|
129 | + $parts = str_split($bin, 8); |
|
130 | + |
|
131 | + $parts = array_map(function($v) { |
|
132 | + $v = str_pad($v, 8, "0", STR_PAD_LEFT); |
|
133 | + $v = dechex(bindec($v)); |
|
134 | + return str_pad($v, 2, "0", STR_PAD_LEFT); |
|
135 | + }, $parts); |
|
136 | + |
|
137 | + return implode("", $parts); |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * Converts a binary representation (ie 01101011) back to a string |
|
143 | + * |
|
144 | + * @param string $bin a binary representation string |
|
145 | + * @return string A string of characters representing the binary |
|
146 | + */ |
|
147 | + public static function bin2Str($bin) |
|
148 | + { |
|
149 | + $hex = self::bin2Hex($bin); |
|
150 | + return self::hex2Str($hex); |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * Convert a binary string (ie: 01101011) to a decimal number |
|
156 | + * |
|
157 | + * @param string A string representation of a binary number |
|
158 | + * @param string $bin |
|
159 | + * @return integer The number converted from the binary string |
|
160 | + */ |
|
161 | + public static function bin2Dec($bin) |
|
162 | + { |
|
163 | + return bindec($bin); |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * Convert a string to hex |
|
169 | + * This function calls the PHP bin2hex(), and is here |
|
170 | + * for consistency with the other string functions |
|
171 | + * |
|
172 | + * @param string $str A string |
|
173 | + * @return string A string representation of hexidecimal number |
|
174 | + */ |
|
175 | + public static function str2Hex($str) |
|
176 | + { |
|
177 | + return bin2hex($str); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * Convert a string of characters to a decimal number |
|
183 | + * |
|
184 | + * @param string $str The string to convert to decimal |
|
185 | + * @return integer The integer converted from the string |
|
186 | + */ |
|
187 | + public static function str2Dec($str) |
|
188 | + { |
|
189 | + $hex = self::str2Hex($str); |
|
190 | + return self::hex2Dec($hex); |
|
191 | + } |
|
192 | + |
|
193 | + |
|
194 | + /** |
|
195 | + * Converts a string to binary representation (ie 01101011) |
|
196 | + * |
|
197 | + * @param string $str A string |
|
198 | + * @return string A binary representation of the the string |
|
199 | + */ |
|
200 | + public static function str2Bin($str) |
|
201 | + { |
|
202 | + $hex = self::str2Hex($str); |
|
203 | + $parts = str_split($hex, 2); |
|
204 | + |
|
205 | + $parts = array_map(function($v) { |
|
206 | + return Core::hex2Bin($v); |
|
207 | + }, $parts); |
|
208 | + |
|
209 | + return implode("", $parts); |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + /** |
|
214 | + * Converts Decimal to Hex |
|
215 | + * This function just calls php's dechex() function, but I |
|
216 | + * encapsulated it in this function to keep things uniform |
|
217 | + * and have all possible conversion function available in |
|
218 | + * the Cipher class |
|
219 | + * |
|
220 | + * The parameter $req_bytes will pad the return hex with NULL (00) |
|
221 | + * until the hex represents the number of bytes given to $req_bytes |
|
222 | + * This is because dechex() drops null bytes from the Hex, which may |
|
223 | + * be needed in some cases |
|
224 | + * |
|
225 | + * @param integer $dec A decimal number to convert |
|
226 | + * @param integer $req_bytes Optional, forces the string to be at least |
|
227 | + * $req_bytes in size, this is needed because on occasion left most null bytes |
|
228 | + * are dropped in dechex(), causing the string to have a shorter byte |
|
229 | + * size than the initial integer. |
|
230 | + * @return string A hexidecimal representation of the decimal number |
|
231 | + */ |
|
232 | + public static function dec2Hex($dec, $req_bytes = 0) |
|
233 | + { |
|
234 | + $hex = dechex($dec); |
|
235 | + |
|
236 | + // if we do not have an even number of hex characters |
|
237 | + // append a 0 to the beginning. dechex() drops leading 0's |
|
238 | + if (strlen($hex) % 2) |
|
239 | + $hex = "0$hex"; |
|
240 | + |
|
241 | + // if the number of bytes in the hex is less than |
|
242 | + // what we need it to be, add null bytes to the |
|
243 | + // front of the hex to padd it to the required size |
|
244 | + if (($req_bytes * 2) > strlen($hex)) |
|
245 | + $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
246 | + |
|
247 | + return $hex; |
|
248 | + } |
|
249 | + |
|
250 | + |
|
251 | + /** |
|
252 | + * Converts Decimal to Binary |
|
253 | + * This function just calls php's decbin() function, but I |
|
254 | + * encapsulated it in this function to keep things uniform |
|
255 | + * and have all possible conversion function available in |
|
256 | + * the Cipher class |
|
257 | + * |
|
258 | + * @param integer $dec A decimal number to convert |
|
259 | + * @param integer $req_bytes Optional, forces the string to be at least |
|
260 | + * $req_bytes in size, this is needed because on occasion left most null bytes |
|
261 | + * are dropped in dechex(), causing the string to have a shorter byte |
|
262 | + * size than the initial integer. |
|
263 | + * @return string A binary representation of the decimal number |
|
264 | + */ |
|
265 | + public static function dec2Bin($dec, $req_bytes = 0) |
|
266 | + { |
|
267 | + $hex = self::dec2Hex($dec, $req_bytes); |
|
268 | + return self::hex2Bin($hex); |
|
269 | + } |
|
270 | + |
|
271 | + |
|
272 | + /** |
|
273 | + * Convert a decimal to a string of bytes |
|
274 | + * |
|
275 | + * @param integer $dec A decimal number |
|
276 | + * @param integer $req_bytes Optional, forces the string to be at least |
|
277 | + * $req_bytes in size, this is needed because on occasion left most null bytes |
|
278 | + * are dropped in dechex(), causing the string to have a shorter byte |
|
279 | + * size than the initial integer. |
|
280 | + * @return string A string with the number of bytes equal to $dec |
|
281 | + */ |
|
282 | + public static function dec2Str($dec, $req_bytes = 0) |
|
283 | + { |
|
284 | + $hex = self::dec2Hex($dec, $req_bytes); |
|
285 | + return self::hex2Str($hex); |
|
286 | + } |
|
287 | + |
|
288 | + |
|
289 | + /** |
|
290 | + * XORs two binary strings (representation of binary, ie 01101011), |
|
291 | + * assumed to be equal length |
|
292 | + * |
|
293 | + * @param string $a A string that represents binary |
|
294 | + * @param string $b A string that represents binary |
|
295 | + * @return string A representation of binary |
|
296 | + */ |
|
297 | + public static function xorBin($a, $b) |
|
298 | + { |
|
299 | + $len_a = strlen($a); |
|
300 | + $len_b = strlen($b); |
|
301 | + $width = $len_a; |
|
302 | + |
|
303 | + // first determine if the two binary strings are the same length, |
|
304 | + // and if not get them to the same length |
|
305 | + if ($len_a > $len_b) |
|
306 | + { |
|
307 | + $width = $len_a; |
|
308 | + $b = str_pad($b, $width, "0", STR_PAD_LEFT); |
|
309 | + } |
|
310 | + else if ($len_a < $len_b) |
|
311 | + { |
|
312 | + $width = $len_b; |
|
313 | + $a = str_pad($a, $width, "0", STR_PAD_LEFT); |
|
314 | + } |
|
315 | + |
|
316 | + // fortunately PHP knows how to XOR each byte in a string |
|
317 | + // so we don't have to loop to do it |
|
318 | + $bin = self::bin2Str($a) ^ self::bin2Str($b); |
|
319 | + return self::str2Bin($bin); |
|
320 | + } |
|
321 | + |
|
322 | + |
|
323 | + /** |
|
324 | + * ExclusiveOR hex values. Supports an unlimited number of parameters. |
|
325 | + * The values are string representations of hex values |
|
326 | + * IE: "0a1b2c3d" not 0x0a1b2c3d |
|
327 | + * |
|
328 | + * @param string Unlimited number parameters, each a string representation of hex |
|
329 | + * @return string A string representation of the result in Hex |
|
330 | + */ |
|
331 | + public static function xorHex() |
|
332 | + { |
|
333 | + $hex = func_get_args(); |
|
334 | + $count = func_num_args(); |
|
335 | + |
|
336 | + // we need a minimum of 2 values |
|
337 | + if ($count < 2) |
|
338 | + return false; |
|
339 | + |
|
340 | + // first get all hex values to an even number |
|
341 | + array_walk($hex, function(&$val, $i) { |
|
342 | + if (strlen($val) % 2) |
|
343 | + $val = "0".$val; |
|
344 | + }); |
|
345 | + |
|
346 | + $res = 0; |
|
347 | + for ($i = 0; $i < $count; ++$i) |
|
348 | + { |
|
349 | + // if this is the first loop, set the 'result' to the first |
|
350 | + // hex value |
|
351 | + if ($i == 0) |
|
352 | + $res = $hex[0]; |
|
353 | + else |
|
354 | + { |
|
355 | + // to make the code easier to follow |
|
356 | + $h1 = $res; |
|
357 | + $h2 = $hex[$i]; |
|
358 | + |
|
359 | + // get lengths |
|
360 | + $len1 = strlen($h1); |
|
361 | + $len2 = strlen($h2); |
|
362 | + |
|
363 | + // now check that both hex values are the same length, |
|
364 | + // if not pad them with 0's until they are |
|
365 | + if ($len1 > $len2) |
|
366 | + $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
367 | + else if ($len1 < $len2) |
|
368 | + $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
369 | + |
|
370 | + // PHP knows how to XOR each byte in a string, so convert the |
|
371 | + // hex to a string, XOR, and convert back |
|
372 | + $res = self::hex2Str($h1) ^ self::hex2Str($h2); |
|
373 | + $res = self::str2Hex($res); |
|
374 | + } |
|
375 | + } |
|
376 | + |
|
377 | + return $res; |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + /** |
|
382 | + * Forces an integer to be signed |
|
383 | + * |
|
384 | + * @param integer $int An integer |
|
385 | + * @return integer An signed integer |
|
386 | + */ |
|
387 | + public static function sInt($int) |
|
388 | + { |
|
389 | + $arr = unpack("i", pack("i", $int)); |
|
390 | + return $arr[1]; |
|
391 | + } |
|
392 | + |
|
393 | + |
|
394 | + /** |
|
395 | + * Forces an integer to be unsigned |
|
396 | + * |
|
397 | + * @param integer $int A signed integer |
|
398 | + * @return integer An unsigned integer |
|
399 | + */ |
|
400 | + public static function uInt($int) |
|
401 | + { |
|
402 | + $arr = unpack("I", pack("I", $int)); |
|
403 | + $ret = $arr[1]; |
|
404 | + |
|
405 | + // On 32 bit platforms unpack() and pack() do not convert |
|
406 | + // from signed to unsigned properly all the time, it will return |
|
407 | + // the same negative number given to it, the work around is |
|
408 | + // to use sprintf(). |
|
409 | + // Tested with php 5.3.x on Windows XP & Linux 32bit |
|
410 | + if ($ret < 0) |
|
411 | + $ret = sprintf("%u", $ret) + 0; // convert from string to int |
|
412 | + |
|
413 | + return $ret; |
|
414 | + } |
|
415 | + |
|
416 | + |
|
417 | + /** |
|
418 | + * Forces an integer to be a 32 bit signed integer |
|
419 | + * |
|
420 | + * @param integer $int An integer |
|
421 | + * @return integer An signed 32 bit integer |
|
422 | + */ |
|
423 | + public static function sInt32($int) |
|
424 | + { |
|
425 | + if (PHP_INT_SIZE === 4) // 32 bit |
|
426 | + return self::sInt($int); |
|
427 | + else // PHP_INT_SIZE === 8 // 64 bit |
|
428 | + { |
|
429 | + $arr = unpack("l", pack("l", $int)); |
|
430 | + return $arr[1]; |
|
431 | + } |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + /** |
|
436 | + * Force an integer to be a 32 bit unsigned integer |
|
437 | + * |
|
438 | + * @param integer $int An integer |
|
439 | + * @return integer An unsigned 32 bit integer |
|
440 | + */ |
|
441 | + public static function uInt32($int) |
|
442 | + { |
|
443 | + if (PHP_INT_SIZE === 4) // 32 bit |
|
444 | + return self::uInt($int); |
|
445 | + else // PHP_INT_SIZE === 8 // 64 bit |
|
446 | + { |
|
447 | + $arr = unpack("L", pack("L", $int)); |
|
448 | + return $arr[1]; |
|
449 | + } |
|
450 | + } |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * Converts an integer to the value for an signed char |
|
455 | + * |
|
456 | + * @param integer $int The integer to convert to a signed char |
|
457 | + * @return integer A signed integer, representing a signed char |
|
458 | + */ |
|
459 | + public static function sChar($int) |
|
460 | + { |
|
461 | + $arr = unpack("c", pack("c", $int)); |
|
462 | + return $arr[1]; |
|
463 | + } |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * Converts an integer to the value for an unsigned char |
|
468 | + * |
|
469 | + * @param integer $int The integer to convert to a unsigned char |
|
470 | + * @return integer An unsigned integer, representing a unsigned char |
|
471 | + */ |
|
472 | + public static function uChar($int) |
|
473 | + { |
|
474 | + $arr = unpack("C", pack("C", $int)); |
|
475 | + return $arr[1]; |
|
476 | + } |
|
477 | + |
|
478 | + |
|
479 | + /** |
|
480 | + * Rotates bits Left, appending the bits pushed off the left onto the right |
|
481 | + * |
|
482 | + * @param integer $shifts The number of shifts left to make |
|
483 | + * @return integer The resulting value from the rotation |
|
484 | + */ |
|
485 | + public static function rotBitsLeft32($i, $shifts) |
|
486 | + { |
|
487 | + if ($shifts <= 0) |
|
488 | + return $i; |
|
489 | + |
|
490 | + $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
491 | + |
|
492 | + // this is causing problems on 32 bit platform |
|
493 | + //return self::uInt32(($i << $shifts) | ($i >> (32 - $shifts))); |
|
494 | + |
|
495 | + // so lets cheat: convert to binary string, rotate left, and |
|
496 | + // convert back to decimal |
|
497 | + $i = self::dec2Bin(self::uInt32($i), 4); |
|
498 | + $i = substr($i, $shifts).substr($i, 0, $shifts); |
|
499 | + return self::bin2Dec($i); |
|
500 | + |
|
501 | + } |
|
502 | + |
|
503 | + |
|
504 | + /** |
|
505 | + * Rotates bits right, appending the bits pushed off the right onto the left |
|
506 | + * |
|
507 | + * @param integer $shifts The number of shifts right to make |
|
508 | + * @return integer The resulting value from the rotation |
|
509 | + */ |
|
510 | + public static function rotBitsRight32($i, $shifts) |
|
511 | + { |
|
512 | + if ($shifts <= 0) |
|
513 | + return $i; |
|
514 | + |
|
515 | + $shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
516 | + |
|
517 | + // this might cause problems on 32 bit platforms since rotBitsLeft32 was |
|
518 | + // having a problem with some bit shifts on 32 bits |
|
519 | + // return self::uInt32(($i >> $shifts) | ($i << (32 - $shifts))); |
|
520 | + |
|
521 | + // so lets cheat: convert to binary string, rotate right, |
|
522 | + // and convert back to decimal |
|
523 | + $i = self::dec2Bin($i, 4); |
|
524 | + $i = substr($i, (-1 * $shifts)).substr($i, 0, (-1 * $shifts)); |
|
525 | + return self::bin2Dec($i); |
|
526 | + } |
|
527 | + |
|
528 | + |
|
529 | + /** |
|
530 | + * Create a string of random bytes, used for creating an IV |
|
531 | + * and a random key. See PHP_Crypt::createKey() and PHP_Crypt::createIV() |
|
532 | + * There are 4 ways to auto generate random bytes by setting $src parameter |
|
533 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
534 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
535 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
536 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
537 | + * |
|
538 | + * @param string $src Optional, Use the $src to create the random bytes |
|
539 | + * by default PHP_Crypt::RAND is used when $src is not specified |
|
540 | + * @param integer $byte_len The length of the byte string to create |
|
541 | + * @return string A random string of bytes |
|
542 | + */ |
|
543 | + public static function randBytes($src = PHP_Crypt::RAND, $byte_len = PHP_Crypt::RAND_DEFAULT_SZ) |
|
544 | + { |
|
545 | + $bytes = ""; |
|
546 | + $err_msg = ""; |
|
547 | + |
|
548 | + if ($src == PHP_Crypt::RAND_DEV_RAND) |
|
549 | + { |
|
550 | + if (file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
551 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
552 | + else |
|
553 | + $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
554 | + } |
|
555 | + else if ($src == PHP_Crypt::RAND_DEV_URAND) |
|
556 | + { |
|
557 | + if (file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
558 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
559 | + else |
|
560 | + $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
561 | + } |
|
562 | + else if ($src == PHP_Crypt::RAND_WIN_COM) |
|
563 | + { |
|
564 | + if (extension_loaded('com_dotnet')) |
|
565 | + { |
|
566 | + // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx |
|
567 | + try |
|
568 | + { |
|
569 | + // request a random number in $byte_len bytes, returned |
|
570 | + // as base_64 encoded string. This is because PHP munges the |
|
571 | + // binary data on Windows |
|
572 | + $com = @new \COM("CAPICOM.Utilities.1"); |
|
573 | + $bytes = $com->GetRandom($byte_len, 0); |
|
574 | + } |
|
575 | + catch (Exception $e) |
|
576 | + { |
|
577 | + $err_msg = "Windows COM exception: ".$e->getMessage(); |
|
578 | + } |
|
579 | + |
|
580 | + if (!$bytes) |
|
581 | + $err_msg = "Windows COM failed to create random string of bytes"; |
|
582 | + } |
|
583 | + else |
|
584 | + $err_msg = "The COM_DOTNET extension is not loaded"; |
|
585 | + } |
|
586 | + |
|
587 | + // trigger a warning if something went wrong |
|
588 | + if ($err_msg != "") |
|
589 | + trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
590 | + |
|
591 | + // if the random bytes where not created properly or PHP_Crypt::RAND was |
|
592 | + // passed as the $src param, create the bytes using mt_rand(). It's not |
|
593 | + // the most secure option but we have no other choice |
|
594 | + if (strlen($bytes) < $byte_len) |
|
595 | + { |
|
596 | + $bytes = ""; |
|
597 | + |
|
598 | + // md5() hash a random number to get a 16 byte string, keep looping |
|
599 | + // until we have a string as long or longer than the ciphers block size |
|
600 | + for ($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
601 | + $bytes .= md5(mt_rand(), true); |
|
602 | + } |
|
603 | + |
|
604 | + // because $bytes may have come from mt_rand() or /dev/urandom which are not |
|
605 | + // cryptographically secure, lets add another layer of 'randomness' before |
|
606 | + // the final md5() below |
|
607 | + $bytes = str_shuffle($bytes); |
|
608 | + |
|
609 | + // md5() the $bytes to add extra randomness. Since md5() only returns |
|
610 | + // 16 bytes, we may need to loop to generate a string of $bytes big enough for |
|
611 | + // some ciphers which have a block size larger than 16 bytes |
|
612 | + $tmp = ""; |
|
613 | + $loop = ceil(strlen($bytes) / self::HASH_LEN); |
|
614 | + for ($i = 0; $i < $loop; ++$i) |
|
615 | + $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
616 | + |
|
617 | + // grab the number of bytes equal to the requested $byte_len |
|
618 | + return substr($tmp, 0, $byte_len); |
|
619 | + } |
|
620 | 620 | } |
621 | 621 | ?> |
@@ -67,8 +67,9 @@ discard block |
||
67 | 67 | { |
68 | 68 | // if we do not have an even number of hex characters |
69 | 69 | // append a 0 to the beginning to make it even |
70 | - if (strlen($hex) % 2) |
|
71 | - $hex = "0$hex"; |
|
70 | + if (strlen($hex) % 2) { |
|
71 | + $hex = "0$hex"; |
|
72 | + } |
|
72 | 73 | |
73 | 74 | $parts = str_split($hex, 2); |
74 | 75 | $parts = array_map(function($v) { |
@@ -90,8 +91,9 @@ discard block |
||
90 | 91 | { |
91 | 92 | // php version >= 5.4 have a hex2bin function, use it |
92 | 93 | // if it exists |
93 | - if (function_exists("hex2bin")) |
|
94 | - return hex2bin($hex); |
|
94 | + if (function_exists("hex2bin")) { |
|
95 | + return hex2bin($hex); |
|
96 | + } |
|
95 | 97 | |
96 | 98 | $parts = str_split($hex, 2); |
97 | 99 | $parts = array_map(function($v) { |
@@ -235,14 +237,16 @@ discard block |
||
235 | 237 | |
236 | 238 | // if we do not have an even number of hex characters |
237 | 239 | // append a 0 to the beginning. dechex() drops leading 0's |
238 | - if (strlen($hex) % 2) |
|
239 | - $hex = "0$hex"; |
|
240 | + if (strlen($hex) % 2) { |
|
241 | + $hex = "0$hex"; |
|
242 | + } |
|
240 | 243 | |
241 | 244 | // if the number of bytes in the hex is less than |
242 | 245 | // what we need it to be, add null bytes to the |
243 | 246 | // front of the hex to padd it to the required size |
244 | - if (($req_bytes * 2) > strlen($hex)) |
|
245 | - $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
247 | + if (($req_bytes * 2) > strlen($hex)) { |
|
248 | + $hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
249 | + } |
|
246 | 250 | |
247 | 251 | return $hex; |
248 | 252 | } |
@@ -306,8 +310,7 @@ discard block |
||
306 | 310 | { |
307 | 311 | $width = $len_a; |
308 | 312 | $b = str_pad($b, $width, "0", STR_PAD_LEFT); |
309 | - } |
|
310 | - else if ($len_a < $len_b) |
|
313 | + } else if ($len_a < $len_b) |
|
311 | 314 | { |
312 | 315 | $width = $len_b; |
313 | 316 | $a = str_pad($a, $width, "0", STR_PAD_LEFT); |
@@ -334,13 +337,15 @@ discard block |
||
334 | 337 | $count = func_num_args(); |
335 | 338 | |
336 | 339 | // we need a minimum of 2 values |
337 | - if ($count < 2) |
|
338 | - return false; |
|
340 | + if ($count < 2) { |
|
341 | + return false; |
|
342 | + } |
|
339 | 343 | |
340 | 344 | // first get all hex values to an even number |
341 | 345 | array_walk($hex, function(&$val, $i) { |
342 | - if (strlen($val) % 2) |
|
343 | - $val = "0".$val; |
|
346 | + if (strlen($val) % 2) { |
|
347 | + $val = "0".$val; |
|
348 | + } |
|
344 | 349 | }); |
345 | 350 | |
346 | 351 | $res = 0; |
@@ -348,9 +353,9 @@ discard block |
||
348 | 353 | { |
349 | 354 | // if this is the first loop, set the 'result' to the first |
350 | 355 | // hex value |
351 | - if ($i == 0) |
|
352 | - $res = $hex[0]; |
|
353 | - else |
|
356 | + if ($i == 0) { |
|
357 | + $res = $hex[0]; |
|
358 | + } else |
|
354 | 359 | { |
355 | 360 | // to make the code easier to follow |
356 | 361 | $h1 = $res; |
@@ -362,10 +367,11 @@ discard block |
||
362 | 367 | |
363 | 368 | // now check that both hex values are the same length, |
364 | 369 | // if not pad them with 0's until they are |
365 | - if ($len1 > $len2) |
|
366 | - $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
367 | - else if ($len1 < $len2) |
|
368 | - $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
370 | + if ($len1 > $len2) { |
|
371 | + $h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
372 | + } else if ($len1 < $len2) { |
|
373 | + $h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
374 | + } |
|
369 | 375 | |
370 | 376 | // PHP knows how to XOR each byte in a string, so convert the |
371 | 377 | // hex to a string, XOR, and convert back |
@@ -407,8 +413,10 @@ discard block |
||
407 | 413 | // the same negative number given to it, the work around is |
408 | 414 | // to use sprintf(). |
409 | 415 | // Tested with php 5.3.x on Windows XP & Linux 32bit |
410 | - if ($ret < 0) |
|
411 | - $ret = sprintf("%u", $ret) + 0; // convert from string to int |
|
416 | + if ($ret < 0) { |
|
417 | + $ret = sprintf("%u", $ret) + 0; |
|
418 | + } |
|
419 | + // convert from string to int |
|
412 | 420 | |
413 | 421 | return $ret; |
414 | 422 | } |
@@ -422,9 +430,10 @@ discard block |
||
422 | 430 | */ |
423 | 431 | public static function sInt32($int) |
424 | 432 | { |
425 | - if (PHP_INT_SIZE === 4) // 32 bit |
|
433 | + if (PHP_INT_SIZE === 4) { |
|
434 | + // 32 bit |
|
426 | 435 | return self::sInt($int); |
427 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
436 | + } else // PHP_INT_SIZE === 8 // 64 bit |
|
428 | 437 | { |
429 | 438 | $arr = unpack("l", pack("l", $int)); |
430 | 439 | return $arr[1]; |
@@ -440,9 +449,10 @@ discard block |
||
440 | 449 | */ |
441 | 450 | public static function uInt32($int) |
442 | 451 | { |
443 | - if (PHP_INT_SIZE === 4) // 32 bit |
|
452 | + if (PHP_INT_SIZE === 4) { |
|
453 | + // 32 bit |
|
444 | 454 | return self::uInt($int); |
445 | - else // PHP_INT_SIZE === 8 // 64 bit |
|
455 | + } else // PHP_INT_SIZE === 8 // 64 bit |
|
446 | 456 | { |
447 | 457 | $arr = unpack("L", pack("L", $int)); |
448 | 458 | return $arr[1]; |
@@ -484,8 +494,9 @@ discard block |
||
484 | 494 | */ |
485 | 495 | public static function rotBitsLeft32($i, $shifts) |
486 | 496 | { |
487 | - if ($shifts <= 0) |
|
488 | - return $i; |
|
497 | + if ($shifts <= 0) { |
|
498 | + return $i; |
|
499 | + } |
|
489 | 500 | |
490 | 501 | $shifts &= 0x1f; /* higher rotates would not bring anything */ |
491 | 502 | |
@@ -509,8 +520,9 @@ discard block |
||
509 | 520 | */ |
510 | 521 | public static function rotBitsRight32($i, $shifts) |
511 | 522 | { |
512 | - if ($shifts <= 0) |
|
513 | - return $i; |
|
523 | + if ($shifts <= 0) { |
|
524 | + return $i; |
|
525 | + } |
|
514 | 526 | |
515 | 527 | $shifts &= 0x1f; /* higher rotates would not bring anything */ |
516 | 528 | |
@@ -547,19 +559,19 @@ discard block |
||
547 | 559 | |
548 | 560 | if ($src == PHP_Crypt::RAND_DEV_RAND) |
549 | 561 | { |
550 | - if (file_exists(PHP_Crypt::RAND_DEV_RAND)) |
|
551 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
552 | - else |
|
553 | - $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
554 | - } |
|
555 | - else if ($src == PHP_Crypt::RAND_DEV_URAND) |
|
562 | + if (file_exists(PHP_Crypt::RAND_DEV_RAND)) { |
|
563 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
564 | + } else { |
|
565 | + $err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
566 | + } |
|
567 | + } else if ($src == PHP_Crypt::RAND_DEV_URAND) |
|
556 | 568 | { |
557 | - if (file_exists(PHP_Crypt::RAND_DEV_URAND)) |
|
558 | - $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
559 | - else |
|
560 | - $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
561 | - } |
|
562 | - else if ($src == PHP_Crypt::RAND_WIN_COM) |
|
569 | + if (file_exists(PHP_Crypt::RAND_DEV_URAND)) { |
|
570 | + $bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
571 | + } else { |
|
572 | + $err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
573 | + } |
|
574 | + } else if ($src == PHP_Crypt::RAND_WIN_COM) |
|
563 | 575 | { |
564 | 576 | if (extension_loaded('com_dotnet')) |
565 | 577 | { |
@@ -571,22 +583,23 @@ discard block |
||
571 | 583 | // binary data on Windows |
572 | 584 | $com = @new \COM("CAPICOM.Utilities.1"); |
573 | 585 | $bytes = $com->GetRandom($byte_len, 0); |
574 | - } |
|
575 | - catch (Exception $e) |
|
586 | + } catch (Exception $e) |
|
576 | 587 | { |
577 | 588 | $err_msg = "Windows COM exception: ".$e->getMessage(); |
578 | 589 | } |
579 | 590 | |
580 | - if (!$bytes) |
|
581 | - $err_msg = "Windows COM failed to create random string of bytes"; |
|
591 | + if (!$bytes) { |
|
592 | + $err_msg = "Windows COM failed to create random string of bytes"; |
|
593 | + } |
|
594 | + } else { |
|
595 | + $err_msg = "The COM_DOTNET extension is not loaded"; |
|
582 | 596 | } |
583 | - else |
|
584 | - $err_msg = "The COM_DOTNET extension is not loaded"; |
|
585 | 597 | } |
586 | 598 | |
587 | 599 | // trigger a warning if something went wrong |
588 | - if ($err_msg != "") |
|
589 | - trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
600 | + if ($err_msg != "") { |
|
601 | + trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
602 | + } |
|
590 | 603 | |
591 | 604 | // if the random bytes where not created properly or PHP_Crypt::RAND was |
592 | 605 | // passed as the $src param, create the bytes using mt_rand(). It's not |
@@ -597,8 +610,9 @@ discard block |
||
597 | 610 | |
598 | 611 | // md5() hash a random number to get a 16 byte string, keep looping |
599 | 612 | // until we have a string as long or longer than the ciphers block size |
600 | - for ($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) |
|
601 | - $bytes .= md5(mt_rand(), true); |
|
613 | + for ($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) { |
|
614 | + $bytes .= md5(mt_rand(), true); |
|
615 | + } |
|
602 | 616 | } |
603 | 617 | |
604 | 618 | // because $bytes may have come from mt_rand() or /dev/urandom which are not |
@@ -611,8 +625,9 @@ discard block |
||
611 | 625 | // some ciphers which have a block size larger than 16 bytes |
612 | 626 | $tmp = ""; |
613 | 627 | $loop = ceil(strlen($bytes) / self::HASH_LEN); |
614 | - for ($i = 0; $i < $loop; ++$i) |
|
615 | - $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
628 | + for ($i = 0; $i < $loop; ++$i) { |
|
629 | + $tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
630 | + } |
|
616 | 631 | |
617 | 632 | // grab the number of bytes equal to the requested $byte_len |
618 | 633 | return substr($tmp, 0, $byte_len); |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | */ |
151 | 151 | public function operation($op = 0) |
152 | 152 | { |
153 | - if($op == self::ENCRYPT || $op == self::DECRYPT) |
|
153 | + if ($op == self::ENCRYPT || $op == self::DECRYPT) |
|
154 | 154 | $this->operation = $op; |
155 | 155 | |
156 | 156 | return $this->operation; |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | */ |
165 | 165 | public function name($name = "") |
166 | 166 | { |
167 | - if($name != "") |
|
167 | + if ($name != "") |
|
168 | 168 | $this->cipher_name = $name; |
169 | 169 | |
170 | 170 | return $this->cipher_name; |
@@ -179,12 +179,12 @@ discard block |
||
179 | 179 | */ |
180 | 180 | public function blockSize($bytes = 0) |
181 | 181 | { |
182 | - if($bytes > 0) |
|
182 | + if ($bytes > 0) |
|
183 | 183 | $this->block_size = $bytes; |
184 | 184 | |
185 | 185 | // in some cases a blockSize is not set, such as stream ciphers. |
186 | 186 | // so just return 0 for the block size |
187 | - if(!isset($this->block_size)) |
|
187 | + if (!isset($this->block_size)) |
|
188 | 188 | return 0; |
189 | 189 | |
190 | 190 | return $this->block_size; |
@@ -217,27 +217,27 @@ discard block |
||
217 | 217 | */ |
218 | 218 | public function key($key = "", $req_sz = 0) |
219 | 219 | { |
220 | - if($key != "" && $key != null) |
|
220 | + if ($key != "" && $key != null) |
|
221 | 221 | { |
222 | 222 | // in the case where the key is changed changed after |
223 | 223 | // creating a new Cipher object and the $req_sz was not |
224 | 224 | // given, we need to make sure the new key meets the size |
225 | 225 | // requirements. This can be determined from the $this->key_len |
226 | 226 | // member set from the previous key |
227 | - if($this->key_len > 0 && $req_sz == 0) |
|
227 | + if ($this->key_len > 0 && $req_sz == 0) |
|
228 | 228 | $req_sz = $this->key_len; |
229 | 229 | else |
230 | 230 | $this->key_len = strlen($key); |
231 | 231 | |
232 | - if($req_sz > 0) |
|
232 | + if ($req_sz > 0) |
|
233 | 233 | { |
234 | - if($this->key_len > $req_sz) |
|
234 | + if ($this->key_len > $req_sz) |
|
235 | 235 | { |
236 | 236 | // shorten the key length |
237 | 237 | $key = substr($key, 0, $req_sz); |
238 | 238 | $this->key_len = $req_sz; |
239 | 239 | } |
240 | - else if($this->key_len < $req_sz) |
|
240 | + else if ($this->key_len < $req_sz) |
|
241 | 241 | { |
242 | 242 | // send a notice that the key was too small |
243 | 243 | // NEVER PAD THE KEY, THIS WOULD BE INSECURE!!!!! |
@@ -150,8 +150,9 @@ discard block |
||
150 | 150 | */ |
151 | 151 | public function operation($op = 0) |
152 | 152 | { |
153 | - if($op == self::ENCRYPT || $op == self::DECRYPT) |
|
154 | - $this->operation = $op; |
|
153 | + if($op == self::ENCRYPT || $op == self::DECRYPT) { |
|
154 | + $this->operation = $op; |
|
155 | + } |
|
155 | 156 | |
156 | 157 | return $this->operation; |
157 | 158 | } |
@@ -164,8 +165,9 @@ discard block |
||
164 | 165 | */ |
165 | 166 | public function name($name = "") |
166 | 167 | { |
167 | - if($name != "") |
|
168 | - $this->cipher_name = $name; |
|
168 | + if($name != "") { |
|
169 | + $this->cipher_name = $name; |
|
170 | + } |
|
169 | 171 | |
170 | 172 | return $this->cipher_name; |
171 | 173 | } |
@@ -179,13 +181,15 @@ discard block |
||
179 | 181 | */ |
180 | 182 | public function blockSize($bytes = 0) |
181 | 183 | { |
182 | - if($bytes > 0) |
|
183 | - $this->block_size = $bytes; |
|
184 | + if($bytes > 0) { |
|
185 | + $this->block_size = $bytes; |
|
186 | + } |
|
184 | 187 | |
185 | 188 | // in some cases a blockSize is not set, such as stream ciphers. |
186 | 189 | // so just return 0 for the block size |
187 | - if(!isset($this->block_size)) |
|
188 | - return 0; |
|
190 | + if(!isset($this->block_size)) { |
|
191 | + return 0; |
|
192 | + } |
|
189 | 193 | |
190 | 194 | return $this->block_size; |
191 | 195 | } |
@@ -224,10 +228,11 @@ discard block |
||
224 | 228 | // given, we need to make sure the new key meets the size |
225 | 229 | // requirements. This can be determined from the $this->key_len |
226 | 230 | // member set from the previous key |
227 | - if($this->key_len > 0 && $req_sz == 0) |
|
228 | - $req_sz = $this->key_len; |
|
229 | - else |
|
230 | - $this->key_len = strlen($key); |
|
231 | + if($this->key_len > 0 && $req_sz == 0) { |
|
232 | + $req_sz = $this->key_len; |
|
233 | + } else { |
|
234 | + $this->key_len = strlen($key); |
|
235 | + } |
|
231 | 236 | |
232 | 237 | if($req_sz > 0) |
233 | 238 | { |
@@ -236,8 +241,7 @@ discard block |
||
236 | 241 | // shorten the key length |
237 | 242 | $key = substr($key, 0, $req_sz); |
238 | 243 | $this->key_len = $req_sz; |
239 | - } |
|
240 | - else if($this->key_len < $req_sz) |
|
244 | + } else if($this->key_len < $req_sz) |
|
241 | 245 | { |
242 | 246 | // send a notice that the key was too small |
243 | 247 | // NEVER PAD THE KEY, THIS WOULD BE INSECURE!!!!! |
@@ -36,299 +36,299 @@ |
||
36 | 36 | */ |
37 | 37 | abstract class Mode |
38 | 38 | { |
39 | - /** |
|
40 | - * @type object $cipher The cipher object used within the mode |
|
41 | - */ |
|
42 | - protected $cipher = null; |
|
43 | - |
|
44 | - /** |
|
45 | - * @type string $iv The IV used for the mode, not all Modes |
|
46 | - * use an IV so this may be empty |
|
47 | - */ |
|
48 | - protected $iv = ""; |
|
49 | - |
|
50 | - /** |
|
51 | - * @type string $register For modes that use a register to do |
|
52 | - * encryption/decryption. This stores the unencrypted register. |
|
53 | - */ |
|
54 | - protected $register = ""; |
|
55 | - |
|
56 | - /** |
|
57 | - * @type string $enc_register For modes that use a register to do |
|
58 | - * encryption/decryption. This stores the encrypted register |
|
59 | - */ |
|
60 | - protected $enc_register = ""; |
|
61 | - |
|
62 | - /** |
|
63 | - * @type integer $block_size The byte size of the block to |
|
64 | - * encrypt/decrypt for the Mode |
|
65 | - */ |
|
66 | - private $block_size = 0; |
|
67 | - |
|
68 | - /** @type string $mode_name The name of mode currently used */ |
|
69 | - private $mode_name = ""; |
|
70 | - |
|
71 | - /** |
|
72 | - * @type string $padding The type of padding to use when required. |
|
73 | - * Padding types are defined in phpCrypt class. Defaults to |
|
74 | - * PHP_Crypt::PAD_ZERO |
|
75 | - */ |
|
76 | - private $padding = PHP_Crypt::PAD_ZERO; |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * Constructor |
|
81 | - * Sets the cipher object that will be used for encryption |
|
82 | - * |
|
83 | - * @param object $cipher One of the phpCrypt encryption cipher objects |
|
84 | - * @param string $mode_name The name of phpCrypt's modes, as defined in the mode |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - protected function __construct($mode_name, $cipher) |
|
88 | - { |
|
89 | - $this->name($mode_name); |
|
90 | - $this->cipher($cipher); |
|
91 | - $this->block_size = $this->cipher->blockSize(); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * Destructor |
|
97 | - * |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - protected function __destruct() |
|
101 | - { |
|
102 | - |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - |
|
107 | - /********************************************************************** |
|
39 | + /** |
|
40 | + * @type object $cipher The cipher object used within the mode |
|
41 | + */ |
|
42 | + protected $cipher = null; |
|
43 | + |
|
44 | + /** |
|
45 | + * @type string $iv The IV used for the mode, not all Modes |
|
46 | + * use an IV so this may be empty |
|
47 | + */ |
|
48 | + protected $iv = ""; |
|
49 | + |
|
50 | + /** |
|
51 | + * @type string $register For modes that use a register to do |
|
52 | + * encryption/decryption. This stores the unencrypted register. |
|
53 | + */ |
|
54 | + protected $register = ""; |
|
55 | + |
|
56 | + /** |
|
57 | + * @type string $enc_register For modes that use a register to do |
|
58 | + * encryption/decryption. This stores the encrypted register |
|
59 | + */ |
|
60 | + protected $enc_register = ""; |
|
61 | + |
|
62 | + /** |
|
63 | + * @type integer $block_size The byte size of the block to |
|
64 | + * encrypt/decrypt for the Mode |
|
65 | + */ |
|
66 | + private $block_size = 0; |
|
67 | + |
|
68 | + /** @type string $mode_name The name of mode currently used */ |
|
69 | + private $mode_name = ""; |
|
70 | + |
|
71 | + /** |
|
72 | + * @type string $padding The type of padding to use when required. |
|
73 | + * Padding types are defined in phpCrypt class. Defaults to |
|
74 | + * PHP_Crypt::PAD_ZERO |
|
75 | + */ |
|
76 | + private $padding = PHP_Crypt::PAD_ZERO; |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * Constructor |
|
81 | + * Sets the cipher object that will be used for encryption |
|
82 | + * |
|
83 | + * @param object $cipher One of the phpCrypt encryption cipher objects |
|
84 | + * @param string $mode_name The name of phpCrypt's modes, as defined in the mode |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + protected function __construct($mode_name, $cipher) |
|
88 | + { |
|
89 | + $this->name($mode_name); |
|
90 | + $this->cipher($cipher); |
|
91 | + $this->block_size = $this->cipher->blockSize(); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * Destructor |
|
97 | + * |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + protected function __destruct() |
|
101 | + { |
|
102 | + |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + |
|
107 | + /********************************************************************** |
|
108 | 108 | * ABSTRACT METHODS |
109 | 109 | * |
110 | 110 | * The abstract methods required by inheriting classes to implement |
111 | 111 | **********************************************************************/ |
112 | 112 | |
113 | - /** |
|
114 | - * All modes must have encrypt(), which implements |
|
115 | - * the mode using the cipher's encrypiton algorithm |
|
116 | - * |
|
117 | - * @param string $text A String to encrypt |
|
118 | - * @return boolean Always returns true |
|
119 | - */ |
|
120 | - abstract public function encrypt(&$text); |
|
113 | + /** |
|
114 | + * All modes must have encrypt(), which implements |
|
115 | + * the mode using the cipher's encrypiton algorithm |
|
116 | + * |
|
117 | + * @param string $text A String to encrypt |
|
118 | + * @return boolean Always returns true |
|
119 | + */ |
|
120 | + abstract public function encrypt(&$text); |
|
121 | 121 | |
122 | 122 | |
123 | - /** |
|
124 | - * All modes must have decrypt(), which implements |
|
125 | - * the mode using the cipher's encryption algorithm |
|
126 | - * |
|
127 | - * @param string $text A String to decrypt |
|
128 | - * @return boolean Always returns true |
|
129 | - */ |
|
130 | - abstract public function decrypt(&$text); |
|
123 | + /** |
|
124 | + * All modes must have decrypt(), which implements |
|
125 | + * the mode using the cipher's encryption algorithm |
|
126 | + * |
|
127 | + * @param string $text A String to decrypt |
|
128 | + * @return boolean Always returns true |
|
129 | + */ |
|
130 | + abstract public function decrypt(&$text); |
|
131 | 131 | |
132 | 132 | |
133 | - /** |
|
134 | - * Indicates whether or not a mode requires an IV |
|
135 | - * |
|
136 | - * @return boolean Always returns true or false |
|
137 | - */ |
|
138 | - abstract public function requiresIV(); |
|
133 | + /** |
|
134 | + * Indicates whether or not a mode requires an IV |
|
135 | + * |
|
136 | + * @return boolean Always returns true or false |
|
137 | + */ |
|
138 | + abstract public function requiresIV(); |
|
139 | 139 | |
140 | 140 | |
141 | 141 | |
142 | 142 | |
143 | - /********************************************************************** |
|
143 | + /********************************************************************** |
|
144 | 144 | * PUBLIC METHODS |
145 | 145 | * |
146 | 146 | **********************************************************************/ |
147 | 147 | |
148 | - /** |
|
149 | - * Create an IV if the Mode used requires an IV. |
|
150 | - * The IV should be saved and used for Encryption/Decryption |
|
151 | - * of the same blocks of data. |
|
152 | - * There are 3 ways to auto generate an IV by setting $src parameter |
|
153 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
154 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
155 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
156 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
157 | - * |
|
158 | - * @param string $src Optional, Sets how the IV is generated, must be |
|
159 | - * one of the predefined PHP_Crypt RAND constants. Defaults to |
|
160 | - * PHP_Crypt::RAND if none is given. |
|
161 | - * @return string The IV that is being used by the mode |
|
162 | - */ |
|
163 | - public function createIV($src = PHP_Crypt::RAND) |
|
164 | - { |
|
165 | - // if the mode does not use an IV, lets not waste time |
|
166 | - if (!$this->requiresIV()) |
|
167 | - return false; |
|
168 | - |
|
169 | - $iv = Core::randBytes($src, $this->block_size); |
|
170 | - return $this->IV($iv); |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * Sets or Returns an IV for the mode to use. If the $iv parameter |
|
176 | - * is not given, this function only returns the current IV in use. |
|
177 | - * |
|
178 | - * @param string $iv Optional, An IV to use for the mode and cipher selected |
|
179 | - * @return string The current IV being used |
|
180 | - */ |
|
181 | - public function IV($iv = null) |
|
182 | - { |
|
183 | - if ($iv != null) |
|
184 | - { |
|
185 | - // check that the iv is the correct length, |
|
186 | - $len = strlen($iv); |
|
187 | - if ($len != $this->block_size) |
|
188 | - { |
|
189 | - $msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes"; |
|
190 | - trigger_error($msg, E_USER_WARNING); |
|
191 | - } |
|
192 | - |
|
193 | - $this->clearRegisters(); |
|
194 | - $this->register = $iv; |
|
195 | - $this->iv = $iv; |
|
196 | - } |
|
197 | - |
|
198 | - return $this->iv; |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - /** |
|
203 | - * Checks to see if the current mode requires an IV and that it is set |
|
204 | - * if it is required. Triggers E_USER_WARNING an IV is required and not set |
|
205 | - * |
|
206 | - * @return void |
|
207 | - */ |
|
208 | - public function checkIV() |
|
209 | - { |
|
210 | - if ($this->requiresIV() && strlen($this->register) == 0) |
|
211 | - { |
|
212 | - $msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty"; |
|
213 | - trigger_error($msg, E_USER_WARNING); |
|
214 | - } |
|
215 | - } |
|
216 | - |
|
217 | - |
|
218 | - /** |
|
219 | - * Sets and returns the name of the mode being used |
|
220 | - * If $name parameter is set, sets the mode. If |
|
221 | - * $name is not set, returns the current mode in use |
|
222 | - * |
|
223 | - * @param string $name Optional, One of the predefined |
|
224 | - * phpCrypt mode constant names |
|
225 | - * @return string One of the predefined phpCrypt mode |
|
226 | - * constant mode names |
|
227 | - */ |
|
228 | - public function name($name = "") |
|
229 | - { |
|
230 | - if ($name != "") |
|
231 | - $this->mode_name = $name; |
|
232 | - |
|
233 | - return $this->mode_name; |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * Sets or Returns the padding type used with the mode |
|
239 | - * If the $type parameter is not given, this function |
|
240 | - * returns the the padding type only. |
|
241 | - * |
|
242 | - * @param string $type One of the predefined padding types |
|
243 | - * @return string |
|
244 | - */ |
|
245 | - public function padding($type = "") |
|
246 | - { |
|
247 | - if ($type != "") |
|
248 | - $this->padding = $type; |
|
249 | - |
|
250 | - return $this->padding; |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * Returns or Sets the cipher object being used |
|
256 | - * If the $cipher parameter is set with a cipher object, |
|
257 | - * the cipher current cipher will be set to this cipher. |
|
258 | - * If the $cipher parameter is not set, returns the |
|
259 | - * current cipher object being used. |
|
260 | - * |
|
261 | - * @param object $cipher Optional, An object of type Cipher |
|
262 | - * @return object An object of type Cipher |
|
263 | - */ |
|
264 | - public function cipher($cipher = null) |
|
265 | - { |
|
266 | - if (is_object($cipher)) |
|
267 | - $this->cipher = $cipher; |
|
268 | - |
|
269 | - return $this->cipher; |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - |
|
274 | - |
|
275 | - /********************************************************************** |
|
148 | + /** |
|
149 | + * Create an IV if the Mode used requires an IV. |
|
150 | + * The IV should be saved and used for Encryption/Decryption |
|
151 | + * of the same blocks of data. |
|
152 | + * There are 3 ways to auto generate an IV by setting $src parameter |
|
153 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
154 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
155 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
156 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
157 | + * |
|
158 | + * @param string $src Optional, Sets how the IV is generated, must be |
|
159 | + * one of the predefined PHP_Crypt RAND constants. Defaults to |
|
160 | + * PHP_Crypt::RAND if none is given. |
|
161 | + * @return string The IV that is being used by the mode |
|
162 | + */ |
|
163 | + public function createIV($src = PHP_Crypt::RAND) |
|
164 | + { |
|
165 | + // if the mode does not use an IV, lets not waste time |
|
166 | + if (!$this->requiresIV()) |
|
167 | + return false; |
|
168 | + |
|
169 | + $iv = Core::randBytes($src, $this->block_size); |
|
170 | + return $this->IV($iv); |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * Sets or Returns an IV for the mode to use. If the $iv parameter |
|
176 | + * is not given, this function only returns the current IV in use. |
|
177 | + * |
|
178 | + * @param string $iv Optional, An IV to use for the mode and cipher selected |
|
179 | + * @return string The current IV being used |
|
180 | + */ |
|
181 | + public function IV($iv = null) |
|
182 | + { |
|
183 | + if ($iv != null) |
|
184 | + { |
|
185 | + // check that the iv is the correct length, |
|
186 | + $len = strlen($iv); |
|
187 | + if ($len != $this->block_size) |
|
188 | + { |
|
189 | + $msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes"; |
|
190 | + trigger_error($msg, E_USER_WARNING); |
|
191 | + } |
|
192 | + |
|
193 | + $this->clearRegisters(); |
|
194 | + $this->register = $iv; |
|
195 | + $this->iv = $iv; |
|
196 | + } |
|
197 | + |
|
198 | + return $this->iv; |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + /** |
|
203 | + * Checks to see if the current mode requires an IV and that it is set |
|
204 | + * if it is required. Triggers E_USER_WARNING an IV is required and not set |
|
205 | + * |
|
206 | + * @return void |
|
207 | + */ |
|
208 | + public function checkIV() |
|
209 | + { |
|
210 | + if ($this->requiresIV() && strlen($this->register) == 0) |
|
211 | + { |
|
212 | + $msg = strtoupper($this->mode_name)." mode requires an IV or the IV is empty"; |
|
213 | + trigger_error($msg, E_USER_WARNING); |
|
214 | + } |
|
215 | + } |
|
216 | + |
|
217 | + |
|
218 | + /** |
|
219 | + * Sets and returns the name of the mode being used |
|
220 | + * If $name parameter is set, sets the mode. If |
|
221 | + * $name is not set, returns the current mode in use |
|
222 | + * |
|
223 | + * @param string $name Optional, One of the predefined |
|
224 | + * phpCrypt mode constant names |
|
225 | + * @return string One of the predefined phpCrypt mode |
|
226 | + * constant mode names |
|
227 | + */ |
|
228 | + public function name($name = "") |
|
229 | + { |
|
230 | + if ($name != "") |
|
231 | + $this->mode_name = $name; |
|
232 | + |
|
233 | + return $this->mode_name; |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * Sets or Returns the padding type used with the mode |
|
239 | + * If the $type parameter is not given, this function |
|
240 | + * returns the the padding type only. |
|
241 | + * |
|
242 | + * @param string $type One of the predefined padding types |
|
243 | + * @return string |
|
244 | + */ |
|
245 | + public function padding($type = "") |
|
246 | + { |
|
247 | + if ($type != "") |
|
248 | + $this->padding = $type; |
|
249 | + |
|
250 | + return $this->padding; |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * Returns or Sets the cipher object being used |
|
256 | + * If the $cipher parameter is set with a cipher object, |
|
257 | + * the cipher current cipher will be set to this cipher. |
|
258 | + * If the $cipher parameter is not set, returns the |
|
259 | + * current cipher object being used. |
|
260 | + * |
|
261 | + * @param object $cipher Optional, An object of type Cipher |
|
262 | + * @return object An object of type Cipher |
|
263 | + */ |
|
264 | + public function cipher($cipher = null) |
|
265 | + { |
|
266 | + if (is_object($cipher)) |
|
267 | + $this->cipher = $cipher; |
|
268 | + |
|
269 | + return $this->cipher; |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + |
|
274 | + |
|
275 | + /********************************************************************** |
|
276 | 276 | * PROTECTED METHODS |
277 | 277 | * |
278 | 278 | **********************************************************************/ |
279 | 279 | |
280 | - /** |
|
281 | - * Pads str so that final block is $block_bits in size, if the final block |
|
282 | - * is $block_bits, then an additional block is added that is $block_bits in size |
|
283 | - * The padding should be set by phpCrypt::setPadding() |
|
284 | - * |
|
285 | - * @param string $str the string to be padded |
|
286 | - * @return boolean Returns true |
|
287 | - */ |
|
288 | - protected function pad(&$str) |
|
289 | - { |
|
290 | - $len = strlen($str); |
|
291 | - $bytes = $this->cipher->blockSize(); // returns bytes |
|
292 | - |
|
293 | - // now determine the next multiple of blockSize(), then find |
|
294 | - // the difference between that and the length of $str, |
|
295 | - // this is how many padding bytes we will need |
|
296 | - $num = ceil($len / $bytes) * $bytes; |
|
297 | - $num = $num - $len; |
|
298 | - |
|
299 | - Padding::pad($str, $num, $this->padding); |
|
300 | - return true; |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * Strip out the padded blocks created from Pad(). |
|
306 | - * Padding type should be set by phpCrypt::setPadding() |
|
307 | - * |
|
308 | - * @param string $str the string to strip padding from |
|
309 | - * @return boolean Returns True |
|
310 | - */ |
|
311 | - protected function strip(&$str) |
|
312 | - { |
|
313 | - Padding::strip($str, $this->padding); |
|
314 | - return true; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /********************************************************************** |
|
280 | + /** |
|
281 | + * Pads str so that final block is $block_bits in size, if the final block |
|
282 | + * is $block_bits, then an additional block is added that is $block_bits in size |
|
283 | + * The padding should be set by phpCrypt::setPadding() |
|
284 | + * |
|
285 | + * @param string $str the string to be padded |
|
286 | + * @return boolean Returns true |
|
287 | + */ |
|
288 | + protected function pad(&$str) |
|
289 | + { |
|
290 | + $len = strlen($str); |
|
291 | + $bytes = $this->cipher->blockSize(); // returns bytes |
|
292 | + |
|
293 | + // now determine the next multiple of blockSize(), then find |
|
294 | + // the difference between that and the length of $str, |
|
295 | + // this is how many padding bytes we will need |
|
296 | + $num = ceil($len / $bytes) * $bytes; |
|
297 | + $num = $num - $len; |
|
298 | + |
|
299 | + Padding::pad($str, $num, $this->padding); |
|
300 | + return true; |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * Strip out the padded blocks created from Pad(). |
|
306 | + * Padding type should be set by phpCrypt::setPadding() |
|
307 | + * |
|
308 | + * @param string $str the string to strip padding from |
|
309 | + * @return boolean Returns True |
|
310 | + */ |
|
311 | + protected function strip(&$str) |
|
312 | + { |
|
313 | + Padding::strip($str, $this->padding); |
|
314 | + return true; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /********************************************************************** |
|
319 | 319 | * PRIVATE METHODS |
320 | 320 | * |
321 | 321 | **********************************************************************/ |
322 | 322 | |
323 | - /** |
|
324 | - * Clears the registers used for some modes |
|
325 | - * |
|
326 | - * @return void |
|
327 | - */ |
|
328 | - private function clearRegisters() |
|
329 | - { |
|
330 | - $this->register = ""; |
|
331 | - $this->enc_register = ""; |
|
332 | - } |
|
323 | + /** |
|
324 | + * Clears the registers used for some modes |
|
325 | + * |
|
326 | + * @return void |
|
327 | + */ |
|
328 | + private function clearRegisters() |
|
329 | + { |
|
330 | + $this->register = ""; |
|
331 | + $this->enc_register = ""; |
|
332 | + } |
|
333 | 333 | } |
334 | 334 | ?> |
@@ -163,8 +163,9 @@ discard block |
||
163 | 163 | public function createIV($src = PHP_Crypt::RAND) |
164 | 164 | { |
165 | 165 | // if the mode does not use an IV, lets not waste time |
166 | - if (!$this->requiresIV()) |
|
167 | - return false; |
|
166 | + if (!$this->requiresIV()) { |
|
167 | + return false; |
|
168 | + } |
|
168 | 169 | |
169 | 170 | $iv = Core::randBytes($src, $this->block_size); |
170 | 171 | return $this->IV($iv); |
@@ -227,8 +228,9 @@ discard block |
||
227 | 228 | */ |
228 | 229 | public function name($name = "") |
229 | 230 | { |
230 | - if ($name != "") |
|
231 | - $this->mode_name = $name; |
|
231 | + if ($name != "") { |
|
232 | + $this->mode_name = $name; |
|
233 | + } |
|
232 | 234 | |
233 | 235 | return $this->mode_name; |
234 | 236 | } |
@@ -244,8 +246,9 @@ discard block |
||
244 | 246 | */ |
245 | 247 | public function padding($type = "") |
246 | 248 | { |
247 | - if ($type != "") |
|
248 | - $this->padding = $type; |
|
249 | + if ($type != "") { |
|
250 | + $this->padding = $type; |
|
251 | + } |
|
249 | 252 | |
250 | 253 | return $this->padding; |
251 | 254 | } |
@@ -263,8 +266,9 @@ discard block |
||
263 | 266 | */ |
264 | 267 | public function cipher($cipher = null) |
265 | 268 | { |
266 | - if (is_object($cipher)) |
|
267 | - $this->cipher = $cipher; |
|
269 | + if (is_object($cipher)) { |
|
270 | + $this->cipher = $cipher; |
|
271 | + } |
|
268 | 272 | |
269 | 273 | return $this->cipher; |
270 | 274 | } |
@@ -39,76 +39,76 @@ |
||
39 | 39 | */ |
40 | 40 | class Mode_Raw extends Mode |
41 | 41 | { |
42 | - /** |
|
43 | - * Constructor |
|
44 | - * Sets the cipher object that will be used for encryption |
|
45 | - * |
|
46 | - * @param object $cipher one of the phpCrypt encryption cipher objects |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function __construct($cipher) |
|
50 | - { |
|
51 | - parent::__construct(PHP_Crypt::MODE_RAW, $cipher); |
|
52 | - } |
|
42 | + /** |
|
43 | + * Constructor |
|
44 | + * Sets the cipher object that will be used for encryption |
|
45 | + * |
|
46 | + * @param object $cipher one of the phpCrypt encryption cipher objects |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function __construct($cipher) |
|
50 | + { |
|
51 | + parent::__construct(PHP_Crypt::MODE_RAW, $cipher); |
|
52 | + } |
|
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Constructor used by classes that extend this class |
|
57 | - * Used by Mode_Stream, which extends this class |
|
58 | - * |
|
59 | - * @param object $cipher One of phpCrypts cipher objects |
|
60 | - * @param integer $mode The mode constant identifier |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - protected function __construct1($mode, $cipher) |
|
64 | - { |
|
65 | - parent::__construct($mode, $cipher); |
|
66 | - } |
|
55 | + /** |
|
56 | + * Constructor used by classes that extend this class |
|
57 | + * Used by Mode_Stream, which extends this class |
|
58 | + * |
|
59 | + * @param object $cipher One of phpCrypts cipher objects |
|
60 | + * @param integer $mode The mode constant identifier |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + protected function __construct1($mode, $cipher) |
|
64 | + { |
|
65 | + parent::__construct($mode, $cipher); |
|
66 | + } |
|
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * Destructor |
|
71 | - */ |
|
72 | - public function __destruct() |
|
73 | - { |
|
74 | - parent::__destruct(); |
|
75 | - } |
|
69 | + /** |
|
70 | + * Destructor |
|
71 | + */ |
|
72 | + public function __destruct() |
|
73 | + { |
|
74 | + parent::__destruct(); |
|
75 | + } |
|
76 | 76 | |
77 | 77 | |
78 | - /** |
|
79 | - * Encrypts an the string using the Cipher with no Mode |
|
80 | - * NOTE: The data in $text must be the exact length required by the Cipher |
|
81 | - * |
|
82 | - * @return boolean Always returns false |
|
83 | - */ |
|
84 | - public function encrypt(&$text) |
|
85 | - { |
|
86 | - $this->cipher->encrypt($text); |
|
87 | - return true; |
|
88 | - } |
|
78 | + /** |
|
79 | + * Encrypts an the string using the Cipher with no Mode |
|
80 | + * NOTE: The data in $text must be the exact length required by the Cipher |
|
81 | + * |
|
82 | + * @return boolean Always returns false |
|
83 | + */ |
|
84 | + public function encrypt(&$text) |
|
85 | + { |
|
86 | + $this->cipher->encrypt($text); |
|
87 | + return true; |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * Decrypts one block of cipher text, not using any mode. |
|
93 | - * NOTE: The data in $text must be the exact length required by the Cipher |
|
94 | - * |
|
95 | - * @return boolean Always returns false |
|
96 | - */ |
|
97 | - public function decrypt(&$text) |
|
98 | - { |
|
99 | - $this->cipher->decrypt($text); |
|
100 | - return true; |
|
101 | - } |
|
91 | + /** |
|
92 | + * Decrypts one block of cipher text, not using any mode. |
|
93 | + * NOTE: The data in $text must be the exact length required by the Cipher |
|
94 | + * |
|
95 | + * @return boolean Always returns false |
|
96 | + */ |
|
97 | + public function decrypt(&$text) |
|
98 | + { |
|
99 | + $this->cipher->decrypt($text); |
|
100 | + return true; |
|
101 | + } |
|
102 | 102 | |
103 | 103 | |
104 | - /** |
|
105 | - * This mode does not require an IV |
|
106 | - * |
|
107 | - * @return boolean false |
|
108 | - */ |
|
109 | - public function requiresIV() |
|
110 | - { |
|
111 | - return false; |
|
112 | - } |
|
104 | + /** |
|
105 | + * This mode does not require an IV |
|
106 | + * |
|
107 | + * @return boolean false |
|
108 | + */ |
|
109 | + public function requiresIV() |
|
110 | + { |
|
111 | + return false; |
|
112 | + } |
|
113 | 113 | } |
114 | 114 | ?> |