module

Beryl::Crypto::V1::HashBasedKeyDerivationFunction(D)

HKDF (RFC 5869): turns key material that is secret but uneven, such as a shared secret from a key exchange, into as many independent keys as a protocol needs. D is SHA256, SHA384 or SHA512; Compatibility::SHA1 is accepted for formats that already use it.

hkdf = Beryl::Crypto::V1::HKDF(Beryl::Crypto::V1::SHA256)
key = hkdf.derive_key(shared_secret, salt: salt, info: "session cookies", bytesize: 32)

derive_key is the whole function. extract and expand are its two steps, for protocols such as TLS 1.3 that call them separately.

Constants

MAXIMUM_BLOCK_COUNT = 255

RFC 5869 §2.3 counts output blocks in one byte.

Class methods

derive_key(input_key_material : Bytes | String, *, salt : Bytes | String = Bytes.empty, info : Bytes | String = Bytes.empty, bytesize : Int32) : Bytes

Returns bytesize bytes of key derived from input_key_material: extract, then expand.

Source
expand(pseudorandom_key : Bytes | String, *, info : Bytes | String = Bytes.empty, bytesize : Int32) : Bytes

Returns bytesize bytes of key derived from pseudorandom_key, which should be one hash output long (the result of extract). info names the purpose, so that different purposes get unrelated keys.

Raises DerivedKeyTooLong when bytesize is over 255 hash outputs.

Source
extract(input_key_material : Bytes | String, *, salt : Bytes | String = Bytes.empty) : Bytes

Returns the pseudorandom key (one hash output long) concentrated from input_key_material. An empty salt stands for a string of zeros one hash output long, as RFC 5869 §2.2 defines.

Source