Skip to Content
oxcrypto

KBKDF

Key-based key derivation from NIST SP 800-108 in counter, feedback and double-pipeline modes with HMAC or CMAC-AES.

from oxcrypto.kdf.kbkdf import KBKDF

algorithm = KBKDF.HMAC_SHA256
key = algorithm.counter(key_in, label=b"label", context=b"context", length=32)
key = algorithm.feedback(key_in, label=b"label", context=b"context", length=32, iv=iv)
key = algorithm.double_pipeline(key_in, label=b"label", context=b"context", length=32)

Each mode method derives length bytes from the key key_in with the PRF of the selector. The fixed input data is label || 0x00 || context || [L]_32, where L is the output length in bits. Set use_separator=False to drop the zero byte and use_l=False to drop the length field, for example to pass a fully encoded fixed input as label.

  • counter: K(i) = PRF(key_in, [i]_r || fixed input).
  • feedback: K(i) = PRF(key_in, K(i-1) || [i]_r || fixed input). K(0) is iv, which must be exactly the PRF output size, or empty when iv is omitted.
  • double_pipeline: A(i) = PRF(key_in, A(i-1)) with A(0) the fixed input, then K(i) = PRF(key_in, A(i) || [i]_r || fixed input).

counter_bits sets the width r of the counter [i]_r to 8, 16, 24 or 32 bits (default 32). Feedback and double-pipeline mode can omit the counter with use_counter=False. The counter position is fixed by the upstream crate: before the fixed input in counter mode and after the iteration data in the other modes.

Selectors: HMAC_SHA1, HMAC_SHA224, HMAC_SHA256, HMAC_SHA384, HMAC_SHA512, HMAC_SHA512_224, HMAC_SHA512_256, HMAC_SHA3_224, HMAC_SHA3_256, HMAC_SHA3_384, HMAC_SHA3_512, CMAC_AES128, CMAC_AES192 and CMAC_AES256. HMAC accepts a key of any length; CMAC requires an AES key of 16, 24 or 32 bytes.

Supported output lengths

length must be 16, 24, 32, 48 or 64 bytes; any other value raises ValueError. The upstream crate fixes the output size at compile time, so oxcrypto builds one instance per supported size.

In double-pipeline mode, kbkdf 0.1.0-rc.1 leaves [L] out of A(0). oxcrypto passes the encoded fixed input as the label so the output matches SP 800-108.

StandardNIST SP 800-108 Rev. 1 (final)
Cratekbkdf 0.1.0-rc.1 (pinned) with hmac 0.13.0, cmac 0.8.0 and aes 0.9.2
MaturityPre-release, no published audit
MSRVRust 1.85, builds with the project MSRV 1.88
ExperimentalNo. The algorithm is standardized; the crate API may change before 0.1.0.
Zeroizationkbkdf keeps A(i) and K(i-1) unwiped, and HMAC keeps its padded key block and inner hash. The cmac and aes crates are written to wipe their states on drop; oxcrypto has not verified this independently.

On this page