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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use super::{AlgorithmName, TruncSide, UpdateCore, VariableOutputCore};
#[cfg(feature = "mac")]
use crate::MacMarker;
use crate::{HashMarker, InvalidBufferSize};
use crate::{InvalidOutputSize, Reset, Update, VariableOutput, VariableOutputReset};
use block_buffer::BlockBuffer;
use core::fmt;
use crypto_common::typenum::{IsLess, Le, NonZero, Unsigned, U256};
#[derive(Clone)]
pub struct RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
core: T,
buffer: BlockBuffer<T::BlockSize, T::BufferKind>,
output_size: usize,
}
impl<T> RtVariableCoreWrapper<T>
where
T: VariableOutputCore,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
#[inline]
fn finalize_dirty(&mut self, out: &mut [u8]) -> Result<(), InvalidBufferSize> {
let Self {
core,
buffer,
output_size,
} = self;
if out.len() != *output_size || out.len() > Self::MAX_OUTPUT_SIZE {
return Err(InvalidBufferSize);
}
let mut full_res = Default::default();
core.finalize_variable_core(buffer, &mut full_res);
let n = out.len();
let m = full_res.len() - n;
match T::TRUNC_SIDE {
TruncSide::Left => out.copy_from_slice(&full_res[..n]),
TruncSide::Right => out.copy_from_slice(&full_res[m..]),
}
Ok(())
}
}
impl<T> HashMarker for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + HashMarker,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
}
#[cfg(feature = "mac")]
#[cfg_attr(docsrs, doc(cfg(feature = "mac")))]
impl<T> MacMarker for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + MacMarker,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
}
impl<T> Reset for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore + Reset,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
#[inline]
fn reset(&mut self) {
self.buffer.reset();
self.core.reset();
}
}
impl<T> Update for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
#[inline]
fn update(&mut self, input: &[u8]) {
let Self { core, buffer, .. } = self;
buffer.digest_blocks(input, |blocks| core.update_blocks(blocks));
}
}
impl<T> VariableOutput for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
const MAX_OUTPUT_SIZE: usize = T::OutputSize::USIZE;
fn new(output_size: usize) -> Result<Self, InvalidOutputSize> {
let buffer = Default::default();
T::new(output_size).map(|core| Self {
core,
buffer,
output_size,
})
}
fn output_size(&self) -> usize {
self.output_size
}
fn finalize_variable(mut self, out: &mut [u8]) -> Result<(), InvalidBufferSize> {
self.finalize_dirty(out)
}
}
impl<T> VariableOutputReset for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore + Reset,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
fn finalize_variable_reset(&mut self, out: &mut [u8]) -> Result<(), InvalidBufferSize> {
self.finalize_dirty(out)?;
self.core.reset();
self.buffer.reset();
Ok(())
}
}
impl<T> fmt::Debug for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore + AlgorithmName,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
T::write_alg_name(f)?;
f.write_str(" { .. }")
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<T> std::io::Write for RtVariableCoreWrapper<T>
where
T: VariableOutputCore + UpdateCore,
T::BlockSize: IsLess<U256>,
Le<T::BlockSize, U256>: NonZero,
{
#[inline]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
Update::update(self, buf);
Ok(buf.len())
}
#[inline]
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}