| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
namespace ParagonIE\ConstantTime; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use Override; |
| 7 |
use RangeException; |
| 8 |
use SensitiveParameter; |
| 9 |
use SodiumException; |
| 10 |
use TypeError; |
| 11 |
use function extension_loaded; |
| 12 |
use function pack; |
| 13 |
use function rtrim; |
| 14 |
use function sodium_base642bin; |
| 15 |
use function sodium_bin2base64; |
| 16 |
use function strlen; |
| 17 |
use function substr; |
| 18 |
use function unpack; |
| 19 |
use const SODIUM_BASE64_VARIANT_ORIGINAL; |
| 20 |
use const SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING; |
| 21 |
use const SODIUM_BASE64_VARIANT_URLSAFE; |
| 22 |
use const SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
abstract class Base64 implements EncoderInterface |
| 54 |
{ |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#[Override] |
| 66 |
public static function encode( |
| 67 |
#[SensitiveParameter] |
| 68 |
string $binString |
| 69 |
): string { |
| 70 |
if (extension_loaded('sodium')) { |
| 71 |
$variant = match(static::class) { |
| 72 |
Base64::class => SODIUM_BASE64_VARIANT_ORIGINAL, |
| 73 |
Base64UrlSafe::class => SODIUM_BASE64_VARIANT_URLSAFE, |
| 74 |
default => 0, |
| 75 |
}; |
| 76 |
if ($variant > 0) { |
| 77 |
try { |
| 78 |
return sodium_bin2base64($binString, $variant); |
| 79 |
} catch (SodiumException $ex) { |
| 80 |
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
return static::doEncode($binString, true); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static function encodeUnpadded( |
| 99 |
#[SensitiveParameter] |
| 100 |
string $src |
| 101 |
): string { |
| 102 |
if (extension_loaded('sodium')) { |
| 103 |
$variant = match(static::class) { |
| 104 |
Base64::class => SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING, |
| 105 |
Base64UrlSafe::class => SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING, |
| 106 |
default => 0, |
| 107 |
}; |
| 108 |
if ($variant > 0) { |
| 109 |
try { |
| 110 |
return sodium_bin2base64($src, $variant); |
| 111 |
} catch (SodiumException $ex) { |
| 112 |
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
return static::doEncode($src, false); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
protected static function doEncode( |
| 127 |
#[SensitiveParameter] |
| 128 |
string $src, |
| 129 |
bool $pad = true |
| 130 |
): string { |
| 131 |
$dest = ''; |
| 132 |
$srcLen = strlen($src); |
| 133 |
|
| 134 |
for ($i = 0; $i + 3 <= $srcLen; $i += 3) { |
| 135 |
|
| 136 |
$chunk = unpack('C*', substr($src, $i, 3)); |
| 137 |
$b0 = $chunk[1]; |
| 138 |
$b1 = $chunk[2]; |
| 139 |
$b2 = $chunk[3]; |
| 140 |
|
| 141 |
$dest .= |
| 142 |
static::encode6Bits( $b0 >> 2 ) . |
| 143 |
static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
| 144 |
static::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) . |
| 145 |
static::encode6Bits( $b2 & 63); |
| 146 |
} |
| 147 |
|
| 148 |
if ($i < $srcLen) { |
| 149 |
|
| 150 |
$chunk = unpack('C*', substr($src, $i, $srcLen - $i)); |
| 151 |
$b0 = $chunk[1]; |
| 152 |
if ($i + 1 < $srcLen) { |
| 153 |
$b1 = $chunk[2]; |
| 154 |
$dest .= |
| 155 |
static::encode6Bits($b0 >> 2) . |
| 156 |
static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
| 157 |
static::encode6Bits(($b1 << 2) & 63); |
| 158 |
if ($pad) { |
| 159 |
$dest .= '='; |
| 160 |
} |
| 161 |
} else { |
| 162 |
$dest .= |
| 163 |
static::encode6Bits( $b0 >> 2) . |
| 164 |
static::encode6Bits(($b0 << 4) & 63); |
| 165 |
if ($pad) { |
| 166 |
$dest .= '=='; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
return $dest; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
#[Override] |
| 186 |
public static function decode( |
| 187 |
#[SensitiveParameter] |
| 188 |
string $encodedString, |
| 189 |
bool $strictPadding = false |
| 190 |
): string { |
| 191 |
|
| 192 |
$srcLen = strlen($encodedString); |
| 193 |
if ($srcLen === 0) { |
| 194 |
return ''; |
| 195 |
} |
| 196 |
|
| 197 |
if ($strictPadding) { |
| 198 |
if (($srcLen & 3) === 0) { |
| 199 |
if ($encodedString[$srcLen - 1] === '=') { |
| 200 |
$srcLen--; |
| 201 |
if ($encodedString[$srcLen - 1] === '=') { |
| 202 |
$srcLen--; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
if (($srcLen & 3) === 1) { |
| 207 |
throw new RangeException( |
| 208 |
'Incorrect padding' |
| 209 |
); |
| 210 |
} |
| 211 |
if ($encodedString[$srcLen - 1] === '=') { |
| 212 |
throw new RangeException( |
| 213 |
'Incorrect padding' |
| 214 |
); |
| 215 |
} |
| 216 |
if (extension_loaded('sodium')) { |
| 217 |
$variant = match(static::class) { |
| 218 |
Base64::class => SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING, |
| 219 |
Base64UrlSafe::class => SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING, |
| 220 |
default => 0, |
| 221 |
}; |
| 222 |
if ($variant > 0) { |
| 223 |
try { |
| 224 |
return sodium_base642bin(substr($encodedString, 0, $srcLen), $variant); |
| 225 |
} catch (SodiumException $ex) { |
| 226 |
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} else { |
| 231 |
|
| 232 |
$encodedString = rtrim($encodedString, '='); |
| 233 |
$srcLen = strlen($encodedString); |
| 234 |
} |
| 235 |
|
| 236 |
$err = 0; |
| 237 |
$dest = ''; |
| 238 |
|
| 239 |
for ($i = 0; $i + 4 <= $srcLen; $i += 4) { |
| 240 |
|
| 241 |
$chunk = unpack('C*', substr($encodedString, $i, 4)); |
| 242 |
$c0 = static::decode6Bits($chunk[1]); |
| 243 |
$c1 = static::decode6Bits($chunk[2]); |
| 244 |
$c2 = static::decode6Bits($chunk[3]); |
| 245 |
$c3 = static::decode6Bits($chunk[4]); |
| 246 |
|
| 247 |
$dest .= pack( |
| 248 |
'CCC', |
| 249 |
((($c0 << 2) | ($c1 >> 4)) & 0xff), |
| 250 |
((($c1 << 4) | ($c2 >> 2)) & 0xff), |
| 251 |
((($c2 << 6) | $c3 ) & 0xff) |
| 252 |
); |
| 253 |
$err |= ($c0 | $c1 | $c2 | $c3) >> 8; |
| 254 |
} |
| 255 |
|
| 256 |
if ($i < $srcLen) { |
| 257 |
|
| 258 |
$chunk = unpack('C*', substr($encodedString, $i, $srcLen - $i)); |
| 259 |
$c0 = static::decode6Bits($chunk[1]); |
| 260 |
|
| 261 |
if ($i + 2 < $srcLen) { |
| 262 |
$c1 = static::decode6Bits($chunk[2]); |
| 263 |
$c2 = static::decode6Bits($chunk[3]); |
| 264 |
$dest .= pack( |
| 265 |
'CC', |
| 266 |
((($c0 << 2) | ($c1 >> 4)) & 0xff), |
| 267 |
((($c1 << 4) | ($c2 >> 2)) & 0xff) |
| 268 |
); |
| 269 |
$err |= ($c0 | $c1 | $c2) >> 8; |
| 270 |
if ($strictPadding) { |
| 271 |
$err |= ($c2 << 6) & 0xff; |
| 272 |
} |
| 273 |
} elseif ($i + 1 < $srcLen) { |
| 274 |
$c1 = static::decode6Bits($chunk[2]); |
| 275 |
$dest .= pack( |
| 276 |
'C', |
| 277 |
((($c0 << 2) | ($c1 >> 4)) & 0xff) |
| 278 |
); |
| 279 |
$err |= ($c0 | $c1) >> 8; |
| 280 |
if ($strictPadding) { |
| 281 |
$err |= ($c1 << 4) & 0xff; |
| 282 |
} |
| 283 |
} elseif ($strictPadding) { |
| 284 |
$err |= 1; |
| 285 |
} |
| 286 |
} |
| 287 |
$check = ($err === 0); |
| 288 |
if (!$check) { |
| 289 |
throw new RangeException( |
| 290 |
'Base64::decode() only expects characters in the correct base64 alphabet' |
| 291 |
); |
| 292 |
} |
| 293 |
return $dest; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public static function decodeNoPadding( |
| 302 |
#[SensitiveParameter] |
| 303 |
string $encodedString |
| 304 |
): string { |
| 305 |
$srcLen = strlen($encodedString); |
| 306 |
if ($srcLen === 0) { |
| 307 |
return ''; |
| 308 |
} |
| 309 |
if (($srcLen & 3) === 0) { |
| 310 |
|
| 311 |
if ($encodedString[$srcLen - 1] === '=' || $encodedString[$srcLen - 2] === '=') { |
| 312 |
throw new InvalidArgumentException( |
| 313 |
"decodeNoPadding() doesn't tolerate padding" |
| 314 |
); |
| 315 |
} |
| 316 |
} |
| 317 |
return static::decode( |
| 318 |
$encodedString, |
| 319 |
true |
| 320 |
); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
protected static function decode6Bits(int $src): int |
| 335 |
{ |
| 336 |
$ret = -1; |
| 337 |
|
| 338 |
|
| 339 |
$ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); |
| 340 |
|
| 341 |
|
| 342 |
$ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); |
| 343 |
|
| 344 |
|
| 345 |
$ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); |
| 346 |
|
| 347 |
|
| 348 |
$ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63; |
| 349 |
|
| 350 |
|
| 351 |
$ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64; |
| 352 |
|
| 353 |
return $ret; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
protected static function encode6Bits(int $src): string |
| 364 |
{ |
| 365 |
$diff = 0x41; |
| 366 |
|
| 367 |
|
| 368 |
$diff += ((25 - $src) >> 8) & 6; |
| 369 |
|
| 370 |
|
| 371 |
$diff -= ((51 - $src) >> 8) & 75; |
| 372 |
|
| 373 |
|
| 374 |
$diff -= ((61 - $src) >> 8) & 15; |
| 375 |
|
| 376 |
|
| 377 |
$diff += ((62 - $src) >> 8) & 3; |
| 378 |
|
| 379 |
return pack('C', $src + $diff); |
| 380 |
} |
| 381 |
} |