extlib required by phpseclib (ParagonIE/ConstantTime)
This commit is contained in:
parent
5bfd9dbaa7
commit
d4216d09c6
396
extlib/ParagonIE/ConstantTime/Base32.php
Normal file
396
extlib/ParagonIE/ConstantTime/Base32.php
Normal file
|
@ -0,0 +1,396 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Base32
|
||||||
|
* [A-Z][2-7]
|
||||||
|
*
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class Base32 implements EncoderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Decode a Base32-encoded string into raw binary
|
||||||
|
*
|
||||||
|
* @param string $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function decode(string $src, bool $strictPadding = false): string
|
||||||
|
{
|
||||||
|
return static::doDecode($src, false, $strictPadding);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode an uppercase Base32-encoded string into raw binary
|
||||||
|
*
|
||||||
|
* @param string $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function decodeUpper(string $src, bool $strictPadding = false): string
|
||||||
|
{
|
||||||
|
return static::doDecode($src, true, $strictPadding);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode into Base32 (RFC 4648)
|
||||||
|
*
|
||||||
|
* @param string $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function encode(string $src): string
|
||||||
|
{
|
||||||
|
return static::doEncode($src, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode into uppercase Base32 (RFC 4648)
|
||||||
|
*
|
||||||
|
* @param string $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function encodeUpper(string $src): string
|
||||||
|
{
|
||||||
|
return static::doEncode($src, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 5-bit integers
|
||||||
|
* into 8-bit integers.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected static function decode5Bits(int $src): int
|
||||||
|
{
|
||||||
|
$ret = -1;
|
||||||
|
|
||||||
|
// if ($src > 96 && $src < 123) $ret += $src - 97 + 1; // -64
|
||||||
|
$ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 96);
|
||||||
|
|
||||||
|
// if ($src > 0x31 && $src < 0x38) $ret += $src - 24 + 1; // -23
|
||||||
|
$ret += (((0x31 - $src) & ($src - 0x38)) >> 8) & ($src - 23);
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 5-bit integers
|
||||||
|
* into 8-bit integers.
|
||||||
|
*
|
||||||
|
* Uppercase variant.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected static function decode5BitsUpper(int $src): int
|
||||||
|
{
|
||||||
|
$ret = -1;
|
||||||
|
|
||||||
|
// if ($src > 64 && $src < 91) $ret += $src - 65 + 1; // -64
|
||||||
|
$ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
|
||||||
|
|
||||||
|
// if ($src > 0x31 && $src < 0x38) $ret += $src - 24 + 1; // -23
|
||||||
|
$ret += (((0x31 - $src) & ($src - 0x38)) >> 8) & ($src - 23);
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 8-bit integers
|
||||||
|
* into 5-bit integers.
|
||||||
|
*
|
||||||
|
* @param $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function encode5Bits(int $src): string
|
||||||
|
{
|
||||||
|
$diff = 0x61;
|
||||||
|
|
||||||
|
// if ($src > 25) $ret -= 72;
|
||||||
|
$diff -= ((25 - $src) >> 8) & 73;
|
||||||
|
|
||||||
|
return \pack('C', $src + $diff);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 8-bit integers
|
||||||
|
* into 5-bit integers.
|
||||||
|
*
|
||||||
|
* Uppercase variant.
|
||||||
|
*
|
||||||
|
* @param $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function encode5BitsUpper(int $src): string
|
||||||
|
{
|
||||||
|
$diff = 0x41;
|
||||||
|
|
||||||
|
// if ($src > 25) $ret -= 40;
|
||||||
|
$diff -= ((25 - $src) >> 8) & 41;
|
||||||
|
|
||||||
|
return \pack('C', $src + $diff);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base32 decoding
|
||||||
|
*
|
||||||
|
* @param string $src
|
||||||
|
* @param bool $upper
|
||||||
|
* @param bool $strictPadding
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function doDecode(string $src, bool $upper = false, bool $strictPadding = false): string
|
||||||
|
{
|
||||||
|
// We do this to reduce code duplication:
|
||||||
|
$method = $upper
|
||||||
|
? 'decode5BitsUpper'
|
||||||
|
: 'decode5Bits';
|
||||||
|
|
||||||
|
// Remove padding
|
||||||
|
$srcLen = Binary::safeStrlen($src);
|
||||||
|
if ($srcLen === 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
if ($strictPadding) {
|
||||||
|
if (($srcLen & 7) === 0) {
|
||||||
|
for ($j = 0; $j < 7; ++$j) {
|
||||||
|
if ($src[$srcLen - 1] === '=') {
|
||||||
|
$srcLen--;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (($srcLen & 7) === 1) {
|
||||||
|
throw new \RangeException(
|
||||||
|
'Incorrect padding'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$src = \rtrim($src, '=');
|
||||||
|
$srcLen = Binary::safeStrlen($src);
|
||||||
|
}
|
||||||
|
|
||||||
|
$err = 0;
|
||||||
|
$dest = '';
|
||||||
|
// Main loop (no padding):
|
||||||
|
for ($i = 0; $i + 8 <= $srcLen; $i += 8) {
|
||||||
|
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 8));
|
||||||
|
$c0 = static::$method($chunk[1]);
|
||||||
|
$c1 = static::$method($chunk[2]);
|
||||||
|
$c2 = static::$method($chunk[3]);
|
||||||
|
$c3 = static::$method($chunk[4]);
|
||||||
|
$c4 = static::$method($chunk[5]);
|
||||||
|
$c5 = static::$method($chunk[6]);
|
||||||
|
$c6 = static::$method($chunk[7]);
|
||||||
|
$c7 = static::$method($chunk[8]);
|
||||||
|
|
||||||
|
$dest .= \pack(
|
||||||
|
'CCCCC',
|
||||||
|
(($c0 << 3) | ($c1 >> 2) ) & 0xff,
|
||||||
|
(($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff,
|
||||||
|
(($c3 << 4) | ($c4 >> 1) ) & 0xff,
|
||||||
|
(($c4 << 7) | ($c5 << 2) | ($c6 >> 3)) & 0xff,
|
||||||
|
(($c6 << 5) | ($c7 ) ) & 0xff
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6 | $c7) >> 8;
|
||||||
|
}
|
||||||
|
// The last chunk, which may have padding:
|
||||||
|
if ($i < $srcLen) {
|
||||||
|
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
|
||||||
|
$c0 = static::$method($chunk[1]);
|
||||||
|
|
||||||
|
if ($i + 6 < $srcLen) {
|
||||||
|
$c1 = static::$method($chunk[2]);
|
||||||
|
$c2 = static::$method($chunk[3]);
|
||||||
|
$c3 = static::$method($chunk[4]);
|
||||||
|
$c4 = static::$method($chunk[5]);
|
||||||
|
$c5 = static::$method($chunk[6]);
|
||||||
|
$c6 = static::$method($chunk[7]);
|
||||||
|
|
||||||
|
$dest .= \pack(
|
||||||
|
'CCCC',
|
||||||
|
(($c0 << 3) | ($c1 >> 2) ) & 0xff,
|
||||||
|
(($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff,
|
||||||
|
(($c3 << 4) | ($c4 >> 1) ) & 0xff,
|
||||||
|
(($c4 << 7) | ($c5 << 2) | ($c6 >> 3)) & 0xff
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6) >> 8;
|
||||||
|
} elseif ($i + 5 < $srcLen) {
|
||||||
|
$c1 = static::$method($chunk[2]);
|
||||||
|
$c2 = static::$method($chunk[3]);
|
||||||
|
$c3 = static::$method($chunk[4]);
|
||||||
|
$c4 = static::$method($chunk[5]);
|
||||||
|
$c5 = static::$method($chunk[6]);
|
||||||
|
|
||||||
|
$dest .= \pack(
|
||||||
|
'CCCC',
|
||||||
|
(($c0 << 3) | ($c1 >> 2) ) & 0xff,
|
||||||
|
(($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff,
|
||||||
|
(($c3 << 4) | ($c4 >> 1) ) & 0xff,
|
||||||
|
(($c4 << 7) | ($c5 << 2) ) & 0xff
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5) >> 8;
|
||||||
|
} elseif ($i + 4 < $srcLen) {
|
||||||
|
$c1 = static::$method($chunk[2]);
|
||||||
|
$c2 = static::$method($chunk[3]);
|
||||||
|
$c3 = static::$method($chunk[4]);
|
||||||
|
$c4 = static::$method($chunk[5]);
|
||||||
|
|
||||||
|
$dest .= \pack(
|
||||||
|
'CCC',
|
||||||
|
(($c0 << 3) | ($c1 >> 2) ) & 0xff,
|
||||||
|
(($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff,
|
||||||
|
(($c3 << 4) | ($c4 >> 1) ) & 0xff
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1 | $c2 | $c3 | $c4) >> 8;
|
||||||
|
} elseif ($i + 3 < $srcLen) {
|
||||||
|
$c1 = static::$method($chunk[2]);
|
||||||
|
$c2 = static::$method($chunk[3]);
|
||||||
|
$c3 = static::$method($chunk[4]);
|
||||||
|
|
||||||
|
$dest .= \pack(
|
||||||
|
'CC',
|
||||||
|
(($c0 << 3) | ($c1 >> 2) ) & 0xff,
|
||||||
|
(($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1 | $c2 | $c3) >> 8;
|
||||||
|
} elseif ($i + 2 < $srcLen) {
|
||||||
|
$c1 = static::$method($chunk[2]);
|
||||||
|
$c2 = static::$method($chunk[3]);
|
||||||
|
|
||||||
|
$dest .= \pack(
|
||||||
|
'CC',
|
||||||
|
(($c0 << 3) | ($c1 >> 2) ) & 0xff,
|
||||||
|
(($c1 << 6) | ($c2 << 1) ) & 0xff
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1 | $c2) >> 8;
|
||||||
|
} elseif ($i + 1 < $srcLen) {
|
||||||
|
$c1 = static::$method($chunk[2]);
|
||||||
|
|
||||||
|
$dest .= \pack(
|
||||||
|
'C',
|
||||||
|
(($c0 << 3) | ($c1 >> 2) ) & 0xff
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1) >> 8;
|
||||||
|
} else {
|
||||||
|
$dest .= \pack(
|
||||||
|
'C',
|
||||||
|
(($c0 << 3) ) & 0xff
|
||||||
|
);
|
||||||
|
$err |= ($c0) >> 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($err !== 0) {
|
||||||
|
throw new \RangeException(
|
||||||
|
'Base32::doDecode() only expects characters in the correct base32 alphabet'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base32 Decoding
|
||||||
|
*
|
||||||
|
* @param string $src
|
||||||
|
* @param bool $upper
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function doEncode(string $src, bool $upper = false): string
|
||||||
|
{
|
||||||
|
// We do this to reduce code duplication:
|
||||||
|
$method = $upper
|
||||||
|
? 'encode5BitsUpper'
|
||||||
|
: 'encode5Bits';
|
||||||
|
|
||||||
|
$dest = '';
|
||||||
|
$srcLen = Binary::safeStrlen($src);
|
||||||
|
|
||||||
|
// Main loop (no padding):
|
||||||
|
for ($i = 0; $i + 5 <= $srcLen; $i += 5) {
|
||||||
|
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 5));
|
||||||
|
$b0 = $chunk[1];
|
||||||
|
$b1 = $chunk[2];
|
||||||
|
$b2 = $chunk[3];
|
||||||
|
$b3 = $chunk[4];
|
||||||
|
$b4 = $chunk[5];
|
||||||
|
$dest .=
|
||||||
|
static::$method( ($b0 >> 3) & 31) .
|
||||||
|
static::$method((($b0 << 2) | ($b1 >> 6)) & 31) .
|
||||||
|
static::$method((($b1 >> 1) ) & 31) .
|
||||||
|
static::$method((($b1 << 4) | ($b2 >> 4)) & 31) .
|
||||||
|
static::$method((($b2 << 1) | ($b3 >> 7)) & 31) .
|
||||||
|
static::$method((($b3 >> 2) ) & 31) .
|
||||||
|
static::$method((($b3 << 3) | ($b4 >> 5)) & 31) .
|
||||||
|
static::$method( $b4 & 31);
|
||||||
|
}
|
||||||
|
// The last chunk, which may have padding:
|
||||||
|
if ($i < $srcLen) {
|
||||||
|
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
|
||||||
|
$b0 = $chunk[1];
|
||||||
|
if ($i + 3 < $srcLen) {
|
||||||
|
$b1 = $chunk[2];
|
||||||
|
$b2 = $chunk[3];
|
||||||
|
$b3 = $chunk[4];
|
||||||
|
$dest .=
|
||||||
|
static::$method( ($b0 >> 3) & 31) .
|
||||||
|
static::$method((($b0 << 2) | ($b1 >> 6)) & 31) .
|
||||||
|
static::$method((($b1 >> 1) ) & 31) .
|
||||||
|
static::$method((($b1 << 4) | ($b2 >> 4)) & 31) .
|
||||||
|
static::$method((($b2 << 1) | ($b3 >> 7)) & 31) .
|
||||||
|
static::$method((($b3 >> 2) ) & 31) .
|
||||||
|
static::$method((($b3 << 3) ) & 31) .
|
||||||
|
'=';
|
||||||
|
} elseif ($i + 2 < $srcLen) {
|
||||||
|
$b1 = $chunk[2];
|
||||||
|
$b2 = $chunk[3];
|
||||||
|
$dest .=
|
||||||
|
static::$method( ($b0 >> 3) & 31) .
|
||||||
|
static::$method((($b0 << 2) | ($b1 >> 6)) & 31) .
|
||||||
|
static::$method((($b1 >> 1) ) & 31) .
|
||||||
|
static::$method((($b1 << 4) | ($b2 >> 4)) & 31) .
|
||||||
|
static::$method((($b2 << 1) ) & 31) .
|
||||||
|
'===';
|
||||||
|
} elseif ($i + 1 < $srcLen) {
|
||||||
|
$b1 = $chunk[2];
|
||||||
|
$dest .=
|
||||||
|
static::$method( ($b0 >> 3) & 31) .
|
||||||
|
static::$method((($b0 << 2) | ($b1 >> 6)) & 31) .
|
||||||
|
static::$method((($b1 >> 1) ) & 31) .
|
||||||
|
static::$method((($b1 << 4) ) & 31) .
|
||||||
|
'====';
|
||||||
|
} else {
|
||||||
|
$dest .=
|
||||||
|
static::$method( ($b0 >> 3) & 31) .
|
||||||
|
static::$method( ($b0 << 2) & 31) .
|
||||||
|
'======';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $dest;
|
||||||
|
}
|
||||||
|
}
|
111
extlib/ParagonIE/ConstantTime/Base32Hex.php
Normal file
111
extlib/ParagonIE/ConstantTime/Base32Hex.php
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Base32Hex
|
||||||
|
* [0-9][A-V]
|
||||||
|
*
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class Base32Hex extends Base32
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 5-bit integers
|
||||||
|
* into 8-bit integers.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected static function decode5Bits(int $src): int
|
||||||
|
{
|
||||||
|
$ret = -1;
|
||||||
|
|
||||||
|
// if ($src > 0x30 && $src < 0x3a) ret += $src - 0x2e + 1; // -47
|
||||||
|
$ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src - 47);
|
||||||
|
|
||||||
|
// if ($src > 0x60 && $src < 0x77) ret += $src - 0x61 + 10 + 1; // -86
|
||||||
|
$ret += (((0x60 - $src) & ($src - 0x77)) >> 8) & ($src - 86);
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 5-bit integers
|
||||||
|
* into 8-bit integers.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected static function decode5BitsUpper(int $src): int
|
||||||
|
{
|
||||||
|
$ret = -1;
|
||||||
|
|
||||||
|
// if ($src > 0x30 && $src < 0x3a) ret += $src - 0x2e + 1; // -47
|
||||||
|
$ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src - 47);
|
||||||
|
|
||||||
|
// if ($src > 0x40 && $src < 0x57) ret += $src - 0x41 + 10 + 1; // -54
|
||||||
|
$ret += (((0x40 - $src) & ($src - 0x57)) >> 8) & ($src - 54);
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 8-bit integers
|
||||||
|
* into 5-bit integers.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function encode5Bits(int $src): string
|
||||||
|
{
|
||||||
|
$src += 0x30;
|
||||||
|
|
||||||
|
// if ($src > 0x39) $src += 0x61 - 0x3a; // 39
|
||||||
|
$src += ((0x39 - $src) >> 8) & 39;
|
||||||
|
|
||||||
|
return \pack('C', $src);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 8-bit integers
|
||||||
|
* into 5-bit integers.
|
||||||
|
*
|
||||||
|
* Uppercase variant.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function encode5BitsUpper(int $src): string
|
||||||
|
{
|
||||||
|
$src += 0x30;
|
||||||
|
|
||||||
|
// if ($src > 0x39) $src += 0x41 - 0x3a; // 7
|
||||||
|
$src += ((0x39 - $src) >> 8) & 7;
|
||||||
|
|
||||||
|
return \pack('C', $src);
|
||||||
|
}
|
||||||
|
}
|
229
extlib/ParagonIE/ConstantTime/Base64.php
Normal file
229
extlib/ParagonIE/ConstantTime/Base64.php
Normal file
|
@ -0,0 +1,229 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Base64
|
||||||
|
* [A-Z][a-z][0-9]+/
|
||||||
|
*
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class Base64 implements EncoderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Encode into Base64
|
||||||
|
*
|
||||||
|
* Base64 character set "[A-Z][a-z][0-9]+/"
|
||||||
|
*
|
||||||
|
* @param string $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function encode(string $src): string
|
||||||
|
{
|
||||||
|
$dest = '';
|
||||||
|
$srcLen = Binary::safeStrlen($src);
|
||||||
|
// Main loop (no padding):
|
||||||
|
for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
|
||||||
|
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 3));
|
||||||
|
$b0 = $chunk[1];
|
||||||
|
$b1 = $chunk[2];
|
||||||
|
$b2 = $chunk[3];
|
||||||
|
|
||||||
|
$dest .=
|
||||||
|
static::encode6Bits( $b0 >> 2 ) .
|
||||||
|
static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
|
||||||
|
static::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
|
||||||
|
static::encode6Bits( $b2 & 63);
|
||||||
|
}
|
||||||
|
// The last chunk, which may have padding:
|
||||||
|
if ($i < $srcLen) {
|
||||||
|
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
|
||||||
|
$b0 = $chunk[1];
|
||||||
|
if ($i + 1 < $srcLen) {
|
||||||
|
$b1 = $chunk[2];
|
||||||
|
$dest .=
|
||||||
|
static::encode6Bits( $b0 >> 2 ) .
|
||||||
|
static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
|
||||||
|
static::encode6Bits( ($b1 << 2) & 63) . '=';
|
||||||
|
} else {
|
||||||
|
$dest .=
|
||||||
|
static::encode6Bits( $b0 >> 2) .
|
||||||
|
static::encode6Bits(($b0 << 4) & 63) . '==';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* decode from base64 into binary
|
||||||
|
*
|
||||||
|
* Base64 character set "./[A-Z][a-z][0-9]"
|
||||||
|
*
|
||||||
|
* @param string $src
|
||||||
|
* @param bool $strictPadding
|
||||||
|
* @return string|bool
|
||||||
|
* @throws \RangeException
|
||||||
|
*/
|
||||||
|
public static function decode(string $src, bool $strictPadding = false): string
|
||||||
|
{
|
||||||
|
// Remove padding
|
||||||
|
$srcLen = Binary::safeStrlen($src);
|
||||||
|
if ($srcLen === 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($strictPadding) {
|
||||||
|
if (($srcLen & 3) === 0) {
|
||||||
|
if ($src[$srcLen - 1] === '=') {
|
||||||
|
$srcLen--;
|
||||||
|
if ($src[$srcLen - 1] === '=') {
|
||||||
|
$srcLen--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (($srcLen & 3) === 1) {
|
||||||
|
throw new \RangeException(
|
||||||
|
'Incorrect padding'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ($src[$srcLen - 1] === '=') {
|
||||||
|
throw new \RangeException(
|
||||||
|
'Incorrect padding'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$src = \rtrim($src, '=');
|
||||||
|
$srcLen = Binary::safeStrlen($src);
|
||||||
|
}
|
||||||
|
|
||||||
|
$err = 0;
|
||||||
|
$dest = '';
|
||||||
|
// Main loop (no padding):
|
||||||
|
for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
|
||||||
|
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 4));
|
||||||
|
$c0 = static::decode6Bits($chunk[1]);
|
||||||
|
$c1 = static::decode6Bits($chunk[2]);
|
||||||
|
$c2 = static::decode6Bits($chunk[3]);
|
||||||
|
$c3 = static::decode6Bits($chunk[4]);
|
||||||
|
|
||||||
|
$dest .= \pack(
|
||||||
|
'CCC',
|
||||||
|
((($c0 << 2) | ($c1 >> 4)) & 0xff),
|
||||||
|
((($c1 << 4) | ($c2 >> 2)) & 0xff),
|
||||||
|
((($c2 << 6) | $c3 ) & 0xff)
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1 | $c2 | $c3) >> 8;
|
||||||
|
}
|
||||||
|
// The last chunk, which may have padding:
|
||||||
|
if ($i < $srcLen) {
|
||||||
|
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
|
||||||
|
$c0 = static::decode6Bits($chunk[1]);
|
||||||
|
|
||||||
|
if ($i + 2 < $srcLen) {
|
||||||
|
$c1 = static::decode6Bits($chunk[2]);
|
||||||
|
$c2 = static::decode6Bits($chunk[3]);
|
||||||
|
$dest .= \pack(
|
||||||
|
'CC',
|
||||||
|
((($c0 << 2) | ($c1 >> 4)) & 0xff),
|
||||||
|
((($c1 << 4) | ($c2 >> 2)) & 0xff)
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1 | $c2) >> 8;
|
||||||
|
} elseif ($i + 1 < $srcLen) {
|
||||||
|
$c1 = static::decode6Bits($chunk[2]);
|
||||||
|
$dest .= \pack(
|
||||||
|
'C',
|
||||||
|
((($c0 << 2) | ($c1 >> 4)) & 0xff)
|
||||||
|
);
|
||||||
|
$err |= ($c0 | $c1) >> 8;
|
||||||
|
} elseif ($i < $srcLen && $strictPadding) {
|
||||||
|
$err |= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($err !== 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 6-bit integers
|
||||||
|
* into 8-bit integers.
|
||||||
|
*
|
||||||
|
* Base64 character set:
|
||||||
|
* [A-Z] [a-z] [0-9] + /
|
||||||
|
* 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected static function decode6Bits(int $src): int
|
||||||
|
{
|
||||||
|
$ret = -1;
|
||||||
|
|
||||||
|
// if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
|
||||||
|
$ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
|
||||||
|
|
||||||
|
// if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
|
||||||
|
$ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
|
||||||
|
|
||||||
|
// if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
|
||||||
|
$ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
|
||||||
|
|
||||||
|
// if ($src == 0x2b) $ret += 62 + 1;
|
||||||
|
$ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63;
|
||||||
|
|
||||||
|
// if ($src == 0x2f) ret += 63 + 1;
|
||||||
|
$ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64;
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 8-bit integers
|
||||||
|
* into 6-bit integers.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function encode6Bits(int $src): string
|
||||||
|
{
|
||||||
|
$diff = 0x41;
|
||||||
|
|
||||||
|
// if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
|
||||||
|
$diff += ((25 - $src) >> 8) & 6;
|
||||||
|
|
||||||
|
// if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
|
||||||
|
$diff -= ((51 - $src) >> 8) & 75;
|
||||||
|
|
||||||
|
// if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15
|
||||||
|
$diff -= ((61 - $src) >> 8) & 15;
|
||||||
|
|
||||||
|
// if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3
|
||||||
|
$diff += ((62 - $src) >> 8) & 3;
|
||||||
|
|
||||||
|
return \pack('C', $src + $diff);
|
||||||
|
}
|
||||||
|
}
|
88
extlib/ParagonIE/ConstantTime/Base64DotSlash.php
Normal file
88
extlib/ParagonIE/ConstantTime/Base64DotSlash.php
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Base64DotSlash
|
||||||
|
* ./[A-Z][a-z][0-9]
|
||||||
|
*
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class Base64DotSlash extends Base64
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 6-bit integers
|
||||||
|
* into 8-bit integers.
|
||||||
|
*
|
||||||
|
* Base64 character set:
|
||||||
|
* ./ [A-Z] [a-z] [0-9]
|
||||||
|
* 0x2e-0x2f, 0x41-0x5a, 0x61-0x7a, 0x30-0x39
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected static function decode6Bits(int $src): int
|
||||||
|
{
|
||||||
|
$ret = -1;
|
||||||
|
|
||||||
|
// if ($src > 0x2d && $src < 0x30) ret += $src - 0x2e + 1; // -45
|
||||||
|
$ret += (((0x2d - $src) & ($src - 0x30)) >> 8) & ($src - 45);
|
||||||
|
|
||||||
|
// if ($src > 0x40 && $src < 0x5b) ret += $src - 0x41 + 2 + 1; // -62
|
||||||
|
$ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 62);
|
||||||
|
|
||||||
|
// if ($src > 0x60 && $src < 0x7b) ret += $src - 0x61 + 28 + 1; // -68
|
||||||
|
$ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 68);
|
||||||
|
|
||||||
|
// if ($src > 0x2f && $src < 0x3a) ret += $src - 0x30 + 54 + 1; // 7
|
||||||
|
$ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 7);
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 8-bit integers
|
||||||
|
* into 6-bit integers.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function encode6Bits(int $src): string
|
||||||
|
{
|
||||||
|
$src += 0x2e;
|
||||||
|
|
||||||
|
// if ($src > 0x2f) $src += 0x41 - 0x30; // 17
|
||||||
|
$src += ((0x2f - $src) >> 8) & 17;
|
||||||
|
|
||||||
|
// if ($src > 0x5a) $src += 0x61 - 0x5b; // 6
|
||||||
|
$src += ((0x5a - $src) >> 8) & 6;
|
||||||
|
|
||||||
|
// if ($src > 0x7a) $src += 0x30 - 0x7b; // -75
|
||||||
|
$src -= ((0x7a - $src) >> 8) & 75;
|
||||||
|
|
||||||
|
return \pack('C', $src);
|
||||||
|
}
|
||||||
|
}
|
82
extlib/ParagonIE/ConstantTime/Base64DotSlashOrdered.php
Normal file
82
extlib/ParagonIE/ConstantTime/Base64DotSlashOrdered.php
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Base64DotSlashOrdered
|
||||||
|
* ./[0-9][A-Z][a-z]
|
||||||
|
*
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class Base64DotSlashOrdered extends Base64
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 6-bit integers
|
||||||
|
* into 8-bit integers.
|
||||||
|
*
|
||||||
|
* Base64 character set:
|
||||||
|
* [.-9] [A-Z] [a-z]
|
||||||
|
* 0x2e-0x39, 0x41-0x5a, 0x61-0x7a
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected static function decode6Bits(int $src): int
|
||||||
|
{
|
||||||
|
$ret = -1;
|
||||||
|
|
||||||
|
// if ($src > 0x2d && $src < 0x3a) ret += $src - 0x2e + 1; // -45
|
||||||
|
$ret += (((0x2d - $src) & ($src - 0x3a)) >> 8) & ($src - 45);
|
||||||
|
|
||||||
|
// if ($src > 0x40 && $src < 0x5b) ret += $src - 0x41 + 12 + 1; // -52
|
||||||
|
$ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 52);
|
||||||
|
|
||||||
|
// if ($src > 0x60 && $src < 0x7b) ret += $src - 0x61 + 38 + 1; // -58
|
||||||
|
$ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 58);
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 8-bit integers
|
||||||
|
* into 6-bit integers.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function encode6Bits(int $src): string
|
||||||
|
{
|
||||||
|
$src += 0x2e;
|
||||||
|
|
||||||
|
// if ($src > 0x39) $src += 0x41 - 0x3a; // 7
|
||||||
|
$src += ((0x39 - $src) >> 8) & 7;
|
||||||
|
|
||||||
|
// if ($src > 0x5a) $src += 0x61 - 0x5b; // 6
|
||||||
|
$src += ((0x5a - $src) >> 8) & 6;
|
||||||
|
|
||||||
|
return \pack('C', $src);
|
||||||
|
}
|
||||||
|
}
|
95
extlib/ParagonIE/ConstantTime/Base64UrlSafe.php
Normal file
95
extlib/ParagonIE/ConstantTime/Base64UrlSafe.php
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Base64DotSlash
|
||||||
|
* ./[A-Z][a-z][0-9]
|
||||||
|
*
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class Base64UrlSafe extends Base64
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 6-bit integers
|
||||||
|
* into 8-bit integers.
|
||||||
|
*
|
||||||
|
* Base64 character set:
|
||||||
|
* [A-Z] [a-z] [0-9] - _
|
||||||
|
* 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2d, 0x5f
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected static function decode6Bits(int $src): int
|
||||||
|
{
|
||||||
|
$ret = -1;
|
||||||
|
|
||||||
|
// if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
|
||||||
|
$ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
|
||||||
|
|
||||||
|
// if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
|
||||||
|
$ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
|
||||||
|
|
||||||
|
// if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
|
||||||
|
$ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
|
||||||
|
|
||||||
|
// if ($src == 0x2c) $ret += 62 + 1;
|
||||||
|
$ret += (((0x2c - $src) & ($src - 0x2e)) >> 8) & 63;
|
||||||
|
|
||||||
|
// if ($src == 0x5f) ret += 63 + 1;
|
||||||
|
$ret += (((0x5e - $src) & ($src - 0x60)) >> 8) & 64;
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses bitwise operators instead of table-lookups to turn 8-bit integers
|
||||||
|
* into 6-bit integers.
|
||||||
|
*
|
||||||
|
* @param int $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function encode6Bits(int $src): string
|
||||||
|
{
|
||||||
|
$diff = 0x41;
|
||||||
|
|
||||||
|
// if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
|
||||||
|
$diff += ((25 - $src) >> 8) & 6;
|
||||||
|
|
||||||
|
// if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
|
||||||
|
$diff -= ((51 - $src) >> 8) & 75;
|
||||||
|
|
||||||
|
// if ($src > 61) $diff += 0x2d - 0x30 - 10; // -13
|
||||||
|
$diff -= ((61 - $src) >> 8) & 13;
|
||||||
|
|
||||||
|
// if ($src > 62) $diff += 0x5f - 0x2b - 1; // 3
|
||||||
|
$diff += ((62 - $src) >> 8) & 49;
|
||||||
|
|
||||||
|
return \pack('C', $src + $diff);
|
||||||
|
}
|
||||||
|
}
|
98
extlib/ParagonIE/ConstantTime/Binary.php
Normal file
98
extlib/ParagonIE/ConstantTime/Binary.php
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Binary
|
||||||
|
*
|
||||||
|
* Binary string operators that don't choke on
|
||||||
|
* mbstring.func_overload
|
||||||
|
*
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class Binary
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Safe string length
|
||||||
|
*
|
||||||
|
* @ref mbstring.func_overload
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public static function safeStrlen(string $str): int
|
||||||
|
{
|
||||||
|
if (\function_exists('mb_strlen')) {
|
||||||
|
return \mb_strlen($str, '8bit');
|
||||||
|
} else {
|
||||||
|
return \strlen($str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safe substring
|
||||||
|
*
|
||||||
|
* @ref mbstring.func_overload
|
||||||
|
*
|
||||||
|
* @staticvar boolean $exists
|
||||||
|
* @param string $str
|
||||||
|
* @param int $start
|
||||||
|
* @param int $length
|
||||||
|
* @return string
|
||||||
|
* @throws \TypeError
|
||||||
|
*/
|
||||||
|
public static function safeSubstr(
|
||||||
|
string $str,
|
||||||
|
int $start = 0,
|
||||||
|
$length = null
|
||||||
|
): string {
|
||||||
|
if (\function_exists('mb_substr')) {
|
||||||
|
// mb_substr($str, 0, NULL, '8bit') returns an empty string on PHP
|
||||||
|
// 5.3, so we have to find the length ourselves.
|
||||||
|
if ($length === null) {
|
||||||
|
if ($start >= 0) {
|
||||||
|
$length = self::safeStrlen($str) - $start;
|
||||||
|
} else {
|
||||||
|
$length = -$start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// $length calculation above might result in a 0-length string
|
||||||
|
if ($length === 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return \mb_substr($str, $start, $length, '8bit');
|
||||||
|
}
|
||||||
|
if ($length === 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
// Unlike mb_substr(), substr() doesn't accept NULL for length
|
||||||
|
if ($length !== null) {
|
||||||
|
return \substr($str, $start, $length);
|
||||||
|
} else {
|
||||||
|
return \substr($str, $start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
extlib/ParagonIE/ConstantTime/EncoderInterface.php
Normal file
52
extlib/ParagonIE/ConstantTime/EncoderInterface.php
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface EncoderInterface
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
interface EncoderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Convert a binary string into a hexadecimal string without cache-timing
|
||||||
|
* leaks
|
||||||
|
*
|
||||||
|
* @param string $binString (raw binary)
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function encode(string $binString): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a binary string into a hexadecimal string without cache-timing
|
||||||
|
* leaks
|
||||||
|
*
|
||||||
|
* @param string $encodedString
|
||||||
|
* @param bool $strictPadding Error on invalid padding
|
||||||
|
* @return string (raw binary)
|
||||||
|
*/
|
||||||
|
public static function decode(string $encodedString, bool $strictPadding = false): string;
|
||||||
|
}
|
245
extlib/ParagonIE/ConstantTime/Encoding.php
Normal file
245
extlib/ParagonIE/ConstantTime/Encoding.php
Normal file
|
@ -0,0 +1,245 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Encoding
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class Encoding
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 encoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base32Encode(string $str): string
|
||||||
|
{
|
||||||
|
return Base32::encode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 encoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base32EncodeUpper(string $str): string
|
||||||
|
{
|
||||||
|
return Base32::encodeUpper($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 decoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base32Decode(string $str): string
|
||||||
|
{
|
||||||
|
return Base32::decode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 decoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base32DecodeUpper(string $str): string
|
||||||
|
{
|
||||||
|
return Base32::decodeUpper($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 encoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base32HexEncode(string $str): string
|
||||||
|
{
|
||||||
|
return Base32Hex::encode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 encoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base32HexEncodeUpper(string $str): string
|
||||||
|
{
|
||||||
|
return Base32Hex::encodeUpper($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 decoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base32HexDecode(string $str): string
|
||||||
|
{
|
||||||
|
return Base32Hex::decode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 decoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base32HexDecodeUpper(string $str): string
|
||||||
|
{
|
||||||
|
return Base32Hex::decodeUpper($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base64 encoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base64Encode(string $str): string
|
||||||
|
{
|
||||||
|
return Base64::encode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 decoding
|
||||||
|
*
|
||||||
|
* @param $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base64Decode(string $str): string
|
||||||
|
{
|
||||||
|
return Base64::decode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode into Base64
|
||||||
|
*
|
||||||
|
* Base64 character set "./[A-Z][a-z][0-9]"
|
||||||
|
* @param $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base64EncodeDotSlash(string $str): string
|
||||||
|
{
|
||||||
|
return Base64DotSlash::encode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode from base64 to raw binary
|
||||||
|
*
|
||||||
|
* Base64 character set "./[A-Z][a-z][0-9]"
|
||||||
|
*
|
||||||
|
* @param $src
|
||||||
|
* @return bool|string
|
||||||
|
* @throws \RangeException
|
||||||
|
*/
|
||||||
|
public static function base64DecodeDotSlash(string $str): string
|
||||||
|
{
|
||||||
|
return Base64DotSlash::decode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode into Base64
|
||||||
|
*
|
||||||
|
* Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
|
||||||
|
* @param $src
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function base64EncodeDotSlashOrdered(string $str): string
|
||||||
|
{
|
||||||
|
return Base64DotSlashOrdered::encode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode from base64 to raw binary
|
||||||
|
*
|
||||||
|
* Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
|
||||||
|
*
|
||||||
|
* @param $src
|
||||||
|
* @return bool|string
|
||||||
|
* @throws \RangeException
|
||||||
|
*/
|
||||||
|
public static function base64DecodeDotSlashOrdered(string $str): string
|
||||||
|
{
|
||||||
|
return Base64DotSlashOrdered::decode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a binary string into a hexadecimal string without cache-timing
|
||||||
|
* leaks
|
||||||
|
*
|
||||||
|
* @param string $bin_string (raw binary)
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function hexEncode(string $bin_string): string
|
||||||
|
{
|
||||||
|
return Hex::encode($bin_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a hexadecimal string into a binary string without cache-timing
|
||||||
|
* leaks
|
||||||
|
*
|
||||||
|
* @param string $hex_string
|
||||||
|
* @return string (raw binary)
|
||||||
|
* @throws \RangeException
|
||||||
|
*/
|
||||||
|
public static function hexDecode(string $hex_string): string
|
||||||
|
{
|
||||||
|
return Hex::decode($hex_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a binary string into a hexadecimal string without cache-timing
|
||||||
|
* leaks
|
||||||
|
*
|
||||||
|
* @param string $bin_string (raw binary)
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function hexEncodeUpper(string $bin_string): string
|
||||||
|
{
|
||||||
|
return Hex::encodeUpper($bin_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a binary string into a hexadecimal string without cache-timing
|
||||||
|
* leaks
|
||||||
|
*
|
||||||
|
* @param string $bin_string (raw binary)
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function hexDecodeUpper(string $bin_string): string
|
||||||
|
{
|
||||||
|
return Hex::decode($bin_string);
|
||||||
|
}
|
||||||
|
}
|
132
extlib/ParagonIE/ConstantTime/Hex.php
Normal file
132
extlib/ParagonIE/ConstantTime/Hex.php
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Hex
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class Hex implements EncoderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Convert a binary string into a hexadecimal string without cache-timing
|
||||||
|
* leaks
|
||||||
|
*
|
||||||
|
* @param string $bin_string (raw binary)
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function encode(string $bin_string): string
|
||||||
|
{
|
||||||
|
$hex = '';
|
||||||
|
$len = Binary::safeStrlen($bin_string);
|
||||||
|
for ($i = 0; $i < $len; ++$i) {
|
||||||
|
$chunk = \unpack('C', Binary::safeSubstr($bin_string, $i, 2));
|
||||||
|
$c = $chunk[1] & 0xf;
|
||||||
|
$b = $chunk[1] >> 4;
|
||||||
|
$hex .= pack(
|
||||||
|
'CC',
|
||||||
|
(87 + $b + ((($b - 10) >> 8) & ~38)),
|
||||||
|
(87 + $c + ((($c - 10) >> 8) & ~38))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a binary string into a hexadecimal string without cache-timing
|
||||||
|
* leaks, returning uppercase letters (as per RFC 4648)
|
||||||
|
*
|
||||||
|
* @param string $bin_string (raw binary)
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function encodeUpper(string $bin_string): string
|
||||||
|
{
|
||||||
|
$hex = '';
|
||||||
|
$len = Binary::safeStrlen($bin_string);
|
||||||
|
for ($i = 0; $i < $len; ++$i) {
|
||||||
|
$chunk = \unpack('C', Binary::safeSubstr($bin_string, $i, 2));
|
||||||
|
$c = $chunk[1] & 0xf;
|
||||||
|
$b = $chunk[1] >> 4;
|
||||||
|
$hex .= pack(
|
||||||
|
'CC',
|
||||||
|
(55 + $b + ((($b - 10) >> 8) & ~6)),
|
||||||
|
(55 + $c + ((($c - 10) >> 8) & ~6))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a hexadecimal string into a binary string without cache-timing
|
||||||
|
* leaks
|
||||||
|
*
|
||||||
|
* @param string $hex_string
|
||||||
|
* @param bool $strictPadding
|
||||||
|
* @return string (raw binary)
|
||||||
|
* @throws \RangeException
|
||||||
|
*/
|
||||||
|
public static function decode(string $hexString, bool $strictPadding = false): string
|
||||||
|
{
|
||||||
|
$hex_pos = 0;
|
||||||
|
$bin = '';
|
||||||
|
$c_acc = 0;
|
||||||
|
$hex_len = Binary::safeStrlen($hexString);
|
||||||
|
$state = 0;
|
||||||
|
if (($hex_len & 1) !== 0) {
|
||||||
|
if ($strictPadding) {
|
||||||
|
throw new \RangeException(
|
||||||
|
'Expected an even number of hexadecimal characters'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$hexString = '0' . $hexString;
|
||||||
|
++$hex_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$chunk = \unpack('C*', $hexString);
|
||||||
|
while ($hex_pos < $hex_len) {
|
||||||
|
++$hex_pos;
|
||||||
|
$c = $chunk[$hex_pos];
|
||||||
|
$c_num = $c ^ 48;
|
||||||
|
$c_num0 = ($c_num - 10) >> 8;
|
||||||
|
$c_alpha = ($c & ~32) - 55;
|
||||||
|
$c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;
|
||||||
|
if (($c_num0 | $c_alpha0) === 0) {
|
||||||
|
throw new \RangeException(
|
||||||
|
'hexEncode() only expects hexadecimal characters'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0);
|
||||||
|
if ($state === 0) {
|
||||||
|
$c_acc = $c_val * 16;
|
||||||
|
} else {
|
||||||
|
$bin .= \pack('C', $c_acc | $c_val);
|
||||||
|
}
|
||||||
|
$state ^= 1;
|
||||||
|
}
|
||||||
|
return $bin;
|
||||||
|
}
|
||||||
|
}
|
166
extlib/ParagonIE/ConstantTime/RFC4648.php
Normal file
166
extlib/ParagonIE/ConstantTime/RFC4648.php
Normal file
|
@ -0,0 +1,166 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace ParagonIE\ConstantTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2016 Paragon Initiative Enterprises.
|
||||||
|
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class RFC4648
|
||||||
|
*
|
||||||
|
* This class conforms strictly to the RFC
|
||||||
|
*
|
||||||
|
* @package ParagonIE\ConstantTime
|
||||||
|
*/
|
||||||
|
abstract class RFC4648
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base64 encoding
|
||||||
|
*
|
||||||
|
* "foo" -> "Zm9v"
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base64Encode(string $str): string
|
||||||
|
{
|
||||||
|
return Base64::encode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base64 decoding
|
||||||
|
*
|
||||||
|
* "Zm9v" -> "foo"
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base64Decode(string $str): string
|
||||||
|
{
|
||||||
|
return Base64::decode($str, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base64 (URL Safe) encoding
|
||||||
|
*
|
||||||
|
* "foo" -> "Zm9v"
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base64UrlSafeEncode(string $str): string
|
||||||
|
{
|
||||||
|
return Base64UrlSafe::encode($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base64 (URL Safe) decoding
|
||||||
|
*
|
||||||
|
* "Zm9v" -> "foo"
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base64UrlSafeDecode(string $str): string
|
||||||
|
{
|
||||||
|
return Base64UrlSafe::decode($str, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 encoding
|
||||||
|
*
|
||||||
|
* "foo" -> "MZXW6==="
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base32Encode(string $str): string
|
||||||
|
{
|
||||||
|
return Base32::encodeUpper($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32 encoding
|
||||||
|
*
|
||||||
|
* "MZXW6===" -> "foo"
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base32Decode(string $str): string
|
||||||
|
{
|
||||||
|
return Base32::decodeUpper($str, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32-Hex encoding
|
||||||
|
*
|
||||||
|
* "foo" -> "CPNMU==="
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base32HexEncode(string $str): string
|
||||||
|
{
|
||||||
|
return Base32::encodeUpper($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base32-Hex decoding
|
||||||
|
*
|
||||||
|
* "CPNMU===" -> "foo"
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base32HexDecode(string $str): string
|
||||||
|
{
|
||||||
|
return Base32::decodeUpper($str, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base16 decoding
|
||||||
|
*
|
||||||
|
* "foo" -> "666F6F"
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base16Encode(string $str): string
|
||||||
|
{
|
||||||
|
return Hex::encodeUpper($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RFC 4648 Base16 decoding
|
||||||
|
*
|
||||||
|
* "666F6F" -> "foo"
|
||||||
|
*
|
||||||
|
* @param string $str
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function base16Decode(string $str): string
|
||||||
|
{
|
||||||
|
return Hex::decode($str, true);
|
||||||
|
}
|
||||||
|
}
|
48
extlib/ParagonIE/LICENSE.txt
Normal file
48
extlib/ParagonIE/LICENSE.txt
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paragon Initiative Enterprises
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
This library was based on the work of Steve "Sc00bz" Thomas.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Steve Thomas
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user