Lunatik

cryptoModule

Lua interface to the Linux Crypto API.

Every class but kpp needs the kernel’s API of its kind, the option its constructor names, and the constructor raises “EOPNOTSUPP” on a kernel without it. On 7.0 and earlier the crypto manager, which a template such as CONFIG_CRYPTO_CBC selects, selects each of these options, and on 7.1 and later only with CONFIG_CRYPTO_SELFTESTS, so a kernel there has one only with an algorithm of its kind. shash and skcipher objects serve softirq runtimes too: a script body, which runs in process context, creates them, or a process runtime hands them over, and the hooks set their keys and run them per packet, which sleeps nowhere. A key setkey sets is the object’s, read by the call that follows it, so each runtime keys an object of its own: two runtimes using one at once can run a call under the other’s key, with no error. Neither serves a hardirq runtime, whose callbacks run with IRQs disabled. Every other object is created in a process runtime alone: in another, its constructor raises “‘crypto.aead’: process-context class in interrupt-context runtime”, naming its class. Errors are raised as errno names: “ENOENT” for an unknown algorithm, “EINVAL” for a wrong IV, key or state length or a block cipher input that is not a multiple of blocksize(), and on 6.12 and later for a name whose template refuses the type the constructor asks for, such as shash("cbc(aes)"), which 6.11 and earlier answer with “ENOENT”, “ENOMEM” when the kernel cannot allocate the transform, “ENOKEY” for a signature verifier used before its key is set, and “not enough memory” when the binding cannot allocate its own state, its output or the copy of its input. An AEAD message that does not authenticate is no error: decrypt answers nil and “EBADMSG” for it, a signature that does not verify is none either: verify answers false and the errno’s name, and nor is a peer’s key the curve refuses: sharedsecret answers nil and “EINVAL” for it.

Functions

aead (algname) Creates an AEAD cipher transform.
comp (algname) Creates a compression transform.
kpp (algname[, key]) Creates an ECDH key agreement object, which holds one private key for its life.
rng ([algname="stdrng"]) Creates a random number generator and seeds it as reset does without a seed.
shash (algname) Creates a synchronous hash transform.
sig (algname) Creates a signature verifier.
skcipher (algname) Creates a symmetric-key cipher transform.

Functions in detail

aead (algname)
Creates an AEAD cipher transform. Only a synchronous implementation is chosen: an asynchronous one, such as a hardware engine’s or a cryptd(...) instance, is passed over. Needs the kernel’s AEAD API, CONFIG_CRYPTO_AEAD2, which an AEAD mode such as CONFIG_CRYPTO_GCM selects.

Parameters

  • algname string algorithm name (e.g., “gcm(aes)”, “ccm(aes)”)

Returns

    crypto_aead

Raises

“EOPNOTSUPP” on a kernel without CONFIG_CRYPTO_AEAD2, “ENOENT” for an unknown algorithm or one no synchronous implementation serves (“EEXIST” on 6.12 and later for a template whose instance is asynchronous, such as cryptd(...)), “ENOMEM” or “not enough memory” on allocation failure

Usage

local aead = require("crypto").aead
local cipher = aead("gcm(aes)")
comp (algname)
Creates a compression transform. Needs the kernel’s compression API, CONFIG_CRYPTO_ACOMP2, which a compression algorithm such as CONFIG_CRYPTO_LZ4 selects.

Parameters

  • algname string algorithm name (e.g., “lz4”, “deflate”)

Returns

    crypto_comp

Raises

“EOPNOTSUPP” on a kernel without CONFIG_CRYPTO_ACOMP2, “ENOENT” for an unknown algorithm, “ENOMEM” or “not enough memory” on allocation failure

Usage

local comp = require("crypto").comp
local c = comp("lz4")
kpp (algname[, key])
Creates an ECDH key agreement object, which holds one private key for its life. Without a key the kernel generates one, which only its public half, publickey(), ever leaves; with one, it is the private scalar, big-endian and of the curve’s size, 32 bytes for P-256, the privateKey octets of SEC1 that tools export. The name is one of the kernel’s ecdh-nist-p192, ecdh-nist-p256 and ecdh-nist-p384; no other key agreement is served, X25519 among them, which the kernel offers as a library and not through this API from 6.18. Absent where the kernel is built without CONFIG_CRYPTO_ECDH, which every ECDH implementation selects.

Parameters

  • algname string algorithm name (e.g., “ecdh-nist-p256”)
  • key string the private key; without it the kernel generates one (optional)

Returns

    crypto_kpp

Raises

“ENOENT” for an unknown algorithm, “EINVAL” for the name of another key agreement, such as ffdhe2048(dh), and for a key of another length than the curve’s or out of its range, “EIO” for a key the engine refuses with a status that is not an errno, “ENOMEM” or “not enough memory” on allocation failure

Usage

local kpp = require("crypto").kpp
local machine = kpp("ecdh-nist-p256")
local public = machine:publickey()
local shared, err = machine:sharedsecret(peer)
rng ([algname="stdrng"])
Creates a random number generator and seeds it as reset does without a seed. Needs the kernel’s rng API, CONFIG_CRYPTO_RNG2, which the crypto manager selects on 7.0 and earlier, and on 7.1 and later only with CONFIG_CRYPTO_SELFTESTS, so a kernel there has it through a user such as CONFIG_CRYPTO_DRBG, CONFIG_CRYPTO_JITTERENTROPY or CONFIG_CRYPTO_USER_API_RNG. The default name, stdrng, is the one a DRBG registers, and an unknown algorithm where nothing does.

Parameters

  • algname string algorithm name (default "stdrng")

Returns

    crypto_rng

Raises

“EOPNOTSUPP” on a kernel without CONFIG_CRYPTO_RNG2, “ENOENT” for an unknown algorithm, “ENOMEM” or “not enough memory” on allocation failure, or the errno of the seeding it runs at creation

Usage

local rng = require("crypto").rng
local r = rng()
shash (algname)
Creates a synchronous hash transform. Needs the kernel’s hash API, CONFIG_CRYPTO_HASH2, which a hash algorithm such as CONFIG_CRYPTO_SHA256 selects.

Parameters

  • algname string algorithm name (e.g., “sha256”, “hmac(sha256)”)

Returns

    crypto_shash

Raises

“EOPNOTSUPP” on a kernel without CONFIG_CRYPTO_HASH2, “ENOENT” for an unknown algorithm, “ENOMEM” or “not enough memory” on allocation failure, “not allowed in a hardirq runtime”, “not allowed once the runtime is armed” from a softirq runtime past its body, since the allocation may sleep, and “not allowed under RTNL” from a callback that runs under it

Usage

local shash = require("crypto").shash
local h = shash("sha256")
sig (algname)
Creates a signature verifier. The name is the kernel’s, which differs by release for a DER or a PKCS#1 v1.5 signature: ECDSA over an X9.62 DER signature is x962(ecdsa-nist-p256) on 6.13 and later and ecdsa-nist-p256 on 6.12 and earlier, the curve one of p192, p256, p384, and p521 from 6.10; RSA PKCS#1 v1.5 is pkcs1(rsa,sha256) on 6.13 and later and pkcs1pad(rsa,sha256) on 6.12 and earlier. On 6.13 and later p1363(ecdsa-nist-p256) takes r and s concatenated big-endian, each of the key’s size, and the bare ecdsa-nist-p256 is the kernel’s raw form, which takes r and s in the kernel’s own digits and answers false and “EINVAL” to a DER or a P1363 signature. A script that runs on both sides tries the newer name first and takes “ENOENT” as the cue for the older one. Needs the kernel’s signature API, CONFIG_CRYPTO_SIG2, which a signature algorithm such as CONFIG_CRYPTO_ECDSA selects on 6.13 and later, and on 6.12 and earlier the crypto manager, since a signature algorithm selects the akcipher API there.

Parameters

  • algname string algorithm name (e.g., “x962(ecdsa-nist-p256)”, “pkcs1(rsa,sha256)”)

Returns

    crypto_sig

Raises

“EOPNOTSUPP” on a kernel without CONFIG_CRYPTO_SIG2, “ENOENT” for an unknown algorithm, or one the kernel serves only for encryption, such as rsa on 6.13 and later, “EINVAL” for a name its template refuses, such as pkcs1pad(rsa,sha256) on 6.14 and later, whose template serves only encryption, where 6.11 and earlier and 6.13 raise “ENOENT”, “ENOMEM” or “not enough memory” on allocation failure

Usage

local sig = require("crypto").sig
local function verifier(name, older)
  local ok, v = pcall(sig, name)
  if ok then
    return v
  end
  assert(v == "ENOENT", v)
  return sig(older)
end
local v = verifier("x962(ecdsa-nist-p256)", "ecdsa-nist-p256")
v:setpubkey(point)
local ok, err = v:verify(signature, digest)
skcipher (algname)
Creates a symmetric-key cipher transform. Only a synchronous implementation is chosen: an asynchronous one, such as a hardware engine’s or a cryptd(...) instance, is passed over. Needs the kernel’s skcipher API, CONFIG_CRYPTO_SKCIPHER2, which a cipher mode such as CONFIG_CRYPTO_CBC selects.

Parameters

  • algname string algorithm name (e.g., “cbc(aes)”, “ctr(aes)”)

Returns

    crypto_skcipher

Raises

“EOPNOTSUPP” on a kernel without CONFIG_CRYPTO_SKCIPHER2, “ENOENT” for an unknown algorithm or one no synchronous implementation serves (“EEXIST” on 6.12 and later for a template whose instance is asynchronous, such as cryptd(...)), “ENOMEM” or “not enough memory” on allocation failure, “not allowed in a hardirq runtime”, “not allowed once the runtime is armed” from a softirq runtime past its body, since the allocation may sleep, and “not allowed under RTNL” from a callback that runs under it

Usage

local skcipher = require("crypto").skcipher
local cipher = skcipher("cbc(aes)")