Packages

The age file encryption format in pure Elixir, with no NIFs and no dependencies. X25519 and post-quantum ML-KEM-768 keys. Also decrypts SOPS files.

Current section

Files

Jump to
age_ex README.md
Raw

README.md

# AgeEx

The [age v1](https://age-encryption.org/v1) file format in pure Elixir. It
uses only OTP `:crypto` (OTP 28.1 or later) and has no dependencies. Files
work with the `age` and `rage` tools in both directions, binary or
ASCII-armored.

Two key types work:

- X25519 keys (`age1...`), as `age-keygen` makes them.
- Hybrid post-quantum ML-KEM-768 + X25519 keys (`age1pq1...`), as
  `age-keygen -pq` (age 1.3 or later) makes them. They need OTP linked to
  OpenSSL 3.5 or later. Without it, the operations that need ML-KEM return
  `{:error, :unsupported}`.

`AgeEx.encrypt/3` also accepts YubiKey recipients (`age1yubikey1...`) from
[age-plugin-yubikey](https://github.com/str4d/age-plugin-yubikey). AgeEx
cannot decrypt these files. Only the YubiKey can, with the `age` CLI and the
plugin.

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

## Installation

```elixir
def deps do
  [{:age_ex, "~> 0.1.0"}]
end
```

## Usage

```elixir
%{identity: identity, recipient: recipient} = AgeEx.generate_identity()
# or AgeEx.generate_identity(post_quantum: true)

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

# A key file from age-keygen, with its comment lines
{:ok, identities} = AgeEx.parse_identities(File.read!("key.txt"))
{:ok, plaintext} = AgeEx.decrypt(File.read!("data.age"), identities)
```

## SOPS files

`AgeEx.Sops` decrypts SOPS files with age keys. Encryption is not supported.

- `AgeEx.Sops.decrypt_json/2` decrypts a JSON file to the terms that
  `JSON.decode/1` gives. SOPS keeps JSON numbers as floats, so a number with
  no fraction part comes back as an integer.
- `AgeEx.Sops.decrypt_dotenv/2` decrypts a dotenv file to a map of names to
  values. The values are strings. A file that SOPS converts from JSON or YAML
  (`--output-type dotenv`) can also have numbers and booleans. These come back
  as Elixir numbers and booleans, with the same number rule as
  `decrypt_json/2`. `sops decrypt` gives them as text in a Go format, for
  example `42.0` or `1E+21`.
- `AgeEx.Sops.decrypt_ini/2` decrypts an INI file to a map of section names to
  maps of keys to values. The `"DEFAULT"` section holds the keys before the
  first section header. The values follow the rules of `decrypt_dotenv/2`. The
  function refuses a duplicate section or key.
- `AgeEx.Sops.decrypt/2` decrypts a file that
  `sops encrypt --input-type binary` makes, and gives the original bytes.
- YAML is not supported

```elixir
{:ok, identities} =
  File.read!("key.txt")
  |> AgeEx.parse_identities()

{:ok, %{"db" => %{"password" => password}}} =
  File.read!("secrets.sops.json")
  |> AgeEx.Sops.decrypt_json(identities)
```

## Config provider

`AgeEx.Sops.ConfigProvider` loads a binary-format file as Elixir config at
release boot. The plaintext stays in memory:

```elixir
releases: [
  my_app: [
    config_providers: [
      {AgeEx.Sops.ConfigProvider,
       path: {:system, "RELEASE_ROOT", "/deploy/my.server/extra_config.exs"},
       identity_file: {:system, "HOME", "/.config/sops/age/keys.txt"}}
    ]
  ]
]
```

To read the key from an environment variable, for example in a container,
use `identity_env: "SOPS_AGE_KEY"` instead of `identity_file`. The variable
holds the text of an age key file.

age encryption keeps the config secret, but it does not show who wrote the
file. Anyone who knows a recipient can make a valid file, and the provider runs
it as Elixir code. Protect the encrypted file against writes as well as the
release itself.

## Tests

```sh
mix test
```

- `test/cctv_test.exs` runs the [C2SP age test vectors](https://github.com/C2SP/CCTV/tree/main/age)
  in `test/fixtures/cctv`. It uses 122 of the 147 vectors. The other 25 are for
  passphrases only.
- `test/sops_test.exs` uses files from the `sops` CLI and test keys in
  `test/fixtures/sops`. `generate.sh` there makes them again. It needs age 1.3
  or later for the post-quantum key. It also checks
  tampered files and the config provider.
- `test/age_ex_test.exs` checks round trips, and interop with the `age` CLI in
  both directions. The CLI tests run only when `age` and `age-keygen` are on
  the `PATH`. The post-quantum CLI tests also need age 1.3 or later.

To update the vectors, copy `age/testdata` from the CCTV repository into
`test/fixtures/cctv`. Then change the commit in `test/cctv_test.exs`.