;; 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/. ;; bjosodium/tests/core.bjo — (bjosodium core) against libsodium's own test ;; vectors, and the failures that must give `None` or raise. ;; ;; bjo run tests/core.bjo ;; ;; Every line should start with "ok:". ;; ;; The vectors are from libsodium's test suite (test/default/ in the ;; libsodium repository): ;; - AEGIS-256: the fourth entry of `tests[]` in aead_aegis256.c. ;; - XChaCha20-Poly1305: `tv()` in aead_xchacha20poly1305.c, with the ;; output in aead_xchacha20poly1305.exp. ;; - Argon2id: `tv3()` in pwhash_argon2id.c, with the results in ;; pwhash_argon2id.exp. Entries 2, 4 and 5 verify, entry 0 does not. ;; ;; `main` is a bjoroutine, so the Argon2id functions are tried in both ;; colours: from `main`, and from the plain `defun`s below. (import (std prelude)) (import (std simpletest)) (import (std crypto hash)) (import (bjosodium core)) (import/class (ArgumentException (: System.ArgumentException (-> string ArgumentException)))) (: (-> string (Array byte))) (defun (unhex text) (match (hex->bytes text) ((Some bytes) bytes) (None (raise (cast Exception (ArgumentException. (str "Not hex: " text))))))) (: (-> (Option (Array byte)) string)) (defun (opened result) (match result ((Some bytes) (bytes->hex bytes)) (None "None"))) (: (-> (-> %a) bool)) (defun (raises? thunk) (match (try (thunk) #:catch (System.ArgumentException)) ((Ok _) #f) ((Err _) #t))) ;; A copy of `bytes` with bit 0 of byte `i` changed. (: (-> (Array byte) int (Array byte))) (defun (flipped bytes i) (def copy (ro-span->array (array->ro-span bytes))) (array-set! copy i (cast byte (bitwise-xor (cast int (array-ref copy i)) 1))) copy) (: (-> (Array byte) int (Array byte))) (defun (first-bytes bytes n) (ro-span->array (array->ro-span bytes #:to n))) ;; The non-bjoroutine versions, called from `main` through these. (: (-> string (Array byte) int int (Array byte))) (defun (derive/sync password salt t m) (argon2id-derive-key password salt #:iterations t #:memory-kib 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)) ;; ---- Vectors ---------------------------------------------------------------- (def aegis-key (unhex "b8c6e8cea59ca9fd2922530ee61911c1ed1c5af98be8fb03cbb449adcea0ed83")) (def aegis-nonce (unhex "af5bc1abe7bafadee790390277874cdfcc1ac1955f249d1131555d345832f555")) (def aegis-message (unhex "b6c15f560be043d06aa27e15d8c901af6b19db7a15e1")) (def aegis-ad (unhex "d899366a0b4e4d86cce5ba61aca2a84349c8de5757e008e94e7d7a3703")) (def aegis-ciphertext "4c8496dfa6c419ef3c4867769a9014bd17118c22eef5") (def aegis-tag "f81ceacaaae6263c33f836fa26d92b0f08eb0796135c7fe312c93add6a208e8a") (def xchacha-key (unhex "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")) (def xchacha-nonce (unhex "07000000404142434445464748494a4b4c4d4e4f50515253")) (def xchacha-ad (unhex "50515253c0c1c2c3c4c5c6c7")) (def xchacha-message (string->bytes "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.")) (def xchacha-ciphertext (str "f8ebea4875044066fc162a0604e171feecfb3d20425248563bcfd5a155dcc47b" "bda70b86e5ab9b55002bd1274c02db35321acd7af8b2e2d25015e136b7679458" "e9f43243bf719d639badb5feac03f80a19a96ef10cb1d15333a837b90946ba38" "54ee74da3f2585efc7e1e170e17e15e563e7")) (def xchacha-tag "7601f4f85cafa8e5877614e143e68420") ;; tv3 entry 4: t=19, m=4096, salt ">A 16-bytes salt". (def argon-password-4 "^T5H$JYt39n%K*j:W]!1s?vg!:jGi]Ax?..l7[p0v:1jHTpla9;]bUN;?bWyCbtqg ") (def argon-stored-4 "$argon2id$v=19$m=4096,t=19,p=1$PkEgMTYtYnl0ZXMgc2FsdA$ltB/ue1kPtBMBGfsysMpPigE6hiNEKZ9vs8vLNVDQGA") (def argon-hash-4 "96d07fb9ed643ed04c0467eccac3293e2804ea188d10a67dbecf2f2cd5434060") ;; tv3 entry 2: the empty password, t=2, m=4882. (def argon-stored-2 "$argon2id$v=19$m=4882,t=2,p=1$bA81arsiXysd3WbTRzmEOw$Nm8QBM+7RH1DXo9rvp5cwKEOOOfD2g6JuxlXihoNcpE") (def argon-salt-2 (unhex "6c0f356abb225f2b1ddd66d34739843b")) (def argon-hash-2 "366f1004cfbb447d435e8f6bbe9e5cc0a10e38e7c3da0e89bb19578a1a0d7291") ;; tv3 entry 5: three lanes, which libsodium verifies but does not make. (def argon-password-5 "K3S=KyH#)36_?]LxeR8QNKw6X=gFbxai$C%29V*") (def argon-stored-5 "$argon2id$v=19$m=4096,t=1,p=3$PkEgcHJldHR5IGxvbmcgc2FsdA$HUqx5Z1b/ZypnUrvvJ5UC2Q+T6Q1WwASK/Kr9dRbGA0") ;; tv3 entry 0: t=0, which libsodium rejects. (def argon-stored-0 "$argon2id$v=19$m=4096,t=0,p=1$X1NhbHQAAAAAAAAAAAAAAA$bWh++MKN1OiFHKgIWTLvIi1iHicmHH7+Fv3K88ifFfI") (: (-> Cipher void)) (defun (check-cipher cipher) (def name (written cipher)) (def key (make-aead-key cipher)) (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 tag-length (cipher-tag-length 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)) (bytes->hex message)) (expect #"${name}: a changed ciphertext byte" (opened (aead-decrypt cipher 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)) "None") (expect #"${name}: another key" (opened (aead-decrypt cipher (make-aead-key cipher) nonce boxed #:aad aad)) "None") (expect #"${name}: another nonce" (opened (aead-decrypt cipher key (flipped nonce 3) boxed #:aad aad)) "None") (expect #"${name}: other associated data" (opened (aead-decrypt cipher 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)) "None") (expect #"${name}: shorter than a tag" (opened (aead-decrypt cipher 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))) (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)) "") ;; 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) (+ (cipher-nonce-length cipher) (array-length message) tag-length)) (expect #"${name}: unsealed" (opened (aead-unseal cipher key sealed #:aad aad)) (bytes->hex message)) (expect-true #"${name}: each seal has its own nonce" (not (= (bytes->hex (aead-seal cipher key message #:aad aad)) (bytes->hex sealed)))) (expect #"${name}: a changed nonce in a sealed message" (opened (aead-unseal cipher key (flipped sealed 0) #:aad aad)) "None") (expect #"${name}: a sealed message with other associated data" (opened (aead-unseal cipher key sealed)) "None") (expect #"${name}: a sealed message shorter than a nonce and a tag" (opened (aead-unseal cipher key (first-bytes sealed (- (+ (cipher-nonce-length cipher) tag-length) 1)))) "None") (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)) (defbjo (main args) ;; ---- Lengths -------------------------------------------------------------- (expect "AEGIS-256 key" (cipher-key-length Aegis256) 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 "the preferred cipher follows the CPU" (preferred-cipher) (if (aegis256-accelerated?) Aegis256 XChaCha20Poly1305)) (println #"note: aegis256-accelerated? is ${(written (aegis256-accelerated?))} here") ;; ---- Known answers -------------------------------------------------------- (expect "AEGIS-256 vector" (bytes->hex (aead-encrypt Aegis256 aegis-key aegis-nonce 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)) (bytes->hex aegis-message)) (expect "XChaCha20-Poly1305 vector" (bytes->hex (aead-encrypt XChaCha20Poly1305 xchacha-key xchacha-nonce xchacha-message #:aad xchacha-ad)) (str xchacha-ciphertext xchacha-tag)) (expect "XChaCha20-Poly1305 vector decrypts" (opened (aead-decrypt XChaCha20Poly1305 xchacha-key xchacha-nonce (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) (unhex (str aegis-ciphertext aegis-tag)) #:aad aegis-ad)) "None") ;; ---- Behaviour, for each cipher ------------------------------------------- (check-cipher Aegis256) (check-cipher XChaCha20Poly1305) ;; ---- Argon2id: known answers ---------------------------------------------- (expect "derive: tv3 entry 4" (bytes->hex (argon2id-derive-key argon-password-4 (string->bytes ">A 16-bytes salt") #:iterations 19 #:memory-kib 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)) argon-hash-4) (expect "derive: tv3 entry 2, the empty password" (bytes->hex (argon2id-derive-key "" argon-salt-2 #:iterations 2 #:memory-kib 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) (expect "verify: tv3 entry 2" (argon2id-verify "" argon-stored-2) #t) (expect "verify: tv3 entry 5, three lanes" (argon2id-verify argon-password-5 argon-stored-5) #t) (expect "verify: tv3 entry 0, no iterations" (argon2id-verify "" argon-stored-0) #f) (expect "verify: a wrong password" (argon2id-verify "^T5H" argon-stored-4) #f) ;; The first letter of the hash, as the last one holds bits that base64 pads. (expect "verify: one changed letter of the hash" (argon2id-verify argon-password-4 "$argon2id$v=19$m=4096,t=19,p=1$PkEgMTYtYnl0ZXMgc2FsdA$mtB/ue1kPtBMBGfsysMpPigE6hiNEKZ9vs8vLNVDQGA") #f) (expect "verify: one changed letter of the salt" (argon2id-verify argon-password-4 "$argon2id$v=19$m=4096,t=19,p=1$QkEgMTYtYnl0ZXMgc2FsdA$ltB/ue1kPtBMBGfsysMpPigE6hiNEKZ9vs8vLNVDQGA") #f) (expect "verify: another cost in the string" (argon2id-verify argon-password-4 "$argon2id$v=19$m=4096,t=18,p=1$PkEgMTYtYnl0ZXMgc2FsdA$ltB/ue1kPtBMBGfsysMpPigE6hiNEKZ9vs8vLNVDQGA") #f) ;; ---- Argon2id: hash and verify -------------------------------------------- (def stored (argon2id-hash "correct horse" #:iterations 1 #:memory-kib 8192)) (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)))) (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) ;; ---- 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) (expect "the empty stored string" (raises? #(verify/sync "x" "")) #t) (expect "Argon2i, not Argon2id" (raises? #(verify/sync "x" "$argon2i$v=19$m=4096,t=3,p=1$c2FsdHNhbHQ$aGFzaGhhc2g")) #t) (expect "another version" (raises? #(verify/sync "x" "$argon2id$v=16$m=4096,t=3,p=1$c2FsdHNhbHQ$aGFzaGhhc2g")) #t) (expect "a cost that is not a number" (raises? #(verify/sync "x" "$argon2id$v=19$m=x,t=3,p=1$c2FsdHNhbHQ$aGFzaGhhc2g")) #t) (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 "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) ;; ---- Memory --------------------------------------------------------------- (def secret (make-aead-key Aegis256)) (wipe! secret) (expect "wipe! clears every byte" (bytes->hex secret) (bytes->hex (make-array 32))) 0)