Class Hkdf
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Kdf
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Implements HKDF (HMAC-based Extract-and-Expand Key Derivation Function) as defined in RFC 5869.
public static class Hkdf
- Inheritance
-
Hkdf
- Inherited Members
Examples
// Derive a 32-byte key using HMAC-SHA-256
byte[] ikm = ...; // input keying material
byte[] salt = ...; // optional salt
byte[] info = ...; // context info
byte[] derivedKey = Hkdf.DeriveKey(
key => new HmacSha256(key),
ikm, outputLength: 32, salt, info);
Remarks
HKDF is a simple and efficient key derivation function based on HMAC. It consists of two stages: Extract concentrates the entropy of the input keying material into a pseudorandom key (PRK), and Expand derives one or more output keys from the PRK.
This implementation uses the CryptoHives IMac interface, making it pluggable with any HMAC variant (SHA-256, SHA-384, SHA-512, SHA3-256, etc.) and fully managed across all platforms.
For situations where the input keying material is already uniformly random, the Extract step can be skipped and the IKM used directly as the PRK in Expand. See RFC 5869 for guidance.
Methods
DeriveKey(HmacFactory, byte[], int, byte[]?, byte[]?)
Performs the combined HKDF Extract-then-Expand key derivation.
public static byte[] DeriveKey(HmacFactory hmacFactory, byte[] ikm, int outputLength, byte[]? salt = null, byte[]? info = null)
Parameters
hmacFactoryHmacFactoryA factory that creates an HMAC instance keyed with the given key.
ikmbyte[]The input keying material.
outputLengthintThe desired length of the output keying material.
saltbyte[]The optional salt value.
infobyte[]The optional context and application-specific information.
Returns
- byte[]
The output keying material.
Exceptions
- ArgumentNullException
hmacFactoryorikmis null.- ArgumentOutOfRangeException
outputLengthis less than 1.
DeriveKey(HmacFactory, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Performs the combined HKDF Extract-then-Expand key derivation.
public static void DeriveKey(HmacFactory hmacFactory, ReadOnlySpan<byte> ikm, Span<byte> output, ReadOnlySpan<byte> salt, ReadOnlySpan<byte> info)
Parameters
hmacFactoryHmacFactoryA factory that creates an HMAC instance keyed with the given key.
ikmReadOnlySpan<byte>The input keying material.
outputSpan<byte>The destination buffer to receive the output keying material.
saltReadOnlySpan<byte>The optional salt value. If empty, defaults to a zero-filled byte array.
infoReadOnlySpan<byte>The optional context and application-specific information.
Exceptions
- ArgumentNullException
hmacFactoryis null.- ArgumentException
outputis empty, or exceeds 255 × MacSize bytes.
Expand(HmacFactory, byte[], int, byte[]?)
Performs the HKDF-Expand function. See RFC 5869 §2.3.
public static byte[] Expand(HmacFactory hmacFactory, byte[] prk, int outputLength, byte[]? info = null)
Parameters
hmacFactoryHmacFactoryA factory that creates an HMAC instance keyed with the given key.
prkbyte[]The pseudorandom key.
outputLengthintThe desired length of the output keying material.
infobyte[]The optional context and application-specific information.
Returns
- byte[]
The output keying material.
Exceptions
- ArgumentNullException
hmacFactoryorprkis null.- ArgumentOutOfRangeException
outputLengthis less than 1.
Expand(HmacFactory, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>)
Performs the HKDF-Expand function. See RFC 5869 §2.3.
public static void Expand(HmacFactory hmacFactory, ReadOnlySpan<byte> prk, Span<byte> output, ReadOnlySpan<byte> info)
Parameters
hmacFactoryHmacFactoryA factory that creates an HMAC instance keyed with the given key.
prkReadOnlySpan<byte>The pseudorandom key (at least MacSize bytes, typically the output of Extract).
outputSpan<byte>The destination buffer to receive the output keying material.
infoReadOnlySpan<byte>The optional context and application-specific information.
Exceptions
- ArgumentNullException
hmacFactoryis null.- ArgumentException
outputis empty, or exceeds 255 × MacSize bytes.
Extract(HmacFactory, byte[], byte[]?)
Performs the HKDF-Extract function. See RFC 5869 §2.2.
public static byte[] Extract(HmacFactory hmacFactory, byte[] ikm, byte[]? salt = null)
Parameters
hmacFactoryHmacFactoryA factory that creates an HMAC instance keyed with the given key.
ikmbyte[]The input keying material.
saltbyte[]The optional salt value. If null, defaults to a zero-filled byte array.
Returns
- byte[]
The pseudorandom key (PRK).
Exceptions
- ArgumentNullException
hmacFactoryorikmis null.
Extract(HmacFactory, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Performs the HKDF-Extract function. See RFC 5869 §2.2.
public static int Extract(HmacFactory hmacFactory, ReadOnlySpan<byte> ikm, ReadOnlySpan<byte> salt, Span<byte> prk)
Parameters
hmacFactoryHmacFactoryA factory that creates an HMAC instance keyed with the given key.
ikmReadOnlySpan<byte>The input keying material.
saltReadOnlySpan<byte>The optional salt value (a non-secret random value). If empty, defaults to a zero-filled byte array of length MacSize.
prkSpan<byte>The destination buffer to receive the pseudorandom key. Must be at least MacSize bytes.
Returns
- int
The number of bytes written to
prk.
Exceptions
- ArgumentNullException
hmacFactoryis null.- ArgumentException
prkis too small.