Skip to content

Commit f31f6aa

Browse files
committed
feat(rand): require CryptoRng
1 parent bb6d910 commit f31f6aa

File tree

8 files changed

+18
-17
lines changed

8 files changed

+18
-17
lines changed

tox_core/src/dht/dht_friend.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Module for friend.
44

55
use std::time::Instant;
66
use std::net::SocketAddr;
7-
use rand::Rng;
7+
use rand::{CryptoRng, Rng};
88

99
use crate::time::*;
1010
use crate::dht::kbucket::*;
@@ -41,7 +41,7 @@ pub struct DhtFriend {
4141

4242
impl DhtFriend {
4343
/// Create new `DhtFriend`.
44-
pub fn new<R: Rng>(rng: &mut R, pk: PublicKey) -> Self {
44+
pub fn new<R: Rng + CryptoRng>(rng: &mut R, pk: PublicKey) -> Self {
4545
DhtFriend {
4646
pk,
4747
close_nodes: Kbucket::new(FRIEND_CLOSE_NODES_COUNT),

tox_core/src/dht/request_queue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::collections::HashMap;
44
use std::collections::hash_map::Entry;
55
use std::time::{Duration, Instant};
6-
use rand::Rng;
6+
use rand::{CryptoRng, Rng};
77

88
use crate::utils::gen_ping_id;
99
use crate::time::*;
@@ -29,7 +29,7 @@ impl<T> RequestQueue<T> {
2929
}
3030

3131
/// Generate unique non zero request ID.
32-
fn generate_ping_id<R: Rng>(&self, rng: &mut R) -> u64 {
32+
fn generate_ping_id<R: Rng + CryptoRng>(&self, rng: &mut R) -> u64 {
3333
loop {
3434
let ping_id = gen_ping_id(rng);
3535
if !self.ping_map.contains_key(&ping_id) {
@@ -40,7 +40,7 @@ impl<T> RequestQueue<T> {
4040

4141
/// Generate and store unique non zero request ID. Later this request ID can
4242
/// be verified with `check_ping_id` function.
43-
pub fn new_ping_id<R: Rng>(&mut self, rng: &mut R, data: T) -> u64 {
43+
pub fn new_ping_id<R: Rng + CryptoRng>(&mut self, rng: &mut R, data: T) -> u64 {
4444
let ping_id = self.generate_ping_id(rng);
4545
self.ping_map.insert(ping_id, (clock_now(), data));
4646
ping_id

tox_core/src/dht/server/hole_punching.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ https://zetok.github.io/tox-spec/#hole-punching
77
use std::net::{IpAddr, SocketAddr};
88
use std::time::{Duration, Instant};
99
use std::collections::HashMap;
10-
use rand::Rng;
10+
use rand::{CryptoRng, Rng};
1111

1212
use crate::dht::dht_friend::*;
1313
use crate::dht::server::*;
@@ -58,7 +58,7 @@ pub struct HolePunching {
5858

5959
impl HolePunching {
6060
/// Create new `HolePunching` object.
61-
pub fn new<R: Rng>(rng: &mut R) -> Self {
61+
pub fn new<R: Rng + CryptoRng>(rng: &mut R) -> Self {
6262
HolePunching {
6363
is_punching_done: true,
6464
num_punch_tries: 0,

tox_core/src/net_crypto/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,7 @@ mod tests {
11371137
// https://github.com/rust-lang/rust/issues/61520
11381138
use super::{*, Packet};
11391139
use futures::{Future, StreamExt};
1140+
use rand::CryptoRng;
11401141

11411142
impl NetCrypto {
11421143
pub async fn has_friend(&self, pk: &PublicKey) -> bool {
@@ -1177,7 +1178,7 @@ mod tests {
11771178
self.connections.write().await.insert(peer_real_pk, Arc::new(RwLock::new(connection)));
11781179
}
11791180

1180-
pub fn get_cookie<R: Rng>(&self, rng: &mut R, real_pk: PublicKey, dht_pk: PublicKey) -> EncryptedCookie {
1181+
pub fn get_cookie<R: Rng + CryptoRng>(&self, rng: &mut R, real_pk: PublicKey, dht_pk: PublicKey) -> EncryptedCookie {
11811182
let cookie = Cookie::new(real_pk, dht_pk);
11821183
EncryptedCookie::new(rng, &self.symmetric_key, &cookie)
11831184
}

tox_core/src/onion/onion_announce.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::net::{IpAddr, SocketAddr};
66
use std::time::{Duration, Instant, SystemTime};
77
use sha2::{Digest, Sha256};
88
use sha2::digest::generic_array::typenum::marker_traits::Unsigned;
9-
use rand::Rng;
9+
use rand::{CryptoRng, Rng};
1010

1111
use tox_binary_io::*;
1212
use tox_crypto::*;
@@ -165,7 +165,7 @@ pub struct OnionAnnounce {
165165

166166
impl OnionAnnounce {
167167
/// Create new `OnionAnnounce` instance.
168-
pub fn new<R: Rng>(rng: &mut R, dht_pk: PublicKey) -> OnionAnnounce {
168+
pub fn new<R: Rng + CryptoRng>(rng: &mut R, dht_pk: PublicKey) -> OnionAnnounce {
169169
OnionAnnounce {
170170
secret_bytes: rng.gen(),
171171
entries: Vec::with_capacity(ONION_ANNOUNCE_MAX_ENTRIES),

tox_core/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*! Common utility functions
22
*/
33

4-
use rand::Rng;
4+
use rand::{CryptoRng, Rng};
55

66
/// Generate non-zero ping_id
7-
pub fn gen_ping_id<R: Rng>(rng: &mut R) -> u64 {
7+
pub fn gen_ping_id<R: Rng + CryptoRng>(rng: &mut R) -> u64 {
88
let mut ping_id = 0;
99
while ping_id == 0 {
1010
ping_id = rng.gen();

tox_packet/src/dht/cookie.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use nom::{AsBytes, map_opt, number::complete::be_u64};
66
use sha2::{Digest, Sha512};
77
use sha2::digest::generic_array::typenum::marker_traits::Unsigned;
88
use xsalsa20poly1305::{XSalsa20Poly1305, aead::{Aead, Error as AeadError}};
9-
use rand::Rng;
9+
use rand::{CryptoRng, Rng};
1010

1111
use std::{convert::TryInto, time::SystemTime};
1212

@@ -131,7 +131,7 @@ impl ToBytes for EncryptedCookie {
131131

132132
impl EncryptedCookie {
133133
/// Create `EncryptedCookie` from `Cookie` encrypting it with `symmetric_key`
134-
pub fn new<R: Rng>(rng: &mut R, symmetric_key: &XSalsa20Poly1305, payload: &Cookie) -> EncryptedCookie {
134+
pub fn new<R: Rng + CryptoRng>(rng: &mut R, symmetric_key: &XSalsa20Poly1305, payload: &Cookie) -> EncryptedCookie {
135135
let nonce = rng.gen::<[u8; xsalsa20poly1305::NONCE_SIZE]>().into();
136136
let mut buf = [0; 72];
137137
let (_, size) = payload.to_bytes((&mut buf, 0)).unwrap();

tox_packet/src/toxid.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use nom::{
1212
named,
1313
do_parse, map, call, take,
1414
};
15-
use rand::{Rng, distributions::{Distribution, Standard}};
15+
use rand::{CryptoRng, Rng, distributions::{Distribution, Standard}};
1616
use cookie_factory::{do_gen, gen_slice};
1717

1818
use tox_binary_io::*;
@@ -178,7 +178,7 @@ impl ToxId {
178178
let _toxid = ToxId::new(&mut rng, pk);
179179
```
180180
*/
181-
pub fn new<R: Rng>(rng: &mut R, pk: PublicKey) -> Self {
181+
pub fn new<R: Rng + CryptoRng>(rng: &mut R, pk: PublicKey) -> Self {
182182
let nospam = rng.gen();
183183
let checksum = Self::checksum(&pk, nospam);
184184
ToxId { pk, nospam, checksum }
@@ -215,7 +215,7 @@ impl ToxId {
215215
*/
216216
// TODO: more tests
217217
// TODO: ↓ split into `new_nospam()` and `set_nospam(NoSpam)` ?
218-
pub fn new_nospam<R: Rng>(&mut self, rng: &mut R, nospam: Option<NoSpam>) {
218+
pub fn new_nospam<R: Rng + CryptoRng>(&mut self, rng: &mut R, nospam: Option<NoSpam>) {
219219
if let Some(nospam) = nospam {
220220
self.nospam = nospam;
221221
} else {

0 commit comments

Comments
 (0)