Available on crate features
kdf-hkdf or kdf-argon2 only.Expand description
Key Derivation Functions (KDF).
Two algorithms ship in 0.6.0, addressing different needs:
| Algorithm | Purpose | Speed | Feature |
|---|---|---|---|
| HKDF-SHA256 | Derive one-or-many subkeys from a high-entropy IKM | Fast (µs) | kdf-hkdf |
| HKDF-SHA512 | Same, wider underlying digest | Fast (µs) | kdf-hkdf |
| Argon2id | Derive a key from a password (low-entropy input) | Slow (~100ms) | kdf-argon2 |
§Which one do I want?
-
HKDF (RFC 5869) for deriving subkeys from a master key, a shared secret from a key exchange, or anything else already high-entropy. HKDF does not protect against weak inputs — feeding it a password is a security mistake.
-
Argon2id (RFC 9106) for deriving a key from a password. The memory-hardness and tuneable cost are what protect against brute-force attempts; the slowness is the point.
§Examples
Deriving a 32-byte subkey from a master:
use crypt_io::kdf;
let master = [0x42u8; 32];
let subkey = kdf::hkdf_sha256(&master, Some(b"salt"), b"app:session:v1", 32)?;
assert_eq!(subkey.len(), 32);Hashing and verifying a password:
use crypt_io::kdf;
let phc = kdf::argon2_hash(b"correct horse battery staple")?;
assert!(kdf::argon2_verify(&phc, b"correct horse battery staple")?);
assert!(!kdf::argon2_verify(&phc, b"wrong guess")?);Structs§
- Argon2
Params kdf-argon2 - Tuneable Argon2id parameters.
Constants§
- ARGO
N2_ DEFAULT_ OUTPUT_ LEN kdf-argon2 - Default Argon2id output length, in bytes. Equal to
32(256 bits). - ARGO
N2_ DEFAULT_ SALT_ LEN kdf-argon2 - Default Argon2id salt length, in bytes. Equal to
16(128 bits, the PHC-recommended minimum). - HKDF_
MAX_ OUTPUT_ SHA256 kdf-hkdf - Maximum HKDF-SHA256 output length, in bytes. Equal to
255 * 32 = 8160. - HKDF_
MAX_ OUTPUT_ SHA512 kdf-hkdf - Maximum HKDF-SHA512 output length, in bytes. Equal to
255 * 64 = 16320.
Functions§
- argon2_
hash kdf-argon2 - Hash
passwordwith Argon2id using the default parameter set and a fresh random salt. Returns the PHC-encoded hash string. - argon2_
hash_ with_ params kdf-argon2 - Like
argon2_hashbut uses caller-supplied parameters. - argon2_
verify kdf-argon2 - Verify
passwordagainst a PHC-encoded Argon2 hash string. - hkdf_
sha256 kdf-hkdf - Derive
lenbytes of output keying material via HKDF-SHA256. - hkdf_
sha512 kdf-hkdf - Derive
lenbytes of output keying material via HKDF-SHA512.