-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement all encryption modes documented by discord (#424)
- Loading branch information
1 parent
85943df
commit 4cef8f2
Showing
12 changed files
with
190 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
voice/src/main/kotlin/encryption/strategies/LiteNonceStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dev.kord.voice.encryption.strategies | ||
|
||
import dev.kord.voice.io.ByteArrayView | ||
import dev.kord.voice.io.MutableByteArrayCursor | ||
import dev.kord.voice.io.mutableCursor | ||
import dev.kord.voice.io.view | ||
import dev.kord.voice.udp.RTPPacket | ||
import kotlinx.atomicfu.atomic | ||
|
||
class LiteNonceStrategy : NonceStrategy { | ||
override val nonceLength: Int = 4 | ||
|
||
private var count: Int by atomic(0) | ||
private val nonceBuffer: ByteArray = ByteArray(4) | ||
private val nonceView = nonceBuffer.view() | ||
private val nonceCursor = nonceBuffer.mutableCursor() | ||
|
||
override fun strip(packet: RTPPacket): ByteArrayView { | ||
return with(packet.payload) { | ||
val nonce = view(dataEnd - 4, dataEnd)!! | ||
resize(dataStart, dataEnd - 4) | ||
nonce | ||
} | ||
} | ||
|
||
override fun generate(header: () -> ByteArrayView): ByteArrayView { | ||
count++ | ||
nonceCursor.reset() | ||
nonceCursor.writeInt(count) | ||
return nonceView | ||
} | ||
|
||
override fun append(nonce: ByteArrayView, cursor: MutableByteArrayCursor) { | ||
cursor.writeByteView(nonce) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
voice/src/main/kotlin/encryption/strategies/NonceStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package dev.kord.voice.encryption.strategies | ||
|
||
import dev.kord.voice.io.ByteArrayView | ||
import dev.kord.voice.io.MutableByteArrayCursor | ||
import dev.kord.voice.udp.RTPPacket | ||
|
||
/** | ||
* An [encryption mode, regarding the nonce](https://discord.com/developers/docs/topics/voice-connections#establishing-a-voice-udp-connection-encryption-modes), supported by Discord. | ||
*/ | ||
sealed interface NonceStrategy { | ||
/** | ||
* The amount of bytes this nonce will take up. | ||
*/ | ||
val nonceLength: Int | ||
|
||
/** | ||
* Reads the nonce from this [packet] (also removes it if it resides in the payload), and returns a [ByteArrayView] of it. | ||
*/ | ||
fun strip(packet: RTPPacket): ByteArrayView | ||
|
||
/** | ||
* Generates a nonce, may use the provided information. | ||
*/ | ||
fun generate(header: () -> ByteArrayView): ByteArrayView | ||
|
||
/** | ||
* Writes the [nonce] to [cursor] in the correct relative position. | ||
*/ | ||
fun append(nonce: ByteArrayView, cursor: MutableByteArrayCursor) | ||
} |
31 changes: 31 additions & 0 deletions
31
voice/src/main/kotlin/encryption/strategies/NormalNonceStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dev.kord.voice.encryption.strategies | ||
|
||
import dev.kord.voice.io.ByteArrayView | ||
import dev.kord.voice.io.MutableByteArrayCursor | ||
import dev.kord.voice.io.mutableCursor | ||
import dev.kord.voice.io.view | ||
import dev.kord.voice.udp.RTPPacket | ||
import dev.kord.voice.udp.RTP_HEADER_LENGTH | ||
|
||
class NormalNonceStrategy : NonceStrategy { | ||
// the nonce is already a part of the rtp header, which means this will take up no extra space. | ||
override val nonceLength: Int = 0 | ||
|
||
private val rtpHeaderBuffer = ByteArray(RTP_HEADER_LENGTH) | ||
private val rtpHeaderCursor = rtpHeaderBuffer.mutableCursor() | ||
private val rtpHeaderView = rtpHeaderBuffer.view() | ||
|
||
override fun strip(packet: RTPPacket): ByteArrayView { | ||
rtpHeaderCursor.reset() | ||
packet.writeHeader(rtpHeaderCursor) | ||
return rtpHeaderView | ||
} | ||
|
||
override fun generate(header: () -> ByteArrayView): ByteArrayView { | ||
return header() | ||
} | ||
|
||
override fun append(nonce: ByteArrayView, cursor: MutableByteArrayCursor) { | ||
/* the nonce is the rtp header which means this should do nothing */ | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
voice/src/main/kotlin/encryption/strategies/SuffixNonceStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dev.kord.voice.encryption.strategies | ||
|
||
import dev.kord.voice.io.ByteArrayView | ||
import dev.kord.voice.io.MutableByteArrayCursor | ||
import dev.kord.voice.io.view | ||
import dev.kord.voice.udp.RTPPacket | ||
import kotlin.random.Random | ||
|
||
private const val SUFFIX_NONCE_LENGTH = 24 | ||
|
||
class SuffixNonceStrategy : NonceStrategy { | ||
override val nonceLength: Int = SUFFIX_NONCE_LENGTH | ||
|
||
private val nonceBuffer: ByteArray = ByteArray(SUFFIX_NONCE_LENGTH) | ||
private val nonceView = nonceBuffer.view() | ||
|
||
override fun strip(packet: RTPPacket): ByteArrayView { | ||
return with(packet.payload) { | ||
val nonce = view(dataEnd - SUFFIX_NONCE_LENGTH, dataEnd)!! | ||
resize(dataStart, dataEnd - SUFFIX_NONCE_LENGTH) | ||
nonce | ||
} | ||
} | ||
|
||
override fun generate(header: () -> ByteArrayView): ByteArrayView { | ||
Random.Default.nextBytes(nonceBuffer) | ||
return nonceView | ||
} | ||
|
||
override fun append(nonce: ByteArrayView, cursor: MutableByteArrayCursor) { | ||
cursor.writeByteView(nonce) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.