Skip to content

Commit caca47e

Browse files
authored
Detect whether AES GCM encryption is supported before using it and fallback to XChaCha20Poly1305RtpSizeEncryption if needed (#364)
1 parent 8fd60a8 commit caca47e

4 files changed

Lines changed: 10 additions & 1 deletion

File tree

NetCord/Gateway/Voice/Encryption/Aes256GcmRtpSizeEncryption.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public sealed class Aes256GcmRtpSizeEncryption : IVoiceEncryption
1111

1212
private const int TagSize = 16;
1313

14+
public static bool IsSupported => AesGcm.IsSupported;
15+
1416
public string Name => "aead_aes256_gcm_rtpsize";
1517

1618
public int Expansion => TagSize + sizeof(int);

NetCord/Gateway/Voice/Encryption/IVoiceEncryption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ namespace NetCord.Gateway.Voice.Encryption;
55
/// </summary>
66
public interface IVoiceEncryption : IDisposable
77
{
8+
/// <summary>
9+
/// Indicates whether the encryption algorithm is supported on the current platform.
10+
/// </summary>
11+
public static abstract bool IsSupported { get; }
12+
813
/// <summary>
914
/// The name of the encryption algorithm.
1015
/// </summary>

NetCord/Gateway/Voice/Encryption/XChaCha20Poly1305RtpSizeEncryption.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public sealed class XChaCha20Poly1305RtpSizeEncryption : IVoiceEncryption
1010
private byte[]? _key;
1111
private int _nonce;
1212

13+
public static bool IsSupported => true;
14+
1315
public string Name => "aead_xchacha20_poly1305_rtpsize";
1416

1517
public int Expansion => XChaCha20Poly1305.ABytes + sizeof(int);

NetCord/Gateway/Voice/VoiceEncryptionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private VoiceEncryptionProvider()
1212

1313
public IVoiceEncryption GetEncryption(IReadOnlyList<string> modes)
1414
{
15-
return modes.Contains("aead_aes256_gcm_rtpsize")
15+
return (modes.Contains("aead_aes256_gcm_rtpsize") && Aes256GcmRtpSizeEncryption.IsSupported)
1616
? new Aes256GcmRtpSizeEncryption()
1717
: new XChaCha20Poly1305RtpSizeEncryption();
1818
}

0 commit comments

Comments
 (0)