Back to blog
·7 min read·BitAtlas Team

Key Management Best Practices: From HSMs to Cloud KMS

From hardware security modules to cloud KMS services, a practical guide to managing cryptographic keys so they stay secret even when the rest of your stack gets breached.

key managementHSMKMScryptographic keyssecrets management

Cryptographic keys are the one thing your entire security posture depends on. You can have perfect encryption, airtight access controls, and comprehensive audit logs — and still lose everything if an attacker gets your keys. This guide covers the practical patterns developers and security engineers use to manage keys correctly, from hardware roots of trust all the way to cloud-managed services.

Why Key Management Is Hard

The problem is straightforward: a key that lives forever is a key that will eventually be compromised. Keys get copied into environment variables, leaked in logs, committed to git repositories, or just sit in a plaintext config file that someone forgot about. The longer a key lives and the more places it touches, the greater the attack surface.

Good key management is about minimising that exposure: rotate often, limit scope, keep keys out of application memory where possible, and always have a plan for what happens when a key is compromised.

The Hierarchy of Key Storage

Level 1: Software Key Stores

The simplest option — keys in environment variables or a secrets manager like HashiCorp Vault, AWS Secrets Manager, or GCP Secret Manager. Keys are encrypted at rest and access-controlled via IAM, but the key material itself can be read by any process with sufficient permissions.

This is fine for many workloads. The threat model you're addressing here is "someone gets read access to the database" or "a config file ends up in a public repo." Against a determined attacker with root on your compute, it doesn't help.

// Fetching a key from AWS Secrets Manager
const client = new SecretsManagerClient({ region: 'eu-west-1' });
const response = await client.send(
  new GetSecretValueCommand({ SecretId: 'my-app/encryption-key' })
);
const keyBytes = Buffer.from(response.SecretString, 'base64');

Level 2: Key Management Services (KMS)

Cloud KMS — AWS KMS, GCP Cloud KMS, Azure Key Vault — takes a different approach: the key material never leaves the HSM-backed KMS boundary. Instead of fetching the key, you send data to the KMS to encrypt or decrypt it.

This is a significant improvement. An attacker who compromises your application server gets the ciphertext and possibly the ability to call KMS, but they cannot exfiltrate the raw key. The key stays in hardware under the cloud provider's control.

// Using AWS KMS to encrypt — key never leaves KMS
const kmsClient = new KMSClient({ region: 'eu-west-1' });
const { CiphertextBlob } = await kmsClient.send(
  new EncryptCommand({
    KeyId: 'arn:aws:kms:eu-west-1:123456789:key/abc-123',
    Plaintext: sensitiveData,
  })
);

The trade-off: you're now dependent on KMS availability (though SLAs are strong), and every encrypt/decrypt operation has latency and cost. For high-throughput workloads, the common pattern is envelope encryption: generate a data encryption key (DEK) locally, encrypt the DEK with KMS, and use the DEK for bulk operations. Only the DEK ciphertext persists.

Level 3: Hardware Security Modules (HSMs)

An HSM is dedicated hardware designed with one purpose: keep keys safe. Key material is generated inside the HSM, operations happen inside the HSM, and the raw key bytes are never exported. Even the manufacturer cannot extract keys after provisioning.

Cloud HSMs (AWS CloudHSM, Azure Dedicated HSM) give you dedicated hardware in the cloud provider's datacenter. On-premises HSMs (Thales Luna, Utimaco, nCipher) give you physical control. For applications that need FIPS 140-2 Level 3 compliance — financial services, healthcare, government — an HSM is often a regulatory requirement, not just a best practice.

The operational complexity is real. HSMs need capacity planning, firmware updates, and careful backup procedures. Losing access to an HSM without a backup means losing access to everything encrypted with it.

Key Rotation Without Downtime

Rotation anxiety is real. Many teams avoid rotating keys because they fear breaking running workloads. The solution is versioned key management:

  1. Each key gets a version identifier. Ciphertext is tagged with the key version used to encrypt it.
  2. Old versions remain decryptable for a transition window. New data is encrypted with the latest version.
  3. A background job re-encrypts old data under the new key version, after which old versions are retired.

AWS KMS and GCP Cloud KMS handle this automatically when you enable automatic rotation. For envelope encryption with DEKs, you need to implement the re-encryption sweep yourself — but the pattern is well-understood.

-- Tag ciphertext with key version for rotation tracking
ALTER TABLE user_documents ADD COLUMN key_version VARCHAR(50) NOT NULL DEFAULT 'v1';
CREATE INDEX idx_key_version ON user_documents(key_version);

Scoped Keys and the Principle of Least Privilege

One key should not encrypt everything. At minimum:

  • Per-tenant keys for SaaS applications. A breach affecting one tenant cannot be used to decrypt another tenant's data.
  • Per-purpose keys — separate keys for data at rest vs. data in transit vs. signing tokens.
  • Short-lived keys for ephemeral workloads. An AI agent that runs for a few minutes should use a key that expires in an hour, not one that lives forever.
// Derive per-tenant keys using HKDF rather than storing N separate keys
import { hkdf } from '@noble/hashes/hkdf';
import { sha256 } from '@noble/hashes/sha256';

function deriveTenantKey(masterKey: Uint8Array, tenantId: string): Uint8Array {
  return hkdf(sha256, masterKey, new Uint8Array(0), tenantId, 32);
}

HKDF lets you maintain a single master key (stored in your KMS) while producing deterministic, isolated per-tenant keys. The master key rotation problem becomes much simpler when there is only one key to rotate.

Detecting Key Misuse Early

Access logging is non-negotiable. Every KMS API call should generate a CloudTrail/audit log entry. Set up alerts for:

  • Unusual call volume from a given IAM principal — could indicate data exfiltration.
  • Cross-region calls — your app running in eu-west-1 should not be making KMS calls to us-east-1.
  • Key access outside normal hours — automated workloads run on a schedule; midnight weekend KMS calls are suspicious.
  • Failed decrypt attempts — repeated failures may indicate an attacker probing with stolen ciphertext.

Most cloud security information and event management tools can wire these alerts up in an hour. There is no reason to fly blind.

Key Backup and Disaster Recovery

The nightmare scenario is not key theft — it is key loss. Encrypted data backed up religiously for five years becomes worthless if you lose the key.

  • KMS key backups: Cloud KMS services store multiple copies across availability zones. For on-premises HSMs, follow the vendor's backup procedures to export a wrapped key backup to offline storage.
  • Test your restore process annually, at minimum. A backup you have never tested is not a backup.
  • Key escrow for compliance: Some regulated industries require the ability to recover plaintext data under court order. This is usually implemented as a secondary encryption under a regulator-held key, separate from your operational key hierarchy.

Putting It Together

The right setup for most applications:

  1. Root of trust: cloud KMS or HSM. Key material never leaves hardware.
  2. Envelope encryption: DEKs generated per-document or per-tenant, encrypted by the root key.
  3. Versioned DEKs: tag ciphertext with key version, automate rotation sweeps.
  4. Scoped keys: per-tenant or per-purpose isolation via HKDF or separate KMS key IDs.
  5. Audit logging: every key operation logged and anomaly-alerted.
  6. Tested backups: quarterly DR drills that actually exercise key restoration.

Key management is one of those areas where doing it correctly from the start is an order of magnitude cheaper than retrofitting it after a breach. The primitives are mature, the cloud services are accessible, and the operational patterns are well-documented. There is very little excuse for leaving key material in a .env file in 2026.

Encrypt your agent's data today

BitAtlas gives your AI agents AES-256-GCM encrypted storage with zero-knowledge guarantees. Free tier, no credit card required.