use an external libsodium-sys dep

This commit is contained in:
Y. T. Chung
2015-01-27 17:17:17 +08:00
parent 6c149962cb
commit ac4d8db159
3 changed files with 11 additions and 16 deletions
+5 -1
View File
@@ -40,7 +40,7 @@ cipher-chacha20 = ["enable-sodium"]
cipher-salsa20 = ["enable-sodium"]
enable-udp = []
enable-sodium = []
enable-sodium = ["libsodium-sys"]
[[bin]]
@@ -56,3 +56,7 @@ path = "src/bin/server.rs"
collect = "*"
rustc-serialize = "*"
log = "*"
[dependencies.libsodium-sys]
git = "https://github.com/zonyitoo/libsodium-sys.git"
optional = true
+4 -15
View File
@@ -30,19 +30,8 @@ use crypto::cipher::{Cipher, CipherType, CipherResult};
const BLOCK_SIZE: usize = 64; // Just for Salsa20 and Chacha20
mod ffi {
extern crate libc;
#[allow(dead_code)]
#[link(name = "sodium")]
extern {
pub fn crypto_stream_salsa20_xor_ic(c: *mut libc::c_uchar, m: *const libc::c_uchar,
mlen: libc::c_ulonglong, n: *const libc::c_uchar,
ic: libc::uint64_t, k: *const libc::c_uchar) -> libc::c_int;
pub fn crypto_stream_chacha20_xor_ic(c: *mut libc::c_uchar, m: *const libc::c_uchar,
mlen: libc::c_ulonglong, n: *const libc::c_uchar,
ic: libc::uint64_t, k: *const libc::c_uchar) -> libc::c_int;
}
pub use libsodium_ffi::crypto_stream_chacha20_xor_ic;
pub use libsodium_ffi::crypto_stream_salsa20_xor_ic;
}
pub struct SodiumCipher {
@@ -88,14 +77,14 @@ impl Cipher for SodiumCipher {
match self.cipher_type {
CipherType::ChaCha20 => unsafe {
ffi::crypto_stream_chacha20_xor_ic(self.buf.as_mut_ptr(), padded_data.as_ptr(),
ffi::crypto_stream_chacha20_xor_ic(self.buf.as_mut_ptr() as *mut libc::c_char, padded_data.as_ptr(),
(padding_len + data.len()) as libc::c_ulonglong,
self.iv.as_ptr(),
(self.counter / BLOCK_SIZE) as libc::uint64_t,
self.key.as_ptr());
},
CipherType::Salsa20 => unsafe {
ffi::crypto_stream_salsa20_xor_ic(self.buf.as_mut_ptr(), padded_data.as_ptr(),
ffi::crypto_stream_salsa20_xor_ic(self.buf.as_mut_ptr() as *mut libc::c_char, padded_data.as_ptr(),
(padding_len + data.len()) as libc::c_ulonglong,
self.iv.as_ptr(),
(self.counter / BLOCK_SIZE) as libc::uint64_t,
+2
View File
@@ -33,6 +33,8 @@ extern crate "rustc-serialize" as serialize;
extern crate log;
extern crate collect;
extern crate "libsodium-sys" as libsodium_ffi;
use std::fmt::{Debug, Formatter, self};
/// VersionCode(major, minor, patch)