From 8d2a2cd5de40e2b94ef5007c32832ed9a063dc40 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 12 Oct 2023 22:09:49 +0800 Subject: +hazel-networking --- .../Hazel/Dtls/IRecordProtection.cs | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Tools/Hazel-Networking/Hazel/Dtls/IRecordProtection.cs (limited to 'Tools/Hazel-Networking/Hazel/Dtls/IRecordProtection.cs') diff --git a/Tools/Hazel-Networking/Hazel/Dtls/IRecordProtection.cs b/Tools/Hazel-Networking/Hazel/Dtls/IRecordProtection.cs new file mode 100644 index 0000000..cbee1b0 --- /dev/null +++ b/Tools/Hazel-Networking/Hazel/Dtls/IRecordProtection.cs @@ -0,0 +1,84 @@ +using System; + +namespace Hazel.Dtls +{ + /// + /// DTLS cipher suite interface for protection of record payload. + /// + public interface IRecordProtection : IDisposable + { + /// + /// Calculate the size of an encrypted plaintext + /// + /// Size of plaintext in bytes + /// Size of encrypted ciphertext in bytes + int GetEncryptedSize(int dataSize); + + /// + /// Calculate the size of decrypted ciphertext + /// + /// Size of ciphertext in bytes + /// Size of decrypted plaintext in bytes + int GetDecryptedSize(int dataSize); + + /// + /// Encrypt a plaintext intput with server keys + /// + /// Output may overlap with input. + /// + /// Output ciphertext + /// Input plaintext + /// Parent DTLS record + void EncryptServerPlaintext(ByteSpan output, ByteSpan input, ref Record record); + + /// + /// Encrypt a plaintext intput with client keys + /// + /// Output may overlap with input. + /// + /// Output ciphertext + /// Input plaintext + /// Parent DTLS record + void EncryptClientPlaintext(ByteSpan output, ByteSpan input, ref Record record); + + /// + /// Decrypt a ciphertext intput with server keys + /// + /// Output may overlap with input. + /// + /// Output plaintext + /// Input ciphertext + /// Parent DTLS record + /// True if the input was authenticated and decrypted. Otherwise false + bool DecryptCiphertextFromServer(ByteSpan output, ByteSpan input, ref Record record); + + /// + /// Decrypt a ciphertext intput with client keys + /// + /// Output may overlap with input. + /// + /// Output plaintext + /// Input ciphertext + /// Parent DTLS record + /// True if the input was authenticated and decrypted. Otherwise false + bool DecryptCiphertextFromClient(ByteSpan output, ByteSpan input, ref Record record); + } + + /// + /// Factory to create record protection from cipher suite identifiers + /// + public sealed class RecordProtectionFactory + { + public static IRecordProtection Create(CipherSuite cipherSuite, ByteSpan masterSecret, ByteSpan serverRandom, ByteSpan clientRandom) + { + switch (cipherSuite) + { + case CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: + return new Aes128GcmRecordProtection(masterSecret, serverRandom, clientRandom); + + default: + return null; + } + } + } +} -- cgit v1.1-26-g67d0