mirror of
https://github.com/phpseclib/phpseclib.git
synced 2024-12-28 12:10:59 +00:00
rm safe_intval
This commit is contained in:
parent
65f405916b
commit
e95febd5aa
@ -22,7 +22,7 @@
|
||||
* and password. This renders OpenSSL unusable and forces us to use a pure-PHP implementation
|
||||
* of blowfish.
|
||||
*
|
||||
* # phpseclib's four different _encryptBlock() implementations
|
||||
* # phpseclib's three different _encryptBlock() implementations
|
||||
*
|
||||
* When using Blowfish as an encryption algorithm, _encryptBlock() is called 9 + 512 +
|
||||
* (the number of blocks in the plaintext) times.
|
||||
@ -57,10 +57,6 @@
|
||||
* _encryptBlock() then the regular Blowfish does. That said, the Blowfish _encryptBlock() is
|
||||
* basically just a thin wrapper around the bcrypt _encryptBlock(), so there's that.
|
||||
*
|
||||
* This explains 3 of the 4 _encryptBlock() implementations. the last _encryptBlock()
|
||||
* implementation can best be understood by doing Ctrl + F and searching for where
|
||||
* self::$use_reg_intval is defined.
|
||||
*
|
||||
* # phpseclib's three different _setupKey() implementations
|
||||
*
|
||||
* Every bcrypt round is the equivalent of encrypting 512KB of data. Since OpenSSH uses 16
|
||||
@ -469,7 +465,7 @@ class Blowfish extends BlockCipher
|
||||
|
||||
for ($i = 0; $i < 64; $i++) {
|
||||
for ($j = 0; $j < 8; $j += 2) { // count($cdata) == 8
|
||||
[$cdata[$j], $cdata[$j + 1]] = self::encryptBlockHelperFast($cdata[$j], $cdata[$j + 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$cdata[$j], $cdata[$j + 1]] = self::encryptBlockHelper($cdata[$j], $cdata[$j + 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
}
|
||||
}
|
||||
|
||||
@ -481,12 +477,6 @@ class Blowfish extends BlockCipher
|
||||
*/
|
||||
public static function bcrypt_pbkdf(string $pass, string $salt, int $keylen, int $rounds): string
|
||||
{
|
||||
self::initialize_static_variables();
|
||||
|
||||
if (!self::$use_reg_intval) {
|
||||
throw new \RuntimeException('ARM-32 systems require a workaround that slows bcrypt down too much');
|
||||
}
|
||||
|
||||
$sha2pass = hash('sha512', $pass, true);
|
||||
$results = [];
|
||||
$count = 1;
|
||||
@ -550,35 +540,35 @@ class Blowfish extends BlockCipher
|
||||
];
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
[ $p[0], $p[1]] = self::encryptBlockHelperFast( 0, 0, $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[2], $p[3]] = self::encryptBlockHelperFast($p[ 0], $p[ 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[4], $p[5]] = self::encryptBlockHelperFast($p[ 2], $p[ 3], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[6], $p[7]] = self::encryptBlockHelperFast($p[ 4], $p[ 5], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[8], $p[9]] = self::encryptBlockHelperFast($p[ 6], $p[ 7], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[10], $p[11]] = self::encryptBlockHelperFast($p[ 8], $p[ 9], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[12], $p[13]] = self::encryptBlockHelperFast($p[10], $p[11], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[14], $p[15]] = self::encryptBlockHelperFast($p[12], $p[13], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[16], $p[17]] = self::encryptBlockHelperFast($p[14], $p[15], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[0], $p[1]] = self::encryptBlockHelper( 0, 0, $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[2], $p[3]] = self::encryptBlockHelper($p[ 0], $p[ 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[4], $p[5]] = self::encryptBlockHelper($p[ 2], $p[ 3], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[6], $p[7]] = self::encryptBlockHelper($p[ 4], $p[ 5], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[8], $p[9]] = self::encryptBlockHelper($p[ 6], $p[ 7], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[10], $p[11]] = self::encryptBlockHelper($p[ 8], $p[ 9], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[12], $p[13]] = self::encryptBlockHelper($p[10], $p[11], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[14], $p[15]] = self::encryptBlockHelper($p[12], $p[13], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[16], $p[17]] = self::encryptBlockHelper($p[14], $p[15], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
[$sbox0[0], $sbox0[1]] = self::encryptBlockHelperFast($p[16], $p[17], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox0[0], $sbox0[1]] = self::encryptBlockHelper($p[16], $p[17], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
for ($i = 2; $i < 256; $i += 2) {
|
||||
[$sbox0[$i], $sbox0[$i + 1]] = self::encryptBlockHelperFast($sbox0[$i - 2], $sbox0[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox0[$i], $sbox0[$i + 1]] = self::encryptBlockHelper($sbox0[$i - 2], $sbox0[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
}
|
||||
|
||||
[$sbox1[0], $sbox1[1]] = self::encryptBlockHelperFast($sbox0[254], $sbox0[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox1[0], $sbox1[1]] = self::encryptBlockHelper($sbox0[254], $sbox0[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
for ($i = 2; $i < 256; $i += 2) {
|
||||
[$sbox1[$i], $sbox1[$i + 1]] = self::encryptBlockHelperFast($sbox1[$i - 2], $sbox1[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox1[$i], $sbox1[$i + 1]] = self::encryptBlockHelper($sbox1[$i - 2], $sbox1[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
}
|
||||
|
||||
[$sbox2[0], $sbox2[1]] = self::encryptBlockHelperFast($sbox1[254], $sbox1[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox2[0], $sbox2[1]] = self::encryptBlockHelper($sbox1[254], $sbox1[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
for ($i = 2; $i < 256; $i += 2) {
|
||||
[$sbox2[$i], $sbox2[$i + 1]] = self::encryptBlockHelperFast($sbox2[$i - 2], $sbox2[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox2[$i], $sbox2[$i + 1]] = self::encryptBlockHelper($sbox2[$i - 2], $sbox2[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
}
|
||||
|
||||
[$sbox3[0], $sbox3[1]] = self::encryptBlockHelperFast($sbox2[254], $sbox2[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox3[0], $sbox3[1]] = self::encryptBlockHelper($sbox2[254], $sbox2[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
for ($i = 2; $i < 256; $i += 2) {
|
||||
[$sbox3[$i], $sbox3[$i + 1]] = self::encryptBlockHelperFast($sbox3[$i - 2], $sbox3[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox3[$i], $sbox3[$i + 1]] = self::encryptBlockHelper($sbox3[$i - 2], $sbox3[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
}
|
||||
}
|
||||
|
||||
@ -619,35 +609,35 @@ class Blowfish extends BlockCipher
|
||||
];
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
[ $p[0], $p[1]] = self::encryptBlockHelperFast($data[ 0] , $data[ 1] , $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[2], $p[3]] = self::encryptBlockHelperFast($data[ 2] ^ $p[ 0], $data[ 3] ^ $p[ 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[4], $p[5]] = self::encryptBlockHelperFast($data[ 4] ^ $p[ 2], $data[ 5] ^ $p[ 3], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[6], $p[7]] = self::encryptBlockHelperFast($data[ 6] ^ $p[ 4], $data[ 7] ^ $p[ 5], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[8], $p[9]] = self::encryptBlockHelperFast($data[ 8] ^ $p[ 6], $data[ 9] ^ $p[ 7], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[10], $p[11]] = self::encryptBlockHelperFast($data[10] ^ $p[ 8], $data[11] ^ $p[ 9], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[12], $p[13]] = self::encryptBlockHelperFast($data[12] ^ $p[10], $data[13] ^ $p[11], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[14], $p[15]] = self::encryptBlockHelperFast($data[14] ^ $p[12], $data[15] ^ $p[13], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[16], $p[17]] = self::encryptBlockHelperFast($data[ 0] ^ $p[14], $data[ 1] ^ $p[15], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[0], $p[1]] = self::encryptBlockHelper($data[ 0] , $data[ 1] , $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[2], $p[3]] = self::encryptBlockHelper($data[ 2] ^ $p[ 0], $data[ 3] ^ $p[ 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[4], $p[5]] = self::encryptBlockHelper($data[ 4] ^ $p[ 2], $data[ 5] ^ $p[ 3], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[6], $p[7]] = self::encryptBlockHelper($data[ 6] ^ $p[ 4], $data[ 7] ^ $p[ 5], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[ $p[8], $p[9]] = self::encryptBlockHelper($data[ 8] ^ $p[ 6], $data[ 9] ^ $p[ 7], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[10], $p[11]] = self::encryptBlockHelper($data[10] ^ $p[ 8], $data[11] ^ $p[ 9], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[12], $p[13]] = self::encryptBlockHelper($data[12] ^ $p[10], $data[13] ^ $p[11], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[14], $p[15]] = self::encryptBlockHelper($data[14] ^ $p[12], $data[15] ^ $p[13], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$p[16], $p[17]] = self::encryptBlockHelper($data[ 0] ^ $p[14], $data[ 1] ^ $p[15], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
[$sbox0[0], $sbox0[1]] = self::encryptBlockHelperFast($data[2] ^ $p[16], $data[3] ^ $p[17], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox0[0], $sbox0[1]] = self::encryptBlockHelper($data[2] ^ $p[16], $data[3] ^ $p[17], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
for ($i = 2, $j = 4; $i < 256; $i += 2, $j = ($j + 2) % 16) { // instead of 16 maybe count($data) would be better?
|
||||
[$sbox0[$i], $sbox0[$i + 1]] = self::encryptBlockHelperFast($data[$j] ^ $sbox0[$i - 2], $data[$j + 1] ^ $sbox0[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox0[$i], $sbox0[$i + 1]] = self::encryptBlockHelper($data[$j] ^ $sbox0[$i - 2], $data[$j + 1] ^ $sbox0[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
}
|
||||
|
||||
[$sbox1[0], $sbox1[1]] = self::encryptBlockHelperFast($data[2] ^ $sbox0[254], $data[3] ^ $sbox0[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox1[0], $sbox1[1]] = self::encryptBlockHelper($data[2] ^ $sbox0[254], $data[3] ^ $sbox0[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
for ($i = 2, $j = 4; $i < 256; $i += 2, $j = ($j + 2) % 16) {
|
||||
[$sbox1[$i], $sbox1[$i + 1]] = self::encryptBlockHelperFast($data[$j] ^ $sbox1[$i - 2], $data[$j + 1] ^ $sbox1[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox1[$i], $sbox1[$i + 1]] = self::encryptBlockHelper($data[$j] ^ $sbox1[$i - 2], $data[$j + 1] ^ $sbox1[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
}
|
||||
|
||||
[$sbox2[0], $sbox2[1]] = self::encryptBlockHelperFast($data[2] ^ $sbox1[254], $data[3] ^ $sbox1[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox2[0], $sbox2[1]] = self::encryptBlockHelper($data[2] ^ $sbox1[254], $data[3] ^ $sbox1[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
for ($i = 2, $j = 4; $i < 256; $i += 2, $j = ($j + 2) % 16) {
|
||||
[$sbox2[$i], $sbox2[$i + 1]] = self::encryptBlockHelperFast($data[$j] ^ $sbox2[$i - 2], $data[$j + 1] ^ $sbox2[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox2[$i], $sbox2[$i + 1]] = self::encryptBlockHelper($data[$j] ^ $sbox2[$i - 2], $data[$j + 1] ^ $sbox2[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
}
|
||||
|
||||
[$sbox3[0], $sbox3[1]] = self::encryptBlockHelperFast($data[2] ^ $sbox2[254], $data[3] ^ $sbox2[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox3[0], $sbox3[1]] = self::encryptBlockHelper($data[2] ^ $sbox2[254], $data[3] ^ $sbox2[255], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
for ($i = 2, $j = 4; $i < 256; $i += 2, $j = ($j + 2) % 16) {
|
||||
[$sbox3[$i], $sbox3[$i + 1]] = self::encryptBlockHelperFast($data[$j] ^ $sbox3[$i - 2], $data[$j + 1] ^ $sbox3[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
[$sbox3[$i], $sbox3[$i + 1]] = self::encryptBlockHelper($data[$j] ^ $sbox3[$i - 2], $data[$j + 1] ^ $sbox3[$i - 1], $sbox0, $sbox1, $sbox2, $sbox3, $p);
|
||||
}
|
||||
}
|
||||
|
||||
@ -667,9 +657,7 @@ class Blowfish extends BlockCipher
|
||||
$l = $in[1];
|
||||
$r = $in[2];
|
||||
|
||||
[$r, $l] = self::$use_reg_intval ?
|
||||
self::encryptBlockHelperFast($l, $r, $sb_0, $sb_1, $sb_2, $sb_3, $p) :
|
||||
self::encryptBlockHelperSlow($l, $r, $sb_0, $sb_1, $sb_2, $sb_3, $p);
|
||||
[$r, $l] = self::encryptBlockHelper($l, $r, $sb_0, $sb_1, $sb_2, $sb_3, $p);
|
||||
|
||||
return pack("N*", $r, $l);
|
||||
}
|
||||
@ -685,7 +673,7 @@ class Blowfish extends BlockCipher
|
||||
* @param int[] $p
|
||||
* @return int[]
|
||||
*/
|
||||
private static function encryptBlockHelperFast(int $x0, int $x1, array $sbox0, array $sbox1, array $sbox2, array $sbox3, array $p): array
|
||||
private static function encryptBlockHelper(int $x0, int $x1, array $sbox0, array $sbox1, array $sbox2, array $sbox3, array $p): array
|
||||
{
|
||||
$x0 ^= $p[0];
|
||||
$x1 ^= ((($sbox0[($x0 & 0xFF000000) >> 24] + $sbox1[($x0 & 0xFF0000) >> 16]) ^ $sbox2[($x0 & 0xFF00) >> 8]) + $sbox3[$x0 & 0xFF]) ^ $p[1];
|
||||
@ -708,40 +696,6 @@ class Blowfish extends BlockCipher
|
||||
return [$x1 & 0xFFFFFFFF ^ $p[17], $x0 & 0xFFFFFFFF];
|
||||
}
|
||||
|
||||
/**
|
||||
* Slow helper function for block encryption
|
||||
*
|
||||
* @access private
|
||||
* @param int[] $sbox0
|
||||
* @param int[] $sbox1
|
||||
* @param int[] $sbox2
|
||||
* @param int[] $sbox3
|
||||
* @param int[] $p
|
||||
* @return int[]
|
||||
*/
|
||||
private static function encryptBlockHelperSlow(int $x0, int $x1, array $sbox0, array $sbox1, array $sbox2, array $sbox3, array $p): array
|
||||
{
|
||||
$x0 ^= $p[0];
|
||||
$x1 ^= $this->safe_intval(($this->safe_intval($sbox0[($x0 & 0xFF000000) >> 24] + $sbox1[($x0 & 0xFF0000) >> 16]) ^ $sbox2[($x0 & 0xFF00) >> 8]) + $sbox3[$x0 & 0xFF]) ^ $p[1];
|
||||
$x0 ^= $this->safe_intval(($this->safe_intval($sbox0[($x1 & 0xFF000000) >> 24] + $sbox1[($x1 & 0xFF0000) >> 16]) ^ $sbox2[($x1 & 0xFF00) >> 8]) + $sbox3[$x1 & 0xFF]) ^ $p[2];
|
||||
$x1 ^= $this->safe_intval(($this->safe_intval($sbox0[($x0 & 0xFF000000) >> 24] + $sbox1[($x0 & 0xFF0000) >> 16]) ^ $sbox2[($x0 & 0xFF00) >> 8]) + $sbox3[$x0 & 0xFF]) ^ $p[3];
|
||||
$x0 ^= $this->safe_intval(($this->safe_intval($sbox0[($x1 & 0xFF000000) >> 24] + $sbox1[($x1 & 0xFF0000) >> 16]) ^ $sbox2[($x1 & 0xFF00) >> 8]) + $sbox3[$x1 & 0xFF]) ^ $p[4];
|
||||
$x1 ^= $this->safe_intval(($this->safe_intval($sbox0[($x0 & 0xFF000000) >> 24] + $sbox1[($x0 & 0xFF0000) >> 16]) ^ $sbox2[($x0 & 0xFF00) >> 8]) + $sbox3[$x0 & 0xFF]) ^ $p[5];
|
||||
$x0 ^= $this->safe_intval(($this->safe_intval($sbox0[($x1 & 0xFF000000) >> 24] + $sbox1[($x1 & 0xFF0000) >> 16]) ^ $sbox2[($x1 & 0xFF00) >> 8]) + $sbox3[$x1 & 0xFF]) ^ $p[6];
|
||||
$x1 ^= $this->safe_intval(($this->safe_intval($sbox0[($x0 & 0xFF000000) >> 24] + $sbox1[($x0 & 0xFF0000) >> 16]) ^ $sbox2[($x0 & 0xFF00) >> 8]) + $sbox3[$x0 & 0xFF]) ^ $p[7];
|
||||
$x0 ^= $this->safe_intval(($this->safe_intval($sbox0[($x1 & 0xFF000000) >> 24] + $sbox1[($x1 & 0xFF0000) >> 16]) ^ $sbox2[($x1 & 0xFF00) >> 8]) + $sbox3[$x1 & 0xFF]) ^ $p[8];
|
||||
$x1 ^= $this->safe_intval(($this->safe_intval($sbox0[($x0 & 0xFF000000) >> 24] + $sbox1[($x0 & 0xFF0000) >> 16]) ^ $sbox2[($x0 & 0xFF00) >> 8]) + $sbox3[$x0 & 0xFF]) ^ $p[9];
|
||||
$x0 ^= $this->safe_intval(($this->safe_intval($sbox0[($x1 & 0xFF000000) >> 24] + $sbox1[($x1 & 0xFF0000) >> 16]) ^ $sbox2[($x1 & 0xFF00) >> 8]) + $sbox3[$x1 & 0xFF]) ^ $p[10];
|
||||
$x1 ^= $this->safe_intval(($this->safe_intval($sbox0[($x0 & 0xFF000000) >> 24] + $sbox1[($x0 & 0xFF0000) >> 16]) ^ $sbox2[($x0 & 0xFF00) >> 8]) + $sbox3[$x0 & 0xFF]) ^ $p[11];
|
||||
$x0 ^= $this->safe_intval(($this->safe_intval($sbox0[($x1 & 0xFF000000) >> 24] + $sbox1[($x1 & 0xFF0000) >> 16]) ^ $sbox2[($x1 & 0xFF00) >> 8]) + $sbox3[$x1 & 0xFF]) ^ $p[12];
|
||||
$x1 ^= $this->safe_intval(($this->safe_intval($sbox0[($x0 & 0xFF000000) >> 24] + $sbox1[($x0 & 0xFF0000) >> 16]) ^ $sbox2[($x0 & 0xFF00) >> 8]) + $sbox3[$x0 & 0xFF]) ^ $p[13];
|
||||
$x0 ^= $this->safe_intval(($this->safe_intval($sbox0[($x1 & 0xFF000000) >> 24] + $sbox1[($x1 & 0xFF0000) >> 16]) ^ $sbox2[($x1 & 0xFF00) >> 8]) + $sbox3[$x1 & 0xFF]) ^ $p[14];
|
||||
$x1 ^= $this->safe_intval(($this->safe_intval($sbox0[($x0 & 0xFF000000) >> 24] + $sbox1[($x0 & 0xFF0000) >> 16]) ^ $sbox2[($x0 & 0xFF00) >> 8]) + $sbox3[$x0 & 0xFF]) ^ $p[15];
|
||||
$x0 ^= $this->safe_intval(($this->safe_intval($sbox0[($x1 & 0xFF000000) >> 24] + $sbox1[($x1 & 0xFF0000) >> 16]) ^ $sbox2[($x1 & 0xFF00) >> 8]) + $sbox3[$x1 & 0xFF]) ^ $p[16];
|
||||
|
||||
return [$x1 & 0xFFFFFFFF ^ $p[17], $x0 & 0xFFFFFFFF];
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts a block
|
||||
*/
|
||||
@ -759,14 +713,14 @@ class Blowfish extends BlockCipher
|
||||
|
||||
for ($i = 17; $i > 2; $i -= 2) {
|
||||
$l ^= $p[$i];
|
||||
$r ^= self::safe_intval((self::safe_intval($sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff]) ^
|
||||
$r ^= ($sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff] ^
|
||||
$sb_2[$l >> 8 & 0xff]) +
|
||||
$sb_3[$l & 0xff]);
|
||||
$sb_3[$l & 0xff];
|
||||
|
||||
$r ^= $p[$i - 1];
|
||||
$l ^= self::safe_intval((self::safe_intval($sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff]) ^
|
||||
$l ^= ($sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff] ^
|
||||
$sb_2[$r >> 8 & 0xff]) +
|
||||
$sb_3[$r & 0xff]);
|
||||
$sb_3[$r & 0xff];
|
||||
}
|
||||
return pack('N*', $r ^ $p[0], $l ^ $p[1]);
|
||||
}
|
||||
@ -789,8 +743,6 @@ class Blowfish extends BlockCipher
|
||||
}
|
||||
';
|
||||
|
||||
$safeint = self::safe_intval_inline();
|
||||
|
||||
// Generating encrypt code:
|
||||
$encrypt_block = '
|
||||
$in = unpack("N*", $in);
|
||||
@ -800,14 +752,14 @@ class Blowfish extends BlockCipher
|
||||
for ($i = 0; $i < 16; $i += 2) {
|
||||
$encrypt_block .= '
|
||||
$l^= ' . $p[$i] . ';
|
||||
$r^= ' . sprintf($safeint, '(' . sprintf($safeint, '$sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff]') . ' ^
|
||||
$r^= ($sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff] ^
|
||||
$sb_2[$l >> 8 & 0xff]) +
|
||||
$sb_3[$l & 0xff]') . ';
|
||||
$sb_3[$l & 0xff];
|
||||
|
||||
$r^= ' . $p[$i + 1] . ';
|
||||
$l^= ' . sprintf($safeint, '(' . sprintf($safeint, '$sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff]') . ' ^
|
||||
$l^= ($sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff] ^
|
||||
$sb_2[$r >> 8 & 0xff]) +
|
||||
$sb_3[$r & 0xff]') . ';
|
||||
$sb_3[$r & 0xff];
|
||||
';
|
||||
}
|
||||
$encrypt_block .= '
|
||||
@ -826,14 +778,14 @@ class Blowfish extends BlockCipher
|
||||
for ($i = 17; $i > 2; $i -= 2) {
|
||||
$decrypt_block .= '
|
||||
$l^= ' . $p[$i] . ';
|
||||
$r^= ' . sprintf($safeint, '(' . sprintf($safeint, '$sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff]') . ' ^
|
||||
$r^= ($sb_0[$l >> 24 & 0xff] + $sb_1[$l >> 16 & 0xff] ^
|
||||
$sb_2[$l >> 8 & 0xff]) +
|
||||
$sb_3[$l & 0xff]') . ';
|
||||
$sb_3[$l & 0xff];
|
||||
|
||||
$r^= ' . $p[$i - 1] . ';
|
||||
$l^= ' . sprintf($safeint, '(' . sprintf($safeint, '$sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff]') . ' ^
|
||||
$l^= ($sb_0[$r >> 24 & 0xff] + $sb_1[$r >> 16 & 0xff] ^
|
||||
$sb_2[$r >> 8 & 0xff]) +
|
||||
$sb_3[$r & 0xff]') . ';
|
||||
$sb_3[$r & 0xff];
|
||||
';
|
||||
}
|
||||
|
||||
|
@ -431,14 +431,6 @@ abstract class SymmetricKey
|
||||
*/
|
||||
private static $poly1305Field;
|
||||
|
||||
/**
|
||||
* Flag for using regular vs "safe" intval
|
||||
*
|
||||
* @see self::initialize_static_variables()
|
||||
* @var boolean
|
||||
*/
|
||||
protected static $use_reg_intval;
|
||||
|
||||
/**
|
||||
* Poly1305 Key
|
||||
*
|
||||
@ -541,43 +533,6 @@ abstract class SymmetricKey
|
||||
}
|
||||
|
||||
$this->mode = $mode;
|
||||
|
||||
self::initialize_static_variables();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize static variables
|
||||
*/
|
||||
protected static function initialize_static_variables(): void
|
||||
{
|
||||
if (!isset(self::$use_reg_intval)) {
|
||||
switch (true) {
|
||||
// PHP_OS & "\xDF\xDF\xDF" == strtoupper(substr(PHP_OS, 0, 3)), but a lot faster
|
||||
case (PHP_OS & "\xDF\xDF\xDF") === 'WIN':
|
||||
case (php_uname('m') & "\xDF\xDF\xDF") != 'ARM':
|
||||
case defined('PHP_INT_SIZE') && PHP_INT_SIZE == 8:
|
||||
self::$use_reg_intval = true;
|
||||
break;
|
||||
case (php_uname('m') & "\xDF\xDF\xDF") == 'ARM':
|
||||
switch (true) {
|
||||
/* PHP 7.0.0 introduced a bug that affected 32-bit ARM processors:
|
||||
|
||||
https://github.com/php/php-src/commit/716da71446ebbd40fa6cf2cea8a4b70f504cc3cd
|
||||
|
||||
altho the changelogs make no mention of it, this bug was fixed with this commit:
|
||||
|
||||
https://github.com/php/php-src/commit/c1729272b17a1fe893d1a54e423d3b71470f3ee8
|
||||
|
||||
affected versions of PHP are: 7.0.x, 7.1.0 - 7.1.23 and 7.2.0 - 7.2.11 */
|
||||
case PHP_VERSION_ID >= 70000 && PHP_VERSION_ID <= 70123:
|
||||
case PHP_VERSION_ID >= 70200 && PHP_VERSION_ID <= 70211:
|
||||
self::$use_reg_intval = false;
|
||||
break;
|
||||
default:
|
||||
self::$use_reg_intval = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2837,35 +2792,6 @@ PHP
|
||||
throw new \LogicException('\Closure::bind() failed.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert float to int
|
||||
*
|
||||
* On ARM CPUs converting floats to ints doesn't always work
|
||||
*
|
||||
* @param float|int $x
|
||||
*/
|
||||
protected static function safe_intval($x): int
|
||||
{
|
||||
if (self::$use_reg_intval || is_int($x)) {
|
||||
return $x;
|
||||
}
|
||||
return (fmod($x, 0x80000000) & 0x7FFFFFFF) |
|
||||
((fmod(floor($x / 0x80000000), 2) & 1) << 31);
|
||||
}
|
||||
|
||||
/**
|
||||
* eval()'able string for in-line float to int
|
||||
*/
|
||||
protected static function safe_intval_inline(): string
|
||||
{
|
||||
if (self::$use_reg_intval) {
|
||||
return '%s';
|
||||
}
|
||||
|
||||
$safeint = '(is_int($temp = %s) ? $temp : (fmod($temp, 0x80000000) & 0x7FFFFFFF) | ';
|
||||
return $safeint . '((fmod(floor($temp / 0x80000000), 2) & 1) << 31))';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up GCM parameters
|
||||
*
|
||||
|
@ -432,9 +432,9 @@ class Twofish extends BlockCipher
|
||||
$m2[$q1[$q0[$j] ^ $key[15]] ^ $key[7]] ^
|
||||
$m3[$q1[$q1[$j] ^ $key[16]] ^ $key[8]];
|
||||
$B = ($B << 8) | ($B >> 24 & 0xff);
|
||||
$A = self::safe_intval($A + $B);
|
||||
$A += $B;
|
||||
$K[] = $A;
|
||||
$A = self::safe_intval($A + $B);
|
||||
$A += $B;
|
||||
$K[] = ($A << 9 | $A >> 23 & 0x1ff);
|
||||
}
|
||||
for ($i = 0; $i < 256; ++$i) {
|
||||
@ -458,9 +458,9 @@ class Twofish extends BlockCipher
|
||||
$m2[$q1[$q0[$q0[$j] ^ $key[23]] ^ $key[15]] ^ $key[7]] ^
|
||||
$m3[$q1[$q1[$q0[$j] ^ $key[24]] ^ $key[16]] ^ $key[8]];
|
||||
$B = ($B << 8) | ($B >> 24 & 0xff);
|
||||
$A = self::safe_intval($A + $B);
|
||||
$A += $B;
|
||||
$K[] = $A;
|
||||
$A = self::safe_intval($A + $B);
|
||||
$A += $B;
|
||||
$K[] = ($A << 9 | $A >> 23 & 0x1ff);
|
||||
}
|
||||
for ($i = 0; $i < 256; ++$i) {
|
||||
@ -485,9 +485,9 @@ class Twofish extends BlockCipher
|
||||
$m2[$q1[$q0[$q0[$q0[$j] ^ $key[31]] ^ $key[23]] ^ $key[15]] ^ $key[7]] ^
|
||||
$m3[$q1[$q1[$q0[$q1[$j] ^ $key[32]] ^ $key[24]] ^ $key[16]] ^ $key[8]];
|
||||
$B = ($B << 8) | ($B >> 24 & 0xff);
|
||||
$A = self::safe_intval($A + $B);
|
||||
$A += $B;
|
||||
$K[] = $A;
|
||||
$A = self::safe_intval($A + $B);
|
||||
$A += $B;
|
||||
$K[] = ($A << 9 | $A >> 23 & 0x1ff);
|
||||
}
|
||||
for ($i = 0; $i < 256; ++$i) {
|
||||
@ -575,9 +575,9 @@ class Twofish extends BlockCipher
|
||||
$S1[ $R1 & 0xff] ^
|
||||
$S2[($R1 >> 8) & 0xff] ^
|
||||
$S3[($R1 >> 16) & 0xff];
|
||||
$R2 ^= self::safe_intval($t0 + $t1 + $K[++$ki]);
|
||||
$R2 ^= $t0 + $t1 + $K[++$ki];
|
||||
$R2 = ($R2 >> 1 & 0x7fffffff) | ($R2 << 31);
|
||||
$R3 = ((($R3 >> 31) & 1) | ($R3 << 1)) ^ self::safe_intval($t0 + ($t1 << 1) + $K[++$ki]);
|
||||
$R3 = ((($R3 >> 31) & 1) | ($R3 << 1)) ^ ($t0 + ($t1 << 1) + $K[++$ki]);
|
||||
|
||||
$t0 = $S0[ $R2 & 0xff] ^
|
||||
$S1[($R2 >> 8) & 0xff] ^
|
||||
@ -587,9 +587,9 @@ class Twofish extends BlockCipher
|
||||
$S1[ $R3 & 0xff] ^
|
||||
$S2[($R3 >> 8) & 0xff] ^
|
||||
$S3[($R3 >> 16) & 0xff];
|
||||
$R0 ^= self::safe_intval($t0 + $t1 + $K[++$ki]);
|
||||
$R0 ^= $t0 + $t1 + $K[++$ki];
|
||||
$R0 = ($R0 >> 1 & 0x7fffffff) | ($R0 << 31);
|
||||
$R1 = ((($R1 >> 31) & 1) | ($R1 << 1)) ^ self::safe_intval($t0 + ($t1 << 1) + $K[++$ki]);
|
||||
$R1 = ((($R1 >> 31) & 1) | ($R1 << 1)) ^ ($t0 + ($t1 << 1) + $K[++$ki]);
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
@ -627,9 +627,9 @@ class Twofish extends BlockCipher
|
||||
$S1[$R1 & 0xff] ^
|
||||
$S2[$R1 >> 8 & 0xff] ^
|
||||
$S3[$R1 >> 16 & 0xff];
|
||||
$R3 ^= self::safe_intval($t0 + ($t1 << 1) + $K[--$ki]);
|
||||
$R3 ^= $t0 + ($t1 << 1) + $K[--$ki];
|
||||
$R3 = $R3 >> 1 & 0x7fffffff | $R3 << 31;
|
||||
$R2 = ($R2 >> 31 & 0x1 | $R2 << 1) ^ self::safe_intval($t0 + $t1 + $K[--$ki]);
|
||||
$R2 = ($R2 >> 31 & 0x1 | $R2 << 1) ^ ($t0 + $t1 + $K[--$ki]);
|
||||
|
||||
$t0 = $S0[$R2 & 0xff] ^
|
||||
$S1[$R2 >> 8 & 0xff] ^
|
||||
@ -639,9 +639,9 @@ class Twofish extends BlockCipher
|
||||
$S1[$R3 & 0xff] ^
|
||||
$S2[$R3 >> 8 & 0xff] ^
|
||||
$S3[$R3 >> 16 & 0xff];
|
||||
$R1 ^= self::safe_intval($t0 + ($t1 << 1) + $K[--$ki]);
|
||||
$R1 ^= $t0 + ($t1 << 1) + $K[--$ki];
|
||||
$R1 = $R1 >> 1 & 0x7fffffff | $R1 << 31;
|
||||
$R0 = ($R0 >> 31 & 0x1 | $R0 << 1) ^ self::safe_intval($t0 + $t1 + $K[--$ki]);
|
||||
$R0 = ($R0 >> 31 & 0x1 | $R0 << 1) ^ ($t0 + $t1 + $K[--$ki]);
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
@ -672,8 +672,6 @@ class Twofish extends BlockCipher
|
||||
}
|
||||
';
|
||||
|
||||
$safeint = self::safe_intval_inline();
|
||||
|
||||
// Generating encrypt code:
|
||||
$encrypt_block = '
|
||||
$in = unpack("V4", $in);
|
||||
@ -692,9 +690,9 @@ class Twofish extends BlockCipher
|
||||
$S1[ $R1 & 0xff] ^
|
||||
$S2[($R1 >> 8) & 0xff] ^
|
||||
$S3[($R1 >> 16) & 0xff];
|
||||
$R2^= ' . sprintf($safeint, '$t0 + $t1 + ' . $K[++$ki]) . ';
|
||||
$R2^= $t0 + $t1 + ' . $K[++$ki] . ';
|
||||
$R2 = ($R2 >> 1 & 0x7fffffff) | ($R2 << 31);
|
||||
$R3 = ((($R3 >> 31) & 1) | ($R3 << 1)) ^ ' . sprintf($safeint, '($t0 + ($t1 << 1) + ' . $K[++$ki] . ')') . ';
|
||||
$R3 = ((($R3 >> 31) & 1) | ($R3 << 1)) ^ ($t0 + ($t1 << 1) + ' . $K[++$ki] . ');
|
||||
|
||||
$t0 = $S0[ $R2 & 0xff] ^
|
||||
$S1[($R2 >> 8) & 0xff] ^
|
||||
@ -704,9 +702,9 @@ class Twofish extends BlockCipher
|
||||
$S1[ $R3 & 0xff] ^
|
||||
$S2[($R3 >> 8) & 0xff] ^
|
||||
$S3[($R3 >> 16) & 0xff];
|
||||
$R0^= ' . sprintf($safeint, '($t0 + $t1 + ' . $K[++$ki] . ')') . ';
|
||||
$R0^= $t0 + $t1 + ' . $K[++$ki] . ';
|
||||
$R0 = ($R0 >> 1 & 0x7fffffff) | ($R0 << 31);
|
||||
$R1 = ((($R1 >> 31) & 1) | ($R1 << 1)) ^ ' . sprintf($safeint, '($t0 + ($t1 << 1) + ' . $K[++$ki] . ')') . ';
|
||||
$R1 = ((($R1 >> 31) & 1) | ($R1 << 1)) ^ ($t0 + ($t1 << 1) + ' . $K[++$ki] . ');
|
||||
';
|
||||
}
|
||||
$encrypt_block .= '
|
||||
@ -734,9 +732,9 @@ class Twofish extends BlockCipher
|
||||
$S1[$R1 & 0xff] ^
|
||||
$S2[$R1 >> 8 & 0xff] ^
|
||||
$S3[$R1 >> 16 & 0xff];
|
||||
$R3^= ' . sprintf($safeint, '$t0 + ($t1 << 1) + ' . $K[--$ki]) . ';
|
||||
$R3^= $t0 + ($t1 << 1) + ' . $K[--$ki] . ';
|
||||
$R3 = $R3 >> 1 & 0x7fffffff | $R3 << 31;
|
||||
$R2 = ($R2 >> 31 & 0x1 | $R2 << 1) ^ ' . sprintf($safeint, '($t0 + $t1 + ' . $K[--$ki] . ')') . ';
|
||||
$R2 = ($R2 >> 31 & 0x1 | $R2 << 1) ^ ($t0 + $t1 + ' . $K[--$ki] . ');
|
||||
|
||||
$t0 = $S0[$R2 & 0xff] ^
|
||||
$S1[$R2 >> 8 & 0xff] ^
|
||||
@ -746,9 +744,9 @@ class Twofish extends BlockCipher
|
||||
$S1[$R3 & 0xff] ^
|
||||
$S2[$R3 >> 8 & 0xff] ^
|
||||
$S3[$R3 >> 16 & 0xff];
|
||||
$R1^= ' . sprintf($safeint, '$t0 + ($t1 << 1) + ' . $K[--$ki]) . ';
|
||||
$R1^= $t0 + ($t1 << 1) + ' . $K[--$ki] . ';
|
||||
$R1 = $R1 >> 1 & 0x7fffffff | $R1 << 31;
|
||||
$R0 = ($R0 >> 31 & 0x1 | $R0 << 1) ^ ' . sprintf($safeint, '($t0 + $t1 + ' . $K[--$ki] . ')') . ';
|
||||
$R0 = ($R0 >> 31 & 0x1 | $R0 << 1) ^ ($t0 + $t1 + ' . $K[--$ki] . ');
|
||||
';
|
||||
}
|
||||
$decrypt_block .= '
|
||||
|
Loading…
Reference in New Issue
Block a user