Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ FillKeyDerivation()

static unsafe void Internal.Cryptography.Pbkdf2Implementation.FillKeyDerivation ( ReadOnlySpan< byte > password,
ReadOnlySpan< byte > salt,
int iterations,
string hashAlgorithmName,
Span< byte > destination )
inlinestaticprivate

Definition at line 27 of file Pbkdf2Implementation.cs.

28 {
29 int hashBlockSize = GetHashBlockSize(hashAlgorithmName);
30 Span<byte> span = default(Span<byte>);
31 ReadOnlySpan<byte> readOnlySpan = default(Span<byte>);
32 int cbSecret;
33 if (password.IsEmpty)
34 {
35 Span<byte> span2 = stackalloc byte[1];
36 readOnlySpan = span2;
37 cbSecret = 0;
38 span = default(Span<byte>);
39 }
40 else if (password.Length <= hashBlockSize)
41 {
42 readOnlySpan = password;
43 cbSecret = password.Length;
44 span = default(Span<byte>);
45 }
46 else
47 {
48 Span<byte> destination2 = stackalloc byte[64];
49 int num = hashAlgorithmName switch
50 {
51 "SHA1" => SHA1.HashData(password, destination2),
52 "SHA256" => SHA256.HashData(password, destination2),
53 "SHA384" => SHA384.HashData(password, destination2),
54 "SHA512" => SHA512.HashData(password, destination2),
55 _ => throw new CryptographicException(),
56 };
57 span = destination2.Slice(0, num);
58 readOnlySpan = span;
59 cbSecret = num;
60 }
61 global::Interop.BCrypt.NTSTATUS nTSTATUS;
63 if (global::Interop.BCrypt.PseudoHandlesSupported)
64 {
65 fixed (byte* pbSecret = readOnlySpan)
66 {
67 nTSTATUS = global::Interop.BCrypt.BCryptGenerateSymmetricKey(817u, out phKey, IntPtr.Zero, 0, pbSecret, cbSecret, 0u);
68 }
69 }
70 else
71 {
72 if (s_pbkdf2AlgorithmHandle == null)
73 {
74 SafeBCryptAlgorithmHandle phAlgorithm;
75 global::Interop.BCrypt.NTSTATUS nTSTATUS2 = global::Interop.BCrypt.BCryptOpenAlgorithmProvider(out phAlgorithm, "PBKDF2", null, global::Interop.BCrypt.BCryptOpenAlgorithmProviderFlags.None);
76 if (nTSTATUS2 != 0)
77 {
78 phAlgorithm.Dispose();
80 throw global::Interop.BCrypt.CreateCryptographicException(nTSTATUS2);
81 }
83 }
84 fixed (byte* pbSecret2 = readOnlySpan)
85 {
86 nTSTATUS = global::Interop.BCrypt.BCryptGenerateSymmetricKey(s_pbkdf2AlgorithmHandle, out phKey, IntPtr.Zero, 0, pbSecret2, cbSecret, 0u);
87 }
88 }
90 if (nTSTATUS != 0)
91 {
92 phKey.Dispose();
93 throw global::Interop.BCrypt.CreateCryptographicException(nTSTATUS);
94 }
95 ulong num2 = (ulong)iterations;
96 using (phKey)
97 {
98 fixed (char* ptr2 = hashAlgorithmName)
99 {
100 fixed (byte* ptr = salt)
101 {
102 fixed (byte* pbDerivedKey = destination)
103 {
104 Span<global::Interop.BCrypt.BCryptBuffer> span3 = stackalloc global::Interop.BCrypt.BCryptBuffer[3];
105 span3[0].BufferType = global::Interop.BCrypt.CngBufferDescriptors.KDF_ITERATION_COUNT;
106 span3[0].pvBuffer = (IntPtr)(&num2);
107 span3[0].cbBuffer = 8;
108 span3[1].BufferType = global::Interop.BCrypt.CngBufferDescriptors.KDF_SALT;
109 span3[1].pvBuffer = (IntPtr)ptr;
110 span3[1].cbBuffer = salt.Length;
111 span3[2].BufferType = global::Interop.BCrypt.CngBufferDescriptors.KDF_HASH_ALGORITHM;
112 span3[2].pvBuffer = (IntPtr)ptr2;
113 span3[2].cbBuffer = checked((hashAlgorithmName.Length + 1) * 2);
114 fixed (global::Interop.BCrypt.BCryptBuffer* ptr3 = span3)
115 {
116 Unsafe.SkipInit(out global::Interop.BCrypt.BCryptBufferDesc bCryptBufferDesc);
117 bCryptBufferDesc.ulVersion = 0;
118 bCryptBufferDesc.cBuffers = span3.Length;
119 bCryptBufferDesc.pBuffers = (IntPtr)ptr3;
120 uint pcbResult;
121 global::Interop.BCrypt.NTSTATUS nTSTATUS3 = global::Interop.BCrypt.BCryptKeyDerivation(phKey, &bCryptBufferDesc, pbDerivedKey, destination.Length, out pcbResult, 0);
122 if (nTSTATUS3 != 0)
123 {
124 throw global::Interop.BCrypt.CreateCryptographicException(nTSTATUS3);
125 }
126 if (destination.Length != pcbResult)
127 {
128 throw new CryptographicException();
129 }
130 }
131 }
132 }
133 }
134 }
135 }
static SafeBCryptAlgorithmHandle s_pbkdf2AlgorithmHandle
static int GetHashBlockSize(string hashAlgorithmName)
static byte[] HashData(byte[] source)
Definition SHA1.cs:66
static byte[] HashData(byte[] source)
Definition SHA256.cs:66
static byte[] HashData(byte[] source)
Definition SHA384.cs:66
static byte[] HashData(byte[] source)
Definition SHA512.cs:66
static int CompareExchange(ref int location1, int value, int comparand)
static readonly IntPtr Zero
Definition IntPtr.cs:18
Span< T > Slice(int start)
Definition Span.cs:271

References System.Threading.Interlocked.CompareExchange(), System.destination, System.Runtime.InteropServices.SafeHandle.Dispose(), Internal.Cryptography.Pbkdf2Implementation.GetHashBlockSize(), System.Security.Cryptography.SHA1.HashData(), System.Security.Cryptography.SHA256.HashData(), System.Security.Cryptography.SHA384.HashData(), System.Security.Cryptography.SHA512.HashData(), System.ReadOnlySpan< T >.IsEmpty, System.ReadOnlySpan< T >.Length, Internal.Cryptography.Pbkdf2Implementation.s_pbkdf2AlgorithmHandle, System.Span< T >.Slice(), System.IntPtr.Zero, and System.Security.Cryptography.CryptographicOperations.ZeroMemory().

Referenced by Internal.Cryptography.Pbkdf2Implementation.Fill().