| @@ 217-265 (lines=49) @@ | ||
| 214 | * @param integer $i The round number |
|
| 215 | * @return void |
|
| 216 | */ |
|
| 217 | private function ruleA(&$bytes, $key, $i) |
|
| 218 | { |
|
| 219 | $w = str_split($bytes, 2); |
|
| 220 | ||
| 221 | if ($this->operation() == parent::ENCRYPT) |
|
| 222 | { |
|
| 223 | /* |
|
| 224 | * Set the W3 as the old W2 |
|
| 225 | * Set the W2 as the old W1 |
|
| 226 | * Set the W1 as the G(W0) |
|
| 227 | * Set the W0 as the W1 xor W4 xor i |
|
| 228 | */ |
|
| 229 | ||
| 230 | $w[4] = $w[3]; |
|
| 231 | $w[3] = $w[2]; |
|
| 232 | $w[2] = $w[1]; |
|
| 233 | $w[1] = $this->gPermutation($w[0], $key); |
|
| 234 | ||
| 235 | $hex1 = $this->str2Hex($w[1]); |
|
| 236 | $hex4 = $this->str2Hex($w[4]); |
|
| 237 | $hexi = $this->dec2Hex($i); |
|
| 238 | $w[0] = $this->xorHex($hex1, $hex4, $hexi); |
|
| 239 | $w[0] = $this->hex2Str($w[0]); |
|
| 240 | } else // parent::DECRYPT |
|
| 241 | { |
|
| 242 | /* |
|
| 243 | * Set W4 as W0 xor W1 xor i |
|
| 244 | * Set W0 as Inverse G(W1) |
|
| 245 | * Set W1 as the old W2 |
|
| 246 | * Set W2 as the old W3 |
|
| 247 | * Set W3 as W4 |
|
| 248 | */ |
|
| 249 | ||
| 250 | $hex0 = $this->str2Hex($w[0]); |
|
| 251 | $hex1 = $this->str2Hex($w[1]); |
|
| 252 | $hexi = $this->dec2Hex($i); |
|
| 253 | $w[4] = $this->xorHex($hex0, $hex1, $hexi); |
|
| 254 | $w[4] = $this->hex2Str($w[4]); |
|
| 255 | ||
| 256 | $w[0] = $this->gPermutation($w[1], $key); |
|
| 257 | $w[1] = $w[2]; |
|
| 258 | $w[2] = $w[3]; |
|
| 259 | $w[3] = $w[4]; |
|
| 260 | } |
|
| 261 | ||
| 262 | // glue all the pieces back together |
|
| 263 | $bytes = $w[0].$w[1].$w[2].$w[3]; |
|
| 264 | } |
|
| 265 | ||
| 266 | ||
| 267 | /** |
|
| 268 | * Perform SkipJacks RuleB function. Split the data into 4 parts, |
|
| @@ 276-325 (lines=50) @@ | ||
| 273 | * @param integer $i The round number |
|
| 274 | * @return void |
|
| 275 | */ |
|
| 276 | private function ruleB(&$bytes, $key, $i) |
|
| 277 | { |
|
| 278 | $w = str_split($bytes, 2); |
|
| 279 | ||
| 280 | if ($this->operation() == parent::ENCRYPT) |
|
| 281 | { |
|
| 282 | /* |
|
| 283 | * Set the new W3 as the old W2 |
|
| 284 | * Set the new W2 as the old W0 xor old W1 xor i |
|
| 285 | * Set the new W1 as G(old W0) |
|
| 286 | * Set the new W0 as the old W3 |
|
| 287 | */ |
|
| 288 | ||
| 289 | $w[4] = $w[3]; |
|
| 290 | $w[3] = $w[2]; |
|
| 291 | ||
| 292 | $hex0 = $this->str2Hex($w[0]); |
|
| 293 | $hex1 = $this->str2Hex($w[1]); |
|
| 294 | $hexi = $this->dec2Hex($i); |
|
| 295 | $w[2] = $this->xorHex($hex0, $hex1, $hexi); |
|
| 296 | $w[2] = $this->hex2Str($w[2]); |
|
| 297 | ||
| 298 | $w[1] = $this->gPermutation($w[0], $key); |
|
| 299 | $w[0] = $w[4]; |
|
| 300 | } else // parent::DECRYPT |
|
| 301 | { |
|
| 302 | /* |
|
| 303 | * Set W4 as the old W0 |
|
| 304 | * Set new W0 as Inverse G(old W1) |
|
| 305 | * Set new W1 as Inverse G(old W1) xor old W2 xor i |
|
| 306 | * Set new W2 as the old W3 |
|
| 307 | * Set new W0 as the old W4 |
|
| 308 | */ |
|
| 309 | ||
| 310 | $w[4] = $w[0]; |
|
| 311 | $w[0] = $this->gPermutation($w[1], $key); |
|
| 312 | ||
| 313 | $hex0 = $this->str2Hex($w[0]); |
|
| 314 | $hex2 = $this->str2Hex($w[2]); |
|
| 315 | $hexi = $this->dec2Hex($i); |
|
| 316 | $w[1] = $this->xorHex($hex0, $hex2, $hexi); |
|
| 317 | $w[1] = $this->hex2Str($w[1]); |
|
| 318 | ||
| 319 | $w[2] = $w[3]; |
|
| 320 | $w[3] = $w[4]; |
|
| 321 | } |
|
| 322 | ||
| 323 | $bytes = $w[0].$w[1].$w[2].$w[3]; |
|
| 324 | } |
|
| 325 | ||
| 326 | ||
| 327 | /** |
|
| 328 | * Expands the key from 10 bytes, to 128 bytes |
|