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
// aez.rs - The rust bindings for a hardware optimized AEZ implemented in C.
// Copyright (C) 2018  David Anthony Stainton.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

extern crate libc;

use std::ptr;
use self::libc::c_int;
use super::error::AezDecryptionError;


pub const AEZ_KEY_SIZE: usize = 48;
pub const AEZ_NONCE_SIZE: usize = 16;


extern "C" {
    fn aez_setup_encrypt(key: *const u8, nonce: *const u8,
                         ad: *const u8, adlen: usize, alen: usize,
                         src: *const u8, srclen: usize, dst: *mut u8);

    fn aez_setup_decrypt(key: *const u8, nonce: *const u8,
                         ad: *const u8, adlen: usize, alen: usize,
                         src: *const u8, srclen: usize, dst: *mut u8) -> c_int;
}




pub fn encrypt(key: &[u8; AEZ_KEY_SIZE], nonce: &[u8; AEZ_NONCE_SIZE], mesg: &Vec<u8>) -> Vec<u8> {
    let mut ciphertext = vec![0u8; mesg.len()];
    unsafe {
        aez_setup_encrypt(key as *const u8, nonce as *const u8, ptr::null(), 0, 0, mesg.as_ptr(), mesg.len(), ciphertext.as_mut_ptr());
    }
    ciphertext
}


pub fn decrypt(key: &[u8; AEZ_KEY_SIZE], nonce: &[u8; AEZ_NONCE_SIZE], mesg: &Vec<u8>) -> Result<Vec<u8>, AezDecryptionError> {
    let mut plaintext = vec![0u8; mesg.len()];
    let mut ret: c_int = 0;
    unsafe {
        ret = aez_setup_decrypt(key as *const u8, nonce as *const u8, ptr::null(), 0, 0, mesg.as_ptr(), mesg.len(), plaintext.as_mut_ptr());
    }
    if ret != 0 {
        return Err(AezDecryptionError::DecryptionError);
    }
    Ok(plaintext)
}



#[cfg(test)]
mod tests {
    extern crate rustc_serialize;

    use super::*;
    use std::ptr;
    use self::rustc_serialize::hex::FromHex;
    use std::fs::File;
    use std::path::PathBuf;
    use std::io::prelude::*;

    #[test]
    fn test_simple_bindings_usage() {
        let key_str = "ec6dc9fb5e68dbc2a7615c67baf5b8e472953b84918f1e0c4e01cf43387535d292c4be5657849d84246c7253a3252577";
        let key = key_str.from_hex().unwrap();
        let nonce_str = "05ef180b20d561bf6024a4ecf725fc17";
        let nonce = nonce_str.from_hex().unwrap();
        let m_str = "82ed7abbe93cb1a7ec2d1072f591c058237ff54fc4d44d86cb07c0620675b56b";
        let plaintext = m_str.from_hex().unwrap();
        let mut case_ciphertext = vec![0u8; plaintext.len()];
        let mut case_plaintext = vec![0u8; plaintext.len()];
        unsafe {
            aez_setup_encrypt(key.as_ptr(), nonce.as_ptr(),
                              ptr::null(), 0, 0,
                              plaintext.as_ptr(), plaintext.len(), case_ciphertext.as_mut_ptr());
            aez_setup_decrypt(key.as_ptr(), nonce.as_ptr(),
                              ptr::null(), 0, 0,
                              case_ciphertext.as_ptr(), case_ciphertext.len(), case_plaintext.as_mut_ptr());
            assert_eq!(plaintext.as_slice(), case_plaintext.as_slice());
        }
    }

    #[test]
    fn test_encrypt_decrypt() {
        let key_str = "ec6dc9fb5e68dbc2a7615c67baf5b8e472953b84918f1e0c4e01cf43387535d292c4be5657849d84246c7253a3252577";
        let key = key_str.from_hex().unwrap();
        let nonce_str = "05ef180b20d561bf6024a4ecf725fc17";
        let nonce = nonce_str.from_hex().unwrap();
        let mut key_array = [0u8; AEZ_KEY_SIZE];
        key_array.clone_from_slice(&key);
        let mut nonce_array = [0u8; AEZ_NONCE_SIZE];
        nonce_array.clone_from_slice(&nonce);

        let m_str = "82ed7abbe93cb1a7ec2d1072f591c058237ff54fc4d44d86cb07c0620675b56b";
        let c_str = "8adacd91e46ed69d6c7396c0933eb4d5c125b202875e496cb32f49fb3304e489";
        let plaintext = m_str.from_hex().unwrap();
        let ciphertext = c_str.from_hex().unwrap();

        let case_plaintext = decrypt(&key_array, &nonce_array, &ciphertext).unwrap();
        assert_eq!(plaintext, case_plaintext);
    }

    fn get_test_file_path(filename: String) -> PathBuf {
        let mut path = PathBuf::from(file!());
        path.pop();
        path.pop();
        path.pop();
        path.push("testdata/");
        path.push(filename);
        path
    }

    fn get_test_data(filename: String) -> String {
        let extract_tests_path = get_test_file_path(filename);
        let mut f = File::open(extract_tests_path).unwrap();
        let mut contents = String::new();
        f.read_to_string(&mut contents).unwrap();
        contents
    }

    #[derive(Serialize, Deserialize, Debug)]
    struct ExtractTestCase {
        k: String,
        nonce: String,
        data: Vec<String>,
        tau: u32,
        m: String,
        c: String,
    }

    #[test]
    fn test_encrypt_no_ad_vectors() {
        let cases_str = get_test_data("encrypt_no_ad.json".to_string());
        let cases: Vec<ExtractTestCase> = serde_json::from_str(&cases_str).unwrap();
        for case in cases {
            if case.tau != 0 || case.nonce.from_hex().unwrap().len() != 16 {
                continue
            }
            let key = case.k.from_hex().unwrap();
            let nonce = case.nonce.from_hex().unwrap();
            let plaintext = case.m.from_hex().unwrap();
            let ciphertext = case.c.from_hex().unwrap();
            let mut key_array = [0u8; AEZ_KEY_SIZE];
            key_array.clone_from_slice(&key);
            let mut nonce_array = [0u8; AEZ_NONCE_SIZE];
            nonce_array.clone_from_slice(&nonce);
            let case_ciphertext = encrypt(&key_array, &nonce_array, &plaintext);
            assert_eq!(case_ciphertext, ciphertext);
            let case_plaintext = decrypt(&key_array, &nonce_array, &ciphertext).unwrap();
            assert_eq!(case_plaintext, plaintext);
        }
    }
}