use crate::ciphertext::CipherTextBlock;
use crate::primitives::{AesBlock, Hash, Prp, Symbol};
use crate::scheme::bit2::block_types::RightBlock32;
use crate::scheme::bit2_w6::block_types::RightBlock8;
use zeroize::{Zeroize, ZeroizeOnDrop};
mod sealed {
pub trait Sealed {}
impl Sealed for super::Bit8 {}
impl Sealed for super::Bit6 {}
impl Sealed for [super::AesBlock; 256] {}
impl Sealed for [super::AesBlock; 64] {}
}
pub trait AesBlockBuf: sealed::Sealed {
fn zeroed() -> Self;
#[allow(dead_code)]
fn as_mut_slice(&mut self) -> &mut [AesBlock];
fn copy_from(&mut self, other: &Self);
}
macro_rules! impl_aes_block_buf {
($n:literal) => {
impl AesBlockBuf for [AesBlock; $n] {
fn zeroed() -> Self {
[AesBlock::default(); $n]
}
fn as_mut_slice(&mut self) -> &mut [AesBlock] {
self
}
fn copy_from(&mut self, other: &Self) {
self.clone_from_slice(other);
}
}
};
}
impl_aes_block_buf!(256);
impl_aes_block_buf!(64);
#[inline]
pub(crate) fn ct_select_byte(block: &[u8], idx: usize) -> u8 {
use subtle_ng::{ConditionallySelectable, ConstantTimeEq};
let mut acc = 0u8;
for (i, &b) in block.iter().enumerate() {
acc.conditional_assign(&b, (i as u8).ct_eq(&(idx as u8)));
}
acc
}
#[inline]
pub(crate) fn ct_assign_bytes(dst: &mut [u8], src: &[u8], choice: subtle_ng::Choice) {
use subtle_ng::ConditionallySelectable;
for (d, s) in dst.iter_mut().zip(src) {
d.conditional_assign(s, choice);
}
}
#[inline]
pub(crate) fn ct_bit(byte: u8, pos: u8) -> u8 {
use subtle_ng::{ConditionallySelectable, ConstantTimeEq};
let mut out = 0u8;
out.conditional_assign(&(byte & 1), pos.ct_eq(&0));
out.conditional_assign(&((byte >> 1) & 1), pos.ct_eq(&1));
out.conditional_assign(&((byte >> 2) & 1), pos.ct_eq(&2));
out.conditional_assign(&((byte >> 3) & 1), pos.ct_eq(&3));
out.conditional_assign(&((byte >> 4) & 1), pos.ct_eq(&4));
out.conditional_assign(&((byte >> 5) & 1), pos.ct_eq(&5));
out.conditional_assign(&((byte >> 6) & 1), pos.ct_eq(&6));
out.conditional_assign(&((byte >> 7) & 1), pos.ct_eq(&7));
out
}
#[derive(Zeroize, ZeroizeOnDrop)]
pub(crate) struct FirstDiff<const R: usize> {
xt: u8,
f: [u8; 16],
right: [u8; R],
}
impl<const R: usize> FirstDiff<R> {
pub(crate) fn new() -> Self {
Self {
xt: 0,
f: [0; 16],
right: [0; R],
}
}
#[inline]
pub(crate) fn latch(&mut self, xt: u8, f: &[u8], right: &[u8], first: subtle_ng::Choice) {
use subtle_ng::ConditionallySelectable;
self.xt.conditional_assign(&xt, first);
ct_assign_bytes(&mut self.f, f, first);
ct_assign_bytes(&mut self.right, right, first);
}
#[inline]
pub(crate) fn resolve<H: Hash>(&self, hasher: &H) -> u8 {
let byte = ct_select_byte(&self.right, usize::from(self.xt / 8));
ct_bit(byte, self.xt % 8) ^ hasher.hash(&self.f)
}
}
pub trait RightBitVec {
#[allow(dead_code)]
fn get_bit(&self, bit: usize) -> u8;
type Bits;
fn as_mut_bits(&mut self) -> &mut Self::Bits;
}
impl RightBitVec for RightBlock32 {
type Bits = [u8; 32];
fn get_bit(&self, bit: usize) -> u8 {
RightBlock32::get_bit(self, bit)
}
fn as_mut_bits(&mut self) -> &mut [u8; 32] {
self.bytes_mut()
}
}
pub trait BlockWidth: sealed::Sealed + 'static {
const BITS: usize;
const DOMAIN: usize = 1 << Self::BITS;
type RightBlock: CipherTextBlock + RightBitVec;
type Symbol: Copy;
type Prp: Prp<Self::Symbol, Mask = <Self::RightBlock as RightBitVec>::Bits>;
type RoKeyBuf: AesBlockBuf;
fn hash_into<H: Hash>(
hasher: &H,
ro: &mut Self::RoKeyBuf,
out: &mut <Self::RightBlock as RightBitVec>::Bits,
);
}
#[derive(Debug)]
pub struct Bit8;
impl BlockWidth for Bit8 {
const BITS: usize = 8;
type RightBlock = RightBlock32;
type Symbol = Symbol<256>;
type Prp = crate::primitives::prp::KnuthShufflePRP<u8, 256>;
type RoKeyBuf = [AesBlock; 256];
fn hash_into<H: Hash>(hasher: &H, ro: &mut [AesBlock; 256], out: &mut [u8; 32]) {
hasher.hash_all_into(ro, out);
}
}
#[derive(Debug)]
pub struct Bit6;
impl BlockWidth for Bit6 {
const BITS: usize = 6;
type RightBlock = RightBlock8;
type Prp = crate::primitives::prp::LemireFyPrp<64>;
type Symbol = Symbol<64>;
type RoKeyBuf = [AesBlock; 64];
fn hash_into<H: Hash>(hasher: &H, ro: &mut [AesBlock; 64], out: &mut [u8; 8]) {
hasher.hash_all_into(ro, out);
}
}
#[cfg(kani)]
mod kani_proofs {
use super::*;
#[cfg(feature = "kani-full")]
#[kani::proof]
#[kani::unwind(257)]
fn ct_select_byte_equals_index_up_to_256() {
let buf: [u8; 256] = kani::any();
let len: usize = kani::any();
kani::assume(len >= 1 && len <= 256);
let idx: usize = kani::any();
kani::assume(idx < len);
let block = &buf[..len];
assert_eq!(ct_select_byte(block, idx), block[idx]);
}
#[kani::proof]
#[kani::unwind(33)]
fn ct_select_byte_equals_index_right_blocks() {
let b8: [u8; 8] = kani::any();
let i8: usize = kani::any();
kani::assume(i8 < 8);
assert_eq!(ct_select_byte(&b8, i8), b8[i8]);
let b32: [u8; 32] = kani::any();
let i32: usize = kani::any();
kani::assume(i32 < 32);
assert_eq!(ct_select_byte(&b32, i32), b32[i32]);
}
#[kani::proof]
fn ct_bit_equals_shift() {
let byte: u8 = kani::any();
let pos: u8 = kani::any();
if pos < 8 {
assert_eq!(ct_bit(byte, pos), (byte >> pos) & 1);
} else {
assert_eq!(ct_bit(byte, pos), 0);
}
}
}