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
use ffi;
use std::mem;
pub fn easy(
cipher_text: &[u8],
key: [u8; 32],
nonce: [u8; 24],
mac: [u8; 16],
) -> Result<Vec<u8>, String> {
unsafe {
let mut plain_text: Vec<u8> = vec![0u8; cipher_text.len()];
if ffi::crypto_unlock(
plain_text.as_mut_ptr(),
key.as_ptr(),
nonce.as_ptr(),
mac.as_ptr(),
cipher_text.as_ptr(),
cipher_text.len(),
) == 0
{
return Ok(plain_text);
}
Err("Message is corrupt.".to_owned())
}
}
pub fn aead(
cipher_text: &[u8],
key: [u8; 32],
nonce: [u8; 24],
mac: [u8; 16],
ad: &[u8],
) -> Result<Vec<u8>, String> {
unsafe {
let mut plain_text: Vec<u8> = vec![0u8; cipher_text.len()];
if ffi::crypto_unlock_aead(
plain_text.as_mut_ptr(),
key.as_ptr(),
nonce.as_ptr(),
mac.as_ptr(),
ad.as_ptr(),
ad.len(),
cipher_text.as_ptr(),
cipher_text.len(),
) == 0
{
return Ok(plain_text);
}
Err("Message is corrupt.".to_owned())
}
}
pub struct Context(ffi::crypto_lock_ctx);
impl Context {
#[inline]
pub fn new(key: [u8; 32], nonce: [u8; 24]) -> Context {
unsafe {
let mut ctx = mem::uninitialized();
ffi::crypto_lock_init(&mut ctx, key.as_ptr(), nonce.as_ptr());
Context(ctx)
}
}
#[inline]
pub fn auth_ad(&mut self, ad: &[u8]) {
unsafe {
ffi::crypto_lock_auth_ad(&mut self.0, ad.as_ptr(), ad.len());
}
}
#[inline]
pub fn auth_message(&mut self, cipher_text: &[u8]) {
unsafe {
ffi::crypto_lock_auth_message(&mut self.0, cipher_text.as_ptr(), cipher_text.len());
}
}
#[inline]
pub fn update(&mut self, cypher_text: &[u8]) -> Vec<u8> {
unsafe {
let mut plain_text: Vec<u8> = vec![0u8; cypher_text.len()];
ffi::crypto_unlock_update(
&mut self.0,
plain_text.as_mut_ptr(),
cypher_text.as_ptr(),
cypher_text.len(),
);
plain_text
}
}
#[inline]
pub fn finalize(&mut self, mac: [u8; 16]) -> Result<(), String> {
unsafe {
if ffi::crypto_unlock_final(&mut self.0, mac.as_ptr()) == 0 {
return Ok(());
}
Err("Message is corrupted.".to_owned())
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn ctx() {
let key = [2u8; 32];
let nonce = [1u8; 24];
let mut ctx = ::aead::lock::Context::new(key, nonce);
ctx.auth_ad("data".as_bytes());
let cip = ctx.update("test".as_bytes());
let mac = ctx.finalize();
let mut ctx_unlock = Context::new(key, nonce);
ctx_unlock.auth_ad("data".as_bytes());
let pt = ctx_unlock.update(&cip);
let r_unlock = ctx_unlock.finalize(mac);
assert_eq!(pt, vec![116, 101, 115, 116]);
assert_eq!(r_unlock.is_ok(), true)
}
#[test]
fn ctx_auth_no_decryption() {
let key = [2u8; 32];
let nonce = [1u8; 24];
let mut ctx = ::aead::lock::Context::new(key, nonce);
ctx.auth_ad("data".as_bytes());
let cip = ctx.update("test".as_bytes());
let mac = ctx.finalize();
let mut ctx_unlock = Context::new(key, nonce);
ctx_unlock.auth_ad("data".as_bytes());
ctx_unlock.auth_message(&cip);
let r_unlock = ctx_unlock.finalize(mac);
assert_eq!(r_unlock.is_ok(), true)
}
#[test]
fn ctx_wrong_mac() {
let key = [2u8; 32];
let nonce = [1u8; 24];
let mut ctx = ::aead::lock::Context::new(key, nonce);
ctx.auth_ad("data".as_bytes());
let cip = ctx.update("test".as_bytes());
let _mac = ctx.finalize();
let mut ctx_unlock = Context::new(key, nonce);
ctx_unlock.auth_ad("data".as_bytes());
let pt = ctx_unlock.update(&cip);
let r_unlock = ctx_unlock.finalize([0; 16]);
assert_eq!(pt, vec![116, 101, 115, 116]);
assert_eq!(r_unlock.is_err(), true)
}
}