Keys and nonces are types; Argon2id costs are explicit; passwords are NFC
- AeadKey and AeadNonce are opaque types. 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 store the key in clear. Both print as #<AeadKey> and #<AeadNonce>. - A nonce knows its cipher, so aead-encrypt and aead-decrypt take none. They and the nonce functions move to (bjosodium raw). (bjosodium core) keeps aead-seal and aead-unseal, which make the nonce. - Every Argon2id function takes an Argon2idCost. There are no defaults: a key made with a default that a later version changed would not be made again. argon2id-interactive, -moderate and -sensitive are libsodium's three costs. - argon2id-derive-key returns an AeadKey of 32 bytes. #:length is gone. - Passwords are normalized to Unicode NFC before Argon2id. - random-bytes. make-aead-key takes no cipher: both keys are 32 bytes. - (bjosodium impl) holds the code; core and raw are facades of it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
911f4bcc46
commit
2b42abb6d9
8 changed files with 738 additions and 522 deletions
78
Readme.org
78
Readme.org
|
|
@ -11,10 +11,12 @@ binding [[https://libsodium.net][LibSodium.Net]].
|
|||
(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))
|
||||
(def cost argon2id-interactive)
|
||||
(def key (argon2id-derive-key "correct horse" salt cost))
|
||||
|
||||
;; Encrypt. The header is not secret, but a change to it is found.
|
||||
(def cipher (preferred-cipher))
|
||||
;; 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)
|
||||
(def header (string->bytes "bookmarks v1"))
|
||||
(def sealed (aead-seal cipher key (string->bytes "https://example.org") #:aad header))
|
||||
|
||||
|
|
@ -29,7 +31,7 @@ binding [[https://libsodium.net][LibSodium.Net]].
|
|||
(depends
|
||||
(package (name (bjosodium))
|
||||
(version (version-at-least "0.1.0"))
|
||||
(source (path (dir "../bjosodium")))))
|
||||
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium")))))
|
||||
#+END_SRC
|
||||
|
||||
A program using this package declares no NuGet packages of its own. The
|
||||
|
|
@ -40,21 +42,49 @@ 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 |
|
||||
~(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 |
|
||||
| ~(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 |
|
||||
| ~(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 |
|
||||
| ~(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-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 |
|
||||
| ~(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-hash pw cost)~ | 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 |
|
||||
| ~(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 |
|
||||
| ~(wipe! bytes)~ | sets every byte to 0, with ~sodium_memzero~ |
|
||||
|
||||
~(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.
|
||||
|
||||
| Cipher | Key | Nonce | Tag |
|
||||
|---------------------+-----+-------+-----|
|
||||
| ~Aegis256~ | 32 | 32 | 32 |
|
||||
|
|
@ -65,14 +95,16 @@ 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.
|
||||
*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.
|
||||
|
||||
*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
|
||||
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
|
||||
~ArgumentException~ for a stored string that is not an Argon2id PHC string:
|
||||
that is an error in the stored data, not a wrong password.
|
||||
|
||||
|
|
@ -139,8 +171,9 @@ 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 |
|
||||
| ~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 |
|
||||
|
||||
- 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
|
||||
|
|
@ -150,8 +183,8 @@ also accepts a string made elsewhere with more lanes.
|
|||
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).
|
||||
- ~argon2id-derive-key~ takes a salt of exactly 16 bytes, and gives an
|
||||
~AeadKey~ of 32 bytes.
|
||||
- The memory is from 8 KiB to 2097151 KiB (just under 2 GiB).
|
||||
|
||||
** Versions
|
||||
|
|
@ -174,6 +207,9 @@ 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.
|
||||
|
||||
~(bjosodium impl)~ is the implementation of both modules. Import ~core~ and
|
||||
~raw~, not ~impl~.
|
||||
|
||||
** Try it
|
||||
|
||||
#+BEGIN_SRC sh
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue