libsodium_crypto_kdf_hkdf_sha256 (libsodium v3.0.1)

View Source

HKDF (RFC 5869) with HMAC-SHA-256. extract/2 condenses input keying material, such as a Diffie-Hellman shared secret, and an optional salt into a keybytes/0 byte pseudorandom key (PRK); expand/3 then derives up to bytes_max/0 bytes from the PRK for each context. A uniformly random key, such as one from keygen/0, can be used as the PRK directly.

The extraction can also be streamed: extract_init/1, extract_update/2, and extract_final/1 thread an opaque state/0, and each call returns a new state and leaves its argument unchanged.

HKDF is mainly useful for interoperability with existing protocols. To derive subkeys from a uniformly random key, libsodium_crypto_kdf is simpler; to derive keys from passwords, use libsodium_crypto_pwhash.

PRK = libsodium_crypto_kdf_hkdf_sha256:extract(<<"salt">>, <<"input keying material">>),
32 = byte_size(PRK),
Key1 = libsodium_crypto_kdf_hkdf_sha256:expand(32, <<"encryption key">>, PRK),
Key2 = libsodium_crypto_kdf_hkdf_sha256:expand(32, <<"authentication key">>, PRK),
true = Key1 =/= Key2,
State0 = libsodium_crypto_kdf_hkdf_sha256:extract_init(<<"salt">>),
State1 = libsodium_crypto_kdf_hkdf_sha256:extract_update(State0, <<"input keying ">>),
State2 = libsodium_crypto_kdf_hkdf_sha256:extract_update(State1, <<"material">>),
PRK = libsodium_crypto_kdf_hkdf_sha256:extract_final(State2).

Summary

Types

A crypto_kdf_hkdf_sha256_KEYBYTES (32-byte) binary.

An opaque native crypto_kdf_hkdf_sha256_state.

Functions

Return crypto_kdf_hkdf_sha256_bytes_max(), in bytes.

Return crypto_kdf_hkdf_sha256_bytes_min(), in bytes.

Derive OutLen bytes from the keybytes/0 byte pseudorandom key PRK for the context Ctx (default empty).

Derive OutLen bytes from the keybytes/0 byte pseudorandom key PRK for the context Ctx (default empty).

Extract a keybytes/0 byte pseudorandom key from the input keying material IKM and the optional Salt (default empty).

Extract a keybytes/0 byte pseudorandom key from the input keying material IKM and the optional Salt (default empty).

Finish the streaming extraction in State, returning a keybytes/0 byte pseudorandom key.

Start a streaming extraction with the optional Salt (default empty) and return its state.

Start a streaming extraction with the optional Salt (default empty) and return its state.

Absorb the input keying material IKM into State and return the updated state.

Return crypto_kdf_hkdf_sha256_keybytes(), in bytes.

Generate a random keybytes/0 byte pseudorandom key.

Return crypto_kdf_hkdf_sha256_statebytes(), in bytes.

Types

prk()

-type prk() :: <<_:256>>.

A crypto_kdf_hkdf_sha256_KEYBYTES (32-byte) binary.

state()

An opaque native crypto_kdf_hkdf_sha256_state.

Operations never modify a state: each returns a new one, so earlier states remain valid and can be reused or shared between processes. States cannot be serialized; they exist only within the running VM.

Functions

bytes_max()

-spec bytes_max() -> Result when Result :: 8160.

Return crypto_kdf_hkdf_sha256_bytes_max(), in bytes.

bytes_min()

-spec bytes_min() -> Result when Result :: 0.

Return crypto_kdf_hkdf_sha256_bytes_min(), in bytes.

expand(OutLen, PRK)

-spec expand(OutLen, PRK) -> Result
                when OutLen :: non_neg_integer(), PRK :: prk(), Result :: binary() | -1.

Derive OutLen bytes from the keybytes/0 byte pseudorandom key PRK for the context Ctx (default empty).

Returns -1 when OutLen is greater than bytes_max/0.

expand(OutLen, Ctx, PRK)

-spec expand(OutLen, Ctx, PRK) -> Result
                when OutLen :: non_neg_integer(), Ctx :: binary(), PRK :: prk(), Result :: binary() | -1.

Derive OutLen bytes from the keybytes/0 byte pseudorandom key PRK for the context Ctx (default empty).

Returns -1 when OutLen is greater than bytes_max/0.

extract(IKM)

-spec extract(IKM) -> Result when IKM :: binary(), Result :: prk() | -1.

Extract a keybytes/0 byte pseudorandom key from the input keying material IKM and the optional Salt (default empty).

extract(Salt, IKM)

-spec extract(Salt, IKM) -> Result when Salt :: binary(), IKM :: binary(), Result :: prk() | -1.

Extract a keybytes/0 byte pseudorandom key from the input keying material IKM and the optional Salt (default empty).

extract_final(State)

-spec extract_final(State) -> Result when State :: state(), Result :: prk() | -1.

Finish the streaming extraction in State, returning a keybytes/0 byte pseudorandom key.

extract_init()

-spec extract_init() -> Result when Result :: state() | -1.

Start a streaming extraction with the optional Salt (default empty) and return its state.

extract_init(Salt)

-spec extract_init(Salt) -> Result when Salt :: binary(), Result :: state() | -1.

Start a streaming extraction with the optional Salt (default empty) and return its state.

extract_update(State, Ikm)

-spec extract_update(State, Ikm) -> Result
                        when State :: state(), Ikm :: binary(), Result :: state() | -1.

Absorb the input keying material IKM into State and return the updated state.

keybytes()

-spec keybytes() -> Result when Result :: 32.

Return crypto_kdf_hkdf_sha256_keybytes(), in bytes.

keygen()

-spec keygen() -> Result when Result :: prk().

Generate a random keybytes/0 byte pseudorandom key.

statebytes()

-spec statebytes() -> Result when Result :: 208.

Return crypto_kdf_hkdf_sha256_statebytes(), in bytes.