Struct secret_integers::U32

source ·
pub struct U32(pub u32);

Tuple Fields§

§0: u32

Implementations§

source§

impl U32

source

pub fn classify<T: Into<u32>>(x: T) -> Self

Examples found in repository?
examples/chacha20.rs (line 13)
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
}
source

pub fn declassify(self) -> u32

Warning: use with caution, breaks the constant-time guarantee.

source

pub fn zero() -> Self

source

pub fn one() -> Self

Examples found in repository?
examples/chacha20.rs (line 100)
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()
}
source

pub fn ones() -> Self

source

pub fn from_le_bytes(bytes: &[U8]) -> Vec<U32>

Examples found in repository?
examples/chacha20.rs (line 46)
43
44
45
46
47
48
49
50
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
}
source

pub fn to_le_bytes(ints: &[U32]) -> Vec<U8>

Examples found in repository?
examples/chacha20.rs (line 71)
68
69
70
71
72
73
fn chacha20_block(k: &Key, counter: U32, nonce: &Nonce) -> Block {
    let st = chacha20(k, counter, nonce);
    let mut block = [U8::classify(0u8); BLOCK_SIZE];
    block.copy_from_slice(U32::to_le_bytes(&st).as_slice());
    block
}
source

pub fn from_be_bytes(bytes: &[U8]) -> Vec<U32>

source

pub fn to_be_bytes(ints: &[U32]) -> Vec<U8>

source

pub fn max_value() -> U32

source§

impl U32

source

pub fn checked_add(self, rhs: Self) -> Self

Warning: panics when overflow.

source§

impl U32

source

pub fn checked_sub(self, rhs: Self) -> Self

Warning: panics when overflow.

source§

impl U32

source

pub fn checked_mul(self, rhs: Self) -> Self

Warning: panics when overflow.

source§

impl U32

source

pub fn rotate_left(self, rotval: usize) -> Self

Examples found in repository?
examples/chacha20.rs (line 19)
16
17
18
19
20
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);
}
source

pub fn rotate_right(self, rotval: usize) -> Self

source§

impl U32

source

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.

source

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.

source

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.

source

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.

source

pub fn comp_lte(self, rhs: Self) -> Self

Produces a new integer which is all ones if the first argument is less than or equal to the second argument, and all zeroes otherwise.

source

pub fn comp_lt(self, rhs: Self) -> Self

Produces a new integer which is all ones if the first argument is strictly less than the second argument, and all zeroes otherwise.

Trait Implementations§

source§

impl Add<U32> for U32

Warning: has wrapping semantics.

§

type Output = U32

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
source§

impl AddAssign<U32> for U32

Warning: has wrapping semantics.

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl BitAnd<U32> for U32

§

type Output = U32

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: Self) -> Self

Performs the & operation. Read more
source§

impl BitAndAssign<U32> for U32

source§

fn bitand_assign(&mut self, rhs: Self)

Performs the &= operation. Read more
source§

impl BitOr<U32> for U32

§

type Output = U32

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: Self) -> Self

Performs the | operation. Read more
source§

impl BitOrAssign<U32> for U32

source§

fn bitor_assign(&mut self, rhs: Self)

Performs the |= operation. Read more
source§

impl BitXor<U32> for U32

§

type Output = U32

The resulting type after applying the ^ operator.
source§

fn bitxor(self, rhs: Self) -> Self

Performs the ^ operation. Read more
source§

impl BitXorAssign<U32> for U32

source§

fn bitxor_assign(&mut self, rhs: Self)

Performs the ^= operation. Read more
source§

impl Clone for U32

source§

fn clone(&self) -> U32

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for U32

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for U32

source§

fn default() -> U32

Returns the “default value” for a type. Read more
source§

impl Display for U32

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<I32> for U32

source§

fn from(x: I32) -> U32

Converts to this type from the input type.
source§

impl From<U128> for U32

Warning: wrapping semantics.

source§

fn from(x: U128) -> U32

Converts to this type from the input type.
source§

impl From<U16> for U32

source§

fn from(x: U16) -> U32

Converts to this type from the input type.
source§

impl From<U32> for I32

Warning: wrapping semantics.

source§

fn from(x: U32) -> I32

Converts to this type from the input type.
source§

impl From<U32> for U128

source§

fn from(x: U32) -> U128

Converts to this type from the input type.
source§

impl From<U32> for U16

Warning: wrapping semantics.

source§

fn from(x: U32) -> U16

Converts to this type from the input type.
source§

impl From<U32> for U64

source§

fn from(x: U32) -> U64

Converts to this type from the input type.
source§

impl From<U32> for U8

Warning: wrapping semantics.

source§

fn from(x: U32) -> U8

Converts to this type from the input type.
source§

impl From<U32> for u128

Warning: conversion can be lossy!

source§

fn from(x: U32) -> u128

Converts to this type from the input type.
source§

impl From<U32> for u32

Warning: conversion can be lossy!

source§

fn from(x: U32) -> u32

Converts to this type from the input type.
source§

impl From<U32> for u64

Warning: conversion can be lossy!

source§

fn from(x: U32) -> u64

Converts to this type from the input type.
source§

impl From<U64> for U32

Warning: wrapping semantics.

source§

fn from(x: U64) -> U32

Converts to this type from the input type.
source§

impl From<U8> for U32

source§

fn from(x: U8) -> U32

Converts to this type from the input type.
source§

impl From<u16> for U32

source§

fn from(x: u16) -> U32

Converts to this type from the input type.
source§

impl From<u32> for U32

source§

fn from(x: u32) -> Self

Converts to this type from the input type.
source§

impl From<u8> for U32

source§

fn from(x: u8) -> U32

Converts to this type from the input type.
source§

impl LowerHex for U32

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter.
source§

impl Mul<U32> for U32

Warning: has wrapping semantics.

§

type Output = U32

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
source§

impl MulAssign<U32> for U32

Warning: has wrapping semantics.

source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
source§

impl Neg for U32

§

type Output = U32

The resulting type after applying the - operator.
source§

fn neg(self) -> Self

Performs the unary - operation. Read more
source§

impl Not for U32

§

type Output = U32

The resulting type after applying the ! operator.
source§

fn not(self) -> Self

Performs the unary ! operation. Read more
source§

impl Shl<usize> for U32

§

type Output = U32

The resulting type after applying the << operator.
source§

fn shl(self, rhs: usize) -> Self

Performs the << operation. Read more
source§

impl ShlAssign<usize> for U32

source§

fn shl_assign(&mut self, rhs: usize)

Performs the <<= operation. Read more
source§

impl Shr<usize> for U32

§

type Output = U32

The resulting type after applying the >> operator.
source§

fn shr(self, rhs: usize) -> Self

Performs the >> operation. Read more
source§

impl ShrAssign<usize> for U32

source§

fn shr_assign(&mut self, rhs: usize)

Performs the >>= operation. Read more
source§

impl Sub<U32> for U32

Warning: has wrapping semantics.

§

type Output = U32

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
source§

impl SubAssign<U32> for U32

Warning: has wrapping semantics.

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl Copy for U32

Auto Trait Implementations§

§

impl RefUnwindSafe for U32

§

impl Send for U32

§

impl Sync for U32

§

impl Unpin for U32

§

impl UnwindSafe for U32

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.