Expand description
§Key derivation function
Implements libsodium’s key derivation functions (crypto_kdf_*).
The Blake2b crypto_kdf_* functions derive bounded subkeys from a random
main key and an 8-byte application context. The HKDF functions derive output
keying material from existing input keying material, using an optional salt
and a public context string.
Use crypto_kdf_derive_from_key when you have one random main key and need
numbered subkeys. Use the HKDF functions when you already have keying
material, such as a key-exchange result, and need to turn it into one or
more purpose-specific keys.
For details, refer to libsodium docs.
§Classic API example
use base64::Engine as _;
use base64::engine::general_purpose;
use dryoc::classic::crypto_kdf::*;
// Generate a random main key
let main_key = crypto_kdf_keygen();
// Provide exactly 8 bytes of public context data
let context = b"WTCHKEYS";
// Derive 20 subkeys
for i in 0..20 {
let mut key = Key::default();
crypto_kdf_derive_from_key(&mut key, i, context, &main_key).expect("kdf failed");
println!("Subkey {}: {}", i, general_purpose::STANDARD.encode(&key));
}§HKDF-SHA-256 example
use dryoc::classic::crypto_kdf::*;
let mut prk = HkdfSha256Key::default();
crypto_kdf_hkdf_sha256_extract(&mut prk, Some(b"salt"), b"Some rise by sin");
let mut output = [0u8; 42];
crypto_kdf_hkdf_sha256_expand(&mut output, b"encryption key", &prk).expect("expand failed");The HKDF extract step can also be fed incrementally. This is useful when the input keying material arrives in pieces:
use dryoc::classic::crypto_kdf::*;
let mut state = crypto_kdf_hkdf_sha256_extract_init(Some(b"salt"));
crypto_kdf_hkdf_sha256_extract_update(&mut state, b"Some rise ");
crypto_kdf_hkdf_sha256_extract_update(&mut state, b"by sin");
let mut prk = HkdfSha256Key::default();
crypto_kdf_hkdf_sha256_extract_final(state, &mut prk);§HKDF-SHA-512 example
use dryoc::classic::crypto_kdf::*;
let mut prk: HkdfSha512Key = [0u8; 64];
crypto_kdf_hkdf_sha512_extract(&mut prk, None, b"and some by virtue fall");
let mut output = [0u8; 64];
crypto_kdf_hkdf_sha512_expand(&mut output, b"authentication key", &prk).expect("expand failed");Structs§
- Hkdf
Sha256 State - Internal state for incremental HKDF-SHA-256 extract.
- Hkdf
Sha512 State - Internal state for incremental HKDF-SHA-512 extract.
Functions§
- crypto_
kdf_ derive_ from_ key - Derives
subkeyfrommain_key, usingcontextandsubkey_idsuch thatsubkeywill always be the same for the given set of inputs, butmain_keycannot be derived fromsubkey. - crypto_
kdf_ hkdf_ sha256_ expand - Expands an HKDF-SHA-256 pseudorandom key into output keying material.
- crypto_
kdf_ hkdf_ sha256_ extract - Creates an HKDF-SHA-256 pseudorandom key from input keying material.
- crypto_
kdf_ hkdf_ sha256_ extract_ final - Finalizes incremental HKDF-SHA-256 extract and writes the pseudorandom key.
- crypto_
kdf_ hkdf_ sha256_ extract_ init - Initializes incremental HKDF-SHA-256 extract.
- crypto_
kdf_ hkdf_ sha256_ extract_ update - Updates incremental HKDF-SHA-256 extract with input keying material.
- crypto_
kdf_ hkdf_ sha256_ keygen - Generates a random pseudorandom key for HKDF-SHA-256 expand.
- crypto_
kdf_ hkdf_ sha512_ expand - Expands an HKDF-SHA-512 pseudorandom key into output keying material.
- crypto_
kdf_ hkdf_ sha512_ extract - Creates an HKDF-SHA-512 pseudorandom key from input keying material.
- crypto_
kdf_ hkdf_ sha512_ extract_ final - Finalizes incremental HKDF-SHA-512 extract and writes the pseudorandom key.
- crypto_
kdf_ hkdf_ sha512_ extract_ init - Initializes incremental HKDF-SHA-512 extract.
- crypto_
kdf_ hkdf_ sha512_ extract_ update - Updates incremental HKDF-SHA-512 extract with input keying material.
- crypto_
kdf_ hkdf_ sha512_ keygen - Generates a random pseudorandom key for HKDF-SHA-512 expand.
- crypto_
kdf_ keygen - Generates a random key, suitable for use as a main key with
crypto_kdf_derive_from_key.
Type Aliases§
- Context
- Context for key derivation.
- Hkdf
Sha256 Key - Pseudorandom key for HKDF-SHA-256.
- Hkdf
Sha512 Key - Pseudorandom key for HKDF-SHA-512.
- Key
- Key type for the main key used for deriving subkeys.