From e922df23b1115a714ebeda31af4a479f822d7c8a Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Fri, 22 Jan 2021 10:39:08 -0800 Subject: [PATCH] Add null record protection implementation --- Hazel/Dtls/NullRecordProtection.cs | 66 ++++++++++++++++++++++++++++++ Hazel/Hazel.csproj | 1 + 2 files changed, 67 insertions(+) create mode 100644 Hazel/Dtls/NullRecordProtection.cs 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 @@ + -- 2.39.5