Getting started
This page adds the crate, encrypts a value with an envelope and with a keyring, and links the choices a build makes. To decide which provider and backend fit, read Choose a provider and a backend.
Cargo.toml:
[dependencies]
kms-aead = { version = "0.34", features = ["gcp-kms-encryption", "aws-lc"] }
The crate has no default features. A build makes three choices, and they do not constrain each other:
- the KMS providers, any number of them, or none for AEAD without a KMS;
- the TLS root store and the TLS crypto provider of each provider that has a choice;
- the crypto backends that run the AEAD algorithms, one or more.
Features
Every feature of both crates is in Cargo features.
Choosing a backend
There is no default backend: each backend feature adds a type to kms_aead::backends, Ring,
AwsLc or RustCrypto, and every type that encrypts takes one as its backend type
parameter. Which one fits a build, FIPS and the C toolchain included, is in
Choose a provider and a backend.
With an envelope
The local KMS needs no cloud account, so it is the quickest way to try the library. An ephemeral KEK lives as long as the process:
use kms_aead::backends::Ring;
use kms_aead::formats::Envelope;
use kms_aead::providers::{LocalKekId, LocalKmsProvider};
use kms_aead::{Encrypted, KmsAeadEnvelope};
use secret_vault_value::SecretValue;
#[tokio::main(flavor = "current_thread")]
async fn main() -> kms_aead::KmsAeadResult<()> {
let provider = LocalKmsProvider::<Ring>::ephemeral(LocalKekId::try_from("example".to_string())?)?;
let envelope = KmsAeadEnvelope::<LocalKmsProvider<Ring>, Ring>::new(provider)?;
let aad = b"orders/1042";
let secret = SecretValue::from("4111 1111 1111 1111");
let encrypted: Encrypted<SecretValue, Envelope> = envelope.encrypt(aad, &secret).await?;
let decrypted: SecretValue = envelope.decrypt(aad, &encrypted).await?;
assert_eq!(decrypted, secret);
Ok(())
}
To move to a cloud KMS, replace the provider, see KMS providers. The rest of the code stays the same.
With a keyring
Field encryption under a keyring is the kms-aead-keyring crate, see
Keyring and field encryption. Its features are in
Cargo features.
Cargo.toml:
[dependencies]
kms-aead = { version = "0.34", features = ["gcp-kms-encryption", "ring"] }
kms-aead-keyring = { version = "0.34", features = ["ring", "admin", "firestore-tls-roots"] }
secret-vault-value = "1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
A keyring on the memory store with a local KEK, for an application type Email. It needs no
cloud credentials, and needs the admin feature for create, rotate and promote.
KeyringAdmincreates and rotates the keyring, andKeyringis the application’s handle that encrypts and decrypts;- a rotation is staged:
rotateadds a version, and a later promotion makes it the one that encrypts, so every process can load the new version before it is used; FieldAadis the AAD of one field of one record, built from the entity kind, the field name and the entity id.
use kms_aead::backends::Ring;
use kms_aead::providers::{LocalKekId, LocalKmsProvider};
use kms_aead::KmsAeadResult;
use kms_aead_keyring::formats::Randomized;
use kms_aead_keyring::testing::MemoryKeyringStore;
use kms_aead_keyring::{
Encrypted, EntityId, EntityKind, FieldAad, FieldName, FieldSpec, KeyId, Keyring, KeyringAdmin,
KeyringAlgorithms, KeyringId, KeyringRegistry, KeyringRegistryOptions, Plaintext, Purpose,
Encryptor,
};
use secret_vault_value::SecretValue;
#[derive(Debug, Clone, PartialEq)]
struct Email(String);
impl Plaintext for Email {
fn to_plaintext(&self) -> KmsAeadResult<SecretValue> {
self.0.to_plaintext()
}
fn from_plaintext(plaintext: SecretValue) -> KmsAeadResult<Self> {
String::from_plaintext(plaintext).map(Email)
}
}
const TENANT: KeyringId = KeyringId::from_static("tenant-42");
const EMAIL: FieldSpec = FieldSpec::new(
EntityKind::from_static("user"),
FieldName::from_static("email"),
);
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let provider =
LocalKmsProvider::<Ring>::ephemeral(LocalKekId::try_from("tenant-kek".to_string())?)?;
let registry = KeyringRegistry::<MemoryKeyringStore, Ring>::new(
MemoryKeyringStore::new(),
KeyringRegistryOptions::new(),
)?;
let admin: KeyringAdmin<MemoryKeyringStore, LocalKmsProvider<Ring>, Ring> = registry
.create(provider, TENANT, KeyringAlgorithms::new())
.await?;
// The application's handle on the same keyring
let keyring: &Keyring<MemoryKeyringStore, LocalKmsProvider<Ring>, Ring> = admin.keyring();
// The AAD ties the value to user 42's email: `user/42/email`
let aad: FieldAad = EMAIL.aad(&EntityId::try_from("42")?);
let alice = Email("alice@example.com".to_string());
// One encryptor per write of a record, for every encrypted field it holds
let encryptor: Encryptor<'_, MemoryKeyringStore, LocalKmsProvider<Ring>, Ring> = keyring.encryptor().await;
let value: Encrypted<Email, Randomized> = encryptor.encrypt(&aad, &alice).await?;
assert_eq!(value.key_id(), KeyId::new(1));
// A staged rotation keeps the previous version decrypting. One process has nothing to wait for,
// so it promotes at once
let staged: KeyId = admin.rotate(Purpose::Randomized).await?;
admin.promote_now(staged).await?;
assert_eq!(keyring.decrypt(&aad, &value).await?, alice);
registry.shutdown(None).await;
Ok(())
}
The registry, its handles and the encryptor are in The registry and its handles.
Full example available
here,
run it with cargo run -p kms-aead-keyring --example local_kms --features ring,admin.
MSRV
The minimum supported Rust version is 1.99, for every feature.