No matching definitions.

secret/kdf

src/secret/kdf.tur

HMAC-SHA256 and HKDF-SHA256 over linear Secrets.

Since: 0.1.0

defn

sha256-raw!

(sha256-raw! [data ptr<void> n : int out ptr<void>] :)

SHA-256 of `n` bytes at `data` into 32 bytes at `out`.

datainput bytes (may be NULL when n is 0)
ninput length in bytes
outdestination, at least 32 bytes

Nothing; `out` holds the digest.

(sha256-raw! msg 3 digest)   ; "abc" => ba7816bf...

Since: 0.1.0

defn

hmac-sha256-raw!

(hmac-sha256-raw! [key ptr<void> klen : int msg ptr<void> mlen : int out ptr<void>] :)

HMAC-SHA256(key, msg) into 32 bytes at `out`.

keykey bytes
klenkey length
msgmessage bytes
mlenmessage length
outdestination, at least 32 bytes

Nothing; `out` holds the MAC.

(hmac-sha256-raw! k 4 m 28 mac)

Since: 0.1.0

defn

hkdf-sha256-raw!

(hkdf-sha256-raw! [ikm ptr<void> ilen : int salt ptr<void> slen : int info ptr<void> inflen : int out ptr<void> olen : int] :)

RFC 5869 extract-then-expand into `olen` bytes.

ikminput keying material
ilenits length
saltoptional salt (NULL/0 means a 32-byte zero salt, per RFC 5869)
slensalt length
infooptional context/application info
infleninfo length
outdestination
olenbytes to derive; at most 255*32 = 8160

true on success; false if olen is out of range or an allocation failed.

(hkdf-sha256-raw! ikm 22 salt 13 info 10 okm 42)

Since: 0.1.0

defn

secret-hmac-sha256

(secret-hmac-sha256 [^borrow key : Secret p ptr<void> n : int])

MAC a message under a borrowed key.

keythe MAC key, borrowed (not consumed)
pmessage bytes
nmessage length

(Result Secret cstr) -- ok carries a 32-byte Secret holding the MAC. It is a Secret rather than a plain buffer because a MAC compared with the wrong primitive is exactly as dangerous as a leaked key; keeping it in a Secret makes `secret-eq?` (constant-time) the path of least resistance.

(let [r (secret-hmac-sha256 k body len)]
    (if (ok? r) (ok-val r) ...))

Since: 0.1.0

defn

secret-hkdf-sha256

(secret-hkdf-sha256 [^borrow ikm : Secret salt : cstr info : cstr n : int])

derive `n` bytes from borrowed input keying material.

ikminput keying material, borrowed (not consumed)
saltoptional salt string ("" selects the RFC 5869 zero salt)
infooptional context/application info string
nbytes to derive; 1..8160

(Result Secret cstr) -- ok carries an n-byte Secret.

(secret-hkdf-sha256 master "v1-salt" "encryption key" 32)

Since: 0.1.0

Internal definitions
sha256-block!-- one SHA-256 compression round over a 64-byte block.
hmac-into-scratch-- with-secret callback: MAC the message under the
hkdf-with-cstrs-- HKDF with cstr salt/info, measured with strlen.
hmac-then-wrap-- MAC into the scratch buffer, copy it into a Secret,
hkdf-then-wrap-- derive into the scratch buffer, copy it into a Secret,
wrap-scratch-- copy n scratch bytes into a Secret, then free the scratch.
fail-scratch-- release the scratch and report `msg`.