Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hash/hmac one shots in NTLM #70857

Merged
merged 2 commits into from
Jun 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions src/libraries/Common/src/System/Net/NTAuthentication.Managed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,10 @@ private static void makeNtlm2Hash(string domain, string userName, ReadOnlySpan<c
{
Encoding.Unicode.GetBytes(password, pwBytes);
MD4.HashData(pwBytes, pwHash);

using (var hmac = IncrementalHash.CreateHMAC(HashAlgorithmName.MD5, pwHash))
{
// strangely, user is upper case, domain is not.
byte[] blob = Encoding.Unicode.GetBytes(string.Concat(userName.ToUpperInvariant(), domain));
hmac.AppendData(blob);
hmac.GetHashAndReset(hash);
}
// strangely, user is upper case, domain is not.
byte[] blob = Encoding.Unicode.GetBytes(string.Concat(userName.ToUpperInvariant(), domain));
int written = HMACMD5.HashData(pwHash, blob, hash);
Debug.Assert(written == HMACMD5.HashSizeInBytes);
}
finally
{
Expand Down Expand Up @@ -502,16 +498,12 @@ private unsafe void WriteChannelBindingHash(Span<byte> hashBuffer)
{
IntPtr cbtData = _channelBinding.DangerousGetHandle();
int cbtDataSize = _channelBinding.Size;

using (var md5 = IncrementalHash.CreateHash(HashAlgorithmName.MD5))
{
md5.AppendData(new Span<byte>((void *)cbtData, cbtDataSize));
md5.GetHashAndReset(hashBuffer);
}
int written = MD5.HashData(new Span<byte>((void*)cbtData, cbtDataSize), hashBuffer);
Debug.Assert(written == MD5.HashSizeInBytes);
}
else
{
hashBuffer.Fill(0);
hashBuffer.Clear();
}
}

Expand Down Expand Up @@ -716,12 +708,9 @@ private static byte[] DeriveKey(ReadOnlySpan<byte> exportedSessionKey, ReadOnlyS
Debug.Assert(flags.HasFlag(Flags.NegotiateSign) && flags.HasFlag(Flags.NegotiateKeyExchange));

// Derive session base key
Span<byte> sessionBaseKey = stackalloc byte[16];
using (var hmacSessionKey = IncrementalHash.CreateHMAC(HashAlgorithmName.MD5, ntlm2hash))
{
hmacSessionKey.AppendData(responseAsSpan.Slice(response.NtChallengeResponse.PayloadOffset, 16));
hmacSessionKey.GetHashAndReset(sessionBaseKey);
}
Span<byte> sessionBaseKey = stackalloc byte[HMACMD5.HashSizeInBytes];
int sessionKeyWritten = HMACMD5.HashData(ntlm2hash, responseAsSpan.Slice(response.NtChallengeResponse.PayloadOffset, 16), sessionBaseKey);
Debug.Assert(sessionKeyWritten == HMACMD5.HashSizeInBytes);

// Encrypt exportedSessionKey with sessionBaseKey
using (RC4 rc4 = new RC4(sessionBaseKey))
Expand Down