2016-02-05 15:58:32 +00:00
|
|
|
// VpnCloud - Peer-to-Peer VPN
|
2017-07-22 14:49:53 +00:00
|
|
|
// Copyright (C) 2015-2017 Dennis Schwerdel
|
2016-02-05 15:58:32 +00:00
|
|
|
// This software is licensed under GPL-3 or newer (see LICENSE.md)
|
|
|
|
|
2015-12-01 09:13:11 +00:00
|
|
|
use std::ptr;
|
2015-11-30 16:27:50 +00:00
|
|
|
use std::ffi::CStr;
|
2016-02-08 19:03:43 +00:00
|
|
|
use std::sync::{Once, ONCE_INIT};
|
|
|
|
|
|
|
|
static CRYPTO_INIT: Once = ONCE_INIT;
|
2015-11-30 16:27:50 +00:00
|
|
|
|
|
|
|
use libc::{size_t, c_char, c_ulonglong, c_int};
|
2015-11-30 21:11:37 +00:00
|
|
|
use aligned_alloc::{aligned_alloc, aligned_free};
|
2015-11-30 16:27:50 +00:00
|
|
|
|
|
|
|
use super::types::Error;
|
|
|
|
|
|
|
|
#[allow(non_upper_case_globals)]
|
2015-12-01 09:13:11 +00:00
|
|
|
const crypto_aead_chacha20poly1305_ietf_KEYBYTES: usize = 32;
|
2015-11-30 16:27:50 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2015-12-01 09:13:11 +00:00
|
|
|
const crypto_aead_chacha20poly1305_ietf_NSECBYTES: usize = 0;
|
2015-11-30 16:27:50 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2015-12-01 09:13:11 +00:00
|
|
|
const crypto_aead_chacha20poly1305_ietf_NPUBBYTES: usize = 12;
|
2015-11-30 16:27:50 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2015-12-01 09:13:11 +00:00
|
|
|
const crypto_aead_chacha20poly1305_ietf_ABYTES: usize = 16;
|
2015-11-30 16:27:50 +00:00
|
|
|
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
const crypto_aead_aes256gcm_KEYBYTES: usize = 32;
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
const crypto_aead_aes256gcm_NSECBYTES: usize = 0;
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
const crypto_aead_aes256gcm_NPUBBYTES: usize = 12;
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
const crypto_aead_aes256gcm_ABYTES: usize = 16;
|
2015-12-01 09:13:11 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
const crypto_aead_aes256gcm_STATEBYTES: usize = 512;
|
2015-11-30 16:27:50 +00:00
|
|
|
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
const crypto_pwhash_scryptsalsa208sha256_SALTBYTES: usize = 32;
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
const crypto_pwhash_scryptsalsa208sha256_STRBYTES: usize = 102;
|
|
|
|
#[allow(non_upper_case_globals)]
|
2019-01-01 23:35:14 +00:00
|
|
|
const crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE: usize = 524_288;
|
2015-11-30 16:27:50 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2019-01-01 23:35:14 +00:00
|
|
|
const crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE: usize = 16_777_216;
|
2015-11-30 16:27:50 +00:00
|
|
|
|
2016-02-02 10:00:03 +00:00
|
|
|
pub struct Aes256State(*mut [u8; crypto_aead_aes256gcm_STATEBYTES]);
|
2015-11-30 21:11:37 +00:00
|
|
|
|
|
|
|
impl Aes256State {
|
|
|
|
fn new() -> Aes256State {
|
2015-12-01 09:13:11 +00:00
|
|
|
let ptr = aligned_alloc(crypto_aead_aes256gcm_STATEBYTES, 16)
|
|
|
|
as *mut [u8; crypto_aead_aes256gcm_STATEBYTES];
|
2015-11-30 21:11:37 +00:00
|
|
|
Aes256State(ptr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Aes256State {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { aligned_free(self.0 as *mut ()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-30 16:27:50 +00:00
|
|
|
#[link(name="sodium", kind="static")]
|
|
|
|
extern {
|
|
|
|
pub fn sodium_init() -> c_int;
|
|
|
|
pub fn randombytes_buf(buf: *mut u8, size: size_t);
|
|
|
|
pub fn sodium_version_string() -> *const c_char;
|
2015-11-30 22:04:24 +00:00
|
|
|
pub fn crypto_aead_aes256gcm_is_available() -> c_int;
|
2015-11-30 16:27:50 +00:00
|
|
|
pub fn crypto_pwhash_scryptsalsa208sha256(
|
|
|
|
out: *mut u8,
|
|
|
|
outlen: c_ulonglong,
|
|
|
|
passwd: *const u8,
|
|
|
|
passwdlen: c_ulonglong,
|
|
|
|
salt: *const [u8; crypto_pwhash_scryptsalsa208sha256_SALTBYTES],
|
|
|
|
opslimit: c_ulonglong,
|
|
|
|
memlimit: size_t) -> c_int;
|
2015-12-01 09:13:11 +00:00
|
|
|
pub fn crypto_aead_chacha20poly1305_ietf_encrypt(
|
2015-11-30 16:27:50 +00:00
|
|
|
c: *mut u8,
|
|
|
|
clen: *mut c_ulonglong,
|
|
|
|
m: *const u8,
|
|
|
|
mlen: c_ulonglong,
|
|
|
|
ad: *const u8,
|
|
|
|
adlen: c_ulonglong,
|
2015-12-01 09:13:11 +00:00
|
|
|
nsec: *const [u8; crypto_aead_chacha20poly1305_ietf_NSECBYTES],
|
|
|
|
npub: *const [u8; crypto_aead_chacha20poly1305_ietf_NPUBBYTES],
|
|
|
|
k: *const [u8; crypto_aead_chacha20poly1305_ietf_KEYBYTES]) -> c_int;
|
|
|
|
pub fn crypto_aead_chacha20poly1305_ietf_decrypt(
|
2015-11-30 16:27:50 +00:00
|
|
|
m: *mut u8,
|
|
|
|
mlen: *mut c_ulonglong,
|
2015-12-01 09:13:11 +00:00
|
|
|
nsec: *mut [u8; crypto_aead_chacha20poly1305_ietf_NSECBYTES],
|
2015-11-30 16:27:50 +00:00
|
|
|
c: *const u8,
|
|
|
|
clen: c_ulonglong,
|
|
|
|
ad: *const u8,
|
|
|
|
adlen: c_ulonglong,
|
2015-12-01 09:13:11 +00:00
|
|
|
npub: *const [u8; crypto_aead_chacha20poly1305_ietf_NPUBBYTES],
|
|
|
|
k: *const [u8; crypto_aead_chacha20poly1305_ietf_KEYBYTES]) -> c_int;
|
2015-11-30 21:11:37 +00:00
|
|
|
pub fn crypto_aead_aes256gcm_beforenm(
|
2015-12-01 09:13:11 +00:00
|
|
|
state: *mut [u8; crypto_aead_aes256gcm_STATEBYTES],
|
2015-11-30 21:11:37 +00:00
|
|
|
k: *const [u8; crypto_aead_aes256gcm_KEYBYTES]) -> c_int;
|
|
|
|
pub fn crypto_aead_aes256gcm_encrypt_afternm(
|
2015-11-30 16:27:50 +00:00
|
|
|
c: *mut u8,
|
|
|
|
clen: *mut c_ulonglong,
|
|
|
|
m: *const u8,
|
|
|
|
mlen: c_ulonglong,
|
|
|
|
ad: *const u8,
|
|
|
|
adlen: c_ulonglong,
|
|
|
|
nsec: *const [u8; crypto_aead_aes256gcm_NSECBYTES],
|
|
|
|
npub: *const [u8; crypto_aead_aes256gcm_NPUBBYTES],
|
2015-12-01 09:13:11 +00:00
|
|
|
state: *const [u8; crypto_aead_aes256gcm_STATEBYTES]) -> c_int;
|
2015-11-30 21:11:37 +00:00
|
|
|
pub fn crypto_aead_aes256gcm_decrypt_afternm(
|
2015-11-30 16:27:50 +00:00
|
|
|
m: *mut u8,
|
|
|
|
mlen: *mut c_ulonglong,
|
|
|
|
nsec: *mut [u8; crypto_aead_aes256gcm_NSECBYTES],
|
|
|
|
c: *const u8,
|
|
|
|
clen: c_ulonglong,
|
|
|
|
ad: *const u8,
|
|
|
|
adlen: c_ulonglong,
|
|
|
|
npub: *const [u8; crypto_aead_aes256gcm_NPUBBYTES],
|
2015-12-01 09:13:11 +00:00
|
|
|
state: *const [u8; crypto_aead_aes256gcm_STATEBYTES]) -> c_int;
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-22 14:34:20 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
2015-11-30 16:27:50 +00:00
|
|
|
pub enum CryptoMethod {
|
2017-07-22 14:42:32 +00:00
|
|
|
#[serde(rename = "chacha20")]
|
|
|
|
ChaCha20,
|
|
|
|
#[serde(rename = "aes256")]
|
|
|
|
AES256
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Crypto {
|
|
|
|
None,
|
2015-12-01 09:13:11 +00:00
|
|
|
ChaCha20Poly1305{
|
|
|
|
key: [u8; crypto_aead_chacha20poly1305_ietf_KEYBYTES],
|
|
|
|
nonce: [u8; crypto_aead_chacha20poly1305_ietf_NPUBBYTES]
|
|
|
|
},
|
|
|
|
AES256GCM{
|
|
|
|
state: Aes256State,
|
|
|
|
nonce: [u8; crypto_aead_aes256gcm_NPUBBYTES]
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inc_nonce_12(nonce: &mut [u8; 12]) {
|
|
|
|
for i in 0..12 {
|
|
|
|
let mut num = nonce[11-i];
|
|
|
|
num = num.wrapping_add(1);
|
|
|
|
nonce[11-i] = num;
|
|
|
|
if num > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Crypto {
|
2016-06-29 06:43:39 +00:00
|
|
|
#[inline]
|
2015-11-30 16:27:50 +00:00
|
|
|
pub fn init() {
|
2016-02-08 19:03:43 +00:00
|
|
|
CRYPTO_INIT.call_once(|| {
|
|
|
|
if unsafe { sodium_init() } != 0 {
|
|
|
|
fail!("Failed to initialize crypto library");
|
|
|
|
}
|
|
|
|
});
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 06:43:39 +00:00
|
|
|
#[inline]
|
2015-11-30 16:27:50 +00:00
|
|
|
pub fn sodium_version() -> String {
|
|
|
|
unsafe {
|
|
|
|
CStr::from_ptr(sodium_version_string()).to_string_lossy().to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 06:43:39 +00:00
|
|
|
#[inline]
|
2015-11-30 22:04:24 +00:00
|
|
|
pub fn aes256_available() -> bool {
|
|
|
|
unsafe {
|
|
|
|
crypto_aead_aes256gcm_is_available() == 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 06:43:39 +00:00
|
|
|
#[inline]
|
2015-11-30 16:27:50 +00:00
|
|
|
pub fn method(&self) -> u8 {
|
2016-06-11 14:08:57 +00:00
|
|
|
match *self {
|
|
|
|
Crypto::None => 0,
|
|
|
|
Crypto::ChaCha20Poly1305{..} => 1,
|
|
|
|
Crypto::AES256GCM{..} => 2
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 06:43:39 +00:00
|
|
|
#[inline]
|
2015-11-30 16:27:50 +00:00
|
|
|
pub fn nonce_bytes(&self) -> usize {
|
2016-06-11 14:08:57 +00:00
|
|
|
match *self {
|
|
|
|
Crypto::None => 0,
|
2016-06-21 06:57:20 +00:00
|
|
|
Crypto::ChaCha20Poly1305{ref nonce, ..} | Crypto::AES256GCM{ref nonce, ..} => nonce.len(),
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 06:43:39 +00:00
|
|
|
#[inline]
|
2019-01-01 23:35:14 +00:00
|
|
|
#[allow(unknown_lints,clippy::match_same_arms)]
|
2015-11-30 16:27:50 +00:00
|
|
|
pub fn additional_bytes(&self) -> usize {
|
2016-06-11 14:08:57 +00:00
|
|
|
match *self {
|
|
|
|
Crypto::None => 0,
|
|
|
|
Crypto::ChaCha20Poly1305{..} => crypto_aead_chacha20poly1305_ietf_ABYTES,
|
|
|
|
Crypto::AES256GCM{..} => crypto_aead_aes256gcm_ABYTES
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_shared_key(method: CryptoMethod, password: &str) -> Self {
|
2016-06-11 14:08:57 +00:00
|
|
|
let salt = b"vpncloudVPNCLOUDvpncl0udVpnCloud";
|
2015-11-30 16:27:50 +00:00
|
|
|
assert_eq!(salt.len(), crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
|
|
|
|
let mut key = [0; crypto_pwhash_scryptsalsa208sha256_STRBYTES];
|
|
|
|
let res = unsafe { crypto_pwhash_scryptsalsa208sha256(
|
|
|
|
key.as_mut_ptr(),
|
|
|
|
key.len() as u64,
|
|
|
|
password.as_bytes().as_ptr(),
|
|
|
|
password.as_bytes().len() as u64,
|
|
|
|
salt.as_ptr() as *const [u8; crypto_pwhash_scryptsalsa208sha256_SALTBYTES],
|
|
|
|
crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE as u64,
|
|
|
|
crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE
|
|
|
|
) };
|
|
|
|
if res != 0 {
|
2015-12-04 10:25:14 +00:00
|
|
|
fail!("Key derivation failed");
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
match method {
|
|
|
|
CryptoMethod::ChaCha20 => {
|
2015-12-01 09:13:11 +00:00
|
|
|
let mut crypto_key = [0; crypto_aead_chacha20poly1305_ietf_KEYBYTES];
|
2016-03-29 12:26:59 +00:00
|
|
|
crypto_key.clone_from_slice(&key[..crypto_aead_chacha20poly1305_ietf_KEYBYTES]);
|
2015-12-01 09:13:11 +00:00
|
|
|
let mut nonce = [0u8; crypto_aead_chacha20poly1305_ietf_NPUBBYTES];
|
2015-11-30 16:27:50 +00:00
|
|
|
unsafe { randombytes_buf(nonce.as_mut_ptr(), nonce.len()) };
|
2019-01-01 23:35:14 +00:00
|
|
|
Crypto::ChaCha20Poly1305{key: crypto_key, nonce}
|
2015-11-30 16:27:50 +00:00
|
|
|
},
|
|
|
|
CryptoMethod::AES256 => {
|
2015-11-30 22:04:24 +00:00
|
|
|
if ! Crypto::aes256_available() {
|
|
|
|
fail!("AES256 is not supported by this processor, use ChaCha20 instead");
|
|
|
|
}
|
2015-11-30 16:27:50 +00:00
|
|
|
let mut nonce = [0u8; crypto_aead_aes256gcm_NPUBBYTES];
|
|
|
|
unsafe { randombytes_buf(nonce.as_mut_ptr(), nonce.len()) };
|
2015-11-30 21:11:37 +00:00
|
|
|
let state = Aes256State::new();
|
|
|
|
let res = unsafe { crypto_aead_aes256gcm_beforenm(
|
|
|
|
state.0,
|
|
|
|
key[..crypto_aead_aes256gcm_KEYBYTES].as_ptr() as *const [u8; crypto_aead_aes256gcm_KEYBYTES]
|
|
|
|
) };
|
|
|
|
assert_eq!(res, 0);
|
2019-01-01 23:35:14 +00:00
|
|
|
Crypto::AES256GCM{state, nonce}
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-01 21:55:15 +00:00
|
|
|
pub fn decrypt(&self, buf: &mut [u8], nonce: &[u8], header: &[u8]) -> Result<usize, Error> {
|
2016-06-11 14:08:57 +00:00
|
|
|
match *self {
|
|
|
|
Crypto::None => Ok(buf.len()),
|
|
|
|
Crypto::ChaCha20Poly1305{ref key, ..} => {
|
2015-11-30 16:27:50 +00:00
|
|
|
let mut mlen: u64 = buf.len() as u64;
|
2015-12-01 09:13:11 +00:00
|
|
|
let res = unsafe { crypto_aead_chacha20poly1305_ietf_decrypt(
|
2015-11-30 16:27:50 +00:00
|
|
|
buf.as_mut_ptr(), // Base pointer to buffer
|
|
|
|
&mut mlen, // Mutable size of buffer (will be set to used size)
|
|
|
|
ptr::null_mut::<[u8; 0]>(), // Mutable base pointer to secret nonce (always NULL)
|
|
|
|
buf.as_ptr(), // Base pointer to message
|
|
|
|
buf.len() as u64, // Size of message
|
|
|
|
header.as_ptr(), // Base pointer to additional data
|
|
|
|
header.len() as u64, // Size of additional data
|
2015-12-01 09:13:11 +00:00
|
|
|
nonce.as_ptr() as *const [u8; crypto_aead_chacha20poly1305_ietf_NPUBBYTES], // Base pointer to public nonce
|
|
|
|
key.as_ptr() as *const [u8; crypto_aead_chacha20poly1305_ietf_KEYBYTES] // Base pointer to key
|
2015-11-30 16:27:50 +00:00
|
|
|
) };
|
|
|
|
match res {
|
|
|
|
0 => Ok(mlen as usize),
|
2016-07-06 19:14:09 +00:00
|
|
|
_ => Err(Error::Crypto("Failed to decrypt"))
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
},
|
2016-06-11 14:08:57 +00:00
|
|
|
Crypto::AES256GCM{ref state, ..} => {
|
2015-11-30 16:27:50 +00:00
|
|
|
let mut mlen: u64 = buf.len() as u64;
|
2015-11-30 21:11:37 +00:00
|
|
|
let res = unsafe { crypto_aead_aes256gcm_decrypt_afternm(
|
2015-11-30 16:27:50 +00:00
|
|
|
buf.as_mut_ptr(), // Base pointer to buffer
|
|
|
|
&mut mlen, // Mutable size of buffer (will be set to used size)
|
|
|
|
ptr::null_mut::<[u8; 0]>(), // Mutable base pointer to secret nonce (always NULL)
|
|
|
|
buf.as_ptr(), // Base pointer to message
|
|
|
|
buf.len() as u64, // Size of message
|
|
|
|
header.as_ptr(), // Base pointer to additional data
|
|
|
|
header.len() as u64, // Size of additional data
|
2015-12-01 09:13:11 +00:00
|
|
|
nonce.as_ptr() as *const [u8; crypto_aead_aes256gcm_NPUBBYTES], // Base pointer to public nonce
|
2015-11-30 21:11:37 +00:00
|
|
|
state.0 // Base pointer to state
|
2015-11-30 16:27:50 +00:00
|
|
|
) };
|
|
|
|
match res {
|
|
|
|
0 => Ok(mlen as usize),
|
2016-07-06 19:14:09 +00:00
|
|
|
_ => Err(Error::Crypto("Failed to decrypt"))
|
2015-11-30 16:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-01 21:55:15 +00:00
|
|
|
pub fn encrypt(&mut self, buf: &mut [u8], mlen: usize, nonce_bytes: &mut [u8], header: &[u8]) -> usize {
|
2016-06-11 14:08:57 +00:00
|
|
|
match *self {
|
|
|
|
Crypto::None => mlen,
|
|
|
|
Crypto::ChaCha20Poly1305{ref key, ref mut nonce} => {
|
2015-12-01 09:13:11 +00:00
|
|
|
inc_nonce_12(nonce);
|
2015-11-30 16:27:50 +00:00
|
|
|
let mut clen: u64 = buf.len() as u64;
|
2017-05-04 05:26:21 +00:00
|
|
|
assert_eq!(nonce_bytes.len(), nonce.len());
|
2015-12-01 09:13:11 +00:00
|
|
|
assert!(clen as usize >= mlen + crypto_aead_chacha20poly1305_ietf_ABYTES);
|
|
|
|
let res = unsafe { crypto_aead_chacha20poly1305_ietf_encrypt(
|
2015-11-30 16:27:50 +00:00
|
|
|
buf.as_mut_ptr(), // Base pointer to buffer
|
|
|
|
&mut clen, // Mutable size of buffer (will be set to used size)
|
|
|
|
buf.as_ptr(), // Base pointer to message
|
|
|
|
mlen as u64, // Size of message
|
|
|
|
header.as_ptr(), // Base pointer to additional data
|
|
|
|
header.len() as u64, // Size of additional data
|
|
|
|
ptr::null::<[u8; 0]>(), // Base pointer to secret nonce (always NULL)
|
2015-12-01 09:13:11 +00:00
|
|
|
nonce.as_ptr() as *const [u8; crypto_aead_chacha20poly1305_ietf_NPUBBYTES], // Base pointer to public nonce
|
|
|
|
key.as_ptr() as *const [u8; crypto_aead_chacha20poly1305_ietf_KEYBYTES] // Base pointer to key
|
2015-11-30 16:27:50 +00:00
|
|
|
) };
|
|
|
|
assert_eq!(res, 0);
|
|
|
|
unsafe {
|
|
|
|
ptr::copy_nonoverlapping(nonce.as_ptr(), nonce_bytes.as_mut_ptr(), nonce.len());
|
|
|
|
}
|
|
|
|
clen as usize
|
|
|
|
},
|
2016-06-11 14:08:57 +00:00
|
|
|
Crypto::AES256GCM{ref state, ref mut nonce} => {
|
2015-11-30 16:27:50 +00:00
|
|
|
inc_nonce_12(nonce);
|
|
|
|
let mut clen: u64 = buf.len() as u64;
|
2017-05-04 05:26:21 +00:00
|
|
|
assert_eq!(nonce_bytes.len(), nonce.len());
|
2015-11-30 16:27:50 +00:00
|
|
|
assert!(clen as usize >= mlen + crypto_aead_aes256gcm_ABYTES);
|
2015-11-30 21:11:37 +00:00
|
|
|
let res = unsafe { crypto_aead_aes256gcm_encrypt_afternm(
|
2015-11-30 16:27:50 +00:00
|
|
|
buf.as_mut_ptr(), // Base pointer to buffer
|
|
|
|
&mut clen, // Mutable size of buffer (will be set to used size)
|
|
|
|
buf.as_ptr(), // Base pointer to message
|
|
|
|
mlen as u64, // Size of message
|
|
|
|
header.as_ptr(), // Base pointer to additional data
|
|
|
|
header.len() as u64, // Size of additional data
|
|
|
|
ptr::null::<[u8; 0]>(), // Base pointer to secret nonce (always NULL)
|
2015-12-01 09:13:11 +00:00
|
|
|
nonce.as_ptr() as *const [u8; crypto_aead_aes256gcm_NPUBBYTES], // Base pointer to public nonce
|
2015-11-30 21:11:37 +00:00
|
|
|
state.0 // Base pointer to state
|
2015-11-30 16:27:50 +00:00
|
|
|
) };
|
|
|
|
assert_eq!(res, 0);
|
|
|
|
unsafe {
|
|
|
|
ptr::copy_nonoverlapping(nonce.as_ptr(), nonce_bytes.as_mut_ptr(), nonce.len());
|
|
|
|
}
|
|
|
|
clen as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|