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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! # AEAD helpers
//!
//! This module contains some wrappers over the AEAD functions in the `ring`
//! library. You are advised to not use these low-level functions directly, and
//! instead use the functions provided by the [`cryptors`] module
//!
//! ## Examples
//!
//! You can encrypt (seal) and decrypt (open) a secret value as follows:
//!
//! ```
//! use ring::aead;
//! use tindercrypt::rand::fill_buf;
//! use tindercrypt::aead::{seal_in_place, open_in_place, NONCE_SIZE};
//!
//! let algo = &aead::AES_256_GCM;
//! let mut nonce = [0u8; NONCE_SIZE];
//! let aad = "My encryption context".as_bytes();
//! let mut key = vec![0u8; algo.key_len()];
//! let data = "The cake is a lie".as_bytes();
//!
//! // Create a unique nonce and key.
//! fill_buf(&mut nonce);
//! fill_buf(&mut key);
//!
//! // Create a buffer large enough to hold the ciphertext and its tag.
//! let mut buf = vec![0; data.len() + algo.tag_len()];
//! buf[..data.len()].copy_from_slice(&data);
//!
//! // Encrypt (seal) the data buffer in place, thereby ovewriting the
//! // plaintext data with the ciphertext, and appending a tag at the end.
//! seal_in_place(algo, nonce.clone(), &aad, &key, &mut buf);
//!
//! // Decrypt (open) the data buffer in place, thereby ovewriting ciphertext
//! // with the plaintext (the previous tag will not be removed).
//! open_in_place(algo, nonce.clone(), &aad, &key, &mut buf);
//! assert_eq!(data, &buf[..data.len()]);
//!
//! // Ensure that the nonce is never used again.
//! drop(nonce);
//!
//! ```
//!
//! [`cryptors`]: ../cryptors/index.html

use crate::errors;
use ring::aead;

/// The size of the nonces that `ring` expects.
pub const NONCE_SIZE: usize = 12;

/// Check if the provided key has the expected size for the specified
/// algorithm.
fn _check_key(
    algo: &'static aead::Algorithm,
    key: &[u8],
) -> Result<(), errors::Error> {
    if key.len() != algo.key_len() {
        return Err(errors::Error::KeySizeMismatch);
    }
    Ok(())
}

/// Check if the buffer where the output will be stored is large enough to
/// contain the tag.
fn _check_in_out(
    algo: &'static aead::Algorithm,
    in_out: &[u8],
) -> Result<(), errors::Error> {
    if in_out.len() < algo.tag_len() {
        return Err(errors::Error::BufferTooSmall);
    }
    Ok(())
}

/// Seal the contents of a data buffer in place.
///
/// This function is a wrapper around the `seal_in_place()` function of the
/// `ring` library. Its purpose is to simplify what needs to be passed to the
/// underlying function and perform some early checks. The produced ciphertext
/// will be stored in the same buffer as the plaintext, effectively erasing it.
///
/// This function accepts the following parameters:
///
/// * A `ring` AEAD algorithm, e.g., AES-256-GCM,
/// * A nonce buffer with a specific size. This nonce must **NEVER** be reused
///   for the same key.
/// * A reference to some data (additional authenticated data), which won't be
///   stored with the ciphertext, but will be used for the encryption and will
///   be required for the decryption as well.
/// * A reference to a symmetric key, whose size must match the size required
///   by the AEAD algorithm.
/// * A data buffer that holds the plaintext. The ciphertext will be
///   stored in this buffer, so it must be large enough to contain the
///   encrypted data and the tag as well. In practice, the user must craft a
///   buffer that starts with the plaintext and add an empty space at the end,
///   as large as the tag size expected by the algorithm.
///
/// This function returns an error if the key/buffer sizes are not the expected
/// ones. If the encryption fails, which should never happen in practice, this
/// function panics. If the encryption succeeds, it returns the length of the
/// plaintext.
pub fn seal_in_place(
    algo: &'static aead::Algorithm,
    nonce: [u8; NONCE_SIZE],
    aad: &[u8],
    key: &[u8],
    in_out: &mut [u8],
) -> Result<usize, errors::Error> {
    _check_key(algo, key)?;
    _check_in_out(algo, in_out)?;

    let tag_size = algo.tag_len();
    let plaintext_size: usize = in_out.len() - tag_size;
    let plaintext = &mut in_out[..plaintext_size];
    let unbound_key = aead::UnboundKey::new(algo, key).unwrap();
    let key = aead::LessSafeKey::new(unbound_key);
    let nonce = aead::Nonce::assume_unique_for_key(nonce);
    let aad = aead::Aad::from(aad);
    let res = key.seal_in_place_separate_tag(nonce, aad, plaintext);

    match res {
        Ok(t) => {
            let tag = &mut in_out[plaintext_size..];
            tag.copy_from_slice(t.as_ref());
            Ok(plaintext_size)
        }
        Err(error) => panic!("Error during sealing: {:?}", error),
    }
}

/// Open the contents of a sealed data buffer in place.
///
/// This function is a wrapper around the `open_in_place()` function of the
/// `ring` library. Its purpose is to simplify what needs to be passed to the
/// underlying function and perform some early checks. The produced plaintext
/// will be stored in the same buffer as the ciphertext, effectively erasing it.
///
/// This function accepts the following parameters:
///
/// * A `ring` AEAD algorithm, e.g., AES-256-GCM,
/// * A nonce buffer with a specific size.
/// * A reference to some data (additional authenticated data), which must be
///   the same as the ones provided during the sealing process.
/// * A reference to a symmetric key, whose size must match the size required
///   by the AEAD algorithm.
/// * A data buffer that holds the ciphertext and its tag.
///
/// This function returns an error if the key/buffer sizes are not the expected
/// ones, or if the decryption process fails, e.g., due to a wrong key, nonce,
/// etc. If the decryption succeeds, it returns the length of the plaintext.
pub fn open_in_place(
    algo: &'static aead::Algorithm,
    nonce: [u8; NONCE_SIZE],
    aad: &[u8],
    key: &[u8],
    in_out: &mut [u8],
) -> Result<usize, errors::Error> {
    _check_key(algo, key)?;
    _check_in_out(algo, in_out)?;

    let unbound_key = aead::UnboundKey::new(algo, key).unwrap();
    let key = aead::LessSafeKey::new(unbound_key);
    let nonce = aead::Nonce::assume_unique_for_key(nonce);
    let aad = aead::Aad::from(aad);
    let res = key.open_in_place(nonce, aad, in_out);

    match res {
        Ok(plaintext) => Ok(plaintext.len()),
        Err(_) => Err(errors::Error::DecryptionError),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const BUF_SIZE: usize = 36; // 36 bytes can contain the tag and data.

    #[test]
    fn test_key() {
        // Check that a key with incorrect size produces an error.
        // FIXME: Why can't I iterate over array pointers with different size?
        for algo in &[&aead::AES_256_GCM, &aead::CHACHA20_POLY1305] {
            for key in &[
                vec![],
                vec![0; 1],
                vec![0; algo.key_len() - 1],
                vec![0; algo.key_len() + 1],
            ] {
                assert_eq!(
                    _check_key(algo, key),
                    Err(errors::Error::KeySizeMismatch)
                );
            }
        }
    }

    #[test]
    fn test_in_out() {
        // Check that a key with incorrect size produces an error.
        // FIXME: Why can't I iterate over array pointers with different size?
        for algo in &[&aead::AES_256_GCM, &aead::CHACHA20_POLY1305] {
            for in_out in &[vec![], vec![0; 1], vec![0; algo.tag_len() - 1]] {
                assert_eq!(
                    _check_in_out(algo, in_out),
                    Err(errors::Error::BufferTooSmall)
                );
            }
        }
    }

    fn _test_seal_open(algo: &'static aead::Algorithm) {
        let nonce = [1; 12];
        let aad = [2; 9];
        let key = vec![3; algo.key_len()];
        let mut in_out: [u8; BUF_SIZE];
        let mut res: Result<usize, errors::Error>;
        let plaintext_size = BUF_SIZE - algo.tag_len();
        let exp_res = Ok(plaintext_size);
        let dec_err = Err(errors::Error::DecryptionError);
        let buf_err = Err(errors::Error::BufferTooSmall);
        let key_err = Err(errors::Error::KeySizeMismatch);

        // NOTE: We create a closure to avoid repetitions.
        let seal = || {
            let r;
            let mut _in_out = [4; BUF_SIZE];
            r = seal_in_place(algo, nonce.clone(), &aad, &key, &mut _in_out);
            assert_eq!(r, exp_res);
            _in_out
        };

        // Check that any type of data corruption makes decryption fail.
        //
        // Corrupted nonce.
        in_out = seal();
        let mut bad_nonce = nonce.clone();
        bad_nonce[0] = 9;
        res = open_in_place(algo, bad_nonce.clone(), &aad, &key, &mut in_out);
        assert_eq!(res, dec_err);

        // Corrupted additional authenticated data.
        in_out = seal();
        let mut bad_aad = aad.clone();
        bad_aad[0] = 9;
        res = open_in_place(algo, nonce.clone(), &bad_aad, &key, &mut in_out);
        assert_eq!(res, dec_err);

        // Corrupted key.
        in_out = seal();
        let mut bad_key = key.clone();
        bad_key[0] = 9;
        res = open_in_place(algo, nonce.clone(), &aad, &bad_key, &mut in_out);
        assert_eq!(res, dec_err);

        // Corrupted data.
        in_out = seal();
        let mut bad_in_out = in_out.clone();
        bad_in_out[0] = 9;
        res = open_in_place(algo, nonce.clone(), &aad, &key, &mut bad_in_out);
        assert_eq!(res, dec_err);

        // Corrupted tag.
        in_out = seal();
        let mut bad_in_out = in_out.clone();
        bad_in_out[in_out.len() - 1] = 9;
        res = open_in_place(algo, nonce.clone(), &aad, &key, &mut bad_in_out);
        assert_eq!(res, dec_err);

        // Incomplete data buffer.
        res = seal_in_place(algo, nonce.clone(), &aad, &key, &mut []);
        assert_eq!(res, buf_err);
        res = open_in_place(algo, nonce.clone(), &aad, &key, &mut []);
        assert_eq!(res, buf_err);

        // Incomplete key.
        res = seal_in_place(algo, nonce.clone(), &aad, &[], &mut in_out);
        assert_eq!(res, key_err);
        res = open_in_place(algo, nonce.clone(), &aad, &[], &mut in_out);
        assert_eq!(res, key_err);

        // Incorrect encryption algorithm.
        let algo2: &'static aead::Algorithm;
        if algo == &aead::AES_256_GCM {
            algo2 = &aead::CHACHA20_POLY1305;
        } else {
            algo2 = &aead::AES_256_GCM;
        }

        in_out = seal();
        res = open_in_place(algo2, nonce.clone(), &aad, &key, &mut in_out);
        assert_eq!(res, dec_err);

        // Correct decryption.
        in_out = seal();
        res = open_in_place(algo, nonce.clone(), &aad, &key, &mut in_out);
        assert_eq!(res, exp_res);
        assert_eq!(in_out[..res.unwrap()], vec![4u8; res.unwrap()][..]);
    }

    #[test]
    fn test_seal_open_aes() {
        _test_seal_open(&aead::AES_256_GCM);
    }

    #[test]
    fn test_seal_open_chacha20() {
        _test_seal_open(&aead::CHACHA20_POLY1305);
    }
}