mneme.crypto

index · mneme

Overview

Encryption key management for MNEME databases. Handles Data Encryption Key (DEK) generation, wrapping, and unwrapping using Ed25519 SSH keys converted to X25519 for ECDH key agreement. Provides per-keyspace sub-key derivation and key slot serialization for multi-key access.

Functions

NameSignature
pack_slotspack_slots(slots) -> data
unpack_slotsunpack_slots(data) -> slots, err
fingerprintfingerprint(ed25519_pk) -> fingerprint
wrap_dekwrap_dek(dek_raw, ed25519_pk) -> slot
unwrap_dekunwrap_dek(slots, ed25519_seed, ed25519_pk) -> dek, err
unwrap_dek_rawunwrap_dek_raw(slots, ed25519_seed, ed25519_pk) -> dek_raw, err
derive_subkeyderive_subkey(dek, keyspace_name) -> subkey

pack_slots(slots) -> data

Serialize key slots to a binary blob

unpack_slots(data) -> slots, err

Deserialize key slots from a binary blob

fingerprint(ed25519_pk) -> fingerprint

Compute SHA-256 fingerprint of an Ed25519 public key

wrap_dek(dek_raw, ed25519_pk) -> slot

Wrap a DEK for an Ed25519 public key

Seals the 32-byte raw DEK so that only the holder of the corresponding Ed25519 private key can recover it. Converts the public key to X25519, performs ECDH with an ephemeral keypair, derives a KEK via HKDF-SHA256, and encrypts the DEK with ChaCha20-Poly1305. Returns a key slot table with fields: fingerprint, ephemeral_pk, nonce, wrapped_dek, tag.

unwrap_dek(slots, ed25519_seed, ed25519_pk) -> dek, err

Unwrap a DEK from key slots using an Ed25519 seed

Finds the key slot matching the given public key's fingerprint, then reverses the ECDH + HKDF + ChaCha20-Poly1305 wrapping to recover the DEK. Returns a secure mlock'd userdata holding the DEK, or nil and an error if no matching slot is found or decryption fails.

unwrap_dek_raw(slots, ed25519_seed, ed25519_pk) -> dek_raw, err

Unwrap a DEK as raw bytes for key management operations

Same as unwrap_dek but returns the raw 32-byte DEK string instead of a secure userdata. Used only for key management (adding new key slots requires the plaintext DEK to re-wrap for a new recipient).

derive_subkey(dek, keyspace_name) -> subkey

Derive a per-keyspace encryption sub-key from the DEK