Fix Aes -ctr algorithms

Move hmac-md5 hashing algorithm into library
This commit is contained in:
olegkap_cp
2011-07-13 14:41:13 +00:00
parent 7d8c9cf61e
commit e3d3796d33
9 changed files with 565 additions and 14 deletions
@@ -441,6 +441,12 @@
<Compile Include="..\Renci.SshNet\Security\Cryptography\Ciphers\TwofishCipher.cs">
<Link>Security\Cryptography\Ciphers\TwofishCipher.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\Security\Cryptography\HMAC.cs">
<Link>Security\Cryptography\HMAC.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\Security\Cryptography\MD5.cs">
<Link>Security\Cryptography\MD5.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\Security\Cryptography\Modes\CbcMode.cs">
<Link>Security\Cryptography\Modes\CbcMode.cs</Link>
</Compile>
@@ -483,6 +489,9 @@
<Compile Include="..\Renci.SshNet\Security\HMac.cs">
<Link>Security\HMac.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\Security\HMacMD5.cs">
<Link>Security\HMacMD5.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet\Security\HMacSha1.cs">
<Link>Security\HMacSha1.cs</Link>
</Compile>
@@ -199,9 +199,9 @@ namespace Renci.SshNet
/// <param name="host">Connection host.</param>
/// <param name="port">Connection port.</param>
/// <param name="username">Connection username.</param>
/// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="username"/> is null or empty.</exception>
protected ConnectionInfo(string host, int port, string username)
: this()
{
@@ -130,6 +130,12 @@
<Compile Include="Security\CipherTripleDesCbc.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Security\Cryptography\HMAC.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Security\Cryptography\MD5.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Security\CryptoKey.cs">
<SubType>Code</SubType>
</Compile>
@@ -157,9 +163,7 @@
<Compile Include="Security\HMacMD5.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Security\HMacSha1.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Security\HMacSha1.cs" />
<Compile Include="Security\KeyExchange.cs">
<SubType>Code</SubType>
</Compile>
@@ -381,7 +385,6 @@
<ItemGroup>
<Content Include="Documentation\SshClient.shfbproj" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -22,7 +22,7 @@ namespace Renci.SshNet.Security
{
get
{
return this._keySize / 8;
return this._keySize;
}
}
@@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace Renci.SshNet.Security.Cryptography
{
/// <summary>
/// Provides HMAC algorithm implementation.
/// </summary>
/// <typeparam name="T"></typeparam>
public class HMAC<T> : KeyedHashAlgorithm where T : HashAlgorithm, new()
{
private HashAlgorithm _hash;
private bool _isHashing;
private byte[] _innerPadding;
private byte[] _outerPadding;
/// <summary>
/// Gets the size of the block.
/// </summary>
/// <value>
/// The size of the block.
/// </value>
protected int BlockSize
{
get
{
return this._hash.InputBlockSize;
}
}
/// <summary>
/// Rfc 2104.
/// </summary>
/// <param name="key">The key.</param>
public HMAC(byte[] key)
{
// Create the hash algorithms.
this._hash = new T();
this.HashSizeValue = this._hash.HashSize;
this.Key = key;
this.Initialize();
}
/// <summary>
///
/// </summary>
public override byte[] Key
{
get { return (byte[])KeyValue.Clone(); }
set
{
if (this._isHashing)
{
throw new Exception("Cannot change key during hash operation");
}
if (value.Length > this.BlockSize)
{
this.KeyValue = this._hash.ComputeHash(value);
// No need to call Initialize, ComputeHash does it automatically.
}
else
{
this.KeyValue = value.Clone() as byte[];
}
this._innerPadding = new byte[this.BlockSize];
this._outerPadding = new byte[this.BlockSize];
// Compute inner and outer padding.
int i = 0;
for (i = 0; i < 64; i++)
{
this._innerPadding[i] = 0x36;
this._outerPadding[i] = 0x5C;
}
for (i = 0; i < this.KeyValue.Length; i++)
{
this._innerPadding[i] ^= this.KeyValue[i];
this._outerPadding[i] ^= this.KeyValue[i];
}
}
}
/// <summary>
///
/// </summary>
public override void Initialize()
{
this._isHashing = false;
}
/// <summary>
///
/// </summary>
/// <param name="rgb"></param>
/// <param name="ib"></param>
/// <param name="cb"></param>
protected override void HashCore(byte[] rgb, int ib, int cb)
{
if (!this._isHashing)
{
this._hash.TransformBlock(this._innerPadding, 0, this.BlockSize, this._innerPadding, 0);
this._isHashing = true;
}
this._hash.TransformBlock(rgb, ib, cb, rgb, ib);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected override byte[] HashFinal()
{
if (!this._isHashing)
{
this._hash.TransformBlock(this._innerPadding, 0, 64, this._innerPadding, 0);
this._isHashing = true;
}
// Finalize the original hash.
this._hash.TransformFinalBlock(new byte[0], 0, 0);
var hashValue = this._hash.Hash;
// Write the outer array.
this._hash.TransformBlock(this._outerPadding, 0, this.BlockSize, this._outerPadding, 0);
// Write the inner hash and finalize the hash.
this._hash.TransformFinalBlock(hashValue, 0, hashValue.Length);
this._isHashing = false;
return this._hash.Hash;
}
}
}
@@ -0,0 +1,383 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace Renci.SshNet.Security.Cryptography
{
/// <summary>
/// MD5 algorithm implementation
/// </summary>
public class MD5 : HashAlgorithm
{
private byte[] _buffer = new byte[4];
private int _bufferOffset;
private long _byteCount;
private int H1, H2, H3, H4; // IV's
private int[] _hashValue = new int[16];
private int _offset;
/// <summary>
/// Gets the size, in bits, of the computed hash code.
/// </summary>
/// <returns>The size, in bits, of the computed hash code.</returns>
public override int HashSize
{
get
{
return 128;
}
}
/// <summary>
/// Gets the input block size.
/// </summary>
/// <returns>The input block size.</returns>
public override int InputBlockSize
{
get
{
return 64;
}
}
/// <summary>
/// Gets the output block size.
/// </summary>
/// <returns>The output block size.</returns>
public override int OutputBlockSize
{
get
{
return 64;
}
}
/// <summary>
/// Gets a value indicating whether the current transform can be reused.
/// </summary>
/// <returns>Always true.</returns>
public override bool CanReuseTransform
{
get
{
return true;
}
}
/// <summary>
/// Gets a value indicating whether multiple blocks can be transformed.
/// </summary>
/// <returns>true if multiple blocks can be transformed; otherwise, false.</returns>
public override bool CanTransformMultipleBlocks
{
get
{
return true;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="MD5"/> class.
/// </summary>
public MD5()
{
this.Initialize();
}
/// <summary>
/// Routes data written to the object into the hash algorithm for computing the hash.
/// </summary>
/// <param name="array">The input to compute the hash code for.</param>
/// <param name="ibStart">The offset into the byte array from which to begin using data.</param>
/// <param name="cbSize">The number of bytes in the byte array to use as data.</param>
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
// Fill the current word
while ((this._bufferOffset != 0) && (cbSize > 0))
{
this.Update(array[ibStart]);
ibStart++;
cbSize--;
}
// Process whole words.
while (cbSize > this._buffer.Length)
{
this.ProcessWord(array, ibStart);
ibStart += this._buffer.Length;
cbSize -= this._buffer.Length;
this._byteCount += this._buffer.Length;
}
// Load in the remainder.
while (cbSize > 0)
{
this.Update(array[ibStart]);
ibStart++;
cbSize--;
}
}
/// <summary>
/// Finalizes the hash computation after the last data is processed by the cryptographic stream object.
/// </summary>
/// <returns>
/// The computed hash code.
/// </returns>
protected override byte[] HashFinal()
{
long bitLength = (this._byteCount << 3);
// Add the pad bytes.
this.Update((byte)128);
while (this._bufferOffset != 0)
this.Update((byte)0);
if (this._offset > 14)
{
this.ProcessBlock();
}
this._hashValue[14] = (int)(bitLength & 0xffffffff);
this._hashValue[15] = (int)((ulong)bitLength >> 32);
this.ProcessBlock();
var output = new byte[16];
this.UnpackWord(H1, output, 0);
this.UnpackWord(H2, output, 0 + 4);
this.UnpackWord(H3, output, 0 + 8);
this.UnpackWord(H4, output, 0 + 12);
this.Initialize();
return output;
}
/// <summary>
/// Initializes an implementation of the <see cref="T:System.Security.Cryptography.HashAlgorithm"/> class.
/// </summary>
public override void Initialize()
{
this._byteCount = 0;
this._bufferOffset = 0;
Array.Clear(this._buffer, 0, this._buffer.Length);
H1 = unchecked((int)0x67452301);
H2 = unchecked((int)0xefcdab89);
H3 = unchecked((int)0x98badcfe);
H4 = unchecked((int)0x10325476);
this._offset = 0;
for (int i = 0; i != this._hashValue.Length; i++)
{
this._hashValue[i] = 0;
}
}
private void Update(byte input)
{
this._buffer[this._bufferOffset++] = input;
if (this._bufferOffset == this._buffer.Length)
{
this.ProcessWord(this._buffer, 0);
this._bufferOffset = 0;
}
this._byteCount++;
}
private void ProcessWord(byte[] input, int inOff)
{
this._hashValue[this._offset++] = (input[inOff] & 0xff) | ((input[inOff + 1] & 0xff) << 8)
| ((input[inOff + 2] & 0xff) << 16) | ((input[inOff + 3] & 0xff) << 24);
if (this._offset == 16)
{
ProcessBlock();
}
}
private void UnpackWord(int word, byte[] outBytes, int outOff)
{
outBytes[outOff] = (byte)word;
outBytes[outOff + 1] = (byte)((uint)word >> 8);
outBytes[outOff + 2] = (byte)((uint)word >> 16);
outBytes[outOff + 3] = (byte)((uint)word >> 24);
}
//
// round 1 left rotates
//
private static readonly int S11 = 7;
private static readonly int S12 = 12;
private static readonly int S13 = 17;
private static readonly int S14 = 22;
//
// round 2 left rotates
//
private static readonly int S21 = 5;
private static readonly int S22 = 9;
private static readonly int S23 = 14;
private static readonly int S24 = 20;
//
// round 3 left rotates
//
private static readonly int S31 = 4;
private static readonly int S32 = 11;
private static readonly int S33 = 16;
private static readonly int S34 = 23;
//
// round 4 left rotates
//
private static readonly int S41 = 6;
private static readonly int S42 = 10;
private static readonly int S43 = 15;
private static readonly int S44 = 21;
/*
* rotate int x left n bits.
*/
private int RotateLeft(int x, int n)
{
return (x << n) | (int)((uint)x >> (32 - n));
}
/*
* F, G, H and I are the basic MD5 functions.
*/
private int F(int u, int v, int w)
{
return (u & v) | (~u & w);
}
private int G(int u, int v, int w)
{
return (u & w) | (v & ~w);
}
private int H(int u, int v, int w)
{
return u ^ v ^ w;
}
private int K(int u, int v, int w)
{
return v ^ (u | ~w);
}
private void ProcessBlock()
{
int a = H1;
int b = H2;
int c = H3;
int d = H4;
//
// Round 1 - F cycle, 16 times.
//
a = RotateLeft((a + F(b, c, d) + this._hashValue[0] + unchecked((int)0xd76aa478)), S11) + b;
d = RotateLeft((d + F(a, b, c) + this._hashValue[1] + unchecked((int)0xe8c7b756)), S12) + a;
c = RotateLeft((c + F(d, a, b) + this._hashValue[2] + unchecked((int)0x242070db)), S13) + d;
b = RotateLeft((b + F(c, d, a) + this._hashValue[3] + unchecked((int)0xc1bdceee)), S14) + c;
a = RotateLeft((a + F(b, c, d) + this._hashValue[4] + unchecked((int)0xf57c0faf)), S11) + b;
d = RotateLeft((d + F(a, b, c) + this._hashValue[5] + unchecked((int)0x4787c62a)), S12) + a;
c = RotateLeft((c + F(d, a, b) + this._hashValue[6] + unchecked((int)0xa8304613)), S13) + d;
b = RotateLeft((b + F(c, d, a) + this._hashValue[7] + unchecked((int)0xfd469501)), S14) + c;
a = RotateLeft((a + F(b, c, d) + this._hashValue[8] + unchecked((int)0x698098d8)), S11) + b;
d = RotateLeft((d + F(a, b, c) + this._hashValue[9] + unchecked((int)0x8b44f7af)), S12) + a;
c = RotateLeft((c + F(d, a, b) + this._hashValue[10] + unchecked((int)0xffff5bb1)), S13) + d;
b = RotateLeft((b + F(c, d, a) + this._hashValue[11] + unchecked((int)0x895cd7be)), S14) + c;
a = RotateLeft((a + F(b, c, d) + this._hashValue[12] + unchecked((int)0x6b901122)), S11) + b;
d = RotateLeft((d + F(a, b, c) + this._hashValue[13] + unchecked((int)0xfd987193)), S12) + a;
c = RotateLeft((c + F(d, a, b) + this._hashValue[14] + unchecked((int)0xa679438e)), S13) + d;
b = RotateLeft((b + F(c, d, a) + this._hashValue[15] + unchecked((int)0x49b40821)), S14) + c;
//
// Round 2 - G cycle, 16 times.
//
a = RotateLeft((a + G(b, c, d) + this._hashValue[1] + unchecked((int)0xf61e2562)), S21) + b;
d = RotateLeft((d + G(a, b, c) + this._hashValue[6] + unchecked((int)0xc040b340)), S22) + a;
c = RotateLeft((c + G(d, a, b) + this._hashValue[11] + unchecked((int)0x265e5a51)), S23) + d;
b = RotateLeft((b + G(c, d, a) + this._hashValue[0] + unchecked((int)0xe9b6c7aa)), S24) + c;
a = RotateLeft((a + G(b, c, d) + this._hashValue[5] + unchecked((int)0xd62f105d)), S21) + b;
d = RotateLeft((d + G(a, b, c) + this._hashValue[10] + unchecked((int)0x02441453)), S22) + a;
c = RotateLeft((c + G(d, a, b) + this._hashValue[15] + unchecked((int)0xd8a1e681)), S23) + d;
b = RotateLeft((b + G(c, d, a) + this._hashValue[4] + unchecked((int)0xe7d3fbc8)), S24) + c;
a = RotateLeft((a + G(b, c, d) + this._hashValue[9] + unchecked((int)0x21e1cde6)), S21) + b;
d = RotateLeft((d + G(a, b, c) + this._hashValue[14] + unchecked((int)0xc33707d6)), S22) + a;
c = RotateLeft((c + G(d, a, b) + this._hashValue[3] + unchecked((int)0xf4d50d87)), S23) + d;
b = RotateLeft((b + G(c, d, a) + this._hashValue[8] + unchecked((int)0x455a14ed)), S24) + c;
a = RotateLeft((a + G(b, c, d) + this._hashValue[13] + unchecked((int)0xa9e3e905)), S21) + b;
d = RotateLeft((d + G(a, b, c) + this._hashValue[2] + unchecked((int)0xfcefa3f8)), S22) + a;
c = RotateLeft((c + G(d, a, b) + this._hashValue[7] + unchecked((int)0x676f02d9)), S23) + d;
b = RotateLeft((b + G(c, d, a) + this._hashValue[12] + unchecked((int)0x8d2a4c8a)), S24) + c;
//
// Round 3 - H cycle, 16 times.
//
a = RotateLeft((a + H(b, c, d) + this._hashValue[5] + unchecked((int)0xfffa3942)), S31) + b;
d = RotateLeft((d + H(a, b, c) + this._hashValue[8] + unchecked((int)0x8771f681)), S32) + a;
c = RotateLeft((c + H(d, a, b) + this._hashValue[11] + unchecked((int)0x6d9d6122)), S33) + d;
b = RotateLeft((b + H(c, d, a) + this._hashValue[14] + unchecked((int)0xfde5380c)), S34) + c;
a = RotateLeft((a + H(b, c, d) + this._hashValue[1] + unchecked((int)0xa4beea44)), S31) + b;
d = RotateLeft((d + H(a, b, c) + this._hashValue[4] + unchecked((int)0x4bdecfa9)), S32) + a;
c = RotateLeft((c + H(d, a, b) + this._hashValue[7] + unchecked((int)0xf6bb4b60)), S33) + d;
b = RotateLeft((b + H(c, d, a) + this._hashValue[10] + unchecked((int)0xbebfbc70)), S34) + c;
a = RotateLeft((a + H(b, c, d) + this._hashValue[13] + unchecked((int)0x289b7ec6)), S31) + b;
d = RotateLeft((d + H(a, b, c) + this._hashValue[0] + unchecked((int)0xeaa127fa)), S32) + a;
c = RotateLeft((c + H(d, a, b) + this._hashValue[3] + unchecked((int)0xd4ef3085)), S33) + d;
b = RotateLeft((b + H(c, d, a) + this._hashValue[6] + unchecked((int)0x04881d05)), S34) + c;
a = RotateLeft((a + H(b, c, d) + this._hashValue[9] + unchecked((int)0xd9d4d039)), S31) + b;
d = RotateLeft((d + H(a, b, c) + this._hashValue[12] + unchecked((int)0xe6db99e5)), S32) + a;
c = RotateLeft((c + H(d, a, b) + this._hashValue[15] + unchecked((int)0x1fa27cf8)), S33) + d;
b = RotateLeft((b + H(c, d, a) + this._hashValue[2] + unchecked((int)0xc4ac5665)), S34) + c;
//
// Round 4 - K cycle, 16 times.
//
a = RotateLeft((a + K(b, c, d) + this._hashValue[0] + unchecked((int)0xf4292244)), S41) + b;
d = RotateLeft((d + K(a, b, c) + this._hashValue[7] + unchecked((int)0x432aff97)), S42) + a;
c = RotateLeft((c + K(d, a, b) + this._hashValue[14] + unchecked((int)0xab9423a7)), S43) + d;
b = RotateLeft((b + K(c, d, a) + this._hashValue[5] + unchecked((int)0xfc93a039)), S44) + c;
a = RotateLeft((a + K(b, c, d) + this._hashValue[12] + unchecked((int)0x655b59c3)), S41) + b;
d = RotateLeft((d + K(a, b, c) + this._hashValue[3] + unchecked((int)0x8f0ccc92)), S42) + a;
c = RotateLeft((c + K(d, a, b) + this._hashValue[10] + unchecked((int)0xffeff47d)), S43) + d;
b = RotateLeft((b + K(c, d, a) + this._hashValue[1] + unchecked((int)0x85845dd1)), S44) + c;
a = RotateLeft((a + K(b, c, d) + this._hashValue[8] + unchecked((int)0x6fa87e4f)), S41) + b;
d = RotateLeft((d + K(a, b, c) + this._hashValue[15] + unchecked((int)0xfe2ce6e0)), S42) + a;
c = RotateLeft((c + K(d, a, b) + this._hashValue[6] + unchecked((int)0xa3014314)), S43) + d;
b = RotateLeft((b + K(c, d, a) + this._hashValue[13] + unchecked((int)0x4e0811a1)), S44) + c;
a = RotateLeft((a + K(b, c, d) + this._hashValue[4] + unchecked((int)0xf7537e82)), S41) + b;
d = RotateLeft((d + K(a, b, c) + this._hashValue[11] + unchecked((int)0xbd3af235)), S42) + a;
c = RotateLeft((c + K(d, a, b) + this._hashValue[2] + unchecked((int)0x2ad7d2bb)), S43) + d;
b = RotateLeft((b + K(c, d, a) + this._hashValue[9] + unchecked((int)0xeb86d391)), S44) + c;
H1 += a;
H2 += b;
H3 += c;
H4 += d;
//
// reset the offset and clean out the word buffer.
//
this._offset = 0;
for (int i = 0; i != this._hashValue.Length; i++)
{
this._hashValue[i] = 0;
}
}
}
}
@@ -12,7 +12,7 @@ namespace Renci.SshNet.Security
/// <summary>
/// Instance of initialized hash algorithm that being used
/// </summary>
protected abstract HMAC Hash { get; }
protected abstract KeyedHashAlgorithm Hash { get; }
/// <summary>
/// Initializes algorithm with specified key.
@@ -7,11 +7,14 @@ namespace Renci.SshNet.Security
/// <summary>
/// Represents MD5 implementation of hashing algorithm
/// </summary>
internal class HMacMD5 : HMac
public class HMacMD5 : HMac
{
private HMACMD5 _hash;
private KeyedHashAlgorithm _hash;
protected override HMAC Hash
/// <summary>
/// Instance of initialized hash algorithm that being used
/// </summary>
protected override KeyedHashAlgorithm Hash
{
get { return this._hash; }
}
@@ -30,9 +33,13 @@ namespace Renci.SshNet.Security
/// <param name="key">The hash key.</param>
public override void Init(IEnumerable<byte> key)
{
this._hash = new HMACMD5(key.Take(16).ToArray());
this._hash = new Renci.SshNet.Security.Cryptography.HMAC<Renci.SshNet.Security.Cryptography.MD5>(key.Take(16).ToArray());
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
protected override void Dispose(bool disposing)
{
// Dispose managed ResourceMessages.
@@ -7,11 +7,14 @@ namespace Renci.SshNet.Security
/// <summary>
/// Represents SHA1 implementation of hashing algorithm
/// </summary>
internal class HMacSha1 : HMac
public class HMacSha1 : HMac
{
private HMACSHA1 _hash;
private KeyedHashAlgorithm _hash;
protected override HMAC Hash
/// <summary>
/// Instance of initialized hash algorithm that being used
/// </summary>
protected override KeyedHashAlgorithm Hash
{
get { return this._hash; }
}
@@ -33,6 +36,10 @@ namespace Renci.SshNet.Security
this._hash = new HMACSHA1(key.Take(20).ToArray());
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
protected override void Dispose(bool disposing)
{
// Dispose managed ResourceMessages.