Keep Enclave Deployment
Deploy Keep on AWS Nitro Enclaves for hardware-isolated signing.
Architecture
┌─────────────────────────────────────────────────────────┐
│ AWS Nitro Enclave │
│ ┌───────────────────────────────────────────────────┐ │
│ │ keep-enclave binary │ │
│ │ • Private keys (memory only) │ │
│ │ • Policy engine (amount limits, rate limits) │ │
│ │ • FROST threshold signing │ │
│ │ • PSBT sighash computation │ │
│ └───────────────────────────────────────────────────┘ │
│ ▲ │
│ │ vsock │
│ ▼ │
│ ┌───────────────────────────────────────────────────┐ │
│ │ Host (EC2) │ │
│ │ • Routes requests to enclave │ │
│ │ • Verifies attestation │ │
│ │ • Never sees private keys │ │
│ └───────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌───────────────────────────────────────────────────┐ │
│ │ AWS KMS │ │
│ │ Key Policy: Decrypt only if PCR0/1/2 match │ │
│ └───────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
Keys live only in enclave memory. KMS decrypts only if PCRs match.
Prerequisites
- AWS account with EC2, KMS, IAM permissions
- AWS CLI configured
- Docker installed
- Nitro SDK binaries, see keep-enclave/build/README.md
Deployment Options
| Method | Best for | Complexity |
|---|---|---|
| Enclaver | Quick setup, simpler operations | Low |
| CDK (automated) | Production with full AWS integration | Medium |
| Manual | Learning, debugging, custom setups | Higher |
Using Enclaver
Enclaver simplifies enclave builds:
curl -sL https://github.com/edgebitio/enclaver/releases/latest/download/enclaver-linux-x86_64.tar.gz | tar xz
sudo mv enclaver /usr/local/bin/
docker build -f keep-enclave/build/Dockerfile.local -t keep-enclave:local .
enclaver build -f keep-enclave/enclaver.yaml
Deploy to EC2 (use CDK or Manual to provision an instance first):
docker save keep-enclave:enclave | gzip > keep-enclave.tar.gz
scp -i ~/.ssh/keep-enclave.pem keep-enclave.tar.gz ec2-user@<IP>:~/
# On EC2
docker load < keep-enclave.tar.gz
enclaver run keep-enclave:enclave
Still need KMS setup for key persistence.
Automated Deployment (CDK)
Create ECR repository and push enclave image first:
aws ecr create-repository --repository-name keep-enclave
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account>.dkr.ecr.us-east-1.amazonaws.com
docker build -f keep-enclave/build/Dockerfile.enclave -t keep-enclave:v1.0.0 .
docker tag keep-enclave:v1.0.0 <account>.dkr.ecr.us-east-1.amazonaws.com/keep-enclave:v1.0.0
docker push <account>.dkr.ecr.us-east-1.amazonaws.com/keep-enclave:v1.0.0
Deploy infrastructure:
cd deploy/cdk
python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
CDK_DEPLOY_ACCOUNT=<account> CDK_DEPLOY_REGION=us-east-1 cdk deploy -c image_tag=v1.0.0
Provisions VPC, ASG, NLB, KMS. Update KMS policy with PCRs after build.
Manual Deployment
IAM Role
aws iam create-role --role-name keep-enclave-role \
--assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
aws iam create-instance-profile --instance-profile-name keep-enclave-profile
aws iam add-role-to-instance-profile --instance-profile-name keep-enclave-profile --role-name keep-enclave-role
export EC2_ROLE_ARN=$(aws iam get-role --role-name keep-enclave-role --query 'Role.Arn' --output text)
SSH Key
aws ec2 create-key-pair --key-name keep-enclave --query 'KeyMaterial' --output text > ~/.ssh/keep-enclave.pem
chmod 400 ~/.ssh/keep-enclave.pem
Launch EC2
aws ec2 run-instances \
--image-id ami-0c02fb55956c7d316 \
--count 1 \
--instance-type m5.xlarge \
--enclave-options 'Enabled=true' \
--key-name keep-enclave \
--iam-instance-profile Name=keep-enclave-profile \
--block-device-mappings '[{"DeviceName":"/dev/xvda","Ebs":{"VolumeSize":20}}]' \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=keep-enclave}]'
Note: AMI ami-0c02fb55956c7d316 is for us-east-1. Find your region’s Amazon Linux 2 AMI via aws ssm get-parameter --name /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --query 'Parameter.Value' --output text.
Supported: m5.xlarge, m6i.xlarge, c5.xlarge, r5.xlarge (and 2xlarge variants).
Instance Setup
ssh -i ~/.ssh/keep-enclave.pem ec2-user@<INSTANCE_IP>
sudo amazon-linux-extras install aws-nitro-enclaves-cli -y
sudo yum install aws-nitro-enclaves-cli-devel docker -y
sudo usermod -aG ne,docker ec2-user
sudo tee /etc/nitro_enclaves/allocator.yaml <<< $'memory_mib: 512\ncpu_count: 2'
sudo systemctl enable --now nitro-enclaves-allocator docker
exit # Re-login to apply groups
Build Enclave
See keep-enclave/build/README.md. Produces keep-enclave.eif and pcrs.json.
PCRs (Platform Configuration Registers) are hashes of the enclave code. They change on rebuild. KMS uses them to restrict decryption to your exact code.
scp -i ~/.ssh/keep-enclave.pem keep-enclave/build/keep-enclave.eif ec2-user@<IP>:~/
KMS Setup
aws kms create-key --description "Keep Enclave" --tags TagKey=Project,TagValue=keep
export KMS_KEY_ID=<key-id-from-output>
aws kms create-alias --alias-name alias/keep-enclave --target-key-id $KMS_KEY_ID
Edit keep-enclave/build/kms-policy.json, replace all placeholders:
<EC2_INSTANCE_ROLE_ARN>: Role ARN from IAM setup (useecho $EC2_ROLE_ARN)<KMS_ADMIN_ROLE_ARN>: Your IAM user/role ARN for key administration<REGION>: AWS region (e.g.,us-east-1)<ACCOUNT_ID>: Your AWS account ID<KMS_KEY_ID>: Key ID from above<PCR0_VALUE>,<PCR1_VALUE>,<PCR2_VALUE>: Frompcrs.jsonafter build
aws kms put-key-policy --key-id $KMS_KEY_ID --policy-name default \
--policy file://keep-enclave/build/kms-policy.json
Run Enclave
nitro-cli run-enclave --eif-path ~/keep-enclave.eif --memory 512 --cpu-count 2
nitro-cli describe-enclaves # Note EnclaveCID (usually 16)
Debug mode (PCR0 = zeros, insecure):
nitro-cli run-enclave --eif-path ~/keep-enclave.eif --memory 512 --cpu-count 2 --debug-mode
nitro-cli console --enclave-id <id> # View logs
Stop: nitro-cli terminate-enclave --all
CLI Commands
keep enclave verify --cid 16 # Verify attestation
keep enclave generate-key --name mykey --cid 16 # Generate key in enclave
keep enclave import-key --name enclavekey --from-vault vaultkey --cid 16
keep enclave sign --key mykey --message <hex> --cid 16
Policy example (enforced inside enclave):
#![allow(unused)]
fn main() {
PolicyRule::MaxAmountSats(1_000_000) // 0.01 BTC limit
PolicyRule::MaxPerHour(10) // Rate limit
}
The amount, fee and address rules judge what the enclave reads from a PSBT it analyzes, never what the host says about a request. A raw Sign message and a FROST message are opaque to the enclave, so they fail MaxAmountSats, AddressAllowlist and AddressBlocklist, and a failed rule acts as it would for a PSBT that broke it: under a Deny policy the request is denied, under RequireApproval it is refused with “Requires approval”, and under Allow evaluation moves on to the next policy. A MaxFeeSats anywhere in the policy, whatever its action and however it is nested, refuses every raw message and FROST message, since they may pay any fee. The rate, AllowedHours and BlockWeekends rules judge them as they judge a PSBT. To spend under amount, fee or address rules, sign a PSBT with a SignPsbt request (EnclaveClient::sign_psbt). No request carries an event kind the enclave can read, so AllowedEventKinds fails every request, a PSBT included.
A FROST share is imported sealed with its policy, the same way as a wallet key: seal the serialized key package with EnvelopeEncryption::encrypt_for_enclave and import it with EnclaveClient::import_encrypted_frost_key, which sends the public key package alongside in the clear. The first sealed key imported, wallet or FROST, sets the enclave’s policy, and a key sealed to any other policy is refused. Both FROST rounds are held to that policy, and round 2 judges it again before making the share, so sessions opened together cannot all be signed past a rate limit; a session the policy refuses is closed. A FROST message is judged like a raw Sign message, as described above. Plaintext ImportFrostKey, like ImportKey, is refused unless the enclave is built with the test-only allow-plaintext-import feature.
A sealed policy can be changed only if the key was sealed with a warden: seal it with EnvelopeEncryption::encrypt_for_enclave_with_warden, naming the warden’s BIP340 public key, which starts the blob at epoch 0. To move the blob to a new policy, the warden signs keep_enclave_host::policy_update_message(&blob, &sealed_policy_hash(Some(&next), &policy)), where next is the warden at the blob’s epoch plus one, and the host sends the blob, the new policy and the signature with EnclaveClient::update_sealed_policy. The enclave checks the signature and the epoch, seals the key again to the new policy inside the enclave, and returns the new blob. The running enclave keeps its policy; the new one applies after a restart that imports the new blob. The epoch sits inside the sealed blob, so the warden has to track it, or find it by comparing the blob’s policy_hash with sealed_policy_hash at epochs 0, 1 and so on. The message hashes the whole blob it moves, so a signature cannot move any other, and sealing the same move again gives the same blob. A key sealed without a warden keeps its policy for good. This is not rollback protection: the enclave has no storage and KMS keeps no counter, so the old blob still opens to its old policy, and a host that kept it can load it after a restart. The loosest policy a warden ever signed for a key therefore bounds it, and loosening a policy cannot be taken back. The first blob loaded sets the policy for the whole enclave, so update every blob an enclave loads together.
Rate limits count each Sign or SignPsbt request a key signs (a PSBT counts once, however many inputs it signs) and each FROST signature share it makes, against the key’s public key (a FROST share’s group key) rather than the name it was imported under. The enclave keeps no count across restarts, so it cannot know what a key signed before it last started; it treats every rate limit as used up when a policy is loaded, which every launch needs before it can sign, and refills from there. A restart therefore buys nothing beyond the normal refill, at the cost of a wait after every launch: MaxPerHour(10) allows its first signature six minutes after the policy is loaded and its full ten an hour after. Each running enclave keeps its own count, so the limits bound one enclave, not every enclave that can unseal the key.
Local Development
Mock mode (no security, dev only):
keep enclave generate-key --name test --local
keep enclave sign --key test --message <hex> --local
QEMU emulation (vsock testing, no real attestation):
qemu-system-x86_64 -M nitro-enclave,vsock=parent -kernel keep-enclave.eif -m 512 -nographic --enable-kvm
Troubleshooting
| Error | Fix |
|---|---|
Enclave boot failed | Increase --memory |
No enclave support | Use Nitro-capable instance |
Resource busy | nitro-cli terminate-enclave --all |
AccessDeniedException | PCR mismatch: rebuild, update KMS policy |
| Vsock failed | Check nitro-cli describe-enclaves, verify CID |
Debug mode KMS: set PCR0 to 96 zeros in policy.
Security Notes
- PCRs change on rebuild; update KMS policy each time
- Debug mode sets PCR0=0; bypasses attestation; never in prod
- Keys lost on restart unless persisted via KMS envelope encryption
- Host can’t decrypt; KMS policy restricts to enclave with matching PCRs
- No network in enclave; host proxies KMS
Checklist
- Runs without
--debug-mode - Attestation passes
- KMS works with real PCRs
- Signing works
- Policy enforced
- Keys survive restart
- Wrong PCRs fail decrypt