Skip to content

Commit 5a62808

Browse files
authored
support aead_xchacha20_poly1305_rtpsize
1 parent e8311f2 commit 5a62808

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
@@ -32,7 +32,7 @@
3232
from .snowflake import Snowflake
3333

3434
SupportedModes = Literal[
35-
"xsalsa20_poly1305_lite", "xsalsa20_poly1305_suffix", "xsalsa20_poly1305"
35+
"xsalsa20_poly1305_lite", "xsalsa20_poly1305_suffix", "xsalsa20_poly1305", "aead_xchacha20_poly1305_rtpsize"
3636
]
3737

3838

discord/voice_client.py

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

275276
@property
@@ -564,19 +565,22 @@ def _get_voice_packet(self, data):
564565
return encrypt_packet(header, data)
565566

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

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

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

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

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

@@ -585,7 +589,18 @@ def _encrypt_xsalsa20_poly1305_lite(self, header: bytes, data) -> bytes:
585589

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

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

591606
nonce = bytearray(24)
@@ -594,6 +609,7 @@ def _decrypt_xsalsa20_poly1305(self, header, data):
594609
return self.strip_header_ext(box.decrypt(bytes(data), bytes(nonce)))
595610

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

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

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

607624
nonce = bytearray(24)
@@ -610,6 +627,16 @@ def _decrypt_xsalsa20_poly1305_lite(self, header, data):
610627

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

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

0 commit comments

Comments
 (0)