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
use sodiumoxide::crypto::stream::chacha20;
use sodiumoxide::crypto::auth::hmacsha512256;
pub const AUTH_TAG_BYTES: usize = hmacsha512256::TAGBYTES;
pub struct ChaCha20HmacSha512256 {
encryption_key: chacha20::Key,
authentication_key: hmacsha512256::Key,
nonce: chacha20::Nonce,
}
impl ChaCha20HmacSha512256 {
#[inline(always)]
pub fn new(encryption_key: chacha20::Key, authentication_key: hmacsha512256::Key, nonce: chacha20::Nonce) -> ChaCha20HmacSha512256 {
ChaCha20HmacSha512256 {
encryption_key: encryption_key,
authentication_key: authentication_key,
nonce: nonce,
}
}
pub fn plain_auth_tag(&self, message: &[u8]) -> [u8; hmacsha512256::TAGBYTES] {
let auth_tag = hmacsha512256::authenticate(message, &self.authentication_key);
let hmacsha512256::Tag(auth_slice) = auth_tag;
auth_slice
}
pub fn verify_auth(&self, auth_tag: &[u8], message: &[u8]) -> bool {
assert_eq!(auth_tag.len(), hmacsha512256::TAGBYTES);
hmacsha512256::verify(&hmacsha512256::Tag::from_slice(&auth_tag).unwrap(), message, &self.authentication_key)
}
pub fn authenticate_and_encrypt(&self, message: &[u8]) -> Vec<u8> {
let auth_slice = self.plain_auth_tag(message);
let mut cleartext = vec![];
cleartext.extend_from_slice(message);
cleartext.extend_from_slice(&auth_slice);
chacha20::stream_xor(&cleartext, &self.nonce, &self.encryption_key)
}
pub fn decrypt_and_authenticate(&self, ciphertext: &[u8]) -> Option<Vec<u8>> {
assert!(ciphertext.len() > hmacsha512256::TAGBYTES);
let plaintext = chacha20::stream_xor(ciphertext, &self.nonce, &self.encryption_key);
let (message, auth_tag) = plaintext.split_at(ciphertext.len() - hmacsha512256::TAGBYTES);
if self.verify_auth(auth_tag, message) {
let mut ret = vec![];
ret.extend_from_slice(message);
Some(ret)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use sodiumoxide::crypto::stream::chacha20;
use sodiumoxide::crypto::auth::hmacsha512256;
use std::str;
use sodiumoxide;
use sodiumoxide::randombytes;
const MESSAGE_LENGTH: usize = 1024;
#[test]
fn new() {
let k_e = chacha20::gen_key();
let k_a = hmacsha512256::gen_key();
let nonce = chacha20::gen_nonce();
let dut = ChaCha20HmacSha512256::new(k_e.clone(), k_a.clone(), nonce.clone());
assert_eq!(k_e, dut.encryption_key);
assert_eq!(k_a, dut.authentication_key);
assert_eq!(nonce, dut.nonce);
}
#[test]
fn authenticated_encryptor_decryptor() {
let e_k = chacha20::gen_key();
let a_k = hmacsha512256::gen_key();
let nonce = chacha20::gen_nonce();
let dut = ChaCha20HmacSha512256::new(e_k.clone(), a_k.clone(), nonce.clone());
let message = &randombytes::randombytes(MESSAGE_LENGTH);
let ciphertext = dut.authenticate_and_encrypt(message);
let transmitted_message = dut.decrypt_and_authenticate(&ciphertext).unwrap();
assert_eq!(message, &transmitted_message);
}
#[test]
#[should_panic]
fn authenticated_encryptor_decryptor_corrupted_auth() {
let e_k = chacha20::gen_key();
let a_k = hmacsha512256::gen_key();
let a_k2 = hmacsha512256::gen_key();
let nonce = chacha20::gen_nonce();
let dut = ChaCha20HmacSha512256::new(e_k.clone(), a_k.clone(), nonce.clone());
let dut_corrupted = ChaCha20HmacSha512256::new(e_k.clone(), a_k2.clone(), nonce.clone());
let message = &randombytes::randombytes(MESSAGE_LENGTH);
let ciphertext = dut.authenticate_and_encrypt(message);
let transmitted_message = dut_corrupted.decrypt_and_authenticate(&ciphertext).unwrap();
assert_eq!(message, &transmitted_message);
}
#[test]
#[should_panic]
fn authenticated_encryptor_decryptor_corrupted_enc() {
let e_k = chacha20::gen_key();
let e_k2 = chacha20::gen_key();
let a_k = hmacsha512256::gen_key();
let nonce = chacha20::gen_nonce();
let dut = ChaCha20HmacSha512256::new(e_k.clone(), a_k.clone(), nonce.clone());
let dut_corrupted = ChaCha20HmacSha512256::new(e_k2.clone(), a_k.clone(), nonce.clone());
let message = &randombytes::randombytes(MESSAGE_LENGTH);
let ciphertext = dut.authenticate_and_encrypt(message);
let transmitted_message = dut_corrupted.decrypt_and_authenticate(&ciphertext).unwrap();
assert_eq!(message, &transmitted_message);
}
#[test]
fn old_example() {
sodiumoxide::init();
let e_k = chacha20::gen_key();
let a_k = hmacsha512256::gen_key();
let nonce = chacha20::gen_nonce();
let dut = ChaCha20HmacSha512256::new(e_k, a_k, nonce);
let message = "hello world!";
let ciphertext = dut.authenticate_and_encrypt(message.as_bytes());
let transmitted_message = dut.decrypt_and_authenticate(&ciphertext).unwrap();
assert_eq!(message, str::from_utf8(&transmitted_message).unwrap());
}
#[test]
fn authentication() {
sodiumoxide::init();
let e_k = chacha20::gen_key();
let a_k = hmacsha512256::gen_key();
let nonce = chacha20::gen_nonce();
let dut = ChaCha20HmacSha512256::new(e_k, a_k, nonce);
let message = &randombytes::randombytes(MESSAGE_LENGTH);
let auth_tag = dut.plain_auth_tag(message);
assert!(dut.verify_auth(&auth_tag, message));
}
}