@@ -1,7 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace ParagonIE\Sodium\Core; |
3 | 3 | |
4 | -class Curve25519 extends \ParagonIE_Sodium_Core_Curve25519 |
|
5 | -{ |
|
4 | +class Curve25519 extends \ParagonIE_Sodium_Core_Curve25519 { |
|
6 | 5 | |
7 | 6 | } |
@@ -1,7 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace ParagonIE\Sodium\Core\ChaCha20; |
3 | 3 | |
4 | -class Ctx extends \ParagonIE_Sodium_Core_ChaCha20_Ctx |
|
5 | -{ |
|
4 | +class Ctx extends \ParagonIE_Sodium_Core_ChaCha20_Ctx { |
|
6 | 5 | |
7 | 6 | } |
@@ -1,7 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace ParagonIE\Sodium\Core\ChaCha20; |
3 | 3 | |
4 | -class IetfCtx extends \ParagonIE_Sodium_Core_ChaCha20_IetfCtx |
|
5 | -{ |
|
4 | +class IetfCtx extends \ParagonIE_Sodium_Core_ChaCha20_IetfCtx { |
|
6 | 5 | |
7 | 6 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | require_once 'autoload.php'; |
4 | -define('DO_PEDANTIC_TEST', true); |
|
4 | +define( 'DO_PEDANTIC_TEST', true ); |
|
5 | 5 | |
6 | 6 | ParagonIE_Sodium_Compat::$fastMult = true; |
@@ -1,72 +1,72 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (PHP_VERSION_ID < 70000) { |
4 | - if (!is_callable('sodiumCompatAutoloader')) { |
|
5 | - /** |
|
6 | - * Sodium_Compat autoloader. |
|
7 | - * |
|
8 | - * @param string $class Class name to be autoloaded. |
|
9 | - * |
|
10 | - * @return bool Stop autoloading? |
|
11 | - */ |
|
12 | - function sodiumCompatAutoloader($class) |
|
13 | - { |
|
14 | - $namespace = 'ParagonIE_Sodium_'; |
|
15 | - // Does the class use the namespace prefix? |
|
16 | - $len = strlen($namespace); |
|
17 | - if (strncmp($namespace, $class, $len) !== 0) { |
|
18 | - // no, move to the next registered autoloader |
|
19 | - return false; |
|
20 | - } |
|
4 | + if (!is_callable('sodiumCompatAutoloader')) { |
|
5 | + /** |
|
6 | + * Sodium_Compat autoloader. |
|
7 | + * |
|
8 | + * @param string $class Class name to be autoloaded. |
|
9 | + * |
|
10 | + * @return bool Stop autoloading? |
|
11 | + */ |
|
12 | + function sodiumCompatAutoloader($class) |
|
13 | + { |
|
14 | + $namespace = 'ParagonIE_Sodium_'; |
|
15 | + // Does the class use the namespace prefix? |
|
16 | + $len = strlen($namespace); |
|
17 | + if (strncmp($namespace, $class, $len) !== 0) { |
|
18 | + // no, move to the next registered autoloader |
|
19 | + return false; |
|
20 | + } |
|
21 | 21 | |
22 | - // Get the relative class name |
|
23 | - $relative_class = substr($class, $len); |
|
22 | + // Get the relative class name |
|
23 | + $relative_class = substr($class, $len); |
|
24 | 24 | |
25 | - // Replace the namespace prefix with the base directory, replace namespace |
|
26 | - // separators with directory separators in the relative class name, append |
|
27 | - // with .php |
|
28 | - $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php'; |
|
29 | - // if the file exists, require it |
|
30 | - if (file_exists($file)) { |
|
31 | - require_once $file; |
|
32 | - return true; |
|
33 | - } |
|
34 | - return false; |
|
35 | - } |
|
25 | + // Replace the namespace prefix with the base directory, replace namespace |
|
26 | + // separators with directory separators in the relative class name, append |
|
27 | + // with .php |
|
28 | + $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php'; |
|
29 | + // if the file exists, require it |
|
30 | + if (file_exists($file)) { |
|
31 | + require_once $file; |
|
32 | + return true; |
|
33 | + } |
|
34 | + return false; |
|
35 | + } |
|
36 | 36 | |
37 | - // Now that we have an autoloader, let's register it! |
|
38 | - spl_autoload_register('sodiumCompatAutoloader'); |
|
39 | - } |
|
37 | + // Now that we have an autoloader, let's register it! |
|
38 | + spl_autoload_register('sodiumCompatAutoloader'); |
|
39 | + } |
|
40 | 40 | } else { |
41 | - require_once dirname(__FILE__) . '/autoload-php7.php'; |
|
41 | + require_once dirname(__FILE__) . '/autoload-php7.php'; |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /* Explicitly, always load the Compat class: */ |
45 | 45 | require_once dirname(__FILE__) . '/src/Compat.php'; |
46 | 46 | |
47 | 47 | if (!class_exists('SodiumException', false)) { |
48 | - require_once dirname(__FILE__) . '/src/SodiumException.php'; |
|
48 | + require_once dirname(__FILE__) . '/src/SodiumException.php'; |
|
49 | 49 | } |
50 | 50 | if (PHP_VERSION_ID >= 50300) { |
51 | - // Namespaces didn't exist before 5.3.0, so don't even try to use this |
|
52 | - // unless PHP >= 5.3.0 |
|
53 | - require_once dirname(__FILE__) . '/lib/namespaced.php'; |
|
54 | - require_once dirname(__FILE__) . '/lib/sodium_compat.php'; |
|
51 | + // Namespaces didn't exist before 5.3.0, so don't even try to use this |
|
52 | + // unless PHP >= 5.3.0 |
|
53 | + require_once dirname(__FILE__) . '/lib/namespaced.php'; |
|
54 | + require_once dirname(__FILE__) . '/lib/sodium_compat.php'; |
|
55 | 55 | } else { |
56 | - require_once dirname(__FILE__) . '/src/PHP52/SplFixedArray.php'; |
|
56 | + require_once dirname(__FILE__) . '/src/PHP52/SplFixedArray.php'; |
|
57 | 57 | } |
58 | 58 | if (PHP_VERSION_ID < 70200 || !extension_loaded('sodium')) { |
59 | - if (PHP_VERSION_ID >= 50300 && !defined('SODIUM_CRYPTO_SCALARMULT_BYTES')) { |
|
60 | - require_once dirname(__FILE__) . '/lib/php72compat_const.php'; |
|
61 | - } |
|
62 | - if (PHP_VERSION_ID >= 70000) { |
|
63 | - assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?'); |
|
64 | - } else { |
|
65 | - assert(class_exists('ParagonIE_Sodium_Compat')); |
|
66 | - } |
|
67 | - require_once(dirname(__FILE__) . '/lib/php72compat.php'); |
|
59 | + if (PHP_VERSION_ID >= 50300 && !defined('SODIUM_CRYPTO_SCALARMULT_BYTES')) { |
|
60 | + require_once dirname(__FILE__) . '/lib/php72compat_const.php'; |
|
61 | + } |
|
62 | + if (PHP_VERSION_ID >= 70000) { |
|
63 | + assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?'); |
|
64 | + } else { |
|
65 | + assert(class_exists('ParagonIE_Sodium_Compat')); |
|
66 | + } |
|
67 | + require_once(dirname(__FILE__) . '/lib/php72compat.php'); |
|
68 | 68 | } elseif (!function_exists('sodium_crypto_stream_xchacha20_xor')) { |
69 | - // Older versions of {PHP, ext/sodium} will not define these |
|
70 | - require_once(dirname(__FILE__) . '/lib/php72compat.php'); |
|
69 | + // Older versions of {PHP, ext/sodium} will not define these |
|
70 | + require_once(dirname(__FILE__) . '/lib/php72compat.php'); |
|
71 | 71 | } |
72 | 72 | require_once(dirname(__FILE__) . '/lib/ristretto255.php'); |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (PHP_VERSION_ID < 70000) { |
|
4 | - if (!is_callable('sodiumCompatAutoloader')) { |
|
3 | +if ( PHP_VERSION_ID < 70000 ) { |
|
4 | + if ( ! is_callable( 'sodiumCompatAutoloader' ) ) { |
|
5 | 5 | /** |
6 | 6 | * Sodium_Compat autoloader. |
7 | 7 | * |
@@ -9,25 +9,25 @@ discard block |
||
9 | 9 | * |
10 | 10 | * @return bool Stop autoloading? |
11 | 11 | */ |
12 | - function sodiumCompatAutoloader($class) |
|
12 | + function sodiumCompatAutoloader( $class ) |
|
13 | 13 | { |
14 | 14 | $namespace = 'ParagonIE_Sodium_'; |
15 | 15 | // Does the class use the namespace prefix? |
16 | - $len = strlen($namespace); |
|
17 | - if (strncmp($namespace, $class, $len) !== 0) { |
|
16 | + $len = strlen( $namespace ); |
|
17 | + if ( strncmp( $namespace, $class, $len ) !== 0 ) { |
|
18 | 18 | // no, move to the next registered autoloader |
19 | 19 | return false; |
20 | 20 | } |
21 | 21 | |
22 | 22 | // Get the relative class name |
23 | - $relative_class = substr($class, $len); |
|
23 | + $relative_class = substr( $class, $len ); |
|
24 | 24 | |
25 | 25 | // Replace the namespace prefix with the base directory, replace namespace |
26 | 26 | // separators with directory separators in the relative class name, append |
27 | 27 | // with .php |
28 | - $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php'; |
|
28 | + $file = dirname( __FILE__ ) . '/src/' . str_replace( '_', '/', $relative_class ) . '.php'; |
|
29 | 29 | // if the file exists, require it |
30 | - if (file_exists($file)) { |
|
30 | + if ( file_exists( $file ) ) { |
|
31 | 31 | require_once $file; |
32 | 32 | return true; |
33 | 33 | } |
@@ -35,38 +35,38 @@ discard block |
||
35 | 35 | } |
36 | 36 | |
37 | 37 | // Now that we have an autoloader, let's register it! |
38 | - spl_autoload_register('sodiumCompatAutoloader'); |
|
38 | + spl_autoload_register( 'sodiumCompatAutoloader' ); |
|
39 | 39 | } |
40 | 40 | } else { |
41 | - require_once dirname(__FILE__) . '/autoload-php7.php'; |
|
41 | + require_once dirname( __FILE__ ) . '/autoload-php7.php'; |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /* Explicitly, always load the Compat class: */ |
45 | -require_once dirname(__FILE__) . '/src/Compat.php'; |
|
45 | +require_once dirname( __FILE__ ) . '/src/Compat.php'; |
|
46 | 46 | |
47 | -if (!class_exists('SodiumException', false)) { |
|
48 | - require_once dirname(__FILE__) . '/src/SodiumException.php'; |
|
47 | +if ( ! class_exists( 'SodiumException', false ) ) { |
|
48 | + require_once dirname( __FILE__ ) . '/src/SodiumException.php'; |
|
49 | 49 | } |
50 | -if (PHP_VERSION_ID >= 50300) { |
|
50 | +if ( PHP_VERSION_ID >= 50300 ) { |
|
51 | 51 | // Namespaces didn't exist before 5.3.0, so don't even try to use this |
52 | 52 | // unless PHP >= 5.3.0 |
53 | - require_once dirname(__FILE__) . '/lib/namespaced.php'; |
|
54 | - require_once dirname(__FILE__) . '/lib/sodium_compat.php'; |
|
53 | + require_once dirname( __FILE__ ) . '/lib/namespaced.php'; |
|
54 | + require_once dirname( __FILE__ ) . '/lib/sodium_compat.php'; |
|
55 | 55 | } else { |
56 | - require_once dirname(__FILE__) . '/src/PHP52/SplFixedArray.php'; |
|
56 | + require_once dirname( __FILE__ ) . '/src/PHP52/SplFixedArray.php'; |
|
57 | 57 | } |
58 | -if (PHP_VERSION_ID < 70200 || !extension_loaded('sodium')) { |
|
59 | - if (PHP_VERSION_ID >= 50300 && !defined('SODIUM_CRYPTO_SCALARMULT_BYTES')) { |
|
60 | - require_once dirname(__FILE__) . '/lib/php72compat_const.php'; |
|
58 | +if ( PHP_VERSION_ID < 70200 || ! extension_loaded( 'sodium' ) ) { |
|
59 | + if ( PHP_VERSION_ID >= 50300 && ! defined( 'SODIUM_CRYPTO_SCALARMULT_BYTES' ) ) { |
|
60 | + require_once dirname( __FILE__ ) . '/lib/php72compat_const.php'; |
|
61 | 61 | } |
62 | - if (PHP_VERSION_ID >= 70000) { |
|
63 | - assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?'); |
|
62 | + if ( PHP_VERSION_ID >= 70000 ) { |
|
63 | + assert( class_exists( 'ParagonIE_Sodium_Compat' ), 'Possible filesystem/autoloader bug?' ); |
|
64 | 64 | } else { |
65 | - assert(class_exists('ParagonIE_Sodium_Compat')); |
|
65 | + assert( class_exists( 'ParagonIE_Sodium_Compat' ) ); |
|
66 | 66 | } |
67 | - require_once(dirname(__FILE__) . '/lib/php72compat.php'); |
|
68 | -} elseif (!function_exists('sodium_crypto_stream_xchacha20_xor')) { |
|
67 | + require_once( dirname( __FILE__ ) . '/lib/php72compat.php' ); |
|
68 | +} elseif ( ! function_exists( 'sodium_crypto_stream_xchacha20_xor' ) ) { |
|
69 | 69 | // Older versions of {PHP, ext/sodium} will not define these |
70 | - require_once(dirname(__FILE__) . '/lib/php72compat.php'); |
|
70 | + require_once( dirname( __FILE__ ) . '/lib/php72compat.php' ); |
|
71 | 71 | } |
72 | -require_once(dirname(__FILE__) . '/lib/ristretto255.php'); |
|
72 | +require_once( dirname( __FILE__ ) . '/lib/ristretto255.php' ); |
@@ -9,8 +9,7 @@ |
||
9 | 9 | * |
10 | 10 | * @return bool Stop autoloading? |
11 | 11 | */ |
12 | - function sodiumCompatAutoloader($class) |
|
13 | - { |
|
12 | + function sodiumCompatAutoloader($class) { |
|
14 | 13 | $namespace = 'ParagonIE_Sodium_'; |
15 | 14 | // Does the class use the namespace prefix? |
16 | 15 | $len = strlen($namespace); |
@@ -3,29 +3,29 @@ |
||
3 | 3 | This file should only ever be loaded on PHP 7+ |
4 | 4 | */ |
5 | 5 | if (PHP_VERSION_ID < 70000) { |
6 | - return; |
|
6 | + return; |
|
7 | 7 | } |
8 | 8 | |
9 | 9 | spl_autoload_register(function ($class) { |
10 | - $namespace = 'ParagonIE_Sodium_'; |
|
11 | - // Does the class use the namespace prefix? |
|
12 | - $len = strlen($namespace); |
|
13 | - if (strncmp($namespace, $class, $len) !== 0) { |
|
14 | - // no, move to the next registered autoloader |
|
15 | - return false; |
|
16 | - } |
|
10 | + $namespace = 'ParagonIE_Sodium_'; |
|
11 | + // Does the class use the namespace prefix? |
|
12 | + $len = strlen($namespace); |
|
13 | + if (strncmp($namespace, $class, $len) !== 0) { |
|
14 | + // no, move to the next registered autoloader |
|
15 | + return false; |
|
16 | + } |
|
17 | 17 | |
18 | - // Get the relative class name |
|
19 | - $relative_class = substr($class, $len); |
|
18 | + // Get the relative class name |
|
19 | + $relative_class = substr($class, $len); |
|
20 | 20 | |
21 | - // Replace the namespace prefix with the base directory, replace namespace |
|
22 | - // separators with directory separators in the relative class name, append |
|
23 | - // with .php |
|
24 | - $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php'; |
|
25 | - // if the file exists, require it |
|
26 | - if (file_exists($file)) { |
|
27 | - require_once $file; |
|
28 | - return true; |
|
29 | - } |
|
30 | - return false; |
|
21 | + // Replace the namespace prefix with the base directory, replace namespace |
|
22 | + // separators with directory separators in the relative class name, append |
|
23 | + // with .php |
|
24 | + $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php'; |
|
25 | + // if the file exists, require it |
|
26 | + if (file_exists($file)) { |
|
27 | + require_once $file; |
|
28 | + return true; |
|
29 | + } |
|
30 | + return false; |
|
31 | 31 | }); |
@@ -2,28 +2,28 @@ |
||
2 | 2 | /* |
3 | 3 | This file should only ever be loaded on PHP 7+ |
4 | 4 | */ |
5 | -if (PHP_VERSION_ID < 70000) { |
|
5 | +if ( PHP_VERSION_ID < 70000 ) { |
|
6 | 6 | return; |
7 | 7 | } |
8 | 8 | |
9 | -spl_autoload_register(function ($class) { |
|
9 | +spl_autoload_register( function( $class ) { |
|
10 | 10 | $namespace = 'ParagonIE_Sodium_'; |
11 | 11 | // Does the class use the namespace prefix? |
12 | - $len = strlen($namespace); |
|
13 | - if (strncmp($namespace, $class, $len) !== 0) { |
|
12 | + $len = strlen( $namespace ); |
|
13 | + if ( strncmp( $namespace, $class, $len ) !== 0 ) { |
|
14 | 14 | // no, move to the next registered autoloader |
15 | 15 | return false; |
16 | 16 | } |
17 | 17 | |
18 | 18 | // Get the relative class name |
19 | - $relative_class = substr($class, $len); |
|
19 | + $relative_class = substr( $class, $len ); |
|
20 | 20 | |
21 | 21 | // Replace the namespace prefix with the base directory, replace namespace |
22 | 22 | // separators with directory separators in the relative class name, append |
23 | 23 | // with .php |
24 | - $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php'; |
|
24 | + $file = dirname( __FILE__ ) . '/src/' . str_replace( '_', '/', $relative_class ) . '.php'; |
|
25 | 25 | // if the file exists, require it |
26 | - if (file_exists($file)) { |
|
26 | + if ( file_exists( $file ) ) { |
|
27 | 27 | require_once $file; |
28 | 28 | return true; |
29 | 29 | } |
@@ -3,6 +3,6 @@ |
||
3 | 3 | require_once (dirname(__FILE__) . '/vendor/autoload.php'); |
4 | 4 | |
5 | 5 | if (PHP_VERSION_ID >= 50300) { |
6 | - require_once (dirname(__FILE__) . '/tests/phpunit-shim.php'); |
|
6 | + require_once (dirname(__FILE__) . '/tests/phpunit-shim.php'); |
|
7 | 7 | } |
8 | 8 | require_once (dirname(__FILE__) . '/autoload.php'); |
@@ -1,8 +1,8 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once (dirname(__FILE__) . '/vendor/autoload.php'); |
|
3 | +require_once ( dirname( __FILE__ ) . '/vendor/autoload.php' ); |
|
4 | 4 | |
5 | -if (PHP_VERSION_ID >= 50300) { |
|
6 | - require_once (dirname(__FILE__) . '/tests/phpunit-shim.php'); |
|
5 | +if ( PHP_VERSION_ID >= 50300 ) { |
|
6 | + require_once ( dirname( __FILE__ ) . '/tests/phpunit-shim.php' ); |
|
7 | 7 | } |
8 | -require_once (dirname(__FILE__) . '/autoload.php'); |
|
8 | +require_once ( dirname( __FILE__ ) . '/autoload.php' ); |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Crypto', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -14,1642 +14,1642 @@ discard block |
||
14 | 14 | */ |
15 | 15 | abstract class ParagonIE_Sodium_Crypto |
16 | 16 | { |
17 | - const aead_chacha20poly1305_KEYBYTES = 32; |
|
18 | - const aead_chacha20poly1305_NSECBYTES = 0; |
|
19 | - const aead_chacha20poly1305_NPUBBYTES = 8; |
|
20 | - const aead_chacha20poly1305_ABYTES = 16; |
|
21 | - |
|
22 | - const aead_chacha20poly1305_IETF_KEYBYTES = 32; |
|
23 | - const aead_chacha20poly1305_IETF_NSECBYTES = 0; |
|
24 | - const aead_chacha20poly1305_IETF_NPUBBYTES = 12; |
|
25 | - const aead_chacha20poly1305_IETF_ABYTES = 16; |
|
26 | - |
|
27 | - const aead_xchacha20poly1305_IETF_KEYBYTES = 32; |
|
28 | - const aead_xchacha20poly1305_IETF_NSECBYTES = 0; |
|
29 | - const aead_xchacha20poly1305_IETF_NPUBBYTES = 24; |
|
30 | - const aead_xchacha20poly1305_IETF_ABYTES = 16; |
|
31 | - |
|
32 | - const box_curve25519xsalsa20poly1305_SEEDBYTES = 32; |
|
33 | - const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32; |
|
34 | - const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32; |
|
35 | - const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32; |
|
36 | - const box_curve25519xsalsa20poly1305_NONCEBYTES = 24; |
|
37 | - const box_curve25519xsalsa20poly1305_MACBYTES = 16; |
|
38 | - const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16; |
|
39 | - const box_curve25519xsalsa20poly1305_ZEROBYTES = 32; |
|
40 | - |
|
41 | - const onetimeauth_poly1305_BYTES = 16; |
|
42 | - const onetimeauth_poly1305_KEYBYTES = 32; |
|
43 | - |
|
44 | - const secretbox_xsalsa20poly1305_KEYBYTES = 32; |
|
45 | - const secretbox_xsalsa20poly1305_NONCEBYTES = 24; |
|
46 | - const secretbox_xsalsa20poly1305_MACBYTES = 16; |
|
47 | - const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16; |
|
48 | - const secretbox_xsalsa20poly1305_ZEROBYTES = 32; |
|
49 | - |
|
50 | - const secretbox_xchacha20poly1305_KEYBYTES = 32; |
|
51 | - const secretbox_xchacha20poly1305_NONCEBYTES = 24; |
|
52 | - const secretbox_xchacha20poly1305_MACBYTES = 16; |
|
53 | - const secretbox_xchacha20poly1305_BOXZEROBYTES = 16; |
|
54 | - const secretbox_xchacha20poly1305_ZEROBYTES = 32; |
|
55 | - |
|
56 | - const stream_salsa20_KEYBYTES = 32; |
|
57 | - |
|
58 | - /** |
|
59 | - * AEAD Decryption with ChaCha20-Poly1305 |
|
60 | - * |
|
61 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
62 | - * |
|
63 | - * @param string $message |
|
64 | - * @param string $ad |
|
65 | - * @param string $nonce |
|
66 | - * @param string $key |
|
67 | - * @return string |
|
68 | - * @throws SodiumException |
|
69 | - * @throws TypeError |
|
70 | - */ |
|
71 | - public static function aead_chacha20poly1305_decrypt( |
|
72 | - $message = '', |
|
73 | - $ad = '', |
|
74 | - $nonce = '', |
|
75 | - $key = '' |
|
76 | - ) { |
|
77 | - /** @var int $len - Length of message (ciphertext + MAC) */ |
|
78 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
79 | - |
|
80 | - /** @var int $clen - Length of ciphertext */ |
|
81 | - $clen = $len - self::aead_chacha20poly1305_ABYTES; |
|
82 | - |
|
83 | - /** @var int $adlen - Length of associated data */ |
|
84 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
85 | - |
|
86 | - /** @var string $mac - Message authentication code */ |
|
87 | - $mac = ParagonIE_Sodium_Core_Util::substr( |
|
88 | - $message, |
|
89 | - $clen, |
|
90 | - self::aead_chacha20poly1305_ABYTES |
|
91 | - ); |
|
92 | - |
|
93 | - /** @var string $ciphertext - The encrypted message (sans MAC) */ |
|
94 | - $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 0, $clen); |
|
95 | - |
|
96 | - /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
97 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
98 | - 32, |
|
99 | - $nonce, |
|
100 | - $key |
|
101 | - ); |
|
102 | - |
|
103 | - /* Recalculate the Poly1305 authentication tag (MAC): */ |
|
104 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
105 | - try { |
|
106 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
107 | - } catch (SodiumException $ex) { |
|
108 | - $block0 = null; |
|
109 | - } |
|
110 | - $state->update($ad); |
|
111 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
112 | - $state->update($ciphertext); |
|
113 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
114 | - $computed_mac = $state->finish(); |
|
115 | - |
|
116 | - /* Compare the given MAC with the recalculated MAC: */ |
|
117 | - if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { |
|
118 | - throw new SodiumException('Invalid MAC'); |
|
119 | - } |
|
120 | - |
|
121 | - // Here, we know that the MAC is valid, so we decrypt and return the plaintext |
|
122 | - return ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
123 | - $ciphertext, |
|
124 | - $nonce, |
|
125 | - $key, |
|
126 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
127 | - ); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * AEAD Encryption with ChaCha20-Poly1305 |
|
132 | - * |
|
133 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
134 | - * |
|
135 | - * @param string $message |
|
136 | - * @param string $ad |
|
137 | - * @param string $nonce |
|
138 | - * @param string $key |
|
139 | - * @return string |
|
140 | - * @throws SodiumException |
|
141 | - * @throws TypeError |
|
142 | - */ |
|
143 | - public static function aead_chacha20poly1305_encrypt( |
|
144 | - $message = '', |
|
145 | - $ad = '', |
|
146 | - $nonce = '', |
|
147 | - $key = '' |
|
148 | - ) { |
|
149 | - /** @var int $len - Length of the plaintext message */ |
|
150 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
151 | - |
|
152 | - /** @var int $adlen - Length of the associated data */ |
|
153 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
154 | - |
|
155 | - /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
156 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
157 | - 32, |
|
158 | - $nonce, |
|
159 | - $key |
|
160 | - ); |
|
161 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
162 | - try { |
|
163 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
164 | - } catch (SodiumException $ex) { |
|
165 | - $block0 = null; |
|
166 | - } |
|
167 | - |
|
168 | - /** @var string $ciphertext - Raw encrypted data */ |
|
169 | - $ciphertext = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
170 | - $message, |
|
171 | - $nonce, |
|
172 | - $key, |
|
173 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
174 | - ); |
|
175 | - |
|
176 | - $state->update($ad); |
|
177 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
178 | - $state->update($ciphertext); |
|
179 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
|
180 | - return $ciphertext . $state->finish(); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|
185 | - * |
|
186 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
187 | - * |
|
188 | - * @param string $message |
|
189 | - * @param string $ad |
|
190 | - * @param string $nonce |
|
191 | - * @param string $key |
|
192 | - * @return string |
|
193 | - * @throws SodiumException |
|
194 | - * @throws TypeError |
|
195 | - */ |
|
196 | - public static function aead_chacha20poly1305_ietf_decrypt( |
|
197 | - $message = '', |
|
198 | - $ad = '', |
|
199 | - $nonce = '', |
|
200 | - $key = '' |
|
201 | - ) { |
|
202 | - /** @var int $adlen - Length of associated data */ |
|
203 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
204 | - |
|
205 | - /** @var int $len - Length of message (ciphertext + MAC) */ |
|
206 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
207 | - |
|
208 | - /** @var int $clen - Length of ciphertext */ |
|
209 | - $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES; |
|
210 | - |
|
211 | - /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
212 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( |
|
213 | - 32, |
|
214 | - $nonce, |
|
215 | - $key |
|
216 | - ); |
|
217 | - |
|
218 | - /** @var string $mac - Message authentication code */ |
|
219 | - $mac = ParagonIE_Sodium_Core_Util::substr( |
|
220 | - $message, |
|
221 | - $len - self::aead_chacha20poly1305_IETF_ABYTES, |
|
222 | - self::aead_chacha20poly1305_IETF_ABYTES |
|
223 | - ); |
|
224 | - |
|
225 | - /** @var string $ciphertext - The encrypted message (sans MAC) */ |
|
226 | - $ciphertext = ParagonIE_Sodium_Core_Util::substr( |
|
227 | - $message, |
|
228 | - 0, |
|
229 | - $len - self::aead_chacha20poly1305_IETF_ABYTES |
|
230 | - ); |
|
231 | - |
|
232 | - /* Recalculate the Poly1305 authentication tag (MAC): */ |
|
233 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
234 | - try { |
|
235 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
236 | - } catch (SodiumException $ex) { |
|
237 | - $block0 = null; |
|
238 | - } |
|
239 | - $state->update($ad); |
|
240 | - $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); |
|
241 | - $state->update($ciphertext); |
|
242 | - $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf)); |
|
243 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
244 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
245 | - $computed_mac = $state->finish(); |
|
246 | - |
|
247 | - /* Compare the given MAC with the recalculated MAC: */ |
|
248 | - if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { |
|
249 | - throw new SodiumException('Invalid MAC'); |
|
250 | - } |
|
251 | - |
|
252 | - // Here, we know that the MAC is valid, so we decrypt and return the plaintext |
|
253 | - return ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
254 | - $ciphertext, |
|
255 | - $nonce, |
|
256 | - $key, |
|
257 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
258 | - ); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|
263 | - * |
|
264 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
265 | - * |
|
266 | - * @param string $message |
|
267 | - * @param string $ad |
|
268 | - * @param string $nonce |
|
269 | - * @param string $key |
|
270 | - * @return string |
|
271 | - * @throws SodiumException |
|
272 | - * @throws TypeError |
|
273 | - */ |
|
274 | - public static function aead_chacha20poly1305_ietf_encrypt( |
|
275 | - $message = '', |
|
276 | - $ad = '', |
|
277 | - $nonce = '', |
|
278 | - $key = '' |
|
279 | - ) { |
|
280 | - /** @var int $len - Length of the plaintext message */ |
|
281 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
282 | - |
|
283 | - /** @var int $adlen - Length of the associated data */ |
|
284 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
285 | - |
|
286 | - /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
287 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( |
|
288 | - 32, |
|
289 | - $nonce, |
|
290 | - $key |
|
291 | - ); |
|
292 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
293 | - try { |
|
294 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
295 | - } catch (SodiumException $ex) { |
|
296 | - $block0 = null; |
|
297 | - } |
|
298 | - |
|
299 | - /** @var string $ciphertext - Raw encrypted data */ |
|
300 | - $ciphertext = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
301 | - $message, |
|
302 | - $nonce, |
|
303 | - $key, |
|
304 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
305 | - ); |
|
306 | - |
|
307 | - $state->update($ad); |
|
308 | - $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); |
|
309 | - $state->update($ciphertext); |
|
310 | - $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf))); |
|
311 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
312 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
|
313 | - return $ciphertext . $state->finish(); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|
318 | - * |
|
319 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
320 | - * |
|
321 | - * @param string $message |
|
322 | - * @param string $ad |
|
323 | - * @param string $nonce |
|
324 | - * @param string $key |
|
325 | - * @return string |
|
326 | - * @throws SodiumException |
|
327 | - * @throws TypeError |
|
328 | - */ |
|
329 | - public static function aead_xchacha20poly1305_ietf_decrypt( |
|
330 | - $message = '', |
|
331 | - $ad = '', |
|
332 | - $nonce = '', |
|
333 | - $key = '' |
|
334 | - ) { |
|
335 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
336 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
337 | - $key |
|
338 | - ); |
|
339 | - $nonceLast = "\x00\x00\x00\x00" . |
|
340 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
341 | - |
|
342 | - return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey); |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|
347 | - * |
|
348 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
349 | - * |
|
350 | - * @param string $message |
|
351 | - * @param string $ad |
|
352 | - * @param string $nonce |
|
353 | - * @param string $key |
|
354 | - * @return string |
|
355 | - * @throws SodiumException |
|
356 | - * @throws TypeError |
|
357 | - */ |
|
358 | - public static function aead_xchacha20poly1305_ietf_encrypt( |
|
359 | - $message = '', |
|
360 | - $ad = '', |
|
361 | - $nonce = '', |
|
362 | - $key = '' |
|
363 | - ) { |
|
364 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
365 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
366 | - $key |
|
367 | - ); |
|
368 | - $nonceLast = "\x00\x00\x00\x00" . |
|
369 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
370 | - |
|
371 | - return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey); |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512) |
|
376 | - * |
|
377 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
378 | - * |
|
379 | - * @param string $message |
|
380 | - * @param string $key |
|
381 | - * @return string |
|
382 | - * @throws TypeError |
|
383 | - */ |
|
384 | - public static function auth($message, $key) |
|
385 | - { |
|
386 | - return ParagonIE_Sodium_Core_Util::substr( |
|
387 | - hash_hmac('sha512', $message, $key, true), |
|
388 | - 0, |
|
389 | - 32 |
|
390 | - ); |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * HMAC-SHA-512-256 validation. Constant-time via hash_equals(). |
|
395 | - * |
|
396 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
397 | - * |
|
398 | - * @param string $mac |
|
399 | - * @param string $message |
|
400 | - * @param string $key |
|
401 | - * @return bool |
|
402 | - * @throws SodiumException |
|
403 | - * @throws TypeError |
|
404 | - */ |
|
405 | - public static function auth_verify($mac, $message, $key) |
|
406 | - { |
|
407 | - return ParagonIE_Sodium_Core_Util::hashEquals( |
|
408 | - $mac, |
|
409 | - self::auth($message, $key) |
|
410 | - ); |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption |
|
415 | - * |
|
416 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
417 | - * |
|
418 | - * @param string $plaintext |
|
419 | - * @param string $nonce |
|
420 | - * @param string $keypair |
|
421 | - * @return string |
|
422 | - * @throws SodiumException |
|
423 | - * @throws TypeError |
|
424 | - */ |
|
425 | - public static function box($plaintext, $nonce, $keypair) |
|
426 | - { |
|
427 | - $c = self::secretbox( |
|
428 | - $plaintext, |
|
429 | - $nonce, |
|
430 | - self::box_beforenm( |
|
431 | - self::box_secretkey($keypair), |
|
432 | - self::box_publickey($keypair) |
|
433 | - ) |
|
434 | - ); |
|
435 | - return $c; |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair. |
|
440 | - * |
|
441 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
442 | - * |
|
443 | - * @param string $message |
|
444 | - * @param string $publicKey |
|
445 | - * @return string |
|
446 | - * @throws SodiumException |
|
447 | - * @throws TypeError |
|
448 | - */ |
|
449 | - public static function box_seal($message, $publicKey) |
|
450 | - { |
|
451 | - /** @var string $ephemeralKeypair */ |
|
452 | - $ephemeralKeypair = self::box_keypair(); |
|
453 | - |
|
454 | - /** @var string $ephemeralSK */ |
|
455 | - $ephemeralSK = self::box_secretkey($ephemeralKeypair); |
|
456 | - |
|
457 | - /** @var string $ephemeralPK */ |
|
458 | - $ephemeralPK = self::box_publickey($ephemeralKeypair); |
|
459 | - |
|
460 | - /** @var string $nonce */ |
|
461 | - $nonce = self::generichash( |
|
462 | - $ephemeralPK . $publicKey, |
|
463 | - '', |
|
464 | - 24 |
|
465 | - ); |
|
466 | - |
|
467 | - /** @var string $keypair - The combined keypair used in crypto_box() */ |
|
468 | - $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey); |
|
469 | - |
|
470 | - /** @var string $ciphertext Ciphertext + MAC from crypto_box */ |
|
471 | - $ciphertext = self::box($message, $nonce, $keypair); |
|
472 | - try { |
|
473 | - ParagonIE_Sodium_Compat::memzero($ephemeralKeypair); |
|
474 | - ParagonIE_Sodium_Compat::memzero($ephemeralSK); |
|
475 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
476 | - } catch (SodiumException $ex) { |
|
477 | - $ephemeralKeypair = null; |
|
478 | - $ephemeralSK = null; |
|
479 | - $nonce = null; |
|
480 | - } |
|
481 | - return $ephemeralPK . $ciphertext; |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Opens a message encrypted via box_seal(). |
|
486 | - * |
|
487 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
488 | - * |
|
489 | - * @param string $message |
|
490 | - * @param string $keypair |
|
491 | - * @return string |
|
492 | - * @throws SodiumException |
|
493 | - * @throws TypeError |
|
494 | - */ |
|
495 | - public static function box_seal_open($message, $keypair) |
|
496 | - { |
|
497 | - /** @var string $ephemeralPK */ |
|
498 | - $ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32); |
|
499 | - |
|
500 | - /** @var string $ciphertext (ciphertext + MAC) */ |
|
501 | - $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 32); |
|
502 | - |
|
503 | - /** @var string $secretKey */ |
|
504 | - $secretKey = self::box_secretkey($keypair); |
|
505 | - |
|
506 | - /** @var string $publicKey */ |
|
507 | - $publicKey = self::box_publickey($keypair); |
|
508 | - |
|
509 | - /** @var string $nonce */ |
|
510 | - $nonce = self::generichash( |
|
511 | - $ephemeralPK . $publicKey, |
|
512 | - '', |
|
513 | - 24 |
|
514 | - ); |
|
515 | - |
|
516 | - /** @var string $keypair */ |
|
517 | - $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK); |
|
518 | - |
|
519 | - /** @var string $m */ |
|
520 | - $m = self::box_open($ciphertext, $nonce, $keypair); |
|
521 | - try { |
|
522 | - ParagonIE_Sodium_Compat::memzero($secretKey); |
|
523 | - ParagonIE_Sodium_Compat::memzero($ephemeralPK); |
|
524 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
525 | - } catch (SodiumException $ex) { |
|
526 | - $secretKey = null; |
|
527 | - $ephemeralPK = null; |
|
528 | - $nonce = null; |
|
529 | - } |
|
530 | - return $m; |
|
531 | - } |
|
532 | - |
|
533 | - /** |
|
534 | - * Used by crypto_box() to get the crypto_secretbox() key. |
|
535 | - * |
|
536 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
537 | - * |
|
538 | - * @param string $sk |
|
539 | - * @param string $pk |
|
540 | - * @return string |
|
541 | - * @throws SodiumException |
|
542 | - * @throws TypeError |
|
543 | - */ |
|
544 | - public static function box_beforenm($sk, $pk) |
|
545 | - { |
|
546 | - return ParagonIE_Sodium_Core_HSalsa20::hsalsa20( |
|
547 | - str_repeat("\x00", 16), |
|
548 | - self::scalarmult($sk, $pk) |
|
549 | - ); |
|
550 | - } |
|
551 | - |
|
552 | - /** |
|
553 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
554 | - * |
|
555 | - * @return string |
|
556 | - * @throws Exception |
|
557 | - * @throws SodiumException |
|
558 | - * @throws TypeError |
|
559 | - */ |
|
560 | - public static function box_keypair() |
|
561 | - { |
|
562 | - $sKey = random_bytes(32); |
|
563 | - $pKey = self::scalarmult_base($sKey); |
|
564 | - return $sKey . $pKey; |
|
565 | - } |
|
566 | - |
|
567 | - /** |
|
568 | - * @param string $seed |
|
569 | - * @return string |
|
570 | - * @throws SodiumException |
|
571 | - * @throws TypeError |
|
572 | - */ |
|
573 | - public static function box_seed_keypair($seed) |
|
574 | - { |
|
575 | - $sKey = ParagonIE_Sodium_Core_Util::substr( |
|
576 | - hash('sha512', $seed, true), |
|
577 | - 0, |
|
578 | - 32 |
|
579 | - ); |
|
580 | - $pKey = self::scalarmult_base($sKey); |
|
581 | - return $sKey . $pKey; |
|
582 | - } |
|
583 | - |
|
584 | - /** |
|
585 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
586 | - * |
|
587 | - * @param string $sKey |
|
588 | - * @param string $pKey |
|
589 | - * @return string |
|
590 | - * @throws TypeError |
|
591 | - */ |
|
592 | - public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) |
|
593 | - { |
|
594 | - return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . |
|
595 | - ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32); |
|
596 | - } |
|
597 | - |
|
598 | - /** |
|
599 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
600 | - * |
|
601 | - * @param string $keypair |
|
602 | - * @return string |
|
603 | - * @throws RangeException |
|
604 | - * @throws TypeError |
|
605 | - */ |
|
606 | - public static function box_secretkey($keypair) |
|
607 | - { |
|
608 | - if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { |
|
609 | - throw new RangeException( |
|
610 | - 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
|
611 | - ); |
|
612 | - } |
|
613 | - return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32); |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
618 | - * |
|
619 | - * @param string $keypair |
|
620 | - * @return string |
|
621 | - * @throws RangeException |
|
622 | - * @throws TypeError |
|
623 | - */ |
|
624 | - public static function box_publickey($keypair) |
|
625 | - { |
|
626 | - if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
627 | - throw new RangeException( |
|
628 | - 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
|
629 | - ); |
|
630 | - } |
|
631 | - return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32); |
|
632 | - } |
|
633 | - |
|
634 | - /** |
|
635 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
636 | - * |
|
637 | - * @param string $sKey |
|
638 | - * @return string |
|
639 | - * @throws RangeException |
|
640 | - * @throws SodiumException |
|
641 | - * @throws TypeError |
|
642 | - */ |
|
643 | - public static function box_publickey_from_secretkey($sKey) |
|
644 | - { |
|
645 | - if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { |
|
646 | - throw new RangeException( |
|
647 | - 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.' |
|
648 | - ); |
|
649 | - } |
|
650 | - return self::scalarmult_base($sKey); |
|
651 | - } |
|
652 | - |
|
653 | - /** |
|
654 | - * Decrypt a message encrypted with box(). |
|
655 | - * |
|
656 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
657 | - * |
|
658 | - * @param string $ciphertext |
|
659 | - * @param string $nonce |
|
660 | - * @param string $keypair |
|
661 | - * @return string |
|
662 | - * @throws SodiumException |
|
663 | - * @throws TypeError |
|
664 | - */ |
|
665 | - public static function box_open($ciphertext, $nonce, $keypair) |
|
666 | - { |
|
667 | - return self::secretbox_open( |
|
668 | - $ciphertext, |
|
669 | - $nonce, |
|
670 | - self::box_beforenm( |
|
671 | - self::box_secretkey($keypair), |
|
672 | - self::box_publickey($keypair) |
|
673 | - ) |
|
674 | - ); |
|
675 | - } |
|
676 | - |
|
677 | - /** |
|
678 | - * Calculate a BLAKE2b hash. |
|
679 | - * |
|
680 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
681 | - * |
|
682 | - * @param string $message |
|
683 | - * @param string|null $key |
|
684 | - * @param int $outlen |
|
685 | - * @return string |
|
686 | - * @throws RangeException |
|
687 | - * @throws SodiumException |
|
688 | - * @throws TypeError |
|
689 | - */ |
|
690 | - public static function generichash($message, $key = '', $outlen = 32) |
|
691 | - { |
|
692 | - // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
693 | - ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
694 | - |
|
695 | - $k = null; |
|
696 | - if (!empty($key)) { |
|
697 | - /** @var SplFixedArray $k */ |
|
698 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
699 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
700 | - throw new RangeException('Invalid key size'); |
|
701 | - } |
|
702 | - } |
|
703 | - |
|
704 | - /** @var SplFixedArray $in */ |
|
705 | - $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
706 | - |
|
707 | - /** @var SplFixedArray $ctx */ |
|
708 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen); |
|
709 | - ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count()); |
|
710 | - |
|
711 | - /** @var SplFixedArray $out */ |
|
712 | - $out = new SplFixedArray($outlen); |
|
713 | - $out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out); |
|
714 | - |
|
715 | - /** @var array<int, int> */ |
|
716 | - $outArray = $out->toArray(); |
|
717 | - return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
718 | - } |
|
719 | - |
|
720 | - /** |
|
721 | - * Finalize a BLAKE2b hashing context, returning the hash. |
|
722 | - * |
|
723 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
724 | - * |
|
725 | - * @param string $ctx |
|
726 | - * @param int $outlen |
|
727 | - * @return string |
|
728 | - * @throws SodiumException |
|
729 | - * @throws TypeError |
|
730 | - */ |
|
731 | - public static function generichash_final($ctx, $outlen = 32) |
|
732 | - { |
|
733 | - if (!is_string($ctx)) { |
|
734 | - throw new TypeError('Context must be a string'); |
|
735 | - } |
|
736 | - $out = new SplFixedArray($outlen); |
|
737 | - |
|
738 | - /** @var SplFixedArray $context */ |
|
739 | - $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
740 | - |
|
741 | - /** @var SplFixedArray $out */ |
|
742 | - $out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out); |
|
743 | - |
|
744 | - /** @var array<int, int> */ |
|
745 | - $outArray = $out->toArray(); |
|
746 | - return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
747 | - } |
|
748 | - |
|
749 | - /** |
|
750 | - * Initialize a hashing context for BLAKE2b. |
|
751 | - * |
|
752 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
753 | - * |
|
754 | - * @param string $key |
|
755 | - * @param int $outputLength |
|
756 | - * @return string |
|
757 | - * @throws RangeException |
|
758 | - * @throws SodiumException |
|
759 | - * @throws TypeError |
|
760 | - */ |
|
761 | - public static function generichash_init($key = '', $outputLength = 32) |
|
762 | - { |
|
763 | - // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
764 | - ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
765 | - |
|
766 | - $k = null; |
|
767 | - if (!empty($key)) { |
|
768 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
769 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
770 | - throw new RangeException('Invalid key size'); |
|
771 | - } |
|
772 | - } |
|
773 | - |
|
774 | - /** @var SplFixedArray $ctx */ |
|
775 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength); |
|
776 | - |
|
777 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
|
778 | - } |
|
779 | - |
|
780 | - /** |
|
781 | - * Initialize a hashing context for BLAKE2b. |
|
782 | - * |
|
783 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
784 | - * |
|
785 | - * @param string $key |
|
786 | - * @param int $outputLength |
|
787 | - * @param string $salt |
|
788 | - * @param string $personal |
|
789 | - * @return string |
|
790 | - * @throws RangeException |
|
791 | - * @throws SodiumException |
|
792 | - * @throws TypeError |
|
793 | - */ |
|
794 | - public static function generichash_init_salt_personal( |
|
795 | - $key = '', |
|
796 | - $outputLength = 32, |
|
797 | - $salt = '', |
|
798 | - $personal = '' |
|
799 | - ) { |
|
800 | - // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
801 | - ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
802 | - |
|
803 | - $k = null; |
|
804 | - if (!empty($key)) { |
|
805 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
806 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
807 | - throw new RangeException('Invalid key size'); |
|
808 | - } |
|
809 | - } |
|
810 | - if (!empty($salt)) { |
|
811 | - $s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($salt); |
|
812 | - } else { |
|
813 | - $s = null; |
|
814 | - } |
|
815 | - if (!empty($salt)) { |
|
816 | - $p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($personal); |
|
817 | - } else { |
|
818 | - $p = null; |
|
819 | - } |
|
820 | - |
|
821 | - /** @var SplFixedArray $ctx */ |
|
822 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength, $s, $p); |
|
823 | - |
|
824 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
|
825 | - } |
|
826 | - |
|
827 | - /** |
|
828 | - * Update a hashing context for BLAKE2b with $message |
|
829 | - * |
|
830 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
831 | - * |
|
832 | - * @param string $ctx |
|
833 | - * @param string $message |
|
834 | - * @return string |
|
835 | - * @throws SodiumException |
|
836 | - * @throws TypeError |
|
837 | - */ |
|
838 | - public static function generichash_update($ctx, $message) |
|
839 | - { |
|
840 | - // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
841 | - ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
842 | - |
|
843 | - /** @var SplFixedArray $context */ |
|
844 | - $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
845 | - |
|
846 | - /** @var SplFixedArray $in */ |
|
847 | - $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
848 | - |
|
849 | - ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count()); |
|
850 | - |
|
851 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context); |
|
852 | - } |
|
853 | - |
|
854 | - /** |
|
855 | - * Libsodium's crypto_kx(). |
|
856 | - * |
|
857 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
858 | - * |
|
859 | - * @param string $my_sk |
|
860 | - * @param string $their_pk |
|
861 | - * @param string $client_pk |
|
862 | - * @param string $server_pk |
|
863 | - * @return string |
|
864 | - * @throws SodiumException |
|
865 | - * @throws TypeError |
|
866 | - */ |
|
867 | - public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) |
|
868 | - { |
|
869 | - return ParagonIE_Sodium_Compat::crypto_generichash( |
|
870 | - ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) . |
|
871 | - $client_pk . |
|
872 | - $server_pk |
|
873 | - ); |
|
874 | - } |
|
875 | - |
|
876 | - /** |
|
877 | - * ECDH over Curve25519 |
|
878 | - * |
|
879 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
880 | - * |
|
881 | - * @param string $sKey |
|
882 | - * @param string $pKey |
|
883 | - * @return string |
|
884 | - * |
|
885 | - * @throws SodiumException |
|
886 | - * @throws TypeError |
|
887 | - */ |
|
888 | - public static function scalarmult($sKey, $pKey) |
|
889 | - { |
|
890 | - $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); |
|
891 | - self::scalarmult_throw_if_zero($q); |
|
892 | - return $q; |
|
893 | - } |
|
894 | - |
|
895 | - /** |
|
896 | - * ECDH over Curve25519, using the basepoint. |
|
897 | - * Used to get a secret key from a public key. |
|
898 | - * |
|
899 | - * @param string $secret |
|
900 | - * @return string |
|
901 | - * |
|
902 | - * @throws SodiumException |
|
903 | - * @throws TypeError |
|
904 | - */ |
|
905 | - public static function scalarmult_base($secret) |
|
906 | - { |
|
907 | - $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); |
|
908 | - self::scalarmult_throw_if_zero($q); |
|
909 | - return $q; |
|
910 | - } |
|
911 | - |
|
912 | - /** |
|
913 | - * This throws an Error if a zero public key was passed to the function. |
|
914 | - * |
|
915 | - * @param string $q |
|
916 | - * @return void |
|
917 | - * @throws SodiumException |
|
918 | - * @throws TypeError |
|
919 | - */ |
|
920 | - protected static function scalarmult_throw_if_zero($q) |
|
921 | - { |
|
922 | - $d = 0; |
|
923 | - for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { |
|
924 | - $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); |
|
925 | - } |
|
926 | - |
|
927 | - /* branch-free variant of === 0 */ |
|
928 | - if (-(1 & (($d - 1) >> 8))) { |
|
929 | - throw new SodiumException('Zero public key is not allowed'); |
|
930 | - } |
|
931 | - } |
|
932 | - |
|
933 | - /** |
|
934 | - * XSalsa20-Poly1305 authenticated symmetric-key encryption. |
|
935 | - * |
|
936 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
937 | - * |
|
938 | - * @param string $plaintext |
|
939 | - * @param string $nonce |
|
940 | - * @param string $key |
|
941 | - * @return string |
|
942 | - * @throws SodiumException |
|
943 | - * @throws TypeError |
|
944 | - */ |
|
945 | - public static function secretbox($plaintext, $nonce, $key) |
|
946 | - { |
|
947 | - /** @var string $subkey */ |
|
948 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
949 | - |
|
950 | - /** @var string $block0 */ |
|
951 | - $block0 = str_repeat("\x00", 32); |
|
952 | - |
|
953 | - /** @var int $mlen - Length of the plaintext message */ |
|
954 | - $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
955 | - $mlen0 = $mlen; |
|
956 | - if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
957 | - $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
958 | - } |
|
959 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
960 | - |
|
961 | - /** @var string $block0 */ |
|
962 | - $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
|
963 | - $block0, |
|
964 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
965 | - $subkey |
|
966 | - ); |
|
967 | - |
|
968 | - /** @var string $c */ |
|
969 | - $c = ParagonIE_Sodium_Core_Util::substr( |
|
970 | - $block0, |
|
971 | - self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
972 | - ); |
|
973 | - if ($mlen > $mlen0) { |
|
974 | - $c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
975 | - ParagonIE_Sodium_Core_Util::substr( |
|
976 | - $plaintext, |
|
977 | - self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
978 | - ), |
|
979 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
980 | - 1, |
|
981 | - $subkey |
|
982 | - ); |
|
983 | - } |
|
984 | - $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
985 | - ParagonIE_Sodium_Core_Util::substr( |
|
986 | - $block0, |
|
987 | - 0, |
|
988 | - self::onetimeauth_poly1305_KEYBYTES |
|
989 | - ) |
|
990 | - ); |
|
991 | - try { |
|
992 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
993 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
994 | - } catch (SodiumException $ex) { |
|
995 | - $block0 = null; |
|
996 | - $subkey = null; |
|
997 | - } |
|
998 | - |
|
999 | - $state->update($c); |
|
1000 | - |
|
1001 | - /** @var string $c - MAC || ciphertext */ |
|
1002 | - $c = $state->finish() . $c; |
|
1003 | - unset($state); |
|
1004 | - |
|
1005 | - return $c; |
|
1006 | - } |
|
1007 | - |
|
1008 | - /** |
|
1009 | - * Decrypt a ciphertext generated via secretbox(). |
|
1010 | - * |
|
1011 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1012 | - * |
|
1013 | - * @param string $ciphertext |
|
1014 | - * @param string $nonce |
|
1015 | - * @param string $key |
|
1016 | - * @return string |
|
1017 | - * @throws SodiumException |
|
1018 | - * @throws TypeError |
|
1019 | - */ |
|
1020 | - public static function secretbox_open($ciphertext, $nonce, $key) |
|
1021 | - { |
|
1022 | - /** @var string $mac */ |
|
1023 | - $mac = ParagonIE_Sodium_Core_Util::substr( |
|
1024 | - $ciphertext, |
|
1025 | - 0, |
|
1026 | - self::secretbox_xsalsa20poly1305_MACBYTES |
|
1027 | - ); |
|
1028 | - |
|
1029 | - /** @var string $c */ |
|
1030 | - $c = ParagonIE_Sodium_Core_Util::substr( |
|
1031 | - $ciphertext, |
|
1032 | - self::secretbox_xsalsa20poly1305_MACBYTES |
|
1033 | - ); |
|
1034 | - |
|
1035 | - /** @var int $clen */ |
|
1036 | - $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
1037 | - |
|
1038 | - /** @var string $subkey */ |
|
1039 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
1040 | - |
|
1041 | - /** @var string $block0 */ |
|
1042 | - $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
|
1043 | - 64, |
|
1044 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1045 | - $subkey |
|
1046 | - ); |
|
1047 | - $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
|
1048 | - $mac, |
|
1049 | - $c, |
|
1050 | - ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
1051 | - ); |
|
1052 | - if (!$verified) { |
|
1053 | - try { |
|
1054 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
1055 | - } catch (SodiumException $ex) { |
|
1056 | - $subkey = null; |
|
1057 | - } |
|
1058 | - throw new SodiumException('Invalid MAC'); |
|
1059 | - } |
|
1060 | - |
|
1061 | - /** @var string $m - Decrypted message */ |
|
1062 | - $m = ParagonIE_Sodium_Core_Util::xorStrings( |
|
1063 | - ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES), |
|
1064 | - ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES) |
|
1065 | - ); |
|
1066 | - if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
1067 | - // We had more than 1 block, so let's continue to decrypt the rest. |
|
1068 | - $m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
1069 | - ParagonIE_Sodium_Core_Util::substr( |
|
1070 | - $c, |
|
1071 | - self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
1072 | - ), |
|
1073 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1074 | - 1, |
|
1075 | - (string) $subkey |
|
1076 | - ); |
|
1077 | - } |
|
1078 | - return $m; |
|
1079 | - } |
|
1080 | - |
|
1081 | - /** |
|
1082 | - * XChaCha20-Poly1305 authenticated symmetric-key encryption. |
|
1083 | - * |
|
1084 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1085 | - * |
|
1086 | - * @param string $plaintext |
|
1087 | - * @param string $nonce |
|
1088 | - * @param string $key |
|
1089 | - * @return string |
|
1090 | - * @throws SodiumException |
|
1091 | - * @throws TypeError |
|
1092 | - */ |
|
1093 | - public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) |
|
1094 | - { |
|
1095 | - /** @var string $subkey */ |
|
1096 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
1097 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
1098 | - $key |
|
1099 | - ); |
|
1100 | - $nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
1101 | - |
|
1102 | - /** @var string $block0 */ |
|
1103 | - $block0 = str_repeat("\x00", 32); |
|
1104 | - |
|
1105 | - /** @var int $mlen - Length of the plaintext message */ |
|
1106 | - $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
1107 | - $mlen0 = $mlen; |
|
1108 | - if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
1109 | - $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES; |
|
1110 | - } |
|
1111 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
1112 | - |
|
1113 | - /** @var string $block0 */ |
|
1114 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
1115 | - $block0, |
|
1116 | - $nonceLast, |
|
1117 | - $subkey |
|
1118 | - ); |
|
1119 | - |
|
1120 | - /** @var string $c */ |
|
1121 | - $c = ParagonIE_Sodium_Core_Util::substr( |
|
1122 | - $block0, |
|
1123 | - self::secretbox_xchacha20poly1305_ZEROBYTES |
|
1124 | - ); |
|
1125 | - if ($mlen > $mlen0) { |
|
1126 | - $c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
1127 | - ParagonIE_Sodium_Core_Util::substr( |
|
1128 | - $plaintext, |
|
1129 | - self::secretbox_xchacha20poly1305_ZEROBYTES |
|
1130 | - ), |
|
1131 | - $nonceLast, |
|
1132 | - $subkey, |
|
1133 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1134 | - ); |
|
1135 | - } |
|
1136 | - $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
1137 | - ParagonIE_Sodium_Core_Util::substr( |
|
1138 | - $block0, |
|
1139 | - 0, |
|
1140 | - self::onetimeauth_poly1305_KEYBYTES |
|
1141 | - ) |
|
1142 | - ); |
|
1143 | - try { |
|
1144 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
1145 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
1146 | - } catch (SodiumException $ex) { |
|
1147 | - $block0 = null; |
|
1148 | - $subkey = null; |
|
1149 | - } |
|
1150 | - |
|
1151 | - $state->update($c); |
|
1152 | - |
|
1153 | - /** @var string $c - MAC || ciphertext */ |
|
1154 | - $c = $state->finish() . $c; |
|
1155 | - unset($state); |
|
1156 | - |
|
1157 | - return $c; |
|
1158 | - } |
|
1159 | - |
|
1160 | - /** |
|
1161 | - * Decrypt a ciphertext generated via secretbox_xchacha20poly1305(). |
|
1162 | - * |
|
1163 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1164 | - * |
|
1165 | - * @param string $ciphertext |
|
1166 | - * @param string $nonce |
|
1167 | - * @param string $key |
|
1168 | - * @return string |
|
1169 | - * @throws SodiumException |
|
1170 | - * @throws TypeError |
|
1171 | - */ |
|
1172 | - public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
|
1173 | - { |
|
1174 | - /** @var string $mac */ |
|
1175 | - $mac = ParagonIE_Sodium_Core_Util::substr( |
|
1176 | - $ciphertext, |
|
1177 | - 0, |
|
1178 | - self::secretbox_xchacha20poly1305_MACBYTES |
|
1179 | - ); |
|
1180 | - |
|
1181 | - /** @var string $c */ |
|
1182 | - $c = ParagonIE_Sodium_Core_Util::substr( |
|
1183 | - $ciphertext, |
|
1184 | - self::secretbox_xchacha20poly1305_MACBYTES |
|
1185 | - ); |
|
1186 | - |
|
1187 | - /** @var int $clen */ |
|
1188 | - $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
1189 | - |
|
1190 | - /** @var string $subkey */ |
|
1191 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key); |
|
1192 | - |
|
1193 | - /** @var string $block0 */ |
|
1194 | - $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
1195 | - 64, |
|
1196 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1197 | - $subkey |
|
1198 | - ); |
|
1199 | - $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
|
1200 | - $mac, |
|
1201 | - $c, |
|
1202 | - ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
1203 | - ); |
|
1204 | - |
|
1205 | - if (!$verified) { |
|
1206 | - try { |
|
1207 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
1208 | - } catch (SodiumException $ex) { |
|
1209 | - $subkey = null; |
|
1210 | - } |
|
1211 | - throw new SodiumException('Invalid MAC'); |
|
1212 | - } |
|
1213 | - |
|
1214 | - /** @var string $m - Decrypted message */ |
|
1215 | - $m = ParagonIE_Sodium_Core_Util::xorStrings( |
|
1216 | - ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES), |
|
1217 | - ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES) |
|
1218 | - ); |
|
1219 | - |
|
1220 | - if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
1221 | - // We had more than 1 block, so let's continue to decrypt the rest. |
|
1222 | - $m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
1223 | - ParagonIE_Sodium_Core_Util::substr( |
|
1224 | - $c, |
|
1225 | - self::secretbox_xchacha20poly1305_ZEROBYTES |
|
1226 | - ), |
|
1227 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1228 | - (string) $subkey, |
|
1229 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1230 | - ); |
|
1231 | - } |
|
1232 | - return $m; |
|
1233 | - } |
|
1234 | - |
|
1235 | - /** |
|
1236 | - * @param string $key |
|
1237 | - * @return array<int, string> Returns a state and a header. |
|
1238 | - * @throws Exception |
|
1239 | - * @throws SodiumException |
|
1240 | - */ |
|
1241 | - public static function secretstream_xchacha20poly1305_init_push($key) |
|
1242 | - { |
|
1243 | - # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES); |
|
1244 | - $out = random_bytes(24); |
|
1245 | - |
|
1246 | - # crypto_core_hchacha20(state->k, out, k, NULL); |
|
1247 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20($out, $key); |
|
1248 | - $state = new ParagonIE_Sodium_Core_SecretStream_State( |
|
1249 | - $subkey, |
|
1250 | - ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4) |
|
1251 | - ); |
|
1252 | - |
|
1253 | - # _crypto_secretstream_xchacha20poly1305_counter_reset(state); |
|
1254 | - $state->counterReset(); |
|
1255 | - |
|
1256 | - # memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES, |
|
1257 | - # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
|
1258 | - # memset(state->_pad, 0, sizeof state->_pad); |
|
1259 | - return array( |
|
1260 | - $state->toString(), |
|
1261 | - $out |
|
1262 | - ); |
|
1263 | - } |
|
1264 | - |
|
1265 | - /** |
|
1266 | - * @param string $key |
|
1267 | - * @param string $header |
|
1268 | - * @return string Returns a state. |
|
1269 | - * @throws Exception |
|
1270 | - */ |
|
1271 | - public static function secretstream_xchacha20poly1305_init_pull($key, $header) |
|
1272 | - { |
|
1273 | - # crypto_core_hchacha20(state->k, in, k, NULL); |
|
1274 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
1275 | - ParagonIE_Sodium_Core_Util::substr($header, 0, 16), |
|
1276 | - $key |
|
1277 | - ); |
|
1278 | - $state = new ParagonIE_Sodium_Core_SecretStream_State( |
|
1279 | - $subkey, |
|
1280 | - ParagonIE_Sodium_Core_Util::substr($header, 16) |
|
1281 | - ); |
|
1282 | - $state->counterReset(); |
|
1283 | - # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES, |
|
1284 | - # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
|
1285 | - # memset(state->_pad, 0, sizeof state->_pad); |
|
1286 | - # return 0; |
|
1287 | - return $state->toString(); |
|
1288 | - } |
|
1289 | - |
|
1290 | - /** |
|
1291 | - * @param string $state |
|
1292 | - * @param string $msg |
|
1293 | - * @param string $aad |
|
1294 | - * @param int $tag |
|
1295 | - * @return string |
|
1296 | - * @throws SodiumException |
|
1297 | - */ |
|
1298 | - public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) |
|
1299 | - { |
|
1300 | - $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
|
1301 | - # crypto_onetimeauth_poly1305_state poly1305_state; |
|
1302 | - # unsigned char block[64U]; |
|
1303 | - # unsigned char slen[8U]; |
|
1304 | - # unsigned char *c; |
|
1305 | - # unsigned char *mac; |
|
1306 | - |
|
1307 | - $msglen = ParagonIE_Sodium_Core_Util::strlen($msg); |
|
1308 | - $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); |
|
1309 | - |
|
1310 | - if ((($msglen + 63) >> 6) > 0xfffffffe) { |
|
1311 | - throw new SodiumException( |
|
1312 | - 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' |
|
1313 | - ); |
|
1314 | - } |
|
1315 | - |
|
1316 | - # if (outlen_p != NULL) { |
|
1317 | - # *outlen_p = 0U; |
|
1318 | - # } |
|
1319 | - # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) { |
|
1320 | - # sodium_misuse(); |
|
1321 | - # } |
|
1322 | - |
|
1323 | - # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k); |
|
1324 | - # crypto_onetimeauth_poly1305_init(&poly1305_state, block); |
|
1325 | - # sodium_memzero(block, sizeof block); |
|
1326 | - $auth = new ParagonIE_Sodium_Core_Poly1305_State( |
|
1327 | - ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) |
|
1328 | - ); |
|
1329 | - |
|
1330 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); |
|
1331 | - $auth->update($aad); |
|
1332 | - |
|
1333 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, |
|
1334 | - # (0x10 - adlen) & 0xf); |
|
1335 | - $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); |
|
1336 | - |
|
1337 | - # memset(block, 0, sizeof block); |
|
1338 | - # block[0] = tag; |
|
1339 | - # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, |
|
1340 | - # state->nonce, 1U, state->k); |
|
1341 | - $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1342 | - ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63), |
|
1343 | - $st->getCombinedNonce(), |
|
1344 | - $st->getKey(), |
|
1345 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1346 | - ); |
|
1347 | - |
|
1348 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); |
|
1349 | - $auth->update($block); |
|
1350 | - |
|
1351 | - # out[0] = block[0]; |
|
1352 | - $out = $block[0]; |
|
1353 | - # c = out + (sizeof tag); |
|
1354 | - # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k); |
|
1355 | - $cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1356 | - $msg, |
|
1357 | - $st->getCombinedNonce(), |
|
1358 | - $st->getKey(), |
|
1359 | - ParagonIE_Sodium_Core_Util::store64_le(2) |
|
1360 | - ); |
|
1361 | - |
|
1362 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); |
|
1363 | - $auth->update($cipher); |
|
1364 | - |
|
1365 | - $out .= $cipher; |
|
1366 | - unset($cipher); |
|
1367 | - |
|
1368 | - # crypto_onetimeauth_poly1305_update |
|
1369 | - # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); |
|
1370 | - $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); |
|
1371 | - |
|
1372 | - # STORE64_LE(slen, (uint64_t) adlen); |
|
1373 | - $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); |
|
1374 | - |
|
1375 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
|
1376 | - $auth->update($slen); |
|
1377 | - |
|
1378 | - # STORE64_LE(slen, (sizeof block) + mlen); |
|
1379 | - $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); |
|
1380 | - |
|
1381 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
|
1382 | - $auth->update($slen); |
|
1383 | - |
|
1384 | - # mac = c + mlen; |
|
1385 | - # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); |
|
1386 | - $mac = $auth->finish(); |
|
1387 | - $out .= $mac; |
|
1388 | - |
|
1389 | - # sodium_memzero(&poly1305_state, sizeof poly1305_state); |
|
1390 | - unset($auth); |
|
1391 | - |
|
1392 | - |
|
1393 | - # XOR_BUF(STATE_INONCE(state), mac, |
|
1394 | - # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
|
1395 | - $st->xorNonce($mac); |
|
1396 | - |
|
1397 | - # sodium_increment(STATE_COUNTER(state), |
|
1398 | - # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); |
|
1399 | - $st->incrementCounter(); |
|
1400 | - // Overwrite by reference: |
|
1401 | - $state = $st->toString(); |
|
1402 | - |
|
1403 | - /** @var bool $rekey */ |
|
1404 | - $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; |
|
1405 | - # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || |
|
1406 | - # sodium_is_zero(STATE_COUNTER(state), |
|
1407 | - # crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) { |
|
1408 | - # crypto_secretstream_xchacha20poly1305_rekey(state); |
|
1409 | - # } |
|
1410 | - if ($rekey || $st->needsRekey()) { |
|
1411 | - // DO REKEY |
|
1412 | - self::secretstream_xchacha20poly1305_rekey($state); |
|
1413 | - } |
|
1414 | - # if (outlen_p != NULL) { |
|
1415 | - # *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen; |
|
1416 | - # } |
|
1417 | - return $out; |
|
1418 | - } |
|
1419 | - |
|
1420 | - /** |
|
1421 | - * @param string $state |
|
1422 | - * @param string $cipher |
|
1423 | - * @param string $aad |
|
1424 | - * @return bool|array{0: string, 1: int} |
|
1425 | - * @throws SodiumException |
|
1426 | - */ |
|
1427 | - public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') |
|
1428 | - { |
|
1429 | - $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
|
1430 | - |
|
1431 | - $cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher); |
|
1432 | - # mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES; |
|
1433 | - $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES; |
|
1434 | - $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); |
|
1435 | - |
|
1436 | - # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) { |
|
1437 | - # sodium_misuse(); |
|
1438 | - # } |
|
1439 | - if ((($msglen + 63) >> 6) > 0xfffffffe) { |
|
1440 | - throw new SodiumException( |
|
1441 | - 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' |
|
1442 | - ); |
|
1443 | - } |
|
1444 | - |
|
1445 | - # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k); |
|
1446 | - # crypto_onetimeauth_poly1305_init(&poly1305_state, block); |
|
1447 | - # sodium_memzero(block, sizeof block); |
|
1448 | - $auth = new ParagonIE_Sodium_Core_Poly1305_State( |
|
1449 | - ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) |
|
1450 | - ); |
|
1451 | - |
|
1452 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); |
|
1453 | - $auth->update($aad); |
|
1454 | - |
|
1455 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, |
|
1456 | - # (0x10 - adlen) & 0xf); |
|
1457 | - $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); |
|
1458 | - |
|
1459 | - |
|
1460 | - # memset(block, 0, sizeof block); |
|
1461 | - # block[0] = in[0]; |
|
1462 | - # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, |
|
1463 | - # state->nonce, 1U, state->k); |
|
1464 | - $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1465 | - $cipher[0] . str_repeat("\0", 63), |
|
1466 | - $st->getCombinedNonce(), |
|
1467 | - $st->getKey(), |
|
1468 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1469 | - ); |
|
1470 | - # tag = block[0]; |
|
1471 | - # block[0] = in[0]; |
|
1472 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); |
|
1473 | - $tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]); |
|
1474 | - $block[0] = $cipher[0]; |
|
1475 | - $auth->update($block); |
|
1476 | - |
|
1477 | - |
|
1478 | - # c = in + (sizeof tag); |
|
1479 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); |
|
1480 | - $auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen)); |
|
1481 | - |
|
1482 | - # crypto_onetimeauth_poly1305_update |
|
1483 | - # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); |
|
1484 | - $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); |
|
1485 | - |
|
1486 | - # STORE64_LE(slen, (uint64_t) adlen); |
|
1487 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
|
1488 | - $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); |
|
1489 | - $auth->update($slen); |
|
1490 | - |
|
1491 | - # STORE64_LE(slen, (sizeof block) + mlen); |
|
1492 | - # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
|
1493 | - $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); |
|
1494 | - $auth->update($slen); |
|
1495 | - |
|
1496 | - # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); |
|
1497 | - # sodium_memzero(&poly1305_state, sizeof poly1305_state); |
|
1498 | - $mac = $auth->finish(); |
|
1499 | - |
|
1500 | - # stored_mac = c + mlen; |
|
1501 | - # if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) { |
|
1502 | - # sodium_memzero(mac, sizeof mac); |
|
1503 | - # return -1; |
|
1504 | - # } |
|
1505 | - |
|
1506 | - $stored = ParagonIE_Sodium_Core_Util::substr($cipher, $msglen + 1, 16); |
|
1507 | - if (!ParagonIE_Sodium_Core_Util::hashEquals($mac, $stored)) { |
|
1508 | - return false; |
|
1509 | - } |
|
1510 | - |
|
1511 | - # crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k); |
|
1512 | - $out = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1513 | - ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen), |
|
1514 | - $st->getCombinedNonce(), |
|
1515 | - $st->getKey(), |
|
1516 | - ParagonIE_Sodium_Core_Util::store64_le(2) |
|
1517 | - ); |
|
1518 | - |
|
1519 | - # XOR_BUF(STATE_INONCE(state), mac, |
|
1520 | - # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
|
1521 | - $st->xorNonce($mac); |
|
1522 | - |
|
1523 | - # sodium_increment(STATE_COUNTER(state), |
|
1524 | - # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); |
|
1525 | - $st->incrementCounter(); |
|
1526 | - |
|
1527 | - # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || |
|
1528 | - # sodium_is_zero(STATE_COUNTER(state), |
|
1529 | - # crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) { |
|
1530 | - # crypto_secretstream_xchacha20poly1305_rekey(state); |
|
1531 | - # } |
|
1532 | - |
|
1533 | - // Overwrite by reference: |
|
1534 | - $state = $st->toString(); |
|
1535 | - |
|
1536 | - /** @var bool $rekey */ |
|
1537 | - $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; |
|
1538 | - if ($rekey || $st->needsRekey()) { |
|
1539 | - // DO REKEY |
|
1540 | - self::secretstream_xchacha20poly1305_rekey($state); |
|
1541 | - } |
|
1542 | - return array($out, $tag); |
|
1543 | - } |
|
1544 | - |
|
1545 | - /** |
|
1546 | - * @param string $state |
|
1547 | - * @return void |
|
1548 | - * @throws SodiumException |
|
1549 | - */ |
|
1550 | - public static function secretstream_xchacha20poly1305_rekey(&$state) |
|
1551 | - { |
|
1552 | - $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
|
1553 | - # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + |
|
1554 | - # crypto_secretstream_xchacha20poly1305_INONCEBYTES]; |
|
1555 | - # size_t i; |
|
1556 | - # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { |
|
1557 | - # new_key_and_inonce[i] = state->k[i]; |
|
1558 | - # } |
|
1559 | - $new_key_and_inonce = $st->getKey(); |
|
1560 | - |
|
1561 | - # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { |
|
1562 | - # new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] = |
|
1563 | - # STATE_INONCE(state)[i]; |
|
1564 | - # } |
|
1565 | - $new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR($st->getNonce(), 0, 8); |
|
1566 | - |
|
1567 | - # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce, |
|
1568 | - # sizeof new_key_and_inonce, |
|
1569 | - # state->nonce, state->k); |
|
1570 | - |
|
1571 | - $st->rekey(ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1572 | - $new_key_and_inonce, |
|
1573 | - $st->getCombinedNonce(), |
|
1574 | - $st->getKey(), |
|
1575 | - ParagonIE_Sodium_Core_Util::store64_le(0) |
|
1576 | - )); |
|
1577 | - |
|
1578 | - # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { |
|
1579 | - # state->k[i] = new_key_and_inonce[i]; |
|
1580 | - # } |
|
1581 | - # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { |
|
1582 | - # STATE_INONCE(state)[i] = |
|
1583 | - # new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i]; |
|
1584 | - # } |
|
1585 | - # _crypto_secretstream_xchacha20poly1305_counter_reset(state); |
|
1586 | - $st->counterReset(); |
|
1587 | - |
|
1588 | - $state = $st->toString(); |
|
1589 | - } |
|
1590 | - |
|
1591 | - /** |
|
1592 | - * Detached Ed25519 signature. |
|
1593 | - * |
|
1594 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1595 | - * |
|
1596 | - * @param string $message |
|
1597 | - * @param string $sk |
|
1598 | - * @return string |
|
1599 | - * @throws SodiumException |
|
1600 | - * @throws TypeError |
|
1601 | - */ |
|
1602 | - public static function sign_detached($message, $sk) |
|
1603 | - { |
|
1604 | - return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); |
|
1605 | - } |
|
1606 | - |
|
1607 | - /** |
|
1608 | - * Attached Ed25519 signature. (Returns a signed message.) |
|
1609 | - * |
|
1610 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1611 | - * |
|
1612 | - * @param string $message |
|
1613 | - * @param string $sk |
|
1614 | - * @return string |
|
1615 | - * @throws SodiumException |
|
1616 | - * @throws TypeError |
|
1617 | - */ |
|
1618 | - public static function sign($message, $sk) |
|
1619 | - { |
|
1620 | - return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); |
|
1621 | - } |
|
1622 | - |
|
1623 | - /** |
|
1624 | - * Opens a signed message. If valid, returns the message. |
|
1625 | - * |
|
1626 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1627 | - * |
|
1628 | - * @param string $signedMessage |
|
1629 | - * @param string $pk |
|
1630 | - * @return string |
|
1631 | - * @throws SodiumException |
|
1632 | - * @throws TypeError |
|
1633 | - */ |
|
1634 | - public static function sign_open($signedMessage, $pk) |
|
1635 | - { |
|
1636 | - return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); |
|
1637 | - } |
|
1638 | - |
|
1639 | - /** |
|
1640 | - * Verify a detached signature of a given message and public key. |
|
1641 | - * |
|
1642 | - * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1643 | - * |
|
1644 | - * @param string $signature |
|
1645 | - * @param string $message |
|
1646 | - * @param string $pk |
|
1647 | - * @return bool |
|
1648 | - * @throws SodiumException |
|
1649 | - * @throws TypeError |
|
1650 | - */ |
|
1651 | - public static function sign_verify_detached($signature, $message, $pk) |
|
1652 | - { |
|
1653 | - return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); |
|
1654 | - } |
|
17 | + const aead_chacha20poly1305_KEYBYTES = 32; |
|
18 | + const aead_chacha20poly1305_NSECBYTES = 0; |
|
19 | + const aead_chacha20poly1305_NPUBBYTES = 8; |
|
20 | + const aead_chacha20poly1305_ABYTES = 16; |
|
21 | + |
|
22 | + const aead_chacha20poly1305_IETF_KEYBYTES = 32; |
|
23 | + const aead_chacha20poly1305_IETF_NSECBYTES = 0; |
|
24 | + const aead_chacha20poly1305_IETF_NPUBBYTES = 12; |
|
25 | + const aead_chacha20poly1305_IETF_ABYTES = 16; |
|
26 | + |
|
27 | + const aead_xchacha20poly1305_IETF_KEYBYTES = 32; |
|
28 | + const aead_xchacha20poly1305_IETF_NSECBYTES = 0; |
|
29 | + const aead_xchacha20poly1305_IETF_NPUBBYTES = 24; |
|
30 | + const aead_xchacha20poly1305_IETF_ABYTES = 16; |
|
31 | + |
|
32 | + const box_curve25519xsalsa20poly1305_SEEDBYTES = 32; |
|
33 | + const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32; |
|
34 | + const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32; |
|
35 | + const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32; |
|
36 | + const box_curve25519xsalsa20poly1305_NONCEBYTES = 24; |
|
37 | + const box_curve25519xsalsa20poly1305_MACBYTES = 16; |
|
38 | + const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16; |
|
39 | + const box_curve25519xsalsa20poly1305_ZEROBYTES = 32; |
|
40 | + |
|
41 | + const onetimeauth_poly1305_BYTES = 16; |
|
42 | + const onetimeauth_poly1305_KEYBYTES = 32; |
|
43 | + |
|
44 | + const secretbox_xsalsa20poly1305_KEYBYTES = 32; |
|
45 | + const secretbox_xsalsa20poly1305_NONCEBYTES = 24; |
|
46 | + const secretbox_xsalsa20poly1305_MACBYTES = 16; |
|
47 | + const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16; |
|
48 | + const secretbox_xsalsa20poly1305_ZEROBYTES = 32; |
|
49 | + |
|
50 | + const secretbox_xchacha20poly1305_KEYBYTES = 32; |
|
51 | + const secretbox_xchacha20poly1305_NONCEBYTES = 24; |
|
52 | + const secretbox_xchacha20poly1305_MACBYTES = 16; |
|
53 | + const secretbox_xchacha20poly1305_BOXZEROBYTES = 16; |
|
54 | + const secretbox_xchacha20poly1305_ZEROBYTES = 32; |
|
55 | + |
|
56 | + const stream_salsa20_KEYBYTES = 32; |
|
57 | + |
|
58 | + /** |
|
59 | + * AEAD Decryption with ChaCha20-Poly1305 |
|
60 | + * |
|
61 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
62 | + * |
|
63 | + * @param string $message |
|
64 | + * @param string $ad |
|
65 | + * @param string $nonce |
|
66 | + * @param string $key |
|
67 | + * @return string |
|
68 | + * @throws SodiumException |
|
69 | + * @throws TypeError |
|
70 | + */ |
|
71 | + public static function aead_chacha20poly1305_decrypt( |
|
72 | + $message = '', |
|
73 | + $ad = '', |
|
74 | + $nonce = '', |
|
75 | + $key = '' |
|
76 | + ) { |
|
77 | + /** @var int $len - Length of message (ciphertext + MAC) */ |
|
78 | + $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
79 | + |
|
80 | + /** @var int $clen - Length of ciphertext */ |
|
81 | + $clen = $len - self::aead_chacha20poly1305_ABYTES; |
|
82 | + |
|
83 | + /** @var int $adlen - Length of associated data */ |
|
84 | + $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
85 | + |
|
86 | + /** @var string $mac - Message authentication code */ |
|
87 | + $mac = ParagonIE_Sodium_Core_Util::substr( |
|
88 | + $message, |
|
89 | + $clen, |
|
90 | + self::aead_chacha20poly1305_ABYTES |
|
91 | + ); |
|
92 | + |
|
93 | + /** @var string $ciphertext - The encrypted message (sans MAC) */ |
|
94 | + $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 0, $clen); |
|
95 | + |
|
96 | + /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
97 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
98 | + 32, |
|
99 | + $nonce, |
|
100 | + $key |
|
101 | + ); |
|
102 | + |
|
103 | + /* Recalculate the Poly1305 authentication tag (MAC): */ |
|
104 | + $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
105 | + try { |
|
106 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
107 | + } catch (SodiumException $ex) { |
|
108 | + $block0 = null; |
|
109 | + } |
|
110 | + $state->update($ad); |
|
111 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
112 | + $state->update($ciphertext); |
|
113 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
114 | + $computed_mac = $state->finish(); |
|
115 | + |
|
116 | + /* Compare the given MAC with the recalculated MAC: */ |
|
117 | + if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { |
|
118 | + throw new SodiumException('Invalid MAC'); |
|
119 | + } |
|
120 | + |
|
121 | + // Here, we know that the MAC is valid, so we decrypt and return the plaintext |
|
122 | + return ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
123 | + $ciphertext, |
|
124 | + $nonce, |
|
125 | + $key, |
|
126 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
127 | + ); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * AEAD Encryption with ChaCha20-Poly1305 |
|
132 | + * |
|
133 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
134 | + * |
|
135 | + * @param string $message |
|
136 | + * @param string $ad |
|
137 | + * @param string $nonce |
|
138 | + * @param string $key |
|
139 | + * @return string |
|
140 | + * @throws SodiumException |
|
141 | + * @throws TypeError |
|
142 | + */ |
|
143 | + public static function aead_chacha20poly1305_encrypt( |
|
144 | + $message = '', |
|
145 | + $ad = '', |
|
146 | + $nonce = '', |
|
147 | + $key = '' |
|
148 | + ) { |
|
149 | + /** @var int $len - Length of the plaintext message */ |
|
150 | + $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
151 | + |
|
152 | + /** @var int $adlen - Length of the associated data */ |
|
153 | + $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
154 | + |
|
155 | + /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
156 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
157 | + 32, |
|
158 | + $nonce, |
|
159 | + $key |
|
160 | + ); |
|
161 | + $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
162 | + try { |
|
163 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
164 | + } catch (SodiumException $ex) { |
|
165 | + $block0 = null; |
|
166 | + } |
|
167 | + |
|
168 | + /** @var string $ciphertext - Raw encrypted data */ |
|
169 | + $ciphertext = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
170 | + $message, |
|
171 | + $nonce, |
|
172 | + $key, |
|
173 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
174 | + ); |
|
175 | + |
|
176 | + $state->update($ad); |
|
177 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
178 | + $state->update($ciphertext); |
|
179 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
|
180 | + return $ciphertext . $state->finish(); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|
185 | + * |
|
186 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
187 | + * |
|
188 | + * @param string $message |
|
189 | + * @param string $ad |
|
190 | + * @param string $nonce |
|
191 | + * @param string $key |
|
192 | + * @return string |
|
193 | + * @throws SodiumException |
|
194 | + * @throws TypeError |
|
195 | + */ |
|
196 | + public static function aead_chacha20poly1305_ietf_decrypt( |
|
197 | + $message = '', |
|
198 | + $ad = '', |
|
199 | + $nonce = '', |
|
200 | + $key = '' |
|
201 | + ) { |
|
202 | + /** @var int $adlen - Length of associated data */ |
|
203 | + $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
204 | + |
|
205 | + /** @var int $len - Length of message (ciphertext + MAC) */ |
|
206 | + $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
207 | + |
|
208 | + /** @var int $clen - Length of ciphertext */ |
|
209 | + $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES; |
|
210 | + |
|
211 | + /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
212 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( |
|
213 | + 32, |
|
214 | + $nonce, |
|
215 | + $key |
|
216 | + ); |
|
217 | + |
|
218 | + /** @var string $mac - Message authentication code */ |
|
219 | + $mac = ParagonIE_Sodium_Core_Util::substr( |
|
220 | + $message, |
|
221 | + $len - self::aead_chacha20poly1305_IETF_ABYTES, |
|
222 | + self::aead_chacha20poly1305_IETF_ABYTES |
|
223 | + ); |
|
224 | + |
|
225 | + /** @var string $ciphertext - The encrypted message (sans MAC) */ |
|
226 | + $ciphertext = ParagonIE_Sodium_Core_Util::substr( |
|
227 | + $message, |
|
228 | + 0, |
|
229 | + $len - self::aead_chacha20poly1305_IETF_ABYTES |
|
230 | + ); |
|
231 | + |
|
232 | + /* Recalculate the Poly1305 authentication tag (MAC): */ |
|
233 | + $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
234 | + try { |
|
235 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
236 | + } catch (SodiumException $ex) { |
|
237 | + $block0 = null; |
|
238 | + } |
|
239 | + $state->update($ad); |
|
240 | + $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); |
|
241 | + $state->update($ciphertext); |
|
242 | + $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf)); |
|
243 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
244 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
245 | + $computed_mac = $state->finish(); |
|
246 | + |
|
247 | + /* Compare the given MAC with the recalculated MAC: */ |
|
248 | + if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { |
|
249 | + throw new SodiumException('Invalid MAC'); |
|
250 | + } |
|
251 | + |
|
252 | + // Here, we know that the MAC is valid, so we decrypt and return the plaintext |
|
253 | + return ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
254 | + $ciphertext, |
|
255 | + $nonce, |
|
256 | + $key, |
|
257 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
258 | + ); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|
263 | + * |
|
264 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
265 | + * |
|
266 | + * @param string $message |
|
267 | + * @param string $ad |
|
268 | + * @param string $nonce |
|
269 | + * @param string $key |
|
270 | + * @return string |
|
271 | + * @throws SodiumException |
|
272 | + * @throws TypeError |
|
273 | + */ |
|
274 | + public static function aead_chacha20poly1305_ietf_encrypt( |
|
275 | + $message = '', |
|
276 | + $ad = '', |
|
277 | + $nonce = '', |
|
278 | + $key = '' |
|
279 | + ) { |
|
280 | + /** @var int $len - Length of the plaintext message */ |
|
281 | + $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
282 | + |
|
283 | + /** @var int $adlen - Length of the associated data */ |
|
284 | + $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
285 | + |
|
286 | + /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
287 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( |
|
288 | + 32, |
|
289 | + $nonce, |
|
290 | + $key |
|
291 | + ); |
|
292 | + $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
293 | + try { |
|
294 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
295 | + } catch (SodiumException $ex) { |
|
296 | + $block0 = null; |
|
297 | + } |
|
298 | + |
|
299 | + /** @var string $ciphertext - Raw encrypted data */ |
|
300 | + $ciphertext = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
301 | + $message, |
|
302 | + $nonce, |
|
303 | + $key, |
|
304 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
305 | + ); |
|
306 | + |
|
307 | + $state->update($ad); |
|
308 | + $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); |
|
309 | + $state->update($ciphertext); |
|
310 | + $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf))); |
|
311 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
312 | + $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
|
313 | + return $ciphertext . $state->finish(); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|
318 | + * |
|
319 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
320 | + * |
|
321 | + * @param string $message |
|
322 | + * @param string $ad |
|
323 | + * @param string $nonce |
|
324 | + * @param string $key |
|
325 | + * @return string |
|
326 | + * @throws SodiumException |
|
327 | + * @throws TypeError |
|
328 | + */ |
|
329 | + public static function aead_xchacha20poly1305_ietf_decrypt( |
|
330 | + $message = '', |
|
331 | + $ad = '', |
|
332 | + $nonce = '', |
|
333 | + $key = '' |
|
334 | + ) { |
|
335 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
336 | + ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
337 | + $key |
|
338 | + ); |
|
339 | + $nonceLast = "\x00\x00\x00\x00" . |
|
340 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
341 | + |
|
342 | + return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey); |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|
347 | + * |
|
348 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
349 | + * |
|
350 | + * @param string $message |
|
351 | + * @param string $ad |
|
352 | + * @param string $nonce |
|
353 | + * @param string $key |
|
354 | + * @return string |
|
355 | + * @throws SodiumException |
|
356 | + * @throws TypeError |
|
357 | + */ |
|
358 | + public static function aead_xchacha20poly1305_ietf_encrypt( |
|
359 | + $message = '', |
|
360 | + $ad = '', |
|
361 | + $nonce = '', |
|
362 | + $key = '' |
|
363 | + ) { |
|
364 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
365 | + ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
366 | + $key |
|
367 | + ); |
|
368 | + $nonceLast = "\x00\x00\x00\x00" . |
|
369 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
370 | + |
|
371 | + return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey); |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512) |
|
376 | + * |
|
377 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
378 | + * |
|
379 | + * @param string $message |
|
380 | + * @param string $key |
|
381 | + * @return string |
|
382 | + * @throws TypeError |
|
383 | + */ |
|
384 | + public static function auth($message, $key) |
|
385 | + { |
|
386 | + return ParagonIE_Sodium_Core_Util::substr( |
|
387 | + hash_hmac('sha512', $message, $key, true), |
|
388 | + 0, |
|
389 | + 32 |
|
390 | + ); |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * HMAC-SHA-512-256 validation. Constant-time via hash_equals(). |
|
395 | + * |
|
396 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
397 | + * |
|
398 | + * @param string $mac |
|
399 | + * @param string $message |
|
400 | + * @param string $key |
|
401 | + * @return bool |
|
402 | + * @throws SodiumException |
|
403 | + * @throws TypeError |
|
404 | + */ |
|
405 | + public static function auth_verify($mac, $message, $key) |
|
406 | + { |
|
407 | + return ParagonIE_Sodium_Core_Util::hashEquals( |
|
408 | + $mac, |
|
409 | + self::auth($message, $key) |
|
410 | + ); |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption |
|
415 | + * |
|
416 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
417 | + * |
|
418 | + * @param string $plaintext |
|
419 | + * @param string $nonce |
|
420 | + * @param string $keypair |
|
421 | + * @return string |
|
422 | + * @throws SodiumException |
|
423 | + * @throws TypeError |
|
424 | + */ |
|
425 | + public static function box($plaintext, $nonce, $keypair) |
|
426 | + { |
|
427 | + $c = self::secretbox( |
|
428 | + $plaintext, |
|
429 | + $nonce, |
|
430 | + self::box_beforenm( |
|
431 | + self::box_secretkey($keypair), |
|
432 | + self::box_publickey($keypair) |
|
433 | + ) |
|
434 | + ); |
|
435 | + return $c; |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair. |
|
440 | + * |
|
441 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
442 | + * |
|
443 | + * @param string $message |
|
444 | + * @param string $publicKey |
|
445 | + * @return string |
|
446 | + * @throws SodiumException |
|
447 | + * @throws TypeError |
|
448 | + */ |
|
449 | + public static function box_seal($message, $publicKey) |
|
450 | + { |
|
451 | + /** @var string $ephemeralKeypair */ |
|
452 | + $ephemeralKeypair = self::box_keypair(); |
|
453 | + |
|
454 | + /** @var string $ephemeralSK */ |
|
455 | + $ephemeralSK = self::box_secretkey($ephemeralKeypair); |
|
456 | + |
|
457 | + /** @var string $ephemeralPK */ |
|
458 | + $ephemeralPK = self::box_publickey($ephemeralKeypair); |
|
459 | + |
|
460 | + /** @var string $nonce */ |
|
461 | + $nonce = self::generichash( |
|
462 | + $ephemeralPK . $publicKey, |
|
463 | + '', |
|
464 | + 24 |
|
465 | + ); |
|
466 | + |
|
467 | + /** @var string $keypair - The combined keypair used in crypto_box() */ |
|
468 | + $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey); |
|
469 | + |
|
470 | + /** @var string $ciphertext Ciphertext + MAC from crypto_box */ |
|
471 | + $ciphertext = self::box($message, $nonce, $keypair); |
|
472 | + try { |
|
473 | + ParagonIE_Sodium_Compat::memzero($ephemeralKeypair); |
|
474 | + ParagonIE_Sodium_Compat::memzero($ephemeralSK); |
|
475 | + ParagonIE_Sodium_Compat::memzero($nonce); |
|
476 | + } catch (SodiumException $ex) { |
|
477 | + $ephemeralKeypair = null; |
|
478 | + $ephemeralSK = null; |
|
479 | + $nonce = null; |
|
480 | + } |
|
481 | + return $ephemeralPK . $ciphertext; |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Opens a message encrypted via box_seal(). |
|
486 | + * |
|
487 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
488 | + * |
|
489 | + * @param string $message |
|
490 | + * @param string $keypair |
|
491 | + * @return string |
|
492 | + * @throws SodiumException |
|
493 | + * @throws TypeError |
|
494 | + */ |
|
495 | + public static function box_seal_open($message, $keypair) |
|
496 | + { |
|
497 | + /** @var string $ephemeralPK */ |
|
498 | + $ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32); |
|
499 | + |
|
500 | + /** @var string $ciphertext (ciphertext + MAC) */ |
|
501 | + $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 32); |
|
502 | + |
|
503 | + /** @var string $secretKey */ |
|
504 | + $secretKey = self::box_secretkey($keypair); |
|
505 | + |
|
506 | + /** @var string $publicKey */ |
|
507 | + $publicKey = self::box_publickey($keypair); |
|
508 | + |
|
509 | + /** @var string $nonce */ |
|
510 | + $nonce = self::generichash( |
|
511 | + $ephemeralPK . $publicKey, |
|
512 | + '', |
|
513 | + 24 |
|
514 | + ); |
|
515 | + |
|
516 | + /** @var string $keypair */ |
|
517 | + $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK); |
|
518 | + |
|
519 | + /** @var string $m */ |
|
520 | + $m = self::box_open($ciphertext, $nonce, $keypair); |
|
521 | + try { |
|
522 | + ParagonIE_Sodium_Compat::memzero($secretKey); |
|
523 | + ParagonIE_Sodium_Compat::memzero($ephemeralPK); |
|
524 | + ParagonIE_Sodium_Compat::memzero($nonce); |
|
525 | + } catch (SodiumException $ex) { |
|
526 | + $secretKey = null; |
|
527 | + $ephemeralPK = null; |
|
528 | + $nonce = null; |
|
529 | + } |
|
530 | + return $m; |
|
531 | + } |
|
532 | + |
|
533 | + /** |
|
534 | + * Used by crypto_box() to get the crypto_secretbox() key. |
|
535 | + * |
|
536 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
537 | + * |
|
538 | + * @param string $sk |
|
539 | + * @param string $pk |
|
540 | + * @return string |
|
541 | + * @throws SodiumException |
|
542 | + * @throws TypeError |
|
543 | + */ |
|
544 | + public static function box_beforenm($sk, $pk) |
|
545 | + { |
|
546 | + return ParagonIE_Sodium_Core_HSalsa20::hsalsa20( |
|
547 | + str_repeat("\x00", 16), |
|
548 | + self::scalarmult($sk, $pk) |
|
549 | + ); |
|
550 | + } |
|
551 | + |
|
552 | + /** |
|
553 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
554 | + * |
|
555 | + * @return string |
|
556 | + * @throws Exception |
|
557 | + * @throws SodiumException |
|
558 | + * @throws TypeError |
|
559 | + */ |
|
560 | + public static function box_keypair() |
|
561 | + { |
|
562 | + $sKey = random_bytes(32); |
|
563 | + $pKey = self::scalarmult_base($sKey); |
|
564 | + return $sKey . $pKey; |
|
565 | + } |
|
566 | + |
|
567 | + /** |
|
568 | + * @param string $seed |
|
569 | + * @return string |
|
570 | + * @throws SodiumException |
|
571 | + * @throws TypeError |
|
572 | + */ |
|
573 | + public static function box_seed_keypair($seed) |
|
574 | + { |
|
575 | + $sKey = ParagonIE_Sodium_Core_Util::substr( |
|
576 | + hash('sha512', $seed, true), |
|
577 | + 0, |
|
578 | + 32 |
|
579 | + ); |
|
580 | + $pKey = self::scalarmult_base($sKey); |
|
581 | + return $sKey . $pKey; |
|
582 | + } |
|
583 | + |
|
584 | + /** |
|
585 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
586 | + * |
|
587 | + * @param string $sKey |
|
588 | + * @param string $pKey |
|
589 | + * @return string |
|
590 | + * @throws TypeError |
|
591 | + */ |
|
592 | + public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) |
|
593 | + { |
|
594 | + return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . |
|
595 | + ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32); |
|
596 | + } |
|
597 | + |
|
598 | + /** |
|
599 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
600 | + * |
|
601 | + * @param string $keypair |
|
602 | + * @return string |
|
603 | + * @throws RangeException |
|
604 | + * @throws TypeError |
|
605 | + */ |
|
606 | + public static function box_secretkey($keypair) |
|
607 | + { |
|
608 | + if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { |
|
609 | + throw new RangeException( |
|
610 | + 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
|
611 | + ); |
|
612 | + } |
|
613 | + return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32); |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
618 | + * |
|
619 | + * @param string $keypair |
|
620 | + * @return string |
|
621 | + * @throws RangeException |
|
622 | + * @throws TypeError |
|
623 | + */ |
|
624 | + public static function box_publickey($keypair) |
|
625 | + { |
|
626 | + if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
627 | + throw new RangeException( |
|
628 | + 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
|
629 | + ); |
|
630 | + } |
|
631 | + return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32); |
|
632 | + } |
|
633 | + |
|
634 | + /** |
|
635 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
636 | + * |
|
637 | + * @param string $sKey |
|
638 | + * @return string |
|
639 | + * @throws RangeException |
|
640 | + * @throws SodiumException |
|
641 | + * @throws TypeError |
|
642 | + */ |
|
643 | + public static function box_publickey_from_secretkey($sKey) |
|
644 | + { |
|
645 | + if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { |
|
646 | + throw new RangeException( |
|
647 | + 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.' |
|
648 | + ); |
|
649 | + } |
|
650 | + return self::scalarmult_base($sKey); |
|
651 | + } |
|
652 | + |
|
653 | + /** |
|
654 | + * Decrypt a message encrypted with box(). |
|
655 | + * |
|
656 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
657 | + * |
|
658 | + * @param string $ciphertext |
|
659 | + * @param string $nonce |
|
660 | + * @param string $keypair |
|
661 | + * @return string |
|
662 | + * @throws SodiumException |
|
663 | + * @throws TypeError |
|
664 | + */ |
|
665 | + public static function box_open($ciphertext, $nonce, $keypair) |
|
666 | + { |
|
667 | + return self::secretbox_open( |
|
668 | + $ciphertext, |
|
669 | + $nonce, |
|
670 | + self::box_beforenm( |
|
671 | + self::box_secretkey($keypair), |
|
672 | + self::box_publickey($keypair) |
|
673 | + ) |
|
674 | + ); |
|
675 | + } |
|
676 | + |
|
677 | + /** |
|
678 | + * Calculate a BLAKE2b hash. |
|
679 | + * |
|
680 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
681 | + * |
|
682 | + * @param string $message |
|
683 | + * @param string|null $key |
|
684 | + * @param int $outlen |
|
685 | + * @return string |
|
686 | + * @throws RangeException |
|
687 | + * @throws SodiumException |
|
688 | + * @throws TypeError |
|
689 | + */ |
|
690 | + public static function generichash($message, $key = '', $outlen = 32) |
|
691 | + { |
|
692 | + // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
693 | + ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
694 | + |
|
695 | + $k = null; |
|
696 | + if (!empty($key)) { |
|
697 | + /** @var SplFixedArray $k */ |
|
698 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
699 | + if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
700 | + throw new RangeException('Invalid key size'); |
|
701 | + } |
|
702 | + } |
|
703 | + |
|
704 | + /** @var SplFixedArray $in */ |
|
705 | + $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
706 | + |
|
707 | + /** @var SplFixedArray $ctx */ |
|
708 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen); |
|
709 | + ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count()); |
|
710 | + |
|
711 | + /** @var SplFixedArray $out */ |
|
712 | + $out = new SplFixedArray($outlen); |
|
713 | + $out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out); |
|
714 | + |
|
715 | + /** @var array<int, int> */ |
|
716 | + $outArray = $out->toArray(); |
|
717 | + return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
718 | + } |
|
719 | + |
|
720 | + /** |
|
721 | + * Finalize a BLAKE2b hashing context, returning the hash. |
|
722 | + * |
|
723 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
724 | + * |
|
725 | + * @param string $ctx |
|
726 | + * @param int $outlen |
|
727 | + * @return string |
|
728 | + * @throws SodiumException |
|
729 | + * @throws TypeError |
|
730 | + */ |
|
731 | + public static function generichash_final($ctx, $outlen = 32) |
|
732 | + { |
|
733 | + if (!is_string($ctx)) { |
|
734 | + throw new TypeError('Context must be a string'); |
|
735 | + } |
|
736 | + $out = new SplFixedArray($outlen); |
|
737 | + |
|
738 | + /** @var SplFixedArray $context */ |
|
739 | + $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
740 | + |
|
741 | + /** @var SplFixedArray $out */ |
|
742 | + $out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out); |
|
743 | + |
|
744 | + /** @var array<int, int> */ |
|
745 | + $outArray = $out->toArray(); |
|
746 | + return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
747 | + } |
|
748 | + |
|
749 | + /** |
|
750 | + * Initialize a hashing context for BLAKE2b. |
|
751 | + * |
|
752 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
753 | + * |
|
754 | + * @param string $key |
|
755 | + * @param int $outputLength |
|
756 | + * @return string |
|
757 | + * @throws RangeException |
|
758 | + * @throws SodiumException |
|
759 | + * @throws TypeError |
|
760 | + */ |
|
761 | + public static function generichash_init($key = '', $outputLength = 32) |
|
762 | + { |
|
763 | + // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
764 | + ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
765 | + |
|
766 | + $k = null; |
|
767 | + if (!empty($key)) { |
|
768 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
769 | + if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
770 | + throw new RangeException('Invalid key size'); |
|
771 | + } |
|
772 | + } |
|
773 | + |
|
774 | + /** @var SplFixedArray $ctx */ |
|
775 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength); |
|
776 | + |
|
777 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
|
778 | + } |
|
779 | + |
|
780 | + /** |
|
781 | + * Initialize a hashing context for BLAKE2b. |
|
782 | + * |
|
783 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
784 | + * |
|
785 | + * @param string $key |
|
786 | + * @param int $outputLength |
|
787 | + * @param string $salt |
|
788 | + * @param string $personal |
|
789 | + * @return string |
|
790 | + * @throws RangeException |
|
791 | + * @throws SodiumException |
|
792 | + * @throws TypeError |
|
793 | + */ |
|
794 | + public static function generichash_init_salt_personal( |
|
795 | + $key = '', |
|
796 | + $outputLength = 32, |
|
797 | + $salt = '', |
|
798 | + $personal = '' |
|
799 | + ) { |
|
800 | + // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
801 | + ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
802 | + |
|
803 | + $k = null; |
|
804 | + if (!empty($key)) { |
|
805 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
806 | + if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
807 | + throw new RangeException('Invalid key size'); |
|
808 | + } |
|
809 | + } |
|
810 | + if (!empty($salt)) { |
|
811 | + $s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($salt); |
|
812 | + } else { |
|
813 | + $s = null; |
|
814 | + } |
|
815 | + if (!empty($salt)) { |
|
816 | + $p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($personal); |
|
817 | + } else { |
|
818 | + $p = null; |
|
819 | + } |
|
820 | + |
|
821 | + /** @var SplFixedArray $ctx */ |
|
822 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength, $s, $p); |
|
823 | + |
|
824 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
|
825 | + } |
|
826 | + |
|
827 | + /** |
|
828 | + * Update a hashing context for BLAKE2b with $message |
|
829 | + * |
|
830 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
831 | + * |
|
832 | + * @param string $ctx |
|
833 | + * @param string $message |
|
834 | + * @return string |
|
835 | + * @throws SodiumException |
|
836 | + * @throws TypeError |
|
837 | + */ |
|
838 | + public static function generichash_update($ctx, $message) |
|
839 | + { |
|
840 | + // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
|
841 | + ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
|
842 | + |
|
843 | + /** @var SplFixedArray $context */ |
|
844 | + $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
845 | + |
|
846 | + /** @var SplFixedArray $in */ |
|
847 | + $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
848 | + |
|
849 | + ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count()); |
|
850 | + |
|
851 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context); |
|
852 | + } |
|
853 | + |
|
854 | + /** |
|
855 | + * Libsodium's crypto_kx(). |
|
856 | + * |
|
857 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
858 | + * |
|
859 | + * @param string $my_sk |
|
860 | + * @param string $their_pk |
|
861 | + * @param string $client_pk |
|
862 | + * @param string $server_pk |
|
863 | + * @return string |
|
864 | + * @throws SodiumException |
|
865 | + * @throws TypeError |
|
866 | + */ |
|
867 | + public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) |
|
868 | + { |
|
869 | + return ParagonIE_Sodium_Compat::crypto_generichash( |
|
870 | + ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) . |
|
871 | + $client_pk . |
|
872 | + $server_pk |
|
873 | + ); |
|
874 | + } |
|
875 | + |
|
876 | + /** |
|
877 | + * ECDH over Curve25519 |
|
878 | + * |
|
879 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
880 | + * |
|
881 | + * @param string $sKey |
|
882 | + * @param string $pKey |
|
883 | + * @return string |
|
884 | + * |
|
885 | + * @throws SodiumException |
|
886 | + * @throws TypeError |
|
887 | + */ |
|
888 | + public static function scalarmult($sKey, $pKey) |
|
889 | + { |
|
890 | + $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); |
|
891 | + self::scalarmult_throw_if_zero($q); |
|
892 | + return $q; |
|
893 | + } |
|
894 | + |
|
895 | + /** |
|
896 | + * ECDH over Curve25519, using the basepoint. |
|
897 | + * Used to get a secret key from a public key. |
|
898 | + * |
|
899 | + * @param string $secret |
|
900 | + * @return string |
|
901 | + * |
|
902 | + * @throws SodiumException |
|
903 | + * @throws TypeError |
|
904 | + */ |
|
905 | + public static function scalarmult_base($secret) |
|
906 | + { |
|
907 | + $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); |
|
908 | + self::scalarmult_throw_if_zero($q); |
|
909 | + return $q; |
|
910 | + } |
|
911 | + |
|
912 | + /** |
|
913 | + * This throws an Error if a zero public key was passed to the function. |
|
914 | + * |
|
915 | + * @param string $q |
|
916 | + * @return void |
|
917 | + * @throws SodiumException |
|
918 | + * @throws TypeError |
|
919 | + */ |
|
920 | + protected static function scalarmult_throw_if_zero($q) |
|
921 | + { |
|
922 | + $d = 0; |
|
923 | + for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { |
|
924 | + $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); |
|
925 | + } |
|
926 | + |
|
927 | + /* branch-free variant of === 0 */ |
|
928 | + if (-(1 & (($d - 1) >> 8))) { |
|
929 | + throw new SodiumException('Zero public key is not allowed'); |
|
930 | + } |
|
931 | + } |
|
932 | + |
|
933 | + /** |
|
934 | + * XSalsa20-Poly1305 authenticated symmetric-key encryption. |
|
935 | + * |
|
936 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
937 | + * |
|
938 | + * @param string $plaintext |
|
939 | + * @param string $nonce |
|
940 | + * @param string $key |
|
941 | + * @return string |
|
942 | + * @throws SodiumException |
|
943 | + * @throws TypeError |
|
944 | + */ |
|
945 | + public static function secretbox($plaintext, $nonce, $key) |
|
946 | + { |
|
947 | + /** @var string $subkey */ |
|
948 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
949 | + |
|
950 | + /** @var string $block0 */ |
|
951 | + $block0 = str_repeat("\x00", 32); |
|
952 | + |
|
953 | + /** @var int $mlen - Length of the plaintext message */ |
|
954 | + $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
955 | + $mlen0 = $mlen; |
|
956 | + if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
957 | + $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
958 | + } |
|
959 | + $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
960 | + |
|
961 | + /** @var string $block0 */ |
|
962 | + $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
|
963 | + $block0, |
|
964 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
965 | + $subkey |
|
966 | + ); |
|
967 | + |
|
968 | + /** @var string $c */ |
|
969 | + $c = ParagonIE_Sodium_Core_Util::substr( |
|
970 | + $block0, |
|
971 | + self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
972 | + ); |
|
973 | + if ($mlen > $mlen0) { |
|
974 | + $c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
975 | + ParagonIE_Sodium_Core_Util::substr( |
|
976 | + $plaintext, |
|
977 | + self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
978 | + ), |
|
979 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
980 | + 1, |
|
981 | + $subkey |
|
982 | + ); |
|
983 | + } |
|
984 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
985 | + ParagonIE_Sodium_Core_Util::substr( |
|
986 | + $block0, |
|
987 | + 0, |
|
988 | + self::onetimeauth_poly1305_KEYBYTES |
|
989 | + ) |
|
990 | + ); |
|
991 | + try { |
|
992 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
993 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
994 | + } catch (SodiumException $ex) { |
|
995 | + $block0 = null; |
|
996 | + $subkey = null; |
|
997 | + } |
|
998 | + |
|
999 | + $state->update($c); |
|
1000 | + |
|
1001 | + /** @var string $c - MAC || ciphertext */ |
|
1002 | + $c = $state->finish() . $c; |
|
1003 | + unset($state); |
|
1004 | + |
|
1005 | + return $c; |
|
1006 | + } |
|
1007 | + |
|
1008 | + /** |
|
1009 | + * Decrypt a ciphertext generated via secretbox(). |
|
1010 | + * |
|
1011 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1012 | + * |
|
1013 | + * @param string $ciphertext |
|
1014 | + * @param string $nonce |
|
1015 | + * @param string $key |
|
1016 | + * @return string |
|
1017 | + * @throws SodiumException |
|
1018 | + * @throws TypeError |
|
1019 | + */ |
|
1020 | + public static function secretbox_open($ciphertext, $nonce, $key) |
|
1021 | + { |
|
1022 | + /** @var string $mac */ |
|
1023 | + $mac = ParagonIE_Sodium_Core_Util::substr( |
|
1024 | + $ciphertext, |
|
1025 | + 0, |
|
1026 | + self::secretbox_xsalsa20poly1305_MACBYTES |
|
1027 | + ); |
|
1028 | + |
|
1029 | + /** @var string $c */ |
|
1030 | + $c = ParagonIE_Sodium_Core_Util::substr( |
|
1031 | + $ciphertext, |
|
1032 | + self::secretbox_xsalsa20poly1305_MACBYTES |
|
1033 | + ); |
|
1034 | + |
|
1035 | + /** @var int $clen */ |
|
1036 | + $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
1037 | + |
|
1038 | + /** @var string $subkey */ |
|
1039 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
1040 | + |
|
1041 | + /** @var string $block0 */ |
|
1042 | + $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
|
1043 | + 64, |
|
1044 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1045 | + $subkey |
|
1046 | + ); |
|
1047 | + $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
|
1048 | + $mac, |
|
1049 | + $c, |
|
1050 | + ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
1051 | + ); |
|
1052 | + if (!$verified) { |
|
1053 | + try { |
|
1054 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
1055 | + } catch (SodiumException $ex) { |
|
1056 | + $subkey = null; |
|
1057 | + } |
|
1058 | + throw new SodiumException('Invalid MAC'); |
|
1059 | + } |
|
1060 | + |
|
1061 | + /** @var string $m - Decrypted message */ |
|
1062 | + $m = ParagonIE_Sodium_Core_Util::xorStrings( |
|
1063 | + ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES), |
|
1064 | + ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES) |
|
1065 | + ); |
|
1066 | + if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
1067 | + // We had more than 1 block, so let's continue to decrypt the rest. |
|
1068 | + $m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
1069 | + ParagonIE_Sodium_Core_Util::substr( |
|
1070 | + $c, |
|
1071 | + self::secretbox_xsalsa20poly1305_ZEROBYTES |
|
1072 | + ), |
|
1073 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1074 | + 1, |
|
1075 | + (string) $subkey |
|
1076 | + ); |
|
1077 | + } |
|
1078 | + return $m; |
|
1079 | + } |
|
1080 | + |
|
1081 | + /** |
|
1082 | + * XChaCha20-Poly1305 authenticated symmetric-key encryption. |
|
1083 | + * |
|
1084 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1085 | + * |
|
1086 | + * @param string $plaintext |
|
1087 | + * @param string $nonce |
|
1088 | + * @param string $key |
|
1089 | + * @return string |
|
1090 | + * @throws SodiumException |
|
1091 | + * @throws TypeError |
|
1092 | + */ |
|
1093 | + public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) |
|
1094 | + { |
|
1095 | + /** @var string $subkey */ |
|
1096 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
1097 | + ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
1098 | + $key |
|
1099 | + ); |
|
1100 | + $nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
1101 | + |
|
1102 | + /** @var string $block0 */ |
|
1103 | + $block0 = str_repeat("\x00", 32); |
|
1104 | + |
|
1105 | + /** @var int $mlen - Length of the plaintext message */ |
|
1106 | + $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
1107 | + $mlen0 = $mlen; |
|
1108 | + if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
1109 | + $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES; |
|
1110 | + } |
|
1111 | + $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
1112 | + |
|
1113 | + /** @var string $block0 */ |
|
1114 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
1115 | + $block0, |
|
1116 | + $nonceLast, |
|
1117 | + $subkey |
|
1118 | + ); |
|
1119 | + |
|
1120 | + /** @var string $c */ |
|
1121 | + $c = ParagonIE_Sodium_Core_Util::substr( |
|
1122 | + $block0, |
|
1123 | + self::secretbox_xchacha20poly1305_ZEROBYTES |
|
1124 | + ); |
|
1125 | + if ($mlen > $mlen0) { |
|
1126 | + $c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
1127 | + ParagonIE_Sodium_Core_Util::substr( |
|
1128 | + $plaintext, |
|
1129 | + self::secretbox_xchacha20poly1305_ZEROBYTES |
|
1130 | + ), |
|
1131 | + $nonceLast, |
|
1132 | + $subkey, |
|
1133 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1134 | + ); |
|
1135 | + } |
|
1136 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
1137 | + ParagonIE_Sodium_Core_Util::substr( |
|
1138 | + $block0, |
|
1139 | + 0, |
|
1140 | + self::onetimeauth_poly1305_KEYBYTES |
|
1141 | + ) |
|
1142 | + ); |
|
1143 | + try { |
|
1144 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
1145 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
1146 | + } catch (SodiumException $ex) { |
|
1147 | + $block0 = null; |
|
1148 | + $subkey = null; |
|
1149 | + } |
|
1150 | + |
|
1151 | + $state->update($c); |
|
1152 | + |
|
1153 | + /** @var string $c - MAC || ciphertext */ |
|
1154 | + $c = $state->finish() . $c; |
|
1155 | + unset($state); |
|
1156 | + |
|
1157 | + return $c; |
|
1158 | + } |
|
1159 | + |
|
1160 | + /** |
|
1161 | + * Decrypt a ciphertext generated via secretbox_xchacha20poly1305(). |
|
1162 | + * |
|
1163 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1164 | + * |
|
1165 | + * @param string $ciphertext |
|
1166 | + * @param string $nonce |
|
1167 | + * @param string $key |
|
1168 | + * @return string |
|
1169 | + * @throws SodiumException |
|
1170 | + * @throws TypeError |
|
1171 | + */ |
|
1172 | + public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
|
1173 | + { |
|
1174 | + /** @var string $mac */ |
|
1175 | + $mac = ParagonIE_Sodium_Core_Util::substr( |
|
1176 | + $ciphertext, |
|
1177 | + 0, |
|
1178 | + self::secretbox_xchacha20poly1305_MACBYTES |
|
1179 | + ); |
|
1180 | + |
|
1181 | + /** @var string $c */ |
|
1182 | + $c = ParagonIE_Sodium_Core_Util::substr( |
|
1183 | + $ciphertext, |
|
1184 | + self::secretbox_xchacha20poly1305_MACBYTES |
|
1185 | + ); |
|
1186 | + |
|
1187 | + /** @var int $clen */ |
|
1188 | + $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
1189 | + |
|
1190 | + /** @var string $subkey */ |
|
1191 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key); |
|
1192 | + |
|
1193 | + /** @var string $block0 */ |
|
1194 | + $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
1195 | + 64, |
|
1196 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1197 | + $subkey |
|
1198 | + ); |
|
1199 | + $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
|
1200 | + $mac, |
|
1201 | + $c, |
|
1202 | + ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
1203 | + ); |
|
1204 | + |
|
1205 | + if (!$verified) { |
|
1206 | + try { |
|
1207 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
1208 | + } catch (SodiumException $ex) { |
|
1209 | + $subkey = null; |
|
1210 | + } |
|
1211 | + throw new SodiumException('Invalid MAC'); |
|
1212 | + } |
|
1213 | + |
|
1214 | + /** @var string $m - Decrypted message */ |
|
1215 | + $m = ParagonIE_Sodium_Core_Util::xorStrings( |
|
1216 | + ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES), |
|
1217 | + ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES) |
|
1218 | + ); |
|
1219 | + |
|
1220 | + if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
1221 | + // We had more than 1 block, so let's continue to decrypt the rest. |
|
1222 | + $m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
1223 | + ParagonIE_Sodium_Core_Util::substr( |
|
1224 | + $c, |
|
1225 | + self::secretbox_xchacha20poly1305_ZEROBYTES |
|
1226 | + ), |
|
1227 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1228 | + (string) $subkey, |
|
1229 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1230 | + ); |
|
1231 | + } |
|
1232 | + return $m; |
|
1233 | + } |
|
1234 | + |
|
1235 | + /** |
|
1236 | + * @param string $key |
|
1237 | + * @return array<int, string> Returns a state and a header. |
|
1238 | + * @throws Exception |
|
1239 | + * @throws SodiumException |
|
1240 | + */ |
|
1241 | + public static function secretstream_xchacha20poly1305_init_push($key) |
|
1242 | + { |
|
1243 | + # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES); |
|
1244 | + $out = random_bytes(24); |
|
1245 | + |
|
1246 | + # crypto_core_hchacha20(state->k, out, k, NULL); |
|
1247 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20($out, $key); |
|
1248 | + $state = new ParagonIE_Sodium_Core_SecretStream_State( |
|
1249 | + $subkey, |
|
1250 | + ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4) |
|
1251 | + ); |
|
1252 | + |
|
1253 | + # _crypto_secretstream_xchacha20poly1305_counter_reset(state); |
|
1254 | + $state->counterReset(); |
|
1255 | + |
|
1256 | + # memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES, |
|
1257 | + # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
|
1258 | + # memset(state->_pad, 0, sizeof state->_pad); |
|
1259 | + return array( |
|
1260 | + $state->toString(), |
|
1261 | + $out |
|
1262 | + ); |
|
1263 | + } |
|
1264 | + |
|
1265 | + /** |
|
1266 | + * @param string $key |
|
1267 | + * @param string $header |
|
1268 | + * @return string Returns a state. |
|
1269 | + * @throws Exception |
|
1270 | + */ |
|
1271 | + public static function secretstream_xchacha20poly1305_init_pull($key, $header) |
|
1272 | + { |
|
1273 | + # crypto_core_hchacha20(state->k, in, k, NULL); |
|
1274 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
|
1275 | + ParagonIE_Sodium_Core_Util::substr($header, 0, 16), |
|
1276 | + $key |
|
1277 | + ); |
|
1278 | + $state = new ParagonIE_Sodium_Core_SecretStream_State( |
|
1279 | + $subkey, |
|
1280 | + ParagonIE_Sodium_Core_Util::substr($header, 16) |
|
1281 | + ); |
|
1282 | + $state->counterReset(); |
|
1283 | + # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES, |
|
1284 | + # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
|
1285 | + # memset(state->_pad, 0, sizeof state->_pad); |
|
1286 | + # return 0; |
|
1287 | + return $state->toString(); |
|
1288 | + } |
|
1289 | + |
|
1290 | + /** |
|
1291 | + * @param string $state |
|
1292 | + * @param string $msg |
|
1293 | + * @param string $aad |
|
1294 | + * @param int $tag |
|
1295 | + * @return string |
|
1296 | + * @throws SodiumException |
|
1297 | + */ |
|
1298 | + public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) |
|
1299 | + { |
|
1300 | + $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
|
1301 | + # crypto_onetimeauth_poly1305_state poly1305_state; |
|
1302 | + # unsigned char block[64U]; |
|
1303 | + # unsigned char slen[8U]; |
|
1304 | + # unsigned char *c; |
|
1305 | + # unsigned char *mac; |
|
1306 | + |
|
1307 | + $msglen = ParagonIE_Sodium_Core_Util::strlen($msg); |
|
1308 | + $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); |
|
1309 | + |
|
1310 | + if ((($msglen + 63) >> 6) > 0xfffffffe) { |
|
1311 | + throw new SodiumException( |
|
1312 | + 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' |
|
1313 | + ); |
|
1314 | + } |
|
1315 | + |
|
1316 | + # if (outlen_p != NULL) { |
|
1317 | + # *outlen_p = 0U; |
|
1318 | + # } |
|
1319 | + # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) { |
|
1320 | + # sodium_misuse(); |
|
1321 | + # } |
|
1322 | + |
|
1323 | + # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k); |
|
1324 | + # crypto_onetimeauth_poly1305_init(&poly1305_state, block); |
|
1325 | + # sodium_memzero(block, sizeof block); |
|
1326 | + $auth = new ParagonIE_Sodium_Core_Poly1305_State( |
|
1327 | + ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) |
|
1328 | + ); |
|
1329 | + |
|
1330 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); |
|
1331 | + $auth->update($aad); |
|
1332 | + |
|
1333 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, |
|
1334 | + # (0x10 - adlen) & 0xf); |
|
1335 | + $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); |
|
1336 | + |
|
1337 | + # memset(block, 0, sizeof block); |
|
1338 | + # block[0] = tag; |
|
1339 | + # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, |
|
1340 | + # state->nonce, 1U, state->k); |
|
1341 | + $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1342 | + ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63), |
|
1343 | + $st->getCombinedNonce(), |
|
1344 | + $st->getKey(), |
|
1345 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1346 | + ); |
|
1347 | + |
|
1348 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); |
|
1349 | + $auth->update($block); |
|
1350 | + |
|
1351 | + # out[0] = block[0]; |
|
1352 | + $out = $block[0]; |
|
1353 | + # c = out + (sizeof tag); |
|
1354 | + # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k); |
|
1355 | + $cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1356 | + $msg, |
|
1357 | + $st->getCombinedNonce(), |
|
1358 | + $st->getKey(), |
|
1359 | + ParagonIE_Sodium_Core_Util::store64_le(2) |
|
1360 | + ); |
|
1361 | + |
|
1362 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); |
|
1363 | + $auth->update($cipher); |
|
1364 | + |
|
1365 | + $out .= $cipher; |
|
1366 | + unset($cipher); |
|
1367 | + |
|
1368 | + # crypto_onetimeauth_poly1305_update |
|
1369 | + # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); |
|
1370 | + $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); |
|
1371 | + |
|
1372 | + # STORE64_LE(slen, (uint64_t) adlen); |
|
1373 | + $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); |
|
1374 | + |
|
1375 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
|
1376 | + $auth->update($slen); |
|
1377 | + |
|
1378 | + # STORE64_LE(slen, (sizeof block) + mlen); |
|
1379 | + $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); |
|
1380 | + |
|
1381 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
|
1382 | + $auth->update($slen); |
|
1383 | + |
|
1384 | + # mac = c + mlen; |
|
1385 | + # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); |
|
1386 | + $mac = $auth->finish(); |
|
1387 | + $out .= $mac; |
|
1388 | + |
|
1389 | + # sodium_memzero(&poly1305_state, sizeof poly1305_state); |
|
1390 | + unset($auth); |
|
1391 | + |
|
1392 | + |
|
1393 | + # XOR_BUF(STATE_INONCE(state), mac, |
|
1394 | + # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
|
1395 | + $st->xorNonce($mac); |
|
1396 | + |
|
1397 | + # sodium_increment(STATE_COUNTER(state), |
|
1398 | + # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); |
|
1399 | + $st->incrementCounter(); |
|
1400 | + // Overwrite by reference: |
|
1401 | + $state = $st->toString(); |
|
1402 | + |
|
1403 | + /** @var bool $rekey */ |
|
1404 | + $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; |
|
1405 | + # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || |
|
1406 | + # sodium_is_zero(STATE_COUNTER(state), |
|
1407 | + # crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) { |
|
1408 | + # crypto_secretstream_xchacha20poly1305_rekey(state); |
|
1409 | + # } |
|
1410 | + if ($rekey || $st->needsRekey()) { |
|
1411 | + // DO REKEY |
|
1412 | + self::secretstream_xchacha20poly1305_rekey($state); |
|
1413 | + } |
|
1414 | + # if (outlen_p != NULL) { |
|
1415 | + # *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen; |
|
1416 | + # } |
|
1417 | + return $out; |
|
1418 | + } |
|
1419 | + |
|
1420 | + /** |
|
1421 | + * @param string $state |
|
1422 | + * @param string $cipher |
|
1423 | + * @param string $aad |
|
1424 | + * @return bool|array{0: string, 1: int} |
|
1425 | + * @throws SodiumException |
|
1426 | + */ |
|
1427 | + public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') |
|
1428 | + { |
|
1429 | + $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
|
1430 | + |
|
1431 | + $cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher); |
|
1432 | + # mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES; |
|
1433 | + $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES; |
|
1434 | + $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); |
|
1435 | + |
|
1436 | + # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) { |
|
1437 | + # sodium_misuse(); |
|
1438 | + # } |
|
1439 | + if ((($msglen + 63) >> 6) > 0xfffffffe) { |
|
1440 | + throw new SodiumException( |
|
1441 | + 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' |
|
1442 | + ); |
|
1443 | + } |
|
1444 | + |
|
1445 | + # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k); |
|
1446 | + # crypto_onetimeauth_poly1305_init(&poly1305_state, block); |
|
1447 | + # sodium_memzero(block, sizeof block); |
|
1448 | + $auth = new ParagonIE_Sodium_Core_Poly1305_State( |
|
1449 | + ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) |
|
1450 | + ); |
|
1451 | + |
|
1452 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); |
|
1453 | + $auth->update($aad); |
|
1454 | + |
|
1455 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, |
|
1456 | + # (0x10 - adlen) & 0xf); |
|
1457 | + $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); |
|
1458 | + |
|
1459 | + |
|
1460 | + # memset(block, 0, sizeof block); |
|
1461 | + # block[0] = in[0]; |
|
1462 | + # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, |
|
1463 | + # state->nonce, 1U, state->k); |
|
1464 | + $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1465 | + $cipher[0] . str_repeat("\0", 63), |
|
1466 | + $st->getCombinedNonce(), |
|
1467 | + $st->getKey(), |
|
1468 | + ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1469 | + ); |
|
1470 | + # tag = block[0]; |
|
1471 | + # block[0] = in[0]; |
|
1472 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); |
|
1473 | + $tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]); |
|
1474 | + $block[0] = $cipher[0]; |
|
1475 | + $auth->update($block); |
|
1476 | + |
|
1477 | + |
|
1478 | + # c = in + (sizeof tag); |
|
1479 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); |
|
1480 | + $auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen)); |
|
1481 | + |
|
1482 | + # crypto_onetimeauth_poly1305_update |
|
1483 | + # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); |
|
1484 | + $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); |
|
1485 | + |
|
1486 | + # STORE64_LE(slen, (uint64_t) adlen); |
|
1487 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
|
1488 | + $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); |
|
1489 | + $auth->update($slen); |
|
1490 | + |
|
1491 | + # STORE64_LE(slen, (sizeof block) + mlen); |
|
1492 | + # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
|
1493 | + $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); |
|
1494 | + $auth->update($slen); |
|
1495 | + |
|
1496 | + # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); |
|
1497 | + # sodium_memzero(&poly1305_state, sizeof poly1305_state); |
|
1498 | + $mac = $auth->finish(); |
|
1499 | + |
|
1500 | + # stored_mac = c + mlen; |
|
1501 | + # if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) { |
|
1502 | + # sodium_memzero(mac, sizeof mac); |
|
1503 | + # return -1; |
|
1504 | + # } |
|
1505 | + |
|
1506 | + $stored = ParagonIE_Sodium_Core_Util::substr($cipher, $msglen + 1, 16); |
|
1507 | + if (!ParagonIE_Sodium_Core_Util::hashEquals($mac, $stored)) { |
|
1508 | + return false; |
|
1509 | + } |
|
1510 | + |
|
1511 | + # crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k); |
|
1512 | + $out = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1513 | + ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen), |
|
1514 | + $st->getCombinedNonce(), |
|
1515 | + $st->getKey(), |
|
1516 | + ParagonIE_Sodium_Core_Util::store64_le(2) |
|
1517 | + ); |
|
1518 | + |
|
1519 | + # XOR_BUF(STATE_INONCE(state), mac, |
|
1520 | + # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
|
1521 | + $st->xorNonce($mac); |
|
1522 | + |
|
1523 | + # sodium_increment(STATE_COUNTER(state), |
|
1524 | + # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); |
|
1525 | + $st->incrementCounter(); |
|
1526 | + |
|
1527 | + # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || |
|
1528 | + # sodium_is_zero(STATE_COUNTER(state), |
|
1529 | + # crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) { |
|
1530 | + # crypto_secretstream_xchacha20poly1305_rekey(state); |
|
1531 | + # } |
|
1532 | + |
|
1533 | + // Overwrite by reference: |
|
1534 | + $state = $st->toString(); |
|
1535 | + |
|
1536 | + /** @var bool $rekey */ |
|
1537 | + $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; |
|
1538 | + if ($rekey || $st->needsRekey()) { |
|
1539 | + // DO REKEY |
|
1540 | + self::secretstream_xchacha20poly1305_rekey($state); |
|
1541 | + } |
|
1542 | + return array($out, $tag); |
|
1543 | + } |
|
1544 | + |
|
1545 | + /** |
|
1546 | + * @param string $state |
|
1547 | + * @return void |
|
1548 | + * @throws SodiumException |
|
1549 | + */ |
|
1550 | + public static function secretstream_xchacha20poly1305_rekey(&$state) |
|
1551 | + { |
|
1552 | + $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
|
1553 | + # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + |
|
1554 | + # crypto_secretstream_xchacha20poly1305_INONCEBYTES]; |
|
1555 | + # size_t i; |
|
1556 | + # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { |
|
1557 | + # new_key_and_inonce[i] = state->k[i]; |
|
1558 | + # } |
|
1559 | + $new_key_and_inonce = $st->getKey(); |
|
1560 | + |
|
1561 | + # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { |
|
1562 | + # new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] = |
|
1563 | + # STATE_INONCE(state)[i]; |
|
1564 | + # } |
|
1565 | + $new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR($st->getNonce(), 0, 8); |
|
1566 | + |
|
1567 | + # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce, |
|
1568 | + # sizeof new_key_and_inonce, |
|
1569 | + # state->nonce, state->k); |
|
1570 | + |
|
1571 | + $st->rekey(ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1572 | + $new_key_and_inonce, |
|
1573 | + $st->getCombinedNonce(), |
|
1574 | + $st->getKey(), |
|
1575 | + ParagonIE_Sodium_Core_Util::store64_le(0) |
|
1576 | + )); |
|
1577 | + |
|
1578 | + # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { |
|
1579 | + # state->k[i] = new_key_and_inonce[i]; |
|
1580 | + # } |
|
1581 | + # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { |
|
1582 | + # STATE_INONCE(state)[i] = |
|
1583 | + # new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i]; |
|
1584 | + # } |
|
1585 | + # _crypto_secretstream_xchacha20poly1305_counter_reset(state); |
|
1586 | + $st->counterReset(); |
|
1587 | + |
|
1588 | + $state = $st->toString(); |
|
1589 | + } |
|
1590 | + |
|
1591 | + /** |
|
1592 | + * Detached Ed25519 signature. |
|
1593 | + * |
|
1594 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1595 | + * |
|
1596 | + * @param string $message |
|
1597 | + * @param string $sk |
|
1598 | + * @return string |
|
1599 | + * @throws SodiumException |
|
1600 | + * @throws TypeError |
|
1601 | + */ |
|
1602 | + public static function sign_detached($message, $sk) |
|
1603 | + { |
|
1604 | + return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); |
|
1605 | + } |
|
1606 | + |
|
1607 | + /** |
|
1608 | + * Attached Ed25519 signature. (Returns a signed message.) |
|
1609 | + * |
|
1610 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1611 | + * |
|
1612 | + * @param string $message |
|
1613 | + * @param string $sk |
|
1614 | + * @return string |
|
1615 | + * @throws SodiumException |
|
1616 | + * @throws TypeError |
|
1617 | + */ |
|
1618 | + public static function sign($message, $sk) |
|
1619 | + { |
|
1620 | + return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); |
|
1621 | + } |
|
1622 | + |
|
1623 | + /** |
|
1624 | + * Opens a signed message. If valid, returns the message. |
|
1625 | + * |
|
1626 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1627 | + * |
|
1628 | + * @param string $signedMessage |
|
1629 | + * @param string $pk |
|
1630 | + * @return string |
|
1631 | + * @throws SodiumException |
|
1632 | + * @throws TypeError |
|
1633 | + */ |
|
1634 | + public static function sign_open($signedMessage, $pk) |
|
1635 | + { |
|
1636 | + return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); |
|
1637 | + } |
|
1638 | + |
|
1639 | + /** |
|
1640 | + * Verify a detached signature of a given message and public key. |
|
1641 | + * |
|
1642 | + * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. |
|
1643 | + * |
|
1644 | + * @param string $signature |
|
1645 | + * @param string $message |
|
1646 | + * @param string $pk |
|
1647 | + * @return bool |
|
1648 | + * @throws SodiumException |
|
1649 | + * @throws TypeError |
|
1650 | + */ |
|
1651 | + public static function sign_verify_detached($signature, $message, $pk) |
|
1652 | + { |
|
1653 | + return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); |
|
1654 | + } |
|
1655 | 1655 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Crypto', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Crypto', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -75,13 +75,13 @@ discard block |
||
75 | 75 | $key = '' |
76 | 76 | ) { |
77 | 77 | /** @var int $len - Length of message (ciphertext + MAC) */ |
78 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
78 | + $len = ParagonIE_Sodium_Core_Util::strlen( $message ); |
|
79 | 79 | |
80 | 80 | /** @var int $clen - Length of ciphertext */ |
81 | 81 | $clen = $len - self::aead_chacha20poly1305_ABYTES; |
82 | 82 | |
83 | 83 | /** @var int $adlen - Length of associated data */ |
84 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
84 | + $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad ); |
|
85 | 85 | |
86 | 86 | /** @var string $mac - Message authentication code */ |
87 | 87 | $mac = ParagonIE_Sodium_Core_Util::substr( |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | ); |
92 | 92 | |
93 | 93 | /** @var string $ciphertext - The encrypted message (sans MAC) */ |
94 | - $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 0, $clen); |
|
94 | + $ciphertext = ParagonIE_Sodium_Core_Util::substr( $message, 0, $clen ); |
|
95 | 95 | |
96 | 96 | /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
97 | 97 | $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
@@ -101,21 +101,21 @@ discard block |
||
101 | 101 | ); |
102 | 102 | |
103 | 103 | /* Recalculate the Poly1305 authentication tag (MAC): */ |
104 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
104 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( $block0 ); |
|
105 | 105 | try { |
106 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
107 | - } catch (SodiumException $ex) { |
|
106 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
107 | + } catch ( SodiumException $ex ) { |
|
108 | 108 | $block0 = null; |
109 | 109 | } |
110 | - $state->update($ad); |
|
111 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
112 | - $state->update($ciphertext); |
|
113 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
110 | + $state->update( $ad ); |
|
111 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) ); |
|
112 | + $state->update( $ciphertext ); |
|
113 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $clen ) ); |
|
114 | 114 | $computed_mac = $state->finish(); |
115 | 115 | |
116 | 116 | /* Compare the given MAC with the recalculated MAC: */ |
117 | - if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { |
|
118 | - throw new SodiumException('Invalid MAC'); |
|
117 | + if ( ! ParagonIE_Sodium_Core_Util::verify_16( $computed_mac, $mac ) ) { |
|
118 | + throw new SodiumException( 'Invalid MAC' ); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | // Here, we know that the MAC is valid, so we decrypt and return the plaintext |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | $ciphertext, |
124 | 124 | $nonce, |
125 | 125 | $key, |
126 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
126 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
127 | 127 | ); |
128 | 128 | } |
129 | 129 | |
@@ -147,10 +147,10 @@ discard block |
||
147 | 147 | $key = '' |
148 | 148 | ) { |
149 | 149 | /** @var int $len - Length of the plaintext message */ |
150 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
150 | + $len = ParagonIE_Sodium_Core_Util::strlen( $message ); |
|
151 | 151 | |
152 | 152 | /** @var int $adlen - Length of the associated data */ |
153 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
153 | + $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad ); |
|
154 | 154 | |
155 | 155 | /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
156 | 156 | $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
@@ -158,10 +158,10 @@ discard block |
||
158 | 158 | $nonce, |
159 | 159 | $key |
160 | 160 | ); |
161 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
161 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( $block0 ); |
|
162 | 162 | try { |
163 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
164 | - } catch (SodiumException $ex) { |
|
163 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
164 | + } catch ( SodiumException $ex ) { |
|
165 | 165 | $block0 = null; |
166 | 166 | } |
167 | 167 | |
@@ -170,13 +170,13 @@ discard block |
||
170 | 170 | $message, |
171 | 171 | $nonce, |
172 | 172 | $key, |
173 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
173 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
174 | 174 | ); |
175 | 175 | |
176 | - $state->update($ad); |
|
177 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
178 | - $state->update($ciphertext); |
|
179 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
|
176 | + $state->update( $ad ); |
|
177 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) ); |
|
178 | + $state->update( $ciphertext ); |
|
179 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $len ) ); |
|
180 | 180 | return $ciphertext . $state->finish(); |
181 | 181 | } |
182 | 182 | |
@@ -200,10 +200,10 @@ discard block |
||
200 | 200 | $key = '' |
201 | 201 | ) { |
202 | 202 | /** @var int $adlen - Length of associated data */ |
203 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
203 | + $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad ); |
|
204 | 204 | |
205 | 205 | /** @var int $len - Length of message (ciphertext + MAC) */ |
206 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
206 | + $len = ParagonIE_Sodium_Core_Util::strlen( $message ); |
|
207 | 207 | |
208 | 208 | /** @var int $clen - Length of ciphertext */ |
209 | 209 | $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES; |
@@ -230,23 +230,23 @@ discard block |
||
230 | 230 | ); |
231 | 231 | |
232 | 232 | /* Recalculate the Poly1305 authentication tag (MAC): */ |
233 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
233 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( $block0 ); |
|
234 | 234 | try { |
235 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
236 | - } catch (SodiumException $ex) { |
|
235 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
236 | + } catch ( SodiumException $ex ) { |
|
237 | 237 | $block0 = null; |
238 | 238 | } |
239 | - $state->update($ad); |
|
240 | - $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); |
|
241 | - $state->update($ciphertext); |
|
242 | - $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf)); |
|
243 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
244 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
|
239 | + $state->update( $ad ); |
|
240 | + $state->update( str_repeat( "\x00", ( ( 0x10 - $adlen ) & 0xf ) ) ); |
|
241 | + $state->update( $ciphertext ); |
|
242 | + $state->update( str_repeat( "\x00", ( 0x10 - $clen ) & 0xf ) ); |
|
243 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) ); |
|
244 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $clen ) ); |
|
245 | 245 | $computed_mac = $state->finish(); |
246 | 246 | |
247 | 247 | /* Compare the given MAC with the recalculated MAC: */ |
248 | - if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { |
|
249 | - throw new SodiumException('Invalid MAC'); |
|
248 | + if ( ! ParagonIE_Sodium_Core_Util::verify_16( $computed_mac, $mac ) ) { |
|
249 | + throw new SodiumException( 'Invalid MAC' ); |
|
250 | 250 | } |
251 | 251 | |
252 | 252 | // Here, we know that the MAC is valid, so we decrypt and return the plaintext |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | $ciphertext, |
255 | 255 | $nonce, |
256 | 256 | $key, |
257 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
257 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
258 | 258 | ); |
259 | 259 | } |
260 | 260 | |
@@ -278,10 +278,10 @@ discard block |
||
278 | 278 | $key = '' |
279 | 279 | ) { |
280 | 280 | /** @var int $len - Length of the plaintext message */ |
281 | - $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
281 | + $len = ParagonIE_Sodium_Core_Util::strlen( $message ); |
|
282 | 282 | |
283 | 283 | /** @var int $adlen - Length of the associated data */ |
284 | - $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
284 | + $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad ); |
|
285 | 285 | |
286 | 286 | /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
287 | 287 | $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( |
@@ -289,10 +289,10 @@ discard block |
||
289 | 289 | $nonce, |
290 | 290 | $key |
291 | 291 | ); |
292 | - $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
292 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( $block0 ); |
|
293 | 293 | try { |
294 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
295 | - } catch (SodiumException $ex) { |
|
294 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
295 | + } catch ( SodiumException $ex ) { |
|
296 | 296 | $block0 = null; |
297 | 297 | } |
298 | 298 | |
@@ -301,15 +301,15 @@ discard block |
||
301 | 301 | $message, |
302 | 302 | $nonce, |
303 | 303 | $key, |
304 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
304 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
305 | 305 | ); |
306 | 306 | |
307 | - $state->update($ad); |
|
308 | - $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); |
|
309 | - $state->update($ciphertext); |
|
310 | - $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf))); |
|
311 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
312 | - $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
|
307 | + $state->update( $ad ); |
|
308 | + $state->update( str_repeat( "\x00", ( ( 0x10 - $adlen ) & 0xf ) ) ); |
|
309 | + $state->update( $ciphertext ); |
|
310 | + $state->update( str_repeat( "\x00", ( ( 0x10 - $len ) & 0xf ) ) ); |
|
311 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) ); |
|
312 | + $state->update( ParagonIE_Sodium_Core_Util::store64_le( $len ) ); |
|
313 | 313 | return $ciphertext . $state->finish(); |
314 | 314 | } |
315 | 315 | |
@@ -333,13 +333,13 @@ discard block |
||
333 | 333 | $key = '' |
334 | 334 | ) { |
335 | 335 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
336 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
336 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 0, 16 ), |
|
337 | 337 | $key |
338 | 338 | ); |
339 | 339 | $nonceLast = "\x00\x00\x00\x00" . |
340 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
340 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ); |
|
341 | 341 | |
342 | - return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey); |
|
342 | + return self::aead_chacha20poly1305_ietf_decrypt( $message, $ad, $nonceLast, $subkey ); |
|
343 | 343 | } |
344 | 344 | |
345 | 345 | /** |
@@ -362,13 +362,13 @@ discard block |
||
362 | 362 | $key = '' |
363 | 363 | ) { |
364 | 364 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
365 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
365 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 0, 16 ), |
|
366 | 366 | $key |
367 | 367 | ); |
368 | 368 | $nonceLast = "\x00\x00\x00\x00" . |
369 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
369 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ); |
|
370 | 370 | |
371 | - return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey); |
|
371 | + return self::aead_chacha20poly1305_ietf_encrypt( $message, $ad, $nonceLast, $subkey ); |
|
372 | 372 | } |
373 | 373 | |
374 | 374 | /** |
@@ -381,10 +381,10 @@ discard block |
||
381 | 381 | * @return string |
382 | 382 | * @throws TypeError |
383 | 383 | */ |
384 | - public static function auth($message, $key) |
|
384 | + public static function auth( $message, $key ) |
|
385 | 385 | { |
386 | 386 | return ParagonIE_Sodium_Core_Util::substr( |
387 | - hash_hmac('sha512', $message, $key, true), |
|
387 | + hash_hmac( 'sha512', $message, $key, true ), |
|
388 | 388 | 0, |
389 | 389 | 32 |
390 | 390 | ); |
@@ -402,11 +402,11 @@ discard block |
||
402 | 402 | * @throws SodiumException |
403 | 403 | * @throws TypeError |
404 | 404 | */ |
405 | - public static function auth_verify($mac, $message, $key) |
|
405 | + public static function auth_verify( $mac, $message, $key ) |
|
406 | 406 | { |
407 | 407 | return ParagonIE_Sodium_Core_Util::hashEquals( |
408 | 408 | $mac, |
409 | - self::auth($message, $key) |
|
409 | + self::auth( $message, $key ) |
|
410 | 410 | ); |
411 | 411 | } |
412 | 412 | |
@@ -422,14 +422,14 @@ discard block |
||
422 | 422 | * @throws SodiumException |
423 | 423 | * @throws TypeError |
424 | 424 | */ |
425 | - public static function box($plaintext, $nonce, $keypair) |
|
425 | + public static function box( $plaintext, $nonce, $keypair ) |
|
426 | 426 | { |
427 | 427 | $c = self::secretbox( |
428 | 428 | $plaintext, |
429 | 429 | $nonce, |
430 | 430 | self::box_beforenm( |
431 | - self::box_secretkey($keypair), |
|
432 | - self::box_publickey($keypair) |
|
431 | + self::box_secretkey( $keypair ), |
|
432 | + self::box_publickey( $keypair ) |
|
433 | 433 | ) |
434 | 434 | ); |
435 | 435 | return $c; |
@@ -446,16 +446,16 @@ discard block |
||
446 | 446 | * @throws SodiumException |
447 | 447 | * @throws TypeError |
448 | 448 | */ |
449 | - public static function box_seal($message, $publicKey) |
|
449 | + public static function box_seal( $message, $publicKey ) |
|
450 | 450 | { |
451 | 451 | /** @var string $ephemeralKeypair */ |
452 | 452 | $ephemeralKeypair = self::box_keypair(); |
453 | 453 | |
454 | 454 | /** @var string $ephemeralSK */ |
455 | - $ephemeralSK = self::box_secretkey($ephemeralKeypair); |
|
455 | + $ephemeralSK = self::box_secretkey( $ephemeralKeypair ); |
|
456 | 456 | |
457 | 457 | /** @var string $ephemeralPK */ |
458 | - $ephemeralPK = self::box_publickey($ephemeralKeypair); |
|
458 | + $ephemeralPK = self::box_publickey( $ephemeralKeypair ); |
|
459 | 459 | |
460 | 460 | /** @var string $nonce */ |
461 | 461 | $nonce = self::generichash( |
@@ -465,15 +465,15 @@ discard block |
||
465 | 465 | ); |
466 | 466 | |
467 | 467 | /** @var string $keypair - The combined keypair used in crypto_box() */ |
468 | - $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey); |
|
468 | + $keypair = self::box_keypair_from_secretkey_and_publickey( $ephemeralSK, $publicKey ); |
|
469 | 469 | |
470 | 470 | /** @var string $ciphertext Ciphertext + MAC from crypto_box */ |
471 | - $ciphertext = self::box($message, $nonce, $keypair); |
|
471 | + $ciphertext = self::box( $message, $nonce, $keypair ); |
|
472 | 472 | try { |
473 | - ParagonIE_Sodium_Compat::memzero($ephemeralKeypair); |
|
474 | - ParagonIE_Sodium_Compat::memzero($ephemeralSK); |
|
475 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
476 | - } catch (SodiumException $ex) { |
|
473 | + ParagonIE_Sodium_Compat::memzero( $ephemeralKeypair ); |
|
474 | + ParagonIE_Sodium_Compat::memzero( $ephemeralSK ); |
|
475 | + ParagonIE_Sodium_Compat::memzero( $nonce ); |
|
476 | + } catch ( SodiumException $ex ) { |
|
477 | 477 | $ephemeralKeypair = null; |
478 | 478 | $ephemeralSK = null; |
479 | 479 | $nonce = null; |
@@ -492,19 +492,19 @@ discard block |
||
492 | 492 | * @throws SodiumException |
493 | 493 | * @throws TypeError |
494 | 494 | */ |
495 | - public static function box_seal_open($message, $keypair) |
|
495 | + public static function box_seal_open( $message, $keypair ) |
|
496 | 496 | { |
497 | 497 | /** @var string $ephemeralPK */ |
498 | - $ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32); |
|
498 | + $ephemeralPK = ParagonIE_Sodium_Core_Util::substr( $message, 0, 32 ); |
|
499 | 499 | |
500 | 500 | /** @var string $ciphertext (ciphertext + MAC) */ |
501 | - $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 32); |
|
501 | + $ciphertext = ParagonIE_Sodium_Core_Util::substr( $message, 32 ); |
|
502 | 502 | |
503 | 503 | /** @var string $secretKey */ |
504 | - $secretKey = self::box_secretkey($keypair); |
|
504 | + $secretKey = self::box_secretkey( $keypair ); |
|
505 | 505 | |
506 | 506 | /** @var string $publicKey */ |
507 | - $publicKey = self::box_publickey($keypair); |
|
507 | + $publicKey = self::box_publickey( $keypair ); |
|
508 | 508 | |
509 | 509 | /** @var string $nonce */ |
510 | 510 | $nonce = self::generichash( |
@@ -514,15 +514,15 @@ discard block |
||
514 | 514 | ); |
515 | 515 | |
516 | 516 | /** @var string $keypair */ |
517 | - $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK); |
|
517 | + $keypair = self::box_keypair_from_secretkey_and_publickey( $secretKey, $ephemeralPK ); |
|
518 | 518 | |
519 | 519 | /** @var string $m */ |
520 | - $m = self::box_open($ciphertext, $nonce, $keypair); |
|
520 | + $m = self::box_open( $ciphertext, $nonce, $keypair ); |
|
521 | 521 | try { |
522 | - ParagonIE_Sodium_Compat::memzero($secretKey); |
|
523 | - ParagonIE_Sodium_Compat::memzero($ephemeralPK); |
|
524 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
525 | - } catch (SodiumException $ex) { |
|
522 | + ParagonIE_Sodium_Compat::memzero( $secretKey ); |
|
523 | + ParagonIE_Sodium_Compat::memzero( $ephemeralPK ); |
|
524 | + ParagonIE_Sodium_Compat::memzero( $nonce ); |
|
525 | + } catch ( SodiumException $ex ) { |
|
526 | 526 | $secretKey = null; |
527 | 527 | $ephemeralPK = null; |
528 | 528 | $nonce = null; |
@@ -541,11 +541,11 @@ discard block |
||
541 | 541 | * @throws SodiumException |
542 | 542 | * @throws TypeError |
543 | 543 | */ |
544 | - public static function box_beforenm($sk, $pk) |
|
544 | + public static function box_beforenm( $sk, $pk ) |
|
545 | 545 | { |
546 | 546 | return ParagonIE_Sodium_Core_HSalsa20::hsalsa20( |
547 | - str_repeat("\x00", 16), |
|
548 | - self::scalarmult($sk, $pk) |
|
547 | + str_repeat( "\x00", 16 ), |
|
548 | + self::scalarmult( $sk, $pk ) |
|
549 | 549 | ); |
550 | 550 | } |
551 | 551 | |
@@ -559,8 +559,8 @@ discard block |
||
559 | 559 | */ |
560 | 560 | public static function box_keypair() |
561 | 561 | { |
562 | - $sKey = random_bytes(32); |
|
563 | - $pKey = self::scalarmult_base($sKey); |
|
562 | + $sKey = random_bytes( 32 ); |
|
563 | + $pKey = self::scalarmult_base( $sKey ); |
|
564 | 564 | return $sKey . $pKey; |
565 | 565 | } |
566 | 566 | |
@@ -570,14 +570,14 @@ discard block |
||
570 | 570 | * @throws SodiumException |
571 | 571 | * @throws TypeError |
572 | 572 | */ |
573 | - public static function box_seed_keypair($seed) |
|
573 | + public static function box_seed_keypair( $seed ) |
|
574 | 574 | { |
575 | 575 | $sKey = ParagonIE_Sodium_Core_Util::substr( |
576 | - hash('sha512', $seed, true), |
|
576 | + hash( 'sha512', $seed, true ), |
|
577 | 577 | 0, |
578 | 578 | 32 |
579 | 579 | ); |
580 | - $pKey = self::scalarmult_base($sKey); |
|
580 | + $pKey = self::scalarmult_base( $sKey ); |
|
581 | 581 | return $sKey . $pKey; |
582 | 582 | } |
583 | 583 | |
@@ -589,10 +589,10 @@ discard block |
||
589 | 589 | * @return string |
590 | 590 | * @throws TypeError |
591 | 591 | */ |
592 | - public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) |
|
592 | + public static function box_keypair_from_secretkey_and_publickey( $sKey, $pKey ) |
|
593 | 593 | { |
594 | - return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . |
|
595 | - ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32); |
|
594 | + return ParagonIE_Sodium_Core_Util::substr( $sKey, 0, 32 ) . |
|
595 | + ParagonIE_Sodium_Core_Util::substr( $pKey, 0, 32 ); |
|
596 | 596 | } |
597 | 597 | |
598 | 598 | /** |
@@ -603,14 +603,14 @@ discard block |
||
603 | 603 | * @throws RangeException |
604 | 604 | * @throws TypeError |
605 | 605 | */ |
606 | - public static function box_secretkey($keypair) |
|
606 | + public static function box_secretkey( $keypair ) |
|
607 | 607 | { |
608 | - if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { |
|
608 | + if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== 64 ) { |
|
609 | 609 | throw new RangeException( |
610 | 610 | 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
611 | 611 | ); |
612 | 612 | } |
613 | - return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32); |
|
613 | + return ParagonIE_Sodium_Core_Util::substr( $keypair, 0, 32 ); |
|
614 | 614 | } |
615 | 615 | |
616 | 616 | /** |
@@ -621,14 +621,14 @@ discard block |
||
621 | 621 | * @throws RangeException |
622 | 622 | * @throws TypeError |
623 | 623 | */ |
624 | - public static function box_publickey($keypair) |
|
624 | + public static function box_publickey( $keypair ) |
|
625 | 625 | { |
626 | - if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
626 | + if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) { |
|
627 | 627 | throw new RangeException( |
628 | 628 | 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
629 | 629 | ); |
630 | 630 | } |
631 | - return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32); |
|
631 | + return ParagonIE_Sodium_Core_Util::substr( $keypair, 32, 32 ); |
|
632 | 632 | } |
633 | 633 | |
634 | 634 | /** |
@@ -640,14 +640,14 @@ discard block |
||
640 | 640 | * @throws SodiumException |
641 | 641 | * @throws TypeError |
642 | 642 | */ |
643 | - public static function box_publickey_from_secretkey($sKey) |
|
643 | + public static function box_publickey_from_secretkey( $sKey ) |
|
644 | 644 | { |
645 | - if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { |
|
645 | + if ( ParagonIE_Sodium_Core_Util::strlen( $sKey ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES ) { |
|
646 | 646 | throw new RangeException( |
647 | 647 | 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.' |
648 | 648 | ); |
649 | 649 | } |
650 | - return self::scalarmult_base($sKey); |
|
650 | + return self::scalarmult_base( $sKey ); |
|
651 | 651 | } |
652 | 652 | |
653 | 653 | /** |
@@ -662,14 +662,14 @@ discard block |
||
662 | 662 | * @throws SodiumException |
663 | 663 | * @throws TypeError |
664 | 664 | */ |
665 | - public static function box_open($ciphertext, $nonce, $keypair) |
|
665 | + public static function box_open( $ciphertext, $nonce, $keypair ) |
|
666 | 666 | { |
667 | 667 | return self::secretbox_open( |
668 | 668 | $ciphertext, |
669 | 669 | $nonce, |
670 | 670 | self::box_beforenm( |
671 | - self::box_secretkey($keypair), |
|
672 | - self::box_publickey($keypair) |
|
671 | + self::box_secretkey( $keypair ), |
|
672 | + self::box_publickey( $keypair ) |
|
673 | 673 | ) |
674 | 674 | ); |
675 | 675 | } |
@@ -687,34 +687,34 @@ discard block |
||
687 | 687 | * @throws SodiumException |
688 | 688 | * @throws TypeError |
689 | 689 | */ |
690 | - public static function generichash($message, $key = '', $outlen = 32) |
|
690 | + public static function generichash( $message, $key = '', $outlen = 32 ) |
|
691 | 691 | { |
692 | 692 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
693 | 693 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
694 | 694 | |
695 | 695 | $k = null; |
696 | - if (!empty($key)) { |
|
696 | + if ( ! empty( $key ) ) { |
|
697 | 697 | /** @var SplFixedArray $k */ |
698 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
699 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
700 | - throw new RangeException('Invalid key size'); |
|
698 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $key ); |
|
699 | + if ( $k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES ) { |
|
700 | + throw new RangeException( 'Invalid key size' ); |
|
701 | 701 | } |
702 | 702 | } |
703 | 703 | |
704 | 704 | /** @var SplFixedArray $in */ |
705 | - $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
705 | + $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $message ); |
|
706 | 706 | |
707 | 707 | /** @var SplFixedArray $ctx */ |
708 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen); |
|
709 | - ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count()); |
|
708 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init( $k, $outlen ); |
|
709 | + ParagonIE_Sodium_Core_BLAKE2b::update( $ctx, $in, $in->count() ); |
|
710 | 710 | |
711 | 711 | /** @var SplFixedArray $out */ |
712 | - $out = new SplFixedArray($outlen); |
|
713 | - $out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out); |
|
712 | + $out = new SplFixedArray( $outlen ); |
|
713 | + $out = ParagonIE_Sodium_Core_BLAKE2b::finish( $ctx, $out ); |
|
714 | 714 | |
715 | 715 | /** @var array<int, int> */ |
716 | 716 | $outArray = $out->toArray(); |
717 | - return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
717 | + return ParagonIE_Sodium_Core_Util::intArrayToString( $outArray ); |
|
718 | 718 | } |
719 | 719 | |
720 | 720 | /** |
@@ -728,22 +728,22 @@ discard block |
||
728 | 728 | * @throws SodiumException |
729 | 729 | * @throws TypeError |
730 | 730 | */ |
731 | - public static function generichash_final($ctx, $outlen = 32) |
|
731 | + public static function generichash_final( $ctx, $outlen = 32 ) |
|
732 | 732 | { |
733 | - if (!is_string($ctx)) { |
|
734 | - throw new TypeError('Context must be a string'); |
|
733 | + if ( ! is_string( $ctx ) ) { |
|
734 | + throw new TypeError( 'Context must be a string' ); |
|
735 | 735 | } |
736 | - $out = new SplFixedArray($outlen); |
|
736 | + $out = new SplFixedArray( $outlen ); |
|
737 | 737 | |
738 | 738 | /** @var SplFixedArray $context */ |
739 | - $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
739 | + $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext( $ctx ); |
|
740 | 740 | |
741 | 741 | /** @var SplFixedArray $out */ |
742 | - $out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out); |
|
742 | + $out = ParagonIE_Sodium_Core_BLAKE2b::finish( $context, $out ); |
|
743 | 743 | |
744 | 744 | /** @var array<int, int> */ |
745 | 745 | $outArray = $out->toArray(); |
746 | - return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
|
746 | + return ParagonIE_Sodium_Core_Util::intArrayToString( $outArray ); |
|
747 | 747 | } |
748 | 748 | |
749 | 749 | /** |
@@ -758,23 +758,23 @@ discard block |
||
758 | 758 | * @throws SodiumException |
759 | 759 | * @throws TypeError |
760 | 760 | */ |
761 | - public static function generichash_init($key = '', $outputLength = 32) |
|
761 | + public static function generichash_init( $key = '', $outputLength = 32 ) |
|
762 | 762 | { |
763 | 763 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
764 | 764 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
765 | 765 | |
766 | 766 | $k = null; |
767 | - if (!empty($key)) { |
|
768 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
769 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
770 | - throw new RangeException('Invalid key size'); |
|
767 | + if ( ! empty( $key ) ) { |
|
768 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $key ); |
|
769 | + if ( $k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES ) { |
|
770 | + throw new RangeException( 'Invalid key size' ); |
|
771 | 771 | } |
772 | 772 | } |
773 | 773 | |
774 | 774 | /** @var SplFixedArray $ctx */ |
775 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength); |
|
775 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init( $k, $outputLength ); |
|
776 | 776 | |
777 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
|
777 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString( $ctx ); |
|
778 | 778 | } |
779 | 779 | |
780 | 780 | /** |
@@ -801,27 +801,27 @@ discard block |
||
801 | 801 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
802 | 802 | |
803 | 803 | $k = null; |
804 | - if (!empty($key)) { |
|
805 | - $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
|
806 | - if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
|
807 | - throw new RangeException('Invalid key size'); |
|
804 | + if ( ! empty( $key ) ) { |
|
805 | + $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $key ); |
|
806 | + if ( $k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES ) { |
|
807 | + throw new RangeException( 'Invalid key size' ); |
|
808 | 808 | } |
809 | 809 | } |
810 | - if (!empty($salt)) { |
|
811 | - $s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($salt); |
|
810 | + if ( ! empty( $salt ) ) { |
|
811 | + $s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $salt ); |
|
812 | 812 | } else { |
813 | 813 | $s = null; |
814 | 814 | } |
815 | - if (!empty($salt)) { |
|
816 | - $p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($personal); |
|
815 | + if ( ! empty( $salt ) ) { |
|
816 | + $p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $personal ); |
|
817 | 817 | } else { |
818 | 818 | $p = null; |
819 | 819 | } |
820 | 820 | |
821 | 821 | /** @var SplFixedArray $ctx */ |
822 | - $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength, $s, $p); |
|
822 | + $ctx = ParagonIE_Sodium_Core_BLAKE2b::init( $k, $outputLength, $s, $p ); |
|
823 | 823 | |
824 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
|
824 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString( $ctx ); |
|
825 | 825 | } |
826 | 826 | |
827 | 827 | /** |
@@ -835,20 +835,20 @@ discard block |
||
835 | 835 | * @throws SodiumException |
836 | 836 | * @throws TypeError |
837 | 837 | */ |
838 | - public static function generichash_update($ctx, $message) |
|
838 | + public static function generichash_update( $ctx, $message ) |
|
839 | 839 | { |
840 | 840 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
841 | 841 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
842 | 842 | |
843 | 843 | /** @var SplFixedArray $context */ |
844 | - $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
|
844 | + $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext( $ctx ); |
|
845 | 845 | |
846 | 846 | /** @var SplFixedArray $in */ |
847 | - $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
|
847 | + $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $message ); |
|
848 | 848 | |
849 | - ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count()); |
|
849 | + ParagonIE_Sodium_Core_BLAKE2b::update( $context, $in, $in->count() ); |
|
850 | 850 | |
851 | - return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context); |
|
851 | + return ParagonIE_Sodium_Core_BLAKE2b::contextToString( $context ); |
|
852 | 852 | } |
853 | 853 | |
854 | 854 | /** |
@@ -864,10 +864,10 @@ discard block |
||
864 | 864 | * @throws SodiumException |
865 | 865 | * @throws TypeError |
866 | 866 | */ |
867 | - public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) |
|
867 | + public static function keyExchange( $my_sk, $their_pk, $client_pk, $server_pk ) |
|
868 | 868 | { |
869 | 869 | return ParagonIE_Sodium_Compat::crypto_generichash( |
870 | - ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) . |
|
870 | + ParagonIE_Sodium_Compat::crypto_scalarmult( $my_sk, $their_pk ) . |
|
871 | 871 | $client_pk . |
872 | 872 | $server_pk |
873 | 873 | ); |
@@ -885,10 +885,10 @@ discard block |
||
885 | 885 | * @throws SodiumException |
886 | 886 | * @throws TypeError |
887 | 887 | */ |
888 | - public static function scalarmult($sKey, $pKey) |
|
888 | + public static function scalarmult( $sKey, $pKey ) |
|
889 | 889 | { |
890 | - $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); |
|
891 | - self::scalarmult_throw_if_zero($q); |
|
890 | + $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10( $sKey, $pKey ); |
|
891 | + self::scalarmult_throw_if_zero( $q ); |
|
892 | 892 | return $q; |
893 | 893 | } |
894 | 894 | |
@@ -902,10 +902,10 @@ discard block |
||
902 | 902 | * @throws SodiumException |
903 | 903 | * @throws TypeError |
904 | 904 | */ |
905 | - public static function scalarmult_base($secret) |
|
905 | + public static function scalarmult_base( $secret ) |
|
906 | 906 | { |
907 | - $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); |
|
908 | - self::scalarmult_throw_if_zero($q); |
|
907 | + $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base( $secret ); |
|
908 | + self::scalarmult_throw_if_zero( $q ); |
|
909 | 909 | return $q; |
910 | 910 | } |
911 | 911 | |
@@ -917,16 +917,16 @@ discard block |
||
917 | 917 | * @throws SodiumException |
918 | 918 | * @throws TypeError |
919 | 919 | */ |
920 | - protected static function scalarmult_throw_if_zero($q) |
|
920 | + protected static function scalarmult_throw_if_zero( $q ) |
|
921 | 921 | { |
922 | 922 | $d = 0; |
923 | - for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { |
|
924 | - $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); |
|
923 | + for ( $i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i ) { |
|
924 | + $d |= ParagonIE_Sodium_Core_Util::chrToInt( $q[ $i ] ); |
|
925 | 925 | } |
926 | 926 | |
927 | 927 | /* branch-free variant of === 0 */ |
928 | - if (-(1 & (($d - 1) >> 8))) { |
|
929 | - throw new SodiumException('Zero public key is not allowed'); |
|
928 | + if (-( 1 & ( ( $d - 1 ) >> 8 ) )) { |
|
929 | + throw new SodiumException( 'Zero public key is not allowed' ); |
|
930 | 930 | } |
931 | 931 | } |
932 | 932 | |
@@ -942,26 +942,26 @@ discard block |
||
942 | 942 | * @throws SodiumException |
943 | 943 | * @throws TypeError |
944 | 944 | */ |
945 | - public static function secretbox($plaintext, $nonce, $key) |
|
945 | + public static function secretbox( $plaintext, $nonce, $key ) |
|
946 | 946 | { |
947 | 947 | /** @var string $subkey */ |
948 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
948 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key ); |
|
949 | 949 | |
950 | 950 | /** @var string $block0 */ |
951 | - $block0 = str_repeat("\x00", 32); |
|
951 | + $block0 = str_repeat( "\x00", 32 ); |
|
952 | 952 | |
953 | 953 | /** @var int $mlen - Length of the plaintext message */ |
954 | - $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
954 | + $mlen = ParagonIE_Sodium_Core_Util::strlen( $plaintext ); |
|
955 | 955 | $mlen0 = $mlen; |
956 | - if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
956 | + if ( $mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES ) { |
|
957 | 957 | $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES; |
958 | 958 | } |
959 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
959 | + $block0 .= ParagonIE_Sodium_Core_Util::substr( $plaintext, 0, $mlen0 ); |
|
960 | 960 | |
961 | 961 | /** @var string $block0 */ |
962 | 962 | $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
963 | 963 | $block0, |
964 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
964 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
965 | 965 | $subkey |
966 | 966 | ); |
967 | 967 | |
@@ -970,13 +970,13 @@ discard block |
||
970 | 970 | $block0, |
971 | 971 | self::secretbox_xsalsa20poly1305_ZEROBYTES |
972 | 972 | ); |
973 | - if ($mlen > $mlen0) { |
|
973 | + if ( $mlen > $mlen0 ) { |
|
974 | 974 | $c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
975 | 975 | ParagonIE_Sodium_Core_Util::substr( |
976 | 976 | $plaintext, |
977 | 977 | self::secretbox_xsalsa20poly1305_ZEROBYTES |
978 | 978 | ), |
979 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
979 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
980 | 980 | 1, |
981 | 981 | $subkey |
982 | 982 | ); |
@@ -989,18 +989,18 @@ discard block |
||
989 | 989 | ) |
990 | 990 | ); |
991 | 991 | try { |
992 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
993 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
994 | - } catch (SodiumException $ex) { |
|
992 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
993 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
994 | + } catch ( SodiumException $ex ) { |
|
995 | 995 | $block0 = null; |
996 | 996 | $subkey = null; |
997 | 997 | } |
998 | 998 | |
999 | - $state->update($c); |
|
999 | + $state->update( $c ); |
|
1000 | 1000 | |
1001 | 1001 | /** @var string $c - MAC || ciphertext */ |
1002 | 1002 | $c = $state->finish() . $c; |
1003 | - unset($state); |
|
1003 | + unset( $state ); |
|
1004 | 1004 | |
1005 | 1005 | return $c; |
1006 | 1006 | } |
@@ -1017,7 +1017,7 @@ discard block |
||
1017 | 1017 | * @throws SodiumException |
1018 | 1018 | * @throws TypeError |
1019 | 1019 | */ |
1020 | - public static function secretbox_open($ciphertext, $nonce, $key) |
|
1020 | + public static function secretbox_open( $ciphertext, $nonce, $key ) |
|
1021 | 1021 | { |
1022 | 1022 | /** @var string $mac */ |
1023 | 1023 | $mac = ParagonIE_Sodium_Core_Util::substr( |
@@ -1033,46 +1033,46 @@ discard block |
||
1033 | 1033 | ); |
1034 | 1034 | |
1035 | 1035 | /** @var int $clen */ |
1036 | - $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
1036 | + $clen = ParagonIE_Sodium_Core_Util::strlen( $c ); |
|
1037 | 1037 | |
1038 | 1038 | /** @var string $subkey */ |
1039 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
1039 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key ); |
|
1040 | 1040 | |
1041 | 1041 | /** @var string $block0 */ |
1042 | 1042 | $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
1043 | 1043 | 64, |
1044 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1044 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
1045 | 1045 | $subkey |
1046 | 1046 | ); |
1047 | 1047 | $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
1048 | 1048 | $mac, |
1049 | 1049 | $c, |
1050 | - ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
1050 | + ParagonIE_Sodium_Core_Util::substr( $block0, 0, 32 ) |
|
1051 | 1051 | ); |
1052 | - if (!$verified) { |
|
1052 | + if ( ! $verified ) { |
|
1053 | 1053 | try { |
1054 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
1055 | - } catch (SodiumException $ex) { |
|
1054 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
1055 | + } catch ( SodiumException $ex ) { |
|
1056 | 1056 | $subkey = null; |
1057 | 1057 | } |
1058 | - throw new SodiumException('Invalid MAC'); |
|
1058 | + throw new SodiumException( 'Invalid MAC' ); |
|
1059 | 1059 | } |
1060 | 1060 | |
1061 | 1061 | /** @var string $m - Decrypted message */ |
1062 | 1062 | $m = ParagonIE_Sodium_Core_Util::xorStrings( |
1063 | - ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES), |
|
1064 | - ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES) |
|
1063 | + ParagonIE_Sodium_Core_Util::substr( $block0, self::secretbox_xsalsa20poly1305_ZEROBYTES ), |
|
1064 | + ParagonIE_Sodium_Core_Util::substr( $c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES ) |
|
1065 | 1065 | ); |
1066 | - if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
1066 | + if ( $clen > self::secretbox_xsalsa20poly1305_ZEROBYTES ) { |
|
1067 | 1067 | // We had more than 1 block, so let's continue to decrypt the rest. |
1068 | 1068 | $m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
1069 | 1069 | ParagonIE_Sodium_Core_Util::substr( |
1070 | 1070 | $c, |
1071 | 1071 | self::secretbox_xsalsa20poly1305_ZEROBYTES |
1072 | 1072 | ), |
1073 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1073 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
1074 | 1074 | 1, |
1075 | - (string) $subkey |
|
1075 | + (string)$subkey |
|
1076 | 1076 | ); |
1077 | 1077 | } |
1078 | 1078 | return $m; |
@@ -1090,25 +1090,25 @@ discard block |
||
1090 | 1090 | * @throws SodiumException |
1091 | 1091 | * @throws TypeError |
1092 | 1092 | */ |
1093 | - public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) |
|
1093 | + public static function secretbox_xchacha20poly1305( $plaintext, $nonce, $key ) |
|
1094 | 1094 | { |
1095 | 1095 | /** @var string $subkey */ |
1096 | 1096 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
1097 | - ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
|
1097 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 0, 16 ), |
|
1098 | 1098 | $key |
1099 | 1099 | ); |
1100 | - $nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
1100 | + $nonceLast = ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ); |
|
1101 | 1101 | |
1102 | 1102 | /** @var string $block0 */ |
1103 | - $block0 = str_repeat("\x00", 32); |
|
1103 | + $block0 = str_repeat( "\x00", 32 ); |
|
1104 | 1104 | |
1105 | 1105 | /** @var int $mlen - Length of the plaintext message */ |
1106 | - $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
|
1106 | + $mlen = ParagonIE_Sodium_Core_Util::strlen( $plaintext ); |
|
1107 | 1107 | $mlen0 = $mlen; |
1108 | - if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
1108 | + if ( $mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES ) { |
|
1109 | 1109 | $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES; |
1110 | 1110 | } |
1111 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
1111 | + $block0 .= ParagonIE_Sodium_Core_Util::substr( $plaintext, 0, $mlen0 ); |
|
1112 | 1112 | |
1113 | 1113 | /** @var string $block0 */ |
1114 | 1114 | $block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
@@ -1122,7 +1122,7 @@ discard block |
||
1122 | 1122 | $block0, |
1123 | 1123 | self::secretbox_xchacha20poly1305_ZEROBYTES |
1124 | 1124 | ); |
1125 | - if ($mlen > $mlen0) { |
|
1125 | + if ( $mlen > $mlen0 ) { |
|
1126 | 1126 | $c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
1127 | 1127 | ParagonIE_Sodium_Core_Util::substr( |
1128 | 1128 | $plaintext, |
@@ -1130,7 +1130,7 @@ discard block |
||
1130 | 1130 | ), |
1131 | 1131 | $nonceLast, |
1132 | 1132 | $subkey, |
1133 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1133 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
1134 | 1134 | ); |
1135 | 1135 | } |
1136 | 1136 | $state = new ParagonIE_Sodium_Core_Poly1305_State( |
@@ -1141,18 +1141,18 @@ discard block |
||
1141 | 1141 | ) |
1142 | 1142 | ); |
1143 | 1143 | try { |
1144 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
1145 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
1146 | - } catch (SodiumException $ex) { |
|
1144 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
1145 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
1146 | + } catch ( SodiumException $ex ) { |
|
1147 | 1147 | $block0 = null; |
1148 | 1148 | $subkey = null; |
1149 | 1149 | } |
1150 | 1150 | |
1151 | - $state->update($c); |
|
1151 | + $state->update( $c ); |
|
1152 | 1152 | |
1153 | 1153 | /** @var string $c - MAC || ciphertext */ |
1154 | 1154 | $c = $state->finish() . $c; |
1155 | - unset($state); |
|
1155 | + unset( $state ); |
|
1156 | 1156 | |
1157 | 1157 | return $c; |
1158 | 1158 | } |
@@ -1169,7 +1169,7 @@ discard block |
||
1169 | 1169 | * @throws SodiumException |
1170 | 1170 | * @throws TypeError |
1171 | 1171 | */ |
1172 | - public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
|
1172 | + public static function secretbox_xchacha20poly1305_open( $ciphertext, $nonce, $key ) |
|
1173 | 1173 | { |
1174 | 1174 | /** @var string $mac */ |
1175 | 1175 | $mac = ParagonIE_Sodium_Core_Util::substr( |
@@ -1185,48 +1185,48 @@ discard block |
||
1185 | 1185 | ); |
1186 | 1186 | |
1187 | 1187 | /** @var int $clen */ |
1188 | - $clen = ParagonIE_Sodium_Core_Util::strlen($c); |
|
1188 | + $clen = ParagonIE_Sodium_Core_Util::strlen( $c ); |
|
1189 | 1189 | |
1190 | 1190 | /** @var string $subkey */ |
1191 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key); |
|
1191 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20( $nonce, $key ); |
|
1192 | 1192 | |
1193 | 1193 | /** @var string $block0 */ |
1194 | 1194 | $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
1195 | 1195 | 64, |
1196 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1196 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
1197 | 1197 | $subkey |
1198 | 1198 | ); |
1199 | 1199 | $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
1200 | 1200 | $mac, |
1201 | 1201 | $c, |
1202 | - ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
|
1202 | + ParagonIE_Sodium_Core_Util::substr( $block0, 0, 32 ) |
|
1203 | 1203 | ); |
1204 | 1204 | |
1205 | - if (!$verified) { |
|
1205 | + if ( ! $verified ) { |
|
1206 | 1206 | try { |
1207 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
1208 | - } catch (SodiumException $ex) { |
|
1207 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
1208 | + } catch ( SodiumException $ex ) { |
|
1209 | 1209 | $subkey = null; |
1210 | 1210 | } |
1211 | - throw new SodiumException('Invalid MAC'); |
|
1211 | + throw new SodiumException( 'Invalid MAC' ); |
|
1212 | 1212 | } |
1213 | 1213 | |
1214 | 1214 | /** @var string $m - Decrypted message */ |
1215 | 1215 | $m = ParagonIE_Sodium_Core_Util::xorStrings( |
1216 | - ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES), |
|
1217 | - ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES) |
|
1216 | + ParagonIE_Sodium_Core_Util::substr( $block0, self::secretbox_xchacha20poly1305_ZEROBYTES ), |
|
1217 | + ParagonIE_Sodium_Core_Util::substr( $c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES ) |
|
1218 | 1218 | ); |
1219 | 1219 | |
1220 | - if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) { |
|
1220 | + if ( $clen > self::secretbox_xchacha20poly1305_ZEROBYTES ) { |
|
1221 | 1221 | // We had more than 1 block, so let's continue to decrypt the rest. |
1222 | 1222 | $m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
1223 | 1223 | ParagonIE_Sodium_Core_Util::substr( |
1224 | 1224 | $c, |
1225 | 1225 | self::secretbox_xchacha20poly1305_ZEROBYTES |
1226 | 1226 | ), |
1227 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
1228 | - (string) $subkey, |
|
1229 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1227 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
1228 | + (string)$subkey, |
|
1229 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
1230 | 1230 | ); |
1231 | 1231 | } |
1232 | 1232 | return $m; |
@@ -1238,16 +1238,16 @@ discard block |
||
1238 | 1238 | * @throws Exception |
1239 | 1239 | * @throws SodiumException |
1240 | 1240 | */ |
1241 | - public static function secretstream_xchacha20poly1305_init_push($key) |
|
1241 | + public static function secretstream_xchacha20poly1305_init_push( $key ) |
|
1242 | 1242 | { |
1243 | 1243 | # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES); |
1244 | - $out = random_bytes(24); |
|
1244 | + $out = random_bytes( 24 ); |
|
1245 | 1245 | |
1246 | 1246 | # crypto_core_hchacha20(state->k, out, k, NULL); |
1247 | - $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20($out, $key); |
|
1247 | + $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( $out, $key ); |
|
1248 | 1248 | $state = new ParagonIE_Sodium_Core_SecretStream_State( |
1249 | 1249 | $subkey, |
1250 | - ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4) |
|
1250 | + ParagonIE_Sodium_Core_Util::substr( $out, 16, 8 ) . str_repeat( "\0", 4 ) |
|
1251 | 1251 | ); |
1252 | 1252 | |
1253 | 1253 | # _crypto_secretstream_xchacha20poly1305_counter_reset(state); |
@@ -1268,16 +1268,16 @@ discard block |
||
1268 | 1268 | * @return string Returns a state. |
1269 | 1269 | * @throws Exception |
1270 | 1270 | */ |
1271 | - public static function secretstream_xchacha20poly1305_init_pull($key, $header) |
|
1271 | + public static function secretstream_xchacha20poly1305_init_pull( $key, $header ) |
|
1272 | 1272 | { |
1273 | 1273 | # crypto_core_hchacha20(state->k, in, k, NULL); |
1274 | 1274 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
1275 | - ParagonIE_Sodium_Core_Util::substr($header, 0, 16), |
|
1275 | + ParagonIE_Sodium_Core_Util::substr( $header, 0, 16 ), |
|
1276 | 1276 | $key |
1277 | 1277 | ); |
1278 | 1278 | $state = new ParagonIE_Sodium_Core_SecretStream_State( |
1279 | 1279 | $subkey, |
1280 | - ParagonIE_Sodium_Core_Util::substr($header, 16) |
|
1280 | + ParagonIE_Sodium_Core_Util::substr( $header, 16 ) |
|
1281 | 1281 | ); |
1282 | 1282 | $state->counterReset(); |
1283 | 1283 | # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES, |
@@ -1295,19 +1295,19 @@ discard block |
||
1295 | 1295 | * @return string |
1296 | 1296 | * @throws SodiumException |
1297 | 1297 | */ |
1298 | - public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) |
|
1298 | + public static function secretstream_xchacha20poly1305_push( &$state, $msg, $aad = '', $tag = 0 ) |
|
1299 | 1299 | { |
1300 | - $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
|
1300 | + $st = ParagonIE_Sodium_Core_SecretStream_State::fromString( $state ); |
|
1301 | 1301 | # crypto_onetimeauth_poly1305_state poly1305_state; |
1302 | 1302 | # unsigned char block[64U]; |
1303 | 1303 | # unsigned char slen[8U]; |
1304 | 1304 | # unsigned char *c; |
1305 | 1305 | # unsigned char *mac; |
1306 | 1306 | |
1307 | - $msglen = ParagonIE_Sodium_Core_Util::strlen($msg); |
|
1308 | - $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); |
|
1307 | + $msglen = ParagonIE_Sodium_Core_Util::strlen( $msg ); |
|
1308 | + $aadlen = ParagonIE_Sodium_Core_Util::strlen( $aad ); |
|
1309 | 1309 | |
1310 | - if ((($msglen + 63) >> 6) > 0xfffffffe) { |
|
1310 | + if ( ( ( $msglen + 63 ) >> 6 ) > 0xfffffffe ) { |
|
1311 | 1311 | throw new SodiumException( |
1312 | 1312 | 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' |
1313 | 1313 | ); |
@@ -1324,62 +1324,62 @@ discard block |
||
1324 | 1324 | # crypto_onetimeauth_poly1305_init(&poly1305_state, block); |
1325 | 1325 | # sodium_memzero(block, sizeof block); |
1326 | 1326 | $auth = new ParagonIE_Sodium_Core_Poly1305_State( |
1327 | - ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) |
|
1327 | + ParagonIE_Sodium_Core_ChaCha20::ietfStream( 32, $st->getCombinedNonce(), $st->getKey() ) |
|
1328 | 1328 | ); |
1329 | 1329 | |
1330 | 1330 | # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); |
1331 | - $auth->update($aad); |
|
1331 | + $auth->update( $aad ); |
|
1332 | 1332 | |
1333 | 1333 | # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, |
1334 | 1334 | # (0x10 - adlen) & 0xf); |
1335 | - $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); |
|
1335 | + $auth->update( str_repeat( "\0", ( ( 0x10 - $aadlen ) & 0xf ) ) ); |
|
1336 | 1336 | |
1337 | 1337 | # memset(block, 0, sizeof block); |
1338 | 1338 | # block[0] = tag; |
1339 | 1339 | # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, |
1340 | 1340 | # state->nonce, 1U, state->k); |
1341 | 1341 | $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
1342 | - ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63), |
|
1342 | + ParagonIE_Sodium_Core_Util::intToChr( $tag ) . str_repeat( "\0", 63 ), |
|
1343 | 1343 | $st->getCombinedNonce(), |
1344 | 1344 | $st->getKey(), |
1345 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1345 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
1346 | 1346 | ); |
1347 | 1347 | |
1348 | 1348 | # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); |
1349 | - $auth->update($block); |
|
1349 | + $auth->update( $block ); |
|
1350 | 1350 | |
1351 | 1351 | # out[0] = block[0]; |
1352 | - $out = $block[0]; |
|
1352 | + $out = $block[ 0 ]; |
|
1353 | 1353 | # c = out + (sizeof tag); |
1354 | 1354 | # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k); |
1355 | 1355 | $cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
1356 | 1356 | $msg, |
1357 | 1357 | $st->getCombinedNonce(), |
1358 | 1358 | $st->getKey(), |
1359 | - ParagonIE_Sodium_Core_Util::store64_le(2) |
|
1359 | + ParagonIE_Sodium_Core_Util::store64_le( 2 ) |
|
1360 | 1360 | ); |
1361 | 1361 | |
1362 | 1362 | # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); |
1363 | - $auth->update($cipher); |
|
1363 | + $auth->update( $cipher ); |
|
1364 | 1364 | |
1365 | 1365 | $out .= $cipher; |
1366 | - unset($cipher); |
|
1366 | + unset( $cipher ); |
|
1367 | 1367 | |
1368 | 1368 | # crypto_onetimeauth_poly1305_update |
1369 | 1369 | # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); |
1370 | - $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); |
|
1370 | + $auth->update( str_repeat( "\0", ( ( 0x10 - 64 + $msglen ) & 0xf ) ) ); |
|
1371 | 1371 | |
1372 | 1372 | # STORE64_LE(slen, (uint64_t) adlen); |
1373 | - $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); |
|
1373 | + $slen = ParagonIE_Sodium_Core_Util::store64_le( $aadlen ); |
|
1374 | 1374 | |
1375 | 1375 | # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
1376 | - $auth->update($slen); |
|
1376 | + $auth->update( $slen ); |
|
1377 | 1377 | |
1378 | 1378 | # STORE64_LE(slen, (sizeof block) + mlen); |
1379 | - $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); |
|
1379 | + $slen = ParagonIE_Sodium_Core_Util::store64_le( 64 + $msglen ); |
|
1380 | 1380 | |
1381 | 1381 | # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
1382 | - $auth->update($slen); |
|
1382 | + $auth->update( $slen ); |
|
1383 | 1383 | |
1384 | 1384 | # mac = c + mlen; |
1385 | 1385 | # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); |
@@ -1387,12 +1387,12 @@ discard block |
||
1387 | 1387 | $out .= $mac; |
1388 | 1388 | |
1389 | 1389 | # sodium_memzero(&poly1305_state, sizeof poly1305_state); |
1390 | - unset($auth); |
|
1390 | + unset( $auth ); |
|
1391 | 1391 | |
1392 | 1392 | |
1393 | 1393 | # XOR_BUF(STATE_INONCE(state), mac, |
1394 | 1394 | # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
1395 | - $st->xorNonce($mac); |
|
1395 | + $st->xorNonce( $mac ); |
|
1396 | 1396 | |
1397 | 1397 | # sodium_increment(STATE_COUNTER(state), |
1398 | 1398 | # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); |
@@ -1401,15 +1401,15 @@ discard block |
||
1401 | 1401 | $state = $st->toString(); |
1402 | 1402 | |
1403 | 1403 | /** @var bool $rekey */ |
1404 | - $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; |
|
1404 | + $rekey = ( $tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY ) !== 0; |
|
1405 | 1405 | # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || |
1406 | 1406 | # sodium_is_zero(STATE_COUNTER(state), |
1407 | 1407 | # crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) { |
1408 | 1408 | # crypto_secretstream_xchacha20poly1305_rekey(state); |
1409 | 1409 | # } |
1410 | - if ($rekey || $st->needsRekey()) { |
|
1410 | + if ( $rekey || $st->needsRekey() ) { |
|
1411 | 1411 | // DO REKEY |
1412 | - self::secretstream_xchacha20poly1305_rekey($state); |
|
1412 | + self::secretstream_xchacha20poly1305_rekey( $state ); |
|
1413 | 1413 | } |
1414 | 1414 | # if (outlen_p != NULL) { |
1415 | 1415 | # *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen; |
@@ -1424,19 +1424,19 @@ discard block |
||
1424 | 1424 | * @return bool|array{0: string, 1: int} |
1425 | 1425 | * @throws SodiumException |
1426 | 1426 | */ |
1427 | - public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') |
|
1427 | + public static function secretstream_xchacha20poly1305_pull( &$state, $cipher, $aad = '' ) |
|
1428 | 1428 | { |
1429 | - $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
|
1429 | + $st = ParagonIE_Sodium_Core_SecretStream_State::fromString( $state ); |
|
1430 | 1430 | |
1431 | - $cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher); |
|
1431 | + $cipherlen = ParagonIE_Sodium_Core_Util::strlen( $cipher ); |
|
1432 | 1432 | # mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES; |
1433 | 1433 | $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES; |
1434 | - $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); |
|
1434 | + $aadlen = ParagonIE_Sodium_Core_Util::strlen( $aad ); |
|
1435 | 1435 | |
1436 | 1436 | # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) { |
1437 | 1437 | # sodium_misuse(); |
1438 | 1438 | # } |
1439 | - if ((($msglen + 63) >> 6) > 0xfffffffe) { |
|
1439 | + if ( ( ( $msglen + 63 ) >> 6 ) > 0xfffffffe ) { |
|
1440 | 1440 | throw new SodiumException( |
1441 | 1441 | 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' |
1442 | 1442 | ); |
@@ -1446,15 +1446,15 @@ discard block |
||
1446 | 1446 | # crypto_onetimeauth_poly1305_init(&poly1305_state, block); |
1447 | 1447 | # sodium_memzero(block, sizeof block); |
1448 | 1448 | $auth = new ParagonIE_Sodium_Core_Poly1305_State( |
1449 | - ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) |
|
1449 | + ParagonIE_Sodium_Core_ChaCha20::ietfStream( 32, $st->getCombinedNonce(), $st->getKey() ) |
|
1450 | 1450 | ); |
1451 | 1451 | |
1452 | 1452 | # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); |
1453 | - $auth->update($aad); |
|
1453 | + $auth->update( $aad ); |
|
1454 | 1454 | |
1455 | 1455 | # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, |
1456 | 1456 | # (0x10 - adlen) & 0xf); |
1457 | - $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); |
|
1457 | + $auth->update( str_repeat( "\0", ( ( 0x10 - $aadlen ) & 0xf ) ) ); |
|
1458 | 1458 | |
1459 | 1459 | |
1460 | 1460 | # memset(block, 0, sizeof block); |
@@ -1462,36 +1462,36 @@ discard block |
||
1462 | 1462 | # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, |
1463 | 1463 | # state->nonce, 1U, state->k); |
1464 | 1464 | $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
1465 | - $cipher[0] . str_repeat("\0", 63), |
|
1465 | + $cipher[ 0 ] . str_repeat( "\0", 63 ), |
|
1466 | 1466 | $st->getCombinedNonce(), |
1467 | 1467 | $st->getKey(), |
1468 | - ParagonIE_Sodium_Core_Util::store64_le(1) |
|
1468 | + ParagonIE_Sodium_Core_Util::store64_le( 1 ) |
|
1469 | 1469 | ); |
1470 | 1470 | # tag = block[0]; |
1471 | 1471 | # block[0] = in[0]; |
1472 | 1472 | # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); |
1473 | - $tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]); |
|
1474 | - $block[0] = $cipher[0]; |
|
1475 | - $auth->update($block); |
|
1473 | + $tag = ParagonIE_Sodium_Core_Util::chrToInt( $block[ 0 ] ); |
|
1474 | + $block[ 0 ] = $cipher[ 0 ]; |
|
1475 | + $auth->update( $block ); |
|
1476 | 1476 | |
1477 | 1477 | |
1478 | 1478 | # c = in + (sizeof tag); |
1479 | 1479 | # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); |
1480 | - $auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen)); |
|
1480 | + $auth->update( ParagonIE_Sodium_Core_Util::substr( $cipher, 1, $msglen ) ); |
|
1481 | 1481 | |
1482 | 1482 | # crypto_onetimeauth_poly1305_update |
1483 | 1483 | # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); |
1484 | - $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); |
|
1484 | + $auth->update( str_repeat( "\0", ( ( 0x10 - 64 + $msglen ) & 0xf ) ) ); |
|
1485 | 1485 | |
1486 | 1486 | # STORE64_LE(slen, (uint64_t) adlen); |
1487 | 1487 | # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
1488 | - $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); |
|
1489 | - $auth->update($slen); |
|
1488 | + $slen = ParagonIE_Sodium_Core_Util::store64_le( $aadlen ); |
|
1489 | + $auth->update( $slen ); |
|
1490 | 1490 | |
1491 | 1491 | # STORE64_LE(slen, (sizeof block) + mlen); |
1492 | 1492 | # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); |
1493 | - $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); |
|
1494 | - $auth->update($slen); |
|
1493 | + $slen = ParagonIE_Sodium_Core_Util::store64_le( 64 + $msglen ); |
|
1494 | + $auth->update( $slen ); |
|
1495 | 1495 | |
1496 | 1496 | # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); |
1497 | 1497 | # sodium_memzero(&poly1305_state, sizeof poly1305_state); |
@@ -1503,22 +1503,22 @@ discard block |
||
1503 | 1503 | # return -1; |
1504 | 1504 | # } |
1505 | 1505 | |
1506 | - $stored = ParagonIE_Sodium_Core_Util::substr($cipher, $msglen + 1, 16); |
|
1507 | - if (!ParagonIE_Sodium_Core_Util::hashEquals($mac, $stored)) { |
|
1506 | + $stored = ParagonIE_Sodium_Core_Util::substr( $cipher, $msglen + 1, 16 ); |
|
1507 | + if ( ! ParagonIE_Sodium_Core_Util::hashEquals( $mac, $stored ) ) { |
|
1508 | 1508 | return false; |
1509 | 1509 | } |
1510 | 1510 | |
1511 | 1511 | # crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k); |
1512 | 1512 | $out = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
1513 | - ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen), |
|
1513 | + ParagonIE_Sodium_Core_Util::substr( $cipher, 1, $msglen ), |
|
1514 | 1514 | $st->getCombinedNonce(), |
1515 | 1515 | $st->getKey(), |
1516 | - ParagonIE_Sodium_Core_Util::store64_le(2) |
|
1516 | + ParagonIE_Sodium_Core_Util::store64_le( 2 ) |
|
1517 | 1517 | ); |
1518 | 1518 | |
1519 | 1519 | # XOR_BUF(STATE_INONCE(state), mac, |
1520 | 1520 | # crypto_secretstream_xchacha20poly1305_INONCEBYTES); |
1521 | - $st->xorNonce($mac); |
|
1521 | + $st->xorNonce( $mac ); |
|
1522 | 1522 | |
1523 | 1523 | # sodium_increment(STATE_COUNTER(state), |
1524 | 1524 | # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); |
@@ -1534,12 +1534,12 @@ discard block |
||
1534 | 1534 | $state = $st->toString(); |
1535 | 1535 | |
1536 | 1536 | /** @var bool $rekey */ |
1537 | - $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; |
|
1538 | - if ($rekey || $st->needsRekey()) { |
|
1537 | + $rekey = ( $tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY ) !== 0; |
|
1538 | + if ( $rekey || $st->needsRekey() ) { |
|
1539 | 1539 | // DO REKEY |
1540 | - self::secretstream_xchacha20poly1305_rekey($state); |
|
1540 | + self::secretstream_xchacha20poly1305_rekey( $state ); |
|
1541 | 1541 | } |
1542 | - return array($out, $tag); |
|
1542 | + return array( $out, $tag ); |
|
1543 | 1543 | } |
1544 | 1544 | |
1545 | 1545 | /** |
@@ -1547,9 +1547,9 @@ discard block |
||
1547 | 1547 | * @return void |
1548 | 1548 | * @throws SodiumException |
1549 | 1549 | */ |
1550 | - public static function secretstream_xchacha20poly1305_rekey(&$state) |
|
1550 | + public static function secretstream_xchacha20poly1305_rekey( &$state ) |
|
1551 | 1551 | { |
1552 | - $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
|
1552 | + $st = ParagonIE_Sodium_Core_SecretStream_State::fromString( $state ); |
|
1553 | 1553 | # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + |
1554 | 1554 | # crypto_secretstream_xchacha20poly1305_INONCEBYTES]; |
1555 | 1555 | # size_t i; |
@@ -1562,18 +1562,18 @@ discard block |
||
1562 | 1562 | # new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] = |
1563 | 1563 | # STATE_INONCE(state)[i]; |
1564 | 1564 | # } |
1565 | - $new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR($st->getNonce(), 0, 8); |
|
1565 | + $new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR( $st->getNonce(), 0, 8 ); |
|
1566 | 1566 | |
1567 | 1567 | # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce, |
1568 | 1568 | # sizeof new_key_and_inonce, |
1569 | 1569 | # state->nonce, state->k); |
1570 | 1570 | |
1571 | - $st->rekey(ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1571 | + $st->rekey( ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
|
1572 | 1572 | $new_key_and_inonce, |
1573 | 1573 | $st->getCombinedNonce(), |
1574 | 1574 | $st->getKey(), |
1575 | - ParagonIE_Sodium_Core_Util::store64_le(0) |
|
1576 | - )); |
|
1575 | + ParagonIE_Sodium_Core_Util::store64_le( 0 ) |
|
1576 | + ) ); |
|
1577 | 1577 | |
1578 | 1578 | # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { |
1579 | 1579 | # state->k[i] = new_key_and_inonce[i]; |
@@ -1599,9 +1599,9 @@ discard block |
||
1599 | 1599 | * @throws SodiumException |
1600 | 1600 | * @throws TypeError |
1601 | 1601 | */ |
1602 | - public static function sign_detached($message, $sk) |
|
1602 | + public static function sign_detached( $message, $sk ) |
|
1603 | 1603 | { |
1604 | - return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); |
|
1604 | + return ParagonIE_Sodium_Core_Ed25519::sign_detached( $message, $sk ); |
|
1605 | 1605 | } |
1606 | 1606 | |
1607 | 1607 | /** |
@@ -1615,9 +1615,9 @@ discard block |
||
1615 | 1615 | * @throws SodiumException |
1616 | 1616 | * @throws TypeError |
1617 | 1617 | */ |
1618 | - public static function sign($message, $sk) |
|
1618 | + public static function sign( $message, $sk ) |
|
1619 | 1619 | { |
1620 | - return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); |
|
1620 | + return ParagonIE_Sodium_Core_Ed25519::sign( $message, $sk ); |
|
1621 | 1621 | } |
1622 | 1622 | |
1623 | 1623 | /** |
@@ -1631,9 +1631,9 @@ discard block |
||
1631 | 1631 | * @throws SodiumException |
1632 | 1632 | * @throws TypeError |
1633 | 1633 | */ |
1634 | - public static function sign_open($signedMessage, $pk) |
|
1634 | + public static function sign_open( $signedMessage, $pk ) |
|
1635 | 1635 | { |
1636 | - return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); |
|
1636 | + return ParagonIE_Sodium_Core_Ed25519::sign_open( $signedMessage, $pk ); |
|
1637 | 1637 | } |
1638 | 1638 | |
1639 | 1639 | /** |
@@ -1648,8 +1648,8 @@ discard block |
||
1648 | 1648 | * @throws SodiumException |
1649 | 1649 | * @throws TypeError |
1650 | 1650 | */ |
1651 | - public static function sign_verify_detached($signature, $message, $pk) |
|
1651 | + public static function sign_verify_detached( $signature, $message, $pk ) |
|
1652 | 1652 | { |
1653 | - return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); |
|
1653 | + return ParagonIE_Sodium_Core_Ed25519::verify_detached( $signature, $message, $pk ); |
|
1654 | 1654 | } |
1655 | 1655 | } |
@@ -12,8 +12,7 @@ discard block |
||
12 | 12 | * If you are using this library, you should be using |
13 | 13 | * ParagonIE_Sodium_Compat in your code, not this class. |
14 | 14 | */ |
15 | -abstract class ParagonIE_Sodium_Crypto |
|
16 | -{ |
|
15 | +abstract class ParagonIE_Sodium_Crypto { |
|
17 | 16 | const aead_chacha20poly1305_KEYBYTES = 32; |
18 | 17 | const aead_chacha20poly1305_NSECBYTES = 0; |
19 | 18 | const aead_chacha20poly1305_NPUBBYTES = 8; |
@@ -381,8 +380,7 @@ discard block |
||
381 | 380 | * @return string |
382 | 381 | * @throws TypeError |
383 | 382 | */ |
384 | - public static function auth($message, $key) |
|
385 | - { |
|
383 | + public static function auth($message, $key) { |
|
386 | 384 | return ParagonIE_Sodium_Core_Util::substr( |
387 | 385 | hash_hmac('sha512', $message, $key, true), |
388 | 386 | 0, |
@@ -402,8 +400,7 @@ discard block |
||
402 | 400 | * @throws SodiumException |
403 | 401 | * @throws TypeError |
404 | 402 | */ |
405 | - public static function auth_verify($mac, $message, $key) |
|
406 | - { |
|
403 | + public static function auth_verify($mac, $message, $key) { |
|
407 | 404 | return ParagonIE_Sodium_Core_Util::hashEquals( |
408 | 405 | $mac, |
409 | 406 | self::auth($message, $key) |
@@ -422,8 +419,7 @@ discard block |
||
422 | 419 | * @throws SodiumException |
423 | 420 | * @throws TypeError |
424 | 421 | */ |
425 | - public static function box($plaintext, $nonce, $keypair) |
|
426 | - { |
|
422 | + public static function box($plaintext, $nonce, $keypair) { |
|
427 | 423 | $c = self::secretbox( |
428 | 424 | $plaintext, |
429 | 425 | $nonce, |
@@ -446,8 +442,7 @@ discard block |
||
446 | 442 | * @throws SodiumException |
447 | 443 | * @throws TypeError |
448 | 444 | */ |
449 | - public static function box_seal($message, $publicKey) |
|
450 | - { |
|
445 | + public static function box_seal($message, $publicKey) { |
|
451 | 446 | /** @var string $ephemeralKeypair */ |
452 | 447 | $ephemeralKeypair = self::box_keypair(); |
453 | 448 | |
@@ -492,8 +487,7 @@ discard block |
||
492 | 487 | * @throws SodiumException |
493 | 488 | * @throws TypeError |
494 | 489 | */ |
495 | - public static function box_seal_open($message, $keypair) |
|
496 | - { |
|
490 | + public static function box_seal_open($message, $keypair) { |
|
497 | 491 | /** @var string $ephemeralPK */ |
498 | 492 | $ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32); |
499 | 493 | |
@@ -541,8 +535,7 @@ discard block |
||
541 | 535 | * @throws SodiumException |
542 | 536 | * @throws TypeError |
543 | 537 | */ |
544 | - public static function box_beforenm($sk, $pk) |
|
545 | - { |
|
538 | + public static function box_beforenm($sk, $pk) { |
|
546 | 539 | return ParagonIE_Sodium_Core_HSalsa20::hsalsa20( |
547 | 540 | str_repeat("\x00", 16), |
548 | 541 | self::scalarmult($sk, $pk) |
@@ -557,8 +550,7 @@ discard block |
||
557 | 550 | * @throws SodiumException |
558 | 551 | * @throws TypeError |
559 | 552 | */ |
560 | - public static function box_keypair() |
|
561 | - { |
|
553 | + public static function box_keypair() { |
|
562 | 554 | $sKey = random_bytes(32); |
563 | 555 | $pKey = self::scalarmult_base($sKey); |
564 | 556 | return $sKey . $pKey; |
@@ -570,8 +562,7 @@ discard block |
||
570 | 562 | * @throws SodiumException |
571 | 563 | * @throws TypeError |
572 | 564 | */ |
573 | - public static function box_seed_keypair($seed) |
|
574 | - { |
|
565 | + public static function box_seed_keypair($seed) { |
|
575 | 566 | $sKey = ParagonIE_Sodium_Core_Util::substr( |
576 | 567 | hash('sha512', $seed, true), |
577 | 568 | 0, |
@@ -589,8 +580,7 @@ discard block |
||
589 | 580 | * @return string |
590 | 581 | * @throws TypeError |
591 | 582 | */ |
592 | - public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) |
|
593 | - { |
|
583 | + public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) { |
|
594 | 584 | return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . |
595 | 585 | ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32); |
596 | 586 | } |
@@ -603,8 +593,7 @@ discard block |
||
603 | 593 | * @throws RangeException |
604 | 594 | * @throws TypeError |
605 | 595 | */ |
606 | - public static function box_secretkey($keypair) |
|
607 | - { |
|
596 | + public static function box_secretkey($keypair) { |
|
608 | 597 | if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { |
609 | 598 | throw new RangeException( |
610 | 599 | 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
@@ -621,8 +610,7 @@ discard block |
||
621 | 610 | * @throws RangeException |
622 | 611 | * @throws TypeError |
623 | 612 | */ |
624 | - public static function box_publickey($keypair) |
|
625 | - { |
|
613 | + public static function box_publickey($keypair) { |
|
626 | 614 | if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
627 | 615 | throw new RangeException( |
628 | 616 | 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
@@ -640,8 +628,7 @@ discard block |
||
640 | 628 | * @throws SodiumException |
641 | 629 | * @throws TypeError |
642 | 630 | */ |
643 | - public static function box_publickey_from_secretkey($sKey) |
|
644 | - { |
|
631 | + public static function box_publickey_from_secretkey($sKey) { |
|
645 | 632 | if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { |
646 | 633 | throw new RangeException( |
647 | 634 | 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.' |
@@ -662,8 +649,7 @@ discard block |
||
662 | 649 | * @throws SodiumException |
663 | 650 | * @throws TypeError |
664 | 651 | */ |
665 | - public static function box_open($ciphertext, $nonce, $keypair) |
|
666 | - { |
|
652 | + public static function box_open($ciphertext, $nonce, $keypair) { |
|
667 | 653 | return self::secretbox_open( |
668 | 654 | $ciphertext, |
669 | 655 | $nonce, |
@@ -687,8 +673,7 @@ discard block |
||
687 | 673 | * @throws SodiumException |
688 | 674 | * @throws TypeError |
689 | 675 | */ |
690 | - public static function generichash($message, $key = '', $outlen = 32) |
|
691 | - { |
|
676 | + public static function generichash($message, $key = '', $outlen = 32) { |
|
692 | 677 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
693 | 678 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
694 | 679 | |
@@ -728,8 +713,7 @@ discard block |
||
728 | 713 | * @throws SodiumException |
729 | 714 | * @throws TypeError |
730 | 715 | */ |
731 | - public static function generichash_final($ctx, $outlen = 32) |
|
732 | - { |
|
716 | + public static function generichash_final($ctx, $outlen = 32) { |
|
733 | 717 | if (!is_string($ctx)) { |
734 | 718 | throw new TypeError('Context must be a string'); |
735 | 719 | } |
@@ -758,8 +742,7 @@ discard block |
||
758 | 742 | * @throws SodiumException |
759 | 743 | * @throws TypeError |
760 | 744 | */ |
761 | - public static function generichash_init($key = '', $outputLength = 32) |
|
762 | - { |
|
745 | + public static function generichash_init($key = '', $outputLength = 32) { |
|
763 | 746 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
764 | 747 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
765 | 748 | |
@@ -835,8 +818,7 @@ discard block |
||
835 | 818 | * @throws SodiumException |
836 | 819 | * @throws TypeError |
837 | 820 | */ |
838 | - public static function generichash_update($ctx, $message) |
|
839 | - { |
|
821 | + public static function generichash_update($ctx, $message) { |
|
840 | 822 | // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized |
841 | 823 | ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
842 | 824 | |
@@ -864,8 +846,7 @@ discard block |
||
864 | 846 | * @throws SodiumException |
865 | 847 | * @throws TypeError |
866 | 848 | */ |
867 | - public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) |
|
868 | - { |
|
849 | + public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) { |
|
869 | 850 | return ParagonIE_Sodium_Compat::crypto_generichash( |
870 | 851 | ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) . |
871 | 852 | $client_pk . |
@@ -885,8 +866,7 @@ discard block |
||
885 | 866 | * @throws SodiumException |
886 | 867 | * @throws TypeError |
887 | 868 | */ |
888 | - public static function scalarmult($sKey, $pKey) |
|
889 | - { |
|
869 | + public static function scalarmult($sKey, $pKey) { |
|
890 | 870 | $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); |
891 | 871 | self::scalarmult_throw_if_zero($q); |
892 | 872 | return $q; |
@@ -902,8 +882,7 @@ discard block |
||
902 | 882 | * @throws SodiumException |
903 | 883 | * @throws TypeError |
904 | 884 | */ |
905 | - public static function scalarmult_base($secret) |
|
906 | - { |
|
885 | + public static function scalarmult_base($secret) { |
|
907 | 886 | $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); |
908 | 887 | self::scalarmult_throw_if_zero($q); |
909 | 888 | return $q; |
@@ -917,8 +896,7 @@ discard block |
||
917 | 896 | * @throws SodiumException |
918 | 897 | * @throws TypeError |
919 | 898 | */ |
920 | - protected static function scalarmult_throw_if_zero($q) |
|
921 | - { |
|
899 | + protected static function scalarmult_throw_if_zero($q) { |
|
922 | 900 | $d = 0; |
923 | 901 | for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { |
924 | 902 | $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); |
@@ -942,8 +920,7 @@ discard block |
||
942 | 920 | * @throws SodiumException |
943 | 921 | * @throws TypeError |
944 | 922 | */ |
945 | - public static function secretbox($plaintext, $nonce, $key) |
|
946 | - { |
|
923 | + public static function secretbox($plaintext, $nonce, $key) { |
|
947 | 924 | /** @var string $subkey */ |
948 | 925 | $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
949 | 926 | |
@@ -1017,8 +994,7 @@ discard block |
||
1017 | 994 | * @throws SodiumException |
1018 | 995 | * @throws TypeError |
1019 | 996 | */ |
1020 | - public static function secretbox_open($ciphertext, $nonce, $key) |
|
1021 | - { |
|
997 | + public static function secretbox_open($ciphertext, $nonce, $key) { |
|
1022 | 998 | /** @var string $mac */ |
1023 | 999 | $mac = ParagonIE_Sodium_Core_Util::substr( |
1024 | 1000 | $ciphertext, |
@@ -1090,8 +1066,7 @@ discard block |
||
1090 | 1066 | * @throws SodiumException |
1091 | 1067 | * @throws TypeError |
1092 | 1068 | */ |
1093 | - public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) |
|
1094 | - { |
|
1069 | + public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) { |
|
1095 | 1070 | /** @var string $subkey */ |
1096 | 1071 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
1097 | 1072 | ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
@@ -1169,8 +1144,7 @@ discard block |
||
1169 | 1144 | * @throws SodiumException |
1170 | 1145 | * @throws TypeError |
1171 | 1146 | */ |
1172 | - public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
|
1173 | - { |
|
1147 | + public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) { |
|
1174 | 1148 | /** @var string $mac */ |
1175 | 1149 | $mac = ParagonIE_Sodium_Core_Util::substr( |
1176 | 1150 | $ciphertext, |
@@ -1238,8 +1212,7 @@ discard block |
||
1238 | 1212 | * @throws Exception |
1239 | 1213 | * @throws SodiumException |
1240 | 1214 | */ |
1241 | - public static function secretstream_xchacha20poly1305_init_push($key) |
|
1242 | - { |
|
1215 | + public static function secretstream_xchacha20poly1305_init_push($key) { |
|
1243 | 1216 | # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES); |
1244 | 1217 | $out = random_bytes(24); |
1245 | 1218 | |
@@ -1268,8 +1241,7 @@ discard block |
||
1268 | 1241 | * @return string Returns a state. |
1269 | 1242 | * @throws Exception |
1270 | 1243 | */ |
1271 | - public static function secretstream_xchacha20poly1305_init_pull($key, $header) |
|
1272 | - { |
|
1244 | + public static function secretstream_xchacha20poly1305_init_pull($key, $header) { |
|
1273 | 1245 | # crypto_core_hchacha20(state->k, in, k, NULL); |
1274 | 1246 | $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
1275 | 1247 | ParagonIE_Sodium_Core_Util::substr($header, 0, 16), |
@@ -1295,8 +1267,7 @@ discard block |
||
1295 | 1267 | * @return string |
1296 | 1268 | * @throws SodiumException |
1297 | 1269 | */ |
1298 | - public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) |
|
1299 | - { |
|
1270 | + public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) { |
|
1300 | 1271 | $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
1301 | 1272 | # crypto_onetimeauth_poly1305_state poly1305_state; |
1302 | 1273 | # unsigned char block[64U]; |
@@ -1424,8 +1395,7 @@ discard block |
||
1424 | 1395 | * @return bool|array{0: string, 1: int} |
1425 | 1396 | * @throws SodiumException |
1426 | 1397 | */ |
1427 | - public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') |
|
1428 | - { |
|
1398 | + public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') { |
|
1429 | 1399 | $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
1430 | 1400 | |
1431 | 1401 | $cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher); |
@@ -1547,8 +1517,7 @@ discard block |
||
1547 | 1517 | * @return void |
1548 | 1518 | * @throws SodiumException |
1549 | 1519 | */ |
1550 | - public static function secretstream_xchacha20poly1305_rekey(&$state) |
|
1551 | - { |
|
1520 | + public static function secretstream_xchacha20poly1305_rekey(&$state) { |
|
1552 | 1521 | $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
1553 | 1522 | # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + |
1554 | 1523 | # crypto_secretstream_xchacha20poly1305_INONCEBYTES]; |
@@ -1599,8 +1568,7 @@ discard block |
||
1599 | 1568 | * @throws SodiumException |
1600 | 1569 | * @throws TypeError |
1601 | 1570 | */ |
1602 | - public static function sign_detached($message, $sk) |
|
1603 | - { |
|
1571 | + public static function sign_detached($message, $sk) { |
|
1604 | 1572 | return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); |
1605 | 1573 | } |
1606 | 1574 | |
@@ -1615,8 +1583,7 @@ discard block |
||
1615 | 1583 | * @throws SodiumException |
1616 | 1584 | * @throws TypeError |
1617 | 1585 | */ |
1618 | - public static function sign($message, $sk) |
|
1619 | - { |
|
1586 | + public static function sign($message, $sk) { |
|
1620 | 1587 | return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); |
1621 | 1588 | } |
1622 | 1589 | |
@@ -1631,8 +1598,7 @@ discard block |
||
1631 | 1598 | * @throws SodiumException |
1632 | 1599 | * @throws TypeError |
1633 | 1600 | */ |
1634 | - public static function sign_open($signedMessage, $pk) |
|
1635 | - { |
|
1601 | + public static function sign_open($signedMessage, $pk) { |
|
1636 | 1602 | return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); |
1637 | 1603 | } |
1638 | 1604 | |
@@ -1648,8 +1614,7 @@ discard block |
||
1648 | 1614 | * @throws SodiumException |
1649 | 1615 | * @throws TypeError |
1650 | 1616 | */ |
1651 | - public static function sign_verify_detached($signature, $message, $pk) |
|
1652 | - { |
|
1617 | + public static function sign_verify_detached($signature, $message, $pk) { |
|
1653 | 1618 | return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); |
1654 | 1619 | } |
1655 | 1620 | } |
@@ -1,1560 +1,1560 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_File', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | /** |
7 | 7 | * Class ParagonIE_Sodium_File |
8 | 8 | */ |
9 | 9 | class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util |
10 | 10 | { |
11 | - /* PHP's default buffer size is 8192 for fread()/fwrite(). */ |
|
12 | - const BUFFER_SIZE = 8192; |
|
13 | - |
|
14 | - /** |
|
15 | - * Box a file (rather than a string). Uses less memory than |
|
16 | - * ParagonIE_Sodium_Compat::crypto_box(), but produces |
|
17 | - * the same result. |
|
18 | - * |
|
19 | - * @param string $inputFile Absolute path to a file on the filesystem |
|
20 | - * @param string $outputFile Absolute path to a file on the filesystem |
|
21 | - * @param string $nonce Number to be used only once |
|
22 | - * @param string $keyPair ECDH secret key and ECDH public key concatenated |
|
23 | - * |
|
24 | - * @return bool |
|
25 | - * @throws SodiumException |
|
26 | - * @throws TypeError |
|
27 | - */ |
|
28 | - public static function box($inputFile, $outputFile, $nonce, $keyPair) |
|
29 | - { |
|
30 | - /* Type checks: */ |
|
31 | - if (!is_string($inputFile)) { |
|
32 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
33 | - } |
|
34 | - if (!is_string($outputFile)) { |
|
35 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
36 | - } |
|
37 | - if (!is_string($nonce)) { |
|
38 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
39 | - } |
|
40 | - |
|
41 | - /* Input validation: */ |
|
42 | - if (!is_string($keyPair)) { |
|
43 | - throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.'); |
|
44 | - } |
|
45 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
46 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
47 | - } |
|
48 | - if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
49 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
50 | - } |
|
51 | - |
|
52 | - /** @var int $size */ |
|
53 | - $size = filesize($inputFile); |
|
54 | - if (!is_int($size)) { |
|
55 | - throw new SodiumException('Could not obtain the file size'); |
|
56 | - } |
|
57 | - |
|
58 | - /** @var resource $ifp */ |
|
59 | - $ifp = fopen($inputFile, 'rb'); |
|
60 | - if (!is_resource($ifp)) { |
|
61 | - throw new SodiumException('Could not open input file for reading'); |
|
62 | - } |
|
63 | - |
|
64 | - /** @var resource $ofp */ |
|
65 | - $ofp = fopen($outputFile, 'wb'); |
|
66 | - if (!is_resource($ofp)) { |
|
67 | - fclose($ifp); |
|
68 | - throw new SodiumException('Could not open output file for writing'); |
|
69 | - } |
|
70 | - |
|
71 | - $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair); |
|
72 | - fclose($ifp); |
|
73 | - fclose($ofp); |
|
74 | - return $res; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Open a boxed file (rather than a string). Uses less memory than |
|
79 | - * ParagonIE_Sodium_Compat::crypto_box_open(), but produces |
|
80 | - * the same result. |
|
81 | - * |
|
82 | - * Warning: Does not protect against TOCTOU attacks. You should |
|
83 | - * just load the file into memory and use crypto_box_open() if |
|
84 | - * you are worried about those. |
|
85 | - * |
|
86 | - * @param string $inputFile |
|
87 | - * @param string $outputFile |
|
88 | - * @param string $nonce |
|
89 | - * @param string $keypair |
|
90 | - * @return bool |
|
91 | - * @throws SodiumException |
|
92 | - * @throws TypeError |
|
93 | - */ |
|
94 | - public static function box_open($inputFile, $outputFile, $nonce, $keypair) |
|
95 | - { |
|
96 | - /* Type checks: */ |
|
97 | - if (!is_string($inputFile)) { |
|
98 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
99 | - } |
|
100 | - if (!is_string($outputFile)) { |
|
101 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
102 | - } |
|
103 | - if (!is_string($nonce)) { |
|
104 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
105 | - } |
|
106 | - if (!is_string($keypair)) { |
|
107 | - throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.'); |
|
108 | - } |
|
109 | - |
|
110 | - /* Input validation: */ |
|
111 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
112 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
113 | - } |
|
114 | - if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
115 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
116 | - } |
|
117 | - |
|
118 | - /** @var int $size */ |
|
119 | - $size = filesize($inputFile); |
|
120 | - if (!is_int($size)) { |
|
121 | - throw new SodiumException('Could not obtain the file size'); |
|
122 | - } |
|
123 | - |
|
124 | - /** @var resource $ifp */ |
|
125 | - $ifp = fopen($inputFile, 'rb'); |
|
126 | - if (!is_resource($ifp)) { |
|
127 | - throw new SodiumException('Could not open input file for reading'); |
|
128 | - } |
|
129 | - |
|
130 | - /** @var resource $ofp */ |
|
131 | - $ofp = fopen($outputFile, 'wb'); |
|
132 | - if (!is_resource($ofp)) { |
|
133 | - fclose($ifp); |
|
134 | - throw new SodiumException('Could not open output file for writing'); |
|
135 | - } |
|
136 | - |
|
137 | - $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair); |
|
138 | - fclose($ifp); |
|
139 | - fclose($ofp); |
|
140 | - try { |
|
141 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
142 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
143 | - } catch (SodiumException $ex) { |
|
144 | - if (isset($ephKeypair)) { |
|
145 | - unset($ephKeypair); |
|
146 | - } |
|
147 | - } |
|
148 | - return $res; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Seal a file (rather than a string). Uses less memory than |
|
153 | - * ParagonIE_Sodium_Compat::crypto_box_seal(), but produces |
|
154 | - * the same result. |
|
155 | - * |
|
156 | - * @param string $inputFile Absolute path to a file on the filesystem |
|
157 | - * @param string $outputFile Absolute path to a file on the filesystem |
|
158 | - * @param string $publicKey ECDH public key |
|
159 | - * |
|
160 | - * @return bool |
|
161 | - * @throws SodiumException |
|
162 | - * @throws TypeError |
|
163 | - */ |
|
164 | - public static function box_seal($inputFile, $outputFile, $publicKey) |
|
165 | - { |
|
166 | - /* Type checks: */ |
|
167 | - if (!is_string($inputFile)) { |
|
168 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
169 | - } |
|
170 | - if (!is_string($outputFile)) { |
|
171 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
172 | - } |
|
173 | - if (!is_string($publicKey)) { |
|
174 | - throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
175 | - } |
|
176 | - |
|
177 | - /* Input validation: */ |
|
178 | - if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
179 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes'); |
|
180 | - } |
|
181 | - |
|
182 | - /** @var int $size */ |
|
183 | - $size = filesize($inputFile); |
|
184 | - if (!is_int($size)) { |
|
185 | - throw new SodiumException('Could not obtain the file size'); |
|
186 | - } |
|
187 | - |
|
188 | - /** @var resource $ifp */ |
|
189 | - $ifp = fopen($inputFile, 'rb'); |
|
190 | - if (!is_resource($ifp)) { |
|
191 | - throw new SodiumException('Could not open input file for reading'); |
|
192 | - } |
|
193 | - |
|
194 | - /** @var resource $ofp */ |
|
195 | - $ofp = fopen($outputFile, 'wb'); |
|
196 | - if (!is_resource($ofp)) { |
|
197 | - fclose($ifp); |
|
198 | - throw new SodiumException('Could not open output file for writing'); |
|
199 | - } |
|
200 | - |
|
201 | - /** @var string $ephKeypair */ |
|
202 | - $ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair(); |
|
203 | - |
|
204 | - /** @var string $msgKeypair */ |
|
205 | - $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
|
206 | - ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair), |
|
207 | - $publicKey |
|
208 | - ); |
|
209 | - |
|
210 | - /** @var string $ephemeralPK */ |
|
211 | - $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair); |
|
212 | - |
|
213 | - /** @var string $nonce */ |
|
214 | - $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
|
215 | - $ephemeralPK . $publicKey, |
|
216 | - '', |
|
217 | - 24 |
|
218 | - ); |
|
219 | - |
|
220 | - /** @var int $firstWrite */ |
|
221 | - $firstWrite = fwrite( |
|
222 | - $ofp, |
|
223 | - $ephemeralPK, |
|
224 | - ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES |
|
225 | - ); |
|
226 | - if (!is_int($firstWrite)) { |
|
227 | - fclose($ifp); |
|
228 | - fclose($ofp); |
|
229 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
230 | - throw new SodiumException('Could not write to output file'); |
|
231 | - } |
|
232 | - if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
233 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
234 | - fclose($ifp); |
|
235 | - fclose($ofp); |
|
236 | - throw new SodiumException('Error writing public key to output file'); |
|
237 | - } |
|
238 | - |
|
239 | - $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
240 | - fclose($ifp); |
|
241 | - fclose($ofp); |
|
242 | - try { |
|
243 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
244 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
245 | - } catch (SodiumException $ex) { |
|
246 | - /** @psalm-suppress PossiblyUndefinedVariable */ |
|
247 | - unset($ephKeypair); |
|
248 | - } |
|
249 | - return $res; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Open a sealed file (rather than a string). Uses less memory than |
|
254 | - * ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces |
|
255 | - * the same result. |
|
256 | - * |
|
257 | - * Warning: Does not protect against TOCTOU attacks. You should |
|
258 | - * just load the file into memory and use crypto_box_seal_open() if |
|
259 | - * you are worried about those. |
|
260 | - * |
|
261 | - * @param string $inputFile |
|
262 | - * @param string $outputFile |
|
263 | - * @param string $ecdhKeypair |
|
264 | - * @return bool |
|
265 | - * @throws SodiumException |
|
266 | - * @throws TypeError |
|
267 | - */ |
|
268 | - public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) |
|
269 | - { |
|
270 | - /* Type checks: */ |
|
271 | - if (!is_string($inputFile)) { |
|
272 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
273 | - } |
|
274 | - if (!is_string($outputFile)) { |
|
275 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
276 | - } |
|
277 | - if (!is_string($ecdhKeypair)) { |
|
278 | - throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.'); |
|
279 | - } |
|
280 | - |
|
281 | - /* Input validation: */ |
|
282 | - if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
283 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
284 | - } |
|
285 | - |
|
286 | - $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair); |
|
287 | - |
|
288 | - /** @var int $size */ |
|
289 | - $size = filesize($inputFile); |
|
290 | - if (!is_int($size)) { |
|
291 | - throw new SodiumException('Could not obtain the file size'); |
|
292 | - } |
|
293 | - |
|
294 | - /** @var resource $ifp */ |
|
295 | - $ifp = fopen($inputFile, 'rb'); |
|
296 | - if (!is_resource($ifp)) { |
|
297 | - throw new SodiumException('Could not open input file for reading'); |
|
298 | - } |
|
299 | - |
|
300 | - /** @var resource $ofp */ |
|
301 | - $ofp = fopen($outputFile, 'wb'); |
|
302 | - if (!is_resource($ofp)) { |
|
303 | - fclose($ifp); |
|
304 | - throw new SodiumException('Could not open output file for writing'); |
|
305 | - } |
|
306 | - |
|
307 | - $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES); |
|
308 | - if (!is_string($ephemeralPK)) { |
|
309 | - throw new SodiumException('Could not read input file'); |
|
310 | - } |
|
311 | - if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
312 | - fclose($ifp); |
|
313 | - fclose($ofp); |
|
314 | - throw new SodiumException('Could not read public key from sealed file'); |
|
315 | - } |
|
316 | - |
|
317 | - $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
|
318 | - $ephemeralPK . $publicKey, |
|
319 | - '', |
|
320 | - 24 |
|
321 | - ); |
|
322 | - $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
|
323 | - ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair), |
|
324 | - $ephemeralPK |
|
325 | - ); |
|
326 | - |
|
327 | - $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
328 | - fclose($ifp); |
|
329 | - fclose($ofp); |
|
330 | - try { |
|
331 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
332 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
333 | - } catch (SodiumException $ex) { |
|
334 | - if (isset($ephKeypair)) { |
|
335 | - unset($ephKeypair); |
|
336 | - } |
|
337 | - } |
|
338 | - return $res; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * Calculate the BLAKE2b hash of a file. |
|
343 | - * |
|
344 | - * @param string $filePath Absolute path to a file on the filesystem |
|
345 | - * @param string|null $key BLAKE2b key |
|
346 | - * @param int $outputLength Length of hash output |
|
347 | - * |
|
348 | - * @return string BLAKE2b hash |
|
349 | - * @throws SodiumException |
|
350 | - * @throws TypeError |
|
351 | - * @psalm-suppress FailedTypeResolution |
|
352 | - */ |
|
353 | - public static function generichash($filePath, $key = '', $outputLength = 32) |
|
354 | - { |
|
355 | - /* Type checks: */ |
|
356 | - if (!is_string($filePath)) { |
|
357 | - throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
358 | - } |
|
359 | - if (!is_string($key)) { |
|
360 | - if (is_null($key)) { |
|
361 | - $key = ''; |
|
362 | - } else { |
|
363 | - throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.'); |
|
364 | - } |
|
365 | - } |
|
366 | - if (!is_int($outputLength)) { |
|
367 | - if (!is_numeric($outputLength)) { |
|
368 | - throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.'); |
|
369 | - } |
|
370 | - $outputLength = (int) $outputLength; |
|
371 | - } |
|
372 | - |
|
373 | - /* Input validation: */ |
|
374 | - if (!empty($key)) { |
|
375 | - if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) { |
|
376 | - throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes'); |
|
377 | - } |
|
378 | - if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) { |
|
379 | - throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes'); |
|
380 | - } |
|
381 | - } |
|
382 | - if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) { |
|
383 | - throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN'); |
|
384 | - } |
|
385 | - if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) { |
|
386 | - throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX'); |
|
387 | - } |
|
388 | - |
|
389 | - /** @var int $size */ |
|
390 | - $size = filesize($filePath); |
|
391 | - if (!is_int($size)) { |
|
392 | - throw new SodiumException('Could not obtain the file size'); |
|
393 | - } |
|
394 | - |
|
395 | - /** @var resource $fp */ |
|
396 | - $fp = fopen($filePath, 'rb'); |
|
397 | - if (!is_resource($fp)) { |
|
398 | - throw new SodiumException('Could not open input file for reading'); |
|
399 | - } |
|
400 | - $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength); |
|
401 | - while ($size > 0) { |
|
402 | - $blockSize = $size > 64 |
|
403 | - ? 64 |
|
404 | - : $size; |
|
405 | - $read = fread($fp, $blockSize); |
|
406 | - if (!is_string($read)) { |
|
407 | - throw new SodiumException('Could not read input file'); |
|
408 | - } |
|
409 | - ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read); |
|
410 | - $size -= $blockSize; |
|
411 | - } |
|
412 | - |
|
413 | - fclose($fp); |
|
414 | - return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength); |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Encrypt a file (rather than a string). Uses less memory than |
|
419 | - * ParagonIE_Sodium_Compat::crypto_secretbox(), but produces |
|
420 | - * the same result. |
|
421 | - * |
|
422 | - * @param string $inputFile Absolute path to a file on the filesystem |
|
423 | - * @param string $outputFile Absolute path to a file on the filesystem |
|
424 | - * @param string $nonce Number to be used only once |
|
425 | - * @param string $key Encryption key |
|
426 | - * |
|
427 | - * @return bool |
|
428 | - * @throws SodiumException |
|
429 | - * @throws TypeError |
|
430 | - */ |
|
431 | - public static function secretbox($inputFile, $outputFile, $nonce, $key) |
|
432 | - { |
|
433 | - /* Type checks: */ |
|
434 | - if (!is_string($inputFile)) { |
|
435 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..'); |
|
436 | - } |
|
437 | - if (!is_string($outputFile)) { |
|
438 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
439 | - } |
|
440 | - if (!is_string($nonce)) { |
|
441 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
442 | - } |
|
443 | - |
|
444 | - /* Input validation: */ |
|
445 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
446 | - throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
447 | - } |
|
448 | - if (!is_string($key)) { |
|
449 | - throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
450 | - } |
|
451 | - if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
452 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes'); |
|
453 | - } |
|
454 | - |
|
455 | - /** @var int $size */ |
|
456 | - $size = filesize($inputFile); |
|
457 | - if (!is_int($size)) { |
|
458 | - throw new SodiumException('Could not obtain the file size'); |
|
459 | - } |
|
460 | - |
|
461 | - /** @var resource $ifp */ |
|
462 | - $ifp = fopen($inputFile, 'rb'); |
|
463 | - if (!is_resource($ifp)) { |
|
464 | - throw new SodiumException('Could not open input file for reading'); |
|
465 | - } |
|
466 | - |
|
467 | - /** @var resource $ofp */ |
|
468 | - $ofp = fopen($outputFile, 'wb'); |
|
469 | - if (!is_resource($ofp)) { |
|
470 | - fclose($ifp); |
|
471 | - throw new SodiumException('Could not open output file for writing'); |
|
472 | - } |
|
473 | - |
|
474 | - $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key); |
|
475 | - fclose($ifp); |
|
476 | - fclose($ofp); |
|
477 | - return $res; |
|
478 | - } |
|
479 | - /** |
|
480 | - * Seal a file (rather than a string). Uses less memory than |
|
481 | - * ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces |
|
482 | - * the same result. |
|
483 | - * |
|
484 | - * Warning: Does not protect against TOCTOU attacks. You should |
|
485 | - * just load the file into memory and use crypto_secretbox_open() if |
|
486 | - * you are worried about those. |
|
487 | - * |
|
488 | - * @param string $inputFile |
|
489 | - * @param string $outputFile |
|
490 | - * @param string $nonce |
|
491 | - * @param string $key |
|
492 | - * @return bool |
|
493 | - * @throws SodiumException |
|
494 | - * @throws TypeError |
|
495 | - */ |
|
496 | - public static function secretbox_open($inputFile, $outputFile, $nonce, $key) |
|
497 | - { |
|
498 | - /* Type checks: */ |
|
499 | - if (!is_string($inputFile)) { |
|
500 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
501 | - } |
|
502 | - if (!is_string($outputFile)) { |
|
503 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
504 | - } |
|
505 | - if (!is_string($nonce)) { |
|
506 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
507 | - } |
|
508 | - if (!is_string($key)) { |
|
509 | - throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
510 | - } |
|
511 | - |
|
512 | - /* Input validation: */ |
|
513 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
514 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
515 | - } |
|
516 | - if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
517 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes'); |
|
518 | - } |
|
519 | - |
|
520 | - /** @var int $size */ |
|
521 | - $size = filesize($inputFile); |
|
522 | - if (!is_int($size)) { |
|
523 | - throw new SodiumException('Could not obtain the file size'); |
|
524 | - } |
|
525 | - |
|
526 | - /** @var resource $ifp */ |
|
527 | - $ifp = fopen($inputFile, 'rb'); |
|
528 | - if (!is_resource($ifp)) { |
|
529 | - throw new SodiumException('Could not open input file for reading'); |
|
530 | - } |
|
531 | - |
|
532 | - /** @var resource $ofp */ |
|
533 | - $ofp = fopen($outputFile, 'wb'); |
|
534 | - if (!is_resource($ofp)) { |
|
535 | - fclose($ifp); |
|
536 | - throw new SodiumException('Could not open output file for writing'); |
|
537 | - } |
|
538 | - |
|
539 | - $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key); |
|
540 | - fclose($ifp); |
|
541 | - fclose($ofp); |
|
542 | - try { |
|
543 | - ParagonIE_Sodium_Compat::memzero($key); |
|
544 | - } catch (SodiumException $ex) { |
|
545 | - /** @psalm-suppress PossiblyUndefinedVariable */ |
|
546 | - unset($key); |
|
547 | - } |
|
548 | - return $res; |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * Sign a file (rather than a string). Uses less memory than |
|
553 | - * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces |
|
554 | - * the same result. |
|
555 | - * |
|
556 | - * @param string $filePath Absolute path to a file on the filesystem |
|
557 | - * @param string $secretKey Secret signing key |
|
558 | - * |
|
559 | - * @return string Ed25519 signature |
|
560 | - * @throws SodiumException |
|
561 | - * @throws TypeError |
|
562 | - */ |
|
563 | - public static function sign($filePath, $secretKey) |
|
564 | - { |
|
565 | - /* Type checks: */ |
|
566 | - if (!is_string($filePath)) { |
|
567 | - throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
568 | - } |
|
569 | - if (!is_string($secretKey)) { |
|
570 | - throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.'); |
|
571 | - } |
|
572 | - |
|
573 | - /* Input validation: */ |
|
574 | - if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) { |
|
575 | - throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes'); |
|
576 | - } |
|
577 | - if (PHP_INT_SIZE === 4) { |
|
578 | - return self::sign_core32($filePath, $secretKey); |
|
579 | - } |
|
580 | - |
|
581 | - /** @var int $size */ |
|
582 | - $size = filesize($filePath); |
|
583 | - if (!is_int($size)) { |
|
584 | - throw new SodiumException('Could not obtain the file size'); |
|
585 | - } |
|
586 | - |
|
587 | - /** @var resource $fp */ |
|
588 | - $fp = fopen($filePath, 'rb'); |
|
589 | - if (!is_resource($fp)) { |
|
590 | - throw new SodiumException('Could not open input file for reading'); |
|
591 | - } |
|
592 | - |
|
593 | - /** @var string $az */ |
|
594 | - $az = hash('sha512', self::substr($secretKey, 0, 32), true); |
|
595 | - |
|
596 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
597 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
598 | - |
|
599 | - $hs = hash_init('sha512'); |
|
600 | - self::hash_update($hs, self::substr($az, 32, 32)); |
|
601 | - /** @var resource $hs */ |
|
602 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
603 | - |
|
604 | - /** @var string $nonceHash */ |
|
605 | - $nonceHash = hash_final($hs, true); |
|
606 | - |
|
607 | - /** @var string $pk */ |
|
608 | - $pk = self::substr($secretKey, 32, 32); |
|
609 | - |
|
610 | - /** @var string $nonce */ |
|
611 | - $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
612 | - |
|
613 | - /** @var string $sig */ |
|
614 | - $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes( |
|
615 | - ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce) |
|
616 | - ); |
|
617 | - |
|
618 | - $hs = hash_init('sha512'); |
|
619 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
620 | - self::hash_update($hs, self::substr($pk, 0, 32)); |
|
621 | - /** @var resource $hs */ |
|
622 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
623 | - |
|
624 | - /** @var string $hramHash */ |
|
625 | - $hramHash = hash_final($hs, true); |
|
626 | - |
|
627 | - /** @var string $hram */ |
|
628 | - $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash); |
|
629 | - |
|
630 | - /** @var string $sigAfter */ |
|
631 | - $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce); |
|
632 | - |
|
633 | - /** @var string $sig */ |
|
634 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
635 | - |
|
636 | - try { |
|
637 | - ParagonIE_Sodium_Compat::memzero($az); |
|
638 | - } catch (SodiumException $ex) { |
|
639 | - $az = null; |
|
640 | - } |
|
641 | - fclose($fp); |
|
642 | - return $sig; |
|
643 | - } |
|
644 | - |
|
645 | - /** |
|
646 | - * Verify a file (rather than a string). Uses less memory than |
|
647 | - * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but |
|
648 | - * produces the same result. |
|
649 | - * |
|
650 | - * @param string $sig Ed25519 signature |
|
651 | - * @param string $filePath Absolute path to a file on the filesystem |
|
652 | - * @param string $publicKey Signing public key |
|
653 | - * |
|
654 | - * @return bool |
|
655 | - * @throws SodiumException |
|
656 | - * @throws TypeError |
|
657 | - * @throws Exception |
|
658 | - */ |
|
659 | - public static function verify($sig, $filePath, $publicKey) |
|
660 | - { |
|
661 | - /* Type checks: */ |
|
662 | - if (!is_string($sig)) { |
|
663 | - throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.'); |
|
664 | - } |
|
665 | - if (!is_string($filePath)) { |
|
666 | - throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.'); |
|
667 | - } |
|
668 | - if (!is_string($publicKey)) { |
|
669 | - throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
670 | - } |
|
671 | - |
|
672 | - /* Input validation: */ |
|
673 | - if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) { |
|
674 | - throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes'); |
|
675 | - } |
|
676 | - if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) { |
|
677 | - throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes'); |
|
678 | - } |
|
679 | - if (self::strlen($sig) < 64) { |
|
680 | - throw new SodiumException('Signature is too short'); |
|
681 | - } |
|
682 | - |
|
683 | - if (PHP_INT_SIZE === 4) { |
|
684 | - return self::verify_core32($sig, $filePath, $publicKey); |
|
685 | - } |
|
686 | - |
|
687 | - /* Security checks */ |
|
688 | - if ( |
|
689 | - (ParagonIE_Sodium_Core_Ed25519::chrToInt($sig[63]) & 240) |
|
690 | - && |
|
691 | - ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32)) |
|
692 | - ) { |
|
693 | - throw new SodiumException('S < L - Invalid signature'); |
|
694 | - } |
|
695 | - if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) { |
|
696 | - throw new SodiumException('Signature is on too small of an order'); |
|
697 | - } |
|
698 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
699 | - throw new SodiumException('Invalid signature'); |
|
700 | - } |
|
701 | - $d = 0; |
|
702 | - for ($i = 0; $i < 32; ++$i) { |
|
703 | - $d |= self::chrToInt($publicKey[$i]); |
|
704 | - } |
|
705 | - if ($d === 0) { |
|
706 | - throw new SodiumException('All zero public key'); |
|
707 | - } |
|
708 | - |
|
709 | - /** @var int $size */ |
|
710 | - $size = filesize($filePath); |
|
711 | - if (!is_int($size)) { |
|
712 | - throw new SodiumException('Could not obtain the file size'); |
|
713 | - } |
|
714 | - |
|
715 | - /** @var resource $fp */ |
|
716 | - $fp = fopen($filePath, 'rb'); |
|
717 | - if (!is_resource($fp)) { |
|
718 | - throw new SodiumException('Could not open input file for reading'); |
|
719 | - } |
|
720 | - |
|
721 | - /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
722 | - $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
723 | - |
|
724 | - // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
725 | - ParagonIE_Sodium_Compat::$fastMult = true; |
|
726 | - |
|
727 | - /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
|
728 | - $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey); |
|
729 | - |
|
730 | - $hs = hash_init('sha512'); |
|
731 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
732 | - self::hash_update($hs, self::substr($publicKey, 0, 32)); |
|
733 | - /** @var resource $hs */ |
|
734 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
735 | - /** @var string $hDigest */ |
|
736 | - $hDigest = hash_final($hs, true); |
|
737 | - |
|
738 | - /** @var string $h */ |
|
739 | - $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
740 | - |
|
741 | - /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ |
|
742 | - $R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime( |
|
743 | - $h, |
|
744 | - $A, |
|
745 | - self::substr($sig, 32) |
|
746 | - ); |
|
747 | - |
|
748 | - /** @var string $rcheck */ |
|
749 | - $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R); |
|
750 | - |
|
751 | - // Close the file handle |
|
752 | - fclose($fp); |
|
753 | - |
|
754 | - // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
755 | - ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
756 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
757 | - } |
|
758 | - |
|
759 | - /** |
|
760 | - * @param resource $ifp |
|
761 | - * @param resource $ofp |
|
762 | - * @param int $mlen |
|
763 | - * @param string $nonce |
|
764 | - * @param string $boxKeypair |
|
765 | - * @return bool |
|
766 | - * @throws SodiumException |
|
767 | - * @throws TypeError |
|
768 | - */ |
|
769 | - protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
770 | - { |
|
771 | - if (PHP_INT_SIZE === 4) { |
|
772 | - return self::secretbox_encrypt( |
|
773 | - $ifp, |
|
774 | - $ofp, |
|
775 | - $mlen, |
|
776 | - $nonce, |
|
777 | - ParagonIE_Sodium_Crypto32::box_beforenm( |
|
778 | - ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), |
|
779 | - ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) |
|
780 | - ) |
|
781 | - ); |
|
782 | - } |
|
783 | - return self::secretbox_encrypt( |
|
784 | - $ifp, |
|
785 | - $ofp, |
|
786 | - $mlen, |
|
787 | - $nonce, |
|
788 | - ParagonIE_Sodium_Crypto::box_beforenm( |
|
789 | - ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
790 | - ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
791 | - ) |
|
792 | - ); |
|
793 | - } |
|
794 | - |
|
795 | - |
|
796 | - /** |
|
797 | - * @param resource $ifp |
|
798 | - * @param resource $ofp |
|
799 | - * @param int $mlen |
|
800 | - * @param string $nonce |
|
801 | - * @param string $boxKeypair |
|
802 | - * @return bool |
|
803 | - * @throws SodiumException |
|
804 | - * @throws TypeError |
|
805 | - */ |
|
806 | - protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
807 | - { |
|
808 | - if (PHP_INT_SIZE === 4) { |
|
809 | - return self::secretbox_decrypt( |
|
810 | - $ifp, |
|
811 | - $ofp, |
|
812 | - $mlen, |
|
813 | - $nonce, |
|
814 | - ParagonIE_Sodium_Crypto32::box_beforenm( |
|
815 | - ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), |
|
816 | - ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) |
|
817 | - ) |
|
818 | - ); |
|
819 | - } |
|
820 | - return self::secretbox_decrypt( |
|
821 | - $ifp, |
|
822 | - $ofp, |
|
823 | - $mlen, |
|
824 | - $nonce, |
|
825 | - ParagonIE_Sodium_Crypto::box_beforenm( |
|
826 | - ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
827 | - ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
828 | - ) |
|
829 | - ); |
|
830 | - } |
|
831 | - |
|
832 | - /** |
|
833 | - * Encrypt a file |
|
834 | - * |
|
835 | - * @param resource $ifp |
|
836 | - * @param resource $ofp |
|
837 | - * @param int $mlen |
|
838 | - * @param string $nonce |
|
839 | - * @param string $key |
|
840 | - * @return bool |
|
841 | - * @throws SodiumException |
|
842 | - * @throws TypeError |
|
843 | - */ |
|
844 | - protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
845 | - { |
|
846 | - if (PHP_INT_SIZE === 4) { |
|
847 | - return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key); |
|
848 | - } |
|
849 | - |
|
850 | - $plaintext = fread($ifp, 32); |
|
851 | - if (!is_string($plaintext)) { |
|
852 | - throw new SodiumException('Could not read input file'); |
|
853 | - } |
|
854 | - $first32 = self::ftell($ifp); |
|
855 | - |
|
856 | - /** @var string $subkey */ |
|
857 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
858 | - |
|
859 | - /** @var string $realNonce */ |
|
860 | - $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
861 | - |
|
862 | - /** @var string $block0 */ |
|
863 | - $block0 = str_repeat("\x00", 32); |
|
864 | - |
|
865 | - /** @var int $mlen - Length of the plaintext message */ |
|
866 | - $mlen0 = $mlen; |
|
867 | - if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
868 | - $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
869 | - } |
|
870 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
871 | - |
|
872 | - /** @var string $block0 */ |
|
873 | - $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
|
874 | - $block0, |
|
875 | - $realNonce, |
|
876 | - $subkey |
|
877 | - ); |
|
878 | - |
|
879 | - $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
880 | - ParagonIE_Sodium_Core_Util::substr( |
|
881 | - $block0, |
|
882 | - 0, |
|
883 | - ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES |
|
884 | - ) |
|
885 | - ); |
|
886 | - |
|
887 | - // Pre-write 16 blank bytes for the Poly1305 tag |
|
888 | - $start = self::ftell($ofp); |
|
889 | - fwrite($ofp, str_repeat("\x00", 16)); |
|
890 | - |
|
891 | - /** @var string $c */ |
|
892 | - $cBlock = ParagonIE_Sodium_Core_Util::substr( |
|
893 | - $block0, |
|
894 | - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES |
|
895 | - ); |
|
896 | - $state->update($cBlock); |
|
897 | - fwrite($ofp, $cBlock); |
|
898 | - $mlen -= 32; |
|
899 | - |
|
900 | - /** @var int $iter */ |
|
901 | - $iter = 1; |
|
902 | - |
|
903 | - /** @var int $incr */ |
|
904 | - $incr = self::BUFFER_SIZE >> 6; |
|
905 | - |
|
906 | - /* |
|
11 | + /* PHP's default buffer size is 8192 for fread()/fwrite(). */ |
|
12 | + const BUFFER_SIZE = 8192; |
|
13 | + |
|
14 | + /** |
|
15 | + * Box a file (rather than a string). Uses less memory than |
|
16 | + * ParagonIE_Sodium_Compat::crypto_box(), but produces |
|
17 | + * the same result. |
|
18 | + * |
|
19 | + * @param string $inputFile Absolute path to a file on the filesystem |
|
20 | + * @param string $outputFile Absolute path to a file on the filesystem |
|
21 | + * @param string $nonce Number to be used only once |
|
22 | + * @param string $keyPair ECDH secret key and ECDH public key concatenated |
|
23 | + * |
|
24 | + * @return bool |
|
25 | + * @throws SodiumException |
|
26 | + * @throws TypeError |
|
27 | + */ |
|
28 | + public static function box($inputFile, $outputFile, $nonce, $keyPair) |
|
29 | + { |
|
30 | + /* Type checks: */ |
|
31 | + if (!is_string($inputFile)) { |
|
32 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
33 | + } |
|
34 | + if (!is_string($outputFile)) { |
|
35 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
36 | + } |
|
37 | + if (!is_string($nonce)) { |
|
38 | + throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
39 | + } |
|
40 | + |
|
41 | + /* Input validation: */ |
|
42 | + if (!is_string($keyPair)) { |
|
43 | + throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.'); |
|
44 | + } |
|
45 | + if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
46 | + throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
47 | + } |
|
48 | + if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
49 | + throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
50 | + } |
|
51 | + |
|
52 | + /** @var int $size */ |
|
53 | + $size = filesize($inputFile); |
|
54 | + if (!is_int($size)) { |
|
55 | + throw new SodiumException('Could not obtain the file size'); |
|
56 | + } |
|
57 | + |
|
58 | + /** @var resource $ifp */ |
|
59 | + $ifp = fopen($inputFile, 'rb'); |
|
60 | + if (!is_resource($ifp)) { |
|
61 | + throw new SodiumException('Could not open input file for reading'); |
|
62 | + } |
|
63 | + |
|
64 | + /** @var resource $ofp */ |
|
65 | + $ofp = fopen($outputFile, 'wb'); |
|
66 | + if (!is_resource($ofp)) { |
|
67 | + fclose($ifp); |
|
68 | + throw new SodiumException('Could not open output file for writing'); |
|
69 | + } |
|
70 | + |
|
71 | + $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair); |
|
72 | + fclose($ifp); |
|
73 | + fclose($ofp); |
|
74 | + return $res; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Open a boxed file (rather than a string). Uses less memory than |
|
79 | + * ParagonIE_Sodium_Compat::crypto_box_open(), but produces |
|
80 | + * the same result. |
|
81 | + * |
|
82 | + * Warning: Does not protect against TOCTOU attacks. You should |
|
83 | + * just load the file into memory and use crypto_box_open() if |
|
84 | + * you are worried about those. |
|
85 | + * |
|
86 | + * @param string $inputFile |
|
87 | + * @param string $outputFile |
|
88 | + * @param string $nonce |
|
89 | + * @param string $keypair |
|
90 | + * @return bool |
|
91 | + * @throws SodiumException |
|
92 | + * @throws TypeError |
|
93 | + */ |
|
94 | + public static function box_open($inputFile, $outputFile, $nonce, $keypair) |
|
95 | + { |
|
96 | + /* Type checks: */ |
|
97 | + if (!is_string($inputFile)) { |
|
98 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
99 | + } |
|
100 | + if (!is_string($outputFile)) { |
|
101 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
102 | + } |
|
103 | + if (!is_string($nonce)) { |
|
104 | + throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
105 | + } |
|
106 | + if (!is_string($keypair)) { |
|
107 | + throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.'); |
|
108 | + } |
|
109 | + |
|
110 | + /* Input validation: */ |
|
111 | + if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
112 | + throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
113 | + } |
|
114 | + if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
115 | + throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
116 | + } |
|
117 | + |
|
118 | + /** @var int $size */ |
|
119 | + $size = filesize($inputFile); |
|
120 | + if (!is_int($size)) { |
|
121 | + throw new SodiumException('Could not obtain the file size'); |
|
122 | + } |
|
123 | + |
|
124 | + /** @var resource $ifp */ |
|
125 | + $ifp = fopen($inputFile, 'rb'); |
|
126 | + if (!is_resource($ifp)) { |
|
127 | + throw new SodiumException('Could not open input file for reading'); |
|
128 | + } |
|
129 | + |
|
130 | + /** @var resource $ofp */ |
|
131 | + $ofp = fopen($outputFile, 'wb'); |
|
132 | + if (!is_resource($ofp)) { |
|
133 | + fclose($ifp); |
|
134 | + throw new SodiumException('Could not open output file for writing'); |
|
135 | + } |
|
136 | + |
|
137 | + $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair); |
|
138 | + fclose($ifp); |
|
139 | + fclose($ofp); |
|
140 | + try { |
|
141 | + ParagonIE_Sodium_Compat::memzero($nonce); |
|
142 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
143 | + } catch (SodiumException $ex) { |
|
144 | + if (isset($ephKeypair)) { |
|
145 | + unset($ephKeypair); |
|
146 | + } |
|
147 | + } |
|
148 | + return $res; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Seal a file (rather than a string). Uses less memory than |
|
153 | + * ParagonIE_Sodium_Compat::crypto_box_seal(), but produces |
|
154 | + * the same result. |
|
155 | + * |
|
156 | + * @param string $inputFile Absolute path to a file on the filesystem |
|
157 | + * @param string $outputFile Absolute path to a file on the filesystem |
|
158 | + * @param string $publicKey ECDH public key |
|
159 | + * |
|
160 | + * @return bool |
|
161 | + * @throws SodiumException |
|
162 | + * @throws TypeError |
|
163 | + */ |
|
164 | + public static function box_seal($inputFile, $outputFile, $publicKey) |
|
165 | + { |
|
166 | + /* Type checks: */ |
|
167 | + if (!is_string($inputFile)) { |
|
168 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
169 | + } |
|
170 | + if (!is_string($outputFile)) { |
|
171 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
172 | + } |
|
173 | + if (!is_string($publicKey)) { |
|
174 | + throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
175 | + } |
|
176 | + |
|
177 | + /* Input validation: */ |
|
178 | + if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
179 | + throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes'); |
|
180 | + } |
|
181 | + |
|
182 | + /** @var int $size */ |
|
183 | + $size = filesize($inputFile); |
|
184 | + if (!is_int($size)) { |
|
185 | + throw new SodiumException('Could not obtain the file size'); |
|
186 | + } |
|
187 | + |
|
188 | + /** @var resource $ifp */ |
|
189 | + $ifp = fopen($inputFile, 'rb'); |
|
190 | + if (!is_resource($ifp)) { |
|
191 | + throw new SodiumException('Could not open input file for reading'); |
|
192 | + } |
|
193 | + |
|
194 | + /** @var resource $ofp */ |
|
195 | + $ofp = fopen($outputFile, 'wb'); |
|
196 | + if (!is_resource($ofp)) { |
|
197 | + fclose($ifp); |
|
198 | + throw new SodiumException('Could not open output file for writing'); |
|
199 | + } |
|
200 | + |
|
201 | + /** @var string $ephKeypair */ |
|
202 | + $ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair(); |
|
203 | + |
|
204 | + /** @var string $msgKeypair */ |
|
205 | + $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
|
206 | + ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair), |
|
207 | + $publicKey |
|
208 | + ); |
|
209 | + |
|
210 | + /** @var string $ephemeralPK */ |
|
211 | + $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair); |
|
212 | + |
|
213 | + /** @var string $nonce */ |
|
214 | + $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
|
215 | + $ephemeralPK . $publicKey, |
|
216 | + '', |
|
217 | + 24 |
|
218 | + ); |
|
219 | + |
|
220 | + /** @var int $firstWrite */ |
|
221 | + $firstWrite = fwrite( |
|
222 | + $ofp, |
|
223 | + $ephemeralPK, |
|
224 | + ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES |
|
225 | + ); |
|
226 | + if (!is_int($firstWrite)) { |
|
227 | + fclose($ifp); |
|
228 | + fclose($ofp); |
|
229 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
230 | + throw new SodiumException('Could not write to output file'); |
|
231 | + } |
|
232 | + if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
233 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
234 | + fclose($ifp); |
|
235 | + fclose($ofp); |
|
236 | + throw new SodiumException('Error writing public key to output file'); |
|
237 | + } |
|
238 | + |
|
239 | + $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
240 | + fclose($ifp); |
|
241 | + fclose($ofp); |
|
242 | + try { |
|
243 | + ParagonIE_Sodium_Compat::memzero($nonce); |
|
244 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
245 | + } catch (SodiumException $ex) { |
|
246 | + /** @psalm-suppress PossiblyUndefinedVariable */ |
|
247 | + unset($ephKeypair); |
|
248 | + } |
|
249 | + return $res; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Open a sealed file (rather than a string). Uses less memory than |
|
254 | + * ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces |
|
255 | + * the same result. |
|
256 | + * |
|
257 | + * Warning: Does not protect against TOCTOU attacks. You should |
|
258 | + * just load the file into memory and use crypto_box_seal_open() if |
|
259 | + * you are worried about those. |
|
260 | + * |
|
261 | + * @param string $inputFile |
|
262 | + * @param string $outputFile |
|
263 | + * @param string $ecdhKeypair |
|
264 | + * @return bool |
|
265 | + * @throws SodiumException |
|
266 | + * @throws TypeError |
|
267 | + */ |
|
268 | + public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) |
|
269 | + { |
|
270 | + /* Type checks: */ |
|
271 | + if (!is_string($inputFile)) { |
|
272 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
273 | + } |
|
274 | + if (!is_string($outputFile)) { |
|
275 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
276 | + } |
|
277 | + if (!is_string($ecdhKeypair)) { |
|
278 | + throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.'); |
|
279 | + } |
|
280 | + |
|
281 | + /* Input validation: */ |
|
282 | + if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
283 | + throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
284 | + } |
|
285 | + |
|
286 | + $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair); |
|
287 | + |
|
288 | + /** @var int $size */ |
|
289 | + $size = filesize($inputFile); |
|
290 | + if (!is_int($size)) { |
|
291 | + throw new SodiumException('Could not obtain the file size'); |
|
292 | + } |
|
293 | + |
|
294 | + /** @var resource $ifp */ |
|
295 | + $ifp = fopen($inputFile, 'rb'); |
|
296 | + if (!is_resource($ifp)) { |
|
297 | + throw new SodiumException('Could not open input file for reading'); |
|
298 | + } |
|
299 | + |
|
300 | + /** @var resource $ofp */ |
|
301 | + $ofp = fopen($outputFile, 'wb'); |
|
302 | + if (!is_resource($ofp)) { |
|
303 | + fclose($ifp); |
|
304 | + throw new SodiumException('Could not open output file for writing'); |
|
305 | + } |
|
306 | + |
|
307 | + $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES); |
|
308 | + if (!is_string($ephemeralPK)) { |
|
309 | + throw new SodiumException('Could not read input file'); |
|
310 | + } |
|
311 | + if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
312 | + fclose($ifp); |
|
313 | + fclose($ofp); |
|
314 | + throw new SodiumException('Could not read public key from sealed file'); |
|
315 | + } |
|
316 | + |
|
317 | + $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
|
318 | + $ephemeralPK . $publicKey, |
|
319 | + '', |
|
320 | + 24 |
|
321 | + ); |
|
322 | + $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
|
323 | + ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair), |
|
324 | + $ephemeralPK |
|
325 | + ); |
|
326 | + |
|
327 | + $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
328 | + fclose($ifp); |
|
329 | + fclose($ofp); |
|
330 | + try { |
|
331 | + ParagonIE_Sodium_Compat::memzero($nonce); |
|
332 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
333 | + } catch (SodiumException $ex) { |
|
334 | + if (isset($ephKeypair)) { |
|
335 | + unset($ephKeypair); |
|
336 | + } |
|
337 | + } |
|
338 | + return $res; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Calculate the BLAKE2b hash of a file. |
|
343 | + * |
|
344 | + * @param string $filePath Absolute path to a file on the filesystem |
|
345 | + * @param string|null $key BLAKE2b key |
|
346 | + * @param int $outputLength Length of hash output |
|
347 | + * |
|
348 | + * @return string BLAKE2b hash |
|
349 | + * @throws SodiumException |
|
350 | + * @throws TypeError |
|
351 | + * @psalm-suppress FailedTypeResolution |
|
352 | + */ |
|
353 | + public static function generichash($filePath, $key = '', $outputLength = 32) |
|
354 | + { |
|
355 | + /* Type checks: */ |
|
356 | + if (!is_string($filePath)) { |
|
357 | + throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
358 | + } |
|
359 | + if (!is_string($key)) { |
|
360 | + if (is_null($key)) { |
|
361 | + $key = ''; |
|
362 | + } else { |
|
363 | + throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.'); |
|
364 | + } |
|
365 | + } |
|
366 | + if (!is_int($outputLength)) { |
|
367 | + if (!is_numeric($outputLength)) { |
|
368 | + throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.'); |
|
369 | + } |
|
370 | + $outputLength = (int) $outputLength; |
|
371 | + } |
|
372 | + |
|
373 | + /* Input validation: */ |
|
374 | + if (!empty($key)) { |
|
375 | + if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) { |
|
376 | + throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes'); |
|
377 | + } |
|
378 | + if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) { |
|
379 | + throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes'); |
|
380 | + } |
|
381 | + } |
|
382 | + if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) { |
|
383 | + throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN'); |
|
384 | + } |
|
385 | + if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) { |
|
386 | + throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX'); |
|
387 | + } |
|
388 | + |
|
389 | + /** @var int $size */ |
|
390 | + $size = filesize($filePath); |
|
391 | + if (!is_int($size)) { |
|
392 | + throw new SodiumException('Could not obtain the file size'); |
|
393 | + } |
|
394 | + |
|
395 | + /** @var resource $fp */ |
|
396 | + $fp = fopen($filePath, 'rb'); |
|
397 | + if (!is_resource($fp)) { |
|
398 | + throw new SodiumException('Could not open input file for reading'); |
|
399 | + } |
|
400 | + $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength); |
|
401 | + while ($size > 0) { |
|
402 | + $blockSize = $size > 64 |
|
403 | + ? 64 |
|
404 | + : $size; |
|
405 | + $read = fread($fp, $blockSize); |
|
406 | + if (!is_string($read)) { |
|
407 | + throw new SodiumException('Could not read input file'); |
|
408 | + } |
|
409 | + ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read); |
|
410 | + $size -= $blockSize; |
|
411 | + } |
|
412 | + |
|
413 | + fclose($fp); |
|
414 | + return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength); |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Encrypt a file (rather than a string). Uses less memory than |
|
419 | + * ParagonIE_Sodium_Compat::crypto_secretbox(), but produces |
|
420 | + * the same result. |
|
421 | + * |
|
422 | + * @param string $inputFile Absolute path to a file on the filesystem |
|
423 | + * @param string $outputFile Absolute path to a file on the filesystem |
|
424 | + * @param string $nonce Number to be used only once |
|
425 | + * @param string $key Encryption key |
|
426 | + * |
|
427 | + * @return bool |
|
428 | + * @throws SodiumException |
|
429 | + * @throws TypeError |
|
430 | + */ |
|
431 | + public static function secretbox($inputFile, $outputFile, $nonce, $key) |
|
432 | + { |
|
433 | + /* Type checks: */ |
|
434 | + if (!is_string($inputFile)) { |
|
435 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..'); |
|
436 | + } |
|
437 | + if (!is_string($outputFile)) { |
|
438 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
439 | + } |
|
440 | + if (!is_string($nonce)) { |
|
441 | + throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
442 | + } |
|
443 | + |
|
444 | + /* Input validation: */ |
|
445 | + if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
446 | + throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
447 | + } |
|
448 | + if (!is_string($key)) { |
|
449 | + throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
450 | + } |
|
451 | + if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
452 | + throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes'); |
|
453 | + } |
|
454 | + |
|
455 | + /** @var int $size */ |
|
456 | + $size = filesize($inputFile); |
|
457 | + if (!is_int($size)) { |
|
458 | + throw new SodiumException('Could not obtain the file size'); |
|
459 | + } |
|
460 | + |
|
461 | + /** @var resource $ifp */ |
|
462 | + $ifp = fopen($inputFile, 'rb'); |
|
463 | + if (!is_resource($ifp)) { |
|
464 | + throw new SodiumException('Could not open input file for reading'); |
|
465 | + } |
|
466 | + |
|
467 | + /** @var resource $ofp */ |
|
468 | + $ofp = fopen($outputFile, 'wb'); |
|
469 | + if (!is_resource($ofp)) { |
|
470 | + fclose($ifp); |
|
471 | + throw new SodiumException('Could not open output file for writing'); |
|
472 | + } |
|
473 | + |
|
474 | + $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key); |
|
475 | + fclose($ifp); |
|
476 | + fclose($ofp); |
|
477 | + return $res; |
|
478 | + } |
|
479 | + /** |
|
480 | + * Seal a file (rather than a string). Uses less memory than |
|
481 | + * ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces |
|
482 | + * the same result. |
|
483 | + * |
|
484 | + * Warning: Does not protect against TOCTOU attacks. You should |
|
485 | + * just load the file into memory and use crypto_secretbox_open() if |
|
486 | + * you are worried about those. |
|
487 | + * |
|
488 | + * @param string $inputFile |
|
489 | + * @param string $outputFile |
|
490 | + * @param string $nonce |
|
491 | + * @param string $key |
|
492 | + * @return bool |
|
493 | + * @throws SodiumException |
|
494 | + * @throws TypeError |
|
495 | + */ |
|
496 | + public static function secretbox_open($inputFile, $outputFile, $nonce, $key) |
|
497 | + { |
|
498 | + /* Type checks: */ |
|
499 | + if (!is_string($inputFile)) { |
|
500 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
501 | + } |
|
502 | + if (!is_string($outputFile)) { |
|
503 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
504 | + } |
|
505 | + if (!is_string($nonce)) { |
|
506 | + throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
507 | + } |
|
508 | + if (!is_string($key)) { |
|
509 | + throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
510 | + } |
|
511 | + |
|
512 | + /* Input validation: */ |
|
513 | + if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
514 | + throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
515 | + } |
|
516 | + if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
517 | + throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes'); |
|
518 | + } |
|
519 | + |
|
520 | + /** @var int $size */ |
|
521 | + $size = filesize($inputFile); |
|
522 | + if (!is_int($size)) { |
|
523 | + throw new SodiumException('Could not obtain the file size'); |
|
524 | + } |
|
525 | + |
|
526 | + /** @var resource $ifp */ |
|
527 | + $ifp = fopen($inputFile, 'rb'); |
|
528 | + if (!is_resource($ifp)) { |
|
529 | + throw new SodiumException('Could not open input file for reading'); |
|
530 | + } |
|
531 | + |
|
532 | + /** @var resource $ofp */ |
|
533 | + $ofp = fopen($outputFile, 'wb'); |
|
534 | + if (!is_resource($ofp)) { |
|
535 | + fclose($ifp); |
|
536 | + throw new SodiumException('Could not open output file for writing'); |
|
537 | + } |
|
538 | + |
|
539 | + $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key); |
|
540 | + fclose($ifp); |
|
541 | + fclose($ofp); |
|
542 | + try { |
|
543 | + ParagonIE_Sodium_Compat::memzero($key); |
|
544 | + } catch (SodiumException $ex) { |
|
545 | + /** @psalm-suppress PossiblyUndefinedVariable */ |
|
546 | + unset($key); |
|
547 | + } |
|
548 | + return $res; |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * Sign a file (rather than a string). Uses less memory than |
|
553 | + * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces |
|
554 | + * the same result. |
|
555 | + * |
|
556 | + * @param string $filePath Absolute path to a file on the filesystem |
|
557 | + * @param string $secretKey Secret signing key |
|
558 | + * |
|
559 | + * @return string Ed25519 signature |
|
560 | + * @throws SodiumException |
|
561 | + * @throws TypeError |
|
562 | + */ |
|
563 | + public static function sign($filePath, $secretKey) |
|
564 | + { |
|
565 | + /* Type checks: */ |
|
566 | + if (!is_string($filePath)) { |
|
567 | + throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
568 | + } |
|
569 | + if (!is_string($secretKey)) { |
|
570 | + throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.'); |
|
571 | + } |
|
572 | + |
|
573 | + /* Input validation: */ |
|
574 | + if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) { |
|
575 | + throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes'); |
|
576 | + } |
|
577 | + if (PHP_INT_SIZE === 4) { |
|
578 | + return self::sign_core32($filePath, $secretKey); |
|
579 | + } |
|
580 | + |
|
581 | + /** @var int $size */ |
|
582 | + $size = filesize($filePath); |
|
583 | + if (!is_int($size)) { |
|
584 | + throw new SodiumException('Could not obtain the file size'); |
|
585 | + } |
|
586 | + |
|
587 | + /** @var resource $fp */ |
|
588 | + $fp = fopen($filePath, 'rb'); |
|
589 | + if (!is_resource($fp)) { |
|
590 | + throw new SodiumException('Could not open input file for reading'); |
|
591 | + } |
|
592 | + |
|
593 | + /** @var string $az */ |
|
594 | + $az = hash('sha512', self::substr($secretKey, 0, 32), true); |
|
595 | + |
|
596 | + $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
597 | + $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
598 | + |
|
599 | + $hs = hash_init('sha512'); |
|
600 | + self::hash_update($hs, self::substr($az, 32, 32)); |
|
601 | + /** @var resource $hs */ |
|
602 | + $hs = self::updateHashWithFile($hs, $fp, $size); |
|
603 | + |
|
604 | + /** @var string $nonceHash */ |
|
605 | + $nonceHash = hash_final($hs, true); |
|
606 | + |
|
607 | + /** @var string $pk */ |
|
608 | + $pk = self::substr($secretKey, 32, 32); |
|
609 | + |
|
610 | + /** @var string $nonce */ |
|
611 | + $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
612 | + |
|
613 | + /** @var string $sig */ |
|
614 | + $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes( |
|
615 | + ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce) |
|
616 | + ); |
|
617 | + |
|
618 | + $hs = hash_init('sha512'); |
|
619 | + self::hash_update($hs, self::substr($sig, 0, 32)); |
|
620 | + self::hash_update($hs, self::substr($pk, 0, 32)); |
|
621 | + /** @var resource $hs */ |
|
622 | + $hs = self::updateHashWithFile($hs, $fp, $size); |
|
623 | + |
|
624 | + /** @var string $hramHash */ |
|
625 | + $hramHash = hash_final($hs, true); |
|
626 | + |
|
627 | + /** @var string $hram */ |
|
628 | + $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash); |
|
629 | + |
|
630 | + /** @var string $sigAfter */ |
|
631 | + $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce); |
|
632 | + |
|
633 | + /** @var string $sig */ |
|
634 | + $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
635 | + |
|
636 | + try { |
|
637 | + ParagonIE_Sodium_Compat::memzero($az); |
|
638 | + } catch (SodiumException $ex) { |
|
639 | + $az = null; |
|
640 | + } |
|
641 | + fclose($fp); |
|
642 | + return $sig; |
|
643 | + } |
|
644 | + |
|
645 | + /** |
|
646 | + * Verify a file (rather than a string). Uses less memory than |
|
647 | + * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but |
|
648 | + * produces the same result. |
|
649 | + * |
|
650 | + * @param string $sig Ed25519 signature |
|
651 | + * @param string $filePath Absolute path to a file on the filesystem |
|
652 | + * @param string $publicKey Signing public key |
|
653 | + * |
|
654 | + * @return bool |
|
655 | + * @throws SodiumException |
|
656 | + * @throws TypeError |
|
657 | + * @throws Exception |
|
658 | + */ |
|
659 | + public static function verify($sig, $filePath, $publicKey) |
|
660 | + { |
|
661 | + /* Type checks: */ |
|
662 | + if (!is_string($sig)) { |
|
663 | + throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.'); |
|
664 | + } |
|
665 | + if (!is_string($filePath)) { |
|
666 | + throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.'); |
|
667 | + } |
|
668 | + if (!is_string($publicKey)) { |
|
669 | + throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
670 | + } |
|
671 | + |
|
672 | + /* Input validation: */ |
|
673 | + if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) { |
|
674 | + throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes'); |
|
675 | + } |
|
676 | + if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) { |
|
677 | + throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes'); |
|
678 | + } |
|
679 | + if (self::strlen($sig) < 64) { |
|
680 | + throw new SodiumException('Signature is too short'); |
|
681 | + } |
|
682 | + |
|
683 | + if (PHP_INT_SIZE === 4) { |
|
684 | + return self::verify_core32($sig, $filePath, $publicKey); |
|
685 | + } |
|
686 | + |
|
687 | + /* Security checks */ |
|
688 | + if ( |
|
689 | + (ParagonIE_Sodium_Core_Ed25519::chrToInt($sig[63]) & 240) |
|
690 | + && |
|
691 | + ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32)) |
|
692 | + ) { |
|
693 | + throw new SodiumException('S < L - Invalid signature'); |
|
694 | + } |
|
695 | + if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) { |
|
696 | + throw new SodiumException('Signature is on too small of an order'); |
|
697 | + } |
|
698 | + if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
699 | + throw new SodiumException('Invalid signature'); |
|
700 | + } |
|
701 | + $d = 0; |
|
702 | + for ($i = 0; $i < 32; ++$i) { |
|
703 | + $d |= self::chrToInt($publicKey[$i]); |
|
704 | + } |
|
705 | + if ($d === 0) { |
|
706 | + throw new SodiumException('All zero public key'); |
|
707 | + } |
|
708 | + |
|
709 | + /** @var int $size */ |
|
710 | + $size = filesize($filePath); |
|
711 | + if (!is_int($size)) { |
|
712 | + throw new SodiumException('Could not obtain the file size'); |
|
713 | + } |
|
714 | + |
|
715 | + /** @var resource $fp */ |
|
716 | + $fp = fopen($filePath, 'rb'); |
|
717 | + if (!is_resource($fp)) { |
|
718 | + throw new SodiumException('Could not open input file for reading'); |
|
719 | + } |
|
720 | + |
|
721 | + /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
722 | + $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
723 | + |
|
724 | + // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
725 | + ParagonIE_Sodium_Compat::$fastMult = true; |
|
726 | + |
|
727 | + /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
|
728 | + $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey); |
|
729 | + |
|
730 | + $hs = hash_init('sha512'); |
|
731 | + self::hash_update($hs, self::substr($sig, 0, 32)); |
|
732 | + self::hash_update($hs, self::substr($publicKey, 0, 32)); |
|
733 | + /** @var resource $hs */ |
|
734 | + $hs = self::updateHashWithFile($hs, $fp, $size); |
|
735 | + /** @var string $hDigest */ |
|
736 | + $hDigest = hash_final($hs, true); |
|
737 | + |
|
738 | + /** @var string $h */ |
|
739 | + $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
740 | + |
|
741 | + /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ |
|
742 | + $R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime( |
|
743 | + $h, |
|
744 | + $A, |
|
745 | + self::substr($sig, 32) |
|
746 | + ); |
|
747 | + |
|
748 | + /** @var string $rcheck */ |
|
749 | + $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R); |
|
750 | + |
|
751 | + // Close the file handle |
|
752 | + fclose($fp); |
|
753 | + |
|
754 | + // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
755 | + ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
756 | + return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
757 | + } |
|
758 | + |
|
759 | + /** |
|
760 | + * @param resource $ifp |
|
761 | + * @param resource $ofp |
|
762 | + * @param int $mlen |
|
763 | + * @param string $nonce |
|
764 | + * @param string $boxKeypair |
|
765 | + * @return bool |
|
766 | + * @throws SodiumException |
|
767 | + * @throws TypeError |
|
768 | + */ |
|
769 | + protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
770 | + { |
|
771 | + if (PHP_INT_SIZE === 4) { |
|
772 | + return self::secretbox_encrypt( |
|
773 | + $ifp, |
|
774 | + $ofp, |
|
775 | + $mlen, |
|
776 | + $nonce, |
|
777 | + ParagonIE_Sodium_Crypto32::box_beforenm( |
|
778 | + ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), |
|
779 | + ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) |
|
780 | + ) |
|
781 | + ); |
|
782 | + } |
|
783 | + return self::secretbox_encrypt( |
|
784 | + $ifp, |
|
785 | + $ofp, |
|
786 | + $mlen, |
|
787 | + $nonce, |
|
788 | + ParagonIE_Sodium_Crypto::box_beforenm( |
|
789 | + ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
790 | + ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
791 | + ) |
|
792 | + ); |
|
793 | + } |
|
794 | + |
|
795 | + |
|
796 | + /** |
|
797 | + * @param resource $ifp |
|
798 | + * @param resource $ofp |
|
799 | + * @param int $mlen |
|
800 | + * @param string $nonce |
|
801 | + * @param string $boxKeypair |
|
802 | + * @return bool |
|
803 | + * @throws SodiumException |
|
804 | + * @throws TypeError |
|
805 | + */ |
|
806 | + protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
807 | + { |
|
808 | + if (PHP_INT_SIZE === 4) { |
|
809 | + return self::secretbox_decrypt( |
|
810 | + $ifp, |
|
811 | + $ofp, |
|
812 | + $mlen, |
|
813 | + $nonce, |
|
814 | + ParagonIE_Sodium_Crypto32::box_beforenm( |
|
815 | + ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), |
|
816 | + ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) |
|
817 | + ) |
|
818 | + ); |
|
819 | + } |
|
820 | + return self::secretbox_decrypt( |
|
821 | + $ifp, |
|
822 | + $ofp, |
|
823 | + $mlen, |
|
824 | + $nonce, |
|
825 | + ParagonIE_Sodium_Crypto::box_beforenm( |
|
826 | + ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
827 | + ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
828 | + ) |
|
829 | + ); |
|
830 | + } |
|
831 | + |
|
832 | + /** |
|
833 | + * Encrypt a file |
|
834 | + * |
|
835 | + * @param resource $ifp |
|
836 | + * @param resource $ofp |
|
837 | + * @param int $mlen |
|
838 | + * @param string $nonce |
|
839 | + * @param string $key |
|
840 | + * @return bool |
|
841 | + * @throws SodiumException |
|
842 | + * @throws TypeError |
|
843 | + */ |
|
844 | + protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
845 | + { |
|
846 | + if (PHP_INT_SIZE === 4) { |
|
847 | + return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key); |
|
848 | + } |
|
849 | + |
|
850 | + $plaintext = fread($ifp, 32); |
|
851 | + if (!is_string($plaintext)) { |
|
852 | + throw new SodiumException('Could not read input file'); |
|
853 | + } |
|
854 | + $first32 = self::ftell($ifp); |
|
855 | + |
|
856 | + /** @var string $subkey */ |
|
857 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
858 | + |
|
859 | + /** @var string $realNonce */ |
|
860 | + $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
861 | + |
|
862 | + /** @var string $block0 */ |
|
863 | + $block0 = str_repeat("\x00", 32); |
|
864 | + |
|
865 | + /** @var int $mlen - Length of the plaintext message */ |
|
866 | + $mlen0 = $mlen; |
|
867 | + if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
868 | + $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
869 | + } |
|
870 | + $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
871 | + |
|
872 | + /** @var string $block0 */ |
|
873 | + $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
|
874 | + $block0, |
|
875 | + $realNonce, |
|
876 | + $subkey |
|
877 | + ); |
|
878 | + |
|
879 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
880 | + ParagonIE_Sodium_Core_Util::substr( |
|
881 | + $block0, |
|
882 | + 0, |
|
883 | + ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES |
|
884 | + ) |
|
885 | + ); |
|
886 | + |
|
887 | + // Pre-write 16 blank bytes for the Poly1305 tag |
|
888 | + $start = self::ftell($ofp); |
|
889 | + fwrite($ofp, str_repeat("\x00", 16)); |
|
890 | + |
|
891 | + /** @var string $c */ |
|
892 | + $cBlock = ParagonIE_Sodium_Core_Util::substr( |
|
893 | + $block0, |
|
894 | + ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES |
|
895 | + ); |
|
896 | + $state->update($cBlock); |
|
897 | + fwrite($ofp, $cBlock); |
|
898 | + $mlen -= 32; |
|
899 | + |
|
900 | + /** @var int $iter */ |
|
901 | + $iter = 1; |
|
902 | + |
|
903 | + /** @var int $incr */ |
|
904 | + $incr = self::BUFFER_SIZE >> 6; |
|
905 | + |
|
906 | + /* |
|
907 | 907 | * Set the cursor to the end of the first half-block. All future bytes will |
908 | 908 | * generated from salsa20_xor_ic, starting from 1 (second block). |
909 | 909 | */ |
910 | - fseek($ifp, $first32, SEEK_SET); |
|
911 | - |
|
912 | - while ($mlen > 0) { |
|
913 | - $blockSize = $mlen > self::BUFFER_SIZE |
|
914 | - ? self::BUFFER_SIZE |
|
915 | - : $mlen; |
|
916 | - $plaintext = fread($ifp, $blockSize); |
|
917 | - if (!is_string($plaintext)) { |
|
918 | - throw new SodiumException('Could not read input file'); |
|
919 | - } |
|
920 | - $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
921 | - $plaintext, |
|
922 | - $realNonce, |
|
923 | - $iter, |
|
924 | - $subkey |
|
925 | - ); |
|
926 | - fwrite($ofp, $cBlock, $blockSize); |
|
927 | - $state->update($cBlock); |
|
928 | - |
|
929 | - $mlen -= $blockSize; |
|
930 | - $iter += $incr; |
|
931 | - } |
|
932 | - try { |
|
933 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
934 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
935 | - } catch (SodiumException $ex) { |
|
936 | - $block0 = null; |
|
937 | - $subkey = null; |
|
938 | - } |
|
939 | - $end = self::ftell($ofp); |
|
940 | - |
|
941 | - /* |
|
910 | + fseek($ifp, $first32, SEEK_SET); |
|
911 | + |
|
912 | + while ($mlen > 0) { |
|
913 | + $blockSize = $mlen > self::BUFFER_SIZE |
|
914 | + ? self::BUFFER_SIZE |
|
915 | + : $mlen; |
|
916 | + $plaintext = fread($ifp, $blockSize); |
|
917 | + if (!is_string($plaintext)) { |
|
918 | + throw new SodiumException('Could not read input file'); |
|
919 | + } |
|
920 | + $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
921 | + $plaintext, |
|
922 | + $realNonce, |
|
923 | + $iter, |
|
924 | + $subkey |
|
925 | + ); |
|
926 | + fwrite($ofp, $cBlock, $blockSize); |
|
927 | + $state->update($cBlock); |
|
928 | + |
|
929 | + $mlen -= $blockSize; |
|
930 | + $iter += $incr; |
|
931 | + } |
|
932 | + try { |
|
933 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
934 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
935 | + } catch (SodiumException $ex) { |
|
936 | + $block0 = null; |
|
937 | + $subkey = null; |
|
938 | + } |
|
939 | + $end = self::ftell($ofp); |
|
940 | + |
|
941 | + /* |
|
942 | 942 | * Write the Poly1305 authentication tag that provides integrity |
943 | 943 | * over the ciphertext (encrypt-then-MAC) |
944 | 944 | */ |
945 | - fseek($ofp, $start, SEEK_SET); |
|
946 | - fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); |
|
947 | - fseek($ofp, $end, SEEK_SET); |
|
948 | - unset($state); |
|
949 | - |
|
950 | - return true; |
|
951 | - } |
|
952 | - |
|
953 | - /** |
|
954 | - * Decrypt a file |
|
955 | - * |
|
956 | - * @param resource $ifp |
|
957 | - * @param resource $ofp |
|
958 | - * @param int $mlen |
|
959 | - * @param string $nonce |
|
960 | - * @param string $key |
|
961 | - * @return bool |
|
962 | - * @throws SodiumException |
|
963 | - * @throws TypeError |
|
964 | - */ |
|
965 | - protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
966 | - { |
|
967 | - if (PHP_INT_SIZE === 4) { |
|
968 | - return self::secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key); |
|
969 | - } |
|
970 | - $tag = fread($ifp, 16); |
|
971 | - if (!is_string($tag)) { |
|
972 | - throw new SodiumException('Could not read input file'); |
|
973 | - } |
|
974 | - |
|
975 | - /** @var string $subkey */ |
|
976 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
977 | - |
|
978 | - /** @var string $realNonce */ |
|
979 | - $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
980 | - |
|
981 | - /** @var string $block0 */ |
|
982 | - $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
|
983 | - 64, |
|
984 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
985 | - $subkey |
|
986 | - ); |
|
987 | - |
|
988 | - /* Verify the Poly1305 MAC -before- attempting to decrypt! */ |
|
989 | - $state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32)); |
|
990 | - if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) { |
|
991 | - throw new SodiumException('Invalid MAC'); |
|
992 | - } |
|
993 | - |
|
994 | - /* |
|
945 | + fseek($ofp, $start, SEEK_SET); |
|
946 | + fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); |
|
947 | + fseek($ofp, $end, SEEK_SET); |
|
948 | + unset($state); |
|
949 | + |
|
950 | + return true; |
|
951 | + } |
|
952 | + |
|
953 | + /** |
|
954 | + * Decrypt a file |
|
955 | + * |
|
956 | + * @param resource $ifp |
|
957 | + * @param resource $ofp |
|
958 | + * @param int $mlen |
|
959 | + * @param string $nonce |
|
960 | + * @param string $key |
|
961 | + * @return bool |
|
962 | + * @throws SodiumException |
|
963 | + * @throws TypeError |
|
964 | + */ |
|
965 | + protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
966 | + { |
|
967 | + if (PHP_INT_SIZE === 4) { |
|
968 | + return self::secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key); |
|
969 | + } |
|
970 | + $tag = fread($ifp, 16); |
|
971 | + if (!is_string($tag)) { |
|
972 | + throw new SodiumException('Could not read input file'); |
|
973 | + } |
|
974 | + |
|
975 | + /** @var string $subkey */ |
|
976 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
977 | + |
|
978 | + /** @var string $realNonce */ |
|
979 | + $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
980 | + |
|
981 | + /** @var string $block0 */ |
|
982 | + $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
|
983 | + 64, |
|
984 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
985 | + $subkey |
|
986 | + ); |
|
987 | + |
|
988 | + /* Verify the Poly1305 MAC -before- attempting to decrypt! */ |
|
989 | + $state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32)); |
|
990 | + if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) { |
|
991 | + throw new SodiumException('Invalid MAC'); |
|
992 | + } |
|
993 | + |
|
994 | + /* |
|
995 | 995 | * Set the cursor to the end of the first half-block. All future bytes will |
996 | 996 | * generated from salsa20_xor_ic, starting from 1 (second block). |
997 | 997 | */ |
998 | - $first32 = fread($ifp, 32); |
|
999 | - if (!is_string($first32)) { |
|
1000 | - throw new SodiumException('Could not read input file'); |
|
1001 | - } |
|
1002 | - $first32len = self::strlen($first32); |
|
1003 | - fwrite( |
|
1004 | - $ofp, |
|
1005 | - self::xorStrings( |
|
1006 | - self::substr($block0, 32, $first32len), |
|
1007 | - self::substr($first32, 0, $first32len) |
|
1008 | - ) |
|
1009 | - ); |
|
1010 | - $mlen -= 32; |
|
1011 | - |
|
1012 | - /** @var int $iter */ |
|
1013 | - $iter = 1; |
|
1014 | - |
|
1015 | - /** @var int $incr */ |
|
1016 | - $incr = self::BUFFER_SIZE >> 6; |
|
1017 | - |
|
1018 | - /* Decrypts ciphertext, writes to output file. */ |
|
1019 | - while ($mlen > 0) { |
|
1020 | - $blockSize = $mlen > self::BUFFER_SIZE |
|
1021 | - ? self::BUFFER_SIZE |
|
1022 | - : $mlen; |
|
1023 | - $ciphertext = fread($ifp, $blockSize); |
|
1024 | - if (!is_string($ciphertext)) { |
|
1025 | - throw new SodiumException('Could not read input file'); |
|
1026 | - } |
|
1027 | - $pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
1028 | - $ciphertext, |
|
1029 | - $realNonce, |
|
1030 | - $iter, |
|
1031 | - $subkey |
|
1032 | - ); |
|
1033 | - fwrite($ofp, $pBlock, $blockSize); |
|
1034 | - $mlen -= $blockSize; |
|
1035 | - $iter += $incr; |
|
1036 | - } |
|
1037 | - return true; |
|
1038 | - } |
|
1039 | - |
|
1040 | - /** |
|
1041 | - * @param ParagonIE_Sodium_Core_Poly1305_State $state |
|
1042 | - * @param resource $ifp |
|
1043 | - * @param string $tag |
|
1044 | - * @param int $mlen |
|
1045 | - * @return bool |
|
1046 | - * @throws SodiumException |
|
1047 | - * @throws TypeError |
|
1048 | - */ |
|
1049 | - protected static function onetimeauth_verify( |
|
1050 | - ParagonIE_Sodium_Core_Poly1305_State $state, |
|
1051 | - $ifp, |
|
1052 | - $tag = '', |
|
1053 | - $mlen = 0 |
|
1054 | - ) { |
|
1055 | - /** @var int $pos */ |
|
1056 | - $pos = self::ftell($ifp); |
|
1057 | - |
|
1058 | - /** @var int $iter */ |
|
1059 | - $iter = 1; |
|
1060 | - |
|
1061 | - /** @var int $incr */ |
|
1062 | - $incr = self::BUFFER_SIZE >> 6; |
|
1063 | - |
|
1064 | - while ($mlen > 0) { |
|
1065 | - $blockSize = $mlen > self::BUFFER_SIZE |
|
1066 | - ? self::BUFFER_SIZE |
|
1067 | - : $mlen; |
|
1068 | - $ciphertext = fread($ifp, $blockSize); |
|
1069 | - if (!is_string($ciphertext)) { |
|
1070 | - throw new SodiumException('Could not read input file'); |
|
1071 | - } |
|
1072 | - $state->update($ciphertext); |
|
1073 | - $mlen -= $blockSize; |
|
1074 | - $iter += $incr; |
|
1075 | - } |
|
1076 | - $res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish()); |
|
1077 | - |
|
1078 | - fseek($ifp, $pos, SEEK_SET); |
|
1079 | - return $res; |
|
1080 | - } |
|
1081 | - |
|
1082 | - /** |
|
1083 | - * Update a hash context with the contents of a file, without |
|
1084 | - * loading the entire file into memory. |
|
1085 | - * |
|
1086 | - * @param resource|HashContext $hash |
|
1087 | - * @param resource $fp |
|
1088 | - * @param int $size |
|
1089 | - * @return resource|object Resource on PHP < 7.2, HashContext object on PHP >= 7.2 |
|
1090 | - * @throws SodiumException |
|
1091 | - * @throws TypeError |
|
1092 | - * @psalm-suppress PossiblyInvalidArgument |
|
1093 | - * PHP 7.2 changes from a resource to an object, |
|
1094 | - * which causes Psalm to complain about an error. |
|
1095 | - * @psalm-suppress TypeCoercion |
|
1096 | - * Ditto. |
|
1097 | - */ |
|
1098 | - public static function updateHashWithFile($hash, $fp, $size = 0) |
|
1099 | - { |
|
1100 | - /* Type checks: */ |
|
1101 | - if (PHP_VERSION_ID < 70200) { |
|
1102 | - if (!is_resource($hash)) { |
|
1103 | - throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.'); |
|
1104 | - } |
|
1105 | - } else { |
|
1106 | - if (!is_object($hash)) { |
|
1107 | - throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.'); |
|
1108 | - } |
|
1109 | - } |
|
1110 | - |
|
1111 | - if (!is_resource($fp)) { |
|
1112 | - throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.'); |
|
1113 | - } |
|
1114 | - if (!is_int($size)) { |
|
1115 | - throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.'); |
|
1116 | - } |
|
1117 | - |
|
1118 | - /** @var int $originalPosition */ |
|
1119 | - $originalPosition = self::ftell($fp); |
|
1120 | - |
|
1121 | - // Move file pointer to beginning of file |
|
1122 | - fseek($fp, 0, SEEK_SET); |
|
1123 | - for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) { |
|
1124 | - /** @var string|bool $message */ |
|
1125 | - $message = fread( |
|
1126 | - $fp, |
|
1127 | - ($size - $i) > self::BUFFER_SIZE |
|
1128 | - ? $size - $i |
|
1129 | - : self::BUFFER_SIZE |
|
1130 | - ); |
|
1131 | - if (!is_string($message)) { |
|
1132 | - throw new SodiumException('Unexpected error reading from file.'); |
|
1133 | - } |
|
1134 | - /** @var string $message */ |
|
1135 | - /** @psalm-suppress InvalidArgument */ |
|
1136 | - self::hash_update($hash, $message); |
|
1137 | - } |
|
1138 | - // Reset file pointer's position |
|
1139 | - fseek($fp, $originalPosition, SEEK_SET); |
|
1140 | - return $hash; |
|
1141 | - } |
|
1142 | - |
|
1143 | - /** |
|
1144 | - * Sign a file (rather than a string). Uses less memory than |
|
1145 | - * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces |
|
1146 | - * the same result. (32-bit) |
|
1147 | - * |
|
1148 | - * @param string $filePath Absolute path to a file on the filesystem |
|
1149 | - * @param string $secretKey Secret signing key |
|
1150 | - * |
|
1151 | - * @return string Ed25519 signature |
|
1152 | - * @throws SodiumException |
|
1153 | - * @throws TypeError |
|
1154 | - */ |
|
1155 | - private static function sign_core32($filePath, $secretKey) |
|
1156 | - { |
|
1157 | - /** @var int|bool $size */ |
|
1158 | - $size = filesize($filePath); |
|
1159 | - if (!is_int($size)) { |
|
1160 | - throw new SodiumException('Could not obtain the file size'); |
|
1161 | - } |
|
1162 | - /** @var int $size */ |
|
1163 | - |
|
1164 | - /** @var resource|bool $fp */ |
|
1165 | - $fp = fopen($filePath, 'rb'); |
|
1166 | - if (!is_resource($fp)) { |
|
1167 | - throw new SodiumException('Could not open input file for reading'); |
|
1168 | - } |
|
1169 | - /** @var resource $fp */ |
|
1170 | - |
|
1171 | - /** @var string $az */ |
|
1172 | - $az = hash('sha512', self::substr($secretKey, 0, 32), true); |
|
1173 | - |
|
1174 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
1175 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
1176 | - |
|
1177 | - $hs = hash_init('sha512'); |
|
1178 | - self::hash_update($hs, self::substr($az, 32, 32)); |
|
1179 | - /** @var resource $hs */ |
|
1180 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
1181 | - |
|
1182 | - /** @var string $nonceHash */ |
|
1183 | - $nonceHash = hash_final($hs, true); |
|
1184 | - |
|
1185 | - /** @var string $pk */ |
|
1186 | - $pk = self::substr($secretKey, 32, 32); |
|
1187 | - |
|
1188 | - /** @var string $nonce */ |
|
1189 | - $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
1190 | - |
|
1191 | - /** @var string $sig */ |
|
1192 | - $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes( |
|
1193 | - ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce) |
|
1194 | - ); |
|
1195 | - |
|
1196 | - $hs = hash_init('sha512'); |
|
1197 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
1198 | - self::hash_update($hs, self::substr($pk, 0, 32)); |
|
1199 | - /** @var resource $hs */ |
|
1200 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
1201 | - |
|
1202 | - /** @var string $hramHash */ |
|
1203 | - $hramHash = hash_final($hs, true); |
|
1204 | - |
|
1205 | - /** @var string $hram */ |
|
1206 | - $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash); |
|
1207 | - |
|
1208 | - /** @var string $sigAfter */ |
|
1209 | - $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce); |
|
1210 | - |
|
1211 | - /** @var string $sig */ |
|
1212 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
1213 | - |
|
1214 | - try { |
|
1215 | - ParagonIE_Sodium_Compat::memzero($az); |
|
1216 | - } catch (SodiumException $ex) { |
|
1217 | - $az = null; |
|
1218 | - } |
|
1219 | - fclose($fp); |
|
1220 | - return $sig; |
|
1221 | - } |
|
1222 | - |
|
1223 | - /** |
|
1224 | - * |
|
1225 | - * Verify a file (rather than a string). Uses less memory than |
|
1226 | - * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but |
|
1227 | - * produces the same result. (32-bit) |
|
1228 | - * |
|
1229 | - * @param string $sig Ed25519 signature |
|
1230 | - * @param string $filePath Absolute path to a file on the filesystem |
|
1231 | - * @param string $publicKey Signing public key |
|
1232 | - * |
|
1233 | - * @return bool |
|
1234 | - * @throws SodiumException |
|
1235 | - * @throws Exception |
|
1236 | - */ |
|
1237 | - public static function verify_core32($sig, $filePath, $publicKey) |
|
1238 | - { |
|
1239 | - /* Security checks */ |
|
1240 | - if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
1241 | - throw new SodiumException('S < L - Invalid signature'); |
|
1242 | - } |
|
1243 | - if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) { |
|
1244 | - throw new SodiumException('Signature is on too small of an order'); |
|
1245 | - } |
|
1246 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
1247 | - throw new SodiumException('Invalid signature'); |
|
1248 | - } |
|
1249 | - $d = 0; |
|
1250 | - for ($i = 0; $i < 32; ++$i) { |
|
1251 | - $d |= self::chrToInt($publicKey[$i]); |
|
1252 | - } |
|
1253 | - if ($d === 0) { |
|
1254 | - throw new SodiumException('All zero public key'); |
|
1255 | - } |
|
1256 | - |
|
1257 | - /** @var int|bool $size */ |
|
1258 | - $size = filesize($filePath); |
|
1259 | - if (!is_int($size)) { |
|
1260 | - throw new SodiumException('Could not obtain the file size'); |
|
1261 | - } |
|
1262 | - /** @var int $size */ |
|
1263 | - |
|
1264 | - /** @var resource|bool $fp */ |
|
1265 | - $fp = fopen($filePath, 'rb'); |
|
1266 | - if (!is_resource($fp)) { |
|
1267 | - throw new SodiumException('Could not open input file for reading'); |
|
1268 | - } |
|
1269 | - /** @var resource $fp */ |
|
1270 | - |
|
1271 | - /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
1272 | - $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
1273 | - |
|
1274 | - // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
1275 | - ParagonIE_Sodium_Compat::$fastMult = true; |
|
1276 | - |
|
1277 | - /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
|
1278 | - $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey); |
|
1279 | - |
|
1280 | - $hs = hash_init('sha512'); |
|
1281 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
1282 | - self::hash_update($hs, self::substr($publicKey, 0, 32)); |
|
1283 | - /** @var resource $hs */ |
|
1284 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
1285 | - /** @var string $hDigest */ |
|
1286 | - $hDigest = hash_final($hs, true); |
|
1287 | - |
|
1288 | - /** @var string $h */ |
|
1289 | - $h = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
1290 | - |
|
1291 | - /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ |
|
1292 | - $R = ParagonIE_Sodium_Core32_Ed25519::ge_double_scalarmult_vartime( |
|
1293 | - $h, |
|
1294 | - $A, |
|
1295 | - self::substr($sig, 32) |
|
1296 | - ); |
|
1297 | - |
|
1298 | - /** @var string $rcheck */ |
|
1299 | - $rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes($R); |
|
1300 | - |
|
1301 | - // Close the file handle |
|
1302 | - fclose($fp); |
|
1303 | - |
|
1304 | - // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
1305 | - ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
1306 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
1307 | - } |
|
1308 | - |
|
1309 | - /** |
|
1310 | - * Encrypt a file (32-bit) |
|
1311 | - * |
|
1312 | - * @param resource $ifp |
|
1313 | - * @param resource $ofp |
|
1314 | - * @param int $mlen |
|
1315 | - * @param string $nonce |
|
1316 | - * @param string $key |
|
1317 | - * @return bool |
|
1318 | - * @throws SodiumException |
|
1319 | - * @throws TypeError |
|
1320 | - */ |
|
1321 | - protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key) |
|
1322 | - { |
|
1323 | - $plaintext = fread($ifp, 32); |
|
1324 | - if (!is_string($plaintext)) { |
|
1325 | - throw new SodiumException('Could not read input file'); |
|
1326 | - } |
|
1327 | - $first32 = self::ftell($ifp); |
|
1328 | - |
|
1329 | - /** @var string $subkey */ |
|
1330 | - $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); |
|
1331 | - |
|
1332 | - /** @var string $realNonce */ |
|
1333 | - $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); |
|
1334 | - |
|
1335 | - /** @var string $block0 */ |
|
1336 | - $block0 = str_repeat("\x00", 32); |
|
1337 | - |
|
1338 | - /** @var int $mlen - Length of the plaintext message */ |
|
1339 | - $mlen0 = $mlen; |
|
1340 | - if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
1341 | - $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
1342 | - } |
|
1343 | - $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0); |
|
1344 | - |
|
1345 | - /** @var string $block0 */ |
|
1346 | - $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor( |
|
1347 | - $block0, |
|
1348 | - $realNonce, |
|
1349 | - $subkey |
|
1350 | - ); |
|
1351 | - |
|
1352 | - $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
1353 | - ParagonIE_Sodium_Core32_Util::substr( |
|
1354 | - $block0, |
|
1355 | - 0, |
|
1356 | - ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES |
|
1357 | - ) |
|
1358 | - ); |
|
1359 | - |
|
1360 | - // Pre-write 16 blank bytes for the Poly1305 tag |
|
1361 | - $start = self::ftell($ofp); |
|
1362 | - fwrite($ofp, str_repeat("\x00", 16)); |
|
1363 | - |
|
1364 | - /** @var string $c */ |
|
1365 | - $cBlock = ParagonIE_Sodium_Core32_Util::substr( |
|
1366 | - $block0, |
|
1367 | - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES |
|
1368 | - ); |
|
1369 | - $state->update($cBlock); |
|
1370 | - fwrite($ofp, $cBlock); |
|
1371 | - $mlen -= 32; |
|
1372 | - |
|
1373 | - /** @var int $iter */ |
|
1374 | - $iter = 1; |
|
1375 | - |
|
1376 | - /** @var int $incr */ |
|
1377 | - $incr = self::BUFFER_SIZE >> 6; |
|
1378 | - |
|
1379 | - /* |
|
998 | + $first32 = fread($ifp, 32); |
|
999 | + if (!is_string($first32)) { |
|
1000 | + throw new SodiumException('Could not read input file'); |
|
1001 | + } |
|
1002 | + $first32len = self::strlen($first32); |
|
1003 | + fwrite( |
|
1004 | + $ofp, |
|
1005 | + self::xorStrings( |
|
1006 | + self::substr($block0, 32, $first32len), |
|
1007 | + self::substr($first32, 0, $first32len) |
|
1008 | + ) |
|
1009 | + ); |
|
1010 | + $mlen -= 32; |
|
1011 | + |
|
1012 | + /** @var int $iter */ |
|
1013 | + $iter = 1; |
|
1014 | + |
|
1015 | + /** @var int $incr */ |
|
1016 | + $incr = self::BUFFER_SIZE >> 6; |
|
1017 | + |
|
1018 | + /* Decrypts ciphertext, writes to output file. */ |
|
1019 | + while ($mlen > 0) { |
|
1020 | + $blockSize = $mlen > self::BUFFER_SIZE |
|
1021 | + ? self::BUFFER_SIZE |
|
1022 | + : $mlen; |
|
1023 | + $ciphertext = fread($ifp, $blockSize); |
|
1024 | + if (!is_string($ciphertext)) { |
|
1025 | + throw new SodiumException('Could not read input file'); |
|
1026 | + } |
|
1027 | + $pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
1028 | + $ciphertext, |
|
1029 | + $realNonce, |
|
1030 | + $iter, |
|
1031 | + $subkey |
|
1032 | + ); |
|
1033 | + fwrite($ofp, $pBlock, $blockSize); |
|
1034 | + $mlen -= $blockSize; |
|
1035 | + $iter += $incr; |
|
1036 | + } |
|
1037 | + return true; |
|
1038 | + } |
|
1039 | + |
|
1040 | + /** |
|
1041 | + * @param ParagonIE_Sodium_Core_Poly1305_State $state |
|
1042 | + * @param resource $ifp |
|
1043 | + * @param string $tag |
|
1044 | + * @param int $mlen |
|
1045 | + * @return bool |
|
1046 | + * @throws SodiumException |
|
1047 | + * @throws TypeError |
|
1048 | + */ |
|
1049 | + protected static function onetimeauth_verify( |
|
1050 | + ParagonIE_Sodium_Core_Poly1305_State $state, |
|
1051 | + $ifp, |
|
1052 | + $tag = '', |
|
1053 | + $mlen = 0 |
|
1054 | + ) { |
|
1055 | + /** @var int $pos */ |
|
1056 | + $pos = self::ftell($ifp); |
|
1057 | + |
|
1058 | + /** @var int $iter */ |
|
1059 | + $iter = 1; |
|
1060 | + |
|
1061 | + /** @var int $incr */ |
|
1062 | + $incr = self::BUFFER_SIZE >> 6; |
|
1063 | + |
|
1064 | + while ($mlen > 0) { |
|
1065 | + $blockSize = $mlen > self::BUFFER_SIZE |
|
1066 | + ? self::BUFFER_SIZE |
|
1067 | + : $mlen; |
|
1068 | + $ciphertext = fread($ifp, $blockSize); |
|
1069 | + if (!is_string($ciphertext)) { |
|
1070 | + throw new SodiumException('Could not read input file'); |
|
1071 | + } |
|
1072 | + $state->update($ciphertext); |
|
1073 | + $mlen -= $blockSize; |
|
1074 | + $iter += $incr; |
|
1075 | + } |
|
1076 | + $res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish()); |
|
1077 | + |
|
1078 | + fseek($ifp, $pos, SEEK_SET); |
|
1079 | + return $res; |
|
1080 | + } |
|
1081 | + |
|
1082 | + /** |
|
1083 | + * Update a hash context with the contents of a file, without |
|
1084 | + * loading the entire file into memory. |
|
1085 | + * |
|
1086 | + * @param resource|HashContext $hash |
|
1087 | + * @param resource $fp |
|
1088 | + * @param int $size |
|
1089 | + * @return resource|object Resource on PHP < 7.2, HashContext object on PHP >= 7.2 |
|
1090 | + * @throws SodiumException |
|
1091 | + * @throws TypeError |
|
1092 | + * @psalm-suppress PossiblyInvalidArgument |
|
1093 | + * PHP 7.2 changes from a resource to an object, |
|
1094 | + * which causes Psalm to complain about an error. |
|
1095 | + * @psalm-suppress TypeCoercion |
|
1096 | + * Ditto. |
|
1097 | + */ |
|
1098 | + public static function updateHashWithFile($hash, $fp, $size = 0) |
|
1099 | + { |
|
1100 | + /* Type checks: */ |
|
1101 | + if (PHP_VERSION_ID < 70200) { |
|
1102 | + if (!is_resource($hash)) { |
|
1103 | + throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.'); |
|
1104 | + } |
|
1105 | + } else { |
|
1106 | + if (!is_object($hash)) { |
|
1107 | + throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.'); |
|
1108 | + } |
|
1109 | + } |
|
1110 | + |
|
1111 | + if (!is_resource($fp)) { |
|
1112 | + throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.'); |
|
1113 | + } |
|
1114 | + if (!is_int($size)) { |
|
1115 | + throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.'); |
|
1116 | + } |
|
1117 | + |
|
1118 | + /** @var int $originalPosition */ |
|
1119 | + $originalPosition = self::ftell($fp); |
|
1120 | + |
|
1121 | + // Move file pointer to beginning of file |
|
1122 | + fseek($fp, 0, SEEK_SET); |
|
1123 | + for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) { |
|
1124 | + /** @var string|bool $message */ |
|
1125 | + $message = fread( |
|
1126 | + $fp, |
|
1127 | + ($size - $i) > self::BUFFER_SIZE |
|
1128 | + ? $size - $i |
|
1129 | + : self::BUFFER_SIZE |
|
1130 | + ); |
|
1131 | + if (!is_string($message)) { |
|
1132 | + throw new SodiumException('Unexpected error reading from file.'); |
|
1133 | + } |
|
1134 | + /** @var string $message */ |
|
1135 | + /** @psalm-suppress InvalidArgument */ |
|
1136 | + self::hash_update($hash, $message); |
|
1137 | + } |
|
1138 | + // Reset file pointer's position |
|
1139 | + fseek($fp, $originalPosition, SEEK_SET); |
|
1140 | + return $hash; |
|
1141 | + } |
|
1142 | + |
|
1143 | + /** |
|
1144 | + * Sign a file (rather than a string). Uses less memory than |
|
1145 | + * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces |
|
1146 | + * the same result. (32-bit) |
|
1147 | + * |
|
1148 | + * @param string $filePath Absolute path to a file on the filesystem |
|
1149 | + * @param string $secretKey Secret signing key |
|
1150 | + * |
|
1151 | + * @return string Ed25519 signature |
|
1152 | + * @throws SodiumException |
|
1153 | + * @throws TypeError |
|
1154 | + */ |
|
1155 | + private static function sign_core32($filePath, $secretKey) |
|
1156 | + { |
|
1157 | + /** @var int|bool $size */ |
|
1158 | + $size = filesize($filePath); |
|
1159 | + if (!is_int($size)) { |
|
1160 | + throw new SodiumException('Could not obtain the file size'); |
|
1161 | + } |
|
1162 | + /** @var int $size */ |
|
1163 | + |
|
1164 | + /** @var resource|bool $fp */ |
|
1165 | + $fp = fopen($filePath, 'rb'); |
|
1166 | + if (!is_resource($fp)) { |
|
1167 | + throw new SodiumException('Could not open input file for reading'); |
|
1168 | + } |
|
1169 | + /** @var resource $fp */ |
|
1170 | + |
|
1171 | + /** @var string $az */ |
|
1172 | + $az = hash('sha512', self::substr($secretKey, 0, 32), true); |
|
1173 | + |
|
1174 | + $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
1175 | + $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
1176 | + |
|
1177 | + $hs = hash_init('sha512'); |
|
1178 | + self::hash_update($hs, self::substr($az, 32, 32)); |
|
1179 | + /** @var resource $hs */ |
|
1180 | + $hs = self::updateHashWithFile($hs, $fp, $size); |
|
1181 | + |
|
1182 | + /** @var string $nonceHash */ |
|
1183 | + $nonceHash = hash_final($hs, true); |
|
1184 | + |
|
1185 | + /** @var string $pk */ |
|
1186 | + $pk = self::substr($secretKey, 32, 32); |
|
1187 | + |
|
1188 | + /** @var string $nonce */ |
|
1189 | + $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
1190 | + |
|
1191 | + /** @var string $sig */ |
|
1192 | + $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes( |
|
1193 | + ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce) |
|
1194 | + ); |
|
1195 | + |
|
1196 | + $hs = hash_init('sha512'); |
|
1197 | + self::hash_update($hs, self::substr($sig, 0, 32)); |
|
1198 | + self::hash_update($hs, self::substr($pk, 0, 32)); |
|
1199 | + /** @var resource $hs */ |
|
1200 | + $hs = self::updateHashWithFile($hs, $fp, $size); |
|
1201 | + |
|
1202 | + /** @var string $hramHash */ |
|
1203 | + $hramHash = hash_final($hs, true); |
|
1204 | + |
|
1205 | + /** @var string $hram */ |
|
1206 | + $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash); |
|
1207 | + |
|
1208 | + /** @var string $sigAfter */ |
|
1209 | + $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce); |
|
1210 | + |
|
1211 | + /** @var string $sig */ |
|
1212 | + $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
1213 | + |
|
1214 | + try { |
|
1215 | + ParagonIE_Sodium_Compat::memzero($az); |
|
1216 | + } catch (SodiumException $ex) { |
|
1217 | + $az = null; |
|
1218 | + } |
|
1219 | + fclose($fp); |
|
1220 | + return $sig; |
|
1221 | + } |
|
1222 | + |
|
1223 | + /** |
|
1224 | + * |
|
1225 | + * Verify a file (rather than a string). Uses less memory than |
|
1226 | + * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but |
|
1227 | + * produces the same result. (32-bit) |
|
1228 | + * |
|
1229 | + * @param string $sig Ed25519 signature |
|
1230 | + * @param string $filePath Absolute path to a file on the filesystem |
|
1231 | + * @param string $publicKey Signing public key |
|
1232 | + * |
|
1233 | + * @return bool |
|
1234 | + * @throws SodiumException |
|
1235 | + * @throws Exception |
|
1236 | + */ |
|
1237 | + public static function verify_core32($sig, $filePath, $publicKey) |
|
1238 | + { |
|
1239 | + /* Security checks */ |
|
1240 | + if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
1241 | + throw new SodiumException('S < L - Invalid signature'); |
|
1242 | + } |
|
1243 | + if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) { |
|
1244 | + throw new SodiumException('Signature is on too small of an order'); |
|
1245 | + } |
|
1246 | + if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
1247 | + throw new SodiumException('Invalid signature'); |
|
1248 | + } |
|
1249 | + $d = 0; |
|
1250 | + for ($i = 0; $i < 32; ++$i) { |
|
1251 | + $d |= self::chrToInt($publicKey[$i]); |
|
1252 | + } |
|
1253 | + if ($d === 0) { |
|
1254 | + throw new SodiumException('All zero public key'); |
|
1255 | + } |
|
1256 | + |
|
1257 | + /** @var int|bool $size */ |
|
1258 | + $size = filesize($filePath); |
|
1259 | + if (!is_int($size)) { |
|
1260 | + throw new SodiumException('Could not obtain the file size'); |
|
1261 | + } |
|
1262 | + /** @var int $size */ |
|
1263 | + |
|
1264 | + /** @var resource|bool $fp */ |
|
1265 | + $fp = fopen($filePath, 'rb'); |
|
1266 | + if (!is_resource($fp)) { |
|
1267 | + throw new SodiumException('Could not open input file for reading'); |
|
1268 | + } |
|
1269 | + /** @var resource $fp */ |
|
1270 | + |
|
1271 | + /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
1272 | + $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
1273 | + |
|
1274 | + // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
1275 | + ParagonIE_Sodium_Compat::$fastMult = true; |
|
1276 | + |
|
1277 | + /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
|
1278 | + $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey); |
|
1279 | + |
|
1280 | + $hs = hash_init('sha512'); |
|
1281 | + self::hash_update($hs, self::substr($sig, 0, 32)); |
|
1282 | + self::hash_update($hs, self::substr($publicKey, 0, 32)); |
|
1283 | + /** @var resource $hs */ |
|
1284 | + $hs = self::updateHashWithFile($hs, $fp, $size); |
|
1285 | + /** @var string $hDigest */ |
|
1286 | + $hDigest = hash_final($hs, true); |
|
1287 | + |
|
1288 | + /** @var string $h */ |
|
1289 | + $h = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
1290 | + |
|
1291 | + /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ |
|
1292 | + $R = ParagonIE_Sodium_Core32_Ed25519::ge_double_scalarmult_vartime( |
|
1293 | + $h, |
|
1294 | + $A, |
|
1295 | + self::substr($sig, 32) |
|
1296 | + ); |
|
1297 | + |
|
1298 | + /** @var string $rcheck */ |
|
1299 | + $rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes($R); |
|
1300 | + |
|
1301 | + // Close the file handle |
|
1302 | + fclose($fp); |
|
1303 | + |
|
1304 | + // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
1305 | + ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
1306 | + return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
1307 | + } |
|
1308 | + |
|
1309 | + /** |
|
1310 | + * Encrypt a file (32-bit) |
|
1311 | + * |
|
1312 | + * @param resource $ifp |
|
1313 | + * @param resource $ofp |
|
1314 | + * @param int $mlen |
|
1315 | + * @param string $nonce |
|
1316 | + * @param string $key |
|
1317 | + * @return bool |
|
1318 | + * @throws SodiumException |
|
1319 | + * @throws TypeError |
|
1320 | + */ |
|
1321 | + protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key) |
|
1322 | + { |
|
1323 | + $plaintext = fread($ifp, 32); |
|
1324 | + if (!is_string($plaintext)) { |
|
1325 | + throw new SodiumException('Could not read input file'); |
|
1326 | + } |
|
1327 | + $first32 = self::ftell($ifp); |
|
1328 | + |
|
1329 | + /** @var string $subkey */ |
|
1330 | + $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); |
|
1331 | + |
|
1332 | + /** @var string $realNonce */ |
|
1333 | + $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); |
|
1334 | + |
|
1335 | + /** @var string $block0 */ |
|
1336 | + $block0 = str_repeat("\x00", 32); |
|
1337 | + |
|
1338 | + /** @var int $mlen - Length of the plaintext message */ |
|
1339 | + $mlen0 = $mlen; |
|
1340 | + if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
1341 | + $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
1342 | + } |
|
1343 | + $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0); |
|
1344 | + |
|
1345 | + /** @var string $block0 */ |
|
1346 | + $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor( |
|
1347 | + $block0, |
|
1348 | + $realNonce, |
|
1349 | + $subkey |
|
1350 | + ); |
|
1351 | + |
|
1352 | + $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
1353 | + ParagonIE_Sodium_Core32_Util::substr( |
|
1354 | + $block0, |
|
1355 | + 0, |
|
1356 | + ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES |
|
1357 | + ) |
|
1358 | + ); |
|
1359 | + |
|
1360 | + // Pre-write 16 blank bytes for the Poly1305 tag |
|
1361 | + $start = self::ftell($ofp); |
|
1362 | + fwrite($ofp, str_repeat("\x00", 16)); |
|
1363 | + |
|
1364 | + /** @var string $c */ |
|
1365 | + $cBlock = ParagonIE_Sodium_Core32_Util::substr( |
|
1366 | + $block0, |
|
1367 | + ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES |
|
1368 | + ); |
|
1369 | + $state->update($cBlock); |
|
1370 | + fwrite($ofp, $cBlock); |
|
1371 | + $mlen -= 32; |
|
1372 | + |
|
1373 | + /** @var int $iter */ |
|
1374 | + $iter = 1; |
|
1375 | + |
|
1376 | + /** @var int $incr */ |
|
1377 | + $incr = self::BUFFER_SIZE >> 6; |
|
1378 | + |
|
1379 | + /* |
|
1380 | 1380 | * Set the cursor to the end of the first half-block. All future bytes will |
1381 | 1381 | * generated from salsa20_xor_ic, starting from 1 (second block). |
1382 | 1382 | */ |
1383 | - fseek($ifp, $first32, SEEK_SET); |
|
1384 | - |
|
1385 | - while ($mlen > 0) { |
|
1386 | - $blockSize = $mlen > self::BUFFER_SIZE |
|
1387 | - ? self::BUFFER_SIZE |
|
1388 | - : $mlen; |
|
1389 | - $plaintext = fread($ifp, $blockSize); |
|
1390 | - if (!is_string($plaintext)) { |
|
1391 | - throw new SodiumException('Could not read input file'); |
|
1392 | - } |
|
1393 | - $cBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( |
|
1394 | - $plaintext, |
|
1395 | - $realNonce, |
|
1396 | - $iter, |
|
1397 | - $subkey |
|
1398 | - ); |
|
1399 | - fwrite($ofp, $cBlock, $blockSize); |
|
1400 | - $state->update($cBlock); |
|
1401 | - |
|
1402 | - $mlen -= $blockSize; |
|
1403 | - $iter += $incr; |
|
1404 | - } |
|
1405 | - try { |
|
1406 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
1407 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
1408 | - } catch (SodiumException $ex) { |
|
1409 | - $block0 = null; |
|
1410 | - $subkey = null; |
|
1411 | - } |
|
1412 | - $end = self::ftell($ofp); |
|
1413 | - |
|
1414 | - /* |
|
1383 | + fseek($ifp, $first32, SEEK_SET); |
|
1384 | + |
|
1385 | + while ($mlen > 0) { |
|
1386 | + $blockSize = $mlen > self::BUFFER_SIZE |
|
1387 | + ? self::BUFFER_SIZE |
|
1388 | + : $mlen; |
|
1389 | + $plaintext = fread($ifp, $blockSize); |
|
1390 | + if (!is_string($plaintext)) { |
|
1391 | + throw new SodiumException('Could not read input file'); |
|
1392 | + } |
|
1393 | + $cBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( |
|
1394 | + $plaintext, |
|
1395 | + $realNonce, |
|
1396 | + $iter, |
|
1397 | + $subkey |
|
1398 | + ); |
|
1399 | + fwrite($ofp, $cBlock, $blockSize); |
|
1400 | + $state->update($cBlock); |
|
1401 | + |
|
1402 | + $mlen -= $blockSize; |
|
1403 | + $iter += $incr; |
|
1404 | + } |
|
1405 | + try { |
|
1406 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
1407 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
1408 | + } catch (SodiumException $ex) { |
|
1409 | + $block0 = null; |
|
1410 | + $subkey = null; |
|
1411 | + } |
|
1412 | + $end = self::ftell($ofp); |
|
1413 | + |
|
1414 | + /* |
|
1415 | 1415 | * Write the Poly1305 authentication tag that provides integrity |
1416 | 1416 | * over the ciphertext (encrypt-then-MAC) |
1417 | 1417 | */ |
1418 | - fseek($ofp, $start, SEEK_SET); |
|
1419 | - fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); |
|
1420 | - fseek($ofp, $end, SEEK_SET); |
|
1421 | - unset($state); |
|
1422 | - |
|
1423 | - return true; |
|
1424 | - } |
|
1425 | - |
|
1426 | - /** |
|
1427 | - * Decrypt a file (32-bit) |
|
1428 | - * |
|
1429 | - * @param resource $ifp |
|
1430 | - * @param resource $ofp |
|
1431 | - * @param int $mlen |
|
1432 | - * @param string $nonce |
|
1433 | - * @param string $key |
|
1434 | - * @return bool |
|
1435 | - * @throws SodiumException |
|
1436 | - * @throws TypeError |
|
1437 | - */ |
|
1438 | - protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key) |
|
1439 | - { |
|
1440 | - $tag = fread($ifp, 16); |
|
1441 | - if (!is_string($tag)) { |
|
1442 | - throw new SodiumException('Could not read input file'); |
|
1443 | - } |
|
1444 | - |
|
1445 | - /** @var string $subkey */ |
|
1446 | - $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); |
|
1447 | - |
|
1448 | - /** @var string $realNonce */ |
|
1449 | - $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); |
|
1450 | - |
|
1451 | - /** @var string $block0 */ |
|
1452 | - $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20( |
|
1453 | - 64, |
|
1454 | - ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), |
|
1455 | - $subkey |
|
1456 | - ); |
|
1457 | - |
|
1458 | - /* Verify the Poly1305 MAC -before- attempting to decrypt! */ |
|
1459 | - $state = new ParagonIE_Sodium_Core32_Poly1305_State(self::substr($block0, 0, 32)); |
|
1460 | - if (!self::onetimeauth_verify_core32($state, $ifp, $tag, $mlen)) { |
|
1461 | - throw new SodiumException('Invalid MAC'); |
|
1462 | - } |
|
1463 | - |
|
1464 | - /* |
|
1418 | + fseek($ofp, $start, SEEK_SET); |
|
1419 | + fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); |
|
1420 | + fseek($ofp, $end, SEEK_SET); |
|
1421 | + unset($state); |
|
1422 | + |
|
1423 | + return true; |
|
1424 | + } |
|
1425 | + |
|
1426 | + /** |
|
1427 | + * Decrypt a file (32-bit) |
|
1428 | + * |
|
1429 | + * @param resource $ifp |
|
1430 | + * @param resource $ofp |
|
1431 | + * @param int $mlen |
|
1432 | + * @param string $nonce |
|
1433 | + * @param string $key |
|
1434 | + * @return bool |
|
1435 | + * @throws SodiumException |
|
1436 | + * @throws TypeError |
|
1437 | + */ |
|
1438 | + protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key) |
|
1439 | + { |
|
1440 | + $tag = fread($ifp, 16); |
|
1441 | + if (!is_string($tag)) { |
|
1442 | + throw new SodiumException('Could not read input file'); |
|
1443 | + } |
|
1444 | + |
|
1445 | + /** @var string $subkey */ |
|
1446 | + $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); |
|
1447 | + |
|
1448 | + /** @var string $realNonce */ |
|
1449 | + $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); |
|
1450 | + |
|
1451 | + /** @var string $block0 */ |
|
1452 | + $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20( |
|
1453 | + 64, |
|
1454 | + ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), |
|
1455 | + $subkey |
|
1456 | + ); |
|
1457 | + |
|
1458 | + /* Verify the Poly1305 MAC -before- attempting to decrypt! */ |
|
1459 | + $state = new ParagonIE_Sodium_Core32_Poly1305_State(self::substr($block0, 0, 32)); |
|
1460 | + if (!self::onetimeauth_verify_core32($state, $ifp, $tag, $mlen)) { |
|
1461 | + throw new SodiumException('Invalid MAC'); |
|
1462 | + } |
|
1463 | + |
|
1464 | + /* |
|
1465 | 1465 | * Set the cursor to the end of the first half-block. All future bytes will |
1466 | 1466 | * generated from salsa20_xor_ic, starting from 1 (second block). |
1467 | 1467 | */ |
1468 | - $first32 = fread($ifp, 32); |
|
1469 | - if (!is_string($first32)) { |
|
1470 | - throw new SodiumException('Could not read input file'); |
|
1471 | - } |
|
1472 | - $first32len = self::strlen($first32); |
|
1473 | - fwrite( |
|
1474 | - $ofp, |
|
1475 | - self::xorStrings( |
|
1476 | - self::substr($block0, 32, $first32len), |
|
1477 | - self::substr($first32, 0, $first32len) |
|
1478 | - ) |
|
1479 | - ); |
|
1480 | - $mlen -= 32; |
|
1481 | - |
|
1482 | - /** @var int $iter */ |
|
1483 | - $iter = 1; |
|
1484 | - |
|
1485 | - /** @var int $incr */ |
|
1486 | - $incr = self::BUFFER_SIZE >> 6; |
|
1487 | - |
|
1488 | - /* Decrypts ciphertext, writes to output file. */ |
|
1489 | - while ($mlen > 0) { |
|
1490 | - $blockSize = $mlen > self::BUFFER_SIZE |
|
1491 | - ? self::BUFFER_SIZE |
|
1492 | - : $mlen; |
|
1493 | - $ciphertext = fread($ifp, $blockSize); |
|
1494 | - if (!is_string($ciphertext)) { |
|
1495 | - throw new SodiumException('Could not read input file'); |
|
1496 | - } |
|
1497 | - $pBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( |
|
1498 | - $ciphertext, |
|
1499 | - $realNonce, |
|
1500 | - $iter, |
|
1501 | - $subkey |
|
1502 | - ); |
|
1503 | - fwrite($ofp, $pBlock, $blockSize); |
|
1504 | - $mlen -= $blockSize; |
|
1505 | - $iter += $incr; |
|
1506 | - } |
|
1507 | - return true; |
|
1508 | - } |
|
1509 | - |
|
1510 | - /** |
|
1511 | - * One-time message authentication for 32-bit systems |
|
1512 | - * |
|
1513 | - * @param ParagonIE_Sodium_Core32_Poly1305_State $state |
|
1514 | - * @param resource $ifp |
|
1515 | - * @param string $tag |
|
1516 | - * @param int $mlen |
|
1517 | - * @return bool |
|
1518 | - * @throws SodiumException |
|
1519 | - * @throws TypeError |
|
1520 | - */ |
|
1521 | - protected static function onetimeauth_verify_core32( |
|
1522 | - ParagonIE_Sodium_Core32_Poly1305_State $state, |
|
1523 | - $ifp, |
|
1524 | - $tag = '', |
|
1525 | - $mlen = 0 |
|
1526 | - ) { |
|
1527 | - /** @var int $pos */ |
|
1528 | - $pos = self::ftell($ifp); |
|
1529 | - |
|
1530 | - while ($mlen > 0) { |
|
1531 | - $blockSize = $mlen > self::BUFFER_SIZE |
|
1532 | - ? self::BUFFER_SIZE |
|
1533 | - : $mlen; |
|
1534 | - $ciphertext = fread($ifp, $blockSize); |
|
1535 | - if (!is_string($ciphertext)) { |
|
1536 | - throw new SodiumException('Could not read input file'); |
|
1537 | - } |
|
1538 | - $state->update($ciphertext); |
|
1539 | - $mlen -= $blockSize; |
|
1540 | - } |
|
1541 | - $res = ParagonIE_Sodium_Core32_Util::verify_16($tag, $state->finish()); |
|
1542 | - |
|
1543 | - fseek($ifp, $pos, SEEK_SET); |
|
1544 | - return $res; |
|
1545 | - } |
|
1546 | - |
|
1547 | - /** |
|
1548 | - * @param resource $resource |
|
1549 | - * @return int |
|
1550 | - * @throws SodiumException |
|
1551 | - */ |
|
1552 | - private static function ftell($resource) |
|
1553 | - { |
|
1554 | - $return = ftell($resource); |
|
1555 | - if (!is_int($return)) { |
|
1556 | - throw new SodiumException('ftell() returned false'); |
|
1557 | - } |
|
1558 | - return (int) $return; |
|
1559 | - } |
|
1468 | + $first32 = fread($ifp, 32); |
|
1469 | + if (!is_string($first32)) { |
|
1470 | + throw new SodiumException('Could not read input file'); |
|
1471 | + } |
|
1472 | + $first32len = self::strlen($first32); |
|
1473 | + fwrite( |
|
1474 | + $ofp, |
|
1475 | + self::xorStrings( |
|
1476 | + self::substr($block0, 32, $first32len), |
|
1477 | + self::substr($first32, 0, $first32len) |
|
1478 | + ) |
|
1479 | + ); |
|
1480 | + $mlen -= 32; |
|
1481 | + |
|
1482 | + /** @var int $iter */ |
|
1483 | + $iter = 1; |
|
1484 | + |
|
1485 | + /** @var int $incr */ |
|
1486 | + $incr = self::BUFFER_SIZE >> 6; |
|
1487 | + |
|
1488 | + /* Decrypts ciphertext, writes to output file. */ |
|
1489 | + while ($mlen > 0) { |
|
1490 | + $blockSize = $mlen > self::BUFFER_SIZE |
|
1491 | + ? self::BUFFER_SIZE |
|
1492 | + : $mlen; |
|
1493 | + $ciphertext = fread($ifp, $blockSize); |
|
1494 | + if (!is_string($ciphertext)) { |
|
1495 | + throw new SodiumException('Could not read input file'); |
|
1496 | + } |
|
1497 | + $pBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( |
|
1498 | + $ciphertext, |
|
1499 | + $realNonce, |
|
1500 | + $iter, |
|
1501 | + $subkey |
|
1502 | + ); |
|
1503 | + fwrite($ofp, $pBlock, $blockSize); |
|
1504 | + $mlen -= $blockSize; |
|
1505 | + $iter += $incr; |
|
1506 | + } |
|
1507 | + return true; |
|
1508 | + } |
|
1509 | + |
|
1510 | + /** |
|
1511 | + * One-time message authentication for 32-bit systems |
|
1512 | + * |
|
1513 | + * @param ParagonIE_Sodium_Core32_Poly1305_State $state |
|
1514 | + * @param resource $ifp |
|
1515 | + * @param string $tag |
|
1516 | + * @param int $mlen |
|
1517 | + * @return bool |
|
1518 | + * @throws SodiumException |
|
1519 | + * @throws TypeError |
|
1520 | + */ |
|
1521 | + protected static function onetimeauth_verify_core32( |
|
1522 | + ParagonIE_Sodium_Core32_Poly1305_State $state, |
|
1523 | + $ifp, |
|
1524 | + $tag = '', |
|
1525 | + $mlen = 0 |
|
1526 | + ) { |
|
1527 | + /** @var int $pos */ |
|
1528 | + $pos = self::ftell($ifp); |
|
1529 | + |
|
1530 | + while ($mlen > 0) { |
|
1531 | + $blockSize = $mlen > self::BUFFER_SIZE |
|
1532 | + ? self::BUFFER_SIZE |
|
1533 | + : $mlen; |
|
1534 | + $ciphertext = fread($ifp, $blockSize); |
|
1535 | + if (!is_string($ciphertext)) { |
|
1536 | + throw new SodiumException('Could not read input file'); |
|
1537 | + } |
|
1538 | + $state->update($ciphertext); |
|
1539 | + $mlen -= $blockSize; |
|
1540 | + } |
|
1541 | + $res = ParagonIE_Sodium_Core32_Util::verify_16($tag, $state->finish()); |
|
1542 | + |
|
1543 | + fseek($ifp, $pos, SEEK_SET); |
|
1544 | + return $res; |
|
1545 | + } |
|
1546 | + |
|
1547 | + /** |
|
1548 | + * @param resource $resource |
|
1549 | + * @return int |
|
1550 | + * @throws SodiumException |
|
1551 | + */ |
|
1552 | + private static function ftell($resource) |
|
1553 | + { |
|
1554 | + $return = ftell($resource); |
|
1555 | + if (!is_int($return)) { |
|
1556 | + throw new SodiumException('ftell() returned false'); |
|
1557 | + } |
|
1558 | + return (int) $return; |
|
1559 | + } |
|
1560 | 1560 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_File', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_File', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | /** |
@@ -25,52 +25,52 @@ discard block |
||
25 | 25 | * @throws SodiumException |
26 | 26 | * @throws TypeError |
27 | 27 | */ |
28 | - public static function box($inputFile, $outputFile, $nonce, $keyPair) |
|
28 | + public static function box( $inputFile, $outputFile, $nonce, $keyPair ) |
|
29 | 29 | { |
30 | 30 | /* Type checks: */ |
31 | - if (!is_string($inputFile)) { |
|
32 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
31 | + if ( ! is_string( $inputFile ) ) { |
|
32 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
33 | 33 | } |
34 | - if (!is_string($outputFile)) { |
|
35 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
34 | + if ( ! is_string( $outputFile ) ) { |
|
35 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
36 | 36 | } |
37 | - if (!is_string($nonce)) { |
|
38 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
37 | + if ( ! is_string( $nonce ) ) { |
|
38 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' ); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | /* Input validation: */ |
42 | - if (!is_string($keyPair)) { |
|
43 | - throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.'); |
|
42 | + if ( ! is_string( $keyPair ) ) { |
|
43 | + throw new TypeError( 'Argument 4 must be a string, ' . gettype( $keyPair ) . ' given.' ); |
|
44 | 44 | } |
45 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
46 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
45 | + if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES ) { |
|
46 | + throw new TypeError( 'Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes' ); |
|
47 | 47 | } |
48 | - if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
49 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
48 | + if ( self::strlen( $keyPair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) { |
|
49 | + throw new TypeError( 'Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes' ); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | /** @var int $size */ |
53 | - $size = filesize($inputFile); |
|
54 | - if (!is_int($size)) { |
|
55 | - throw new SodiumException('Could not obtain the file size'); |
|
53 | + $size = filesize( $inputFile ); |
|
54 | + if ( ! is_int( $size ) ) { |
|
55 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** @var resource $ifp */ |
59 | - $ifp = fopen($inputFile, 'rb'); |
|
60 | - if (!is_resource($ifp)) { |
|
61 | - throw new SodiumException('Could not open input file for reading'); |
|
59 | + $ifp = fopen( $inputFile, 'rb' ); |
|
60 | + if ( ! is_resource( $ifp ) ) { |
|
61 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | /** @var resource $ofp */ |
65 | - $ofp = fopen($outputFile, 'wb'); |
|
66 | - if (!is_resource($ofp)) { |
|
67 | - fclose($ifp); |
|
68 | - throw new SodiumException('Could not open output file for writing'); |
|
65 | + $ofp = fopen( $outputFile, 'wb' ); |
|
66 | + if ( ! is_resource( $ofp ) ) { |
|
67 | + fclose( $ifp ); |
|
68 | + throw new SodiumException( 'Could not open output file for writing' ); |
|
69 | 69 | } |
70 | 70 | |
71 | - $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair); |
|
72 | - fclose($ifp); |
|
73 | - fclose($ofp); |
|
71 | + $res = self::box_encrypt( $ifp, $ofp, $size, $nonce, $keyPair ); |
|
72 | + fclose( $ifp ); |
|
73 | + fclose( $ofp ); |
|
74 | 74 | return $res; |
75 | 75 | } |
76 | 76 | |
@@ -91,58 +91,58 @@ discard block |
||
91 | 91 | * @throws SodiumException |
92 | 92 | * @throws TypeError |
93 | 93 | */ |
94 | - public static function box_open($inputFile, $outputFile, $nonce, $keypair) |
|
94 | + public static function box_open( $inputFile, $outputFile, $nonce, $keypair ) |
|
95 | 95 | { |
96 | 96 | /* Type checks: */ |
97 | - if (!is_string($inputFile)) { |
|
98 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
97 | + if ( ! is_string( $inputFile ) ) { |
|
98 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
99 | 99 | } |
100 | - if (!is_string($outputFile)) { |
|
101 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
100 | + if ( ! is_string( $outputFile ) ) { |
|
101 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
102 | 102 | } |
103 | - if (!is_string($nonce)) { |
|
104 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
103 | + if ( ! is_string( $nonce ) ) { |
|
104 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' ); |
|
105 | 105 | } |
106 | - if (!is_string($keypair)) { |
|
107 | - throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.'); |
|
106 | + if ( ! is_string( $keypair ) ) { |
|
107 | + throw new TypeError( 'Argument 4 must be a string, ' . gettype( $keypair ) . ' given.' ); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | /* Input validation: */ |
111 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
112 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
111 | + if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES ) { |
|
112 | + throw new TypeError( 'Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes' ); |
|
113 | 113 | } |
114 | - if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
115 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
114 | + if ( self::strlen( $keypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) { |
|
115 | + throw new TypeError( 'Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes' ); |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | /** @var int $size */ |
119 | - $size = filesize($inputFile); |
|
120 | - if (!is_int($size)) { |
|
121 | - throw new SodiumException('Could not obtain the file size'); |
|
119 | + $size = filesize( $inputFile ); |
|
120 | + if ( ! is_int( $size ) ) { |
|
121 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | /** @var resource $ifp */ |
125 | - $ifp = fopen($inputFile, 'rb'); |
|
126 | - if (!is_resource($ifp)) { |
|
127 | - throw new SodiumException('Could not open input file for reading'); |
|
125 | + $ifp = fopen( $inputFile, 'rb' ); |
|
126 | + if ( ! is_resource( $ifp ) ) { |
|
127 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
128 | 128 | } |
129 | 129 | |
130 | 130 | /** @var resource $ofp */ |
131 | - $ofp = fopen($outputFile, 'wb'); |
|
132 | - if (!is_resource($ofp)) { |
|
133 | - fclose($ifp); |
|
134 | - throw new SodiumException('Could not open output file for writing'); |
|
131 | + $ofp = fopen( $outputFile, 'wb' ); |
|
132 | + if ( ! is_resource( $ofp ) ) { |
|
133 | + fclose( $ifp ); |
|
134 | + throw new SodiumException( 'Could not open output file for writing' ); |
|
135 | 135 | } |
136 | 136 | |
137 | - $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair); |
|
138 | - fclose($ifp); |
|
139 | - fclose($ofp); |
|
137 | + $res = self::box_decrypt( $ifp, $ofp, $size, $nonce, $keypair ); |
|
138 | + fclose( $ifp ); |
|
139 | + fclose( $ofp ); |
|
140 | 140 | try { |
141 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
142 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
143 | - } catch (SodiumException $ex) { |
|
144 | - if (isset($ephKeypair)) { |
|
145 | - unset($ephKeypair); |
|
141 | + ParagonIE_Sodium_Compat::memzero( $nonce ); |
|
142 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
143 | + } catch ( SodiumException $ex ) { |
|
144 | + if ( isset( $ephKeypair ) ) { |
|
145 | + unset( $ephKeypair ); |
|
146 | 146 | } |
147 | 147 | } |
148 | 148 | return $res; |
@@ -161,41 +161,41 @@ discard block |
||
161 | 161 | * @throws SodiumException |
162 | 162 | * @throws TypeError |
163 | 163 | */ |
164 | - public static function box_seal($inputFile, $outputFile, $publicKey) |
|
164 | + public static function box_seal( $inputFile, $outputFile, $publicKey ) |
|
165 | 165 | { |
166 | 166 | /* Type checks: */ |
167 | - if (!is_string($inputFile)) { |
|
168 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
167 | + if ( ! is_string( $inputFile ) ) { |
|
168 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
169 | 169 | } |
170 | - if (!is_string($outputFile)) { |
|
171 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
170 | + if ( ! is_string( $outputFile ) ) { |
|
171 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
172 | 172 | } |
173 | - if (!is_string($publicKey)) { |
|
174 | - throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
173 | + if ( ! is_string( $publicKey ) ) { |
|
174 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $publicKey ) . ' given.' ); |
|
175 | 175 | } |
176 | 176 | |
177 | 177 | /* Input validation: */ |
178 | - if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
179 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes'); |
|
178 | + if ( self::strlen( $publicKey ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ) { |
|
179 | + throw new TypeError( 'Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes' ); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | /** @var int $size */ |
183 | - $size = filesize($inputFile); |
|
184 | - if (!is_int($size)) { |
|
185 | - throw new SodiumException('Could not obtain the file size'); |
|
183 | + $size = filesize( $inputFile ); |
|
184 | + if ( ! is_int( $size ) ) { |
|
185 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | /** @var resource $ifp */ |
189 | - $ifp = fopen($inputFile, 'rb'); |
|
190 | - if (!is_resource($ifp)) { |
|
191 | - throw new SodiumException('Could not open input file for reading'); |
|
189 | + $ifp = fopen( $inputFile, 'rb' ); |
|
190 | + if ( ! is_resource( $ifp ) ) { |
|
191 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | /** @var resource $ofp */ |
195 | - $ofp = fopen($outputFile, 'wb'); |
|
196 | - if (!is_resource($ofp)) { |
|
197 | - fclose($ifp); |
|
198 | - throw new SodiumException('Could not open output file for writing'); |
|
195 | + $ofp = fopen( $outputFile, 'wb' ); |
|
196 | + if ( ! is_resource( $ofp ) ) { |
|
197 | + fclose( $ifp ); |
|
198 | + throw new SodiumException( 'Could not open output file for writing' ); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** @var string $ephKeypair */ |
@@ -203,12 +203,12 @@ discard block |
||
203 | 203 | |
204 | 204 | /** @var string $msgKeypair */ |
205 | 205 | $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
206 | - ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair), |
|
206 | + ParagonIE_Sodium_Compat::crypto_box_secretkey( $ephKeypair ), |
|
207 | 207 | $publicKey |
208 | 208 | ); |
209 | 209 | |
210 | 210 | /** @var string $ephemeralPK */ |
211 | - $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair); |
|
211 | + $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey( $ephKeypair ); |
|
212 | 212 | |
213 | 213 | /** @var string $nonce */ |
214 | 214 | $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
@@ -223,28 +223,28 @@ discard block |
||
223 | 223 | $ephemeralPK, |
224 | 224 | ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES |
225 | 225 | ); |
226 | - if (!is_int($firstWrite)) { |
|
227 | - fclose($ifp); |
|
228 | - fclose($ofp); |
|
229 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
230 | - throw new SodiumException('Could not write to output file'); |
|
231 | - } |
|
232 | - if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
233 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
234 | - fclose($ifp); |
|
235 | - fclose($ofp); |
|
236 | - throw new SodiumException('Error writing public key to output file'); |
|
237 | - } |
|
238 | - |
|
239 | - $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
240 | - fclose($ifp); |
|
241 | - fclose($ofp); |
|
226 | + if ( ! is_int( $firstWrite ) ) { |
|
227 | + fclose( $ifp ); |
|
228 | + fclose( $ofp ); |
|
229 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
230 | + throw new SodiumException( 'Could not write to output file' ); |
|
231 | + } |
|
232 | + if ( $firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ) { |
|
233 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
234 | + fclose( $ifp ); |
|
235 | + fclose( $ofp ); |
|
236 | + throw new SodiumException( 'Error writing public key to output file' ); |
|
237 | + } |
|
238 | + |
|
239 | + $res = self::box_encrypt( $ifp, $ofp, $size, $nonce, $msgKeypair ); |
|
240 | + fclose( $ifp ); |
|
241 | + fclose( $ofp ); |
|
242 | 242 | try { |
243 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
244 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
245 | - } catch (SodiumException $ex) { |
|
243 | + ParagonIE_Sodium_Compat::memzero( $nonce ); |
|
244 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
245 | + } catch ( SodiumException $ex ) { |
|
246 | 246 | /** @psalm-suppress PossiblyUndefinedVariable */ |
247 | - unset($ephKeypair); |
|
247 | + unset( $ephKeypair ); |
|
248 | 248 | } |
249 | 249 | return $res; |
250 | 250 | } |
@@ -265,53 +265,53 @@ discard block |
||
265 | 265 | * @throws SodiumException |
266 | 266 | * @throws TypeError |
267 | 267 | */ |
268 | - public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) |
|
268 | + public static function box_seal_open( $inputFile, $outputFile, $ecdhKeypair ) |
|
269 | 269 | { |
270 | 270 | /* Type checks: */ |
271 | - if (!is_string($inputFile)) { |
|
272 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
271 | + if ( ! is_string( $inputFile ) ) { |
|
272 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
273 | 273 | } |
274 | - if (!is_string($outputFile)) { |
|
275 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
274 | + if ( ! is_string( $outputFile ) ) { |
|
275 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
276 | 276 | } |
277 | - if (!is_string($ecdhKeypair)) { |
|
278 | - throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.'); |
|
277 | + if ( ! is_string( $ecdhKeypair ) ) { |
|
278 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $ecdhKeypair ) . ' given.' ); |
|
279 | 279 | } |
280 | 280 | |
281 | 281 | /* Input validation: */ |
282 | - if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
283 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
282 | + if ( self::strlen( $ecdhKeypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) { |
|
283 | + throw new TypeError( 'Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes' ); |
|
284 | 284 | } |
285 | 285 | |
286 | - $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair); |
|
286 | + $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey( $ecdhKeypair ); |
|
287 | 287 | |
288 | 288 | /** @var int $size */ |
289 | - $size = filesize($inputFile); |
|
290 | - if (!is_int($size)) { |
|
291 | - throw new SodiumException('Could not obtain the file size'); |
|
289 | + $size = filesize( $inputFile ); |
|
290 | + if ( ! is_int( $size ) ) { |
|
291 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
292 | 292 | } |
293 | 293 | |
294 | 294 | /** @var resource $ifp */ |
295 | - $ifp = fopen($inputFile, 'rb'); |
|
296 | - if (!is_resource($ifp)) { |
|
297 | - throw new SodiumException('Could not open input file for reading'); |
|
295 | + $ifp = fopen( $inputFile, 'rb' ); |
|
296 | + if ( ! is_resource( $ifp ) ) { |
|
297 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | /** @var resource $ofp */ |
301 | - $ofp = fopen($outputFile, 'wb'); |
|
302 | - if (!is_resource($ofp)) { |
|
303 | - fclose($ifp); |
|
304 | - throw new SodiumException('Could not open output file for writing'); |
|
301 | + $ofp = fopen( $outputFile, 'wb' ); |
|
302 | + if ( ! is_resource( $ofp ) ) { |
|
303 | + fclose( $ifp ); |
|
304 | + throw new SodiumException( 'Could not open output file for writing' ); |
|
305 | 305 | } |
306 | 306 | |
307 | - $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES); |
|
308 | - if (!is_string($ephemeralPK)) { |
|
309 | - throw new SodiumException('Could not read input file'); |
|
307 | + $ephemeralPK = fread( $ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ); |
|
308 | + if ( ! is_string( $ephemeralPK ) ) { |
|
309 | + throw new SodiumException( 'Could not read input file' ); |
|
310 | 310 | } |
311 | - if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
312 | - fclose($ifp); |
|
313 | - fclose($ofp); |
|
314 | - throw new SodiumException('Could not read public key from sealed file'); |
|
311 | + if ( self::strlen( $ephemeralPK ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ) { |
|
312 | + fclose( $ifp ); |
|
313 | + fclose( $ofp ); |
|
314 | + throw new SodiumException( 'Could not read public key from sealed file' ); |
|
315 | 315 | } |
316 | 316 | |
317 | 317 | $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
@@ -320,19 +320,19 @@ discard block |
||
320 | 320 | 24 |
321 | 321 | ); |
322 | 322 | $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
323 | - ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair), |
|
323 | + ParagonIE_Sodium_Compat::crypto_box_secretkey( $ecdhKeypair ), |
|
324 | 324 | $ephemeralPK |
325 | 325 | ); |
326 | 326 | |
327 | - $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
328 | - fclose($ifp); |
|
329 | - fclose($ofp); |
|
327 | + $res = self::box_decrypt( $ifp, $ofp, $size, $nonce, $msgKeypair ); |
|
328 | + fclose( $ifp ); |
|
329 | + fclose( $ofp ); |
|
330 | 330 | try { |
331 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
332 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
333 | - } catch (SodiumException $ex) { |
|
334 | - if (isset($ephKeypair)) { |
|
335 | - unset($ephKeypair); |
|
331 | + ParagonIE_Sodium_Compat::memzero( $nonce ); |
|
332 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
333 | + } catch ( SodiumException $ex ) { |
|
334 | + if ( isset( $ephKeypair ) ) { |
|
335 | + unset( $ephKeypair ); |
|
336 | 336 | } |
337 | 337 | } |
338 | 338 | return $res; |
@@ -350,68 +350,68 @@ discard block |
||
350 | 350 | * @throws TypeError |
351 | 351 | * @psalm-suppress FailedTypeResolution |
352 | 352 | */ |
353 | - public static function generichash($filePath, $key = '', $outputLength = 32) |
|
353 | + public static function generichash( $filePath, $key = '', $outputLength = 32 ) |
|
354 | 354 | { |
355 | 355 | /* Type checks: */ |
356 | - if (!is_string($filePath)) { |
|
357 | - throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
356 | + if ( ! is_string( $filePath ) ) { |
|
357 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $filePath ) . ' given.' ); |
|
358 | 358 | } |
359 | - if (!is_string($key)) { |
|
360 | - if (is_null($key)) { |
|
359 | + if ( ! is_string( $key ) ) { |
|
360 | + if ( is_null( $key ) ) { |
|
361 | 361 | $key = ''; |
362 | 362 | } else { |
363 | - throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.'); |
|
363 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $key ) . ' given.' ); |
|
364 | 364 | } |
365 | 365 | } |
366 | - if (!is_int($outputLength)) { |
|
367 | - if (!is_numeric($outputLength)) { |
|
368 | - throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.'); |
|
366 | + if ( ! is_int( $outputLength ) ) { |
|
367 | + if ( ! is_numeric( $outputLength ) ) { |
|
368 | + throw new TypeError( 'Argument 3 must be an integer, ' . gettype( $outputLength ) . ' given.' ); |
|
369 | 369 | } |
370 | - $outputLength = (int) $outputLength; |
|
370 | + $outputLength = (int)$outputLength; |
|
371 | 371 | } |
372 | 372 | |
373 | 373 | /* Input validation: */ |
374 | - if (!empty($key)) { |
|
375 | - if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) { |
|
376 | - throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes'); |
|
374 | + if ( ! empty( $key ) ) { |
|
375 | + if ( self::strlen( $key ) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN ) { |
|
376 | + throw new TypeError( 'Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes' ); |
|
377 | 377 | } |
378 | - if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) { |
|
379 | - throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes'); |
|
378 | + if ( self::strlen( $key ) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX ) { |
|
379 | + throw new TypeError( 'Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes' ); |
|
380 | 380 | } |
381 | 381 | } |
382 | - if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) { |
|
383 | - throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN'); |
|
382 | + if ( $outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN ) { |
|
383 | + throw new SodiumException( 'Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN' ); |
|
384 | 384 | } |
385 | - if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) { |
|
386 | - throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX'); |
|
385 | + if ( $outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX ) { |
|
386 | + throw new SodiumException( 'Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX' ); |
|
387 | 387 | } |
388 | 388 | |
389 | 389 | /** @var int $size */ |
390 | - $size = filesize($filePath); |
|
391 | - if (!is_int($size)) { |
|
392 | - throw new SodiumException('Could not obtain the file size'); |
|
390 | + $size = filesize( $filePath ); |
|
391 | + if ( ! is_int( $size ) ) { |
|
392 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
393 | 393 | } |
394 | 394 | |
395 | 395 | /** @var resource $fp */ |
396 | - $fp = fopen($filePath, 'rb'); |
|
397 | - if (!is_resource($fp)) { |
|
398 | - throw new SodiumException('Could not open input file for reading'); |
|
396 | + $fp = fopen( $filePath, 'rb' ); |
|
397 | + if ( ! is_resource( $fp ) ) { |
|
398 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
399 | 399 | } |
400 | - $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength); |
|
401 | - while ($size > 0) { |
|
400 | + $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init( $key, $outputLength ); |
|
401 | + while ( $size > 0 ) { |
|
402 | 402 | $blockSize = $size > 64 |
403 | 403 | ? 64 |
404 | 404 | : $size; |
405 | - $read = fread($fp, $blockSize); |
|
406 | - if (!is_string($read)) { |
|
407 | - throw new SodiumException('Could not read input file'); |
|
405 | + $read = fread( $fp, $blockSize ); |
|
406 | + if ( ! is_string( $read ) ) { |
|
407 | + throw new SodiumException( 'Could not read input file' ); |
|
408 | 408 | } |
409 | - ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read); |
|
409 | + ParagonIE_Sodium_Compat::crypto_generichash_update( $ctx, $read ); |
|
410 | 410 | $size -= $blockSize; |
411 | 411 | } |
412 | 412 | |
413 | - fclose($fp); |
|
414 | - return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength); |
|
413 | + fclose( $fp ); |
|
414 | + return ParagonIE_Sodium_Compat::crypto_generichash_final( $ctx, $outputLength ); |
|
415 | 415 | } |
416 | 416 | |
417 | 417 | /** |
@@ -428,52 +428,52 @@ discard block |
||
428 | 428 | * @throws SodiumException |
429 | 429 | * @throws TypeError |
430 | 430 | */ |
431 | - public static function secretbox($inputFile, $outputFile, $nonce, $key) |
|
431 | + public static function secretbox( $inputFile, $outputFile, $nonce, $key ) |
|
432 | 432 | { |
433 | 433 | /* Type checks: */ |
434 | - if (!is_string($inputFile)) { |
|
435 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..'); |
|
434 | + if ( ! is_string( $inputFile ) ) { |
|
435 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given..' ); |
|
436 | 436 | } |
437 | - if (!is_string($outputFile)) { |
|
438 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
437 | + if ( ! is_string( $outputFile ) ) { |
|
438 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
439 | 439 | } |
440 | - if (!is_string($nonce)) { |
|
441 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
440 | + if ( ! is_string( $nonce ) ) { |
|
441 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' ); |
|
442 | 442 | } |
443 | 443 | |
444 | 444 | /* Input validation: */ |
445 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
446 | - throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
445 | + if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES ) { |
|
446 | + throw new TypeError( 'Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes' ); |
|
447 | 447 | } |
448 | - if (!is_string($key)) { |
|
449 | - throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
448 | + if ( ! is_string( $key ) ) { |
|
449 | + throw new TypeError( 'Argument 4 must be a string, ' . gettype( $key ) . ' given.' ); |
|
450 | 450 | } |
451 | - if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
452 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes'); |
|
451 | + if ( self::strlen( $key ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES ) { |
|
452 | + throw new TypeError( 'Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes' ); |
|
453 | 453 | } |
454 | 454 | |
455 | 455 | /** @var int $size */ |
456 | - $size = filesize($inputFile); |
|
457 | - if (!is_int($size)) { |
|
458 | - throw new SodiumException('Could not obtain the file size'); |
|
456 | + $size = filesize( $inputFile ); |
|
457 | + if ( ! is_int( $size ) ) { |
|
458 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
459 | 459 | } |
460 | 460 | |
461 | 461 | /** @var resource $ifp */ |
462 | - $ifp = fopen($inputFile, 'rb'); |
|
463 | - if (!is_resource($ifp)) { |
|
464 | - throw new SodiumException('Could not open input file for reading'); |
|
462 | + $ifp = fopen( $inputFile, 'rb' ); |
|
463 | + if ( ! is_resource( $ifp ) ) { |
|
464 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
465 | 465 | } |
466 | 466 | |
467 | 467 | /** @var resource $ofp */ |
468 | - $ofp = fopen($outputFile, 'wb'); |
|
469 | - if (!is_resource($ofp)) { |
|
470 | - fclose($ifp); |
|
471 | - throw new SodiumException('Could not open output file for writing'); |
|
468 | + $ofp = fopen( $outputFile, 'wb' ); |
|
469 | + if ( ! is_resource( $ofp ) ) { |
|
470 | + fclose( $ifp ); |
|
471 | + throw new SodiumException( 'Could not open output file for writing' ); |
|
472 | 472 | } |
473 | 473 | |
474 | - $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key); |
|
475 | - fclose($ifp); |
|
476 | - fclose($ofp); |
|
474 | + $res = self::secretbox_encrypt( $ifp, $ofp, $size, $nonce, $key ); |
|
475 | + fclose( $ifp ); |
|
476 | + fclose( $ofp ); |
|
477 | 477 | return $res; |
478 | 478 | } |
479 | 479 | /** |
@@ -493,57 +493,57 @@ discard block |
||
493 | 493 | * @throws SodiumException |
494 | 494 | * @throws TypeError |
495 | 495 | */ |
496 | - public static function secretbox_open($inputFile, $outputFile, $nonce, $key) |
|
496 | + public static function secretbox_open( $inputFile, $outputFile, $nonce, $key ) |
|
497 | 497 | { |
498 | 498 | /* Type checks: */ |
499 | - if (!is_string($inputFile)) { |
|
500 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
499 | + if ( ! is_string( $inputFile ) ) { |
|
500 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
501 | 501 | } |
502 | - if (!is_string($outputFile)) { |
|
503 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
502 | + if ( ! is_string( $outputFile ) ) { |
|
503 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
504 | 504 | } |
505 | - if (!is_string($nonce)) { |
|
506 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
505 | + if ( ! is_string( $nonce ) ) { |
|
506 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' ); |
|
507 | 507 | } |
508 | - if (!is_string($key)) { |
|
509 | - throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
508 | + if ( ! is_string( $key ) ) { |
|
509 | + throw new TypeError( 'Argument 4 must be a string, ' . gettype( $key ) . ' given.' ); |
|
510 | 510 | } |
511 | 511 | |
512 | 512 | /* Input validation: */ |
513 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
514 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
513 | + if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES ) { |
|
514 | + throw new TypeError( 'Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes' ); |
|
515 | 515 | } |
516 | - if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
517 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes'); |
|
516 | + if ( self::strlen( $key ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES ) { |
|
517 | + throw new TypeError( 'Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes' ); |
|
518 | 518 | } |
519 | 519 | |
520 | 520 | /** @var int $size */ |
521 | - $size = filesize($inputFile); |
|
522 | - if (!is_int($size)) { |
|
523 | - throw new SodiumException('Could not obtain the file size'); |
|
521 | + $size = filesize( $inputFile ); |
|
522 | + if ( ! is_int( $size ) ) { |
|
523 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
524 | 524 | } |
525 | 525 | |
526 | 526 | /** @var resource $ifp */ |
527 | - $ifp = fopen($inputFile, 'rb'); |
|
528 | - if (!is_resource($ifp)) { |
|
529 | - throw new SodiumException('Could not open input file for reading'); |
|
527 | + $ifp = fopen( $inputFile, 'rb' ); |
|
528 | + if ( ! is_resource( $ifp ) ) { |
|
529 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
530 | 530 | } |
531 | 531 | |
532 | 532 | /** @var resource $ofp */ |
533 | - $ofp = fopen($outputFile, 'wb'); |
|
534 | - if (!is_resource($ofp)) { |
|
535 | - fclose($ifp); |
|
536 | - throw new SodiumException('Could not open output file for writing'); |
|
533 | + $ofp = fopen( $outputFile, 'wb' ); |
|
534 | + if ( ! is_resource( $ofp ) ) { |
|
535 | + fclose( $ifp ); |
|
536 | + throw new SodiumException( 'Could not open output file for writing' ); |
|
537 | 537 | } |
538 | 538 | |
539 | - $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key); |
|
540 | - fclose($ifp); |
|
541 | - fclose($ofp); |
|
539 | + $res = self::secretbox_decrypt( $ifp, $ofp, $size, $nonce, $key ); |
|
540 | + fclose( $ifp ); |
|
541 | + fclose( $ofp ); |
|
542 | 542 | try { |
543 | - ParagonIE_Sodium_Compat::memzero($key); |
|
544 | - } catch (SodiumException $ex) { |
|
543 | + ParagonIE_Sodium_Compat::memzero( $key ); |
|
544 | + } catch ( SodiumException $ex ) { |
|
545 | 545 | /** @psalm-suppress PossiblyUndefinedVariable */ |
546 | - unset($key); |
|
546 | + unset( $key ); |
|
547 | 547 | } |
548 | 548 | return $res; |
549 | 549 | } |
@@ -560,85 +560,85 @@ discard block |
||
560 | 560 | * @throws SodiumException |
561 | 561 | * @throws TypeError |
562 | 562 | */ |
563 | - public static function sign($filePath, $secretKey) |
|
563 | + public static function sign( $filePath, $secretKey ) |
|
564 | 564 | { |
565 | 565 | /* Type checks: */ |
566 | - if (!is_string($filePath)) { |
|
567 | - throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
566 | + if ( ! is_string( $filePath ) ) { |
|
567 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $filePath ) . ' given.' ); |
|
568 | 568 | } |
569 | - if (!is_string($secretKey)) { |
|
570 | - throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.'); |
|
569 | + if ( ! is_string( $secretKey ) ) { |
|
570 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $secretKey ) . ' given.' ); |
|
571 | 571 | } |
572 | 572 | |
573 | 573 | /* Input validation: */ |
574 | - if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) { |
|
575 | - throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes'); |
|
574 | + if ( self::strlen( $secretKey ) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES ) { |
|
575 | + throw new TypeError( 'Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes' ); |
|
576 | 576 | } |
577 | - if (PHP_INT_SIZE === 4) { |
|
578 | - return self::sign_core32($filePath, $secretKey); |
|
577 | + if ( PHP_INT_SIZE === 4 ) { |
|
578 | + return self::sign_core32( $filePath, $secretKey ); |
|
579 | 579 | } |
580 | 580 | |
581 | 581 | /** @var int $size */ |
582 | - $size = filesize($filePath); |
|
583 | - if (!is_int($size)) { |
|
584 | - throw new SodiumException('Could not obtain the file size'); |
|
582 | + $size = filesize( $filePath ); |
|
583 | + if ( ! is_int( $size ) ) { |
|
584 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
585 | 585 | } |
586 | 586 | |
587 | 587 | /** @var resource $fp */ |
588 | - $fp = fopen($filePath, 'rb'); |
|
589 | - if (!is_resource($fp)) { |
|
590 | - throw new SodiumException('Could not open input file for reading'); |
|
588 | + $fp = fopen( $filePath, 'rb' ); |
|
589 | + if ( ! is_resource( $fp ) ) { |
|
590 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
591 | 591 | } |
592 | 592 | |
593 | 593 | /** @var string $az */ |
594 | - $az = hash('sha512', self::substr($secretKey, 0, 32), true); |
|
594 | + $az = hash( 'sha512', self::substr( $secretKey, 0, 32 ), true ); |
|
595 | 595 | |
596 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
597 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
596 | + $az[ 0 ] = self::intToChr( self::chrToInt( $az[ 0 ] ) & 248 ); |
|
597 | + $az[ 31 ] = self::intToChr( ( self::chrToInt( $az[ 31 ] ) & 63 ) | 64 ); |
|
598 | 598 | |
599 | - $hs = hash_init('sha512'); |
|
600 | - self::hash_update($hs, self::substr($az, 32, 32)); |
|
599 | + $hs = hash_init( 'sha512' ); |
|
600 | + self::hash_update( $hs, self::substr( $az, 32, 32 ) ); |
|
601 | 601 | /** @var resource $hs */ |
602 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
602 | + $hs = self::updateHashWithFile( $hs, $fp, $size ); |
|
603 | 603 | |
604 | 604 | /** @var string $nonceHash */ |
605 | - $nonceHash = hash_final($hs, true); |
|
605 | + $nonceHash = hash_final( $hs, true ); |
|
606 | 606 | |
607 | 607 | /** @var string $pk */ |
608 | - $pk = self::substr($secretKey, 32, 32); |
|
608 | + $pk = self::substr( $secretKey, 32, 32 ); |
|
609 | 609 | |
610 | 610 | /** @var string $nonce */ |
611 | - $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
611 | + $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 ); |
|
612 | 612 | |
613 | 613 | /** @var string $sig */ |
614 | 614 | $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes( |
615 | - ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce) |
|
615 | + ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base( $nonce ) |
|
616 | 616 | ); |
617 | 617 | |
618 | - $hs = hash_init('sha512'); |
|
619 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
620 | - self::hash_update($hs, self::substr($pk, 0, 32)); |
|
618 | + $hs = hash_init( 'sha512' ); |
|
619 | + self::hash_update( $hs, self::substr( $sig, 0, 32 ) ); |
|
620 | + self::hash_update( $hs, self::substr( $pk, 0, 32 ) ); |
|
621 | 621 | /** @var resource $hs */ |
622 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
622 | + $hs = self::updateHashWithFile( $hs, $fp, $size ); |
|
623 | 623 | |
624 | 624 | /** @var string $hramHash */ |
625 | - $hramHash = hash_final($hs, true); |
|
625 | + $hramHash = hash_final( $hs, true ); |
|
626 | 626 | |
627 | 627 | /** @var string $hram */ |
628 | - $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash); |
|
628 | + $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce( $hramHash ); |
|
629 | 629 | |
630 | 630 | /** @var string $sigAfter */ |
631 | - $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce); |
|
631 | + $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd( $hram, $az, $nonce ); |
|
632 | 632 | |
633 | 633 | /** @var string $sig */ |
634 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
634 | + $sig = self::substr( $sig, 0, 32 ) . self::substr( $sigAfter, 0, 32 ); |
|
635 | 635 | |
636 | 636 | try { |
637 | - ParagonIE_Sodium_Compat::memzero($az); |
|
638 | - } catch (SodiumException $ex) { |
|
637 | + ParagonIE_Sodium_Compat::memzero( $az ); |
|
638 | + } catch ( SodiumException $ex ) { |
|
639 | 639 | $az = null; |
640 | 640 | } |
641 | - fclose($fp); |
|
641 | + fclose( $fp ); |
|
642 | 642 | return $sig; |
643 | 643 | } |
644 | 644 | |
@@ -656,66 +656,66 @@ discard block |
||
656 | 656 | * @throws TypeError |
657 | 657 | * @throws Exception |
658 | 658 | */ |
659 | - public static function verify($sig, $filePath, $publicKey) |
|
659 | + public static function verify( $sig, $filePath, $publicKey ) |
|
660 | 660 | { |
661 | 661 | /* Type checks: */ |
662 | - if (!is_string($sig)) { |
|
663 | - throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.'); |
|
662 | + if ( ! is_string( $sig ) ) { |
|
663 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $sig ) . ' given.' ); |
|
664 | 664 | } |
665 | - if (!is_string($filePath)) { |
|
666 | - throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.'); |
|
665 | + if ( ! is_string( $filePath ) ) { |
|
666 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $filePath ) . ' given.' ); |
|
667 | 667 | } |
668 | - if (!is_string($publicKey)) { |
|
669 | - throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
668 | + if ( ! is_string( $publicKey ) ) { |
|
669 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $publicKey ) . ' given.' ); |
|
670 | 670 | } |
671 | 671 | |
672 | 672 | /* Input validation: */ |
673 | - if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) { |
|
674 | - throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes'); |
|
673 | + if ( self::strlen( $sig ) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES ) { |
|
674 | + throw new TypeError( 'Argument 1 must be CRYPTO_SIGN_BYTES bytes' ); |
|
675 | 675 | } |
676 | - if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) { |
|
677 | - throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes'); |
|
676 | + if ( self::strlen( $publicKey ) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES ) { |
|
677 | + throw new TypeError( 'Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes' ); |
|
678 | 678 | } |
679 | - if (self::strlen($sig) < 64) { |
|
680 | - throw new SodiumException('Signature is too short'); |
|
679 | + if ( self::strlen( $sig ) < 64 ) { |
|
680 | + throw new SodiumException( 'Signature is too short' ); |
|
681 | 681 | } |
682 | 682 | |
683 | - if (PHP_INT_SIZE === 4) { |
|
684 | - return self::verify_core32($sig, $filePath, $publicKey); |
|
683 | + if ( PHP_INT_SIZE === 4 ) { |
|
684 | + return self::verify_core32( $sig, $filePath, $publicKey ); |
|
685 | 685 | } |
686 | 686 | |
687 | 687 | /* Security checks */ |
688 | 688 | if ( |
689 | - (ParagonIE_Sodium_Core_Ed25519::chrToInt($sig[63]) & 240) |
|
689 | + ( ParagonIE_Sodium_Core_Ed25519::chrToInt( $sig[ 63 ] ) & 240 ) |
|
690 | 690 | && |
691 | - ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32)) |
|
691 | + ParagonIE_Sodium_Core_Ed25519::check_S_lt_L( self::substr( $sig, 32, 32 ) ) |
|
692 | 692 | ) { |
693 | - throw new SodiumException('S < L - Invalid signature'); |
|
693 | + throw new SodiumException( 'S < L - Invalid signature' ); |
|
694 | 694 | } |
695 | - if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) { |
|
696 | - throw new SodiumException('Signature is on too small of an order'); |
|
695 | + if ( ParagonIE_Sodium_Core_Ed25519::small_order( $sig ) ) { |
|
696 | + throw new SodiumException( 'Signature is on too small of an order' ); |
|
697 | 697 | } |
698 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
699 | - throw new SodiumException('Invalid signature'); |
|
698 | + if ( ( self::chrToInt( $sig[ 63 ] ) & 224 ) !== 0 ) { |
|
699 | + throw new SodiumException( 'Invalid signature' ); |
|
700 | 700 | } |
701 | 701 | $d = 0; |
702 | - for ($i = 0; $i < 32; ++$i) { |
|
703 | - $d |= self::chrToInt($publicKey[$i]); |
|
702 | + for ( $i = 0; $i < 32; ++$i ) { |
|
703 | + $d |= self::chrToInt( $publicKey[ $i ] ); |
|
704 | 704 | } |
705 | - if ($d === 0) { |
|
706 | - throw new SodiumException('All zero public key'); |
|
705 | + if ( $d === 0 ) { |
|
706 | + throw new SodiumException( 'All zero public key' ); |
|
707 | 707 | } |
708 | 708 | |
709 | 709 | /** @var int $size */ |
710 | - $size = filesize($filePath); |
|
711 | - if (!is_int($size)) { |
|
712 | - throw new SodiumException('Could not obtain the file size'); |
|
710 | + $size = filesize( $filePath ); |
|
711 | + if ( ! is_int( $size ) ) { |
|
712 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
713 | 713 | } |
714 | 714 | |
715 | 715 | /** @var resource $fp */ |
716 | - $fp = fopen($filePath, 'rb'); |
|
717 | - if (!is_resource($fp)) { |
|
718 | - throw new SodiumException('Could not open input file for reading'); |
|
716 | + $fp = fopen( $filePath, 'rb' ); |
|
717 | + if ( ! is_resource( $fp ) ) { |
|
718 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
719 | 719 | } |
720 | 720 | |
721 | 721 | /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
@@ -725,35 +725,35 @@ discard block |
||
725 | 725 | ParagonIE_Sodium_Compat::$fastMult = true; |
726 | 726 | |
727 | 727 | /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
728 | - $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey); |
|
728 | + $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime( $publicKey ); |
|
729 | 729 | |
730 | - $hs = hash_init('sha512'); |
|
731 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
732 | - self::hash_update($hs, self::substr($publicKey, 0, 32)); |
|
730 | + $hs = hash_init( 'sha512' ); |
|
731 | + self::hash_update( $hs, self::substr( $sig, 0, 32 ) ); |
|
732 | + self::hash_update( $hs, self::substr( $publicKey, 0, 32 ) ); |
|
733 | 733 | /** @var resource $hs */ |
734 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
734 | + $hs = self::updateHashWithFile( $hs, $fp, $size ); |
|
735 | 735 | /** @var string $hDigest */ |
736 | - $hDigest = hash_final($hs, true); |
|
736 | + $hDigest = hash_final( $hs, true ); |
|
737 | 737 | |
738 | 738 | /** @var string $h */ |
739 | - $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
739 | + $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 ); |
|
740 | 740 | |
741 | 741 | /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ |
742 | 742 | $R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime( |
743 | 743 | $h, |
744 | 744 | $A, |
745 | - self::substr($sig, 32) |
|
745 | + self::substr( $sig, 32 ) |
|
746 | 746 | ); |
747 | 747 | |
748 | 748 | /** @var string $rcheck */ |
749 | - $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R); |
|
749 | + $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes( $R ); |
|
750 | 750 | |
751 | 751 | // Close the file handle |
752 | - fclose($fp); |
|
752 | + fclose( $fp ); |
|
753 | 753 | |
754 | 754 | // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
755 | 755 | ParagonIE_Sodium_Compat::$fastMult = $orig; |
756 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
756 | + return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) ); |
|
757 | 757 | } |
758 | 758 | |
759 | 759 | /** |
@@ -766,17 +766,17 @@ discard block |
||
766 | 766 | * @throws SodiumException |
767 | 767 | * @throws TypeError |
768 | 768 | */ |
769 | - protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
769 | + protected static function box_encrypt( $ifp, $ofp, $mlen, $nonce, $boxKeypair ) |
|
770 | 770 | { |
771 | - if (PHP_INT_SIZE === 4) { |
|
771 | + if ( PHP_INT_SIZE === 4 ) { |
|
772 | 772 | return self::secretbox_encrypt( |
773 | 773 | $ifp, |
774 | 774 | $ofp, |
775 | 775 | $mlen, |
776 | 776 | $nonce, |
777 | 777 | ParagonIE_Sodium_Crypto32::box_beforenm( |
778 | - ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), |
|
779 | - ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) |
|
778 | + ParagonIE_Sodium_Crypto32::box_secretkey( $boxKeypair ), |
|
779 | + ParagonIE_Sodium_Crypto32::box_publickey( $boxKeypair ) |
|
780 | 780 | ) |
781 | 781 | ); |
782 | 782 | } |
@@ -786,8 +786,8 @@ discard block |
||
786 | 786 | $mlen, |
787 | 787 | $nonce, |
788 | 788 | ParagonIE_Sodium_Crypto::box_beforenm( |
789 | - ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
790 | - ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
789 | + ParagonIE_Sodium_Crypto::box_secretkey( $boxKeypair ), |
|
790 | + ParagonIE_Sodium_Crypto::box_publickey( $boxKeypair ) |
|
791 | 791 | ) |
792 | 792 | ); |
793 | 793 | } |
@@ -803,17 +803,17 @@ discard block |
||
803 | 803 | * @throws SodiumException |
804 | 804 | * @throws TypeError |
805 | 805 | */ |
806 | - protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
806 | + protected static function box_decrypt( $ifp, $ofp, $mlen, $nonce, $boxKeypair ) |
|
807 | 807 | { |
808 | - if (PHP_INT_SIZE === 4) { |
|
808 | + if ( PHP_INT_SIZE === 4 ) { |
|
809 | 809 | return self::secretbox_decrypt( |
810 | 810 | $ifp, |
811 | 811 | $ofp, |
812 | 812 | $mlen, |
813 | 813 | $nonce, |
814 | 814 | ParagonIE_Sodium_Crypto32::box_beforenm( |
815 | - ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), |
|
816 | - ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) |
|
815 | + ParagonIE_Sodium_Crypto32::box_secretkey( $boxKeypair ), |
|
816 | + ParagonIE_Sodium_Crypto32::box_publickey( $boxKeypair ) |
|
817 | 817 | ) |
818 | 818 | ); |
819 | 819 | } |
@@ -823,8 +823,8 @@ discard block |
||
823 | 823 | $mlen, |
824 | 824 | $nonce, |
825 | 825 | ParagonIE_Sodium_Crypto::box_beforenm( |
826 | - ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
827 | - ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
826 | + ParagonIE_Sodium_Crypto::box_secretkey( $boxKeypair ), |
|
827 | + ParagonIE_Sodium_Crypto::box_publickey( $boxKeypair ) |
|
828 | 828 | ) |
829 | 829 | ); |
830 | 830 | } |
@@ -841,33 +841,33 @@ discard block |
||
841 | 841 | * @throws SodiumException |
842 | 842 | * @throws TypeError |
843 | 843 | */ |
844 | - protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
844 | + protected static function secretbox_encrypt( $ifp, $ofp, $mlen, $nonce, $key ) |
|
845 | 845 | { |
846 | - if (PHP_INT_SIZE === 4) { |
|
847 | - return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key); |
|
846 | + if ( PHP_INT_SIZE === 4 ) { |
|
847 | + return self::secretbox_encrypt_core32( $ifp, $ofp, $mlen, $nonce, $key ); |
|
848 | 848 | } |
849 | 849 | |
850 | - $plaintext = fread($ifp, 32); |
|
851 | - if (!is_string($plaintext)) { |
|
852 | - throw new SodiumException('Could not read input file'); |
|
850 | + $plaintext = fread( $ifp, 32 ); |
|
851 | + if ( ! is_string( $plaintext ) ) { |
|
852 | + throw new SodiumException( 'Could not read input file' ); |
|
853 | 853 | } |
854 | - $first32 = self::ftell($ifp); |
|
854 | + $first32 = self::ftell( $ifp ); |
|
855 | 855 | |
856 | 856 | /** @var string $subkey */ |
857 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
857 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key ); |
|
858 | 858 | |
859 | 859 | /** @var string $realNonce */ |
860 | - $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
860 | + $realNonce = ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ); |
|
861 | 861 | |
862 | 862 | /** @var string $block0 */ |
863 | - $block0 = str_repeat("\x00", 32); |
|
863 | + $block0 = str_repeat( "\x00", 32 ); |
|
864 | 864 | |
865 | 865 | /** @var int $mlen - Length of the plaintext message */ |
866 | 866 | $mlen0 = $mlen; |
867 | - if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
867 | + if ( $mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES ) { |
|
868 | 868 | $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; |
869 | 869 | } |
870 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
870 | + $block0 .= ParagonIE_Sodium_Core_Util::substr( $plaintext, 0, $mlen0 ); |
|
871 | 871 | |
872 | 872 | /** @var string $block0 */ |
873 | 873 | $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
@@ -885,16 +885,16 @@ discard block |
||
885 | 885 | ); |
886 | 886 | |
887 | 887 | // Pre-write 16 blank bytes for the Poly1305 tag |
888 | - $start = self::ftell($ofp); |
|
889 | - fwrite($ofp, str_repeat("\x00", 16)); |
|
888 | + $start = self::ftell( $ofp ); |
|
889 | + fwrite( $ofp, str_repeat( "\x00", 16 ) ); |
|
890 | 890 | |
891 | 891 | /** @var string $c */ |
892 | 892 | $cBlock = ParagonIE_Sodium_Core_Util::substr( |
893 | 893 | $block0, |
894 | 894 | ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES |
895 | 895 | ); |
896 | - $state->update($cBlock); |
|
897 | - fwrite($ofp, $cBlock); |
|
896 | + $state->update( $cBlock ); |
|
897 | + fwrite( $ofp, $cBlock ); |
|
898 | 898 | $mlen -= 32; |
899 | 899 | |
900 | 900 | /** @var int $iter */ |
@@ -907,15 +907,15 @@ discard block |
||
907 | 907 | * Set the cursor to the end of the first half-block. All future bytes will |
908 | 908 | * generated from salsa20_xor_ic, starting from 1 (second block). |
909 | 909 | */ |
910 | - fseek($ifp, $first32, SEEK_SET); |
|
910 | + fseek( $ifp, $first32, SEEK_SET ); |
|
911 | 911 | |
912 | - while ($mlen > 0) { |
|
912 | + while ( $mlen > 0 ) { |
|
913 | 913 | $blockSize = $mlen > self::BUFFER_SIZE |
914 | 914 | ? self::BUFFER_SIZE |
915 | 915 | : $mlen; |
916 | - $plaintext = fread($ifp, $blockSize); |
|
917 | - if (!is_string($plaintext)) { |
|
918 | - throw new SodiumException('Could not read input file'); |
|
916 | + $plaintext = fread( $ifp, $blockSize ); |
|
917 | + if ( ! is_string( $plaintext ) ) { |
|
918 | + throw new SodiumException( 'Could not read input file' ); |
|
919 | 919 | } |
920 | 920 | $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
921 | 921 | $plaintext, |
@@ -923,29 +923,29 @@ discard block |
||
923 | 923 | $iter, |
924 | 924 | $subkey |
925 | 925 | ); |
926 | - fwrite($ofp, $cBlock, $blockSize); |
|
927 | - $state->update($cBlock); |
|
926 | + fwrite( $ofp, $cBlock, $blockSize ); |
|
927 | + $state->update( $cBlock ); |
|
928 | 928 | |
929 | 929 | $mlen -= $blockSize; |
930 | 930 | $iter += $incr; |
931 | 931 | } |
932 | 932 | try { |
933 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
934 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
935 | - } catch (SodiumException $ex) { |
|
933 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
934 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
935 | + } catch ( SodiumException $ex ) { |
|
936 | 936 | $block0 = null; |
937 | 937 | $subkey = null; |
938 | 938 | } |
939 | - $end = self::ftell($ofp); |
|
939 | + $end = self::ftell( $ofp ); |
|
940 | 940 | |
941 | 941 | /* |
942 | 942 | * Write the Poly1305 authentication tag that provides integrity |
943 | 943 | * over the ciphertext (encrypt-then-MAC) |
944 | 944 | */ |
945 | - fseek($ofp, $start, SEEK_SET); |
|
946 | - fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); |
|
947 | - fseek($ofp, $end, SEEK_SET); |
|
948 | - unset($state); |
|
945 | + fseek( $ofp, $start, SEEK_SET ); |
|
946 | + fwrite( $ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES ); |
|
947 | + fseek( $ofp, $end, SEEK_SET ); |
|
948 | + unset( $state ); |
|
949 | 949 | |
950 | 950 | return true; |
951 | 951 | } |
@@ -962,49 +962,49 @@ discard block |
||
962 | 962 | * @throws SodiumException |
963 | 963 | * @throws TypeError |
964 | 964 | */ |
965 | - protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
965 | + protected static function secretbox_decrypt( $ifp, $ofp, $mlen, $nonce, $key ) |
|
966 | 966 | { |
967 | - if (PHP_INT_SIZE === 4) { |
|
968 | - return self::secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key); |
|
967 | + if ( PHP_INT_SIZE === 4 ) { |
|
968 | + return self::secretbox_decrypt_core32( $ifp, $ofp, $mlen, $nonce, $key ); |
|
969 | 969 | } |
970 | - $tag = fread($ifp, 16); |
|
971 | - if (!is_string($tag)) { |
|
972 | - throw new SodiumException('Could not read input file'); |
|
970 | + $tag = fread( $ifp, 16 ); |
|
971 | + if ( ! is_string( $tag ) ) { |
|
972 | + throw new SodiumException( 'Could not read input file' ); |
|
973 | 973 | } |
974 | 974 | |
975 | 975 | /** @var string $subkey */ |
976 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
976 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key ); |
|
977 | 977 | |
978 | 978 | /** @var string $realNonce */ |
979 | - $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
979 | + $realNonce = ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ); |
|
980 | 980 | |
981 | 981 | /** @var string $block0 */ |
982 | 982 | $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
983 | 983 | 64, |
984 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
984 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
985 | 985 | $subkey |
986 | 986 | ); |
987 | 987 | |
988 | 988 | /* Verify the Poly1305 MAC -before- attempting to decrypt! */ |
989 | - $state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32)); |
|
990 | - if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) { |
|
991 | - throw new SodiumException('Invalid MAC'); |
|
989 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( self::substr( $block0, 0, 32 ) ); |
|
990 | + if ( ! self::onetimeauth_verify( $state, $ifp, $tag, $mlen ) ) { |
|
991 | + throw new SodiumException( 'Invalid MAC' ); |
|
992 | 992 | } |
993 | 993 | |
994 | 994 | /* |
995 | 995 | * Set the cursor to the end of the first half-block. All future bytes will |
996 | 996 | * generated from salsa20_xor_ic, starting from 1 (second block). |
997 | 997 | */ |
998 | - $first32 = fread($ifp, 32); |
|
999 | - if (!is_string($first32)) { |
|
1000 | - throw new SodiumException('Could not read input file'); |
|
998 | + $first32 = fread( $ifp, 32 ); |
|
999 | + if ( ! is_string( $first32 ) ) { |
|
1000 | + throw new SodiumException( 'Could not read input file' ); |
|
1001 | 1001 | } |
1002 | - $first32len = self::strlen($first32); |
|
1002 | + $first32len = self::strlen( $first32 ); |
|
1003 | 1003 | fwrite( |
1004 | 1004 | $ofp, |
1005 | 1005 | self::xorStrings( |
1006 | - self::substr($block0, 32, $first32len), |
|
1007 | - self::substr($first32, 0, $first32len) |
|
1006 | + self::substr( $block0, 32, $first32len ), |
|
1007 | + self::substr( $first32, 0, $first32len ) |
|
1008 | 1008 | ) |
1009 | 1009 | ); |
1010 | 1010 | $mlen -= 32; |
@@ -1016,13 +1016,13 @@ discard block |
||
1016 | 1016 | $incr = self::BUFFER_SIZE >> 6; |
1017 | 1017 | |
1018 | 1018 | /* Decrypts ciphertext, writes to output file. */ |
1019 | - while ($mlen > 0) { |
|
1019 | + while ( $mlen > 0 ) { |
|
1020 | 1020 | $blockSize = $mlen > self::BUFFER_SIZE |
1021 | 1021 | ? self::BUFFER_SIZE |
1022 | 1022 | : $mlen; |
1023 | - $ciphertext = fread($ifp, $blockSize); |
|
1024 | - if (!is_string($ciphertext)) { |
|
1025 | - throw new SodiumException('Could not read input file'); |
|
1023 | + $ciphertext = fread( $ifp, $blockSize ); |
|
1024 | + if ( ! is_string( $ciphertext ) ) { |
|
1025 | + throw new SodiumException( 'Could not read input file' ); |
|
1026 | 1026 | } |
1027 | 1027 | $pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
1028 | 1028 | $ciphertext, |
@@ -1030,7 +1030,7 @@ discard block |
||
1030 | 1030 | $iter, |
1031 | 1031 | $subkey |
1032 | 1032 | ); |
1033 | - fwrite($ofp, $pBlock, $blockSize); |
|
1033 | + fwrite( $ofp, $pBlock, $blockSize ); |
|
1034 | 1034 | $mlen -= $blockSize; |
1035 | 1035 | $iter += $incr; |
1036 | 1036 | } |
@@ -1053,7 +1053,7 @@ discard block |
||
1053 | 1053 | $mlen = 0 |
1054 | 1054 | ) { |
1055 | 1055 | /** @var int $pos */ |
1056 | - $pos = self::ftell($ifp); |
|
1056 | + $pos = self::ftell( $ifp ); |
|
1057 | 1057 | |
1058 | 1058 | /** @var int $iter */ |
1059 | 1059 | $iter = 1; |
@@ -1061,21 +1061,21 @@ discard block |
||
1061 | 1061 | /** @var int $incr */ |
1062 | 1062 | $incr = self::BUFFER_SIZE >> 6; |
1063 | 1063 | |
1064 | - while ($mlen > 0) { |
|
1064 | + while ( $mlen > 0 ) { |
|
1065 | 1065 | $blockSize = $mlen > self::BUFFER_SIZE |
1066 | 1066 | ? self::BUFFER_SIZE |
1067 | 1067 | : $mlen; |
1068 | - $ciphertext = fread($ifp, $blockSize); |
|
1069 | - if (!is_string($ciphertext)) { |
|
1070 | - throw new SodiumException('Could not read input file'); |
|
1068 | + $ciphertext = fread( $ifp, $blockSize ); |
|
1069 | + if ( ! is_string( $ciphertext ) ) { |
|
1070 | + throw new SodiumException( 'Could not read input file' ); |
|
1071 | 1071 | } |
1072 | - $state->update($ciphertext); |
|
1072 | + $state->update( $ciphertext ); |
|
1073 | 1073 | $mlen -= $blockSize; |
1074 | 1074 | $iter += $incr; |
1075 | 1075 | } |
1076 | - $res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish()); |
|
1076 | + $res = ParagonIE_Sodium_Core_Util::verify_16( $tag, $state->finish() ); |
|
1077 | 1077 | |
1078 | - fseek($ifp, $pos, SEEK_SET); |
|
1078 | + fseek( $ifp, $pos, SEEK_SET ); |
|
1079 | 1079 | return $res; |
1080 | 1080 | } |
1081 | 1081 | |
@@ -1095,48 +1095,48 @@ discard block |
||
1095 | 1095 | * @psalm-suppress TypeCoercion |
1096 | 1096 | * Ditto. |
1097 | 1097 | */ |
1098 | - public static function updateHashWithFile($hash, $fp, $size = 0) |
|
1098 | + public static function updateHashWithFile( $hash, $fp, $size = 0 ) |
|
1099 | 1099 | { |
1100 | 1100 | /* Type checks: */ |
1101 | - if (PHP_VERSION_ID < 70200) { |
|
1102 | - if (!is_resource($hash)) { |
|
1103 | - throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.'); |
|
1101 | + if ( PHP_VERSION_ID < 70200 ) { |
|
1102 | + if ( ! is_resource( $hash ) ) { |
|
1103 | + throw new TypeError( 'Argument 1 must be a resource, ' . gettype( $hash ) . ' given.' ); |
|
1104 | 1104 | } |
1105 | 1105 | } else { |
1106 | - if (!is_object($hash)) { |
|
1107 | - throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.'); |
|
1106 | + if ( ! is_object( $hash ) ) { |
|
1107 | + throw new TypeError( 'Argument 1 must be an object (PHP 7.2+), ' . gettype( $hash ) . ' given.' ); |
|
1108 | 1108 | } |
1109 | 1109 | } |
1110 | 1110 | |
1111 | - if (!is_resource($fp)) { |
|
1112 | - throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.'); |
|
1111 | + if ( ! is_resource( $fp ) ) { |
|
1112 | + throw new TypeError( 'Argument 2 must be a resource, ' . gettype( $fp ) . ' given.' ); |
|
1113 | 1113 | } |
1114 | - if (!is_int($size)) { |
|
1115 | - throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.'); |
|
1114 | + if ( ! is_int( $size ) ) { |
|
1115 | + throw new TypeError( 'Argument 3 must be an integer, ' . gettype( $size ) . ' given.' ); |
|
1116 | 1116 | } |
1117 | 1117 | |
1118 | 1118 | /** @var int $originalPosition */ |
1119 | - $originalPosition = self::ftell($fp); |
|
1119 | + $originalPosition = self::ftell( $fp ); |
|
1120 | 1120 | |
1121 | 1121 | // Move file pointer to beginning of file |
1122 | - fseek($fp, 0, SEEK_SET); |
|
1123 | - for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) { |
|
1122 | + fseek( $fp, 0, SEEK_SET ); |
|
1123 | + for ( $i = 0; $i < $size; $i += self::BUFFER_SIZE ) { |
|
1124 | 1124 | /** @var string|bool $message */ |
1125 | 1125 | $message = fread( |
1126 | 1126 | $fp, |
1127 | - ($size - $i) > self::BUFFER_SIZE |
|
1127 | + ( $size - $i ) > self::BUFFER_SIZE |
|
1128 | 1128 | ? $size - $i |
1129 | 1129 | : self::BUFFER_SIZE |
1130 | 1130 | ); |
1131 | - if (!is_string($message)) { |
|
1132 | - throw new SodiumException('Unexpected error reading from file.'); |
|
1131 | + if ( ! is_string( $message ) ) { |
|
1132 | + throw new SodiumException( 'Unexpected error reading from file.' ); |
|
1133 | 1133 | } |
1134 | 1134 | /** @var string $message */ |
1135 | 1135 | /** @psalm-suppress InvalidArgument */ |
1136 | - self::hash_update($hash, $message); |
|
1136 | + self::hash_update( $hash, $message ); |
|
1137 | 1137 | } |
1138 | 1138 | // Reset file pointer's position |
1139 | - fseek($fp, $originalPosition, SEEK_SET); |
|
1139 | + fseek( $fp, $originalPosition, SEEK_SET ); |
|
1140 | 1140 | return $hash; |
1141 | 1141 | } |
1142 | 1142 | |
@@ -1152,71 +1152,71 @@ discard block |
||
1152 | 1152 | * @throws SodiumException |
1153 | 1153 | * @throws TypeError |
1154 | 1154 | */ |
1155 | - private static function sign_core32($filePath, $secretKey) |
|
1155 | + private static function sign_core32( $filePath, $secretKey ) |
|
1156 | 1156 | { |
1157 | 1157 | /** @var int|bool $size */ |
1158 | - $size = filesize($filePath); |
|
1159 | - if (!is_int($size)) { |
|
1160 | - throw new SodiumException('Could not obtain the file size'); |
|
1158 | + $size = filesize( $filePath ); |
|
1159 | + if ( ! is_int( $size ) ) { |
|
1160 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
1161 | 1161 | } |
1162 | 1162 | /** @var int $size */ |
1163 | 1163 | |
1164 | 1164 | /** @var resource|bool $fp */ |
1165 | - $fp = fopen($filePath, 'rb'); |
|
1166 | - if (!is_resource($fp)) { |
|
1167 | - throw new SodiumException('Could not open input file for reading'); |
|
1165 | + $fp = fopen( $filePath, 'rb' ); |
|
1166 | + if ( ! is_resource( $fp ) ) { |
|
1167 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
1168 | 1168 | } |
1169 | 1169 | /** @var resource $fp */ |
1170 | 1170 | |
1171 | 1171 | /** @var string $az */ |
1172 | - $az = hash('sha512', self::substr($secretKey, 0, 32), true); |
|
1172 | + $az = hash( 'sha512', self::substr( $secretKey, 0, 32 ), true ); |
|
1173 | 1173 | |
1174 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
1175 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
1174 | + $az[ 0 ] = self::intToChr( self::chrToInt( $az[ 0 ] ) & 248 ); |
|
1175 | + $az[ 31 ] = self::intToChr( ( self::chrToInt( $az[ 31 ] ) & 63 ) | 64 ); |
|
1176 | 1176 | |
1177 | - $hs = hash_init('sha512'); |
|
1178 | - self::hash_update($hs, self::substr($az, 32, 32)); |
|
1177 | + $hs = hash_init( 'sha512' ); |
|
1178 | + self::hash_update( $hs, self::substr( $az, 32, 32 ) ); |
|
1179 | 1179 | /** @var resource $hs */ |
1180 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
1180 | + $hs = self::updateHashWithFile( $hs, $fp, $size ); |
|
1181 | 1181 | |
1182 | 1182 | /** @var string $nonceHash */ |
1183 | - $nonceHash = hash_final($hs, true); |
|
1183 | + $nonceHash = hash_final( $hs, true ); |
|
1184 | 1184 | |
1185 | 1185 | /** @var string $pk */ |
1186 | - $pk = self::substr($secretKey, 32, 32); |
|
1186 | + $pk = self::substr( $secretKey, 32, 32 ); |
|
1187 | 1187 | |
1188 | 1188 | /** @var string $nonce */ |
1189 | - $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
1189 | + $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 ); |
|
1190 | 1190 | |
1191 | 1191 | /** @var string $sig */ |
1192 | 1192 | $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes( |
1193 | - ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce) |
|
1193 | + ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base( $nonce ) |
|
1194 | 1194 | ); |
1195 | 1195 | |
1196 | - $hs = hash_init('sha512'); |
|
1197 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
1198 | - self::hash_update($hs, self::substr($pk, 0, 32)); |
|
1196 | + $hs = hash_init( 'sha512' ); |
|
1197 | + self::hash_update( $hs, self::substr( $sig, 0, 32 ) ); |
|
1198 | + self::hash_update( $hs, self::substr( $pk, 0, 32 ) ); |
|
1199 | 1199 | /** @var resource $hs */ |
1200 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
1200 | + $hs = self::updateHashWithFile( $hs, $fp, $size ); |
|
1201 | 1201 | |
1202 | 1202 | /** @var string $hramHash */ |
1203 | - $hramHash = hash_final($hs, true); |
|
1203 | + $hramHash = hash_final( $hs, true ); |
|
1204 | 1204 | |
1205 | 1205 | /** @var string $hram */ |
1206 | - $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash); |
|
1206 | + $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce( $hramHash ); |
|
1207 | 1207 | |
1208 | 1208 | /** @var string $sigAfter */ |
1209 | - $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce); |
|
1209 | + $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd( $hram, $az, $nonce ); |
|
1210 | 1210 | |
1211 | 1211 | /** @var string $sig */ |
1212 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
1212 | + $sig = self::substr( $sig, 0, 32 ) . self::substr( $sigAfter, 0, 32 ); |
|
1213 | 1213 | |
1214 | 1214 | try { |
1215 | - ParagonIE_Sodium_Compat::memzero($az); |
|
1216 | - } catch (SodiumException $ex) { |
|
1215 | + ParagonIE_Sodium_Compat::memzero( $az ); |
|
1216 | + } catch ( SodiumException $ex ) { |
|
1217 | 1217 | $az = null; |
1218 | 1218 | } |
1219 | - fclose($fp); |
|
1219 | + fclose( $fp ); |
|
1220 | 1220 | return $sig; |
1221 | 1221 | } |
1222 | 1222 | |
@@ -1234,37 +1234,37 @@ discard block |
||
1234 | 1234 | * @throws SodiumException |
1235 | 1235 | * @throws Exception |
1236 | 1236 | */ |
1237 | - public static function verify_core32($sig, $filePath, $publicKey) |
|
1237 | + public static function verify_core32( $sig, $filePath, $publicKey ) |
|
1238 | 1238 | { |
1239 | 1239 | /* Security checks */ |
1240 | - if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
1241 | - throw new SodiumException('S < L - Invalid signature'); |
|
1240 | + if ( ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L( self::substr( $sig, 32, 32 ) ) ) { |
|
1241 | + throw new SodiumException( 'S < L - Invalid signature' ); |
|
1242 | 1242 | } |
1243 | - if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) { |
|
1244 | - throw new SodiumException('Signature is on too small of an order'); |
|
1243 | + if ( ParagonIE_Sodium_Core32_Ed25519::small_order( $sig ) ) { |
|
1244 | + throw new SodiumException( 'Signature is on too small of an order' ); |
|
1245 | 1245 | } |
1246 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
1247 | - throw new SodiumException('Invalid signature'); |
|
1246 | + if ( ( self::chrToInt( $sig[ 63 ] ) & 224 ) !== 0 ) { |
|
1247 | + throw new SodiumException( 'Invalid signature' ); |
|
1248 | 1248 | } |
1249 | 1249 | $d = 0; |
1250 | - for ($i = 0; $i < 32; ++$i) { |
|
1251 | - $d |= self::chrToInt($publicKey[$i]); |
|
1250 | + for ( $i = 0; $i < 32; ++$i ) { |
|
1251 | + $d |= self::chrToInt( $publicKey[ $i ] ); |
|
1252 | 1252 | } |
1253 | - if ($d === 0) { |
|
1254 | - throw new SodiumException('All zero public key'); |
|
1253 | + if ( $d === 0 ) { |
|
1254 | + throw new SodiumException( 'All zero public key' ); |
|
1255 | 1255 | } |
1256 | 1256 | |
1257 | 1257 | /** @var int|bool $size */ |
1258 | - $size = filesize($filePath); |
|
1259 | - if (!is_int($size)) { |
|
1260 | - throw new SodiumException('Could not obtain the file size'); |
|
1258 | + $size = filesize( $filePath ); |
|
1259 | + if ( ! is_int( $size ) ) { |
|
1260 | + throw new SodiumException( 'Could not obtain the file size' ); |
|
1261 | 1261 | } |
1262 | 1262 | /** @var int $size */ |
1263 | 1263 | |
1264 | 1264 | /** @var resource|bool $fp */ |
1265 | - $fp = fopen($filePath, 'rb'); |
|
1266 | - if (!is_resource($fp)) { |
|
1267 | - throw new SodiumException('Could not open input file for reading'); |
|
1265 | + $fp = fopen( $filePath, 'rb' ); |
|
1266 | + if ( ! is_resource( $fp ) ) { |
|
1267 | + throw new SodiumException( 'Could not open input file for reading' ); |
|
1268 | 1268 | } |
1269 | 1269 | /** @var resource $fp */ |
1270 | 1270 | |
@@ -1275,35 +1275,35 @@ discard block |
||
1275 | 1275 | ParagonIE_Sodium_Compat::$fastMult = true; |
1276 | 1276 | |
1277 | 1277 | /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
1278 | - $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey); |
|
1278 | + $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime( $publicKey ); |
|
1279 | 1279 | |
1280 | - $hs = hash_init('sha512'); |
|
1281 | - self::hash_update($hs, self::substr($sig, 0, 32)); |
|
1282 | - self::hash_update($hs, self::substr($publicKey, 0, 32)); |
|
1280 | + $hs = hash_init( 'sha512' ); |
|
1281 | + self::hash_update( $hs, self::substr( $sig, 0, 32 ) ); |
|
1282 | + self::hash_update( $hs, self::substr( $publicKey, 0, 32 ) ); |
|
1283 | 1283 | /** @var resource $hs */ |
1284 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
1284 | + $hs = self::updateHashWithFile( $hs, $fp, $size ); |
|
1285 | 1285 | /** @var string $hDigest */ |
1286 | - $hDigest = hash_final($hs, true); |
|
1286 | + $hDigest = hash_final( $hs, true ); |
|
1287 | 1287 | |
1288 | 1288 | /** @var string $h */ |
1289 | - $h = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
1289 | + $h = ParagonIE_Sodium_Core32_Ed25519::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 ); |
|
1290 | 1290 | |
1291 | 1291 | /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ |
1292 | 1292 | $R = ParagonIE_Sodium_Core32_Ed25519::ge_double_scalarmult_vartime( |
1293 | 1293 | $h, |
1294 | 1294 | $A, |
1295 | - self::substr($sig, 32) |
|
1295 | + self::substr( $sig, 32 ) |
|
1296 | 1296 | ); |
1297 | 1297 | |
1298 | 1298 | /** @var string $rcheck */ |
1299 | - $rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes($R); |
|
1299 | + $rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes( $R ); |
|
1300 | 1300 | |
1301 | 1301 | // Close the file handle |
1302 | - fclose($fp); |
|
1302 | + fclose( $fp ); |
|
1303 | 1303 | |
1304 | 1304 | // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
1305 | 1305 | ParagonIE_Sodium_Compat::$fastMult = $orig; |
1306 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
1306 | + return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) ); |
|
1307 | 1307 | } |
1308 | 1308 | |
1309 | 1309 | /** |
@@ -1318,29 +1318,29 @@ discard block |
||
1318 | 1318 | * @throws SodiumException |
1319 | 1319 | * @throws TypeError |
1320 | 1320 | */ |
1321 | - protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key) |
|
1321 | + protected static function secretbox_encrypt_core32( $ifp, $ofp, $mlen, $nonce, $key ) |
|
1322 | 1322 | { |
1323 | - $plaintext = fread($ifp, 32); |
|
1324 | - if (!is_string($plaintext)) { |
|
1325 | - throw new SodiumException('Could not read input file'); |
|
1323 | + $plaintext = fread( $ifp, 32 ); |
|
1324 | + if ( ! is_string( $plaintext ) ) { |
|
1325 | + throw new SodiumException( 'Could not read input file' ); |
|
1326 | 1326 | } |
1327 | - $first32 = self::ftell($ifp); |
|
1327 | + $first32 = self::ftell( $ifp ); |
|
1328 | 1328 | |
1329 | 1329 | /** @var string $subkey */ |
1330 | - $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); |
|
1330 | + $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20( $nonce, $key ); |
|
1331 | 1331 | |
1332 | 1332 | /** @var string $realNonce */ |
1333 | - $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); |
|
1333 | + $realNonce = ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ); |
|
1334 | 1334 | |
1335 | 1335 | /** @var string $block0 */ |
1336 | - $block0 = str_repeat("\x00", 32); |
|
1336 | + $block0 = str_repeat( "\x00", 32 ); |
|
1337 | 1337 | |
1338 | 1338 | /** @var int $mlen - Length of the plaintext message */ |
1339 | 1339 | $mlen0 = $mlen; |
1340 | - if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
1340 | + if ( $mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES ) { |
|
1341 | 1341 | $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; |
1342 | 1342 | } |
1343 | - $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0); |
|
1343 | + $block0 .= ParagonIE_Sodium_Core32_Util::substr( $plaintext, 0, $mlen0 ); |
|
1344 | 1344 | |
1345 | 1345 | /** @var string $block0 */ |
1346 | 1346 | $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor( |
@@ -1358,16 +1358,16 @@ discard block |
||
1358 | 1358 | ); |
1359 | 1359 | |
1360 | 1360 | // Pre-write 16 blank bytes for the Poly1305 tag |
1361 | - $start = self::ftell($ofp); |
|
1362 | - fwrite($ofp, str_repeat("\x00", 16)); |
|
1361 | + $start = self::ftell( $ofp ); |
|
1362 | + fwrite( $ofp, str_repeat( "\x00", 16 ) ); |
|
1363 | 1363 | |
1364 | 1364 | /** @var string $c */ |
1365 | 1365 | $cBlock = ParagonIE_Sodium_Core32_Util::substr( |
1366 | 1366 | $block0, |
1367 | 1367 | ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES |
1368 | 1368 | ); |
1369 | - $state->update($cBlock); |
|
1370 | - fwrite($ofp, $cBlock); |
|
1369 | + $state->update( $cBlock ); |
|
1370 | + fwrite( $ofp, $cBlock ); |
|
1371 | 1371 | $mlen -= 32; |
1372 | 1372 | |
1373 | 1373 | /** @var int $iter */ |
@@ -1380,15 +1380,15 @@ discard block |
||
1380 | 1380 | * Set the cursor to the end of the first half-block. All future bytes will |
1381 | 1381 | * generated from salsa20_xor_ic, starting from 1 (second block). |
1382 | 1382 | */ |
1383 | - fseek($ifp, $first32, SEEK_SET); |
|
1383 | + fseek( $ifp, $first32, SEEK_SET ); |
|
1384 | 1384 | |
1385 | - while ($mlen > 0) { |
|
1385 | + while ( $mlen > 0 ) { |
|
1386 | 1386 | $blockSize = $mlen > self::BUFFER_SIZE |
1387 | 1387 | ? self::BUFFER_SIZE |
1388 | 1388 | : $mlen; |
1389 | - $plaintext = fread($ifp, $blockSize); |
|
1390 | - if (!is_string($plaintext)) { |
|
1391 | - throw new SodiumException('Could not read input file'); |
|
1389 | + $plaintext = fread( $ifp, $blockSize ); |
|
1390 | + if ( ! is_string( $plaintext ) ) { |
|
1391 | + throw new SodiumException( 'Could not read input file' ); |
|
1392 | 1392 | } |
1393 | 1393 | $cBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( |
1394 | 1394 | $plaintext, |
@@ -1396,29 +1396,29 @@ discard block |
||
1396 | 1396 | $iter, |
1397 | 1397 | $subkey |
1398 | 1398 | ); |
1399 | - fwrite($ofp, $cBlock, $blockSize); |
|
1400 | - $state->update($cBlock); |
|
1399 | + fwrite( $ofp, $cBlock, $blockSize ); |
|
1400 | + $state->update( $cBlock ); |
|
1401 | 1401 | |
1402 | 1402 | $mlen -= $blockSize; |
1403 | 1403 | $iter += $incr; |
1404 | 1404 | } |
1405 | 1405 | try { |
1406 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
1407 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
1408 | - } catch (SodiumException $ex) { |
|
1406 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
1407 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
1408 | + } catch ( SodiumException $ex ) { |
|
1409 | 1409 | $block0 = null; |
1410 | 1410 | $subkey = null; |
1411 | 1411 | } |
1412 | - $end = self::ftell($ofp); |
|
1412 | + $end = self::ftell( $ofp ); |
|
1413 | 1413 | |
1414 | 1414 | /* |
1415 | 1415 | * Write the Poly1305 authentication tag that provides integrity |
1416 | 1416 | * over the ciphertext (encrypt-then-MAC) |
1417 | 1417 | */ |
1418 | - fseek($ofp, $start, SEEK_SET); |
|
1419 | - fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); |
|
1420 | - fseek($ofp, $end, SEEK_SET); |
|
1421 | - unset($state); |
|
1418 | + fseek( $ofp, $start, SEEK_SET ); |
|
1419 | + fwrite( $ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES ); |
|
1420 | + fseek( $ofp, $end, SEEK_SET ); |
|
1421 | + unset( $state ); |
|
1422 | 1422 | |
1423 | 1423 | return true; |
1424 | 1424 | } |
@@ -1435,46 +1435,46 @@ discard block |
||
1435 | 1435 | * @throws SodiumException |
1436 | 1436 | * @throws TypeError |
1437 | 1437 | */ |
1438 | - protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key) |
|
1438 | + protected static function secretbox_decrypt_core32( $ifp, $ofp, $mlen, $nonce, $key ) |
|
1439 | 1439 | { |
1440 | - $tag = fread($ifp, 16); |
|
1441 | - if (!is_string($tag)) { |
|
1442 | - throw new SodiumException('Could not read input file'); |
|
1440 | + $tag = fread( $ifp, 16 ); |
|
1441 | + if ( ! is_string( $tag ) ) { |
|
1442 | + throw new SodiumException( 'Could not read input file' ); |
|
1443 | 1443 | } |
1444 | 1444 | |
1445 | 1445 | /** @var string $subkey */ |
1446 | - $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); |
|
1446 | + $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20( $nonce, $key ); |
|
1447 | 1447 | |
1448 | 1448 | /** @var string $realNonce */ |
1449 | - $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); |
|
1449 | + $realNonce = ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ); |
|
1450 | 1450 | |
1451 | 1451 | /** @var string $block0 */ |
1452 | 1452 | $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20( |
1453 | 1453 | 64, |
1454 | - ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), |
|
1454 | + ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ), |
|
1455 | 1455 | $subkey |
1456 | 1456 | ); |
1457 | 1457 | |
1458 | 1458 | /* Verify the Poly1305 MAC -before- attempting to decrypt! */ |
1459 | - $state = new ParagonIE_Sodium_Core32_Poly1305_State(self::substr($block0, 0, 32)); |
|
1460 | - if (!self::onetimeauth_verify_core32($state, $ifp, $tag, $mlen)) { |
|
1461 | - throw new SodiumException('Invalid MAC'); |
|
1459 | + $state = new ParagonIE_Sodium_Core32_Poly1305_State( self::substr( $block0, 0, 32 ) ); |
|
1460 | + if ( ! self::onetimeauth_verify_core32( $state, $ifp, $tag, $mlen ) ) { |
|
1461 | + throw new SodiumException( 'Invalid MAC' ); |
|
1462 | 1462 | } |
1463 | 1463 | |
1464 | 1464 | /* |
1465 | 1465 | * Set the cursor to the end of the first half-block. All future bytes will |
1466 | 1466 | * generated from salsa20_xor_ic, starting from 1 (second block). |
1467 | 1467 | */ |
1468 | - $first32 = fread($ifp, 32); |
|
1469 | - if (!is_string($first32)) { |
|
1470 | - throw new SodiumException('Could not read input file'); |
|
1468 | + $first32 = fread( $ifp, 32 ); |
|
1469 | + if ( ! is_string( $first32 ) ) { |
|
1470 | + throw new SodiumException( 'Could not read input file' ); |
|
1471 | 1471 | } |
1472 | - $first32len = self::strlen($first32); |
|
1472 | + $first32len = self::strlen( $first32 ); |
|
1473 | 1473 | fwrite( |
1474 | 1474 | $ofp, |
1475 | 1475 | self::xorStrings( |
1476 | - self::substr($block0, 32, $first32len), |
|
1477 | - self::substr($first32, 0, $first32len) |
|
1476 | + self::substr( $block0, 32, $first32len ), |
|
1477 | + self::substr( $first32, 0, $first32len ) |
|
1478 | 1478 | ) |
1479 | 1479 | ); |
1480 | 1480 | $mlen -= 32; |
@@ -1486,13 +1486,13 @@ discard block |
||
1486 | 1486 | $incr = self::BUFFER_SIZE >> 6; |
1487 | 1487 | |
1488 | 1488 | /* Decrypts ciphertext, writes to output file. */ |
1489 | - while ($mlen > 0) { |
|
1489 | + while ( $mlen > 0 ) { |
|
1490 | 1490 | $blockSize = $mlen > self::BUFFER_SIZE |
1491 | 1491 | ? self::BUFFER_SIZE |
1492 | 1492 | : $mlen; |
1493 | - $ciphertext = fread($ifp, $blockSize); |
|
1494 | - if (!is_string($ciphertext)) { |
|
1495 | - throw new SodiumException('Could not read input file'); |
|
1493 | + $ciphertext = fread( $ifp, $blockSize ); |
|
1494 | + if ( ! is_string( $ciphertext ) ) { |
|
1495 | + throw new SodiumException( 'Could not read input file' ); |
|
1496 | 1496 | } |
1497 | 1497 | $pBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( |
1498 | 1498 | $ciphertext, |
@@ -1500,7 +1500,7 @@ discard block |
||
1500 | 1500 | $iter, |
1501 | 1501 | $subkey |
1502 | 1502 | ); |
1503 | - fwrite($ofp, $pBlock, $blockSize); |
|
1503 | + fwrite( $ofp, $pBlock, $blockSize ); |
|
1504 | 1504 | $mlen -= $blockSize; |
1505 | 1505 | $iter += $incr; |
1506 | 1506 | } |
@@ -1525,22 +1525,22 @@ discard block |
||
1525 | 1525 | $mlen = 0 |
1526 | 1526 | ) { |
1527 | 1527 | /** @var int $pos */ |
1528 | - $pos = self::ftell($ifp); |
|
1528 | + $pos = self::ftell( $ifp ); |
|
1529 | 1529 | |
1530 | - while ($mlen > 0) { |
|
1530 | + while ( $mlen > 0 ) { |
|
1531 | 1531 | $blockSize = $mlen > self::BUFFER_SIZE |
1532 | 1532 | ? self::BUFFER_SIZE |
1533 | 1533 | : $mlen; |
1534 | - $ciphertext = fread($ifp, $blockSize); |
|
1535 | - if (!is_string($ciphertext)) { |
|
1536 | - throw new SodiumException('Could not read input file'); |
|
1534 | + $ciphertext = fread( $ifp, $blockSize ); |
|
1535 | + if ( ! is_string( $ciphertext ) ) { |
|
1536 | + throw new SodiumException( 'Could not read input file' ); |
|
1537 | 1537 | } |
1538 | - $state->update($ciphertext); |
|
1538 | + $state->update( $ciphertext ); |
|
1539 | 1539 | $mlen -= $blockSize; |
1540 | 1540 | } |
1541 | - $res = ParagonIE_Sodium_Core32_Util::verify_16($tag, $state->finish()); |
|
1541 | + $res = ParagonIE_Sodium_Core32_Util::verify_16( $tag, $state->finish() ); |
|
1542 | 1542 | |
1543 | - fseek($ifp, $pos, SEEK_SET); |
|
1543 | + fseek( $ifp, $pos, SEEK_SET ); |
|
1544 | 1544 | return $res; |
1545 | 1545 | } |
1546 | 1546 | |
@@ -1549,12 +1549,12 @@ discard block |
||
1549 | 1549 | * @return int |
1550 | 1550 | * @throws SodiumException |
1551 | 1551 | */ |
1552 | - private static function ftell($resource) |
|
1552 | + private static function ftell( $resource ) |
|
1553 | 1553 | { |
1554 | - $return = ftell($resource); |
|
1555 | - if (!is_int($return)) { |
|
1556 | - throw new SodiumException('ftell() returned false'); |
|
1554 | + $return = ftell( $resource ); |
|
1555 | + if ( ! is_int( $return ) ) { |
|
1556 | + throw new SodiumException( 'ftell() returned false' ); |
|
1557 | 1557 | } |
1558 | - return (int) $return; |
|
1558 | + return (int)$return; |
|
1559 | 1559 | } |
1560 | 1560 | } |
@@ -6,8 +6,7 @@ discard block |
||
6 | 6 | /** |
7 | 7 | * Class ParagonIE_Sodium_File |
8 | 8 | */ |
9 | -class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util |
|
10 | -{ |
|
9 | +class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util { |
|
11 | 10 | /* PHP's default buffer size is 8192 for fread()/fwrite(). */ |
12 | 11 | const BUFFER_SIZE = 8192; |
13 | 12 | |
@@ -25,8 +24,7 @@ discard block |
||
25 | 24 | * @throws SodiumException |
26 | 25 | * @throws TypeError |
27 | 26 | */ |
28 | - public static function box($inputFile, $outputFile, $nonce, $keyPair) |
|
29 | - { |
|
27 | + public static function box($inputFile, $outputFile, $nonce, $keyPair) { |
|
30 | 28 | /* Type checks: */ |
31 | 29 | if (!is_string($inputFile)) { |
32 | 30 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -91,8 +89,7 @@ discard block |
||
91 | 89 | * @throws SodiumException |
92 | 90 | * @throws TypeError |
93 | 91 | */ |
94 | - public static function box_open($inputFile, $outputFile, $nonce, $keypair) |
|
95 | - { |
|
92 | + public static function box_open($inputFile, $outputFile, $nonce, $keypair) { |
|
96 | 93 | /* Type checks: */ |
97 | 94 | if (!is_string($inputFile)) { |
98 | 95 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -161,8 +158,7 @@ discard block |
||
161 | 158 | * @throws SodiumException |
162 | 159 | * @throws TypeError |
163 | 160 | */ |
164 | - public static function box_seal($inputFile, $outputFile, $publicKey) |
|
165 | - { |
|
161 | + public static function box_seal($inputFile, $outputFile, $publicKey) { |
|
166 | 162 | /* Type checks: */ |
167 | 163 | if (!is_string($inputFile)) { |
168 | 164 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -265,8 +261,7 @@ discard block |
||
265 | 261 | * @throws SodiumException |
266 | 262 | * @throws TypeError |
267 | 263 | */ |
268 | - public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) |
|
269 | - { |
|
264 | + public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) { |
|
270 | 265 | /* Type checks: */ |
271 | 266 | if (!is_string($inputFile)) { |
272 | 267 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -350,8 +345,7 @@ discard block |
||
350 | 345 | * @throws TypeError |
351 | 346 | * @psalm-suppress FailedTypeResolution |
352 | 347 | */ |
353 | - public static function generichash($filePath, $key = '', $outputLength = 32) |
|
354 | - { |
|
348 | + public static function generichash($filePath, $key = '', $outputLength = 32) { |
|
355 | 349 | /* Type checks: */ |
356 | 350 | if (!is_string($filePath)) { |
357 | 351 | throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
@@ -428,8 +422,7 @@ discard block |
||
428 | 422 | * @throws SodiumException |
429 | 423 | * @throws TypeError |
430 | 424 | */ |
431 | - public static function secretbox($inputFile, $outputFile, $nonce, $key) |
|
432 | - { |
|
425 | + public static function secretbox($inputFile, $outputFile, $nonce, $key) { |
|
433 | 426 | /* Type checks: */ |
434 | 427 | if (!is_string($inputFile)) { |
435 | 428 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..'); |
@@ -493,8 +486,7 @@ discard block |
||
493 | 486 | * @throws SodiumException |
494 | 487 | * @throws TypeError |
495 | 488 | */ |
496 | - public static function secretbox_open($inputFile, $outputFile, $nonce, $key) |
|
497 | - { |
|
489 | + public static function secretbox_open($inputFile, $outputFile, $nonce, $key) { |
|
498 | 490 | /* Type checks: */ |
499 | 491 | if (!is_string($inputFile)) { |
500 | 492 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -560,8 +552,7 @@ discard block |
||
560 | 552 | * @throws SodiumException |
561 | 553 | * @throws TypeError |
562 | 554 | */ |
563 | - public static function sign($filePath, $secretKey) |
|
564 | - { |
|
555 | + public static function sign($filePath, $secretKey) { |
|
565 | 556 | /* Type checks: */ |
566 | 557 | if (!is_string($filePath)) { |
567 | 558 | throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
@@ -656,8 +647,7 @@ discard block |
||
656 | 647 | * @throws TypeError |
657 | 648 | * @throws Exception |
658 | 649 | */ |
659 | - public static function verify($sig, $filePath, $publicKey) |
|
660 | - { |
|
650 | + public static function verify($sig, $filePath, $publicKey) { |
|
661 | 651 | /* Type checks: */ |
662 | 652 | if (!is_string($sig)) { |
663 | 653 | throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.'); |
@@ -766,8 +756,7 @@ discard block |
||
766 | 756 | * @throws SodiumException |
767 | 757 | * @throws TypeError |
768 | 758 | */ |
769 | - protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
770 | - { |
|
759 | + protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) { |
|
771 | 760 | if (PHP_INT_SIZE === 4) { |
772 | 761 | return self::secretbox_encrypt( |
773 | 762 | $ifp, |
@@ -803,8 +792,7 @@ discard block |
||
803 | 792 | * @throws SodiumException |
804 | 793 | * @throws TypeError |
805 | 794 | */ |
806 | - protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
807 | - { |
|
795 | + protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) { |
|
808 | 796 | if (PHP_INT_SIZE === 4) { |
809 | 797 | return self::secretbox_decrypt( |
810 | 798 | $ifp, |
@@ -841,8 +829,7 @@ discard block |
||
841 | 829 | * @throws SodiumException |
842 | 830 | * @throws TypeError |
843 | 831 | */ |
844 | - protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
845 | - { |
|
832 | + protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) { |
|
846 | 833 | if (PHP_INT_SIZE === 4) { |
847 | 834 | return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key); |
848 | 835 | } |
@@ -962,8 +949,7 @@ discard block |
||
962 | 949 | * @throws SodiumException |
963 | 950 | * @throws TypeError |
964 | 951 | */ |
965 | - protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
966 | - { |
|
952 | + protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) { |
|
967 | 953 | if (PHP_INT_SIZE === 4) { |
968 | 954 | return self::secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key); |
969 | 955 | } |
@@ -1095,8 +1081,7 @@ discard block |
||
1095 | 1081 | * @psalm-suppress TypeCoercion |
1096 | 1082 | * Ditto. |
1097 | 1083 | */ |
1098 | - public static function updateHashWithFile($hash, $fp, $size = 0) |
|
1099 | - { |
|
1084 | + public static function updateHashWithFile($hash, $fp, $size = 0) { |
|
1100 | 1085 | /* Type checks: */ |
1101 | 1086 | if (PHP_VERSION_ID < 70200) { |
1102 | 1087 | if (!is_resource($hash)) { |
@@ -1152,8 +1137,7 @@ discard block |
||
1152 | 1137 | * @throws SodiumException |
1153 | 1138 | * @throws TypeError |
1154 | 1139 | */ |
1155 | - private static function sign_core32($filePath, $secretKey) |
|
1156 | - { |
|
1140 | + private static function sign_core32($filePath, $secretKey) { |
|
1157 | 1141 | /** @var int|bool $size */ |
1158 | 1142 | $size = filesize($filePath); |
1159 | 1143 | if (!is_int($size)) { |
@@ -1234,8 +1218,7 @@ discard block |
||
1234 | 1218 | * @throws SodiumException |
1235 | 1219 | * @throws Exception |
1236 | 1220 | */ |
1237 | - public static function verify_core32($sig, $filePath, $publicKey) |
|
1238 | - { |
|
1221 | + public static function verify_core32($sig, $filePath, $publicKey) { |
|
1239 | 1222 | /* Security checks */ |
1240 | 1223 | if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) { |
1241 | 1224 | throw new SodiumException('S < L - Invalid signature'); |
@@ -1318,8 +1301,7 @@ discard block |
||
1318 | 1301 | * @throws SodiumException |
1319 | 1302 | * @throws TypeError |
1320 | 1303 | */ |
1321 | - protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key) |
|
1322 | - { |
|
1304 | + protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key) { |
|
1323 | 1305 | $plaintext = fread($ifp, 32); |
1324 | 1306 | if (!is_string($plaintext)) { |
1325 | 1307 | throw new SodiumException('Could not read input file'); |
@@ -1435,8 +1417,7 @@ discard block |
||
1435 | 1417 | * @throws SodiumException |
1436 | 1418 | * @throws TypeError |
1437 | 1419 | */ |
1438 | - protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key) |
|
1439 | - { |
|
1420 | + protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key) { |
|
1440 | 1421 | $tag = fread($ifp, 16); |
1441 | 1422 | if (!is_string($tag)) { |
1442 | 1423 | throw new SodiumException('Could not read input file'); |
@@ -1549,8 +1530,7 @@ discard block |
||
1549 | 1530 | * @return int |
1550 | 1531 | * @throws SodiumException |
1551 | 1532 | */ |
1552 | - private static function ftell($resource) |
|
1553 | - { |
|
1533 | + private static function ftell($resource) { |
|
1554 | 1534 | $return = ftell($resource); |
1555 | 1535 | if (!is_int($return)) { |
1556 | 1536 | throw new SodiumException('ftell() returned false'); |