]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Add null record protection implementation
authorMatthew Endsley <mendsley@gmail.com>
Fri, 22 Jan 2021 18:39:08 +0000 (10:39 -0800)
committerMatthew Endsley <mendsley@gmail.com>
Tue, 2 Feb 2021 16:53:34 +0000 (08:53 -0800)
Hazel/Dtls/NullRecordProtection.cs [new file with mode: 0644]
Hazel/Hazel.csproj

diff --git a/Hazel/Dtls/NullRecordProtection.cs b/Hazel/Dtls/NullRecordProtection.cs
new file mode 100644 (file)
index 0000000..76fa132
--- /dev/null
@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Hazel.Dtls
+{
+    /// <summary>
+    /// Passthrough record protection implementaion
+    /// </summary>
+    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);
+        }
+    }
+}
index 18e7fa00a1dffd6e043b016927abf0edff28faaf..9becc82e711638d2d1a106718a98cfc5a8b51951 100644 (file)
@@ -82,6 +82,7 @@
     <Compile Include="Dtls\Handshake.cs" />
     <Compile Include="Dtls\IHandshakeCipherSuite.cs" />
     <Compile Include="Dtls\IRecordProtection.cs" />
+    <Compile Include="Dtls\NullRecordProtection.cs" />
     <Compile Include="Dtls\PrfSha256.cs" />
     <Compile Include="Dtls\Record.cs" />
     <Compile Include="Dtls\X25519EcdheRsaSha256.cs" />