2026-11-21 21:07:27 +01:00
|
|
|
* 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))
|
2026-11-22 08:13:50 +01:00
|
|
|
(def cost argon2id-interactive)
|
|
|
|
|
(def key (argon2id-derive-key "correct horse" salt cost))
|
2026-11-21 21:07:27 +01:00
|
|
|
|
2026-11-22 08:13:50 +01:00
|
|
|
;; Encrypt. The header is not secret, but a change to it is found. Data
|
|
|
|
|
;; that other devices open must say which cipher it was sealed with.
|
|
|
|
|
(def cipher XChaCha20Poly1305)
|
2026-11-21 21:07:27 +01:00
|
|
|
(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"))
|
2026-11-22 08:13:50 +01:00
|
|
|
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium")))))
|
2026-11-21 21:07:27 +01:00
|
|
|
#+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
|
|
|
|
|
|
2026-11-22 08:13:50 +01:00
|
|
|
~(bjosodium core)~:
|
|
|
|
|
|
|
|
|
|
| ~(make-aead-key)~ | a new random ~AeadKey~ |
|
|
|
|
|
| ~(bytes->aead-key bytes)~ ~(aead-key->bytes key)~ | a key from 32 bytes, and a copy of its bytes |
|
|
|
|
|
| ~(wipe-aead-key! key)~ | sets the bytes of the key to 0 |
|
2026-11-21 21:07:27 +01:00
|
|
|
| ~(aead-seal c key data #:aad ad)~ | a new random nonce, the ciphertext and the tag, in one array |
|
2026-11-22 08:13:50 +01:00
|
|
|
| ~(aead-unseal c key sealed #:aad ad)~ | ~(Some data)~, or ~None~ when the tag does not verify |
|
|
|
|
|
| ~(cipher-nonce-length c)~ ~(cipher-tag-length c)~ ~aead-key-length~ | in bytes |
|
2026-11-21 21:07:27 +01:00
|
|
|
| ~(aegis256-accelerated?)~ | true when this CPU runs AEGIS-256 on AES instructions |
|
|
|
|
|
| ~(preferred-cipher)~ | ~Aegis256~ when it is accelerated, else ~XChaCha20Poly1305~ |
|
2026-11-22 08:13:50 +01:00
|
|
|
| ~(make-argon2id-cost t m)~ | an ~Argon2idCost~: ~t~ iterations over ~m~ KiB |
|
|
|
|
|
| ~argon2id-interactive~ ~argon2id-moderate~ ~argon2id-sensitive~ | libsodium's three costs |
|
|
|
|
|
| ~(argon2id-derive-key pw salt cost)~ | an ~AeadKey~ from a password and a 16-byte salt |
|
2026-11-21 21:07:27 +01:00
|
|
|
| ~(make-argon2id-salt)~ | 16 random bytes; store them with the data |
|
2026-11-22 08:13:50 +01:00
|
|
|
| ~(argon2id-hash pw cost)~ | a PHC string to store: ~$argon2id$v=19$m=65536,t=2,p=1$...~ |
|
2026-11-21 21:07:27 +01:00
|
|
|
| ~(argon2id-verify pw stored)~ | true when the password gives the stored string |
|
2026-11-22 08:13:50 +01:00
|
|
|
| ~(argon2id-needs-rehash? stored cost)~ | true when the stored cost is less than ~cost~ |
|
|
|
|
|
| ~(random-bytes n)~ | ~n~ bytes from the system's random source |
|
2026-11-21 21:07:27 +01:00
|
|
|
| ~(wipe! bytes)~ | sets every byte to 0, with ~sodium_memzero~ |
|
|
|
|
|
|
2026-11-22 08:13:50 +01:00
|
|
|
~(bjosodium raw)~, for a format that stores the nonce in its own place:
|
|
|
|
|
|
|
|
|
|
| ~(make-aead-nonce c)~ | a new random ~AeadNonce~ of cipher ~c~ |
|
|
|
|
|
| ~(bytes->aead-nonce c bytes)~ ~(aead-nonce->bytes n)~ | a nonce read back from stored bytes, and a copy of its bytes |
|
|
|
|
|
| ~(aead-encrypt key nonce data #:aad ad)~ | the ciphertext and the tag; the caller stores the nonce |
|
|
|
|
|
| ~(aead-decrypt key nonce ct #:aad ad)~ | ~(Some data)~, or ~None~ |
|
|
|
|
|
|
|
|
|
|
*Keys and nonces are types.* ~AeadKey~ and ~AeadNonce~ are opaque, so a key
|
|
|
|
|
given where a nonce is wanted is a type error. For AEGIS-256 both are 32
|
|
|
|
|
bytes, and as arrays a swap would not be found: the key would then be stored
|
|
|
|
|
in clear beside the data. Both print as ~#<AeadKey>~ and ~#<AeadNonce>~, not
|
|
|
|
|
as their bytes. A nonce knows its cipher, so ~aead-encrypt~ and
|
|
|
|
|
~aead-decrypt~ take none.
|
|
|
|
|
|
|
|
|
|
*Costs are explicit.* No Argon2id function has a default cost. A key made
|
|
|
|
|
with a default would not be made again if a later version changed the
|
|
|
|
|
default, and the data would not open. Store the cost with the data.
|
|
|
|
|
|
|
|
|
|
*Passwords are normalized to NFC.* A password typed on two devices can come
|
|
|
|
|
as different code points: ~é~ as one, or as ~e~ and a combining accent. Both
|
|
|
|
|
are normalized to Unicode NFC before Argon2id, so they give the same key. It
|
|
|
|
|
is NFC and not NFKC, as in RFC 8265: ~fi~ and ~fi~ are different passwords.
|
|
|
|
|
|
2026-11-21 21:07:27 +01:00
|
|
|
| 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.
|
|
|
|
|
|
2026-11-22 08:13:50 +01:00
|
|
|
*Nonces.* ~aead-seal~ makes a random nonce for each message, which is safe
|
|
|
|
|
with both ciphers: their nonces are large enough to be random, and there is
|
|
|
|
|
no counter to keep. With ~(bjosodium raw)~, make each nonce with
|
|
|
|
|
~make-aead-nonce~, and never encrypt two messages with the same key and
|
|
|
|
|
nonce.
|
2026-11-21 21:07:27 +01:00
|
|
|
|
|
|
|
|
*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
|
2026-11-22 08:13:50 +01:00
|
|
|
of the wrong length raises ~ArgumentException~, and so does
|
|
|
|
|
~make-argon2id-cost~ with a cost out of range. ~argon2id-verify~ and ~argon2id-needs-rehash?~ raise
|
2026-11-21 21:07:27 +01:00
|
|
|
~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 |
|
|
|
|
|
|--------------------------------+-------------------+-------------------------|
|
2026-11-22 08:13:50 +01:00
|
|
|
| ~t=2~, ~m=65536~ (64 MiB), ~argon2id-interactive~ | INTERACTIVE | 50 ms |
|
|
|
|
|
| ~t=3~, ~m=262144~ (256 MiB), ~argon2id-moderate~ | MODERATE | 293 ms |
|
|
|
|
|
| ~t=4~, ~m=1048576~ (1 GiB), ~argon2id-sensitive~ | SENSITIVE | not measured |
|
2026-11-21 21:07:27 +01:00
|
|
|
|
|
|
|
|
- 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.
|
2026-11-22 08:13:50 +01:00
|
|
|
- ~argon2id-derive-key~ takes a salt of exactly 16 bytes, and gives an
|
|
|
|
|
~AeadKey~ of 32 bytes.
|
2026-11-21 21:07:27 +01:00
|
|
|
- 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.
|
|
|
|
|
|
2026-11-22 08:13:50 +01:00
|
|
|
~(bjosodium impl)~ is the implementation of both modules. Import ~core~ and
|
|
|
|
|
~raw~, not ~impl~.
|
|
|
|
|
|
2026-11-21 21:07:27 +01:00
|
|
|
** 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.
|