AgeEx (age_ex v0.1.0)

Copy Markdown View Source

The age v1 file format with X25519 keys, in pure Elixir.

It uses only OTP :crypto. Files work with the age and rage tools in both directions, binary or ASCII-armored.

Two key types work: X25519 (age1...) and the hybrid post-quantum ML-KEM-768 + X25519 type (age1pq1...). The post-quantum type needs OpenSSL 3.5 or later in OTP. Without it, the operations that need ML-KEM return {:error, :unsupported}.

encrypt/3 also accepts YubiKey recipients (age1yubikey1...) from age-plugin-yubikey. Only the YubiKey can decrypt them, with the age CLI and the plugin.

Not supported: passphrases (scrypt), SSH keys, other plugins. All data stays in memory.

Example

%{identity: identity, recipient: recipient} = AgeEx.generate_identity()
{:ok, ciphertext} = AgeEx.encrypt("secret", recipient)
{:ok, "secret"} = AgeEx.decrypt(ciphertext, identity)

Summary

Types

Why an operation failed. The decrypt reasons match the classes of the C2SP age test vectors.

A private key: AGE-SECRET-KEY-1... or AGE-SECRET-KEY-PQ-1....

A public key: age1..., age1pq1... or age1yubikey1....

Functions

Decrypts an age file with one identity or a list of them.

Encrypts plaintext to one recipient or a list of them.

Generates a new key pair.

Reads the identities from the text of a key file, as age-keygen writes it.

Returns the recipient (public key) of an identity.

Types

error()

@type error() ::
  :invalid_identity
  | :invalid_recipient
  | :no_recipients
  | :incompatible_recipients
  | :unsupported
  | :armor_failure
  | :header_failure
  | :no_match
  | :hmac_failure
  | :payload_failure

Why an operation failed. The decrypt reasons match the classes of the C2SP age test vectors.

identity()

@type identity() :: String.t()

A private key: AGE-SECRET-KEY-1... or AGE-SECRET-KEY-PQ-1....

recipient()

@type recipient() :: String.t()

A public key: age1..., age1pq1... or age1yubikey1....

Functions

decrypt(ciphertext, identities)

@spec decrypt(binary(), identity() | [identity()]) ::
  {:ok, binary()} | {:error, error()}

Decrypts an age file with one identity or a list of them.

Accepts binary and ASCII-armored files. Nothing is returned unless the whole file decrypts and authenticates.

encrypt(plaintext, recipients, opts \\ [])

@spec encrypt(iodata(), recipient() | [recipient()], keyword()) ::
  {:ok, binary()} | {:error, error()}

Encrypts plaintext to one recipient or a list of them.

Post-quantum recipients cannot be mixed with X25519 or YubiKey recipients, because the file would then have no protection against quantum computers. The age CLI refuses this too.

Options

  • :armor - when true, returns ASCII-armored text. Defaults to false.

generate_identity(opts \\ [])

@spec generate_identity(keyword()) :: %{identity: identity(), recipient: recipient()}

Generates a new key pair.

Options

  • :post_quantum - when true, makes a hybrid ML-KEM-768 + X25519 key, as age-keygen -pq does. Defaults to false.

parse_identities(text)

@spec parse_identities(String.t()) ::
  {:ok, [identity()]} | {:error, :invalid_identity}

Reads the identities from the text of a key file, as age-keygen writes it.

Empty lines and lines that start with # are skipped. Every other line must be an identity: X25519 (age-keygen) or post-quantum (age-keygen -pq).

recipient(identity)

@spec recipient(identity()) :: {:ok, recipient()} | {:error, :invalid_identity}

Returns the recipient (public key) of an identity.