Skip to content

Commit

Permalink
Merge pull request #376 from tox-rs/libsodium
Browse files Browse the repository at this point in the history
Update to sodiumoxide 0.2.2
  • Loading branch information
kpp authored May 18, 2019
2 parents fefeff0 + bcf27ea commit eabbe7a
Show file tree
Hide file tree
Showing 46 changed files with 324 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bytes = "0.4"
byteorder = "1"
futures = "0.1"
log = "0.4"
sodiumoxide = { git = "https://github.com/sodiumoxide/sodiumoxide.git", rev = "baf88e5" }
sodiumoxide = "0.2.2"
nom = "3.2"
cookie-factory = "0.2"
get_if_addrs = "0.5"
Expand Down
4 changes: 4 additions & 0 deletions examples/dht_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ fn bind_socket(addr: SocketAddr) -> UdpSocket {
fn main() {
env_logger::init();

if crypto_init().is_err() {
panic!("Crypto initialization failed.");
}

let (server_pk, server_sk) = gen_keypair();

// Create a channel for server to communicate with network
Expand Down
4 changes: 4 additions & 0 deletions src/toxcore/binary_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ macro_rules! encode_decode_test (
($test:ident, $value:expr) => (
#[test]
fn $test() {
use crate::toxcore::crypto_core::*;

crypto_init().unwrap();

let value = $value;
let mut buf = [0; 1024 * 1024];
let (_, size) = value.to_bytes((&mut buf, 0)).unwrap();
Expand Down
38 changes: 32 additions & 6 deletions src/toxcore/crypto_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ use crate::toxcore::binary_io::*;

// TODO: check if `#[inline]` is actually useful

/** Run before using crypto.
/// Initialize sodiumoxide
#[deprecated(
since = "0.0.10",
note = "libsodium is automatically initialized by sodiumoxide now."
)]
Runs [`sodiumoxide::init()`](../../../sodiumoxide/fn.init.html).
Returns `Ok` on success, `Err` otherwise.
E.g.
```
use ::tox::toxcore::crypto_core::crypto_init;
crypto_init().unwrap();
// second call should yield same result
crypto_init().unwrap();
```
*/
pub fn crypto_init() -> Result<(), ()> {
Ok(())
::sodiumoxide::init()
}


Expand Down Expand Up @@ -241,6 +251,7 @@ pub mod tests {
// test comparing random public keys
// testing since it would appear that sodiumoxide doesn't do testing for it
fn public_key_cmp_test_random() {
crypto_init().unwrap();
let (alice_publickey, _alice_secretkey) = gen_keypair();
let (bob_publickey, _bob_secretkey) = gen_keypair();

Expand All @@ -254,6 +265,7 @@ pub mod tests {

#[test]
fn random_u32_test() {
crypto_init().unwrap();
let a = random_u32();
let b = random_u32();
assert_ne!(a, 0);
Expand All @@ -265,6 +277,7 @@ pub mod tests {

#[test]
fn random_u64_test() {
crypto_init().unwrap();
let a = random_u64();
let b = random_u64();
assert_ne!(a, 0);
Expand All @@ -275,6 +288,7 @@ pub mod tests {

#[test]
fn random_usize_test() {
crypto_init().unwrap();
let a = random_usize();
let b = random_usize();
assert_ne!(a, 0);
Expand All @@ -285,12 +299,14 @@ pub mod tests {

#[test]
fn random_limit_usize_test() {
crypto_init().unwrap();
let n = random_limit_usize(7);
assert!(n < 7);
}

#[test]
fn public_key_valid_test() {
crypto_init().unwrap();
let (pk, _) = gen_keypair();
assert!(public_key_valid(&pk));

Expand All @@ -305,6 +321,7 @@ pub mod tests {
// test uses "bare" functions provided by `sodiumoxide`, with an exception
// of the tested function
fn encrypt_precompute_test() {
crypto_init().unwrap();
let (alice_pk, alice_sk) = gen_keypair();
let (bob_pk, bob_sk) = gen_keypair();

Expand All @@ -326,6 +343,7 @@ pub mod tests {
// test uses "bare" functions provided by `sodiumoxide`, with an "exception"
// of the tested function
fn encrypt_data_symmetric_test() {
crypto_init().unwrap();
let (alice_pk, alice_sk) = gen_keypair();
let (bob_pk, bob_sk) = gen_keypair();

Expand Down Expand Up @@ -356,6 +374,7 @@ pub mod tests {
// test uses "bare" functions provided by `sodiumoxide`, with an exception
// of the tested function
fn decrypt_data_symmetric_test() {
crypto_init().unwrap();
let (alice_pk, alice_sk) = gen_keypair();
let (bob_pk, bob_sk) = gen_keypair();

Expand All @@ -376,6 +395,7 @@ pub mod tests {

#[test]
fn increment_nonce_test_zero_plus_one() {
crypto_init().unwrap();
let cmp_nonce = Nonce([0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1]);
Expand All @@ -387,6 +407,7 @@ pub mod tests {

#[test]
fn increment_nonce_test_0xf_plus_one() {
crypto_init().unwrap();
let cmp_nonce = Nonce([0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0x10]);
Expand All @@ -400,6 +421,7 @@ pub mod tests {

#[test]
fn increment_nonce_test_0xff_plus_one() {
crypto_init().unwrap();
let cmp_nonce = Nonce([0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0]);
Expand All @@ -413,6 +435,7 @@ pub mod tests {

#[test]
fn increment_nonce_test_0xff_max() {
crypto_init().unwrap();
let cmp_nonce = Nonce([0; NONCEBYTES]);
let mut nonce = Nonce([0xff; NONCEBYTES]);
increment_nonce(&mut nonce);
Expand All @@ -421,6 +444,7 @@ pub mod tests {

#[test]
fn increment_nonce_test_random() {
crypto_init().unwrap();
let mut nonce = gen_nonce();
let cmp_nonce = nonce;
increment_nonce(&mut nonce);
Expand All @@ -431,6 +455,7 @@ pub mod tests {

#[test]
fn increment_nonce_number_test_zero_plus_0xff00() {
crypto_init().unwrap();
let cmp_nonce = Nonce([0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0xff, 0]);
Expand All @@ -442,6 +467,7 @@ pub mod tests {

#[test]
fn increment_nonce_number_test_0xff0000_plus_0x011000() {
crypto_init().unwrap();
let cmp_nonce = Nonce([0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0x10, 0]);
Expand Down
6 changes: 6 additions & 0 deletions src/toxcore/dht/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ mod tests {

#[test]
fn encode_decode() {
crypto_init().unwrap();
let test_packets = vec![
Packet::PingRequest(PingRequest {
pk: gen_keypair().0,
Expand Down Expand Up @@ -318,6 +319,7 @@ mod tests {

#[test]
fn decode_encrypted_packet_incomplete() {
crypto_init().unwrap();
let stats = Stats::new();
let mut codec = DhtCodec::new(stats);
let mut buf = BytesMut::new();
Expand All @@ -336,6 +338,7 @@ mod tests {

#[test]
fn decode_encrypted_packet_error() {
crypto_init().unwrap();
let stats = Stats::new();
let mut codec = DhtCodec::new(stats);
let mut buf = BytesMut::new();
Expand All @@ -350,6 +353,7 @@ mod tests {

#[test]
fn decode_encrypted_packet_zero_length() {
crypto_init().unwrap();
let stats = Stats::new();
let mut codec = DhtCodec::new(stats);
let mut buf = BytesMut::new();
Expand All @@ -362,6 +366,7 @@ mod tests {

#[test]
fn encode_packet_too_big() {
crypto_init().unwrap();
let stats = Stats::new();
let mut codec = DhtCodec::new(stats);
let mut buf = BytesMut::new();
Expand All @@ -378,6 +383,7 @@ mod tests {

#[test]
fn codec_is_clonable() {
crypto_init().unwrap();
let stats = Stats::new();
let codec = DhtCodec::new(stats);
let _codec_c = codec.clone();
Expand Down
1 change: 1 addition & 0 deletions src/toxcore/dht/daemon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ mod tests {

#[test]
fn daemon_state_serialize_deserialize() {
crypto_init().unwrap();
let (pk, sk) = gen_keypair();
let (tx, rx) = mpsc::channel(1);
let alice = Server::new(tx, pk, sk);
Expand Down
5 changes: 5 additions & 0 deletions src/toxcore/dht/dht_friend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ mod tests {

#[test]
fn addr_is_unknown() {
crypto_init().unwrap();
let pk = gen_keypair().0;
let mut friend = DhtFriend::new(pk);

Expand All @@ -117,6 +118,7 @@ mod tests {

#[test]
fn addr_is_known() {
crypto_init().unwrap();
let pk = gen_keypair().0;
let mut friend = DhtFriend::new(pk);

Expand All @@ -130,6 +132,7 @@ mod tests {

#[test]
fn get_returned_addrs() {
crypto_init().unwrap();
let pk = gen_keypair().0;
let mut friend = DhtFriend::new(pk);

Expand Down Expand Up @@ -162,6 +165,7 @@ mod tests {

#[test]
fn get_returned_addrs_timed_out() {
crypto_init().unwrap();
let pk = gen_keypair().0;
let mut friend = DhtFriend::new(pk);

Expand Down Expand Up @@ -194,6 +198,7 @@ mod tests {

#[test]
fn can_and_try_add_to_close() {
crypto_init().unwrap();
let pk = PublicKey([0; PUBLICKEYBYTES]);
let mut friend = DhtFriend::new(pk);

Expand Down
1 change: 1 addition & 0 deletions src/toxcore/dht/dht_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ mod tests {

#[test]
fn dht_node_clonable() {
crypto_init().unwrap();
let pn = PackedNode {
pk: gen_keypair().0,
saddr: "127.0.0.1:33445".parse().unwrap(),
Expand Down
2 changes: 2 additions & 0 deletions src/toxcore/dht/kbucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ mod tests {

#[test]
fn kbucket_get_node() {
crypto_init().unwrap();
let (pk, _) = gen_keypair();
let mut kbucket = Kbucket::<DhtNode>::new(KBUCKET_DEFAULT_SIZE);

Expand All @@ -580,6 +581,7 @@ mod tests {

#[test]
fn kbucket_get_node_mut() {
crypto_init().unwrap();
let (pk, _) = gen_keypair();
let mut kbucket = Kbucket::<DhtNode>::new(KBUCKET_DEFAULT_SIZE);

Expand Down
4 changes: 4 additions & 0 deletions src/toxcore/dht/ktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ mod tests {

#[test]
fn ktree_new() {
crypto_init().unwrap();
let pk = gen_keypair().0;
let ktree = Ktree::new(&pk);
assert_eq!(pk, ktree.pk);
Expand Down Expand Up @@ -332,6 +333,7 @@ mod tests {

#[test]
fn ktree_contains() {
crypto_init().unwrap();
let (pk, _) = gen_keypair();
let mut ktree = Ktree::new(&pk);

Expand All @@ -351,6 +353,7 @@ mod tests {

#[test]
fn ktree_can_add() {
crypto_init().unwrap();
let (pk, _) = gen_keypair();
let mut ktree = Ktree::new(&pk);

Expand Down Expand Up @@ -416,6 +419,7 @@ mod tests {

#[test]
fn ktree_is_all_discarded() {
crypto_init().unwrap();
let (pk, _) = gen_keypair();
let mut ktree = Ktree::new(&pk);

Expand Down
3 changes: 3 additions & 0 deletions src/toxcore/dht/lan_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ mod tests {

#[test]
fn send_ipv4() {
crypto_init().unwrap();
// `+1` for 255.255.255.255
let packets_count = (broadcast_addrs_count() + 1) * (PORTS_PER_DISCOVERY + 1) as usize;

Expand All @@ -206,6 +207,7 @@ mod tests {

#[test]
fn send_ipv6() {
crypto_init().unwrap();
// `+2` for ::1 and ::ffff:255.255.255.255
let packets_count = (broadcast_addrs_count() + 2) * (PORTS_PER_DISCOVERY + 1) as usize;

Expand All @@ -231,6 +233,7 @@ mod tests {

#[test]
fn cycle_around_ports() {
crypto_init().unwrap();
// `+1` for 255.255.255.255
let packets_count = (broadcast_addrs_count() + 1) * (PORTS_PER_DISCOVERY + 1) as usize;

Expand Down
Loading

0 comments on commit eabbe7a

Please sign in to comment.