secret/kdf
HMAC-SHA256 and HKDF-SHA256 over linear Secrets.
Since: 0.1.0
sha256-raw!
(sha256-raw! [data ptr<void> n : int out ptr<void>] :)
SHA-256 of `n` bytes at `data` into 32 bytes at `out`.
| data | input bytes (may be NULL when n is 0) | |
| n | input length in bytes | |
| out | destination, at least 32 bytes |
Nothing; `out` holds the digest.
(sha256-raw! msg 3 digest) ; "abc" => ba7816bf...
Since: 0.1.0
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`.
| key | key bytes | |
| klen | key length | |
| msg | message bytes | |
| mlen | message length | |
| out | destination, at least 32 bytes |
Nothing; `out` holds the MAC.
(hmac-sha256-raw! k 4 m 28 mac)
Since: 0.1.0
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.
| ikm | input keying material | |
| ilen | its length | |
| salt | optional salt (NULL/0 means a 32-byte zero salt, per RFC 5869) | |
| slen | salt length | |
| info | optional context/application info | |
| inflen | info length | |
| out | destination | |
| olen | bytes 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
secret-hmac-sha256
(secret-hmac-sha256 [^borrow key : Secret p ptr<void> n : int])
MAC a message under a borrowed key.
| key | the MAC key, borrowed (not consumed) | |
| p | message bytes | |
| n | message 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
secret-hkdf-sha256
(secret-hkdf-sha256 [^borrow ikm : Secret salt : cstr info : cstr n : int])
derive `n` bytes from borrowed input keying material.
| ikm | input keying material, borrowed (not consumed) | |
| salt | optional salt string ("" selects the RFC 5869 zero salt) | |
| info | optional context/application info string | |
| n | bytes 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 thehkdf-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`.