Local KMS
No Cargo feature: the local providers are in every build, see Cargo features.
LocalKmsProvider encrypts DEKs with a key encryption key the process holds, with AES-256-GCM on
the backend it is built with. It is for:
- development and tests without a cloud KMS;
- single-host installs where the operator protects the KEK.
kms-aead builds one only when the application calls one of its constructors; no provider falls
back to it. Each constructor logs one INFO event naming the KEK id, its versions and where
each version came from (ephemeral, file, env or secret), never the key.
This page specifies LocalKmsProvider and the local signing and asymmetric providers. To compare
them with the cloud KMS providers, read Choose a provider and a backend.
To run an application and its tests without a cloud account, read Develop and test without a
cloud. The first example is in Tests and
development.
Configuration, authentication and permissions
No credentials and no permissions apply: the process holds the KEK, and protecting it is the operator’s job, see Limits.
KEK ids and versions
A LocalKekId names the KEK: 1 to 255 bytes of ASCII alphanumerics, -, _ and .. Every
encrypted DEK records it, and a decryption refuses a DEK encrypted under another id. The id is also
the key of the DEK cache, see DEK caching and reuse, so one id must name one set of KEK material: two
providers with the same id and different keys share cache entries.
A provider holds one or more LocalKek versions of 32 bytes each. The highest version encrypts new
DEKs unless with_encrypting_version names another, and every version it holds decrypts. Each
encrypted DEK records its version, so adding a new version keeps the DEKs encrypted under older
ones readable.
A KEK comes from one of three loaders, each taking the SymmetricKeyFormat the material is in:
Raw, the 32 bytes themselves, Base64, their standard base64 with trailing whitespace allowed,
or Jwk, an oct JSON Web Key. The format is always named and never guessed from the bytes:
LocalKek::from_secret(version, material, format): bytes the application already holds, such as a key an operator unseals with their own tool (systemd-creds, a TPM, sops, age);LocalKek::from_file(version, path, format): the material in a file. The file must be a regular file of at most 32 KiB. No permission or ownership check runs: restricting who can read or replace the file is the operator’s job;LocalKek::from_env(version, variable, format): the material in an environment variable.Rawtakes the variable’s bytes as the key, which an environment holds only when none of them is0x00. Be aware the variable stays in the process environment for the life of the process: the same user can read it through/proc/<pid>/environ, and every child process inherits it.
Jwk reads the JSON object of RFC 7518 section 6.4,
where k is the 32 key bytes in base64url without padding:
{"kty": "oct", "k": "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkI"}
kty must be oct. alg is optional and must be A256GCM or A256KW when present; kid,
use, key_ops and any other member are ignored. A k in padded or standard base64, or one that
does not decode to 32 bytes, is refused. There is no PEM form of a symmetric key, since no standard
gives it a label.
use std::num::NonZeroU32;
use kms_aead::formats::SymmetricKeyFormat;
use kms_aead::providers::{LocalKek, LocalKekVersion};
use secret_vault_value::SecretValue;
fn example() -> kms_aead::KmsAeadResult<()> {
let jwk = r#"{"kty": "oct", "k": "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkI"}"#;
let kek = LocalKek::from_secret(
LocalKekVersion::from(NonZeroU32::MIN),
SecretValue::from(jwk),
SymmetricKeyFormat::Jwk,
)?;
let _ = kek;
Ok(())
}
Errors name the file or the variable, never its content. For the format an older release assumed in each loader, see Loading a local KEK.
Tests and development
An ephemeral provider has one random version-1 KEK that lives as long as the provider, so the DEKs it encrypts cannot be decrypted once the process exits. It suits tests:
use kms_aead::backends::Ring;
use kms_aead::providers::{LocalKekId, LocalKmsProvider};
use kms_aead::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("tests".to_string())?)?;
let envelope = KmsAeadEnvelope::<LocalKmsProvider<Ring>, Ring>::new(provider)?;
let encrypted = envelope
.encrypt(b"orders/1042", &SecretValue::from("secret"))
.await?;
let decrypted: SecretValue = envelope.decrypt(b"orders/1042", &encrypted).await?;
assert_eq!(decrypted, SecretValue::from("secret"));
Ok(())
}
For a development environment whose data outlives a run, keep a KEK in a variable or a file. Generate one with:
openssl rand -base64 32
use std::num::NonZeroU32;
use kms_aead::backends::Ring;
use kms_aead::formats::SymmetricKeyFormat;
use kms_aead::providers::{LocalKek, LocalKekId, LocalKekVersion, LocalKmsProvider};
fn example() -> kms_aead::KmsAeadResult<()> {
let version = LocalKekVersion::from(NonZeroU32::MIN);
let provider = LocalKmsProvider::<Ring>::new(
LocalKekId::try_from("dev".to_string())?,
vec![LocalKek::from_env(version, "APP_DEV_KEK", SymmetricKeyFormat::Base64)?],
)?;
let _ = provider;
Ok(())
}
A VM or a single host
On a host, keep the KEK out of the environment. With systemd, encrypt it with systemd-creds,
bound to the host’s TPM where it has one, and let systemd pass it to the service as a credential:
openssl rand -base64 32 | systemd-creds encrypt --name=kek-v1 - /etc/credstore.encrypted/kek-v1
[Service]
LoadCredentialEncrypted=kek-v1:/etc/credstore.encrypted/kek-v1
systemd decrypts it into $CREDENTIALS_DIRECTORY, readable only by the service:
use std::num::NonZeroU32;
use std::path::PathBuf;
use kms_aead::backends::AwsLc;
use kms_aead::formats::SymmetricKeyFormat;
use kms_aead::providers::{LocalKek, LocalKekId, LocalKekVersion, LocalKmsProvider};
fn example() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let credentials = PathBuf::from(std::env::var("CREDENTIALS_DIRECTORY")?);
let version = LocalKekVersion::from(NonZeroU32::MIN);
let provider = LocalKmsProvider::<AwsLc>::new(
LocalKekId::try_from("orders-host-1".to_string())?,
vec![LocalKek::from_file(version, &credentials.join("kek-v1"), SymmetricKeyFormat::Base64)?],
)?;
let _ = provider;
Ok(())
}
To rotate, add kek-v2 the same way and list both versions, as the next section describes.
Key versions and rotation
Every version in the provider’s list decrypts, and the highest one encrypts unless
with_encrypting_version names another. The kek_version() of the encrypted DEK an encryption
returns is the version that encrypted, in decimal. A process that does not list a version fails on
a DEK under it with LOCAL_KEK_UNKNOWN_VERSION, so a rotation without downtime takes two deploys:
- list the new version in every process, with
with_encrypting_versionkeeping the current one encrypting; - once every process lists the new version, drop
with_encrypting_version, or point it at the new version, so the new version encrypts.
use std::num::NonZeroU32;
use kms_aead::backends::Ring;
use kms_aead::formats::SymmetricKeyFormat;
use kms_aead::providers::{LocalKek, LocalKekId, LocalKekVersion, LocalKmsProvider};
use secret_vault_value::SecretValue;
fn example(
kek_v1: SecretValue,
kek_v2: SecretValue,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let v1 = LocalKekVersion::from(NonZeroU32::try_from(1)?);
let v2 = LocalKekVersion::from(NonZeroU32::try_from(2)?);
// The first deploy: version 2 decrypts, version 1 still encrypts
let provider = LocalKmsProvider::<Ring>::new(
LocalKekId::try_from("orders".to_string())?,
vec![
LocalKek::from_secret(v1, kek_v1, SymmetricKeyFormat::Raw)?,
LocalKek::from_secret(v2, kek_v2, SymmetricKeyFormat::Raw)?,
],
)?
.with_encrypting_version(v1)?;
let _ = provider;
Ok(())
}
A version that is not in the list fails with_encrypting_version with LOCAL_KEK_VERSIONS.
Then sweep, see Re-encrypt DEKs after a KEK rotation, and check with a provider that lists the new version alone, before the deploy that drops the old version, so that a DEK the sweep missed fails the check and no read in the application. Dropping the old version from the list disables it, and listing it again restores it. Destroying it means destroying every copy of its material, backups included.
Rolling back a rotation
Point with_encrypting_version at the old version, and keep the new version in the list: every
swept value and every value written since the rotation is under the new version only, and dropping
it makes them unreadable until it is listed again. See
Rolling back a rotation.
Asymmetric KEKs
LocalAsymmetricKmsProvider is the local KMS with an RSA key pair per version, for the same uses as
LocalKmsProvider, see Asymmetric KEKs. A LocalAsymmetricKek is one
version: an RSA private key of 2048, 3072 or 4096 bits in PKCS#8, zeroised on drop. 2048 bits is
deprecated and stays for the keys that exist. The loaders have the names of LocalKek’s and take a
PrivateKeyFormat from kms_aead::formats: Pkcs8Der, the DER bytes, or Pkcs8Pem, a
PRIVATE KEY PEM block, as openssl genpkey writes it. The format is never guessed from the
bytes, and a file is at most 32 KiB. from_env reads the PEM text for Pkcs8Pem, and for Pkcs8Der
the standard base64 of the DER, since a variable holds text.
let version = LocalKekVersion::from(NonZeroU32::MIN);
let provider = LocalAsymmetricKmsProvider::<AwsLc>::new(
LocalKekId::try_from("dev".to_string())?,
AsymmetricKekAlgorithm::RsaOaepSha256,
vec![LocalAsymmetricKek::from_file(version, &path, PrivateKeyFormat::Pkcs8Pem)?],
)?;
// The public half of the encrypting version, for the producers
let public_key = provider.public_key(None)?;
- the KEK id names one KEK, symmetric or asymmetric, never both, and the version list, the encrypting version and the rotation work as for the symmetric KEK;
- an encrypted DEK is the
0x02layout with the KEK id and the decimal version. A decryption fails withASYMMETRIC_ENCRYPTED_DEK_FORMAT,LOCAL_KEK_ID_MISMATCH,LOCAL_KEK_UNKNOWN_VERSIONorDECRYPT; LocalAsymmetricKmsProvider::ephemeralhas one generated version-1 key of 2048 bits, for tests;- it needs a backend with RSA: aws-lc-rs or RustCrypto. On ring it fails at construction with
UNSUPPORTED_ALGORITHM. On RustCrypto the private half decrypts DEKs from trusted sources only, see RustCrypto.
Signing
LocalSigningProvider signs with a private key the process holds, on the backend it is built with,
as LocalKmsProvider does for KEKs, see Signatures and MACs. LocalSigningKeyName
names the key: 1 to 255 bytes of ASCII alphanumerics, -, _ and .. A LocalSigningKey is one
version of it, numbered by a LocalSigningKeyVersion: a PKCS#8 private key, either an Ed25519 key or
a P-256 key, and the PKCS#8 names the algorithm. The loaders are those of an asymmetric KEK above,
with the same PrivateKeyFormat, see Key formats.
use std::num::NonZeroU32;
use kms_aead::backends::AwsLc;
use kms_aead::formats::{PrivateKeyFormat, PublicKeyFormat};
use kms_aead::providers::{
LocalSigningKey, LocalSigningKeyName, LocalSigningKeyVersion, LocalSigningProvider,
};
use kms_aead::signing::Signer;
async fn example() -> kms_aead::KmsAeadResult<()> {
let key = LocalSigningKey::from_env(
LocalSigningKeyVersion::from(NonZeroU32::MIN),
"APP_SIGNING_KEY",
PrivateKeyFormat::Pkcs8Pem,
)?;
let provider = LocalSigningProvider::<AwsLc>::new(
LocalSigningKeyName::try_from("invoices".to_string())?,
vec![key],
)?;
let signer = Signer::<_, AwsLc>::new(provider).await?;
// The public key to publish for the verifiers
let jwk = signer.verifying_key().encode(PublicKeyFormat::Jwk)?;
let _ = jwk;
Ok(())
}
- the highest version signs, or the one
with_signing_versionnames, andverifying_keysreturns the public keys of every version as aVerifyingKeySetfor the published set. A rotation is two deploys: list the new version while the current one signs and publish the new public key, then let the new version sign. Dropping a version from the list stops its export, see Key ids and rotation; signnever fails with a network error, andverifying_keymakes no call;LocalSigningProvider::ephemeralhas one random version-1 key of the algorithm it names, for tests. Its signatures verify only against a set that holds its verifying key, so a test builds the verifier from the provider.
Limits
- No IAM, no audit log and no HSM. Protecting the KEK, at rest and in backups, is the operator’s job;
- Every DEK encryption is one AES-256-GCM encryption with a random 96-bit nonce, so a KEK version is safe for about 2^32 encryptions, see Nonces. With the per-call envelope every value costs one encryption: a host encrypting 1,000 values per second spends 2^32 encryptions in 50 days. Use DEK caching and reuse so that one encryption serves many values, and add a new KEK version before the budget is spent;
- Crypto-shredding means destroying every copy of the KEK, backups included;
- DEKs encrypted by a local KEK cannot be exported to BigQuery, which decrypts with Cloud KMS;
- A
LocalSigningProviderkeeps its private key in process memory for its life: whoever reads that memory, a core dump or a swap file included, signs as the application does. Removing the key from every verifier’s set revokes a compromised key.
Errors
An encryption fails with RANDOM_GENERATOR when the backend’s CSPRNG (its cryptographically secure
random number generator) fails. A decryption fails with
LOCAL_ENCRYPTED_DEK_FORMAT when the encrypted DEK is not in the local layout,
LOCAL_KEK_ID_MISMATCH when it was encrypted under another KEK id, LOCAL_KEK_UNKNOWN_VERSION
when the provider does not hold its version, and DECRYPT when it is not authentic under that
version.
Creating a provider fails with INVALID_LOCAL_KEK_ID for an invalid id, LOCAL_KEK_VERSIONS when
the versions are empty or list one version twice, and LOCAL_KEK_SOURCE or LOCAL_KEK_LENGTH when
a KEK cannot be read or is not 32 bytes. An asymmetric provider fails with
ASYMMETRIC_KEK_PRIVATE_KEY for private key bytes that are not in the named format or not an RSA
key of 2048, 3072 or 4096 bits.
A LocalSigningProvider fails at creation with LOCAL_SIGNING_KEY_VERSIONS when the versions are
empty or list one twice, LOCAL_SIGNING_KEY_FORMAT when the backend refuses a key, and
DUPLICATE_KEY_ID when two versions share a key id. A loader fails with LOCAL_SIGNING_KEY_SOURCE
when a file or a variable cannot be read, and with LOCAL_SIGNING_KEY_FORMAT for bytes that are not
the named format. A name that is not valid fails with INVALID_LOCAL_SIGNING_KEY_NAME, and sign
fails with SIGN, a SignatureError, when the backend fails to sign.