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
use super::{block::BLOCK_LEN, Tag, TAG_LEN};
use crate::{c, cpu};
pub(super) struct Key {
key_and_nonce: [u8; KEY_LEN],
cpu_features: cpu::Features,
}
const KEY_LEN: usize = 2 * BLOCK_LEN;
impl Key {
#[inline]
pub(super) fn new(key_and_nonce: [u8; KEY_LEN], cpu_features: cpu::Features) -> Self {
Self {
key_and_nonce,
cpu_features,
}
}
}
pub struct Context {
state: poly1305_state,
#[allow(dead_code)]
cpu_features: cpu::Features,
}
#[repr(C, align(64))]
struct poly1305_state([u8; OPAQUE_LEN]);
const OPAQUE_LEN: usize = 512;
macro_rules! dispatch {
( $features:expr =>
( $f:ident | $neon_f:ident )
( $( $p:ident : $t:ty ),+ )
( $( $a:expr ),+ ) ) => {
match () {
#[cfg(all(target_arch = "arm", not(target_vendor = "apple")))]
() if cpu::arm::NEON.available($features) => {
extern "C" {
fn $neon_f( $( $p : $t ),+ );
}
unsafe { $neon_f( $( $a ),+ ) }
}
() => {
extern "C" {
fn $f( $( $p : $t ),+ );
}
unsafe { $f( $( $a ),+ ) }
}
}
}
}
impl Context {
#[inline]
pub(super) fn from_key(
Key {
key_and_nonce,
cpu_features,
}: Key,
) -> Self {
let mut ctx = Self {
state: poly1305_state([0u8; OPAQUE_LEN]),
cpu_features,
};
dispatch!(
cpu_features =>
(GFp_poly1305_init | GFp_poly1305_init_neon)
(statep: &mut poly1305_state, key: &[u8; KEY_LEN])
(&mut ctx.state, &key_and_nonce));
ctx
}
#[inline(always)]
pub fn update(&mut self, input: &[u8]) {
dispatch!(
self.cpu_features =>
(GFp_poly1305_update | GFp_poly1305_update_neon)
(statep: &mut poly1305_state, input: *const u8, in_len: c::size_t)
(&mut self.state, input.as_ptr(), input.len()));
}
pub(super) fn finish(mut self) -> Tag {
let mut tag = Tag([0u8; TAG_LEN]);
dispatch!(
self.cpu_features =>
(GFp_poly1305_finish | GFp_poly1305_finish_neon)
(statep: &mut poly1305_state, mac: &mut [u8; TAG_LEN])
(&mut self.state, &mut tag.0));
tag
}
}
pub(super) fn sign(key: Key, input: &[u8]) -> Tag {
let mut ctx = Context::from_key(key);
ctx.update(input);
ctx.finish()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test;
use core::convert::TryInto;
#[test]
pub fn test_poly1305() {
let cpu_features = cpu::features();
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 input = test_case.consume_bytes("Input");
let expected_mac = test_case.consume_bytes("MAC");
let key = Key::new(*key, cpu_features);
let Tag(actual_mac) = sign(key, &input);
assert_eq!(expected_mac, actual_mac.as_ref());
Ok(())
})
}
}