crypto

index

Overview

Cryptographic primitives and utilities built on LITLS. Provides encoding/decoding (hex, base64, base64url), hashing (SHA-1, SHA-256, HMAC), ECC P-256 and Ed25519 key generation, signing, and verification, as well as X.509 certificate parsing, CSR generation, and self-signed Ed25519 certificates.

Functions

NameSignature
bin_to_hexbin_to_hex(bin) -> hex
hex_to_binhex_to_bin(hex) -> bin, err
sha1sha1(data) -> hash
sha1_streamsha1_stream() -> hasher
sha256sha256(data) -> hash
hmachmac(secret, msg) -> mac
b64_encodeb64_encode(str) -> encoded
b64_decodeb64_decode(str) -> decoded
b64url_encodeb64url_encode(str) -> encoded
b64url_decodeb64url_decode(str) -> decoded, err
b64url_encode_jsonb64url_encode_json(tbl) -> encoded, err
ecc_generate_keyecc_generate_key() -> key_obj
ecc_save_keyecc_save_key(key_obj, key_file) -> ok, err
ecc_load_keyecc_load_key(key_file) -> key_obj, err
ecc_signecc_sign(key, pub_key, msg) -> sig, err
ecc_verifyecc_verify(pub_key, msg, sig) -> ok, err
ed25519_generate_keyed25519_generate_key() -> private_key, public_key
ed25519_signed25519_sign(key, msg) -> sig, err
ed25519_public_keyed25519_public_key(key) -> pub_key, err
ed25519_verifyed25519_verify(pub_key, msg, sig) -> ok, err
generate_csrgenerate_csr(key, pub_key, domain, alt_names) -> csr, err
der_to_pem_ecc_keyder_to_pem_ecc_key(key_obj) -> pem, err
parse_x509_certparse_x509_cert(cert) -> cert_info, err
self_signed_certself_signed_cert(seed, opts) -> cert, err
ed25519_key_to_pemed25519_key_to_pem(seed) -> pem, err
ssh_parse_pubkeyssh_parse_pubkey(content) -> pubkey, err
ssh_parse_privkeyssh_parse_privkey(pem) -> seed, pubkey
ssh_load_pubkeyssh_load_pubkey(path) -> pubkey, err
ssh_load_keyssh_load_key(path) -> seed, pubkey
ssh_pubkey_fingerprintssh_pubkey_fingerprint(pubkey) -> fingerprint, err

bin_to_hex(bin) -> hex

Convert a binary string to its hexadecimal representation

hex_to_bin(hex) -> bin, err

Convert a hexadecimal string to binary

Accepts uppercase or lowercase ASCII hex digits of even length, including the empty string. Returns nil and a reason if hex is not a string, has odd length, or contains non-hex characters.

sha1(data) -> hash

Compute SHA-1 hash of data

sha1_stream() -> hasher

Start a SHA-1 hash fed in pieces

hasher:update(data) feeds the next piece and returns the hasher. hasher:finish() returns the 20-byte hash of every byte fed, the same sha1 gives for them joined. A call after finish raises.

sha256(data) -> hash

Compute SHA-256 hash of data

hmac(secret, msg) -> mac

Compute HMAC-SHA256

b64_encode(str) -> encoded

Encode a string to base64

b64_decode(str) -> decoded

Decode a base64-encoded string

b64url_encode(str) -> encoded

Encode a string to base64url (URL-safe, no padding)

b64url_decode(str) -> decoded, err

Decode a base64url-encoded string

Accepts base64url strings with or without = padding, as well as + and /. Returns nil and a reason if str is not a string or cannot be decoded as base64.

b64url_encode_json(tbl) -> encoded, err

JSON-encode a table and base64url-encode the result

Encodes tbl with cjson.safe and base64url-encodes the JSON bytes. Returns nil and a reason if JSON encoding fails.

ecc_generate_key() -> key_obj

Generate an ECC P-256 key pair

Returns a key object table with raw binary string fields: private (scalar d), public (uncompressed point), x (32-byte affine x), y (32-byte affine y).

ecc_save_key(key_obj, key_file) -> ok, err

Save an ECC key object to a file in JWK format

Serializes key_obj as an RFC 7517 P-256 JWK (kty, crv, x, y, d, and optional kid) and writes it to key_file.

Requires x, y, and private to be 32-byte raw binary strings. When public is present, it must equal "\4" .. x .. y. kid, when present, must be a string. Returns nil and a reason on invalid input or write failure.

ecc_load_key(key_file) -> key_obj, err

Load an ECC key object from a JWK file

Reads an RFC 7517 P-256 JWK file, or a legacy { x, y, d, pub } file without kty and crv, and returns a key object table with raw binary fields: private, public, x, y, and optionally kid.

Derives public as "\4" .. x .. y when pub is absent; when pub is present, it must match "\4" .. x .. y. Returns nil and a reason on unreadable files, invalid JSON, unsupported kty or crv, or missing or malformed fields.

ecc_sign(key, pub_key, msg) -> sig, err

Sign a message using an ECC P-256 private key

All arguments are raw binary strings. key is the private scalar, 32 bytes. pub_key is the uncompressed public point, and is not read: the signature needs the private key alone. Returns a 64-byte signature in r||s format (32 bytes each, big-endian).

Returns nil and a reason for a key that is not a 32-byte string, and for a message that is no string.

ecc_verify(pub_key, msg, sig) -> ok, err

Verify an ECC P-256 signature

Counterpart to ecc_sign. pub_key is the raw uncompressed public point, 65 bytes, and sig is the 64-byte r||s signature. Returns true where the signature is the key's over the message, and false where it is not.

Returns nil and a reason for an argument of the wrong type or length: a key that is not 65 bytes, a signature that is not 64, a message that is no string.

ed25519_generate_key() -> private_key, public_key

Generate an Ed25519 key pair

ed25519_sign(key, msg) -> sig, err

Sign a message using an Ed25519 private key

key is the 32-byte seed, as ed25519_generate_key returns it. Returns the 64-byte signature. The same seed and message always give the same bytes.

Returns nil and a reason for a seed that is not a 32-byte string, and for a message that is no string.

ed25519_public_key(key) -> pub_key, err

The public key an Ed25519 seed makes

key is the 32-byte seed. Returns the 32-byte public key, the one ed25519_generate_key returned beside that seed. A key file can so keep the seed alone.

Returns nil and a reason for a seed that is not a 32-byte string.

ed25519_verify(pub_key, msg, sig) -> ok, err

Verify an Ed25519 signature

pub_key is the raw 32-byte public key and sig the 64-byte signature. Returns true where the signature is the key's over the message, and false where it is not.

Two things are refused as false that a looser reading takes:

Returns nil and a reason for an argument of the wrong type or length: a key that is not 32 bytes, a signature that is not 64, a message that is no string.

generate_csr(key, pub_key, domain, alt_names) -> csr, err

Generate a PKCS#10 certificate signing request

Creates a DER-encoded CSR for domain using raw ECC P-256 key material. key is the 32-byte private scalar and pub_key is the 65-byte uncompressed public point. alt_names, when provided, is an array of up to 64 additional DNS names for the Subject Alternative Name extension.

Returns nil and a reason for a wrong key length, an invalid domain, or an invalid alt_names table. domain and every alt_names entry must be non-empty strings with no NUL bytes.

der_to_pem_ecc_key(key_obj) -> pem, err

Convert an ECC key object to SEC 1 PEM format

Accepts a key object table with private (32 bytes) and public (65 bytes) raw binary strings, as returned by ecc_generate_key. Returns a PEM-encoded EC PRIVATE KEY string.

Returns nil and a reason if key_obj is not a table or its private or public field has the wrong type or length.

parse_x509_cert(cert) -> cert_info, err

Parse an X.509 certificate

Accepts a certificate in PEM or DER format. PEM armor is stripped automatically. Returns a table with these fields:

key_hash is the value peer_key_hash returns on a LEV TLS client socket whose server shows this certificate. It depends on the key alone: a certificate renewed over the same key keeps it.

self_signed_cert(seed, opts) -> cert, err

Generate a self-signed X.509 certificate over an Ed25519 key

seed is the 32-byte private key ed25519_generate_key returns. The key signs its own certificate, so no CA is asked. opts holds:

Returns a table with der, the certificate, and pem, the same bytes under CERTIFICATE armor. parse_x509_cert reads either, and gives the key_hash a client pins.

A client that verifies hostnames matches sans when it is present and common_name only when it is not. Ed25519 signs without a nonce: one seed, one opts with a serial and both dates give the same bytes on every call.

ed25519_key_to_pem(seed) -> pem, err

Encode an Ed25519 private key as a PKCS#8 PEM key file

seed is the 32-byte private key ed25519_generate_key returns. The result is the PRIVATE KEY block a LEV TLS server reads as its key file, beside the pem of self_signed_cert as its cert file. The text holds the private key in the clear.

ssh_parse_pubkey(content) -> pubkey, err

Parse an ssh-ed25519 public key string

Parses an ssh-ed25519 public key string (the format found in .pub files or authorized_keys) and extracts the raw 32-byte Ed25519 public key. Returns nil and an error if the content is not a valid ssh-ed25519 key.

ssh_parse_privkey(pem) -> seed, pubkey

Parse an OpenSSH Ed25519 private key from PEM content

Parses an unencrypted OpenSSH Ed25519 private key (PEM format with -----BEGIN OPENSSH PRIVATE KEY----- armor). Returns the 32-byte seed and 32-byte public key, or nil and an error message.

ssh_load_pubkey(path) -> pubkey, err

Load an Ed25519 public key from an OpenSSH .pub file

ssh_load_key(path) -> seed, pubkey

Load an Ed25519 key from an OpenSSH private key file

ssh_pubkey_fingerprint(pubkey) -> fingerprint, err

Compute the hex-encoded SHA-256 fingerprint of an Ed25519 public key

Hashes the raw 32-byte Ed25519 public key with SHA-256 and returns lowercase hex. Returns nil and a reason if pubkey is not a 32-byte string.