123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- 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($src)
- {
- return static::doDecode($src, false);
- }
- /**
- * Decode an uppercase Base32-encoded string into raw binary
- *
- * @param string $src
- * @return string
- */
- public static function decodeUpper($src)
- {
- return static::doDecode($src, true);
- }
- /**
- * Encode into Base32 (RFC 4648)
- *
- * @param string $src
- * @return string
- */
- public static function encode($src)
- {
- return static::doEncode($src, false);
- }
- /**
- * Encode into uppercase Base32 (RFC 4648)
- *
- * @param string $src
- * @return string
- */
- public static function encodeUpper($src)
- {
- 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($src)
- {
- $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($src)
- {
- $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($src)
- {
- $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($src)
- {
- $diff = 0x41;
- // if ($src > 25) $ret -= 40;
- $diff -= ((25 - $src) >> 8) & 41;
- return \pack('C', $src + $diff);
- }
- /**
- * Base32 decoding
- *
- * @param $src
- * @param bool $upper
- * @return string
- */
- protected static function doDecode($src, $upper = false)
- {
- // We do this to reduce code duplication:
- $method = $upper
- ? 'decode5BitsUpper'
- : 'decode5Bits';
- // Remove padding
- $srcLen = Binary::safeStrlen($src);
- if ($srcLen === 0) {
- return '';
- }
- 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'
- );
- }
- $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($src, $upper = false)
- {
- // 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;
- }
- }
|