bjosodium: libsodium's authenticated encryption and Argon2id for Bjolang
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
911f4bcc46
9 changed files with 1409 additions and 0 deletions
434
src/core.bjo
Normal file
434
src/core.bjo
Normal file
|
|
@ -0,0 +1,434 @@
|
|||
;; 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/src/core.bjo — the module (bjosodium core): authenticated
|
||||
;; encryption with AEGIS-256 or XChaCha20-Poly1305, and password hashing with
|
||||
;; Argon2id. The work is done by libsodium, through LibSodium.Net.
|
||||
;;
|
||||
;; - Keys, nonces, data and associated data are (Array byte). A password is a
|
||||
;; string, used as its UTF-8 bytes.
|
||||
;; - Encryption is AEAD: the result has a tag, and decryption returns `None`
|
||||
;; when the tag does not verify. Then the key, the nonce, the associated
|
||||
;; data or the bytes are not the ones that were used to encrypt.
|
||||
;; - `aead-encrypt` takes a nonce from the caller. `aead-seal` makes a random
|
||||
;; nonce and puts it in front of the result.
|
||||
;; - Both ciphers have nonces large enough to be random: do not count them.
|
||||
;; Never encrypt two messages with the same key and nonce.
|
||||
;; - A wrong length of a key, a nonce or a salt, and a cost out of range,
|
||||
;; raise `ArgumentException`. These are errors in the program, not in the
|
||||
;; data.
|
||||
;; - The Argon2id functions take a core for tens to hundreds of milliseconds.
|
||||
;; In a bjoroutine they run on a thread of their own, so the fiber pool is
|
||||
;; not blocked.
|
||||
|
||||
(export
|
||||
;; Ciphers. Transparent: Aegis256, XChaCha20Poly1305.
|
||||
Cipher cipher-key-length cipher-nonce-length cipher-tag-length
|
||||
aegis256-accelerated? preferred-cipher
|
||||
;; Keys and nonces
|
||||
make-aead-key make-aead-nonce
|
||||
;; Encryption
|
||||
aead-encrypt aead-decrypt aead-seal aead-unseal
|
||||
;; Argon2id
|
||||
argon2id-derive-key argon2id-hash argon2id-verify argon2id-needs-rehash?
|
||||
make-argon2id-salt argon2id-salt-length default-argon2id-iterations default-argon2id-memory-kib
|
||||
;; Memory
|
||||
wipe!)
|
||||
|
||||
(import (std prelude))
|
||||
|
||||
(type (: Cipher (Union Aegis256 XChaCha20Poly1305)))
|
||||
|
||||
(import/class
|
||||
(ArgumentException (: System.ArgumentException (-> string ArgumentException)))
|
||||
(Algorithm (: LibSodium.PasswordHashArgonAlgorithm)))
|
||||
|
||||
;; LibSodium.Net's AEAD methods take the output, the input, the key, the tag,
|
||||
;; the associated data and the nonce, in that order. A tag span makes the
|
||||
;; call detached: the tag goes into that span. Each method returns the part
|
||||
;; of the output it wrote, which this module does not need.
|
||||
;;
|
||||
;; An empty span for the tag or the nonce is not the same as no argument, so
|
||||
;; every call gives all six.
|
||||
(import/extern
|
||||
(clr-aegis-encrypt (: LibSodium.Aegis256.Encrypt
|
||||
(-> (Span byte) (RoSpan byte) (RoSpan byte) (Span byte) (RoSpan byte) (RoSpan byte)
|
||||
(Span byte))))
|
||||
;; Raises `LibSodiumException` when the tag does not verify.
|
||||
(clr-aegis-decrypt (: LibSodium.Aegis256.Decrypt
|
||||
(-> (Span byte) (RoSpan byte) (RoSpan byte) (RoSpan byte) (RoSpan byte) (RoSpan byte)
|
||||
(Span byte))))
|
||||
(clr-xchacha-encrypt (: LibSodium.XChaCha20Poly1305.Encrypt
|
||||
(-> (Span byte) (RoSpan byte) (RoSpan byte) (Span byte) (RoSpan byte) (RoSpan byte)
|
||||
(Span byte))))
|
||||
;; Raises `LibSodiumException` when the tag does not verify.
|
||||
(clr-xchacha-decrypt (: LibSodium.XChaCha20Poly1305.Decrypt
|
||||
(-> (Span byte) (RoSpan byte) (RoSpan byte) (RoSpan byte) (RoSpan byte) (RoSpan byte)
|
||||
(Span byte))))
|
||||
;; libsodium's randombytes_buf: the system's random source.
|
||||
(clr-random-fill (: LibSodium.RandomGenerator.Fill (-> (Span byte) void)))
|
||||
;; sodium_memzero.
|
||||
(clr-mem-zero (: LibSodium.SecureMemory.MemZero (-> (Array byte) void)))
|
||||
;; The key, the password, the salt, the iterations, the memory in bytes,
|
||||
;; and the algorithm.
|
||||
(clr-argon2-derive (: LibSodium.CryptoPasswordHashArgon.DeriveKey
|
||||
(-> (Span byte) (RoSpan byte) (RoSpan byte) int int Algorithm void)))
|
||||
;; The password, the iterations and the memory in bytes. Argon2id, with a
|
||||
;; random salt.
|
||||
(clr-argon2-hash (: LibSodium.CryptoPasswordHashArgon.HashPassword (-> (RoSpan byte) int int string)))
|
||||
(clr-argon2-verify (: LibSodium.CryptoPasswordHashArgon.VerifyPassword (-> string (RoSpan byte) bool)))
|
||||
(clr-arm-aes? (: System.Runtime.Intrinsics.Arm.Aes.IsSupported #:get))
|
||||
(clr-x86-aes? (: System.Runtime.Intrinsics.X86.Aes.IsSupported #:get))
|
||||
(clr-x86-avx? (: System.Runtime.Intrinsics.X86.Avx.IsSupported #:get)))
|
||||
|
||||
(: (-> string %a))
|
||||
(defun (refuse message) (raise (cast Exception (ArgumentException. message))))
|
||||
|
||||
;; ---------------------------------------------------------------------------
|
||||
;; Ciphers
|
||||
;; ---------------------------------------------------------------------------
|
||||
|
||||
;; In bytes.
|
||||
(: (-> Cipher int))
|
||||
(defun (cipher-key-length cipher)
|
||||
(match cipher
|
||||
(Aegis256 32)
|
||||
(XChaCha20Poly1305 32)))
|
||||
|
||||
;; In bytes.
|
||||
(: (-> Cipher int))
|
||||
(defun (cipher-nonce-length cipher)
|
||||
(match cipher
|
||||
(Aegis256 32)
|
||||
(XChaCha20Poly1305 24)))
|
||||
|
||||
;; In bytes. The ciphertext is this much longer than the plaintext.
|
||||
(: (-> Cipher int))
|
||||
(defun (cipher-tag-length cipher)
|
||||
(match cipher
|
||||
(Aegis256 32)
|
||||
(XChaCha20Poly1305 16)))
|
||||
|
||||
;; libsodium uses AES instructions for AEGIS-256 when the CPU has them: the
|
||||
;; ARMv8 AES instructions on 64-bit ARM, or AES-NI and AVX on x86. Without
|
||||
;; them it uses AES in software, which is slower than XChaCha20-Poly1305.
|
||||
;; This is the same test, as .NET reports the CPU.
|
||||
(: (-> bool))
|
||||
(defun (aegis256-accelerated?)
|
||||
(or clr-arm-aes? (and clr-x86-aes? clr-x86-avx?)))
|
||||
|
||||
;; Aegis256 when `aegis256-accelerated?`, else XChaCha20Poly1305. Both are
|
||||
;; safe; this is the faster one on this CPU.
|
||||
(: (-> Cipher))
|
||||
(defun (preferred-cipher)
|
||||
(if (aegis256-accelerated?) Aegis256 XChaCha20Poly1305))
|
||||
|
||||
(: (-> Cipher string))
|
||||
(defun (cipher-label cipher)
|
||||
(match cipher
|
||||
(Aegis256 "AEGIS-256")
|
||||
(XChaCha20Poly1305 "XChaCha20-Poly1305")))
|
||||
|
||||
(: (-> Cipher string (Array byte) int void))
|
||||
(defun (check-length! cipher what bytes wanted)
|
||||
(def n (array-length bytes))
|
||||
(when (not (= n wanted))
|
||||
(refuse (str "The " what " of " (cipher-label cipher) " must be " (int->string wanted)
|
||||
" bytes, not " (int->string n) "."))))
|
||||
|
||||
(: (-> Cipher (Array byte) void))
|
||||
(defun (check-key! cipher key) (check-length! cipher "key" key (cipher-key-length cipher)))
|
||||
|
||||
(: (-> Cipher (Array byte) void))
|
||||
(defun (check-nonce! cipher nonce) (check-length! cipher "nonce" nonce (cipher-nonce-length cipher)))
|
||||
|
||||
;; ---------------------------------------------------------------------------
|
||||
;; Keys and nonces
|
||||
;; ---------------------------------------------------------------------------
|
||||
|
||||
(: (-> int (Array byte)))
|
||||
(defun (random-array n)
|
||||
(def bytes (: (make-array n) (Array byte)))
|
||||
(clr-random-fill (array->span bytes))
|
||||
bytes)
|
||||
|
||||
;; A new random key, from the system's random source.
|
||||
(: (-> Cipher (Array byte)))
|
||||
(defun (make-aead-key cipher) (random-array (cipher-key-length cipher)))
|
||||
|
||||
;; A new random nonce, from the system's random source.
|
||||
(: (-> Cipher (Array byte)))
|
||||
(defun (make-aead-nonce cipher) (random-array (cipher-nonce-length cipher)))
|
||||
|
||||
;; ---------------------------------------------------------------------------
|
||||
;; Encryption
|
||||
;; ---------------------------------------------------------------------------
|
||||
|
||||
(: (Array byte))
|
||||
(def no-aad (make-array 0))
|
||||
|
||||
;; Writes the ciphertext of `plaintext` into `out`, then the tag. `out` is
|
||||
;; exactly as long as the two. The caller checks the key and the nonce.
|
||||
(: (-> Cipher (RoSpan byte) (RoSpan byte) (RoSpan byte) (RoSpan byte) (Span byte) void))
|
||||
(defun (encrypt-into! cipher key nonce aad plaintext out)
|
||||
(def n (ro-span-length plaintext))
|
||||
(def text (span-slice out #:to n))
|
||||
(def tag (span-slice out #:from n))
|
||||
(ignore
|
||||
(match cipher
|
||||
(Aegis256 (span-length (clr-aegis-encrypt text plaintext key tag aad nonce)))
|
||||
(XChaCha20Poly1305 (span-length (clr-xchacha-encrypt text plaintext key tag aad nonce))))))
|
||||
|
||||
;; The plaintext of the bytes of `box` from `from`: the ciphertext, then the
|
||||
;; tag. The caller checks the key, the nonce, and that the tag fits.
|
||||
;; Raises `LibSodiumException` when the tag does not verify.
|
||||
(: (-> Cipher (Array byte) (Array byte) (Array byte) (Array byte) int (Array byte)))
|
||||
(defun (decrypt-now cipher key nonce aad box from)
|
||||
(def n (- (array-length box) from (cipher-tag-length cipher)))
|
||||
(def out (: (make-array n) (Array byte)))
|
||||
(def text (array->ro-span box #:from from #:to (+ from n)))
|
||||
(def tag (array->ro-span box #:from (+ from n)))
|
||||
(def k (array->ro-span key))
|
||||
(def a (array->ro-span aad))
|
||||
(def iv (array->ro-span nonce))
|
||||
(ignore
|
||||
(match cipher
|
||||
(Aegis256 (span-length (clr-aegis-decrypt (array->span out) text k tag a iv)))
|
||||
(XChaCha20Poly1305 (span-length (clr-xchacha-decrypt (array->span out) text k tag a iv)))))
|
||||
out)
|
||||
|
||||
;; `decrypt-now`, with `None` when the tag does not verify.
|
||||
(: (-> Cipher (Array byte) (Array byte) (Array byte) (Array byte) int (Option (Array byte))))
|
||||
(defun (decrypt-checked cipher key nonce aad box from)
|
||||
(match (try (decrypt-now cipher key nonce aad box from) #:catch (LibSodium.LibSodiumException))
|
||||
((Ok plaintext) (Some plaintext))
|
||||
((Err _) None)))
|
||||
|
||||
;; The ciphertext and then the tag: `cipher-tag-length` bytes longer than
|
||||
;; `plaintext`. The nonce is not in the result; store it beside it.
|
||||
(: (-> Cipher (Array byte) (Array byte) (Array byte) (#:aad (Array byte)) (Array byte)))
|
||||
(defun (aead-encrypt cipher key nonce plaintext #:aad no-aad)
|
||||
(check-key! cipher key)
|
||||
(check-nonce! cipher nonce)
|
||||
(def out (: (make-array (+ (array-length plaintext) (cipher-tag-length cipher))) (Array byte)))
|
||||
(encrypt-into! cipher (array->ro-span key) (array->ro-span nonce) (array->ro-span aad)
|
||||
(array->ro-span plaintext) (array->span out))
|
||||
out)
|
||||
|
||||
;; The plaintext of a result of `aead-encrypt`. `None` when the tag does not
|
||||
;; verify, and when `ciphertext` is shorter than a tag.
|
||||
(: (-> Cipher (Array byte) (Array byte) (Array byte) (#:aad (Array byte)) (Option (Array byte))))
|
||||
(defun (aead-decrypt cipher key nonce ciphertext #:aad no-aad)
|
||||
(check-key! cipher key)
|
||||
(check-nonce! cipher nonce)
|
||||
(if (< (array-length ciphertext) (cipher-tag-length cipher))
|
||||
None
|
||||
(decrypt-checked cipher key nonce aad ciphertext 0)))
|
||||
|
||||
;; A new random nonce, the ciphertext, and the tag, in one array.
|
||||
(: (-> Cipher (Array byte) (Array byte) (#:aad (Array byte)) (Array byte)))
|
||||
(defun (aead-seal cipher key plaintext #:aad no-aad)
|
||||
(check-key! cipher key)
|
||||
(def nonce-length (cipher-nonce-length cipher))
|
||||
(def out (: (make-array (+ nonce-length (array-length plaintext) (cipher-tag-length cipher)))
|
||||
(Array byte)))
|
||||
(clr-random-fill (array->span out #:to nonce-length))
|
||||
(encrypt-into! cipher (array->ro-span key) (array->ro-span out #:to nonce-length)
|
||||
(array->ro-span aad) (array->ro-span plaintext) (array->span out #:from nonce-length))
|
||||
out)
|
||||
|
||||
;; The plaintext of a result of `aead-seal`. `None` when the tag does not
|
||||
;; verify, and when `sealed` is shorter than a nonce and a tag.
|
||||
(: (-> Cipher (Array byte) (Array byte) (#:aad (Array byte)) (Option (Array byte))))
|
||||
(defun (aead-unseal cipher key sealed #:aad no-aad)
|
||||
(check-key! cipher key)
|
||||
(def nonce-length (cipher-nonce-length cipher))
|
||||
(if (< (array-length sealed) (+ nonce-length (cipher-tag-length cipher)))
|
||||
None
|
||||
(decrypt-checked cipher key (ro-span->array (array->ro-span sealed #:to nonce-length)) aad
|
||||
sealed nonce-length)))
|
||||
|
||||
;; ---------------------------------------------------------------------------
|
||||
;; Argon2id
|
||||
;; ---------------------------------------------------------------------------
|
||||
;;
|
||||
;; libsodium's Argon2id is version 1.3 with one lane (p=1). The cost is the
|
||||
;; number of iterations (t) and the memory in KiB (m).
|
||||
|
||||
;; In bytes: crypto_pwhash_SALTBYTES.
|
||||
(: int)
|
||||
(def argon2id-salt-length 16)
|
||||
|
||||
;; A new random salt for `argon2id-derive-key`, from the system's random
|
||||
;; source. A salt need not be secret: store it with the data.
|
||||
(: (-> (Array byte)))
|
||||
(defun (make-argon2id-salt) (random-array argon2id-salt-length))
|
||||
|
||||
;; libsodium's OPSLIMIT_INTERACTIVE and MEMLIMIT_INTERACTIVE: 2 iterations
|
||||
;; over 64 MiB.
|
||||
(: int)
|
||||
(def default-argon2id-iterations 2)
|
||||
|
||||
(: int)
|
||||
(def default-argon2id-memory-kib 65536)
|
||||
|
||||
;; crypto_pwhash_BYTES_MIN.
|
||||
(: int)
|
||||
(def min-key-length 16)
|
||||
|
||||
;; crypto_pwhash_MEMLIMIT_MIN is 8 KiB. LibSodium.Net takes the memory in
|
||||
;; bytes as an int, so the most it can give is just under 2 GiB.
|
||||
(: int)
|
||||
(def min-memory-kib 8)
|
||||
|
||||
(: int)
|
||||
(def max-memory-kib 2097151)
|
||||
|
||||
(: (-> int int void))
|
||||
(defun (check-cost! iterations memory-kib)
|
||||
(when (< iterations 1)
|
||||
(refuse (str "The iteration count must be at least 1, not " (int->string iterations) ".")))
|
||||
(when (or (< memory-kib min-memory-kib) (> memory-kib max-memory-kib))
|
||||
(refuse (str "The memory must be from " (int->string min-memory-kib) " to "
|
||||
(int->string max-memory-kib) " KiB, not " (int->string memory-kib) "."))))
|
||||
|
||||
;; The value of a thread's result, or its exception raised again.
|
||||
(: (-> (Result Exception %a) %a))
|
||||
(defun (unwrap-thread r)
|
||||
(match r
|
||||
((Ok v) v)
|
||||
((Err e) (raise e))))
|
||||
|
||||
(: (-> string (Array byte) int int int (Array byte)))
|
||||
(defun (derive-now password salt iterations memory-kib length)
|
||||
(check-cost! iterations memory-kib)
|
||||
(when (< length min-key-length)
|
||||
(refuse (str "The key length must be at least " (int->string min-key-length) " bytes, not "
|
||||
(int->string length) ".")))
|
||||
(when (not (= (array-length salt) argon2id-salt-length))
|
||||
(refuse (str "The salt must be " (int->string argon2id-salt-length) " bytes, not "
|
||||
(int->string (array-length salt)) ".")))
|
||||
(def key (: (make-array length) (Array byte)))
|
||||
(def secret (string->bytes password))
|
||||
(try (clr-argon2-derive (array->span key) (array->ro-span secret) (array->ro-span salt)
|
||||
iterations (* memory-kib 1024) Algorithm.Argon2id13)
|
||||
#:finally (clr-mem-zero secret))
|
||||
key)
|
||||
|
||||
(: (-> string int int string))
|
||||
(defun (hash-now password iterations memory-kib)
|
||||
(check-cost! iterations memory-kib)
|
||||
(def secret (string->bytes password))
|
||||
(try (clr-argon2-hash (array->ro-span secret) iterations (* memory-kib 1024))
|
||||
#:finally (clr-mem-zero secret)))
|
||||
|
||||
;; What a stored hash says about its cost.
|
||||
(type (: Argon2Cost (Record (: cost-memory-kib int)
|
||||
(: cost-iterations int)
|
||||
(: cost-lanes int))))
|
||||
|
||||
;; ASCII digits, as an int. `None` for an empty string, for any other
|
||||
;; character, and for a number too big for an int.
|
||||
(: (-> string (Option int)))
|
||||
(defun (decimal text)
|
||||
(def bytes (string->bytes text))
|
||||
(def n (array-length bytes))
|
||||
(let go ((i 0) (v 0L))
|
||||
(cond
|
||||
((= i n) (if (> n 0) (Some (cast int v)) None))
|
||||
(else
|
||||
(def b (cast int (array-ref bytes i)))
|
||||
(def next (+ (* v 10L) (cast long (- b 48))))
|
||||
(if (and (>= b 48) (<= b 57) (<= next 2147483647L))
|
||||
(go (+ i 1) next)
|
||||
None)))))
|
||||
|
||||
;; The number after `prefix` in `field`, as `m=65536`.
|
||||
(: (-> string string (Option int)))
|
||||
(defun (field-value field prefix)
|
||||
(if (string-starts-with? field prefix)
|
||||
(decimal (string-drop field (string-length prefix)))
|
||||
None))
|
||||
|
||||
;; Raises `ArgumentException` when `stored` is not
|
||||
;; `$argon2id$v=19$m=<n>,t=<n>,p=<n>$<salt>$<hash>`. The salt and the hash
|
||||
;; must not be empty. Their base64 is checked by libsodium.
|
||||
(: (-> string Argon2Cost))
|
||||
(defun (parse-stored stored)
|
||||
(def fail "Not an Argon2id hash: expected $argon2id$v=19$m=<n>,t=<n>,p=<n>$<salt>$<hash>.")
|
||||
(def parts (string-split stored "$"))
|
||||
;; The leading `$` makes the first part empty.
|
||||
(if (not (and (= (vec-length parts) 6)
|
||||
(= (vec-ref parts 0) "")
|
||||
(= (vec-ref parts 1) "argon2id")
|
||||
(= (vec-ref parts 2) "v=19")
|
||||
(> (string-length (vec-ref parts 4)) 0)
|
||||
(> (string-length (vec-ref parts 5)) 0)))
|
||||
(refuse fail)
|
||||
(let ((fields (string-split (vec-ref parts 3) ",")))
|
||||
(if (not (= (vec-length fields) 3))
|
||||
(refuse fail)
|
||||
(match (Tuple (field-value (vec-ref fields 0) "m=")
|
||||
(field-value (vec-ref fields 1) "t=")
|
||||
(field-value (vec-ref fields 2) "p="))
|
||||
((Tuple (Some m) (Some t) (Some p))
|
||||
(Argon2Cost (cost-memory-kib m) (cost-iterations t) (cost-lanes p)))
|
||||
(_ (refuse fail)))))))
|
||||
|
||||
(: (-> string string bool))
|
||||
(defun (verify-now password stored)
|
||||
(ignore (parse-stored stored))
|
||||
(def secret (string->bytes password))
|
||||
(try (clr-argon2-verify stored (array->ro-span secret))
|
||||
#:finally (clr-mem-zero secret)))
|
||||
|
||||
;; Argon2id bytes of `password` with the salt given: the same password, salt
|
||||
;; and cost give the same bytes. Use it to make a key from a password.
|
||||
;; Raises `ArgumentException` when the salt is not `argon2id-salt-length`
|
||||
;; bytes, the length is less than 16, or the cost is out of range.
|
||||
(: (-> string (Array byte) (#:iterations int) (#:memory-kib int) (#:length int) (Array byte)))
|
||||
(defbjouble (argon2id-derive-key password salt #:iterations default-argon2id-iterations
|
||||
#:memory-kib default-argon2id-memory-kib #:length 32)
|
||||
(#:sync (derive-now password salt iterations memory-kib length))
|
||||
(#:bjo (unwrap-thread
|
||||
(sync (spawn/thread #(derive-now password salt iterations memory-kib length))))))
|
||||
|
||||
;; A PHC string to store: `$argon2id$v=19$m=65536,t=2,p=1$<salt>$<hash>`,
|
||||
;; with a random salt of 16 bytes and a hash of 32. Raises
|
||||
;; `ArgumentException` when the cost is out of range.
|
||||
(: (-> string (#:iterations int) (#:memory-kib int) string))
|
||||
(defbjouble (argon2id-hash password #:iterations default-argon2id-iterations
|
||||
#:memory-kib default-argon2id-memory-kib)
|
||||
(#:sync (hash-now password iterations memory-kib))
|
||||
(#:bjo (unwrap-thread (sync (spawn/thread #(hash-now password iterations memory-kib))))))
|
||||
|
||||
;; True when `password` gives `stored`. The cost comes from `stored`.
|
||||
;; Raises `ArgumentException` when `stored` is not an Argon2id PHC string.
|
||||
;; This is an error in the stored data, not a wrong password.
|
||||
(: (-> string string bool))
|
||||
(defbjouble (argon2id-verify password stored)
|
||||
(#:sync (verify-now password stored))
|
||||
(#:bjo (unwrap-thread (sync (spawn/thread #(verify-now password stored))))))
|
||||
|
||||
;; True when `stored` was made with fewer iterations or less memory than
|
||||
;; given. Check this after a correct `argon2id-verify`, and store a new hash
|
||||
;; of the password if it is true. Raises `ArgumentException` when `stored` is
|
||||
;; not an Argon2id PHC string.
|
||||
(: (-> string (#:iterations int) (#:memory-kib int) bool))
|
||||
(defun (argon2id-needs-rehash? stored #:iterations default-argon2id-iterations
|
||||
#:memory-kib default-argon2id-memory-kib)
|
||||
(def cost (parse-stored stored))
|
||||
(or (< (record-ref cost cost-iterations) iterations)
|
||||
(< (record-ref cost cost-memory-kib) memory-kib)))
|
||||
|
||||
;; ---------------------------------------------------------------------------
|
||||
;; Memory
|
||||
;; ---------------------------------------------------------------------------
|
||||
|
||||
;; Sets every byte of `bytes` to 0, with sodium_memzero, which the compiler
|
||||
;; does not remove. Use it on a key when you no longer need it. The garbage
|
||||
;; collector can have moved the array before, and it does not clear the old
|
||||
;; copy, so this makes copies fewer but does not remove all of them.
|
||||
(: (-> (Array byte) void))
|
||||
(defun (wipe! bytes) (clr-mem-zero bytes))
|
||||
Loading…
Add table
Add a link
Reference in a new issue