The age 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...), asage-keygenmakes them. - Hybrid post-quantum ML-KEM-768 + X25519 keys (
age1pq1...), asage-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. 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
def deps do
[{:age_ex, "~> 0.1.0"}]
endUsage
%{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/2decrypts a JSON file to the terms thatJSON.decode/1gives. SOPS keeps JSON numbers as floats, so a number with no fraction part comes back as an integer.AgeEx.Sops.decrypt_dotenv/2decrypts 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 asdecrypt_json/2.sops decryptgives them as text in a Go format, for example42.0or1E+21.AgeEx.Sops.decrypt_ini/2decrypts 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 ofdecrypt_dotenv/2. The function refuses a duplicate section or key.AgeEx.Sops.decrypt/2decrypts a file thatsops encrypt --input-type binarymakes, and gives the original bytes.- YAML is not supported
{: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:
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
mix test
test/cctv_test.exsruns the C2SP age test vectors intest/fixtures/cctv. It uses 122 of the 147 vectors. The other 25 are for passphrases only.test/sops_test.exsuses files from thesopsCLI and test keys intest/fixtures/sops.generate.shthere 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.exschecks round trips, and interop with theageCLI in both directions. The CLI tests run only whenageandage-keygenare on thePATH. 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.