Skip to content

Commit 0178e94

Browse files
committed
Git struggles :(
2 parents 7107f70 + cde11b9 commit 0178e94

File tree

3 files changed

+151
-99
lines changed

3 files changed

+151
-99
lines changed

src/SshNet.Security.Cryptography.Shared/HMAC.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public abstract class HMAC : KeyedHashAlgorithm
1313
private byte[] _outerPadding;
1414
private readonly int _hashSize;
1515

16+
/// <summary>
17+
/// Holds value indicating whether the inner padding was already written.
18+
/// </summary>
19+
private bool _innerPaddingWritten;
20+
1621
/// <summary>
1722
/// Gets the size of the block.
1823
/// </summary>
@@ -68,7 +73,7 @@ internal HMAC(IHashProvider hashProvider, byte[] key, int hashSize)
6873
internal HMAC(IHashProvider hashProvider, byte[] key)
6974
: this(hashProvider)
7075
{
71-
InternalInitialize(key);
76+
SetKey(key);
7277
}
7378

7479
/// <summary>
@@ -85,7 +90,6 @@ public override byte[] Key
8590
}
8691
set
8792
{
88-
_hashProvider.Reset();
8993
SetKey(value);
9094
}
9195
}
@@ -95,7 +99,8 @@ public override byte[] Key
9599
/// </summary>
96100
public override void Initialize()
97101
{
98-
InternalInitialize(Key);
102+
_hashProvider.Reset();
103+
_innerPaddingWritten = false;
99104
}
100105

101106
/// <summary>
@@ -106,6 +111,15 @@ public override void Initialize()
106111
/// <param name="cb">The cb.</param>
107112
protected override void HashCore(byte[] rgb, int ib, int cb)
108113
{
114+
if (!_innerPaddingWritten)
115+
{
116+
// write the inner padding
117+
_hashProvider.TransformBlock(_innerPadding, 0, BlockSize, _innerPadding, 0);
118+
119+
// ensure we only write inner padding once
120+
_innerPaddingWritten = true;
121+
}
122+
109123
_hashProvider.HashCore(rgb, ib, cb);
110124
}
111125

@@ -117,17 +131,13 @@ protected override void HashCore(byte[] rgb, int ib, int cb)
117131
/// </returns>
118132
protected override byte[] HashFinal()
119133
{
120-
// Finalize the original hash.
121-
_hashProvider.TransformFinalBlock(new byte[0], 0, 0);
134+
// finalize the original hash
135+
var hashValue = _hashProvider.ComputeHash(new byte[0]);
122136

123-
var hashValue = _hashProvider.Hash;
124-
125-
_hashProvider.Reset();
126-
127-
// Write the outer array.
137+
// write the outer padding
128138
_hashProvider.TransformBlock(_outerPadding, 0, BlockSize, _outerPadding, 0);
129139

130-
// Write the inner hash and finalize the hash.
140+
// write the inner hash and finalize the hash
131141
_hashProvider.TransformFinalBlock(hashValue, 0, hashValue.Length);
132142

133143
var hash = _hashProvider.Hash;
@@ -155,7 +165,6 @@ private byte[] GetTruncatedHash(byte[] hash)
155165
var hashSizeBytes = HashSize / 8;
156166
if (hash.Length == hashSizeBytes)
157167
{
158-
// TODO: check if we should clone the hash
159168
return hash;
160169
}
161170

@@ -164,11 +173,6 @@ private byte[] GetTruncatedHash(byte[] hash)
164173
return truncatedHash;
165174
}
166175

167-
private void InternalInitialize(byte[] key)
168-
{
169-
SetKey(key);
170-
}
171-
172176
private void SetKey(byte[] value)
173177
{
174178
var shortenedKey = GetShortenedKey(value);
@@ -188,8 +192,6 @@ private void SetKey(byte[] value)
188192
_outerPadding[i] = 0x5C;
189193
}
190194

191-
_hashProvider.TransformBlock(_innerPadding, 0, BlockSize, _innerPadding, 0);
192-
193195
// no need to explicitly clone as this is already done in the setter
194196
base.Key = shortenedKey;
195197
}

src/SshNet.Security.Cryptography.Shared/MD5HashProvider.cs

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ public override void Reset()
161161

162162
private void InitializeHashValue()
163163
{
164-
_h1 = unchecked(0x67452301);
164+
_h1 = 0x67452301;
165165
_h2 = unchecked((int) 0xefcdab89);
166166
_h3 = unchecked((int) 0x98badcfe);
167-
_h4 = unchecked(0x10325476);
167+
_h4 = 0x10325476;
168168
}
169169

170170
private void Update(byte input)
@@ -272,82 +272,82 @@ private void ProcessBlock()
272272
//
273273
// Round 1 - F cycle, 16 times.
274274
//
275-
a = RotateLeft((a + F(b, c, d) + _x[0] + unchecked((int) 0xd76aa478)), S11) + b;
276-
d = RotateLeft((d + F(a, b, c) + _x[1] + unchecked((int) 0xe8c7b756)), S12) + a;
277-
c = RotateLeft((c + F(d, a, b) + _x[2] + unchecked(0x242070db)), S13) + d;
278-
b = RotateLeft((b + F(c, d, a) + _x[3] + unchecked((int) 0xc1bdceee)), S14) + c;
279-
a = RotateLeft((a + F(b, c, d) + _x[4] + unchecked((int) 0xf57c0faf)), S11) + b;
280-
d = RotateLeft((d + F(a, b, c) + _x[5] + unchecked(0x4787c62a)), S12) + a;
281-
c = RotateLeft((c + F(d, a, b) + _x[6] + unchecked((int) 0xa8304613)), S13) + d;
282-
b = RotateLeft((b + F(c, d, a) + _x[7] + unchecked((int) 0xfd469501)), S14) + c;
283-
a = RotateLeft((a + F(b, c, d) + _x[8] + unchecked(0x698098d8)), S11) + b;
284-
d = RotateLeft((d + F(a, b, c) + _x[9] + unchecked((int) 0x8b44f7af)), S12) + a;
285-
c = RotateLeft((c + F(d, a, b) + _x[10] + unchecked((int) 0xffff5bb1)), S13) + d;
286-
b = RotateLeft((b + F(c, d, a) + _x[11] + unchecked((int) 0x895cd7be)), S14) + c;
287-
a = RotateLeft((a + F(b, c, d) + _x[12] + unchecked(0x6b901122)), S11) + b;
288-
d = RotateLeft((d + F(a, b, c) + _x[13] + unchecked((int) 0xfd987193)), S12) + a;
289-
c = RotateLeft((c + F(d, a, b) + _x[14] + unchecked((int) 0xa679438e)), S13) + d;
290-
b = RotateLeft((b + F(c, d, a) + _x[15] + unchecked(0x49b40821)), S14) + c;
275+
a = RotateLeft(a + F(b, c, d) + _x[0] + unchecked((int) 0xd76aa478), S11) + b;
276+
d = RotateLeft(d + F(a, b, c) + _x[1] + unchecked((int) 0xe8c7b756), S12) + a;
277+
c = RotateLeft(c + F(d, a, b) + _x[2] + 0x242070db, S13) + d;
278+
b = RotateLeft(b + F(c, d, a) + _x[3] + unchecked((int) 0xc1bdceee), S14) + c;
279+
a = RotateLeft(a + F(b, c, d) + _x[4] + unchecked((int) 0xf57c0faf), S11) + b;
280+
d = RotateLeft(d + F(a, b, c) + _x[5] + 0x4787c62a, S12) + a;
281+
c = RotateLeft(c + F(d, a, b) + _x[6] + unchecked((int) 0xa8304613), S13) + d;
282+
b = RotateLeft(b + F(c, d, a) + _x[7] + unchecked((int) 0xfd469501), S14) + c;
283+
a = RotateLeft(a + F(b, c, d) + _x[8] + 0x698098d8, S11) + b;
284+
d = RotateLeft(d + F(a, b, c) + _x[9] + unchecked((int) 0x8b44f7af), S12) + a;
285+
c = RotateLeft(c + F(d, a, b) + _x[10] + unchecked((int) 0xffff5bb1), S13) + d;
286+
b = RotateLeft(b + F(c, d, a) + _x[11] + unchecked((int) 0x895cd7be), S14) + c;
287+
a = RotateLeft(a + F(b, c, d) + _x[12] + 0x6b901122, S11) + b;
288+
d = RotateLeft(d + F(a, b, c) + _x[13] + unchecked((int) 0xfd987193), S12) + a;
289+
c = RotateLeft(c + F(d, a, b) + _x[14] + unchecked((int) 0xa679438e), S13) + d;
290+
b = RotateLeft(b + F(c, d, a) + _x[15] + 0x49b40821, S14) + c;
291291

292292
//
293293
// Round 2 - G cycle, 16 times.
294294
//
295-
a = RotateLeft((a + G(b, c, d) + _x[1] + unchecked((int) 0xf61e2562)), S21) + b;
296-
d = RotateLeft((d + G(a, b, c) + _x[6] + unchecked((int) 0xc040b340)), S22) + a;
297-
c = RotateLeft((c + G(d, a, b) + _x[11] + unchecked(0x265e5a51)), S23) + d;
298-
b = RotateLeft((b + G(c, d, a) + _x[0] + unchecked((int) 0xe9b6c7aa)), S24) + c;
299-
a = RotateLeft((a + G(b, c, d) + _x[5] + unchecked((int) 0xd62f105d)), S21) + b;
300-
d = RotateLeft((d + G(a, b, c) + _x[10] + unchecked(0x02441453)), S22) + a;
301-
c = RotateLeft((c + G(d, a, b) + _x[15] + unchecked((int) 0xd8a1e681)), S23) + d;
302-
b = RotateLeft((b + G(c, d, a) + _x[4] + unchecked((int) 0xe7d3fbc8)), S24) + c;
303-
a = RotateLeft((a + G(b, c, d) + _x[9] + unchecked(0x21e1cde6)), S21) + b;
304-
d = RotateLeft((d + G(a, b, c) + _x[14] + unchecked((int) 0xc33707d6)), S22) + a;
305-
c = RotateLeft((c + G(d, a, b) + _x[3] + unchecked((int) 0xf4d50d87)), S23) + d;
306-
b = RotateLeft((b + G(c, d, a) + _x[8] + unchecked(0x455a14ed)), S24) + c;
307-
a = RotateLeft((a + G(b, c, d) + _x[13] + unchecked((int) 0xa9e3e905)), S21) + b;
308-
d = RotateLeft((d + G(a, b, c) + _x[2] + unchecked((int) 0xfcefa3f8)), S22) + a;
309-
c = RotateLeft((c + G(d, a, b) + _x[7] + unchecked(0x676f02d9)), S23) + d;
310-
b = RotateLeft((b + G(c, d, a) + _x[12] + unchecked((int) 0x8d2a4c8a)), S24) + c;
295+
a = RotateLeft(a + G(b, c, d) + _x[1] + unchecked((int) 0xf61e2562), S21) + b;
296+
d = RotateLeft(d + G(a, b, c) + _x[6] + unchecked((int) 0xc040b340), S22) + a;
297+
c = RotateLeft(c + G(d, a, b) + _x[11] + 0x265e5a51, S23) + d;
298+
b = RotateLeft(b + G(c, d, a) + _x[0] + unchecked((int) 0xe9b6c7aa), S24) + c;
299+
a = RotateLeft(a + G(b, c, d) + _x[5] + unchecked((int) 0xd62f105d), S21) + b;
300+
d = RotateLeft(d + G(a, b, c) + _x[10] + 0x02441453, S22) + a;
301+
c = RotateLeft(c + G(d, a, b) + _x[15] + unchecked((int) 0xd8a1e681), S23) + d;
302+
b = RotateLeft(b + G(c, d, a) + _x[4] + unchecked((int) 0xe7d3fbc8), S24) + c;
303+
a = RotateLeft(a + G(b, c, d) + _x[9] + 0x21e1cde6, S21) + b;
304+
d = RotateLeft(d + G(a, b, c) + _x[14] + unchecked((int) 0xc33707d6), S22) + a;
305+
c = RotateLeft(c + G(d, a, b) + _x[3] + unchecked((int) 0xf4d50d87), S23) + d;
306+
b = RotateLeft(b + G(c, d, a) + _x[8] + 0x455a14ed, S24) + c;
307+
a = RotateLeft(a + G(b, c, d) + _x[13] + unchecked((int) 0xa9e3e905), S21) + b;
308+
d = RotateLeft(d + G(a, b, c) + _x[2] + unchecked((int) 0xfcefa3f8), S22) + a;
309+
c = RotateLeft(c + G(d, a, b) + _x[7] + 0x676f02d9, S23) + d;
310+
b = RotateLeft(b + G(c, d, a) + _x[12] + unchecked((int) 0x8d2a4c8a), S24) + c;
311311

312312
//
313313
// Round 3 - H cycle, 16 times.
314314
//
315-
a = RotateLeft((a + H(b, c, d) + _x[5] + unchecked((int)0xfffa3942)), S31) + b;
316-
d = RotateLeft((d + H(a, b, c) + _x[8] + unchecked((int)0x8771f681)), S32) + a;
317-
c = RotateLeft((c + H(d, a, b) + _x[11] + unchecked(0x6d9d6122)), S33) + d;
318-
b = RotateLeft((b + H(c, d, a) + _x[14] + unchecked((int)0xfde5380c)), S34) + c;
319-
a = RotateLeft((a + H(b, c, d) + _x[1] + unchecked((int)0xa4beea44)), S31) + b;
320-
d = RotateLeft((d + H(a, b, c) + _x[4] + unchecked(0x4bdecfa9)), S32) + a;
321-
c = RotateLeft((c + H(d, a, b) + _x[7] + unchecked((int)0xf6bb4b60)), S33) + d;
322-
b = RotateLeft((b + H(c, d, a) + _x[10] + unchecked((int)0xbebfbc70)), S34) + c;
323-
a = RotateLeft((a + H(b, c, d) + _x[13] + unchecked(0x289b7ec6)), S31) + b;
324-
d = RotateLeft((d + H(a, b, c) + _x[0] + unchecked((int)0xeaa127fa)), S32) + a;
325-
c = RotateLeft((c + H(d, a, b) + _x[3] + unchecked((int)0xd4ef3085)), S33) + d;
326-
b = RotateLeft((b + H(c, d, a) + _x[6] + unchecked(0x04881d05)), S34) + c;
327-
a = RotateLeft((a + H(b, c, d) + _x[9] + unchecked((int)0xd9d4d039)), S31) + b;
328-
d = RotateLeft((d + H(a, b, c) + _x[12] + unchecked((int)0xe6db99e5)), S32) + a;
329-
c = RotateLeft((c + H(d, a, b) + _x[15] + unchecked(0x1fa27cf8)), S33) + d;
330-
b = RotateLeft((b + H(c, d, a) + _x[2] + unchecked((int)0xc4ac5665)), S34) + c;
315+
a = RotateLeft(a + H(b, c, d) + _x[5] + unchecked((int) 0xfffa3942), S31) + b;
316+
d = RotateLeft(d + H(a, b, c) + _x[8] + unchecked((int) 0x8771f681), S32) + a;
317+
c = RotateLeft(c + H(d, a, b) + _x[11] + 0x6d9d6122, S33) + d;
318+
b = RotateLeft(b + H(c, d, a) + _x[14] + unchecked((int) 0xfde5380c), S34) + c;
319+
a = RotateLeft(a + H(b, c, d) + _x[1] + unchecked((int) 0xa4beea44), S31) + b;
320+
d = RotateLeft(d + H(a, b, c) + _x[4] + 0x4bdecfa9, S32) + a;
321+
c = RotateLeft(c + H(d, a, b) + _x[7] + unchecked((int) 0xf6bb4b60), S33) + d;
322+
b = RotateLeft(b + H(c, d, a) + _x[10] + unchecked((int) 0xbebfbc70), S34) + c;
323+
a = RotateLeft(a + H(b, c, d) + _x[13] + 0x289b7ec6, S31) + b;
324+
d = RotateLeft(d + H(a, b, c) + _x[0] + unchecked((int) 0xeaa127fa), S32) + a;
325+
c = RotateLeft(c + H(d, a, b) + _x[3] + unchecked((int) 0xd4ef3085), S33) + d;
326+
b = RotateLeft(b + H(c, d, a) + _x[6] + 0x04881d05, S34) + c;
327+
a = RotateLeft(a + H(b, c, d) + _x[9] + unchecked((int) 0xd9d4d039), S31) + b;
328+
d = RotateLeft(d + H(a, b, c) + _x[12] + unchecked((int) 0xe6db99e5), S32) + a;
329+
c = RotateLeft(c + H(d, a, b) + _x[15] + 0x1fa27cf8, S33) + d;
330+
b = RotateLeft(b + H(c, d, a) + _x[2] + unchecked((int) 0xc4ac5665), S34) + c;
331331

332332
//
333333
// Round 4 - K cycle, 16 times.
334334
//
335-
a = RotateLeft((a + K(b, c, d) + _x[0] + unchecked((int)0xf4292244)), S41) + b;
336-
d = RotateLeft((d + K(a, b, c) + _x[7] + unchecked(0x432aff97)), S42) + a;
337-
c = RotateLeft((c + K(d, a, b) + _x[14] + unchecked((int)0xab9423a7)), S43) + d;
338-
b = RotateLeft((b + K(c, d, a) + _x[5] + unchecked((int)0xfc93a039)), S44) + c;
339-
a = RotateLeft((a + K(b, c, d) + _x[12] + unchecked(0x655b59c3)), S41) + b;
340-
d = RotateLeft((d + K(a, b, c) + _x[3] + unchecked((int)0x8f0ccc92)), S42) + a;
341-
c = RotateLeft((c + K(d, a, b) + _x[10] + unchecked((int)0xffeff47d)), S43) + d;
342-
b = RotateLeft((b + K(c, d, a) + _x[1] + unchecked((int)0x85845dd1)), S44) + c;
343-
a = RotateLeft((a + K(b, c, d) + _x[8] + unchecked(0x6fa87e4f)), S41) + b;
344-
d = RotateLeft((d + K(a, b, c) + _x[15] + unchecked((int)0xfe2ce6e0)), S42) + a;
345-
c = RotateLeft((c + K(d, a, b) + _x[6] + unchecked((int)0xa3014314)), S43) + d;
346-
b = RotateLeft((b + K(c, d, a) + _x[13] + unchecked(0x4e0811a1)), S44) + c;
347-
a = RotateLeft((a + K(b, c, d) + _x[4] + unchecked((int)0xf7537e82)), S41) + b;
348-
d = RotateLeft((d + K(a, b, c) + _x[11] + unchecked((int)0xbd3af235)), S42) + a;
349-
c = RotateLeft((c + K(d, a, b) + _x[2] + unchecked(0x2ad7d2bb)), S43) + d;
350-
b = RotateLeft((b + K(c, d, a) + _x[9] + unchecked((int)0xeb86d391)), S44) + c;
335+
a = RotateLeft(a + K(b, c, d) + _x[0] + unchecked((int) 0xf4292244), S41) + b;
336+
d = RotateLeft(d + K(a, b, c) + _x[7] + 0x432aff97, S42) + a;
337+
c = RotateLeft(c + K(d, a, b) + _x[14] + unchecked((int) 0xab9423a7), S43) + d;
338+
b = RotateLeft(b + K(c, d, a) + _x[5] + unchecked((int) 0xfc93a039), S44) + c;
339+
a = RotateLeft(a + K(b, c, d) + _x[12] + 0x655b59c3, S41) + b;
340+
d = RotateLeft(d + K(a, b, c) + _x[3] + unchecked((int) 0x8f0ccc92), S42) + a;
341+
c = RotateLeft(c + K(d, a, b) + _x[10] + unchecked((int) 0xffeff47d), S43) + d;
342+
b = RotateLeft(b + K(c, d, a) + _x[1] + unchecked((int) 0x85845dd1), S44) + c;
343+
a = RotateLeft(a + K(b, c, d) + _x[8] + 0x6fa87e4f, S41) + b;
344+
d = RotateLeft(d + K(a, b, c) + _x[15] + unchecked((int) 0xfe2ce6e0), S42) + a;
345+
c = RotateLeft(c + K(d, a, b) + _x[6] + unchecked((int) 0xa3014314), S43) + d;
346+
b = RotateLeft(b + K(c, d, a) + _x[13] + 0x4e0811a1, S44) + c;
347+
a = RotateLeft(a + K(b, c, d) + _x[4] + unchecked((int) 0xf7537e82), S41) + b;
348+
d = RotateLeft(d + K(a, b, c) + _x[11] + unchecked((int) 0xbd3af235), S42) + a;
349+
c = RotateLeft(c + K(d, a, b) + _x[2] + 0x2ad7d2bb, S43) + d;
350+
b = RotateLeft(b + K(c, d, a) + _x[9] + unchecked((int) 0xeb86d391), S44) + c;
351351

352352
_h1 += a;
353353
_h2 += b;

0 commit comments

Comments
 (0)