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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use crate::{ExtendableOutput, FixedOutput, Reset, Update, XofReader};
use block_buffer::BlockBuffer;
use core::fmt;
use generic_array::{ArrayLength, GenericArray};
pub trait AlgorithmName {
const NAME: &'static str;
}
pub trait UpdateCore {
type BlockSize: ArrayLength<u8>;
fn update_blocks(&mut self, blocks: &[GenericArray<u8, Self::BlockSize>]);
}
pub trait FixedOutputCore: UpdateCore {
type OutputSize: ArrayLength<u8>;
fn finalize_fixed_core(
&mut self,
buffer: &mut block_buffer::BlockBuffer<Self::BlockSize>,
out: &mut GenericArray<u8, Self::OutputSize>,
);
}
pub trait ExtendableOutputCore: UpdateCore {
type ReaderCore: XofReaderCore;
fn finalize_xof_core(
&mut self,
buffer: &mut block_buffer::BlockBuffer<Self::BlockSize>,
) -> Self::ReaderCore;
}
pub trait XofReaderCore {
type BlockSize: ArrayLength<u8>;
fn read_block(&mut self) -> GenericArray<u8, Self::BlockSize>;
}
#[derive(Clone, Default)]
pub struct UpdateCoreWrapper<T: UpdateCore> {
core: T,
buffer: BlockBuffer<T::BlockSize>,
}
#[derive(Clone, Default)]
pub struct XofReaderCoreWrapper<T: XofReaderCore> {
core: T,
buffer: BlockBuffer<T::BlockSize>,
}
impl<T: UpdateCore + AlgorithmName> fmt::Debug for UpdateCoreWrapper<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str(T::NAME)?;
f.write_str(" { .. }")
}
}
impl<T: XofReaderCore + AlgorithmName> fmt::Debug for XofReaderCoreWrapper<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str(T::NAME)?;
f.write_str(" { .. }")
}
}
impl<D: Reset + UpdateCore> Reset for UpdateCoreWrapper<D> {
#[inline]
fn reset(&mut self) {
self.core.reset();
self.buffer.reset();
}
}
impl<D: UpdateCore> Update for UpdateCoreWrapper<D> {
#[inline]
fn update(&mut self, input: &[u8]) {
let Self { core, buffer } = self;
buffer.digest_blocks(input, |blocks| core.update_blocks(blocks));
}
}
impl<D: FixedOutputCore + Reset> FixedOutput for UpdateCoreWrapper<D> {
type OutputSize = D::OutputSize;
#[inline]
fn finalize_into(mut self, out: &mut GenericArray<u8, Self::OutputSize>) {
let Self { core, buffer } = &mut self;
core.finalize_fixed_core(buffer, out);
}
#[inline]
fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>) {
let Self { core, buffer } = self;
core.finalize_fixed_core(buffer, out);
self.reset();
}
}
impl<R: XofReaderCore> XofReader for XofReaderCoreWrapper<R> {
#[inline]
fn read(&mut self, buffer: &mut [u8]) {
let Self { core, buffer: buf } = self;
buf.set_data(buffer, || core.read_block());
}
}
impl<D: ExtendableOutputCore + Reset> ExtendableOutput for UpdateCoreWrapper<D> {
type Reader = XofReaderCoreWrapper<D::ReaderCore>;
#[inline]
fn finalize_xof(mut self) -> Self::Reader {
let Self { core, buffer } = &mut self;
let reader_core = core.finalize_xof_core(buffer);
XofReaderCoreWrapper {
core: reader_core,
buffer: Default::default(),
}
}
#[inline]
fn finalize_xof_reset(&mut self) -> Self::Reader {
let Self { core, buffer } = self;
let reader_core = core.finalize_xof_core(buffer);
self.reset();
XofReaderCoreWrapper {
core: reader_core,
buffer: Default::default(),
}
}
}
#[cfg(feature = "std")]
impl<D: UpdateCore> std::io::Write for UpdateCoreWrapper<D> {
#[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(())
}
}
#[cfg(feature = "std")]
impl<R: XofReaderCore> std::io::Read for XofReaderCoreWrapper<R> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
XofReader::read(self, buf);
Ok(buf.len())
}
}