Pure PHP polyfill for ext/sodium
Sodium Compat is a pure PHP polyfill for the Sodium cryptography library (libsodium), a core extension in PHP 7.2.0+ and otherwise available in PECL.
If you have the PHP extension installed, Sodium Compat will opportunistically and transparently use the PHP extension instead of our implementation.
sodium_compat v1.21.0 was the last v1.x release from the master branch. From now
on, all future releases that support PHP 5.2 - 8.0 and 32-bit integers will be
in the v1.x branch.
Newer versions of sodium_compat (i.e., v2.0.0) will continue to live in the master branch, unless a new major version is needed. The goal of this work is to improve code readability and performance, while reducing boilerplate code.
When in doubt, refer to the README file in the master branch for the latest in version information.
| sodium_compat version | PHP versions supported | 32-bit support? | Branch |
|---|---|---|---|
v1.x.y |
5.2.4 - LATEST | YES | v1.x |
v2.x.y |
8.1 - LATEST | NO | master |
If you need 32-bit PHP support (PHP_INT_SIZE == 4), continue using sodium_compat v1.x.
If you want improved performance and smaller dependencies, use v2.x.
We recommend libraries and frameworks set a Composer version constraint as follows:
{
"require": {
/* ... */
"paragonie/sodium_compat": ">= 1"
/* ... */
}
}Applications should, conversely, specify the actual version that matters to them and their deployments.
This cryptography library has not been formally audited by an independent third party that specializes in cryptography or cryptanalysis.
If you require such an audit before you can use sodium_compat in your projects
and have the funds for such an audit, please open an issue or contact
security at paragonie dot com so we can help get the ball rolling.
However, sodium_compat has been adopted by high profile open source projects, such as Joomla! and Magento. Furthermore, sodium_compat was developed by Paragon Initiative Enterprises, a company that specializes in secure PHP development and PHP cryptography, and has been informally reviewed by many other security experts who also specialize in PHP.
If you'd like to learn more about the defensive security measures we've taken to prevent sodium_compat from being a source of vulnerability in your systems, please read Cryptographically Secure PHP Development.
If you're using Composer:
composer require paragonie/sodium_compatIf you're not using Composer, download a release tarball
(which should be signed with our GnuPG public key), extract
its contents, then include our autoload.php script in your project.
gpg --fingerprint 7F52D5C61D1255C731362E826B97A1C2826404DA
if [ $? -ne 0 ]; then
echo -e "\033[31mCould not download PGP public key for verification\033[0m"
exit 1
fi
fi
# Verifying the PHP Archive
gpg --verify sodium-compat.phar.sig sodium-compat.pharNow, simply include this .phar file in your application.
execute();
} else {
// Defer to a cron job or other sort of asynchronous process
$process->enqueue();
}First, you'll want to read the Libsodium Quick Reference. It aims to answer, "Which function should I use for [common problem]?".
If you don't find the answers in the Quick Reference page, check out Using Libsodium in PHP Projects.
Finally, the official libsodium documentation (which was written for the C library, not the PHP library) also contains a lot of insightful technical information you may find helpful.
Recommended reading: Libsodium Quick Reference
crypto_auth()crypto_auth_verify()crypto_box()crypto_box_open()crypto_scalarmult()crypto_secretbox()crypto_secretbox_open()crypto_sign()crypto_sign_open()crypto_aead_aegis128l_encrypt()crypto_aead_aegis128l_decrypt()crypto_aead_aegis256_encrypt()crypto_aead_aegis256_decrypt()crypto_aead_aes256gcm_encrypt()crypto_aead_aes256gcm_decrypt()crypto_aead_chacha20poly1305_encrypt()crypto_aead_chacha20poly1305_decrypt()crypto_aead_chacha20poly1305_ietf_encrypt()crypto_aead_chacha20poly1305_ietf_decrypt()crypto_aead_xchacha20poly1305_ietf_encrypt()crypto_aead_xchacha20poly1305_ietf_decrypt()crypto_box_xchacha20poly1305()crypto_box_xchacha20poly1305_open()crypto_box_seal()crypto_box_seal_open()crypto_generichash()crypto_generichash_init()crypto_generichash_update()crypto_generichash_final()crypto_kx()crypto_secretbox_xchacha20poly1305()crypto_secretbox_xchacha20poly1305_open()crypto_shorthash()crypto_sign_detached()crypto_sign_ed25519_pk_to_curve25519()crypto_sign_ed25519_sk_to_curve25519()crypto_sign_verify_detached()crypto_core_ristretto255_add()crypto_core_ristretto255_from_hash()crypto_core_ristretto255_is_valid_point()crypto_core_ristretto255_random()crypto_core_ristretto255_scalar_add()crypto_core_ristretto255_scalar_complement()crypto_core_ristretto255_scalar_invert()crypto_core_ristretto255_scalar_mul()crypto_core_ristretto255_scalar_negate()crypto_core_ristretto255_scalar_random()crypto_core_ristretto255_scalar_reduce()crypto_core_ristretto255_scalar_sub()crypto_core_ristretto255_sub()crypto_scalarmult_ristretto255_base()crypto_scalarmult_ristretto255()crypto_stream()crypto_stream_keygen()crypto_stream_xor()crypto_stream_xchacha20()crypto_stream_xchacha20_keygen()crypto_stream_xchacha20_xor()crypto_stream_xchacha20_xor_ic()crypto_*_keypair())add()base642bin()bin2base64()bin2hex()hex2bin()crypto_kdf_derive_from_key()crypto_kx_client_session_keys()crypto_kx_server_session_keys()crypto_secretstream_xchacha20poly1305_init_push()crypto_secretstream_xchacha20poly1305_push()crypto_secretstream_xchacha20poly1305_init_pull()crypto_secretstream_xchacha20poly1305_pull()crypto_secretstream_xchacha20poly1305_rekey()pad()unpad()sodium_memzero() - Although we expose this API endpoint, we can't reliably
zero buffers from PHP.
If you have the PHP extension installed, sodium_compat
will use the native implementation to zero out the string provided. Otherwise
it will throw a SodiumException.
sodium_crypto_pwhash() - It's not feasible to polyfill scrypt or Argon2
into PHP and get reasonable performance. Users would feel motivated to select
parameters that downgrade security to avoid denial of service (DoS) attacks.
The only winning move is not to play.
If ext/sodium or ext/libsodium is installed, these API methods will fallthrough
to the extension. Otherwise, our polyfill library will throw a SodiumException.
To detect support for Argon2i at runtime, use
ParagonIE_Sodium_Compat::crypto_pwhash_is_available(), which returns a
boolean value (TRUE or FALSE).
Libsodium's HKDF API (crypto_kdf_hkdf_*()) is not included because PHP has
its own HMAC features amd it was not deemed necessary.
For sodium_compat users and that utilize PHPCompatibility
in their CI process, there is now a custom ruleset available which can be used
to prevent false positives being thrown by PHPCompatibility for the native
PHP functionality being polyfilled by this repo.
You can find the repo for the PHPCompatibilityParagonieSodiumCompat ruleset
here on Github
and on Packagist.
No open issues yet, or sync has not completed.