Class Kbkdf
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Kdf
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Implements the Key-Based Key Derivation Function (KBKDF) in Counter Mode as defined in NIST SP 800-108r1 §4.1.
public static class Kbkdf
- Inheritance
-
Kbkdf
- Inherited Members
Examples
// Derive a 32-byte key using HMAC-SHA-256
byte[] masterKey = ...; // key derivation key
byte[] label = Encoding.UTF8.GetBytes("encryption");
byte[] context = Encoding.UTF8.GetBytes("session-001");
byte[] derivedKey = Kbkdf.DeriveKey(
key => new HmacSha256(key),
masterKey, outputLength: 32, label, context);
Remarks
KBKDF Counter Mode derives keying material from a key derivation key (KI) using a pseudorandom function (PRF) iterated with an incrementing counter. The PRF can be any keyed MAC such as HMAC-SHA-256, HMAC-SHA-512, or AES-CMAC.
The PRF input for each iteration is composed as:
[i]₄ ‖ Label ‖ 0x00 ‖ Context ‖ [L]₄
where [i]₄ is a 32-bit big-endian counter (1-based) and [L]₄ is
the output length in bits as a 32-bit big-endian integer. This format is compatible
with the .NET SP800108DeriveBytes class.
This KDF is widely used in IPsec (RFC 7296), Microsoft CNG, Windows DPAPI, Kerberos, and many enterprise security protocols.
Methods
DeriveKey(HmacFactory, byte[], int, byte[]?, byte[]?)
Derives keying material using Counter Mode with structured input.
public static byte[] DeriveKey(HmacFactory macFactory, byte[] key, int outputLength, byte[]? label = null, byte[]? context = null)
Parameters
macFactoryHmacFactoryA factory that creates an IMac instance keyed with the given key.
keybyte[]The key derivation key (KI).
outputLengthintThe desired length of the derived keying material in bytes.
labelbyte[]A label that identifies the purpose of the derived key. If null, treated as empty.
contextbyte[]Context information that binds the derived key to a specific use. If null, treated as empty.
Returns
- byte[]
The derived keying material.
Exceptions
- ArgumentNullException
macFactoryorkeyis null.- ArgumentOutOfRangeException
outputLengthis less than 1.
DeriveKey(HmacFactory, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Derives keying material using Counter Mode with structured input.
public static void DeriveKey(HmacFactory macFactory, ReadOnlySpan<byte> key, Span<byte> output, ReadOnlySpan<byte> label, ReadOnlySpan<byte> context)
Parameters
macFactoryHmacFactoryA factory that creates an IMac instance keyed with the given key. Supports HMAC variants (e.g.,
HmacSha256) and CMAC (e.g.,AesCmac).keyReadOnlySpan<byte>The key derivation key (KI).
outputSpan<byte>The destination buffer to receive the derived keying material.
labelReadOnlySpan<byte>A label that identifies the purpose of the derived key.
contextReadOnlySpan<byte>Context information that binds the derived key to a specific use.
Exceptions
- ArgumentNullException
macFactoryis null.- ArgumentException
outputis empty or exceeds the maximum derivable length.