Skip to content

Commit b91ef85

Browse files
NeloBlivionLulalaby
authored andcommitted
support aead_xchacha20_poly1305_rtpsize
1 parent 1c65fc8 commit b91ef85

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

discord/types/voice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from .snowflake import Snowflake
3434

3535
SupportedModes = Literal[
36-
"xsalsa20_poly1305_lite", "xsalsa20_poly1305_suffix", "xsalsa20_poly1305"
36+
"xsalsa20_poly1305_lite", "xsalsa20_poly1305_suffix", "xsalsa20_poly1305", "aead_xchacha20_poly1305_rtpsize"
3737
]
3838

3939

discord/voice_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def __init__(self, client: Client, channel: abc.Connectable):
271271
"xsalsa20_poly1305_lite",
272272
"xsalsa20_poly1305_suffix",
273273
"xsalsa20_poly1305",
274+
"aead_xchacha20_poly1305_rtpsize",
274275
)
275276

276277
@property
@@ -565,19 +566,22 @@ def _get_voice_packet(self, data):
565566
return encrypt_packet(header, data)
566567

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

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

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

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

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

@@ -586,7 +590,18 @@ def _encrypt_xsalsa20_poly1305_lite(self, header: bytes, data) -> bytes:
586590

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

593+
def _encrypt_aead_xchacha20_poly1305_rtpsize(self, header: bytes, data) -> bytes:
594+
# Required as of Nov 18 2024
595+
box = nacl.secret.Aead(bytes(self.secret_key))
596+
nonce = bytearray(24)
597+
598+
nonce[:4] = struct.pack('>I', self._lite_nonce)
599+
self.checked_add('_lite_nonce', 1, 4294967295)
600+
601+
return header + box.encrypt(bytes(data), bytes(header), bytes(nonce)).ciphertext + nonce[:4]
602+
589603
def _decrypt_xsalsa20_poly1305(self, header, data):
604+
# Deprecated, remove in 2.7
590605
box = nacl.secret.SecretBox(bytes(self.secret_key))
591606

592607
nonce = bytearray(24)
@@ -595,6 +610,7 @@ def _decrypt_xsalsa20_poly1305(self, header, data):
595610
return self.strip_header_ext(box.decrypt(bytes(data), bytes(nonce)))
596611

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

600616
nonce_size = nacl.secret.SecretBox.NONCE_SIZE
@@ -603,6 +619,7 @@ def _decrypt_xsalsa20_poly1305_suffix(self, header, data):
603619
return self.strip_header_ext(box.decrypt(bytes(data[:-nonce_size]), nonce))
604620

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

608625
nonce = bytearray(24)
@@ -611,6 +628,16 @@ def _decrypt_xsalsa20_poly1305_lite(self, header, data):
611628

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

631+
def _decrypt_aead_xchacha20_poly1305_rtpsize(self, header, data):
632+
# Required as of Nov 18 2024
633+
box = nacl.secret.Aead(bytes(self.secret_key))
634+
635+
nonce = bytearray(24)
636+
nonce[:4] = data[-4:]
637+
data = data[:-4]
638+
639+
return self.strip_header_ext(box.decrypt(bytes(data), bytes(header), bytes(nonce)))
640+
614641
@staticmethod
615642
def strip_header_ext(data):
616643
if len(data) > 4 and data[0] == 0xBE and data[1] == 0xDE:

0 commit comments

Comments
 (0)