# RFC: Libsodium

- Status: Implemented (PHP 7.2)
- Authors: Scott Arciszewski, scott@paragonie.com
- Date: 2016-01-11
- Version: 0.5.1
- Original: https://wiki.php.net/rfc/libsodium
- Page: https://externals.io/rfc/libsodium
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Libsodium as a Core Extension in PHP 7.2 (closed)

Yes: 37 · No: 0

Voters: aharvey (Yes), bishop (Yes), bwoebi (Yes), colinodell (Yes), daverandom (Yes), derick (Yes), dm (Yes), eliw (Yes), galvao (Yes), gooh (Yes), guilhermeblanco (Yes), jedisct1 (Yes), joey (Yes), jpauli (Yes), krakjoe (Yes), lcobucci (Yes), leigh (Yes), mbeccati (Yes), mike (Yes), nikic (Yes), ocramius (Yes), omars (Yes), pmjones (Yes), pollita (Yes), ramsey (Yes), rdohms (Yes), remi (Yes), salathe (Yes), sammyk (Yes), sebastian (Yes), stas (Yes), svpernova09 (Yes), tpunt (Yes), trowski (Yes), yohgaki (Yes), yunosh (Yes), zeev (Yes)

### Use the current PECL syntax? (closed)

Yes, \Sodium\foo: 15 · No, sodium_foo: 23

Voters: aharvey (Yes, \Sodium\foo), ajf (Yes, \Sodium\foo), bishop (No, sodium_foo), bwoebi (No, sodium_foo), danack (No, sodium_foo), daverandom (Yes, \Sodium\foo), derick (No, sodium_foo), dm (Yes, \Sodium\foo), eliw (No, sodium_foo), galvao (Yes, \Sodium\foo), guilhermeblanco (Yes, \Sodium\foo), jedisct1 (Yes, \Sodium\foo), jhdxr (No, sodium_foo), joey (Yes, \Sodium\foo), jpauli (No, sodium_foo), kelunik (No, sodium_foo), krakjoe (Yes, \Sodium\foo), lcobucci (Yes, \Sodium\foo), leigh (No, sodium_foo), levim (Yes, \Sodium\foo), mbeccati (No, sodium_foo), nikic (Yes, \Sodium\foo), ocramius (Yes, \Sodium\foo), omars (No, sodium_foo), pmjones (No, sodium_foo), pollita (No, sodium_foo), ramsey (No, sodium_foo), rdohms (No, sodium_foo), remi (Yes, \Sodium\foo), salathe (No, sodium_foo), sammyk (No, sodium_foo), sebastian (Yes, \Sodium\foo), stas (No, sodium_foo), svpernova09 (No, sodium_foo), trowski (No, sodium_foo), yohgaki (No, sodium_foo), yunosh (No, sodium_foo), zeev (No, sodium_foo)

## Discussions on the mailing list

- [\[RFC\] \[PHP 7.1\] libsodium](https://externals.io/message/86329.md) — started on 2015-05-21, 12 messages
- [\[RFC\] Libsodium](https://externals.io/message/90249.md) — started on 2016-01-07, 25 messages
- [\[RFC\] Libsodium (bump)](https://externals.io/message/91678.md) — started on 2016-03-15, 14 messages
- [\[RFC\] Libsodium (bump)](https://externals.io/message/91968.md) — started on 2016-03-27, 1 messages
- [\[RFC\] Libsodium - Discussion](https://externals.io/message/93663.md) — started on 2016-06-01, 39 messages
- [\[RFC\] \[VOTE\] Libsodium as a core extension in PHP 7.2](https://externals.io/message/98146.md) — started on 2017-02-03, 13 messages
- [Namespaces in Core](https://externals.io/message/98218.md) — started on 2017-02-06, 27 messages

---

## Introduction

As we move towards PHP 7.0.0, we must look at the current state of cryptography in PHP. Libmcrypt hasn't been touched in eight years (last release was in 2007), leaving openssl as the only viable option for PHP 5.x and 7.0 users.

Meanwhile, libsodium bindings have been available in PECL for a while now, and has reached stability.

Libsodium is a modern cryptography library that offers authenticated encryption, high-speed elliptic curve cryptography, and much more. Unlike other cryptography standards (which are a potluck of cryptography primitives; i.e. WebCrypto), libsodium is comprised of carefully selected algorithms implemented by security experts to avoid side-channel vulnerabilities.

I maintain the documentation for the PHP extension [here](https://paragonie.com/book/pecl-libsodium).

## Proposal

This proposal is to adopt the libsodium extension (as ext/sodium) in the PHP core in PHP 7.2.0.

Currently, the libsodium extension in PECL uses the &#96;Sodium&#96; namespace, which runs contrary to the coding standards. If this RFC adopted into the PHP core, the namespace can be changed to conform to the coding standards (pending the outcome of a secondary vote).

```
    // In PECL today:
    $key = \Sodium\randombytes_buf(\Sodium\CRYPTO_SECRETBOX_KEYBYTES);
    $nonce = \Sodium\randombytes_buf(\Sodium\CRYPTO_SECRETBOX_NONCEBYTES);
    $message = \Sodium\crypto_secretbox(
        $plaintext,
        $key,
        $nonce
    );
    
    // If the second vote is against the use of a namespace:
    $key = sodium_randombytes_buf(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
    $nonce = sodium_randombytes_buf(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
    $message = sodium_crypto_secretbox(
        $plaintext,
        $key,
        $nonce
    );
```

Libsodium has a plethora of useful cryptography features:

- Password hashing and key derivation (sodium\_crypto\_pwhash\_\*) 
    - Argon2i (the chosen algorithm of the Password Hashing Competition)
    - Scrypt
- ECDH over Curve25519 (sodium\_crypto\_box)
- Authenticated secret key encryption (sodium\_crypto\_secretbox)
- Ed25519 digital signatures (sodium\_crypto\_sign)
- AEAD Modes 
    - ChaCha20-Poly1305
    - AES-256-GCM

Libsodium (like NaCl, from which it was forked) is widely regarded by cryptography and security industry experts for many reasons:

- No guesswork. The APIs are simple and powerful.
- Conservative security. Great effort was taken to perform every security-critical operation in constant time.
- Best-in-class elliptic curve cryptography.

## Libsodium API Subset

- Mainline NaCl Features 
    - \\Sodium\\crypto\_auth()
    - \\Sodium\\crypto\_auth\_verify()
    - \\Sodium\\crypto\_box()
    - \\Sodium\\crypto\_box\_open()
    - \\Sodium\\crypto\_scalarmult()
    - \\Sodium\\crypto\_secretbox()
    - \\Sodium\\crypto\_secretbox\_open()
    - \\Sodium\\crypto\_sign()
    - \\Sodium\\crypto\_sign\_open()
- PECL Libsodium Features 
    - \\Sodium\\crypto\_aead\_chacha20poly1305\_encrypt()
    - \\Sodium\\crypto\_aead\_chacha20poly1305\_decrypt()
    - \\Sodium\\crypto\_aead\_chacha20poly1305\_ietf\_encrypt()
    - \\Sodium\\crypto\_aead\_chacha20poly1305\_ietf\_decrypt()
    - \\Sodium\\crypto\_box\_keypair()
    - \\Sodium\\crypto\_box\_keypair\_from\_secretkey\_and\_publickey()
    - \\Sodium\\crypto\_box\_publickey()
    - \\Sodium\\crypto\_box\_publickey\_from\_secretkey()
    - \\Sodium\\crypto\_box\_seal()
    - \\Sodium\\crypto\_box\_seal\_open()
    - \\Sodium\\crypto\_box\_secretkey()
    - \\Sodium\\crypto\_generichash()
    - \\Sodium\\crypto\_generichash\_init()
    - \\Sodium\\crypto\_generichash\_update()
    - \\Sodium\\crypto\_generichash\_final()
    - \\Sodium\\crypto\_kx()
    - \\Sodium\\crypto\_pwhash()
    - \\Sodium\\crypto\_pwhash\_str()
    - \\Sodium\\crypto\_pwhash\_str\_verify()
    - \\Sodium\\crypto\_shorthash()
    - \\Sodium\\crypto\_sign\_detached()
    - \\Sodium\\crypto\_sign\_keypair()
    - \\Sodium\\crypto\_sign\_publickey()
    - \\Sodium\\crypto\_sign\_publickey\_from\_secretkey()
    - \\Sodium\\crypto\_sign\_secretkey()
    - \\Sodium\\crypto\_sign\_verify\_detached()
    - \\Sodium\\crypto\_stream()
    - \\Sodium\\crypto\_stream\_xor()
    - \\Sodium\\compare()
    - \\Sodium\\memzero()
    - \\Sodium\\increment()

Because crypto\_aead\_encrypt() will be the CAESAR finalist, we should tentatively commit to adding that one day. <https://competitions.cr.yp.to/caesar.html>

We don't need crypto\_aead\_aes256gcm since that's provided by OpenSSL. We only provide ChaCha20-Poly1305 for e.g. Noise protocol integrations.

We don't need scrypt; we have crypto\_pwhash() which is Argon2i.

We don't need several other utilities (bin2hex, hex2bin, etc.). Instead, we should make those existing mainline functions cache-timing safe. We MAY decide to add function aliases (e.g. \\Sodium\\bin2hex() -&gt; \\bin2hex()) for compatibility with software already written for ext/sodium.

## Proposed PHP Version(s)

This RFC targets PHP 7.2.

## RFC Impact

I'm not aware of any potential impact that adopting ext/sodium will have on other RFCs.

## Future Scope

With libsodium in the PHP core, we may be able to update the Phar extension to support Ed25519 signatures. This will be a great boon for authentic PHP Archive distribution. The current best option, OpenSSL, may provide inadequate security.

## New Constants

See the list of all libsodium constants in the reference. In every case, \\Sodium\\FOO will be transformed to SODIUM\_FOO.

```
\Sodium\CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES	=>	32
\Sodium\CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES	=>	0
\Sodium\CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES	=>	8
\Sodium\CRYPTO_AEAD_CHACHA20POLY1305_ABYTES	=>	16
\Sodium\CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES	=>	32
\Sodium\CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES	=>	0
\Sodium\CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES	=>	12
\Sodium\CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES	=>	16 
\Sodium\CRYPTO_AUTH_BYTES	=>	32
\Sodium\CRYPTO_AUTH_KEYBYTES	=>	32
\Sodium\CRYPTO_BOX_SEALBYTES	=>	16
\Sodium\CRYPTO_BOX_SECRETKEYBYTES	=>	32
\Sodium\CRYPTO_BOX_PUBLICKEYBYTES	=>	32
\Sodium\CRYPTO_BOX_KEYPAIRBYTES	=>	64
\Sodium\CRYPTO_BOX_MACBYTES	=>	16
\Sodium\CRYPTO_BOX_NONCEBYTES	=>	24
\Sodium\CRYPTO_BOX_SEEDBYTES	=>	32
\Sodium\CRYPTO_KX_BYTES	=>	32
\Sodium\CRYPTO_KX_PUBLICKEYBYTES	=>	32
\Sodium\CRYPTO_KX_SECRETKEYBYTES	=>	32
\Sodium\CRYPTO_GENERICHASH_BYTES	=>	32
\Sodium\CRYPTO_GENERICHASH_BYTES_MIN	=>	16
\Sodium\CRYPTO_GENERICHASH_BYTES_MAX	=>	64
\Sodium\CRYPTO_GENERICHASH_KEYBYTES	=>	32
\Sodium\CRYPTO_GENERICHASH_KEYBYTES_MIN	=>	16
\Sodium\CRYPTO_GENERICHASH_KEYBYTES_MAX	=>	64
\Sodium\CRYPTO_PWHASH_SALTBYTES	=>	16
\Sodium\CRYPTO_PWHASH_STRPREFIX	=>	$argon2i$
\Sodium\CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE	=>	4
\Sodium\CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE	=>	33554432
\Sodium\CRYPTO_PWHASH_OPSLIMIT_MODERATE	=>	6
\Sodium\CRYPTO_PWHASH_MEMLIMIT_MODERATE	=>	134217728
\Sodium\CRYPTO_PWHASH_OPSLIMIT_SENSITIVE	=>	8
\Sodium\CRYPTO_PWHASH_MEMLIMIT_SENSITIVE	=>	536870912
\Sodium\CRYPTO_SCALARMULT_BYTES	=>	32
\Sodium\CRYPTO_SCALARMULT_SCALARBYTES	=>	32
\Sodium\CRYPTO_SHORTHASH_BYTES	=>	8
\Sodium\CRYPTO_SHORTHASH_KEYBYTES	=>	16
\Sodium\CRYPTO_SECRETBOX_KEYBYTES	=>	32
\Sodium\CRYPTO_SECRETBOX_MACBYTES	=>	16
\Sodium\CRYPTO_SECRETBOX_NONCEBYTES	=>	24
\Sodium\CRYPTO_SIGN_BYTES	=>	64
\Sodium\CRYPTO_SIGN_SEEDBYTES	=>	32
\Sodium\CRYPTO_SIGN_PUBLICKEYBYTES	=>	32
\Sodium\CRYPTO_SIGN_SECRETKEYBYTES	=>	64
\Sodium\CRYPTO_SIGN_KEYPAIRBYTES	=>	96
\Sodium\CRYPTO_STREAM_KEYBYTES	=>	32
\Sodium\CRYPTO_STREAM_NONCEBYTES	=>	24
```

## Proposed Voting Choices

Voting starts on 2017-02-03 20:42 UTC and closes on 2017-02-10 21:00 UTC.

Vote YES to add ext/sodium to PHP 7.2. As per new voting rules, a 2/3 majority is required.

**Vote: Libsodium as a Core Extension in PHP 7.2** — Yes: 37 · No: 0 (closed)

Second vote: Vote YES to keep the namespace (\\Sodium\\etc), vote NO to switch to a prefix in the global namespace (sodium\_etc). A 2/3 majority is also required for “Yes” to be accepted. The default is for “No”.

**Vote: Use the current PECL syntax?** — Yes, \\Sodium\\foo: 15 · No, sodium\_foo: 23 (closed)

## Implementation

1. merged to PHP 7.2+ in <http://git.php.net/?p=php-src.git;a=commit;h=5cfa26c18189ae5e0ae8bb1eac5dd0e213a2bb3e>
2. PHP manual section: yet missing

## References

Links to external references, discussions or RFCs:

- [PECL libsodium](https://github.com/jedisct1/libsodium-php) (the current implementation)
- [Libsodium PHP documentation](https://paragonie.com/book/pecl-libsodium)
- [List of all libsodium constants](https://paragonie.com/book/pecl-libsodium/read/01-quick-start.md#constant-index)
