From: Matthew Endsley Date: Fri, 22 Jan 2021 18:39:08 +0000 (-0800) Subject: Add null record protection implementation X-Git-Tag: 1.0.0~20^2~22 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=e922df23b1115a714ebeda31af4a479f822d7c8a;p=rhonda%2Fimpostor.hazel.git Add null record protection implementation --- diff --git a/Hazel/Dtls/NullRecordProtection.cs b/Hazel/Dtls/NullRecordProtection.cs new file mode 100644 index 0000000..76fa132 --- /dev/null +++ b/Hazel/Dtls/NullRecordProtection.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hazel.Dtls +{ + /// + /// Passthrough record protection implementaion + /// + public class NullRecordProtection : IRecordProtection + { + public readonly static NullRecordProtection Instance = new NullRecordProtection(); + + public void Dispose() + { + } + + public int GetEncryptedSize(int dataSize) + { + return dataSize; + } + + public int GetDecryptedSize(int dataSize) + { + return dataSize; + } + + public void EncryptServerPlaintext(ByteSpan output, ByteSpan input, ref Record record) + { + CopyMaybeOverlappingSpans(output, input); + } + + public void EncryptClientPlaintext(ByteSpan output, ByteSpan input, ref Record record) + { + CopyMaybeOverlappingSpans(output, input); + } + + public bool DecryptCiphertextFromServer(ByteSpan output, ByteSpan input, ref Record record) + { + CopyMaybeOverlappingSpans(output, input); + return true; + } + + public bool DecryptCiphertextFromClient(ByteSpan output, ByteSpan input, ref Record record) + { + CopyMaybeOverlappingSpans(output, input); + return true; + } + + private static void CopyMaybeOverlappingSpans(ByteSpan output, ByteSpan input) + { + // Early out if the ranges `output` is equal to `input` + if (output.GetUnderlyingArray() == input.GetUnderlyingArray()) + { + if (output.Offset == input.Offset && output.Length == input.Length) + { + return; + } + } + + input.CopyTo(output); + } + } +} diff --git a/Hazel/Hazel.csproj b/Hazel/Hazel.csproj index 18e7fa0..9becc82 100644 --- a/Hazel/Hazel.csproj +++ b/Hazel/Hazel.csproj @@ -82,6 +82,7 @@ +