Acceleration Security API Documentation

⚠️ CRITICAL HARDWARE ACCELERATION MODULE ⚠️

THIS MODULE MANAGES HARDWARE CRYPTOGRAPHIC ACCELERATION

1. Core Acceleration Interfaces

1.1 Hardware Capability Detection

detect_hardware_features() -> HardwareFeatures

Security Contract:

Feature Detection:

pub struct HardwareFeatures {
    pub aes_ni: bool,           // AES New Instructions
    pub sha_ext: bool,          // SHA Extensions
    pub avx2: bool,             // Advanced Vector Extensions 2
    pub avx512: bool,           // AVX-512
    pub rdrand: bool,           // Hardware RNG
    pub rdseed: bool,           // Hardware seed RNG
    pub sgx: bool,              // Intel SGX
    pub constant_time_ops: bool, // Hardware constant-time support
}

validate_acceleration_safety(feature: Feature) -> Result<()>

Security Contract:

1.2 Accelerated Operations

accelerated_aes_encrypt(key: &[u8], plaintext: &[u8], mode: AesMode) -> Result<Vec<u8>>

Security Contract:

Critical Requirements:

accelerated_sha256(data: &[u8]) -> [u8; 32]

Security Contract:

1.3 Side-Channel Protection

enable_side_channel_protections() -> Result<()>

Security Contract:

pub struct SideChannelProtections {
    pub disable_smt: bool,
    pub cache_flush: bool,
    pub speculation_barrier: bool,
    pub power_analysis_resist: bool,
}

2. Platform-Specific Implementations

2.1 x86/x64 Acceleration

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub mod x86_acceleration {
    use core::arch::x86_64::*;
    
    pub unsafe fn aes_encrypt_block_ni(
        key_schedule: &[__m128i],
        block: __m128i,
    ) -> __m128i {
        let mut state = block;
        
        // Initial round
        state = _mm_xor_si128(state, key_schedule[0]);
        
        // Main rounds
        for i in 1..10 {
            state = _mm_aesenc_si128(state, key_schedule[i]);
        }
        
        // Final round
        state = _mm_aesenclast_si128(state, key_schedule[10]);
        
        // Critical: Clear registers
        _mm_setzero_si128();
        
        state
    }
    
    pub fn is_aes_ni_safe() -> bool {
        // Check for known vulnerable implementations
        let cpuid = CpuId::new();
        
        if let Some(info) = cpuid.get_processor_info() {
            let family = info.family_id();
            let model = info.model_id();
            let stepping = info.stepping_id();
            
            // Blacklist known vulnerable CPUs
            match (family, model) {
                (0x06, 0x3C) if stepping < 3 => false, // Haswell early stepping
                _ => true,
            }
        } else {
            false
        }
    }
}

2.2 ARM Acceleration

#[cfg(target_arch = "aarch64")]
pub mod arm_acceleration {
    use core::arch::aarch64::*;
    
    pub unsafe fn sha256_round_arm(
        state: uint32x4_t,
        msg: uint32x4_t,
        k: uint32x4_t,
    ) -> uint32x4_t {
        // SHA256 round using ARM crypto extensions
        let tmp = vaddq_u32(msg, k);
        vsha256hq_u32(state, tmp)
    }
    
    pub fn has_crypto_extensions() -> bool {
        // Runtime detection of ARM crypto extensions
        std::arch::is_aarch64_feature_detected!("sha2") &&
        std::arch::is_aarch64_feature_detected!("aes")
    }
}

3. Security Vulnerability Mitigations

3.1 Spectre/Meltdown Protection

pub fn speculation_barrier() {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    unsafe {
        core::arch::x86_64::_mm_mfence();
        core::arch::x86_64::_mm_lfence();
    }
    
    #[cfg(target_arch = "aarch64")]
    unsafe {
        core::arch::aarch64::__dsb(core::arch::aarch64::DSB_SY);
        core::arch::aarch64::__isb(core::arch::aarch64::ISB_SY);
    }
}

pub fn flush_cache_line(addr: *const u8) {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    unsafe {
        core::arch::x86_64::_mm_clflush(addr);
    }
}

3.2 Power Analysis Protection

class PowerAnalysisProtection:
    @staticmethod
    def add_noise_operations():
        """Add dummy operations to mask power consumption."""
        import random
        
        # Random dummy calculations
        dummy = random.randint(1, 100)
        for _ in range(dummy):
            _ = random.randint(0, 255) ^ random.randint(0, 255)
    
    @staticmethod
    def randomize_operation_order(operations: list):
        """Randomize independent operations to prevent SPA."""
        import random
        
        # Group independent operations
        independent_groups = PowerAnalysisProtection._find_independent_ops(operations)
        
        # Shuffle within groups
        for group in independent_groups:
            random.shuffle(group)
        
        return operations

4. Attack Resistance Matrix

Attack Type Hardware Acceleration Risk Mitigation
Timing Attacks Medium - Hardware may leak timing Use constant-time fallbacks
Power Analysis High - Direct hardware access Add noise, randomize operations
Cache Attacks High - Shared caches Flush caches, disable SMT
Speculative Execution High - CPU vulnerabilities Speculation barriers
Fault Injection Medium - Hardware faults Redundancy, integrity checks
Microarchitectural High - Side channels Isolate critical operations

5. Safe Acceleration Patterns

5.1 Fallback Strategy

pub fn aes_encrypt_with_fallback(
    key: &[u8],
    data: &[u8],
) -> Result<Vec<u8>> {
    if acceleration_available() && acceleration_safe() {
        // Use hardware acceleration
        accelerated_aes_encrypt(key, data)
    } else {
        // Fall back to software implementation
        software_aes_encrypt(key, data)
    }
}

5.2 Side-Channel Safe Usage

def side_channel_safe_operation(sensitive_data: bytes) -> bytes:
    """Perform operation with side-channel protections."""
    # Disable SMT
    original_affinity = os.sched_getaffinity(0)
    os.sched_setaffinity(0, {0})  # Pin to single core
    
    try:
        # Pre-operation cache flush
        flush_all_caches()
        
        # Add speculation barriers
        speculation_barrier()
        
        # Perform operation
        result = accelerated_operation(sensitive_data)
        
        # Post-operation cleanup
        clear_hardware_state()
        flush_all_caches()
        
        return result
    finally:
        # Restore affinity
        os.sched_setaffinity(0, original_affinity)

6. Performance vs Security Trade-offs

Feature Performance Gain Security Impact Recommendation
AES-NI 5-10x Low risk if properly used Use with register clearing
SHA Extensions 3-5x Low risk Use with state clearing
AVX-512 2-4x High side-channel risk Avoid for secrets
GPU Acceleration 10-100x Very high risk Never for secret data
Parallel Processing 2-8x Medium risk Isolate threads

7. Implementation Examples

Secure Python Wrapper

import ctypes
import platform

class SecureAcceleration:
    def __init__(self):
        self.features = self._detect_features()
        self._load_native_lib()
    
    def _detect_features(self) -> dict:
        """Safely detect hardware features."""
        features = {
            'aes_ni': False,
            'sha_ext': False,
            'avx2': False,
            'safe_for_crypto': False
        }
        
        try:
            # Platform-specific detection
            if platform.machine() in ['x86_64', 'AMD64']:
                import cpuinfo
                info = cpuinfo.get_cpu_info()
                
                features['aes_ni'] = 'aes' in info.get('flags', [])
                features['sha_ext'] = 'sha_ni' in info.get('flags', [])
                features['avx2'] = 'avx2' in info.get('flags', [])
                
                # Check safety
                features['safe_for_crypto'] = self._check_cpu_safety(info)
        except Exception:
            # Fail closed - no acceleration if detection fails
            pass
        
        return features
    
    def _check_cpu_safety(self, cpu_info: dict) -> bool:
        """Check for known vulnerable CPUs."""
        # Simplified check - expand based on security advisories
        vendor = cpu_info.get('vendor_id_raw', '').lower()
        family = cpu_info.get('family', 0)
        model = cpu_info.get('model', 0)
        
        # Blacklist known vulnerable combinations
        vulnerable_cpus = [
            ('genuineintel', 6, 60),  # Example vulnerable CPU
        ]
        
        return (vendor, family, model) not in vulnerable_cpus

Secure Rust Acceleration Manager

use std::sync::Once;

pub struct AccelerationManager {
    features: HardwareFeatures,
    safety_verified: bool,
}

impl AccelerationManager {
    pub fn new() -> Self {
        static INIT: Once = Once::new();
        let mut features = HardwareFeatures::default();
        let mut safety_verified = false;
        
        INIT.call_once(|| {
            features = Self::detect_features();
            safety_verified = Self::verify_safety(&features);
        });
        
        Self { features, safety_verified }
    }
    
    fn detect_features() -> HardwareFeatures {
        HardwareFeatures {
            aes_ni: is_x86_feature_detected!("aes"),
            sha_ext: is_x86_feature_detected!("sha"),
            avx2: is_x86_feature_detected!("avx2"),
            rdrand: is_x86_feature_detected!("rdrand"),
            constant_time_ops: Self::has_constant_time_support(),
            ..Default::default()
        }
    }
    
    fn verify_safety(features: &HardwareFeatures) -> bool {
        // Verify no known vulnerabilities
        if features.aes_ni && !Self::is_aes_ni_safe() {
            return false;
        }
        
        // Run self-tests
        if !Self::run_acceleration_self_tests() {
            return false;
        }
        
        true
    }
    
    pub fn use_acceleration(&self) -> bool {
        self.safety_verified && 
        !cfg!(feature = "force_software_only")
    }
}

8. Security Checklist

9. Debugging and Monitoring

#[cfg(debug_assertions)]
pub mod acceleration_debug {
    pub fn verify_state_cleared() {
        // Check that sensitive registers are cleared
        #[cfg(target_arch = "x86_64")]
        unsafe {
            let xmm0: u128 = std::mem::transmute(_mm_setzero_si128());
            assert_eq!(xmm0, 0, "XMM0 not cleared!");
        }
    }
    
    pub fn log_acceleration_usage(operation: &str, accelerated: bool) {
        eprintln!("[CRYPTO] {} using {} implementation", 
            operation, 
            if accelerated { "hardware" } else { "software" }
        );
    }
}

10. References

Security Analysis

Threat Model: Hardware Acceleration Threat Model

This component provides critical security infrastructure. For comprehensive security analysis including:

See the dedicated threat model documentation for complete details.