Cryptography

HSMs and key management: the root of trust in financial infrastructure

Every financial system reaches the moment when an encryption key has to be born, stored and rotated. The question is where — and in financial infrastructure, "a file on the server" is not an answer.

Article summary
  • An HSM is not built to encrypt fast — it is built so that the key cannot leave it.
  • FIPS 140-3 level 3 is the practical bar for payments; level 4 is mainly required for critical infrastructure.
  • A KEK/DEK hierarchy lets you rotate keys without downtime and without touching the data itself.
  • The common failure is not technical: organisations buy an HSM and then do not maintain the ceremony, the backups or the HA.
A hardware security module (HSM) — an illuminated cube in shades of green

Why you cannot do without it

Every encryption key has to live somewhere, and every such place is a compromise. In a file on disk, in the memory of a process, in a managed KMS service or inside a hardware module — the difference is not how fast the encryption runs but what can be taken out of it. In any architecture I ask one question: what leaves the boundary. A key you can copy quietly is a key you cannot prove was never copied, and an attacker already on the network will try exactly that — read it. A hardware module does not make a system impenetrable; it changes the question from "how do we block access" to "what can be done with the key even when there is access".

A file, an environment variable, a secret in CI

The cheap option is also the only one you cannot fix afterwards. Once a raw key sits in config.yaml, in a Kubernetes Secret or in a pipeline environment variable, it is everywhere those files are: in a backup, in an image layer, in a build log, on the machine of whoever ran the script and on the screen of whoever is watching the job. You can encrypt the secret, but then the encryption key lives somewhere else, and the question has moved one step sideways.

The real cost is not a single leak but the inability to rotate: a key with no history, no owner and no replacement process will never be replaced. I have seen this more than once — a key from 2019 holding all internal mTLS traffic, and nobody knows who holds it.

A key in process memory

The second option looks clean: the key is loaded into memory when the service starts, never written to disk, and anyone not connected to the server does not see it. But memory is not a safe. The operating system writes pages to swap, creates a core dump when it crashes, keeps a hibernation file, and lets the hypervisor snapshot all of memory. Each of those is a copy of the key, and sometimes it is kept for months.

And on top of that sits the least discussed vector: cold boot. DRAM keeps its contents for a short while after the power is cut, and when chilled it keeps them for far longer. Anyone with physical access switches the server off at the mains, boots from an external disk and reads what is left. Disk-encryption keys have to be in memory for as long as the drive is mounted, so there they are. Even without physical access they are reachable through root privileges, kernel crash dumps and vulnerabilities in memory management.

Managed KMS: the right default for most

A managed KMS service solves most of the problem with less operational work. The key is not in the application, you hold a permission rather than the material, and every use is written to a log you cannot delete. The price is that the risk moves to the authorisation layer: kms:Decrypt is a very broad permission, and whoever gets it can decrypt everything. The protection around the key is complete, and the permission on it is wide open.

There are two technical costs as well: every operation is an API call, so there is latency and a dependency on the service being available. You use it to wrap data keys, not to encrypt records.

HSM: the key does not leave

A hardware security module (HSM) is the other end of the scale. The key is born inside, kept inside, and used for the signing and decryption operations the module performs itself. What comes out is only a result: a signature, ciphertext or a wrapped key. Whoever takes over the server can send signing requests and cannot copy the key. Even the person who administers the module cannot read it, and at level 3 cannot open it without it erasing itself.

Diagram of the HSM boundary: the master key, the KEKs and the wrapped keys that cross the boundary
The module boundary is marked by the enclosing line. Inside, the master key (MK) and the wrapping keys (KEK) are born, and from them the data encryption keys (DEK) are derived. What crosses the boundary is only the DEK, wrapped in a KEK; the master key never leaves the module in any situation — not in a backup, not in a restore and not in a rotation. The arrows mark operations the module performs, not key material passing through it.

The cost is real: a purchase or a subscription, dependence on a vendor, firmware updated once a year, a ceremony that needs people in a room, and an operation rate lower than a software key allows. So the right question is not "do we need an HSM" but "which key has to live in one". Ephemeral traffic keys do not. Keys that sign payment instructions do.

FIPS 140-3: what it means in practice

FIPS 140-3 is the US standard for the security of cryptographic modules, and it rests on ISO/IEC 19790. It does not test whether a product is "good"; it tests whether the module meets defined requirements for physical security, key management, interfaces, self-tests and documentation. Certification is issued under the CMVP, applies to a specific firmware version and is published on a public list. So "our product is FIPS" means nothing until it is clear which level, which version, and whether the certificate is still valid.

What the standard does give you: a basis for comparison, self-tests the module runs at start-up and during operation, firmware integrity checks, and documentation you can put in front of an auditor. What it does not give you: a guarantee that the implementation has no vulnerabilities, coverage of your own application, or any statement about how permissions are managed. A good certificate with a shaky operational process is worse than a good process without one.

Four levels, four promises

LevelMain requirementsTypical use
1Software or hardware module; approved algorithms; no real physical protection requirementDevelopment tools, conformance testing, software products
2Role-based authentication; enclosure with evident tamper marks or locksInternal enterprise systems, terminals, secrets management
3Tamper resistance with a response (zeroization); identity-based authentication; separation of roles and protection of the modulePayments, clearing, PKI, card issuance, signing
4As level 3, plus protection of the whole module enclosure, detection of abnormal environmental conditions (voltage, temperature) and safe shutdown, and stricter penetration testingCritical infrastructure, government and defence environments, large payment systems

Level 3 is the practical bar for most financial infrastructure, and not because it is "good enough for us". Here the requirement is identity-based authentication rather than a role password: the device has to identify who is in front of it and tell a user from an administrator. Above that sits a tamper response mechanism — an attempt to open the enclosure triggers zeroization and the keys are erased. An attacker who physically reaches the device gets dead hardware. For payments this is the bar because it addresses the two vectors that actually matter: the insider with a permission, and the person who carries the device out of the cabinet.

Why level 4 is rare

Level 4 requires protection of the whole module enclosure, detection of abnormal environmental conditions and safe shutdown, and stricter penetration testing. That costs performance, hardware, maintenance and the number of products actually certified. You do not buy it to protect the signing of an API token; you buy it when the vector is a laboratory or a state, not an attacker on the network.

And in any case, no level compensates for a shaky process. Bangladesh Bank lost about US$81 million in 2016 without anyone breaking encryption: the attackers used legitimate credentials and SWIFT terminals. An HSM is not supposed to stop a properly signed instruction; it is supposed to ensure that the key which signed it could not quietly sign something else.

PKCS#11: the language of the HSM

PKCS#11, also called Cryptoki, is a C interface standardised at OASIS, and almost every module in the world implements it. It is the reason you can move code between vendors but not move keys — and also the place where marketing meets reality: a missing or slow mechanism shows up here, not in the brochure.

The object model: slot, token, session, object

The model is small and precise. Slot is a place where a token can sit — a physical card reader, a partition on a network module or a virtual slot. Token is the cryptographic device itself, with a label, a serial number and flags. Session is a logical connection: read-only, read-write, or an administrator (SO) session opened only for management. Object is anything stored in the module — a private key, a public key, a symmetric key, a certificate, data. Token objects survive a disconnect; session objects vanish with it.

Most behaviour is set in attributes, not in code. CKA_TOKEN decides whether the object is stored, CKA_PRIVATE requires a login, CKA_SENSITIVE forbids reading the material, and CKA_EXTRACTABLE decides whether the key can be exported even when it is wrapped. I have found most of the security failures in projects like these not in the cryptography but in the wrong combination of attributes.

Login happens through C_Login, with a user role (CKU_USER) or an administrator role (CKU_SO). The administrator creates users and resets passwords but cannot see keys — a separation enforced in the module, not in your code. There is also a per-operation authentication mode (CKU_CONTEXT_SPECIFIC) that requires approval for every signature; it suits signing payment instructions, but creates a real operational threat for high-throughput signing systems.

Real code: create a key pair and sign

The example is C against PKCS#11 directly, because it shows exactly what happens: an RSA key pair whose private key cannot be exported, and a signature over an existing digest.

static CK_FUNCTION_LIST_PTR p11;

/* A signing key that never leaves the module */
CK_RV hsm_make_signing_key(CK_SESSION_HANDLE s,
                           CK_OBJECT_HANDLE *pub, CK_OBJECT_HANDLE *priv)
{
    CK_MECHANISM mech    = { CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0 };
    CK_BBOOL     yes     = CK_TRUE, no = CK_FALSE;
    CK_ULONG     bits    = 3072;
    CK_BYTE      e[]     = { 0x01, 0x00, 0x01 };          /* 65537 */
    CK_UTF8CHAR  label[] = "payments-sign-2026-11";

    CK_ATTRIBUTE pub_tpl[] = {
        { CKA_TOKEN,           &yes,  sizeof(yes) },
        { CKA_VERIFY,          &yes,  sizeof(yes) },
        { CKA_MODULUS_BITS,    &bits, sizeof(bits) },
        { CKA_PUBLIC_EXPONENT, e,      sizeof(e) },
        { CKA_LABEL,           label,  sizeof(label) - 1 }
    };
    CK_ATTRIBUTE priv_tpl[] = {
        { CKA_TOKEN,       &yes, sizeof(yes) },
        { CKA_PRIVATE,     &yes, sizeof(yes) },
        { CKA_SENSITIVE,   &yes, sizeof(yes) },
        { CKA_EXTRACTABLE, &no,  sizeof(no) },            /* no export */
        { CKA_SIGN,        &yes, sizeof(yes) },
        { CKA_LABEL,       label, sizeof(label) - 1 }
    };

    return p11->C_GenerateKeyPair(s, &mech, pub_tpl, 5, priv_tpl, 6, pub, priv);
}

/* Sign a prepared digest */
CK_RV hsm_sign(CK_SESSION_HANDLE s, CK_OBJECT_HANDLE priv,
               CK_BYTE_PTR digest, CK_ULONG len,
               CK_BYTE_PTR sig, CK_ULONG_PTR sig_len)
{
    CK_MECHANISM mech = { CKM_RSA_PKCS, NULL_PTR, 0 };
    CK_RV rv = p11->C_SignInit(s, &mech, priv);
    return rv == CKR_OK ? p11->C_Sign(s, digest, len, sig, sig_len) : rv;
}

Three notes. C_GenerateKeyPair is atomic: either both keys are created or nothing is. C_Sign is called twice in real code — once with a null pointer to get the signature length, and once with a buffer of the right size. And CKM_RSA_PKCS signs a prepared DigestInfo, whereas CKM_SHA256_RSA_PKCS does the hashing inside the module; that difference is a common source of invalid signatures.

Vendor wrappers and the JCE provider

Most code in the world does not call C_Sign directly. It uses Java's SunPKCS11, a dedicated provider from the cloud vendor or the vendor's SDK. That is convenient, but it hides exactly what matters: the choice of mechanism, the key's attributes and the error code. An error that should have come back as CKR_KEY_UNEXTRACTABLE turns into a meaningless InvalidKeyException. In a POC I check that every signature lands in the module and not in a software implementation, and that no compatibility setting quietly allows a fallback — a signature produced in software is exactly the failure no log shows.

CloudHSM versus a physical HSM

The real division is not cloud versus cabinet. It is who holds the management plane, who can read the audit log, and who controls the firmware. A cloud vendor saves operational work and adds a party to the trust boundary; a physical vendor leaves control with you, along with the spare parts and the night shifts.

AWS CloudHSM

A dedicated module, single tenant, in your VPC and on your responsibility. You manage users, permissions and backups, and the vendor states that it has no access to key material. You can deploy a cluster across availability zones with key synchronisation, so a failure of one module is not a failure of the service. The drawback: it is still an operational system, and teams that treated it like KMS discovered they were running an HSM.

Azure Dedicated HSM

A dedicated appliance in your VNet, based on Thales Luna, with PKCS#11, JCE and CNG interfaces. Microsoft manages the hardware and the deployment, and you manage the device itself. Worth knowing that Microsoft's own recommendation for most scenarios is Managed HSM rather than Dedicated HSM, because Dedicated is meant for lift and shift of existing workloads. That distinction saves money and operational effort.

Google Cloud HSM

The approach here is fundamentally different: there is no slot or session of your own, only a key and its versions. You set protectionLevel: HSM and get encryption and signing operations through the API, with Cloud's audit log. It is excellent for wrapping data keys and for native cloud services, and less suited to a system that has to sign thousands of messages a second. The pricing is different too: per key, per version and per operation.

Thales Luna

A network appliance divided into partitions, where each partition behaves like a separate HSM with its own administrators and policy. It suits an organisation with several teams that need isolation, or one that needs both cloud and an on-premises room under the same procedures. HA is done on the client side, and a group of modules shares load and handles failure; backup depends on the cloning domain, and that is exactly the detail that gets forgotten when people change.

Entrust nShield

Two main models: Connect on the network and Solo on PCIe. The central model is Security World, in which smart cards hold the module keys, and you can require a quorum of k out of n cards before the module starts or performs an operation. That is the answer for anyone who needs genuine collective control over the root of trust, with no single person able to bring the system up alone. The price is an orderly ceremony and card management over the years.

OptionDeploymentCertification levelControl and oversightLatencyCost model
AWS CloudHSMA dedicated instance in a VPC, a cluster across zonesLevel 3You manage users and keys; the cloud manages the hardwareA network call from inside the VPCPriced per instance per month
Azure Dedicated HSMA dedicated appliance in a VNet, based on LunaLevel 3You manage the device; Microsoft manages the deploymentA network call from inside the VNetPriced per appliance per month
Google Cloud HSMKeys in Cloud KMS with protectionLevel HSMLevel 3The API manages versions; there is no session of your ownAn API call; not suited to a high signing ratePer key, version and operation
Thales LunaA network appliance at your site or at a host; partitionsLevel 3You own the partition and are its administratorsThe lowest when it is in the same rackLicence, support and hardware
Entrust nShieldConnect on the network or Solo on PCIe; Security WorldLevel 3Collective control through quorum cardsLow; local hardwareLicence, support and hardware

Every row shows level 3, and this is where marketing and paperwork meet. Not every product is certified to 140-3; some certificates are 140-2 ones still in force, and a certificate applies to a specific firmware version. I open the CMVP list before every POC. What the table does not show is who updates firmware at two in the morning, who can reset an administrator password, and who sees the audit log — three questions that determine the risk more than any row.

Key hierarchy and rotation

Anyone who starts building an encryption system discovers very quickly that one key is not enough. What you need is a hierarchy: a few root keys, wrapping keys derived from them, and data keys derived from those. The hierarchy lets you rotate without touching the data itself, and that is the difference between a week-long project and a year-long one.

KEK wraps DEK, not the other way round

A data encryption key (DEK) encrypts the record or the field. A key encryption key (KEK) encrypts the DEK, and the result — a wrapped key — is stored next to the data with a version identifier. The DEK is born inside the HSM, wrapped by a KEK that lives inside, and leaves only in wrapped form. Replacing a KEK means re-wrapping keys — an operation on kilobytes, not terabytes. Replacing a DEK requires re-encryption, so you do it gradually, with a version in every record.

There is a trade-off here worth knowing. If the application holds a decrypted DEK in memory, we are back to the memory problem from the first section. If every request calls the HSM, you pay in latency and throughput. The practical answer is a short-lived cache per process, or a key that stays in the module and signs a digest only. You make that choice on the operation rate, not on instinct.

A rotation schedule

  • DEK: yearly, and on an event — a staff member leaving, an exposure, a change of vendor.
  • KEK: every year or two, with re-wrapping of every subordinate key.
  • Root key and ceremony keys: rare, and only on a real failure — every such rotation is an operational event.
  • Signing keys: by certificate validity, with an overlap of two keys for a transition period.
  • Traffic keys: very short-lived and automated, so they do not move the needle.

The rule I apply is a reversible rotation: the old version stays for a defined period, and every record carries its identifier. Without that, rotation is a gamble, and without a test in staging it is only an intention. You run the exercise once a quarter, on a real system, measuring what breaks.

Ceremony, backup, availability and logs

A ceremony is the process in which a root key is created or loaded: a written script, a room, at least two people, smart cards, and a backup card in a separate place. The evidence is produced by the documentation — who was present, which quorum was used, which firmware version, serial numbers, and a printed hash of the public key. When it finishes without a signed document, the organisation will not be able to restore itself three years later.

A ceremony without witnesses and documents is not a ceremony

If the only evidence is a photo on someone's phone, you have a ritual, not a process. Three years later it emerges that the only person who knew the administrator password has left, that the cards are in a drawer nobody can open, and that the backup cannot be restored because the cloning domain password was in his memory. A key that was not documented is a key that cannot be restored, and that is exactly the failure that surfaces on the worst possible day.

Backup and HA are two different things. HA is an additional module serving the same keys in real time (active-active), so that a failure of one module does not stop the service, and in good systems it is spread across two zones. Backup is an encrypted copy of the keys, kept elsewhere, that allows a restore into a clean module. An untested backup is a hope, not a plan, so the restore is tested once a quarter — with the quorum cards that sit at the recovery site.

The audit log is the part nobody builds in time. The module knows what the operation was, not why; the application knows which instruction was signed. Only the two together, with synchronised time, answer the question that will be asked in an investigation: who signed what, and from which service. Send the logs somewhere they cannot be deleted, with retention defined in advance.

How to choose — the procurement process

HSM procurement usually fails not because of a wrong choice but because of a requirements list written by the vendor. I demand three numbers before I agree to talk about a product: peak signing rate, the number of keys and versions, and the number of concurrent clients. Without those, every demonstration looks impressive.

Requirements before a demo

  • Performance: peak signing rate, including a burst of three times that, and p99 rather than an average.
  • Keys: the number of keys, the number of versions per key, and the number of partitions.
  • Availability: topology, RTO and RPO, and behaviour during the failure of a single module.
  • Certification: FIPS level, firmware version and certificate validity — not just the logo.
  • Integration: PKCS#11, JCE, CNG and KMIP; and which of them will still be there in five years.
  • Cryptographic agility: support for FIPS 204 and for the move to PQC, as described in the article on post-quantum cryptography.
  • Export policy: what happens in an emergency, and who can take a wrapped key out, under what approval.
  • Operations: firmware updates remotely or on site, the update window, spare-part availability and EOL.

POC: what to actually test

In a POC I run my own load, not the vendor's: real code, real rate, and p99 over an hour. Then we pull a module's power under load and check whether the service survived. We restore a backup into a clean module and compare signatures, and rotate a KEK end to end. We try to export a key with CKA_EXTRACTABLE=false and confirm that we get CKR_KEY_UNEXTRACTABLE and nothing else.

The ceremony is run once with a vendor engineer, and you see what documentation it produces at the end. That is the moment it becomes clear whether this is a process or a performance. Firmware testing belongs here too: how long an update takes, whether there is downtime, and what happens to signatures in the middle.

What to put in the contract

A good contract covers the things that are not in the data sheet. A commitment on firmware updates and on response time for a security advisory, and a year's notice of EOL. A written export policy — the right answer is usually no export, and that should be explicit. Responsibility for spare parts and for returning a product with evidence of tampering, a locked price for the contract term, and exit terms including migration support.

About technical lock-in you have to be honest: keys do not leave, so switching vendors is not key migration but re-issuance. That is the price of the feature, not a bug. So you decide on the vendor before the hierarchy sets, you stay on PKCS#11 because it is a portable interface, and you avoid a proprietary SDK wherever you can. The ceremony scripts are kept in git without secrets, so a move to another vendor is a diff rather than a rewrite.

When you do not need an HSM

I am writing this because too many articles about HSMs end with "buy an HSM". That is wrong for most systems, and sometimes it is harmful: money spent on hardware nobody will operate is money not spent on permissions, on key rotation and on monitoring. A startup with a small team needs a managed KMS, envelope encryption, narrow permissions and logs — not a module in an air-conditioned room.

When the company grows and a contractual or regulatory requirement appears, you can add a module, but the key hierarchy has to be designed properly from the start. Even someone who only manages TLS keys does not need an HSM: certificates that renew automatically and are managed with the tools in Kubernetes secrets management are enough, because the root of trust is the CA, not you.

There is a good middle path: Vault with auto-unseal against a cloud KMS, and a transit engine that encrypts without exposing keys to the application. It solves most of the operational problem, and still leaves the material in the memory of a software process while the vault is open. If the requirement is that the key never sits in a server's memory at any moment, you need a PKCS#11-based seal, and that difference is worth stating to MAS before you choose a product.

So when yes: when a key has to be used for an operation without anyone seeing it (PINs, card issuance, a CA root, signing for a clearing system); when there is a contractual requirement, or one from a card scheme, covering payments; when you need controlled evidence of every use of a key; and when you need collective control — two people rather than one. And when no: when the problem is over-broad permissions or keys in git. An HSM solves none of that, and that is exactly what happened at Equifax in 2017 with about 147 million people — the problem was not the key, it was the process.

Key takeaways

  • An HSM is not built to encrypt fast — it is built so that the key cannot leave it, even for the people who administer it.
  • FIPS 140-3 level 3 is the practical bar for payments: identity authentication, tamper response and key erasure. Level 4 is rare and answers a different threat.
  • PKCS#11 is the same interface at almost every vendor, and the real security failures sit in the combination of attributes, not in the mechanism.
  • A KEK/DEK hierarchy allows rotation without downtime, but a ceremony, a backup and an HA setup that were never rehearsed do not exist.
  • Not every system needs an HSM. A managed KMS with tight permissions beats a module nobody is there to operate.

Sources and links

  1. NIST FIPS 140-3 — Security Requirements for Cryptographic Modules
  2. PCI Security Standards Council — Document Library (PCI DSS 4.0.1)
  3. AWS CloudHSM — product documentation
  4. Google Cloud KMS — HSM protection level
  5. Entrust nShield HSM
  6. HashiCorp Vault — documentation (seal, transit)
  7. NIST FIPS 204 — ML-DSA
  8. 2016 Bangladesh Bank heist — overview