Skip to main content
โšก Calmops

Confidential Computing: Building Secure Enclaves and Privacy-First Systems

Introduction

Confidential computing represents a paradigm shift in data securityโ€”protecting data while it’s being processed, not just at rest or in transit. This comprehensive guide covers trusted execution environments, secure enclaves, homomorphic encryption, and building applications that protect sensitive data during computation.

What is Confidential Computing?

The Three States of Data

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  Three States of Data Protection                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚   At Rest   โ”‚ โ”€โ”€โ–ถ โ”‚  In Transit โ”‚ โ”€โ”€โ–ถ โ”‚ In Use      โ”‚  โ”‚
โ”‚  โ”‚  (Storage)  โ”‚      โ”‚  (Network)  โ”‚      โ”‚ (Memory)   โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                                                             โ”‚
โ”‚  Solutions:             Solutions:            Solutions:        โ”‚
โ”‚  โ€ข Encryption          โ€ข TLS               โ€ข TEEs            โ”‚
โ”‚  โ€ข Access Control      โ€ข mTLS             โ€ข HE              โ”‚
โ”‚  โ€ข Key Management      โ€ข Certificates     โ€ข Secure Enclaves โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Trusted Execution Environments (TEEs)

How TEEs Work

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   TEE Architecture                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                             โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚   โ”‚              Trusted Execution Environment         โ”‚     โ”‚
โ”‚   โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚     โ”‚
โ”‚   โ”‚  โ”‚         Enclave (Protected Memory)       โ”‚   โ”‚     โ”‚
โ”‚   โ”‚  โ”‚                                          โ”‚   โ”‚     โ”‚
โ”‚   โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”            โ”‚   โ”‚     โ”‚
โ”‚   โ”‚  โ”‚  โ”‚  Code   โ”‚    โ”‚  Data   โ”‚            โ”‚   โ”‚     โ”‚
โ”‚   โ”‚  โ”‚  โ”‚         โ”‚    โ”‚         โ”‚            โ”‚   โ”‚     โ”‚
โ”‚   โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜            โ”‚   โ”‚     โ”‚
โ”‚   โ”‚  โ”‚                                          โ”‚   โ”‚     โ”‚
โ”‚   โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚     โ”‚
โ”‚   โ”‚                                                   โ”‚     โ”‚
โ”‚   โ”‚  Attestation: Verify code authenticity          โ”‚     โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                           โ”‚                                 โ”‚
โ”‚   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚   โ”‚              Untrusted World                      โ”‚     โ”‚
โ”‚   โ”‚  OS, Hypervisor, Other Applications            โ”‚     โ”‚
โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Intel SGX

Programming with SGX

// SGX enclave code
#include <sgx_enclave_common.h>
#include <sgx_urts.h>

// Enclave function
public void process_secure_data(
    uint8_t* input_data,
    size_t input_size,
    uint8_t* output_data,
    size_t* output_size
) {
    // This code runs in protected enclave
    // Cannot be accessed by OS, hypervisor, or other software
    
    // Process sensitive data
    encrypt_data(input_data, input_size, output_data, output_size);
}

// ECALL - Call from untrusted to trusted
sgx_status_t ecall_process_data(
    sgx_enclave_id_t eid,
    uint8_t* input_data,
    size_t input_size,
    uint8_t* output_data,
    size_t* output_size
) {
    // Create enclave instance
    sgx_launch_token_t token = {0};
    int updated = 0;
    
    sgx_create_enclave(
        ENCLAVE_PATH,
        SGX_DEBUG_FLAG,
        &token,
        &updated,
        &eid,
        NULL
    );
    
    // Process in enclave
    process_secure_data(input_data, input_size, output_data, output_size);
    
    return SGX_SUCCESS;
}

Remote Attestation

# Remote attestation verification
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

class AttestationService:
    """Verify enclave attestation."""
    
    def __init__(self, enclave_public_key):
        self.enclave_key = enclave_public_key
    
    def verify_attestation(self, quote: bytes, expected_data: dict) -> bool:
        """Verify SGX quote from enclave."""
        
        # Verify quote structure
        if not self.verify_quote_structure(quote):
            return False
        
        # Verify MRENCLAVE (measurement)
        mr_enclave = self.extract_mrenclave(quote)
        if mr_enclave != expected_data["expected_enclave"]:
            return False
        
        # Verify REPORTDATA
        report_data = self.extract_report_data(quote)
        expected = self.create_expected_report(expected_data)
        if report_data != expected:
            return False
        
        # Verify signature with Intel PKI
        return self.verify_intel_signature(quote)

AMD SEV

SEV-SNP Features

# AMD SEV-SNP configuration
sev_configuration:
  # Secure Nested Paging
  snp_enabled: true
  
  # Memory Encryption
  encryption:
    key_bits: 256
    key_lifetime: "infinite"
  
  # Confidential VM
  cvm:
    cpu_type: "AMD EPYC"
    memory_encryption: true
    secure_nested_paging: true
    
    # Launch configuration
    launch:
      # Measure kernel/initrd
      measure_kernel: true
      measure_initrd: true
      
      # Policy
      policy:
        # Allow debugging with authorized key
        debug_with_key: false
        
        # Secrets injection
        secrets_injection: false
        
        # Migration
        migration_agent: false

Arm TrustZone

TrustZone Implementation

// Secure world (TEE) code
#include <tee_internal_api.h>

TEE_Result process_secure_payment(
    TEE_Param params[4]
) {
    // Input parameters
    void* encrypted_card = params[0].memref.buffer;
    size_t card_size = params[0].memref.size;
    
    // Decrypt in secure world
    uint8_t decrypted[256];
    TEE_CipherInit(
        TEE_ALG_AES_GCM,
        TEE_MODE_DECRYPT,
        &key_handle,
        NULL,  // IV
        0       // IV size
    );
    
    TEE_CipherDoFinal(
        &key_handle,
        encrypted_card,
        card_size,
        decrypted,
        &out_size
    );
    
    // Process payment securely
    TEE_Result result = process_transaction(decrypted);
    
    // Return result
    params[1].memref.buffer = &result;
    params[1].memref.size = sizeof(result);
    
    return TEE_SUCCESS;
}

// Normal world (REE) call
TEE_Result process_payment(uint8_t* card_data, size_t size) {
    TEEC_Result result;
    TEEC_Session session;
    
    // Open session to Trusted Application
    TEEC_InvokeCommand(
        &session,
        CMD_PROCESS_PAYMENT,
        &operation,
        NULL
    );
    
    return result;
}

Confidential Containers

Kubernetes Confidential Pods

# Confidential pod configuration
apiVersion: v1
kind: Pod
metadata:
  name: confidential-app
spec:
  securityContext:
    # Enable confidential computing
    confidentialComputing:
      mode: SEV-ES
    
  containers:
    - name: app
      image: confidential-app:latest
      
      securityContext:
        # Run inside enclave
        seccompProfile:
          type: RuntimeDefault
        
      resources:
        limits:
          # Confidential memory
          confidential-memory: "2Gi"
    
  # Attestation
  attestation:
    enabled: true
    service: "attestation-service:8080"

Confidential Container Runtime

# Kata Containers with SEV
# /etc/kata-containers/config.toml
[hypervisor.sev]
path = "/usr/bin/qemu-system-x86_64"
kernel = "/usr/share/kata-containers/vmlinuz.container"

[hypervisor.sev]
# SEV configuration
sev_enabled = true
sev_es_enabled = true
kernel_hash = "sha256:..."

# Launch template
[hypervisor.sev.launch_template]
# Encrypted memory
memory_encryption = true

Homomorphic Encryption

Working with Encrypted Data

# Microsoft SEAL - Homomorphic encryption
import seal

class HomomorphicEncryptor:
    """Work with data without decrypting."""
    
    def __init__(self):
        # Create encryption parameters
        parms = seal.EncryptionParameters(scheme.seal_scheme.BFV)
        
        poly_modulus_degree = 4096
        parms.set_poly_modulus_degree(poly_modulus_degree)
        
        # Set coefficient modulus
        parms.set_coeff_modulus(
            seal.coeff_modulus_128(poly_modulus_degree)
        )
        
        # Create context
        self.context = seal.SEALContext.Create(parms)
        
        # Generate keys
        self.keygen = seal.KeyGenerator(self.context)
        self.public_key = self.keygen.public_key()
        self.secret_key = self.keygen.secret_key()
        
        # Create encryptor/evaluator
        self.encryptor = seal.Encryptor(self.context, self.public_key)
        self.evaluator = seal.Evaluator(self.context)
        self.encoder = seal.IntegerEncoder(self.context)
    
    def encrypt(self, value: int):
        """Encrypt value without knowing it."""
        plain = self.encoder.encode(value)
        return self.encryptor.encrypt(plain)
    
    def compute_add(self, encrypted_a, encrypted_b):
        """Add two encrypted numbers."""
        return self.evaluator.add(encrypted_a, encrypted_b)
    
    def compute_multiply(self, encrypted_a, encrypted_b):
        """Multiply two encrypted numbers."""
        return self.evaluator.multiply(encrypted_a, encrypted_b)
    
    def decrypt(self, encrypted_result):
        """Decrypt final result."""
        plain = self.encryptor.decrypt(encrypted_result)
        return self.encoder.decode_int32(plain)

Practical Application

# Privacy-preserving analytics
class PrivacyPreservingAnalytics:
    """Analytics on encrypted data."""
    
    def __init__(self):
        self.encryptor = HomomorphicEncryptor()
    
    def sum_encrypted_values(self, encrypted_values: list):
        """Sum encrypted values without decryption."""
        result = encrypted_values[0]
        
        for value in encrypted_values[1:]:
            result = self.encryptor.compute_add(result, value)
        
        return self.encryptor.decrypt(result)
    
    def average_encrypted_values(self, encrypted_values: list):
        """Calculate average of encrypted values."""
        total = self.sum_encrypted_values(encrypted_values)
        
        # Divide by count (still encrypted)
        divisor = self.encryptor.encrypt(len(encrypted_values))
        average = self.encryptor.compute_multiply(total, divisor)
        
        return self.encryptor.decrypt(average)

Use Cases

Healthcare

# Medical research on encrypted patient data
class MedicalResearchPlatform:
    """Process encrypted medical records."""
    
    def train_model(self, encrypted_records):
        """Train ML model without exposing patient data."""
        # Model training happens on encrypted data
        # Only encrypted weights are returned
        return self.model.fit_encrypted(encrypted_records)
    
    def predict(self, encrypted_patient_data, model):
        """Make predictions on encrypted data."""
        return self.model.predict_encrypted(encrypted_patient_data)

Financial Services

# Fraud detection on encrypted transactions
class SecureFraudDetection:
    """Detect fraud without seeing transaction details."""
    
    def detect_fraud(self, encrypted_transactions):
        """Analyze transactions in encrypted domain."""
        # Calculate features on encrypted data
        features = self.extract_features(encrypted_transactions)
        
        # Run fraud detection
        return self.fraud_model.predict(features)

Best Practices

1. Minimize TCB

# Keep trusted computing base small
tcb_minimization = {
    "principle": "Only essential code in enclave",
    "practices": [
        "Minimal API surface",
        "No external dependencies in enclave",
        "Verified boot chain",
        "Remote attestation required"
    ]
}

2. Key Management

# Secure key handling
secure_key_management = {
    "derivation": "Derive keys from attestation",
    "rotation": "Automated key rotation",
    "storage": "Hardware security module",
    "backup": "Encrypted backups with separate keys"
}

3. Attestation

# Comprehensive attestation
attestation_checklist = [
    "Verify enclave measurement (MRENCLAVE)",
    "Verify code signing identity",
    "Verify platform state (TEE initialized correctly)",
    "Verify secure boot state",
    "Verify OS/hypervisor configuration"
]

Conclusion

Confidential computing enables new security paradigms. Key takeaways:

  • TEEs provide protection: Hardware-backed isolation
  • Encrypt data in use: Process sensitive information securely
  • Attestation ensures trust: Verify code authenticity
  • Multiple technologies: SGX, SEV, TrustZone serve different needs

As privacy requirements grow, confidential computing becomes essential for sensitive applications.

Comments