Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support new voice encryption modes #2651

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion discord/types/voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
from .snowflake import Snowflake

SupportedModes = Literal[
"xsalsa20_poly1305_lite", "xsalsa20_poly1305_suffix", "xsalsa20_poly1305"
"xsalsa20_poly1305_lite",
"xsalsa20_poly1305_suffix",
"xsalsa20_poly1305",
"aead_xchacha20_poly1305_rtpsize",
]


Expand Down
33 changes: 33 additions & 0 deletions discord/voice_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def __init__(self, client: Client, channel: abc.Connectable):
"xsalsa20_poly1305_lite",
"xsalsa20_poly1305_suffix",
"xsalsa20_poly1305",
"aead_xchacha20_poly1305_rtpsize",
)

@property
Expand Down Expand Up @@ -564,19 +565,22 @@ def _get_voice_packet(self, data):
return encrypt_packet(header, data)

def _encrypt_xsalsa20_poly1305(self, header: bytes, data) -> bytes:
# Deprecated, remove in 2.7
box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce = bytearray(24)
nonce[:12] = header

return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext

def _encrypt_xsalsa20_poly1305_suffix(self, header: bytes, data) -> bytes:
# Deprecated, remove in 2.7
box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE)

return header + box.encrypt(bytes(data), nonce).ciphertext + nonce

def _encrypt_xsalsa20_poly1305_lite(self, header: bytes, data) -> bytes:
# Deprecated, remove in 2.7
box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce = bytearray(24)

Expand All @@ -585,7 +589,22 @@ def _encrypt_xsalsa20_poly1305_lite(self, header: bytes, data) -> bytes:

return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext + nonce[:4]

def _encrypt_aead_xchacha20_poly1305_rtpsize(self, header: bytes, data) -> bytes:
# Required as of Nov 18 2024
box = nacl.secret.Aead(bytes(self.secret_key))
nonce = bytearray(24)

nonce[:4] = struct.pack(">I", self._lite_nonce)
self.checked_add("_lite_nonce", 1, 4294967295)

return (
header
+ box.encrypt(bytes(data), bytes(header), bytes(nonce)).ciphertext
+ nonce[:4]
)

def _decrypt_xsalsa20_poly1305(self, header, data):
# Deprecated, remove in 2.7
box = nacl.secret.SecretBox(bytes(self.secret_key))

nonce = bytearray(24)
Expand All @@ -594,6 +613,7 @@ def _decrypt_xsalsa20_poly1305(self, header, data):
return self.strip_header_ext(box.decrypt(bytes(data), bytes(nonce)))

def _decrypt_xsalsa20_poly1305_suffix(self, header, data):
# Deprecated, remove in 2.7
box = nacl.secret.SecretBox(bytes(self.secret_key))

nonce_size = nacl.secret.SecretBox.NONCE_SIZE
Expand All @@ -602,6 +622,7 @@ def _decrypt_xsalsa20_poly1305_suffix(self, header, data):
return self.strip_header_ext(box.decrypt(bytes(data[:-nonce_size]), nonce))

def _decrypt_xsalsa20_poly1305_lite(self, header, data):
# Deprecated, remove in 2.7
box = nacl.secret.SecretBox(bytes(self.secret_key))

nonce = bytearray(24)
Expand All @@ -610,6 +631,18 @@ def _decrypt_xsalsa20_poly1305_lite(self, header, data):

return self.strip_header_ext(box.decrypt(bytes(data), bytes(nonce)))

def _decrypt_aead_xchacha20_poly1305_rtpsize(self, header, data):
# Required as of Nov 18 2024
box = nacl.secret.Aead(bytes(self.secret_key))

nonce = bytearray(24)
nonce[:4] = data[-4:]
data = data[:-4]

return self.strip_header_ext(
box.decrypt(bytes(data), bytes(header), bytes(nonce))
)

@staticmethod
def strip_header_ext(data):
if data[0] == 0xBE and data[1] == 0xDE and len(data) > 4:
Expand Down