-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new voice encryption modes
- Loading branch information
Showing
12 changed files
with
272 additions
and
76 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
65 changes: 27 additions & 38 deletions
65
src/Disqord.Voice/Encryption/Default/DefaultVoiceEncryptionProvider.cs
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
62 changes: 62 additions & 0 deletions
62
src/Disqord.Voice/Encryption/Default/Modes/AEADAes256GcmRtpSizeEncryption.cs
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,62 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
|
||
namespace Disqord.Voice.Default; | ||
|
||
/// <summary> | ||
/// Represents the <see cref="KnownEncryptionModes.AEADAes256GcmRtpSize"/> encryption. | ||
/// </summary> | ||
public sealed class AEADAes256GcmRtpSizeEncryption : IVoiceEncryption | ||
{ | ||
/// <summary> | ||
/// Checks whether this mode is supported by the host machine's hardware. | ||
/// </summary> | ||
public static bool IsSupported => Sodium.crypto_aead_aes256gcm_is_available(); | ||
|
||
/// <inheritdoc/> | ||
public string ModeName => KnownEncryptionModes.AEADAes256GcmRtpSize; | ||
|
||
private uint _nonceValue; | ||
|
||
private readonly int _abytes; | ||
private readonly int _npubbytes; | ||
|
||
public AEADAes256GcmRtpSizeEncryption() | ||
{ | ||
_abytes = Sodium.crypto_aead_aes256gcm_abytes(); | ||
_npubbytes = Sodium.crypto_aead_aes256gcm_npubbytes(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public int GetEncryptedLength(int length) | ||
{ | ||
return length + _abytes + 4; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public unsafe void Encrypt(ReadOnlySpan<byte> rtpHeader, Span<byte> encryptedAudio, ReadOnlySpan<byte> audio, ReadOnlySpan<byte> key) | ||
{ | ||
var nonce = (stackalloc byte[_npubbytes]); | ||
BinaryPrimitives.WriteUInt32BigEndian(nonce, _nonceValue); | ||
nonce[..4].CopyTo(encryptedAudio[^4..]); | ||
|
||
fixed (byte* encryptedAudioPtr = encryptedAudio) | ||
fixed (byte* rtpHeaderPtr = rtpHeader) | ||
fixed (byte* audioPtr = audio) | ||
fixed (byte* noncePtr = nonce) | ||
fixed (byte* keyPtr = key) | ||
{ | ||
var result = Sodium.crypto_aead_aes256gcm_encrypt(c: encryptedAudioPtr, clen_p: null, | ||
m: audioPtr, mlen: (ulong) audio.Length, | ||
ad: rtpHeaderPtr, adlen: (ulong) rtpHeader.Length, | ||
nsec: null, npub: noncePtr, k: keyPtr); | ||
|
||
if (result != 0) | ||
{ | ||
Sodium.CheckEncryptionResult(result); | ||
} | ||
} | ||
|
||
_nonceValue++; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Disqord.Voice/Encryption/Default/Modes/AEADXChaCha20Poly1305RtpSizeEncryption.cs
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,57 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
|
||
namespace Disqord.Voice.Default; | ||
|
||
/// <summary> | ||
/// Represents the <see cref="KnownEncryptionModes.AEADXChaCha20Poly1305RtpSize"/> encryption. | ||
/// </summary> | ||
public sealed class AEADXChaCha20Poly1305RtpSizeEncryption : IVoiceEncryption | ||
{ | ||
/// <inheritdoc/> | ||
public string ModeName => KnownEncryptionModes.AEADXChaCha20Poly1305RtpSize; | ||
|
||
private uint _nonceValue; | ||
|
||
private readonly int _npubbytes; | ||
private readonly int _abytes; | ||
|
||
public AEADXChaCha20Poly1305RtpSizeEncryption() | ||
{ | ||
_npubbytes = Sodium.crypto_aead_xchacha20poly1305_ietf_npubbytes(); | ||
_abytes = Sodium.crypto_aead_xchacha20poly1305_ietf_abytes(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public int GetEncryptedLength(int length) | ||
{ | ||
return length + _abytes + 4; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public unsafe void Encrypt(ReadOnlySpan<byte> rtpHeader, Span<byte> encryptedAudio, ReadOnlySpan<byte> audio, ReadOnlySpan<byte> key) | ||
{ | ||
var nonce = (stackalloc byte[_npubbytes]); | ||
BinaryPrimitives.WriteUInt32BigEndian(nonce, _nonceValue); | ||
nonce[..4].CopyTo(encryptedAudio[^4..]); | ||
|
||
fixed (byte* encryptedAudioPtr = encryptedAudio) | ||
fixed (byte* rtpHeaderPtr = rtpHeader) | ||
fixed (byte* audioPtr = audio) | ||
fixed (byte* noncePtr = nonce) | ||
fixed (byte* keyPtr = key) | ||
{ | ||
var result = Sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(c: encryptedAudioPtr, clen_p: null, | ||
m: audioPtr, mlen: (ulong) audio.Length, | ||
ad: rtpHeaderPtr, adlen: (ulong) rtpHeader.Length, | ||
nsec: null, npub: noncePtr, k: keyPtr); | ||
|
||
if (result != 0) | ||
{ | ||
Sodium.CheckEncryptionResult(result); | ||
} | ||
} | ||
|
||
_nonceValue++; | ||
} | ||
} |
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
12 changes: 6 additions & 6 deletions
12
...Default/XSalsa20Poly1305LiteEncryption.cs → ...t/Modes/XSalsa20Poly1305LiteEncryption.cs
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 |
---|---|---|
@@ -1,34 +1,34 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
using Disqord.Voice.Api; | ||
|
||
namespace Disqord.Voice.Default; | ||
|
||
/// <summary> | ||
/// Represents the <see cref="KnownEncryptionModes.XSalsa20Poly1305Lite"/> encryption. | ||
/// </summary> | ||
[Obsolete("This encryption mode has been deprecated by Discord.")] | ||
public sealed class XSalsa20Poly1305LiteEncryption : IVoiceEncryption | ||
{ | ||
/// <inheritdoc/> | ||
public string ModeName => KnownEncryptionModes.XSalsa20Poly1305Lite; | ||
|
||
private uint _value = uint.MaxValue - 1; | ||
private uint _nonceValue; | ||
|
||
/// <inheritdoc/> | ||
public int GetEncryptedLength(int length) | ||
{ | ||
return length + Sodium.MacLength + 4; | ||
return length + Sodium.XSalsa20Poly1305MacLength + 4; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Encrypt(ReadOnlySpan<byte> rtpHeader, Span<byte> encryptedAudio, ReadOnlySpan<byte> audio, ReadOnlySpan<byte> key) | ||
{ | ||
var nonce = (stackalloc byte[Sodium.NonceLength]); | ||
BinaryPrimitives.WriteUInt32BigEndian(nonce, _value); | ||
var nonce = (stackalloc byte[Sodium.XSalsa20Poly1305NonceLength]); | ||
BinaryPrimitives.WriteUInt32BigEndian(nonce, _nonceValue); | ||
|
||
Sodium.Encrypt(encryptedAudio[..^4], audio, nonce, key); | ||
nonce[..4].CopyTo(encryptedAudio[^4..]); | ||
|
||
_value++; | ||
_nonceValue++; | ||
} | ||
} |
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.