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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use super::{
block::{Block, BLOCK_LEN},
Tag,
};
use crate::{bssl, c, error};
pub struct Key([Block; KEY_BLOCKS]);
impl From<[Block; KEY_BLOCKS]> for Key {
fn from(value: [Block; KEY_BLOCKS]) -> Self {
Self(value)
}
}
pub const KEY_BLOCKS: usize = 2;
pub struct Context {
opaque: Opaque,
nonce: Nonce,
func: Funcs,
}
#[repr(C, align(8))]
struct Opaque([u8; OPAQUE_LEN]);
const OPAQUE_LEN: usize = 192;
impl Context {
#[inline]
pub fn from_key(Key(key_and_nonce): Key) -> Self {
extern "C" {
fn GFp_poly1305_blocks(
state: &mut Opaque,
input: *const u8,
len: c::size_t,
should_pad: Pad,
);
fn GFp_poly1305_emit(state: &mut Opaque, tag: &mut Tag, nonce: &Nonce);
}
let key = DerivedKey(key_and_nonce[0].clone());
let nonce = Nonce(key_and_nonce[1].clone());
let mut ctx = Self {
opaque: Opaque([0u8; OPAQUE_LEN]),
nonce,
func: Funcs {
blocks_fn: GFp_poly1305_blocks,
emit_fn: GFp_poly1305_emit,
},
};
let _ = init(&mut ctx.opaque, key, &mut ctx.func);
ctx
}
pub fn update_block(&mut self, block: Block, pad: Pad) {
self.func.blocks(&mut self.opaque, block.as_ref(), pad);
}
pub fn update_blocks(&mut self, input: &[u8]) {
debug_assert_eq!(input.len() % BLOCK_LEN, 0);
self.func.blocks(&mut self.opaque, input, Pad::Pad);
}
pub(super) fn finish(mut self) -> Tag {
self.func.emit(&mut self.opaque, &self.nonce)
}
}
#[cfg(test)]
pub fn check_state_layout() {
let required_state_size = if cfg!(target_arch = "x86") {
Some(4 * (5 + 1 + 4 + 2 + 4 * 9))
} else if cfg!(target_arch = "x86_64") {
Some(4 * (5 + 1 + 2 * 2 + 2 + 4 * 9))
} else {
None
};
if let Some(required_state_size) = required_state_size {
assert!(core::mem::size_of::<Opaque>() >= required_state_size);
}
}
#[repr(C)]
struct DerivedKey(Block);
#[repr(C)]
struct Nonce(Block);
#[repr(C)]
struct Funcs {
blocks_fn:
unsafe extern "C" fn(&mut Opaque, input: *const u8, input_len: c::size_t, should_pad: Pad),
emit_fn: unsafe extern "C" fn(&mut Opaque, &mut Tag, nonce: &Nonce),
}
#[inline]
fn init(state: &mut Opaque, key: DerivedKey, func: &mut Funcs) -> Result<(), error::Unspecified> {
extern "C" {
fn GFp_poly1305_init_asm(
state: &mut Opaque,
key: &DerivedKey,
out_func: &mut Funcs,
) -> bssl::Result;
}
Result::from(unsafe { GFp_poly1305_init_asm(state, &key, func) })
}
#[repr(u32)]
pub enum Pad {
AlreadyPadded = 0,
Pad = 1,
}
impl Funcs {
#[inline]
fn blocks(&self, state: &mut Opaque, data: &[u8], should_pad: Pad) {
unsafe {
(self.blocks_fn)(state, data.as_ptr(), data.len(), should_pad);
}
}
#[inline]
fn emit(&self, state: &mut Opaque, nonce: &Nonce) -> Tag {
let mut tag = Tag(Block::zero());
unsafe {
(self.emit_fn)(state, &mut tag, nonce);
}
tag
}
}
pub(super) fn sign(key: Key, input: &[u8]) -> Tag {
let mut ctx = Context::from_key(key);
let remainder_len = input.len() % BLOCK_LEN;
let full_blocks_len = input.len() - remainder_len;
let (full_blocks, remainder) = input.split_at(full_blocks_len);
ctx.update_blocks(full_blocks);
if remainder_len > 0 {
let mut bytes = [0; BLOCK_LEN];
bytes[..remainder_len].copy_from_slice(remainder);
bytes[remainder_len] = 1;
ctx.update_block(Block::from(&bytes), Pad::AlreadyPadded);
}
ctx.finish()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{polyfill::convert::Into_, test};
use core::convert::TryInto;
#[test]
pub fn test_state_layout() {
check_state_layout();
}
#[test]
pub fn test_poly1305() {
test::run(test_file!("poly1305_test.txt"), |section, test_case| {
assert_eq!(section, "");
let key = test_case.consume_bytes("Key");
let key: &[u8; BLOCK_LEN * 2] = key.as_slice().try_into().unwrap();
let key: [Block; 2] = key.into_();
let input = test_case.consume_bytes("Input");
let expected_mac = test_case.consume_bytes("MAC");
let key = Key::from(key);
let Tag(actual_mac) = sign(key, &input);
assert_eq!(expected_mac, actual_mac.as_ref());
Ok(())
})
}
}