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

Move Sha1ForNonSecretPurposes under Common #45242

Merged
merged 5 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
168 changes: 168 additions & 0 deletions src/libraries/Common/src/System/Sha1ForNonSecretPurposes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Numerics;

namespace System
{
/// <summary>
/// Implements the SHA1 hashing algorithm. Note that
/// implementation is for hashing public information. Do not
/// use code to hash private data, as implementation does
/// not take any steps to avoid information disclosure.
/// </summary>
internal struct Sha1ForNonSecretPurposes
{
private long _length; // Total message length in bits
private uint[] _w; // Workspace
private int _pos; // Length of current chunk in bytes

/// <summary>
/// Call Start() to initialize the hash object.
/// </summary>
public void Start()
{
_w ??= new uint[85];

_length = 0;
_pos = 0;
_w[80] = 0x67452301;
_w[81] = 0xEFCDAB89;
_w[82] = 0x98BADCFE;
_w[83] = 0x10325476;
_w[84] = 0xC3D2E1F0;
}

/// <summary>
/// Adds an input byte to the hash.
/// </summary>
/// <param name="input">Data to include in the hash.</param>
public void Append(byte input)
{
_w[_pos / 4] = (_w[_pos / 4] << 8) | input;
jkotas marked this conversation as resolved.
Show resolved Hide resolved
jkotas marked this conversation as resolved.
Show resolved Hide resolved
if (64 == ++_pos)
{
Drain();
}
}

/// <summary>
/// Adds input bytes to the hash.
/// </summary>
/// <param name="input">
/// Data to include in the hash. Must not be null.
/// </param>
#if ES_BUILD_STANDALONE
public void Append(byte[] input)
#else
public void Append(ReadOnlySpan<byte> input)
#endif
{
foreach (byte b in input)
{
Append(b);
}
}

/// <summary>
/// Retrieves the hash value.
/// Note that after calling function, the hash object should
/// be considered uninitialized. Subsequent calls to Append or
/// Finish will produce useless results. Call Start() to
/// reinitialize.
/// </summary>
/// <param name="output">
/// Buffer to receive the hash value. Must not be null.
/// Up to 20 bytes of hash will be written to the output buffer.
/// If the buffer is smaller than 20 bytes, the remaining hash
/// bytes will be lost. If the buffer is larger than 20 bytes, the
/// rest of the buffer is left unmodified.
/// </param>
public void Finish(byte[] output)
{
long l = _length + 8 * _pos;
Append(0x80);
while (_pos != 56)
{
Append(0x00);
}

unchecked
{
Append((byte)(l >> 56));
Append((byte)(l >> 48));
Append((byte)(l >> 40));
Append((byte)(l >> 32));
Append((byte)(l >> 24));
Append((byte)(l >> 16));
Append((byte)(l >> 8));
Append((byte)l);

int end = output.Length < 20 ? output.Length : 20;
for (int i = 0; i != end; i++)
{
uint temp = _w[80 + i / 4];
output[i] = (byte)(temp >> 24);
_w[80 + i / 4] = temp << 8;
}
}
}

/// <summary>
/// Called when pos reaches 64.
/// </summary>
private void Drain()
{
for (int i = 16; i != 80; i++)
{
_w[i] = BitOperations.RotateLeft(_w[i - 3] ^ _w[i - 8] ^ _w[i - 14] ^ _w[i - 16], 1);
}

unchecked
{
uint a = _w[80];
uint b = _w[81];
uint c = _w[82];
uint d = _w[83];
uint e = _w[84];

for (int i = 0; i != 20; i++)
{
const uint k = 0x5A827999;
uint f = (b & c) | ((~b) & d);
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + _w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

for (int i = 20; i != 40; i++)
{
uint f = b ^ c ^ d;
const uint k = 0x6ED9EBA1;
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + _w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

for (int i = 40; i != 60; i++)
{
uint f = (b & c) | (b & d) | (c & d);
const uint k = 0x8F1BBCDC;
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + _w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

for (int i = 60; i != 80; i++)
{
uint f = b ^ c ^ d;
const uint k = 0xCA62C1D6;
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + _w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

_w[80] += a;
_w[81] += b;
_w[82] += c;
_w[83] += d;
_w[84] += e;
}

_length += 512; // 64 bytes == 512 bits
_pos = 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.EventUnregister.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.EventWriteString.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.EventWriteTransfer.cs" />
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.cs" />
<Compile Include="..\..\System.Private.CoreLib\src\System\Diagnostics\Tracing\ActivityTracker.cs" />
<Compile Include="..\..\System.Private.CoreLib\src\System\Diagnostics\Tracing\CounterGroup.cs" />
<Compile Include="..\..\System.Private.CoreLib\src\System\Diagnostics\Tracing\CounterPayload.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ private static int GetCurrentProcessId()
return (int)Interop.Kernel32.GetCurrentProcessId();
}
}
}

namespace System.Numerics
{
internal static class BitOperations
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,9 @@
<Compile Include="$(CommonPath)System\Obsoletions.cs">
<Link>Common\System\Obsoletions.cs</Link>
</Compile>
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.cs">
<Link>Common\System\Sha1ForNonSecretPurposes.cs</Link>
</Compile>
<Compile Include="$(CommonPath)System\SR.cs">
<Link>Common\System\SR.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1560,167 +1560,6 @@ private static string GetName(Type eventSourceType, EventManifestOptions flags)
return eventSourceType.Name;
}

/// <summary>
/// Implements the SHA1 hashing algorithm. Note that this
/// implementation is for hashing public information. Do not
/// use this code to hash private data, as this implementation does
/// not take any steps to avoid information disclosure.
/// </summary>
private struct Sha1ForNonSecretPurposes
{
private long length; // Total message length in bits
private uint[] w; // Workspace
private int pos; // Length of current chunk in bytes

/// <summary>
/// Call Start() to initialize the hash object.
/// </summary>
public void Start()
{
this.w ??= new uint[85];

this.length = 0;
this.pos = 0;
this.w[80] = 0x67452301;
this.w[81] = 0xEFCDAB89;
this.w[82] = 0x98BADCFE;
this.w[83] = 0x10325476;
this.w[84] = 0xC3D2E1F0;
}

/// <summary>
/// Adds an input byte to the hash.
/// </summary>
/// <param name="input">Data to include in the hash.</param>
public void Append(byte input)
{
this.w[this.pos / 4] = (this.w[this.pos / 4] << 8) | input;
if (64 == ++this.pos)
{
this.Drain();
}
}

/// <summary>
/// Adds input bytes to the hash.
/// </summary>
/// <param name="input">
/// Data to include in the hash. Must not be null.
/// </param>
#if ES_BUILD_STANDALONE
public void Append(byte[] input)
#else
public void Append(ReadOnlySpan<byte> input)
#endif
{
foreach (byte b in input)
{
this.Append(b);
}
}

/// <summary>
/// Retrieves the hash value.
/// Note that after calling this function, the hash object should
/// be considered uninitialized. Subsequent calls to Append or
/// Finish will produce useless results. Call Start() to
/// reinitialize.
/// </summary>
/// <param name="output">
/// Buffer to receive the hash value. Must not be null.
/// Up to 20 bytes of hash will be written to the output buffer.
/// If the buffer is smaller than 20 bytes, the remaining hash
/// bytes will be lost. If the buffer is larger than 20 bytes, the
/// rest of the buffer is left unmodified.
/// </param>
public void Finish(byte[] output)
{
long l = this.length + 8 * this.pos;
this.Append(0x80);
while (this.pos != 56)
{
this.Append(0x00);
}

unchecked
{
this.Append((byte)(l >> 56));
this.Append((byte)(l >> 48));
this.Append((byte)(l >> 40));
this.Append((byte)(l >> 32));
this.Append((byte)(l >> 24));
this.Append((byte)(l >> 16));
this.Append((byte)(l >> 8));
this.Append((byte)l);

int end = output.Length < 20 ? output.Length : 20;
for (int i = 0; i != end; i++)
{
uint temp = this.w[80 + i / 4];
output[i] = (byte)(temp >> 24);
this.w[80 + i / 4] = temp << 8;
}
}
}

/// <summary>
/// Called when this.pos reaches 64.
/// </summary>
private void Drain()
{
for (int i = 16; i != 80; i++)
{
this.w[i] = BitOperations.RotateLeft(this.w[i - 3] ^ this.w[i - 8] ^ this.w[i - 14] ^ this.w[i - 16], 1);
}

unchecked
{
uint a = this.w[80];
uint b = this.w[81];
uint c = this.w[82];
uint d = this.w[83];
uint e = this.w[84];

for (int i = 0; i != 20; i++)
{
const uint k = 0x5A827999;
uint f = (b & c) | ((~b) & d);
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + this.w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

for (int i = 20; i != 40; i++)
{
uint f = b ^ c ^ d;
const uint k = 0x6ED9EBA1;
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + this.w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

for (int i = 40; i != 60; i++)
{
uint f = (b & c) | (b & d) | (c & d);
const uint k = 0x8F1BBCDC;
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + this.w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

for (int i = 60; i != 80; i++)
{
uint f = b ^ c ^ d;
const uint k = 0xCA62C1D6;
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + this.w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

this.w[80] += a;
this.w[81] += b;
this.w[82] += c;
this.w[83] += d;
this.w[84] += e;
}

this.length += 512; // 64 bytes == 512 bits
this.pos = 0;
}
}

private static Guid GenerateGuidFromName(string name)
{
#if ES_BUILD_STANDALONE
Expand Down
Loading