Merge branch '1.0' into 2.0

This commit is contained in:
terrafrost 2022-08-28 10:34:26 -05:00
commit 88fbf6ab97
5 changed files with 105 additions and 24 deletions

View File

@ -506,12 +506,16 @@ abstract class Base
$this->use_inline_crypt = version_compare(PHP_VERSION, '5.3.0') >= 0 || function_exists('create_function'); $this->use_inline_crypt = version_compare(PHP_VERSION, '5.3.0') >= 0 || function_exists('create_function');
} }
if (!defined('PHP_INT_SIZE')) {
define('PHP_INT_SIZE', 4);
}
if (!defined('CRYPT_BASE_USE_REG_INTVAL')) { if (!defined('CRYPT_BASE_USE_REG_INTVAL')) {
switch (true) { switch (true) {
// PHP_OS & "\xDF\xDF\xDF" == strtoupper(substr(PHP_OS, 0, 3)), but a lot faster // PHP_OS & "\xDF\xDF\xDF" == strtoupper(substr(PHP_OS, 0, 3)), but a lot faster
case (PHP_OS & "\xDF\xDF\xDF") === 'WIN': case (PHP_OS & "\xDF\xDF\xDF") === 'WIN':
case (php_uname('m') & "\xDF\xDF\xDF") != 'ARM': case (php_uname('m') & "\xDF\xDF\xDF") != 'ARM':
case defined('PHP_INT_SIZE') && PHP_INT_SIZE == 8: case PHP_INT_SIZE == 8:
define('CRYPT_BASE_USE_REG_INTVAL', true); define('CRYPT_BASE_USE_REG_INTVAL', true);
break; break;
case (php_uname('m') & "\xDF\xDF\xDF") == 'ARM': case (php_uname('m') & "\xDF\xDF\xDF") == 'ARM':
@ -2858,7 +2862,7 @@ abstract class Base
*/ */
function safe_intval($x) function safe_intval($x)
{ {
if (!CRYPT_BASE_USE_REG_INTVAL || is_int($x)) { if (is_int($x)) {
return $x; return $x;
} }
return (fmod($x, 0x80000000) & 0x7FFFFFFF) | return (fmod($x, 0x80000000) & 0x7FFFFFFF) |
@ -2874,7 +2878,7 @@ abstract class Base
function safe_intval_inline() function safe_intval_inline()
{ {
if (CRYPT_BASE_USE_REG_INTVAL) { if (CRYPT_BASE_USE_REG_INTVAL) {
return '%s'; return PHP_INT_SIZE == 4 ? 'intval(%s)' : '%s';
} }
$safeint = '(is_int($temp = %s) ? $temp : (fmod($temp, 0x80000000) & 0x7FFFFFFF) | '; $safeint = '(is_int($temp = %s) ? $temp : (fmod($temp, 0x80000000) & 0x7FFFFFFF) | ';

View File

@ -373,6 +373,41 @@ class Blowfish extends Base
*/ */
var $key_length = 16; var $key_length = 16;
/**
* Default Constructor.
*
* Determines whether or not the mcrypt extension should be used.
*
* $mode could be:
*
* - CRYPT_MODE_ECB
*
* - CRYPT_MODE_CBC
*
* - CRYPT_MODE_CTR
*
* - CRYPT_MODE_CFB
*
* - CRYPT_MODE_OFB
*
* (or the alias constants of the chosen cipher, for example for AES: CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC ...)
*
* If not explicitly set, CRYPT_MODE_CBC will be used.
*
* @param int $mode
* @access public
*/
function __construct($mode = CRYPT_MODE_CBC)
{
parent::__construct($mode);
$this->sbox0 = array_map('intval', $this->sbox0);
$this->sbox1 = array_map('intval', $this->sbox1);
$this->sbox2 = array_map('intval', $this->sbox2);
$this->sbox3 = array_map('intval', $this->sbox3);
$this->parray = array_map('intval', $this->parray);
}
/** /**
* Sets the key length. * Sets the key length.
* *
@ -457,7 +492,7 @@ class Blowfish extends Base
$j = 0; $j = 0;
} }
} }
$this->bctx['p'][] = $this->parray[$i] ^ $data; $this->bctx['p'][] = $this->parray[$i] ^ intval($data);
} }
// encrypt the zero-string, replace P1 and P2 with the encrypted data, // encrypt the zero-string, replace P1 and P2 with the encrypted data,
@ -528,7 +563,8 @@ class Blowfish extends Base
*/ */
function bcrypt_pbkdf($pass, $salt, $keylen, $rounds) function bcrypt_pbkdf($pass, $salt, $keylen, $rounds)
{ {
if (!CRYPT_BASE_USE_REG_INTVAL) { if (PHP_INT_SIZE == 4) {
user_error('bcrypt is far too slow to be practical on 32-bit versions of PHP');
return false; return false;
} }

View File

@ -1246,9 +1246,9 @@ class DES extends Base
$pc2mapd3[($d >> 8) & 0xFF] | $pc2mapd4[ $d & 0xFF]; $pc2mapd3[($d >> 8) & 0xFF] | $pc2mapd4[ $d & 0xFF];
// Reorder: odd bytes/even bytes. Push the result in key schedule. // Reorder: odd bytes/even bytes. Push the result in key schedule.
$val1 = ( $cp & 0xFF000000) | (($cp << 8) & 0x00FF0000) | $val1 = ( $cp & intval(0xFF000000)) | (($cp << 8) & 0x00FF0000) |
(($dp >> 16) & 0x0000FF00) | (($dp >> 8) & 0x000000FF); (($dp >> 16) & 0x0000FF00) | (($dp >> 8) & 0x000000FF);
$val2 = (($cp << 8) & 0xFF000000) | (($cp << 16) & 0x00FF0000) | $val2 = (($cp << 8) & intval(0xFF000000)) | (($cp << 16) & 0x00FF0000) |
(($dp >> 8) & 0x0000FF00) | ( $dp & 0x000000FF); (($dp >> 8) & 0x0000FF00) | ( $dp & 0x000000FF);
$keys[$des_round][self::ENCRYPT][ ] = $val1; $keys[$des_round][self::ENCRYPT][ ] = $val1;
$keys[$des_round][self::DECRYPT][$ki - 1] = $val1; $keys[$des_round][self::DECRYPT][$ki - 1] = $val1;

View File

@ -340,7 +340,7 @@ class Rijndael extends Base
$k = $c[2]; $k = $c[2];
$l = $c[3]; $l = $c[3];
while ($i < $Nb) { while ($i < $Nb) {
$temp[$i] = ($state[$i] & 0xFF000000) ^ $temp[$i] = ($state[$i] & intval(0xFF000000)) ^
($state[$j] & 0x00FF0000) ^ ($state[$j] & 0x00FF0000) ^
($state[$k] & 0x0000FF00) ^ ($state[$k] & 0x0000FF00) ^
($state[$l] & 0x000000FF) ^ ($state[$l] & 0x000000FF) ^
@ -426,7 +426,7 @@ class Rijndael extends Base
$l = $Nb - $c[3]; $l = $Nb - $c[3];
while ($i < $Nb) { while ($i < $Nb) {
$word = ($state[$i] & 0xFF000000) | $word = ($state[$i] & intval(0xFF000000)) |
($state[$j] & 0x00FF0000) | ($state[$j] & 0x00FF0000) |
($state[$k] & 0x0000FF00) | ($state[$k] & 0x0000FF00) |
($state[$l] & 0x000000FF); ($state[$l] & 0x000000FF);
@ -465,7 +465,10 @@ class Rijndael extends Base
{ {
// Each number in $rcon is equal to the previous number multiplied by two in Rijndael's finite field. // Each number in $rcon is equal to the previous number multiplied by two in Rijndael's finite field.
// See http://en.wikipedia.org/wiki/Finite_field_arithmetic#Multiplicative_inverse // See http://en.wikipedia.org/wiki/Finite_field_arithmetic#Multiplicative_inverse
static $rcon = array(0, static $rcon;
if (!isset($rcon)) {
$rcon = array(0,
0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000,
0x6C000000, 0xD8000000, 0xAB000000, 0x4D000000, 0x9A000000, 0x6C000000, 0xD8000000, 0xAB000000, 0x4D000000, 0x9A000000,
@ -473,6 +476,8 @@ class Rijndael extends Base
0x97000000, 0x35000000, 0x6A000000, 0xD4000000, 0xB3000000, 0x97000000, 0x35000000, 0x6A000000, 0xD4000000, 0xB3000000,
0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000 0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000
); );
$rcon = array_map('intval', $rcon);
}
if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->key_length === $this->kl['key_length'] && $this->block_size === $this->kl['block_size']) { if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->key_length === $this->kl['key_length'] && $this->block_size === $this->kl['block_size']) {
// already expanded // already expanded
@ -511,7 +516,7 @@ class Rijndael extends Base
// on a 32-bit machine, it's 32-bits, and on a 64-bit machine, it's 64-bits. on a 32-bit machine, // on a 32-bit machine, it's 32-bits, and on a 64-bit machine, it's 64-bits. on a 32-bit machine,
// 0xFFFFFFFF << 8 == 0xFFFFFF00, but on a 64-bit machine, it equals 0xFFFFFFFF00. as such, doing 'and' // 0xFFFFFFFF << 8 == 0xFFFFFF00, but on a 64-bit machine, it equals 0xFFFFFFFF00. as such, doing 'and'
// with 0xFFFFFFFF (or 0xFFFFFF00) on a 32-bit machine is unnecessary, but on a 64-bit machine, it is. // with 0xFFFFFFFF (or 0xFFFFFF00) on a 32-bit machine is unnecessary, but on a 64-bit machine, it is.
$temp = (($temp << 8) & 0xFFFFFF00) | (($temp >> 24) & 0x000000FF); // rotWord $temp = (($temp << 8) & intval(0xFFFFFF00)) | (($temp >> 24) & 0x000000FF); // rotWord
$temp = $this->_subWord($temp) ^ $rcon[$i / $this->Nk]; $temp = $this->_subWord($temp) ^ $rcon[$i / $this->Nk];
} elseif ($this->Nk > 6 && $i % $this->Nk == 4) { } elseif ($this->Nk > 6 && $i % $this->Nk == 4) {
$temp = $this->_subWord($temp); $temp = $this->_subWord($temp);
@ -641,9 +646,9 @@ class Rijndael extends Base
)); ));
foreach ($t3 as $t3i) { foreach ($t3 as $t3i) {
$t0[] = (($t3i << 24) & 0xFF000000) | (($t3i >> 8) & 0x00FFFFFF); $t0[] = (($t3i << 24) & intval(0xFF000000)) | (($t3i >> 8) & 0x00FFFFFF);
$t1[] = (($t3i << 16) & 0xFFFF0000) | (($t3i >> 16) & 0x0000FFFF); $t1[] = (($t3i << 16) & intval(0xFFFF0000)) | (($t3i >> 16) & 0x0000FFFF);
$t2[] = (($t3i << 8) & 0xFFFFFF00) | (($t3i >> 24) & 0x000000FF); $t2[] = (($t3i << 8) & intval(0xFFFFFF00)) | (($t3i >> 24) & 0x000000FF);
} }
$tables = array( $tables = array(
@ -725,9 +730,9 @@ class Rijndael extends Base
)); ));
foreach ($dt3 as $dt3i) { foreach ($dt3 as $dt3i) {
$dt0[] = (($dt3i << 24) & 0xFF000000) | (($dt3i >> 8) & 0x00FFFFFF); $dt0[] = (($dt3i << 24) & intval(0xFF000000)) | (($dt3i >> 8) & 0x00FFFFFF);
$dt1[] = (($dt3i << 16) & 0xFFFF0000) | (($dt3i >> 16) & 0x0000FFFF); $dt1[] = (($dt3i << 16) & intval(0xFFFF0000)) | (($dt3i >> 16) & 0x0000FFFF);
$dt2[] = (($dt3i << 8) & 0xFFFFFF00) | (($dt3i >> 24) & 0x000000FF); $dt2[] = (($dt3i << 8) & intval(0xFFFFFF00)) | (($dt3i >> 24) & 0x000000FF);
}; };
$tables = array( $tables = array(

View File

@ -368,6 +368,42 @@ class Twofish extends Base
*/ */
var $key_length = 16; var $key_length = 16;
/**
* Default Constructor.
*
* Determines whether or not the mcrypt extension should be used.
*
* $mode could be:
*
* - CRYPT_MODE_ECB
*
* - CRYPT_MODE_CBC
*
* - CRYPT_MODE_CTR
*
* - CRYPT_MODE_CFB
*
* - CRYPT_MODE_OFB
*
* (or the alias constants of the chosen cipher, for example for AES: CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC ...)
*
* If not explicitly set, CRYPT_MODE_CBC will be used.
*
* @param int $mode
* @access public
*/
function __construct($mode = CRYPT_MODE_CBC)
{
parent::__construct($mode);
$this->m0 = array_map('intval', $this->m0);
$this->m1 = array_map('intval', $this->m1);
$this->m2 = array_map('intval', $this->m2);
$this->m3 = array_map('intval', $this->m3);
$this->q0 = array_map('intval', $this->q0);
$this->q1 = array_map('intval', $this->q1);
}
/** /**
* Sets the key length. * Sets the key length.
* *