libsodium_crypto_kdf (libsodium v3.0.1)
View SourceDerive many subkeys from a single keybytes/0 byte master key with the
default construction, currently BLAKE2b.
Each subkey is identified by a 64-bit integer SubkeyId and a
contextbytes/0 byte context, such as <<"Examples">>, which separates
keys derived for different purposes. Subkeys are between bytes_min/0
and bytes_max/0 bytes long. This is not a password hash; use
libsodium_crypto_pwhash to derive keys from passwords.
Key = libsodium_crypto_kdf:keygen(),
Subkey1 = libsodium_crypto_kdf:derive_from_key(32, 1, <<"Examples">>, Key),
Subkey2 = libsodium_crypto_kdf:derive_from_key(32, 2, <<"Examples">>, Key),
32 = byte_size(Subkey1),
true = Subkey1 =/= Subkey2,
Subkey1 = libsodium_crypto_kdf:derive_from_key(32, 1, <<"Examples">>, Key).
Summary
Types
A crypto_kdf_CONTEXTBYTES (8-byte) binary.
A crypto_kdf_KEYBYTES (32-byte) binary.
A crypto_kdf_KEYBYTES (32-byte) binary.
Functions
Return crypto_kdf_bytes_max(), in bytes.
Return crypto_kdf_bytes_min(), in bytes.
Return crypto_kdf_contextbytes(), in bytes.
Derive the SubkeyLen byte subkey number SubkeyId for the
contextbytes/0 byte context Ctx from the keybytes/0 byte master
key Key.
Return crypto_kdf_keybytes(), in bytes.
Generate a random keybytes/0 byte master key.
Return crypto_kdf_primitive() as an atom.
Types
Functions
-spec bytes_max() -> Result when Result :: 64.
Return crypto_kdf_bytes_max(), in bytes.
-spec bytes_min() -> Result when Result :: 16.
Return crypto_kdf_bytes_min(), in bytes.
-spec contextbytes() -> Result when Result :: 8.
Return crypto_kdf_contextbytes(), in bytes.
-spec derive_from_key(SubkeyLen, SubkeyId, Ctx, Key) -> Result when SubkeyLen :: non_neg_integer(), SubkeyId :: non_neg_integer(), Ctx :: ctx(), Key :: key(), Result :: binary() | -1.
Derive the SubkeyLen byte subkey number SubkeyId for the
contextbytes/0 byte context Ctx from the keybytes/0 byte master
key Key.
Returns -1 when SubkeyLen is outside bytes_min/0 to bytes_max/0.
-spec keybytes() -> Result when Result :: 32.
Return crypto_kdf_keybytes(), in bytes.
-spec keygen() -> Result when Result :: k().
Generate a random keybytes/0 byte master key.
-spec primitive() -> Result when Result :: blake2b.
Return crypto_kdf_primitive() as an atom.