bjosodium 0.2.0: keys are AeadKey, nonces AeadNonce, costs explicit

- The DEK of a vault, its earlier keys and the KEKs are AeadKey. Unlocked
  and the vault hold them so; copies of their bytes are wiped after a
  wrap or an unwrap.
- Nonces are AeadNonce, made with (bjosodium raw) and read back from the
  files with bytes->aead-nonce.
- The Argon2id cost of a slot is given as an Argon2idCost, from KeyCost.
- A key id comes from random-bytes.
- The file formats do not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-11-22 09:59:59 +01:00
parent a4b2aaf94e
commit 3e07b6be4c
6 changed files with 80 additions and 54 deletions

View file

@ -28,6 +28,9 @@
(: (-> (Array byte) (Array byte)))
(defun (copy-of a) (get-bytes a 0 (array-length a)))
(: (-> AeadKey AeadKey bool))
(defun (same-key? a b) (bytes=? (aead-key->bytes a) (aead-key->bytes b)))
;; A copy of `a` with bit 0 of byte `at` changed.
(: (-> (Array byte) int (Array byte)))
(defun (flipped a at)
@ -52,8 +55,8 @@
(def (Ok opened) (unlock-keys file "hunter2") :leave-with (check-fail "it did not open"))
(check (= (.vault-id opened) (.vault-id made)))
(check (= (.key-id opened) 1))
(check (bytes=? (.dek opened) (.dek made)))
(check (= (array-length (.dek made)) 32))
(check (same-key? (.dek opened) (.dek made)))
(check (= (array-length (aead-key->bytes (.dek made))) 32))
(check (= (keys-vault-id file) (Ok (.vault-id made)))))
(deftest "each cipher wraps the key"
@ -75,14 +78,14 @@
(def (Tuple _f1 a) (make-keys "pw" #:cost cheap))
(def (Tuple _f2 b) (make-keys "pw" #:cost cheap))
(check (not (= (.vault-id a) (.vault-id b))))
(check (not (bytes=? (.dek a) (.dek b)))))
(check (not (same-key? (.dek a) (.dek b)))))
(deftest "a new password keeps the vault and its key"
(def (Tuple old made) (make-keys "old password" #:cost cheap))
(def new (wrap-keys (.vault-id made) (.key-id made) (.dek made) "new password" #:cost cheap))
(check (= (outcome (unlock-keys new "old password")) "WrongPassword"))
(def (Ok opened) (unlock-keys new "new password") :leave-with (check-fail "it did not open"))
(check (bytes=? (.dek opened) (.dek made)))
(check (same-key? (.dek opened) (.dek made)))
(check (= (.vault-id opened) (.vault-id made)))
(check (= (outcome (unlock-keys old "old password")) "Ok")))
@ -135,13 +138,13 @@
(def (Tuple new rotated) (new-keys made "new password" #:cost cheap))
(check (= (.vault-id rotated) (.vault-id made)))
(check (not (= (.key-id rotated) (.key-id made))))
(check (not (bytes=? (.dek rotated) (.dek made))))
(check (not (same-key? (.dek rotated) (.dek made))))
;; The new file opens with the new password, and gives the earlier key.
(def (Ok opened) (unlock-keys new "new password") :leave-with (check-fail "it did not open"))
(check (= (.key-id opened) (.key-id rotated)))
(check (bytes=? (.dek opened) (.dek rotated)))
(check (same-key? (.dek opened) (.dek rotated)))
(check (= (vec-map fst (.earlier opened)) [1]))
(check (bytes=? (snd (vec-ref (.earlier opened) 0)) (.dek made)))
(check (same-key? (snd (vec-ref (.earlier opened) 0)) (.dek made)))
(check (= (outcome (unlock-keys new "old password")) "WrongPassword"))
;; The old file and the old password give the old key only.
(def (Ok was) (unlock-keys old "old password") :leave-with (check-fail "the old file did not open"))

View file

@ -543,7 +543,7 @@
(def a0 (vault-save (fst (add-bookmark (create f (.one f)) "https://example.org/a" []))))
(def mine (own-keys f (.one f)))
;; The same vault id, another key, and another password.
(set-shared-keys! f (wrap-keys (vault-id a0) 7 (make-aead-key XChaCha20Poly1305) "not yours" #:cost cheap))
(set-shared-keys! f (wrap-keys (vault-id a0) 7 (make-aead-key) "not yours" #:cost cheap))
(def (Tuple a1 report) (sync (opened (reopen f (.one f) "hunter2"))))
(check (told? report "holds none of this device's keys"))
(check (= (urls a1) ["https://example.org/a"]))