Table of Contents

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

macFactory HmacFactory

A factory that creates an IMac instance keyed with the given key.

key byte[]

The key derivation key (KI).

outputLength int

The desired length of the derived keying material in bytes.

label byte[]

A label that identifies the purpose of the derived key. If null, treated as empty.

context byte[]

Context information that binds the derived key to a specific use. If null, treated as empty.

Returns

byte[]

The derived keying material.

Exceptions

ArgumentNullException

macFactory or key is null.

ArgumentOutOfRangeException

outputLength is 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

macFactory HmacFactory

A factory that creates an IMac instance keyed with the given key. Supports HMAC variants (e.g., HmacSha256) and CMAC (e.g., AesCmac).

key ReadOnlySpan<byte>

The key derivation key (KI).

output Span<byte>

The destination buffer to receive the derived keying material.

label ReadOnlySpan<byte>

A label that identifies the purpose of the derived key.

context ReadOnlySpan<byte>

Context information that binds the derived key to a specific use.

Exceptions

ArgumentNullException

macFactory is null.

ArgumentException

output is empty or exceeds the maximum derivable length.