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
#![no_std]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_root_url = "https://docs.rs/poly1305/0.7.1"
)]
#![warn(missing_docs, rust_2018_idioms)]
#[cfg(feature = "std")]
extern crate std;
pub use universal_hash;
use universal_hash::{
consts::{U16, U32},
generic_array::GenericArray,
NewUniversalHash, UniversalHash,
};
mod backend;
#[cfg(all(
any(target_arch = "x86", target_arch = "x86_64"),
not(feature = "force-soft"),
target_feature = "avx2",
any(fuzzing, test)
))]
mod fuzz;
#[cfg(all(
any(target_arch = "x86", target_arch = "x86_64"),
not(feature = "force-soft")
))]
use crate::backend::autodetect::State;
#[cfg(not(all(
any(target_arch = "x86", target_arch = "x86_64"),
not(feature = "force-soft")
)))]
use crate::backend::soft::State;
pub const KEY_SIZE: usize = 32;
pub const BLOCK_SIZE: usize = 16;
pub type Key = universal_hash::Key<Poly1305>;
pub type Block = universal_hash::Block<Poly1305>;
pub type Tag = universal_hash::Output<Poly1305>;
#[derive(Clone)]
pub struct Poly1305 {
state: State,
}
impl NewUniversalHash for Poly1305 {
type KeySize = U32;
fn new(key: &Key) -> Poly1305 {
Poly1305 {
state: State::new(key),
}
}
}
impl UniversalHash for Poly1305 {
type BlockSize = U16;
fn update(&mut self, block: &Block) {
self.state.compute_block(block, false);
}
fn reset(&mut self) {
self.state.reset();
}
fn finalize(mut self) -> Tag {
self.state.finalize()
}
}
impl Poly1305 {
pub fn compute_unpadded(mut self, data: &[u8]) -> Tag {
for chunk in data.chunks(BLOCK_SIZE) {
if chunk.len() == BLOCK_SIZE {
let block = GenericArray::from_slice(chunk);
self.state.compute_block(block, false);
} else {
let mut block = Block::default();
block[..chunk.len()].copy_from_slice(chunk);
block[chunk.len()] = 1;
self.state.compute_block(&block, true)
}
}
self.state.finalize()
}
}
opaque_debug::implement!(Poly1305);
#[cfg(all(
any(target_arch = "x86", target_arch = "x86_64"),
not(feature = "force-soft"),
target_feature = "avx2",
any(fuzzing, test)
))]
pub use crate::fuzz::fuzz_avx2;