Skip to main content

Module hkdf

Module hkdf 

Source
Expand description

§HKDF key derivation

HkdfSha256 and HkdfSha512 provide Rustaceous wrappers around libsodium’s HKDF-SHA-256 and HKDF-SHA-512 functions.

HKDF turns input keying material into one or more independent keys. It has two steps:

  • extract: mix the input keying material with an optional salt to produce a pseudorandom key (PRK)
  • expand: derive output bytes from that PRK and a context string

Use HKDF when you already have keying material, such as a shared secret from key exchange, and need separate keys for different purposes. The context is public domain-separation data; changing it changes the derived output.

§Rustaceous API example

use dryoc::hkdf::{HkdfSha256, HkdfSha256Prk};

let hkdf: HkdfSha256 =
    HkdfSha256::extract(Some(b"Act IV salt"), b"Now is the winter of our discontent");
let output: Vec<u8> = hkdf
    .expand_to_vec(42, b"session key")
    .expect("expand failed");
assert_eq!(output.len(), 42);

§One-shot extract and expand

use dryoc::hkdf::HkdfSha512;

let output = HkdfSha512::extract_and_expand_to_vec(
    64,
    Some(b"optional deployment salt"),
    b"Our remedies oft in ourselves do lie",
    b"application secret",
)
.expect("expand failed");
assert_eq!(output.len(), 64);

§Reusing an extracted PRK

use dryoc::hkdf::{HkdfSha256, HkdfSha256Prk};

let hkdf = HkdfSha256::extract(Some(b"deployment salt"), b"We know what we are");
let encryption_key: HkdfSha256Prk = hkdf.expand(b"encryption key").expect("expand failed");
let authentication_key: HkdfSha256Prk =
    hkdf.expand(b"authentication key").expect("expand failed");
assert_ne!(encryption_key, authentication_key);

The concrete expanders are type aliases over Hkdf and can also be used through HkdfVariant in generic code.

Modules§

protectedprotected
Protected memory type aliases for HKDF

Structs§

Hkdf
HKDF expander for a specific HkdfVariant.
HkdfSha256Variant
HKDF-SHA-256 algorithm marker.
HkdfSha512Variant
HKDF-SHA-512 algorithm marker.

Traits§

HkdfVariant
HKDF algorithm variant used by Hkdf.

Type Aliases§

HkdfSha256
Stack-allocated HKDF-SHA-256 expander.
HkdfSha512
Stack-allocated HKDF-SHA-512 expander.
HkdfSha256Expander
HKDF-SHA-256 expander.
HkdfSha256Prk
Stack-allocated pseudorandom key for HKDF-SHA-256.
HkdfSha512Expander
HKDF-SHA-512 expander.
HkdfSha512Prk
Stack-allocated pseudorandom key for HKDF-SHA-512.