Skip to content

Commit

Permalink
Update public API
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Apr 19, 2024
1 parent 683ff14 commit d90caf8
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 136 deletions.
31 changes: 0 additions & 31 deletions autoload-php7.php

This file was deleted.

83 changes: 29 additions & 54 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,28 @@
<?php

if (PHP_VERSION_ID < 70000) {
if (!is_callable('sodiumCompatAutoloader')) {
/**
* Sodium_Compat autoloader.
*
* @param string $class Class name to be autoloaded.
*
* @return bool Stop autoloading?
*/
function sodiumCompatAutoloader($class)
{
$namespace = 'ParagonIE_Sodium_';
// Does the class use the namespace prefix?
$len = strlen($namespace);
if (strncmp($namespace, $class, $len) !== 0) {
// no, move to the next registered autoloader
return false;
}

// Get the relative class name
$relative_class = substr($class, $len);
spl_autoload_register(function ($class) {
$namespace = 'ParagonIE_Sodium_';
// Does the class use the namespace prefix?
$len = strlen($namespace);
if (strncmp($namespace, $class, $len) !== 0) {
// no, move to the next registered autoloader
return false;
}

// Replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require_once $file;
return true;
}
return false;
}
// Get the relative class name
$relative_class = substr($class, $len);

// Now that we have an autoloader, let's register it!
spl_autoload_register('sodiumCompatAutoloader');
// Replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require_once $file;
return true;
}
} else {
require_once dirname(__FILE__) . '/autoload-php7.php';
}
return false;
});

/* Explicitly, always load the Compat class: */
if (!class_exists('ParagonIE_Sodium_Compat', false)) {
Expand All @@ -49,26 +32,18 @@ function sodiumCompatAutoloader($class)
if (!class_exists('SodiumException', false)) {
require_once dirname(__FILE__) . '/src/SodiumException.php';
}
if (PHP_VERSION_ID >= 50300) {
// Namespaces didn't exist before 5.3.0, so don't even try to use this
// unless PHP >= 5.3.0
require_once dirname(__FILE__) . '/lib/namespaced.php';
require_once dirname(__FILE__) . '/lib/sodium_compat.php';
if (!defined('SODIUM_CRYPTO_AEAD_AEGIS128L_KEYBYTES')) {
require_once dirname(__FILE__) . '/lib/php84compat_const.php';
}
} else {
require_once dirname(__FILE__) . '/src/PHP52/SplFixedArray.php';

require_once dirname(__FILE__) . '/lib/namespaced.php';
require_once dirname(__FILE__) . '/lib/sodium_compat.php';
if (!defined('SODIUM_CRYPTO_AEAD_AEGIS128L_KEYBYTES')) {
require_once dirname(__FILE__) . '/lib/php84compat_const.php';
}
if (PHP_VERSION_ID < 70200 || !extension_loaded('sodium')) {
if (PHP_VERSION_ID >= 50300 && !defined('SODIUM_CRYPTO_SCALARMULT_BYTES')) {

if (!extension_loaded('sodium')) {
if (!defined('SODIUM_CRYPTO_SCALARMULT_BYTES')) {
require_once dirname(__FILE__) . '/lib/php72compat_const.php';
}
if (PHP_VERSION_ID >= 70000) {
assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?');
} else {
assert(class_exists('ParagonIE_Sodium_Compat'));
}
assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?');
require_once(dirname(__FILE__) . '/lib/php72compat.php');
} elseif (!function_exists('sodium_crypto_stream_xchacha20_xor')) {
// Older versions of {PHP, ext/sodium} will not define these
Expand Down
2 changes: 1 addition & 1 deletion lib/constants.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Sodium;

require_once dirname(dirname(__FILE__)) . '/autoload.php';
require_once dirname(__DIR__) . '/autoload.php';

use ParagonIE_Sodium_Compat;

Expand Down
4 changes: 0 additions & 4 deletions lib/namespaced.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

require_once dirname(dirname(__FILE__)) . '/autoload.php';

if (PHP_VERSION_ID < 50300) {
return;
}

/*
* This file is just for convenience, to allow developers to reduce verbosity when
* they add this project to their libraries.
Expand Down
2 changes: 1 addition & 1 deletion lib/php72compat.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once dirname(dirname(__FILE__)) . '/autoload.php';
require_once dirname(__DIR__) . '/autoload.php';

/**
* This file will monkey patch the pure-PHP implementation in place of the
Expand Down
39 changes: 31 additions & 8 deletions lib/php84compat.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

require_once dirname(dirname(__FILE__)) . '/autoload.php';

Expand Down Expand Up @@ -34,8 +35,13 @@
* @return string
* @throws SodiumException
*/
function sodium_crypto_aead_aegis128l_decrypt($ciphertext, $additional_data, $nonce, $key)
{
function sodium_crypto_aead_aegis128l_decrypt(
string $ciphertext,
string $additional_data,
string $nonce,
#[\SensitiveParameter]
string $key
): string {
return ParagonIE_Sodium_Compat::crypto_aead_aegis128l_decrypt(
$ciphertext,
$additional_data,
Expand All @@ -55,8 +61,14 @@ function sodium_crypto_aead_aegis128l_decrypt($ciphertext, $additional_data, $no
* @throws SodiumException
* @throws TypeError
*/
function sodium_crypto_aead_aegis128l_encrypt($message, $additional_data, $nonce, $key)
{
function sodium_crypto_aead_aegis128l_encrypt(
#[\SensitiveParameter]
string $message,
string $additional_data,
string $nonce,
#[\SensitiveParameter]
string $key
): string {
return ParagonIE_Sodium_Compat::crypto_aead_aegis128l_encrypt(
$message,
$additional_data,
Expand All @@ -75,8 +87,13 @@ function sodium_crypto_aead_aegis128l_encrypt($message, $additional_data, $nonce
* @return string
* @throws SodiumException
*/
function sodium_crypto_aead_aegis256_decrypt($ciphertext, $additional_data, $nonce, $key)
{
function sodium_crypto_aead_aegis256_decrypt(
string $ciphertext,
string $additional_data,
string $nonce,
#[\SensitiveParameter]
string $key
): string {
return ParagonIE_Sodium_Compat::crypto_aead_aegis256_decrypt(
$ciphertext,
$additional_data,
Expand All @@ -96,8 +113,14 @@ function sodium_crypto_aead_aegis256_decrypt($ciphertext, $additional_data, $non
* @throws SodiumException
* @throws TypeError
*/
function sodium_crypto_aead_aegis256_encrypt($message, $additional_data, $nonce, $key)
{
function sodium_crypto_aead_aegis256_encrypt(
#[\SensitiveParameter]
string $message,
string $additional_data,
string $nonce,
#[\SensitiveParameter]
string $key
): string {
return ParagonIE_Sodium_Compat::crypto_aead_aegis256_encrypt(
$message,
$additional_data,
Expand Down
1 change: 0 additions & 1 deletion lib/php84compat_const.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const SODIUM_CRYPTO_AEAD_AEGIS128L_NSECBYTES = 0;
const SODIUM_CRYPTO_AEAD_AEGIS128L_NPUBBYTES = 32;
const SODIUM_CRYPTO_AEAD_AEGIS128L_ABYTES = 32;

const SODIUM_CRYPTO_AEAD_AEGIS256_KEYBYTES = 32;
const SODIUM_CRYPTO_AEAD_AEGIS256_NSECBYTES = 0;
const SODIUM_CRYPTO_AEAD_AEGIS256_NPUBBYTES = 32;
Expand Down
Loading

0 comments on commit d90caf8

Please sign in to comment.