1 // ring has a garbage API so its use is avoided, but rust-crypto doesn't have RFC-variant poly1305
2 // Instead, we steal rust-crypto's implementation and tweak it to match the RFC.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 // This is a port of Andrew Moons poly1305-donna
11 // https://github.com/floodyberry/poly1305-donna
13 #[cfg(not(feature = "fuzztarget"))]
15 use util::chacha20::ChaCha20;
17 use crypto::aead::{AeadEncryptor,AeadDecryptor};
18 use crypto::symmetriccipher::SynchronousStreamCipher;
20 use crypto::util::fixed_time_eq;
21 use crypto::poly1305::Poly1305;
25 #[derive(Clone, Copy)]
26 pub struct ChaCha20Poly1305RFC {
34 impl ChaCha20Poly1305RFC {
36 fn pad_mac_16(mac: &mut Poly1305, len: usize) {
38 mac.input(&[0; 16][0..16 - (len % 16)]);
41 pub fn new(key: &[u8], nonce: &[u8], aad: &[u8]) -> ChaCha20Poly1305RFC {
42 assert!(key.len() == 16 || key.len() == 32);
43 assert!(nonce.len() == 12);
45 // Ehh, I'm too lazy to *also* tweak ChaCha20 to make it RFC-compliant
46 assert!(nonce[0] == 0 && nonce[1] == 0 && nonce[2] == 0 && nonce[3] == 0);
48 let mut cipher = ChaCha20::new(key, &nonce[4..]);
49 let mut mac_key = [0u8; 64];
50 let zero_key = [0u8; 64];
51 cipher.process(&zero_key, &mut mac_key);
53 let mut mac = Poly1305::new(&mac_key[..32]);
55 ChaCha20Poly1305RFC::pad_mac_16(&mut mac, aad.len());
62 aad_len: aad.len() as u64,
67 impl AeadEncryptor for ChaCha20Poly1305RFC {
68 fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
69 assert!(input.len() == output.len());
70 assert!(self.finished == false);
71 self.cipher.process(input, output);
72 self.data_len += input.len();
73 self.mac.input(output);
74 ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len);
76 self.mac.input(&byte_utils::le64_to_array(self.aad_len));
77 self.mac.input(&byte_utils::le64_to_array(self.data_len as u64));
78 self.mac.raw_result(out_tag);
82 impl AeadDecryptor for ChaCha20Poly1305RFC {
83 fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
84 assert!(input.len() == output.len());
85 assert!(self.finished == false);
89 self.mac.input(input);
91 self.data_len += input.len();
92 ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len);
93 self.mac.input(&byte_utils::le64_to_array(self.aad_len));
94 self.mac.input(&byte_utils::le64_to_array(self.data_len as u64));
96 let mut calc_tag = [0u8; 16];
97 self.mac.raw_result(&mut calc_tag);
98 if fixed_time_eq(&calc_tag, tag) {
99 self.cipher.process(input, output);
107 #[cfg(not(feature = "fuzztarget"))]
108 pub use self::real_chachapoly::ChaCha20Poly1305RFC;
110 #[cfg(feature = "fuzztarget")]
111 mod fuzzy_chachapoly {
112 use crypto::aead::{AeadEncryptor,AeadDecryptor};
114 #[derive(Clone, Copy)]
115 pub struct ChaCha20Poly1305RFC {
119 impl ChaCha20Poly1305RFC {
120 pub fn new(key: &[u8], nonce: &[u8], _aad: &[u8]) -> ChaCha20Poly1305RFC {
121 assert!(key.len() == 16 || key.len() == 32);
122 assert!(nonce.len() == 12);
124 // Ehh, I'm too lazy to *also* tweak ChaCha20 to make it RFC-compliant
125 assert!(nonce[0] == 0 && nonce[1] == 0 && nonce[2] == 0 && nonce[3] == 0);
127 let mut tag = [0; 16];
128 tag.copy_from_slice(&key[0..16]);
130 ChaCha20Poly1305RFC {
137 impl AeadEncryptor for ChaCha20Poly1305RFC {
138 fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
139 assert!(input.len() == output.len());
140 assert!(self.finished == false);
142 output.copy_from_slice(&input);
143 out_tag.copy_from_slice(&self.tag);
144 self.finished = true;
148 impl AeadDecryptor for ChaCha20Poly1305RFC {
149 fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
150 assert!(input.len() == output.len());
151 assert!(self.finished == false);
153 if tag[..] != self.tag[..] { return false; }
154 output.copy_from_slice(input);
155 self.finished = true;
160 #[cfg(feature = "fuzztarget")]
161 pub use self::fuzzy_chachapoly::ChaCha20Poly1305RFC;