Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RsaPaddingProcessor.cs
Go to the documentation of this file.
3
5
6internal sealed class RsaPaddingProcessor
7{
8 private static readonly byte[] s_digestInfoMD5 = new byte[18]
9 {
10 48, 32, 48, 12, 6, 8, 42, 134, 72, 134,
11 247, 13, 2, 5, 5, 0, 4, 16
12 };
13
14 private static readonly byte[] s_digestInfoSha1 = new byte[15]
15 {
16 48, 33, 48, 9, 6, 5, 43, 14, 3, 2,
17 26, 5, 0, 4, 20
18 };
19
20 private static readonly byte[] s_digestInfoSha256 = new byte[19]
21 {
22 48, 49, 48, 13, 6, 9, 96, 134, 72, 1,
23 101, 3, 4, 2, 1, 5, 0, 4, 32
24 };
25
26 private static readonly byte[] s_digestInfoSha384 = new byte[19]
27 {
28 48, 65, 48, 13, 6, 9, 96, 134, 72, 1,
29 101, 3, 4, 2, 2, 5, 0, 4, 48
30 };
31
32 private static readonly byte[] s_digestInfoSha512 = new byte[19]
33 {
34 48, 81, 48, 13, 6, 9, 96, 134, 72, 1,
35 101, 3, 4, 2, 3, 5, 0, 4, 64
36 };
37
39
41
42 private readonly int _hLen;
43
45
46 private RsaPaddingProcessor(HashAlgorithmName hashAlgorithmName, int hLen, ReadOnlyMemory<byte> digestInfoPrefix)
47 {
48 _hashAlgorithmName = hashAlgorithmName;
49 _hLen = hLen;
50 _digestInfoPrefix = digestInfoPrefix;
51 }
52
53 internal static int BytesRequiredForBitCount(int keySizeInBits)
54 {
55 return (int)((uint)(keySizeInBits + 7) / 8u);
56 }
57
59 {
60 return s_lookup.GetOrAdd(hashAlgorithmName, delegate(HashAlgorithmName hashAlgorithmName)
61 {
62 using IncrementalHash incrementalHash = IncrementalHash.CreateHash(hashAlgorithmName);
63 Span<byte> destination = stackalloc byte[64];
64 ReadOnlyMemory<byte> digestInfoPrefix;
65 if (hashAlgorithmName == HashAlgorithmName.MD5)
66 {
67 digestInfoPrefix = s_digestInfoMD5;
68 }
69 else if (hashAlgorithmName == HashAlgorithmName.SHA1)
70 {
71 digestInfoPrefix = s_digestInfoSha1;
72 }
73 else if (hashAlgorithmName == HashAlgorithmName.SHA256)
74 {
75 digestInfoPrefix = s_digestInfoSha256;
76 }
77 else if (hashAlgorithmName == HashAlgorithmName.SHA384)
78 {
79 digestInfoPrefix = s_digestInfoSha384;
80 }
81 else
82 {
83 if (!(hashAlgorithmName == HashAlgorithmName.SHA512))
84 {
85 throw new CryptographicException();
86 }
87 digestInfoPrefix = s_digestInfoSha512;
88 }
89 if (incrementalHash.TryGetHashAndReset(destination, out var bytesWritten))
90 {
91 return new System.Security.Cryptography.RsaPaddingProcessor(hashAlgorithmName, bytesWritten, digestInfoPrefix);
92 }
93 byte[] hashAndReset = incrementalHash.GetHashAndReset();
94 return new System.Security.Cryptography.RsaPaddingProcessor(hashAlgorithmName, hashAndReset.Length, digestInfoPrefix);
95 });
96 }
97
99 {
100 int length = source.Length;
101 int length2 = destination.Length;
102 if (length > length2 - 11)
103 {
105 }
106 Span<byte> destination2 = destination.Slice(destination.Length - source.Length);
107 Span<byte> data = destination.Slice(2, destination.Length - source.Length - 3);
108 destination[0] = 0;
109 destination[1] = 2;
110 destination[data.Length + 2] = 0;
111 FillNonZeroBytes(data);
112 source.CopyTo(destination2);
113 }
114
116 {
117 byte[] array = null;
119 try
120 {
121 int num = checked(destination.Length - _hLen - _hLen - 2);
122 if (source.Length > num)
123 {
125 }
126 Span<byte> span2 = destination.Slice(1, _hLen);
127 Span<byte> span3 = destination.Slice(1 + _hLen);
128 using IncrementalHash incrementalHash = IncrementalHash.CreateHash(_hashAlgorithmName);
129 Span<byte> destination2 = span3.Slice(0, _hLen);
130 Span<byte> destination3 = span3.Slice(span3.Length - source.Length);
131 Span<byte> span4 = span3.Slice(_hLen, span3.Length - _hLen - 1 - destination3.Length);
132 Span<byte> span5 = span3.Slice(_hLen + span4.Length, 1);
133 if (!incrementalHash.TryGetHashAndReset(destination2, out var bytesWritten) || bytesWritten != _hLen)
134 {
135 throw new CryptographicException();
136 }
137 span4.Clear();
138 span5[0] = 1;
139 source.CopyTo(destination3);
142 span = new Span<byte>(array, 0, span3.Length);
143 Mgf1(incrementalHash, span2, span);
144 Xor(span3, span);
145 Span<byte> span6 = stackalloc byte[_hLen];
146 Mgf1(incrementalHash, span3, span6);
147 Xor(span2, span6);
148 destination[0] = 0;
149 }
150 catch (Exception ex) when (!(ex is CryptographicException))
151 {
152 throw new CryptographicException();
153 }
154 finally
155 {
156 if (array != null)
157 {
160 }
161 }
162 }
163
164 private void Mgf1(IncrementalHash hasher, ReadOnlySpan<byte> mgfSeed, Span<byte> mask)
165 {
166 Span<byte> destination = mask;
167 int num = 0;
168 Span<byte> span = stackalloc byte[4];
169 while (destination.Length > 0)
170 {
171 hasher.AppendData(mgfSeed);
173 hasher.AppendData(span);
174 if (destination.Length >= _hLen)
175 {
176 if (!hasher.TryGetHashAndReset(destination, out var bytesWritten))
177 {
178 throw new CryptographicException();
179 }
180 destination = destination.Slice(bytesWritten);
181 num++;
182 continue;
183 }
184 Span<byte> destination2 = stackalloc byte[_hLen];
185 if (!hasher.TryGetHashAndReset(destination2, out var _))
186 {
187 throw new CryptographicException();
188 }
189 destination2.Slice(0, destination.Length).CopyTo(destination);
190 break;
191 }
192 }
193
194 private static void FillNonZeroBytes(Span<byte> data)
195 {
196 while (data.Length > 0)
197 {
199 int num = data.Length;
200 for (int i = 0; i < data.Length; i++)
201 {
202 if (data[i] == 0)
203 {
204 num = i;
205 break;
206 }
207 }
208 for (int j = num + 1; j < data.Length; j++)
209 {
210 if (data[j] != 0)
211 {
212 data[num++] = data[j];
213 }
214 }
215 data = data.Slice(num);
216 }
217 }
218
219 private static void Xor(Span<byte> a, ReadOnlySpan<byte> b)
220 {
221 if (a.Length != b.Length)
222 {
223 throw new InvalidOperationException();
224 }
225 for (int i = 0; i < b.Length; i++)
226 {
227 a[i] ^= b[i];
228 }
229 }
230}
static void WriteInt32BigEndian(Span< byte > destination, int value)
static string Cryptography_KeyTooSmall
Definition SR.cs:110
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_Encryption_MessageTooLong
Definition SR.cs:52
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
static IncrementalHash CreateHash(HashAlgorithmName hashAlgorithm)
bool TryGetHashAndReset(Span< byte > destination, out int bytesWritten)
void Mgf1(IncrementalHash hasher, ReadOnlySpan< byte > mgfSeed, Span< byte > mask)
void PadOaep(ReadOnlySpan< byte > source, Span< byte > destination)
static void PadPkcs1Encryption(ReadOnlySpan< byte > source, Span< byte > destination)
RsaPaddingProcessor(HashAlgorithmName hashAlgorithmName, int hLen, ReadOnlyMemory< byte > digestInfoPrefix)
static System.Security.Cryptography.RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorithmName)
static void Xor(Span< byte > a, ReadOnlySpan< byte > b)
static readonly ConcurrentDictionary< HashAlgorithmName, RsaPaddingProcessor > s_lookup
void CopyTo(Span< T > destination)
Definition Span.cs:224
Span< T > Slice(int start)
Definition Span.cs:271
unsafe void Clear()
Definition Span.cs:198
static Span< T > Empty
Definition Span.cs:87
int Length
Definition Span.cs:70