Base32.php 13 KB

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