Skip to content

Commit 7c4bbe0

Browse files
Upgrade crypto_box and xsalsa20poly1305
1 parent e868983 commit 7c4bbe0

File tree

7 files changed

+37
-30
lines changed

7 files changed

+37
-30
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ maintenance = { status = "passively-maintained" }
1717

1818
[dependencies]
1919
byteorder = "1.1"
20-
crypto_box = { version = "0.7.1", features = ["serde"] }
20+
crypto_box = { version = "0.9.1", features = ["serde"] }
2121
data-encoding = "2.1"
2222
failure = "0.1"
2323
futures = "0.1.0" # Make sure to use same version as websocket
@@ -31,7 +31,7 @@ serde = { version = "1", features = ["derive"] }
3131
tokio-core = "0.1"
3232
tokio-timer = "0.1"
3333
websocket = { version = "0.26", default-features = false, features = ["async", "async-ssl"] }
34-
xsalsa20poly1305 = "0.8"
34+
xsalsa20poly1305 = "0.9"
3535

3636
[dev-dependencies]
3737
anyhow = "1"

src/crypto_types.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::io::Write;
88
use std::{cmp, convert::TryInto, fmt};
99

1010
use crypto_box::{
11-
aead::{generic_array::GenericArray, Aead, NewAead},
12-
rand_core::OsRng,
11+
aead::{generic_array::GenericArray, Aead, KeyInit, OsRng},
12+
SalsaBox,
1313
};
1414
use data_encoding::{HEXLOWER, HEXLOWER_PERMISSIVE};
1515
use serde::{
@@ -141,7 +141,7 @@ impl KeyPair {
141141
/// Warning: Be careful with this! The only reason to access the private
142142
/// key is probably to be able to restore it when working with trusted keys.
143143
pub fn private_key_hex(&self) -> String {
144-
HEXLOWER.encode(self.private_key.as_bytes())
144+
HEXLOWER.encode(&self.private_key.to_bytes())
145145
}
146146

147147
/// Encrypt data for the specified public key with the private key.
@@ -151,7 +151,7 @@ impl KeyPair {
151151
nonce: Nonce,
152152
other_key: &PublicKey,
153153
) -> SignalingResult<Vec<u8>> {
154-
let cbox = crypto_box::Box::new(other_key, &self.private_key);
154+
let cbox = SalsaBox::new(other_key, &self.private_key);
155155
cbox.encrypt(&nonce.into(), data)
156156
.map_err(|_| SignalingError::Crypto("Could not encrypt data".to_string()))
157157
}
@@ -167,7 +167,7 @@ impl KeyPair {
167167
nonce: Nonce,
168168
other_key: &PublicKey,
169169
) -> SignalingResult<Vec<u8>> {
170-
let cbox = crypto_box::Box::new(other_key, &self.private_key);
170+
let cbox = SalsaBox::new(other_key, &self.private_key);
171171
cbox.decrypt(&nonce.into(), data)
172172
.map_err(|_| SignalingError::Crypto("Could not decrypt data".to_string()))
173173
}
@@ -287,7 +287,7 @@ impl UnsignedKeys {
287287
(&mut bytes[32..64])
288288
.write_all(self.client_public_permanent_key.as_bytes())
289289
.unwrap();
290-
let cbox = crypto_box::Box::new(
290+
let cbox = SalsaBox::new(
291291
client_public_permanent_key,
292292
server_session_keypair.private_key(),
293293
);
@@ -315,7 +315,7 @@ impl SignedKeys {
315315
nonce: Nonce,
316316
) -> SignalingResult<UnsignedKeys> {
317317
// Decrypt bytes
318-
let cbox = crypto_box::Box::new(server_public_permanent_key, permanent_key.private_key());
318+
let cbox = SalsaBox::new(server_public_permanent_key, permanent_key.private_key());
319319
let decrypted = cbox
320320
.decrypt(&nonce.into(), &self.0[..])
321321
.map_err(|_| SignalingError::Crypto("Could not decrypt signed keys".to_string()))?;
@@ -413,7 +413,7 @@ use crate::test_helpers::TestRandom;
413413
#[cfg(test)]
414414
impl TestRandom for PublicKey {
415415
fn random() -> PublicKey {
416-
let mut rng = crypto_box::rand_core::OsRng;
416+
let mut rng = crypto_box::aead::OsRng;
417417
let private_key = PrivateKey::generate(&mut rng);
418418
private_key.public_key()
419419
}
@@ -431,7 +431,7 @@ mod tests {
431431
let ks1 = KeyPair::new();
432432
let ks2 = KeyPair::new();
433433
assert_ne!(ks1.public_key(), ks2.public_key());
434-
assert_ne!(ks1.private_key().as_bytes(), ks2.private_key().as_bytes());
434+
assert_ne!(ks1.private_key().to_bytes(), ks2.private_key().to_bytes());
435435
}
436436
}
437437

@@ -633,7 +633,7 @@ mod tests {
633633
.sign(&kp_server, kp_client.public_key(), unsafe { nonce.clone() });
634634

635635
// Decrypt directly
636-
let cbox = crypto_box::Box::new(kp_server.public_key(), kp_client.private_key());
636+
let cbox = SalsaBox::new(kp_server.public_key(), kp_client.private_key());
637637
let decrypted = cbox
638638
.decrypt(&unsafe { nonce.clone() }.into(), &signed.0[..])
639639
.unwrap();

src/protocol/cookie.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fmt;
44

5-
use crypto_box::rand_core::{OsRng, RngCore};
5+
use crypto_box::aead::{OsRng, rand_core::RngCore};
66
use serde::{
77
de::{Deserialize, Deserializer, Error as SerdeError, Visitor},
88
ser::{Serialize, Serializer},

src/protocol/csn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use std::cmp;
88

9-
use crypto_box::rand_core::{OsRng, RngCore};
9+
use crypto_box::aead::{OsRng, rand_core::RngCore};
1010

1111
use crate::errors::{SignalingError, SignalingResult};
1212

src/protocol/messages.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl ClientHello {
140140
/// Create a new instance with dummy data. Used in testing.
141141
#[cfg(test)]
142142
pub(crate) fn random() -> Self {
143-
use crypto_box::rand_core::{OsRng, RngCore};
143+
use crypto_box::aead::{OsRng, rand_core::RngCore};
144144
let mut bytes = [0u8; 32];
145145
OsRng.fill_bytes(&mut bytes);
146146
Self {
@@ -164,7 +164,7 @@ impl ServerHello {
164164
/// Create a new instance with dummy data. Used in testing.
165165
#[cfg(test)]
166166
pub(crate) fn random() -> Self {
167-
use crypto_box::rand_core::{OsRng, RngCore};
167+
use crypto_box::aead::{OsRng, rand_core::RngCore};
168168
let mut bytes = [0u8; 32];
169169
OsRng.fill_bytes(&mut bytes);
170170
Self {
@@ -304,7 +304,7 @@ impl Token {
304304
/// Create a new instance with dummy data. Used in testing.
305305
#[cfg(test)]
306306
pub(crate) fn random() -> Self {
307-
use crypto_box::rand_core::{OsRng, RngCore};
307+
use crypto_box::aead::{OsRng, rand_core::RngCore};
308308
let mut bytes = [0u8; 32];
309309
OsRng.fill_bytes(&mut bytes);
310310
Self {
@@ -325,7 +325,7 @@ impl Key {
325325
/// Create a new instance with dummy data. Used in testing.
326326
#[cfg(test)]
327327
pub(crate) fn random() -> Self {
328-
use crypto_box::rand_core::{OsRng, RngCore};
328+
use crypto_box::aead::{OsRng, rand_core::RngCore};
329329
let mut bytes = [0u8; 32];
330330
OsRng.fill_bytes(&mut bytes);
331331
Self {

src/protocol/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ use std::{
1515
time::Duration,
1616
};
1717

18-
use crypto_box::aead::{
19-
generic_array::{typenum::U24, GenericArray},
20-
Aead,
18+
use crypto_box::{
19+
aead::{
20+
generic_array::{typenum::U24, GenericArray},
21+
Aead,
22+
},
23+
SalsaBox,
2124
};
2225
use rmpv::Value;
2326

@@ -924,7 +927,7 @@ pub(crate) trait Signaling {
924927

925928
// Raw encryption / decryption
926929

927-
fn get_crypto_box(&self) -> SignalingResult<crypto_box::Box> {
930+
fn get_crypto_box(&self) -> SignalingResult<SalsaBox> {
928931
let peer = self.get_peer().ok_or_else(|| SignalingError::NoPeer)?;
929932
let peer_session_public_key = peer
930933
.session_key()
@@ -933,7 +936,7 @@ pub(crate) trait Signaling {
933936
.keypair()
934937
.map(|keypair: &KeyPair| keypair.private_key())
935938
.ok_or_else(|| SignalingError::Crash("Our session private key not set".into()))?;
936-
Ok(crypto_box::Box::new(
939+
Ok(SalsaBox::new(
937940
peer_session_public_key,
938941
our_session_private_key,
939942
))

src/protocol/tests/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Protocol tests.
2-
use crypto_box::{generate_nonce, rand_core::OsRng};
2+
use crypto_box::{aead::OsRng, SalsaBox};
3+
use xsalsa20poly1305::XSalsa20Poly1305;
34

45
use crate::{
56
crypto::PrivateKey,
@@ -176,7 +177,7 @@ fn test_encrypt_decrypt_raw_with_session_keys_no_peer() {
176177
None,
177178
None,
178179
);
179-
let nonce = generate_nonce(&mut OsRng);
180+
let nonce = XSalsa20Poly1305::generate_nonce(&mut OsRng);
180181
assert_eq!(
181182
signaling.encrypt_raw_with_session_keys(&[1, 2, 3], &nonce),
182183
Err(SignalingError::NoPeer)
@@ -194,7 +195,7 @@ fn test_encrypt_raw_with_session_keys_with_peer() {
194195
let peer_kp = KeyPair::new();
195196
let our_kp = KeyPair::new();
196197
let our_private_key_clone = our_kp.private_key().clone();
197-
let nonce = generate_nonce(&mut OsRng);
198+
let nonce = XSalsa20Poly1305::generate_nonce(&mut OsRng);
198199

199200
// Create signaling instance
200201
let mut signaling = MockSignaling::new(
@@ -215,7 +216,7 @@ fn test_encrypt_raw_with_session_keys_with_peer() {
215216
assert_ne!(&data, ciphertext.as_slice());
216217

217218
// Verify
218-
let cbox = crypto_box::Box::new(peer_kp.public_key(), &our_private_key_clone);
219+
let cbox = SalsaBox::new(peer_kp.public_key(), &our_private_key_clone);
219220
assert_eq!(cbox.decrypt(&nonce, &*ciphertext), Ok(vec![2, 3, 4, 5]));
220221
}
221222

@@ -266,12 +267,12 @@ fn test_decrypt_raw_with_session_keys_with_peer() {
266267
// Generate keypairs and nonce
267268
let peer_kp = KeyPair::new();
268269
let our_kp = KeyPair::new();
269-
let nonce = generate_nonce(&mut OsRng);
270+
let nonce = XSalsa20Poly1305::generate_nonce(&mut OsRng);
270271

271272
// Encrypt data
272273
let data = [1, 2, 3, 4];
273274

274-
let cbox = crypto_box::Box::new(peer_kp.public_key(), our_kp.private_key());
275+
let cbox = SalsaBox::new(peer_kp.public_key(), our_kp.private_key());
275276
let ciphertext = cbox.encrypt(&nonce, &data[..]).unwrap();
276277

277278
// Create signaling instance
@@ -287,7 +288,10 @@ fn test_decrypt_raw_with_session_keys_with_peer() {
287288

288289
// Decrypt with wrong nonce
289290
assert_eq!(
290-
signaling.decrypt_raw_with_session_keys(&ciphertext, &generate_nonce(&mut OsRng)),
291+
signaling.decrypt_raw_with_session_keys(
292+
&ciphertext,
293+
&XSalsa20Poly1305::generate_nonce(&mut OsRng)
294+
),
291295
Err(SignalingError::Crypto("Could not decrypt bytes".into()))
292296
);
293297

0 commit comments

Comments
 (0)