Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Pkcs12Kdf.cs
Go to the documentation of this file.
1using System.Text;
2
4
5internal static class Pkcs12Kdf
6{
7 private static readonly (HashAlgorithmName, int, int)[] s_uvLookup = new(HashAlgorithmName, int, int)[5]
8 {
9 (HashAlgorithmName.MD5, 128, 512),
10 (HashAlgorithmName.SHA1, 160, 512),
11 (HashAlgorithmName.SHA256, 256, 512),
12 (HashAlgorithmName.SHA384, 384, 1024),
13 (HashAlgorithmName.SHA512, 512, 1024)
14 };
15
16 internal static void DeriveCipherKey(ReadOnlySpan<char> password, HashAlgorithmName hashAlgorithm, int iterationCount, ReadOnlySpan<byte> salt, Span<byte> destination)
17 {
18 Derive(password, hashAlgorithm, iterationCount, 1, salt, destination);
19 }
20
21 internal static void DeriveIV(ReadOnlySpan<char> password, HashAlgorithmName hashAlgorithm, int iterationCount, ReadOnlySpan<byte> salt, Span<byte> destination)
22 {
23 Derive(password, hashAlgorithm, iterationCount, 2, salt, destination);
24 }
25
26 private static void Derive(ReadOnlySpan<char> password, HashAlgorithmName hashAlgorithm, int iterationCount, byte id, ReadOnlySpan<byte> salt, Span<byte> destination)
27 {
28 int num = -1;
29 int num2 = -1;
30 (HashAlgorithmName, int, int)[] array = s_uvLookup;
31 for (int i = 0; i < array.Length; i++)
32 {
33 (HashAlgorithmName, int, int) tuple = array[i];
34 if (tuple.Item1 == hashAlgorithm)
35 {
36 num = tuple.Item2;
37 num2 = tuple.Item3;
38 break;
39 }
40 }
41 if (num == -1)
42 {
44 }
45 int num3 = num2 >> 3;
46 Span<byte> span = stackalloc byte[num3];
47 span.Fill(id);
48 int num4 = (salt.Length - 1 + num3) / num3 * num3;
49 int num5 = checked((password.Length + 1) * 2);
50 if (password == default(ReadOnlySpan<char>))
51 {
52 num5 = 0;
53 }
54 int num6 = (num5 - 1 + num3) / num3 * num3;
55 int num7 = num4 + num6;
56 Span<byte> span2 = default(Span<byte>);
57 byte[] array2 = null;
58 if (num7 <= 1024)
59 {
60 span2 = stackalloc byte[num7];
61 }
62 else
63 {
65 span2 = array2.AsSpan(0, num7);
66 }
67 IncrementalHash incrementalHash = IncrementalHash.CreateHash(hashAlgorithm);
68 try
69 {
70 CircularCopy(salt, span2.Slice(0, num4));
71 CircularCopyUtf16BE(password, span2.Slice(num4));
72 Span<byte> span3 = stackalloc byte[num >> 3];
73 Span<byte> span4 = stackalloc byte[num3];
74 while (true)
75 {
76 incrementalHash.AppendData(span);
77 incrementalHash.AppendData(span2);
78 for (int num8 = iterationCount; num8 > 0; num8--)
79 {
80 if (!incrementalHash.TryGetHashAndReset(span3, out var bytesWritten) || bytesWritten != span3.Length)
81 {
82 throw new CryptographicException();
83 }
84 if (num8 != 1)
85 {
86 incrementalHash.AppendData(span3);
87 }
88 }
89 if (span3.Length >= destination.Length)
90 {
91 break;
92 }
93 span3.CopyTo(destination);
94 destination = destination.Slice(span3.Length);
95 CircularCopy(span3, span4);
96 for (int num9 = span2.Length / num3 - 1; num9 >= 0; num9--)
97 {
98 Span<byte> into = span2.Slice(num9 * num3, num3);
99 AddPlusOne(into, span4);
100 }
101 }
102 span3.Slice(0, destination.Length).CopyTo(destination);
103 }
104 finally
105 {
107 if (array2 != null)
108 {
110 }
111 incrementalHash.Dispose();
112 }
113 }
114
115 private static void AddPlusOne(Span<byte> into, Span<byte> addend)
116 {
117 int num = 1;
118 for (int num2 = into.Length - 1; num2 >= 0; num2--)
119 {
120 int num3 = num + into[num2] + addend[num2];
121 into[num2] = (byte)num3;
122 num = num3 >> 8;
123 }
124 }
125
127 {
128 while (destination.Length > 0)
129 {
130 if (destination.Length >= bytes.Length)
131 {
132 bytes.CopyTo(destination);
133 destination = destination.Slice(bytes.Length);
134 continue;
135 }
136 bytes.Slice(0, destination.Length).CopyTo(destination);
137 break;
138 }
139 }
140
142 {
143 int num = password.Length * 2;
144 Encoding bigEndianUnicode = Encoding.BigEndianUnicode;
145 while (destination.Length > 0)
146 {
147 if (destination.Length >= num)
148 {
149 int bytes = bigEndianUnicode.GetBytes(password, destination);
150 if (bytes != num)
151 {
152 throw new CryptographicException();
153 }
155 Span<byte> span = destination.Slice(0, Math.Min(2, destination.Length));
156 span.Clear();
157 destination = destination.Slice(span.Length);
158 continue;
159 }
160 ReadOnlySpan<char> chars = password.Slice(0, destination.Length / 2);
161 int bytes2 = bigEndianUnicode.GetBytes(chars, destination);
162 if (bytes2 != destination.Length)
163 {
164 throw new CryptographicException();
165 }
166 break;
167 }
168 }
169}
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static string Cryptography_UnknownHashAlgorithm
Definition SR.cs:152
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)
static void DeriveCipherKey(ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, int iterationCount, ReadOnlySpan< byte > salt, Span< byte > destination)
Definition Pkcs12Kdf.cs:16
static void AddPlusOne(Span< byte > into, Span< byte > addend)
Definition Pkcs12Kdf.cs:115
static readonly(HashAlgorithmName, int, int)[] s_uvLookup
static void CircularCopy(ReadOnlySpan< byte > bytes, Span< byte > destination)
Definition Pkcs12Kdf.cs:126
static void CircularCopyUtf16BE(ReadOnlySpan< char > password, Span< byte > destination)
Definition Pkcs12Kdf.cs:141
static void Derive(ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, int iterationCount, byte id, ReadOnlySpan< byte > salt, Span< byte > destination)
Definition Pkcs12Kdf.cs:26
static void DeriveIV(ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, int iterationCount, ReadOnlySpan< byte > salt, Span< byte > destination)
Definition Pkcs12Kdf.cs:21
static Encoding BigEndianUnicode
Definition Encoding.cs:521
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781
ReadOnlySpan< T > Slice(int start)
void CopyTo(Span< T > destination)
Definition Span.cs:224
void Fill(T value)
Definition Span.cs:211
Span< T > Slice(int start)
Definition Span.cs:271
unsafe void Clear()
Definition Span.cs:198
int Length
Definition Span.cs:70