Skip to content

Commit

Permalink
Boyscouting
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jun 14, 2022
1 parent 817e88a commit 58c3f47
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 47 deletions.
24 changes: 15 additions & 9 deletions src/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use RangeException;
use TypeError;

/**
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
Expand Down Expand Up @@ -65,7 +66,7 @@ public static function decodeUpper(string $src, bool $strictPadding = false): st
*
* @param string $binString
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function encode(string $binString): string
{
Expand All @@ -76,7 +77,7 @@ public static function encode(string $binString): string
*
* @param string $src
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function encodeUnpadded(string $src): string
{
Expand All @@ -88,7 +89,7 @@ public static function encodeUnpadded(string $src): string
*
* @param string $src
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function encodeUpper(string $src): string
{
Expand All @@ -100,7 +101,7 @@ public static function encodeUpper(string $src): string
*
* @param string $src
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function encodeUpperUnpadded(string $src): string
{
Expand Down Expand Up @@ -187,6 +188,7 @@ protected static function encode5BitsUpper(int $src): string

/**
* @param string $encodedString
* @param bool $upper
* @return string
*/
public static function decodeNoPadding(string $encodedString, bool $upper = false): string
Expand Down Expand Up @@ -218,11 +220,15 @@ public static function decodeNoPadding(string $encodedString, bool $upper = fals
* @param bool $upper
* @param bool $strictPadding
* @return string
* @throws \TypeError
*
* @throws TypeError
* @psalm-suppress RedundantCondition
*/
protected static function doDecode(string $src, bool $upper = false, bool $strictPadding = false): string
{
protected static function doDecode(
string $src,
bool $upper = false,
bool $strictPadding = false
): string {
// We do this to reduce code duplication:
$method = $upper
? 'decode5BitsUpper'
Expand All @@ -244,7 +250,7 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
}
}
if (($srcLen & 7) === 1) {
throw new \RangeException(
throw new RangeException(
'Incorrect padding'
);
}
Expand Down Expand Up @@ -426,7 +432,7 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
* @param bool $upper
* @param bool $pad
* @return string
* @throws \TypeError
* @throws TypeError
*/
protected static function doEncode(string $src, bool $upper = false, $pad = true): string
{
Expand Down
15 changes: 10 additions & 5 deletions src/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use RangeException;
use TypeError;

/**
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
Expand Down Expand Up @@ -43,7 +44,8 @@ abstract class Base64 implements EncoderInterface
*
* @param string $binString
* @return string
* @throws \TypeError
*
* @throws TypeError
*/
public static function encode(string $binString): string
{
Expand All @@ -57,7 +59,8 @@ public static function encode(string $binString): string
*
* @param string $src
* @return string
* @throws \TypeError
*
* @throws TypeError
*/
public static function encodeUnpadded(string $src): string
{
Expand All @@ -68,7 +71,8 @@ public static function encodeUnpadded(string $src): string
* @param string $src
* @param bool $pad Include = padding?
* @return string
* @throws \TypeError
*
* @throws TypeError
*/
protected static function doEncode(string $src, bool $pad = true): string
{
Expand Down Expand Up @@ -122,8 +126,9 @@ protected static function doEncode(string $src, bool $pad = true): string
* @param string $encodedString
* @param bool $strictPadding
* @return string
* @throws \RangeException
* @throws \TypeError
*
* @throws RangeException
* @throws TypeError
* @psalm-suppress RedundantCondition
*/
public static function decode(string $encodedString, bool $strictPadding = false): string
Expand Down
5 changes: 4 additions & 1 deletion src/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
declare(strict_types=1);
namespace ParagonIE\ConstantTime;

use TypeError;

/**
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
Expand Down Expand Up @@ -64,7 +66,8 @@ public static function safeStrlen(string $str): int
* @param int $start
* @param ?int $length
* @return string
* @throws \TypeError
*
* @throws TypeError
*/
public static function safeSubstr(
string $str,
Expand Down
34 changes: 18 additions & 16 deletions src/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
declare(strict_types=1);
namespace ParagonIE\ConstantTime;

use TypeError;

/**
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
Expand Down Expand Up @@ -36,7 +38,7 @@ abstract class Encoding
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base32Encode(string $str): string
{
Expand All @@ -48,7 +50,7 @@ public static function base32Encode(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base32EncodeUpper(string $str): string
{
Expand All @@ -60,7 +62,7 @@ public static function base32EncodeUpper(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base32Decode(string $str): string
{
Expand All @@ -72,7 +74,7 @@ public static function base32Decode(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base32DecodeUpper(string $str): string
{
Expand All @@ -84,7 +86,7 @@ public static function base32DecodeUpper(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base32HexEncode(string $str): string
{
Expand All @@ -96,7 +98,7 @@ public static function base32HexEncode(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base32HexEncodeUpper(string $str): string
{
Expand All @@ -108,7 +110,7 @@ public static function base32HexEncodeUpper(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base32HexDecode(string $str): string
{
Expand All @@ -120,7 +122,7 @@ public static function base32HexDecode(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base32HexDecodeUpper(string $str): string
{
Expand All @@ -132,7 +134,7 @@ public static function base32HexDecodeUpper(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base64Encode(string $str): string
{
Expand All @@ -144,7 +146,7 @@ public static function base64Encode(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base64Decode(string $str): string
{
Expand All @@ -157,7 +159,7 @@ public static function base64Decode(string $str): string
* Base64 character set "./[A-Z][a-z][0-9]"
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base64EncodeDotSlash(string $str): string
{
Expand All @@ -172,7 +174,7 @@ public static function base64EncodeDotSlash(string $str): string
* @param string $str
* @return string
* @throws \RangeException
* @throws \TypeError
* @throws TypeError
*/
public static function base64DecodeDotSlash(string $str): string
{
Expand All @@ -185,7 +187,7 @@ public static function base64DecodeDotSlash(string $str): string
* Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
* @param string $str
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function base64EncodeDotSlashOrdered(string $str): string
{
Expand All @@ -200,7 +202,7 @@ public static function base64EncodeDotSlashOrdered(string $str): string
* @param string $str
* @return string
* @throws \RangeException
* @throws \TypeError
* @throws TypeError
*/
public static function base64DecodeDotSlashOrdered(string $str): string
{
Expand All @@ -213,7 +215,7 @@ public static function base64DecodeDotSlashOrdered(string $str): string
*
* @param string $bin_string (raw binary)
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function hexEncode(string $bin_string): string
{
Expand All @@ -239,7 +241,7 @@ public static function hexDecode(string $hex_string): string
*
* @param string $bin_string (raw binary)
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function hexEncodeUpper(string $bin_string): string
{
Expand Down
19 changes: 12 additions & 7 deletions src/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
declare(strict_types=1);
namespace ParagonIE\ConstantTime;

use RangeException;
use TypeError;

/**
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
Expand Down Expand Up @@ -37,7 +40,7 @@ abstract class Hex implements EncoderInterface
*
* @param string $binString (raw binary)
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function encode(string $binString): string
{
Expand All @@ -64,7 +67,7 @@ public static function encode(string $binString): string
*
* @param string $binString (raw binary)
* @return string
* @throws \TypeError
* @throws TypeError
*/
public static function encodeUpper(string $binString): string
{
Expand Down Expand Up @@ -93,18 +96,20 @@ public static function encodeUpper(string $binString): string
* @param string $encodedString
* @param bool $strictPadding
* @return string (raw binary)
* @throws \RangeException
* @throws RangeException
*/
public static function decode(string $encodedString, bool $strictPadding = false): string
{
public static function decode(
string $encodedString,
bool $strictPadding = false
): string {
$hex_pos = 0;
$bin = '';
$c_acc = 0;
$hex_len = Binary::safeStrlen($encodedString);
$state = 0;
if (($hex_len & 1) !== 0) {
if ($strictPadding) {
throw new \RangeException(
throw new RangeException(
'Expected an even number of hexadecimal characters'
);
} else {
Expand All @@ -124,7 +129,7 @@ public static function decode(string $encodedString, bool $strictPadding = false
$c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;

if (($c_num0 | $c_alpha0) === 0) {
throw new \RangeException(
throw new RangeException(
'Expected hexadecimal character'
);
}
Expand Down
Loading

0 comments on commit 58c3f47

Please sign in to comment.