Base32.php
7 months ago
Base32Hex.php
7 months ago
Base64.php
7 months ago
Base64DotSlash.php
7 months ago
Base64DotSlashOrdered.php
7 months ago
Base64UrlSafe.php
7 months ago
Binary.php
7 months ago
EncoderInterface.php
7 months ago
Encoding.php
7 months ago
Hex.php
7 months ago
RFC4648.php
7 months ago
Base32.php
440 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace AmeliaVendor\ParagonIE\ConstantTime; |
| 5 | |
| 6 | use InvalidArgumentException; |
| 7 | use RangeException; |
| 8 | use SensitiveParameter; |
| 9 | use TypeError; |
| 10 | use function pack; |
| 11 | use function unpack; |
| 12 | /** |
| 13 | * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. |
| 14 | * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) |
| 15 | * |
| 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 17 | * of this software and associated documentation files (the "Software"), to deal |
| 18 | * in the Software without restriction, including without limitation the rights |
| 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 20 | * copies of the Software, and to permit persons to whom the Software is |
| 21 | * furnished to do so, subject to the following conditions: |
| 22 | * |
| 23 | * The above copyright notice and this permission notice shall be included in all |
| 24 | * copies or substantial portions of the Software. |
| 25 | * |
| 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 | * SOFTWARE. |
| 33 | */ |
| 34 | /** |
| 35 | * Class Base32 |
| 36 | * [A-Z][2-7] |
| 37 | * |
| 38 | * @package \ParagonIE\ConstantTime |
| 39 | */ |
| 40 | abstract class Base32 implements EncoderInterface |
| 41 | { |
| 42 | /** |
| 43 | * Decode a Base32-encoded string into raw binary |
| 44 | * |
| 45 | * @param string $encodedString |
| 46 | * @param bool $strictPadding |
| 47 | * @return string |
| 48 | */ |
| 49 | public static function decode( |
| 50 | #[SensitiveParameter] |
| 51 | string $encodedString, |
| 52 | bool $strictPadding = false |
| 53 | ): string |
| 54 | { |
| 55 | return static::doDecode($encodedString, false, $strictPadding); |
| 56 | } |
| 57 | /** |
| 58 | * Decode an uppercase Base32-encoded string into raw binary |
| 59 | * |
| 60 | * @param string $src |
| 61 | * @param bool $strictPadding |
| 62 | * @return string |
| 63 | */ |
| 64 | public static function decodeUpper( |
| 65 | #[SensitiveParameter] |
| 66 | string $src, |
| 67 | bool $strictPadding = false |
| 68 | ): string |
| 69 | { |
| 70 | return static::doDecode($src, true, $strictPadding); |
| 71 | } |
| 72 | /** |
| 73 | * Encode into Base32 (RFC 4648) |
| 74 | * |
| 75 | * @param string $binString |
| 76 | * @return string |
| 77 | * @throws TypeError |
| 78 | */ |
| 79 | public static function encode( |
| 80 | #[SensitiveParameter] |
| 81 | string $binString |
| 82 | ): string |
| 83 | { |
| 84 | return static::doEncode($binString, false, true); |
| 85 | } |
| 86 | /** |
| 87 | * Encode into Base32 (RFC 4648) |
| 88 | * |
| 89 | * @param string $src |
| 90 | * @return string |
| 91 | * @throws TypeError |
| 92 | */ |
| 93 | public static function encodeUnpadded( |
| 94 | #[SensitiveParameter] |
| 95 | string $src |
| 96 | ): string |
| 97 | { |
| 98 | return static::doEncode($src, false, false); |
| 99 | } |
| 100 | /** |
| 101 | * Encode into uppercase Base32 (RFC 4648) |
| 102 | * |
| 103 | * @param string $src |
| 104 | * @return string |
| 105 | * @throws TypeError |
| 106 | */ |
| 107 | public static function encodeUpper( |
| 108 | #[SensitiveParameter] |
| 109 | string $src |
| 110 | ): string |
| 111 | { |
| 112 | return static::doEncode($src, true, true); |
| 113 | } |
| 114 | /** |
| 115 | * Encode into uppercase Base32 (RFC 4648) |
| 116 | * |
| 117 | * @param string $src |
| 118 | * @return string |
| 119 | * @throws TypeError |
| 120 | */ |
| 121 | public static function encodeUpperUnpadded( |
| 122 | #[SensitiveParameter] |
| 123 | string $src |
| 124 | ): string |
| 125 | { |
| 126 | return static::doEncode($src, true, false); |
| 127 | } |
| 128 | /** |
| 129 | * Uses bitwise operators instead of table-lookups to turn 5-bit integers |
| 130 | * into 8-bit integers. |
| 131 | * |
| 132 | * @param int $src |
| 133 | * @return int |
| 134 | */ |
| 135 | protected static function decode5Bits(int $src): int |
| 136 | { |
| 137 | $ret = -1; |
| 138 | // if ($src > 96 && $src < 123) $ret += $src - 97 + 1; // -64 |
| 139 | $ret += (0x60 - $src & $src - 0x7b) >> 8 & $src - 96; |
| 140 | // if ($src > 0x31 && $src < 0x38) $ret += $src - 24 + 1; // -23 |
| 141 | $ret += (0x31 - $src & $src - 0x38) >> 8 & $src - 23; |
| 142 | return $ret; |
| 143 | } |
| 144 | /** |
| 145 | * Uses bitwise operators instead of table-lookups to turn 5-bit integers |
| 146 | * into 8-bit integers. |
| 147 | * |
| 148 | * Uppercase variant. |
| 149 | * |
| 150 | * @param int $src |
| 151 | * @return int |
| 152 | */ |
| 153 | protected static function decode5BitsUpper(int $src): int |
| 154 | { |
| 155 | $ret = -1; |
| 156 | // if ($src > 64 && $src < 91) $ret += $src - 65 + 1; // -64 |
| 157 | $ret += (0x40 - $src & $src - 0x5b) >> 8 & $src - 64; |
| 158 | // if ($src > 0x31 && $src < 0x38) $ret += $src - 24 + 1; // -23 |
| 159 | $ret += (0x31 - $src & $src - 0x38) >> 8 & $src - 23; |
| 160 | return $ret; |
| 161 | } |
| 162 | /** |
| 163 | * Uses bitwise operators instead of table-lookups to turn 8-bit integers |
| 164 | * into 5-bit integers. |
| 165 | * |
| 166 | * @param int $src |
| 167 | * @return string |
| 168 | */ |
| 169 | protected static function encode5Bits(int $src): string |
| 170 | { |
| 171 | $diff = 0x61; |
| 172 | // if ($src > 25) $ret -= 72; |
| 173 | $diff -= 25 - $src >> 8 & 73; |
| 174 | return pack('C', $src + $diff); |
| 175 | } |
| 176 | /** |
| 177 | * Uses bitwise operators instead of table-lookups to turn 8-bit integers |
| 178 | * into 5-bit integers. |
| 179 | * |
| 180 | * Uppercase variant. |
| 181 | * |
| 182 | * @param int $src |
| 183 | * @return string |
| 184 | */ |
| 185 | protected static function encode5BitsUpper(int $src): string |
| 186 | { |
| 187 | $diff = 0x41; |
| 188 | // if ($src > 25) $ret -= 40; |
| 189 | $diff -= 25 - $src >> 8 & 41; |
| 190 | return pack('C', $src + $diff); |
| 191 | } |
| 192 | /** |
| 193 | * @param string $encodedString |
| 194 | * @param bool $upper |
| 195 | * @return string |
| 196 | */ |
| 197 | public static function decodeNoPadding( |
| 198 | #[SensitiveParameter] |
| 199 | string $encodedString, |
| 200 | bool $upper = false |
| 201 | ): string |
| 202 | { |
| 203 | $srcLen = Binary::safeStrlen($encodedString); |
| 204 | if ($srcLen === 0) { |
| 205 | return ''; |
| 206 | } |
| 207 | if (($srcLen & 7) === 0) { |
| 208 | for ($j = 0; $j < 7 && $j < $srcLen; ++$j) { |
| 209 | if ($encodedString[$srcLen - $j - 1] === '=') { |
| 210 | throw new InvalidArgumentException("decodeNoPadding() doesn't tolerate padding"); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | return static::doDecode($encodedString, $upper, true); |
| 215 | } |
| 216 | /** |
| 217 | * Base32 decoding |
| 218 | * |
| 219 | * @param string $src |
| 220 | * @param bool $upper |
| 221 | * @param bool $strictPadding |
| 222 | * @return string |
| 223 | * |
| 224 | * @throws TypeError |
| 225 | */ |
| 226 | protected static function doDecode( |
| 227 | #[SensitiveParameter] |
| 228 | string $src, |
| 229 | bool $upper = false, |
| 230 | bool $strictPadding = false |
| 231 | ): string |
| 232 | { |
| 233 | // We do this to reduce code duplication: |
| 234 | $method = $upper ? 'decode5BitsUpper' : 'decode5Bits'; |
| 235 | // Remove padding |
| 236 | $srcLen = Binary::safeStrlen($src); |
| 237 | if ($srcLen === 0) { |
| 238 | return ''; |
| 239 | } |
| 240 | if ($strictPadding) { |
| 241 | if (($srcLen & 7) === 0) { |
| 242 | for ($j = 0; $j < 7; ++$j) { |
| 243 | if ($src[$srcLen - 1] === '=') { |
| 244 | $srcLen--; |
| 245 | } else { |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | if (($srcLen & 7) === 1) { |
| 251 | throw new RangeException('Incorrect padding'); |
| 252 | } |
| 253 | } else { |
| 254 | $src = \rtrim($src, '='); |
| 255 | $srcLen = Binary::safeStrlen($src); |
| 256 | } |
| 257 | $err = 0; |
| 258 | $dest = ''; |
| 259 | // Main loop (no padding): |
| 260 | for ($i = 0; $i + 8 <= $srcLen; $i += 8) { |
| 261 | /** @var array<int, int> $chunk */ |
| 262 | $chunk = unpack('C*', Binary::safeSubstr($src, $i, 8)); |
| 263 | /** @var int $c0 */ |
| 264 | $c0 = static::$method($chunk[1]); |
| 265 | /** @var int $c1 */ |
| 266 | $c1 = static::$method($chunk[2]); |
| 267 | /** @var int $c2 */ |
| 268 | $c2 = static::$method($chunk[3]); |
| 269 | /** @var int $c3 */ |
| 270 | $c3 = static::$method($chunk[4]); |
| 271 | /** @var int $c4 */ |
| 272 | $c4 = static::$method($chunk[5]); |
| 273 | /** @var int $c5 */ |
| 274 | $c5 = static::$method($chunk[6]); |
| 275 | /** @var int $c6 */ |
| 276 | $c6 = static::$method($chunk[7]); |
| 277 | /** @var int $c7 */ |
| 278 | $c7 = static::$method($chunk[8]); |
| 279 | $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); |
| 280 | $err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6 | $c7) >> 8; |
| 281 | } |
| 282 | // The last chunk, which may have padding: |
| 283 | if ($i < $srcLen) { |
| 284 | /** @var array<int, int> $chunk */ |
| 285 | $chunk = unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i)); |
| 286 | /** @var int $c0 */ |
| 287 | $c0 = static::$method($chunk[1]); |
| 288 | if ($i + 6 < $srcLen) { |
| 289 | /** @var int $c1 */ |
| 290 | $c1 = static::$method($chunk[2]); |
| 291 | /** @var int $c2 */ |
| 292 | $c2 = static::$method($chunk[3]); |
| 293 | /** @var int $c3 */ |
| 294 | $c3 = static::$method($chunk[4]); |
| 295 | /** @var int $c4 */ |
| 296 | $c4 = static::$method($chunk[5]); |
| 297 | /** @var int $c5 */ |
| 298 | $c5 = static::$method($chunk[6]); |
| 299 | /** @var int $c6 */ |
| 300 | $c6 = static::$method($chunk[7]); |
| 301 | $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); |
| 302 | $err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6) >> 8; |
| 303 | if ($strictPadding) { |
| 304 | $err |= $c6 << 5 & 0xff; |
| 305 | } |
| 306 | } elseif ($i + 5 < $srcLen) { |
| 307 | /** @var int $c1 */ |
| 308 | $c1 = static::$method($chunk[2]); |
| 309 | /** @var int $c2 */ |
| 310 | $c2 = static::$method($chunk[3]); |
| 311 | /** @var int $c3 */ |
| 312 | $c3 = static::$method($chunk[4]); |
| 313 | /** @var int $c4 */ |
| 314 | $c4 = static::$method($chunk[5]); |
| 315 | /** @var int $c5 */ |
| 316 | $c5 = static::$method($chunk[6]); |
| 317 | $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); |
| 318 | $err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5) >> 8; |
| 319 | } elseif ($i + 4 < $srcLen) { |
| 320 | /** @var int $c1 */ |
| 321 | $c1 = static::$method($chunk[2]); |
| 322 | /** @var int $c2 */ |
| 323 | $c2 = static::$method($chunk[3]); |
| 324 | /** @var int $c3 */ |
| 325 | $c3 = static::$method($chunk[4]); |
| 326 | /** @var int $c4 */ |
| 327 | $c4 = static::$method($chunk[5]); |
| 328 | $dest .= pack('CCC', ($c0 << 3 | $c1 >> 2) & 0xff, ($c1 << 6 | $c2 << 1 | $c3 >> 4) & 0xff, ($c3 << 4 | $c4 >> 1) & 0xff); |
| 329 | $err |= ($c0 | $c1 | $c2 | $c3 | $c4) >> 8; |
| 330 | if ($strictPadding) { |
| 331 | $err |= $c4 << 7 & 0xff; |
| 332 | } |
| 333 | } elseif ($i + 3 < $srcLen) { |
| 334 | /** @var int $c1 */ |
| 335 | $c1 = static::$method($chunk[2]); |
| 336 | /** @var int $c2 */ |
| 337 | $c2 = static::$method($chunk[3]); |
| 338 | /** @var int $c3 */ |
| 339 | $c3 = static::$method($chunk[4]); |
| 340 | $dest .= pack('CC', ($c0 << 3 | $c1 >> 2) & 0xff, ($c1 << 6 | $c2 << 1 | $c3 >> 4) & 0xff); |
| 341 | $err |= ($c0 | $c1 | $c2 | $c3) >> 8; |
| 342 | if ($strictPadding) { |
| 343 | $err |= $c3 << 4 & 0xff; |
| 344 | } |
| 345 | } elseif ($i + 2 < $srcLen) { |
| 346 | /** @var int $c1 */ |
| 347 | $c1 = static::$method($chunk[2]); |
| 348 | /** @var int $c2 */ |
| 349 | $c2 = static::$method($chunk[3]); |
| 350 | $dest .= pack('CC', ($c0 << 3 | $c1 >> 2) & 0xff, ($c1 << 6 | $c2 << 1) & 0xff); |
| 351 | $err |= ($c0 | $c1 | $c2) >> 8; |
| 352 | if ($strictPadding) { |
| 353 | $err |= $c2 << 6 & 0xff; |
| 354 | } |
| 355 | } elseif ($i + 1 < $srcLen) { |
| 356 | /** @var int $c1 */ |
| 357 | $c1 = static::$method($chunk[2]); |
| 358 | $dest .= pack('C', ($c0 << 3 | $c1 >> 2) & 0xff); |
| 359 | $err |= ($c0 | $c1) >> 8; |
| 360 | if ($strictPadding) { |
| 361 | $err |= $c1 << 6 & 0xff; |
| 362 | } |
| 363 | } else { |
| 364 | $dest .= pack('C', $c0 << 3 & 0xff); |
| 365 | $err |= $c0 >> 8; |
| 366 | } |
| 367 | } |
| 368 | $check = $err === 0; |
| 369 | if (!$check) { |
| 370 | throw new RangeException('Base32::doDecode() only expects characters in the correct base32 alphabet'); |
| 371 | } |
| 372 | return $dest; |
| 373 | } |
| 374 | /** |
| 375 | * Base32 Encoding |
| 376 | * |
| 377 | * @param string $src |
| 378 | * @param bool $upper |
| 379 | * @param bool $pad |
| 380 | * @return string |
| 381 | * @throws TypeError |
| 382 | */ |
| 383 | protected static function doEncode( |
| 384 | #[SensitiveParameter] |
| 385 | string $src, |
| 386 | bool $upper = false, |
| 387 | $pad = true |
| 388 | ): string |
| 389 | { |
| 390 | // We do this to reduce code duplication: |
| 391 | $method = $upper ? 'encode5BitsUpper' : 'encode5Bits'; |
| 392 | $dest = ''; |
| 393 | $srcLen = Binary::safeStrlen($src); |
| 394 | // Main loop (no padding): |
| 395 | for ($i = 0; $i + 5 <= $srcLen; $i += 5) { |
| 396 | /** @var array<int, int> $chunk */ |
| 397 | $chunk = unpack('C*', Binary::safeSubstr($src, $i, 5)); |
| 398 | $b0 = $chunk[1]; |
| 399 | $b1 = $chunk[2]; |
| 400 | $b2 = $chunk[3]; |
| 401 | $b3 = $chunk[4]; |
| 402 | $b4 = $chunk[5]; |
| 403 | $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); |
| 404 | } |
| 405 | // The last chunk, which may have padding: |
| 406 | if ($i < $srcLen) { |
| 407 | /** @var array<int, int> $chunk */ |
| 408 | $chunk = unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i)); |
| 409 | $b0 = $chunk[1]; |
| 410 | if ($i + 3 < $srcLen) { |
| 411 | $b1 = $chunk[2]; |
| 412 | $b2 = $chunk[3]; |
| 413 | $b3 = $chunk[4]; |
| 414 | $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); |
| 415 | if ($pad) { |
| 416 | $dest .= '='; |
| 417 | } |
| 418 | } elseif ($i + 2 < $srcLen) { |
| 419 | $b1 = $chunk[2]; |
| 420 | $b2 = $chunk[3]; |
| 421 | $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); |
| 422 | if ($pad) { |
| 423 | $dest .= '==='; |
| 424 | } |
| 425 | } elseif ($i + 1 < $srcLen) { |
| 426 | $b1 = $chunk[2]; |
| 427 | $dest .= static::$method($b0 >> 3 & 31) . static::$method(($b0 << 2 | $b1 >> 6) & 31) . static::$method($b1 >> 1 & 31) . static::$method($b1 << 4 & 31); |
| 428 | if ($pad) { |
| 429 | $dest .= '===='; |
| 430 | } |
| 431 | } else { |
| 432 | $dest .= static::$method($b0 >> 3 & 31) . static::$method($b0 << 2 & 31); |
| 433 | if ($pad) { |
| 434 | $dest .= '======'; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | return $dest; |
| 439 | } |
| 440 | } |