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
use crate::{backend, Block, Key, Tag};
cpuid_bool::new!(avx2_cpuid, "avx2");
pub struct State {
inner: Inner,
token: avx2_cpuid::InitToken,
}
union Inner {
avx2: backend::avx2::State,
soft: backend::soft::State,
}
impl State {
#[inline]
pub(crate) fn new(key: &Key) -> State {
let (token, avx2_present) = avx2_cpuid::init_get();
let inner = if avx2_present {
Inner {
avx2: backend::avx2::State::new(key),
}
} else {
Inner {
soft: backend::soft::State::new(key),
}
};
Self { inner, token }
}
#[inline]
pub(crate) fn reset(&mut self) {
if self.token.get() {
unsafe { self.inner.avx2.reset() }
} else {
unsafe { self.inner.soft.reset() }
}
}
#[inline]
pub(crate) fn compute_block(&mut self, block: &Block, partial: bool) {
if self.token.get() {
unsafe { self.inner.avx2.compute_block(block, partial) }
} else {
unsafe { self.inner.soft.compute_block(block, partial) }
}
}
#[inline]
pub(crate) fn finalize(&mut self) -> Tag {
if self.token.get() {
unsafe { self.inner.avx2.finalize() }
} else {
unsafe { self.inner.soft.finalize() }
}
}
}
impl Clone for State {
fn clone(&self) -> Self {
let inner = if self.token.get() {
Inner {
avx2: unsafe { self.inner.avx2 },
}
} else {
Inner {
soft: unsafe { self.inner.soft },
}
};
Self {
inner,
token: self.token,
}
}
}
#[cfg(feature = "zeroize")]
impl Drop for State {
fn drop(&mut self) {
use zeroize::Zeroize;
const SIZE: usize = core::mem::size_of::<State>();
let state = unsafe { &mut *(self as *mut State as *mut [u8; SIZE]) };
state.zeroize();
}
}