bjomarks/tests/keys.bjo

170 lines
8 KiB
Text
Raw Permalink Normal View History

;; This Source Code Form is subject to the terms of the Mozilla Public
;; 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/.
;; bjomarks/tests/keys.bjo — (bjomarks keys): keys files that open, and the
;; changes to one that must make it fail.
;;
;; bjo run tests/keys.bjo
;;
;; The tests use a cost of 1 iteration over 8 KiB, so that they are quick.
(import (std prelude))
(import (std test))
(import (std uuid))
(import (bjosodium core))
(import (bjomarks binary))
(import (bjomarks keys))
(def cheap (KeyCost (iterations 1) (memory-kib 8)))
(: (-> (Result KeysError Unlocked) string))
(defun (outcome r)
(match r
((Ok _) "Ok")
((Err WrongPassword) "WrongPassword")
((Err (BadKeys why)) why)))
(: (-> (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)
(def b (copy-of a))
(put-u8! b at (bitwise-xor (get-u8 b at) 1))
b)
(: (-> (Array byte) int int (Array byte)))
(defun (with-u8 a at v)
(def b (copy-of a))
(put-u8! b at v)
b)
(: (-> (Array byte) int int (Array byte)))
(defun (with-u32 a at v)
(def b (copy-of a))
(put-u32! b at v)
b)
(deftest "a new vault opens with its password"
(def (Tuple file made) (make-keys "hunter2" #:cost cheap))
(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 (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"
#:cases ((cipher length)
(Aegis256 150)
(XChaCha20Poly1305 126))
(def (Tuple file _made) (make-keys "hunter2" #:cost cheap #:cipher cipher))
(check (= (array-length file) length))
(check (= (get-u8 file 53) (cipher->code cipher)))
(check (= (outcome (unlock-keys file "hunter2")) "Ok"))
(check (= (outcome (unlock-keys file "hunter3")) "WrongPassword")))
(deftest "a wrong password"
(def (Tuple file _made) (make-keys "hunter2" #:cost cheap))
(check (= (outcome (unlock-keys file "")) "WrongPassword"))
(check (= (outcome (unlock-keys file "Hunter2")) "WrongPassword")))
(deftest "two vaults have their own ids and keys"
(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 (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 (same-key? (.dek opened) (.dek made)))
(check (= (.vault-id opened) (.vault-id made)))
(check (= (outcome (unlock-keys old "old password")) "Ok")))
;; The offsets of the fields of the one slot, which starts at 23.
(deftest "a change to a field that the slot's tag covers"
#:cases ((what at)
("the vault id" 6)
("the vault id's last byte" 21)
("the key id" 27)
("the memory" 36)
("the salt" 37)
("the nonce" 54)
("the wrapped key" 90)
("the tag" 149))
(def (Tuple file _made) (make-keys "hunter2" #:cost cheap #:cipher Aegis256))
(check (= (outcome (unlock-keys (flipped file at) "hunter2")) "WrongPassword")))
(deftest "another cost in bounds does not open the slot"
(def (Tuple file _made) (make-keys "hunter2" #:cost cheap #:cipher Aegis256))
(check (= (outcome (unlock-keys (with-u32 file 29 2) "hunter2")) "WrongPassword")))
(deftest "what a keys file must not be"
(def (Tuple file _made) (make-keys "hunter2" #:cost cheap #:cipher Aegis256))
(check (= (outcome (unlock-keys (make-array 0) "hunter2")) "too short for a keys file"))
(check (= (outcome (unlock-keys (flipped file 0) "hunter2")) "not a keys file"))
(check (= (outcome (unlock-keys (with-u8 file 5 2) "hunter2")) "keys file version 2 is not version 1"))
(check (= (outcome (unlock-keys (with-u8 file 22 2) "hunter2")) "a slot is cut short"))
(check (= (outcome (unlock-keys (with-u8 file 22 0) "hunter2")) "bytes after the last slot"))
(check (= (outcome (unlock-keys (get-bytes file 0 (- (array-length file) 1)) "hunter2")) "a slot is cut short"))
(check (= (outcome (unlock-keys (bytes-concat file (make-array 1)) "hunter2")) "bytes after the last slot"))
(check (= (outcome (unlock-keys (with-u8 file 53 9) "hunter2")) "unknown cipher 9"))
(check (= (outcome (unlock-keys (with-u8 file 23 2) "hunter2")) "no password slot"))
(check (= (outcome (unlock-keys (with-u8 file 28 2) "hunter2")) "no password slot"))
(check (= (outcome (unlock-keys (with-u32 file 29 1000) "hunter2")) "a slot asks for 1000 iterations"))
(check (= (outcome (unlock-keys (with-u32 file 29 0) "hunter2")) "a slot asks for 0 iterations"))
(check (= (outcome (unlock-keys (with-u32 file 33 4) "hunter2")) "a slot asks for 4 KiB"))
(check (= (outcome (unlock-keys (with-u32 file 33 -1) "hunter2")) "a slot asks for -1 KiB"))
;; At most 1 GiB: a file of an attacker cannot make a phone use more.
(check (= (outcome (unlock-keys (with-u32 file 33 1048577) "hunter2")) "a slot asks for 1048577 KiB")))
(deftest "the vault id needs no password"
(def (Tuple file made) (make-keys "hunter2" #:cost cheap))
(check (= (keys-vault-id file) (Ok (.vault-id made))))
(check-match (keys-vault-id (flipped file 1)) (Err (BadKeys _))))
;; ---- A new key for a new password ------------------------------------------
(deftest "a new password comes with a new key, and keeps the earlier one"
(def (Tuple old made) (make-keys "old password" #:cost cheap))
(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 (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 (same-key? (.dek opened) (.dek rotated)))
(check (= (vec-map fst (.earlier opened)) [1]))
(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"))
(check (= (.key-id was) 1))
(check (vec-empty? (.earlier was)))
;; The ids show without a password.
(check (= (keys-key-ids new) (Ok (KeyIds (current (.key-id rotated)) (earlier-ids [1])))))
(check (= (keys-key-ids old) (Ok (KeyIds (current 1) (earlier-ids [])))))
;; A second change keeps both, the latest first.
(def (Tuple third again) (new-keys opened "third" #:cost cheap))
(def (Ok opened3) (unlock-keys third "third") :leave-with (check-fail "the third did not open"))
(check (= (vec-map fst (.earlier opened3)) [(.key-id rotated) 1]))
(check (= (.key-id opened3) (.key-id again))))
(deftest "an earlier key that was changed does not open"
(def (Tuple _old made) (make-keys "pw" #:cost cheap #:cipher Aegis256))
(def (Tuple new _rotated) (new-keys made "pw2" #:cost cheap #:cipher Aegis256))
;; The second slot starts at 23 + 127; its wrapped key at 23 + 127 + 63.
(check (= (outcome (unlock-keys (flipped new (+ 150 70)) "pw2")) "the earlier key 1 does not open"))
(check (= (outcome (unlock-keys (with-u32 new 151 7) "pw2")) "the earlier key 7 does not open")))
(run-tests)