From ac4d8db159e29d07f5a1665bc06bb00259708871 Mon Sep 17 00:00:00 2001 From: "Y. T. Chung" Date: Tue, 27 Jan 2015 17:17:17 +0800 Subject: [PATCH] use an external libsodium-sys dep --- Cargo.toml | 6 +++++- src/crypto/sodium.rs | 19 ++++--------------- src/lib.rs | 2 ++ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d2d2d91a..7c19d38a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/crypto/sodium.rs b/src/crypto/sodium.rs index f10c0128..e3a30297 100644 --- a/src/crypto/sodium.rs +++ b/src/crypto/sodium.rs @@ -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, diff --git a/src/lib.rs b/src/lib.rs index e9de85a9..9fe999bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)