Skip to content

Commit

Permalink
Support newer versions of Psalm.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jul 4, 2018
1 parent 7d0549c commit db9a78b
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -51,7 +51,7 @@ matrix:

install:
- composer self-update
- if [[ $USE_PSALM -eq 1 ]]; then composer require --dev "vimeo/psalm:1.0.5"; fi
- if [[ $USE_PSALM -eq 1 ]]; then composer require --dev "vimeo/psalm:^1|^2"; fi
- composer update

script:
Expand Down
13 changes: 12 additions & 1 deletion psalm.xml
@@ -1,6 +1,5 @@
<?xml version="1.0"?>
<psalm
stopOnFirstError="false"
useDocblockTypes="true"
totallyTyped="true"
>
Expand Down Expand Up @@ -28,5 +27,17 @@
<RedundantConditionGivenDocblockType errorLevel="suppress" />

<TooFewArguments errorLevel="info" />

<DocblockTypeContradiction errorLevel="info" />
<RedundantCondition errorLevel="info" />
<!--
Redundancy is good for PHP <7
-->

<TypeDoesNotContainType errorLevel="info" />
<!--
This mostly fails on `PHP_INT_SIZE === 4`
-->

</issueHandlers>
</psalm>
41 changes: 35 additions & 6 deletions src/Compat.php
Expand Up @@ -1587,10 +1587,23 @@ public static function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $sal
ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5);

if (self::useNewSodiumAPI()) {
return (string) sodium_crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit);
return (string) sodium_crypto_pwhash_scryptsalsa208sha256(
(int) $outlen,
(string) $passwd,
(string) $salt,
(int) $opslimit,
(int) $memlimit
);
}
if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256')) {
return (string) call_user_func('\\Sodium\\crypto_pwhash_scryptsalsa208sha256', $outlen, $passwd, $salt, $opslimit, $memlimit);
return (string) call_user_func(
'\\Sodium\\crypto_pwhash_scryptsalsa208sha256',
(int) $outlen,
(string) $passwd,
(string) $salt,
(int) $opslimit,
(int) $memlimit
);
}
// This is the best we can do.
throw new SodiumException(
Expand Down Expand Up @@ -1632,10 +1645,19 @@ public static function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit
ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);

if (self::useNewSodiumAPI()) {
return (string) sodium_crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit);
return (string) sodium_crypto_pwhash_scryptsalsa208sha256_str(
(string) $passwd,
(int) $opslimit,
(int) $memlimit
);
}
if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str')) {
return (string) call_user_func('\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str', $passwd, $opslimit, $memlimit);
return (string) call_user_func(
'\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str',
(string) $passwd,
(int) $opslimit,
(int) $memlimit
);
}
// This is the best we can do.
throw new SodiumException(
Expand All @@ -1656,10 +1678,17 @@ public static function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $h
ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2);

if (self::useNewSodiumAPI()) {
return (bool) sodium_crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash);
return (bool) sodium_crypto_pwhash_scryptsalsa208sha256_str_verify(
(string) $passwd,
(string) $hash
);
}
if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str_verify')) {
return (bool) call_user_func('\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str_verify', $passwd, $hash);
return (bool) call_user_func(
'\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str_verify',
(string) $passwd,
(string) $hash
);
}
// This is the best we can do.
throw new SodiumException(
Expand Down
52 changes: 32 additions & 20 deletions src/Core/BLAKE2b.php
Expand Up @@ -131,7 +131,10 @@ protected static function xor64(SplFixedArray $x, SplFixedArray $y)
if (!is_numeric($y[1])) {
throw new SodiumException('y[1] is not an integer');
}
return self::new64($x[0] ^ $y[0], $x[1] ^ $y[1]);
return self::new64(
(int) ($x[0] ^ $y[0]),
(int) ($x[1] ^ $y[1])
);
}

/**
Expand Down Expand Up @@ -163,31 +166,31 @@ public static function rotr64($x, $c)

if ($c < 32) {
/** @var int $h0 */
$h0 = ($x[0] << $c) | (
$h0 = ((int) ($x[0]) << $c) | (
(
$x[1] & ((1 << $c) - 1)
(int) ($x[1]) & ((1 << $c) - 1)
<<
(32 - $c)
) >> (32 - $c)
);
/** @var int $l0 */
$l0 = $x[1] << $c;
$l0 = (int) ($x[1]) << $c;
} else {
/** @var int $h0 */
$h0 = $x[1] << ($c - 32);
$h0 = (int) ($x[1]) << ($c - 32);
}

$h1 = 0;
$c1 = 64 - $c;

if ($c1 < 32) {
/** @var int $h1 */
$h1 = $x[0] >> $c1;
$h1 = (int) ($x[0]) >> $c1;
/** @var int $l1 */
$l1 = ($x[1] >> $c1) | ($x[0] & ((1 << $c1) - 1)) << (32 - $c1);
$l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1);
} else {
/** @var int $l1 */
$l1 = $x[0] >> ($c1 - 32);
$l1 = (int) ($x[0]) >> ($c1 - 32);
}

return self::new64($h0 | $h1, $l0 | $l1);
Expand Down Expand Up @@ -217,9 +220,15 @@ protected static function flatten64($x)
protected static function load64(SplFixedArray $x, $i)
{
/** @var int $l */
$l = $x[$i] | ($x[$i+1]<<8) | ($x[$i+2]<<16) | ($x[$i+3]<<24);
$l = (int) ($x[$i])
| ((int) ($x[$i+1]) << 8)
| ((int) ($x[$i+2]) << 16)
| ((int) ($x[$i+3]) << 24);
/** @var int $h */
$h = $x[$i+4] | ($x[$i+5]<<8) | ($x[$i+6]<<16) | ($x[$i+7]<<24);
$h = (int) ($x[$i+4])
| ((int) ($x[$i+5]) << 8)
| ((int) ($x[$i+6]) << 16)
| ((int) ($x[$i+7]) << 24);
return self::new64($h, $l);
}

Expand All @@ -243,7 +252,7 @@ protected static function store64(SplFixedArray $x, $i, SplFixedArray $u)
*/
/** @var int $uIdx */
$uIdx = ((7 - $j) & 4) >> 2;
$x[$i] = ($u[$uIdx] & 0xff);
$x[$i] = ((int) ($u[$uIdx]) & 0xff);
if (++$i > $maxLength) {
return;
}
Expand Down Expand Up @@ -622,7 +631,7 @@ public static function stringToSplFixedArray($str = '')
public static function SplFixedArrayToString(SplFixedArray $a)
{
/**
* @var array<mixed, int>
* @var array<int, int|string> $arr
*/
$arr = $a->toArray();
$c = $a->count();
Expand Down Expand Up @@ -668,16 +677,19 @@ public static function contextToString(SplFixedArray $ctx)
# uint8_t buf[2 * 128];
$str .= self::SplFixedArrayToString($ctx[3]);

/** @var int $ctx4 */
$ctx4 = (int) $ctx[4];

# size_t buflen;
$str .= implode('', array(
self::intToChr($ctx[4] & 0xff),
self::intToChr(($ctx[4] >> 8) & 0xff),
self::intToChr(($ctx[4] >> 16) & 0xff),
self::intToChr(($ctx[4] >> 24) & 0xff),
self::intToChr(($ctx[4] >> 32) & 0xff),
self::intToChr(($ctx[4] >> 40) & 0xff),
self::intToChr(($ctx[4] >> 48) & 0xff),
self::intToChr(($ctx[4] >> 56) & 0xff)
self::intToChr($ctx4 & 0xff),
self::intToChr(($ctx4 >> 8) & 0xff),
self::intToChr(($ctx4 >> 16) & 0xff),
self::intToChr(($ctx4 >> 24) & 0xff),
self::intToChr(($ctx4 >> 32) & 0xff),
self::intToChr(($ctx4 >> 40) & 0xff),
self::intToChr(($ctx4 >> 48) & 0xff),
self::intToChr(($ctx4 >> 56) & 0xff)
));
# uint8_t last_node;
return $str . "\x00";
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Curve25519.php
Expand Up @@ -88,7 +88,7 @@ public static function fe_cmov(
for ($i = 0; $i < 10; ++$i) {
/** @var int $x */
$x = (($f[$i] ^ $g[$i]) & $b);
$h[$i] = (int) ($f[$i] ^ $x);
$h[$i] = (int) ((int) ($f[$i]) ^ $x);
}
return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h);
}
Expand Down Expand Up @@ -1840,7 +1840,9 @@ public static function ge_double_scalarmult_vartime(

# slide(aslide,a);
# slide(bslide,b);
/** @var array<int, int> $aslide */
$aslide = self::slide($a);
/** @var array<int, int> $bslide */
$bslide = self::slide($b);

# ge_p3_to_cached(&Ai[0],A);
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Util.php
Expand Up @@ -549,7 +549,7 @@ public static function mul($a, $b, $size = 0)
*
* @var int
*/
$mask = -(($b >> $defaultSize) & 1);
$mask = -(($b >> ((int) $defaultSize)) & 1);

/**
* Ensure $b is a positive integer, without creating
Expand Down Expand Up @@ -912,7 +912,7 @@ protected static function isMbStringOverride()
if ($mbstring === null) {
$mbstring = extension_loaded('mbstring')
&&
(ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING);
((int) (ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING);
}
/** @var bool $mbstring */

Expand Down
31 changes: 20 additions & 11 deletions src/Core32/BLAKE2b.php
Expand Up @@ -138,9 +138,15 @@ public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c)
public static function load64($x, $i)
{
/** @var int $l */
$l = $x[$i] | ($x[$i+1]<<8) | ($x[$i+2]<<16) | ($x[$i+3]<<24);
$l = (int) ($x[$i])
| ((int) ($x[$i+1]) << 8)
| ((int) ($x[$i+2]) << 16)
| ((int) ($x[$i+3]) << 24);
/** @var int $h */
$h = $x[$i+4] | ($x[$i+5]<<8) | ($x[$i+6]<<16) | ($x[$i+7]<<24);
$h = (int) ($x[$i+4])
| ((int) ($x[$i+5]) << 8)
| ((int) ($x[$i+6]) << 16)
| ((int) ($x[$i+7]) << 24);
return self::new64($h, $l);
}

Expand Down Expand Up @@ -557,7 +563,7 @@ public static function stringToSplFixedArray($str = '')
public static function SplFixedArrayToString(SplFixedArray $a)
{
/**
* @var array<mixed, int>
* @var array<int, string|int>
*/
$arr = $a->toArray();
$c = $a->count();
Expand Down Expand Up @@ -609,16 +615,19 @@ public static function contextToString(SplFixedArray $ctx)
# uint8_t buf[2 * 128];
$str .= self::SplFixedArrayToString($ctx[3]);

/** @var int $ctx4 */
$ctx4 = $ctx[4];

# size_t buflen;
$str .= implode('', array(
self::intToChr($ctx[4] & 0xff),
self::intToChr(($ctx[4] >> 8) & 0xff),
self::intToChr(($ctx[4] >> 16) & 0xff),
self::intToChr(($ctx[4] >> 24) & 0xff),
self::intToChr(($ctx[4] >> 32) & 0xff),
self::intToChr(($ctx[4] >> 40) & 0xff),
self::intToChr(($ctx[4] >> 48) & 0xff),
self::intToChr(($ctx[4] >> 56) & 0xff)
self::intToChr($ctx4 & 0xff),
self::intToChr(($ctx4 >> 8) & 0xff),
self::intToChr(($ctx4 >> 16) & 0xff),
self::intToChr(($ctx4 >> 24) & 0xff),
self::intToChr(($ctx4 >> 32) & 0xff),
self::intToChr(($ctx4 >> 40) & 0xff),
self::intToChr(($ctx4 >> 48) & 0xff),
self::intToChr(($ctx4 >> 56) & 0xff)
));
# uint8_t last_node;
return $str . "\x00";
Expand Down
2 changes: 2 additions & 0 deletions src/Core32/Curve25519.php
Expand Up @@ -2116,7 +2116,9 @@ public static function ge_double_scalarmult_vartime(

# slide(aslide,a);
# slide(bslide,b);
/** @var array<int, int> $aslide */
$aslide = self::slide($a);
/** @var array<int, int> $bslide */
$bslide = self::slide($b);

# ge_p3_to_cached(&Ai[0],A);
Expand Down

0 comments on commit db9a78b

Please sign in to comment.