bjosodium/Readme.org

187 lines
8.8 KiB
Org Mode
Raw Normal View History

* bjosodium — libsodium for Bjolang
Authenticated encryption with AEGIS-256 or XChaCha20-Poly1305, and password
hashing with Argon2id. The work is done by [[https://libsodium.org][libsodium]], through the .NET
binding [[https://libsodium.net][LibSodium.Net]].
#+BEGIN_SRC scheme
(import (std prelude))
(import (bjosodium core))
(defun (main)
;; A key from a password. Store the salt and the cost beside the data.
(def salt (make-argon2id-salt))
(def key (argon2id-derive-key "correct horse" salt))
;; Encrypt. The header is not secret, but a change to it is found.
(def cipher (preferred-cipher))
(def header (string->bytes "bookmarks v1"))
(def sealed (aead-seal cipher key (string->bytes "https://example.org") #:aad header))
(match (aead-unseal cipher key sealed #:aad header)
((Some plaintext) (println (bytes->string plaintext)) 0)
(None (println "wrong key, or the data was changed") 1)))
#+END_SRC
** Using it
#+BEGIN_SRC scheme
(depends
(package (name (bjosodium))
(version (version-at-least "0.1.0"))
(source (path (dir "../bjosodium")))))
#+END_SRC
A program using this package declares no NuGet packages of its own. The
package declares LibSodium.Net and libsodium, and ~bjo~ restores them for
any build that uses the package. The libsodium package has the native
library for each platform: Windows, Linux (glibc and musl), macOS, Android,
iOS, tvOS and Mac Catalyst. Nothing has to be installed.
** Operations
| ~(aead-encrypt c key nonce data #:aad ad)~ | the ciphertext and the tag; the caller stores the nonce |
| ~(aead-decrypt c key nonce ct #:aad ad)~ | ~(Some data)~, or ~None~ when the tag does not verify |
| ~(aead-seal c key data #:aad ad)~ | a new random nonce, the ciphertext and the tag, in one array |
| ~(aead-unseal c key sealed #:aad ad)~ | ~(Some data)~, or ~None~ |
| ~(make-aead-key c)~ ~(make-aead-nonce c)~ | random bytes from the system's random source |
| ~(cipher-key-length c)~ ~(cipher-nonce-length c)~ ~(cipher-tag-length c)~ | in bytes |
| ~(aegis256-accelerated?)~ | true when this CPU runs AEGIS-256 on AES instructions |
| ~(preferred-cipher)~ | ~Aegis256~ when it is accelerated, else ~XChaCha20Poly1305~ |
| ~(argon2id-derive-key pw salt #:iterations #:memory-kib #:length)~ | a key from a password and a 16-byte salt |
| ~(make-argon2id-salt)~ | 16 random bytes; store them with the data |
| ~(argon2id-hash pw #:iterations #:memory-kib)~ | a PHC string to store: ~$argon2id$v=19$m=65536,t=2,p=1$...~ |
| ~(argon2id-verify pw stored)~ | true when the password gives the stored string |
| ~(argon2id-needs-rehash? stored #:iterations #:memory-kib)~ | true when the stored cost is less than the one given |
| ~(wipe! bytes)~ | sets every byte to 0, with ~sodium_memzero~ |
| Cipher | Key | Nonce | Tag |
|---------------------+-----+-------+-----|
| ~Aegis256~ | 32 | 32 | 32 |
| ~XChaCha20Poly1305~ | 32 | 24 | 16 |
The lengths are in bytes. ~#:aad~ is the associated data. It is not
encrypted, but decryption returns ~None~ when it is not the same as when the
data was encrypted. Use it for a header that must be read before the data is
decrypted, such as a version, a revision or a device id.
*Nonces.* Both ciphers have nonces large enough to be random, so a random
nonce for each message is safe and there is no counter to keep. Never encrypt
two messages with the same key and nonce.
*Errors.* Decryption returns ~None~ when the key, the nonce, the associated
data or the bytes are not the ones that were used. A key, a nonce or a salt
of the wrong length raises ~ArgumentException~, and so does an Argon2id cost
out of range. ~argon2id-verify~ and ~argon2id-needs-rehash?~ raise
~ArgumentException~ for a stored string that is not an Argon2id PHC string:
that is an error in the stored data, not a wrong password.
*Threads and fibers.* The Argon2id functions take a core for tens to hundreds
of milliseconds. In a bjoroutine they run on a thread of their own, so the
fiber pool is not blocked. Encryption is fast and runs on the caller's thread.
** Which cipher
Both ciphers are safe. The choice is about speed, and the speed depends on
the CPU.
- *AEGIS-256 is fast where the CPU has AES instructions*: the ARMv8 AES
instructions on 64-bit ARM, or AES-NI and AVX on x86. libsodium examines
the CPU when it starts and uses those instructions when they are there.
- *Without them*, libsodium runs AES in software, and then AEGIS-256 is
slower than XChaCha20-Poly1305. XChaCha20-Poly1305 needs no special
instructions.
- ~(aegis256-accelerated?)~ makes the same test as libsodium, from what .NET
reports about the CPU, and ~(preferred-cipher)~ chooses from it.
*On Android phones AEGIS-256 is kept*, because a moderately modern phone has
the AES instructions:
- The 64-bit Android library in the libsodium package has libsodium's
ARM code for AEGIS. The disassembly of
~runtimes/android-arm64/native/libsodium.so~ in libsodium 1.0.20.2 has 301
~aese~ instructions.
- SUPERCOP measured libsodium's AEGIS-256 on a Raspberry Pi 5. Its CPU is a
Cortex-A76, which is also the large core of many phones from 2019 to 2021.
AEGIS-256 took about 4,440 cycles in the test where the fastest AES-256-GCM
took about 7,860.
- The ARMv8 AES instructions are optional. Most 64-bit phone CPUs have them.
Some low-cost CPUs do not, and Android encrypts the storage of those phones
with Adiantum, not AES. On such a phone, ~preferred-cipher~ returns
~XChaCha20Poly1305~.
- The 32-bit ARM libraries (~android-arm~, ~linux-arm~) have no ~aese~
instructions, so a 32-bit process always runs AEGIS in software.
~aegis256-accelerated?~ is false there, because .NET has the ARM AES
instructions only on 64-bit ARM.
Not measured on a phone: this package was run only on Linux x64.
*Write the cipher into the data.* A file encrypted on one device must open on
the others. Put the cipher in the header, and include the header in
~#:aad~. Each device can then decrypt both, whichever it prefers to write.
On an AMD Ryzen 9 5900X, from ~bench/ciphers.bjo~. A call makes a new array
for its result, and the times include it. At 1 MiB that array is most of the
difference between this table and libsodium's own speed.
| Operation | 1 KiB | 1 MiB |
|----------------------------+--------+--------|
| encrypt AEGIS-256 | 139 ns | 244 µs |
| encrypt XChaCha20-Poly1305 | 774 ns | 666 µs |
| decrypt AEGIS-256 | 148 ns | 262 µs |
| decrypt XChaCha20-Poly1305 | 786 ns | 729 µs |
** Argon2id
libsodium's Argon2id is version 1.3 with one lane (~p=1~). The cost is the
number of iterations (~t~) and the memory in KiB (~m~). ~argon2id-verify~
also accepts a string made elsewhere with more lanes.
| Cost | Name in libsodium | Here on a Ryzen 9 5900X |
|--------------------------------+-------------------+-------------------------|
| ~t=2~, ~m=65536~ (64 MiB), the default | INTERACTIVE | 50 ms |
| ~t=3~, ~m=262144~ (256 MiB) | MODERATE | 293 ms |
- A key that unlocks a vault is derived once for each unlock, and an
attacker with the stored data can try passwords offline. Choose the
highest cost that the slowest device accepts, and measure it on that
device.
- The memory must be available. 256 MiB can be too much for a phone that
is short of memory.
- Store the salt and the cost with the data, so that a device can derive the
same key, and so that the cost can increase later.
- ~argon2id-derive-key~ takes a salt of exactly 16 bytes, and gives 32 bytes
unless ~#:length~ says otherwise (at least 16).
- The memory is from 8 KiB to 2097151 KiB (just under 2 GiB).
** Versions
LibSodium.Net 0.16.0-alpha is a pre-release. When it starts, it raises
~LibSodiumException~ if the native library is not the libsodium 1.0.x that it
was built for, which is 1.0.20. So the manifest fixes both:
- LibSodium.Net exactly 0.16.0-alpha;
- libsodium from 1.0.20.2, which is the first build of 1.0.20 with the Android
libraries, up to but not including 1.0.21.
libsodium 1.0.21 makes Argon2 faster on ARM with NEON. To get it, change both
versions together, when a version of LibSodium.Net for 1.0.21 or later is
released.
** Not here
LibSodium.Net has more of libsodium than this package exposes: secret streams,
HKDF, public-key encryption, signatures. Add them when they are needed.
Data goes in and out as arrays, not as spans.
** Try it
#+BEGIN_SRC sh
bjo run tests/core.bjo # every line should start with "ok:"
bjo run examples/vault.bjo # the example above
bjo bench bench/ciphers.bjo --all
#+END_SRC
The tests use libsodium's own test vectors for AEGIS-256,
XChaCha20-Poly1305 and Argon2id.