dfe2c19e by Jeff Balicki

stuff

1 parent 3fc53708
Showing 703 changed files with 4964 additions and 0 deletions
1 <?php
2
3 // autoload.php @generated by Composer
4
5 if (!class_exists('ComposerAutoloaderInite213e65b2dafae4ad799b13fe0d36f8e')) {
6 require_once __DIR__ . '/composer/autoload_real.php';
7 }
8
9 return ComposerAutoloaderInite213e65b2dafae4ad799b13fe0d36f8e::getLoader();
1 <?php
2
3 namespace Composer;
4
5 use Composer\Semver\VersionParser;
6
7
8
9
10
11
12 class InstalledVersions
13 {
14 private static $installed = array (
15 'root' =>
16 array (
17 'pretty_version' => 'dev-master',
18 'version' => 'dev-master',
19 'aliases' =>
20 array (
21 ),
22 'reference' => '547ce2234fff039cbd57697d05f7236e431868e5',
23 'name' => '__root__',
24 ),
25 'versions' =>
26 array (
27 '__root__' =>
28 array (
29 'pretty_version' => 'dev-master',
30 'version' => 'dev-master',
31 'aliases' =>
32 array (
33 ),
34 'reference' => '547ce2234fff039cbd57697d05f7236e431868e5',
35 ),
36 'paragonie/random_compat' =>
37 array (
38 'pretty_version' => 'v2.0.10',
39 'version' => '2.0.10.0',
40 'aliases' =>
41 array (
42 ),
43 'reference' => '634bae8e911eefa89c1abfbf1b66da679ac8f54d',
44 ),
45 'paragonie/sodium_compat' =>
46 array (
47 'pretty_version' => 'v1.13.0',
48 'version' => '1.13.0.0',
49 'aliases' =>
50 array (
51 ),
52 'reference' => 'bbade402cbe84c69b718120911506a3aa2bae653',
53 ),
54 ),
55 );
56
57
58
59
60
61
62
63 public static function getInstalledPackages()
64 {
65 return array_keys(self::$installed['versions']);
66 }
67
68
69
70
71
72
73
74
75
76 public static function isInstalled($packageName)
77 {
78 return isset(self::$installed['versions'][$packageName]);
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public static function satisfies(VersionParser $parser, $packageName, $constraint)
95 {
96 $constraint = $parser->parseConstraints($constraint);
97 $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
98
99 return $provided->matches($constraint);
100 }
101
102
103
104
105
106
107
108
109
110
111 public static function getVersionRanges($packageName)
112 {
113 if (!isset(self::$installed['versions'][$packageName])) {
114 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
115 }
116
117 $ranges = array();
118 if (isset(self::$installed['versions'][$packageName]['pretty_version'])) {
119 $ranges[] = self::$installed['versions'][$packageName]['pretty_version'];
120 }
121 if (array_key_exists('aliases', self::$installed['versions'][$packageName])) {
122 $ranges = array_merge($ranges, self::$installed['versions'][$packageName]['aliases']);
123 }
124 if (array_key_exists('replaced', self::$installed['versions'][$packageName])) {
125 $ranges = array_merge($ranges, self::$installed['versions'][$packageName]['replaced']);
126 }
127 if (array_key_exists('provided', self::$installed['versions'][$packageName])) {
128 $ranges = array_merge($ranges, self::$installed['versions'][$packageName]['provided']);
129 }
130
131 return implode(' || ', $ranges);
132 }
133
134
135
136
137
138 public static function getVersion($packageName)
139 {
140 if (!isset(self::$installed['versions'][$packageName])) {
141 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
142 }
143
144 if (!isset(self::$installed['versions'][$packageName]['version'])) {
145 return null;
146 }
147
148 return self::$installed['versions'][$packageName]['version'];
149 }
150
151
152
153
154
155 public static function getPrettyVersion($packageName)
156 {
157 if (!isset(self::$installed['versions'][$packageName])) {
158 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
159 }
160
161 if (!isset(self::$installed['versions'][$packageName]['pretty_version'])) {
162 return null;
163 }
164
165 return self::$installed['versions'][$packageName]['pretty_version'];
166 }
167
168
169
170
171
172 public static function getReference($packageName)
173 {
174 if (!isset(self::$installed['versions'][$packageName])) {
175 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
176 }
177
178 if (!isset(self::$installed['versions'][$packageName]['reference'])) {
179 return null;
180 }
181
182 return self::$installed['versions'][$packageName]['reference'];
183 }
184
185
186
187
188
189 public static function getRootPackage()
190 {
191 return self::$installed['root'];
192 }
193
194
195
196
197
198
199
200 public static function getRawData()
201 {
202 return self::$installed;
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 public static function reload($data)
224 {
225 self::$installed = $data;
226 }
227 }
1
2 Copyright (c) Nils Adermann, Jordi Boggiano
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is furnished
9 to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21
1 <?php
2
3 // autoload_classmap.php @generated by Composer
4
5 $vendorDir = dirname(dirname(__FILE__));
6 $baseDir = dirname($vendorDir);
7
8 return array(
9 );
1 <?php
2
3 // autoload_files.php @generated by Composer
4
5 $vendorDir = dirname(dirname(__FILE__));
6 $baseDir = dirname($vendorDir);
7
8 return array(
9 '5255c38a0faeba867671b61dfda6d864' => $vendorDir . '/paragonie/random_compat/lib/random.php',
10 '3109cb1a231dcd04bee1f9f620d46975' => $vendorDir . '/paragonie/sodium_compat/autoload.php',
11 );
1 <?php
2
3 // autoload_namespaces.php @generated by Composer
4
5 $vendorDir = dirname(dirname(__FILE__));
6 $baseDir = dirname($vendorDir);
7
8 return array(
9 );
1 <?php
2
3 // autoload_psr4.php @generated by Composer
4
5 $vendorDir = dirname(dirname(__FILE__));
6 $baseDir = dirname($vendorDir);
7
8 return array(
9 );
1 <?php
2
3 // autoload_real.php @generated by Composer
4
5 class ComposerAutoloaderInite213e65b2dafae4ad799b13fe0d36f8e
6 {
7 private static $loader;
8
9 public static function loadClassLoader($class)
10 {
11 if ('Composer\Autoload\ClassLoader' === $class) {
12 require __DIR__ . '/ClassLoader.php';
13 }
14 }
15
16 /**
17 * @return \Composer\Autoload\ClassLoader
18 */
19 public static function getLoader()
20 {
21 if (null !== self::$loader) {
22 return self::$loader;
23 }
24
25 spl_autoload_register(array('ComposerAutoloaderInite213e65b2dafae4ad799b13fe0d36f8e', 'loadClassLoader'), true, true);
26 self::$loader = $loader = new \Composer\Autoload\ClassLoader();
27 spl_autoload_unregister(array('ComposerAutoloaderInite213e65b2dafae4ad799b13fe0d36f8e', 'loadClassLoader'));
28
29 $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
30 if ($useStaticLoader) {
31 require_once __DIR__ . '/autoload_static.php';
32
33 call_user_func(\Composer\Autoload\ComposerStaticInite213e65b2dafae4ad799b13fe0d36f8e::getInitializer($loader));
34 } else {
35 $map = require __DIR__ . '/autoload_namespaces.php';
36 foreach ($map as $namespace => $path) {
37 $loader->set($namespace, $path);
38 }
39
40 $map = require __DIR__ . '/autoload_psr4.php';
41 foreach ($map as $namespace => $path) {
42 $loader->setPsr4($namespace, $path);
43 }
44
45 $classMap = require __DIR__ . '/autoload_classmap.php';
46 if ($classMap) {
47 $loader->addClassMap($classMap);
48 }
49 }
50
51 $loader->register(true);
52
53 if ($useStaticLoader) {
54 $includeFiles = Composer\Autoload\ComposerStaticInite213e65b2dafae4ad799b13fe0d36f8e::$files;
55 } else {
56 $includeFiles = require __DIR__ . '/autoload_files.php';
57 }
58 foreach ($includeFiles as $fileIdentifier => $file) {
59 composerRequiree213e65b2dafae4ad799b13fe0d36f8e($fileIdentifier, $file);
60 }
61
62 return $loader;
63 }
64 }
65
66 function composerRequiree213e65b2dafae4ad799b13fe0d36f8e($fileIdentifier, $file)
67 {
68 if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
69 require $file;
70
71 $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
72 }
73 }
1 <?php
2
3 // autoload_static.php @generated by Composer
4
5 namespace Composer\Autoload;
6
7 class ComposerStaticInite213e65b2dafae4ad799b13fe0d36f8e
8 {
9 public static $files = array (
10 '5255c38a0faeba867671b61dfda6d864' => __DIR__ . '/..' . '/paragonie/random_compat/lib/random.php',
11 '3109cb1a231dcd04bee1f9f620d46975' => __DIR__ . '/..' . '/paragonie/sodium_compat/autoload.php',
12 );
13
14 public static function getInitializer(ClassLoader $loader)
15 {
16 return \Closure::bind(function () use ($loader) {
17
18 }, null, ClassLoader::class);
19 }
20 }
1 [
2 {
3 "name": "paragonie/random_compat",
4 "version": "v2.0.17",
5 "version_normalized": "2.0.17.0",
6 "source": {
7 "type": "git",
8 "url": "https://github.com/paragonie/random_compat.git",
9 "reference": "29af24f25bab834fcbb38ad2a69fa93b867e070d"
10 },
11 "dist": {
12 "type": "zip",
13 "url": "https://api.github.com/repos/paragonie/random_compat/zipball/29af24f25bab834fcbb38ad2a69fa93b867e070d",
14 "reference": "29af24f25bab834fcbb38ad2a69fa93b867e070d",
15 "shasum": ""
16 },
17 "require": {
18 "php": ">=5.2.0"
19 },
20 "require-dev": {
21 "phpunit/phpunit": "4.*|5.*"
22 },
23 "suggest": {
24 "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes."
25 },
26 "time": "2018-07-04T16:31:37+00:00",
27 "type": "library",
28 "installation-source": "dist",
29 "autoload": {
30 "files": [
31 "lib/random.php"
32 ]
33 },
34 "notification-url": "https://packagist.org/downloads/",
35 "license": [
36 "MIT"
37 ],
38 "authors": [
39 {
40 "name": "Paragon Initiative Enterprises",
41 "email": "security@paragonie.com",
42 "homepage": "https://paragonie.com"
43 }
44 ],
45 "description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7",
46 "keywords": [
47 "csprng",
48 "polyfill",
49 "pseudorandom",
50 "random"
51 ]
52 },
53 {
54 "name": "paragonie/sodium_compat",
55 "version": "v1.13.0",
56 "version_normalized": "1.13.0.0",
57 "source": {
58 "type": "git",
59 "url": "https://github.com/paragonie/sodium_compat.git",
60 "reference": "bbade402cbe84c69b718120911506a3aa2bae653"
61 },
62 "dist": {
63 "type": "zip",
64 "url": "https://api.github.com/repos/paragonie/sodium_compat/zipball/bbade402cbe84c69b718120911506a3aa2bae653",
65 "reference": "bbade402cbe84c69b718120911506a3aa2bae653",
66 "shasum": ""
67 },
68 "require": {
69 "paragonie/random_compat": ">=1",
70 "php": "^5.2.4|^5.3|^5.4|^5.5|^5.6|^7|^8"
71 },
72 "require-dev": {
73 "phpunit/phpunit": "^3|^4|^5|^6|^7"
74 },
75 "suggest": {
76 "ext-libsodium": "PHP < 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security.",
77 "ext-sodium": "PHP >= 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security."
78 },
79 "time": "2020-03-20T21:48:09+00:00",
80 "type": "library",
81 "installation-source": "dist",
82 "autoload": {
83 "files": [
84 "autoload.php"
85 ]
86 },
87 "notification-url": "https://packagist.org/downloads/",
88 "license": [
89 "ISC"
90 ],
91 "authors": [
92 {
93 "name": "Paragon Initiative Enterprises",
94 "email": "security@paragonie.com"
95 },
96 {
97 "name": "Frank Denis",
98 "email": "jedisct1@pureftpd.org"
99 }
100 ],
101 "description": "Pure PHP implementation of libsodium; uses the PHP extension if it exists",
102 "keywords": [
103 "Authentication",
104 "BLAKE2b",
105 "ChaCha20",
106 "ChaCha20-Poly1305",
107 "Chapoly",
108 "Curve25519",
109 "Ed25519",
110 "EdDSA",
111 "Edwards-curve Digital Signature Algorithm",
112 "Elliptic Curve Diffie-Hellman",
113 "Poly1305",
114 "Pure-PHP cryptography",
115 "RFC 7748",
116 "RFC 8032",
117 "Salpoly",
118 "Salsa20",
119 "X25519",
120 "XChaCha20-Poly1305",
121 "XSalsa20-Poly1305",
122 "Xchacha20",
123 "Xsalsa20",
124 "aead",
125 "cryptography",
126 "ecdh",
127 "elliptic curve",
128 "elliptic curve cryptography",
129 "encryption",
130 "libsodium",
131 "php",
132 "public-key cryptography",
133 "secret-key cryptography",
134 "side-channel resistant"
135 ]
136 }
137 ]
1 <?php return array (
2 'root' =>
3 array (
4 'pretty_version' => 'dev-master',
5 'version' => 'dev-master',
6 'aliases' =>
7 array (
8 ),
9 'reference' => '547ce2234fff039cbd57697d05f7236e431868e5',
10 'name' => '__root__',
11 ),
12 'versions' =>
13 array (
14 '__root__' =>
15 array (
16 'pretty_version' => 'dev-master',
17 'version' => 'dev-master',
18 'aliases' =>
19 array (
20 ),
21 'reference' => '547ce2234fff039cbd57697d05f7236e431868e5',
22 ),
23 'paragonie/random_compat' =>
24 array (
25 'pretty_version' => 'v2.0.10',
26 'version' => '2.0.10.0',
27 'aliases' =>
28 array (
29 ),
30 'reference' => '634bae8e911eefa89c1abfbf1b66da679ac8f54d',
31 ),
32 'paragonie/sodium_compat' =>
33 array (
34 'pretty_version' => 'v1.13.0',
35 'version' => '1.13.0.0',
36 'aliases' =>
37 array (
38 ),
39 'reference' => 'bbade402cbe84c69b718120911506a3aa2bae653',
40 ),
41 ),
42 );
1 <?php
2
3 // platform_check.php @generated by Composer
4
5 $issues = array();
6
7 if (!(PHP_VERSION_ID >= 50204)) {
8 $issues[] = 'Your Composer dependencies require a PHP version ">= 5.2.4". You are running ' . PHP_VERSION . '.';
9 }
10
11 if ($issues) {
12 if (!headers_sent()) {
13 header('HTTP/1.1 500 Internal Server Error');
14 }
15 if (!ini_get('display_errors')) {
16 if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
17 fwrite(STDERR, 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . implode(PHP_EOL, $issues) . PHP_EOL.PHP_EOL);
18 } elseif (!headers_sent()) {
19 echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL;
20 }
21 }
22 trigger_error(
23 'Composer detected issues in your platform: ' . implode(' ', $issues),
24 E_USER_ERROR
25 );
26 }
1 The MIT License (MIT)
2
3 Copyright (c) 2015 Paragon Initiative Enterprises
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 SOFTWARE.
22
1 -----BEGIN PUBLIC KEY-----
2 MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEEd+wCqJDrx5B4OldM0dQE0ZMX+lx1ZWm
3 pui0SUqD4G29L3NGsz9UhJ/0HjBdbnkhIK5xviT0X5vtjacF6ajgcCArbTB+ds+p
4 +h7Q084NuSuIpNb6YPfoUFgC/CL9kAoc
5 -----END PUBLIC KEY-----
1 -----BEGIN PGP SIGNATURE-----
2 Version: GnuPG v2.0.22 (MingW32)
3
4 iQEcBAABAgAGBQJWtW1hAAoJEGuXocKCZATaJf0H+wbZGgskK1dcRTsuVJl9IWip
5 QwGw/qIKI280SD6/ckoUMxKDCJiFuPR14zmqnS36k7N5UNPnpdTJTS8T11jttSpg
6 1LCmgpbEIpgaTah+cELDqFCav99fS+bEiAL5lWDAHBTE/XPjGVCqeehyPYref4IW
7 NDBIEsvnHPHPLsn6X5jq4+Yj5oUixgxaMPiR+bcO4Sh+RzOVB6i2D0upWfRXBFXA
8 NNnsg9/zjvoC7ZW73y9uSH+dPJTt/Vgfeiv52/v41XliyzbUyLalf02GNPY+9goV
9 JHG1ulEEBJOCiUD9cE1PUIJwHA/HqyhHIvV350YoEFiHl8iSwm7SiZu5kPjaq74=
10 =B6+8
11 -----END PGP SIGNATURE-----
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /**
4 * Random_* Compatibility Library
5 * for using the new PHP 7 random_* API in PHP 5 projects
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 if (!is_callable('RandomCompat_strlen')) {
31 if (
32 defined('MB_OVERLOAD_STRING')
33 &&
34 ((int) ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING
35 ) {
36 /**
37 * strlen() implementation that isn't brittle to mbstring.func_overload
38 *
39 * This version uses mb_strlen() in '8bit' mode to treat strings as raw
40 * binary rather than UTF-8, ISO-8859-1, etc
41 *
42 * @param string $binary_string
43 *
44 * @throws TypeError
45 *
46 * @return int
47 */
48 function RandomCompat_strlen($binary_string)
49 {
50 if (!is_string($binary_string)) {
51 throw new TypeError(
52 'RandomCompat_strlen() expects a string'
53 );
54 }
55
56 return (int) mb_strlen($binary_string, '8bit');
57 }
58
59 } else {
60 /**
61 * strlen() implementation that isn't brittle to mbstring.func_overload
62 *
63 * This version just used the default strlen()
64 *
65 * @param string $binary_string
66 *
67 * @throws TypeError
68 *
69 * @return int
70 */
71 function RandomCompat_strlen($binary_string)
72 {
73 if (!is_string($binary_string)) {
74 throw new TypeError(
75 'RandomCompat_strlen() expects a string'
76 );
77 }
78 return (int) strlen($binary_string);
79 }
80 }
81 }
82
83 if (!is_callable('RandomCompat_substr')) {
84
85 if (
86 defined('MB_OVERLOAD_STRING')
87 &&
88 ((int) ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING
89 ) {
90 /**
91 * substr() implementation that isn't brittle to mbstring.func_overload
92 *
93 * This version uses mb_substr() in '8bit' mode to treat strings as raw
94 * binary rather than UTF-8, ISO-8859-1, etc
95 *
96 * @param string $binary_string
97 * @param int $start
98 * @param int|null $length (optional)
99 *
100 * @throws TypeError
101 *
102 * @return string
103 */
104 function RandomCompat_substr($binary_string, $start, $length = null)
105 {
106 if (!is_string($binary_string)) {
107 throw new TypeError(
108 'RandomCompat_substr(): First argument should be a string'
109 );
110 }
111
112 if (!is_int($start)) {
113 throw new TypeError(
114 'RandomCompat_substr(): Second argument should be an integer'
115 );
116 }
117
118 if ($length === null) {
119 /**
120 * mb_substr($str, 0, NULL, '8bit') returns an empty string on
121 * PHP 5.3, so we have to find the length ourselves.
122 */
123 /** @var int $length */
124 $length = RandomCompat_strlen($binary_string) - $start;
125 } elseif (!is_int($length)) {
126 throw new TypeError(
127 'RandomCompat_substr(): Third argument should be an integer, or omitted'
128 );
129 }
130
131 // Consistency with PHP's behavior
132 if ($start === RandomCompat_strlen($binary_string) && $length === 0) {
133 return '';
134 }
135 if ($start > RandomCompat_strlen($binary_string)) {
136 return '';
137 }
138
139 return (string) mb_substr(
140 (string) $binary_string,
141 (int) $start,
142 (int) $length,
143 '8bit'
144 );
145 }
146
147 } else {
148
149 /**
150 * substr() implementation that isn't brittle to mbstring.func_overload
151 *
152 * This version just uses the default substr()
153 *
154 * @param string $binary_string
155 * @param int $start
156 * @param int|null $length (optional)
157 *
158 * @throws TypeError
159 *
160 * @return string
161 */
162 function RandomCompat_substr($binary_string, $start, $length = null)
163 {
164 if (!is_string($binary_string)) {
165 throw new TypeError(
166 'RandomCompat_substr(): First argument should be a string'
167 );
168 }
169
170 if (!is_int($start)) {
171 throw new TypeError(
172 'RandomCompat_substr(): Second argument should be an integer'
173 );
174 }
175
176 if ($length !== null) {
177 if (!is_int($length)) {
178 throw new TypeError(
179 'RandomCompat_substr(): Third argument should be an integer, or omitted'
180 );
181 }
182
183 return (string) substr(
184 (string )$binary_string,
185 (int) $start,
186 (int) $length
187 );
188 }
189
190 return (string) substr(
191 (string) $binary_string,
192 (int) $start
193 );
194 }
195 }
196 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /**
4 * Random_* Compatibility Library
5 * for using the new PHP 7 random_* API in PHP 5 projects
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 if (!is_callable('RandomCompat_intval')) {
31
32 /**
33 * Cast to an integer if we can, safely.
34 *
35 * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX)
36 * (non-inclusive), it will sanely cast it to an int. If you it's equal to
37 * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats
38 * lose precision, so the <= and => operators might accidentally let a float
39 * through.
40 *
41 * @param int|float $number The number we want to convert to an int
42 * @param bool $fail_open Set to true to not throw an exception
43 *
44 * @return float|int
45 * @psalm-suppress InvalidReturnType
46 *
47 * @throws TypeError
48 */
49 function RandomCompat_intval($number, $fail_open = false)
50 {
51 if (is_int($number) || is_float($number)) {
52 $number += 0;
53 } elseif (is_numeric($number)) {
54 /** @psalm-suppress InvalidOperand */
55 $number += 0;
56 }
57 /** @var int|float $number */
58
59 if (
60 is_float($number)
61 &&
62 $number > ~PHP_INT_MAX
63 &&
64 $number < PHP_INT_MAX
65 ) {
66 $number = (int) $number;
67 }
68
69 if (is_int($number)) {
70 return (int) $number;
71 } elseif (!$fail_open) {
72 throw new TypeError(
73 'Expected an integer.'
74 );
75 }
76 return $number;
77 }
78 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /**
4 * Random_* Compatibility Library
5 * for using the new PHP 7 random_* API in PHP 5 projects
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 if (!class_exists('Error', false)) {
31 // We can't really avoid making this extend Exception in PHP 5.
32 class Error extends Exception
33 {
34
35 }
36 }
37
38 if (!class_exists('TypeError', false)) {
39 if (is_subclass_of('Error', 'Exception')) {
40 class TypeError extends Error
41 {
42
43 }
44 } else {
45 class TypeError extends Exception
46 {
47
48 }
49 }
50 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /**
4 * Random_* Compatibility Library
5 * for using the new PHP 7 random_* API in PHP 5 projects
6 *
7 * @version 2.0.17
8 * @released 2018-07-04
9 *
10 * The MIT License (MIT)
11 *
12 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33 if (!defined('PHP_VERSION_ID')) {
34 // This constant was introduced in PHP 5.2.7
35 $RandomCompatversion = array_map('intval', explode('.', PHP_VERSION));
36 define(
37 'PHP_VERSION_ID',
38 $RandomCompatversion[0] * 10000
39 + $RandomCompatversion[1] * 100
40 + $RandomCompatversion[2]
41 );
42 $RandomCompatversion = null;
43 }
44
45 /**
46 * PHP 7.0.0 and newer have these functions natively.
47 */
48 if (PHP_VERSION_ID >= 70000) {
49 return;
50 }
51
52 if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
53 define('RANDOM_COMPAT_READ_BUFFER', 8);
54 }
55
56 $RandomCompatDIR = dirname(__FILE__);
57
58 require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'byte_safe_strings.php';
59 require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'cast_to_int.php';
60 require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'error_polyfill.php';
61
62 if (!is_callable('random_bytes')) {
63 /**
64 * PHP 5.2.0 - 5.6.x way to implement random_bytes()
65 *
66 * We use conditional statements here to define the function in accordance
67 * to the operating environment. It's a micro-optimization.
68 *
69 * In order of preference:
70 * 1. Use libsodium if available.
71 * 2. fread() /dev/urandom if available (never on Windows)
72 * 3. mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM)
73 * 4. COM('CAPICOM.Utilities.1')->GetRandom()
74 *
75 * See RATIONALE.md for our reasoning behind this particular order
76 */
77 if (extension_loaded('libsodium')) {
78 // See random_bytes_libsodium.php
79 if (PHP_VERSION_ID >= 50300 && is_callable('\\Sodium\\randombytes_buf')) {
80 require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_libsodium.php';
81 } elseif (method_exists('Sodium', 'randombytes_buf')) {
82 require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_libsodium_legacy.php';
83 }
84 }
85
86 /**
87 * Reading directly from /dev/urandom:
88 */
89 if (DIRECTORY_SEPARATOR === '/') {
90 // DIRECTORY_SEPARATOR === '/' on Unix-like OSes -- this is a fast
91 // way to exclude Windows.
92 $RandomCompatUrandom = true;
93 $RandomCompat_basedir = ini_get('open_basedir');
94
95 if (!empty($RandomCompat_basedir)) {
96 $RandomCompat_open_basedir = explode(
97 PATH_SEPARATOR,
98 strtolower($RandomCompat_basedir)
99 );
100 $RandomCompatUrandom = (array() !== array_intersect(
101 array('/dev', '/dev/', '/dev/urandom'),
102 $RandomCompat_open_basedir
103 ));
104 $RandomCompat_open_basedir = null;
105 }
106
107 if (
108 !is_callable('random_bytes')
109 &&
110 $RandomCompatUrandom
111 &&
112 @is_readable('/dev/urandom')
113 ) {
114 // Error suppression on is_readable() in case of an open_basedir
115 // or safe_mode failure. All we care about is whether or not we
116 // can read it at this point. If the PHP environment is going to
117 // panic over trying to see if the file can be read in the first
118 // place, that is not helpful to us here.
119
120 // See random_bytes_dev_urandom.php
121 require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_dev_urandom.php';
122 }
123 // Unset variables after use
124 $RandomCompat_basedir = null;
125 } else {
126 $RandomCompatUrandom = false;
127 }
128
129 /**
130 * mcrypt_create_iv()
131 *
132 * We only want to use mcypt_create_iv() if:
133 *
134 * - random_bytes() hasn't already been defined
135 * - the mcrypt extensions is loaded
136 * - One of these two conditions is true:
137 * - We're on Windows (DIRECTORY_SEPARATOR !== '/')
138 * - We're not on Windows and /dev/urandom is readabale
139 * (i.e. we're not in a chroot jail)
140 * - Special case:
141 * - If we're not on Windows, but the PHP version is between
142 * 5.6.10 and 5.6.12, we don't want to use mcrypt. It will
143 * hang indefinitely. This is bad.
144 * - If we're on Windows, we want to use PHP >= 5.3.7 or else
145 * we get insufficient entropy errors.
146 */
147 if (
148 !is_callable('random_bytes')
149 &&
150 // Windows on PHP < 5.3.7 is broken, but non-Windows is not known to be.
151 (DIRECTORY_SEPARATOR === '/' || PHP_VERSION_ID >= 50307)
152 &&
153 // Prevent this code from hanging indefinitely on non-Windows;
154 // see https://bugs.php.net/bug.php?id=69833
155 (
156 DIRECTORY_SEPARATOR !== '/' ||
157 (PHP_VERSION_ID <= 50609 || PHP_VERSION_ID >= 50613)
158 )
159 &&
160 extension_loaded('mcrypt')
161 ) {
162 // See random_bytes_mcrypt.php
163 require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_mcrypt.php';
164 }
165 $RandomCompatUrandom = null;
166
167 /**
168 * This is a Windows-specific fallback, for when the mcrypt extension
169 * isn't loaded.
170 */
171 if (
172 !is_callable('random_bytes')
173 &&
174 extension_loaded('com_dotnet')
175 &&
176 class_exists('COM')
177 ) {
178 $RandomCompat_disabled_classes = preg_split(
179 '#\s*,\s*#',
180 strtolower(ini_get('disable_classes'))
181 );
182
183 if (!in_array('com', $RandomCompat_disabled_classes)) {
184 try {
185 $RandomCompatCOMtest = new COM('CAPICOM.Utilities.1');
186 if (method_exists($RandomCompatCOMtest, 'GetRandom')) {
187 // See random_bytes_com_dotnet.php
188 require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_bytes_com_dotnet.php';
189 }
190 } catch (com_exception $e) {
191 // Don't try to use it.
192 }
193 }
194 $RandomCompat_disabled_classes = null;
195 $RandomCompatCOMtest = null;
196 }
197
198 /**
199 * throw new Exception
200 */
201 if (!is_callable('random_bytes')) {
202 /**
203 * We don't have any more options, so let's throw an exception right now
204 * and hope the developer won't let it fail silently.
205 *
206 * @param mixed $length
207 * @psalm-suppress InvalidReturnType
208 * @throws Exception
209 * @return string
210 */
211 function random_bytes($length)
212 {
213 unset($length); // Suppress "variable not used" warnings.
214 throw new Exception(
215 'There is no suitable CSPRNG installed on your system'
216 );
217 return '';
218 }
219 }
220 }
221
222 if (!is_callable('random_int')) {
223 require_once $RandomCompatDIR . DIRECTORY_SEPARATOR . 'random_int.php';
224 }
225
226 $RandomCompatDIR = null;
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /**
4 * Random_* Compatibility Library
5 * for using the new PHP 7 random_* API in PHP 5 projects
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 if (!is_callable('random_bytes')) {
31 /**
32 * Windows with PHP < 5.3.0 will not have the function
33 * openssl_random_pseudo_bytes() available, so let's use
34 * CAPICOM to work around this deficiency.
35 *
36 * @param int $bytes
37 *
38 * @throws Exception
39 *
40 * @return string
41 */
42 function random_bytes($bytes)
43 {
44 try {
45 /** @var int $bytes */
46 $bytes = RandomCompat_intval($bytes);
47 } catch (TypeError $ex) {
48 throw new TypeError(
49 'random_bytes(): $bytes must be an integer'
50 );
51 }
52
53 if ($bytes < 1) {
54 throw new Error(
55 'Length must be greater than 0'
56 );
57 }
58
59 /** @var string $buf */
60 $buf = '';
61 if (!class_exists('COM')) {
62 throw new Error(
63 'COM does not exist'
64 );
65 }
66 /** @var COM $util */
67 $util = new COM('CAPICOM.Utilities.1');
68 $execCount = 0;
69
70 /**
71 * Let's not let it loop forever. If we run N times and fail to
72 * get N bytes of random data, then CAPICOM has failed us.
73 */
74 do {
75 $buf .= base64_decode((string) $util->GetRandom($bytes, 0));
76 if (RandomCompat_strlen($buf) >= $bytes) {
77 /**
78 * Return our random entropy buffer here:
79 */
80 return (string) RandomCompat_substr($buf, 0, $bytes);
81 }
82 ++$execCount;
83 } while ($execCount < $bytes);
84
85 /**
86 * If we reach here, PHP has failed us.
87 */
88 throw new Exception(
89 'Could not gather sufficient random data'
90 );
91 }
92 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /**
4 * Random_* Compatibility Library
5 * for using the new PHP 7 random_* API in PHP 5 projects
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
31 define('RANDOM_COMPAT_READ_BUFFER', 8);
32 }
33
34 if (!is_callable('random_bytes')) {
35 /**
36 * Unless open_basedir is enabled, use /dev/urandom for
37 * random numbers in accordance with best practices
38 *
39 * Why we use /dev/urandom and not /dev/random
40 * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers
41 *
42 * @param int $bytes
43 *
44 * @throws Exception
45 *
46 * @return string
47 */
48 function random_bytes($bytes)
49 {
50 /** @var resource $fp */
51 static $fp = null;
52
53 /**
54 * This block should only be run once
55 */
56 if (empty($fp)) {
57 /**
58 * We use /dev/urandom if it is a char device.
59 * We never fall back to /dev/random
60 */
61 /** @var resource|bool $fp */
62 $fp = fopen('/dev/urandom', 'rb');
63 if (is_resource($fp)) {
64 /** @var array<string, int> $st */
65 $st = fstat($fp);
66 if (($st['mode'] & 0170000) !== 020000) {
67 fclose($fp);
68 $fp = false;
69 }
70 }
71
72 if (is_resource($fp)) {
73 /**
74 * stream_set_read_buffer() does not exist in HHVM
75 *
76 * If we don't set the stream's read buffer to 0, PHP will
77 * internally buffer 8192 bytes, which can waste entropy
78 *
79 * stream_set_read_buffer returns 0 on success
80 */
81 if (is_callable('stream_set_read_buffer')) {
82 stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER);
83 }
84 if (is_callable('stream_set_chunk_size')) {
85 stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER);
86 }
87 }
88 }
89
90 try {
91 /** @var int $bytes */
92 $bytes = RandomCompat_intval($bytes);
93 } catch (TypeError $ex) {
94 throw new TypeError(
95 'random_bytes(): $bytes must be an integer'
96 );
97 }
98
99 if ($bytes < 1) {
100 throw new Error(
101 'Length must be greater than 0'
102 );
103 }
104
105 /**
106 * This if() block only runs if we managed to open a file handle
107 *
108 * It does not belong in an else {} block, because the above
109 * if (empty($fp)) line is logic that should only be run once per
110 * page load.
111 */
112 if (is_resource($fp)) {
113 /**
114 * @var int
115 */
116 $remaining = $bytes;
117
118 /**
119 * @var string|bool
120 */
121 $buf = '';
122
123 /**
124 * We use fread() in a loop to protect against partial reads
125 */
126 do {
127 /**
128 * @var string|bool
129 */
130 $read = fread($fp, $remaining);
131 if (!is_string($read)) {
132 if ($read === false) {
133 /**
134 * We cannot safely read from the file. Exit the
135 * do-while loop and trigger the exception condition
136 *
137 * @var string|bool
138 */
139 $buf = false;
140 break;
141 }
142 }
143 /**
144 * Decrease the number of bytes returned from remaining
145 */
146 $remaining -= RandomCompat_strlen($read);
147 /**
148 * @var string|bool
149 */
150 $buf = $buf . $read;
151 } while ($remaining > 0);
152
153 /**
154 * Is our result valid?
155 */
156 if (is_string($buf)) {
157 if (RandomCompat_strlen($buf) === $bytes) {
158 /**
159 * Return our random entropy buffer here:
160 */
161 return $buf;
162 }
163 }
164 }
165
166 /**
167 * If we reach here, PHP has failed us.
168 */
169 throw new Exception(
170 'Error reading from source device'
171 );
172 }
173 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /**
4 * Random_* Compatibility Library
5 * for using the new PHP 7 random_* API in PHP 5 projects
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 if (!is_callable('random_bytes')) {
31 /**
32 * If the libsodium PHP extension is loaded, we'll use it above any other
33 * solution.
34 *
35 * libsodium-php project:
36 * @ref https://github.com/jedisct1/libsodium-php
37 *
38 * @param int $bytes
39 *
40 * @throws Exception
41 *
42 * @return string
43 */
44 function random_bytes($bytes)
45 {
46 try {
47 /** @var int $bytes */
48 $bytes = RandomCompat_intval($bytes);
49 } catch (TypeError $ex) {
50 throw new TypeError(
51 'random_bytes(): $bytes must be an integer'
52 );
53 }
54
55 if ($bytes < 1) {
56 throw new Error(
57 'Length must be greater than 0'
58 );
59 }
60
61 /**
62 * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
63 * generated in one invocation.
64 */
65 /** @var string|bool $buf */
66 if ($bytes > 2147483647) {
67 $buf = '';
68 for ($i = 0; $i < $bytes; $i += 1073741824) {
69 $n = ($bytes - $i) > 1073741824
70 ? 1073741824
71 : $bytes - $i;
72 $buf .= \Sodium\randombytes_buf($n);
73 }
74 } else {
75 /** @var string|bool $buf */
76 $buf = \Sodium\randombytes_buf($bytes);
77 }
78
79 if (is_string($buf)) {
80 if (RandomCompat_strlen($buf) === $bytes) {
81 return $buf;
82 }
83 }
84
85 /**
86 * If we reach here, PHP has failed us.
87 */
88 throw new Exception(
89 'Could not gather sufficient random data'
90 );
91 }
92 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /**
4 * Random_* Compatibility Library
5 * for using the new PHP 7 random_* API in PHP 5 projects
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 if (!is_callable('random_bytes')) {
31 /**
32 * If the libsodium PHP extension is loaded, we'll use it above any other
33 * solution.
34 *
35 * libsodium-php project:
36 * @ref https://github.com/jedisct1/libsodium-php
37 *
38 * @param int $bytes
39 *
40 * @throws Exception
41 *
42 * @return string
43 */
44 function random_bytes($bytes)
45 {
46 try {
47 /** @var int $bytes */
48 $bytes = RandomCompat_intval($bytes);
49 } catch (TypeError $ex) {
50 throw new TypeError(
51 'random_bytes(): $bytes must be an integer'
52 );
53 }
54
55 if ($bytes < 1) {
56 throw new Error(
57 'Length must be greater than 0'
58 );
59 }
60
61 /**
62 * @var string
63 */
64 $buf = '';
65
66 /**
67 * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
68 * generated in one invocation.
69 */
70 if ($bytes > 2147483647) {
71 for ($i = 0; $i < $bytes; $i += 1073741824) {
72 $n = ($bytes - $i) > 1073741824
73 ? 1073741824
74 : $bytes - $i;
75 $buf .= Sodium::randombytes_buf((int) $n);
76 }
77 } else {
78 $buf .= Sodium::randombytes_buf((int) $bytes);
79 }
80
81 if (is_string($buf)) {
82 if (RandomCompat_strlen($buf) === $bytes) {
83 return $buf;
84 }
85 }
86
87 /**
88 * If we reach here, PHP has failed us.
89 */
90 throw new Exception(
91 'Could not gather sufficient random data'
92 );
93 }
94 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /**
4 * Random_* Compatibility Library
5 * for using the new PHP 7 random_* API in PHP 5 projects
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 if (!is_callable('random_bytes')) {
31 /**
32 * Powered by ext/mcrypt (and thankfully NOT libmcrypt)
33 *
34 * @ref https://bugs.php.net/bug.php?id=55169
35 * @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386
36 *
37 * @param int $bytes
38 *
39 * @throws Exception
40 *
41 * @return string
42 */
43 function random_bytes($bytes)
44 {
45 try {
46 /** @var int $bytes */
47 $bytes = RandomCompat_intval($bytes);
48 } catch (TypeError $ex) {
49 throw new TypeError(
50 'random_bytes(): $bytes must be an integer'
51 );
52 }
53
54 if ($bytes < 1) {
55 throw new Error(
56 'Length must be greater than 0'
57 );
58 }
59
60 /** @var string|bool $buf */
61 $buf = @mcrypt_create_iv((int) $bytes, (int) MCRYPT_DEV_URANDOM);
62 if (
63 is_string($buf)
64 &&
65 RandomCompat_strlen($buf) === $bytes
66 ) {
67 /**
68 * Return our random entropy buffer here:
69 */
70 return $buf;
71 }
72
73 /**
74 * If we reach here, PHP has failed us.
75 */
76 throw new Exception(
77 'Could not gather sufficient random data'
78 );
79 }
80 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (!is_callable('random_int')) {
5 /**
6 * Random_* Compatibility Library
7 * for using the new PHP 7 random_* API in PHP 5 projects
8 *
9 * The MIT License (MIT)
10 *
11 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * SOFTWARE.
30 */
31
32 /**
33 * Fetch a random integer between $min and $max inclusive
34 *
35 * @param int $min
36 * @param int $max
37 *
38 * @throws Exception
39 *
40 * @return int
41 */
42 function random_int($min, $max)
43 {
44 /**
45 * Type and input logic checks
46 *
47 * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX)
48 * (non-inclusive), it will sanely cast it to an int. If you it's equal to
49 * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats
50 * lose precision, so the <= and => operators might accidentally let a float
51 * through.
52 */
53
54 try {
55 /** @var int $min */
56 $min = RandomCompat_intval($min);
57 } catch (TypeError $ex) {
58 throw new TypeError(
59 'random_int(): $min must be an integer'
60 );
61 }
62
63 try {
64 /** @var int $max */
65 $max = RandomCompat_intval($max);
66 } catch (TypeError $ex) {
67 throw new TypeError(
68 'random_int(): $max must be an integer'
69 );
70 }
71
72 /**
73 * Now that we've verified our weak typing system has given us an integer,
74 * let's validate the logic then we can move forward with generating random
75 * integers along a given range.
76 */
77 if ($min > $max) {
78 throw new Error(
79 'Minimum value must be less than or equal to the maximum value'
80 );
81 }
82
83 if ($max === $min) {
84 return (int) $min;
85 }
86
87 /**
88 * Initialize variables to 0
89 *
90 * We want to store:
91 * $bytes => the number of random bytes we need
92 * $mask => an integer bitmask (for use with the &) operator
93 * so we can minimize the number of discards
94 */
95 $attempts = $bits = $bytes = $mask = $valueShift = 0;
96 /** @var int $attempts */
97 /** @var int $bits */
98 /** @var int $bytes */
99 /** @var int $mask */
100 /** @var int $valueShift */
101
102 /**
103 * At this point, $range is a positive number greater than 0. It might
104 * overflow, however, if $max - $min > PHP_INT_MAX. PHP will cast it to
105 * a float and we will lose some precision.
106 *
107 * @var int|float $range
108 */
109 $range = $max - $min;
110
111 /**
112 * Test for integer overflow:
113 */
114 if (!is_int($range)) {
115
116 /**
117 * Still safely calculate wider ranges.
118 * Provided by @CodesInChaos, @oittaa
119 *
120 * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435
121 *
122 * We use ~0 as a mask in this case because it generates all 1s
123 *
124 * @ref https://eval.in/400356 (32-bit)
125 * @ref http://3v4l.org/XX9r5 (64-bit)
126 */
127 $bytes = PHP_INT_SIZE;
128 /** @var int $mask */
129 $mask = ~0;
130
131 } else {
132
133 /**
134 * $bits is effectively ceil(log($range, 2)) without dealing with
135 * type juggling
136 */
137 while ($range > 0) {
138 if ($bits % 8 === 0) {
139 ++$bytes;
140 }
141 ++$bits;
142 $range >>= 1;
143 /** @var int $mask */
144 $mask = $mask << 1 | 1;
145 }
146 $valueShift = $min;
147 }
148
149 /** @var int $val */
150 $val = 0;
151 /**
152 * Now that we have our parameters set up, let's begin generating
153 * random integers until one falls between $min and $max
154 */
155 /** @psalm-suppress RedundantCondition */
156 do {
157 /**
158 * The rejection probability is at most 0.5, so this corresponds
159 * to a failure probability of 2^-128 for a working RNG
160 */
161 if ($attempts > 128) {
162 throw new Exception(
163 'random_int: RNG is broken - too many rejections'
164 );
165 }
166
167 /**
168 * Let's grab the necessary number of random bytes
169 */
170 $randomByteString = random_bytes($bytes);
171
172 /**
173 * Let's turn $randomByteString into an integer
174 *
175 * This uses bitwise operators (<< and |) to build an integer
176 * out of the values extracted from ord()
177 *
178 * Example: [9F] | [6D] | [32] | [0C] =>
179 * 159 + 27904 + 3276800 + 201326592 =>
180 * 204631455
181 */
182 $val &= 0;
183 for ($i = 0; $i < $bytes; ++$i) {
184 $val |= ord($randomByteString[$i]) << ($i * 8);
185 }
186 /** @var int $val */
187
188 /**
189 * Apply mask
190 */
191 $val &= $mask;
192 $val += $valueShift;
193
194 ++$attempts;
195 /**
196 * If $val overflows to a floating point number,
197 * ... or is larger than $max,
198 * ... or smaller than $min,
199 * then try again.
200 */
201 } while (!is_int($val) || $val > $max || $val < $min);
202
203 return (int) $val;
204 }
205 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 require_once 'lib/byte_safe_strings.php';
5 require_once 'lib/cast_to_int.php';
6 require_once 'lib/error_polyfill.php';
7 require_once 'other/ide_stubs/libsodium.php';
8 require_once 'lib/random.php';
9
10 $int = random_int(0, 65536);
1 /*
2 * ISC License
3 *
4 * Copyright (c) 2016-2019
5 * Paragon Initiative Enterprises <security at paragonie dot com>
6 *
7 * Copyright (c) 2013-2019
8 * Frank Denis <j at pureftpd dot org>
9 *
10 * Permission to use, copy, modify, and/or distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
...\ No newline at end of file ...\ No newline at end of file
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 require_once 'autoload.php';
5 ParagonIE_Sodium_Compat::$fastMult = true;
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 require_once 'autoload.php';
5 define('DO_PEDANTIC_TEST', true);
6
7 ParagonIE_Sodium_Compat::$fastMult = true;
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 /*
4 This file should only ever be loaded on PHP 7+
5 */
6 if (PHP_VERSION_ID < 70000) {
7 return;
8 }
9
10 spl_autoload_register(function ($class) {
11 $namespace = 'ParagonIE_Sodium_';
12 // Does the class use the namespace prefix?
13 $len = strlen($namespace);
14 if (strncmp($namespace, $class, $len) !== 0) {
15 // no, move to the next registered autoloader
16 return false;
17 }
18
19 // Get the relative class name
20 $relative_class = substr($class, $len);
21
22 // Replace the namespace prefix with the base directory, replace namespace
23 // separators with directory separators in the relative class name, append
24 // with .php
25 $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
26 // if the file exists, require it
27 if (file_exists($file)) {
28 require_once $file;
29 return true;
30 }
31 return false;
32 });
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 require_once (dirname(__FILE__) . '/vendor/autoload.php');
5
6 if (PHP_VERSION_ID >= 50300) {
7 require_once (dirname(__FILE__) . '/tests/phpunit-shim.php');
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (PHP_VERSION_ID < 70000) {
5 if (!is_callable('sodiumCompatAutoloader')) {
6 /**
7 * Sodium_Compat autoloader.
8 *
9 * @param string $class Class name to be autoloaded.
10 *
11 * @return bool Stop autoloading?
12 */
13 function sodiumCompatAutoloader($class)
14 {
15 $namespace = 'ParagonIE_Sodium_';
16 // Does the class use the namespace prefix?
17 $len = strlen($namespace);
18 if (strncmp($namespace, $class, $len) !== 0) {
19 // no, move to the next registered autoloader
20 return false;
21 }
22
23 // Get the relative class name
24 $relative_class = substr($class, $len);
25
26 // Replace the namespace prefix with the base directory, replace namespace
27 // separators with directory separators in the relative class name, append
28 // with .php
29 $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
30 // if the file exists, require it
31 if (file_exists($file)) {
32 require_once $file;
33 return true;
34 }
35 return false;
36 }
37
38 // Now that we have an autoloader, let's register it!
39 spl_autoload_register('sodiumCompatAutoloader');
40 }
41 } else {
42 require_once dirname(__FILE__) . '/autoload-php7.php';
43 }
44
45 if (!class_exists('SodiumException', false)) {
46 require_once dirname(__FILE__) . '/src/SodiumException.php';
47 }
48 if (PHP_VERSION_ID >= 50300) {
49 // Namespaces didn't exist before 5.3.0, so don't even try to use this
50 // unless PHP >= 5.3.0
51 require_once dirname(__FILE__) . '/lib/namespaced.php';
52 require_once dirname(__FILE__) . '/lib/sodium_compat.php';
53 } else {
54 require_once dirname(__FILE__) . '/src/PHP52/SplFixedArray.php';
55 }
56 if (PHP_VERSION_ID < 70200 || !extension_loaded('sodium')) {
57 if (PHP_VERSION_ID >= 50300 && !defined('SODIUM_CRYPTO_SCALARMULT_BYTES')) {
58 require_once dirname(__FILE__) . '/lib/php72compat_const.php';
59 }
60 if (PHP_VERSION_ID >= 70000) {
61 assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?');
62 } else {
63 assert(class_exists('ParagonIE_Sodium_Compat'));
64 }
65 require_once (dirname(__FILE__) . '/lib/php72compat.php');
66 }
1 {
2 "name": "paragonie/sodium_compat",
3 "description": "Pure PHP implementation of libsodium; uses the PHP extension if it exists",
4 "keywords": [
5 "PHP",
6 "cryptography",
7 "elliptic curve",
8 "elliptic curve cryptography",
9 "Pure-PHP cryptography",
10 "side-channel resistant",
11 "Curve25519",
12 "X25519",
13 "ECDH",
14 "Elliptic Curve Diffie-Hellman",
15 "Ed25519",
16 "RFC 7748",
17 "RFC 8032",
18 "EdDSA",
19 "Edwards-curve Digital Signature Algorithm",
20 "ChaCha20",
21 "Salsa20",
22 "Xchacha20",
23 "Xsalsa20",
24 "Poly1305",
25 "BLAKE2b",
26 "public-key cryptography",
27 "secret-key cryptography",
28 "AEAD",
29 "Chapoly",
30 "Salpoly",
31 "ChaCha20-Poly1305",
32 "XSalsa20-Poly1305",
33 "XChaCha20-Poly1305",
34 "encryption",
35 "authentication",
36 "libsodium"
37 ],
38 "license": "ISC",
39 "authors": [
40 {
41 "name": "Paragon Initiative Enterprises",
42 "email": "security@paragonie.com"
43 },
44 {
45 "name": "Frank Denis",
46 "email": "jedisct1@pureftpd.org"
47 }
48 ],
49 "autoload": {
50 "files": ["autoload.php"]
51 },
52 "repositories": [
53 {
54 "type": "git",
55 "url": "https://github.com/garex/phpunit"
56 },
57 {
58 "type": "git",
59 "url": "https://github.com/garex/phpunit-mock-objects"
60 }
61 ],
62 "require": {
63 "php": "^5.2.4|^5.3|^5.4|^5.5|^5.6|^7|^8",
64 "xrstf/composer-php52": "1.*",
65 "paragonie/random_compat": ">=1"
66 },
67 "minimum-stability": "dev",
68 "require-dev": {
69 "phpunit/phpunit-php52": "dev-3.6.12-php52",
70 "phpunit/phpunit-mock-objects-php52": "dev-1.1.0-php52"
71 },
72 "scripts": {
73 "post-install-cmd": [
74 "xrstf\\Composer52\\Generator::onPostInstallCmd"
75 ],
76 "post-update-cmd": [
77 "xrstf\\Composer52\\Generator::onPostInstallCmd"
78 ],
79 "post-autoload-dump": [
80 "xrstf\\Composer52\\Generator::onPostInstallCmd"
81 ]
82 },
83 "suggest": {
84 "ext-libsodium": "PHP < 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security.",
85 "ext-sodium": "PHP >= 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security."
86 }
87 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace Sodium;
4
5 require_once dirname(dirname(__FILE__)) . '/autoload.php';
6
7 use ParagonIE_Sodium_Compat;
8
9 const CRYPTO_AEAD_AES256GCM_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_AES256GCM_KEYBYTES;
10 const CRYPTO_AEAD_AES256GCM_NSECBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_AES256GCM_NSECBYTES;
11 const CRYPTO_AEAD_AES256GCM_NPUBBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_AES256GCM_NPUBBYTES;
12 const CRYPTO_AEAD_AES256GCM_ABYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_AES256GCM_ABYTES;
13 const CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES;
14 const CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES;
15 const CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES;
16 const CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES;
17 const CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES;
18 const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES;
19 const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES;
20 const CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES;
21 const CRYPTO_AUTH_BYTES = ParagonIE_Sodium_Compat::CRYPTO_AUTH_BYTES;
22 const CRYPTO_AUTH_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_AUTH_KEYBYTES;
23 const CRYPTO_BOX_SEALBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_SEALBYTES;
24 const CRYPTO_BOX_SECRETKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES;
25 const CRYPTO_BOX_PUBLICKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES;
26 const CRYPTO_BOX_KEYPAIRBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES;
27 const CRYPTO_BOX_MACBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_MACBYTES;
28 const CRYPTO_BOX_NONCEBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES;
29 const CRYPTO_BOX_SEEDBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_SEEDBYTES;
30 const CRYPTO_KX_BYTES = ParagonIE_Sodium_Compat::CRYPTO_KX_BYTES;
31 const CRYPTO_KX_SEEDBYTES = ParagonIE_Sodium_Compat::CRYPTO_KX_SEEDBYTES;
32 const CRYPTO_KX_PUBLICKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_KX_PUBLICKEYBYTES;
33 const CRYPTO_KX_SECRETKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_KX_SECRETKEYBYTES;
34 const CRYPTO_GENERICHASH_BYTES = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES;
35 const CRYPTO_GENERICHASH_BYTES_MIN = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN;
36 const CRYPTO_GENERICHASH_BYTES_MAX = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX;
37 const CRYPTO_GENERICHASH_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES;
38 const CRYPTO_GENERICHASH_KEYBYTES_MIN = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN;
39 const CRYPTO_GENERICHASH_KEYBYTES_MAX = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX;
40 const CRYPTO_SCALARMULT_BYTES = ParagonIE_Sodium_Compat::CRYPTO_SCALARMULT_BYTES;
41 const CRYPTO_SCALARMULT_SCALARBYTES = ParagonIE_Sodium_Compat::CRYPTO_SCALARMULT_SCALARBYTES;
42 const CRYPTO_SHORTHASH_BYTES = ParagonIE_Sodium_Compat::CRYPTO_SHORTHASH_BYTES;
43 const CRYPTO_SHORTHASH_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_SHORTHASH_KEYBYTES;
44 const CRYPTO_SECRETBOX_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES;
45 const CRYPTO_SECRETBOX_MACBYTES = ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES;
46 const CRYPTO_SECRETBOX_NONCEBYTES = ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES;
47 const CRYPTO_SIGN_BYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES;
48 const CRYPTO_SIGN_SEEDBYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_SEEDBYTES;
49 const CRYPTO_SIGN_PUBLICKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES;
50 const CRYPTO_SIGN_SECRETKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES;
51 const CRYPTO_SIGN_KEYPAIRBYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_KEYPAIRBYTES;
52 const CRYPTO_STREAM_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_STREAM_KEYBYTES;
53 const CRYPTO_STREAM_NONCEBYTES = ParagonIE_Sodium_Compat::CRYPTO_STREAM_NONCEBYTES;
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 require_once dirname(dirname(__FILE__)) . '/autoload.php';
5
6 if (PHP_VERSION_ID < 50300) {
7 return;
8 }
9
10 /*
11 * This file is just for convenience, to allow developers to reduce verbosity when
12 * they add this project to their libraries.
13 *
14 * Replace this:
15 *
16 * $x = ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_encrypt(...$args);
17 *
18 * with this:
19 *
20 * use ParagonIE\Sodium\Compat;
21 *
22 * $x = Compat::crypto_aead_xchacha20poly1305_encrypt(...$args);
23 */
24 spl_autoload_register(function ($class) {
25 if ($class[0] === '\\') {
26 $class = substr($class, 1);
27 }
28 $namespace = 'ParagonIE\\Sodium';
29 // Does the class use the namespace prefix?
30 $len = strlen($namespace);
31 if (strncmp($namespace, $class, $len) !== 0) {
32 // no, move to the next registered autoloader
33 return false;
34 }
35
36 // Get the relative class name
37 $relative_class = substr($class, $len);
38
39 // Replace the namespace prefix with the base directory, replace namespace
40 // separators with directory separators in the relative class name, append
41 // with .php
42 $file = dirname(dirname(__FILE__)) . '/namespaced/' . str_replace('\\', '/', $relative_class) . '.php';
43 // if the file exists, require it
44 if (file_exists($file)) {
45 require_once $file;
46 return true;
47 }
48 return false;
49 });
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 const SODIUM_LIBRARY_MAJOR_VERSION = 9;
5 const SODIUM_LIBRARY_MINOR_VERSION = 1;
6 const SODIUM_LIBRARY_VERSION = '1.0.8';
7
8 const SODIUM_BASE64_VARIANT_ORIGINAL = 1;
9 const SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING = 3;
10 const SODIUM_BASE64_VARIANT_URLSAFE = 5;
11 const SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING = 7;
12 const SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES = 32;
13 const SODIUM_CRYPTO_AEAD_AES256GCM_NSECBYTES = 0;
14 const SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12;
15 const SODIUM_CRYPTO_AEAD_AES256GCM_ABYTES = 16;
16 const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32;
17 const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0;
18 const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8;
19 const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16;
20 const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32;
21 const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = 0;
22 const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12;
23 const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = 16;
24 const SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES = 32;
25 const SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES = 0;
26 const SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES = 24;
27 const SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES = 16;
28 const SODIUM_CRYPTO_AUTH_BYTES = 32;
29 const SODIUM_CRYPTO_AUTH_KEYBYTES = 32;
30 const SODIUM_CRYPTO_BOX_SEALBYTES = 16;
31 const SODIUM_CRYPTO_BOX_SECRETKEYBYTES = 32;
32 const SODIUM_CRYPTO_BOX_PUBLICKEYBYTES = 32;
33 const SODIUM_CRYPTO_BOX_KEYPAIRBYTES = 64;
34 const SODIUM_CRYPTO_BOX_MACBYTES = 16;
35 const SODIUM_CRYPTO_BOX_NONCEBYTES = 24;
36 const SODIUM_CRYPTO_BOX_SEEDBYTES = 32;
37 const SODIUM_CRYPTO_KDF_BYTES_MIN = 16;
38 const SODIUM_CRYPTO_KDF_BYTES_MAX = 64;
39 const SODIUM_CRYPTO_KDF_CONTEXTBYTES = 8;
40 const SODIUM_CRYPTO_KDF_KEYBYTES = 32;
41 const SODIUM_CRYPTO_KX_BYTES = 32;
42 const SODIUM_CRYPTO_KX_PRIMITIVE = 'x25519blake2b';
43 const SODIUM_CRYPTO_KX_SEEDBYTES = 32;
44 const SODIUM_CRYPTO_KX_KEYPAIRBYTES = 64;
45 const SODIUM_CRYPTO_KX_PUBLICKEYBYTES = 32;
46 const SODIUM_CRYPTO_KX_SECRETKEYBYTES = 32;
47 const SODIUM_CRYPTO_KX_SESSIONKEYBYTES = 32;
48 const SODIUM_CRYPTO_GENERICHASH_BYTES = 32;
49 const SODIUM_CRYPTO_GENERICHASH_BYTES_MIN = 16;
50 const SODIUM_CRYPTO_GENERICHASH_BYTES_MAX = 64;
51 const SODIUM_CRYPTO_GENERICHASH_KEYBYTES = 32;
52 const SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MIN = 16;
53 const SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MAX = 64;
54 const SODIUM_CRYPTO_PWHASH_SALTBYTES = 16;
55 const SODIUM_CRYPTO_PWHASH_STRPREFIX = '$argon2id$';
56 const SODIUM_CRYPTO_PWHASH_ALG_ARGON2I13 = 1;
57 const SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13 = 2;
58 const SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 33554432;
59 const SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 4;
60 const SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE = 134217728;
61 const SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE = 6;
62 const SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 536870912;
63 const SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 8;
64 const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES = 32;
65 const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX = '$7$';
66 const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE = 534288;
67 const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE = 16777216;
68 const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE = 33554432;
69 const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE = 1073741824;
70 const SODIUM_CRYPTO_SCALARMULT_BYTES = 32;
71 const SODIUM_CRYPTO_SCALARMULT_SCALARBYTES = 32;
72 const SODIUM_CRYPTO_SHORTHASH_BYTES = 8;
73 const SODIUM_CRYPTO_SHORTHASH_KEYBYTES = 16;
74 const SODIUM_CRYPTO_SECRETBOX_KEYBYTES = 32;
75 const SODIUM_CRYPTO_SECRETBOX_MACBYTES = 16;
76 const SODIUM_CRYPTO_SECRETBOX_NONCEBYTES = 24;
77 const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES = 17;
78 const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES = 24;
79 const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES = 32;
80 const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH = 0;
81 const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL = 1;
82 const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY = 2;
83 const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL = 3;
84 const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX = 0x3fffffff80;
85 const SODIUM_CRYPTO_SIGN_BYTES = 64;
86 const SODIUM_CRYPTO_SIGN_SEEDBYTES = 32;
87 const SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES = 32;
88 const SODIUM_CRYPTO_SIGN_SECRETKEYBYTES = 64;
89 const SODIUM_CRYPTO_SIGN_KEYPAIRBYTES = 96;
90 const SODIUM_CRYPTO_STREAM_KEYBYTES = 32;
91 const SODIUM_CRYPTO_STREAM_NONCEBYTES = 24;
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium;
4
5 class Compat extends \ParagonIE_Sodium_Compat
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class BLAKE2b extends \ParagonIE_Sodium_Core_BLAKE2b
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class ChaCha20 extends \ParagonIE_Sodium_Core_ChaCha20
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\ChaCha20;
4
5 class Ctx extends \ParagonIE_Sodium_Core_ChaCha20_Ctx
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\ChaCha20;
4
5 class IetfCtx extends \ParagonIE_Sodium_Core_ChaCha20_IetfCtx
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class Curve25519 extends \ParagonIE_Sodium_Core_Curve25519
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\Curve25519;
4
5 class Fe extends \ParagonIE_Sodium_Core_Curve25519_Fe
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\Curve25519\Ge;
4
5 class Cached extends \ParagonIE_Sodium_Core_Curve25519_Ge_Cached
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\Curve25519\Ge;
4
5 class P1p1 extends \ParagonIE_Sodium_Core_Curve25519_Ge_P1p1
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\Curve25519\Ge;
4
5 class P2 extends \ParagonIE_Sodium_Core_Curve25519_Ge_P2
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\Curve25519\Ge;
4
5 class P3 extends \ParagonIE_Sodium_Core_Curve25519_Ge_P3
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\Curve25519\Ge;
4
5 class Precomp extends \ParagonIE_Sodium_Core_Curve25519_Ge_Precomp
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\Curve25519;
4
5 class H extends \ParagonIE_Sodium_Core_Curve25519_H
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class Ed25519 extends \ParagonIE_Sodium_Core_Ed25519
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class HChaCha20 extends \ParagonIE_Sodium_Core_HChaCha20
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class HSalsa20 extends \ParagonIE_Sodium_Core_HSalsa20
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class Poly1305 extends \ParagonIE_Sodium_Core_Poly1305
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core\Poly1305;
4
5 class State extends \ParagonIE_Sodium_Core_Poly1305_State
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class Salsa20 extends \ParagonIE_Sodium_Core_Salsa20
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class SipHash extends \ParagonIE_Sodium_Core_SipHash
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class Util extends \ParagonIE_Sodium_Core_Util
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class X25519 extends \ParagonIE_Sodium_Core_X25519
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class XChaCha20 extends \ParagonIE_Sodium_Core_XChaCha20
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium\Core;
4
5 class Xsalsa20 extends \ParagonIE_Sodium_Core_XSalsa20
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium;
4
5 class Crypto extends \ParagonIE_Sodium_Crypto
6 {
7
8 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3 namespace ParagonIE\Sodium;
4
5 class File extends \ParagonIE_Sodium_File
6 {
7
8 }
1 <?xml version="1.0"?>
2 <psalm
3 useDocblockTypes="true"
4 totallyTyped="true"
5 >
6 <projectFiles>
7 <directory name="src" />
8 </projectFiles>
9 <issueHandlers>
10 <InvalidFunctionCall errorLevel="suppress" />
11 <!--
12 Previous issue type is suppressed because we have to use a string
13 as a function name for PHP 5.2 compatibility. When testing, be sure
14 to change this to "info".
15 -->
16
17 <UndefinedFunction errorLevel="info" />
18 <!--
19 Inconsistently erroneous.
20 -->
21
22 <DuplicateClass errorLevel="info" />
23 <!--
24 Psalm isn't correctly identifying the guard conditions that return
25 early if a class already exists.
26 -->
27 <RedundantConditionGivenDocblockType errorLevel="suppress" />
28
29 <TooFewArguments errorLevel="info" />
30
31 <DocblockTypeContradiction errorLevel="suppress" />
32 <RedundantCondition errorLevel="info" />
33 <!--
34 Redundancy is good for PHP <7
35 -->
36
37 <TypeDoesNotContainType errorLevel="info" />
38 <!--
39 This mostly fails on `PHP_INT_SIZE === 4`
40 -->
41
42 <InternalMethod errorLevel="suppress" />
43 <!--
44 We mark methods as internal.
45 -->
46 <PossiblyNullArgument errorLevel="suppress" />
47 <!--
48 Not interesting
49 -->
50
51 </issueHandlers>
52 </psalm>
1 <?xml version="1.0"?>
2 <psalm
3 useDocblockTypes="true"
4 totallyTyped="true"
5 >
6 <projectFiles>
7 <directory name="src" />
8 </projectFiles>
9 <issueHandlers>
10 <InvalidFunctionCall errorLevel="suppress" />
11 <!--
12 Previous issue type is suppressed because we have to use a string
13 as a function name for PHP 5.2 compatibility. When testing, be sure
14 to change this to "info".
15 -->
16
17 <UndefinedFunction errorLevel="info" />
18 <!--
19 Inconsistently erroneous.
20 -->
21
22 <DuplicateClass errorLevel="info" />
23 <!--
24 Psalm isn't correctly identifying the guard conditions that return
25 early if a class already exists.
26 -->
27 <RedundantConditionGivenDocblockType errorLevel="suppress" />
28
29 <TooFewArguments errorLevel="info" />
30
31 <DocblockTypeContradiction errorLevel="info" />
32 <RedundantCondition errorLevel="info" />
33 <!--
34 Redundancy is good for PHP <7
35 -->
36
37 <TypeDoesNotContainType errorLevel="info" />
38 <!--
39 This mostly fails on `PHP_INT_SIZE === 4`
40 -->
41
42 </issueHandlers>
43 </psalm>
This diff could not be displayed because it is too large.
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 /**
5 * Class ParagonIE_Sodium_Core_Base64
6 *
7 * Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
8 * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
9 *
10 * We have to copy/paste the contents into the variant files because PHP 5.2
11 * doesn't support late static binding, and we have no better workaround
12 * available that won't break PHP 7+. Therefore, we're forced to duplicate code.
13 */
14 abstract class ParagonIE_Sodium_Core_Base64_Common
15 {
16 /**
17 * Encode into Base64
18 *
19 * Base64 character set "[A-Z][a-z][0-9]+/"
20 *
21 * @param string $src
22 * @return string
23 * @throws TypeError
24 */
25 public static function encode($src)
26 {
27 return self::doEncode($src, true);
28 }
29
30 /**
31 * Encode into Base64, no = padding
32 *
33 * Base64 character set "[A-Z][a-z][0-9]+/"
34 *
35 * @param string $src
36 * @return string
37 * @throws TypeError
38 */
39 public static function encodeUnpadded($src)
40 {
41 return self::doEncode($src, false);
42 }
43
44 /**
45 * @param string $src
46 * @param bool $pad Include = padding?
47 * @return string
48 * @throws TypeError
49 */
50 protected static function doEncode($src, $pad = true)
51 {
52 $dest = '';
53 $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
54 // Main loop (no padding):
55 for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
56 /** @var array<int, int> $chunk */
57 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3));
58 $b0 = $chunk[1];
59 $b1 = $chunk[2];
60 $b2 = $chunk[3];
61
62 $dest .=
63 self::encode6Bits( $b0 >> 2 ) .
64 self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
65 self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
66 self::encode6Bits( $b2 & 63);
67 }
68 // The last chunk, which may have padding:
69 if ($i < $srcLen) {
70 /** @var array<int, int> $chunk */
71 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
72 $b0 = $chunk[1];
73 if ($i + 1 < $srcLen) {
74 $b1 = $chunk[2];
75 $dest .=
76 self::encode6Bits($b0 >> 2) .
77 self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
78 self::encode6Bits(($b1 << 2) & 63);
79 if ($pad) {
80 $dest .= '=';
81 }
82 } else {
83 $dest .=
84 self::encode6Bits( $b0 >> 2) .
85 self::encode6Bits(($b0 << 4) & 63);
86 if ($pad) {
87 $dest .= '==';
88 }
89 }
90 }
91 return $dest;
92 }
93
94 /**
95 * decode from base64 into binary
96 *
97 * Base64 character set "./[A-Z][a-z][0-9]"
98 *
99 * @param string $src
100 * @param bool $strictPadding
101 * @return string
102 * @throws RangeException
103 * @throws TypeError
104 * @psalm-suppress RedundantCondition
105 */
106 public static function decode($src, $strictPadding = false)
107 {
108 // Remove padding
109 $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
110 if ($srcLen === 0) {
111 return '';
112 }
113
114 if ($strictPadding) {
115 if (($srcLen & 3) === 0) {
116 if ($src[$srcLen - 1] === '=') {
117 $srcLen--;
118 if ($src[$srcLen - 1] === '=') {
119 $srcLen--;
120 }
121 }
122 }
123 if (($srcLen & 3) === 1) {
124 throw new RangeException(
125 'Incorrect padding'
126 );
127 }
128 if ($src[$srcLen - 1] === '=') {
129 throw new RangeException(
130 'Incorrect padding'
131 );
132 }
133 } else {
134 $src = rtrim($src, '=');
135 $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
136 }
137
138 $err = 0;
139 $dest = '';
140 // Main loop (no padding):
141 for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
142 /** @var array<int, int> $chunk */
143 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4));
144 $c0 = self::decode6Bits($chunk[1]);
145 $c1 = self::decode6Bits($chunk[2]);
146 $c2 = self::decode6Bits($chunk[3]);
147 $c3 = self::decode6Bits($chunk[4]);
148
149 $dest .= pack(
150 'CCC',
151 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
152 ((($c1 << 4) | ($c2 >> 2)) & 0xff),
153 ((($c2 << 6) | $c3 ) & 0xff)
154 );
155 $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
156 }
157 // The last chunk, which may have padding:
158 if ($i < $srcLen) {
159 /** @var array<int, int> $chunk */
160 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
161 $c0 = self::decode6Bits($chunk[1]);
162
163 if ($i + 2 < $srcLen) {
164 $c1 = self::decode6Bits($chunk[2]);
165 $c2 = self::decode6Bits($chunk[3]);
166 $dest .= pack(
167 'CC',
168 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
169 ((($c1 << 4) | ($c2 >> 2)) & 0xff)
170 );
171 $err |= ($c0 | $c1 | $c2) >> 8;
172 } elseif ($i + 1 < $srcLen) {
173 $c1 = self::decode6Bits($chunk[2]);
174 $dest .= pack(
175 'C',
176 ((($c0 << 2) | ($c1 >> 4)) & 0xff)
177 );
178 $err |= ($c0 | $c1) >> 8;
179 } elseif ($i < $srcLen && $strictPadding) {
180 $err |= 1;
181 }
182 }
183 /** @var bool $check */
184 $check = ($err === 0);
185 if (!$check) {
186 throw new RangeException(
187 'Base64::decode() only expects characters in the correct base64 alphabet'
188 );
189 }
190 return $dest;
191 }
192
193 /**
194 * Uses bitwise operators instead of table-lookups to turn 6-bit integers
195 * into 8-bit integers.
196 *
197 * Base64 character set:
198 * [A-Z] [a-z] [0-9] + /
199 * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
200 *
201 * @param int $src
202 * @return int
203 */
204 abstract protected static function decode6Bits($src);
205
206 /**
207 * Uses bitwise operators instead of table-lookups to turn 8-bit integers
208 * into 6-bit integers.
209 *
210 * @param int $src
211 * @return string
212 */
213 abstract protected static function encode6Bits($src);
214 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 /**
5 * Class ParagonIE_Sodium_Core_Base64
6 *
7 * Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
8 * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
9 */
10 class ParagonIE_Sodium_Core_Base64_Original
11 {
12 // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE
13 /**
14 * Encode into Base64
15 *
16 * Base64 character set "[A-Z][a-z][0-9]+/"
17 *
18 * @param string $src
19 * @return string
20 * @throws TypeError
21 */
22 public static function encode($src)
23 {
24 return self::doEncode($src, true);
25 }
26
27 /**
28 * Encode into Base64, no = padding
29 *
30 * Base64 character set "[A-Z][a-z][0-9]+/"
31 *
32 * @param string $src
33 * @return string
34 * @throws TypeError
35 */
36 public static function encodeUnpadded($src)
37 {
38 return self::doEncode($src, false);
39 }
40
41 /**
42 * @param string $src
43 * @param bool $pad Include = padding?
44 * @return string
45 * @throws TypeError
46 */
47 protected static function doEncode($src, $pad = true)
48 {
49 $dest = '';
50 $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
51 // Main loop (no padding):
52 for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
53 /** @var array<int, int> $chunk */
54 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3));
55 $b0 = $chunk[1];
56 $b1 = $chunk[2];
57 $b2 = $chunk[3];
58
59 $dest .=
60 self::encode6Bits( $b0 >> 2 ) .
61 self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
62 self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
63 self::encode6Bits( $b2 & 63);
64 }
65 // The last chunk, which may have padding:
66 if ($i < $srcLen) {
67 /** @var array<int, int> $chunk */
68 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
69 $b0 = $chunk[1];
70 if ($i + 1 < $srcLen) {
71 $b1 = $chunk[2];
72 $dest .=
73 self::encode6Bits($b0 >> 2) .
74 self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
75 self::encode6Bits(($b1 << 2) & 63);
76 if ($pad) {
77 $dest .= '=';
78 }
79 } else {
80 $dest .=
81 self::encode6Bits( $b0 >> 2) .
82 self::encode6Bits(($b0 << 4) & 63);
83 if ($pad) {
84 $dest .= '==';
85 }
86 }
87 }
88 return $dest;
89 }
90
91 /**
92 * decode from base64 into binary
93 *
94 * Base64 character set "./[A-Z][a-z][0-9]"
95 *
96 * @param string $src
97 * @param bool $strictPadding
98 * @return string
99 * @throws RangeException
100 * @throws TypeError
101 * @psalm-suppress RedundantCondition
102 */
103 public static function decode($src, $strictPadding = false)
104 {
105 // Remove padding
106 $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
107 if ($srcLen === 0) {
108 return '';
109 }
110
111 if ($strictPadding) {
112 if (($srcLen & 3) === 0) {
113 if ($src[$srcLen - 1] === '=') {
114 $srcLen--;
115 if ($src[$srcLen - 1] === '=') {
116 $srcLen--;
117 }
118 }
119 }
120 if (($srcLen & 3) === 1) {
121 throw new RangeException(
122 'Incorrect padding'
123 );
124 }
125 if ($src[$srcLen - 1] === '=') {
126 throw new RangeException(
127 'Incorrect padding'
128 );
129 }
130 } else {
131 $src = rtrim($src, '=');
132 $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
133 }
134
135 $err = 0;
136 $dest = '';
137 // Main loop (no padding):
138 for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
139 /** @var array<int, int> $chunk */
140 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4));
141 $c0 = self::decode6Bits($chunk[1]);
142 $c1 = self::decode6Bits($chunk[2]);
143 $c2 = self::decode6Bits($chunk[3]);
144 $c3 = self::decode6Bits($chunk[4]);
145
146 $dest .= pack(
147 'CCC',
148 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
149 ((($c1 << 4) | ($c2 >> 2)) & 0xff),
150 ((($c2 << 6) | $c3) & 0xff)
151 );
152 $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
153 }
154 // The last chunk, which may have padding:
155 if ($i < $srcLen) {
156 /** @var array<int, int> $chunk */
157 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
158 $c0 = self::decode6Bits($chunk[1]);
159
160 if ($i + 2 < $srcLen) {
161 $c1 = self::decode6Bits($chunk[2]);
162 $c2 = self::decode6Bits($chunk[3]);
163 $dest .= pack(
164 'CC',
165 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
166 ((($c1 << 4) | ($c2 >> 2)) & 0xff)
167 );
168 $err |= ($c0 | $c1 | $c2) >> 8;
169 } elseif ($i + 1 < $srcLen) {
170 $c1 = self::decode6Bits($chunk[2]);
171 $dest .= pack(
172 'C',
173 ((($c0 << 2) | ($c1 >> 4)) & 0xff)
174 );
175 $err |= ($c0 | $c1) >> 8;
176 } elseif ($i < $srcLen && $strictPadding) {
177 $err |= 1;
178 }
179 }
180 /** @var bool $check */
181 $check = ($err === 0);
182 if (!$check) {
183 throw new RangeException(
184 'Base64::decode() only expects characters in the correct base64 alphabet'
185 );
186 }
187 return $dest;
188 }
189 // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE
190
191 /**
192 * Uses bitwise operators instead of table-lookups to turn 6-bit integers
193 * into 8-bit integers.
194 *
195 * Base64 character set:
196 * [A-Z] [a-z] [0-9] + /
197 * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
198 *
199 * @param int $src
200 * @return int
201 */
202 protected static function decode6Bits($src)
203 {
204 $ret = -1;
205
206 // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
207 $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
208
209 // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
210 $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
211
212 // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
213 $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
214
215 // if ($src == 0x2b) $ret += 62 + 1;
216 $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63;
217
218 // if ($src == 0x2f) ret += 63 + 1;
219 $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64;
220
221 return $ret;
222 }
223
224 /**
225 * Uses bitwise operators instead of table-lookups to turn 8-bit integers
226 * into 6-bit integers.
227 *
228 * @param int $src
229 * @return string
230 */
231 protected static function encode6Bits($src)
232 {
233 $diff = 0x41;
234
235 // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
236 $diff += ((25 - $src) >> 8) & 6;
237
238 // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
239 $diff -= ((51 - $src) >> 8) & 75;
240
241 // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15
242 $diff -= ((61 - $src) >> 8) & 15;
243
244 // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3
245 $diff += ((62 - $src) >> 8) & 3;
246
247 return pack('C', $src + $diff);
248 }
249 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 /**
5 * Class ParagonIE_Sodium_Core_Base64UrlSafe
6 *
7 * Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
8 * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
9 */
10 class ParagonIE_Sodium_Core_Base64_UrlSafe
11 {
12 // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE
13 /**
14 * Encode into Base64
15 *
16 * Base64 character set "[A-Z][a-z][0-9]+/"
17 *
18 * @param string $src
19 * @return string
20 * @throws TypeError
21 */
22 public static function encode($src)
23 {
24 return self::doEncode($src, true);
25 }
26
27 /**
28 * Encode into Base64, no = padding
29 *
30 * Base64 character set "[A-Z][a-z][0-9]+/"
31 *
32 * @param string $src
33 * @return string
34 * @throws TypeError
35 */
36 public static function encodeUnpadded($src)
37 {
38 return self::doEncode($src, false);
39 }
40
41 /**
42 * @param string $src
43 * @param bool $pad Include = padding?
44 * @return string
45 * @throws TypeError
46 */
47 protected static function doEncode($src, $pad = true)
48 {
49 $dest = '';
50 $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
51 // Main loop (no padding):
52 for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
53 /** @var array<int, int> $chunk */
54 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3));
55 $b0 = $chunk[1];
56 $b1 = $chunk[2];
57 $b2 = $chunk[3];
58
59 $dest .=
60 self::encode6Bits( $b0 >> 2 ) .
61 self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
62 self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
63 self::encode6Bits( $b2 & 63);
64 }
65 // The last chunk, which may have padding:
66 if ($i < $srcLen) {
67 /** @var array<int, int> $chunk */
68 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
69 $b0 = $chunk[1];
70 if ($i + 1 < $srcLen) {
71 $b1 = $chunk[2];
72 $dest .=
73 self::encode6Bits($b0 >> 2) .
74 self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
75 self::encode6Bits(($b1 << 2) & 63);
76 if ($pad) {
77 $dest .= '=';
78 }
79 } else {
80 $dest .=
81 self::encode6Bits( $b0 >> 2) .
82 self::encode6Bits(($b0 << 4) & 63);
83 if ($pad) {
84 $dest .= '==';
85 }
86 }
87 }
88 return $dest;
89 }
90
91 /**
92 * decode from base64 into binary
93 *
94 * Base64 character set "./[A-Z][a-z][0-9]"
95 *
96 * @param string $src
97 * @param bool $strictPadding
98 * @return string
99 * @throws RangeException
100 * @throws TypeError
101 * @psalm-suppress RedundantCondition
102 */
103 public static function decode($src, $strictPadding = false)
104 {
105 // Remove padding
106 $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
107 if ($srcLen === 0) {
108 return '';
109 }
110
111 if ($strictPadding) {
112 if (($srcLen & 3) === 0) {
113 if ($src[$srcLen - 1] === '=') {
114 $srcLen--;
115 if ($src[$srcLen - 1] === '=') {
116 $srcLen--;
117 }
118 }
119 }
120 if (($srcLen & 3) === 1) {
121 throw new RangeException(
122 'Incorrect padding'
123 );
124 }
125 if ($src[$srcLen - 1] === '=') {
126 throw new RangeException(
127 'Incorrect padding'
128 );
129 }
130 } else {
131 $src = rtrim($src, '=');
132 $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
133 }
134
135 $err = 0;
136 $dest = '';
137 // Main loop (no padding):
138 for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
139 /** @var array<int, int> $chunk */
140 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4));
141 $c0 = self::decode6Bits($chunk[1]);
142 $c1 = self::decode6Bits($chunk[2]);
143 $c2 = self::decode6Bits($chunk[3]);
144 $c3 = self::decode6Bits($chunk[4]);
145
146 $dest .= pack(
147 'CCC',
148 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
149 ((($c1 << 4) | ($c2 >> 2)) & 0xff),
150 ((($c2 << 6) | $c3) & 0xff)
151 );
152 $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
153 }
154 // The last chunk, which may have padding:
155 if ($i < $srcLen) {
156 /** @var array<int, int> $chunk */
157 $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
158 $c0 = self::decode6Bits($chunk[1]);
159
160 if ($i + 2 < $srcLen) {
161 $c1 = self::decode6Bits($chunk[2]);
162 $c2 = self::decode6Bits($chunk[3]);
163 $dest .= pack(
164 'CC',
165 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
166 ((($c1 << 4) | ($c2 >> 2)) & 0xff)
167 );
168 $err |= ($c0 | $c1 | $c2) >> 8;
169 } elseif ($i + 1 < $srcLen) {
170 $c1 = self::decode6Bits($chunk[2]);
171 $dest .= pack(
172 'C',
173 ((($c0 << 2) | ($c1 >> 4)) & 0xff)
174 );
175 $err |= ($c0 | $c1) >> 8;
176 } elseif ($i < $srcLen && $strictPadding) {
177 $err |= 1;
178 }
179 }
180 /** @var bool $check */
181 $check = ($err === 0);
182 if (!$check) {
183 throw new RangeException(
184 'Base64::decode() only expects characters in the correct base64 alphabet'
185 );
186 }
187 return $dest;
188 }
189 // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE
190 /**
191 * Uses bitwise operators instead of table-lookups to turn 6-bit integers
192 * into 8-bit integers.
193 *
194 * Base64 character set:
195 * [A-Z] [a-z] [0-9] + /
196 * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
197 *
198 * @param int $src
199 * @return int
200 */
201 protected static function decode6Bits($src)
202 {
203 $ret = -1;
204
205 // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
206 $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
207
208 // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
209 $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
210
211 // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
212 $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
213
214 // if ($src == 0x2c) $ret += 62 + 1;
215 $ret += (((0x2c - $src) & ($src - 0x2e)) >> 8) & 63;
216
217 // if ($src == 0x5f) ret += 63 + 1;
218 $ret += (((0x5e - $src) & ($src - 0x60)) >> 8) & 64;
219
220 return $ret;
221 }
222
223 /**
224 * Uses bitwise operators instead of table-lookups to turn 8-bit integers
225 * into 6-bit integers.
226 *
227 * @param int $src
228 * @return string
229 */
230 protected static function encode6Bits($src)
231 {
232 $diff = 0x41;
233
234 // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
235 $diff += ((25 - $src) >> 8) & 6;
236
237 // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
238 $diff -= ((51 - $src) >> 8) & 75;
239
240 // if ($src > 61) $diff += 0x2d - 0x30 - 10; // -13
241 $diff -= ((61 - $src) >> 8) & 13;
242
243 // if ($src > 62) $diff += 0x5f - 0x2b - 1; // 3
244 $diff += ((62 - $src) >> 8) & 49;
245
246 return pack('C', $src + $diff);
247 }
248 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_ChaCha20_Ctx', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_ChaCha20_Ctx
10 */
11 class ParagonIE_Sodium_Core_ChaCha20_Ctx extends ParagonIE_Sodium_Core_Util implements ArrayAccess
12 {
13 /**
14 * @var SplFixedArray internally, <int, int>
15 */
16 protected $container;
17
18 /**
19 * ParagonIE_Sodium_Core_ChaCha20_Ctx constructor.
20 *
21 * @internal You should not use this directly from another application
22 *
23 * @param string $key ChaCha20 key.
24 * @param string $iv Initialization Vector (a.k.a. nonce).
25 * @param string $counter The initial counter value.
26 * Defaults to 8 0x00 bytes.
27 * @throws InvalidArgumentException
28 * @throws TypeError
29 */
30 public function __construct($key = '', $iv = '', $counter = '')
31 {
32 if (self::strlen($key) !== 32) {
33 throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.');
34 }
35 if (self::strlen($iv) !== 8) {
36 throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.');
37 }
38 $this->container = new SplFixedArray(16);
39
40 /* "expand 32-byte k" as per ChaCha20 spec */
41 $this->container[0] = 0x61707865;
42 $this->container[1] = 0x3320646e;
43 $this->container[2] = 0x79622d32;
44 $this->container[3] = 0x6b206574;
45 $this->container[4] = self::load_4(self::substr($key, 0, 4));
46 $this->container[5] = self::load_4(self::substr($key, 4, 4));
47 $this->container[6] = self::load_4(self::substr($key, 8, 4));
48 $this->container[7] = self::load_4(self::substr($key, 12, 4));
49 $this->container[8] = self::load_4(self::substr($key, 16, 4));
50 $this->container[9] = self::load_4(self::substr($key, 20, 4));
51 $this->container[10] = self::load_4(self::substr($key, 24, 4));
52 $this->container[11] = self::load_4(self::substr($key, 28, 4));
53
54 if (empty($counter)) {
55 $this->container[12] = 0;
56 $this->container[13] = 0;
57 } else {
58 $this->container[12] = self::load_4(self::substr($counter, 0, 4));
59 $this->container[13] = self::load_4(self::substr($counter, 4, 4));
60 }
61 $this->container[14] = self::load_4(self::substr($iv, 0, 4));
62 $this->container[15] = self::load_4(self::substr($iv, 4, 4));
63 }
64
65 /**
66 * @internal You should not use this directly from another application
67 *
68 * @param int $offset
69 * @param int $value
70 * @return void
71 * @psalm-suppress MixedArrayOffset
72 */
73 public function offsetSet($offset, $value)
74 {
75 if (!is_int($offset)) {
76 throw new InvalidArgumentException('Expected an integer');
77 }
78 if (!is_int($value)) {
79 throw new InvalidArgumentException('Expected an integer');
80 }
81 $this->container[$offset] = $value;
82 }
83
84 /**
85 * @internal You should not use this directly from another application
86 *
87 * @param int $offset
88 * @return bool
89 */
90 public function offsetExists($offset)
91 {
92 return isset($this->container[$offset]);
93 }
94
95 /**
96 * @internal You should not use this directly from another application
97 *
98 * @param int $offset
99 * @return void
100 * @psalm-suppress MixedArrayOffset
101 */
102 public function offsetUnset($offset)
103 {
104 unset($this->container[$offset]);
105 }
106
107 /**
108 * @internal You should not use this directly from another application
109 *
110 * @param int $offset
111 * @return mixed|null
112 * @psalm-suppress MixedArrayOffset
113 */
114 public function offsetGet($offset)
115 {
116 return isset($this->container[$offset])
117 ? $this->container[$offset]
118 : null;
119 }
120 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_ChaCha20_IetfCtx
10 */
11 class ParagonIE_Sodium_Core_ChaCha20_IetfCtx extends ParagonIE_Sodium_Core_ChaCha20_Ctx
12 {
13 /**
14 * ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor.
15 *
16 * @internal You should not use this directly from another application
17 *
18 * @param string $key ChaCha20 key.
19 * @param string $iv Initialization Vector (a.k.a. nonce).
20 * @param string $counter The initial counter value.
21 * Defaults to 4 0x00 bytes.
22 * @throws InvalidArgumentException
23 * @throws TypeError
24 */
25 public function __construct($key = '', $iv = '', $counter = '')
26 {
27 if (self::strlen($iv) !== 12) {
28 throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.');
29 }
30 parent::__construct($key, self::substr($iv, 0, 8), $counter);
31
32 if (!empty($counter)) {
33 $this->container[12] = self::load_4(self::substr($counter, 0, 4));
34 }
35 $this->container[13] = self::load_4(self::substr($iv, 0, 4));
36 $this->container[14] = self::load_4(self::substr($iv, 4, 4));
37 $this->container[15] = self::load_4(self::substr($iv, 8, 4));
38 }
39 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_Curve25519_Fe', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_Curve25519_Fe
10 *
11 * This represents a Field Element
12 */
13 class ParagonIE_Sodium_Core_Curve25519_Fe implements ArrayAccess
14 {
15 /**
16 * @var array<int, int>
17 */
18 protected $container = array();
19
20 /**
21 * @var int
22 */
23 protected $size = 10;
24
25 /**
26 * @internal You should not use this directly from another application
27 *
28 * @param array<int, int> $array
29 * @param bool $save_indexes
30 * @return self
31 */
32 public static function fromArray($array, $save_indexes = null)
33 {
34 $count = count($array);
35 if ($save_indexes) {
36 $keys = array_keys($array);
37 } else {
38 $keys = range(0, $count - 1);
39 }
40 $array = array_values($array);
41 /** @var array<int, int> $keys */
42
43 $obj = new ParagonIE_Sodium_Core_Curve25519_Fe();
44 if ($save_indexes) {
45 for ($i = 0; $i < $count; ++$i) {
46 $obj->offsetSet($keys[$i], $array[$i]);
47 }
48 } else {
49 for ($i = 0; $i < $count; ++$i) {
50 $obj->offsetSet($i, $array[$i]);
51 }
52 }
53 return $obj;
54 }
55
56 /**
57 * @internal You should not use this directly from another application
58 *
59 * @param int|null $offset
60 * @param int $value
61 * @return void
62 * @psalm-suppress MixedArrayOffset
63 */
64 public function offsetSet($offset, $value)
65 {
66 if (!is_int($value)) {
67 throw new InvalidArgumentException('Expected an integer');
68 }
69 if (is_null($offset)) {
70 $this->container[] = $value;
71 } else {
72 $this->container[$offset] = $value;
73 }
74 }
75
76 /**
77 * @internal You should not use this directly from another application
78 *
79 * @param int $offset
80 * @return bool
81 * @psalm-suppress MixedArrayOffset
82 */
83 public function offsetExists($offset)
84 {
85 return isset($this->container[$offset]);
86 }
87
88 /**
89 * @internal You should not use this directly from another application
90 *
91 * @param int $offset
92 * @return void
93 * @psalm-suppress MixedArrayOffset
94 */
95 public function offsetUnset($offset)
96 {
97 unset($this->container[$offset]);
98 }
99
100 /**
101 * @internal You should not use this directly from another application
102 *
103 * @param int $offset
104 * @return int
105 * @psalm-suppress MixedArrayOffset
106 */
107 public function offsetGet($offset)
108 {
109 if (!isset($this->container[$offset])) {
110 $this->container[$offset] = 0;
111 }
112 return (int) ($this->container[$offset]);
113 }
114
115 /**
116 * @internal You should not use this directly from another application
117 *
118 * @return array
119 */
120 public function __debugInfo()
121 {
122 return array(implode(', ', $this->container));
123 }
124 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4
5 if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Cached', false)) {
6 return;
7 }
8 /**
9 * Class ParagonIE_Sodium_Core_Curve25519_Ge_Cached
10 */
11 class ParagonIE_Sodium_Core_Curve25519_Ge_Cached
12 {
13 /**
14 * @var ParagonIE_Sodium_Core_Curve25519_Fe
15 */
16 public $YplusX;
17
18 /**
19 * @var ParagonIE_Sodium_Core_Curve25519_Fe
20 */
21 public $YminusX;
22
23 /**
24 * @var ParagonIE_Sodium_Core_Curve25519_Fe
25 */
26 public $Z;
27
28 /**
29 * @var ParagonIE_Sodium_Core_Curve25519_Fe
30 */
31 public $T2d;
32
33 /**
34 * ParagonIE_Sodium_Core_Curve25519_Ge_Cached constructor.
35 *
36 * @internal You should not use this directly from another application
37 *
38 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YplusX
39 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YminusX
40 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $Z
41 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $T2d
42 */
43 public function __construct(
44 ParagonIE_Sodium_Core_Curve25519_Fe $YplusX = null,
45 ParagonIE_Sodium_Core_Curve25519_Fe $YminusX = null,
46 ParagonIE_Sodium_Core_Curve25519_Fe $Z = null,
47 ParagonIE_Sodium_Core_Curve25519_Fe $T2d = null
48 ) {
49 if ($YplusX === null) {
50 $YplusX = new ParagonIE_Sodium_Core_Curve25519_Fe();
51 }
52 $this->YplusX = $YplusX;
53 if ($YminusX === null) {
54 $YminusX = new ParagonIE_Sodium_Core_Curve25519_Fe();
55 }
56 $this->YminusX = $YminusX;
57 if ($Z === null) {
58 $Z = new ParagonIE_Sodium_Core_Curve25519_Fe();
59 }
60 $this->Z = $Z;
61 if ($T2d === null) {
62 $T2d = new ParagonIE_Sodium_Core_Curve25519_Fe();
63 }
64 $this->T2d = $T2d;
65 }
66 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P1p1', false)) {
5 return;
6 }
7 /**
8 * Class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1
9 */
10 class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1
11 {
12 /**
13 * @var ParagonIE_Sodium_Core_Curve25519_Fe
14 */
15 public $X;
16
17 /**
18 * @var ParagonIE_Sodium_Core_Curve25519_Fe
19 */
20 public $Y;
21
22 /**
23 * @var ParagonIE_Sodium_Core_Curve25519_Fe
24 */
25 public $Z;
26
27 /**
28 * @var ParagonIE_Sodium_Core_Curve25519_Fe
29 */
30 public $T;
31
32 /**
33 * ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 constructor.
34 *
35 * @internal You should not use this directly from another application
36 *
37 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x
38 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y
39 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z
40 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t
41 */
42 public function __construct(
43 ParagonIE_Sodium_Core_Curve25519_Fe $x = null,
44 ParagonIE_Sodium_Core_Curve25519_Fe $y = null,
45 ParagonIE_Sodium_Core_Curve25519_Fe $z = null,
46 ParagonIE_Sodium_Core_Curve25519_Fe $t = null
47 ) {
48 if ($x === null) {
49 $x = new ParagonIE_Sodium_Core_Curve25519_Fe();
50 }
51 $this->X = $x;
52 if ($y === null) {
53 $y = new ParagonIE_Sodium_Core_Curve25519_Fe();
54 }
55 $this->Y = $y;
56 if ($z === null) {
57 $z = new ParagonIE_Sodium_Core_Curve25519_Fe();
58 }
59 $this->Z = $z;
60 if ($t === null) {
61 $t = new ParagonIE_Sodium_Core_Curve25519_Fe();
62 }
63 $this->T = $t;
64 }
65 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P2', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_Curve25519_Ge_P2
10 */
11 class ParagonIE_Sodium_Core_Curve25519_Ge_P2
12 {
13 /**
14 * @var ParagonIE_Sodium_Core_Curve25519_Fe
15 */
16 public $X;
17
18 /**
19 * @var ParagonIE_Sodium_Core_Curve25519_Fe
20 */
21 public $Y;
22
23 /**
24 * @var ParagonIE_Sodium_Core_Curve25519_Fe
25 */
26 public $Z;
27
28 /**
29 * ParagonIE_Sodium_Core_Curve25519_Ge_P2 constructor.
30 *
31 * @internal You should not use this directly from another application
32 *
33 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x
34 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y
35 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z
36 */
37 public function __construct(
38 ParagonIE_Sodium_Core_Curve25519_Fe $x = null,
39 ParagonIE_Sodium_Core_Curve25519_Fe $y = null,
40 ParagonIE_Sodium_Core_Curve25519_Fe $z = null
41 ) {
42 if ($x === null) {
43 $x = new ParagonIE_Sodium_Core_Curve25519_Fe();
44 }
45 $this->X = $x;
46 if ($y === null) {
47 $y = new ParagonIE_Sodium_Core_Curve25519_Fe();
48 }
49 $this->Y = $y;
50 if ($z === null) {
51 $z = new ParagonIE_Sodium_Core_Curve25519_Fe();
52 }
53 $this->Z = $z;
54 }
55 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P3', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_Curve25519_Ge_P3
10 */
11 class ParagonIE_Sodium_Core_Curve25519_Ge_P3
12 {
13 /**
14 * @var ParagonIE_Sodium_Core_Curve25519_Fe
15 */
16 public $X;
17
18 /**
19 * @var ParagonIE_Sodium_Core_Curve25519_Fe
20 */
21 public $Y;
22
23 /**
24 * @var ParagonIE_Sodium_Core_Curve25519_Fe
25 */
26 public $Z;
27
28 /**
29 * @var ParagonIE_Sodium_Core_Curve25519_Fe
30 */
31 public $T;
32
33 /**
34 * ParagonIE_Sodium_Core_Curve25519_Ge_P3 constructor.
35 *
36 * @internal You should not use this directly from another application
37 *
38 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x
39 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y
40 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z
41 * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t
42 */
43 public function __construct(
44 ParagonIE_Sodium_Core_Curve25519_Fe $x = null,
45 ParagonIE_Sodium_Core_Curve25519_Fe $y = null,
46 ParagonIE_Sodium_Core_Curve25519_Fe $z = null,
47 ParagonIE_Sodium_Core_Curve25519_Fe $t = null
48 ) {
49 if ($x === null) {
50 $x = new ParagonIE_Sodium_Core_Curve25519_Fe();
51 }
52 $this->X = $x;
53 if ($y === null) {
54 $y = new ParagonIE_Sodium_Core_Curve25519_Fe();
55 }
56 $this->Y = $y;
57 if ($z === null) {
58 $z = new ParagonIE_Sodium_Core_Curve25519_Fe();
59 }
60 $this->Z = $z;
61 if ($t === null) {
62 $t = new ParagonIE_Sodium_Core_Curve25519_Fe();
63 }
64 $this->T = $t;
65 }
66 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Precomp', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp
10 */
11 class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp
12 {
13 /**
14 * @var ParagonIE_Sodium_Core_Curve25519_Fe
15 */
16 public $yplusx;
17
18 /**
19 * @var ParagonIE_Sodium_Core_Curve25519_Fe
20 */
21 public $yminusx;
22
23 /**
24 * @var ParagonIE_Sodium_Core_Curve25519_Fe
25 */
26 public $xy2d;
27
28 /**
29 * ParagonIE_Sodium_Core_Curve25519_Ge_Precomp constructor.
30 *
31 * @internal You should not use this directly from another application
32 *
33 * @param ParagonIE_Sodium_Core_Curve25519_Fe $yplusx
34 * @param ParagonIE_Sodium_Core_Curve25519_Fe $yminusx
35 * @param ParagonIE_Sodium_Core_Curve25519_Fe $xy2d
36 */
37 public function __construct(
38 ParagonIE_Sodium_Core_Curve25519_Fe $yplusx = null,
39 ParagonIE_Sodium_Core_Curve25519_Fe $yminusx = null,
40 ParagonIE_Sodium_Core_Curve25519_Fe $xy2d = null
41 ) {
42 if ($yplusx === null) {
43 $yplusx = new ParagonIE_Sodium_Core_Curve25519_Fe();
44 }
45 $this->yplusx = $yplusx;
46 if ($yminusx === null) {
47 $yminusx = new ParagonIE_Sodium_Core_Curve25519_Fe();
48 }
49 $this->yminusx = $yminusx;
50 if ($xy2d === null) {
51 $xy2d = new ParagonIE_Sodium_Core_Curve25519_Fe();
52 }
53 $this->xy2d = $xy2d;
54 }
55 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_HChaCha20', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_HChaCha20
10 */
11 class ParagonIE_Sodium_Core_HChaCha20 extends ParagonIE_Sodium_Core_ChaCha20
12 {
13 /**
14 * @param string $in
15 * @param string $key
16 * @param string|null $c
17 * @return string
18 * @throws TypeError
19 */
20 public static function hChaCha20($in = '', $key = '', $c = null)
21 {
22 $ctx = array();
23
24 if ($c === null) {
25 $ctx[0] = 0x61707865;
26 $ctx[1] = 0x3320646e;
27 $ctx[2] = 0x79622d32;
28 $ctx[3] = 0x6b206574;
29 } else {
30 $ctx[0] = self::load_4(self::substr($c, 0, 4));
31 $ctx[1] = self::load_4(self::substr($c, 4, 4));
32 $ctx[2] = self::load_4(self::substr($c, 8, 4));
33 $ctx[3] = self::load_4(self::substr($c, 12, 4));
34 }
35 $ctx[4] = self::load_4(self::substr($key, 0, 4));
36 $ctx[5] = self::load_4(self::substr($key, 4, 4));
37 $ctx[6] = self::load_4(self::substr($key, 8, 4));
38 $ctx[7] = self::load_4(self::substr($key, 12, 4));
39 $ctx[8] = self::load_4(self::substr($key, 16, 4));
40 $ctx[9] = self::load_4(self::substr($key, 20, 4));
41 $ctx[10] = self::load_4(self::substr($key, 24, 4));
42 $ctx[11] = self::load_4(self::substr($key, 28, 4));
43 $ctx[12] = self::load_4(self::substr($in, 0, 4));
44 $ctx[13] = self::load_4(self::substr($in, 4, 4));
45 $ctx[14] = self::load_4(self::substr($in, 8, 4));
46 $ctx[15] = self::load_4(self::substr($in, 12, 4));
47 return self::hChaCha20Bytes($ctx);
48 }
49
50 /**
51 * @param array $ctx
52 * @return string
53 * @throws TypeError
54 */
55 protected static function hChaCha20Bytes(array $ctx)
56 {
57 $x0 = (int) $ctx[0];
58 $x1 = (int) $ctx[1];
59 $x2 = (int) $ctx[2];
60 $x3 = (int) $ctx[3];
61 $x4 = (int) $ctx[4];
62 $x5 = (int) $ctx[5];
63 $x6 = (int) $ctx[6];
64 $x7 = (int) $ctx[7];
65 $x8 = (int) $ctx[8];
66 $x9 = (int) $ctx[9];
67 $x10 = (int) $ctx[10];
68 $x11 = (int) $ctx[11];
69 $x12 = (int) $ctx[12];
70 $x13 = (int) $ctx[13];
71 $x14 = (int) $ctx[14];
72 $x15 = (int) $ctx[15];
73
74 for ($i = 0; $i < 10; ++$i) {
75 # QUARTERROUND( x0, x4, x8, x12)
76 list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
77
78 # QUARTERROUND( x1, x5, x9, x13)
79 list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);
80
81 # QUARTERROUND( x2, x6, x10, x14)
82 list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);
83
84 # QUARTERROUND( x3, x7, x11, x15)
85 list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);
86
87 # QUARTERROUND( x0, x5, x10, x15)
88 list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);
89
90 # QUARTERROUND( x1, x6, x11, x12)
91 list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);
92
93 # QUARTERROUND( x2, x7, x8, x13)
94 list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);
95
96 # QUARTERROUND( x3, x4, x9, x14)
97 list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
98 }
99
100 return self::store32_le((int) ($x0 & 0xffffffff)) .
101 self::store32_le((int) ($x1 & 0xffffffff)) .
102 self::store32_le((int) ($x2 & 0xffffffff)) .
103 self::store32_le((int) ($x3 & 0xffffffff)) .
104 self::store32_le((int) ($x12 & 0xffffffff)) .
105 self::store32_le((int) ($x13 & 0xffffffff)) .
106 self::store32_le((int) ($x14 & 0xffffffff)) .
107 self::store32_le((int) ($x15 & 0xffffffff));
108 }
109 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_HSalsa20', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_HSalsa20
10 */
11 abstract class ParagonIE_Sodium_Core_HSalsa20 extends ParagonIE_Sodium_Core_Salsa20
12 {
13 /**
14 * Calculate an hsalsa20 hash of a single block
15 *
16 * HSalsa20 doesn't have a counter and will never be used for more than
17 * one block (used to derive a subkey for xsalsa20).
18 *
19 * @internal You should not use this directly from another application
20 *
21 * @param string $in
22 * @param string $k
23 * @param string|null $c
24 * @return string
25 * @throws TypeError
26 */
27 public static function hsalsa20($in, $k, $c = null)
28 {
29 if ($c === null) {
30 $x0 = 0x61707865;
31 $x5 = 0x3320646e;
32 $x10 = 0x79622d32;
33 $x15 = 0x6b206574;
34 } else {
35 $x0 = self::load_4(self::substr($c, 0, 4));
36 $x5 = self::load_4(self::substr($c, 4, 4));
37 $x10 = self::load_4(self::substr($c, 8, 4));
38 $x15 = self::load_4(self::substr($c, 12, 4));
39 }
40 $x1 = self::load_4(self::substr($k, 0, 4));
41 $x2 = self::load_4(self::substr($k, 4, 4));
42 $x3 = self::load_4(self::substr($k, 8, 4));
43 $x4 = self::load_4(self::substr($k, 12, 4));
44 $x11 = self::load_4(self::substr($k, 16, 4));
45 $x12 = self::load_4(self::substr($k, 20, 4));
46 $x13 = self::load_4(self::substr($k, 24, 4));
47 $x14 = self::load_4(self::substr($k, 28, 4));
48 $x6 = self::load_4(self::substr($in, 0, 4));
49 $x7 = self::load_4(self::substr($in, 4, 4));
50 $x8 = self::load_4(self::substr($in, 8, 4));
51 $x9 = self::load_4(self::substr($in, 12, 4));
52
53 for ($i = self::ROUNDS; $i > 0; $i -= 2) {
54 $x4 ^= self::rotate($x0 + $x12, 7);
55 $x8 ^= self::rotate($x4 + $x0, 9);
56 $x12 ^= self::rotate($x8 + $x4, 13);
57 $x0 ^= self::rotate($x12 + $x8, 18);
58 $x9 ^= self::rotate($x5 + $x1, 7);
59 $x13 ^= self::rotate($x9 + $x5, 9);
60 $x1 ^= self::rotate($x13 + $x9, 13);
61 $x5 ^= self::rotate($x1 + $x13, 18);
62 $x14 ^= self::rotate($x10 + $x6, 7);
63 $x2 ^= self::rotate($x14 + $x10, 9);
64 $x6 ^= self::rotate($x2 + $x14, 13);
65 $x10 ^= self::rotate($x6 + $x2, 18);
66 $x3 ^= self::rotate($x15 + $x11, 7);
67 $x7 ^= self::rotate($x3 + $x15, 9);
68 $x11 ^= self::rotate($x7 + $x3, 13);
69 $x15 ^= self::rotate($x11 + $x7, 18);
70 $x1 ^= self::rotate($x0 + $x3, 7);
71 $x2 ^= self::rotate($x1 + $x0, 9);
72 $x3 ^= self::rotate($x2 + $x1, 13);
73 $x0 ^= self::rotate($x3 + $x2, 18);
74 $x6 ^= self::rotate($x5 + $x4, 7);
75 $x7 ^= self::rotate($x6 + $x5, 9);
76 $x4 ^= self::rotate($x7 + $x6, 13);
77 $x5 ^= self::rotate($x4 + $x7, 18);
78 $x11 ^= self::rotate($x10 + $x9, 7);
79 $x8 ^= self::rotate($x11 + $x10, 9);
80 $x9 ^= self::rotate($x8 + $x11, 13);
81 $x10 ^= self::rotate($x9 + $x8, 18);
82 $x12 ^= self::rotate($x15 + $x14, 7);
83 $x13 ^= self::rotate($x12 + $x15, 9);
84 $x14 ^= self::rotate($x13 + $x12, 13);
85 $x15 ^= self::rotate($x14 + $x13, 18);
86 }
87
88 return self::store32_le($x0) .
89 self::store32_le($x5) .
90 self::store32_le($x10) .
91 self::store32_le($x15) .
92 self::store32_le($x6) .
93 self::store32_le($x7) .
94 self::store32_le($x8) .
95 self::store32_le($x9);
96 }
97 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_Poly1305', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_Poly1305
10 */
11 abstract class ParagonIE_Sodium_Core_Poly1305 extends ParagonIE_Sodium_Core_Util
12 {
13 const BLOCK_SIZE = 16;
14
15 /**
16 * @internal You should not use this directly from another application
17 *
18 * @param string $m
19 * @param string $key
20 * @return string
21 * @throws SodiumException
22 * @throws TypeError
23 */
24 public static function onetimeauth($m, $key)
25 {
26 if (self::strlen($key) < 32) {
27 throw new InvalidArgumentException(
28 'Key must be 32 bytes long.'
29 );
30 }
31 $state = new ParagonIE_Sodium_Core_Poly1305_State(
32 self::substr($key, 0, 32)
33 );
34 return $state
35 ->update($m)
36 ->finish();
37 }
38
39 /**
40 * @internal You should not use this directly from another application
41 *
42 * @param string $mac
43 * @param string $m
44 * @param string $key
45 * @return bool
46 * @throws SodiumException
47 * @throws TypeError
48 */
49 public static function onetimeauth_verify($mac, $m, $key)
50 {
51 if (self::strlen($key) < 32) {
52 throw new InvalidArgumentException(
53 'Key must be 32 bytes long.'
54 );
55 }
56 $state = new ParagonIE_Sodium_Core_Poly1305_State(
57 self::substr($key, 0, 32)
58 );
59 $calc = $state
60 ->update($m)
61 ->finish();
62 return self::verify_16($calc, $mac);
63 }
64 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_Salsa20', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_Salsa20
10 */
11 abstract class ParagonIE_Sodium_Core_Salsa20 extends ParagonIE_Sodium_Core_Util
12 {
13 const ROUNDS = 20;
14
15 /**
16 * Calculate an salsa20 hash of a single block
17 *
18 * @internal You should not use this directly from another application
19 *
20 * @param string $in
21 * @param string $k
22 * @param string|null $c
23 * @return string
24 * @throws TypeError
25 */
26 public static function core_salsa20($in, $k, $c = null)
27 {
28 if (self::strlen($k) < 32) {
29 throw new RangeException('Key must be 32 bytes long');
30 }
31 if ($c === null) {
32 $j0 = $x0 = 0x61707865;
33 $j5 = $x5 = 0x3320646e;
34 $j10 = $x10 = 0x79622d32;
35 $j15 = $x15 = 0x6b206574;
36 } else {
37 $j0 = $x0 = self::load_4(self::substr($c, 0, 4));
38 $j5 = $x5 = self::load_4(self::substr($c, 4, 4));
39 $j10 = $x10 = self::load_4(self::substr($c, 8, 4));
40 $j15 = $x15 = self::load_4(self::substr($c, 12, 4));
41 }
42 $j1 = $x1 = self::load_4(self::substr($k, 0, 4));
43 $j2 = $x2 = self::load_4(self::substr($k, 4, 4));
44 $j3 = $x3 = self::load_4(self::substr($k, 8, 4));
45 $j4 = $x4 = self::load_4(self::substr($k, 12, 4));
46 $j6 = $x6 = self::load_4(self::substr($in, 0, 4));
47 $j7 = $x7 = self::load_4(self::substr($in, 4, 4));
48 $j8 = $x8 = self::load_4(self::substr($in, 8, 4));
49 $j9 = $x9 = self::load_4(self::substr($in, 12, 4));
50 $j11 = $x11 = self::load_4(self::substr($k, 16, 4));
51 $j12 = $x12 = self::load_4(self::substr($k, 20, 4));
52 $j13 = $x13 = self::load_4(self::substr($k, 24, 4));
53 $j14 = $x14 = self::load_4(self::substr($k, 28, 4));
54
55 for ($i = self::ROUNDS; $i > 0; $i -= 2) {
56 $x4 ^= self::rotate($x0 + $x12, 7);
57 $x8 ^= self::rotate($x4 + $x0, 9);
58 $x12 ^= self::rotate($x8 + $x4, 13);
59 $x0 ^= self::rotate($x12 + $x8, 18);
60
61 $x9 ^= self::rotate($x5 + $x1, 7);
62 $x13 ^= self::rotate($x9 + $x5, 9);
63 $x1 ^= self::rotate($x13 + $x9, 13);
64 $x5 ^= self::rotate($x1 + $x13, 18);
65
66 $x14 ^= self::rotate($x10 + $x6, 7);
67 $x2 ^= self::rotate($x14 + $x10, 9);
68 $x6 ^= self::rotate($x2 + $x14, 13);
69 $x10 ^= self::rotate($x6 + $x2, 18);
70
71 $x3 ^= self::rotate($x15 + $x11, 7);
72 $x7 ^= self::rotate($x3 + $x15, 9);
73 $x11 ^= self::rotate($x7 + $x3, 13);
74 $x15 ^= self::rotate($x11 + $x7, 18);
75
76 $x1 ^= self::rotate($x0 + $x3, 7);
77 $x2 ^= self::rotate($x1 + $x0, 9);
78 $x3 ^= self::rotate($x2 + $x1, 13);
79 $x0 ^= self::rotate($x3 + $x2, 18);
80
81 $x6 ^= self::rotate($x5 + $x4, 7);
82 $x7 ^= self::rotate($x6 + $x5, 9);
83 $x4 ^= self::rotate($x7 + $x6, 13);
84 $x5 ^= self::rotate($x4 + $x7, 18);
85
86 $x11 ^= self::rotate($x10 + $x9, 7);
87 $x8 ^= self::rotate($x11 + $x10, 9);
88 $x9 ^= self::rotate($x8 + $x11, 13);
89 $x10 ^= self::rotate($x9 + $x8, 18);
90
91 $x12 ^= self::rotate($x15 + $x14, 7);
92 $x13 ^= self::rotate($x12 + $x15, 9);
93 $x14 ^= self::rotate($x13 + $x12, 13);
94 $x15 ^= self::rotate($x14 + $x13, 18);
95 }
96
97 $x0 += $j0;
98 $x1 += $j1;
99 $x2 += $j2;
100 $x3 += $j3;
101 $x4 += $j4;
102 $x5 += $j5;
103 $x6 += $j6;
104 $x7 += $j7;
105 $x8 += $j8;
106 $x9 += $j9;
107 $x10 += $j10;
108 $x11 += $j11;
109 $x12 += $j12;
110 $x13 += $j13;
111 $x14 += $j14;
112 $x15 += $j15;
113
114 return self::store32_le($x0) .
115 self::store32_le($x1) .
116 self::store32_le($x2) .
117 self::store32_le($x3) .
118 self::store32_le($x4) .
119 self::store32_le($x5) .
120 self::store32_le($x6) .
121 self::store32_le($x7) .
122 self::store32_le($x8) .
123 self::store32_le($x9) .
124 self::store32_le($x10) .
125 self::store32_le($x11) .
126 self::store32_le($x12) .
127 self::store32_le($x13) .
128 self::store32_le($x14) .
129 self::store32_le($x15);
130 }
131
132 /**
133 * @internal You should not use this directly from another application
134 *
135 * @param int $len
136 * @param string $nonce
137 * @param string $key
138 * @return string
139 * @throws SodiumException
140 * @throws TypeError
141 */
142 public static function salsa20($len, $nonce, $key)
143 {
144 if (self::strlen($key) !== 32) {
145 throw new RangeException('Key must be 32 bytes long');
146 }
147 $kcopy = '' . $key;
148 $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8);
149 $c = '';
150 while ($len >= 64) {
151 $c .= self::core_salsa20($in, $kcopy, null);
152 $u = 1;
153 // Internal counter.
154 for ($i = 8; $i < 16; ++$i) {
155 $u += self::chrToInt($in[$i]);
156 $in[$i] = self::intToChr($u & 0xff);
157 $u >>= 8;
158 }
159 $len -= 64;
160 }
161 if ($len > 0) {
162 $c .= self::substr(
163 self::core_salsa20($in, $kcopy, null),
164 0,
165 $len
166 );
167 }
168 try {
169 ParagonIE_Sodium_Compat::memzero($kcopy);
170 } catch (SodiumException $ex) {
171 $kcopy = null;
172 }
173 return $c;
174 }
175
176 /**
177 * @internal You should not use this directly from another application
178 *
179 * @param string $m
180 * @param string $n
181 * @param int $ic
182 * @param string $k
183 * @return string
184 * @throws SodiumException
185 * @throws TypeError
186 */
187 public static function salsa20_xor_ic($m, $n, $ic, $k)
188 {
189 $mlen = self::strlen($m);
190 if ($mlen < 1) {
191 return '';
192 }
193 $kcopy = self::substr($k, 0, 32);
194 $in = self::substr($n, 0, 8);
195 // Initialize the counter
196 $in .= ParagonIE_Sodium_Core_Util::store64_le($ic);
197
198 $c = '';
199 while ($mlen >= 64) {
200 $block = self::core_salsa20($in, $kcopy, null);
201 $c .= self::xorStrings(
202 self::substr($m, 0, 64),
203 self::substr($block, 0, 64)
204 );
205 $u = 1;
206 for ($i = 8; $i < 16; ++$i) {
207 $u += self::chrToInt($in[$i]);
208 $in[$i] = self::intToChr($u & 0xff);
209 $u >>= 8;
210 }
211
212 $mlen -= 64;
213 $m = self::substr($m, 64);
214 }
215
216 if ($mlen) {
217 $block = self::core_salsa20($in, $kcopy, null);
218 $c .= self::xorStrings(
219 self::substr($m, 0, $mlen),
220 self::substr($block, 0, $mlen)
221 );
222 }
223 try {
224 ParagonIE_Sodium_Compat::memzero($block);
225 ParagonIE_Sodium_Compat::memzero($kcopy);
226 } catch (SodiumException $ex) {
227 $block = null;
228 $kcopy = null;
229 }
230
231 return $c;
232 }
233
234 /**
235 * @internal You should not use this directly from another application
236 *
237 * @param string $message
238 * @param string $nonce
239 * @param string $key
240 * @return string
241 * @throws SodiumException
242 * @throws TypeError
243 */
244 public static function salsa20_xor($message, $nonce, $key)
245 {
246 return self::xorStrings(
247 $message,
248 self::salsa20(
249 self::strlen($message),
250 $nonce,
251 $key
252 )
253 );
254 }
255
256 /**
257 * @internal You should not use this directly from another application
258 *
259 * @param int $u
260 * @param int $c
261 * @return int
262 */
263 public static function rotate($u, $c)
264 {
265 $u &= 0xffffffff;
266 $c %= 32;
267 return (int) (0xffffffff & (
268 ($u << $c)
269 |
270 ($u >> (32 - $c))
271 )
272 );
273 }
274 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 /**
5 * Class ParagonIE_Sodium_Core_SecretStream_State
6 */
7 class ParagonIE_Sodium_Core_SecretStream_State
8 {
9 /** @var string $key */
10 protected $key;
11
12 /** @var int $counter */
13 protected $counter;
14
15 /** @var string $nonce */
16 protected $nonce;
17
18 /** @var string $_pad */
19 protected $_pad;
20
21 /**
22 * ParagonIE_Sodium_Core_SecretStream_State constructor.
23 * @param string $key
24 * @param string|null $nonce
25 */
26 public function __construct($key, $nonce = null)
27 {
28 $this->key = $key;
29 $this->counter = 1;
30 if (is_null($nonce)) {
31 $nonce = str_repeat("\0", 12);
32 }
33 $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);;
34 $this->_pad = str_repeat("\0", 4);
35 }
36
37 /**
38 * @return self
39 */
40 public function counterReset()
41 {
42 $this->counter = 1;
43 $this->_pad = str_repeat("\0", 4);
44 return $this;
45 }
46
47 /**
48 * @return string
49 */
50 public function getKey()
51 {
52 return $this->key;
53 }
54
55 /**
56 * @return string
57 */
58 public function getCounter()
59 {
60 return ParagonIE_Sodium_Core_Util::store32_le($this->counter);
61 }
62
63 /**
64 * @return string
65 */
66 public function getNonce()
67 {
68 if (!is_string($this->nonce)) {
69 $this->nonce = str_repeat("\0", 12);
70 }
71 if (ParagonIE_Sodium_Core_Util::strlen($this->nonce) !== 12) {
72 $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT);
73 }
74 return $this->nonce;
75 }
76
77 /**
78 * @return string
79 */
80 public function getCombinedNonce()
81 {
82 return $this->getCounter() .
83 ParagonIE_Sodium_Core_Util::substr($this->getNonce(), 0, 8);
84 }
85
86 /**
87 * @return self
88 */
89 public function incrementCounter()
90 {
91 ++$this->counter;
92 return $this;
93 }
94
95 /**
96 * @return bool
97 */
98 public function needsRekey()
99 {
100 return ($this->counter & 0xffff) === 0;
101 }
102
103 /**
104 * @param string $newKeyAndNonce
105 * @return self
106 */
107 public function rekey($newKeyAndNonce)
108 {
109 $this->key = ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 0, 32);
110 $this->nonce = str_pad(
111 ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 32),
112 12,
113 "\0",
114 STR_PAD_RIGHT
115 );
116 return $this;
117 }
118
119 /**
120 * @param string $str
121 * @return self
122 */
123 public function xorNonce($str)
124 {
125 $this->nonce = ParagonIE_Sodium_Core_Util::xorStrings(
126 $this->getNonce(),
127 str_pad(
128 ParagonIE_Sodium_Core_Util::substr($str, 0, 8),
129 12,
130 "\0",
131 STR_PAD_RIGHT
132 )
133 );
134 return $this;
135 }
136
137 /**
138 * @param string $string
139 * @return self
140 */
141 public static function fromString($string)
142 {
143 $state = new ParagonIE_Sodium_Core_SecretStream_State(
144 ParagonIE_Sodium_Core_Util::substr($string, 0, 32)
145 );
146 $state->counter = ParagonIE_Sodium_Core_Util::load_4(
147 ParagonIE_Sodium_Core_Util::substr($string, 32, 4)
148 );
149 $state->nonce = ParagonIE_Sodium_Core_Util::substr($string, 36, 12);
150 $state->_pad = ParagonIE_Sodium_Core_Util::substr($string, 48, 8);
151 return $state;
152 }
153
154 /**
155 * @return string
156 */
157 public function toString()
158 {
159 return $this->key .
160 $this->getCounter() .
161 $this->nonce .
162 $this->_pad;
163 }
164 }
1 <?php
2 // phpcs:ignoreFile -- compatibility library for PHP 5-7.1
3
4 if (class_exists('ParagonIE_Sodium_Core_SipHash', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_SodiumCompat_Core_SipHash
10 *
11 * Only uses 32-bit arithmetic, while the original SipHash used 64-bit integers
12 */
13 class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util
14 {
15 /**
16 * @internal You should not use this directly from another application
17 *
18 * @param int[] $v
19 * @return int[]
20 */
21 public static function sipRound(array $v)
22 {
23 # v0 += v1;
24 list($v[0], $v[1]) = self::add(
25 array($v[0], $v[1]),
26 array($v[2], $v[3])
27 );
28
29 # v1=ROTL(v1,13);
30 list($v[2], $v[3]) = self::rotl_64($v[2], $v[3], 13);
31
32 # v1 ^= v0;
33 $v[2] ^= $v[0];
34 $v[3] ^= $v[1];
35
36 # v0=ROTL(v0,32);
37 list($v[0], $v[1]) = self::rotl_64((int) $v[0], (int) $v[1], 32);
38
39 # v2 += v3;
40 list($v[4], $v[5]) = self::add(
41 array($v[4], $v[5]),
42 array($v[6], $v[7])
43 );
44
45 # v3=ROTL(v3,16);
46 list($v[6], $v[7]) = self::rotl_64($v[6], $v[7], 16);
47
48 # v3 ^= v2;
49 $v[6] ^= $v[4];
50 $v[7] ^= $v[5];
51
52 # v0 += v3;
53 list($v[0], $v[1]) = self::add(
54 array((int) $v[0], (int) $v[1]),
55 array((int) $v[6], (int) $v[7])
56 );
57
58 # v3=ROTL(v3,21);
59 list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 21);
60
61 # v3 ^= v0;
62 $v[6] ^= $v[0];
63 $v[7] ^= $v[1];
64
65 # v2 += v1;
66 list($v[4], $v[5]) = self::add(
67 array((int) $v[4], (int) $v[5]),
68 array((int) $v[2], (int) $v[3])
69 );
70
71 # v1=ROTL(v1,17);
72 list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 17);
73
74 # v1 ^= v2;;
75 $v[2] ^= $v[4];
76 $v[3] ^= $v[5];
77
78 # v2=ROTL(v2,32)
79 list($v[4], $v[5]) = self::rotl_64((int) $v[4], (int) $v[5], 32);
80
81 return $v;
82 }
83
84 /**
85 * Add two 32 bit integers representing a 64-bit integer.
86 *
87 * @internal You should not use this directly from another application
88 *
89 * @param int[] $a
90 * @param int[] $b
91 * @return array<int, mixed>
92 */
93 public static function add(array $a, array $b)
94 {
95 /** @var int $x1 */
96 $x1 = $a[1] + $b[1];
97 /** @var int $c */
98 $c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff
99 /** @var int $x0 */
100 $x0 = $a[0] + $b[0] + $c;
101 return array(
102 $x0 & 0xffffffff,
103 $x1 & 0xffffffff
104 );
105 }
106
107 /**
108 * @internal You should not use this directly from another application
109 *
110 * @param int $int0
111 * @param int $int1
112 * @param int $c
113 * @return array<int, mixed>
114 */
115 public static function rotl_64($int0, $int1, $c)
116 {
117 $int0 &= 0xffffffff;
118 $int1 &= 0xffffffff;
119 $c &= 63;
120 if ($c === 32) {
121 return array($int1, $int0);
122 }
123 if ($c > 31) {
124 $tmp = $int1;
125 $int1 = $int0;
126 $int0 = $tmp;
127 $c &= 31;
128 }
129 if ($c === 0) {
130 return array($int0, $int1);
131 }
132 return array(
133 0xffffffff & (
134 ($int0 << $c)
135 |
136 ($int1 >> (32 - $c))
137 ),
138 0xffffffff & (
139 ($int1 << $c)
140 |
141 ($int0 >> (32 - $c))
142 ),
143 );
144 }
145
146 /**
147 * Implements Siphash-2-4 using only 32-bit numbers.
148 *
149 * When we split an int into two, the higher bits go to the lower index.
150 * e.g. 0xDEADBEEFAB10C92D becomes [
151 * 0 => 0xDEADBEEF,
152 * 1 => 0xAB10C92D
153 * ].
154 *
155 * @internal You should not use this directly from another application
156 *
157 * @param string $in
158 * @param string $key
159 * @return string
160 * @throws SodiumException
161 * @throws TypeError
162 */
163 public static function sipHash24($in, $key)
164 {
165 $inlen = self::strlen($in);
166
167 # /* "somepseudorandomlygeneratedbytes" */
168 # u64 v0 = 0x736f6d6570736575ULL;
169 # u64 v1 = 0x646f72616e646f6dULL;
170 # u64 v2 = 0x6c7967656e657261ULL;
171 # u64 v3 = 0x7465646279746573ULL;
172 $v = array(
173 0x736f6d65, // 0
174 0x70736575, // 1
175 0x646f7261, // 2
176 0x6e646f6d, // 3
177 0x6c796765, // 4
178 0x6e657261, // 5
179 0x74656462, // 6
180 0x79746573 // 7
181 );
182 // v0 => $v[0], $v[1]
183 // v1 => $v[2], $v[3]
184 // v2 => $v[4], $v[5]
185 // v3 => $v[6], $v[7]
186
187 # u64 k0 = LOAD64_LE( k );
188 # u64 k1 = LOAD64_LE( k + 8 );
189 $k = array(
190 self::load_4(self::substr($key, 4, 4)),
191 self::load_4(self::substr($key, 0, 4)),
192 self::load_4(self::substr($key, 12, 4)),
193 self::load_4(self::substr($key, 8, 4))
194 );
195 // k0 => $k[0], $k[1]
196 // k1 => $k[2], $k[3]
197
198 # b = ( ( u64 )inlen ) << 56;
199 $b = array(
200 $inlen << 24,
201 0
202 );
203 // See docblock for why the 0th index gets the higher bits.
204
205 # v3 ^= k1;
206 $v[6] ^= $k[2];
207 $v[7] ^= $k[3];
208 # v2 ^= k0;
209 $v[4] ^= $k[0];
210 $v[5] ^= $k[1];
211 # v1 ^= k1;
212 $v[2] ^= $k[2];
213 $v[3] ^= $k[3];
214 # v0 ^= k0;
215 $v[0] ^= $k[0];
216 $v[1] ^= $k[1];
217
218 $left = $inlen;
219 # for ( ; in != end; in += 8 )
220 while ($left >= 8) {
221 # m = LOAD64_LE( in );
222 $m = array(
223 self::load_4(self::substr($in, 4, 4)),
224 self::load_4(self::substr($in, 0, 4))
225 );
226
227 # v3 ^= m;
228 $v[6] ^= $m[0];
229 $v[7] ^= $m[1];
230
231 # SIPROUND;
232 # SIPROUND;
233 $v = self::sipRound($v);
234 $v = self::sipRound($v);
235
236 # v0 ^= m;
237 $v[0] ^= $m[0];
238 $v[1] ^= $m[1];
239
240 $in = self::substr($in, 8);
241 $left -= 8;
242 }
243
244 # switch( left )
245 # {
246 # case 7: b |= ( ( u64 )in[ 6] ) << 48;
247 # case 6: b |= ( ( u64 )in[ 5] ) << 40;
248 # case 5: b |= ( ( u64 )in[ 4] ) << 32;
249 # case 4: b |= ( ( u64 )in[ 3] ) << 24;
250 # case 3: b |= ( ( u64 )in[ 2] ) << 16;
251 # case 2: b |= ( ( u64 )in[ 1] ) << 8;
252 # case 1: b |= ( ( u64 )in[ 0] ); break;
253 # case 0: break;
254 # }
255 switch ($left) {
256 case 7:
257 $b[0] |= self::chrToInt($in[6]) << 16;
258 case 6:
259 $b[0] |= self::chrToInt($in[5]) << 8;
260 case 5:
261 $b[0] |= self::chrToInt($in[4]);
262 case 4:
263 $b[1] |= self::chrToInt($in[3]) << 24;
264 case 3:
265 $b[1] |= self::chrToInt($in[2]) << 16;
266 case 2:
267 $b[1] |= self::chrToInt($in[1]) << 8;
268 case 1:
269 $b[1] |= self::chrToInt($in[0]);
270 case 0:
271 break;
272 }
273 // See docblock for why the 0th index gets the higher bits.
274
275 # v3 ^= b;
276 $v[6] ^= $b[0];
277 $v[7] ^= $b[1];
278
279 # SIPROUND;
280 # SIPROUND;
281 $v = self::sipRound($v);
282 $v = self::sipRound($v);
283
284 # v0 ^= b;
285 $v[0] ^= $b[0];
286 $v[1] ^= $b[1];
287
288 // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation
289 # v2 ^= 0xff;
290 $v[5] ^= 0xff;
291
292 # SIPROUND;
293 # SIPROUND;
294 # SIPROUND;
295 # SIPROUND;
296 $v = self::sipRound($v);
297 $v = self::sipRound($v);
298 $v = self::sipRound($v);
299 $v = self::sipRound($v);
300
301 # b = v0 ^ v1 ^ v2 ^ v3;
302 # STORE64_LE( out, b );
303 return self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) .
304 self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]);
305 }
306 }
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.