Security Best Practices for MetaMUI Crypto Primitives

Side-channel claim limit. Nothing on this page claims constant-time behaviour or side-channel resistance for any target: the side-channel harness covers nothing (finding A48) and no measurement exists. The Rust and C cores are designed for constant time and unmeasured; managed runtimes (JIT, GC, bounds checks) make no timing claim. Where this page names constant-time or side-channel techniques, in prose or in code, they are design intent or recommendations, not measured properties.

Overview

This document describes common practices against timing attacks, other side-channel attacks and memory-based vulnerabilities, for anyone writing or reviewing code that handles secrets. It does not say which of them each MetaMUI target applies. Constant-time behaviour and side-channel resistance are not claimed for any MetaMUI target: none has been measured (finding A48).

Constant-Time Operations

Principle

All cryptographic operations must execute in constant time regardless of input values to prevent timing attacks.

Implementation Guidelines

  1. Conditional Operations
    • Never use data-dependent branches in cryptographic code
    • Use constant-time selection functions instead of if/else statements
    • Example (Rust): ```rust // BAD: Timing leak if secret_bit == 1 { result = a; } else { result = b; }

    // BETTER: no branch on the secret (measure timing on your target) use subtle::ConditionallySelectable; result = u32::conditional_select(&a, &b, secret_bit.into()); ```

  2. Comparisons
    • Never use standard equality operators for secret data
    • Use constant-time comparison functions
    • Example (Python): ```python

      BAD: Timing leak

      if secret_key == expected_key: return True

    BETTER: the stdlib’s comparison for secrets (timing not measured here)

    import hmac return hmac.compare_digest(secret_key, expected_key) ```

  3. Array Access
    • Avoid secret-dependent array indexing
    • Use masking or conditional selection for lookups
    • Example (TypeScript): ```typescript // BAD: Cache timing leak result = lookup_table[secret_index];

    // BETTER: linear scan, no secret-dependent index // (a JIT may still introduce timing differences; measure on your target) let result = 0; for (let i = 0; i < table.length; i++) { const mask = constantTimeEq(i, secret_index) ? 0xFF : 0x00; result |= table[i] & mask; } ```

Memory Security

Secure Memory Handling

  1. Zeroing Sensitive Data
    • Always clear sensitive data from memory after use
    • Use secure zeroing functions that won’t be optimized away
    • Examples: ```rust // Rust use zeroize::Zeroize; secret_key.zeroize();

    // Python import ctypes ctypes.memset(ctypes.addressof(ctypes.c_char.from_buffer(secret)), 0, len(secret))

    // TypeScript crypto.getRandomValues(secret); // Overwrite with random data secret.fill(0); ```

  2. Memory Allocation
    • Use secure allocators when available
    • Lock memory pages to prevent swapping (platform-specific)
    • Minimize copies of sensitive data
  3. Stack Cleaning
    • Clear local variables containing secrets
    • Be aware of compiler optimizations that might leave copies

Input Validation

Comprehensive Validation

  1. Size Validation
    pub fn validate_key(key: &[u8], expected_size: usize) -> Result<()> {
        if key.len() != expected_size {
            return Err(SecurityError::InvalidKeySize {
                expected: expected_size,
                actual: key.len(),
            });
        }
        Ok(())
    }
    
  2. Range Validation
    • Check all inputs are within valid ranges
    • Validate polynomial coefficients in lattice-based crypto
    • Ensure modular arithmetic inputs are reduced
  3. Format Validation
    • Verify data structures are well-formed
    • Check for canonical representations
    • Validate encoding formats (base64, hex, etc.)

Side-Channel Protection

Generic countermeasures, listed as advice. This page does not state that any MetaMUI target implements them, and none has been measured (A48).

Power Analysis Protection

  1. Masking
    • Split sensitive values into random shares
    • Process shares independently
    • Recombine only when necessary
  2. Blinding
    • Add random factors to computations
    • Remove factors at the end
    • Particularly important for RSA and ECC
  3. Dummy Operations
    • Add noise to power traces
    • Execute dummy operations randomly
    • Balance Hamming weights

Cache Attack Protection

  1. Cache-Oblivious Algorithms
    • Avoid secret-dependent memory access patterns
    • Use linear scans instead of indexed lookups
    • Prefetch data to normalize cache behavior
  2. Cache Flushing
    • Clear caches after sensitive operations
    • Use memory barriers appropriately
    • Consider cache-line alignment

Implementation-Specific Guidelines

Rust

Python

TypeScript/JavaScript

Swift

Kotlin

Testing Security Properties

Timing Analysis

  1. Measure execution time with different inputs
  2. Statistical analysis to detect variations
  3. Use tools like dudect for timing leak detection

Memory Analysis

  1. Use memory sanitizers (ASan, MSan)
  2. Check for uninitialized memory reads
  3. Verify proper cleanup with debugging tools

Fuzzing

  1. Use coverage-guided fuzzing
  2. Target edge cases and error paths
  3. Include differential fuzzing between implementations

Security Checklist

Before releasing any cryptographic code (for MetaMUI the side-channel and timing items are open: no target has been measured, A48):

References

  1. BearSSL Constant-Time Crypto
  2. Cryptography Coding Standard
  3. NIST Post-Quantum Cryptography
  4. Side-Channel Analysis Resistant Implementation
  5. Timing Attacks on Implementations