Secure enclaves

How to protect code and data even from whoever controls the machine.

The problem

Normally, whoever runs the computer sees everything. The operating system can read the memory of any process, the hypervisor can read the memory of any virtual machine, and whoever administers the server (or the cloud) can read all of it together.

Without an enclaveWith an enclaveYour app + secretOperating systemreads the secretHypervisorreads the secretAdmin / cloudreads the secretEnclave + secretOperating systemsees only ciphertextHypervisorsees only ciphertextAdmin / cloudsees only ciphertext
The same layers exist in both cases. What changes is who can read the secret.

That is a problem when you want to process something sensitive, such as a private key, medical data or another company's model, on a machine that is not yours or that you do not fully trust.

The idea in one sentence: a secure enclave is an isolated area, guaranteed by the hardware itself, where code runs and data stays protected even from the operating system, the hypervisor and the owner of the machine.

Who you trust

The enclave flips the trust stack. Instead of trusting every software layer below your program, you trust only the processor and the code inside the enclave. This minimal set of trusted things is called the TCB (Trusted Computing Base).

Application (regular part)Enclave: code + secretsOperating systemuntrustedHypervisor / clouduntrustedProcessor (hardware)
Only the dashed parts belong to the TCB. Everything in between may be compromised.

The four pieces

1. Memory isolation

The processor reserves a memory region for the enclave and blocks any access coming from outside it, including from the kernel. If the operating system tries to read those addresses, it gets garbage or an error.

This isolation comes in different sizes. Some technologies protect only a piece of a process; others protect a whole virtual machine:

Process enclave (SGX)Operating systemYour processEnclaveonly the sensitive partConfidential VM (TDX, SEV-SNP)Cloud hypervisorWhole VM protectedGuest OSyour apps
Protecting a process keeps the TCB smaller. Protecting the whole VM makes it easier to run existing software without rewriting anything.

2. Encrypted memory

Inside the processor the data is in plaintext, because that is where the code works on it. When it leaves the chip for RAM, an encryption engine encrypts everything automatically with a key that never leaves the processor. On the way back, it decrypts. So even someone with physical access to the memory sticks sees only scrambled bytes.

inside the chipCoreplaintext datapass=hunter2Crypto enginethe key neverleaves the chipRAM9f a2 7c 01e3 5b d8 44OS or attackerreads only ciphertext
The data only exists in plaintext inside the processor.

3. Remote attestation

How do you know the code running in there is really what you expect, and that it is in a real enclave and not a simulator? The processor computes a hash (the measurement) of the code loaded into the enclave and signs it with a key that only a genuine chip has. You check the signature, compare the hash with the code you audited, and only then send your secrets.

You (client)HostEnclaveManufacturer1. attestation + nonce?2. forwards the request3. CPU signs a report:hash + nonce + public key4. signed report5. is this signature from a genuine chip?6. yes, valid certificate7. compares the hash withthe code it audited8. secret encrypted for the enclavethe host forwards it, but sees only encrypted bytes
The nonce stops an old report from being replayed. The public key in the report is what lets you encrypt the secret for that enclave only.

4. Sealing

The enclave needs to keep things across reboots, but the disk is controlled by the operating system. The solution is to encrypt the data with a key derived from two ingredients: a secret key burned into the chip and the measurement of the enclave itself.

Chip root keyEnclave measurementsecretSeal (encrypt)blob on disksame codeand same chipopens the secretchanged codeor another chipcannot open it
Change one byte of the code and the measurement changes, the key changes and the blob becomes garbage.

The life of an enclave

  1. Create. The application asks the processor for a protected memory region.
  2. Load and measure. The enclave code is copied in, page by page, and the processor keeps hashing everything that goes in.
  3. Initialize. The measurement is frozen. From here on nobody outside can change the code.
  4. Attest. Whoever needs to trust the enclave asks for the signed report and checks the measurement.
  5. Run. The application calls enclave functions, which process the secrets inside.
  6. Destroy. The processor wipes the region. Only what was sealed survives.

While running, code crosses the enclave boundary through well-defined gates. Entering the enclave is an ECALL; when the enclave needs something only the operating system does, such as reading a file or using the network, it goes out through an OCALL.

Regular applicationuntrustedtalks to the OS, network,diskEnclavetrustedvalidates everythingcrossing the boundaryboundaryECALL: entersOCALL: asks the OS
Everything that comes back from an OCALL came from outside and may be a lie. The enclave has to check it.

A concrete example

Imagine a transaction signing service that keeps a private key on a rented server:

  1. The service starts the enclave with the signing code, which is open source.
  2. The key owner asks for attestation, checks that the hash matches the published version of the code, and only then sends the private key, encrypted for the enclave.
  3. The enclave seals the key and stores the blob on disk.
  4. The regular application sends transactions through an ECALL; the enclave returns signatures.
  5. If the server reboots, the enclave opens the sealed blob and carries on. The key never shows up in plaintext outside it, not even to the server's administrator.

Implementations you will run into

TechnologyWhat it isolatesWhere it shows up
Intel SGXA piece of a processIntel servers, Azure
Intel TDXA whole virtual machineClouds with recent Xeons
AMD SEV-SNPA whole virtual machineAWS, Azure, GCP
Arm TrustZone / CCAA separate "secure world"Phones, embedded devices
Apple Secure EnclaveA dedicated coprocessoriPhone, Mac (Face ID, keys)
AWS Nitro EnclavesAn isolated VM with no network or diskAWS EC2

The umbrella name for all of this is TEE (Trusted Execution Environment), and in the cloud it usually shows up as confidential computing.

What enclaves do not solve

Quick glossary

TCB (Trusted Computing Base)
Everything you need to trust for the system to be secure. Enclaves exist to shrink the TCB.
Measurement
Hash of the code and configuration loaded into the enclave.
Attestation
Proof signed by the hardware that a specific measurement is running in a genuine enclave.
Nonce
Random number used only once, to prove the answer is fresh and not a recording.
Sealing
Encrypting data so that only the same enclave, on the same chip, can decrypt it later.
ECALL / OCALL
Calls that enter the enclave and leave it, respectively.

Next part: a policy engine inside an enclave →

The idea

A policy engine is the part of the system that answers a single question: "can whoever is asking do this to this resource?". It takes the request, looks at the rules written by the owner of the data and returns allow or deny.

The problem is the same as in the first part. If the policy engine runs on a server someone else controls, that person can swap the rules, forge an "allow" or wipe the record of what happened. With the policy engine inside an enclave, whoever operates the server still runs the service, but loses the power to tamper with the decisions.

The split of roles: the gateway sits outside the enclave and only enforces decisions (it is the PEP, Policy Enforcement Point). The policy engine sits inside and only decides (it is the PDP, Policy Decision Point). The data owner trusts the enclave, not the gateway.

Who talks to whom

rented server (untrusted)Gateway (PEP)only enforces the decisionEnclave: policy engine (PDP)decides and signs the decisionpolicies sealed on disk2. ECALL3. signeddecisionAgentor app1. requestResource4. request+ decision5. checks theenclave'ssignaturePolicy author0. attests, then sends encrypted policies
The gateway carries the decision, but cannot make one up: it does not have the key the resource expects to see in the signature.

Before the first request

The setup uses exactly the pieces from the first part:

  1. Attestation. The policy author asks for the enclave's report and checks that the measurement matches the audited version of the policy engine. The report carries the public signing key the enclave generated inside.
  2. Sending the policies. The policy bundle is signed by the author and encrypted for the enclave. The host forwards it, but cannot read or change it.
  3. Sealing. The enclave checks the author's signature, stores the bundle sealed on disk and records its version.
  4. Publishing the key. The protected resources now accept only decisions signed by the key that appeared in the attested report.

The rules

For the example, a small bundle with three rules. Deny always beats allow, and whatever no rule allows is forbidden.

deny never-export-pii:
  action = "export" and resource has the tag "pii"

allow read-own-team:
  action = "read" and resource.team = requester.team

default: deny

The flow of each request

Pick a request to see the path it takes inside the enclave:

No request picked: the flowchart shows every path.

Request enters via ECALLValid format?noDeny: malformedyesRequester'ssignature checks out?noDeny: fake identityyesOpens the sealed policiesDoes any denyrule match?yesDeny: deny rulenoDoes any allowrule match?noDeny by defaultyesAllowSigns: decision + request hash + policy versionWrites to the chained log and returns across the boundary
Everything shown here happens inside the enclave. Every path ends in a signed decision, denials included.

What goes into the decision

The answer is not just "yes" or "no". It ties the decision to the request and to the rules used, so nobody can reuse an old "allow" on another request:

decision:      allow
rule:          read-own-team
request:       sha256(ana · read · q3-report.pdf · nonce)
policies:      version 12 (sha256 of the bundle)
previous log:  sha256 of entry 4,311
signature:     enclave key (the same as in the attested report)

What the enclave guarantees here

What is still on you

Glossary for this part

Policy engine
Component that evaluates rules and decides whether a request is allowed.
PDP (Policy Decision Point)
Who decides. Here, the policy engine inside the enclave.
PEP (Policy Enforcement Point)
Who enforces the decision on the request's path. Here, the gateway.
Deny by default
Whatever no rule explicitly allows is forbidden.
Chained log
Record where each entry includes the hash of the previous one, so any change in the middle shows.

← Back to the concept