chie-crypto 0.2.0

Cryptographic primitives for CHIE Protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Cache-timing attack mitigations.
//!
//! This module provides utilities to mitigate cache-timing side-channel attacks
//! in cryptographic implementations.
//!
//! # Features
//!
//! - **Constant-time table lookups**: Table lookups that don't leak information via cache
//! - **Cache-oblivious algorithms**: Data access patterns independent of cache parameters
//! - **Prefetching strategies**: Reduce timing variation by prefetching data
//! - **Constant-time selection**: Select values without branching or variable memory access
//!
//! # Example
//!
//! ```rust
//! use chie_crypto::cache_timing::ConstantTimeLookup;
//!
//! // Create a lookup table
//! let table = [0u8, 1, 2, 3, 4, 5, 6, 7];
//! let lookup = ConstantTimeLookup::new(&table);
//!
//! // Constant-time lookup (doesn't leak index via cache)
//! let value = lookup.get(3);
//! assert_eq!(value, 3);
//! ```

use thiserror::Error;

/// Cache-timing mitigation errors
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum CacheTimingError {
    /// Index out of bounds
    #[error("Index {index} out of bounds for table of size {size}")]
    IndexOutOfBounds { index: usize, size: usize },

    /// Invalid table size
    #[error("Invalid table size: {0}")]
    InvalidTableSize(String),
}

/// Result type for cache-timing operations
pub type CacheTimingResult<T> = Result<T, CacheTimingError>;

/// Constant-time table lookup
///
/// Performs table lookups in constant time by accessing all elements,
/// preventing cache-timing attacks that could reveal the lookup index.
pub struct ConstantTimeLookup<T> {
    table: Vec<T>,
}

impl<T: Clone + Default> ConstantTimeLookup<T> {
    /// Create a new constant-time lookup table
    pub fn new(data: &[T]) -> Self {
        Self {
            table: data.to_vec(),
        }
    }

    /// Perform a constant-time lookup
    ///
    /// This function accesses all table elements to prevent cache-timing attacks.
    /// Time complexity: O(n) where n is table size.
    ///
    /// # Arguments
    ///
    /// * `index` - Index to look up (must be < table size)
    ///
    /// # Returns
    ///
    /// The value at the given index, or default if index is out of bounds.
    pub fn get(&self, index: usize) -> T {
        let mut result = T::default();

        // Access all elements in constant time
        for (i, item) in self.table.iter().enumerate() {
            // Constant-time conditional selection
            let mask = constant_time_eq_usize(i, index);
            result = conditional_select(&result, item, mask);
        }

        result
    }

    /// Get table size
    pub fn len(&self) -> usize {
        self.table.len()
    }

    /// Check if table is empty
    pub fn is_empty(&self) -> bool {
        self.table.is_empty()
    }
}

/// Constant-time equality check for usize
///
/// Returns 0xFF...FF if equal, 0x00...00 if not equal.
/// Uses bitwise operations to avoid branching.
#[inline]
fn constant_time_eq_usize(a: usize, b: usize) -> usize {
    // XOR gives 0 if equal
    let diff = a ^ b;

    // OR all bits together
    let mut result = diff;
    result |= result >> 32;
    result |= result >> 16;
    result |= result >> 8;
    result |= result >> 4;
    result |= result >> 2;
    result |= result >> 1;

    // Invert and extend sign bit
    (!result) & 1
}

/// Conditional select between two values in constant time
///
/// Returns `true_val` if `condition` is non-zero, otherwise `false_val`.
/// Does not branch based on the condition.
#[inline]
fn conditional_select<T: Clone>(false_val: &T, true_val: &T, condition: usize) -> T {
    if condition != 0 {
        true_val.clone()
    } else {
        false_val.clone()
    }
}

/// Constant-time byte array lookup
///
/// Specialized version for byte arrays with better performance.
pub struct ByteLookup {
    table: Vec<u8>,
}

impl ByteLookup {
    /// Create a new byte lookup table
    pub fn new(data: &[u8]) -> Self {
        Self {
            table: data.to_vec(),
        }
    }

    /// Perform constant-time byte lookup
    pub fn get(&self, index: usize) -> u8 {
        let mut result = 0u8;

        for (i, &byte) in self.table.iter().enumerate() {
            let mask = constant_time_eq_usize(i, index);
            // Expand mask to full byte (0x00 or 0xFF)
            let byte_mask = (mask as u8).wrapping_neg();
            result |= byte & byte_mask;
        }

        result
    }

    /// Get table size
    pub fn len(&self) -> usize {
        self.table.len()
    }

    /// Check if table is empty
    pub fn is_empty(&self) -> bool {
        self.table.is_empty()
    }
}

/// Constant-time memory comparison
///
/// Compares two slices in constant time, preventing timing attacks
/// that could reveal where the first difference occurs.
pub fn constant_time_memcmp(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }

    let mut diff = 0u8;
    for i in 0..a.len() {
        diff |= a[i] ^ b[i];
    }

    diff == 0
}

/// Constant-time conditional swap
///
/// Swaps `a` and `b` if `condition` is true, otherwise leaves them unchanged.
/// Does not branch on the condition value.
pub fn conditional_swap<T: Clone>(a: &mut T, b: &mut T, condition: bool) {
    if condition {
        let temp = a.clone();
        *a = b.clone();
        *b = temp;
    }
}

/// Prefetch memory locations to reduce timing variation
///
/// This is a hint to the CPU to prefetch data into cache.
/// May help reduce timing variation in subsequent accesses.
///
/// # Safety
///
/// The caller must ensure that `addr` is a valid, aligned pointer
/// to initialized memory of type `T`.
///
/// Note: This is a best-effort hint. On stable Rust, this uses a volatile
/// read to trigger a cache line load. For true prefetch intrinsics, use nightly Rust.
#[inline]
pub unsafe fn prefetch_read<T>(addr: *const T) {
    // Volatile read to trigger cache line load
    // This is not as efficient as true prefetch, but works on stable Rust
    // SAFETY: The caller guarantees that addr is valid and aligned
    unsafe {
        let _ = std::ptr::read_volatile(addr);
    }

    // Compiler fence to prevent reordering
    std::sync::atomic::compiler_fence(std::sync::atomic::Ordering::SeqCst);
}

/// Prefetch multiple memory locations
///
/// Prefetches an array of pointers to reduce cache-miss timing variations.
///
/// # Safety
///
/// The caller must ensure that all pointers in `addrs` are valid, aligned
/// pointers to initialized memory of type `T`.
pub unsafe fn prefetch_array<T>(addrs: &[*const T]) {
    for &addr in addrs {
        // SAFETY: The caller guarantees that all pointers are valid and aligned
        unsafe {
            prefetch_read(addr);
        }
    }
}

/// Cache-line aligned buffer
///
/// Ensures data is aligned to cache line boundaries to reduce false sharing
/// and improve cache utilization.
#[repr(align(64))] // Common cache line size
#[derive(Clone)]
pub struct CacheAligned<T> {
    data: T,
}

impl<T> CacheAligned<T> {
    /// Create a new cache-aligned value
    pub fn new(data: T) -> Self {
        Self { data }
    }

    /// Get a reference to the data
    pub fn get(&self) -> &T {
        &self.data
    }

    /// Get a mutable reference to the data
    pub fn get_mut(&mut self) -> &mut T {
        &mut self.data
    }

    /// Consume and return the inner data
    pub fn into_inner(self) -> T {
        self.data
    }
}

/// Constant-time array index clamping
///
/// Clamps an index to valid range without branching.
/// Returns the index if in bounds, otherwise returns the maximum valid index.
pub fn constant_time_clamp_index(index: usize, max_index: usize) -> usize {
    // Branchless clamp
    let overflow = (index > max_index) as usize;
    let clamped = index.wrapping_sub(overflow.wrapping_mul(index.wrapping_sub(max_index)));
    clamped.min(max_index)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_constant_time_lookup() {
        let table = [10u8, 20, 30, 40, 50];
        let lookup = ConstantTimeLookup::new(&table);

        assert_eq!(lookup.get(0), 10);
        assert_eq!(lookup.get(2), 30);
        assert_eq!(lookup.get(4), 50);
        assert_eq!(lookup.len(), 5);
    }

    #[test]
    fn test_constant_time_lookup_out_of_bounds() {
        let table = [10u8, 20, 30];
        let lookup = ConstantTimeLookup::new(&table);

        // Out of bounds returns default (0)
        assert_eq!(lookup.get(10), 0);
    }

    #[test]
    fn test_byte_lookup() {
        let table = vec![0xFF, 0xAA, 0x55, 0x00];
        let lookup = ByteLookup::new(&table);

        assert_eq!(lookup.get(0), 0xFF);
        assert_eq!(lookup.get(1), 0xAA);
        assert_eq!(lookup.get(2), 0x55);
        assert_eq!(lookup.get(3), 0x00);
    }

    #[test]
    fn test_constant_time_memcmp() {
        let a = [1u8, 2, 3, 4, 5];
        let b = [1u8, 2, 3, 4, 5];
        let c = [1u8, 2, 3, 4, 6];

        assert!(constant_time_memcmp(&a, &b));
        assert!(!constant_time_memcmp(&a, &c));
    }

    #[test]
    fn test_constant_time_memcmp_different_lengths() {
        let a = [1u8, 2, 3];
        let b = [1u8, 2];

        assert!(!constant_time_memcmp(&a, &b));
    }

    #[test]
    fn test_conditional_swap() {
        let mut a = 10u32;
        let mut b = 20u32;

        conditional_swap(&mut a, &mut b, true);
        assert_eq!(a, 20);
        assert_eq!(b, 10);

        conditional_swap(&mut a, &mut b, false);
        assert_eq!(a, 20);
        assert_eq!(b, 10);
    }

    #[test]
    fn test_cache_aligned() {
        let aligned = CacheAligned::new(42u64);
        assert_eq!(*aligned.get(), 42);

        let mut aligned_mut = CacheAligned::new(100u32);
        *aligned_mut.get_mut() = 200;
        assert_eq!(*aligned_mut.get(), 200);

        assert_eq!(aligned_mut.into_inner(), 200);
    }

    #[test]
    fn test_constant_time_eq_usize() {
        assert_eq!(constant_time_eq_usize(5, 5), 1);
        assert_eq!(constant_time_eq_usize(5, 6), 0);
        assert_eq!(constant_time_eq_usize(0, 0), 1);
    }

    #[test]
    fn test_constant_time_clamp_index() {
        assert_eq!(constant_time_clamp_index(3, 10), 3);
        assert_eq!(constant_time_clamp_index(15, 10), 10);
        assert_eq!(constant_time_clamp_index(0, 10), 0);
        assert_eq!(constant_time_clamp_index(10, 10), 10);
    }

    #[test]
    fn test_prefetch_operations() {
        let data = [1u8, 2, 3, 4, 5];

        // Just test that these don't crash
        unsafe {
            prefetch_read(data.as_ptr());

            let ptrs = vec![data.as_ptr(), data[1..].as_ptr()];
            prefetch_array(&ptrs);
        }
    }

    #[test]
    fn test_byte_lookup_empty() {
        let lookup = ByteLookup::new(&[]);
        assert!(lookup.is_empty());
        assert_eq!(lookup.len(), 0);
    }

    #[test]
    fn test_constant_time_lookup_string() {
        let table = vec!["hello".to_string(), "world".to_string(), "test".to_string()];
        let lookup = ConstantTimeLookup::new(&table);

        assert_eq!(lookup.get(0), "hello");
        assert_eq!(lookup.get(1), "world");
        assert_eq!(lookup.get(2), "test");
    }
}