age-crypto
A safe, ergonomic, and pure Rust wrapper around the age encryption library. age-crypto provides a high-level, idiomatic API for encrypting and decrypting data using modern cryptographic primitives. It supports both X25519 public-key encryption and passphrase-based encryption (scrypt), with options for both binary and PEM-armored (ASCII) output formats.
Designed for seamless integration with age-setup, this crate handles the heavy lifting of encryption while ensuring type safety and secure memory handling.
Features
- Multiple Encryption Modes:
- Public Key (X25519): Encrypt data for specific recipients using their public keys.
- Passphrase (Scrypt): Encrypt data using a passphrase, suitable for backups and personal use.
- Output Formats:
- Binary: Compact, efficient for storage and network transmission.
- Armored (PEM): Text-safe format (Base64) suitable for email, JSON, or copy-pasting.
- Multiple Recipients: Encrypt a single file for multiple recipients in one operation.
- Type Safety: Strong types (
EncryptedData,ArmoredData) prevent misuse of ciphertext. - Secure Memory: Automatic zeroization of sensitive data (passphrases) upon drop.
- C Binding Support: Full Foreign Function Interface (FFI) for integration with C/C++ projects.
Installation
Add this to your Cargo.toml:
[]
= "0.2" # Check crates.io for the latest version
For key generation capabilities, we recommend using the companion crate:
[]
= "0.1"
Quick Start
1. Passphrase-Based Encryption
The simplest way to encrypt data. No key management required.
use ;
2. Public Key Encryption (with age-setup)
Ideal for secure communication between parties.
use ;
use build_keypair;
API Reference
The library provides 8 core functions divided by encryption mode and output format.
Encryption Functions
| Function | Mode | Output Format | Description |
|---|---|---|---|
[encrypt] |
Public Key | Binary (Vec<u8>) |
Encrypts for one or more recipients. Most compact format. |
[encrypt_armor] |
Public Key | Armored (String) |
Encrypts for recipients, wrapped in PEM armor. |
[encrypt_with_passphrase] |
Passphrase | Binary (Vec<u8>) |
Encrypts using a passphrase via scrypt. |
[encrypt_with_passphrase_armor] |
Passphrase | Armored (String) |
Encrypts with passphrase, wrapped in PEM armor. |
Decryption Functions
| Function | Mode | Input Format | Description |
|---|---|---|---|
[decrypt] |
Public Key | Binary | Decrypts using a secret key (AGE-SECRET-KEY-1...). |
[decrypt_armor] |
Public Key | Armored | Decrypts armored data using a secret key. |
[decrypt_with_passphrase] |
Passphrase | Binary | Decrypts using the passphrase. |
[decrypt_with_passphrase_armor] |
Passphrase | Armored | Decrypts armored data using the passphrase. |
Usage Guide
Output Types
The library uses wrapper types to ensure data integrity and prevent accidental misuse.
EncryptedData
Represents raw binary encrypted data.
let data = encrypt?;
let bytes: & = data.as_bytes;
let vec: = data.to_vec;
ArmoredData
Represents ASCII-armored encrypted data. It implements Display to output the armored string.
let armored = encrypt_with_passphrase_armor?;
println!; // Prints the full PEM block
assert!;
Multiple Recipients
You can encrypt data for multiple recipients simultaneously. Any one of the corresponding secret keys can decrypt the file.
use build_keypair;
let alice = build_keypair?;
let bob = build_keypair?;
let recipients = ;
let encrypted = encrypt?;
// Bob can decrypt it
let dec = decrypt?;
assert_eq!;
Error Handling
Errors are categorized into EncryptError and DecryptError wrapped in a top-level Error enum.
use ;
match decrypt
C/C++ Integration (FFI)
age-crypto provides a stable C API for use in other languages. The bindings are generated using cbindgen.
Building the Shared Library
- Ensure you have the
cdylibcrate type in yourCargo.toml. - Build with cargo:
The output will be located attarget/release/libage_crypto.so(Linux),.dylib(macOS), or.dll(Windows).
Header File (age-crypto.h)
The header file defines the available functions. You can generate it using cbindgen or find it in the clib/ directory of the repository.
C API Reference
Memory Management
age_free_string(char *s): Frees a string returned by the library.age_free_bytes(uint8_t *data, size_t len): Frees a byte array returned by the library.
Encryption
age_encrypt(...): Binary public key encryption.age_encrypt_armor(...): Armored public key encryption.age_encrypt_with_passphrase(...): Binary passphrase encryption.age_encrypt_with_passphrase_armor(...): Armored passphrase encryption.
Decryption
age_decrypt(...): Binary public key decryption.age_decrypt_armor(...): Armored public key decryption.age_decrypt_with_passphrase(...): Binary passphrase decryption.age_decrypt_with_passphrase_armor(...): Armored passphrase decryption.
C Example
int
Security Considerations
- Memory Zeroization: The
Passphrasetype and secret keys managed byage-setupare automatically zeroized when they go out of scope, minimizing the risk of secrets remaining in memory. - Nonce Management: The library automatically generates unique nonces for every encryption operation. You do not need to manage them.
- Passphrase Strength: For passphrase-based encryption, the security relies entirely on the strength of the passphrase. Use long, high-entropy passphrases (e.g., Diceware).
- Armored Data: While armored data is base64 encoded, it is not "encrypted twice". It is simply a text-safe representation of the ciphertext.
License
Licensed under either of
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
Contribution
Contributions are welcome! Please ensure cargo test passes and run cargo clippy to check for linting issues before submitting a pull request.