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:
parent
a4b2aaf94e
commit
3e07b6be4c
6 changed files with 80 additions and 54 deletions
4
bjo.lock
4
bjo.lock
|
|
@ -1,5 +1,5 @@
|
||||||
(lock
|
(lock
|
||||||
(locked
|
(locked
|
||||||
(package (name (bjosodium)) (version "0.1.0")
|
(package (name (bjosodium)) (version "0.2.0")
|
||||||
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium")))
|
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium")))
|
||||||
(commit "911f4bcc4676ca6393f8eb43286fdddd69cf6b5665aca0770ceaf869fb73767b"))))
|
(commit "2be75d9b22691b44d571ef4fa8516390ca9a47a4d64a3d92b39d6f9878627b13"))))
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
(depends
|
(depends
|
||||||
(package (name (bjosodium))
|
(package (name (bjosodium))
|
||||||
(version (version-at-least "0.1.0"))
|
(version (version-at-least "0.2.0"))
|
||||||
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium")))))
|
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium")))))
|
||||||
|
|
||||||
;; A library: there is no src/main.bjo.
|
;; A library: there is no src/main.bjo.
|
||||||
|
|
|
||||||
75
src/keys.bjo
75
src/keys.bjo
|
|
@ -5,8 +5,8 @@
|
||||||
;; bjomarks/src/keys.bjo — the module (bjomarks keys): `keys.bin`, the file
|
;; bjomarks/src/keys.bjo — the module (bjomarks keys): `keys.bin`, the file
|
||||||
;; that holds the key of a vault, encrypted with keys made from passwords.
|
;; that holds the key of a vault, encrypted with keys made from passwords.
|
||||||
;;
|
;;
|
||||||
;; - A vault has one data key (DEK): 32 random bytes that encrypt every
|
;; - A vault has one data key (DEK): a random key of 32 bytes that encrypts
|
||||||
;; device's file.
|
;; every device's file.
|
||||||
;; - `keys.bin` holds the DEK wrapped (encrypted) in one or more slots. A
|
;; - `keys.bin` holds the DEK wrapped (encrypted) in one or more slots. A
|
||||||
;; password slot wraps it with a key (KEK) that Argon2id makes from the
|
;; password slot wraps it with a key (KEK) that Argon2id makes from the
|
||||||
;; password and the slot's salt and cost.
|
;; password and the slot's salt and cost.
|
||||||
|
|
@ -65,6 +65,7 @@
|
||||||
(import (std prelude))
|
(import (std prelude))
|
||||||
(import (std uuid))
|
(import (std uuid))
|
||||||
(import (bjosodium core))
|
(import (bjosodium core))
|
||||||
|
(import (bjosodium raw))
|
||||||
(import (bjomarks binary))
|
(import (bjomarks binary))
|
||||||
|
|
||||||
(: string)
|
(: string)
|
||||||
|
|
@ -83,8 +84,8 @@
|
||||||
;; each with its key id, the latest first.
|
;; each with its key id, the latest first.
|
||||||
(type (: Unlocked (Record (: vault-id Uuid)
|
(type (: Unlocked (Record (: vault-id Uuid)
|
||||||
(: key-id int)
|
(: key-id int)
|
||||||
(: dek (Array byte))
|
(: dek AeadKey)
|
||||||
(: earlier (Vec (Tuple int (Array byte)))))))
|
(: earlier (Vec (Tuple int AeadKey))))))
|
||||||
|
|
||||||
;; The key ids of a keys file, which it shows without a password: the
|
;; The key ids of a keys file, which it shows without a password: the
|
||||||
;; current one, and the earlier ones, the latest first.
|
;; current one, and the earlier ones, the latest first.
|
||||||
|
|
@ -131,14 +132,33 @@
|
||||||
(: int)
|
(: int)
|
||||||
(def max-earlier-keys 200)
|
(def max-earlier-keys 200)
|
||||||
|
|
||||||
|
;; The Argon2id cost of `cost`. Raises `ArgumentException` when it is out of
|
||||||
|
;; libsodium's range.
|
||||||
|
(: (-> KeyCost Argon2idCost))
|
||||||
|
(defun (argon2id-cost cost) (make-argon2id-cost (.iterations cost) (.memory-kib cost)))
|
||||||
|
|
||||||
|
;; `key`, encrypted with `kek` and `nonce`. The copy of its bytes is wiped.
|
||||||
|
(: (-> AeadKey AeadNonce AeadKey (Array byte) (Array byte)))
|
||||||
|
(defun (wrap-key kek nonce key aad)
|
||||||
|
(def plain (aead-key->bytes key))
|
||||||
|
(try (aead-encrypt kek nonce plain #:aad aad) #:finally (wipe! plain)))
|
||||||
|
|
||||||
|
;; The key of the decrypted bytes `plain`, which are `dek-length` long. The
|
||||||
|
;; bytes are wiped.
|
||||||
|
(: (-> (Array byte) AeadKey))
|
||||||
|
(defun (take-key plain)
|
||||||
|
(def key (bytes->aead-key plain))
|
||||||
|
(wipe! plain)
|
||||||
|
key)
|
||||||
|
|
||||||
(: (-> Cipher int))
|
(: (-> Cipher int))
|
||||||
(defun (slot-length cipher) #fl(+ slot-head-length (cipher-nonce-length cipher) dek-length (cipher-tag-length cipher)))
|
(defun (slot-length cipher) #fl(+ slot-head-length (cipher-nonce-length cipher) dek-length (cipher-tag-length cipher)))
|
||||||
|
|
||||||
;; The DEK, wrapped for `password` in a new slot, as the whole of a keys
|
;; The DEK, wrapped for `password` in a new slot, as the whole of a keys
|
||||||
;; file with that one password slot, and a slot for each of the `#:earlier`
|
;; file with that one password slot, and a slot for each of the `#:earlier`
|
||||||
;; keys, wrapped with the DEK.
|
;; keys, wrapped with the DEK.
|
||||||
(: (-> Uuid int (Array byte) string (#:cost KeyCost) (#:cipher Cipher)
|
(: (-> Uuid int AeadKey string (#:cost KeyCost) (#:cipher Cipher)
|
||||||
(#:earlier (Vec (Tuple int (Array byte))))
|
(#:earlier (Vec (Tuple int AeadKey)))
|
||||||
(Array byte)))
|
(Array byte)))
|
||||||
(defun (wrap-keys vault-id key-id dek password #:cost default-key-cost #:cipher (preferred-cipher)
|
(defun (wrap-keys vault-id key-id dek password #:cost default-key-cost #:cipher (preferred-cipher)
|
||||||
#:earlier [])
|
#:earlier [])
|
||||||
|
|
@ -159,10 +179,10 @@
|
||||||
(put-u32! file (+ at 10) (.memory-kib cost))
|
(put-u32! file (+ at 10) (.memory-kib cost))
|
||||||
(put-bytes! file (+ at 14) salt)
|
(put-bytes! file (+ at 14) salt)
|
||||||
(put-u8! file (+ at 30) (cipher->code cipher))
|
(put-u8! file (+ at 30) (cipher->code cipher))
|
||||||
(put-bytes! file (+ at slot-head-length) nonce)
|
(put-bytes! file (+ at slot-head-length) (aead-nonce->bytes nonce))
|
||||||
(def aad (bytes-concat (get-bytes file 0 22) (get-bytes file at slot-head-length)))
|
(def aad (bytes-concat (get-bytes file 0 22) (get-bytes file at slot-head-length)))
|
||||||
(def kek (argon2id-derive-key password salt #:iterations (.iterations cost) #:memory-kib (.memory-kib cost)))
|
(def kek (argon2id-derive-key password salt (argon2id-cost cost)))
|
||||||
(def wrapped (try (aead-encrypt cipher kek nonce dek #:aad aad) #:finally (wipe! kek)))
|
(def wrapped (try (wrap-key kek nonce dek aad) #:finally (wipe-aead-key! kek)))
|
||||||
(put-bytes! file (+ at slot-head-length nonce-length) wrapped)
|
(put-bytes! file (+ at slot-head-length nonce-length) wrapped)
|
||||||
(loop (:for (Tuple i old) (enumerate kept))
|
(loop (:for (Tuple i old) (enumerate kept))
|
||||||
(:do (def (Tuple old-id old-dek) old)
|
(:do (def (Tuple old-id old-dek) old)
|
||||||
|
|
@ -173,10 +193,10 @@
|
||||||
(put-u8! file (+ there 5) 0)
|
(put-u8! file (+ there 5) 0)
|
||||||
(put-u32! file (+ there 6) key-id)
|
(put-u32! file (+ there 6) key-id)
|
||||||
(put-u8! file (+ there 30) (cipher->code cipher))
|
(put-u8! file (+ there 30) (cipher->code cipher))
|
||||||
(put-bytes! file (+ there slot-head-length) old-nonce)
|
(put-bytes! file (+ there slot-head-length) (aead-nonce->bytes old-nonce))
|
||||||
(def old-aad (bytes-concat (get-bytes file 0 22) (get-bytes file there slot-head-length)))
|
(def old-aad (bytes-concat (get-bytes file 0 22) (get-bytes file there slot-head-length)))
|
||||||
(put-bytes! file (+ there slot-head-length nonce-length)
|
(put-bytes! file (+ there slot-head-length nonce-length)
|
||||||
(aead-encrypt cipher dek old-nonce old-dek #:aad old-aad))))
|
(wrap-key dek old-nonce old-dek old-aad))))
|
||||||
file)
|
file)
|
||||||
|
|
||||||
;; A new vault: a new vault id and a new DEK, with key id 1, and its keys
|
;; A new vault: a new vault id and a new DEK, with key id 1, and its keys
|
||||||
|
|
@ -184,7 +204,7 @@
|
||||||
(: (-> string (#:cost KeyCost) (#:cipher Cipher) (Tuple (Array byte) Unlocked)))
|
(: (-> string (#:cost KeyCost) (#:cipher Cipher) (Tuple (Array byte) Unlocked)))
|
||||||
(defun (make-keys password #:cost default-key-cost #:cipher (preferred-cipher))
|
(defun (make-keys password #:cost default-key-cost #:cipher (preferred-cipher))
|
||||||
(def vault-id (uuid-v4))
|
(def vault-id (uuid-v4))
|
||||||
(def dek (make-aead-key XChaCha20Poly1305))
|
(def dek (make-aead-key))
|
||||||
(Tuple (wrap-keys vault-id 1 dek password #:cost cost #:cipher cipher)
|
(Tuple (wrap-keys vault-id 1 dek password #:cost cost #:cipher cipher)
|
||||||
(Unlocked (vault-id vault-id) (key-id 1) (dek dek) (earlier []))))
|
(Unlocked (vault-id vault-id) (key-id 1) (dek dek) (earlier []))))
|
||||||
|
|
||||||
|
|
@ -192,7 +212,7 @@
|
||||||
;; a new key at once do not give theirs the same id.
|
;; a new key at once do not give theirs the same id.
|
||||||
(: (-> (Vec int) int))
|
(: (-> (Vec int) int))
|
||||||
(defun (new-key-id known)
|
(defun (new-key-id known)
|
||||||
(def id (get-u32 (make-aead-key XChaCha20Poly1305) 0))
|
(def id (get-u32 (random-bytes 4) 0))
|
||||||
(if (or (<= id 1) (vec-contains? known id)) (new-key-id known) id))
|
(if (or (<= id 1) (vec-contains? known id)) (new-key-id known) id))
|
||||||
|
|
||||||
;; A new DEK for the vault of `u`, for a new password: its keys file, and
|
;; A new DEK for the vault of `u`, for a new password: its keys file, and
|
||||||
|
|
@ -202,7 +222,7 @@
|
||||||
(defun (new-keys u password #:cost default-key-cost #:cipher (preferred-cipher))
|
(defun (new-keys u password #:cost default-key-cost #:cipher (preferred-cipher))
|
||||||
(def earlier (vec-append [(Tuple (.key-id u) (.dek u))] (.earlier u)))
|
(def earlier (vec-append [(Tuple (.key-id u) (.dek u))] (.earlier u)))
|
||||||
(def key-id (new-key-id (vec-map fst earlier)))
|
(def key-id (new-key-id (vec-map fst earlier)))
|
||||||
(def dek (make-aead-key XChaCha20Poly1305))
|
(def dek (make-aead-key))
|
||||||
(def kept (vec-take earlier max-earlier-keys))
|
(def kept (vec-take earlier max-earlier-keys))
|
||||||
(Tuple (wrap-keys (.vault-id u) key-id dek password #:cost cost #:cipher cipher #:earlier kept)
|
(Tuple (wrap-keys (.vault-id u) key-id dek password #:cost cost #:cipher cipher #:earlier kept)
|
||||||
(Unlocked (vault-id (.vault-id u)) (key-id key-id) (dek dek) (earlier kept))))
|
(Unlocked (vault-id (.vault-id u)) (key-id key-id) (dek dek) (earlier kept))))
|
||||||
|
|
@ -229,7 +249,7 @@
|
||||||
(: salt (Array byte))
|
(: salt (Array byte))
|
||||||
(: cipher Cipher)
|
(: cipher Cipher)
|
||||||
(: aad (Array byte))
|
(: aad (Array byte))
|
||||||
(: nonce (Array byte))
|
(: nonce AeadNonce)
|
||||||
(: wrapped (Array byte)))))
|
(: wrapped (Array byte)))))
|
||||||
|
|
||||||
;; The slot at `at`, and where the next one starts, or why it is not a slot.
|
;; The slot at `at`, and where the next one starts, or why it is not a slot.
|
||||||
|
|
@ -251,7 +271,7 @@
|
||||||
(salt (get-bytes file (+ at 14) 16))
|
(salt (get-bytes file (+ at 14) 16))
|
||||||
(cipher cipher)
|
(cipher cipher)
|
||||||
(aad (bytes-concat (get-bytes file 0 22) (get-bytes file at slot-head-length)))
|
(aad (bytes-concat (get-bytes file 0 22) (get-bytes file at slot-head-length)))
|
||||||
(nonce (get-bytes file (+ at slot-head-length) nonce-length))
|
(nonce (bytes->aead-nonce cipher (get-bytes file (+ at slot-head-length) nonce-length)))
|
||||||
(wrapped (get-bytes file (+ at slot-head-length nonce-length)
|
(wrapped (get-bytes file (+ at slot-head-length nonce-length)
|
||||||
(+ dek-length (cipher-tag-length cipher)))))
|
(+ dek-length (cipher-tag-length cipher)))))
|
||||||
(+ at length))))))
|
(+ at length))))))
|
||||||
|
|
@ -275,13 +295,13 @@
|
||||||
(Err (BadKeys (str "a slot asks for " (int->string (.memory-kib cost)) " KiB"))))
|
(Err (BadKeys (str "a slot asks for " (int->string (.memory-kib cost)) " KiB"))))
|
||||||
(else (Ok cost))))
|
(else (Ok cost))))
|
||||||
|
|
||||||
;; The DEK of a password slot, or `None` when the password does not open it.
|
;; The bytes of the DEK of a password slot, or `None` when the password does
|
||||||
|
;; not open it.
|
||||||
(: (-> Slot string KeyCost (Option (Array byte))))
|
(: (-> Slot string KeyCost (Option (Array byte))))
|
||||||
(defun (open-slot slot password cost)
|
(defun (open-slot slot password cost)
|
||||||
(def kek (argon2id-derive-key password (.salt slot) #:iterations (.iterations cost)
|
(def kek (argon2id-derive-key password (.salt slot) (argon2id-cost cost)))
|
||||||
#:memory-kib (.memory-kib cost)))
|
(try (aead-decrypt kek (.nonce slot) (.wrapped slot) #:aad (.aad slot))
|
||||||
(try (aead-decrypt (.cipher slot) kek (.nonce slot) (.wrapped slot) #:aad (.aad slot))
|
#:finally (wipe-aead-key! kek)))
|
||||||
#:finally (wipe! kek)))
|
|
||||||
|
|
||||||
;; The vault id, the key id and the DEK, from the first password slot that
|
;; The vault id, the key id and the DEK, from the first password slot that
|
||||||
;; `password` opens.
|
;; `password` opens.
|
||||||
|
|
@ -295,16 +315,16 @@
|
||||||
(def (Ok slots) (read-slots file count) :propagate)
|
(def (Ok slots) (read-slots file count) :propagate)
|
||||||
(def passwords (vec-filter (fun (s) (and (= (.kind s) 1) (= (.kdf s) 1))) slots))
|
(def passwords (vec-filter (fun (s) (and (= (.kind s) 1) (= (.kdf s) 1))) slots))
|
||||||
;; The earlier keys that the DEK `key-id` wraps, or why one does not open.
|
;; The earlier keys that the DEK `key-id` wraps, or why one does not open.
|
||||||
(: (-> int (Array byte) (Result KeysError (Vec (Tuple int (Array byte))))))
|
(: (-> int AeadKey (Result KeysError (Vec (Tuple int AeadKey)))))
|
||||||
(defun (earlier-of key-id dek)
|
(defun (earlier-of key-id dek)
|
||||||
(let go ((i 0) (out []))
|
(let go ((i 0) (out []))
|
||||||
(if (= i (vec-length slots))
|
(if (= i (vec-length slots))
|
||||||
(Ok out)
|
(Ok out)
|
||||||
(let ((s (vec-ref slots i)))
|
(let ((s (vec-ref slots i)))
|
||||||
(if (and (= (.kind s) 2) (= (get-u32 (.aad s) 28) key-id))
|
(if (and (= (.kind s) 2) (= (get-u32 (.aad s) 28) key-id))
|
||||||
(match (aead-decrypt (.cipher s) dek (.nonce s) (.wrapped s) #:aad (.aad s))
|
(match (aead-decrypt dek (.nonce s) (.wrapped s) #:aad (.aad s))
|
||||||
((Some old) #:when (= (array-length old) dek-length)
|
((Some old) #:when (= (array-length old) dek-length)
|
||||||
(go (+ i 1) (vec-add out (Tuple (.slot-key-id s) old))))
|
(go (+ i 1) (vec-add out (Tuple (.slot-key-id s) (take-key old)))))
|
||||||
(_ (Err (BadKeys (str "the earlier key " (int->string (.slot-key-id s)) " does not open")))))
|
(_ (Err (BadKeys (str "the earlier key " (int->string (.slot-key-id s)) " does not open")))))
|
||||||
(go (+ i 1) out))))))
|
(go (+ i 1) out))))))
|
||||||
(cond
|
(cond
|
||||||
|
|
@ -320,12 +340,13 @@
|
||||||
((Err why) (Err why))
|
((Err why) (Err why))
|
||||||
((Ok cost)
|
((Ok cost)
|
||||||
(match (open-slot slot password cost)
|
(match (open-slot slot password cost)
|
||||||
((Some dek)
|
((Some plain)
|
||||||
(if (= (array-length dek) dek-length)
|
(if (= (array-length plain) dek-length)
|
||||||
|
(let ((dek (take-key plain)))
|
||||||
(match (earlier-of (.slot-key-id slot) dek)
|
(match (earlier-of (.slot-key-id slot) dek)
|
||||||
((Ok earlier)
|
((Ok earlier)
|
||||||
(Ok (Unlocked (vault-id vault-id) (key-id (.slot-key-id slot)) (dek dek) (earlier earlier))))
|
(Ok (Unlocked (vault-id vault-id) (key-id (.slot-key-id slot)) (dek dek) (earlier earlier))))
|
||||||
((Err why) (Err why)))
|
((Err why) (Err why))))
|
||||||
(Err (BadKeys "the key in a slot has the wrong length"))))
|
(Err (BadKeys "the key in a slot has the wrong length"))))
|
||||||
(None (go (+ i 1))))))))))))
|
(None (go (+ i 1))))))))))))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,7 @@
|
||||||
(import (std uuid))
|
(import (std uuid))
|
||||||
(import (text json))
|
(import (text json))
|
||||||
(import (bjosodium core))
|
(import (bjosodium core))
|
||||||
|
(import (bjosodium raw))
|
||||||
(import (bjomarks binary))
|
(import (bjomarks binary))
|
||||||
(import (bjomarks bookmark))
|
(import (bjomarks bookmark))
|
||||||
(import (bjomarks keys))
|
(import (bjomarks keys))
|
||||||
|
|
@ -268,11 +269,12 @@
|
||||||
(else (Err (str "unknown compression " (int->string compression))))))))
|
(else (Err (str "unknown compression " (int->string compression))))))))
|
||||||
|
|
||||||
;; A device file with a new random nonce.
|
;; A device file with a new random nonce.
|
||||||
(: (-> FileHead (Array byte) DeviceDoc (Array byte)))
|
(: (-> FileHead AeadKey DeviceDoc (Array byte)))
|
||||||
(defun (encode-device-file head dek doc)
|
(defun (encode-device-file head dek doc)
|
||||||
(def cipher (.head-cipher head))
|
(def cipher (.head-cipher head))
|
||||||
(def nonce (make-aead-nonce cipher))
|
(def nonce (make-aead-nonce cipher))
|
||||||
(def header (: (make-array (+ head-length (array-length nonce))) (Array byte)))
|
(def nonce-bytes (aead-nonce->bytes nonce))
|
||||||
|
(def header (: (make-array (+ head-length (array-length nonce-bytes))) (Array byte)))
|
||||||
(put-bytes! header 0 device-magic)
|
(put-bytes! header 0 device-magic)
|
||||||
(put-u16! header 4 device-format)
|
(put-u16! header 4 device-format)
|
||||||
(put-bytes! header 6 (uuid->bytes (.head-vault head)))
|
(put-bytes! header 6 (uuid->bytes (.head-vault head)))
|
||||||
|
|
@ -280,14 +282,14 @@
|
||||||
(put-u64! header 38 (.head-revision head))
|
(put-u64! header 38 (.head-revision head))
|
||||||
(put-u32! header 46 (.head-key-id head))
|
(put-u32! header 46 (.head-key-id head))
|
||||||
(put-u8! header 50 (cipher->code cipher))
|
(put-u8! header 50 (cipher->code cipher))
|
||||||
(put-bytes! header head-length nonce)
|
(put-bytes! header head-length nonce-bytes)
|
||||||
(def contents (pack (string->bytes (device-doc->string doc))))
|
(def contents (pack (string->bytes (device-doc->string doc))))
|
||||||
(def sealed (aead-encrypt cipher dek nonce contents #:aad header))
|
(def sealed (aead-encrypt dek nonce contents #:aad header))
|
||||||
(wipe! contents)
|
(wipe! contents)
|
||||||
(bytes-concat header sealed))
|
(bytes-concat header sealed))
|
||||||
|
|
||||||
;; The DEK of the key id `id`, of those `known`.
|
;; The DEK of the key id `id`, of those `known`.
|
||||||
(: (-> (Vec (Tuple int (Array byte))) int (Option (Array byte))))
|
(: (-> (Vec (Tuple int AeadKey)) int (Option AeadKey)))
|
||||||
(defun (key-of known id)
|
(defun (key-of known id)
|
||||||
(let go ((i 0))
|
(let go ((i 0))
|
||||||
(cond ((= i (vec-length known)) None)
|
(cond ((= i (vec-length known)) None)
|
||||||
|
|
@ -298,7 +300,7 @@
|
||||||
;; - The file must be of the vault `vault-id`, and encrypted with one of the
|
;; - The file must be of the vault `vault-id`, and encrypted with one of the
|
||||||
;; DEKs `known`, each with its key id.
|
;; DEKs `known`, each with its key id.
|
||||||
;; - The document must name the device that the header names.
|
;; - The document must name the device that the header names.
|
||||||
(: (-> (Array byte) Uuid (Vec (Tuple int (Array byte))) (Result string (Tuple FileHead DeviceDoc))))
|
(: (-> (Array byte) Uuid (Vec (Tuple int AeadKey)) (Result string (Tuple FileHead DeviceDoc))))
|
||||||
(defun (decode-device-file file vault-id known)
|
(defun (decode-device-file file vault-id known)
|
||||||
(def n (array-length file))
|
(def n (array-length file))
|
||||||
(def version (if (>= n 6) (get-u16 file 4) 0))
|
(def version (if (>= n 6) (get-u16 file 4) 0))
|
||||||
|
|
@ -322,9 +324,9 @@
|
||||||
(:def nonce-length (cipher-nonce-length cipher))
|
(:def nonce-length (cipher-nonce-length cipher))
|
||||||
((< n (+ head-length nonce-length (cipher-tag-length cipher))) (Err "too short for a device file"))
|
((< n (+ head-length nonce-length (cipher-tag-length cipher))) (Err "too short for a device file"))
|
||||||
(:def header (get-bytes file 0 (+ head-length nonce-length)))
|
(:def header (get-bytes file 0 (+ head-length nonce-length)))
|
||||||
(:def nonce (get-bytes file head-length nonce-length))
|
(:def nonce (bytes->aead-nonce cipher (get-bytes file head-length nonce-length)))
|
||||||
(:def sealed (get-bytes file (+ head-length nonce-length) (- n (+ head-length nonce-length))))
|
(:def sealed (get-bytes file (+ head-length nonce-length) (- n (+ head-length nonce-length))))
|
||||||
(:def (Some contents) (aead-decrypt cipher dek nonce sealed #:aad header)
|
(:def (Some contents) (aead-decrypt dek nonce sealed #:aad header)
|
||||||
:leave-with (Err "it does not decrypt: it was changed, or it was encrypted with another key"))
|
:leave-with (Err "it does not decrypt: it was changed, or it was encrypted with another key"))
|
||||||
(:def (Ok json) (unpack contents) :propagate)
|
(:def (Ok json) (unpack contents) :propagate)
|
||||||
(:def (Ok doc) (parse-device-doc (bytes->string json)) :propagate)
|
(:def (Ok doc) (parse-device-doc (bytes->string json)) :propagate)
|
||||||
|
|
@ -415,8 +417,8 @@
|
||||||
(: id Uuid)
|
(: id Uuid)
|
||||||
(: device Uuid)
|
(: device Uuid)
|
||||||
(: key-id int)
|
(: key-id int)
|
||||||
(: dek (Array byte))
|
(: dek AeadKey)
|
||||||
(: known (Vec (Tuple int (Array byte))))
|
(: known (Vec (Tuple int AeadKey)))
|
||||||
(: cipher Cipher)
|
(: cipher Cipher)
|
||||||
(: entries (Map string Entry))
|
(: entries (Map string Entry))
|
||||||
(: seen (Map string long))
|
(: seen (Map string long))
|
||||||
|
|
@ -449,7 +451,7 @@
|
||||||
((BadKeys why) (BadFile (str keys-file-name ": " why)))))
|
((BadKeys why) (BadFile (str keys-file-name ": " why)))))
|
||||||
|
|
||||||
;; The keys that `u` gives: the current one first, then the earlier ones.
|
;; The keys that `u` gives: the current one first, then the earlier ones.
|
||||||
(: (-> Unlocked (Vec (Tuple int (Array byte)))))
|
(: (-> Unlocked (Vec (Tuple int AeadKey))))
|
||||||
(defun (keys-of u) (vec-append [(Tuple (.key-id u) (.dek u))] (.earlier u)))
|
(defun (keys-of u) (vec-append [(Tuple (.key-id u) (.dek u))] (.earlier u)))
|
||||||
|
|
||||||
(: (-> Storage Unlocked string Uuid Vault))
|
(: (-> Storage Unlocked string Uuid Vault))
|
||||||
|
|
@ -574,7 +576,7 @@
|
||||||
(None (Err e))))))))
|
(None (Err e))))))))
|
||||||
|
|
||||||
;; `u` with the keys of `more` that it does not have, as earlier keys.
|
;; `u` with the keys of `more` that it does not have, as earlier keys.
|
||||||
(: (-> Unlocked (Vec (Tuple int (Array byte))) Unlocked))
|
(: (-> Unlocked (Vec (Tuple int AeadKey)) Unlocked))
|
||||||
(defun (with-keys u more)
|
(defun (with-keys u more)
|
||||||
(def ids (vec-map fst (keys-of u)))
|
(def ids (vec-map fst (keys-of u)))
|
||||||
(record-set u (earlier (vec-append (.earlier u) (vec-filter (fun (k) (not (vec-contains? ids (fst k)))) more)))))
|
(record-set u (earlier (vec-append (.earlier u) (vec-filter (fun (k) (not (vec-contains? ids (fst k)))) more)))))
|
||||||
|
|
@ -699,7 +701,7 @@
|
||||||
;; Sets the DEKs to zeros. Do not use the vault after this.
|
;; Sets the DEKs to zeros. Do not use the vault after this.
|
||||||
(: (-> Vault void))
|
(: (-> Vault void))
|
||||||
(defun (vault-close v)
|
(defun (vault-close v)
|
||||||
(loop (:for k (.known v)) (:do (wipe! (snd k)))))
|
(loop (:for k (.known v)) (:do (wipe-aead-key! (snd k)))))
|
||||||
|
|
||||||
;; ---------------------------------------------------------------------------
|
;; ---------------------------------------------------------------------------
|
||||||
;; Changes
|
;; Changes
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,9 @@
|
||||||
(: (-> (Array byte) (Array byte)))
|
(: (-> (Array byte) (Array byte)))
|
||||||
(defun (copy-of a) (get-bytes a 0 (array-length a)))
|
(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.
|
;; A copy of `a` with bit 0 of byte `at` changed.
|
||||||
(: (-> (Array byte) int (Array byte)))
|
(: (-> (Array byte) int (Array byte)))
|
||||||
(defun (flipped a at)
|
(defun (flipped a at)
|
||||||
|
|
@ -52,8 +55,8 @@
|
||||||
(def (Ok opened) (unlock-keys file "hunter2") :leave-with (check-fail "it did not open"))
|
(def (Ok opened) (unlock-keys file "hunter2") :leave-with (check-fail "it did not open"))
|
||||||
(check (= (.vault-id opened) (.vault-id made)))
|
(check (= (.vault-id opened) (.vault-id made)))
|
||||||
(check (= (.key-id opened) 1))
|
(check (= (.key-id opened) 1))
|
||||||
(check (bytes=? (.dek opened) (.dek made)))
|
(check (same-key? (.dek opened) (.dek made)))
|
||||||
(check (= (array-length (.dek made)) 32))
|
(check (= (array-length (aead-key->bytes (.dek made))) 32))
|
||||||
(check (= (keys-vault-id file) (Ok (.vault-id made)))))
|
(check (= (keys-vault-id file) (Ok (.vault-id made)))))
|
||||||
|
|
||||||
(deftest "each cipher wraps the key"
|
(deftest "each cipher wraps the key"
|
||||||
|
|
@ -75,14 +78,14 @@
|
||||||
(def (Tuple _f1 a) (make-keys "pw" #:cost cheap))
|
(def (Tuple _f1 a) (make-keys "pw" #:cost cheap))
|
||||||
(def (Tuple _f2 b) (make-keys "pw" #:cost cheap))
|
(def (Tuple _f2 b) (make-keys "pw" #:cost cheap))
|
||||||
(check (not (= (.vault-id a) (.vault-id b))))
|
(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"
|
(deftest "a new password keeps the vault and its key"
|
||||||
(def (Tuple old made) (make-keys "old password" #:cost cheap))
|
(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))
|
(def new (wrap-keys (.vault-id made) (.key-id made) (.dek made) "new password" #:cost cheap))
|
||||||
(check (= (outcome (unlock-keys new "old password")) "WrongPassword"))
|
(check (= (outcome (unlock-keys new "old password")) "WrongPassword"))
|
||||||
(def (Ok opened) (unlock-keys new "new password") :leave-with (check-fail "it did not open"))
|
(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 (= (.vault-id opened) (.vault-id made)))
|
||||||
(check (= (outcome (unlock-keys old "old password")) "Ok")))
|
(check (= (outcome (unlock-keys old "old password")) "Ok")))
|
||||||
|
|
||||||
|
|
@ -135,13 +138,13 @@
|
||||||
(def (Tuple new rotated) (new-keys made "new password" #:cost cheap))
|
(def (Tuple new rotated) (new-keys made "new password" #:cost cheap))
|
||||||
(check (= (.vault-id rotated) (.vault-id made)))
|
(check (= (.vault-id rotated) (.vault-id made)))
|
||||||
(check (not (= (.key-id rotated) (.key-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.
|
;; 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"))
|
(def (Ok opened) (unlock-keys new "new password") :leave-with (check-fail "it did not open"))
|
||||||
(check (= (.key-id opened) (.key-id rotated)))
|
(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 (= (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"))
|
(check (= (outcome (unlock-keys new "old password")) "WrongPassword"))
|
||||||
;; The old file and the old password give the old key only.
|
;; 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"))
|
(def (Ok was) (unlock-keys old "old password") :leave-with (check-fail "the old file did not open"))
|
||||||
|
|
|
||||||
|
|
@ -543,7 +543,7 @@
|
||||||
(def a0 (vault-save (fst (add-bookmark (create f (.one f)) "https://example.org/a" []))))
|
(def a0 (vault-save (fst (add-bookmark (create f (.one f)) "https://example.org/a" []))))
|
||||||
(def mine (own-keys f (.one f)))
|
(def mine (own-keys f (.one f)))
|
||||||
;; The same vault id, another key, and another password.
|
;; 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"))))
|
(def (Tuple a1 report) (sync (opened (reopen f (.one f) "hunter2"))))
|
||||||
(check (told? report "holds none of this device's keys"))
|
(check (told? report "holds none of this device's keys"))
|
||||||
(check (= (urls a1) ["https://example.org/a"]))
|
(check (= (urls a1) ["https://example.org/a"]))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue