Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UniversalCryptoDecryptor.cs
Go to the documentation of this file.
1using System;
3
5
6internal sealed class UniversalCryptoDecryptor : Internal.Cryptography.UniversalCryptoTransform
7{
8 private byte[] _heldoverCipher;
9
10 private bool DepaddingRequired
11 {
12 get
13 {
14 switch (base.PaddingMode)
15 {
16 case PaddingMode.PKCS7:
17 case PaddingMode.ANSIX923:
18 case PaddingMode.ISO10126:
19 return true;
20 case PaddingMode.None:
21 case PaddingMode.Zeros:
22 return false;
23 default:
25 }
26 }
27 }
28
30 : base(paddingMode, basicSymmetricCipher)
31 {
32 }
33
34 protected override int UncheckedTransformBlock(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer)
35 {
36 int num = 0;
38 {
39 if (_heldoverCipher != null)
40 {
41 int num2 = base.BasicSymmetricCipher.Transform(_heldoverCipher, outputBuffer);
42 outputBuffer = outputBuffer.Slice(num2);
43 num += num2;
44 }
45 else
46 {
47 _heldoverCipher = new byte[base.InputBlockSize];
48 }
49 inputBuffer.Slice(inputBuffer.Length - _heldoverCipher.Length).CopyTo(_heldoverCipher);
50 inputBuffer = inputBuffer.Slice(0, inputBuffer.Length - _heldoverCipher.Length);
51 }
52 if (inputBuffer.Length > 0)
53 {
54 num += base.BasicSymmetricCipher.Transform(inputBuffer, outputBuffer);
55 }
56 return num;
57 }
58
59 protected unsafe override int UncheckedTransformFinalBlock(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer)
60 {
61 if (inputBuffer.Length % base.PaddingSizeBytes != 0)
62 {
64 }
65 byte[] array = null;
66 int num = 0;
67 try
68 {
69 Span<byte> span;
71 if (_heldoverCipher == null)
72 {
73 num = inputBuffer.Length;
75 span = array.AsSpan(0, inputBuffer.Length);
76 input = inputBuffer;
77 }
78 else
79 {
80 num = _heldoverCipher.Length + inputBuffer.Length;
82 span = array.AsSpan(0, num);
83 _heldoverCipher.AsSpan().CopyTo(span);
84 inputBuffer.CopyTo(span.Slice(_heldoverCipher.Length));
85 input = span;
86 }
87 int num2 = 0;
88 fixed (byte* ptr = span)
89 {
90 Span<byte> span2 = span[..base.BasicSymmetricCipher.TransformFinal(input, span)];
91 if (span2.Length > 0)
92 {
93 num2 = GetPaddingLength(span2);
94 span2.Slice(0, num2).CopyTo(outputBuffer);
95 }
96 }
97 Reset();
98 return num2;
99 }
100 finally
101 {
102 if (array != null)
103 {
105 }
106 }
107 }
108
109 protected unsafe override byte[] UncheckedTransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
110 {
112 {
113 byte[] array = System.Security.Cryptography.CryptoPool.Rent(inputCount + base.InputBlockSize);
114 int num = 0;
115 fixed (byte* ptr = array)
116 {
117 try
118 {
119 num = UncheckedTransformFinalBlock(inputBuffer.AsSpan(inputOffset, inputCount), array);
120 return array.AsSpan(0, num).ToArray();
121 }
122 finally
123 {
125 }
126 }
127 }
128 byte[] array2 = GC.AllocateUninitializedArray<byte>(inputCount);
129 int num2 = UncheckedTransformFinalBlock(inputBuffer.AsSpan(inputOffset, inputCount), array2);
130 return array2;
131 }
132
133 protected sealed override void Dispose(bool disposing)
134 {
135 if (disposing)
136 {
137 byte[] heldoverCipher = _heldoverCipher;
138 _heldoverCipher = null;
139 if (heldoverCipher != null)
140 {
141 Array.Clear(heldoverCipher);
142 }
143 }
144 base.Dispose(disposing);
145 }
146
147 public unsafe override bool TransformOneShot(ReadOnlySpan<byte> input, Span<byte> output, out int bytesWritten)
148 {
149 if (input.Length % base.PaddingSizeBytes != 0)
150 {
152 }
154 {
155 if (output.Length >= input.Length)
156 {
157 bytesWritten = base.BasicSymmetricCipher.TransformFinal(input, output);
158 return true;
159 }
160 bytesWritten = 0;
161 return false;
162 }
164 Span<byte> output2 = array.AsSpan(0, input.Length);
165 Span<byte> span = default(Span<byte>);
166 fixed (byte* ptr = output2)
167 {
168 try
169 {
170 span = output2[..base.BasicSymmetricCipher.TransformFinal(input, output2)];
171 int paddingLength = GetPaddingLength(span);
172 if (paddingLength > output.Length)
173 {
174 bytesWritten = 0;
175 return false;
176 }
177 span.Slice(0, paddingLength).CopyTo(output);
178 bytesWritten = paddingLength;
179 return true;
180 }
181 finally
182 {
185 }
186 }
187 }
188
189 private void Reset()
190 {
191 if (_heldoverCipher != null)
192 {
194 _heldoverCipher = null;
195 }
196 }
197
199 {
200 int num = 0;
201 switch (base.PaddingMode)
202 {
203 case PaddingMode.ANSIX923:
204 {
205 num = block[^1];
206 if (num <= 0 || num > base.InputBlockSize)
207 {
209 }
210 for (int j = block.Length - num; j < block.Length - 1; j++)
211 {
212 if (block[j] != 0)
213 {
215 }
216 }
217 break;
218 }
219 case PaddingMode.ISO10126:
220 num = block[^1];
221 if (num <= 0 || num > base.InputBlockSize)
222 {
224 }
225 break;
226 case PaddingMode.PKCS7:
227 {
228 num = block[^1];
229 if (num <= 0 || num > base.InputBlockSize)
230 {
232 }
233 for (int i = block.Length - num; i < block.Length - 1; i++)
234 {
235 if (block[i] != num)
236 {
238 }
239 }
240 break;
241 }
242 case PaddingMode.None:
243 case PaddingMode.Zeros:
244 num = 0;
245 break;
246 default:
248 }
249 return block.Length - num;
250 }
251}
override int UncheckedTransformBlock(ReadOnlySpan< byte > inputBuffer, Span< byte > outputBuffer)
UniversalCryptoDecryptor(PaddingMode paddingMode, Internal.Cryptography.BasicSymmetricCipher basicSymmetricCipher)
unsafe override byte[] UncheckedTransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
unsafe override bool TransformOneShot(ReadOnlySpan< byte > input, Span< byte > output, out int bytesWritten)
unsafe override int UncheckedTransformFinalBlock(ReadOnlySpan< byte > inputBuffer, Span< byte > outputBuffer)
static unsafe void Clear(Array array)
Definition Array.cs:755
Definition GC.cs:8
static string Cryptography_UnknownPaddingMode
Definition SR.cs:154
static string Cryptography_InvalidPadding
Definition SR.cs:106
static string Cryptography_PartialBlock
Definition SR.cs:124
Definition SR.cs:7
static void Return(byte[] array, int clearSize=-1)
Definition CryptoPool.cs:12
static byte[] Rent(int minimumLength)
Definition CryptoPool.cs:7
void CopyTo(Span< T > destination)
ReadOnlySpan< T > Slice(int start)
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70