This is the hands-on companion to our chapter on protecting identity-verification data with HSM key management. The concept chapter ended on a concrete problem: the largest store of sensitive data in most estates is a SQL Server database, and SQL Server’s default Transparent Data Encryption keeps its key material on the same machine as the data. This walkthrough moves the TDE protector into a Thales Luna HSM using SQL Server’s Extensible Key Management (EKM) interface. The T-SQL is verified against current Microsoft documentation; Luna-specific steps follow the Thales Microsoft SQL Server integration guide, and anything we could not pin to a specific document this session is flagged inline rather than invented.
Why TDE’s default protector undermines the point
Out of the box, TDE encrypts each database with a database encryption key (DEK), protects the DEK with a certificate in master, protects that certificate with the database master key, and protects that with the service master key — a chain of keys that lives, in its entirety, on the server it is supposed to be protecting. Anyone who obtains the certificate backup (taken once at setup, then copied wherever backups go) plus a database backup can restore your encrypted database somewhere else and read it. TDE in this shape defends against exactly one threat — someone stealing raw disks or backup files without also getting the certificate — and in real estates the certificate backup usually sits in the same backup system as the databases. For ordinary workloads that may be an acceptable trade. For the identity-verification class of data the previous chapter covered, it fails the first question an assessor asks: the key is not separated from the data, and you cannot bound who has ever been able to copy it.
EKM changes the shape of the chain. The DEK stays where it is — that part of TDE is unchanged — but its protector becomes an asymmetric key that was generated inside an HSM partition and never leaves it. SQL Server asks the HSM to unwrap the DEK when it needs to open the database; the private key itself is never in SQL Server’s memory, never on its disk, never in its backups.
Step 1: Prerequisites, then enable EKM
You need: SQL Server Enterprise edition (TDE and EKM are Enterprise-tier features — Developer and Evaluation also carry them for non-production; check the feature matrix for your exact version); a Luna Network HSM partition initialised with its roles, reachable from the SQL Server host over a registered NTLS client connection; and the Luna HSM client plus the Luna EKM provider package installed on the SQL Server machine. The Thales integration guide lists tested combinations from SQL Server 2016 through 2022 with Luna EKM provider versions 1.4 and 1.5 — if you are on newer versions of either, confirm the current support matrix on thalesdocs.com before you build. If the partition side is not done yet, work through the Luna HSM design and operational guide first; this walkthrough assumes the HSM estate exists.
EKM is off by default. Enabling it is an advanced configuration option:
EXECUTE sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXECUTE sp_configure 'EKM provider enabled', 1;
GO
RECONFIGURE;
GO
Step 2: Install the Luna EKM provider and register it with SQL Server
Install the Luna EKM package on the SQL Server host, then run the LunaEKMConfig utility from the Luna EKM installation folder to bind the provider to your HSM partition. The commands that matter: RegisterSlot to register the partition’s slot with the EKM provider, ViewSlots to confirm what the client can see, and LogSettings to set provider logging (levels 0/1/2 — none, info, debug; turn info logging on now, you will want it the first time an unwrap fails at 2am).
Then register the provider library with SQL Server. This is a one-time, instance-level operation:
CREATE CRYPTOGRAPHIC PROVIDER LunaEKMProvider
FROM FILE = '<path to the Luna EKM provider DLL>';
GO
-- Confirm registration and provider health
SELECT * FROM sys.cryptographic_providers;
SELECT * FROM sys.dm_cryptographic_provider_properties;
The DLL path and file name depend on the Luna EKM package version you installed — take them from the integration guide for your client version rather than from a blog post (this one included). If sys.dm_cryptographic_provider_properties shows the provider but a later step fails authentication, the problem is almost always the slot registration from LunaEKMConfig, not the T-SQL.
Step 3: Credentials, the HSM key, and the login mapping
This step trips people up because it happens twice, for two different principals. The EKM model requires that anyone using the provider authenticates to the HSM via a SQL Server credential holding the partition secret — first the human administrator doing this setup, later the Database Engine itself.
First, the administrator’s credential, mapped to your own login:
CREATE CREDENTIAL sa_ekm_tde_cred
WITH IDENTITY = '<EKM user>',
SECRET = '<partition challenge secret>'
FOR CRYPTOGRAPHIC PROVIDER LunaEKMProvider;
GO
ALTER LOGIN [DOMAIN\YourAdminLogin] ADD CREDENTIAL sa_ekm_tde_cred;
GO
Per the Thales guide, on Luna EKM provider 1.5 an identity authenticating as the partition’s Crypto User role prefixes the identity value with CU_ — a least-privilege option worth using where the key already exists and SQL Server only needs to use it. Now create the TDE protector inside the HSM, as an asymmetric key generated in the partition:
USE master;
GO
CREATE ASYMMETRIC KEY ekm_tde_key
FROM PROVIDER [LunaEKMProvider]
WITH ALGORITHM = RSA_2048,
PROVIDER_KEY_NAME = 'SQL_TDE_Protector_01';
GO
PROVIDER_KEY_NAME is the label the key carries inside the Luna partition — name it like you’ll be reading it in a partition object listing during an audit, because you will. Finally, the mapping that lets the Database Engine authenticate to the HSM on its own, without a human logged in — a second credential, attached to a login created from the asymmetric key. Nobody can log in with this login; it exists purely so the engine can open the database at startup:
CREATE CREDENTIAL ekm_tde_engine_cred
WITH IDENTITY = '<EKM user>',
SECRET = '<partition challenge secret>'
FOR CRYPTOGRAPHIC PROVIDER LunaEKMProvider;
GO
CREATE LOGIN EKM_TDE_Login
FROM ASYMMETRIC KEY ekm_tde_key;
GO
ALTER LOGIN EKM_TDE_Login ADD CREDENTIAL ekm_tde_engine_cred;
GO
Step 4: Create the DEK, encrypt the database, verify
With the protector in hardware and the engine able to reach it, the TDE part is almost anticlimactic:
USE [YourDatabase];
GO
CREATE DATABASE ENCRYPTION KEY
WITH ALGORITHM = AES_256
ENCRYPTION BY SERVER ASYMMETRIC KEY ekm_tde_key;
GO
ALTER DATABASE [YourDatabase] SET ENCRYPTION ON;
GO
Watch the encryption scan complete, then confirm the state and — the part that matters for evidence — the encryptor type:
SELECT DB_NAME(database_id) AS database_name,
encryption_state, -- 3 = encrypted
encryptor_type, -- ASYMMETRIC KEY, not CERTIFICATE
percent_complete
FROM sys.dm_database_encryption_keys;
encryptor_type reading ASYMMETRIC KEY is the one-line proof that the DEK’s protector is the EKM key rather than a certificate on disk. Screenshot it for the evidence pack, and note that tempdb is encrypted automatically once any user database on the instance is.
Step 5: Key rotation under EKM
Rotation is two distinct operations, and conflating them is the most common runbook error. Rotating the DEK re-encrypts the database and is done in place:
USE [YourDatabase];
GO
ALTER DATABASE ENCRYPTION KEY
REGENERATE WITH ALGORITHM = AES_256;
GO
Rotating the protector — the HSM-held key — does not touch the data at all; it re-wraps the DEK under a new key. Create a new asymmetric key from the provider with a new PROVIDER_KEY_NAME (as in Step 3, including a new login-from-key and credential mapping for the engine), then re-point the DEK:
USE [YourDatabase];
GO
ALTER DATABASE ENCRYPTION KEY
ENCRYPTION BY SERVER ASYMMETRIC KEY ekm_tde_key_02;
GO
Two operational notes from the Microsoft documentation worth wiring into the runbook: after a DEK has been modified twice you must take a log backup before it can be modified again, and the old protector key must remain available in the partition until every backup encrypted under it has aged out of retention — restoring last month’s backup needs last month’s protector. Retire HSM keys on your backup retention clock, not your rotation clock.
What happens when the HSM is unreachable
The question every architect should ask before signing off, and the answer is more forgiving than most people expect — in one direction only. TDE holds the unwrapped DEK in memory while a database is open, so an HSM outage does not take a running database down: reads, writes and page encryption carry on. What fails is every operation that needs the protector: opening the database (instance restart, failover, ALTER DATABASE ... SET ONLINE), restoring a backup, and rotating keys. An HSM outage therefore converts “SQL Server restarts” into “database stuck in a recovery-pending state until the HSM returns” — the failure arrives later, at the worst possible time, wearing a different incident ticket. This is the same deferred-failure shape we drilled in the CyberArk HSM failure drill, and the mitigation is identical: drill the restart-during-outage scenario deliberately, and make the HSM layer redundant enough that the scenario is rare. That means a Luna HA group across appliances and sites, plus attention to how the client connection behaves at failover — covered in our chapter on NTLS vs STC and what breaks at DR time.
For Always On Availability Groups there is a sharper edge, called out in the Thales integration guide: the cryptographic provider, credentials, logins and the asymmetric key are objects in master, and master does not replicate. You must create the provider, credentials, login mapping and the same HSM key reference on every replica by hand, and every replica needs its own working NTLS path to the partition. A secondary that can synchronise happily all year will still fail to open the database at failover if its EKM plumbing was never built — test the failover, not the synchronisation.
Performance: what changes and what doesn’t
Nothing about EKM changes TDE’s steady-state cost. Page encryption and decryption still use the DEK, which still sits in SQL Server’s memory; the usual low-single-digit CPU overhead of TDE is what it always was, HSM or no HSM. The HSM is on the path only for key operations — unwrapping the DEK at database open, wrapping at creation and rotation — which are rare, milliseconds-scale events. If someone models this integration as “every query round-trips to an HSM”, correct them early; it is the most persistent misconception in the design review, and it kills good architectures for imaginary reasons.
Where this leaves you
Your TDE protector now lives in hardware, your encryptor_type says so, and your rotation runbook distinguishes the DEK from the key that wraps it. The problems you have inherited are operational: proving the failover behaviour before production does, and making sure every replica’s EKM plumbing exists — put the restart-during-HSM-outage drill on the calendar next to your AG failover test. The chapters on HA failure drills and client connectivity linked above are the natural next reads, and the series so far lives at The Luna Field Guide.
Securitribe designs, deploys and operates Thales Luna HSM estates — partitions, HA, key ceremonies and DR that stand up to audit.
Explore Cryptographic HSM SolutionsBook a strategy call →