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:
Linus Björnstam 2026-11-22 08:13:50 +01:00
parent 911f4bcc46
commit 2b42abb6d9
8 changed files with 738 additions and 522 deletions

View file

@ -2,8 +2,9 @@
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; bjosodium/tests/core.bjo — (bjosodium core) against libsodium's own test
;; vectors, and the failures that must give `None` or raise.
;; bjosodium/tests/core.bjo — (bjosodium core) and (bjosodium raw) against
;; libsodium's own test vectors, and the failures that must give `None` or
;; raise.
;;
;; bjo run tests/core.bjo
;;
@ -24,6 +25,7 @@
(import (std simpletest))
(import (std crypto hash))
(import (bjosodium core))
(import (bjosodium raw))
(import/class
(ArgumentException (: System.ArgumentException (-> string ArgumentException))))
@ -56,16 +58,22 @@
(: (-> (Array byte) int (Array byte)))
(defun (first-bytes bytes n) (ro-span->array (array->ro-span bytes #:to n)))
(: (-> AeadKey string))
(defun (key-hex key) (bytes->hex (aead-key->bytes key)))
(: Argon2idCost)
(def cheap (make-argon2id-cost 1 8192))
;; The non-bjoroutine versions, called from `main` through these.
(: (-> string (Array byte) int int (Array byte)))
(: (-> string (Array byte) int int string))
(defun (derive/sync password salt t m)
(argon2id-derive-key password salt #:iterations t #:memory-kib m))
(key-hex (argon2id-derive-key password salt (make-argon2id-cost t m))))
(: (-> string string bool))
(defun (verify/sync password stored) (argon2id-verify password stored))
(: (-> string string))
(defun (hash/sync password) (argon2id-hash password #:iterations 1 #:memory-kib 8192))
(defun (hash/sync password) (argon2id-hash password cheap))
;; ---- Vectors ----------------------------------------------------------------
@ -105,37 +113,47 @@
(: (-> Cipher void))
(defun (check-cipher cipher)
(def name (written cipher))
(def key (make-aead-key cipher))
(def key (make-aead-key))
(def nonce (make-aead-nonce cipher))
(def aad (string->bytes "header, revision 7"))
(def message (string->bytes "https://example.org/ — a bookmark"))
(def boxed (aead-encrypt cipher key nonce message #:aad aad))
(def boxed (aead-encrypt key nonce message #:aad aad))
(def tag-length (cipher-tag-length cipher))
(expect #"${name}: a new nonce has the cipher's length" (array-length (aead-nonce->bytes nonce))
(cipher-nonce-length cipher))
(expect #"${name}: a nonce knows its cipher" (aead-nonce-cipher nonce) cipher)
(expect #"${name}: the ciphertext is a tag longer" (array-length boxed)
(+ (array-length message) tag-length))
(expect #"${name}: round trip" (opened (aead-decrypt cipher key nonce boxed #:aad aad))
(expect #"${name}: round trip" (opened (aead-decrypt key nonce boxed #:aad aad))
(bytes->hex message))
(expect #"${name}: a changed ciphertext byte" (opened (aead-decrypt cipher key nonce (flipped boxed 0) #:aad aad))
(expect #"${name}: a changed ciphertext byte" (opened (aead-decrypt key nonce (flipped boxed 0) #:aad aad))
"None")
(expect #"${name}: a changed tag byte"
(opened (aead-decrypt cipher key nonce (flipped boxed (- (array-length boxed) 1)) #:aad aad))
(opened (aead-decrypt key nonce (flipped boxed (- (array-length boxed) 1)) #:aad aad))
"None")
(expect #"${name}: another key" (opened (aead-decrypt cipher (make-aead-key cipher) nonce boxed #:aad aad))
(expect #"${name}: another key" (opened (aead-decrypt (make-aead-key) nonce boxed #:aad aad))
"None")
(expect #"${name}: another nonce" (opened (aead-decrypt cipher key (flipped nonce 3) boxed #:aad aad))
(expect #"${name}: another nonce"
(opened (aead-decrypt key (bytes->aead-nonce cipher (flipped (aead-nonce->bytes nonce) 3)) boxed #:aad aad))
"None")
(expect #"${name}: the nonce read back from its bytes"
(opened (aead-decrypt key (bytes->aead-nonce cipher (aead-nonce->bytes nonce)) boxed #:aad aad))
(bytes->hex message))
(expect #"${name}: the key read back from its bytes"
(opened (aead-decrypt (bytes->aead-key (aead-key->bytes key)) nonce boxed #:aad aad))
(bytes->hex message))
(expect #"${name}: other associated data"
(opened (aead-decrypt cipher key nonce boxed #:aad (string->bytes "header, revision 6")))
(opened (aead-decrypt key nonce boxed #:aad (string->bytes "header, revision 6")))
"None")
(expect #"${name}: no associated data" (opened (aead-decrypt cipher key nonce boxed)) "None")
(expect #"${name}: cut short" (opened (aead-decrypt cipher key nonce (first-bytes boxed (- (array-length boxed) 1)) #:aad aad))
(expect #"${name}: no associated data" (opened (aead-decrypt key nonce boxed)) "None")
(expect #"${name}: cut short" (opened (aead-decrypt key nonce (first-bytes boxed (- (array-length boxed) 1)) #:aad aad))
"None")
(expect #"${name}: shorter than a tag" (opened (aead-decrypt cipher key nonce (first-bytes boxed (- tag-length 1)) #:aad aad))
(expect #"${name}: shorter than a tag" (opened (aead-decrypt key nonce (first-bytes boxed (- tag-length 1)) #:aad aad))
"None")
;; The empty message: only the tag.
(def empty (aead-encrypt cipher key nonce (make-array 0)))
(def empty (aead-encrypt key nonce (make-array 0)))
(expect #"${name}: an empty message is a tag" (array-length empty) tag-length)
(expect #"${name}: an empty message opens" (opened (aead-decrypt cipher key nonce empty)) "")
(expect #"${name}: an empty message opens" (opened (aead-decrypt key nonce empty)) "")
;; Sealed: a new nonce in front.
(def sealed (aead-seal cipher key message #:aad aad))
(expect #"${name}: sealed is a nonce and a tag longer" (array-length sealed)
@ -153,44 +171,59 @@
(def sealed-empty (aead-seal cipher key (make-array 0)))
(expect #"${name}: an empty message sealed" (opened (aead-unseal cipher key sealed-empty)) "")
;; Wrong lengths are errors in the program.
(expect #"${name}: a short key raises" (raises? #(aead-encrypt cipher (make-array 16) nonce message)) #t)
(expect #"${name}: a short nonce raises" (raises? #(aead-encrypt cipher key (make-array 12) message)) #t)
(expect #"${name}: decrypt with a short key raises" (raises? #(aead-decrypt cipher (make-array 16) nonce boxed)) #t)
(expect #"${name}: decrypt with a long nonce raises" (raises? #(aead-decrypt cipher key (make-array 40) boxed)) #t)
(expect #"${name}: seal with a short key raises" (raises? #(aead-seal cipher (make-array 31) message)) #t)
(expect #"${name}: unseal with a short key raises" (raises? #(aead-unseal cipher (make-array 31) sealed)) #t))
(expect #"${name}: a nonce from too few bytes raises" (raises? #(bytes->aead-nonce cipher (make-array 12))) #t)
(expect #"${name}: a nonce from too many bytes raises" (raises? #(bytes->aead-nonce cipher (make-array 40))) #t))
(defbjo (main args)
;; ---- Lengths --------------------------------------------------------------
(expect "AEGIS-256 key" (cipher-key-length Aegis256) 32)
(expect "a key" aead-key-length 32)
(expect "AEGIS-256 nonce" (cipher-nonce-length Aegis256) 32)
(expect "AEGIS-256 tag" (cipher-tag-length Aegis256) 32)
(expect "XChaCha20-Poly1305 key" (cipher-key-length XChaCha20Poly1305) 32)
(expect "XChaCha20-Poly1305 nonce" (cipher-nonce-length XChaCha20Poly1305) 24)
(expect "XChaCha20-Poly1305 tag" (cipher-tag-length XChaCha20Poly1305) 16)
(expect "a key has the key length" (array-length (make-aead-key XChaCha20Poly1305)) 32)
(expect "a nonce has the nonce length" (array-length (make-aead-nonce XChaCha20Poly1305)) 24)
(expect-true "two keys differ"
(not (= (bytes->hex (make-aead-key Aegis256)) (bytes->hex (make-aead-key Aegis256)))))
(expect "a new key has the key length" (array-length (aead-key->bytes (make-aead-key))) 32)
(expect-true "two keys differ" (not (= (key-hex (make-aead-key)) (key-hex (make-aead-key)))))
(expect "the preferred cipher follows the CPU" (preferred-cipher)
(if (aegis256-accelerated?) Aegis256 XChaCha20Poly1305))
(println #"note: aegis256-accelerated? is ${(written (aegis256-accelerated?))} here")
;; ---- Keys -----------------------------------------------------------------
(def bytes32 (random-bytes 32))
(expect "a key from bytes has them" (key-hex (bytes->aead-key bytes32)) (bytes->hex bytes32))
(def from-bytes (bytes->aead-key bytes32))
(wipe! bytes32)
(expect-true "a key from bytes is a copy of them" (not (= (key-hex from-bytes) (bytes->hex bytes32))))
(def out (aead-key->bytes from-bytes))
(wipe! out)
(expect-true "the bytes of a key are a copy" (not (= (key-hex from-bytes) (bytes->hex out))))
(expect "a key from 31 bytes raises" (raises? #(bytes->aead-key (make-array 31))) #t)
(expect "a key from 33 bytes raises" (raises? #(bytes->aead-key (make-array 33))) #t)
(expect "a key is not printed" (displayed (make-aead-key)) "#<AeadKey>")
(expect "a nonce is not printed" (displayed (make-aead-nonce Aegis256)) "#<AeadNonce>")
(def wiped (make-aead-key))
(wipe-aead-key! wiped)
(expect "wipe-aead-key! clears every byte" (key-hex wiped) (bytes->hex (make-array 32)))
;; ---- Known answers --------------------------------------------------------
(expect "AEGIS-256 vector" (bytes->hex (aead-encrypt Aegis256 aegis-key aegis-nonce aegis-message #:aad aegis-ad))
(def aegis (bytes->aead-nonce Aegis256 aegis-nonce))
(expect "AEGIS-256 vector"
(bytes->hex (aead-encrypt (bytes->aead-key aegis-key) aegis aegis-message #:aad aegis-ad))
(str aegis-ciphertext aegis-tag))
(expect "AEGIS-256 vector decrypts"
(opened (aead-decrypt Aegis256 aegis-key aegis-nonce (unhex (str aegis-ciphertext aegis-tag)) #:aad aegis-ad))
(opened (aead-decrypt (bytes->aead-key aegis-key) aegis (unhex (str aegis-ciphertext aegis-tag))
#:aad aegis-ad))
(bytes->hex aegis-message))
(def xchacha (bytes->aead-nonce XChaCha20Poly1305 xchacha-nonce))
(expect "XChaCha20-Poly1305 vector"
(bytes->hex (aead-encrypt XChaCha20Poly1305 xchacha-key xchacha-nonce xchacha-message #:aad xchacha-ad))
(bytes->hex (aead-encrypt (bytes->aead-key xchacha-key) xchacha xchacha-message #:aad xchacha-ad))
(str xchacha-ciphertext xchacha-tag))
(expect "XChaCha20-Poly1305 vector decrypts"
(opened (aead-decrypt XChaCha20Poly1305 xchacha-key xchacha-nonce
(opened (aead-decrypt (bytes->aead-key xchacha-key) xchacha
(unhex (str xchacha-ciphertext xchacha-tag)) #:aad xchacha-ad))
(bytes->hex xchacha-message))
(expect "a ciphertext of one cipher does not open with the other"
(opened (aead-decrypt XChaCha20Poly1305 aegis-key (first-bytes aegis-nonce 24)
(opened (aead-decrypt (bytes->aead-key aegis-key)
(bytes->aead-nonce XChaCha20Poly1305 (first-bytes aegis-nonce 24))
(unhex (str aegis-ciphertext aegis-tag)) #:aad aegis-ad))
"None")
@ -198,16 +231,29 @@
(check-cipher Aegis256)
(check-cipher XChaCha20Poly1305)
;; ---- Argon2id: costs ------------------------------------------------------
(expect "interactive" (Tuple (argon2id-cost-iterations argon2id-interactive)
(argon2id-cost-memory-kib argon2id-interactive))
(Tuple 2 65536))
(expect "moderate" (Tuple (argon2id-cost-iterations argon2id-moderate) (argon2id-cost-memory-kib argon2id-moderate))
(Tuple 3 262144))
(expect "sensitive" (Tuple (argon2id-cost-iterations argon2id-sensitive)
(argon2id-cost-memory-kib argon2id-sensitive))
(Tuple 4 1048576))
(expect "no iterations" (raises? #(make-argon2id-cost 0 4096)) #t)
(expect "too little memory" (raises? #(make-argon2id-cost 1 7)) #t)
(expect "too much memory" (raises? #(make-argon2id-cost 1 2097152)) #t)
(expect "the smallest cost" (argon2id-cost-memory-kib (make-argon2id-cost 1 8)) 8)
;; ---- Argon2id: known answers ----------------------------------------------
(def salt-4 (string->bytes ">A 16-bytes salt"))
(expect "derive: tv3 entry 4"
(bytes->hex (argon2id-derive-key argon-password-4 (string->bytes ">A 16-bytes salt")
#:iterations 19 #:memory-kib 4096))
(key-hex (argon2id-derive-key argon-password-4 salt-4 (make-argon2id-cost 19 4096)))
argon-hash-4)
(expect "derive: tv3 entry 4, from a plain function"
(bytes->hex (derive/sync argon-password-4 (string->bytes ">A 16-bytes salt") 19 4096))
(expect "derive: tv3 entry 4, from a plain function" (derive/sync argon-password-4 salt-4 19 4096)
argon-hash-4)
(expect "derive: tv3 entry 2, the empty password"
(bytes->hex (argon2id-derive-key "" argon-salt-2 #:iterations 2 #:memory-kib 4882))
(key-hex (argon2id-derive-key "" argon-salt-2 (make-argon2id-cost 2 4882)))
argon-hash-2)
(expect "verify: tv3 entry 4" (argon2id-verify argon-password-4 argon-stored-4) #t)
(expect "verify: tv3 entry 4, from a plain function" (verify/sync argon-password-4 argon-stored-4) #t)
@ -229,21 +275,34 @@
"$argon2id$v=19$m=4096,t=18,p=1$PkEgMTYtYnl0ZXMgc2FsdA$ltB/ue1kPtBMBGfsysMpPigE6hiNEKZ9vs8vLNVDQGA")
#f)
;; ---- Argon2id: Unicode NFC ------------------------------------------------
;; "é" as one code point, U+00E9, and as "e" and U+0301.
(def composed "caf\u{E9}")
(def decomposed "cafe\u{301}")
(expect-true "the two forms are different strings" (not (= composed decomposed)))
(expect "derive: the two forms give one key"
(key-hex (argon2id-derive-key decomposed salt-4 cheap))
(key-hex (argon2id-derive-key composed salt-4 cheap)))
(expect "verify: a hash of one form verifies the other" (argon2id-verify decomposed (hash/sync composed)) #t)
(expect "derive: NFC, not NFKC: the ligature fi is not fi"
(= (key-hex (argon2id-derive-key "\u{FB01}" salt-4 cheap)) (key-hex (argon2id-derive-key "fi" salt-4 cheap)))
#f)
;; ---- Argon2id: hash and verify --------------------------------------------
(def stored (argon2id-hash "correct horse" #:iterations 1 #:memory-kib 8192))
(def stored (argon2id-hash "correct horse" cheap))
(expect-true "a hash is a PHC string with its cost" (string-starts-with? stored "$argon2id$v=19$m=8192,t=1,p=1$"))
(expect "the hash verifies" (argon2id-verify "correct horse" stored) #t)
(expect "another password does not" (argon2id-verify "correct horsf" stored) #f)
(expect-true "two hashes of a password differ, by the salt"
(not (= stored (argon2id-hash "correct horse" #:iterations 1 #:memory-kib 8192))))
(not (= stored (argon2id-hash "correct horse" cheap))))
(expect "hashed from a plain function" (argon2id-verify "pw" (hash/sync "pw")) #t)
(expect "a password outside ASCII" (argon2id-verify "blåbär" (hash/sync "blåbär")) #t)
(expect "the default cost" (string-starts-with? (argon2id-hash "x") "$argon2id$v=19$m=65536,t=2,p=1$") #t)
(expect "needs rehash: less than the defaults" (argon2id-needs-rehash? stored) #t)
(expect "needs rehash: the same cost" (argon2id-needs-rehash? stored #:iterations 1 #:memory-kib 8192) #f)
(expect "needs rehash: more memory asked" (argon2id-needs-rehash? stored #:iterations 1 #:memory-kib 16384) #t)
(expect "needs rehash: more iterations asked" (argon2id-needs-rehash? stored #:iterations 2 #:memory-kib 8192) #t)
(expect "needs rehash: a cost higher than asked" (argon2id-needs-rehash? argon-stored-4 #:iterations 3 #:memory-kib 4096) #f)
(expect "needs rehash: less than interactive" (argon2id-needs-rehash? stored argon2id-interactive) #t)
(expect "needs rehash: the same cost" (argon2id-needs-rehash? stored cheap) #f)
(expect "needs rehash: more memory asked" (argon2id-needs-rehash? stored (make-argon2id-cost 1 16384)) #t)
(expect "needs rehash: more iterations asked" (argon2id-needs-rehash? stored (make-argon2id-cost 2 8192)) #t)
(expect "needs rehash: a cost higher than asked"
(argon2id-needs-rehash? argon-stored-4 (make-argon2id-cost 3 4096)) #f)
;; ---- Argon2id: errors in the program or in the stored data ----------------
(expect "a stored string that is not a hash" (raises? #(verify/sync "x" "hunter2")) #t)
@ -255,30 +314,25 @@
(expect "the fields in another order" (raises? #(verify/sync "x" "$argon2id$v=19$t=3,m=4096,p=1$c2FsdHNhbHQ$aGFzaGhhc2g")) #t)
(expect "no hash" (raises? #(verify/sync "x" "$argon2id$v=19$m=4096,t=3,p=1$c2FsdHNhbHQ$")) #t)
(expect "PBKDF2" (raises? #(verify/sync "x" "$pbkdf2-sha256$i=1$c2FsdA$Eg+2z/z4syxD5yJSVsT4N6hlSMkszDVICAWYfLcL4Xs")) #t)
(expect "needs rehash of a stored string that is not a hash" (raises? #(argon2id-needs-rehash? "hunter2")) #t)
(expect "needs rehash of a stored string that is not a hash" (raises? #(argon2id-needs-rehash? "hunter2" cheap)) #t)
(expect "a raise reaches a bjoroutine too"
(match (try (argon2id-verify "x" "nonsense") #:catch (System.ArgumentException))
((Ok _) "returned")
((Err _) "raised"))
"raised")
(def salt (string->bytes ">A 16-bytes salt"))
(expect "no iterations" (raises? #(derive/sync "pw" salt 0 4096)) #t)
(expect "too little memory" (raises? #(derive/sync "pw" salt 1 7)) #t)
(expect "too much memory" (raises? #(derive/sync "pw" salt 1 2097152)) #t)
(expect "a short salt" (raises? #(derive/sync "pw" (make-array 15) 1 4096)) #t)
(expect "a long salt" (raises? #(derive/sync "pw" (make-array 17) 1 4096)) #t)
(expect "a short key" (raises? #(argon2id-derive-key "pw" salt #:iterations 1 #:memory-kib 8 #:length 15)) #t)
(expect "a hash with no iterations" (raises? #(argon2id-hash "pw" #:iterations 0 #:memory-kib 8192)) #t)
(expect "the smallest cost works"
(array-length (argon2id-derive-key "pw" salt #:iterations 1 #:memory-kib 8 #:length 16)) 16)
(expect "a new salt has the salt length" (array-length (make-argon2id-salt)) argon2id-salt-length)
(expect-true "two salts differ"
(not (= (bytes->hex (make-argon2id-salt)) (bytes->hex (make-argon2id-salt)))))
(expect "a new salt derives a key"
(array-length (argon2id-derive-key "pw" (make-argon2id-salt) #:iterations 1 #:memory-kib 8)) 32)
(array-length (aead-key->bytes (argon2id-derive-key "pw" (make-argon2id-salt) (make-argon2id-cost 1 8)))) 32)
;; ---- Memory ---------------------------------------------------------------
(def secret (make-aead-key Aegis256))
;; ---- Random bytes and memory ----------------------------------------------
(expect "random-bytes" (array-length (random-bytes 7)) 7)
(expect "no random bytes" (array-length (random-bytes 0)) 0)
(expect "a negative count raises" (raises? #(random-bytes -1)) #t)
(def secret (random-bytes 32))
(wipe! secret)
(expect "wipe! clears every byte" (bytes->hex secret) (bytes->hex (make-array 32)))
0)