Skip to content

Commit

Permalink
Driver: Replace xsalsa20poly1305 with crypto_secretbox (#198)
Browse files Browse the repository at this point in the history
As of v0.9.1, `xsalsa20poly1305` has been deprecated. This is a mostly seamless replacement, as it appears to be the same crate authors / code / etc.

Co-authored-by: Kyle Simpson <[email protected]>
  • Loading branch information
Sebbl0508 and FelixMcFelix committed Nov 20, 2023
1 parent 5ddc8f4 commit 77a9b46
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async-trait = { optional = true, version = "0.1" }
audiopus = { optional = true, version = "0.3.0-rc.0" }
byteorder = { optional = true, version = "1" }
bytes = { optional = true, version = "1" }
crypto_secretbox = { optional = true, features = ["std"], version = "0.1" }
dashmap = { optional = true, version = "5" }
derivative = "2"
discortp = { default-features = false, features = ["discord", "pnet", "rtp"], optional = true, version = "0.5" }
Expand Down Expand Up @@ -50,7 +51,6 @@ twilight-model = { default-features = false, optional = true, version = "0.15.0"
typemap_rev = { optional = true, version = "0.3" }
url = { optional = true, version = "2" }
uuid = { features = ["v4"], optional = true, version = "1" }
xsalsa20poly1305 = { features = ["std"], optional = true, version = "0.9" }

[dependencies.serenity]
version = "0.11"
Expand Down Expand Up @@ -91,6 +91,7 @@ driver = [
"dep:async-trait",
"dep:audiopus",
"dep:byteorder",
"dep:crypto_secretbox",
"dep:discortp",
"dep:reqwest",
"dep:flume",
Expand All @@ -113,7 +114,6 @@ driver = [
"dep:typemap_rev",
"dep:url",
"dep:uuid",
"dep:xsalsa20poly1305",
"tokio?/fs",
"tokio?/io-util",
"tokio?/macros",
Expand Down
2 changes: 1 addition & 1 deletion src/driver/connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::{
driver::tasks::{error::Recipient, message::*},
ws::Error as WsError,
};
use crypto_secretbox::Error as CryptoError;
use flume::SendError;
use serde_json::Error as JsonError;
use std::{error::Error as StdError, fmt, io::Error as IoError};
use tokio::time::error::Elapsed;
use xsalsa20poly1305::aead::Error as CryptoError;

/// Errors encountered while connecting to a Discord voice server over the driver.
#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/driver/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
ws::WsStream,
ConnectionInfo,
};
use crypto_secretbox::{KeyInit, XSalsa20Poly1305 as Cipher};
use discortp::discord::{IpDiscoveryPacket, IpDiscoveryType, MutableIpDiscoveryPacket};
use error::{Error, Result};
use flume::Sender;
Expand All @@ -30,7 +31,6 @@ use std::{net::IpAddr, str::FromStr};
use tokio::{net::UdpSocket, spawn, time::timeout};
use tracing::{debug, info, instrument};
use url::Url;
use xsalsa20poly1305::{KeyInit, XSalsa20Poly1305 as Cipher};

pub(crate) struct Connection {
pub(crate) info: ConnectionInfo,
Expand Down
20 changes: 12 additions & 8 deletions src/driver/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
//! Encryption schemes supported by Discord's secure RTP negotiation.
use byteorder::{NetworkEndian, WriteBytesExt};
use discortp::{rtp::RtpPacket, MutablePacket};
use rand::Rng;
use std::num::Wrapping;
#[cfg(any(feature = "receive", test))]
use xsalsa20poly1305::Tag;
use xsalsa20poly1305::{
use crypto_secretbox::Tag;
use crypto_secretbox::{
aead::{AeadInPlace, Error as CryptoError},
Nonce,
SecretBox,
XSalsa20Poly1305 as Cipher,
NONCE_SIZE,
TAG_SIZE,
};
use discortp::{rtp::RtpPacket, MutablePacket};
use rand::Rng;
use std::num::Wrapping;

#[cfg(test)]
pub const KEY_SIZE: usize = SecretBox::<()>::KEY_SIZE;
pub const NONCE_SIZE: usize = SecretBox::<()>::NONCE_SIZE;
pub const TAG_SIZE: usize = SecretBox::<()>::TAG_SIZE;

/// Variants of the `XSalsa20Poly1305` encryption scheme.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -253,8 +257,8 @@ impl CryptoState {
#[cfg(test)]
mod test {
use super::*;
use crypto_secretbox::KeyInit;
use discortp::rtp::MutableRtpPacket;
use xsalsa20poly1305::{KeyInit, KEY_SIZE, TAG_SIZE};

#[test]
fn small_packet_decrypts_error() {
Expand Down
2 changes: 1 addition & 1 deletion src/driver/tasks/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::message::*;
use crate::ws::Error as WsError;
use audiopus::Error as OpusError;
use crypto_secretbox::aead::Error as CryptoError;
use flume::SendError;
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use xsalsa20poly1305::aead::Error as CryptoError;

#[derive(Debug)]
pub enum Recipient {
Expand Down
2 changes: 1 addition & 1 deletion src/driver/tasks/message/mixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use crate::{
driver::{Bitrate, Config, CryptoState},
input::{AudioStreamError, Compose, Parsed},
};
use crypto_secretbox::XSalsa20Poly1305 as Cipher;
use flume::Sender;
use std::{net::UdpSocket, sync::Arc};
use symphonia_core::{errors::Error as SymphoniaError, formats::SeekedTo};
use xsalsa20poly1305::XSalsa20Poly1305 as Cipher;

pub struct MixerConnection {
pub cipher: Cipher,
Expand Down
2 changes: 1 addition & 1 deletion src/driver/tasks/mixer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::{
error::{Error, Result},
message::*,
};
use crate::driver::crypto::TAG_SIZE;
use crate::{
constants::*,
driver::MixMode,
Expand Down Expand Up @@ -53,7 +54,6 @@ use symphonia_core::{
};
use tokio::runtime::Handle;
use tracing::error;
use xsalsa20poly1305::TAG_SIZE;

#[cfg(test)]
use crate::driver::test_config::{OutputMessage, OutputMode};
Expand Down
2 changes: 1 addition & 1 deletion src/driver/tasks/udp_rx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
Config,
};
use bytes::BytesMut;
use crypto_secretbox::XSalsa20Poly1305 as Cipher;
use discortp::{
demux::{self, DemuxedMut},
rtp::RtpPacket,
Expand All @@ -25,7 +26,6 @@ use std::{
};
use tokio::{net::UdpSocket, select, time::Instant};
use tracing::{error, instrument, trace, warn};
use xsalsa20poly1305::XSalsa20Poly1305 as Cipher;

type RtpSequence = Wrapping<u16>;
type RtpTimestamp = Wrapping<u32>;
Expand Down
3 changes: 2 additions & 1 deletion src/driver/test_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::{
constants::*,
driver::crypto::KEY_SIZE,
input::{
cached::Compressed,
codecs::{CODEC_REGISTRY, PROBE},
Expand All @@ -10,10 +11,10 @@ use crate::{
test_utils,
tracks::LoopState,
};
use crypto_secretbox::{KeyInit, XSalsa20Poly1305 as Cipher};
use flume::{Receiver, Sender};
use std::{io::Cursor, net::UdpSocket, sync::Arc};
use tokio::runtime::Handle;
use xsalsa20poly1305::{KeyInit, XSalsa20Poly1305 as Cipher, KEY_SIZE};

use super::{
scheduler::*,
Expand Down

0 comments on commit 77a9b46

Please sign in to comment.