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
#![no_std]
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
#![warn(missing_docs, rust_2018_idioms)]
#[cfg(feature = "std")]
extern crate std;
pub use generic_array;
use generic_array::typenum::Unsigned;
use generic_array::{ArrayLength, GenericArray};
use subtle::{Choice, ConstantTimeEq};
pub trait UniversalHash: Clone {
type BlockSize: ArrayLength<u8>;
fn new(key: &GenericArray<u8, Self::BlockSize>) -> Self;
fn update_block(&mut self, block: &GenericArray<u8, Self::BlockSize>);
fn update_padded(&mut self, data: &[u8]) {
let mut chunks = data.chunks_exact(Self::BlockSize::to_usize());
for chunk in &mut chunks {
self.update_block(GenericArray::from_slice(chunk));
}
let rem = chunks.remainder();
if !rem.is_empty() {
let mut padded_block = GenericArray::default();
padded_block[..rem.len()].copy_from_slice(rem);
self.update_block(&padded_block);
}
}
fn reset(&mut self);
fn result(self) -> Output<Self::BlockSize>;
fn result_reset(&mut self) -> Output<Self::BlockSize> {
let res = self.clone().result();
self.reset();
res
}
fn verify(self, other: &GenericArray<u8, Self::BlockSize>) -> Result<(), Error> {
if self.result() == other.into() {
Ok(())
} else {
Err(Error)
}
}
}
#[derive(Clone)]
pub struct Output<N: ArrayLength<u8>> {
bytes: GenericArray<u8, N>,
}
impl<N> Output<N>
where
N: ArrayLength<u8>,
{
pub fn new(bytes: GenericArray<u8, N>) -> Output<N> {
Output { bytes }
}
pub fn into_bytes(self) -> GenericArray<u8, N> {
self.bytes
}
}
impl<N> From<GenericArray<u8, N>> for Output<N>
where
N: ArrayLength<u8>,
{
fn from(bytes: GenericArray<u8, N>) -> Self {
Output { bytes }
}
}
impl<'a, N> From<&'a GenericArray<u8, N>> for Output<N>
where
N: ArrayLength<u8>,
{
fn from(bytes: &'a GenericArray<u8, N>) -> Self {
bytes.clone().into()
}
}
impl<N> ConstantTimeEq for Output<N>
where
N: ArrayLength<u8>,
{
fn ct_eq(&self, other: &Self) -> Choice {
self.bytes.ct_eq(&other.bytes)
}
}
impl<N> PartialEq for Output<N>
where
N: ArrayLength<u8>,
{
fn eq(&self, x: &Output<N>) -> bool {
self.ct_eq(x).unwrap_u8() == 1
}
}
impl<N: ArrayLength<u8>> Eq for Output<N> {}
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
pub struct Error;
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn description(&self) -> &'static str {
"UHF output mismatch"
}
}
#[cfg(feature = "std")]
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use std::error::Error;
self.description().fmt(f)
}
}