Struct secret_integers::U32
source · pub struct U32(pub u32);
Tuple Fields§
§0: u32
Implementations§
source§impl U32
impl U32
sourcepub fn classify<T: Into<u32>>(x: T) -> Self
pub fn classify<T: Into<u32>>(x: T) -> Self
Examples found in repository?
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
pub fn classify_u32s(v: &[u32]) -> Vec<U32> {
v.iter().map(|x| U32::classify(*x)).collect()
}
fn line(a: Index, b: Index, d: Index, s: RotVal, m: &mut State) {
m[a] = m[a] + m[b];
m[d] = m[d] ^ m[a];
m[d] = m[d].rotate_left(s);
}
fn quarter_round(a: Index, b: Index, c: Index, d: Index, m: &mut State) {
line(a, b, d, 16, m);
line(c, d, b, 12, m);
line(a, b, d, 8, m);
line(c, d, b, 7, m);
}
fn double_round(m: &mut State) {
quarter_round(0, 4, 8, 12, m);
quarter_round(1, 5, 9, 13, m);
quarter_round(2, 6, 10, 14, m);
quarter_round(3, 7, 11, 15, m);
quarter_round(0, 5, 10, 15, m);
quarter_round(1, 6, 11, 12, m);
quarter_round(2, 7, 8, 13, m);
quarter_round(3, 4, 9, 14, m);
}
const CONSTANTS: Constants = [0x61707865, 0x3320646e, 0x79622d32, 0x6b206574];
fn chacha20_init(k: &Key, counter: U32, nonce: &Nonce) -> State {
let mut st = [U32::classify(0u32); 16];
st[0..4].copy_from_slice(&classify_u32s(&CONSTANTS));
st[4..12].copy_from_slice(U32::from_le_bytes(k).as_slice());
st[12] = counter;
st[13..16].copy_from_slice(U32::from_le_bytes(nonce).as_slice());
st
}
sourcepub fn declassify(self) -> u32
pub fn declassify(self) -> u32
Warning: use with caution, breaks the constant-time guarantee.
pub fn zero() -> Self
sourcepub fn one() -> Self
pub fn one() -> Self
Examples found in repository?
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
fn chacha20_counter_mode(key: &Key, counter: U32, nonce: &Nonce, msg: &Vec<U8>) -> Vec<U8> {
let mut blocks: Vec<[U8; BLOCK_SIZE]> = msg
.chunks(BLOCK_SIZE)
.map(|block| {
let mut new_block = [U8::zero(); BLOCK_SIZE];
new_block[0..block.len()].copy_from_slice(block);
new_block
})
.collect();
let nb_blocks = blocks.len();
let mut key_block: [U8; BLOCK_SIZE];
let mut ctr = counter;
for i in 0..blocks.len() - 1 {
key_block = chacha20_block(key, ctr, nonce);
blocks[i] = xor_block(&blocks[i], &key_block);
ctr += U32::one();
}
let last = &mut blocks[nb_blocks - 1];
key_block = chacha20_block(key, ctr, nonce);
*last = xor_block(last, &key_block);
blocks
.iter()
.map(|block| block.to_vec())
.flatten()
.take(msg.len())
.collect()
}
pub fn ones() -> Self
sourcepub fn from_le_bytes(bytes: &[U8]) -> Vec<U32>
pub fn from_le_bytes(bytes: &[U8]) -> Vec<U32>
sourcepub fn to_le_bytes(ints: &[U32]) -> Vec<U8>
pub fn to_le_bytes(ints: &[U32]) -> Vec<U8>
pub fn from_be_bytes(bytes: &[U8]) -> Vec<U32>
pub fn to_be_bytes(ints: &[U32]) -> Vec<U8>
pub fn max_value() -> U32
source§impl U32
impl U32
sourcepub fn checked_add(self, rhs: Self) -> Self
pub fn checked_add(self, rhs: Self) -> Self
Warning: panics when overflow.
source§impl U32
impl U32
sourcepub fn checked_sub(self, rhs: Self) -> Self
pub fn checked_sub(self, rhs: Self) -> Self
Warning: panics when overflow.
source§impl U32
impl U32
sourcepub fn checked_mul(self, rhs: Self) -> Self
pub fn checked_mul(self, rhs: Self) -> Self
Warning: panics when overflow.
source§impl U32
impl U32
sourcepub fn rotate_left(self, rotval: usize) -> Self
pub fn rotate_left(self, rotval: usize) -> Self
pub fn rotate_right(self, rotval: usize) -> Self
source§impl U32
impl U32
sourcepub fn comp_eq(self, rhs: Self) -> Self
pub fn comp_eq(self, rhs: Self) -> Self
Produces a new integer which is all ones if the two arguments are equal and all zeroes otherwise. With inspiration from Wireguard.
sourcepub fn comp_ne(self, rhs: Self) -> Self
pub fn comp_ne(self, rhs: Self) -> Self
Produces a new integer which is all ones if the first argument is different from the second argument, and all zeroes otherwise.
sourcepub fn comp_gte(self, rhs: Self) -> Self
pub fn comp_gte(self, rhs: Self) -> Self
Produces a new integer which is all ones if the first argument is greater than or equal to the second argument, and all zeroes otherwise. With inspiration from WireGuard.
sourcepub fn comp_gt(self, rhs: Self) -> Self
pub fn comp_gt(self, rhs: Self) -> Self
Produces a new integer which is all ones if the first argument is strictly greater than the second argument, and all zeroes otherwise.
Trait Implementations§
source§impl AddAssign<U32> for U32
impl AddAssign<U32> for U32
Warning: has wrapping semantics.
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moresource§impl BitAndAssign<U32> for U32
impl BitAndAssign<U32> for U32
source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&=
operation. Read moresource§impl BitOrAssign<U32> for U32
impl BitOrAssign<U32> for U32
source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|=
operation. Read moresource§impl BitXorAssign<U32> for U32
impl BitXorAssign<U32> for U32
source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^=
operation. Read moresource§impl MulAssign<U32> for U32
impl MulAssign<U32> for U32
Warning: has wrapping semantics.
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*=
operation. Read moresource§impl ShlAssign<usize> for U32
impl ShlAssign<usize> for U32
source§fn shl_assign(&mut self, rhs: usize)
fn shl_assign(&mut self, rhs: usize)
<<=
operation. Read moresource§impl ShrAssign<usize> for U32
impl ShrAssign<usize> for U32
source§fn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
>>=
operation. Read moresource§impl SubAssign<U32> for U32
impl SubAssign<U32> for U32
Warning: has wrapping semantics.
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read more