Lunatik

crypto.hkdfClass

HMAC-based Extract-and-Expand Key Derivation Function (HKDF) based on RFC 5869.

This module provides functions to perform HKDF operations, utilizing the underlying crypto C module for HMAC calculations. An instance is created where its shash is, in a process runtime or in a softirq script’s body, whose hooks then derive keys with it.

Functions

crypto.hkdf.new (alg) Creates a new HKDF instance for a given hash algorithm.

Methods

crypto.hkdf:close () Closes the HKDF instance and releases the underlying HMAC transform.
crypto.hkdf:expand (prk[, info], length) Performs the HKDF Expand step.
crypto.hkdf:extract ([salt], ikm) Performs the HKDF Extract step.
crypto.hkdf:hkdf ([salt], ikm[, info], length) Performs the full HKDF (Extract and Expand) operation.

Functions in detail

crypto.hkdf.new (alg)
Creates a new HKDF instance for a given hash algorithm.

Parameters

  • alg string base hash algorithm name (e.g., “sha256”, “sha512”). The “hmac(” prefix will be added automatically.

Returns

    hkdf An HKDF instance table with methods for key derivation.

Raises

what crypto.shash raises, “EOPNOTSUPP” on a kernel without CONFIG_CRYPTO_HASH2 among it.

Usage

local hkdf_sha256 = require("crypto.hkdf").new("sha256")

Methods in detail

crypto.hkdf:close ()
Closes the HKDF instance and releases the underlying HMAC transform. It also runs when a to-be-closed variable holding the instance goes out of scope. Without it, the transform is freed when it is collected.
crypto.hkdf:expand (prk[, info], length)
Performs the HKDF Expand step.

Parameters

  • prk string Pseudorandom Key.
  • info string Optional context and application-specific information. Defaults to an empty string if nil. (optional)
  • length number desired length in bytes for the Output Keying Material (OKM), at most 255 times the digest size.

Returns

    string Output Keying Material of the specified length.
crypto.hkdf:extract ([salt], ikm)
Performs the HKDF Extract step.

Parameters

  • salt string Optional salt value. If nil or not provided, a salt of hash_len zeros is used. (optional)
  • ikm string Input Keying Material.

Returns

    string Pseudorandom Key (PRK).
crypto.hkdf:hkdf ([salt], ikm[, info], length)
Performs the full HKDF (Extract and Expand) operation.

Parameters

  • salt string Optional salt value. (optional)
  • ikm string Input Keying Material.
  • info string Optional context and application-specific information. (optional)
  • length number desired length in bytes for the Output Keying Material.

Returns

    string Output Keying Material.