Class ConcatKdf
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Kdf
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Implements the Concatenation Key Derivation Function (Concat KDF) as defined in NIST SP 800-56A §5.8.1 and NIST SP 800-56C rev2 Option 1.
public static class ConcatKdf
- Inheritance
-
ConcatKdf
- Inherited Members
Examples
// Hash-based: derive 32 bytes from ECDH shared secret
using var sha256 = SHA256.Create();
byte[] derivedKey = ConcatKdf.DeriveKey(sha256, sharedSecret, 32, otherInfo);
// HMAC-based: derive 32 bytes with salt
byte[] derivedKey = ConcatKdf.DeriveKey(
key => new HmacSha256(key),
sharedSecret, 32, otherInfo, salt);
Remarks
Concat KDF is a single-step key derivation function used to derive keying material
from a shared secret (e.g., from ECDH key agreement). Each iteration computes:
Hash(counter ‖ Z ‖ OtherInfo) with a 32-bit big-endian counter starting at 1.
Two variants are supported:
- Hash-based (SP 800-56A §5.8.1): Uses a plain hash function as the auxiliary function. The DeriveKey(HashAlgorithm, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>) overloads accept any HashAlgorithm.
- HMAC-based (SP 800-56C rev2 Option 1): Uses an HMAC keyed with an optional salt as the auxiliary function. The DeriveKey(HmacFactory, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>) overloads accept an HmacFactory.
This KDF is used in ECDH key agreement (NIST SP 800-56A), JOSE/JWE (RFC 7518), and various ECC-based protocols.
Methods
DeriveKey(HmacFactory, byte[], int, byte[]?, byte[]?)
Derives keying material using the HMAC-based Concat KDF.
public static byte[] DeriveKey(HmacFactory hmacFactory, byte[] sharedSecret, int outputLength, byte[]? otherInfo = null, byte[]? salt = null)
Parameters
hmacFactoryHmacFactoryA factory that creates an IMac instance keyed with the given key.
sharedSecretbyte[]The shared secret (Z).
outputLengthintThe desired length of the derived keying material in bytes.
otherInfobyte[]Supplementary public information. If null, treated as empty.
saltbyte[]The optional salt used as the HMAC key. If null, defaults to a zero-filled byte array.
Returns
- byte[]
The derived keying material.
Exceptions
- ArgumentNullException
hmacFactoryorsharedSecretis null.- ArgumentOutOfRangeException
outputLengthis less than 1.
DeriveKey(HmacFactory, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Derives keying material using the HMAC-based Concat KDF.
public static void DeriveKey(HmacFactory hmacFactory, ReadOnlySpan<byte> sharedSecret, Span<byte> output, ReadOnlySpan<byte> otherInfo, ReadOnlySpan<byte> salt)
Parameters
hmacFactoryHmacFactoryA factory that creates an IMac instance keyed with the given key.
sharedSecretReadOnlySpan<byte>The shared secret (Z), typically from a key agreement such as ECDH.
outputSpan<byte>The destination buffer to receive the derived keying material.
otherInfoReadOnlySpan<byte>Supplementary public information.
saltReadOnlySpan<byte>The optional salt used as the HMAC key. If empty, defaults to a zero-filled byte array of length MacSize.
Exceptions
- ArgumentNullException
hmacFactoryis null.- ArgumentException
outputis empty.
DeriveKey(HashAlgorithm, byte[], int, byte[]?)
Derives keying material using the hash-based Concat KDF.
public static byte[] DeriveKey(HashAlgorithm hash, byte[] sharedSecret, int outputLength, byte[]? otherInfo = null)
Parameters
hashHashAlgorithmThe hash algorithm to use.
sharedSecretbyte[]The shared secret (Z).
outputLengthintThe desired length of the derived keying material in bytes.
otherInfobyte[]Supplementary public information. If null, treated as empty.
Returns
- byte[]
The derived keying material.
Exceptions
- ArgumentNullException
hashorsharedSecretis null.- ArgumentOutOfRangeException
outputLengthis less than 1.
DeriveKey(HashAlgorithm, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>)
Derives keying material using the hash-based Concat KDF.
public static void DeriveKey(HashAlgorithm hash, ReadOnlySpan<byte> sharedSecret, Span<byte> output, ReadOnlySpan<byte> otherInfo)
Parameters
hashHashAlgorithmThe hash algorithm to use. The instance is reused across iterations via ComputeHash(byte[]).
sharedSecretReadOnlySpan<byte>The shared secret (Z), typically from a key agreement such as ECDH.
outputSpan<byte>The destination buffer to receive the derived keying material.
otherInfoReadOnlySpan<byte>Supplementary public information (e.g., algorithm ID, party info, key length).
Exceptions
- ArgumentNullException
hashis null.- ArgumentException
outputis empty.