SPB Git

Repository Public Apache-2.0

mod.rs 4.5 KB 137 lines rust

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SPB Quantum Crypto7// File             : crates/spb-pqc-core/src/ct/mod.rs8// Module           : Crates9// Repository       : https://git.spboucher.ai/spb-quantum-crypto10// Project Website  : https://pqce.spboucher.ai11// Created          : 2026-12-0312// Last Modified    : 2026-12-0313//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : Apache-2.033//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041//! Constant-time helpers and memory wiping.42//!43//! Every function here is written so that its control flow and memory access44//! pattern do not depend on the *values* of its inputs (only on their public45//! lengths). They are best-effort at the source level: the compiler is free to46//! transform the code, which is why `docs/security/constant-time.md` records47//! the disassembly review and the statistical timing tests (dudect style) that48//! complement this module. No absolute absence of leakage is claimed.4950pub mod wipe;5152pub use wipe::{wipe, Secret};5354use core::hint::black_box;5556/// Returns `0xFF` when `a == b` and `0x00` otherwise, in time that depends only57/// on the lengths. Slices of different lengths compare unequal.58#[inline(never)]59pub fn eq_mask(a: &[u8], b: &[u8]) -> u8 {60    if a.len() != b.len() {61        return 0;62    }63    let mut acc: u8 = 0;64    for i in 0..a.len() {65        acc |= a[i] ^ b[i];66    }67    // acc == 0  <=>  equal.  Map 0 -> 0xFF, non-zero -> 0x00 without branching.68    let acc = black_box(acc) as u16;69    let nz = ((acc | acc.wrapping_neg()) >> 15) as u8; // 1 if acc != 070    nz.wrapping_sub(1)71}7273/// Constant-time equality as a `bool` (the final conversion is public by design).74#[inline]75pub fn eq(a: &[u8], b: &[u8]) -> bool {76    eq_mask(a, b) == 0xFF77}7879/// `out[i] = if mask == 0xFF { a[i] } else { b[i] }` for a mask in `{0x00, 0xFF}`.80///81/// Panics if the three slices do not have the same length (public information).82#[inline(never)]83pub fn select(out: &mut [u8], a: &[u8], b: &[u8], mask: u8) {84    assert!(out.len() == a.len() && a.len() == b.len());85    let mask = black_box(mask);86    for i in 0..out.len() {87        out[i] = b[i] ^ (mask & (a[i] ^ b[i]));88    }89}9091/// Returns `0xFFFF_FFFF` if `a < b`, else `0`, for 32-bit unsigned values.92#[inline]93pub fn lt_mask_u32(a: u32, b: u32) -> u32 {94    // Borrow of the 33-bit subtraction a - b.95    let d = (a as u64).wrapping_sub(b as u64);96    ((d >> 63) as u32).wrapping_neg()97}9899#[cfg(test)]100mod tests {101    use super::*;102103    #[test]104    fn eq_mask_basic() {105        assert_eq!(eq_mask(b"", b""), 0xFF);106        assert_eq!(eq_mask(b"abc", b"abc"), 0xFF);107        assert_eq!(eq_mask(b"abc", b"abd"), 0x00);108        assert_eq!(eq_mask(b"abc", b"ab"), 0x00);109        for bit in 0..8 {110            let a = [0u8; 4];111            let mut b = [0u8; 4];112            b[2] = 1 << bit;113            assert_eq!(eq_mask(&a, &b), 0x00);114        }115    }116117    #[test]118    fn select_basic() {119        let a = [1u8, 2, 3];120        let b = [9u8, 8, 7];121        let mut o = [0u8; 3];122        select(&mut o, &a, &b, 0xFF);123        assert_eq!(o, a);124        select(&mut o, &a, &b, 0x00);125        assert_eq!(o, b);126    }127128    #[test]129    fn lt_mask() {130        assert_eq!(lt_mask_u32(1, 2), u32::MAX);131        assert_eq!(lt_mask_u32(2, 2), 0);132        assert_eq!(lt_mask_u32(3, 2), 0);133        assert_eq!(lt_mask_u32(0, u32::MAX), u32::MAX);134        assert_eq!(lt_mask_u32(u32::MAX, 0), 0);135    }136}137
Files main
Menu

Theme