aboutsummaryrefslogtreecommitdiff
path: root/Tools/Hazel-Networking/Hazel/Dtls/NullRecordProtection.cs
blob: 76fa1325cb8938fd9ad782f88473c0db844f110b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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);
        }
    }
}