init commit
This commit is contained in:
@@ -0,0 +1,398 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Blake2bDigest : IDigest
|
||||
{
|
||||
private const int ROUNDS = 12;
|
||||
|
||||
private const int BLOCK_LENGTH_BYTES = 128;
|
||||
|
||||
private static readonly ulong[] blake2b_IV = new ulong[8] { 7640891576956012808uL, 13503953896175478587uL, 4354685564936845355uL, 11912009170470909681uL, 5840696475078001361uL, 11170449401992604703uL, 2270897969802886507uL, 6620516959819538809uL };
|
||||
|
||||
private static readonly byte[,] blake2b_sigma = new byte[12, 16]
|
||||
{
|
||||
{
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15
|
||||
},
|
||||
{
|
||||
14, 10, 4, 8, 9, 15, 13, 6, 1, 12,
|
||||
0, 2, 11, 7, 5, 3
|
||||
},
|
||||
{
|
||||
11, 8, 12, 0, 5, 2, 15, 13, 10, 14,
|
||||
3, 6, 7, 1, 9, 4
|
||||
},
|
||||
{
|
||||
7, 9, 3, 1, 13, 12, 11, 14, 2, 6,
|
||||
5, 10, 4, 0, 15, 8
|
||||
},
|
||||
{
|
||||
9, 0, 5, 7, 2, 4, 10, 15, 14, 1,
|
||||
11, 12, 6, 8, 3, 13
|
||||
},
|
||||
{
|
||||
2, 12, 6, 10, 0, 11, 8, 3, 4, 13,
|
||||
7, 5, 15, 14, 1, 9
|
||||
},
|
||||
{
|
||||
12, 5, 1, 15, 14, 13, 4, 10, 0, 7,
|
||||
6, 3, 9, 2, 8, 11
|
||||
},
|
||||
{
|
||||
13, 11, 7, 14, 12, 1, 3, 9, 5, 0,
|
||||
15, 4, 8, 6, 2, 10
|
||||
},
|
||||
{
|
||||
6, 15, 14, 9, 11, 3, 0, 8, 12, 2,
|
||||
13, 7, 1, 4, 10, 5
|
||||
},
|
||||
{
|
||||
10, 2, 8, 4, 7, 6, 1, 5, 15, 11,
|
||||
9, 14, 3, 12, 13, 0
|
||||
},
|
||||
{
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15
|
||||
},
|
||||
{
|
||||
14, 10, 4, 8, 9, 15, 13, 6, 1, 12,
|
||||
0, 2, 11, 7, 5, 3
|
||||
}
|
||||
};
|
||||
|
||||
private int digestLength = 64;
|
||||
|
||||
private int keyLength = 0;
|
||||
|
||||
private byte[] salt = null;
|
||||
|
||||
private byte[] personalization = null;
|
||||
|
||||
private byte[] key = null;
|
||||
|
||||
private byte[] buffer = null;
|
||||
|
||||
private int bufferPos = 0;
|
||||
|
||||
private ulong[] internalState = new ulong[16];
|
||||
|
||||
private ulong[] chainValue = null;
|
||||
|
||||
private ulong t0 = 0uL;
|
||||
|
||||
private ulong t1 = 0uL;
|
||||
|
||||
private ulong f0 = 0uL;
|
||||
|
||||
public virtual string AlgorithmName => "BLAKE2b";
|
||||
|
||||
public Blake2bDigest()
|
||||
: this(512)
|
||||
{
|
||||
}
|
||||
|
||||
public Blake2bDigest(Blake2bDigest digest)
|
||||
{
|
||||
bufferPos = digest.bufferPos;
|
||||
buffer = Arrays.Clone(digest.buffer);
|
||||
keyLength = digest.keyLength;
|
||||
key = Arrays.Clone(digest.key);
|
||||
digestLength = digest.digestLength;
|
||||
chainValue = Arrays.Clone(digest.chainValue);
|
||||
personalization = Arrays.Clone(digest.personalization);
|
||||
salt = Arrays.Clone(digest.salt);
|
||||
t0 = digest.t0;
|
||||
t1 = digest.t1;
|
||||
f0 = digest.f0;
|
||||
}
|
||||
|
||||
public Blake2bDigest(int digestSize)
|
||||
{
|
||||
if (digestSize < 8 || digestSize > 512 || digestSize % 8 != 0)
|
||||
{
|
||||
throw new ArgumentException("BLAKE2b digest bit length must be a multiple of 8 and not greater than 512");
|
||||
}
|
||||
buffer = new byte[128];
|
||||
keyLength = 0;
|
||||
digestLength = digestSize / 8;
|
||||
Init();
|
||||
}
|
||||
|
||||
public Blake2bDigest(byte[] key)
|
||||
{
|
||||
buffer = new byte[128];
|
||||
if (key != null)
|
||||
{
|
||||
this.key = new byte[key.Length];
|
||||
Array.Copy(key, 0, this.key, 0, key.Length);
|
||||
if (key.Length > 64)
|
||||
{
|
||||
throw new ArgumentException("Keys > 64 are not supported");
|
||||
}
|
||||
keyLength = key.Length;
|
||||
Array.Copy(key, 0, buffer, 0, key.Length);
|
||||
bufferPos = 128;
|
||||
}
|
||||
digestLength = 64;
|
||||
Init();
|
||||
}
|
||||
|
||||
public Blake2bDigest(byte[] key, int digestLength, byte[] salt, byte[] personalization)
|
||||
{
|
||||
if (digestLength < 1 || digestLength > 64)
|
||||
{
|
||||
throw new ArgumentException("Invalid digest length (required: 1 - 64)");
|
||||
}
|
||||
this.digestLength = digestLength;
|
||||
buffer = new byte[128];
|
||||
if (salt != null)
|
||||
{
|
||||
if (salt.Length != 16)
|
||||
{
|
||||
throw new ArgumentException("salt length must be exactly 16 bytes");
|
||||
}
|
||||
this.salt = new byte[16];
|
||||
Array.Copy(salt, 0, this.salt, 0, salt.Length);
|
||||
}
|
||||
if (personalization != null)
|
||||
{
|
||||
if (personalization.Length != 16)
|
||||
{
|
||||
throw new ArgumentException("personalization length must be exactly 16 bytes");
|
||||
}
|
||||
this.personalization = new byte[16];
|
||||
Array.Copy(personalization, 0, this.personalization, 0, personalization.Length);
|
||||
}
|
||||
if (key != null)
|
||||
{
|
||||
if (key.Length > 64)
|
||||
{
|
||||
throw new ArgumentException("Keys > 64 are not supported");
|
||||
}
|
||||
this.key = new byte[key.Length];
|
||||
Array.Copy(key, 0, this.key, 0, key.Length);
|
||||
keyLength = key.Length;
|
||||
Array.Copy(key, 0, buffer, 0, key.Length);
|
||||
bufferPos = 128;
|
||||
}
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
if (chainValue == null)
|
||||
{
|
||||
chainValue = new ulong[8];
|
||||
chainValue[0] = blake2b_IV[0] ^ (ulong)(digestLength | (keyLength << 8) | 0x1010000);
|
||||
chainValue[1] = blake2b_IV[1];
|
||||
chainValue[2] = blake2b_IV[2];
|
||||
chainValue[3] = blake2b_IV[3];
|
||||
chainValue[4] = blake2b_IV[4];
|
||||
chainValue[5] = blake2b_IV[5];
|
||||
if (salt != null)
|
||||
{
|
||||
ulong[] array;
|
||||
(array = chainValue)[4] = array[4] ^ Pack.LE_To_UInt64(salt, 0);
|
||||
(array = chainValue)[5] = array[5] ^ Pack.LE_To_UInt64(salt, 8);
|
||||
}
|
||||
chainValue[6] = blake2b_IV[6];
|
||||
chainValue[7] = blake2b_IV[7];
|
||||
if (personalization != null)
|
||||
{
|
||||
ulong[] array;
|
||||
(array = chainValue)[6] = array[6] ^ Pack.LE_To_UInt64(personalization, 0);
|
||||
(array = chainValue)[7] = array[7] ^ Pack.LE_To_UInt64(personalization, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeInternalState()
|
||||
{
|
||||
Array.Copy(chainValue, 0, internalState, 0, chainValue.Length);
|
||||
Array.Copy(blake2b_IV, 0, internalState, chainValue.Length, 4);
|
||||
internalState[12] = t0 ^ blake2b_IV[4];
|
||||
internalState[13] = t1 ^ blake2b_IV[5];
|
||||
internalState[14] = f0 ^ blake2b_IV[6];
|
||||
internalState[15] = blake2b_IV[7];
|
||||
}
|
||||
|
||||
public virtual void Update(byte b)
|
||||
{
|
||||
int num = 0;
|
||||
if (128 - bufferPos == 0)
|
||||
{
|
||||
t0 += 128uL;
|
||||
if (t0 == 0)
|
||||
{
|
||||
t1++;
|
||||
}
|
||||
Compress(buffer, 0);
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
buffer[0] = b;
|
||||
bufferPos = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer[bufferPos] = b;
|
||||
bufferPos++;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void BlockUpdate(byte[] message, int offset, int len)
|
||||
{
|
||||
if (message == null || len == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int num = 0;
|
||||
if (bufferPos != 0)
|
||||
{
|
||||
num = 128 - bufferPos;
|
||||
if (num >= len)
|
||||
{
|
||||
Array.Copy(message, offset, buffer, bufferPos, len);
|
||||
bufferPos += len;
|
||||
return;
|
||||
}
|
||||
Array.Copy(message, offset, buffer, bufferPos, num);
|
||||
t0 += 128uL;
|
||||
if (t0 == 0)
|
||||
{
|
||||
t1++;
|
||||
}
|
||||
Compress(buffer, 0);
|
||||
bufferPos = 0;
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
}
|
||||
int num2 = offset + len - 128;
|
||||
int i;
|
||||
for (i = offset + num; i < num2; i += 128)
|
||||
{
|
||||
t0 += 128uL;
|
||||
if (t0 == 0)
|
||||
{
|
||||
t1++;
|
||||
}
|
||||
Compress(message, i);
|
||||
}
|
||||
Array.Copy(message, i, buffer, 0, offset + len - i);
|
||||
bufferPos += offset + len - i;
|
||||
}
|
||||
|
||||
public virtual int DoFinal(byte[] output, int outOffset)
|
||||
{
|
||||
f0 = ulong.MaxValue;
|
||||
t0 += (ulong)bufferPos;
|
||||
if (bufferPos > 0 && t0 == 0)
|
||||
{
|
||||
t1++;
|
||||
}
|
||||
Compress(buffer, 0);
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
Array.Clear(internalState, 0, internalState.Length);
|
||||
for (int i = 0; i < chainValue.Length && i * 8 < digestLength; i++)
|
||||
{
|
||||
byte[] sourceArray = Pack.UInt64_To_LE(chainValue[i]);
|
||||
if (i * 8 < digestLength - 8)
|
||||
{
|
||||
Array.Copy(sourceArray, 0, output, outOffset + i * 8, 8);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(sourceArray, 0, output, outOffset + i * 8, digestLength - i * 8);
|
||||
}
|
||||
}
|
||||
Array.Clear(chainValue, 0, chainValue.Length);
|
||||
Reset();
|
||||
return digestLength;
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
bufferPos = 0;
|
||||
f0 = 0uL;
|
||||
t0 = 0uL;
|
||||
t1 = 0uL;
|
||||
chainValue = null;
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
if (key != null)
|
||||
{
|
||||
Array.Copy(key, 0, buffer, 0, key.Length);
|
||||
bufferPos = 128;
|
||||
}
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Compress(byte[] message, int messagePos)
|
||||
{
|
||||
InitializeInternalState();
|
||||
ulong[] array = new ulong[16];
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
array[i] = Pack.LE_To_UInt64(message, messagePos + i * 8);
|
||||
}
|
||||
for (int j = 0; j < 12; j++)
|
||||
{
|
||||
G(array[blake2b_sigma[j, 0]], array[blake2b_sigma[j, 1]], 0, 4, 8, 12);
|
||||
G(array[blake2b_sigma[j, 2]], array[blake2b_sigma[j, 3]], 1, 5, 9, 13);
|
||||
G(array[blake2b_sigma[j, 4]], array[blake2b_sigma[j, 5]], 2, 6, 10, 14);
|
||||
G(array[blake2b_sigma[j, 6]], array[blake2b_sigma[j, 7]], 3, 7, 11, 15);
|
||||
G(array[blake2b_sigma[j, 8]], array[blake2b_sigma[j, 9]], 0, 5, 10, 15);
|
||||
G(array[blake2b_sigma[j, 10]], array[blake2b_sigma[j, 11]], 1, 6, 11, 12);
|
||||
G(array[blake2b_sigma[j, 12]], array[blake2b_sigma[j, 13]], 2, 7, 8, 13);
|
||||
G(array[blake2b_sigma[j, 14]], array[blake2b_sigma[j, 15]], 3, 4, 9, 14);
|
||||
}
|
||||
for (int k = 0; k < chainValue.Length; k++)
|
||||
{
|
||||
chainValue[k] = chainValue[k] ^ internalState[k] ^ internalState[k + 8];
|
||||
}
|
||||
}
|
||||
|
||||
private void G(ulong m1, ulong m2, int posA, int posB, int posC, int posD)
|
||||
{
|
||||
internalState[posA] = internalState[posA] + internalState[posB] + m1;
|
||||
internalState[posD] = Rotr64(internalState[posD] ^ internalState[posA], 32);
|
||||
internalState[posC] += internalState[posD];
|
||||
internalState[posB] = Rotr64(internalState[posB] ^ internalState[posC], 24);
|
||||
internalState[posA] = internalState[posA] + internalState[posB] + m2;
|
||||
internalState[posD] = Rotr64(internalState[posD] ^ internalState[posA], 16);
|
||||
internalState[posC] += internalState[posD];
|
||||
internalState[posB] = Rotr64(internalState[posB] ^ internalState[posC], 63);
|
||||
}
|
||||
|
||||
private static ulong Rotr64(ulong x, int rot)
|
||||
{
|
||||
return (x >> rot) | (x << -rot);
|
||||
}
|
||||
|
||||
public virtual int GetDigestSize()
|
||||
{
|
||||
return digestLength;
|
||||
}
|
||||
|
||||
public virtual int GetByteLength()
|
||||
{
|
||||
return 128;
|
||||
}
|
||||
|
||||
public virtual void ClearKey()
|
||||
{
|
||||
if (key != null)
|
||||
{
|
||||
Array.Clear(key, 0, key.Length);
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ClearSalt()
|
||||
{
|
||||
if (salt != null)
|
||||
{
|
||||
Array.Clear(salt, 0, salt.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,385 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Blake2sDigest : IDigest
|
||||
{
|
||||
private const int ROUNDS = 10;
|
||||
|
||||
private const int BLOCK_LENGTH_BYTES = 64;
|
||||
|
||||
private static readonly uint[] blake2s_IV = new uint[8] { 1779033703u, 3144134277u, 1013904242u, 2773480762u, 1359893119u, 2600822924u, 528734635u, 1541459225u };
|
||||
|
||||
private static readonly byte[,] blake2s_sigma = new byte[10, 16]
|
||||
{
|
||||
{
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15
|
||||
},
|
||||
{
|
||||
14, 10, 4, 8, 9, 15, 13, 6, 1, 12,
|
||||
0, 2, 11, 7, 5, 3
|
||||
},
|
||||
{
|
||||
11, 8, 12, 0, 5, 2, 15, 13, 10, 14,
|
||||
3, 6, 7, 1, 9, 4
|
||||
},
|
||||
{
|
||||
7, 9, 3, 1, 13, 12, 11, 14, 2, 6,
|
||||
5, 10, 4, 0, 15, 8
|
||||
},
|
||||
{
|
||||
9, 0, 5, 7, 2, 4, 10, 15, 14, 1,
|
||||
11, 12, 6, 8, 3, 13
|
||||
},
|
||||
{
|
||||
2, 12, 6, 10, 0, 11, 8, 3, 4, 13,
|
||||
7, 5, 15, 14, 1, 9
|
||||
},
|
||||
{
|
||||
12, 5, 1, 15, 14, 13, 4, 10, 0, 7,
|
||||
6, 3, 9, 2, 8, 11
|
||||
},
|
||||
{
|
||||
13, 11, 7, 14, 12, 1, 3, 9, 5, 0,
|
||||
15, 4, 8, 6, 2, 10
|
||||
},
|
||||
{
|
||||
6, 15, 14, 9, 11, 3, 0, 8, 12, 2,
|
||||
13, 7, 1, 4, 10, 5
|
||||
},
|
||||
{
|
||||
10, 2, 8, 4, 7, 6, 1, 5, 15, 11,
|
||||
9, 14, 3, 12, 13, 0
|
||||
}
|
||||
};
|
||||
|
||||
private int digestLength = 32;
|
||||
|
||||
private int keyLength = 0;
|
||||
|
||||
private byte[] salt = null;
|
||||
|
||||
private byte[] personalization = null;
|
||||
|
||||
private byte[] key = null;
|
||||
|
||||
private byte[] buffer = null;
|
||||
|
||||
private int bufferPos = 0;
|
||||
|
||||
private uint[] internalState = new uint[16];
|
||||
|
||||
private uint[] chainValue = null;
|
||||
|
||||
private uint t0 = 0u;
|
||||
|
||||
private uint t1 = 0u;
|
||||
|
||||
private uint f0 = 0u;
|
||||
|
||||
public virtual string AlgorithmName => "BLAKE2s";
|
||||
|
||||
public Blake2sDigest()
|
||||
: this(256)
|
||||
{
|
||||
}
|
||||
|
||||
public Blake2sDigest(Blake2sDigest digest)
|
||||
{
|
||||
bufferPos = digest.bufferPos;
|
||||
buffer = Arrays.Clone(digest.buffer);
|
||||
keyLength = digest.keyLength;
|
||||
key = Arrays.Clone(digest.key);
|
||||
digestLength = digest.digestLength;
|
||||
chainValue = Arrays.Clone(digest.chainValue);
|
||||
personalization = Arrays.Clone(digest.personalization);
|
||||
}
|
||||
|
||||
public Blake2sDigest(int digestBits)
|
||||
{
|
||||
if (digestBits < 8 || digestBits > 256 || digestBits % 8 != 0)
|
||||
{
|
||||
throw new ArgumentException("BLAKE2s digest bit length must be a multiple of 8 and not greater than 256");
|
||||
}
|
||||
buffer = new byte[64];
|
||||
keyLength = 0;
|
||||
digestLength = digestBits / 8;
|
||||
Init();
|
||||
}
|
||||
|
||||
public Blake2sDigest(byte[] key)
|
||||
{
|
||||
buffer = new byte[64];
|
||||
if (key != null)
|
||||
{
|
||||
if (key.Length > 32)
|
||||
{
|
||||
throw new ArgumentException("Keys > 32 are not supported");
|
||||
}
|
||||
this.key = new byte[key.Length];
|
||||
Array.Copy(key, 0, this.key, 0, key.Length);
|
||||
keyLength = key.Length;
|
||||
Array.Copy(key, 0, buffer, 0, key.Length);
|
||||
bufferPos = 64;
|
||||
}
|
||||
digestLength = 32;
|
||||
Init();
|
||||
}
|
||||
|
||||
public Blake2sDigest(byte[] key, int digestBytes, byte[] salt, byte[] personalization)
|
||||
{
|
||||
if (digestBytes < 1 || digestBytes > 32)
|
||||
{
|
||||
throw new ArgumentException("Invalid digest length (required: 1 - 32)");
|
||||
}
|
||||
digestLength = digestBytes;
|
||||
buffer = new byte[64];
|
||||
if (salt != null)
|
||||
{
|
||||
if (salt.Length != 8)
|
||||
{
|
||||
throw new ArgumentException("Salt length must be exactly 8 bytes");
|
||||
}
|
||||
this.salt = new byte[8];
|
||||
Array.Copy(salt, 0, this.salt, 0, salt.Length);
|
||||
}
|
||||
if (personalization != null)
|
||||
{
|
||||
if (personalization.Length != 8)
|
||||
{
|
||||
throw new ArgumentException("Personalization length must be exactly 8 bytes");
|
||||
}
|
||||
this.personalization = new byte[8];
|
||||
Array.Copy(personalization, 0, this.personalization, 0, personalization.Length);
|
||||
}
|
||||
if (key != null)
|
||||
{
|
||||
if (key.Length > 32)
|
||||
{
|
||||
throw new ArgumentException("Keys > 32 bytes are not supported");
|
||||
}
|
||||
this.key = new byte[key.Length];
|
||||
Array.Copy(key, 0, this.key, 0, key.Length);
|
||||
keyLength = key.Length;
|
||||
Array.Copy(key, 0, buffer, 0, key.Length);
|
||||
bufferPos = 64;
|
||||
}
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
if (chainValue == null)
|
||||
{
|
||||
chainValue = new uint[8];
|
||||
chainValue[0] = blake2s_IV[0] ^ (uint)(digestLength | (keyLength << 8) | 0x1010000);
|
||||
chainValue[1] = blake2s_IV[1];
|
||||
chainValue[2] = blake2s_IV[2];
|
||||
chainValue[3] = blake2s_IV[3];
|
||||
chainValue[4] = blake2s_IV[4];
|
||||
chainValue[5] = blake2s_IV[5];
|
||||
if (salt != null)
|
||||
{
|
||||
uint[] array;
|
||||
(array = chainValue)[4] = array[4] ^ Pack.LE_To_UInt32(salt, 0);
|
||||
(array = chainValue)[5] = array[5] ^ Pack.LE_To_UInt32(salt, 4);
|
||||
}
|
||||
chainValue[6] = blake2s_IV[6];
|
||||
chainValue[7] = blake2s_IV[7];
|
||||
if (personalization != null)
|
||||
{
|
||||
uint[] array;
|
||||
(array = chainValue)[6] = array[6] ^ Pack.LE_To_UInt32(personalization, 0);
|
||||
(array = chainValue)[7] = array[7] ^ Pack.LE_To_UInt32(personalization, 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeInternalState()
|
||||
{
|
||||
Array.Copy(chainValue, 0, internalState, 0, chainValue.Length);
|
||||
Array.Copy(blake2s_IV, 0, internalState, chainValue.Length, 4);
|
||||
internalState[12] = t0 ^ blake2s_IV[4];
|
||||
internalState[13] = t1 ^ blake2s_IV[5];
|
||||
internalState[14] = f0 ^ blake2s_IV[6];
|
||||
internalState[15] = blake2s_IV[7];
|
||||
}
|
||||
|
||||
public virtual void Update(byte b)
|
||||
{
|
||||
if (64 - bufferPos == 0)
|
||||
{
|
||||
t0 += 64u;
|
||||
if (t0 == 0)
|
||||
{
|
||||
t1++;
|
||||
}
|
||||
Compress(buffer, 0);
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
buffer[0] = b;
|
||||
bufferPos = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer[bufferPos] = b;
|
||||
bufferPos++;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void BlockUpdate(byte[] message, int offset, int len)
|
||||
{
|
||||
if (message == null || len == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int num = 0;
|
||||
if (bufferPos != 0)
|
||||
{
|
||||
num = 64 - bufferPos;
|
||||
if (num >= len)
|
||||
{
|
||||
Array.Copy(message, offset, buffer, bufferPos, len);
|
||||
bufferPos += len;
|
||||
return;
|
||||
}
|
||||
Array.Copy(message, offset, buffer, bufferPos, num);
|
||||
t0 += 64u;
|
||||
if (t0 == 0)
|
||||
{
|
||||
t1++;
|
||||
}
|
||||
Compress(buffer, 0);
|
||||
bufferPos = 0;
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
}
|
||||
int num2 = offset + len - 64;
|
||||
int i;
|
||||
for (i = offset + num; i < num2; i += 64)
|
||||
{
|
||||
t0 += 64u;
|
||||
if (t0 == 0)
|
||||
{
|
||||
t1++;
|
||||
}
|
||||
Compress(message, i);
|
||||
}
|
||||
Array.Copy(message, i, buffer, 0, offset + len - i);
|
||||
bufferPos += offset + len - i;
|
||||
}
|
||||
|
||||
public virtual int DoFinal(byte[] output, int outOffset)
|
||||
{
|
||||
f0 = uint.MaxValue;
|
||||
t0 += (uint)bufferPos;
|
||||
if (t0 < 0 && bufferPos > 0L - (long)t0)
|
||||
{
|
||||
t1++;
|
||||
}
|
||||
Compress(buffer, 0);
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
Array.Clear(internalState, 0, internalState.Length);
|
||||
for (int i = 0; i < chainValue.Length && i * 4 < digestLength; i++)
|
||||
{
|
||||
byte[] sourceArray = Pack.UInt32_To_LE(chainValue[i]);
|
||||
if (i * 4 < digestLength - 4)
|
||||
{
|
||||
Array.Copy(sourceArray, 0, output, outOffset + i * 4, 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(sourceArray, 0, output, outOffset + i * 4, digestLength - i * 4);
|
||||
}
|
||||
}
|
||||
Array.Clear(chainValue, 0, chainValue.Length);
|
||||
Reset();
|
||||
return digestLength;
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
bufferPos = 0;
|
||||
f0 = 0u;
|
||||
t0 = 0u;
|
||||
t1 = 0u;
|
||||
chainValue = null;
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
if (key != null)
|
||||
{
|
||||
Array.Copy(key, 0, buffer, 0, key.Length);
|
||||
bufferPos = 64;
|
||||
}
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Compress(byte[] message, int messagePos)
|
||||
{
|
||||
InitializeInternalState();
|
||||
uint[] array = new uint[16];
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
array[i] = Pack.LE_To_UInt32(message, messagePos + i * 4);
|
||||
}
|
||||
for (int j = 0; j < 10; j++)
|
||||
{
|
||||
G(array[blake2s_sigma[j, 0]], array[blake2s_sigma[j, 1]], 0, 4, 8, 12);
|
||||
G(array[blake2s_sigma[j, 2]], array[blake2s_sigma[j, 3]], 1, 5, 9, 13);
|
||||
G(array[blake2s_sigma[j, 4]], array[blake2s_sigma[j, 5]], 2, 6, 10, 14);
|
||||
G(array[blake2s_sigma[j, 6]], array[blake2s_sigma[j, 7]], 3, 7, 11, 15);
|
||||
G(array[blake2s_sigma[j, 8]], array[blake2s_sigma[j, 9]], 0, 5, 10, 15);
|
||||
G(array[blake2s_sigma[j, 10]], array[blake2s_sigma[j, 11]], 1, 6, 11, 12);
|
||||
G(array[blake2s_sigma[j, 12]], array[blake2s_sigma[j, 13]], 2, 7, 8, 13);
|
||||
G(array[blake2s_sigma[j, 14]], array[blake2s_sigma[j, 15]], 3, 4, 9, 14);
|
||||
}
|
||||
for (int k = 0; k < chainValue.Length; k++)
|
||||
{
|
||||
chainValue[k] = chainValue[k] ^ internalState[k] ^ internalState[k + 8];
|
||||
}
|
||||
}
|
||||
|
||||
private void G(uint m1, uint m2, int posA, int posB, int posC, int posD)
|
||||
{
|
||||
internalState[posA] = internalState[posA] + internalState[posB] + m1;
|
||||
internalState[posD] = rotr32(internalState[posD] ^ internalState[posA], 16);
|
||||
internalState[posC] += internalState[posD];
|
||||
internalState[posB] = rotr32(internalState[posB] ^ internalState[posC], 12);
|
||||
internalState[posA] = internalState[posA] + internalState[posB] + m2;
|
||||
internalState[posD] = rotr32(internalState[posD] ^ internalState[posA], 8);
|
||||
internalState[posC] += internalState[posD];
|
||||
internalState[posB] = rotr32(internalState[posB] ^ internalState[posC], 7);
|
||||
}
|
||||
|
||||
private uint rotr32(uint x, int rot)
|
||||
{
|
||||
return (x >> rot) | (x << -rot);
|
||||
}
|
||||
|
||||
public virtual int GetDigestSize()
|
||||
{
|
||||
return digestLength;
|
||||
}
|
||||
|
||||
public virtual int GetByteLength()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
public virtual void ClearKey()
|
||||
{
|
||||
if (key != null)
|
||||
{
|
||||
Array.Clear(key, 0, key.Length);
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ClearSalt()
|
||||
{
|
||||
if (salt != null)
|
||||
{
|
||||
Array.Clear(salt, 0, salt.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,624 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Dstu7564Digest : IDigest, IMemoable
|
||||
{
|
||||
private const int NB_512 = 8;
|
||||
|
||||
private const int NB_1024 = 16;
|
||||
|
||||
private const int NR_512 = 10;
|
||||
|
||||
private const int NR_1024 = 14;
|
||||
|
||||
private int hashSize;
|
||||
|
||||
private int blockSize;
|
||||
|
||||
private int columns;
|
||||
|
||||
private int rounds;
|
||||
|
||||
private ulong[] state;
|
||||
|
||||
private ulong[] tempState1;
|
||||
|
||||
private ulong[] tempState2;
|
||||
|
||||
private ulong inputBlocks;
|
||||
|
||||
private int bufOff;
|
||||
|
||||
private byte[] buf;
|
||||
|
||||
private static readonly byte[] S0 = new byte[256]
|
||||
{
|
||||
168, 67, 95, 6, 107, 117, 108, 89, 113, 223,
|
||||
135, 149, 23, 240, 216, 9, 109, 243, 29, 203,
|
||||
201, 77, 44, 175, 121, 224, 151, 253, 111, 75,
|
||||
69, 57, 62, 221, 163, 79, 180, 182, 154, 14,
|
||||
31, 191, 21, 225, 73, 210, 147, 198, 146, 114,
|
||||
158, 97, 209, 99, 250, 238, 244, 25, 213, 173,
|
||||
88, 164, 187, 161, 220, 242, 131, 55, 66, 228,
|
||||
122, 50, 156, 204, 171, 74, 143, 110, 4, 39,
|
||||
46, 231, 226, 90, 150, 22, 35, 43, 194, 101,
|
||||
102, 15, 188, 169, 71, 65, 52, 72, 252, 183,
|
||||
106, 136, 165, 83, 134, 249, 91, 219, 56, 123,
|
||||
195, 30, 34, 51, 36, 40, 54, 199, 178, 59,
|
||||
142, 119, 186, 245, 20, 159, 8, 85, 155, 76,
|
||||
254, 96, 92, 218, 24, 70, 205, 125, 33, 176,
|
||||
63, 27, 137, 255, 235, 132, 105, 58, 157, 215,
|
||||
211, 112, 103, 64, 181, 222, 93, 48, 145, 177,
|
||||
120, 17, 1, 229, 0, 104, 152, 160, 197, 2,
|
||||
166, 116, 45, 11, 162, 118, 179, 190, 206, 189,
|
||||
174, 233, 138, 49, 28, 236, 241, 153, 148, 170,
|
||||
246, 38, 47, 239, 232, 140, 53, 3, 212, 127,
|
||||
251, 5, 193, 94, 144, 32, 61, 130, 247, 234,
|
||||
10, 13, 126, 248, 80, 26, 196, 7, 87, 184,
|
||||
60, 98, 227, 200, 172, 82, 100, 16, 208, 217,
|
||||
19, 12, 18, 41, 81, 185, 207, 214, 115, 141,
|
||||
129, 84, 192, 237, 78, 68, 167, 42, 133, 37,
|
||||
230, 202, 124, 139, 86, 128
|
||||
};
|
||||
|
||||
private static readonly byte[] S1 = new byte[256]
|
||||
{
|
||||
206, 187, 235, 146, 234, 203, 19, 193, 233, 58,
|
||||
214, 178, 210, 144, 23, 248, 66, 21, 86, 180,
|
||||
101, 28, 136, 67, 197, 92, 54, 186, 245, 87,
|
||||
103, 141, 49, 246, 100, 88, 158, 244, 34, 170,
|
||||
117, 15, 2, 177, 223, 109, 115, 77, 124, 38,
|
||||
46, 247, 8, 93, 68, 62, 159, 20, 200, 174,
|
||||
84, 16, 216, 188, 26, 107, 105, 243, 189, 51,
|
||||
171, 250, 209, 155, 104, 78, 22, 149, 145, 238,
|
||||
76, 99, 142, 91, 204, 60, 25, 161, 129, 73,
|
||||
123, 217, 111, 55, 96, 202, 231, 43, 72, 253,
|
||||
150, 69, 252, 65, 18, 13, 121, 229, 137, 140,
|
||||
227, 32, 48, 220, 183, 108, 74, 181, 63, 151,
|
||||
212, 98, 45, 6, 164, 165, 131, 95, 42, 218,
|
||||
201, 0, 126, 162, 85, 191, 17, 213, 156, 207,
|
||||
14, 10, 61, 81, 125, 147, 27, 254, 196, 71,
|
||||
9, 134, 11, 143, 157, 106, 7, 185, 176, 152,
|
||||
24, 50, 113, 75, 239, 59, 112, 160, 228, 64,
|
||||
255, 195, 169, 230, 120, 249, 139, 70, 128, 30,
|
||||
56, 225, 184, 168, 224, 12, 35, 118, 29, 37,
|
||||
36, 5, 241, 110, 148, 40, 154, 132, 232, 163,
|
||||
79, 119, 211, 133, 226, 82, 242, 130, 80, 122,
|
||||
47, 116, 83, 179, 97, 175, 57, 53, 222, 205,
|
||||
31, 153, 172, 173, 114, 44, 221, 208, 135, 190,
|
||||
94, 166, 236, 4, 198, 3, 52, 251, 219, 89,
|
||||
182, 194, 1, 240, 90, 237, 167, 102, 33, 127,
|
||||
138, 39, 199, 192, 41, 215
|
||||
};
|
||||
|
||||
private static readonly byte[] S2 = new byte[256]
|
||||
{
|
||||
147, 217, 154, 181, 152, 34, 69, 252, 186, 106,
|
||||
223, 2, 159, 220, 81, 89, 74, 23, 43, 194,
|
||||
148, 244, 187, 163, 98, 228, 113, 212, 205, 112,
|
||||
22, 225, 73, 60, 192, 216, 92, 155, 173, 133,
|
||||
83, 161, 122, 200, 45, 224, 209, 114, 166, 44,
|
||||
196, 227, 118, 120, 183, 180, 9, 59, 14, 65,
|
||||
76, 222, 178, 144, 37, 165, 215, 3, 17, 0,
|
||||
195, 46, 146, 239, 78, 18, 157, 125, 203, 53,
|
||||
16, 213, 79, 158, 77, 169, 85, 198, 208, 123,
|
||||
24, 151, 211, 54, 230, 72, 86, 129, 143, 119,
|
||||
204, 156, 185, 226, 172, 184, 47, 21, 164, 124,
|
||||
218, 56, 30, 11, 5, 214, 20, 110, 108, 126,
|
||||
102, 253, 177, 229, 96, 175, 94, 51, 135, 201,
|
||||
240, 93, 109, 63, 136, 141, 199, 247, 29, 233,
|
||||
236, 237, 128, 41, 39, 207, 153, 168, 80, 15,
|
||||
55, 36, 40, 48, 149, 210, 62, 91, 64, 131,
|
||||
179, 105, 87, 31, 7, 28, 138, 188, 32, 235,
|
||||
206, 142, 171, 238, 49, 162, 115, 249, 202, 58,
|
||||
26, 251, 13, 193, 254, 250, 242, 111, 189, 150,
|
||||
221, 67, 82, 182, 8, 243, 174, 190, 25, 137,
|
||||
50, 38, 176, 234, 75, 100, 132, 130, 107, 245,
|
||||
121, 191, 1, 95, 117, 99, 27, 35, 61, 104,
|
||||
42, 101, 232, 145, 246, 255, 19, 88, 241, 71,
|
||||
10, 127, 197, 167, 231, 97, 90, 6, 70, 68,
|
||||
66, 4, 160, 219, 57, 134, 84, 170, 140, 52,
|
||||
33, 139, 248, 12, 116, 103
|
||||
};
|
||||
|
||||
private static readonly byte[] S3 = new byte[256]
|
||||
{
|
||||
104, 141, 202, 77, 115, 75, 78, 42, 212, 82,
|
||||
38, 179, 84, 30, 25, 31, 34, 3, 70, 61,
|
||||
45, 74, 83, 131, 19, 138, 183, 213, 37, 121,
|
||||
245, 189, 88, 47, 13, 2, 237, 81, 158, 17,
|
||||
242, 62, 85, 94, 209, 22, 60, 102, 112, 93,
|
||||
243, 69, 64, 204, 232, 148, 86, 8, 206, 26,
|
||||
58, 210, 225, 223, 181, 56, 110, 14, 229, 244,
|
||||
249, 134, 233, 79, 214, 133, 35, 207, 50, 153,
|
||||
49, 20, 174, 238, 200, 72, 211, 48, 161, 146,
|
||||
65, 177, 24, 196, 44, 113, 114, 68, 21, 253,
|
||||
55, 190, 95, 170, 155, 136, 216, 171, 137, 156,
|
||||
250, 96, 234, 188, 98, 12, 36, 166, 168, 236,
|
||||
103, 32, 219, 124, 40, 221, 172, 91, 52, 126,
|
||||
16, 241, 123, 143, 99, 160, 5, 154, 67, 119,
|
||||
33, 191, 39, 9, 195, 159, 182, 215, 41, 194,
|
||||
235, 192, 164, 139, 140, 29, 251, 255, 193, 178,
|
||||
151, 46, 248, 101, 246, 117, 7, 4, 73, 51,
|
||||
228, 217, 185, 208, 66, 199, 108, 144, 0, 142,
|
||||
111, 80, 1, 197, 218, 71, 63, 205, 105, 162,
|
||||
226, 122, 167, 198, 147, 15, 10, 6, 230, 43,
|
||||
150, 163, 28, 175, 106, 18, 132, 57, 231, 176,
|
||||
130, 247, 254, 157, 135, 92, 129, 53, 222, 180,
|
||||
165, 252, 128, 239, 203, 187, 107, 118, 186, 90,
|
||||
125, 120, 11, 149, 227, 173, 116, 152, 59, 54,
|
||||
100, 109, 220, 240, 89, 169, 76, 23, 127, 145,
|
||||
184, 201, 87, 27, 224, 97
|
||||
};
|
||||
|
||||
public virtual string AlgorithmName => "DSTU7564";
|
||||
|
||||
public Dstu7564Digest(Dstu7564Digest digest)
|
||||
{
|
||||
CopyIn(digest);
|
||||
}
|
||||
|
||||
private void CopyIn(Dstu7564Digest digest)
|
||||
{
|
||||
hashSize = digest.hashSize;
|
||||
blockSize = digest.blockSize;
|
||||
rounds = digest.rounds;
|
||||
if (columns > 0 && columns == digest.columns)
|
||||
{
|
||||
Array.Copy(digest.state, 0, state, 0, columns);
|
||||
Array.Copy(digest.buf, 0, buf, 0, blockSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
columns = digest.columns;
|
||||
state = Arrays.Clone(digest.state);
|
||||
tempState1 = new ulong[columns];
|
||||
tempState2 = new ulong[columns];
|
||||
buf = Arrays.Clone(digest.buf);
|
||||
}
|
||||
inputBlocks = digest.inputBlocks;
|
||||
bufOff = digest.bufOff;
|
||||
}
|
||||
|
||||
public Dstu7564Digest(int hashSizeBits)
|
||||
{
|
||||
if (hashSizeBits == 256 || hashSizeBits == 384 || hashSizeBits == 512)
|
||||
{
|
||||
hashSize = hashSizeBits / 8;
|
||||
if (hashSizeBits > 256)
|
||||
{
|
||||
columns = 16;
|
||||
rounds = 14;
|
||||
}
|
||||
else
|
||||
{
|
||||
columns = 8;
|
||||
rounds = 10;
|
||||
}
|
||||
blockSize = columns << 3;
|
||||
state = new ulong[columns];
|
||||
state[0] = (ulong)blockSize;
|
||||
tempState1 = new ulong[columns];
|
||||
tempState2 = new ulong[columns];
|
||||
buf = new byte[blockSize];
|
||||
return;
|
||||
}
|
||||
throw new ArgumentException("Hash size is not recommended. Use 256/384/512 instead");
|
||||
}
|
||||
|
||||
public virtual int GetDigestSize()
|
||||
{
|
||||
return hashSize;
|
||||
}
|
||||
|
||||
public virtual int GetByteLength()
|
||||
{
|
||||
return blockSize;
|
||||
}
|
||||
|
||||
public virtual void Update(byte input)
|
||||
{
|
||||
buf[bufOff++] = input;
|
||||
if (bufOff == blockSize)
|
||||
{
|
||||
ProcessBlock(buf, 0);
|
||||
bufOff = 0;
|
||||
inputBlocks++;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void BlockUpdate(byte[] input, int inOff, int length)
|
||||
{
|
||||
while (bufOff != 0 && length > 0)
|
||||
{
|
||||
Update(input[inOff++]);
|
||||
length--;
|
||||
}
|
||||
if (length > 0)
|
||||
{
|
||||
while (length >= blockSize)
|
||||
{
|
||||
ProcessBlock(input, inOff);
|
||||
inOff += blockSize;
|
||||
length -= blockSize;
|
||||
inputBlocks++;
|
||||
}
|
||||
while (length > 0)
|
||||
{
|
||||
Update(input[inOff++]);
|
||||
length--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
int num = bufOff;
|
||||
buf[bufOff++] = 128;
|
||||
int num2 = blockSize - 12;
|
||||
if (bufOff > num2)
|
||||
{
|
||||
while (bufOff < blockSize)
|
||||
{
|
||||
buf[bufOff++] = 0;
|
||||
}
|
||||
bufOff = 0;
|
||||
ProcessBlock(buf, 0);
|
||||
}
|
||||
while (bufOff < num2)
|
||||
{
|
||||
buf[bufOff++] = 0;
|
||||
}
|
||||
ulong num3 = (ulong)((long)(inputBlocks & 0xFFFFFFFFu) * (long)blockSize + (uint)num << 3);
|
||||
Pack.UInt32_To_LE((uint)num3, buf, bufOff);
|
||||
bufOff += 4;
|
||||
num3 >>= 32;
|
||||
num3 += (ulong)((long)(inputBlocks >> 32) * (long)blockSize << 3);
|
||||
Pack.UInt64_To_LE(num3, buf, bufOff);
|
||||
ProcessBlock(buf, 0);
|
||||
Array.Copy(state, 0, tempState1, 0, columns);
|
||||
P(tempState1);
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
ulong[] array2;
|
||||
ulong[] array = (array2 = state);
|
||||
int num4 = i;
|
||||
nint num5 = num4;
|
||||
array[num4] = array2[num5] ^ tempState1[i];
|
||||
}
|
||||
int num6 = hashSize / 8;
|
||||
for (int j = columns - num6; j < columns; j++)
|
||||
{
|
||||
Pack.UInt64_To_LE(state[j], output, outOff);
|
||||
outOff += 8;
|
||||
}
|
||||
Reset();
|
||||
return hashSize;
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
Array.Clear(state, 0, state.Length);
|
||||
state[0] = (ulong)blockSize;
|
||||
inputBlocks = 0uL;
|
||||
bufOff = 0;
|
||||
}
|
||||
|
||||
protected virtual void ProcessBlock(byte[] input, int inOff)
|
||||
{
|
||||
int num = inOff;
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
ulong num2 = Pack.LE_To_UInt64(input, num);
|
||||
num += 8;
|
||||
tempState1[i] = state[i] ^ num2;
|
||||
tempState2[i] = num2;
|
||||
}
|
||||
P(tempState1);
|
||||
Q(tempState2);
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
ulong[] array2;
|
||||
ulong[] array = (array2 = state);
|
||||
int num3 = j;
|
||||
nint num4 = num3;
|
||||
array[num3] = array2[num4] ^ (tempState1[j] ^ tempState2[j]);
|
||||
}
|
||||
}
|
||||
|
||||
private void P(ulong[] s)
|
||||
{
|
||||
for (int i = 0; i < rounds; i++)
|
||||
{
|
||||
ulong num = (ulong)i;
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
ulong[] array2;
|
||||
ulong[] array = (array2 = s);
|
||||
int num2 = j;
|
||||
nint num3 = num2;
|
||||
array[num2] = array2[num3] ^ num;
|
||||
num += 16;
|
||||
}
|
||||
ShiftRows(s);
|
||||
SubBytes(s);
|
||||
MixColumns(s);
|
||||
}
|
||||
}
|
||||
|
||||
private void Q(ulong[] s)
|
||||
{
|
||||
for (int i = 0; i < rounds; i++)
|
||||
{
|
||||
ulong num = (ulong)(((long)((columns - 1 << 4) ^ i) << 56) | 0xF0F0F0F0F0F0F3L);
|
||||
for (int j = 0; j < columns; j++)
|
||||
{
|
||||
ulong[] array2;
|
||||
ulong[] array = (array2 = s);
|
||||
int num2 = j;
|
||||
nint num3 = num2;
|
||||
array[num2] = array2[num3] + num;
|
||||
num -= 1152921504606846976L;
|
||||
}
|
||||
ShiftRows(s);
|
||||
SubBytes(s);
|
||||
MixColumns(s);
|
||||
}
|
||||
}
|
||||
|
||||
private static ulong MixColumn(ulong c)
|
||||
{
|
||||
ulong num = ((c & 0x7F7F7F7F7F7F7F7FL) << 1) ^ (((c & 0x8080808080808080uL) >> 7) * 29);
|
||||
ulong num2 = Rotate(8, c) ^ c;
|
||||
num2 ^= Rotate(16, num2);
|
||||
num2 ^= Rotate(48, c);
|
||||
ulong num3 = num2 ^ c ^ num;
|
||||
num3 = ((num3 & 0x3F3F3F3F3F3F3F3FL) << 2) ^ (((num3 & 0x8080808080808080uL) >> 6) * 29) ^ (((num3 & 0x4040404040404040L) >> 6) * 29);
|
||||
return num2 ^ Rotate(32, num3) ^ Rotate(40, num) ^ Rotate(48, num);
|
||||
}
|
||||
|
||||
private void MixColumns(ulong[] s)
|
||||
{
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
s[i] = MixColumn(s[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private static ulong Rotate(int n, ulong x)
|
||||
{
|
||||
return (x >> n) | (x << -n);
|
||||
}
|
||||
|
||||
private void ShiftRows(ulong[] s)
|
||||
{
|
||||
switch (columns)
|
||||
{
|
||||
case 8:
|
||||
{
|
||||
ulong num18 = s[0];
|
||||
ulong num19 = s[1];
|
||||
ulong num20 = s[2];
|
||||
ulong num21 = s[3];
|
||||
ulong num22 = s[4];
|
||||
ulong num23 = s[5];
|
||||
ulong num24 = s[6];
|
||||
ulong num25 = s[7];
|
||||
ulong num26 = (num18 ^ num22) & 0xFFFFFFFF00000000uL;
|
||||
num18 ^= num26;
|
||||
num22 ^= num26;
|
||||
num26 = (num19 ^ num23) & 0xFFFFFFFF000000L;
|
||||
num19 ^= num26;
|
||||
num23 ^= num26;
|
||||
num26 = (num20 ^ num24) & 0xFFFFFFFF0000L;
|
||||
num20 ^= num26;
|
||||
num24 ^= num26;
|
||||
num26 = (num21 ^ num25) & 0xFFFFFFFF00L;
|
||||
num21 ^= num26;
|
||||
num25 ^= num26;
|
||||
num26 = (num18 ^ num20) & 0xFFFF0000FFFF0000uL;
|
||||
num18 ^= num26;
|
||||
num20 ^= num26;
|
||||
num26 = (num19 ^ num21) & 0xFFFF0000FFFF00L;
|
||||
num19 ^= num26;
|
||||
num21 ^= num26;
|
||||
num26 = (num22 ^ num24) & 0xFFFF0000FFFF0000uL;
|
||||
num22 ^= num26;
|
||||
num24 ^= num26;
|
||||
num26 = (num23 ^ num25) & 0xFFFF0000FFFF00L;
|
||||
num23 ^= num26;
|
||||
num25 ^= num26;
|
||||
num26 = (num18 ^ num19) & 0xFF00FF00FF00FF00uL;
|
||||
num18 ^= num26;
|
||||
num19 ^= num26;
|
||||
num26 = (num20 ^ num21) & 0xFF00FF00FF00FF00uL;
|
||||
num20 ^= num26;
|
||||
num21 ^= num26;
|
||||
num26 = (num22 ^ num23) & 0xFF00FF00FF00FF00uL;
|
||||
num22 ^= num26;
|
||||
num23 ^= num26;
|
||||
num26 = (num24 ^ num25) & 0xFF00FF00FF00FF00uL;
|
||||
num24 ^= num26;
|
||||
num25 ^= num26;
|
||||
s[0] = num18;
|
||||
s[1] = num19;
|
||||
s[2] = num20;
|
||||
s[3] = num21;
|
||||
s[4] = num22;
|
||||
s[5] = num23;
|
||||
s[6] = num24;
|
||||
s[7] = num25;
|
||||
break;
|
||||
}
|
||||
case 16:
|
||||
{
|
||||
ulong num = s[0];
|
||||
ulong num2 = s[1];
|
||||
ulong num3 = s[2];
|
||||
ulong num4 = s[3];
|
||||
ulong num5 = s[4];
|
||||
ulong num6 = s[5];
|
||||
ulong num7 = s[6];
|
||||
ulong num8 = s[7];
|
||||
ulong num9 = s[8];
|
||||
ulong num10 = s[9];
|
||||
ulong num11 = s[10];
|
||||
ulong num12 = s[11];
|
||||
ulong num13 = s[12];
|
||||
ulong num14 = s[13];
|
||||
ulong num15 = s[14];
|
||||
ulong num16 = s[15];
|
||||
ulong num17 = (num ^ num9) & 0xFF00000000000000uL;
|
||||
num ^= num17;
|
||||
num9 ^= num17;
|
||||
num17 = (num2 ^ num10) & 0xFF00000000000000uL;
|
||||
num2 ^= num17;
|
||||
num10 ^= num17;
|
||||
num17 = (num3 ^ num11) & 0xFFFF000000000000uL;
|
||||
num3 ^= num17;
|
||||
num11 ^= num17;
|
||||
num17 = (num4 ^ num12) & 0xFFFFFF0000000000uL;
|
||||
num4 ^= num17;
|
||||
num12 ^= num17;
|
||||
num17 = (num5 ^ num13) & 0xFFFFFFFF00000000uL;
|
||||
num5 ^= num17;
|
||||
num13 ^= num17;
|
||||
num17 = (num6 ^ num14) & 0xFFFFFFFF000000L;
|
||||
num6 ^= num17;
|
||||
num14 ^= num17;
|
||||
num17 = (num7 ^ num15) & 0xFFFFFFFFFF0000L;
|
||||
num7 ^= num17;
|
||||
num15 ^= num17;
|
||||
num17 = (num8 ^ num16) & 0xFFFFFFFFFFFF00L;
|
||||
num8 ^= num17;
|
||||
num16 ^= num17;
|
||||
num17 = (num ^ num5) & 0xFFFFFF00000000L;
|
||||
num ^= num17;
|
||||
num5 ^= num17;
|
||||
num17 = (num2 ^ num6) & 0xFFFFFFFFFF000000uL;
|
||||
num2 ^= num17;
|
||||
num6 ^= num17;
|
||||
num17 = (num3 ^ num7) & 0xFF00FFFFFFFF0000uL;
|
||||
num3 ^= num17;
|
||||
num7 ^= num17;
|
||||
num17 = (num4 ^ num8) & 0xFF0000FFFFFFFF00uL;
|
||||
num4 ^= num17;
|
||||
num8 ^= num17;
|
||||
num17 = (num9 ^ num13) & 0xFFFFFF00000000L;
|
||||
num9 ^= num17;
|
||||
num13 ^= num17;
|
||||
num17 = (num10 ^ num14) & 0xFFFFFFFFFF000000uL;
|
||||
num10 ^= num17;
|
||||
num14 ^= num17;
|
||||
num17 = (num11 ^ num15) & 0xFF00FFFFFFFF0000uL;
|
||||
num11 ^= num17;
|
||||
num15 ^= num17;
|
||||
num17 = (num12 ^ num16) & 0xFF0000FFFFFFFF00uL;
|
||||
num12 ^= num17;
|
||||
num16 ^= num17;
|
||||
num17 = (num ^ num3) & 0xFFFF0000FFFF0000uL;
|
||||
num ^= num17;
|
||||
num3 ^= num17;
|
||||
num17 = (num2 ^ num4) & 0xFFFF0000FFFF00L;
|
||||
num2 ^= num17;
|
||||
num4 ^= num17;
|
||||
num17 = (num5 ^ num7) & 0xFFFF0000FFFF0000uL;
|
||||
num5 ^= num17;
|
||||
num7 ^= num17;
|
||||
num17 = (num6 ^ num8) & 0xFFFF0000FFFF00L;
|
||||
num6 ^= num17;
|
||||
num8 ^= num17;
|
||||
num17 = (num9 ^ num11) & 0xFFFF0000FFFF0000uL;
|
||||
num9 ^= num17;
|
||||
num11 ^= num17;
|
||||
num17 = (num10 ^ num12) & 0xFFFF0000FFFF00L;
|
||||
num10 ^= num17;
|
||||
num12 ^= num17;
|
||||
num17 = (num13 ^ num15) & 0xFFFF0000FFFF0000uL;
|
||||
num13 ^= num17;
|
||||
num15 ^= num17;
|
||||
num17 = (num14 ^ num16) & 0xFFFF0000FFFF00L;
|
||||
num14 ^= num17;
|
||||
num16 ^= num17;
|
||||
num17 = (num ^ num2) & 0xFF00FF00FF00FF00uL;
|
||||
num ^= num17;
|
||||
num2 ^= num17;
|
||||
num17 = (num3 ^ num4) & 0xFF00FF00FF00FF00uL;
|
||||
num3 ^= num17;
|
||||
num4 ^= num17;
|
||||
num17 = (num5 ^ num6) & 0xFF00FF00FF00FF00uL;
|
||||
num5 ^= num17;
|
||||
num6 ^= num17;
|
||||
num17 = (num7 ^ num8) & 0xFF00FF00FF00FF00uL;
|
||||
num7 ^= num17;
|
||||
num8 ^= num17;
|
||||
num17 = (num9 ^ num10) & 0xFF00FF00FF00FF00uL;
|
||||
num9 ^= num17;
|
||||
num10 ^= num17;
|
||||
num17 = (num11 ^ num12) & 0xFF00FF00FF00FF00uL;
|
||||
num11 ^= num17;
|
||||
num12 ^= num17;
|
||||
num17 = (num13 ^ num14) & 0xFF00FF00FF00FF00uL;
|
||||
num13 ^= num17;
|
||||
num14 ^= num17;
|
||||
num17 = (num15 ^ num16) & 0xFF00FF00FF00FF00uL;
|
||||
num15 ^= num17;
|
||||
num16 ^= num17;
|
||||
s[0] = num;
|
||||
s[1] = num2;
|
||||
s[2] = num3;
|
||||
s[3] = num4;
|
||||
s[4] = num5;
|
||||
s[5] = num6;
|
||||
s[6] = num7;
|
||||
s[7] = num8;
|
||||
s[8] = num9;
|
||||
s[9] = num10;
|
||||
s[10] = num11;
|
||||
s[11] = num12;
|
||||
s[12] = num13;
|
||||
s[13] = num14;
|
||||
s[14] = num15;
|
||||
s[15] = num16;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new InvalidOperationException("unsupported state size: only 512/1024 are allowed");
|
||||
}
|
||||
}
|
||||
|
||||
private void SubBytes(ulong[] s)
|
||||
{
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
ulong num = s[i];
|
||||
uint num2 = (uint)num;
|
||||
uint num3 = (uint)(num >> 32);
|
||||
byte b = S0[num2 & 0xFF];
|
||||
byte b2 = S1[(num2 >> 8) & 0xFF];
|
||||
byte b3 = S2[(num2 >> 16) & 0xFF];
|
||||
byte b4 = S3[num2 >> 24];
|
||||
num2 = (uint)(b | (b2 << 8) | (b3 << 16) | (b4 << 24));
|
||||
byte b5 = S0[num3 & 0xFF];
|
||||
byte b6 = S1[(num3 >> 8) & 0xFF];
|
||||
byte b7 = S2[(num3 >> 16) & 0xFF];
|
||||
byte b8 = S3[num3 >> 24];
|
||||
num3 = (uint)(b5 | (b6 << 8) | (b7 << 16) | (b8 << 24));
|
||||
s[i] = num2 | ((ulong)num3 << 32);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual IMemoable Copy()
|
||||
{
|
||||
return new Dstu7564Digest(this);
|
||||
}
|
||||
|
||||
public virtual void Reset(IMemoable other)
|
||||
{
|
||||
Dstu7564Digest digest = (Dstu7564Digest)other;
|
||||
CopyIn(digest);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public abstract class GeneralDigest : IDigest, IMemoable
|
||||
{
|
||||
private const int BYTE_LENGTH = 64;
|
||||
|
||||
private byte[] xBuf;
|
||||
|
||||
private int xBufOff;
|
||||
|
||||
private long byteCount;
|
||||
|
||||
public abstract string AlgorithmName { get; }
|
||||
|
||||
internal GeneralDigest()
|
||||
{
|
||||
xBuf = new byte[4];
|
||||
}
|
||||
|
||||
internal GeneralDigest(GeneralDigest t)
|
||||
{
|
||||
xBuf = new byte[t.xBuf.Length];
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
protected void CopyIn(GeneralDigest t)
|
||||
{
|
||||
Array.Copy(t.xBuf, 0, xBuf, 0, t.xBuf.Length);
|
||||
xBufOff = t.xBufOff;
|
||||
byteCount = t.byteCount;
|
||||
}
|
||||
|
||||
public void Update(byte input)
|
||||
{
|
||||
xBuf[xBufOff++] = input;
|
||||
if (xBufOff == xBuf.Length)
|
||||
{
|
||||
ProcessWord(xBuf, 0);
|
||||
xBufOff = 0;
|
||||
}
|
||||
byteCount++;
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] input, int inOff, int length)
|
||||
{
|
||||
length = System.Math.Max(0, length);
|
||||
int i = 0;
|
||||
if (xBufOff != 0)
|
||||
{
|
||||
while (i < length)
|
||||
{
|
||||
xBuf[xBufOff++] = input[inOff + i++];
|
||||
if (xBufOff == 4)
|
||||
{
|
||||
ProcessWord(xBuf, 0);
|
||||
xBufOff = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int num = ((length - i) & -4) + i; i < num; i += 4)
|
||||
{
|
||||
ProcessWord(input, inOff + i);
|
||||
}
|
||||
while (i < length)
|
||||
{
|
||||
xBuf[xBufOff++] = input[inOff + i++];
|
||||
}
|
||||
byteCount += length;
|
||||
}
|
||||
|
||||
public void Finish()
|
||||
{
|
||||
long bitLength = byteCount << 3;
|
||||
Update(128);
|
||||
while (xBufOff != 0)
|
||||
{
|
||||
Update(0);
|
||||
}
|
||||
ProcessLength(bitLength);
|
||||
ProcessBlock();
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
byteCount = 0L;
|
||||
xBufOff = 0;
|
||||
Array.Clear(xBuf, 0, xBuf.Length);
|
||||
}
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
internal abstract void ProcessWord(byte[] input, int inOff);
|
||||
|
||||
internal abstract void ProcessLength(long bitLength);
|
||||
|
||||
internal abstract void ProcessBlock();
|
||||
|
||||
public abstract int GetDigestSize();
|
||||
|
||||
public abstract int DoFinal(byte[] output, int outOff);
|
||||
|
||||
public abstract IMemoable Copy();
|
||||
|
||||
public abstract void Reset(IMemoable t);
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Gost3411Digest : IDigest, IMemoable
|
||||
{
|
||||
private const int DIGEST_LENGTH = 32;
|
||||
|
||||
private byte[] H = new byte[32];
|
||||
|
||||
private byte[] L = new byte[32];
|
||||
|
||||
private byte[] M = new byte[32];
|
||||
|
||||
private byte[] Sum = new byte[32];
|
||||
|
||||
private byte[][] C = MakeC();
|
||||
|
||||
private byte[] xBuf = new byte[32];
|
||||
|
||||
private int xBufOff;
|
||||
|
||||
private ulong byteCount;
|
||||
|
||||
private readonly IBlockCipher cipher = new Gost28147Engine();
|
||||
|
||||
private byte[] sBox;
|
||||
|
||||
private byte[] K = new byte[32];
|
||||
|
||||
private byte[] a = new byte[8];
|
||||
|
||||
internal short[] wS = new short[16];
|
||||
|
||||
internal short[] w_S = new short[16];
|
||||
|
||||
internal byte[] S = new byte[32];
|
||||
|
||||
internal byte[] U = new byte[32];
|
||||
|
||||
internal byte[] V = new byte[32];
|
||||
|
||||
internal byte[] W = new byte[32];
|
||||
|
||||
private static readonly byte[] C2 = new byte[32]
|
||||
{
|
||||
0, 255, 0, 255, 0, 255, 0, 255, 255, 0,
|
||||
255, 0, 255, 0, 255, 0, 0, 255, 255, 0,
|
||||
255, 0, 0, 255, 255, 0, 0, 0, 255, 255,
|
||||
0, 255
|
||||
};
|
||||
|
||||
public string AlgorithmName => "Gost3411";
|
||||
|
||||
private static byte[][] MakeC()
|
||||
{
|
||||
byte[][] array = new byte[4][];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
array[i] = new byte[32];
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Gost3411Digest()
|
||||
{
|
||||
sBox = Gost28147Engine.GetSBox("D-A");
|
||||
cipher.Init(forEncryption: true, new ParametersWithSBox(null, sBox));
|
||||
Reset();
|
||||
}
|
||||
|
||||
public Gost3411Digest(byte[] sBoxParam)
|
||||
{
|
||||
sBox = Arrays.Clone(sBoxParam);
|
||||
cipher.Init(forEncryption: true, new ParametersWithSBox(null, sBox));
|
||||
Reset();
|
||||
}
|
||||
|
||||
public Gost3411Digest(Gost3411Digest t)
|
||||
{
|
||||
Reset(t);
|
||||
}
|
||||
|
||||
public int GetDigestSize()
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
public void Update(byte input)
|
||||
{
|
||||
xBuf[xBufOff++] = input;
|
||||
if (xBufOff == xBuf.Length)
|
||||
{
|
||||
sumByteArray(xBuf);
|
||||
processBlock(xBuf, 0);
|
||||
xBufOff = 0;
|
||||
}
|
||||
byteCount++;
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] input, int inOff, int length)
|
||||
{
|
||||
while (xBufOff != 0 && length > 0)
|
||||
{
|
||||
Update(input[inOff]);
|
||||
inOff++;
|
||||
length--;
|
||||
}
|
||||
while (length > xBuf.Length)
|
||||
{
|
||||
Array.Copy(input, inOff, xBuf, 0, xBuf.Length);
|
||||
sumByteArray(xBuf);
|
||||
processBlock(xBuf, 0);
|
||||
inOff += xBuf.Length;
|
||||
length -= xBuf.Length;
|
||||
byteCount += (uint)xBuf.Length;
|
||||
}
|
||||
while (length > 0)
|
||||
{
|
||||
Update(input[inOff]);
|
||||
inOff++;
|
||||
length--;
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] P(byte[] input)
|
||||
{
|
||||
int num = 0;
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
K[num++] = input[i];
|
||||
K[num++] = input[8 + i];
|
||||
K[num++] = input[16 + i];
|
||||
K[num++] = input[24 + i];
|
||||
}
|
||||
return K;
|
||||
}
|
||||
|
||||
private byte[] A(byte[] input)
|
||||
{
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
a[i] = (byte)(input[i] ^ input[i + 8]);
|
||||
}
|
||||
Array.Copy(input, 8, input, 0, 24);
|
||||
Array.Copy(a, 0, input, 24, 8);
|
||||
return input;
|
||||
}
|
||||
|
||||
private void E(byte[] key, byte[] s, int sOff, byte[] input, int inOff)
|
||||
{
|
||||
cipher.Init(forEncryption: true, new KeyParameter(key));
|
||||
cipher.ProcessBlock(input, inOff, s, sOff);
|
||||
}
|
||||
|
||||
private void fw(byte[] input)
|
||||
{
|
||||
cpyBytesToShort(input, wS);
|
||||
w_S[15] = (short)(wS[0] ^ wS[1] ^ wS[2] ^ wS[3] ^ wS[12] ^ wS[15]);
|
||||
Array.Copy(wS, 1, w_S, 0, 15);
|
||||
cpyShortToBytes(w_S, input);
|
||||
}
|
||||
|
||||
private void processBlock(byte[] input, int inOff)
|
||||
{
|
||||
Array.Copy(input, inOff, M, 0, 32);
|
||||
H.CopyTo(U, 0);
|
||||
M.CopyTo(V, 0);
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
W[i] = (byte)(U[i] ^ V[i]);
|
||||
}
|
||||
E(P(W), S, 0, H, 0);
|
||||
for (int j = 1; j < 4; j++)
|
||||
{
|
||||
byte[] array = A(U);
|
||||
for (int k = 0; k < 32; k++)
|
||||
{
|
||||
U[k] = (byte)(array[k] ^ C[j][k]);
|
||||
}
|
||||
V = A(A(V));
|
||||
for (int l = 0; l < 32; l++)
|
||||
{
|
||||
W[l] = (byte)(U[l] ^ V[l]);
|
||||
}
|
||||
E(P(W), S, j * 8, H, j * 8);
|
||||
}
|
||||
for (int m = 0; m < 12; m++)
|
||||
{
|
||||
fw(S);
|
||||
}
|
||||
for (int n = 0; n < 32; n++)
|
||||
{
|
||||
S[n] ^= M[n];
|
||||
}
|
||||
fw(S);
|
||||
for (int num = 0; num < 32; num++)
|
||||
{
|
||||
S[num] = (byte)(H[num] ^ S[num]);
|
||||
}
|
||||
for (int num2 = 0; num2 < 61; num2++)
|
||||
{
|
||||
fw(S);
|
||||
}
|
||||
Array.Copy(S, 0, H, 0, H.Length);
|
||||
}
|
||||
|
||||
private void finish()
|
||||
{
|
||||
ulong n = byteCount * 8;
|
||||
Pack.UInt64_To_LE(n, L);
|
||||
while (xBufOff != 0)
|
||||
{
|
||||
Update(0);
|
||||
}
|
||||
processBlock(L, 0);
|
||||
processBlock(Sum, 0);
|
||||
}
|
||||
|
||||
public int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
finish();
|
||||
H.CopyTo(output, outOff);
|
||||
Reset();
|
||||
return 32;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
byteCount = 0uL;
|
||||
xBufOff = 0;
|
||||
Array.Clear(H, 0, H.Length);
|
||||
Array.Clear(L, 0, L.Length);
|
||||
Array.Clear(M, 0, M.Length);
|
||||
Array.Clear(C[1], 0, C[1].Length);
|
||||
Array.Clear(C[3], 0, C[3].Length);
|
||||
Array.Clear(Sum, 0, Sum.Length);
|
||||
Array.Clear(xBuf, 0, xBuf.Length);
|
||||
C2.CopyTo(C[2], 0);
|
||||
}
|
||||
|
||||
private void sumByteArray(byte[] input)
|
||||
{
|
||||
int num = 0;
|
||||
for (int i = 0; i != Sum.Length; i++)
|
||||
{
|
||||
int num2 = (Sum[i] & 0xFF) + (input[i] & 0xFF) + num;
|
||||
Sum[i] = (byte)num2;
|
||||
num = num2 >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
private static void cpyBytesToShort(byte[] S, short[] wS)
|
||||
{
|
||||
for (int i = 0; i < S.Length / 2; i++)
|
||||
{
|
||||
wS[i] = (short)(((S[i * 2 + 1] << 8) & 0xFF00) | (S[i * 2] & 0xFF));
|
||||
}
|
||||
}
|
||||
|
||||
private static void cpyShortToBytes(short[] wS, byte[] S)
|
||||
{
|
||||
for (int i = 0; i < S.Length / 2; i++)
|
||||
{
|
||||
S[i * 2 + 1] = (byte)(wS[i] >> 8);
|
||||
S[i * 2] = (byte)wS[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
public IMemoable Copy()
|
||||
{
|
||||
return new Gost3411Digest(this);
|
||||
}
|
||||
|
||||
public void Reset(IMemoable other)
|
||||
{
|
||||
Gost3411Digest gost3411Digest = (Gost3411Digest)other;
|
||||
sBox = gost3411Digest.sBox;
|
||||
cipher.Init(forEncryption: true, new ParametersWithSBox(null, sBox));
|
||||
Reset();
|
||||
Array.Copy(gost3411Digest.H, 0, H, 0, gost3411Digest.H.Length);
|
||||
Array.Copy(gost3411Digest.L, 0, L, 0, gost3411Digest.L.Length);
|
||||
Array.Copy(gost3411Digest.M, 0, M, 0, gost3411Digest.M.Length);
|
||||
Array.Copy(gost3411Digest.Sum, 0, Sum, 0, gost3411Digest.Sum.Length);
|
||||
Array.Copy(gost3411Digest.C[1], 0, C[1], 0, gost3411Digest.C[1].Length);
|
||||
Array.Copy(gost3411Digest.C[2], 0, C[2], 0, gost3411Digest.C[2].Length);
|
||||
Array.Copy(gost3411Digest.C[3], 0, C[3], 0, gost3411Digest.C[3].Length);
|
||||
Array.Copy(gost3411Digest.xBuf, 0, xBuf, 0, gost3411Digest.xBuf.Length);
|
||||
xBufOff = gost3411Digest.xBufOff;
|
||||
byteCount = gost3411Digest.byteCount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,722 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public abstract class Gost3411_2012Digest : IDigest, IMemoable
|
||||
{
|
||||
private readonly byte[] IV = new byte[64];
|
||||
|
||||
private readonly byte[] N = new byte[64];
|
||||
|
||||
private readonly byte[] Sigma = new byte[64];
|
||||
|
||||
private readonly byte[] Ki = new byte[64];
|
||||
|
||||
private readonly byte[] m = new byte[64];
|
||||
|
||||
private readonly byte[] h = new byte[64];
|
||||
|
||||
private readonly byte[] tmp = new byte[64];
|
||||
|
||||
private readonly byte[] block = new byte[64];
|
||||
|
||||
private int bOff = 64;
|
||||
|
||||
private static readonly byte[][] C = new byte[12][]
|
||||
{
|
||||
new byte[64]
|
||||
{
|
||||
177, 8, 91, 218, 30, 202, 218, 233, 235, 203,
|
||||
47, 129, 192, 101, 124, 31, 47, 106, 118, 67,
|
||||
46, 69, 208, 22, 113, 78, 184, 141, 117, 133,
|
||||
196, 252, 75, 124, 224, 145, 146, 103, 105, 1,
|
||||
162, 66, 42, 8, 164, 96, 211, 21, 5, 118,
|
||||
116, 54, 204, 116, 77, 35, 221, 128, 101, 89,
|
||||
242, 166, 69, 7
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
111, 163, 181, 138, 169, 157, 47, 26, 79, 227,
|
||||
157, 70, 15, 112, 181, 215, 243, 254, 234, 114,
|
||||
10, 35, 43, 152, 97, 213, 94, 15, 22, 181,
|
||||
1, 49, 154, 181, 23, 107, 18, 214, 153, 88,
|
||||
92, 181, 97, 194, 219, 10, 167, 202, 85, 221,
|
||||
162, 27, 215, 203, 205, 86, 230, 121, 4, 112,
|
||||
33, 177, 155, 183
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
245, 116, 220, 172, 43, 206, 47, 199, 10, 57,
|
||||
252, 40, 106, 61, 132, 53, 6, 241, 94, 95,
|
||||
82, 156, 31, 139, 242, 234, 117, 20, 177, 41,
|
||||
123, 123, 211, 226, 15, 228, 144, 53, 158, 177,
|
||||
193, 201, 58, 55, 96, 98, 219, 9, 194, 182,
|
||||
244, 67, 134, 122, 219, 49, 153, 30, 150, 245,
|
||||
10, 186, 10, 178
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
239, 31, 223, 179, 232, 21, 102, 210, 249, 72,
|
||||
225, 160, 93, 113, 228, 221, 72, 142, 133, 126,
|
||||
51, 92, 60, 125, 157, 114, 28, 173, 104, 94,
|
||||
53, 63, 169, 215, 44, 130, 237, 3, 214, 117,
|
||||
216, 183, 19, 51, 147, 82, 3, 190, 52, 83,
|
||||
234, 161, 147, 232, 55, 241, 34, 12, 190, 188,
|
||||
132, 227, 209, 46
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
75, 234, 107, 172, 173, 71, 71, 153, 154, 63,
|
||||
65, 12, 108, 169, 35, 99, 127, 21, 28, 31,
|
||||
22, 134, 16, 74, 53, 158, 53, 215, 128, 15,
|
||||
255, 189, 191, 205, 23, 71, 37, 58, 245, 163,
|
||||
223, 255, 0, 183, 35, 39, 26, 22, 122, 86,
|
||||
162, 126, 169, 234, 99, 245, 96, 23, 88, 253,
|
||||
124, 108, 254, 87
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
174, 79, 174, 174, 29, 58, 211, 217, 111, 164,
|
||||
195, 59, 122, 48, 57, 192, 45, 102, 196, 249,
|
||||
81, 66, 164, 108, 24, 127, 154, 180, 154, 240,
|
||||
142, 198, 207, 250, 166, 183, 28, 154, 183, 180,
|
||||
10, 242, 31, 102, 194, 190, 198, 182, 191, 113,
|
||||
197, 114, 54, 144, 79, 53, 250, 104, 64, 122,
|
||||
70, 100, 125, 110
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
244, 199, 14, 22, 238, 170, 197, 236, 81, 172,
|
||||
134, 254, 191, 36, 9, 84, 57, 158, 198, 199,
|
||||
230, 191, 135, 201, 211, 71, 62, 51, 25, 122,
|
||||
147, 201, 9, 146, 171, 197, 45, 130, 44, 55,
|
||||
6, 71, 105, 131, 40, 74, 5, 4, 53, 23,
|
||||
69, 76, 162, 60, 74, 243, 136, 134, 86, 77,
|
||||
58, 20, 212, 147
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
155, 31, 91, 66, 77, 147, 201, 167, 3, 231,
|
||||
170, 2, 12, 110, 65, 65, 78, 183, 248, 113,
|
||||
156, 54, 222, 30, 137, 180, 68, 59, 77, 219,
|
||||
196, 154, 244, 137, 43, 203, 146, 155, 6, 144,
|
||||
105, 209, 141, 43, 209, 165, 196, 47, 54, 172,
|
||||
194, 53, 89, 81, 168, 217, 164, 127, 13, 212,
|
||||
191, 2, 231, 30
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
55, 143, 90, 84, 22, 49, 34, 155, 148, 76,
|
||||
154, 216, 236, 22, 95, 222, 58, 125, 58, 27,
|
||||
37, 137, 66, 36, 60, 217, 85, 183, 224, 13,
|
||||
9, 132, 128, 10, 68, 11, 219, 178, 206, 177,
|
||||
123, 43, 138, 154, 166, 7, 156, 84, 14, 56,
|
||||
220, 146, 203, 31, 42, 96, 114, 97, 68, 81,
|
||||
131, 35, 90, 219
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
171, 190, 222, 166, 128, 5, 111, 82, 56, 42,
|
||||
229, 72, 178, 228, 243, 243, 137, 65, 231, 28,
|
||||
255, 138, 120, 219, 31, 255, 225, 138, 27, 51,
|
||||
97, 3, 159, 231, 103, 2, 175, 105, 51, 75,
|
||||
122, 30, 108, 48, 59, 118, 82, 244, 54, 152,
|
||||
250, 209, 21, 59, 182, 195, 116, 180, 199, 251,
|
||||
152, 69, 156, 237
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
123, 205, 158, 208, 239, 200, 137, 251, 48, 2,
|
||||
198, 205, 99, 90, 254, 148, 216, 250, 107, 187,
|
||||
235, 171, 7, 97, 32, 1, 128, 33, 20, 132,
|
||||
102, 121, 138, 29, 113, 239, 234, 72, 185, 202,
|
||||
239, 186, 205, 29, 125, 71, 110, 152, 222, 162,
|
||||
89, 74, 192, 111, 216, 93, 107, 202, 164, 205,
|
||||
129, 243, 45, 27
|
||||
},
|
||||
new byte[64]
|
||||
{
|
||||
55, 142, 231, 103, 241, 22, 49, 186, 210, 19,
|
||||
128, 176, 4, 73, 177, 122, 205, 164, 60, 50,
|
||||
188, 223, 29, 119, 248, 32, 18, 212, 48, 33,
|
||||
159, 155, 93, 128, 239, 157, 24, 145, 204, 134,
|
||||
231, 29, 164, 170, 136, 225, 40, 82, 250, 244,
|
||||
23, 213, 217, 178, 27, 153, 72, 188, 146, 74,
|
||||
241, 27, 215, 32
|
||||
}
|
||||
};
|
||||
|
||||
private static readonly byte[] Zero;
|
||||
|
||||
private static readonly ulong[][] T;
|
||||
|
||||
public abstract string AlgorithmName { get; }
|
||||
|
||||
protected Gost3411_2012Digest(byte[] IV)
|
||||
{
|
||||
Array.Copy(IV, this.IV, 64);
|
||||
Array.Copy(IV, h, 64);
|
||||
}
|
||||
|
||||
public abstract IMemoable Copy();
|
||||
|
||||
public virtual int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
int num = 64 - bOff;
|
||||
for (int i = 0; i != 64 - num; i++)
|
||||
{
|
||||
m[i] = 0;
|
||||
}
|
||||
m[63 - num] = 1;
|
||||
if (bOff != 64)
|
||||
{
|
||||
Array.Copy(block, bOff, m, 64 - num, num);
|
||||
}
|
||||
g_N(h, N, m);
|
||||
addMod512(N, num * 8);
|
||||
addMod512(Sigma, m);
|
||||
g_N(h, Zero, N);
|
||||
g_N(h, Zero, Sigma);
|
||||
reverse(h, tmp);
|
||||
Array.Copy(tmp, 0, output, outOff, 64);
|
||||
Reset();
|
||||
return 64;
|
||||
}
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
public abstract int GetDigestSize();
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
bOff = 64;
|
||||
Arrays.Fill(N, 0);
|
||||
Arrays.Fill(Sigma, 0);
|
||||
Array.Copy(IV, 0, h, 0, 64);
|
||||
Arrays.Fill(block, 0);
|
||||
}
|
||||
|
||||
public void Reset(IMemoable other)
|
||||
{
|
||||
Gost3411_2012Digest gost3411_2012Digest = (Gost3411_2012Digest)other;
|
||||
Array.Copy(gost3411_2012Digest.IV, 0, IV, 0, 64);
|
||||
Array.Copy(gost3411_2012Digest.N, 0, N, 0, 64);
|
||||
Array.Copy(gost3411_2012Digest.Sigma, 0, Sigma, 0, 64);
|
||||
Array.Copy(gost3411_2012Digest.Ki, 0, Ki, 0, 64);
|
||||
Array.Copy(gost3411_2012Digest.m, 0, m, 0, 64);
|
||||
Array.Copy(gost3411_2012Digest.h, 0, h, 0, 64);
|
||||
Array.Copy(gost3411_2012Digest.block, 0, block, 0, 64);
|
||||
bOff = gost3411_2012Digest.bOff;
|
||||
}
|
||||
|
||||
public void Update(byte input)
|
||||
{
|
||||
block[--bOff] = input;
|
||||
if (bOff == 0)
|
||||
{
|
||||
g_N(h, N, block);
|
||||
addMod512(N, 512);
|
||||
addMod512(Sigma, block);
|
||||
bOff = 64;
|
||||
}
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] input, int inOff, int len)
|
||||
{
|
||||
while (bOff != 64 && len > 0)
|
||||
{
|
||||
Update(input[inOff++]);
|
||||
len--;
|
||||
}
|
||||
while (len >= 64)
|
||||
{
|
||||
Array.Copy(input, inOff, tmp, 0, 64);
|
||||
reverse(tmp, block);
|
||||
g_N(h, N, block);
|
||||
addMod512(N, 512);
|
||||
addMod512(Sigma, block);
|
||||
len -= 64;
|
||||
inOff += 64;
|
||||
}
|
||||
while (len > 0)
|
||||
{
|
||||
Update(input[inOff++]);
|
||||
len--;
|
||||
}
|
||||
}
|
||||
|
||||
private void F(byte[] V)
|
||||
{
|
||||
ulong[] array = new ulong[8];
|
||||
ulong num = 0uL;
|
||||
num ^= T[0][V[56] & 0xFF];
|
||||
num ^= T[1][V[48] & 0xFF];
|
||||
num ^= T[2][V[40] & 0xFF];
|
||||
num ^= T[3][V[32] & 0xFF];
|
||||
num ^= T[4][V[24] & 0xFF];
|
||||
num ^= T[5][V[16] & 0xFF];
|
||||
num ^= T[6][V[8] & 0xFF];
|
||||
num ^= T[7][V[0] & 0xFF];
|
||||
array[0] = num;
|
||||
num = 0uL;
|
||||
num ^= T[0][V[57] & 0xFF];
|
||||
num ^= T[1][V[49] & 0xFF];
|
||||
num ^= T[2][V[41] & 0xFF];
|
||||
num ^= T[3][V[33] & 0xFF];
|
||||
num ^= T[4][V[25] & 0xFF];
|
||||
num ^= T[5][V[17] & 0xFF];
|
||||
num ^= T[6][V[9] & 0xFF];
|
||||
num ^= T[7][V[1] & 0xFF];
|
||||
array[1] = num;
|
||||
num = 0uL;
|
||||
num ^= T[0][V[58] & 0xFF];
|
||||
num ^= T[1][V[50] & 0xFF];
|
||||
num ^= T[2][V[42] & 0xFF];
|
||||
num ^= T[3][V[34] & 0xFF];
|
||||
num ^= T[4][V[26] & 0xFF];
|
||||
num ^= T[5][V[18] & 0xFF];
|
||||
num ^= T[6][V[10] & 0xFF];
|
||||
num ^= T[7][V[2] & 0xFF];
|
||||
array[2] = num;
|
||||
num = 0uL;
|
||||
num ^= T[0][V[59] & 0xFF];
|
||||
num ^= T[1][V[51] & 0xFF];
|
||||
num ^= T[2][V[43] & 0xFF];
|
||||
num ^= T[3][V[35] & 0xFF];
|
||||
num ^= T[4][V[27] & 0xFF];
|
||||
num ^= T[5][V[19] & 0xFF];
|
||||
num ^= T[6][V[11] & 0xFF];
|
||||
num ^= T[7][V[3] & 0xFF];
|
||||
array[3] = num;
|
||||
num = 0uL;
|
||||
num ^= T[0][V[60] & 0xFF];
|
||||
num ^= T[1][V[52] & 0xFF];
|
||||
num ^= T[2][V[44] & 0xFF];
|
||||
num ^= T[3][V[36] & 0xFF];
|
||||
num ^= T[4][V[28] & 0xFF];
|
||||
num ^= T[5][V[20] & 0xFF];
|
||||
num ^= T[6][V[12] & 0xFF];
|
||||
num ^= T[7][V[4] & 0xFF];
|
||||
array[4] = num;
|
||||
num = 0uL;
|
||||
num ^= T[0][V[61] & 0xFF];
|
||||
num ^= T[1][V[53] & 0xFF];
|
||||
num ^= T[2][V[45] & 0xFF];
|
||||
num ^= T[3][V[37] & 0xFF];
|
||||
num ^= T[4][V[29] & 0xFF];
|
||||
num ^= T[5][V[21] & 0xFF];
|
||||
num ^= T[6][V[13] & 0xFF];
|
||||
num ^= T[7][V[5] & 0xFF];
|
||||
array[5] = num;
|
||||
num = 0uL;
|
||||
num ^= T[0][V[62] & 0xFF];
|
||||
num ^= T[1][V[54] & 0xFF];
|
||||
num ^= T[2][V[46] & 0xFF];
|
||||
num ^= T[3][V[38] & 0xFF];
|
||||
num ^= T[4][V[30] & 0xFF];
|
||||
num ^= T[5][V[22] & 0xFF];
|
||||
num ^= T[6][V[14] & 0xFF];
|
||||
num ^= T[7][V[6] & 0xFF];
|
||||
array[6] = num;
|
||||
num = 0uL;
|
||||
num ^= T[0][V[63] & 0xFF];
|
||||
num ^= T[1][V[55] & 0xFF];
|
||||
num ^= T[2][V[47] & 0xFF];
|
||||
num ^= T[3][V[39] & 0xFF];
|
||||
num ^= T[4][V[31] & 0xFF];
|
||||
num ^= T[5][V[23] & 0xFF];
|
||||
num ^= T[6][V[15] & 0xFF];
|
||||
num ^= T[7][V[7] & 0xFF];
|
||||
array[7] = num;
|
||||
num = array[0];
|
||||
V[7] = (byte)(num >> 56);
|
||||
V[6] = (byte)(num >> 48);
|
||||
V[5] = (byte)(num >> 40);
|
||||
V[4] = (byte)(num >> 32);
|
||||
V[3] = (byte)(num >> 24);
|
||||
V[2] = (byte)(num >> 16);
|
||||
V[1] = (byte)(num >> 8);
|
||||
V[0] = (byte)num;
|
||||
num = array[1];
|
||||
V[15] = (byte)(num >> 56);
|
||||
V[14] = (byte)(num >> 48);
|
||||
V[13] = (byte)(num >> 40);
|
||||
V[12] = (byte)(num >> 32);
|
||||
V[11] = (byte)(num >> 24);
|
||||
V[10] = (byte)(num >> 16);
|
||||
V[9] = (byte)(num >> 8);
|
||||
V[8] = (byte)num;
|
||||
num = array[2];
|
||||
V[23] = (byte)(num >> 56);
|
||||
V[22] = (byte)(num >> 48);
|
||||
V[21] = (byte)(num >> 40);
|
||||
V[20] = (byte)(num >> 32);
|
||||
V[19] = (byte)(num >> 24);
|
||||
V[18] = (byte)(num >> 16);
|
||||
V[17] = (byte)(num >> 8);
|
||||
V[16] = (byte)num;
|
||||
num = array[3];
|
||||
V[31] = (byte)(num >> 56);
|
||||
V[30] = (byte)(num >> 48);
|
||||
V[29] = (byte)(num >> 40);
|
||||
V[28] = (byte)(num >> 32);
|
||||
V[27] = (byte)(num >> 24);
|
||||
V[26] = (byte)(num >> 16);
|
||||
V[25] = (byte)(num >> 8);
|
||||
V[24] = (byte)num;
|
||||
num = array[4];
|
||||
V[39] = (byte)(num >> 56);
|
||||
V[38] = (byte)(num >> 48);
|
||||
V[37] = (byte)(num >> 40);
|
||||
V[36] = (byte)(num >> 32);
|
||||
V[35] = (byte)(num >> 24);
|
||||
V[34] = (byte)(num >> 16);
|
||||
V[33] = (byte)(num >> 8);
|
||||
V[32] = (byte)num;
|
||||
num = array[5];
|
||||
V[47] = (byte)(num >> 56);
|
||||
V[46] = (byte)(num >> 48);
|
||||
V[45] = (byte)(num >> 40);
|
||||
V[44] = (byte)(num >> 32);
|
||||
V[43] = (byte)(num >> 24);
|
||||
V[42] = (byte)(num >> 16);
|
||||
V[41] = (byte)(num >> 8);
|
||||
V[40] = (byte)num;
|
||||
num = array[6];
|
||||
V[55] = (byte)(num >> 56);
|
||||
V[54] = (byte)(num >> 48);
|
||||
V[53] = (byte)(num >> 40);
|
||||
V[52] = (byte)(num >> 32);
|
||||
V[51] = (byte)(num >> 24);
|
||||
V[50] = (byte)(num >> 16);
|
||||
V[49] = (byte)(num >> 8);
|
||||
V[48] = (byte)num;
|
||||
num = array[7];
|
||||
V[63] = (byte)(num >> 56);
|
||||
V[62] = (byte)(num >> 48);
|
||||
V[61] = (byte)(num >> 40);
|
||||
V[60] = (byte)(num >> 32);
|
||||
V[59] = (byte)(num >> 24);
|
||||
V[58] = (byte)(num >> 16);
|
||||
V[57] = (byte)(num >> 8);
|
||||
V[56] = (byte)num;
|
||||
}
|
||||
|
||||
private void xor512(byte[] A, byte[] B)
|
||||
{
|
||||
for (int i = 0; i < 64; i++)
|
||||
{
|
||||
byte[] array2;
|
||||
byte[] array = (array2 = A);
|
||||
int num = i;
|
||||
nint num2 = num;
|
||||
array[num] = (byte)(array2[num2] ^ B[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private void E(byte[] K, byte[] m)
|
||||
{
|
||||
Array.Copy(K, 0, Ki, 0, 64);
|
||||
xor512(K, m);
|
||||
F(K);
|
||||
for (int i = 0; i < 11; i++)
|
||||
{
|
||||
xor512(Ki, C[i]);
|
||||
F(Ki);
|
||||
xor512(K, Ki);
|
||||
F(K);
|
||||
}
|
||||
xor512(Ki, C[11]);
|
||||
F(Ki);
|
||||
xor512(K, Ki);
|
||||
}
|
||||
|
||||
private void g_N(byte[] h, byte[] N, byte[] m)
|
||||
{
|
||||
Array.Copy(h, 0, tmp, 0, 64);
|
||||
xor512(h, N);
|
||||
F(h);
|
||||
E(h, m);
|
||||
xor512(h, tmp);
|
||||
xor512(h, m);
|
||||
}
|
||||
|
||||
private void addMod512(byte[] A, int num)
|
||||
{
|
||||
int num2 = (A[63] & 0xFF) + (num & 0xFF);
|
||||
A[63] = (byte)num2;
|
||||
num2 = (A[62] & 0xFF) + ((num >> 8) & 0xFF) + (num2 >> 8);
|
||||
A[62] = (byte)num2;
|
||||
int num3 = 61;
|
||||
while (num3 >= 0 && num2 > 0)
|
||||
{
|
||||
num2 = (A[num3] & 0xFF) + (num2 >> 8);
|
||||
A[num3] = (byte)num2;
|
||||
num3--;
|
||||
}
|
||||
}
|
||||
|
||||
private void addMod512(byte[] A, byte[] B)
|
||||
{
|
||||
int num = 0;
|
||||
for (int num2 = 63; num2 >= 0; num2--)
|
||||
{
|
||||
num = (A[num2] & 0xFF) + (B[num2] & 0xFF) + (num >> 8);
|
||||
A[num2] = (byte)num;
|
||||
}
|
||||
}
|
||||
|
||||
private void reverse(byte[] src, byte[] dst)
|
||||
{
|
||||
int num = src.Length;
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
dst[num - 1 - i] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
static Gost3411_2012Digest()
|
||||
{
|
||||
byte[] zero = new byte[64];
|
||||
Zero = zero;
|
||||
T = new ulong[8][]
|
||||
{
|
||||
new ulong[256]
|
||||
{
|
||||
16643191358083899344uL, 2703135593145367062uL, 14432313314890392744uL, 6577092334268629354uL, 806964168861892974uL, 12383271304659295334uL, 17732807706536339062uL, 17716047611503416651uL, 5215999108449717233uL, 18400690592850740677uL,
|
||||
14664421492831770517uL, 7409386412115095689uL, 3849627103271945136uL, 8988319201874450849uL, 3938119337751376013uL, 11436513915861524438uL, 10023317675342802487uL, 11299449278894865675uL, 13550338097771745114uL, 16515794415152327917uL,
|
||||
14898690228356210642uL, 17039395261956034398uL, 13352261127337576221uL, 10097173395646645421uL, 8519831221648263471uL, 6380786457702773335uL, 4606327678483665726uL, 1135139788101916873uL, 17150000018454982019uL, 1220450159802546598uL,
|
||||
6759235690777098768uL, 5340123591806085420uL, 6034809048673841977uL, 13039342382114553097uL, 6896344311240562893uL, 16756255438728353529uL, 9184934662348565148uL, 14262951085445095100uL, 11989701146933847666uL, 3364644269219363704uL,
|
||||
989048157634869780uL, 9390583993925520772uL, 3387584785362250392uL, 6665082552186727408uL, 8806730920978768603uL, 9502751530347994212uL, 14947525897166917170uL, 9613964225661560606uL, 13062563757722791145uL, 13330165100546479869uL,
|
||||
2007762480278943944uL, 7071029175581714734uL, 18201297078263772920uL, 11348284945966902507uL, 5517131305049330262uL, 2524355749569298796uL, 3276936053954857029uL, 17613168268544492715uL, 13672639287818444474uL, 4227838725751020409uL,
|
||||
17337927304403721484uL, 2880731531503622347uL, 9899166798150700266uL, 1904883134495025448uL, 10431738646323369727uL, 4850405589464713887uL, 17844694253179975574uL, 2081972218582700626uL, 1379356218675464859uL, 11936901258434843983uL,
|
||||
3166351970529817407uL, 15521334050161542599uL, 12092580492986202002uL, 15771347623125549011uL, 9700811178911405091uL, 4095269026725968292uL, 7284836791436182452uL, 178206167350026973uL, 9859056374997736714uL, 10596801556360226242uL,
|
||||
5686309239594266763uL, 3626867272058218794uL, 4695292606739097666uL, 12861765287137887859uL, 7805587275216445447uL, 6553870956925915274uL, 2247080073069027695uL, 14085863101410174406uL, 4136504802722867268uL, 2992705483290644962uL,
|
||||
4655464135170259362uL, 16805373066242361113uL, 867873424238963700uL, 6139766898342725699uL, 5048711494641582808uL, 2360957084007330385uL, 7917754814463914471uL, 11594761360985693448uL, 8900603062938514235uL, 4819584321579539327uL,
|
||||
15611174550416907534uL, 14284208185211612545uL, 2409792751347803057uL, 7449496838093054313uL, 2608138101170875382uL, 15357211532675213082uL, 6775169570724432173uL, 2898833334747545602uL, 10370594140041967504uL, 1717647244194337596uL,
|
||||
16173369581285336234uL, 8669102231478190086uL, 7938185699155198682uL, 9733039751760463299uL, 3969222566346957165uL, 12457959604312733436uL, 9134972545477524348uL, 13884760396816717563uL, 18002934140551462591uL, 8321595681021526376uL,
|
||||
3740161260836255946uL, 12223660219698249403uL, 16465551444743798029uL, 10257300151532365424uL, 5892660270079857124uL, 2502541675832561804uL, 15642277772978588910uL, 3575476887658224151uL, 15246058200794700896uL, 669897467106812851uL,
|
||||
11238190422791214993uL, 14752624989210410255uL, 3809516679850545744uL, 5718256960103440747uL, 16113790886966425492uL, 2758712437335984427uL, 13819266239833930855uL, 15989664336639546697uL, 4334551115747581955uL, 10481700764928886559uL,
|
||||
7539375937215052192uL, 13153907711413100500uL, 1449546416188301313uL, 10970082038091293868uL, 7253733569142936148uL, 14035900984807568422uL, 4494160142329627358uL, 5797380680492875780uL, 5033684639528710629uL, 6942380200648117235uL,
|
||||
18135530224823884898uL, 11626709078934113512uL, 5452010138718157004uL, 646676091767009875uL, 13907700920084023067uL, 11458327989329825334uL, 471514214048988026uL, 17134674851811450558uL, 8614828720478738639uL, 3471190102295415799uL,
|
||||
10201460180741018957uL, 17898929444480167775uL, 16929928319467713572uL, 12279266611417189766uL, 11065079529797350732uL, 0uL, 17378882219737664748uL, 8143953700710785973uL, 12621352907304863169uL, 10952011996134456421uL,
|
||||
1847633384726895125uL, 10773808170632978104uL, 15876370350422262953uL, 10679936021821443928uL, 9235497396103343961uL, 14142057341829074780uL, 18022518333658336642uL, 17502585284089954257uL, 7698943044753169277uL, 5558086220651709366uL,
|
||||
273485755470967613uL, 16009504095842346612uL, 3045458228384031455uL, 8041346034719003450uL, 15324982954333320954uL, 13489933794196071872uL, 10831118253720235397uL, 16952024345057011140uL, 378507382706538567uL, 11825014717556671663uL,
|
||||
16409080926193522583uL, 9338657737701047993uL, 12581524441769923617uL, 17534533004330692145uL, 7175315966437357774uL, 5161723385538695992uL, 11755314076501405237uL, 8789995975362646502uL, 122301190315135456uL, 1339527752872090491uL,
|
||||
8500289773969701394uL, 15811458049371942451uL, 2206125151973814415uL, 5912507865751560921uL, 16287906185945826935uL, 18369869324697130021uL, 14569423993732859509uL, 7055674186596964883uL, 16666412735158177328uL, 3226973935617776037uL,
|
||||
1613642550683796188uL, 12110648323039866715uL, 2113919936262685618uL, 13204150689214484020uL, 15014857054700793693uL, 18241125550101047064uL, 6290905146667117214uL, 520631834711206554uL, 6090649271097153955uL, 6268809121346255742uL,
|
||||
13723986651981426055uL, 9011259725410191425uL, 4260067298331642521uL, 7571604515825379392uL, 12488780875022903068uL, 12805342026244830542uL, 4929679943725850629uL, 1562558313115120097uL, 8378270681804975090uL, 6431029428379739063uL,
|
||||
17272301201645698147uL, 7740178818457750173uL, 4437711457076851171uL, 11714359155137757141uL, 1735746844117158901uL, 1251271430781151302uL, 13440816173802288160uL, 15127024593679826621uL, 12684167292580488366uL, 9572728449396225790uL,
|
||||
8122139634094273109uL, 1040986648309745961uL, 356411355647007143uL, 756721191328944270uL, 5318878451677075985uL, 9992214447015657431uL, 14774439056095358191uL, 14554332407323555144uL, 10573861039948903458uL, 14378080326902922337uL,
|
||||
16267522719607272778uL, 15142897865499331968uL, 11116171328892012145uL, 12955918427198494611uL, 8272760012479254664uL, 15480098276725396519uL
|
||||
},
|
||||
new ulong[256]
|
||||
{
|
||||
14416488623970932190uL, 7346976492199675417uL, 17820627941079412291uL, 9627885164147495734uL, 6533467991705271684uL, 17998565102924987645uL, 11682290849618289423uL, 14822454089690596717uL, 9241203884170920837uL, 12861281897341358396uL,
|
||||
11016838668432197665uL, 13790417110750001176uL, 1151735774811599048uL, 12294974454994705048uL, 6942284815962019498uL, 17743186425950817333uL, 3304311495801540674uL, 13615607175011632002uL, 7559468737934159677uL, 12086978407351415740uL,
|
||||
2957424603572281991uL, 13249085212744581519uL, 15637041785948145051uL, 15490736964426020179uL, 15379540838187847369uL, 16875309257069950292uL, 8085181091649676441uL, 14854792058562395895uL, 18236790505793211960uL, 7642183762405766865uL,
|
||||
6618156441656008690uL, 14625287379506105394uL, 12725675947919526096uL, 6768646983239759067uL, 1229560361235927109uL, 2545497942825370364uL, 14198931498357636346uL, 8332055718082846467uL, 9018811952317763005uL, 4566156016530439069uL,
|
||||
9578872965805076800uL, 4447555131281990257uL, 1889868047608435272uL, 9132310989383824935uL, 8423405394594584156uL, 1791576812070341540uL, 895921998879640402uL, 14310611323784447840uL, 8686043850126826766uL, 18187216356076809294uL,
|
||||
3505479608743889749uL, 3164974344323824491uL, 15983355795994687326uL, 11065428791749044823uL, 1491256427646959862uL, 13524521346665040605uL, 5832729293860314623uL, 16778152971070057656uL, 5588041959860897512uL, 13969351553252232767uL,
|
||||
8960651507453538251uL, 3404044757246527918uL, 7427396029290209269uL, 1567782000193888896uL, 2125332809319160599uL, 15750446333599546881uL, 9762612037615848154uL, 14665011075415780932uL, 405965478745608371uL, 1334608620921465823uL,
|
||||
10283812314525356347uL, 6009835698019100261uL, 6362838478626120808uL, 15531164210339026725uL, 12192858250690617602uL, 10021977722754318728uL, 15045784853695487610uL, 5388605995385671607uL, 4789436484882904608uL, 8273825248612433269uL,
|
||||
6807803589745610561uL, 17643602969366402521uL, 10402470611175420342uL, 9487505662496421919uL, 13884311246691511369uL, 11599311963427125987uL, 11828896870259984422uL, 12530171073360712647uL, 12569865343065303133uL, 14159137227003829900uL,
|
||||
13058444037354018915uL, 447961557886364457uL, 11640371804876806293uL, 7989344413684106260uL, 7745519667033183399uL, 15284366481134480575uL, 16558308431455503772uL, 11775845520284565881uL, 5873227033075236745uL, 18442457284438988756uL,
|
||||
11331596615082596498uL, 2021362311995275617uL, 17399916748381652330uL, 11098401108231983565uL, 4652678544566382540uL, 1850721214614677970uL, 3779734978877634960uL, 16220639879209580377uL, 13354894618632655665uL, 10461614931578557376uL,
|
||||
1609153642657618202uL, 4480481336861891051uL, 9972894948967769086uL, 10111053643493542103uL, 4986581067319345023uL, 209097870346312172uL, 16170362179490862383uL, 13999482202363688357uL, 7798271984477724152uL, 7243570012128126063uL,
|
||||
15113725370733198348uL, 12902693100934476618uL, 9428713116562070121uL, 11255071236346149604uL, 18037622684799010663uL, 11445008741517796104uL, 13269290432258683207uL, 11202301361362088891uL, 14405433630425769238uL, 1180970181547717171uL,
|
||||
5176105366933055123uL, 7188535108749356336uL, 3071419591516346653uL, 8871593212975408788uL, 7377047836268559747uL, 745784333737205883uL, 523712867556149599uL, 3218003841565405748uL, 2276692251301888763uL, 4042724623974101698uL,
|
||||
6062869490421444410uL, 1668368207247227756uL, 18339543398465779106uL, 2916365256580759793uL, 17701763194650376111uL, 9835873103984114194uL, 6147487227497352524uL, 16084205907180383922uL, 347243151226876613uL, 6999741710823961820uL,
|
||||
2806071973676585551uL, 9658008234483460268uL, 17263250852726433255uL, 3821216675772636134uL, 16657742647079378032uL, 15229027975163028960uL, 16016335965578415300uL, 10572801009443498074uL, 17296932700355831580uL, 13696158890081409646uL,
|
||||
17554522822051683462uL, 5286400504730760237uL, 7609746906341231947uL, 15898737839338493224uL, 944934528471361828uL, 0uL, 6430778939292435998uL, 3336736409998420952uL, 7875361642996334478uL, 16600215533801848326uL,
|
||||
8821948844834997474uL, 12436686564748095921uL, 706136312374746081uL, 9940546931731077220uL, 8606606803205598662uL, 6252543065422558934uL, 258180561991695258uL, 3932361041049477244uL, 2228031909561065613uL, 3591083712976693539uL,
|
||||
8046871333151188578uL, 4829160237163942998uL, 12381352628294170862uL, 12610854471631768107uL, 4380598834454315015uL, 10649256363573758508uL, 14068055968846895059uL, 5548248045526197406uL, 12819864006535506598uL, 4026619618737144330uL,
|
||||
8789027980293848952uL, 5914590821894013971uL, 5201360692628199003uL, 1438508405363181993uL, 1047132165489660606uL, 5627682402975382898uL, 14753809747772696347uL, 1966322907280535614uL, 2694302999865518549uL, 2361664381868374374uL,
|
||||
13457635568901744811uL, 17504877959453114096uL, 9317025438026129907uL, 10808279102775697669uL, 6673472819996841645uL, 10887564197671041257uL, 13657089091486917108uL, 4917937056650714377uL, 17110791576323341323uL, 8472979738121466922uL,
|
||||
12991487546715896341uL, 10838903498911111839uL, 17212902440124499857uL, 3621785006072287929uL, 12125479410148983668uL, 17921053425751121547uL, 2600818822502173603uL, 18095853622957810961uL, 5457179844010077633uL, 104674322205155958uL,
|
||||
2459119713154539658uL, 10359563843775427405uL, 16481219104186197994uL, 8162692825194907375uL, 2847061296053530681uL, 17007385453270867581uL, 2429042753721621264uL, 15845690715266440311uL, 11504223637356675454uL, 14939902389728255105uL,
|
||||
15596543964592056301uL, 4291817720210491736uL, 4250406047929019182uL, 16835609371473809102uL, 6848231166907919671uL, 5722504929196957444uL, 13163410864325338105uL, 10169775613993200289uL, 14511240905997678504uL, 16428453455105502901uL,
|
||||
804576823300162061uL, 16952328422295033634uL, 10704308825032065907uL, 9209752147493311569uL, 11915204881515227728uL, 630314289831180695uL, 5090994841956713701uL, 15144339993900648342uL, 6320413364043601056uL, 4136912626218684596uL,
|
||||
3688671278293774543uL, 16325117494106688707uL, 7111516025665441606uL, 12019670279683559882uL, 4747430357904739770uL, 8503693248481660848uL
|
||||
},
|
||||
new ulong[256]
|
||||
{
|
||||
5022191610516858060uL, 12645839371960098056uL, 325196729119310435uL, 10544111738277527250uL, 17171723943877278631uL, 1619663718359036237uL, 18091587880400184708uL, 5335828035754758151uL, 11379178129143138841uL, 14648217640238887722uL,
|
||||
13086832364598786016uL, 15914535959731104950uL, 1287844858498292800uL, 16375051820953423571uL, 11583759697334954435uL, 550964732710513933uL, 8585995564513096669uL, 15686472308550713348uL, 13325075562694951572uL, 17630740042449523023uL,
|
||||
8916962000159598712uL, 14192583705586485217uL, 8560921352511315212uL, 7426955371667046732uL, 7096434795747339774uL, 2543027200294554449uL, 10419956338547081069uL, 5721517685081291957uL, 1937338776563641064uL, 13736071204485071176uL,
|
||||
17106932809158825161uL, 4899378514713126672uL, 14773093419524387062uL, 16698373717390806192uL, 3710095511616993728uL, 7340570904524980467uL, 5918098101825092432uL, 10804520625326298079uL, 9730090089577636081uL, 4697601972911171247uL,
|
||||
10480451310123618748uL, 5151346661287437683uL, 3331340938598661669uL, 9395203941325327427uL, 11242079635202581704uL, 2915854855682031097uL, 1492126639482712306uL, 6290138385502410722uL, 9851336768255988794uL, 1856873279258002822uL,
|
||||
5831708560246045423uL, 9075623880903535524uL, 8815370023405405865uL, 13167394464051894414uL, 4378109956818586891uL, 15006898764834764563uL, 17307241128934970156uL, 2668061343609997453uL, 15557224681013713438uL, 6825682518352077045uL,
|
||||
9524557205897875359uL, 8172884461239939073uL, 12192217863250075348uL, 4603827798664618597uL, 2867088101655710002uL, 8332998285759836606uL, 10671362497273609486uL, 4672530790940461182uL, 1134559637999613131uL, 3897488771451098482uL,
|
||||
10309743511802411319uL, 17673793229178945438uL, 16718005676701005947uL, 7221495326637043234uL, 16597347416266760801uL, 9272158342823316988uL, 7909827430729881909uL, 16002662719275707657uL, 14283453708927338078uL, 10869263363796925617uL,
|
||||
16345475348111384578uL, 137665338910158545uL, 12515539920715504823uL, 10988984776972674731uL, 6746265954835287963uL, 18257106040948926040uL, 17421816711713963517uL, 16919288509122574788uL, 17268808778493482358uL, 5838760703968564286uL,
|
||||
14031617523076498493uL, 780392312595241081uL, 18336540041995274550uL, 12842206973407828717uL, 13656722531824102950uL, 7015947325939974800uL, 3645098545556231953uL, 17884924832500353770uL, 17512690432282197058uL, 2066784160966486324uL,
|
||||
13412572121352667947uL, 2661572689932379740uL, 1033530071105264154uL, 12957343035395093564uL, 13868125990049039746uL, 3121291101747188375uL, 6662680657495023946uL, 4471231211228269748uL, 15424627025727239375uL, 12580275654379647961uL,
|
||||
4219404094166535895uL, 4762278602948970945uL, 9478790587773903506uL, 9898893830113671915uL, 14973264685418819913uL, 453876631214605276uL, 4407685011702703066uL, 6616899736578362439uL, 13003248198425336113uL, 12419015558860141158uL,
|
||||
7990341289637279323uL, 14442713494739508292uL, 11215801851269958085uL, 13332124370822523973uL, 1373724075619900927uL, 13783625232014225305uL, 15359939653945809825uL, 12751808085835111250uL, 6083572248920684684uL, 3484333688484746926uL,
|
||||
14521793786758034069uL, 11937786166553331057uL, 9139255992929895626uL, 10222251748408277128uL, 12255784141711061434uL, 2189202321390233739uL, 716843780901418775uL, 8359149623152811187uL, 2415494092048157422uL, 5243176795453837752uL,
|
||||
18210112488466909321uL, 4154687863979303353uL, 2272222198031926874uL, 8690072078618429206uL, 202427615156584895uL, 9639695198538801486uL, 18415623364178962407uL, 8942462418363155829uL, 907753262429150117uL, 11810680268281218733uL,
|
||||
7665181140788048952uL, 10898276329415234144uL, 3239326427400699802uL, 6868174095610514980uL, 3808219771792118989uL, 7543278091629671815uL, 9168270020997671451uL, 12392885120222240619uL, 968811775037617524uL, 16140326090467902424uL,
|
||||
584245825585813958uL, 15816256665709806011uL, 13531648566339513850uL, 8750568181363911111uL, 1698743779072106396uL, 0uL, 16943798148194031381uL, 8236472591980224367uL, 13209882775544247903uL, 4035151075898375075uL,
|
||||
10302693318479402982uL, 16855667680011982506uL, 1948891778265760825uL, 9719098429884157472uL, 11054545481701918842uL, 18085100094794517333uL, 390196158411648178uL, 6501762612143760022uL, 2496032298569901440uL, 6037175610368660865uL,
|
||||
12762796728381896067uL, 14204135840323823920uL, 16148186350413554109uL, 17041934512146922008uL, 4925668384127994397uL, 12167143351085101061uL, 6410254435445760809uL, 15330361267764327792uL, 14853624750388125592uL, 6421246412478107128uL,
|
||||
10056755301905365844uL, 17754265602204553456uL, 15589384456651501269uL, 3832728561082858012uL, 1367237356757087022uL, 15171532294165509804uL, 5272189459801538409uL, 3250881896629883211uL, 8001331881476413578uL, 6968391399550921793uL,
|
||||
15232592138832550013uL, 809969847888836264uL, 11442741800557897079uL, 11677520222457276540uL, 16472678002634826718uL, 11836194988174371744uL, 15751256438176362346uL, 14766608101443821095uL, 3078237133952334918uL, 11467814949005020070uL,
|
||||
13951147765685021523uL, 12061945554227725518uL, 3445914570843754740uL, 6242581091097786675uL, 16533735384072260879uL, 1539119056502398499uL, 7794692769390540772uL, 1168206872894059153uL, 15939048079214002791uL, 226938317788999534uL,
|
||||
7419906245093191581uL, 10103172840323946073uL, 3581420404919564415uL, 10645700449263505411uL, 7585766634561542998uL, 2998877762491110184uL, 7174951347083690799uL, 8107322624236560080uL, 13578204632486592759uL, 5494625981129708507uL,
|
||||
8496251452686250594uL, 5596216022932782346uL, 5661024595188978276uL, 2747447103066187747uL, 16280780971239378796uL, 15107926451017931202uL, 14398026278718810255uL, 9978235489092097413uL, 5085786205181774754uL, 4053661314365738856uL,
|
||||
2109836211124176869uL, 2336412667751326783uL, 11612775692986304274uL, 9314648022329008941uL, 17501136362727821971uL, 1742301027778505559uL, 14601226005391810043uL, 7748764524654516969uL, 18004566148294681659uL, 5468987013722291926uL,
|
||||
6162091280534394461uL, 4280460676255686662uL, 12001449201751239199uL, 17837288741426265633uL, 14074668225098984172uL, 11119279424363751188uL
|
||||
},
|
||||
new ulong[256]
|
||||
{
|
||||
412778415529865760uL, 3577342882794069583uL, 12805512713741006036uL, 10086389182405830274uL, 9631348414619490491uL, 11029226397073373641uL, 11627792458048644653uL, 8540370589362648094uL, 17925746536371200188uL, 13719614496949379764uL,
|
||||
7380585705851070183uL, 5532990722829248072uL, 10754888447158766146uL, 2363770411641709807uL, 4799219136276863089uL, 886827555711267723uL, 14761170299289685203uL, 10870602893012709289uL, 14053970170398186163uL, 15159452414945845267uL,
|
||||
152494743391849906uL, 14797380007077078154uL, 12067788661428416998uL, 3618765265895209892uL, 16730148953297482309uL, 6638666397703976113uL, 6746862217775881448uL, 12434579095834639871uL, 6325409020385589416uL, 11881705736862929933uL,
|
||||
4058193356686090212uL, 7086287341885701278uL, 15674826541200387082uL, 10483145580681646139uL, 11728250915792503750uL, 16457218618247725518uL, 17804566359647483612uL, 9778627006082503945uL, 12613833228843374612uL, 12973791078126503668uL,
|
||||
3891946276334412253uL, 15267727468379129930uL, 17962016790450440421uL, 8437586805018431989uL, 10621274027794111881uL, 11432405351048939099uL, 5716202677831336867uL, 3243582143937888061uL, 15866832917474188842uL, 16875173606078234615uL,
|
||||
2399609570378190006uL, 7345426154655236876uL, 7619693495944769671uL, 15822815208912544184uL, 15130995325890332152uL, 9371659578628761385uL, 7194324827586368711uL, 4309013456660281871uL, 10250322826443576482uL, 4166240605864818621uL,
|
||||
1366177816882650955uL, 12337542112347144813uL, 6217414342487101681uL, 7783606731388437159uL, 17120176822620799077uL, 9037712741102696455uL, 17012341578268600380uL, 16118235504218274753uL, 8304362858347251262uL, 8707177795482651175uL,
|
||||
15023151349731995041uL, 13385369152941776166uL, 16304327354866472453uL, 10358449395342638331uL, 18075424330969541902uL, 11764513330537491359uL, 11174112167783249019uL, 11138113584220445730uL, 1945973140213898976uL, 4200834197375652438uL,
|
||||
5435457027409418714uL, 7756135292223372597uL, 16717045777202619429uL, 2508515451320396125uL, 9523284449426682082uL, 14317999436018362515uL, 609168629503044082uL, 3755788431048666223uL, 17152592151573603726uL, 5399380291345002883uL,
|
||||
12301351165578275380uL, 12189559766827372511uL, 8884253657276313548uL, 7648167075477099884uL, 1809525021741863762uL, 6185017703168619802uL, 14510683649402109683uL, 2064551487375270258uL, 16010179378360767384uL, 3469367101608228374uL,
|
||||
1536322983539794137uL, 5796100959046534970uL, 1773386847580617483uL, 108346540153546841uL, 9001722862285623390uL, 2544785687349641476uL, 17260840743582043607uL, 551619354788593554uL, 16565125331131263383uL, 6486896028513362810uL,
|
||||
13119530067680070470uL, 717443701121057195uL, 2214371934970162368uL, 13227357483453784863uL, 1140410460104840800uL, 7488913346602372798uL, 13429536041485875405uL, 16412374620625092188uL, 994812197283306450uL, 10114437580603988240uL,
|
||||
2804228341285155620uL, 260410206251574763uL, 11468694089796595202uL, 12153782305157883782uL, 4835367340339408936uL, 14935941453496953144uL, 6348897592219414216uL, 13580632830920879878uL, 10447068863337265250uL, 2250650893928301721uL,
|
||||
7511575676904462558uL, 10993078211198701968uL, 15566911097060688979uL, 443413779826989003uL, 16983149403301320622uL, 16608725716676480124uL, 1500526641193339008uL, 8152292870049500044uL, 9978535451240877787uL, 11591943528381025908uL,
|
||||
12470629360879408550uL, 858245894210178073uL, 10222432276826665289uL, 13827511182212795117uL, 15446122921225714282uL, 8743440194190385790uL, 1218336215312585465uL, 1032276353674575417uL, 14900145093364744545uL, 18402470457342123822uL,
|
||||
10906820163225880560uL, 5903954706119241571uL, 3395223664118723215uL, 5940852870679710344uL, 12577914836476031053uL, 4685830293981560218uL, 12946605235894991206uL, 9228745979626367643uL, 11917827469888011348uL, 17379906352690864407uL,
|
||||
9183300283681142197uL, 4649831692234020291uL, 9147178532201324012uL, 15296022985070163928uL, 17486629686828287228uL, 0uL, 6898510060154343770uL, 1643589765241144626uL, 12697544478944746637uL, 1402114711967872786uL,
|
||||
13537723148088938644uL, 9937131744855075632uL, 4353588900056941981uL, 14172271166703691041uL, 5292673456375059560uL, 14626957485788666648uL, 15714468687486372321uL, 9828864528591782761uL, 3999781539141355908uL, 11292166047742839785uL,
|
||||
15554107579106054707uL, 12031798801067137471uL, 11328023846638773168uL, 6049120102845506257uL, 7933416458737094421uL, 4461908978498334148uL, 6790446112744143107uL, 3726873057198478333uL, 7237529453753559893uL, 3864037041512891446uL,
|
||||
7046626506133690741uL, 304722305440127609uL, 18266442068824101532uL, 14208550143986027896uL, 2934657069911674141uL, 6076891152586415427uL, 14663026374551417665uL, 4953135073593642938uL, 5569207977274658321uL, 4501008218251986991uL,
|
||||
4608914949056012406uL, 14017832012144685802uL, 4989423796032989155uL, 10719102116777386523uL, 3207522108247185252uL, 17522399594662456485uL, 3108845752482162934uL, 12838487398799446335uL, 5256746322603637809uL, 3359313093287303894uL,
|
||||
1679799455245938027uL, 5680416363222969338uL, 10585346912750056912uL, 5089304188877242888uL, 5125161971465923153uL, 2663698829192085142uL, 17343705367582804302uL, 18366341168314316663uL, 2840208181611518845uL, 13688960456037058399uL,
|
||||
6456820608799639185uL, 7891434163331525374uL, 8401668394735843756uL, 16166056177409219511uL, 3072644785963581615uL, 17657281445989989230uL, 9479582578768039792uL, 13876877822288985944uL, 8268585413250313831uL, 1254405120516291232uL,
|
||||
1909966976157497017uL, 8848404743649085333uL, 14353865065657041098uL, 9670431168224853328uL, 17768506339595050629uL, 16838256760641149468uL, 8116101939860513749uL, 750401899863958592uL, 13277331648651621759uL, 6938439418119057708uL,
|
||||
6595232530173053731uL, 16274235453266392046uL, 2970426959157404996uL, 14474746770758882986uL, 13082049433730969261uL, 9337082464844055234uL, 15404157107807690625uL, 18230462244674452165uL, 8576420872327912519uL, 2100417098697099563uL,
|
||||
18111263507761251671uL, 15975038508208797299uL, 2699828134389257935uL, 8041674830518265676uL, 13912883970442876673uL, 17621370890793685815uL
|
||||
},
|
||||
new ulong[256]
|
||||
{
|
||||
4535860555263248921uL, 16852860281346875951uL, 6140263643322089338uL, 11478926500824812339uL, 9378015351203239756uL, 13327564710645084082uL, 14720227639147585363uL, 3976828915743059002uL, 10848963853955034384uL, 1674533312481288529uL,
|
||||
12560205794591624211uL, 18251760262641326195uL, 1971238590622152549uL, 3457832774275448914uL, 16150883333172724748uL, 11794428727689681111uL, 4727861667757458726uL, 18086103234836705689uL, 258683541723454221uL, 14234423747971299696uL,
|
||||
13746687986412618920uL, 2233564259985426290uL, 1378062126584976310uL, 595850907021173971uL, 1148729542089166137uL, 7220711707371485274uL, 4275768676387371534uL, 3496324401055548880uL, 5165544922527804988uL, 17490393461649265994uL,
|
||||
8412095690608439521uL, 14978908882929042526uL, 16577882496662810390uL, 8847930418030264104uL, 3309372870217980335uL, 12420682712621503982uL, 15313638727721349947uL, 4387469920445666276uL, 15069308889790759724uL, 7765336488733668707uL,
|
||||
7626271911434349726uL, 11162557623114971940uL, 14550225336477740868uL, 15462274553512913606uL, 14126703745409497984uL, 4089712366057758979uL, 2084822880951770767uL, 15217945823675505361uL, 15628305894032057615uL, 12769693467676363153uL,
|
||||
8115270375799669254uL, 5758785072471788271uL, 17025131558859428408uL, 6401431119593581502uL, 2607396063788253068uL, 149165799264492029uL, 10429296302893549066uL, 10743412135794079261uL, 11655258597668356394uL, 3161051439931945653uL,
|
||||
2862129545546507393uL, 12732210800733670241uL, 1786865009825524840uL, 15183877174701053985uL, 3531814185980970784uL, 1489042636343605852uL, 7953372014297400692uL, 4127379132466308083uL, 703148615521134115uL, 3051392466055327813uL,
|
||||
8884721462095101400uL, 10883429997862481888uL, 8288941250235652363uL, 18364623810490598270uL, 2902931453887900088uL, 8254758264932555771uL, 446716860198103834uL, 6435790520947517774uL, 13467158152785180239uL, 14590483660463305801uL,
|
||||
9043176952977856194uL, 5945702658036389520uL, 5618980515518141202uL, 10464882797601703162uL, 15944691810401457432uL, 4424111993825693972uL, 13996955283450564762uL, 15498010045204162614uL, 15034711375462777308uL, 2457938976092230806uL,
|
||||
17280995377108044085uL, 1042563110803492809uL, 14698967823614015161uL, 1000057432295396548uL, 298081034413880039uL, 17972098505603721876uL, 9182311907627011903uL, 4237995257073580286uL, 7360269973856297383uL, 13151329901121790075uL,
|
||||
12073241782278564400uL, 14383131042028101773uL, 6575665438039683251uL, 6873491385755763284uL, 5461089107914913800uL, 14868969018190677678uL, 10359842278252856200uL, 9238174518821588657uL, 17527071737039935418uL, 13502357048437945535uL,
|
||||
15348847553065546187uL, 11339121943878505166uL, 9517911241040346198uL, 9971396702494946207uL, 4759102407226695211uL, 17378647008906169927uL, 8551269127753525532uL, 3200854107087625055uL, 18216411296800512643uL, 2268055074172700034uL,
|
||||
17342145754611272887uL, 16259542889635186428uL, 7467557820838912855uL, 5842407176390331805uL, 13781293231417820760uL, 11545326463606686682uL, 406460662087397399uL, 12248428481276306937uL, 2713878548564622716uL, 15813794323767901698uL,
|
||||
12909252825081827436uL, 16687682160775540198uL, 13363292471131286850uL, 16093363920219344101uL, 17173522193701129157uL, 1340149631943889222uL, 17937642222676617316uL, 13187698676877812363uL, 13848249080340613479uL, 10604652161736989959uL,
|
||||
8063408691889325956uL, 2565552841162102374uL, 4619087852284151766uL, 5214791045043151621uL, 11825258692200411453uL, 8447401294454977041uL, 11060374817020715049uL, 2118998135578172543uL, 18400220166708232590uL, 1638138058256147361uL,
|
||||
11933752785043076045uL, 11022751975238574809uL, 7923814141646626425uL, 8745197289204327461uL, 10251172860281438584uL, 110046607829271280uL, 17001249825241725906uL, 13607620101989539157uL, 12527281127159060254uL, 852275931467612126uL,
|
||||
12280242513132584692uL, 6838009262779170980uL, 17658513533341858976uL, 1935925255570044309uL, 16894801472302283042uL, 0uL, 9624367324648619686uL, 5055648419791580364uL, 744521925918424366uL, 8708792173461317333uL,
|
||||
3012835619118082888uL, 2417128113004220315uL, 1525826018450700460uL, 9485585257278707132uL, 14103543837016167018uL, 11442415316829571523uL, 555623154201897450uL, 9144795202799799759uL, 3942477176883605194uL, 9657855435820246443uL,
|
||||
18120314326393467753uL, 5511376580148613602uL, 15664667007830861823uL, 2755821882842654321uL, 7814304129864883337uL, 5354596692910111480uL, 15776976912903093490uL, 6280103384783087239uL, 1191443428809820347uL, 9831487700257630818uL,
|
||||
3349066620751495842uL, 7030425627006208077uL, 12141104268537423625uL, 15906742984900188136uL, 17132709196148330696uL, 11964432129338157248uL, 13954451705047518103uL, 4572398174778067177uL, 11684814352765786663uL, 12387952654176517124uL,
|
||||
17620705939241991760uL, 6698133237560285529uL, 6924249334748056253uL, 4915877947555788081uL, 10111296835088767109uL, 13048598907028568950uL, 893432504920462900uL, 13011771634646415750uL, 11302435937273876542uL, 3644679859973545005uL,
|
||||
1822496103478537880uL, 4867631031866531035uL, 11200357486546532820uL, 9345637772528684097uL, 16055873590790557205uL, 5651355959728424991uL, 6102500154237255050uL, 5321106438415311349uL, 12666416081784098531uL, 17769131758322608045uL,
|
||||
6541340121823556163uL, 10219929968899119733uL, 9941277503389493394uL, 14829851944308301219uL, 5982354661116236896uL, 7656949129855104915uL, 6733579076392590249uL, 16299238792081284593uL, 6242444349043369079uL, 14275799167331476093uL,
|
||||
16539327809960162331uL, 10709193381743359213uL, 7063352394543305536uL, 7169984984404332976uL, 10081152420505776495uL, 5805861825662078829uL, 16746518581295898847uL, 9005272120822338610uL, 14441423414742904244uL, 8149771120223764726uL,
|
||||
16429564429454849771uL, 10569310857845530615uL, 12871313860553036444uL, 2309691269046898027uL, 3794159109699761975uL, 16407864263942807297uL, 5025527060452063169uL, 17806764530333181277uL, 7517355687739125358uL, 7328458050963454634uL,
|
||||
8586890291714583532uL, 9764207066100180827uL, 1228969994608986699uL, 3680135560314805981uL, 13641698681201336229uL, 3828474564935022023uL
|
||||
},
|
||||
new ulong[256]
|
||||
{
|
||||
9993673838465935981uL, 4884127456122942266uL, 633234397777501925uL, 4175331866391974078uL, 13960862214643959730uL, 3179702086296496554uL, 16933906489121384602uL, 10694170786544668381uL, 2821567796802171918uL, 10017225945983459410uL,
|
||||
8620367719189969058uL, 11912340740684202118uL, 2483193514877514189uL, 15820547210744392958uL, 6709821147687032714uL, 1033155461149792359uL, 17176581185527344473uL, 12676916669343913757uL, 7895548116537857006uL, 17715094920495729706uL,
|
||||
17733400074663874411uL, 10798980099858302178uL, 17017586219309622311uL, 14869918229733283306uL, 14186578078356341361uL, 5087312367917758201uL, 4403136284925796162uL, 11445060531346954566uL, 3775805186400799640uL, 7039684027147830007uL,
|
||||
14348961028615061808uL, 9697319919564979060uL, 9738807224246382923uL, 15223411982051930651uL, 7393281011622612554uL, 14606960238789696022uL, 11882147622572427961uL, 4010248068099147993uL, 1391157335655481238uL, 9401574542884107400uL,
|
||||
4570854599768479292uL, 10257649927482729873uL, 6677411409753660853uL, 1869614486838700045uL, 2692552123250949488uL, 5803321545042218156uL, 2939237684151739990uL, 12563222234819403042uL, 979419644422445862uL, 3673082354135452954uL,
|
||||
11533447497148368197uL, 18030958354471640690uL, 17612545135863809557uL, 8160101394646269472uL, 8665997893524695802uL, 13625616718215733940uL, 18235386795261924047uL, 5384800866856167392uL, 12916723256358730451uL, 13592733055320369291uL,
|
||||
1572125733959825684uL, 16319952048648813632uL, 5756880660441440803uL, 9141459606454807672uL, 5643134515567552540uL, 16615690806985356220uL, 3878345071173861799uL, 9227030292403715574uL, 2223083610095396016uL, 7793025783231257041uL,
|
||||
270525564056519489uL, 17847006580419200340uL, 14816163592450856619uL, 14466615231819486056uL, 15073980746734173029uL, 2096675571659106289uL, 15724638447412750017uL, 11654178806006959994uL, 10463964225571785502uL, 3546709370140069467uL,
|
||||
15706368347685578112uL, 177299642257213822uL, 5128824965654363334uL, 3284371762775499669uL, 13419641178229583369uL, 16654934045169170819uL, 18403114047913597873uL, 14517143817528858967uL, 14108395695510474444uL, 11707930154007137339uL,
|
||||
11096530083400914427uL, 1469770529583346475uL, 16266162354777931521uL, 8788550027033796805uL, 7435101501630170229uL, 14015179664977724659uL, 7273261710249789236uL, 17122228403727216152uL, 18126471264669209677uL, 4054715969654905473uL,
|
||||
7599903567200476178uL, 5246485543787097758uL, 2066061333143563726uL, 8899911909190592955uL, 11318703078624401927uL, 6203347905963379246uL, 13279357838919968631uL, 8982737327710905606uL, 12768987285517661101uL, 4733860583378205252uL,
|
||||
8385890890472733667uL, 9576385652165374474uL, 1983784429268132211uL, 1144615724587504216uL, 9497500870535659191uL, 874653248460230937uL, 8806272569836118916uL, 13123279255654448912uL, 8490507753801150940uL, 4996983704035276216uL,
|
||||
15899414662248931395uL, 10203863540057188048uL, 3101674511553930519uL, 8063548039483480720uL, 2782314667368316465uL, 7188904195392064393uL, 13234879149618951471uL, 9015613289737121593uL, 12161612559689161632uL, 6918945025995344072uL,
|
||||
10371370779234316206uL, 6314684983379722257uL, 18205211228616546544uL, 516733887222336701uL, 5295035016025023649uL, 4372970618206377341uL, 1367579971541251497uL, 16771488207379590107uL, 4788193436127426821uL, 10413312453672812833uL,
|
||||
16525909700027724029uL, 8286510523341552993uL, 4528946180991933443uL, 17331684625243261417uL, 354332099228667900uL, 1703685653761847402uL, 16413616708890474111uL, 17892572632091416332uL, 1749306493112033842uL, 6593587578816598280uL,
|
||||
15307631993767476902uL, 3696774096474081061uL, 6359145787360465481uL, 13751446413401596405uL, 7551609152712119853uL, 15025544492090470746uL, 18361178145650889102uL, 5408360666650553823uL, 1887903149406137164uL, 11803390319907948036uL,
|
||||
1193308391354416343uL, 12323998817483741409uL, 7714012032021747052uL, 17506448615360610455uL, 3338705706857455828uL, 0uL, 14628435370100609065uL, 16493078850282879682uL, 8307846011238545246uL, 7949845912698622127uL,
|
||||
123000737600621119uL, 14981351001126100949uL, 3896669045897085670uL, 1241734746156300008uL, 3450192318869875435uL, 16822271919490900452uL, 773588076271081371uL, 13000385682190022254uL, 4966387029739569031uL, 12002158269635560391uL,
|
||||
9180589562619618887uL, 10928066111586602396uL, 15930056384200567420uL, 14219030733708445774uL, 9855367461831697171uL, 5589364474795928413uL, 12718291570760726930uL, 13105010246454705233uL, 9347225068603062217uL, 12895380067559026924uL,
|
||||
795073098915667364uL, 17380094451920307158uL, 12508908074841628259uL, 7690267545386164051uL, 2659693781377578831uL, 13401935256959085896uL, 10589758866316740703uL, 6500344023943830327uL, 16056445859796592957uL, 13837888969188904333uL,
|
||||
13513288218419651638uL, 1589851584549665365uL, 2429422383846896268uL, 6482023338771290230uL, 15410014681647150233uL, 6185590308720085359uL, 12209899281950724511uL, 9606974634150672437uL, 6080806279852835664uL, 2576885667214146546uL,
|
||||
11484734779036570489uL, 6001208834408615917uL, 6836195239104500939uL, 12406411034606736476uL, 15541565663606373863uL, 3080356643708727080uL, 15586050454049992639uL, 2306958247692105907uL, 12043971067315669496uL, 477155333442143874uL,
|
||||
14711407087855869076uL, 386794645880882627uL, 7093418736453091766uL, 4288595764497750464uL, 15199807134400952356uL, 5979776620583929298uL, 14309409966946880271uL, 16143708109906736446uL, 7303480121366170891uL, 10973644831137980356uL,
|
||||
16912616113304036005uL, 11114834129878537914uL, 11286268056177528888uL, 5486956530453307746uL, 16097845240139637506uL, 13790601654062869450uL, 10611216364554256992uL, 683790475565323482uL, 10101463287957875951uL, 8109430964352234527uL,
|
||||
4193092754259694591uL, 4613640514157211771uL, 6875861785975561972uL, 2264473090495966863uL, 18013216012281093427uL, 3498327026982732900uL, 9903891640980875564uL, 15427721694518818776uL, 17288057906222606182uL, 10816721333284587427uL,
|
||||
12300279623953421022uL, 8508793109654811293uL, 5853965616021964435uL, 17530150257591650984uL, 11208102969231252613uL, 2990031287613762665uL
|
||||
},
|
||||
new ulong[256]
|
||||
{
|
||||
9094991057681989827uL, 8606138803235823195uL, 16160543087529014701uL, 9722527943682365778uL, 16797595215276895029uL, 4026406244121087995uL, 10810880735842556538uL, 793466831469952157uL, 17570873574801040652uL, 7069806061322532341uL,
|
||||
9025437343642800970uL, 7902240744518357090uL, 11483956163069641954uL, 17042883494617269171uL, 169507756355412485uL, 12159611706920405785uL, 18050581113201408916uL, 13094669269567229859uL, 8708198706664012748uL, 16371399438833100324uL,
|
||||
14361924315882092926uL, 1669125631761648043uL, 14039613357159666168uL, 6740073072435942682uL, 9627137974267120347uL, 2010954567078870965uL, 17744533894276238730uL, 15208954621100286812uL, 734067863229058321uL, 16730891229222345401uL,
|
||||
11666396599453668737uL, 17212276558031167926uL, 17506990798142313600uL, 919214179616323102uL, 13610264944790049083uL, 3253262042600145346uL, 8178958160323126612uL, 3337947751052939851uL, 18012048573685573661uL, 11417239120312989038uL,
|
||||
14850353257827313638uL, 1052577896389186587uL, 2193586592470914867uL, 2442179127876909020uL, 14667795724615946080uL, 10275106697555981894uL, 5137708932055784227uL, 12218975353350290069uL, 10142868812109927491uL, 4815958689639822245uL,
|
||||
84753878177706380uL, 6315843737766598667uL, 18382000460611982098uL, 3871579785502944766uL, 14543332760762117477uL, 5793075969118851720uL, 249718177605522313uL, 10016556418199002816uL, 17931853270716930449uL, 5498396982908774334uL,
|
||||
12758117762690160297uL, 7667703386526505697uL, 16683173451180408622uL, 9308406252338238417uL, 18135310527241439768uL, 1929022448056995385uL, 1586665506641377319uL, 11971724948115332866uL, 8445678447034858590uL, 7274058453835599610uL,
|
||||
2629305166521655130uL, 15808997092930047048uL, 8787833086900898368uL, 1545819354008644526uL, 3789645467455986802uL, 5856993792710681348uL, 17343191981176845957uL, 2091361543251510458uL, 7459518732372467059uL, 7397325080452181247uL,
|
||||
15335405622202837471uL, 4059487177496226412uL, 559362848516098691uL, 3155811880282733261uL, 339015512710764810uL, 499436355210984207uL, 4260721118560188901uL, 673503137787469464uL, 15081211333024874841uL, 4141949501641595872uL,
|
||||
9781889391086627038uL, 12476916589205824415uL, 5626135598191838651uL, 15420137235268491347uL, 11586150856392483853uL, 14798864416891614319uL, 9563233069935360855uL, 16068176052423353550uL, 5444143179324534077uL, 11152674838723308644uL,
|
||||
609631631123197716uL, 12923016366159787180uL, 5576337977799677490uL, 17125332349745738303uL, 17828665414509968390uL, 14295222528848431346uL, 4387173184941765478uL, 13364476147137460669uL, 13874622463430112253uL, 5708620050009289783uL,
|
||||
6430542613299891216uL, 15458737944500718554uL, 1851664366124113328uL, 13012174097513791023uL, 9392599482526842968uL, 10442066843459281269uL, 17402610465748322057uL, 10690912919127767167uL, 4978440652618840358uL, 4581695294717042927uL,
|
||||
13282578800170846257uL, 3091344129077791553uL, 7147744857192531065uL, 4652093626960253344uL, 17290252874733146170uL, 15016728463536386261uL, 9833286126441041225uL, 11912919544491543691uL, 16242464486312756257uL, 17589663661478784911uL,
|
||||
1346746777927262509uL, 15729327665759993284uL, 2259738658050909887uL, 6111753265489546114uL, 12645228008055390618uL, 16891355708455647420uL, 16563324312999238955uL, 12860835633502684448uL, 7729369582102031213uL, 8912221009216268357uL,
|
||||
4519477316101755235uL, 1281193087194051236uL, 15149005862685217488uL, 10355350241595738058uL, 8118974354974954712uL, 9897718555834973381uL, 8521442237103337431uL, 8066035438451585639uL, 4453855455643222762uL, 5377403871458974897uL,
|
||||
6345848246193028508uL, 6845086483143640723uL, 7988624098924168171uL, 8365446172989525458uL, 1467823520972380706uL, 2935396374752471364uL, 2813067799104965843uL, 10892780281834253302uL, 403485462937118854uL, 2688701935736925910uL,
|
||||
15963727550304450375uL, 14201530205386742651uL, 3468874151188153720uL, 13250621696684830118uL, 1132260792577222039uL, 0uL, 3535028415789053172uL, 2491459977799754837uL, 1003369614229768082uL, 6675627174024062102uL,
|
||||
2029743588748113206uL, 4729507165508558892uL, 3637180897245407101uL, 13952596581111186033uL, 1413426849607831713uL, 12561037251630870550uL, 15570063509623791553uL, 12397269289706770963uL, 2575613213387593177uL, 5320806183970131768uL,
|
||||
11314453079775012583uL, 3422699430205023175uL, 11852957454497512711uL, 11074756040358150125uL, 5071567999032830639uL, 16127596736020452674uL, 15899842574624294603uL, 12691402918954395429uL, 13406940540233951796uL, 16497147783287368871uL,
|
||||
14139300956777540343uL, 10637081362510967548uL, 10227584894573028815uL, 3173053820371464270uL, 15538935446490790486uL, 2362498430710285904uL, 4897844766612488745uL, 6236211556551125383uL, 9224217694939325021uL, 17669330889627426307uL,
|
||||
1774266358176803900uL, 10768348173029132787uL, 13484373595114429368uL, 8283899003267266269uL, 4916772258018216106uL, 16294021496973776808uL, 18249652692721135895uL, 14601621393924980460uL, 18317519790144905886uL, 9472244583004484052uL,
|
||||
14932272457585972842uL, 12314433492482058524uL, 2749198491462122847uL, 10503721769042972921uL, 8966051432100897478uL, 9158911080295248207uL, 14481701886997601513uL, 12036159576530677390uL, 6195919557778536974uL, 5258610333024763572uL,
|
||||
7340795562680270710uL, 7820356866570772974uL, 14721488674053178851uL, 13743694907000058686uL, 1218977307603845928uL, 13172660635703827498uL, 13676381963436941495uL, 5926479337025089677uL, 10996792780356243041uL, 11235172209801846248uL,
|
||||
9956596527230578508uL, 11798704853650126724uL, 16953587156089932080uL, 14122060013263036532uL, 11252270148092827499uL, 7508364738339027172uL, 6905068089466205983uL, 18189706133331673243uL, 3949006037152890487uL, 2995320669031979208uL,
|
||||
11713986572161402376uL, 5985863049541370113uL, 4182723086503020649uL, 7574503472341076328uL, 8348347100700559185uL, 8828052517901670857uL, 6506238233651847065uL, 6937453620375099376uL, 16621540378390793890uL, 3698801050773903089uL,
|
||||
15654197228883127885uL, 7019935873167451772uL, 10570962144843170672uL, 12378340595835246736uL, 13805347633558651570uL, 6586468308675751445uL
|
||||
},
|
||||
new ulong[256]
|
||||
{
|
||||
15001986890517004262uL, 1637721477308921125uL, 12149495623811231944uL, 7646921253694161755uL, 7930897700415732235uL, 7354091399752226219uL, 8517004666874317814uL, 5453187778286554101uL, 17402767330240624456uL, 14251910686351372799uL,
|
||||
10737788899373396171uL, 9883539568181426278uL, 12734457949266690357uL, 11623707008269186684uL, 10170823345874272566uL, 15471862682949014686uL, 3965582442916511115uL, 818990539008627868uL, 6511022660614307004uL, 17126666484504068069uL,
|
||||
15184024750296067534uL, 6797246517703968236uL, 2154964243676750265uL, 12492451583515533452uL, 3398770582681996406uL, 6305239209379988312uL, 4529647259392179007uL, 14544757854385923855uL, 9493043882612360686uL, 11962608485028754448uL,
|
||||
1169952615461543517uL, 3200289132027606858uL, 4147956921301236051uL, 708224122457687732uL, 14832107397699362399uL, 17986746842616076520uL, 11299623869485987455uL, 13594198486351288261uL, 8231768351619019430uL, 8694883466081947438uL,
|
||||
1455760652066973453uL, 9579750024852670338uL, 11015579459201929007uL, 17314613109171068508uL, 15811118054803696506uL, 7243836256093423491uL, 3660620939660078543uL, 2208053939432577669uL, 16834810824707884725uL, 18271848992774824376uL,
|
||||
14417021283697659419uL, 3377712478417560930uL, 17894792228341543420uL, 16945073801847805085uL, 6217894297849621068uL, 7825436358742779939uL, 4987043392780078893uL, 12382858497269484532uL, 13415820639865306301uL, 8746199649574856250uL,
|
||||
869662533239166704uL, 14650457721364100391uL, 16925141562858405257uL, 2911244210510417434uL, 18382312974373876624uL, 11498597901617990979uL, 10828667572279970807uL, 5928147536793457436uL, 11212941572902532115uL, 5738424067781505701uL,
|
||||
4545920254677684779uL, 14357837190168765399uL, 10540746283984138919uL, 15236432095734105306uL, 2538748850867346054uL, 11856293991052060216uL, 13021741769935452261uL, 15990798677628125186uL, 763205940277591432uL, 14016609120497251219uL,
|
||||
10061287064328956494uL, 3093811003563252786uL, 4790004205042634049uL, 8356374131917757362uL, 525807322001048172uL, 9973800865292223322uL, 8060006530596508447uL, 14306696413404433091uL, 4971791225320957497uL, 16356195123205149225uL,
|
||||
11676952182109293888uL, 1866917567049488617uL, 17601962695443621644uL, 4878334347704591445uL, 15577684812609376326uL, 5838476093026002976uL, 16661444707372843629uL, 585131888633540512uL, 4258502335484898171uL, 9216164226481474882uL,
|
||||
1051613531605566680uL, 9311100443926583238uL, 12786619038457767969uL, 7592012931346331751uL, 17784440428065453348uL, 1921896258116084693uL, 3288584640079142494uL, 182885720694182440uL, 10430896929374429327uL, 4347442274373970199uL,
|
||||
12254875670338446048uL, 470303682846092152uL, 15755474228186677870uL, 14073238624926194311uL, 7877737635941138743uL, 18183412781327927468uL, 8981595325954651774uL, 18092225707227040448uL, 13829189599099727097uL, 7536474657577537907uL,
|
||||
14599035985023753267uL, 13539018426536891817uL, 998525038457700324uL, 8121292137418072206uL, 16463535689691560273uL, 10154726272180381730uL, 17216498010023871705uL, 1689003026191473713uL, 6955846591918243539uL, 12968836256081541641uL,
|
||||
10451497052958755227uL, 1104161065046732236uL, 5822238831865856308uL, 7770259743761115727uL, 10722677434777197023uL, 3109098905151763750uL, 7477148998953467071uL, 5270373732550072797uL, 235152363269307196uL, 2264823631868769169uL,
|
||||
11566760878849073000uL, 15293842507372922550uL, 129144564315678996uL, 12435787444463389080uL, 6107963510664673380uL, 2809900646717377218uL, 16044592061068688190uL, 292565944326076752uL, 16550986154605973573uL, 17513004949887526240uL,
|
||||
7062097245496290555uL, 16747641319377612705uL, 14707889229881751371uL, 6021262719684655624uL, 1972895747902540481uL, 3945791491441759391uL, 8801111098641915142uL, 13700503016009418221uL, 14942809973861092471uL, 17799868517652798512uL,
|
||||
5560617129282953357uL, 6890080809738628344uL, 2627701227983280362uL, 9690522075866191274uL, 5539875718724946329uL, 0uL, 17034008114061809393uL, 13077421330682024305uL, 13963290176307300527uL, 7299656588959035031uL,
|
||||
1583357035284794905uL, 13305652357639548565uL, 12200671035189303772uL, 6400302286636641940uL, 6420058603497817984uL, 6691528990365472708uL, 9400350877559778554uL, 15130709207736669938uL, 9038681643377794922uL, 13132277426487405133uL,
|
||||
4440522822651396099uL, 8411535559894538718uL, 16096717416137511466uL, 4238992274866487919uL, 18077078508495971284uL, 13838789949392609723uL, 9599646529712778902uL, 14123892125294942443uL, 5169063728564034821uL, 12670779828505572004uL,
|
||||
10920656821177770723uL, 13361612877962987393uL, 2428230993188407470uL, 3570877924549138163uL, 14885899578566174051uL, 4060646644394037319uL, 3855408526229221283uL, 16641794458759736185uL, 16173936262127370241uL, 8928671747737777170uL,
|
||||
1348049825574685813uL, 3677174894530839771uL, 10353696833961841438uL, 15701199224267946322uL, 8644847306339132130uL, 2719514554751545854uL, 8462570783810646218uL, 1405101508380157281uL, 3478079366649118695uL, 11907751461527015212uL,
|
||||
15861795400831396118uL, 6598086612559222184uL, 12839867613895398173uL, 3768380308409468599uL, 6708048310613262032uL, 1739040292966674941uL, 11410407946413892695uL, 11121233283590249735uL, 11801135715313362004uL, 9091840505278506070uL,
|
||||
9777726215814167742uL, 4700331558701315709uL, 11030972914175796795uL, 4679309188558905193uL, 2030323828767328429uL, 5649811934742993841uL, 415940168518455364uL, 16266875523426953493uL, 17492088132592553076uL, 13255757203528008537uL,
|
||||
7190925116101314031uL, 11320506052158021483uL, 16372326831517072189uL, 15417076028166565794uL, 17693635250630774296uL, 5077496726762238993uL, 2339654594279481786uL, 13646640466407096017uL, 12546315335725685680uL, 18361677116664093316uL,
|
||||
6127437837148246384uL, 3002776577694476046uL, 12090310421094014212uL, 10260721838532639242uL, 1294312070793836361uL, 15527788568662064522uL, 17232911777424223693uL, 5255402455948538057uL, 2517972805691405202uL, 7008429859484506055uL,
|
||||
5361338717396067041uL, 9294722410648223442uL, 8178097563976550810uL, 10629217130044840883uL, 9863923954178969970uL, 2824906557929530326uL
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Gost3411_2012_256Digest : Gost3411_2012Digest
|
||||
{
|
||||
private static readonly byte[] IV = new byte[64]
|
||||
{
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1
|
||||
};
|
||||
|
||||
public override string AlgorithmName => "GOST3411-2012-256";
|
||||
|
||||
public Gost3411_2012_256Digest()
|
||||
: base(IV)
|
||||
{
|
||||
}
|
||||
|
||||
public Gost3411_2012_256Digest(Gost3411_2012_256Digest other)
|
||||
: base(IV)
|
||||
{
|
||||
Reset(other);
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
byte[] array = new byte[64];
|
||||
base.DoFinal(array, 0);
|
||||
Array.Copy(array, 32, output, outOff, 32);
|
||||
return 32;
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new Gost3411_2012_256Digest(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Gost3411_2012_512Digest : Gost3411_2012Digest
|
||||
{
|
||||
private static readonly byte[] IV;
|
||||
|
||||
public override string AlgorithmName => "GOST3411-2012-512";
|
||||
|
||||
public Gost3411_2012_512Digest()
|
||||
: base(IV)
|
||||
{
|
||||
}
|
||||
|
||||
public Gost3411_2012_512Digest(Gost3411_2012_512Digest other)
|
||||
: base(IV)
|
||||
{
|
||||
Reset(other);
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new Gost3411_2012_512Digest(this);
|
||||
}
|
||||
|
||||
static Gost3411_2012_512Digest()
|
||||
{
|
||||
byte[] iV = new byte[64];
|
||||
IV = iV;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,435 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class KeccakDigest : IDigest, IMemoable
|
||||
{
|
||||
private static readonly ulong[] KeccakRoundConstants = new ulong[24]
|
||||
{
|
||||
1uL, 32898uL, 9223372036854808714uL, 9223372039002292224uL, 32907uL, 2147483649uL, 9223372039002292353uL, 9223372036854808585uL, 138uL, 136uL,
|
||||
2147516425uL, 2147483658uL, 2147516555uL, 9223372036854775947uL, 9223372036854808713uL, 9223372036854808579uL, 9223372036854808578uL, 9223372036854775936uL, 32778uL, 9223372039002259466uL,
|
||||
9223372039002292353uL, 9223372036854808704uL, 2147483649uL, 9223372039002292232uL
|
||||
};
|
||||
|
||||
private ulong[] state = new ulong[25];
|
||||
|
||||
protected byte[] dataQueue = new byte[192];
|
||||
|
||||
protected int rate;
|
||||
|
||||
protected int bitsInQueue;
|
||||
|
||||
protected int fixedOutputLength;
|
||||
|
||||
protected bool squeezing;
|
||||
|
||||
public virtual string AlgorithmName => "Keccak-" + fixedOutputLength;
|
||||
|
||||
public KeccakDigest()
|
||||
: this(288)
|
||||
{
|
||||
}
|
||||
|
||||
public KeccakDigest(int bitLength)
|
||||
{
|
||||
Init(bitLength);
|
||||
}
|
||||
|
||||
public KeccakDigest(KeccakDigest source)
|
||||
{
|
||||
CopyIn(source);
|
||||
}
|
||||
|
||||
private void CopyIn(KeccakDigest source)
|
||||
{
|
||||
Array.Copy(source.state, 0, state, 0, source.state.Length);
|
||||
Array.Copy(source.dataQueue, 0, dataQueue, 0, source.dataQueue.Length);
|
||||
rate = source.rate;
|
||||
bitsInQueue = source.bitsInQueue;
|
||||
fixedOutputLength = source.fixedOutputLength;
|
||||
squeezing = source.squeezing;
|
||||
}
|
||||
|
||||
public virtual int GetDigestSize()
|
||||
{
|
||||
return fixedOutputLength >> 3;
|
||||
}
|
||||
|
||||
public virtual void Update(byte input)
|
||||
{
|
||||
Absorb(new byte[1] { input }, 0, 1);
|
||||
}
|
||||
|
||||
public virtual void BlockUpdate(byte[] input, int inOff, int len)
|
||||
{
|
||||
Absorb(input, inOff, len);
|
||||
}
|
||||
|
||||
public virtual int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Squeeze(output, outOff, fixedOutputLength);
|
||||
Reset();
|
||||
return GetDigestSize();
|
||||
}
|
||||
|
||||
protected virtual int DoFinal(byte[] output, int outOff, byte partialByte, int partialBits)
|
||||
{
|
||||
if (partialBits > 0)
|
||||
{
|
||||
AbsorbBits(partialByte, partialBits);
|
||||
}
|
||||
Squeeze(output, outOff, fixedOutputLength);
|
||||
Reset();
|
||||
return GetDigestSize();
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
Init(fixedOutputLength);
|
||||
}
|
||||
|
||||
public virtual int GetByteLength()
|
||||
{
|
||||
return rate >> 3;
|
||||
}
|
||||
|
||||
private void Init(int bitLength)
|
||||
{
|
||||
switch (bitLength)
|
||||
{
|
||||
case 128:
|
||||
case 224:
|
||||
case 256:
|
||||
case 288:
|
||||
case 384:
|
||||
case 512:
|
||||
InitSponge(1600 - (bitLength << 1));
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("must be one of 128, 224, 256, 288, 384, or 512.", "bitLength");
|
||||
}
|
||||
}
|
||||
|
||||
private void InitSponge(int rate)
|
||||
{
|
||||
if (rate <= 0 || rate >= 1600 || (rate & 0x3F) != 0)
|
||||
{
|
||||
throw new InvalidOperationException("invalid rate value");
|
||||
}
|
||||
this.rate = rate;
|
||||
Array.Clear(state, 0, state.Length);
|
||||
Arrays.Fill(dataQueue, 0);
|
||||
bitsInQueue = 0;
|
||||
squeezing = false;
|
||||
fixedOutputLength = 1600 - rate >> 1;
|
||||
}
|
||||
|
||||
protected void Absorb(byte[] data, int off, int len)
|
||||
{
|
||||
if ((bitsInQueue & 7) != 0)
|
||||
{
|
||||
throw new InvalidOperationException("attempt to absorb with odd length queue");
|
||||
}
|
||||
if (squeezing)
|
||||
{
|
||||
throw new InvalidOperationException("attempt to absorb while squeezing");
|
||||
}
|
||||
int num = bitsInQueue >> 3;
|
||||
int num2 = rate >> 3;
|
||||
int num3 = 0;
|
||||
while (num3 < len)
|
||||
{
|
||||
if (num == 0 && num3 <= len - num2)
|
||||
{
|
||||
do
|
||||
{
|
||||
KeccakAbsorb(data, off + num3);
|
||||
num3 += num2;
|
||||
}
|
||||
while (num3 <= len - num2);
|
||||
continue;
|
||||
}
|
||||
int num4 = System.Math.Min(num2 - num, len - num3);
|
||||
Array.Copy(data, off + num3, dataQueue, num, num4);
|
||||
num += num4;
|
||||
num3 += num4;
|
||||
if (num == num2)
|
||||
{
|
||||
KeccakAbsorb(dataQueue, 0);
|
||||
num = 0;
|
||||
}
|
||||
}
|
||||
bitsInQueue = num << 3;
|
||||
}
|
||||
|
||||
protected void AbsorbBits(int data, int bits)
|
||||
{
|
||||
if (bits < 1 || bits > 7)
|
||||
{
|
||||
throw new ArgumentException("must be in the range 1 to 7", "bits");
|
||||
}
|
||||
if ((bitsInQueue & 7) != 0)
|
||||
{
|
||||
throw new InvalidOperationException("attempt to absorb with odd length queue");
|
||||
}
|
||||
if (squeezing)
|
||||
{
|
||||
throw new InvalidOperationException("attempt to absorb while squeezing");
|
||||
}
|
||||
int num = (1 << bits) - 1;
|
||||
dataQueue[bitsInQueue >> 3] = (byte)(data & num);
|
||||
bitsInQueue += bits;
|
||||
}
|
||||
|
||||
private void PadAndSwitchToSqueezingPhase()
|
||||
{
|
||||
byte[] array2;
|
||||
byte[] array = (array2 = dataQueue);
|
||||
int num = bitsInQueue >> 3;
|
||||
nint num2 = num;
|
||||
array[num] = (byte)(array2[num2] | (byte)(1 << (bitsInQueue & 7)));
|
||||
if (++bitsInQueue == rate)
|
||||
{
|
||||
KeccakAbsorb(dataQueue, 0);
|
||||
bitsInQueue = 0;
|
||||
}
|
||||
int num3 = bitsInQueue >> 6;
|
||||
int num4 = bitsInQueue & 0x3F;
|
||||
int num5 = 0;
|
||||
ulong[] array4;
|
||||
for (int i = 0; i < num3; i++)
|
||||
{
|
||||
ulong[] array3 = (array4 = state);
|
||||
int num6 = i;
|
||||
num2 = num6;
|
||||
array3[num6] = array4[num2] ^ Pack.LE_To_UInt64(dataQueue, num5);
|
||||
num5 += 8;
|
||||
}
|
||||
if (num4 > 0)
|
||||
{
|
||||
ulong num7 = (ulong)((1L << num4) - 1);
|
||||
ulong[] array5 = (array4 = state);
|
||||
num2 = num3;
|
||||
array5[num3] = array4[num2] ^ (Pack.LE_To_UInt64(dataQueue, num5) & num7);
|
||||
}
|
||||
ulong[] array6 = (array4 = state);
|
||||
int num8 = rate - 1 >> 6;
|
||||
num2 = num8;
|
||||
array6[num8] = array4[num2] ^ 0x8000000000000000uL;
|
||||
KeccakPermutation();
|
||||
KeccakExtract();
|
||||
bitsInQueue = rate;
|
||||
squeezing = true;
|
||||
}
|
||||
|
||||
protected void Squeeze(byte[] output, int offset, long outputLength)
|
||||
{
|
||||
if (!squeezing)
|
||||
{
|
||||
PadAndSwitchToSqueezingPhase();
|
||||
}
|
||||
if ((outputLength & 7) != 0)
|
||||
{
|
||||
throw new InvalidOperationException("outputLength not a multiple of 8");
|
||||
}
|
||||
int num2;
|
||||
for (long num = 0L; num < outputLength; num += num2)
|
||||
{
|
||||
if (bitsInQueue == 0)
|
||||
{
|
||||
KeccakPermutation();
|
||||
KeccakExtract();
|
||||
bitsInQueue = rate;
|
||||
}
|
||||
num2 = (int)System.Math.Min(bitsInQueue, outputLength - num);
|
||||
Array.Copy(dataQueue, rate - bitsInQueue >> 3, output, offset + (int)(num >> 3), num2 >> 3);
|
||||
bitsInQueue -= num2;
|
||||
}
|
||||
}
|
||||
|
||||
private void KeccakAbsorb(byte[] data, int off)
|
||||
{
|
||||
int num = rate >> 6;
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
ulong[] array2;
|
||||
ulong[] array = (array2 = state);
|
||||
int num2 = i;
|
||||
nint num3 = num2;
|
||||
array[num2] = array2[num3] ^ Pack.LE_To_UInt64(data, off);
|
||||
off += 8;
|
||||
}
|
||||
KeccakPermutation();
|
||||
}
|
||||
|
||||
private void KeccakExtract()
|
||||
{
|
||||
Pack.UInt64_To_LE(state, 0, rate >> 6, dataQueue, 0);
|
||||
}
|
||||
|
||||
private void KeccakPermutation()
|
||||
{
|
||||
ulong[] array = state;
|
||||
ulong num = array[0];
|
||||
ulong num2 = array[1];
|
||||
ulong num3 = array[2];
|
||||
ulong num4 = array[3];
|
||||
ulong num5 = array[4];
|
||||
ulong num6 = array[5];
|
||||
ulong num7 = array[6];
|
||||
ulong num8 = array[7];
|
||||
ulong num9 = array[8];
|
||||
ulong num10 = array[9];
|
||||
ulong num11 = array[10];
|
||||
ulong num12 = array[11];
|
||||
ulong num13 = array[12];
|
||||
ulong num14 = array[13];
|
||||
ulong num15 = array[14];
|
||||
ulong num16 = array[15];
|
||||
ulong num17 = array[16];
|
||||
ulong num18 = array[17];
|
||||
ulong num19 = array[18];
|
||||
ulong num20 = array[19];
|
||||
ulong num21 = array[20];
|
||||
ulong num22 = array[21];
|
||||
ulong num23 = array[22];
|
||||
ulong num24 = array[23];
|
||||
ulong num25 = array[24];
|
||||
for (int i = 0; i < 24; i++)
|
||||
{
|
||||
ulong num26 = num ^ num6 ^ num11 ^ num16 ^ num21;
|
||||
ulong num27 = num2 ^ num7 ^ num12 ^ num17 ^ num22;
|
||||
ulong num28 = num3 ^ num8 ^ num13 ^ num18 ^ num23;
|
||||
ulong num29 = num4 ^ num9 ^ num14 ^ num19 ^ num24;
|
||||
ulong num30 = num5 ^ num10 ^ num15 ^ num20 ^ num25;
|
||||
ulong num31 = ((num27 << 1) | (num27 >> 63)) ^ num30;
|
||||
ulong num32 = ((num28 << 1) | (num28 >> 63)) ^ num26;
|
||||
ulong num33 = ((num29 << 1) | (num29 >> 63)) ^ num27;
|
||||
ulong num34 = ((num30 << 1) | (num30 >> 63)) ^ num28;
|
||||
ulong num35 = ((num26 << 1) | (num26 >> 63)) ^ num29;
|
||||
num ^= num31;
|
||||
num6 ^= num31;
|
||||
num11 ^= num31;
|
||||
num16 ^= num31;
|
||||
num21 ^= num31;
|
||||
num2 ^= num32;
|
||||
num7 ^= num32;
|
||||
num12 ^= num32;
|
||||
num17 ^= num32;
|
||||
num22 ^= num32;
|
||||
num3 ^= num33;
|
||||
num8 ^= num33;
|
||||
num13 ^= num33;
|
||||
num18 ^= num33;
|
||||
num23 ^= num33;
|
||||
num4 ^= num34;
|
||||
num9 ^= num34;
|
||||
num14 ^= num34;
|
||||
num19 ^= num34;
|
||||
num24 ^= num34;
|
||||
num5 ^= num35;
|
||||
num10 ^= num35;
|
||||
num15 ^= num35;
|
||||
num20 ^= num35;
|
||||
num25 ^= num35;
|
||||
num27 = (num2 << 1) | (num2 >> 63);
|
||||
num2 = (num7 << 44) | (num7 >> 20);
|
||||
num7 = (num10 << 20) | (num10 >> 44);
|
||||
num10 = (num23 << 61) | (num23 >> 3);
|
||||
num23 = (num15 << 39) | (num15 >> 25);
|
||||
num15 = (num21 << 18) | (num21 >> 46);
|
||||
num21 = (num3 << 62) | (num3 >> 2);
|
||||
num3 = (num13 << 43) | (num13 >> 21);
|
||||
num13 = (num14 << 25) | (num14 >> 39);
|
||||
num14 = (num20 << 8) | (num20 >> 56);
|
||||
num20 = (num24 << 56) | (num24 >> 8);
|
||||
num24 = (num16 << 41) | (num16 >> 23);
|
||||
num16 = (num5 << 27) | (num5 >> 37);
|
||||
num5 = (num25 << 14) | (num25 >> 50);
|
||||
num25 = (num22 << 2) | (num22 >> 62);
|
||||
num22 = (num9 << 55) | (num9 >> 9);
|
||||
num9 = (num17 << 45) | (num17 >> 19);
|
||||
num17 = (num6 << 36) | (num6 >> 28);
|
||||
num6 = (num4 << 28) | (num4 >> 36);
|
||||
num4 = (num19 << 21) | (num19 >> 43);
|
||||
num19 = (num18 << 15) | (num18 >> 49);
|
||||
num18 = (num12 << 10) | (num12 >> 54);
|
||||
num12 = (num8 << 6) | (num8 >> 58);
|
||||
num8 = (num11 << 3) | (num11 >> 61);
|
||||
num11 = num27;
|
||||
num26 = num ^ (~num2 & num3);
|
||||
num27 = num2 ^ (~num3 & num4);
|
||||
num3 ^= ~num4 & num5;
|
||||
num4 ^= ~num5 & num;
|
||||
num5 ^= ~num & num2;
|
||||
num = num26;
|
||||
num2 = num27;
|
||||
num26 = num6 ^ (~num7 & num8);
|
||||
num27 = num7 ^ (~num8 & num9);
|
||||
num8 ^= ~num9 & num10;
|
||||
num9 ^= ~num10 & num6;
|
||||
num10 ^= ~num6 & num7;
|
||||
num6 = num26;
|
||||
num7 = num27;
|
||||
num26 = num11 ^ (~num12 & num13);
|
||||
num27 = num12 ^ (~num13 & num14);
|
||||
num13 ^= ~num14 & num15;
|
||||
num14 ^= ~num15 & num11;
|
||||
num15 ^= ~num11 & num12;
|
||||
num11 = num26;
|
||||
num12 = num27;
|
||||
num26 = num16 ^ (~num17 & num18);
|
||||
num27 = num17 ^ (~num18 & num19);
|
||||
num18 ^= ~num19 & num20;
|
||||
num19 ^= ~num20 & num16;
|
||||
num20 ^= ~num16 & num17;
|
||||
num16 = num26;
|
||||
num17 = num27;
|
||||
num26 = num21 ^ (~num22 & num23);
|
||||
num27 = num22 ^ (~num23 & num24);
|
||||
num23 ^= ~num24 & num25;
|
||||
num24 ^= ~num25 & num21;
|
||||
num25 ^= ~num21 & num22;
|
||||
num21 = num26;
|
||||
num22 = num27;
|
||||
num ^= KeccakRoundConstants[i];
|
||||
}
|
||||
array[0] = num;
|
||||
array[1] = num2;
|
||||
array[2] = num3;
|
||||
array[3] = num4;
|
||||
array[4] = num5;
|
||||
array[5] = num6;
|
||||
array[6] = num7;
|
||||
array[7] = num8;
|
||||
array[8] = num9;
|
||||
array[9] = num10;
|
||||
array[10] = num11;
|
||||
array[11] = num12;
|
||||
array[12] = num13;
|
||||
array[13] = num14;
|
||||
array[14] = num15;
|
||||
array[15] = num16;
|
||||
array[16] = num17;
|
||||
array[17] = num18;
|
||||
array[18] = num19;
|
||||
array[19] = num20;
|
||||
array[20] = num21;
|
||||
array[21] = num22;
|
||||
array[22] = num23;
|
||||
array[23] = num24;
|
||||
array[24] = num25;
|
||||
}
|
||||
|
||||
public virtual IMemoable Copy()
|
||||
{
|
||||
return new KeccakDigest(this);
|
||||
}
|
||||
|
||||
public virtual void Reset(IMemoable other)
|
||||
{
|
||||
CopyIn((KeccakDigest)other);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,269 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public abstract class LongDigest : IDigest, IMemoable
|
||||
{
|
||||
private int MyByteLength = 128;
|
||||
|
||||
private byte[] xBuf;
|
||||
|
||||
private int xBufOff;
|
||||
|
||||
private long byteCount1;
|
||||
|
||||
private long byteCount2;
|
||||
|
||||
internal ulong H1;
|
||||
|
||||
internal ulong H2;
|
||||
|
||||
internal ulong H3;
|
||||
|
||||
internal ulong H4;
|
||||
|
||||
internal ulong H5;
|
||||
|
||||
internal ulong H6;
|
||||
|
||||
internal ulong H7;
|
||||
|
||||
internal ulong H8;
|
||||
|
||||
private ulong[] W = new ulong[80];
|
||||
|
||||
private int wOff;
|
||||
|
||||
internal static readonly ulong[] K = new ulong[80]
|
||||
{
|
||||
4794697086780616226uL, 8158064640168781261uL, 13096744586834688815uL, 16840607885511220156uL, 4131703408338449720uL, 6480981068601479193uL, 10538285296894168987uL, 12329834152419229976uL, 15566598209576043074uL, 1334009975649890238uL,
|
||||
2608012711638119052uL, 6128411473006802146uL, 8268148722764581231uL, 9286055187155687089uL, 11230858885718282805uL, 13951009754708518548uL, 16472876342353939154uL, 17275323862435702243uL, 1135362057144423861uL, 2597628984639134821uL,
|
||||
3308224258029322869uL, 5365058923640841347uL, 6679025012923562964uL, 8573033837759648693uL, 10970295158949994411uL, 12119686244451234320uL, 12683024718118986047uL, 13788192230050041572uL, 14330467153632333762uL, 15395433587784984357uL,
|
||||
489312712824947311uL, 1452737877330783856uL, 2861767655752347644uL, 3322285676063803686uL, 5560940570517711597uL, 5996557281743188959uL, 7280758554555802590uL, 8532644243296465576uL, 9350256976987008742uL, 10552545826968843579uL,
|
||||
11727347734174303076uL, 12113106623233404929uL, 14000437183269869457uL, 14369950271660146224uL, 15101387698204529176uL, 15463397548674623760uL, 17586052441742319658uL, 1182934255886127544uL, 1847814050463011016uL, 2177327727835720531uL,
|
||||
2830643537854262169uL, 3796741975233480872uL, 4115178125766777443uL, 5681478168544905931uL, 6601373596472566643uL, 7507060721942968483uL, 8399075790359081724uL, 8693463985226723168uL, 9568029438360202098uL, 10144078919501101548uL,
|
||||
10430055236837252648uL, 11840083180663258601uL, 13761210420658862357uL, 14299343276471374635uL, 14566680578165727644uL, 15097957966210449927uL, 16922976911328602910uL, 17689382322260857208uL, 500013540394364858uL, 748580250866718886uL,
|
||||
1242879168328830382uL, 1977374033974150939uL, 2944078676154940804uL, 3659926193048069267uL, 4368137639120453308uL, 4836135668995329356uL, 5532061633213252278uL, 6448918945643986474uL, 6902733635092675308uL, 7801388544844847127uL
|
||||
};
|
||||
|
||||
public abstract string AlgorithmName { get; }
|
||||
|
||||
internal LongDigest()
|
||||
{
|
||||
xBuf = new byte[8];
|
||||
Reset();
|
||||
}
|
||||
|
||||
internal LongDigest(LongDigest t)
|
||||
{
|
||||
xBuf = new byte[t.xBuf.Length];
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
protected void CopyIn(LongDigest t)
|
||||
{
|
||||
Array.Copy(t.xBuf, 0, xBuf, 0, t.xBuf.Length);
|
||||
xBufOff = t.xBufOff;
|
||||
byteCount1 = t.byteCount1;
|
||||
byteCount2 = t.byteCount2;
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
H4 = t.H4;
|
||||
H5 = t.H5;
|
||||
H6 = t.H6;
|
||||
H7 = t.H7;
|
||||
H8 = t.H8;
|
||||
Array.Copy(t.W, 0, W, 0, t.W.Length);
|
||||
wOff = t.wOff;
|
||||
}
|
||||
|
||||
public void Update(byte input)
|
||||
{
|
||||
xBuf[xBufOff++] = input;
|
||||
if (xBufOff == xBuf.Length)
|
||||
{
|
||||
ProcessWord(xBuf, 0);
|
||||
xBufOff = 0;
|
||||
}
|
||||
byteCount1++;
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] input, int inOff, int length)
|
||||
{
|
||||
while (xBufOff != 0 && length > 0)
|
||||
{
|
||||
Update(input[inOff]);
|
||||
inOff++;
|
||||
length--;
|
||||
}
|
||||
while (length > xBuf.Length)
|
||||
{
|
||||
ProcessWord(input, inOff);
|
||||
inOff += xBuf.Length;
|
||||
length -= xBuf.Length;
|
||||
byteCount1 += xBuf.Length;
|
||||
}
|
||||
while (length > 0)
|
||||
{
|
||||
Update(input[inOff]);
|
||||
inOff++;
|
||||
length--;
|
||||
}
|
||||
}
|
||||
|
||||
public void Finish()
|
||||
{
|
||||
AdjustByteCounts();
|
||||
long lowW = byteCount1 << 3;
|
||||
long hiW = byteCount2;
|
||||
Update(128);
|
||||
while (xBufOff != 0)
|
||||
{
|
||||
Update(0);
|
||||
}
|
||||
ProcessLength(lowW, hiW);
|
||||
ProcessBlock();
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
byteCount1 = 0L;
|
||||
byteCount2 = 0L;
|
||||
xBufOff = 0;
|
||||
for (int i = 0; i < xBuf.Length; i++)
|
||||
{
|
||||
xBuf[i] = 0;
|
||||
}
|
||||
wOff = 0;
|
||||
Array.Clear(W, 0, W.Length);
|
||||
}
|
||||
|
||||
internal void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
W[wOff] = Pack.BE_To_UInt64(input, inOff);
|
||||
if (++wOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void AdjustByteCounts()
|
||||
{
|
||||
if (byteCount1 > 2305843009213693951L)
|
||||
{
|
||||
byteCount2 += byteCount1 >>> 61;
|
||||
byteCount1 &= 2305843009213693951L;
|
||||
}
|
||||
}
|
||||
|
||||
internal void ProcessLength(long lowW, long hiW)
|
||||
{
|
||||
if (wOff > 14)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
W[14] = (ulong)hiW;
|
||||
W[15] = (ulong)lowW;
|
||||
}
|
||||
|
||||
internal void ProcessBlock()
|
||||
{
|
||||
AdjustByteCounts();
|
||||
for (int i = 16; i <= 79; i++)
|
||||
{
|
||||
W[i] = Sigma1(W[i - 2]) + W[i - 7] + Sigma0(W[i - 15]) + W[i - 16];
|
||||
}
|
||||
ulong num = H1;
|
||||
ulong num2 = H2;
|
||||
ulong num3 = H3;
|
||||
ulong num4 = H4;
|
||||
ulong num5 = H5;
|
||||
ulong num6 = H6;
|
||||
ulong num7 = H7;
|
||||
ulong num8 = H8;
|
||||
int num9 = 0;
|
||||
for (int j = 0; j < 10; j++)
|
||||
{
|
||||
num8 += Sum1(num5) + Ch(num5, num6, num7) + K[num9] + W[num9++];
|
||||
num4 += num8;
|
||||
num8 += Sum0(num) + Maj(num, num2, num3);
|
||||
num7 += Sum1(num4) + Ch(num4, num5, num6) + K[num9] + W[num9++];
|
||||
num3 += num7;
|
||||
num7 += Sum0(num8) + Maj(num8, num, num2);
|
||||
num6 += Sum1(num3) + Ch(num3, num4, num5) + K[num9] + W[num9++];
|
||||
num2 += num6;
|
||||
num6 += Sum0(num7) + Maj(num7, num8, num);
|
||||
num5 += Sum1(num2) + Ch(num2, num3, num4) + K[num9] + W[num9++];
|
||||
num += num5;
|
||||
num5 += Sum0(num6) + Maj(num6, num7, num8);
|
||||
num4 += Sum1(num) + Ch(num, num2, num3) + K[num9] + W[num9++];
|
||||
num8 += num4;
|
||||
num4 += Sum0(num5) + Maj(num5, num6, num7);
|
||||
num3 += Sum1(num8) + Ch(num8, num, num2) + K[num9] + W[num9++];
|
||||
num7 += num3;
|
||||
num3 += Sum0(num4) + Maj(num4, num5, num6);
|
||||
num2 += Sum1(num7) + Ch(num7, num8, num) + K[num9] + W[num9++];
|
||||
num6 += num2;
|
||||
num2 += Sum0(num3) + Maj(num3, num4, num5);
|
||||
num += Sum1(num6) + Ch(num6, num7, num8) + K[num9] + W[num9++];
|
||||
num5 += num;
|
||||
num += Sum0(num2) + Maj(num2, num3, num4);
|
||||
}
|
||||
H1 += num;
|
||||
H2 += num2;
|
||||
H3 += num3;
|
||||
H4 += num4;
|
||||
H5 += num5;
|
||||
H6 += num6;
|
||||
H7 += num7;
|
||||
H8 += num8;
|
||||
wOff = 0;
|
||||
Array.Clear(W, 0, 16);
|
||||
}
|
||||
|
||||
private static ulong Ch(ulong x, ulong y, ulong z)
|
||||
{
|
||||
return (x & y) ^ (~x & z);
|
||||
}
|
||||
|
||||
private static ulong Maj(ulong x, ulong y, ulong z)
|
||||
{
|
||||
return (x & y) ^ (x & z) ^ (y & z);
|
||||
}
|
||||
|
||||
private static ulong Sum0(ulong x)
|
||||
{
|
||||
return ((x << 36) | (x >> 28)) ^ ((x << 30) | (x >> 34)) ^ ((x << 25) | (x >> 39));
|
||||
}
|
||||
|
||||
private static ulong Sum1(ulong x)
|
||||
{
|
||||
return ((x << 50) | (x >> 14)) ^ ((x << 46) | (x >> 18)) ^ ((x << 23) | (x >> 41));
|
||||
}
|
||||
|
||||
private static ulong Sigma0(ulong x)
|
||||
{
|
||||
return ((x << 63) | (x >> 1)) ^ ((x << 56) | (x >> 8)) ^ (x >> 7);
|
||||
}
|
||||
|
||||
private static ulong Sigma1(ulong x)
|
||||
{
|
||||
return ((x << 45) | (x >> 19)) ^ ((x << 3) | (x >> 61)) ^ (x >> 6);
|
||||
}
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return MyByteLength;
|
||||
}
|
||||
|
||||
public abstract int GetDigestSize();
|
||||
|
||||
public abstract int DoFinal(byte[] output, int outOff);
|
||||
|
||||
public abstract IMemoable Copy();
|
||||
|
||||
public abstract void Reset(IMemoable t);
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class MD2Digest : IDigest, IMemoable
|
||||
{
|
||||
private const int DigestLength = 16;
|
||||
|
||||
private const int BYTE_LENGTH = 16;
|
||||
|
||||
private byte[] X = new byte[48];
|
||||
|
||||
private int xOff;
|
||||
|
||||
private byte[] M = new byte[16];
|
||||
|
||||
private int mOff;
|
||||
|
||||
private byte[] C = new byte[16];
|
||||
|
||||
private int COff;
|
||||
|
||||
private static readonly byte[] S = new byte[256]
|
||||
{
|
||||
41, 46, 67, 201, 162, 216, 124, 1, 61, 54,
|
||||
84, 161, 236, 240, 6, 19, 98, 167, 5, 243,
|
||||
192, 199, 115, 140, 152, 147, 43, 217, 188, 76,
|
||||
130, 202, 30, 155, 87, 60, 253, 212, 224, 22,
|
||||
103, 66, 111, 24, 138, 23, 229, 18, 190, 78,
|
||||
196, 214, 218, 158, 222, 73, 160, 251, 245, 142,
|
||||
187, 47, 238, 122, 169, 104, 121, 145, 21, 178,
|
||||
7, 63, 148, 194, 16, 137, 11, 34, 95, 33,
|
||||
128, 127, 93, 154, 90, 144, 50, 39, 53, 62,
|
||||
204, 231, 191, 247, 151, 3, 255, 25, 48, 179,
|
||||
72, 165, 181, 209, 215, 94, 146, 42, 172, 86,
|
||||
170, 198, 79, 184, 56, 210, 150, 164, 125, 182,
|
||||
118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
|
||||
112, 89, 100, 113, 135, 32, 134, 91, 207, 101,
|
||||
230, 45, 168, 2, 27, 96, 37, 173, 174, 176,
|
||||
185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
|
||||
85, 71, 163, 35, 221, 81, 175, 58, 195, 92,
|
||||
249, 206, 186, 197, 234, 38, 44, 83, 13, 110,
|
||||
133, 40, 132, 9, 211, 223, 205, 244, 65, 129,
|
||||
77, 82, 106, 220, 55, 200, 108, 193, 171, 250,
|
||||
36, 225, 123, 8, 12, 189, 177, 74, 120, 136,
|
||||
149, 139, 227, 99, 232, 109, 233, 203, 213, 254,
|
||||
59, 0, 29, 57, 242, 239, 183, 14, 102, 88,
|
||||
208, 228, 166, 119, 114, 248, 235, 117, 75, 10,
|
||||
49, 68, 80, 180, 143, 237, 31, 26, 219, 153,
|
||||
141, 51, 159, 17, 131, 20
|
||||
};
|
||||
|
||||
public string AlgorithmName => "MD2";
|
||||
|
||||
public MD2Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public MD2Digest(MD2Digest t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(MD2Digest t)
|
||||
{
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
Array.Copy(t.M, 0, M, 0, t.M.Length);
|
||||
mOff = t.mOff;
|
||||
Array.Copy(t.C, 0, C, 0, t.C.Length);
|
||||
COff = t.COff;
|
||||
}
|
||||
|
||||
public int GetDigestSize()
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
|
||||
public int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
byte b = (byte)(M.Length - mOff);
|
||||
for (int i = mOff; i < M.Length; i++)
|
||||
{
|
||||
M[i] = b;
|
||||
}
|
||||
ProcessChecksum(M);
|
||||
ProcessBlock(M);
|
||||
ProcessBlock(C);
|
||||
Array.Copy(X, xOff, output, outOff, 16);
|
||||
Reset();
|
||||
return 16;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
mOff = 0;
|
||||
for (int j = 0; j != M.Length; j++)
|
||||
{
|
||||
M[j] = 0;
|
||||
}
|
||||
COff = 0;
|
||||
for (int k = 0; k != C.Length; k++)
|
||||
{
|
||||
C[k] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(byte input)
|
||||
{
|
||||
M[mOff++] = input;
|
||||
if (mOff == 16)
|
||||
{
|
||||
ProcessChecksum(M);
|
||||
ProcessBlock(M);
|
||||
mOff = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] input, int inOff, int length)
|
||||
{
|
||||
while (mOff != 0 && length > 0)
|
||||
{
|
||||
Update(input[inOff]);
|
||||
inOff++;
|
||||
length--;
|
||||
}
|
||||
while (length > 16)
|
||||
{
|
||||
Array.Copy(input, inOff, M, 0, 16);
|
||||
ProcessChecksum(M);
|
||||
ProcessBlock(M);
|
||||
length -= 16;
|
||||
inOff += 16;
|
||||
}
|
||||
while (length > 0)
|
||||
{
|
||||
Update(input[inOff]);
|
||||
inOff++;
|
||||
length--;
|
||||
}
|
||||
}
|
||||
|
||||
internal void ProcessChecksum(byte[] m)
|
||||
{
|
||||
int num = C[15];
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
byte[] c;
|
||||
byte[] array = (c = C);
|
||||
int num2 = i;
|
||||
nint num3 = num2;
|
||||
array[num2] = (byte)(c[num3] ^ S[(m[i] ^ num) & 0xFF]);
|
||||
num = C[i];
|
||||
}
|
||||
}
|
||||
|
||||
internal void ProcessBlock(byte[] m)
|
||||
{
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
X[i + 16] = m[i];
|
||||
X[i + 32] = (byte)(m[i] ^ X[i]);
|
||||
}
|
||||
int num = 0;
|
||||
for (int j = 0; j < 18; j++)
|
||||
{
|
||||
for (int k = 0; k < 48; k++)
|
||||
{
|
||||
byte[] x;
|
||||
byte[] array = (x = X);
|
||||
int num2 = k;
|
||||
nint num3 = num2;
|
||||
num = (array[num2] = (byte)(x[num3] ^ S[num])) & 0xFF;
|
||||
}
|
||||
num = (num + j) % 256;
|
||||
}
|
||||
}
|
||||
|
||||
public IMemoable Copy()
|
||||
{
|
||||
return new MD2Digest(this);
|
||||
}
|
||||
|
||||
public void Reset(IMemoable other)
|
||||
{
|
||||
MD2Digest t = (MD2Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class MD4Digest : GeneralDigest
|
||||
{
|
||||
private const int DigestLength = 16;
|
||||
|
||||
private const int S11 = 3;
|
||||
|
||||
private const int S12 = 7;
|
||||
|
||||
private const int S13 = 11;
|
||||
|
||||
private const int S14 = 19;
|
||||
|
||||
private const int S21 = 3;
|
||||
|
||||
private const int S22 = 5;
|
||||
|
||||
private const int S23 = 9;
|
||||
|
||||
private const int S24 = 13;
|
||||
|
||||
private const int S31 = 3;
|
||||
|
||||
private const int S32 = 9;
|
||||
|
||||
private const int S33 = 11;
|
||||
|
||||
private const int S34 = 15;
|
||||
|
||||
private int H1;
|
||||
|
||||
private int H2;
|
||||
|
||||
private int H3;
|
||||
|
||||
private int H4;
|
||||
|
||||
private int[] X = new int[16];
|
||||
|
||||
private int xOff;
|
||||
|
||||
public override string AlgorithmName => "MD4";
|
||||
|
||||
public MD4Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public MD4Digest(MD4Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(MD4Digest t)
|
||||
{
|
||||
CopyIn((GeneralDigest)t);
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
H4 = t.H4;
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
X[xOff++] = (input[inOff] & 0xFF) | ((input[inOff + 1] & 0xFF) << 8) | ((input[inOff + 2] & 0xFF) << 16) | ((input[inOff + 3] & 0xFF) << 24);
|
||||
if (xOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
X[14] = (int)(bitLength & 0xFFFFFFFFu);
|
||||
X[15] = (int)(bitLength >>> 32);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
UnpackWord(H1, output, outOff);
|
||||
UnpackWord(H2, output, outOff + 4);
|
||||
UnpackWord(H3, output, outOff + 8);
|
||||
UnpackWord(H4, output, outOff + 12);
|
||||
Reset();
|
||||
return 16;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H1 = 1732584193;
|
||||
H2 = -271733879;
|
||||
H3 = -1732584194;
|
||||
H4 = 271733878;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private int RotateLeft(int x, int n)
|
||||
{
|
||||
return (x << n) | (x >>> 32 - n);
|
||||
}
|
||||
|
||||
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 & v) | (u & w) | (v & w);
|
||||
}
|
||||
|
||||
private int H(int u, int v, int w)
|
||||
{
|
||||
return u ^ v ^ w;
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
int h = H1;
|
||||
int h2 = H2;
|
||||
int h3 = H3;
|
||||
int h4 = H4;
|
||||
h = RotateLeft(h + F(h2, h3, h4) + X[0], 3);
|
||||
h4 = RotateLeft(h4 + F(h, h2, h3) + X[1], 7);
|
||||
h3 = RotateLeft(h3 + F(h4, h, h2) + X[2], 11);
|
||||
h2 = RotateLeft(h2 + F(h3, h4, h) + X[3], 19);
|
||||
h = RotateLeft(h + F(h2, h3, h4) + X[4], 3);
|
||||
h4 = RotateLeft(h4 + F(h, h2, h3) + X[5], 7);
|
||||
h3 = RotateLeft(h3 + F(h4, h, h2) + X[6], 11);
|
||||
h2 = RotateLeft(h2 + F(h3, h4, h) + X[7], 19);
|
||||
h = RotateLeft(h + F(h2, h3, h4) + X[8], 3);
|
||||
h4 = RotateLeft(h4 + F(h, h2, h3) + X[9], 7);
|
||||
h3 = RotateLeft(h3 + F(h4, h, h2) + X[10], 11);
|
||||
h2 = RotateLeft(h2 + F(h3, h4, h) + X[11], 19);
|
||||
h = RotateLeft(h + F(h2, h3, h4) + X[12], 3);
|
||||
h4 = RotateLeft(h4 + F(h, h2, h3) + X[13], 7);
|
||||
h3 = RotateLeft(h3 + F(h4, h, h2) + X[14], 11);
|
||||
h2 = RotateLeft(h2 + F(h3, h4, h) + X[15], 19);
|
||||
h = RotateLeft(h + G(h2, h3, h4) + X[0] + 1518500249, 3);
|
||||
h4 = RotateLeft(h4 + G(h, h2, h3) + X[4] + 1518500249, 5);
|
||||
h3 = RotateLeft(h3 + G(h4, h, h2) + X[8] + 1518500249, 9);
|
||||
h2 = RotateLeft(h2 + G(h3, h4, h) + X[12] + 1518500249, 13);
|
||||
h = RotateLeft(h + G(h2, h3, h4) + X[1] + 1518500249, 3);
|
||||
h4 = RotateLeft(h4 + G(h, h2, h3) + X[5] + 1518500249, 5);
|
||||
h3 = RotateLeft(h3 + G(h4, h, h2) + X[9] + 1518500249, 9);
|
||||
h2 = RotateLeft(h2 + G(h3, h4, h) + X[13] + 1518500249, 13);
|
||||
h = RotateLeft(h + G(h2, h3, h4) + X[2] + 1518500249, 3);
|
||||
h4 = RotateLeft(h4 + G(h, h2, h3) + X[6] + 1518500249, 5);
|
||||
h3 = RotateLeft(h3 + G(h4, h, h2) + X[10] + 1518500249, 9);
|
||||
h2 = RotateLeft(h2 + G(h3, h4, h) + X[14] + 1518500249, 13);
|
||||
h = RotateLeft(h + G(h2, h3, h4) + X[3] + 1518500249, 3);
|
||||
h4 = RotateLeft(h4 + G(h, h2, h3) + X[7] + 1518500249, 5);
|
||||
h3 = RotateLeft(h3 + G(h4, h, h2) + X[11] + 1518500249, 9);
|
||||
h2 = RotateLeft(h2 + G(h3, h4, h) + X[15] + 1518500249, 13);
|
||||
h = RotateLeft(h + H(h2, h3, h4) + X[0] + 1859775393, 3);
|
||||
h4 = RotateLeft(h4 + H(h, h2, h3) + X[8] + 1859775393, 9);
|
||||
h3 = RotateLeft(h3 + H(h4, h, h2) + X[4] + 1859775393, 11);
|
||||
h2 = RotateLeft(h2 + H(h3, h4, h) + X[12] + 1859775393, 15);
|
||||
h = RotateLeft(h + H(h2, h3, h4) + X[2] + 1859775393, 3);
|
||||
h4 = RotateLeft(h4 + H(h, h2, h3) + X[10] + 1859775393, 9);
|
||||
h3 = RotateLeft(h3 + H(h4, h, h2) + X[6] + 1859775393, 11);
|
||||
h2 = RotateLeft(h2 + H(h3, h4, h) + X[14] + 1859775393, 15);
|
||||
h = RotateLeft(h + H(h2, h3, h4) + X[1] + 1859775393, 3);
|
||||
h4 = RotateLeft(h4 + H(h, h2, h3) + X[9] + 1859775393, 9);
|
||||
h3 = RotateLeft(h3 + H(h4, h, h2) + X[5] + 1859775393, 11);
|
||||
h2 = RotateLeft(h2 + H(h3, h4, h) + X[13] + 1859775393, 15);
|
||||
h = RotateLeft(h + H(h2, h3, h4) + X[3] + 1859775393, 3);
|
||||
h4 = RotateLeft(h4 + H(h, h2, h3) + X[11] + 1859775393, 9);
|
||||
h3 = RotateLeft(h3 + H(h4, h, h2) + X[7] + 1859775393, 11);
|
||||
h2 = RotateLeft(h2 + H(h3, h4, h) + X[15] + 1859775393, 15);
|
||||
H1 += h;
|
||||
H2 += h2;
|
||||
H3 += h3;
|
||||
H4 += h4;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new MD4Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
MD4Digest t = (MD4Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class MD5Digest : GeneralDigest
|
||||
{
|
||||
private const int DigestLength = 16;
|
||||
|
||||
private uint H1;
|
||||
|
||||
private uint H2;
|
||||
|
||||
private uint H3;
|
||||
|
||||
private uint H4;
|
||||
|
||||
private uint[] X = new uint[16];
|
||||
|
||||
private int xOff;
|
||||
|
||||
private static readonly int S11 = 7;
|
||||
|
||||
private static readonly int S12 = 12;
|
||||
|
||||
private static readonly int S13 = 17;
|
||||
|
||||
private static readonly int S14 = 22;
|
||||
|
||||
private static readonly int S21 = 5;
|
||||
|
||||
private static readonly int S22 = 9;
|
||||
|
||||
private static readonly int S23 = 14;
|
||||
|
||||
private static readonly int S24 = 20;
|
||||
|
||||
private static readonly int S31 = 4;
|
||||
|
||||
private static readonly int S32 = 11;
|
||||
|
||||
private static readonly int S33 = 16;
|
||||
|
||||
private static readonly int S34 = 23;
|
||||
|
||||
private static readonly int S41 = 6;
|
||||
|
||||
private static readonly int S42 = 10;
|
||||
|
||||
private static readonly int S43 = 15;
|
||||
|
||||
private static readonly int S44 = 21;
|
||||
|
||||
public override string AlgorithmName => "MD5";
|
||||
|
||||
public MD5Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public MD5Digest(MD5Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(MD5Digest t)
|
||||
{
|
||||
CopyIn((GeneralDigest)t);
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
H4 = t.H4;
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
X[xOff] = Pack.LE_To_UInt32(input, inOff);
|
||||
if (++xOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
if (xOff == 15)
|
||||
{
|
||||
X[15] = 0u;
|
||||
}
|
||||
ProcessBlock();
|
||||
}
|
||||
for (int i = xOff; i < 14; i++)
|
||||
{
|
||||
X[i] = 0u;
|
||||
}
|
||||
X[14] = (uint)bitLength;
|
||||
X[15] = (uint)((ulong)bitLength >> 32);
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
Pack.UInt32_To_LE(H1, output, outOff);
|
||||
Pack.UInt32_To_LE(H2, output, outOff + 4);
|
||||
Pack.UInt32_To_LE(H3, output, outOff + 8);
|
||||
Pack.UInt32_To_LE(H4, output, outOff + 12);
|
||||
Reset();
|
||||
return 16;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H1 = 1732584193u;
|
||||
H2 = 4023233417u;
|
||||
H3 = 2562383102u;
|
||||
H4 = 271733878u;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0u;
|
||||
}
|
||||
}
|
||||
|
||||
private static uint RotateLeft(uint x, int n)
|
||||
{
|
||||
return (x << n) | (x >> 32 - n);
|
||||
}
|
||||
|
||||
private static uint F(uint u, uint v, uint w)
|
||||
{
|
||||
return (u & v) | (~u & w);
|
||||
}
|
||||
|
||||
private static uint G(uint u, uint v, uint w)
|
||||
{
|
||||
return (u & w) | (v & ~w);
|
||||
}
|
||||
|
||||
private static uint H(uint u, uint v, uint w)
|
||||
{
|
||||
return u ^ v ^ w;
|
||||
}
|
||||
|
||||
private static uint K(uint u, uint v, uint w)
|
||||
{
|
||||
return v ^ (u | ~w);
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
uint h = H1;
|
||||
uint h2 = H2;
|
||||
uint h3 = H3;
|
||||
uint h4 = H4;
|
||||
h = RotateLeft(h + F(h2, h3, h4) + X[0] + 3614090360u, S11) + h2;
|
||||
h4 = RotateLeft(h4 + F(h, h2, h3) + X[1] + 3905402710u, S12) + h;
|
||||
h3 = RotateLeft(h3 + F(h4, h, h2) + X[2] + 606105819, S13) + h4;
|
||||
h2 = RotateLeft(h2 + F(h3, h4, h) + X[3] + 3250441966u, S14) + h3;
|
||||
h = RotateLeft(h + F(h2, h3, h4) + X[4] + 4118548399u, S11) + h2;
|
||||
h4 = RotateLeft(h4 + F(h, h2, h3) + X[5] + 1200080426, S12) + h;
|
||||
h3 = RotateLeft(h3 + F(h4, h, h2) + X[6] + 2821735955u, S13) + h4;
|
||||
h2 = RotateLeft(h2 + F(h3, h4, h) + X[7] + 4249261313u, S14) + h3;
|
||||
h = RotateLeft(h + F(h2, h3, h4) + X[8] + 1770035416, S11) + h2;
|
||||
h4 = RotateLeft(h4 + F(h, h2, h3) + X[9] + 2336552879u, S12) + h;
|
||||
h3 = RotateLeft(h3 + F(h4, h, h2) + X[10] + 4294925233u, S13) + h4;
|
||||
h2 = RotateLeft(h2 + F(h3, h4, h) + X[11] + 2304563134u, S14) + h3;
|
||||
h = RotateLeft(h + F(h2, h3, h4) + X[12] + 1804603682, S11) + h2;
|
||||
h4 = RotateLeft(h4 + F(h, h2, h3) + X[13] + 4254626195u, S12) + h;
|
||||
h3 = RotateLeft(h3 + F(h4, h, h2) + X[14] + 2792965006u, S13) + h4;
|
||||
h2 = RotateLeft(h2 + F(h3, h4, h) + X[15] + 1236535329, S14) + h3;
|
||||
h = RotateLeft(h + G(h2, h3, h4) + X[1] + 4129170786u, S21) + h2;
|
||||
h4 = RotateLeft(h4 + G(h, h2, h3) + X[6] + 3225465664u, S22) + h;
|
||||
h3 = RotateLeft(h3 + G(h4, h, h2) + X[11] + 643717713, S23) + h4;
|
||||
h2 = RotateLeft(h2 + G(h3, h4, h) + X[0] + 3921069994u, S24) + h3;
|
||||
h = RotateLeft(h + G(h2, h3, h4) + X[5] + 3593408605u, S21) + h2;
|
||||
h4 = RotateLeft(h4 + G(h, h2, h3) + X[10] + 38016083, S22) + h;
|
||||
h3 = RotateLeft(h3 + G(h4, h, h2) + X[15] + 3634488961u, S23) + h4;
|
||||
h2 = RotateLeft(h2 + G(h3, h4, h) + X[4] + 3889429448u, S24) + h3;
|
||||
h = RotateLeft(h + G(h2, h3, h4) + X[9] + 568446438, S21) + h2;
|
||||
h4 = RotateLeft(h4 + G(h, h2, h3) + X[14] + 3275163606u, S22) + h;
|
||||
h3 = RotateLeft(h3 + G(h4, h, h2) + X[3] + 4107603335u, S23) + h4;
|
||||
h2 = RotateLeft(h2 + G(h3, h4, h) + X[8] + 1163531501, S24) + h3;
|
||||
h = RotateLeft(h + G(h2, h3, h4) + X[13] + 2850285829u, S21) + h2;
|
||||
h4 = RotateLeft(h4 + G(h, h2, h3) + X[2] + 4243563512u, S22) + h;
|
||||
h3 = RotateLeft(h3 + G(h4, h, h2) + X[7] + 1735328473, S23) + h4;
|
||||
h2 = RotateLeft(h2 + G(h3, h4, h) + X[12] + 2368359562u, S24) + h3;
|
||||
h = RotateLeft(h + H(h2, h3, h4) + X[5] + 4294588738u, S31) + h2;
|
||||
h4 = RotateLeft(h4 + H(h, h2, h3) + X[8] + 2272392833u, S32) + h;
|
||||
h3 = RotateLeft(h3 + H(h4, h, h2) + X[11] + 1839030562, S33) + h4;
|
||||
h2 = RotateLeft(h2 + H(h3, h4, h) + X[14] + 4259657740u, S34) + h3;
|
||||
h = RotateLeft(h + H(h2, h3, h4) + X[1] + 2763975236u, S31) + h2;
|
||||
h4 = RotateLeft(h4 + H(h, h2, h3) + X[4] + 1272893353, S32) + h;
|
||||
h3 = RotateLeft(h3 + H(h4, h, h2) + X[7] + 4139469664u, S33) + h4;
|
||||
h2 = RotateLeft(h2 + H(h3, h4, h) + X[10] + 3200236656u, S34) + h3;
|
||||
h = RotateLeft(h + H(h2, h3, h4) + X[13] + 681279174, S31) + h2;
|
||||
h4 = RotateLeft(h4 + H(h, h2, h3) + X[0] + 3936430074u, S32) + h;
|
||||
h3 = RotateLeft(h3 + H(h4, h, h2) + X[3] + 3572445317u, S33) + h4;
|
||||
h2 = RotateLeft(h2 + H(h3, h4, h) + X[6] + 76029189, S34) + h3;
|
||||
h = RotateLeft(h + H(h2, h3, h4) + X[9] + 3654602809u, S31) + h2;
|
||||
h4 = RotateLeft(h4 + H(h, h2, h3) + X[12] + 3873151461u, S32) + h;
|
||||
h3 = RotateLeft(h3 + H(h4, h, h2) + X[15] + 530742520, S33) + h4;
|
||||
h2 = RotateLeft(h2 + H(h3, h4, h) + X[2] + 3299628645u, S34) + h3;
|
||||
h = RotateLeft(h + K(h2, h3, h4) + X[0] + 4096336452u, S41) + h2;
|
||||
h4 = RotateLeft(h4 + K(h, h2, h3) + X[7] + 1126891415, S42) + h;
|
||||
h3 = RotateLeft(h3 + K(h4, h, h2) + X[14] + 2878612391u, S43) + h4;
|
||||
h2 = RotateLeft(h2 + K(h3, h4, h) + X[5] + 4237533241u, S44) + h3;
|
||||
h = RotateLeft(h + K(h2, h3, h4) + X[12] + 1700485571, S41) + h2;
|
||||
h4 = RotateLeft(h4 + K(h, h2, h3) + X[3] + 2399980690u, S42) + h;
|
||||
h3 = RotateLeft(h3 + K(h4, h, h2) + X[10] + 4293915773u, S43) + h4;
|
||||
h2 = RotateLeft(h2 + K(h3, h4, h) + X[1] + 2240044497u, S44) + h3;
|
||||
h = RotateLeft(h + K(h2, h3, h4) + X[8] + 1873313359, S41) + h2;
|
||||
h4 = RotateLeft(h4 + K(h, h2, h3) + X[15] + 4264355552u, S42) + h;
|
||||
h3 = RotateLeft(h3 + K(h4, h, h2) + X[6] + 2734768916u, S43) + h4;
|
||||
h2 = RotateLeft(h2 + K(h3, h4, h) + X[13] + 1309151649, S44) + h3;
|
||||
h = RotateLeft(h + K(h2, h3, h4) + X[4] + 4149444226u, S41) + h2;
|
||||
h4 = RotateLeft(h4 + K(h, h2, h3) + X[11] + 3174756917u, S42) + h;
|
||||
h3 = RotateLeft(h3 + K(h4, h, h2) + X[2] + 718787259, S43) + h4;
|
||||
h2 = RotateLeft(h2 + K(h3, h4, h) + X[9] + 3951481745u, S44) + h3;
|
||||
H1 += h;
|
||||
H2 += h2;
|
||||
H3 += h3;
|
||||
H4 += h4;
|
||||
xOff = 0;
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new MD5Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
MD5Digest t = (MD5Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class NonMemoableDigest : IDigest
|
||||
{
|
||||
protected readonly IDigest mBaseDigest;
|
||||
|
||||
public virtual string AlgorithmName => mBaseDigest.AlgorithmName;
|
||||
|
||||
public NonMemoableDigest(IDigest baseDigest)
|
||||
{
|
||||
if (baseDigest == null)
|
||||
{
|
||||
throw new ArgumentNullException("baseDigest");
|
||||
}
|
||||
mBaseDigest = baseDigest;
|
||||
}
|
||||
|
||||
public virtual int GetDigestSize()
|
||||
{
|
||||
return mBaseDigest.GetDigestSize();
|
||||
}
|
||||
|
||||
public virtual void Update(byte input)
|
||||
{
|
||||
mBaseDigest.Update(input);
|
||||
}
|
||||
|
||||
public virtual void BlockUpdate(byte[] input, int inOff, int len)
|
||||
{
|
||||
mBaseDigest.BlockUpdate(input, inOff, len);
|
||||
}
|
||||
|
||||
public virtual int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
return mBaseDigest.DoFinal(output, outOff);
|
||||
}
|
||||
|
||||
public virtual void Reset()
|
||||
{
|
||||
mBaseDigest.Reset();
|
||||
}
|
||||
|
||||
public virtual int GetByteLength()
|
||||
{
|
||||
return mBaseDigest.GetByteLength();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class NullDigest : IDigest
|
||||
{
|
||||
private readonly MemoryStream bOut = new MemoryStream();
|
||||
|
||||
public string AlgorithmName => "NULL";
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int GetDigestSize()
|
||||
{
|
||||
return (int)bOut.Length;
|
||||
}
|
||||
|
||||
public void Update(byte b)
|
||||
{
|
||||
bOut.WriteByte(b);
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] inBytes, int inOff, int len)
|
||||
{
|
||||
bOut.Write(inBytes, inOff, len);
|
||||
}
|
||||
|
||||
public int DoFinal(byte[] outBytes, int outOff)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Streams.WriteBufTo(bOut, outBytes, outOff);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
bOut.SetLength(0L);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class RipeMD128Digest : GeneralDigest
|
||||
{
|
||||
private const int DigestLength = 16;
|
||||
|
||||
private int H0;
|
||||
|
||||
private int H1;
|
||||
|
||||
private int H2;
|
||||
|
||||
private int H3;
|
||||
|
||||
private int[] X = new int[16];
|
||||
|
||||
private int xOff;
|
||||
|
||||
public override string AlgorithmName => "RIPEMD128";
|
||||
|
||||
public RipeMD128Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public RipeMD128Digest(RipeMD128Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(RipeMD128Digest t)
|
||||
{
|
||||
CopyIn((GeneralDigest)t);
|
||||
H0 = t.H0;
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
X[xOff++] = (input[inOff] & 0xFF) | ((input[inOff + 1] & 0xFF) << 8) | ((input[inOff + 2] & 0xFF) << 16) | ((input[inOff + 3] & 0xFF) << 24);
|
||||
if (xOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
X[14] = (int)(bitLength & 0xFFFFFFFFu);
|
||||
X[15] = (int)(bitLength >>> 32);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
UnpackWord(H0, output, outOff);
|
||||
UnpackWord(H1, output, outOff + 4);
|
||||
UnpackWord(H2, output, outOff + 8);
|
||||
UnpackWord(H3, output, outOff + 12);
|
||||
Reset();
|
||||
return 16;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H0 = 1732584193;
|
||||
H1 = -271733879;
|
||||
H2 = -1732584194;
|
||||
H3 = 271733878;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private int RL(int x, int n)
|
||||
{
|
||||
return (x << n) | (x >>> 32 - n);
|
||||
}
|
||||
|
||||
private int F1(int x, int y, int z)
|
||||
{
|
||||
return x ^ y ^ z;
|
||||
}
|
||||
|
||||
private int F2(int x, int y, int z)
|
||||
{
|
||||
return (x & y) | (~x & z);
|
||||
}
|
||||
|
||||
private int F3(int x, int y, int z)
|
||||
{
|
||||
return (x | ~y) ^ z;
|
||||
}
|
||||
|
||||
private int F4(int x, int y, int z)
|
||||
{
|
||||
return (x & z) | (y & ~z);
|
||||
}
|
||||
|
||||
private int F1(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F1(b, c, d) + x, s);
|
||||
}
|
||||
|
||||
private int F2(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F2(b, c, d) + x + 1518500249, s);
|
||||
}
|
||||
|
||||
private int F3(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F3(b, c, d) + x + 1859775393, s);
|
||||
}
|
||||
|
||||
private int F4(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F4(b, c, d) + x + -1894007588, s);
|
||||
}
|
||||
|
||||
private int FF1(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F1(b, c, d) + x, s);
|
||||
}
|
||||
|
||||
private int FF2(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F2(b, c, d) + x + 1836072691, s);
|
||||
}
|
||||
|
||||
private int FF3(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F3(b, c, d) + x + 1548603684, s);
|
||||
}
|
||||
|
||||
private int FF4(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F4(b, c, d) + x + 1352829926, s);
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
int h;
|
||||
int a = (h = H0);
|
||||
int h2;
|
||||
int num = (h2 = H1);
|
||||
int h3;
|
||||
int num2 = (h3 = H2);
|
||||
int h4;
|
||||
int num3 = (h4 = H3);
|
||||
a = F1(a, num, num2, num3, X[0], 11);
|
||||
num3 = F1(num3, a, num, num2, X[1], 14);
|
||||
num2 = F1(num2, num3, a, num, X[2], 15);
|
||||
num = F1(num, num2, num3, a, X[3], 12);
|
||||
a = F1(a, num, num2, num3, X[4], 5);
|
||||
num3 = F1(num3, a, num, num2, X[5], 8);
|
||||
num2 = F1(num2, num3, a, num, X[6], 7);
|
||||
num = F1(num, num2, num3, a, X[7], 9);
|
||||
a = F1(a, num, num2, num3, X[8], 11);
|
||||
num3 = F1(num3, a, num, num2, X[9], 13);
|
||||
num2 = F1(num2, num3, a, num, X[10], 14);
|
||||
num = F1(num, num2, num3, a, X[11], 15);
|
||||
a = F1(a, num, num2, num3, X[12], 6);
|
||||
num3 = F1(num3, a, num, num2, X[13], 7);
|
||||
num2 = F1(num2, num3, a, num, X[14], 9);
|
||||
num = F1(num, num2, num3, a, X[15], 8);
|
||||
a = F2(a, num, num2, num3, X[7], 7);
|
||||
num3 = F2(num3, a, num, num2, X[4], 6);
|
||||
num2 = F2(num2, num3, a, num, X[13], 8);
|
||||
num = F2(num, num2, num3, a, X[1], 13);
|
||||
a = F2(a, num, num2, num3, X[10], 11);
|
||||
num3 = F2(num3, a, num, num2, X[6], 9);
|
||||
num2 = F2(num2, num3, a, num, X[15], 7);
|
||||
num = F2(num, num2, num3, a, X[3], 15);
|
||||
a = F2(a, num, num2, num3, X[12], 7);
|
||||
num3 = F2(num3, a, num, num2, X[0], 12);
|
||||
num2 = F2(num2, num3, a, num, X[9], 15);
|
||||
num = F2(num, num2, num3, a, X[5], 9);
|
||||
a = F2(a, num, num2, num3, X[2], 11);
|
||||
num3 = F2(num3, a, num, num2, X[14], 7);
|
||||
num2 = F2(num2, num3, a, num, X[11], 13);
|
||||
num = F2(num, num2, num3, a, X[8], 12);
|
||||
a = F3(a, num, num2, num3, X[3], 11);
|
||||
num3 = F3(num3, a, num, num2, X[10], 13);
|
||||
num2 = F3(num2, num3, a, num, X[14], 6);
|
||||
num = F3(num, num2, num3, a, X[4], 7);
|
||||
a = F3(a, num, num2, num3, X[9], 14);
|
||||
num3 = F3(num3, a, num, num2, X[15], 9);
|
||||
num2 = F3(num2, num3, a, num, X[8], 13);
|
||||
num = F3(num, num2, num3, a, X[1], 15);
|
||||
a = F3(a, num, num2, num3, X[2], 14);
|
||||
num3 = F3(num3, a, num, num2, X[7], 8);
|
||||
num2 = F3(num2, num3, a, num, X[0], 13);
|
||||
num = F3(num, num2, num3, a, X[6], 6);
|
||||
a = F3(a, num, num2, num3, X[13], 5);
|
||||
num3 = F3(num3, a, num, num2, X[11], 12);
|
||||
num2 = F3(num2, num3, a, num, X[5], 7);
|
||||
num = F3(num, num2, num3, a, X[12], 5);
|
||||
a = F4(a, num, num2, num3, X[1], 11);
|
||||
num3 = F4(num3, a, num, num2, X[9], 12);
|
||||
num2 = F4(num2, num3, a, num, X[11], 14);
|
||||
num = F4(num, num2, num3, a, X[10], 15);
|
||||
a = F4(a, num, num2, num3, X[0], 14);
|
||||
num3 = F4(num3, a, num, num2, X[8], 15);
|
||||
num2 = F4(num2, num3, a, num, X[12], 9);
|
||||
num = F4(num, num2, num3, a, X[4], 8);
|
||||
a = F4(a, num, num2, num3, X[13], 9);
|
||||
num3 = F4(num3, a, num, num2, X[3], 14);
|
||||
num2 = F4(num2, num3, a, num, X[7], 5);
|
||||
num = F4(num, num2, num3, a, X[15], 6);
|
||||
a = F4(a, num, num2, num3, X[14], 8);
|
||||
num3 = F4(num3, a, num, num2, X[5], 6);
|
||||
num2 = F4(num2, num3, a, num, X[6], 5);
|
||||
num = F4(num, num2, num3, a, X[2], 12);
|
||||
h = FF4(h, h2, h3, h4, X[5], 8);
|
||||
h4 = FF4(h4, h, h2, h3, X[14], 9);
|
||||
h3 = FF4(h3, h4, h, h2, X[7], 9);
|
||||
h2 = FF4(h2, h3, h4, h, X[0], 11);
|
||||
h = FF4(h, h2, h3, h4, X[9], 13);
|
||||
h4 = FF4(h4, h, h2, h3, X[2], 15);
|
||||
h3 = FF4(h3, h4, h, h2, X[11], 15);
|
||||
h2 = FF4(h2, h3, h4, h, X[4], 5);
|
||||
h = FF4(h, h2, h3, h4, X[13], 7);
|
||||
h4 = FF4(h4, h, h2, h3, X[6], 7);
|
||||
h3 = FF4(h3, h4, h, h2, X[15], 8);
|
||||
h2 = FF4(h2, h3, h4, h, X[8], 11);
|
||||
h = FF4(h, h2, h3, h4, X[1], 14);
|
||||
h4 = FF4(h4, h, h2, h3, X[10], 14);
|
||||
h3 = FF4(h3, h4, h, h2, X[3], 12);
|
||||
h2 = FF4(h2, h3, h4, h, X[12], 6);
|
||||
h = FF3(h, h2, h3, h4, X[6], 9);
|
||||
h4 = FF3(h4, h, h2, h3, X[11], 13);
|
||||
h3 = FF3(h3, h4, h, h2, X[3], 15);
|
||||
h2 = FF3(h2, h3, h4, h, X[7], 7);
|
||||
h = FF3(h, h2, h3, h4, X[0], 12);
|
||||
h4 = FF3(h4, h, h2, h3, X[13], 8);
|
||||
h3 = FF3(h3, h4, h, h2, X[5], 9);
|
||||
h2 = FF3(h2, h3, h4, h, X[10], 11);
|
||||
h = FF3(h, h2, h3, h4, X[14], 7);
|
||||
h4 = FF3(h4, h, h2, h3, X[15], 7);
|
||||
h3 = FF3(h3, h4, h, h2, X[8], 12);
|
||||
h2 = FF3(h2, h3, h4, h, X[12], 7);
|
||||
h = FF3(h, h2, h3, h4, X[4], 6);
|
||||
h4 = FF3(h4, h, h2, h3, X[9], 15);
|
||||
h3 = FF3(h3, h4, h, h2, X[1], 13);
|
||||
h2 = FF3(h2, h3, h4, h, X[2], 11);
|
||||
h = FF2(h, h2, h3, h4, X[15], 9);
|
||||
h4 = FF2(h4, h, h2, h3, X[5], 7);
|
||||
h3 = FF2(h3, h4, h, h2, X[1], 15);
|
||||
h2 = FF2(h2, h3, h4, h, X[3], 11);
|
||||
h = FF2(h, h2, h3, h4, X[7], 8);
|
||||
h4 = FF2(h4, h, h2, h3, X[14], 6);
|
||||
h3 = FF2(h3, h4, h, h2, X[6], 6);
|
||||
h2 = FF2(h2, h3, h4, h, X[9], 14);
|
||||
h = FF2(h, h2, h3, h4, X[11], 12);
|
||||
h4 = FF2(h4, h, h2, h3, X[8], 13);
|
||||
h3 = FF2(h3, h4, h, h2, X[12], 5);
|
||||
h2 = FF2(h2, h3, h4, h, X[2], 14);
|
||||
h = FF2(h, h2, h3, h4, X[10], 13);
|
||||
h4 = FF2(h4, h, h2, h3, X[0], 13);
|
||||
h3 = FF2(h3, h4, h, h2, X[4], 7);
|
||||
h2 = FF2(h2, h3, h4, h, X[13], 5);
|
||||
h = FF1(h, h2, h3, h4, X[8], 15);
|
||||
h4 = FF1(h4, h, h2, h3, X[6], 5);
|
||||
h3 = FF1(h3, h4, h, h2, X[4], 8);
|
||||
h2 = FF1(h2, h3, h4, h, X[1], 11);
|
||||
h = FF1(h, h2, h3, h4, X[3], 14);
|
||||
h4 = FF1(h4, h, h2, h3, X[11], 14);
|
||||
h3 = FF1(h3, h4, h, h2, X[15], 6);
|
||||
h2 = FF1(h2, h3, h4, h, X[0], 14);
|
||||
h = FF1(h, h2, h3, h4, X[5], 6);
|
||||
h4 = FF1(h4, h, h2, h3, X[12], 9);
|
||||
h3 = FF1(h3, h4, h, h2, X[2], 12);
|
||||
h2 = FF1(h2, h3, h4, h, X[13], 9);
|
||||
h = FF1(h, h2, h3, h4, X[9], 12);
|
||||
h4 = FF1(h4, h, h2, h3, X[7], 5);
|
||||
h3 = FF1(h3, h4, h, h2, X[10], 15);
|
||||
h2 = FF1(h2, h3, h4, h, X[14], 8);
|
||||
h4 += num2 + H1;
|
||||
H1 = H2 + num3 + h;
|
||||
H2 = H3 + a + h2;
|
||||
H3 = H0 + num + h3;
|
||||
H0 = h4;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new RipeMD128Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
RipeMD128Digest t = (RipeMD128Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,493 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class RipeMD160Digest : GeneralDigest
|
||||
{
|
||||
private const int DigestLength = 20;
|
||||
|
||||
private int H0;
|
||||
|
||||
private int H1;
|
||||
|
||||
private int H2;
|
||||
|
||||
private int H3;
|
||||
|
||||
private int H4;
|
||||
|
||||
private int[] X = new int[16];
|
||||
|
||||
private int xOff;
|
||||
|
||||
public override string AlgorithmName => "RIPEMD160";
|
||||
|
||||
public RipeMD160Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public RipeMD160Digest(RipeMD160Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(RipeMD160Digest t)
|
||||
{
|
||||
CopyIn((GeneralDigest)t);
|
||||
H0 = t.H0;
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
H4 = t.H4;
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 20;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
X[xOff++] = (input[inOff] & 0xFF) | ((input[inOff + 1] & 0xFF) << 8) | ((input[inOff + 2] & 0xFF) << 16) | ((input[inOff + 3] & 0xFF) << 24);
|
||||
if (xOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
X[14] = (int)(bitLength & 0xFFFFFFFFu);
|
||||
X[15] = (int)(bitLength >>> 32);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
UnpackWord(H0, output, outOff);
|
||||
UnpackWord(H1, output, outOff + 4);
|
||||
UnpackWord(H2, output, outOff + 8);
|
||||
UnpackWord(H3, output, outOff + 12);
|
||||
UnpackWord(H4, output, outOff + 16);
|
||||
Reset();
|
||||
return 20;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H0 = 1732584193;
|
||||
H1 = -271733879;
|
||||
H2 = -1732584194;
|
||||
H3 = 271733878;
|
||||
H4 = -1009589776;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private int RL(int x, int n)
|
||||
{
|
||||
return (x << n) | (x >>> 32 - n);
|
||||
}
|
||||
|
||||
private int F1(int x, int y, int z)
|
||||
{
|
||||
return x ^ y ^ z;
|
||||
}
|
||||
|
||||
private int F2(int x, int y, int z)
|
||||
{
|
||||
return (x & y) | (~x & z);
|
||||
}
|
||||
|
||||
private int F3(int x, int y, int z)
|
||||
{
|
||||
return (x | ~y) ^ z;
|
||||
}
|
||||
|
||||
private int F4(int x, int y, int z)
|
||||
{
|
||||
return (x & z) | (y & ~z);
|
||||
}
|
||||
|
||||
private int F5(int x, int y, int z)
|
||||
{
|
||||
return x ^ (y | ~z);
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
int h;
|
||||
int num = (h = H0);
|
||||
int h2;
|
||||
int num2 = (h2 = H1);
|
||||
int h3;
|
||||
int num3 = (h3 = H2);
|
||||
int h4;
|
||||
int num4 = (h4 = H3);
|
||||
int h5;
|
||||
int num5 = (h5 = H4);
|
||||
num = RL(num + F1(num2, num3, num4) + X[0], 11) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F1(num, num2, num3) + X[1], 14) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F1(num5, num, num2) + X[2], 15) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F1(num4, num5, num) + X[3], 12) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F1(num3, num4, num5) + X[4], 5) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F1(num2, num3, num4) + X[5], 8) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F1(num, num2, num3) + X[6], 7) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F1(num5, num, num2) + X[7], 9) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F1(num4, num5, num) + X[8], 11) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F1(num3, num4, num5) + X[9], 13) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F1(num2, num3, num4) + X[10], 14) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F1(num, num2, num3) + X[11], 15) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F1(num5, num, num2) + X[12], 6) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F1(num4, num5, num) + X[13], 7) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F1(num3, num4, num5) + X[14], 9) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F1(num2, num3, num4) + X[15], 8) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
h = RL(h + F5(h2, h3, h4) + X[5] + 1352829926, 8) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F5(h, h2, h3) + X[14] + 1352829926, 9) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F5(h5, h, h2) + X[7] + 1352829926, 9) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F5(h4, h5, h) + X[0] + 1352829926, 11) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F5(h3, h4, h5) + X[9] + 1352829926, 13) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F5(h2, h3, h4) + X[2] + 1352829926, 15) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F5(h, h2, h3) + X[11] + 1352829926, 15) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F5(h5, h, h2) + X[4] + 1352829926, 5) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F5(h4, h5, h) + X[13] + 1352829926, 7) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F5(h3, h4, h5) + X[6] + 1352829926, 7) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F5(h2, h3, h4) + X[15] + 1352829926, 8) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F5(h, h2, h3) + X[8] + 1352829926, 11) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F5(h5, h, h2) + X[1] + 1352829926, 14) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F5(h4, h5, h) + X[10] + 1352829926, 14) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F5(h3, h4, h5) + X[3] + 1352829926, 12) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F5(h2, h3, h4) + X[12] + 1352829926, 6) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
num5 = RL(num5 + F2(num, num2, num3) + X[7] + 1518500249, 7) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F2(num5, num, num2) + X[4] + 1518500249, 6) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F2(num4, num5, num) + X[13] + 1518500249, 8) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F2(num3, num4, num5) + X[1] + 1518500249, 13) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F2(num2, num3, num4) + X[10] + 1518500249, 11) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F2(num, num2, num3) + X[6] + 1518500249, 9) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F2(num5, num, num2) + X[15] + 1518500249, 7) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F2(num4, num5, num) + X[3] + 1518500249, 15) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F2(num3, num4, num5) + X[12] + 1518500249, 7) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F2(num2, num3, num4) + X[0] + 1518500249, 12) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F2(num, num2, num3) + X[9] + 1518500249, 15) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F2(num5, num, num2) + X[5] + 1518500249, 9) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F2(num4, num5, num) + X[2] + 1518500249, 11) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F2(num3, num4, num5) + X[14] + 1518500249, 7) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F2(num2, num3, num4) + X[11] + 1518500249, 13) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F2(num, num2, num3) + X[8] + 1518500249, 12) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
h5 = RL(h5 + F4(h, h2, h3) + X[6] + 1548603684, 9) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F4(h5, h, h2) + X[11] + 1548603684, 13) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F4(h4, h5, h) + X[3] + 1548603684, 15) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F4(h3, h4, h5) + X[7] + 1548603684, 7) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F4(h2, h3, h4) + X[0] + 1548603684, 12) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F4(h, h2, h3) + X[13] + 1548603684, 8) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F4(h5, h, h2) + X[5] + 1548603684, 9) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F4(h4, h5, h) + X[10] + 1548603684, 11) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F4(h3, h4, h5) + X[14] + 1548603684, 7) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F4(h2, h3, h4) + X[15] + 1548603684, 7) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F4(h, h2, h3) + X[8] + 1548603684, 12) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F4(h5, h, h2) + X[12] + 1548603684, 7) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F4(h4, h5, h) + X[4] + 1548603684, 6) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F4(h3, h4, h5) + X[9] + 1548603684, 15) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F4(h2, h3, h4) + X[1] + 1548603684, 13) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F4(h, h2, h3) + X[2] + 1548603684, 11) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
num4 = RL(num4 + F3(num5, num, num2) + X[3] + 1859775393, 11) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F3(num4, num5, num) + X[10] + 1859775393, 13) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F3(num3, num4, num5) + X[14] + 1859775393, 6) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F3(num2, num3, num4) + X[4] + 1859775393, 7) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F3(num, num2, num3) + X[9] + 1859775393, 14) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F3(num5, num, num2) + X[15] + 1859775393, 9) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F3(num4, num5, num) + X[8] + 1859775393, 13) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F3(num3, num4, num5) + X[1] + 1859775393, 15) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F3(num2, num3, num4) + X[2] + 1859775393, 14) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F3(num, num2, num3) + X[7] + 1859775393, 8) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F3(num5, num, num2) + X[0] + 1859775393, 13) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F3(num4, num5, num) + X[6] + 1859775393, 6) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F3(num3, num4, num5) + X[13] + 1859775393, 5) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F3(num2, num3, num4) + X[11] + 1859775393, 12) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F3(num, num2, num3) + X[5] + 1859775393, 7) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F3(num5, num, num2) + X[12] + 1859775393, 5) + num3;
|
||||
num = RL(num, 10);
|
||||
h4 = RL(h4 + F3(h5, h, h2) + X[15] + 1836072691, 9) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F3(h4, h5, h) + X[5] + 1836072691, 7) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F3(h3, h4, h5) + X[1] + 1836072691, 15) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F3(h2, h3, h4) + X[3] + 1836072691, 11) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F3(h, h2, h3) + X[7] + 1836072691, 8) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F3(h5, h, h2) + X[14] + 1836072691, 6) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F3(h4, h5, h) + X[6] + 1836072691, 6) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F3(h3, h4, h5) + X[9] + 1836072691, 14) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F3(h2, h3, h4) + X[11] + 1836072691, 12) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F3(h, h2, h3) + X[8] + 1836072691, 13) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F3(h5, h, h2) + X[12] + 1836072691, 5) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F3(h4, h5, h) + X[2] + 1836072691, 14) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F3(h3, h4, h5) + X[10] + 1836072691, 13) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F3(h2, h3, h4) + X[0] + 1836072691, 13) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F3(h, h2, h3) + X[4] + 1836072691, 7) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F3(h5, h, h2) + X[13] + 1836072691, 5) + h3;
|
||||
h = RL(h, 10);
|
||||
num3 = RL(num3 + F4(num4, num5, num) + X[1] + -1894007588, 11) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F4(num3, num4, num5) + X[9] + -1894007588, 12) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F4(num2, num3, num4) + X[11] + -1894007588, 14) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F4(num, num2, num3) + X[10] + -1894007588, 15) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F4(num5, num, num2) + X[0] + -1894007588, 14) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F4(num4, num5, num) + X[8] + -1894007588, 15) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F4(num3, num4, num5) + X[12] + -1894007588, 9) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F4(num2, num3, num4) + X[4] + -1894007588, 8) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F4(num, num2, num3) + X[13] + -1894007588, 9) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F4(num5, num, num2) + X[3] + -1894007588, 14) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F4(num4, num5, num) + X[7] + -1894007588, 5) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F4(num3, num4, num5) + X[15] + -1894007588, 6) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F4(num2, num3, num4) + X[14] + -1894007588, 8) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F4(num, num2, num3) + X[5] + -1894007588, 6) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F4(num5, num, num2) + X[6] + -1894007588, 5) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F4(num4, num5, num) + X[2] + -1894007588, 12) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
h3 = RL(h3 + F2(h4, h5, h) + X[8] + 2053994217, 15) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F2(h3, h4, h5) + X[6] + 2053994217, 5) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F2(h2, h3, h4) + X[4] + 2053994217, 8) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F2(h, h2, h3) + X[1] + 2053994217, 11) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F2(h5, h, h2) + X[3] + 2053994217, 14) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F2(h4, h5, h) + X[11] + 2053994217, 14) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F2(h3, h4, h5) + X[15] + 2053994217, 6) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F2(h2, h3, h4) + X[0] + 2053994217, 14) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F2(h, h2, h3) + X[5] + 2053994217, 6) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F2(h5, h, h2) + X[12] + 2053994217, 9) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F2(h4, h5, h) + X[2] + 2053994217, 12) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F2(h3, h4, h5) + X[13] + 2053994217, 9) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F2(h2, h3, h4) + X[9] + 2053994217, 12) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F2(h, h2, h3) + X[7] + 2053994217, 5) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F2(h5, h, h2) + X[10] + 2053994217, 15) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F2(h4, h5, h) + X[14] + 2053994217, 8) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
num2 = RL(num2 + F5(num3, num4, num5) + X[4] + -1454113458, 9) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F5(num2, num3, num4) + X[0] + -1454113458, 15) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F5(num, num2, num3) + X[5] + -1454113458, 5) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F5(num5, num, num2) + X[9] + -1454113458, 11) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F5(num4, num5, num) + X[7] + -1454113458, 6) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F5(num3, num4, num5) + X[12] + -1454113458, 8) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F5(num2, num3, num4) + X[2] + -1454113458, 13) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F5(num, num2, num3) + X[10] + -1454113458, 12) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F5(num5, num, num2) + X[14] + -1454113458, 5) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F5(num4, num5, num) + X[1] + -1454113458, 12) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F5(num3, num4, num5) + X[3] + -1454113458, 13) + num;
|
||||
num4 = RL(num4, 10);
|
||||
num = RL(num + F5(num2, num3, num4) + X[8] + -1454113458, 14) + num5;
|
||||
num3 = RL(num3, 10);
|
||||
num5 = RL(num5 + F5(num, num2, num3) + X[11] + -1454113458, 11) + num4;
|
||||
num2 = RL(num2, 10);
|
||||
num4 = RL(num4 + F5(num5, num, num2) + X[6] + -1454113458, 8) + num3;
|
||||
num = RL(num, 10);
|
||||
num3 = RL(num3 + F5(num4, num5, num) + X[15] + -1454113458, 5) + num2;
|
||||
num5 = RL(num5, 10);
|
||||
num2 = RL(num2 + F5(num3, num4, num5) + X[13] + -1454113458, 6) + num;
|
||||
num4 = RL(num4, 10);
|
||||
h2 = RL(h2 + F1(h3, h4, h5) + X[12], 8) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F1(h2, h3, h4) + X[15], 5) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F1(h, h2, h3) + X[10], 12) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F1(h5, h, h2) + X[4], 9) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F1(h4, h5, h) + X[1], 12) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F1(h3, h4, h5) + X[5], 5) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F1(h2, h3, h4) + X[8], 14) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F1(h, h2, h3) + X[7], 6) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F1(h5, h, h2) + X[6], 8) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F1(h4, h5, h) + X[2], 13) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F1(h3, h4, h5) + X[13], 6) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F1(h2, h3, h4) + X[14], 5) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F1(h, h2, h3) + X[0], 15) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F1(h5, h, h2) + X[3], 13) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F1(h4, h5, h) + X[9], 11) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F1(h3, h4, h5) + X[11], 11) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h4 += num3 + H1;
|
||||
H1 = H2 + num4 + h5;
|
||||
H2 = H3 + num5 + h;
|
||||
H3 = H4 + num + h2;
|
||||
H4 = H0 + num2 + h3;
|
||||
H0 = h4;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new RipeMD160Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
RipeMD160Digest t = (RipeMD160Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class RipeMD256Digest : GeneralDigest
|
||||
{
|
||||
private const int DigestLength = 32;
|
||||
|
||||
private int H0;
|
||||
|
||||
private int H1;
|
||||
|
||||
private int H2;
|
||||
|
||||
private int H3;
|
||||
|
||||
private int H4;
|
||||
|
||||
private int H5;
|
||||
|
||||
private int H6;
|
||||
|
||||
private int H7;
|
||||
|
||||
private int[] X = new int[16];
|
||||
|
||||
private int xOff;
|
||||
|
||||
public override string AlgorithmName => "RIPEMD256";
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
public RipeMD256Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public RipeMD256Digest(RipeMD256Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(RipeMD256Digest t)
|
||||
{
|
||||
CopyIn((GeneralDigest)t);
|
||||
H0 = t.H0;
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
H4 = t.H4;
|
||||
H5 = t.H5;
|
||||
H6 = t.H6;
|
||||
H7 = t.H7;
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
X[xOff++] = (input[inOff] & 0xFF) | ((input[inOff + 1] & 0xFF) << 8) | ((input[inOff + 2] & 0xFF) << 16) | ((input[inOff + 3] & 0xFF) << 24);
|
||||
if (xOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
X[14] = (int)(bitLength & 0xFFFFFFFFu);
|
||||
X[15] = (int)(bitLength >>> 32);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
UnpackWord(H0, output, outOff);
|
||||
UnpackWord(H1, output, outOff + 4);
|
||||
UnpackWord(H2, output, outOff + 8);
|
||||
UnpackWord(H3, output, outOff + 12);
|
||||
UnpackWord(H4, output, outOff + 16);
|
||||
UnpackWord(H5, output, outOff + 20);
|
||||
UnpackWord(H6, output, outOff + 24);
|
||||
UnpackWord(H7, output, outOff + 28);
|
||||
Reset();
|
||||
return 32;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H0 = 1732584193;
|
||||
H1 = -271733879;
|
||||
H2 = -1732584194;
|
||||
H3 = 271733878;
|
||||
H4 = 1985229328;
|
||||
H5 = -19088744;
|
||||
H6 = -1985229329;
|
||||
H7 = 19088743;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private int RL(int x, int n)
|
||||
{
|
||||
return (x << n) | (x >>> 32 - n);
|
||||
}
|
||||
|
||||
private int F1(int x, int y, int z)
|
||||
{
|
||||
return x ^ y ^ z;
|
||||
}
|
||||
|
||||
private int F2(int x, int y, int z)
|
||||
{
|
||||
return (x & y) | (~x & z);
|
||||
}
|
||||
|
||||
private int F3(int x, int y, int z)
|
||||
{
|
||||
return (x | ~y) ^ z;
|
||||
}
|
||||
|
||||
private int F4(int x, int y, int z)
|
||||
{
|
||||
return (x & z) | (y & ~z);
|
||||
}
|
||||
|
||||
private int F1(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F1(b, c, d) + x, s);
|
||||
}
|
||||
|
||||
private int F2(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F2(b, c, d) + x + 1518500249, s);
|
||||
}
|
||||
|
||||
private int F3(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F3(b, c, d) + x + 1859775393, s);
|
||||
}
|
||||
|
||||
private int F4(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F4(b, c, d) + x + -1894007588, s);
|
||||
}
|
||||
|
||||
private int FF1(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F1(b, c, d) + x, s);
|
||||
}
|
||||
|
||||
private int FF2(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F2(b, c, d) + x + 1836072691, s);
|
||||
}
|
||||
|
||||
private int FF3(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F3(b, c, d) + x + 1548603684, s);
|
||||
}
|
||||
|
||||
private int FF4(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
return RL(a + F4(b, c, d) + x + 1352829926, s);
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
int h = H0;
|
||||
int h2 = H1;
|
||||
int h3 = H2;
|
||||
int h4 = H3;
|
||||
int h5 = H4;
|
||||
int h6 = H5;
|
||||
int h7 = H6;
|
||||
int h8 = H7;
|
||||
h = F1(h, h2, h3, h4, X[0], 11);
|
||||
h4 = F1(h4, h, h2, h3, X[1], 14);
|
||||
h3 = F1(h3, h4, h, h2, X[2], 15);
|
||||
h2 = F1(h2, h3, h4, h, X[3], 12);
|
||||
h = F1(h, h2, h3, h4, X[4], 5);
|
||||
h4 = F1(h4, h, h2, h3, X[5], 8);
|
||||
h3 = F1(h3, h4, h, h2, X[6], 7);
|
||||
h2 = F1(h2, h3, h4, h, X[7], 9);
|
||||
h = F1(h, h2, h3, h4, X[8], 11);
|
||||
h4 = F1(h4, h, h2, h3, X[9], 13);
|
||||
h3 = F1(h3, h4, h, h2, X[10], 14);
|
||||
h2 = F1(h2, h3, h4, h, X[11], 15);
|
||||
h = F1(h, h2, h3, h4, X[12], 6);
|
||||
h4 = F1(h4, h, h2, h3, X[13], 7);
|
||||
h3 = F1(h3, h4, h, h2, X[14], 9);
|
||||
h2 = F1(h2, h3, h4, h, X[15], 8);
|
||||
h5 = FF4(h5, h6, h7, h8, X[5], 8);
|
||||
h8 = FF4(h8, h5, h6, h7, X[14], 9);
|
||||
h7 = FF4(h7, h8, h5, h6, X[7], 9);
|
||||
h6 = FF4(h6, h7, h8, h5, X[0], 11);
|
||||
h5 = FF4(h5, h6, h7, h8, X[9], 13);
|
||||
h8 = FF4(h8, h5, h6, h7, X[2], 15);
|
||||
h7 = FF4(h7, h8, h5, h6, X[11], 15);
|
||||
h6 = FF4(h6, h7, h8, h5, X[4], 5);
|
||||
h5 = FF4(h5, h6, h7, h8, X[13], 7);
|
||||
h8 = FF4(h8, h5, h6, h7, X[6], 7);
|
||||
h7 = FF4(h7, h8, h5, h6, X[15], 8);
|
||||
h6 = FF4(h6, h7, h8, h5, X[8], 11);
|
||||
h5 = FF4(h5, h6, h7, h8, X[1], 14);
|
||||
h8 = FF4(h8, h5, h6, h7, X[10], 14);
|
||||
h7 = FF4(h7, h8, h5, h6, X[3], 12);
|
||||
h6 = FF4(h6, h7, h8, h5, X[12], 6);
|
||||
int num = h;
|
||||
h = h5;
|
||||
h5 = num;
|
||||
h = F2(h, h2, h3, h4, X[7], 7);
|
||||
h4 = F2(h4, h, h2, h3, X[4], 6);
|
||||
h3 = F2(h3, h4, h, h2, X[13], 8);
|
||||
h2 = F2(h2, h3, h4, h, X[1], 13);
|
||||
h = F2(h, h2, h3, h4, X[10], 11);
|
||||
h4 = F2(h4, h, h2, h3, X[6], 9);
|
||||
h3 = F2(h3, h4, h, h2, X[15], 7);
|
||||
h2 = F2(h2, h3, h4, h, X[3], 15);
|
||||
h = F2(h, h2, h3, h4, X[12], 7);
|
||||
h4 = F2(h4, h, h2, h3, X[0], 12);
|
||||
h3 = F2(h3, h4, h, h2, X[9], 15);
|
||||
h2 = F2(h2, h3, h4, h, X[5], 9);
|
||||
h = F2(h, h2, h3, h4, X[2], 11);
|
||||
h4 = F2(h4, h, h2, h3, X[14], 7);
|
||||
h3 = F2(h3, h4, h, h2, X[11], 13);
|
||||
h2 = F2(h2, h3, h4, h, X[8], 12);
|
||||
h5 = FF3(h5, h6, h7, h8, X[6], 9);
|
||||
h8 = FF3(h8, h5, h6, h7, X[11], 13);
|
||||
h7 = FF3(h7, h8, h5, h6, X[3], 15);
|
||||
h6 = FF3(h6, h7, h8, h5, X[7], 7);
|
||||
h5 = FF3(h5, h6, h7, h8, X[0], 12);
|
||||
h8 = FF3(h8, h5, h6, h7, X[13], 8);
|
||||
h7 = FF3(h7, h8, h5, h6, X[5], 9);
|
||||
h6 = FF3(h6, h7, h8, h5, X[10], 11);
|
||||
h5 = FF3(h5, h6, h7, h8, X[14], 7);
|
||||
h8 = FF3(h8, h5, h6, h7, X[15], 7);
|
||||
h7 = FF3(h7, h8, h5, h6, X[8], 12);
|
||||
h6 = FF3(h6, h7, h8, h5, X[12], 7);
|
||||
h5 = FF3(h5, h6, h7, h8, X[4], 6);
|
||||
h8 = FF3(h8, h5, h6, h7, X[9], 15);
|
||||
h7 = FF3(h7, h8, h5, h6, X[1], 13);
|
||||
h6 = FF3(h6, h7, h8, h5, X[2], 11);
|
||||
num = h2;
|
||||
h2 = h6;
|
||||
h6 = num;
|
||||
h = F3(h, h2, h3, h4, X[3], 11);
|
||||
h4 = F3(h4, h, h2, h3, X[10], 13);
|
||||
h3 = F3(h3, h4, h, h2, X[14], 6);
|
||||
h2 = F3(h2, h3, h4, h, X[4], 7);
|
||||
h = F3(h, h2, h3, h4, X[9], 14);
|
||||
h4 = F3(h4, h, h2, h3, X[15], 9);
|
||||
h3 = F3(h3, h4, h, h2, X[8], 13);
|
||||
h2 = F3(h2, h3, h4, h, X[1], 15);
|
||||
h = F3(h, h2, h3, h4, X[2], 14);
|
||||
h4 = F3(h4, h, h2, h3, X[7], 8);
|
||||
h3 = F3(h3, h4, h, h2, X[0], 13);
|
||||
h2 = F3(h2, h3, h4, h, X[6], 6);
|
||||
h = F3(h, h2, h3, h4, X[13], 5);
|
||||
h4 = F3(h4, h, h2, h3, X[11], 12);
|
||||
h3 = F3(h3, h4, h, h2, X[5], 7);
|
||||
h2 = F3(h2, h3, h4, h, X[12], 5);
|
||||
h5 = FF2(h5, h6, h7, h8, X[15], 9);
|
||||
h8 = FF2(h8, h5, h6, h7, X[5], 7);
|
||||
h7 = FF2(h7, h8, h5, h6, X[1], 15);
|
||||
h6 = FF2(h6, h7, h8, h5, X[3], 11);
|
||||
h5 = FF2(h5, h6, h7, h8, X[7], 8);
|
||||
h8 = FF2(h8, h5, h6, h7, X[14], 6);
|
||||
h7 = FF2(h7, h8, h5, h6, X[6], 6);
|
||||
h6 = FF2(h6, h7, h8, h5, X[9], 14);
|
||||
h5 = FF2(h5, h6, h7, h8, X[11], 12);
|
||||
h8 = FF2(h8, h5, h6, h7, X[8], 13);
|
||||
h7 = FF2(h7, h8, h5, h6, X[12], 5);
|
||||
h6 = FF2(h6, h7, h8, h5, X[2], 14);
|
||||
h5 = FF2(h5, h6, h7, h8, X[10], 13);
|
||||
h8 = FF2(h8, h5, h6, h7, X[0], 13);
|
||||
h7 = FF2(h7, h8, h5, h6, X[4], 7);
|
||||
h6 = FF2(h6, h7, h8, h5, X[13], 5);
|
||||
num = h3;
|
||||
h3 = h7;
|
||||
h7 = num;
|
||||
h = F4(h, h2, h3, h4, X[1], 11);
|
||||
h4 = F4(h4, h, h2, h3, X[9], 12);
|
||||
h3 = F4(h3, h4, h, h2, X[11], 14);
|
||||
h2 = F4(h2, h3, h4, h, X[10], 15);
|
||||
h = F4(h, h2, h3, h4, X[0], 14);
|
||||
h4 = F4(h4, h, h2, h3, X[8], 15);
|
||||
h3 = F4(h3, h4, h, h2, X[12], 9);
|
||||
h2 = F4(h2, h3, h4, h, X[4], 8);
|
||||
h = F4(h, h2, h3, h4, X[13], 9);
|
||||
h4 = F4(h4, h, h2, h3, X[3], 14);
|
||||
h3 = F4(h3, h4, h, h2, X[7], 5);
|
||||
h2 = F4(h2, h3, h4, h, X[15], 6);
|
||||
h = F4(h, h2, h3, h4, X[14], 8);
|
||||
h4 = F4(h4, h, h2, h3, X[5], 6);
|
||||
h3 = F4(h3, h4, h, h2, X[6], 5);
|
||||
h2 = F4(h2, h3, h4, h, X[2], 12);
|
||||
h5 = FF1(h5, h6, h7, h8, X[8], 15);
|
||||
h8 = FF1(h8, h5, h6, h7, X[6], 5);
|
||||
h7 = FF1(h7, h8, h5, h6, X[4], 8);
|
||||
h6 = FF1(h6, h7, h8, h5, X[1], 11);
|
||||
h5 = FF1(h5, h6, h7, h8, X[3], 14);
|
||||
h8 = FF1(h8, h5, h6, h7, X[11], 14);
|
||||
h7 = FF1(h7, h8, h5, h6, X[15], 6);
|
||||
h6 = FF1(h6, h7, h8, h5, X[0], 14);
|
||||
h5 = FF1(h5, h6, h7, h8, X[5], 6);
|
||||
h8 = FF1(h8, h5, h6, h7, X[12], 9);
|
||||
h7 = FF1(h7, h8, h5, h6, X[2], 12);
|
||||
h6 = FF1(h6, h7, h8, h5, X[13], 9);
|
||||
h5 = FF1(h5, h6, h7, h8, X[9], 12);
|
||||
h8 = FF1(h8, h5, h6, h7, X[7], 5);
|
||||
h7 = FF1(h7, h8, h5, h6, X[10], 15);
|
||||
h6 = FF1(h6, h7, h8, h5, X[14], 8);
|
||||
num = h4;
|
||||
h4 = h8;
|
||||
h8 = num;
|
||||
H0 += h;
|
||||
H1 += h2;
|
||||
H2 += h3;
|
||||
H3 += h4;
|
||||
H4 += h5;
|
||||
H5 += h6;
|
||||
H6 += h7;
|
||||
H7 += h8;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new RipeMD256Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
RipeMD256Digest t = (RipeMD256Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,534 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class RipeMD320Digest : GeneralDigest
|
||||
{
|
||||
private const int DigestLength = 40;
|
||||
|
||||
private int H0;
|
||||
|
||||
private int H1;
|
||||
|
||||
private int H2;
|
||||
|
||||
private int H3;
|
||||
|
||||
private int H4;
|
||||
|
||||
private int H5;
|
||||
|
||||
private int H6;
|
||||
|
||||
private int H7;
|
||||
|
||||
private int H8;
|
||||
|
||||
private int H9;
|
||||
|
||||
private int[] X = new int[16];
|
||||
|
||||
private int xOff;
|
||||
|
||||
public override string AlgorithmName => "RIPEMD320";
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 40;
|
||||
}
|
||||
|
||||
public RipeMD320Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public RipeMD320Digest(RipeMD320Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(RipeMD320Digest t)
|
||||
{
|
||||
CopyIn((GeneralDigest)t);
|
||||
H0 = t.H0;
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
H4 = t.H4;
|
||||
H5 = t.H5;
|
||||
H6 = t.H6;
|
||||
H7 = t.H7;
|
||||
H8 = t.H8;
|
||||
H9 = t.H9;
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
X[xOff++] = (input[inOff] & 0xFF) | ((input[inOff + 1] & 0xFF) << 8) | ((input[inOff + 2] & 0xFF) << 16) | ((input[inOff + 3] & 0xFF) << 24);
|
||||
if (xOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
X[14] = (int)(bitLength & 0xFFFFFFFFu);
|
||||
X[15] = (int)(bitLength >>> 32);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
UnpackWord(H0, output, outOff);
|
||||
UnpackWord(H1, output, outOff + 4);
|
||||
UnpackWord(H2, output, outOff + 8);
|
||||
UnpackWord(H3, output, outOff + 12);
|
||||
UnpackWord(H4, output, outOff + 16);
|
||||
UnpackWord(H5, output, outOff + 20);
|
||||
UnpackWord(H6, output, outOff + 24);
|
||||
UnpackWord(H7, output, outOff + 28);
|
||||
UnpackWord(H8, output, outOff + 32);
|
||||
UnpackWord(H9, output, outOff + 36);
|
||||
Reset();
|
||||
return 40;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H0 = 1732584193;
|
||||
H1 = -271733879;
|
||||
H2 = -1732584194;
|
||||
H3 = 271733878;
|
||||
H4 = -1009589776;
|
||||
H5 = 1985229328;
|
||||
H6 = -19088744;
|
||||
H7 = -1985229329;
|
||||
H8 = 19088743;
|
||||
H9 = 1009589775;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private int RL(int x, int n)
|
||||
{
|
||||
return (x << n) | (x >>> 32 - n);
|
||||
}
|
||||
|
||||
private int F1(int x, int y, int z)
|
||||
{
|
||||
return x ^ y ^ z;
|
||||
}
|
||||
|
||||
private int F2(int x, int y, int z)
|
||||
{
|
||||
return (x & y) | (~x & z);
|
||||
}
|
||||
|
||||
private int F3(int x, int y, int z)
|
||||
{
|
||||
return (x | ~y) ^ z;
|
||||
}
|
||||
|
||||
private int F4(int x, int y, int z)
|
||||
{
|
||||
return (x & z) | (y & ~z);
|
||||
}
|
||||
|
||||
private int F5(int x, int y, int z)
|
||||
{
|
||||
return x ^ (y | ~z);
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
int h = H0;
|
||||
int h2 = H1;
|
||||
int h3 = H2;
|
||||
int h4 = H3;
|
||||
int h5 = H4;
|
||||
int h6 = H5;
|
||||
int h7 = H6;
|
||||
int h8 = H7;
|
||||
int h9 = H8;
|
||||
int h10 = H9;
|
||||
h = RL(h + F1(h2, h3, h4) + X[0], 11) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F1(h, h2, h3) + X[1], 14) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F1(h5, h, h2) + X[2], 15) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F1(h4, h5, h) + X[3], 12) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F1(h3, h4, h5) + X[4], 5) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F1(h2, h3, h4) + X[5], 8) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F1(h, h2, h3) + X[6], 7) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F1(h5, h, h2) + X[7], 9) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F1(h4, h5, h) + X[8], 11) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F1(h3, h4, h5) + X[9], 13) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F1(h2, h3, h4) + X[10], 14) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F1(h, h2, h3) + X[11], 15) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F1(h5, h, h2) + X[12], 6) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F1(h4, h5, h) + X[13], 7) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F1(h3, h4, h5) + X[14], 9) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F1(h2, h3, h4) + X[15], 8) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h6 = RL(h6 + F5(h7, h8, h9) + X[5] + 1352829926, 8) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F5(h6, h7, h8) + X[14] + 1352829926, 9) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F5(h10, h6, h7) + X[7] + 1352829926, 9) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F5(h9, h10, h6) + X[0] + 1352829926, 11) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F5(h8, h9, h10) + X[9] + 1352829926, 13) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F5(h7, h8, h9) + X[2] + 1352829926, 15) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F5(h6, h7, h8) + X[11] + 1352829926, 15) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F5(h10, h6, h7) + X[4] + 1352829926, 5) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F5(h9, h10, h6) + X[13] + 1352829926, 7) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F5(h8, h9, h10) + X[6] + 1352829926, 7) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F5(h7, h8, h9) + X[15] + 1352829926, 8) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F5(h6, h7, h8) + X[8] + 1352829926, 11) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F5(h10, h6, h7) + X[1] + 1352829926, 14) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F5(h9, h10, h6) + X[10] + 1352829926, 14) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F5(h8, h9, h10) + X[3] + 1352829926, 12) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F5(h7, h8, h9) + X[12] + 1352829926, 6) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
int num = h;
|
||||
h = h6;
|
||||
h6 = num;
|
||||
h5 = RL(h5 + F2(h, h2, h3) + X[7] + 1518500249, 7) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F2(h5, h, h2) + X[4] + 1518500249, 6) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F2(h4, h5, h) + X[13] + 1518500249, 8) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F2(h3, h4, h5) + X[1] + 1518500249, 13) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F2(h2, h3, h4) + X[10] + 1518500249, 11) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F2(h, h2, h3) + X[6] + 1518500249, 9) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F2(h5, h, h2) + X[15] + 1518500249, 7) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F2(h4, h5, h) + X[3] + 1518500249, 15) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F2(h3, h4, h5) + X[12] + 1518500249, 7) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F2(h2, h3, h4) + X[0] + 1518500249, 12) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F2(h, h2, h3) + X[9] + 1518500249, 15) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F2(h5, h, h2) + X[5] + 1518500249, 9) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F2(h4, h5, h) + X[2] + 1518500249, 11) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F2(h3, h4, h5) + X[14] + 1518500249, 7) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F2(h2, h3, h4) + X[11] + 1518500249, 13) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F2(h, h2, h3) + X[8] + 1518500249, 12) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h10 = RL(h10 + F4(h6, h7, h8) + X[6] + 1548603684, 9) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F4(h10, h6, h7) + X[11] + 1548603684, 13) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F4(h9, h10, h6) + X[3] + 1548603684, 15) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F4(h8, h9, h10) + X[7] + 1548603684, 7) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F4(h7, h8, h9) + X[0] + 1548603684, 12) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F4(h6, h7, h8) + X[13] + 1548603684, 8) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F4(h10, h6, h7) + X[5] + 1548603684, 9) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F4(h9, h10, h6) + X[10] + 1548603684, 11) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F4(h8, h9, h10) + X[14] + 1548603684, 7) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F4(h7, h8, h9) + X[15] + 1548603684, 7) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F4(h6, h7, h8) + X[8] + 1548603684, 12) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F4(h10, h6, h7) + X[12] + 1548603684, 7) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F4(h9, h10, h6) + X[4] + 1548603684, 6) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F4(h8, h9, h10) + X[9] + 1548603684, 15) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F4(h7, h8, h9) + X[1] + 1548603684, 13) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F4(h6, h7, h8) + X[2] + 1548603684, 11) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
num = h2;
|
||||
h2 = h7;
|
||||
h7 = num;
|
||||
h4 = RL(h4 + F3(h5, h, h2) + X[3] + 1859775393, 11) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F3(h4, h5, h) + X[10] + 1859775393, 13) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F3(h3, h4, h5) + X[14] + 1859775393, 6) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F3(h2, h3, h4) + X[4] + 1859775393, 7) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F3(h, h2, h3) + X[9] + 1859775393, 14) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F3(h5, h, h2) + X[15] + 1859775393, 9) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F3(h4, h5, h) + X[8] + 1859775393, 13) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F3(h3, h4, h5) + X[1] + 1859775393, 15) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F3(h2, h3, h4) + X[2] + 1859775393, 14) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F3(h, h2, h3) + X[7] + 1859775393, 8) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F3(h5, h, h2) + X[0] + 1859775393, 13) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F3(h4, h5, h) + X[6] + 1859775393, 6) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F3(h3, h4, h5) + X[13] + 1859775393, 5) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F3(h2, h3, h4) + X[11] + 1859775393, 12) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F3(h, h2, h3) + X[5] + 1859775393, 7) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F3(h5, h, h2) + X[12] + 1859775393, 5) + h3;
|
||||
h = RL(h, 10);
|
||||
h9 = RL(h9 + F3(h10, h6, h7) + X[15] + 1836072691, 9) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F3(h9, h10, h6) + X[5] + 1836072691, 7) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F3(h8, h9, h10) + X[1] + 1836072691, 15) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F3(h7, h8, h9) + X[3] + 1836072691, 11) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F3(h6, h7, h8) + X[7] + 1836072691, 8) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F3(h10, h6, h7) + X[14] + 1836072691, 6) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F3(h9, h10, h6) + X[6] + 1836072691, 6) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F3(h8, h9, h10) + X[9] + 1836072691, 14) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F3(h7, h8, h9) + X[11] + 1836072691, 12) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F3(h6, h7, h8) + X[8] + 1836072691, 13) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F3(h10, h6, h7) + X[12] + 1836072691, 5) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F3(h9, h10, h6) + X[2] + 1836072691, 14) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F3(h8, h9, h10) + X[10] + 1836072691, 13) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F3(h7, h8, h9) + X[0] + 1836072691, 13) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F3(h6, h7, h8) + X[4] + 1836072691, 7) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F3(h10, h6, h7) + X[13] + 1836072691, 5) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
num = h3;
|
||||
h3 = h8;
|
||||
h8 = num;
|
||||
h3 = RL(h3 + F4(h4, h5, h) + X[1] + -1894007588, 11) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F4(h3, h4, h5) + X[9] + -1894007588, 12) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F4(h2, h3, h4) + X[11] + -1894007588, 14) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F4(h, h2, h3) + X[10] + -1894007588, 15) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F4(h5, h, h2) + X[0] + -1894007588, 14) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F4(h4, h5, h) + X[8] + -1894007588, 15) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F4(h3, h4, h5) + X[12] + -1894007588, 9) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F4(h2, h3, h4) + X[4] + -1894007588, 8) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F4(h, h2, h3) + X[13] + -1894007588, 9) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F4(h5, h, h2) + X[3] + -1894007588, 14) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F4(h4, h5, h) + X[7] + -1894007588, 5) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F4(h3, h4, h5) + X[15] + -1894007588, 6) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F4(h2, h3, h4) + X[14] + -1894007588, 8) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F4(h, h2, h3) + X[5] + -1894007588, 6) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F4(h5, h, h2) + X[6] + -1894007588, 5) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F4(h4, h5, h) + X[2] + -1894007588, 12) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h8 = RL(h8 + F2(h9, h10, h6) + X[8] + 2053994217, 15) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F2(h8, h9, h10) + X[6] + 2053994217, 5) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F2(h7, h8, h9) + X[4] + 2053994217, 8) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F2(h6, h7, h8) + X[1] + 2053994217, 11) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F2(h10, h6, h7) + X[3] + 2053994217, 14) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F2(h9, h10, h6) + X[11] + 2053994217, 14) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F2(h8, h9, h10) + X[15] + 2053994217, 6) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F2(h7, h8, h9) + X[0] + 2053994217, 14) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F2(h6, h7, h8) + X[5] + 2053994217, 6) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F2(h10, h6, h7) + X[12] + 2053994217, 9) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F2(h9, h10, h6) + X[2] + 2053994217, 12) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F2(h8, h9, h10) + X[13] + 2053994217, 9) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F2(h7, h8, h9) + X[9] + 2053994217, 12) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F2(h6, h7, h8) + X[7] + 2053994217, 5) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F2(h10, h6, h7) + X[10] + 2053994217, 15) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F2(h9, h10, h6) + X[14] + 2053994217, 8) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
num = h4;
|
||||
h4 = h9;
|
||||
h9 = num;
|
||||
h2 = RL(h2 + F5(h3, h4, h5) + X[4] + -1454113458, 9) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F5(h2, h3, h4) + X[0] + -1454113458, 15) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F5(h, h2, h3) + X[5] + -1454113458, 5) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F5(h5, h, h2) + X[9] + -1454113458, 11) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F5(h4, h5, h) + X[7] + -1454113458, 6) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F5(h3, h4, h5) + X[12] + -1454113458, 8) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F5(h2, h3, h4) + X[2] + -1454113458, 13) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F5(h, h2, h3) + X[10] + -1454113458, 12) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F5(h5, h, h2) + X[14] + -1454113458, 5) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F5(h4, h5, h) + X[1] + -1454113458, 12) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F5(h3, h4, h5) + X[3] + -1454113458, 13) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h = RL(h + F5(h2, h3, h4) + X[8] + -1454113458, 14) + h5;
|
||||
h3 = RL(h3, 10);
|
||||
h5 = RL(h5 + F5(h, h2, h3) + X[11] + -1454113458, 11) + h4;
|
||||
h2 = RL(h2, 10);
|
||||
h4 = RL(h4 + F5(h5, h, h2) + X[6] + -1454113458, 8) + h3;
|
||||
h = RL(h, 10);
|
||||
h3 = RL(h3 + F5(h4, h5, h) + X[15] + -1454113458, 5) + h2;
|
||||
h5 = RL(h5, 10);
|
||||
h2 = RL(h2 + F5(h3, h4, h5) + X[13] + -1454113458, 6) + h;
|
||||
h4 = RL(h4, 10);
|
||||
h7 = RL(h7 + F1(h8, h9, h10) + X[12], 8) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F1(h7, h8, h9) + X[15], 5) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F1(h6, h7, h8) + X[10], 12) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F1(h10, h6, h7) + X[4], 9) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F1(h9, h10, h6) + X[1], 12) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F1(h8, h9, h10) + X[5], 5) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F1(h7, h8, h9) + X[8], 14) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F1(h6, h7, h8) + X[7], 6) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F1(h10, h6, h7) + X[6], 8) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F1(h9, h10, h6) + X[2], 13) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F1(h8, h9, h10) + X[13], 6) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
h6 = RL(h6 + F1(h7, h8, h9) + X[14], 5) + h10;
|
||||
h8 = RL(h8, 10);
|
||||
h10 = RL(h10 + F1(h6, h7, h8) + X[0], 15) + h9;
|
||||
h7 = RL(h7, 10);
|
||||
h9 = RL(h9 + F1(h10, h6, h7) + X[3], 13) + h8;
|
||||
h6 = RL(h6, 10);
|
||||
h8 = RL(h8 + F1(h9, h10, h6) + X[9], 11) + h7;
|
||||
h10 = RL(h10, 10);
|
||||
h7 = RL(h7 + F1(h8, h9, h10) + X[11], 11) + h6;
|
||||
h9 = RL(h9, 10);
|
||||
H0 += h;
|
||||
H1 += h2;
|
||||
H2 += h3;
|
||||
H3 += h4;
|
||||
H4 += h10;
|
||||
H5 += h6;
|
||||
H6 += h7;
|
||||
H7 += h8;
|
||||
H8 += h9;
|
||||
H9 += h5;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != X.Length; i++)
|
||||
{
|
||||
X[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new RipeMD320Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
RipeMD320Digest t = (RipeMD320Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class SM3Digest : GeneralDigest
|
||||
{
|
||||
private const int DIGEST_LENGTH = 32;
|
||||
|
||||
private const int BLOCK_SIZE = 16;
|
||||
|
||||
private uint[] V = new uint[8];
|
||||
|
||||
private uint[] inwords = new uint[16];
|
||||
|
||||
private int xOff;
|
||||
|
||||
private uint[] W = new uint[68];
|
||||
|
||||
private static readonly uint[] T;
|
||||
|
||||
public override string AlgorithmName => "SM3";
|
||||
|
||||
static SM3Digest()
|
||||
{
|
||||
T = new uint[64];
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
uint num = 2043430169u;
|
||||
T[i] = (num << i) | (num >> 32 - i);
|
||||
}
|
||||
for (int j = 16; j < 64; j++)
|
||||
{
|
||||
int num2 = j % 32;
|
||||
uint num3 = 2055708042u;
|
||||
T[j] = (num3 << num2) | (num3 >> 32 - num2);
|
||||
}
|
||||
}
|
||||
|
||||
public SM3Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public SM3Digest(SM3Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(SM3Digest t)
|
||||
{
|
||||
Array.Copy(t.V, 0, V, 0, V.Length);
|
||||
Array.Copy(t.inwords, 0, inwords, 0, inwords.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new SM3Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
SM3Digest t = (SM3Digest)other;
|
||||
CopyIn((GeneralDigest)t);
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
V[0] = 1937774191u;
|
||||
V[1] = 1226093241u;
|
||||
V[2] = 388252375u;
|
||||
V[3] = 3666478592u;
|
||||
V[4] = 2842636476u;
|
||||
V[5] = 372324522u;
|
||||
V[6] = 3817729613u;
|
||||
V[7] = 2969243214u;
|
||||
xOff = 0;
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
Pack.UInt32_To_BE(V, output, outOff);
|
||||
Reset();
|
||||
return 32;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
uint num = Pack.BE_To_UInt32(input, inOff);
|
||||
inwords[xOff] = num;
|
||||
xOff++;
|
||||
if (xOff >= 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
inwords[xOff] = 0u;
|
||||
xOff++;
|
||||
ProcessBlock();
|
||||
}
|
||||
while (xOff < 14)
|
||||
{
|
||||
inwords[xOff] = 0u;
|
||||
xOff++;
|
||||
}
|
||||
inwords[xOff++] = (uint)(bitLength >> 32);
|
||||
inwords[xOff++] = (uint)bitLength;
|
||||
}
|
||||
|
||||
private uint P0(uint x)
|
||||
{
|
||||
uint num = (x << 9) | (x >> 23);
|
||||
uint num2 = (x << 17) | (x >> 15);
|
||||
return x ^ num ^ num2;
|
||||
}
|
||||
|
||||
private uint P1(uint x)
|
||||
{
|
||||
uint num = (x << 15) | (x >> 17);
|
||||
uint num2 = (x << 23) | (x >> 9);
|
||||
return x ^ num ^ num2;
|
||||
}
|
||||
|
||||
private uint FF0(uint x, uint y, uint z)
|
||||
{
|
||||
return x ^ y ^ z;
|
||||
}
|
||||
|
||||
private uint FF1(uint x, uint y, uint z)
|
||||
{
|
||||
return (x & y) | (x & z) | (y & z);
|
||||
}
|
||||
|
||||
private uint GG0(uint x, uint y, uint z)
|
||||
{
|
||||
return x ^ y ^ z;
|
||||
}
|
||||
|
||||
private uint GG1(uint x, uint y, uint z)
|
||||
{
|
||||
return (x & y) | (~x & z);
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
W[i] = inwords[i];
|
||||
}
|
||||
for (int j = 16; j < 68; j++)
|
||||
{
|
||||
uint num = W[j - 3];
|
||||
uint num2 = (num << 15) | (num >> 17);
|
||||
uint num3 = W[j - 13];
|
||||
uint num4 = (num3 << 7) | (num3 >> 25);
|
||||
W[j] = P1(W[j - 16] ^ W[j - 9] ^ num2) ^ num4 ^ W[j - 6];
|
||||
}
|
||||
uint num5 = V[0];
|
||||
uint num6 = V[1];
|
||||
uint num7 = V[2];
|
||||
uint num8 = V[3];
|
||||
uint num9 = V[4];
|
||||
uint num10 = V[5];
|
||||
uint num11 = V[6];
|
||||
uint num12 = V[7];
|
||||
for (int k = 0; k < 16; k++)
|
||||
{
|
||||
uint num13 = (num5 << 12) | (num5 >> 20);
|
||||
uint num14 = num13 + num9 + T[k];
|
||||
uint num15 = (num14 << 7) | (num14 >> 25);
|
||||
uint num16 = num15 ^ num13;
|
||||
uint num17 = W[k];
|
||||
uint num18 = num17 ^ W[k + 4];
|
||||
uint num19 = FF0(num5, num6, num7) + num8 + num16 + num18;
|
||||
uint x = GG0(num9, num10, num11) + num12 + num15 + num17;
|
||||
num8 = num7;
|
||||
num7 = (num6 << 9) | (num6 >> 23);
|
||||
num6 = num5;
|
||||
num5 = num19;
|
||||
num12 = num11;
|
||||
num11 = (num10 << 19) | (num10 >> 13);
|
||||
num10 = num9;
|
||||
num9 = P0(x);
|
||||
}
|
||||
for (int l = 16; l < 64; l++)
|
||||
{
|
||||
uint num20 = (num5 << 12) | (num5 >> 20);
|
||||
uint num21 = num20 + num9 + T[l];
|
||||
uint num22 = (num21 << 7) | (num21 >> 25);
|
||||
uint num23 = num22 ^ num20;
|
||||
uint num24 = W[l];
|
||||
uint num25 = num24 ^ W[l + 4];
|
||||
uint num26 = FF1(num5, num6, num7) + num8 + num23 + num25;
|
||||
uint x2 = GG1(num9, num10, num11) + num12 + num22 + num24;
|
||||
num8 = num7;
|
||||
num7 = (num6 << 9) | (num6 >> 23);
|
||||
num6 = num5;
|
||||
num5 = num26;
|
||||
num12 = num11;
|
||||
num11 = (num10 << 19) | (num10 >> 13);
|
||||
num10 = num9;
|
||||
num9 = P0(x2);
|
||||
}
|
||||
uint[] v;
|
||||
(v = V)[0] = v[0] ^ num5;
|
||||
(v = V)[1] = v[1] ^ num6;
|
||||
(v = V)[2] = v[2] ^ num7;
|
||||
(v = V)[3] = v[3] ^ num8;
|
||||
(v = V)[4] = v[4] ^ num9;
|
||||
(v = V)[5] = v[5] ^ num10;
|
||||
(v = V)[6] = v[6] ^ num11;
|
||||
(v = V)[7] = v[7] ^ num12;
|
||||
xOff = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Sha1Digest : GeneralDigest
|
||||
{
|
||||
private const int DigestLength = 20;
|
||||
|
||||
private const uint Y1 = 1518500249u;
|
||||
|
||||
private const uint Y2 = 1859775393u;
|
||||
|
||||
private const uint Y3 = 2400959708u;
|
||||
|
||||
private const uint Y4 = 3395469782u;
|
||||
|
||||
private uint H1;
|
||||
|
||||
private uint H2;
|
||||
|
||||
private uint H3;
|
||||
|
||||
private uint H4;
|
||||
|
||||
private uint H5;
|
||||
|
||||
private uint[] X = new uint[80];
|
||||
|
||||
private int xOff;
|
||||
|
||||
public override string AlgorithmName => "SHA-1";
|
||||
|
||||
public Sha1Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public Sha1Digest(Sha1Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(Sha1Digest t)
|
||||
{
|
||||
CopyIn((GeneralDigest)t);
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
H4 = t.H4;
|
||||
H5 = t.H5;
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 20;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
X[xOff] = Pack.BE_To_UInt32(input, inOff);
|
||||
if (++xOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
X[14] = (uint)((ulong)bitLength >> 32);
|
||||
X[15] = (uint)bitLength;
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
Pack.UInt32_To_BE(H1, output, outOff);
|
||||
Pack.UInt32_To_BE(H2, output, outOff + 4);
|
||||
Pack.UInt32_To_BE(H3, output, outOff + 8);
|
||||
Pack.UInt32_To_BE(H4, output, outOff + 12);
|
||||
Pack.UInt32_To_BE(H5, output, outOff + 16);
|
||||
Reset();
|
||||
return 20;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H1 = 1732584193u;
|
||||
H2 = 4023233417u;
|
||||
H3 = 2562383102u;
|
||||
H4 = 271733878u;
|
||||
H5 = 3285377520u;
|
||||
xOff = 0;
|
||||
Array.Clear(X, 0, X.Length);
|
||||
}
|
||||
|
||||
private static uint F(uint u, uint v, uint w)
|
||||
{
|
||||
return (u & v) | (~u & w);
|
||||
}
|
||||
|
||||
private static uint H(uint u, uint v, uint w)
|
||||
{
|
||||
return u ^ v ^ w;
|
||||
}
|
||||
|
||||
private static uint G(uint u, uint v, uint w)
|
||||
{
|
||||
return (u & v) | (u & w) | (v & w);
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
for (int i = 16; i < 80; i++)
|
||||
{
|
||||
uint num = X[i - 3] ^ X[i - 8] ^ X[i - 14] ^ X[i - 16];
|
||||
X[i] = (num << 1) | (num >> 31);
|
||||
}
|
||||
uint num2 = H1;
|
||||
uint num3 = H2;
|
||||
uint num4 = H3;
|
||||
uint num5 = H4;
|
||||
uint num6 = H5;
|
||||
int num7 = 0;
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
num6 += ((num2 << 5) | (num2 >> 27)) + F(num3, num4, num5) + X[num7++] + 1518500249;
|
||||
num3 = (num3 << 30) | (num3 >> 2);
|
||||
num5 += ((num6 << 5) | (num6 >> 27)) + F(num2, num3, num4) + X[num7++] + 1518500249;
|
||||
num2 = (num2 << 30) | (num2 >> 2);
|
||||
num4 += ((num5 << 5) | (num5 >> 27)) + F(num6, num2, num3) + X[num7++] + 1518500249;
|
||||
num6 = (num6 << 30) | (num6 >> 2);
|
||||
num3 += ((num4 << 5) | (num4 >> 27)) + F(num5, num6, num2) + X[num7++] + 1518500249;
|
||||
num5 = (num5 << 30) | (num5 >> 2);
|
||||
num2 += ((num3 << 5) | (num3 >> 27)) + F(num4, num5, num6) + X[num7++] + 1518500249;
|
||||
num4 = (num4 << 30) | (num4 >> 2);
|
||||
}
|
||||
for (int k = 0; k < 4; k++)
|
||||
{
|
||||
num6 += ((num2 << 5) | (num2 >> 27)) + H(num3, num4, num5) + X[num7++] + 1859775393;
|
||||
num3 = (num3 << 30) | (num3 >> 2);
|
||||
num5 += ((num6 << 5) | (num6 >> 27)) + H(num2, num3, num4) + X[num7++] + 1859775393;
|
||||
num2 = (num2 << 30) | (num2 >> 2);
|
||||
num4 += ((num5 << 5) | (num5 >> 27)) + H(num6, num2, num3) + X[num7++] + 1859775393;
|
||||
num6 = (num6 << 30) | (num6 >> 2);
|
||||
num3 += ((num4 << 5) | (num4 >> 27)) + H(num5, num6, num2) + X[num7++] + 1859775393;
|
||||
num5 = (num5 << 30) | (num5 >> 2);
|
||||
num2 += ((num3 << 5) | (num3 >> 27)) + H(num4, num5, num6) + X[num7++] + 1859775393;
|
||||
num4 = (num4 << 30) | (num4 >> 2);
|
||||
}
|
||||
for (int l = 0; l < 4; l++)
|
||||
{
|
||||
num6 += (uint)((int)(((num2 << 5) | (num2 >> 27)) + G(num3, num4, num5) + X[num7++]) + -1894007588);
|
||||
num3 = (num3 << 30) | (num3 >> 2);
|
||||
num5 += (uint)((int)(((num6 << 5) | (num6 >> 27)) + G(num2, num3, num4) + X[num7++]) + -1894007588);
|
||||
num2 = (num2 << 30) | (num2 >> 2);
|
||||
num4 += (uint)((int)(((num5 << 5) | (num5 >> 27)) + G(num6, num2, num3) + X[num7++]) + -1894007588);
|
||||
num6 = (num6 << 30) | (num6 >> 2);
|
||||
num3 += (uint)((int)(((num4 << 5) | (num4 >> 27)) + G(num5, num6, num2) + X[num7++]) + -1894007588);
|
||||
num5 = (num5 << 30) | (num5 >> 2);
|
||||
num2 += (uint)((int)(((num3 << 5) | (num3 >> 27)) + G(num4, num5, num6) + X[num7++]) + -1894007588);
|
||||
num4 = (num4 << 30) | (num4 >> 2);
|
||||
}
|
||||
for (int m = 0; m < 4; m++)
|
||||
{
|
||||
num6 += (uint)((int)(((num2 << 5) | (num2 >> 27)) + H(num3, num4, num5) + X[num7++]) + -899497514);
|
||||
num3 = (num3 << 30) | (num3 >> 2);
|
||||
num5 += (uint)((int)(((num6 << 5) | (num6 >> 27)) + H(num2, num3, num4) + X[num7++]) + -899497514);
|
||||
num2 = (num2 << 30) | (num2 >> 2);
|
||||
num4 += (uint)((int)(((num5 << 5) | (num5 >> 27)) + H(num6, num2, num3) + X[num7++]) + -899497514);
|
||||
num6 = (num6 << 30) | (num6 >> 2);
|
||||
num3 += (uint)((int)(((num4 << 5) | (num4 >> 27)) + H(num5, num6, num2) + X[num7++]) + -899497514);
|
||||
num5 = (num5 << 30) | (num5 >> 2);
|
||||
num2 += (uint)((int)(((num3 << 5) | (num3 >> 27)) + H(num4, num5, num6) + X[num7++]) + -899497514);
|
||||
num4 = (num4 << 30) | (num4 >> 2);
|
||||
}
|
||||
H1 += num2;
|
||||
H2 += num3;
|
||||
H3 += num4;
|
||||
H4 += num5;
|
||||
H5 += num6;
|
||||
xOff = 0;
|
||||
Array.Clear(X, 0, 16);
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new Sha1Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
Sha1Digest t = (Sha1Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Sha224Digest : GeneralDigest
|
||||
{
|
||||
private const int DigestLength = 28;
|
||||
|
||||
private uint H1;
|
||||
|
||||
private uint H2;
|
||||
|
||||
private uint H3;
|
||||
|
||||
private uint H4;
|
||||
|
||||
private uint H5;
|
||||
|
||||
private uint H6;
|
||||
|
||||
private uint H7;
|
||||
|
||||
private uint H8;
|
||||
|
||||
private uint[] X = new uint[64];
|
||||
|
||||
private int xOff;
|
||||
|
||||
internal static readonly uint[] K = new uint[64]
|
||||
{
|
||||
1116352408u, 1899447441u, 3049323471u, 3921009573u, 961987163u, 1508970993u, 2453635748u, 2870763221u, 3624381080u, 310598401u,
|
||||
607225278u, 1426881987u, 1925078388u, 2162078206u, 2614888103u, 3248222580u, 3835390401u, 4022224774u, 264347078u, 604807628u,
|
||||
770255983u, 1249150122u, 1555081692u, 1996064986u, 2554220882u, 2821834349u, 2952996808u, 3210313671u, 3336571891u, 3584528711u,
|
||||
113926993u, 338241895u, 666307205u, 773529912u, 1294757372u, 1396182291u, 1695183700u, 1986661051u, 2177026350u, 2456956037u,
|
||||
2730485921u, 2820302411u, 3259730800u, 3345764771u, 3516065817u, 3600352804u, 4094571909u, 275423344u, 430227734u, 506948616u,
|
||||
659060556u, 883997877u, 958139571u, 1322822218u, 1537002063u, 1747873779u, 1955562222u, 2024104815u, 2227730452u, 2361852424u,
|
||||
2428436474u, 2756734187u, 3204031479u, 3329325298u
|
||||
};
|
||||
|
||||
public override string AlgorithmName => "SHA-224";
|
||||
|
||||
public Sha224Digest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public Sha224Digest(Sha224Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(Sha224Digest t)
|
||||
{
|
||||
CopyIn((GeneralDigest)t);
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
H4 = t.H4;
|
||||
H5 = t.H5;
|
||||
H6 = t.H6;
|
||||
H7 = t.H7;
|
||||
H8 = t.H8;
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 28;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
X[xOff] = Pack.BE_To_UInt32(input, inOff);
|
||||
if (++xOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
X[14] = (uint)((ulong)bitLength >> 32);
|
||||
X[15] = (uint)bitLength;
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
Pack.UInt32_To_BE(H1, output, outOff);
|
||||
Pack.UInt32_To_BE(H2, output, outOff + 4);
|
||||
Pack.UInt32_To_BE(H3, output, outOff + 8);
|
||||
Pack.UInt32_To_BE(H4, output, outOff + 12);
|
||||
Pack.UInt32_To_BE(H5, output, outOff + 16);
|
||||
Pack.UInt32_To_BE(H6, output, outOff + 20);
|
||||
Pack.UInt32_To_BE(H7, output, outOff + 24);
|
||||
Reset();
|
||||
return 28;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H1 = 3238371032u;
|
||||
H2 = 914150663u;
|
||||
H3 = 812702999u;
|
||||
H4 = 4144912697u;
|
||||
H5 = 4290775857u;
|
||||
H6 = 1750603025u;
|
||||
H7 = 1694076839u;
|
||||
H8 = 3204075428u;
|
||||
xOff = 0;
|
||||
Array.Clear(X, 0, X.Length);
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
for (int i = 16; i <= 63; i++)
|
||||
{
|
||||
X[i] = Theta1(X[i - 2]) + X[i - 7] + Theta0(X[i - 15]) + X[i - 16];
|
||||
}
|
||||
uint num = H1;
|
||||
uint num2 = H2;
|
||||
uint num3 = H3;
|
||||
uint num4 = H4;
|
||||
uint num5 = H5;
|
||||
uint num6 = H6;
|
||||
uint num7 = H7;
|
||||
uint num8 = H8;
|
||||
int num9 = 0;
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
num8 += Sum1(num5) + Ch(num5, num6, num7) + K[num9] + X[num9];
|
||||
num4 += num8;
|
||||
num8 += Sum0(num) + Maj(num, num2, num3);
|
||||
num9++;
|
||||
num7 += Sum1(num4) + Ch(num4, num5, num6) + K[num9] + X[num9];
|
||||
num3 += num7;
|
||||
num7 += Sum0(num8) + Maj(num8, num, num2);
|
||||
num9++;
|
||||
num6 += Sum1(num3) + Ch(num3, num4, num5) + K[num9] + X[num9];
|
||||
num2 += num6;
|
||||
num6 += Sum0(num7) + Maj(num7, num8, num);
|
||||
num9++;
|
||||
num5 += Sum1(num2) + Ch(num2, num3, num4) + K[num9] + X[num9];
|
||||
num += num5;
|
||||
num5 += Sum0(num6) + Maj(num6, num7, num8);
|
||||
num9++;
|
||||
num4 += Sum1(num) + Ch(num, num2, num3) + K[num9] + X[num9];
|
||||
num8 += num4;
|
||||
num4 += Sum0(num5) + Maj(num5, num6, num7);
|
||||
num9++;
|
||||
num3 += Sum1(num8) + Ch(num8, num, num2) + K[num9] + X[num9];
|
||||
num7 += num3;
|
||||
num3 += Sum0(num4) + Maj(num4, num5, num6);
|
||||
num9++;
|
||||
num2 += Sum1(num7) + Ch(num7, num8, num) + K[num9] + X[num9];
|
||||
num6 += num2;
|
||||
num2 += Sum0(num3) + Maj(num3, num4, num5);
|
||||
num9++;
|
||||
num += Sum1(num6) + Ch(num6, num7, num8) + K[num9] + X[num9];
|
||||
num5 += num;
|
||||
num += Sum0(num2) + Maj(num2, num3, num4);
|
||||
num9++;
|
||||
}
|
||||
H1 += num;
|
||||
H2 += num2;
|
||||
H3 += num3;
|
||||
H4 += num4;
|
||||
H5 += num5;
|
||||
H6 += num6;
|
||||
H7 += num7;
|
||||
H8 += num8;
|
||||
xOff = 0;
|
||||
Array.Clear(X, 0, 16);
|
||||
}
|
||||
|
||||
private static uint Ch(uint x, uint y, uint z)
|
||||
{
|
||||
return (x & y) ^ (~x & z);
|
||||
}
|
||||
|
||||
private static uint Maj(uint x, uint y, uint z)
|
||||
{
|
||||
return (x & y) ^ (x & z) ^ (y & z);
|
||||
}
|
||||
|
||||
private static uint Sum0(uint x)
|
||||
{
|
||||
return ((x >> 2) | (x << 30)) ^ ((x >> 13) | (x << 19)) ^ ((x >> 22) | (x << 10));
|
||||
}
|
||||
|
||||
private static uint Sum1(uint x)
|
||||
{
|
||||
return ((x >> 6) | (x << 26)) ^ ((x >> 11) | (x << 21)) ^ ((x >> 25) | (x << 7));
|
||||
}
|
||||
|
||||
private static uint Theta0(uint x)
|
||||
{
|
||||
return ((x >> 7) | (x << 25)) ^ ((x >> 18) | (x << 14)) ^ (x >> 3);
|
||||
}
|
||||
|
||||
private static uint Theta1(uint x)
|
||||
{
|
||||
return ((x >> 17) | (x << 15)) ^ ((x >> 19) | (x << 13)) ^ (x >> 10);
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new Sha224Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
Sha224Digest t = (Sha224Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Sha256Digest : GeneralDigest
|
||||
{
|
||||
private const int DigestLength = 32;
|
||||
|
||||
private uint H1;
|
||||
|
||||
private uint H2;
|
||||
|
||||
private uint H3;
|
||||
|
||||
private uint H4;
|
||||
|
||||
private uint H5;
|
||||
|
||||
private uint H6;
|
||||
|
||||
private uint H7;
|
||||
|
||||
private uint H8;
|
||||
|
||||
private uint[] X = new uint[64];
|
||||
|
||||
private int xOff;
|
||||
|
||||
private static readonly uint[] K = new uint[64]
|
||||
{
|
||||
1116352408u, 1899447441u, 3049323471u, 3921009573u, 961987163u, 1508970993u, 2453635748u, 2870763221u, 3624381080u, 310598401u,
|
||||
607225278u, 1426881987u, 1925078388u, 2162078206u, 2614888103u, 3248222580u, 3835390401u, 4022224774u, 264347078u, 604807628u,
|
||||
770255983u, 1249150122u, 1555081692u, 1996064986u, 2554220882u, 2821834349u, 2952996808u, 3210313671u, 3336571891u, 3584528711u,
|
||||
113926993u, 338241895u, 666307205u, 773529912u, 1294757372u, 1396182291u, 1695183700u, 1986661051u, 2177026350u, 2456956037u,
|
||||
2730485921u, 2820302411u, 3259730800u, 3345764771u, 3516065817u, 3600352804u, 4094571909u, 275423344u, 430227734u, 506948616u,
|
||||
659060556u, 883997877u, 958139571u, 1322822218u, 1537002063u, 1747873779u, 1955562222u, 2024104815u, 2227730452u, 2361852424u,
|
||||
2428436474u, 2756734187u, 3204031479u, 3329325298u
|
||||
};
|
||||
|
||||
public override string AlgorithmName => "SHA-256";
|
||||
|
||||
public Sha256Digest()
|
||||
{
|
||||
initHs();
|
||||
}
|
||||
|
||||
public Sha256Digest(Sha256Digest t)
|
||||
: base(t)
|
||||
{
|
||||
CopyIn(t);
|
||||
}
|
||||
|
||||
private void CopyIn(Sha256Digest t)
|
||||
{
|
||||
CopyIn((GeneralDigest)t);
|
||||
H1 = t.H1;
|
||||
H2 = t.H2;
|
||||
H3 = t.H3;
|
||||
H4 = t.H4;
|
||||
H5 = t.H5;
|
||||
H6 = t.H6;
|
||||
H7 = t.H7;
|
||||
H8 = t.H8;
|
||||
Array.Copy(t.X, 0, X, 0, t.X.Length);
|
||||
xOff = t.xOff;
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
internal override void ProcessWord(byte[] input, int inOff)
|
||||
{
|
||||
X[xOff] = Pack.BE_To_UInt32(input, inOff);
|
||||
if (++xOff == 16)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
internal override void ProcessLength(long bitLength)
|
||||
{
|
||||
if (xOff > 14)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
X[14] = (uint)((ulong)bitLength >> 32);
|
||||
X[15] = (uint)bitLength;
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
Pack.UInt32_To_BE(H1, output, outOff);
|
||||
Pack.UInt32_To_BE(H2, output, outOff + 4);
|
||||
Pack.UInt32_To_BE(H3, output, outOff + 8);
|
||||
Pack.UInt32_To_BE(H4, output, outOff + 12);
|
||||
Pack.UInt32_To_BE(H5, output, outOff + 16);
|
||||
Pack.UInt32_To_BE(H6, output, outOff + 20);
|
||||
Pack.UInt32_To_BE(H7, output, outOff + 24);
|
||||
Pack.UInt32_To_BE(H8, output, outOff + 28);
|
||||
Reset();
|
||||
return 32;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
initHs();
|
||||
xOff = 0;
|
||||
Array.Clear(X, 0, X.Length);
|
||||
}
|
||||
|
||||
private void initHs()
|
||||
{
|
||||
H1 = 1779033703u;
|
||||
H2 = 3144134277u;
|
||||
H3 = 1013904242u;
|
||||
H4 = 2773480762u;
|
||||
H5 = 1359893119u;
|
||||
H6 = 2600822924u;
|
||||
H7 = 528734635u;
|
||||
H8 = 1541459225u;
|
||||
}
|
||||
|
||||
internal override void ProcessBlock()
|
||||
{
|
||||
for (int i = 16; i <= 63; i++)
|
||||
{
|
||||
X[i] = Theta1(X[i - 2]) + X[i - 7] + Theta0(X[i - 15]) + X[i - 16];
|
||||
}
|
||||
uint num = H1;
|
||||
uint num2 = H2;
|
||||
uint num3 = H3;
|
||||
uint num4 = H4;
|
||||
uint num5 = H5;
|
||||
uint num6 = H6;
|
||||
uint num7 = H7;
|
||||
uint num8 = H8;
|
||||
int num9 = 0;
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
num8 += Sum1Ch(num5, num6, num7) + K[num9] + X[num9];
|
||||
num4 += num8;
|
||||
num8 += Sum0Maj(num, num2, num3);
|
||||
num9++;
|
||||
num7 += Sum1Ch(num4, num5, num6) + K[num9] + X[num9];
|
||||
num3 += num7;
|
||||
num7 += Sum0Maj(num8, num, num2);
|
||||
num9++;
|
||||
num6 += Sum1Ch(num3, num4, num5) + K[num9] + X[num9];
|
||||
num2 += num6;
|
||||
num6 += Sum0Maj(num7, num8, num);
|
||||
num9++;
|
||||
num5 += Sum1Ch(num2, num3, num4) + K[num9] + X[num9];
|
||||
num += num5;
|
||||
num5 += Sum0Maj(num6, num7, num8);
|
||||
num9++;
|
||||
num4 += Sum1Ch(num, num2, num3) + K[num9] + X[num9];
|
||||
num8 += num4;
|
||||
num4 += Sum0Maj(num5, num6, num7);
|
||||
num9++;
|
||||
num3 += Sum1Ch(num8, num, num2) + K[num9] + X[num9];
|
||||
num7 += num3;
|
||||
num3 += Sum0Maj(num4, num5, num6);
|
||||
num9++;
|
||||
num2 += Sum1Ch(num7, num8, num) + K[num9] + X[num9];
|
||||
num6 += num2;
|
||||
num2 += Sum0Maj(num3, num4, num5);
|
||||
num9++;
|
||||
num += Sum1Ch(num6, num7, num8) + K[num9] + X[num9];
|
||||
num5 += num;
|
||||
num += Sum0Maj(num2, num3, num4);
|
||||
num9++;
|
||||
}
|
||||
H1 += num;
|
||||
H2 += num2;
|
||||
H3 += num3;
|
||||
H4 += num4;
|
||||
H5 += num5;
|
||||
H6 += num6;
|
||||
H7 += num7;
|
||||
H8 += num8;
|
||||
xOff = 0;
|
||||
Array.Clear(X, 0, 16);
|
||||
}
|
||||
|
||||
private static uint Sum1Ch(uint x, uint y, uint z)
|
||||
{
|
||||
return (((x >> 6) | (x << 26)) ^ ((x >> 11) | (x << 21)) ^ ((x >> 25) | (x << 7))) + ((x & y) ^ (~x & z));
|
||||
}
|
||||
|
||||
private static uint Sum0Maj(uint x, uint y, uint z)
|
||||
{
|
||||
return (((x >> 2) | (x << 30)) ^ ((x >> 13) | (x << 19)) ^ ((x >> 22) | (x << 10))) + ((x & y) ^ (x & z) ^ (y & z));
|
||||
}
|
||||
|
||||
private static uint Theta0(uint x)
|
||||
{
|
||||
return ((x >> 7) | (x << 25)) ^ ((x >> 18) | (x << 14)) ^ (x >> 3);
|
||||
}
|
||||
|
||||
private static uint Theta1(uint x)
|
||||
{
|
||||
return ((x >> 17) | (x << 15)) ^ ((x >> 19) | (x << 13)) ^ (x >> 10);
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new Sha256Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
Sha256Digest t = (Sha256Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Sha384Digest : LongDigest
|
||||
{
|
||||
private const int DigestLength = 48;
|
||||
|
||||
public override string AlgorithmName => "SHA-384";
|
||||
|
||||
public Sha384Digest()
|
||||
{
|
||||
}
|
||||
|
||||
public Sha384Digest(Sha384Digest t)
|
||||
: base(t)
|
||||
{
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 48;
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
Pack.UInt64_To_BE(H1, output, outOff);
|
||||
Pack.UInt64_To_BE(H2, output, outOff + 8);
|
||||
Pack.UInt64_To_BE(H3, output, outOff + 16);
|
||||
Pack.UInt64_To_BE(H4, output, outOff + 24);
|
||||
Pack.UInt64_To_BE(H5, output, outOff + 32);
|
||||
Pack.UInt64_To_BE(H6, output, outOff + 40);
|
||||
Reset();
|
||||
return 48;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H1 = 14680500436340154072uL;
|
||||
H2 = 7105036623409894663uL;
|
||||
H3 = 10473403895298186519uL;
|
||||
H4 = 1526699215303891257uL;
|
||||
H5 = 7436329637833083697uL;
|
||||
H6 = 10282925794625328401uL;
|
||||
H7 = 15784041429090275239uL;
|
||||
H8 = 5167115440072839076uL;
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new Sha384Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
Sha384Digest t = (Sha384Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Sha3Digest : KeccakDigest
|
||||
{
|
||||
public override string AlgorithmName => "SHA3-" + fixedOutputLength;
|
||||
|
||||
private static int CheckBitLength(int bitLength)
|
||||
{
|
||||
switch (bitLength)
|
||||
{
|
||||
case 224:
|
||||
case 256:
|
||||
case 384:
|
||||
case 512:
|
||||
return bitLength;
|
||||
default:
|
||||
throw new ArgumentException(bitLength + " not supported for SHA-3", "bitLength");
|
||||
}
|
||||
}
|
||||
|
||||
public Sha3Digest()
|
||||
: this(256)
|
||||
{
|
||||
}
|
||||
|
||||
public Sha3Digest(int bitLength)
|
||||
: base(CheckBitLength(bitLength))
|
||||
{
|
||||
}
|
||||
|
||||
public Sha3Digest(Sha3Digest source)
|
||||
: base(source)
|
||||
{
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
AbsorbBits(2, 2);
|
||||
return base.DoFinal(output, outOff);
|
||||
}
|
||||
|
||||
protected override int DoFinal(byte[] output, int outOff, byte partialByte, int partialBits)
|
||||
{
|
||||
if (partialBits < 0 || partialBits > 7)
|
||||
{
|
||||
throw new ArgumentException("must be in the range [0,7]", "partialBits");
|
||||
}
|
||||
int num = (partialByte & ((1 << partialBits) - 1)) | (2 << partialBits);
|
||||
int num2 = partialBits + 2;
|
||||
if (num2 >= 8)
|
||||
{
|
||||
Absorb(new byte[1] { (byte)num }, 0, 1);
|
||||
num2 -= 8;
|
||||
num >>= 8;
|
||||
}
|
||||
return base.DoFinal(output, outOff, (byte)num, num2);
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new Sha3Digest(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Sha512Digest : LongDigest
|
||||
{
|
||||
private const int DigestLength = 64;
|
||||
|
||||
public override string AlgorithmName => "SHA-512";
|
||||
|
||||
public Sha512Digest()
|
||||
{
|
||||
}
|
||||
|
||||
public Sha512Digest(Sha512Digest t)
|
||||
: base(t)
|
||||
{
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
Pack.UInt64_To_BE(H1, output, outOff);
|
||||
Pack.UInt64_To_BE(H2, output, outOff + 8);
|
||||
Pack.UInt64_To_BE(H3, output, outOff + 16);
|
||||
Pack.UInt64_To_BE(H4, output, outOff + 24);
|
||||
Pack.UInt64_To_BE(H5, output, outOff + 32);
|
||||
Pack.UInt64_To_BE(H6, output, outOff + 40);
|
||||
Pack.UInt64_To_BE(H7, output, outOff + 48);
|
||||
Pack.UInt64_To_BE(H8, output, outOff + 56);
|
||||
Reset();
|
||||
return 64;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H1 = 7640891576956012808uL;
|
||||
H2 = 13503953896175478587uL;
|
||||
H3 = 4354685564936845355uL;
|
||||
H4 = 11912009170470909681uL;
|
||||
H5 = 5840696475078001361uL;
|
||||
H6 = 11170449401992604703uL;
|
||||
H7 = 2270897969802886507uL;
|
||||
H8 = 6620516959819538809uL;
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new Sha512Digest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
Sha512Digest t = (Sha512Digest)other;
|
||||
CopyIn(t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class Sha512tDigest : LongDigest
|
||||
{
|
||||
private const ulong A5 = 11936128518282651045uL;
|
||||
|
||||
private readonly int digestLength;
|
||||
|
||||
private ulong H1t;
|
||||
|
||||
private ulong H2t;
|
||||
|
||||
private ulong H3t;
|
||||
|
||||
private ulong H4t;
|
||||
|
||||
private ulong H5t;
|
||||
|
||||
private ulong H6t;
|
||||
|
||||
private ulong H7t;
|
||||
|
||||
private ulong H8t;
|
||||
|
||||
public override string AlgorithmName => "SHA-512/" + digestLength * 8;
|
||||
|
||||
public Sha512tDigest(int bitLength)
|
||||
{
|
||||
if (bitLength >= 512)
|
||||
{
|
||||
throw new ArgumentException("cannot be >= 512", "bitLength");
|
||||
}
|
||||
if (bitLength % 8 != 0)
|
||||
{
|
||||
throw new ArgumentException("needs to be a multiple of 8", "bitLength");
|
||||
}
|
||||
if (bitLength == 384)
|
||||
{
|
||||
throw new ArgumentException("cannot be 384 use SHA384 instead", "bitLength");
|
||||
}
|
||||
digestLength = bitLength / 8;
|
||||
tIvGenerate(digestLength * 8);
|
||||
Reset();
|
||||
}
|
||||
|
||||
public Sha512tDigest(Sha512tDigest t)
|
||||
: base(t)
|
||||
{
|
||||
digestLength = t.digestLength;
|
||||
Reset(t);
|
||||
}
|
||||
|
||||
public override int GetDigestSize()
|
||||
{
|
||||
return digestLength;
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
UInt64_To_BE(H1, output, outOff, digestLength);
|
||||
UInt64_To_BE(H2, output, outOff + 8, digestLength - 8);
|
||||
UInt64_To_BE(H3, output, outOff + 16, digestLength - 16);
|
||||
UInt64_To_BE(H4, output, outOff + 24, digestLength - 24);
|
||||
UInt64_To_BE(H5, output, outOff + 32, digestLength - 32);
|
||||
UInt64_To_BE(H6, output, outOff + 40, digestLength - 40);
|
||||
UInt64_To_BE(H7, output, outOff + 48, digestLength - 48);
|
||||
UInt64_To_BE(H8, output, outOff + 56, digestLength - 56);
|
||||
Reset();
|
||||
return digestLength;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
H1 = H1t;
|
||||
H2 = H2t;
|
||||
H3 = H3t;
|
||||
H4 = H4t;
|
||||
H5 = H5t;
|
||||
H6 = H6t;
|
||||
H7 = H7t;
|
||||
H8 = H8t;
|
||||
}
|
||||
|
||||
private void tIvGenerate(int bitLength)
|
||||
{
|
||||
H1 = 14964410163792538797uL;
|
||||
H2 = 2216346199247487646uL;
|
||||
H3 = 11082046791023156622uL;
|
||||
H4 = 65953792586715988uL;
|
||||
H5 = 17630457682085488500uL;
|
||||
H6 = 4512832404995164602uL;
|
||||
H7 = 13413544941332994254uL;
|
||||
H8 = 18322165818757711068uL;
|
||||
Update(83);
|
||||
Update(72);
|
||||
Update(65);
|
||||
Update(45);
|
||||
Update(53);
|
||||
Update(49);
|
||||
Update(50);
|
||||
Update(47);
|
||||
if (bitLength > 100)
|
||||
{
|
||||
Update((byte)(bitLength / 100 + 48));
|
||||
bitLength %= 100;
|
||||
Update((byte)(bitLength / 10 + 48));
|
||||
bitLength %= 10;
|
||||
Update((byte)(bitLength + 48));
|
||||
}
|
||||
else if (bitLength > 10)
|
||||
{
|
||||
Update((byte)(bitLength / 10 + 48));
|
||||
bitLength %= 10;
|
||||
Update((byte)(bitLength + 48));
|
||||
}
|
||||
else
|
||||
{
|
||||
Update((byte)(bitLength + 48));
|
||||
}
|
||||
Finish();
|
||||
H1t = H1;
|
||||
H2t = H2;
|
||||
H3t = H3;
|
||||
H4t = H4;
|
||||
H5t = H5;
|
||||
H6t = H6;
|
||||
H7t = H7;
|
||||
H8t = H8;
|
||||
}
|
||||
|
||||
private static void UInt64_To_BE(ulong n, byte[] bs, int off, int max)
|
||||
{
|
||||
if (max > 0)
|
||||
{
|
||||
UInt32_To_BE((uint)(n >> 32), bs, off, max);
|
||||
if (max > 4)
|
||||
{
|
||||
UInt32_To_BE((uint)n, bs, off + 4, max - 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void UInt32_To_BE(uint n, byte[] bs, int off, int max)
|
||||
{
|
||||
int num = System.Math.Min(4, max);
|
||||
while (--num >= 0)
|
||||
{
|
||||
int num2 = 8 * (3 - num);
|
||||
bs[off + num] = (byte)(n >> num2);
|
||||
}
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new Sha512tDigest(this);
|
||||
}
|
||||
|
||||
public override void Reset(IMemoable other)
|
||||
{
|
||||
Sha512tDigest sha512tDigest = (Sha512tDigest)other;
|
||||
if (digestLength != sha512tDigest.digestLength)
|
||||
{
|
||||
throw new MemoableResetException("digestLength inappropriate in other");
|
||||
}
|
||||
CopyIn(sha512tDigest);
|
||||
H1t = sha512tDigest.H1t;
|
||||
H2t = sha512tDigest.H2t;
|
||||
H3t = sha512tDigest.H3t;
|
||||
H4t = sha512tDigest.H4t;
|
||||
H5t = sha512tDigest.H5t;
|
||||
H6t = sha512tDigest.H6t;
|
||||
H7t = sha512tDigest.H7t;
|
||||
H8t = sha512tDigest.H8t;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class ShakeDigest : KeccakDigest, IXof, IDigest
|
||||
{
|
||||
public override string AlgorithmName => "SHAKE" + fixedOutputLength;
|
||||
|
||||
private static int CheckBitLength(int bitLength)
|
||||
{
|
||||
if (bitLength == 128 || bitLength == 256)
|
||||
{
|
||||
return bitLength;
|
||||
}
|
||||
throw new ArgumentException(bitLength + " not supported for SHAKE", "bitLength");
|
||||
}
|
||||
|
||||
public ShakeDigest()
|
||||
: this(128)
|
||||
{
|
||||
}
|
||||
|
||||
public ShakeDigest(int bitLength)
|
||||
: base(CheckBitLength(bitLength))
|
||||
{
|
||||
}
|
||||
|
||||
public ShakeDigest(ShakeDigest source)
|
||||
: base(source)
|
||||
{
|
||||
}
|
||||
|
||||
public override int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
return DoFinal(output, outOff, GetDigestSize());
|
||||
}
|
||||
|
||||
public virtual int DoFinal(byte[] output, int outOff, int outLen)
|
||||
{
|
||||
DoOutput(output, outOff, outLen);
|
||||
Reset();
|
||||
return outLen;
|
||||
}
|
||||
|
||||
public virtual int DoOutput(byte[] output, int outOff, int outLen)
|
||||
{
|
||||
if (!squeezing)
|
||||
{
|
||||
AbsorbBits(15, 4);
|
||||
}
|
||||
Squeeze(output, outOff, (long)outLen << 3);
|
||||
return outLen;
|
||||
}
|
||||
|
||||
protected override int DoFinal(byte[] output, int outOff, byte partialByte, int partialBits)
|
||||
{
|
||||
return DoFinal(output, outOff, GetDigestSize(), partialByte, partialBits);
|
||||
}
|
||||
|
||||
protected virtual int DoFinal(byte[] output, int outOff, int outLen, byte partialByte, int partialBits)
|
||||
{
|
||||
if (partialBits < 0 || partialBits > 7)
|
||||
{
|
||||
throw new ArgumentException("must be in the range [0,7]", "partialBits");
|
||||
}
|
||||
int num = (partialByte & ((1 << partialBits) - 1)) | (15 << partialBits);
|
||||
int num2 = partialBits + 4;
|
||||
if (num2 >= 8)
|
||||
{
|
||||
Absorb(new byte[1] { (byte)num }, 0, 1);
|
||||
num2 -= 8;
|
||||
num >>= 8;
|
||||
}
|
||||
if (num2 > 0)
|
||||
{
|
||||
AbsorbBits(num, num2);
|
||||
}
|
||||
Squeeze(output, outOff, (long)outLen << 3);
|
||||
Reset();
|
||||
return outLen;
|
||||
}
|
||||
|
||||
public override IMemoable Copy()
|
||||
{
|
||||
return new ShakeDigest(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class ShortenedDigest : IDigest
|
||||
{
|
||||
private IDigest baseDigest;
|
||||
|
||||
private int length;
|
||||
|
||||
public string AlgorithmName => baseDigest.AlgorithmName + "(" + length * 8 + ")";
|
||||
|
||||
public ShortenedDigest(IDigest baseDigest, int length)
|
||||
{
|
||||
if (baseDigest == null)
|
||||
{
|
||||
throw new ArgumentNullException("baseDigest");
|
||||
}
|
||||
if (length > baseDigest.GetDigestSize())
|
||||
{
|
||||
throw new ArgumentException("baseDigest output not large enough to support length");
|
||||
}
|
||||
this.baseDigest = baseDigest;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public int GetDigestSize()
|
||||
{
|
||||
return length;
|
||||
}
|
||||
|
||||
public void Update(byte input)
|
||||
{
|
||||
baseDigest.Update(input);
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] input, int inOff, int length)
|
||||
{
|
||||
baseDigest.BlockUpdate(input, inOff, length);
|
||||
}
|
||||
|
||||
public int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
byte[] array = new byte[baseDigest.GetDigestSize()];
|
||||
baseDigest.DoFinal(array, 0);
|
||||
Array.Copy(array, 0, output, outOff, length);
|
||||
return length;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
baseDigest.Reset();
|
||||
}
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return baseDigest.GetByteLength();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class SkeinDigest : IDigest, IMemoable
|
||||
{
|
||||
public const int SKEIN_256 = 256;
|
||||
|
||||
public const int SKEIN_512 = 512;
|
||||
|
||||
public const int SKEIN_1024 = 1024;
|
||||
|
||||
private readonly SkeinEngine engine;
|
||||
|
||||
public string AlgorithmName => "Skein-" + engine.BlockSize * 8 + "-" + engine.OutputSize * 8;
|
||||
|
||||
public SkeinDigest(int stateSizeBits, int digestSizeBits)
|
||||
{
|
||||
engine = new SkeinEngine(stateSizeBits, digestSizeBits);
|
||||
Init(null);
|
||||
}
|
||||
|
||||
public SkeinDigest(SkeinDigest digest)
|
||||
{
|
||||
engine = new SkeinEngine(digest.engine);
|
||||
}
|
||||
|
||||
public void Reset(IMemoable other)
|
||||
{
|
||||
SkeinDigest skeinDigest = (SkeinDigest)other;
|
||||
engine.Reset(skeinDigest.engine);
|
||||
}
|
||||
|
||||
public IMemoable Copy()
|
||||
{
|
||||
return new SkeinDigest(this);
|
||||
}
|
||||
|
||||
public int GetDigestSize()
|
||||
{
|
||||
return engine.OutputSize;
|
||||
}
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return engine.BlockSize;
|
||||
}
|
||||
|
||||
public void Init(SkeinParameters parameters)
|
||||
{
|
||||
engine.Init(parameters);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
engine.Reset();
|
||||
}
|
||||
|
||||
public void Update(byte inByte)
|
||||
{
|
||||
engine.Update(inByte);
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] inBytes, int inOff, int len)
|
||||
{
|
||||
engine.Update(inBytes, inOff, len);
|
||||
}
|
||||
|
||||
public int DoFinal(byte[] outBytes, int outOff)
|
||||
{
|
||||
return engine.DoFinal(outBytes, outOff);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,539 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class SkeinEngine : IMemoable
|
||||
{
|
||||
private class Configuration
|
||||
{
|
||||
private byte[] bytes = new byte[32];
|
||||
|
||||
public byte[] Bytes => bytes;
|
||||
|
||||
public Configuration(long outputSizeBits)
|
||||
{
|
||||
bytes[0] = 83;
|
||||
bytes[1] = 72;
|
||||
bytes[2] = 65;
|
||||
bytes[3] = 51;
|
||||
bytes[4] = 1;
|
||||
bytes[5] = 0;
|
||||
ThreefishEngine.WordToBytes((ulong)outputSizeBits, bytes, 8);
|
||||
}
|
||||
}
|
||||
|
||||
public class Parameter
|
||||
{
|
||||
private int type;
|
||||
|
||||
private byte[] value;
|
||||
|
||||
public int Type => type;
|
||||
|
||||
public byte[] Value => value;
|
||||
|
||||
public Parameter(int type, byte[] value)
|
||||
{
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
private class UbiTweak
|
||||
{
|
||||
private const ulong LOW_RANGE = 18446744069414584320uL;
|
||||
|
||||
private const ulong T1_FINAL = 9223372036854775808uL;
|
||||
|
||||
private const ulong T1_FIRST = 4611686018427387904uL;
|
||||
|
||||
private ulong[] tweak = new ulong[2];
|
||||
|
||||
private bool extendedPosition;
|
||||
|
||||
public uint Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return (uint)((tweak[1] >> 56) & 0x3F);
|
||||
}
|
||||
set
|
||||
{
|
||||
tweak[1] = (tweak[1] & 0xFFFFFFC000000000uL) | (((ulong)value & 0x3FuL) << 56);
|
||||
}
|
||||
}
|
||||
|
||||
public bool First
|
||||
{
|
||||
get
|
||||
{
|
||||
return (tweak[1] & 0x4000000000000000L) != 0;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
ulong[] array;
|
||||
(array = tweak)[1] = array[1] | 0x4000000000000000L;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong[] array;
|
||||
(array = tweak)[1] = array[1] & 0xBFFFFFFFFFFFFFFFuL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool Final
|
||||
{
|
||||
get
|
||||
{
|
||||
return (tweak[1] & 0x8000000000000000uL) != 0;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
ulong[] array;
|
||||
(array = tweak)[1] = array[1] | 0x8000000000000000uL;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong[] array;
|
||||
(array = tweak)[1] = array[1] & 0x7FFFFFFFFFFFFFFFL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public UbiTweak()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public void Reset(UbiTweak tweak)
|
||||
{
|
||||
this.tweak = Arrays.Clone(tweak.tweak, this.tweak);
|
||||
extendedPosition = tweak.extendedPosition;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
tweak[0] = 0uL;
|
||||
tweak[1] = 0uL;
|
||||
extendedPosition = false;
|
||||
First = true;
|
||||
}
|
||||
|
||||
public void AdvancePosition(int advance)
|
||||
{
|
||||
if (extendedPosition)
|
||||
{
|
||||
ulong[] array = new ulong[3]
|
||||
{
|
||||
tweak[0] & 0xFFFFFFFFu,
|
||||
(tweak[0] >> 32) & 0xFFFFFFFFu,
|
||||
tweak[1] & 0xFFFFFFFFu
|
||||
};
|
||||
ulong num = (ulong)advance;
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
num = (array[i] = num + array[i]) >> 32;
|
||||
}
|
||||
tweak[0] = ((array[1] & 0xFFFFFFFFu) << 32) | (array[0] & 0xFFFFFFFFu);
|
||||
tweak[1] = (tweak[1] & 0xFFFFFFFF00000000uL) | (array[2] & 0xFFFFFFFFu);
|
||||
}
|
||||
else
|
||||
{
|
||||
ulong num2 = tweak[0];
|
||||
num2 += (uint)advance;
|
||||
tweak[0] = num2;
|
||||
if (num2 > 18446744069414584320uL)
|
||||
{
|
||||
extendedPosition = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ulong[] GetWords()
|
||||
{
|
||||
return tweak;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Type + " first: " + First + ", final: " + Final;
|
||||
}
|
||||
}
|
||||
|
||||
private class UBI
|
||||
{
|
||||
private readonly UbiTweak tweak = new UbiTweak();
|
||||
|
||||
private readonly SkeinEngine engine;
|
||||
|
||||
private byte[] currentBlock;
|
||||
|
||||
private int currentOffset;
|
||||
|
||||
private ulong[] message;
|
||||
|
||||
public UBI(SkeinEngine engine, int blockSize)
|
||||
{
|
||||
this.engine = engine;
|
||||
currentBlock = new byte[blockSize];
|
||||
message = new ulong[currentBlock.Length / 8];
|
||||
}
|
||||
|
||||
public void Reset(UBI ubi)
|
||||
{
|
||||
currentBlock = Arrays.Clone(ubi.currentBlock, currentBlock);
|
||||
currentOffset = ubi.currentOffset;
|
||||
message = Arrays.Clone(ubi.message, message);
|
||||
tweak.Reset(ubi.tweak);
|
||||
}
|
||||
|
||||
public void Reset(int type)
|
||||
{
|
||||
tweak.Reset();
|
||||
tweak.Type = (uint)type;
|
||||
currentOffset = 0;
|
||||
}
|
||||
|
||||
public void Update(byte[] value, int offset, int len, ulong[] output)
|
||||
{
|
||||
int num = 0;
|
||||
while (len > num)
|
||||
{
|
||||
if (currentOffset == currentBlock.Length)
|
||||
{
|
||||
ProcessBlock(output);
|
||||
tweak.First = false;
|
||||
currentOffset = 0;
|
||||
}
|
||||
int num2 = System.Math.Min(len - num, currentBlock.Length - currentOffset);
|
||||
Array.Copy(value, offset + num, currentBlock, currentOffset, num2);
|
||||
num += num2;
|
||||
currentOffset += num2;
|
||||
tweak.AdvancePosition(num2);
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessBlock(ulong[] output)
|
||||
{
|
||||
engine.threefish.Init(forEncryption: true, engine.chain, tweak.GetWords());
|
||||
for (int i = 0; i < message.Length; i++)
|
||||
{
|
||||
message[i] = ThreefishEngine.BytesToWord(currentBlock, i * 8);
|
||||
}
|
||||
engine.threefish.ProcessBlock(message, output);
|
||||
for (int j = 0; j < output.Length; j++)
|
||||
{
|
||||
ulong[] array2;
|
||||
ulong[] array = (array2 = output);
|
||||
int num = j;
|
||||
nint num2 = num;
|
||||
array[num] = array2[num2] ^ message[j];
|
||||
}
|
||||
}
|
||||
|
||||
public void DoFinal(ulong[] output)
|
||||
{
|
||||
for (int i = currentOffset; i < currentBlock.Length; i++)
|
||||
{
|
||||
currentBlock[i] = 0;
|
||||
}
|
||||
tweak.Final = true;
|
||||
ProcessBlock(output);
|
||||
}
|
||||
}
|
||||
|
||||
public const int SKEIN_256 = 256;
|
||||
|
||||
public const int SKEIN_512 = 512;
|
||||
|
||||
public const int SKEIN_1024 = 1024;
|
||||
|
||||
private const int PARAM_TYPE_KEY = 0;
|
||||
|
||||
private const int PARAM_TYPE_CONFIG = 4;
|
||||
|
||||
private const int PARAM_TYPE_MESSAGE = 48;
|
||||
|
||||
private const int PARAM_TYPE_OUTPUT = 63;
|
||||
|
||||
private static readonly IDictionary INITIAL_STATES;
|
||||
|
||||
private readonly ThreefishEngine threefish;
|
||||
|
||||
private readonly int outputSizeBytes;
|
||||
|
||||
private ulong[] chain;
|
||||
|
||||
private ulong[] initialState;
|
||||
|
||||
private byte[] key;
|
||||
|
||||
private Parameter[] preMessageParameters;
|
||||
|
||||
private Parameter[] postMessageParameters;
|
||||
|
||||
private readonly UBI ubi;
|
||||
|
||||
private readonly byte[] singleByte = new byte[1];
|
||||
|
||||
public int OutputSize => outputSizeBytes;
|
||||
|
||||
public int BlockSize => threefish.GetBlockSize();
|
||||
|
||||
static SkeinEngine()
|
||||
{
|
||||
INITIAL_STATES = Platform.CreateHashtable();
|
||||
InitialState(256, 128, new ulong[4] { 16217771249220022880uL, 9817190399063458076uL, 1155188648486244218uL, 14769517481627992514uL });
|
||||
InitialState(256, 160, new ulong[4] { 1450197650740764312uL, 3081844928540042640uL, 15310647011875280446uL, 3301952811952417661uL });
|
||||
InitialState(256, 224, new ulong[4] { 14270089230798940683uL, 9758551101254474012uL, 11082101768697755780uL, 4056579644589979102uL });
|
||||
InitialState(256, 256, new ulong[4] { 18202890402666165321uL, 3443677322885453875uL, 12915131351309911055uL, 7662005193972177513uL });
|
||||
InitialState(512, 128, new ulong[8] { 12158729379475595090uL, 2204638249859346602uL, 3502419045458743507uL, 13617680570268287068uL, 983504137758028059uL, 1880512238245786339uL, 11730851291495443074uL, 7602827311880509485uL });
|
||||
InitialState(512, 160, new ulong[8] { 2934123928682216849uL, 14047033351726823311uL, 1684584802963255058uL, 5744138295201861711uL, 2444857010922934358uL, 15638910433986703544uL, 13325156239043941114uL, 118355523173251694uL });
|
||||
InitialState(512, 224, new ulong[8] { 14758403053642543652uL, 14674518637417806319uL, 10145881904771976036uL, 4146387520469897396uL, 1106145742801415120uL, 7455425944880474941uL, 11095680972475339753uL, 11397762726744039159uL });
|
||||
InitialState(512, 384, new ulong[8] { 11814849197074935647uL, 12753905853581818532uL, 11346781217370868990uL, 15535391162178797018uL, 2000907093792408677uL, 9140007292425499655uL, 6093301768906360022uL, 2769176472213098488uL });
|
||||
InitialState(512, 512, new ulong[8] { 5261240102383538638uL, 978932832955457283uL, 10363226125605772238uL, 11107378794354519217uL, 6752626034097301424uL, 16915020251879818228uL, 11029617608758768931uL, 12544957130904423475uL });
|
||||
}
|
||||
|
||||
private static void InitialState(int blockSize, int outputSize, ulong[] state)
|
||||
{
|
||||
INITIAL_STATES.Add(VariantIdentifier(blockSize / 8, outputSize / 8), state);
|
||||
}
|
||||
|
||||
private static int VariantIdentifier(int blockSizeBytes, int outputSizeBytes)
|
||||
{
|
||||
return (outputSizeBytes << 16) | blockSizeBytes;
|
||||
}
|
||||
|
||||
public SkeinEngine(int blockSizeBits, int outputSizeBits)
|
||||
{
|
||||
if (outputSizeBits % 8 != 0)
|
||||
{
|
||||
throw new ArgumentException("Output size must be a multiple of 8 bits. :" + outputSizeBits);
|
||||
}
|
||||
outputSizeBytes = outputSizeBits / 8;
|
||||
threefish = new ThreefishEngine(blockSizeBits);
|
||||
ubi = new UBI(this, threefish.GetBlockSize());
|
||||
}
|
||||
|
||||
public SkeinEngine(SkeinEngine engine)
|
||||
: this(engine.BlockSize * 8, engine.OutputSize * 8)
|
||||
{
|
||||
CopyIn(engine);
|
||||
}
|
||||
|
||||
private void CopyIn(SkeinEngine engine)
|
||||
{
|
||||
ubi.Reset(engine.ubi);
|
||||
chain = Arrays.Clone(engine.chain, chain);
|
||||
initialState = Arrays.Clone(engine.initialState, initialState);
|
||||
key = Arrays.Clone(engine.key, key);
|
||||
preMessageParameters = Clone(engine.preMessageParameters, preMessageParameters);
|
||||
postMessageParameters = Clone(engine.postMessageParameters, postMessageParameters);
|
||||
}
|
||||
|
||||
private static Parameter[] Clone(Parameter[] data, Parameter[] existing)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (existing == null || existing.Length != data.Length)
|
||||
{
|
||||
existing = new Parameter[data.Length];
|
||||
}
|
||||
Array.Copy(data, 0, existing, 0, existing.Length);
|
||||
return existing;
|
||||
}
|
||||
|
||||
public IMemoable Copy()
|
||||
{
|
||||
return new SkeinEngine(this);
|
||||
}
|
||||
|
||||
public void Reset(IMemoable other)
|
||||
{
|
||||
SkeinEngine skeinEngine = (SkeinEngine)other;
|
||||
if (BlockSize != skeinEngine.BlockSize || outputSizeBytes != skeinEngine.outputSizeBytes)
|
||||
{
|
||||
throw new MemoableResetException("Incompatible parameters in provided SkeinEngine.");
|
||||
}
|
||||
CopyIn(skeinEngine);
|
||||
}
|
||||
|
||||
public void Init(SkeinParameters parameters)
|
||||
{
|
||||
chain = null;
|
||||
key = null;
|
||||
preMessageParameters = null;
|
||||
postMessageParameters = null;
|
||||
if (parameters != null)
|
||||
{
|
||||
byte[] array = parameters.GetKey();
|
||||
if (array.Length < 16)
|
||||
{
|
||||
throw new ArgumentException("Skein key must be at least 128 bits.");
|
||||
}
|
||||
InitParams(parameters.GetParameters());
|
||||
}
|
||||
CreateInitialState();
|
||||
UbiInit(48);
|
||||
}
|
||||
|
||||
private void InitParams(IDictionary parameters)
|
||||
{
|
||||
IEnumerator enumerator = parameters.Keys.GetEnumerator();
|
||||
IList list = Platform.CreateArrayList();
|
||||
IList list2 = Platform.CreateArrayList();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
int num = (int)enumerator.Current;
|
||||
byte[] value = (byte[])parameters[num];
|
||||
if (num == 0)
|
||||
{
|
||||
key = value;
|
||||
}
|
||||
else if (num < 48)
|
||||
{
|
||||
list.Add(new Parameter(num, value));
|
||||
}
|
||||
else
|
||||
{
|
||||
list2.Add(new Parameter(num, value));
|
||||
}
|
||||
}
|
||||
preMessageParameters = new Parameter[list.Count];
|
||||
list.CopyTo(preMessageParameters, 0);
|
||||
Array.Sort((Array)preMessageParameters);
|
||||
postMessageParameters = new Parameter[list2.Count];
|
||||
list2.CopyTo(postMessageParameters, 0);
|
||||
Array.Sort((Array)postMessageParameters);
|
||||
}
|
||||
|
||||
private void CreateInitialState()
|
||||
{
|
||||
ulong[] array = (ulong[])INITIAL_STATES[VariantIdentifier(BlockSize, OutputSize)];
|
||||
if (key == null && array != null)
|
||||
{
|
||||
chain = Arrays.Clone(array);
|
||||
}
|
||||
else
|
||||
{
|
||||
chain = new ulong[BlockSize / 8];
|
||||
if (key != null)
|
||||
{
|
||||
UbiComplete(0, key);
|
||||
}
|
||||
UbiComplete(4, new Configuration(outputSizeBytes * 8).Bytes);
|
||||
}
|
||||
if (preMessageParameters != null)
|
||||
{
|
||||
for (int i = 0; i < preMessageParameters.Length; i++)
|
||||
{
|
||||
Parameter parameter = preMessageParameters[i];
|
||||
UbiComplete(parameter.Type, parameter.Value);
|
||||
}
|
||||
}
|
||||
initialState = Arrays.Clone(chain);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Array.Copy(initialState, 0, chain, 0, chain.Length);
|
||||
UbiInit(48);
|
||||
}
|
||||
|
||||
private void UbiComplete(int type, byte[] value)
|
||||
{
|
||||
UbiInit(type);
|
||||
ubi.Update(value, 0, value.Length, chain);
|
||||
UbiFinal();
|
||||
}
|
||||
|
||||
private void UbiInit(int type)
|
||||
{
|
||||
ubi.Reset(type);
|
||||
}
|
||||
|
||||
private void UbiFinal()
|
||||
{
|
||||
ubi.DoFinal(chain);
|
||||
}
|
||||
|
||||
private void CheckInitialised()
|
||||
{
|
||||
if (ubi == null)
|
||||
{
|
||||
throw new ArgumentException("Skein engine is not initialised.");
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(byte inByte)
|
||||
{
|
||||
singleByte[0] = inByte;
|
||||
Update(singleByte, 0, 1);
|
||||
}
|
||||
|
||||
public void Update(byte[] inBytes, int inOff, int len)
|
||||
{
|
||||
CheckInitialised();
|
||||
ubi.Update(inBytes, inOff, len, chain);
|
||||
}
|
||||
|
||||
public int DoFinal(byte[] outBytes, int outOff)
|
||||
{
|
||||
CheckInitialised();
|
||||
if (outBytes.Length < outOff + outputSizeBytes)
|
||||
{
|
||||
throw new DataLengthException("Output buffer is too short to hold output");
|
||||
}
|
||||
UbiFinal();
|
||||
if (postMessageParameters != null)
|
||||
{
|
||||
for (int i = 0; i < postMessageParameters.Length; i++)
|
||||
{
|
||||
Parameter parameter = postMessageParameters[i];
|
||||
UbiComplete(parameter.Type, parameter.Value);
|
||||
}
|
||||
}
|
||||
int blockSize = BlockSize;
|
||||
int num = (outputSizeBytes + blockSize - 1) / blockSize;
|
||||
for (int j = 0; j < num; j++)
|
||||
{
|
||||
int outputBytes = System.Math.Min(blockSize, outputSizeBytes - j * blockSize);
|
||||
Output((ulong)j, outBytes, outOff + j * blockSize, outputBytes);
|
||||
}
|
||||
Reset();
|
||||
return outputSizeBytes;
|
||||
}
|
||||
|
||||
private void Output(ulong outputSequence, byte[] outBytes, int outOff, int outputBytes)
|
||||
{
|
||||
byte[] array = new byte[8];
|
||||
ThreefishEngine.WordToBytes(outputSequence, array, 0);
|
||||
ulong[] array2 = new ulong[chain.Length];
|
||||
UbiInit(63);
|
||||
ubi.Update(array, 0, array.Length, array2);
|
||||
ubi.DoFinal(array2);
|
||||
int num = (outputBytes + 8 - 1) / 8;
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
int num2 = System.Math.Min(8, outputBytes - i * 8);
|
||||
if (num2 == 8)
|
||||
{
|
||||
ThreefishEngine.WordToBytes(array2[i], outBytes, outOff + i * 8);
|
||||
continue;
|
||||
}
|
||||
ThreefishEngine.WordToBytes(array2[i], array, 0);
|
||||
Array.Copy(array, 0, outBytes, outOff + i * 8, num2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public class TigerDigest : IDigest, IMemoable
|
||||
{
|
||||
private const int MyByteLength = 64;
|
||||
|
||||
private const int DigestLength = 24;
|
||||
|
||||
private static readonly long[] t1 = new long[256]
|
||||
{
|
||||
192161084409973854L, -6034178070669973268L, 8272369121297300691L, 7854730284916899642L, -3631738584360316525L, 8463286011307239906L, -5664346993730092093L, 5082381371487377520L, -1536603760329757466L, -4232985935611735204L,
|
||||
5541490850629862524L, 766444128913191948L, 1204553577021685498L, -4121719295987045526L, 1401289229890216703L, 1893918052108309022L, 5461170853188208586L, 2807403890869420487L, -8822417684582283338L, 5699452412975025298L,
|
||||
-2914262034798377397L, -8199292901130911363L, 7624427211800470465L, -5330070367527189138L, 9043806901924967914L, 7231827479902542914L, -4667804575905660192L, 6875646691050945796L, -954047427515838778L, 7786398710221814956L,
|
||||
8167597339425066981L, 1830707105885056415L, -192929137551915557L, -4000909680243679221L, -8790383730744944306L, -6559119868654993229L, -8046943608939121133L, -2635222011098072079L, 1783120314242633559L, 248005612187258982L,
|
||||
7688500634458409525L, -799055769434250085L, 8591138587399736033L, -2813706756098348539L, -4803442773389201549L, 5042603696143252264L, 2053990370701680515L, -8434990628116389527L, 3741955435321465241L, 4334407786093429776L,
|
||||
-5399798173115342087L, 1449859124008718907L, -259597992345095852L, -2299784421946890745L, -8624947886301142065L, -7850603641235491331L, 3847074041673952000L, 4649400157396704725L, -4273499526689310132L, -3840742565288711634L,
|
||||
2909491499011162061L, 4458122598401901638L, 7071481730398905774L, 6725294491764459774L, -6201551736110472662L, -4372530048007926361L, 1226483701329067140L, -2522035007050864557L, -3676115808446124170L, -4975751036383735295L,
|
||||
-1831728144282101387L, -7732658914112356844L, 479582384021555544L, 8040612334407127321L, -2798227069691230528L, -1334228551670664750L, 8751740296797632830L, 6603430683508552489L, 8942924799792477540L, 3573742753214737511L,
|
||||
-2419519573825602302L, 6349030933445924429L, -2501945979441900175L, -6177453506703404958L, -7885857697280165792L, 5194369709296555225L, 7174555471952375656L, 7982812746821821468L, -8707669106532426453L, 3232013613859041307L,
|
||||
-5747376245209101971L, -2231459388012946321L, 3112410413624570453L, -2336602742119691332L, 6658792778814911418L, 6126246269502162262L, -6070952467612144753L, 4721051187472420532L, -5533619424469951182L, -4853025588084287359L,
|
||||
2663576151211431276L, 928112258657309258L, 5664920977038299994L, 2704699625848084345L, 2312925355491498803L, -528812816973409076L, 2964761606854114992L, 4148718494125202372L, 4082542483235864459L, 5171535286737311423L,
|
||||
2166137813939512309L, 8844224567096109974L, -6373247044080797239L, -8133614489572350707L, 7053919794999990929L, 5576291611870337032L, -1374825740467639573L, -734453569254161202L, -705972172313107935L, -6688726126811769884L,
|
||||
-7468621655906046812L, -3527580439205474383L, -6956282119872554589L, -6281089153129775081L, 853355433004222246L, -1924221946255713479L, 2124075034376372323L, 5881355904936746717L, 1033318428544969251L, 1692585388818821524L,
|
||||
-1245985052454466526L, 1107424405919510210L, -9211670503851965599L, -5975256720516651978L, 963191604767572015L, 4506934758573727688L, -6511972687387035778L, -6714534832456272315L, 7421261837586505858L, 3318186242040429129L,
|
||||
-4402061108394378299L, 1910808081503L, 4771413979138012118L, -3357965141731676491L, -6811660122601107496L, 3247421105326436348L, -1009844908434318049L, 8353265116968520410L, -5881406294935394735L, -7574869783018555510L,
|
||||
6528592316425799439L, -3049672598698961616L, -3303981966096002009L, 7320455443630736945L, -7351974990356818097L, 2539802313181221187L, -7307523792611951465L, 6084456898448652712L, 1615327116689102472L, 8126548348642832045L,
|
||||
-1094214848903295726L, 6320848846662414801L, -1163799684465161365L, 3439926484095136410L, -7218302546559918104L, 4583261464596863494L, 5278432013075676693L, 672210957064462075L, -5420889727701263133L, -3948047341652367807L,
|
||||
3753742208681096767L, -5185515461782971584L, -460252340867529358L, 111470777923844445L, 1951374535466601971L, -8875343681432095955L, -4493729248843343338L, 4830799035278983864L, -5224728565293047538L, 6842302225500364445L,
|
||||
-7111193868311747516L, -2729919277420993032L, -5582278241003401657L, -126421769187551098L, -4035721366655415313L, -1986169280154305277L, 3977519900599801820L, 9148781857317432677L, 6468933130968205401L, 8516219711084257782L,
|
||||
1539015908620793624L, 7527026033758878374L, -1647949680688450337L, 3088835283432281588L, 3651919061693825289L, -8985256062000155568L, -423165018983337331L, -7032056788937726985L, 308165109378616703L, 8884692927086426203L,
|
||||
2438838841395254149L, -3550173447755953499L, 2823241734971430590L, 3896218688877146334L, 393786506094771122L, -3117973570538943511L, -7973569017697024389L, -8368763565314219996L, 6934559736714979565L, -589348163057397487L,
|
||||
-7554853961030558080L, -6878676038788161577L, -3798065817641571893L, -9101961441510934879L, -4559443103670756675L, -7665374195348870830L, -8336074436196531783L, 4236391428300945648L, 555138268555536248L, 5351590591369890935L,
|
||||
4306521946498657944L, -7151482210676895604L, 4901816398460471456L, -9033789479800328823L, 7485939926152528684L, -5105994143555176462L, 6245128712556390173L, -4718679834244078161L, -325273111308121687L, 7772052866533484500L,
|
||||
639373189613950878L, 2515940555210603828L, -2058685867725021174L, 9187445612742136046L, -5771987833248487369L, -2125811817212952004L, -3204735567712096048L, -3393897870002714342L, 1313621308117380133L, 3526835097255131285L,
|
||||
-4953033604042954265L, 8704164972314360376L, -920137909863202916L, 5969067443919232116L, 5791404459833380522L, -1682712826007985785L, 6001456072058810555L, -8273861206301250160L, 2241175407069758350L, -2962551490920225208L,
|
||||
8359644330926224055L, -8523485772611717717L, -5183265553382750L, -1789270636298447811L, -6471656072873752544L, -1458735953920612486L
|
||||
};
|
||||
|
||||
private static readonly long[] t2 = new long[256]
|
||||
{
|
||||
-1826563305001377480L, -5358963986493047656L, 6213947727727520144L, 5496303794639560149L, -2795981259149962188L, 642450021946863605L, -2925749420550550287L, -4252676236223476327L, -2372897249057438062L, -2455723000952046826L,
|
||||
8011611286970690052L, 5372247966639775667L, -6490268738015937967L, -265982677241022690L, -1711898199407229911L, -2553549223344005918L, -3655427155680827379L, 1788379855404599063L, 3792259505844355329L, 857793142685420274L,
|
||||
2176386753693503798L, -2281187609587102471L, -12877901320348396L, 6070247714570225101L, 7358743242340641331L, -8703516059324417162L, 1522910625901990663L, -2134847759353728262L, 5235630359010597374L, -5774648161970196758L,
|
||||
277273466943670671L, 3580831169916433691L, -1032406685548103719L, 4657750985732713388L, 1177149711660596421L, 8685721698255572101L, -3227632359902186326L, -6349410231276355429L, -4809500581665772080L, -7923309769729008016L,
|
||||
-6726740716384263588L, -4587792071496920925L, -658271017113840853L, 3834592178494549117L, -3853851402329989932L, -8865288174312808228L, 8774750272303345432L, -8428026360225307604L, -3404183201405868250L, 6519077675840655372L,
|
||||
1009372798613472243L, -4504928615151511518L, 7670504156571609794L, -9068448121725124008L, 7481699948221361317L, 2131352009749933493L, 7854556580946198495L, 5848046147829288198L, 6811751916476253359L, -635956774299390418L,
|
||||
-4737535235939835750L, -1614809042241653147L, 8245611441321613668L, 8087057586628171618L, 5058061449640751271L, -5151918184365513026L, 7212395796113148780L, 8872633840395976086L, 8602726521519041395L, -5885490816789515276L,
|
||||
6042660761688602872L, 1642367900117110883L, 25924001596622557L, 7531865058110106323L, 4223621278438660202L, 3926684511422013614L, -2064363959953346089L, 5939130201053773422L, 8312208923375399755L, 5278156969609628584L,
|
||||
-5712322089306707131L, 3610014133393185213L, -8850224129823554669L, -7989215126425784091L, 7953444341930717599L, -5072589324995998940L, -3677986556148923193L, 5127306049615917691L, 9121210965518562125L, 8462056263389103903L,
|
||||
-743704981880018871L, 5658738406708581754L, 3084862250275496789L, -2839477530259368618L, -3966384508771725354L, -3487534071112132806L, -123994483119243460L, -1345606558677941971L, -8999779576894164844L, -4191785782441631580L,
|
||||
1116769798908306816L, 1871732813531574911L, -5639228995346094013L, 2050857069623328786L, 942713319182180155L, -8555767913901511542L, -1938713800388260250L, 7028952989422544417L, 9018945159409650955L, -9098571702620193189L,
|
||||
512456053301416255L, -4053543709501018729L, -4330900206871259305L, -1512795427272957464L, -3102984968199159270L, -7389706432295929941L, -6638196300801425917L, -7112719166685012944L, 4569666897377300404L, -7151449437793514816L,
|
||||
4462677101358564049L, 3679240545963649394L, -4129112553160565951L, 776201060342576796L, -1202834617519492059L, -842133208882402856L, -8445297248460022090L, 3458390008116962295L, -8107400727032609416L, 6618311662604863029L,
|
||||
4790267690900900096L, 1716087693007726108L, 4148457837926911568L, -5418957485852076861L, 8968309666649857421L, -2611360075161572255L, 6968029403465067289L, -3584187592496365262L, 500987773930853904L, -8168172799095912208L,
|
||||
2355660670689429871L, 3178293543037890097L, -5583593033549110520L, -6297125087914569009L, 894835714693979080L, -5305826774090122525L, -348051181029808153L, 352461093517089771L, 5441805419015688358L, -3049381223523647492L,
|
||||
3501129463520285556L, -4980126173351398283L, -8303518980934164731L, -7446347735086057113L, 2615208954064994172L, -522603252265687058L, 2237558221535645089L, -3911919600557704777L, -5210711461681408094L, 7102368496127332321L,
|
||||
-7719366717024918019L, 399232473491847935L, 7140013836546489399L, -8234741283244511424L, -2231392863125672626L, -7060197492102713059L, 5038446221635409553L, 6294769326316815049L, -387802090031244907L, -3350046130045840024L,
|
||||
-2666808022981539793L, -6161723600240465717L, 2783168786742146440L, 1986639352536355296L, -1988727118208302602L, 8799325730492140254L, 7305467695957075406L, 2551364576700533681L, -6081001307066006598L, -4889804522683628146L,
|
||||
-7324859595388608820L, -6885748294050442179L, 5760535140236403614L, 1501217875009212803L, -1291632093432900094L, -7706153952057205239L, 6454505253869455699L, 4319683495060363885L, -6244922308576078969L, -6818767823778904188L,
|
||||
2960027307368769952L, 8570410701452901115L, 160427886842421800L, -4969938860820756853L, -4627442630994782527L, -3285648034072744413L, -7606118162332863056L, 6176075057452006273L, 7582622308322968760L, 6649763778434249567L,
|
||||
-183456705028906550L, 2699628156079216836L, -1767231947251866451L, 2945653313023238585L, 2813841150172635667L, 8163160757531991904L, -7212422464109809801L, -5924618728816493121L, 649720531103423106L, 6394120152722619742L,
|
||||
-934965811117111118L, 4753049982369101610L, 2408845162401379802L, 1253140645631747605L, -7799048643966905049L, -1584266091164108743L, -456002869645138839L, 8367255505928917714L, 91400768704631494L, -4464375255980341934L,
|
||||
1938401838693046941L, -7520293791609324052L, -8636597607271566304L, 3990523136699180870L, 7731749711829208666L, 4875740361372990282L, 9173201802070489451L, 7834799413446679311L, -6433392137177717442L, 3325271250982575439L,
|
||||
-8730608807451740020L, -2389358865336045484L, -9209652622095187875L, 4359958813756723849L, 4539467735137059035L, -5508531677782308793L, 1312945880979454078L, -947428475416758718L, 4958176066159770025L, 1374196081931091686L,
|
||||
-6918434684938959032L, -1095184559281703237L, -1411469442470588444L, 3145683508650593868L, -6039522865352658195L, -3804467173852034031L, -6563710254104815428L, 6868326517302426863L, 6758043032196830276L, 5827167051130463242L,
|
||||
4074828688890126937L, 3293442170241026694L, -8065760984084440343L, 5618223731912049521L, -3014545685365689991L, 2520538699101199374L
|
||||
};
|
||||
|
||||
private static readonly long[] t3 = new long[256]
|
||||
{
|
||||
-819712100864953445L, 5224129141031473793L, -1683494792012715969L, 3214246200928423523L, -2720183745931134014L, 3432136347919366758L, -6844377996819786796L, -4697838837464539535L, -3480123136110369641L, -5257202687841710057L,
|
||||
-3160671586143389472L, -8143604544638974599L, -7582212342885995579L, 7399204607179264370L, 2410740665327626235L, -5531319028708868287L, -1132011872800708955L, -8244108713684067595L, -8100030830173699490L, -865042824158552761L,
|
||||
-1406263208487841571L, -743744098937138031L, -7255025749313877870L, 5293216666010209768L, -6686350151342941087L, 505172698323928814L, -8504163865352868456L, -6039198373597746942L, 2102395425312436973L, -1480681786698906867L,
|
||||
6364975572501938982L, -7035658141633266754L, -8022507636838873565L, -4480433668109774745L, 2328871106231838244L, 1378680973804076623L, -3586772320324138908L, -2755027987269747529L, 7519553577929664460L, 460638964809724379L,
|
||||
-99820877092259348L, 6562793443469826132L, 1580997072160885165L, 859005579845670993L, -3058956174016989192L, -3379814835910611228L, -3936971176641920257L, -8723858077265400670L, 3784640730692549981L, -2514946515147142870L,
|
||||
-718211188705137671L, 5877026246039211124L, -8623573777109189598L, -6383628662057423219L, 4036482174343220762L, -6451625591996463702L, -5974472282720051687L, -4119613249555124729L, -4204805774663870152L, 1637614953354483776L,
|
||||
1768420517056302872L, -6063481615036972513L, 4469119677486524438L, 6862084742702193339L, 2666591392741323510L, 1958911907595193257L, 2078226524874004819L, 9182514826368667184L, -5667455777910095811L, -6961112304229951815L,
|
||||
7984583406477441100L, 5152724216922222472L, -2011927023009527807L, -212234053999724107L, 4838452819165657451L, -8437636414480207278L, -4364095106444861094L, -8843563141488759799L, -952547977505311611L, 7192165871822020282L,
|
||||
-8957588412064574366L, 4293149567017494192L, 6266031685674981260L, 3297360663327026118L, -7424220229153493459L, 1848411117523063487L, 4803542876947788811L, -6514007507455064743L, 3918859449562378630L, 7730455268829558643L,
|
||||
2300310138214025757L, 5073098731442674389L, -1867327214174801803L, -5119713925479725192L, 2481833961960165662L, 3483465760582650171L, -3799159280037322961L, -2614176868807805682L, 3683901813415452623L, -6586240258798896426L,
|
||||
-6280196637815307286L, -6878770741467980580L, -8649528727307138543L, 1263269478536931145L, -7419991789716909164L, -5769815365846261236L, 7280608515770959015L, 7790930297845911262L, -5059374975740702796L, -6705059931318638429L,
|
||||
8900403996915095151L, 8816891275549542045L, -476483339080012016L, -1232282160203339243L, 3119849171172694992L, 7662494604586420558L, 149203013753700084L, 5530308158539891708L, 4143436129840869576L, -3411623459852687238L,
|
||||
-1026352410626214551L, -8324492521276276327L, 6707891355510602429L, 5715986277202524800L, -393206988093480487L, 4600951196636466039L, -4593511655318796512L, 9065747437067558111L, -8901650410637853864L, 2592076422926394627L,
|
||||
228032410479194937L, 6667480117540136779L, 588648581915253038L, -2336950474993240516L, 3634608293302267354L, 1202024298738736502L, 6299068367672194603L, 1932346445954743183L, 7573861666572117031L, -61815566784892605L,
|
||||
3549459440654955014L, 8158286332358861718L, -7670372790848096527L, -515956617046547146L, -3963219078081420846L, 8464707252757847009L, 397230465775035974L, -4957137534187579283L, 675316509725923312L, 2628613740627889320L,
|
||||
-2532211618462009391L, 5345232712238813773L, -4776658006885916949L, 3062009004852183467L, -2381228231588757251L, 74184876899443393L, -1882978417976974457L, 9131956796466541322L, 8604540880985875509L, 22099178757704754L,
|
||||
-1755823172185693422L, -7115222264497037070L, 2945473010562318822L, -3264392033958139096L, 2789803412788518275L, -5023951698716947073L, -2879016497062593138L, 1017933909609308228L, -2136777458168640962L, 8230916861376446652L,
|
||||
-4050239832011059757L, 8983610917420146076L, 8543542228473779244L, 1721876046845854392L, -2252284190053484385L, 5559864569757380000L, 4937681992884682033L, -5441254327629638811L, -9066842030330493037L, 5670390740934713304L,
|
||||
2219071780988037499L, 7008521987288882964L, 6028345117330418825L, -7500176903196747008L, 7071075452076274675L, -1604175089662029304L, 1445978213955986826L, -7979034942316814172L, 951333080223670799L, 6099155138413436065L,
|
||||
-4305900099056973791L, -6236769450809946705L, -2912898243239114769L, -2065740773420267803L, -3827177893057145596L, 1340472571717533606L, -3648363291767490877L, -5756567784146095673L, 4461163794677446508L, -5848717005041324781L,
|
||||
3341940384398866564L, -4882598382547103543L, 3829921822543532494L, 899996630714791418L, 6478536468284266291L, 2994597028103565543L, 6124895672834828926L, -8376542604899771579L, -4412652237062246342L, -7724700941812371646L,
|
||||
728866099714851926L, 339635816873858970L, -1153572816294167456L, -592215260546165052L, -7150089944179092253L, 8700134485486622004L, -5552633324984327062L, -1298517758115136471L, 8749621007278605595L, -6133576477421907076L,
|
||||
4199955888901663150L, -5341432795218012713L, -239890188217778377L, 8106773277103211697L, -2229320058079270256L, 5930619164422717276L, 4368075505682949467L, 4623369983466747106L, 8403817438537116875L, -5327756068839670070L,
|
||||
1151085119119418028L, 6933250016240323664L, 6814675599201764477L, -2995490164984896514L, 5778917359701360712L, -7334472845550608018L, -9212347808668562614L, -7786744047088363785L, 4025584697920591189L, 5446500518121291045L,
|
||||
-7866665254384488512L, -352887593087136842L, 8290028954029701554L, -9087549732707247512L, 7234639242841923679L, 2860911103167493259L, -3716770017321781837L, 7444204691177324181L, 8012224255291120002L, 6549509778060988165L,
|
||||
-4656265058823564969L, -1532696805485516055L, 4993489137437819341L, 4727924503904151836L, -3180601338503688336L, 7858325008468642462L
|
||||
};
|
||||
|
||||
private static readonly long[] t4 = new long[256]
|
||||
{
|
||||
6561287832113134677L, 1893413629145602549L, -6205320776685678598L, 7334764389497132503L, 421942495471316930L, -9085229951450268347L, 5948965432456907277L, -6872877502453521409L, 4831763938021002582L, -4272888574428519313L,
|
||||
5678704711006605406L, 4536654317168965104L, 802439540090739142L, 1728614842704535657L, 7852250862810361152L, -2970083550513149273L, 6999787169451700297L, 327545298748531618L, -2764213178345403342L, 9213801181845131435L,
|
||||
-5950018878971805109L, -2186876610533351532L, -3100863505161590557L, -194921935069456237L, 2629011484744925146L, 679658461659738748L, -3068808746888436091L, 2845612796809381245L, -7722098226173915145L, 7273530125705028225L,
|
||||
4410076014410041819L, -2304212329100317967L, -45936371244098582L, -5712723046817425393L, 8922873767131958175L, -3382299200423854708L, -3236816455951139535L, -4036747678298392505L, 5226125132195873799L, 2940247444995640068L,
|
||||
-4418018165041970817L, 6671397049608501367L, 8821388386505911040L, -3580187736799586652L, -1447046360908978430L, 2147098610462912262L, -1956265881574637814L, -2856917834249223582L, 5141735866072457044L, 3265027362719053310L,
|
||||
-6450920645962515936L, 6017965846669640613L, 4287051124723328232L, 8655371236021312991L, -1156847972119148173L, 2365060307249772354L, 1630631832073154105L, 1828719980936758421L, 2674037562503248056L, -7295616781251116690L,
|
||||
-1363141094472255887L, 204405347605452144L, 5797523068258732423L, 8122903338174012641L, 8739821670855295734L, 961841682317282412L, 3487881148722869326L, -7995384159388863717L, 7665614591556333409L, -7831409025227614873L,
|
||||
-822907162794399275L, -1691135090558933875L, 3797048810173566205L, -2578904300750297763L, -3410711173298709536L, 577633178325057199L, -7379212936790430923L, -9035774148364232240L, 2754939666238358593L, 8444132705799138470L,
|
||||
-7894221632442939675L, 3065464070595795438L, -6610449357786147779L, 3184382822055416328L, 5740274767717360273L, 6179930651821454089L, -4826152258144849421L, 5115645765347262247L, 4602739923119569497L, -3465801151231271281L,
|
||||
-6359599548771540712L, -1926152657970122275L, -8468989295385802946L, -6500580506154635033L, 4125629484990072616L, -6834670983768857044L, -4845179353893108027L, 4230689665262407186L, -1849684427061896393L, 9047540561879224854L,
|
||||
1112218670439199625L, 8426162753992594376L, -5990769681480860131L, -2503790423972405993L, 4028912247909671416L, -409156412951274838L, -8377831951645714695L, -1152570669068554652L, -6327418252815316840L, -3725559206061705268L,
|
||||
1964465731879646024L, -2441760721249263597L, 6946242362685775318L, -3298979752616086841L, -7236283555339513389L, -1419193050620496778L, -93735727476260563L, -5905399081030416230L, 2507248404937789251L, 7581261321693772141L,
|
||||
-8836566033099333598L, 520172056875071564L, 3738403388662150470L, -2357506837776452040L, -5002739851233418934L, 930169001927683533L, 6889748805645999668L, -1031349426815687751L, 7941113837267854943L, -1243211017071393764L,
|
||||
-2154628650105719635L, 6332043450707792835L, 3386824618901547762L, 7130458179308482168L, 1271522336860346025L, -997034324337437613L, 4823850509807911142L, 3107332511049695348L, 5437793788182680416L, -8315628002795417155L,
|
||||
1494290439970088554L, -8609438560643873897L, -8207953325454440687L, -5432621302919780015L, 1159256241058966379L, 1026141471931805870L, -8215608786054685932L, -609691062749569444L, 7511556330643118785L, -3915792337899679783L,
|
||||
3932170512244996561L, 6834333685245251200L, 4355290964656419152L, 6487547078612259600L, 6267880520331323438L, -1545475867304599653L, 8190919284549556346L, 3366895789332200348L, 2444540809879438627L, 6459524513146455969L,
|
||||
4077716903750958194L, -6168929569432701476L, -6973483665415634802L, -5197441416039796052L, 7734160491610189202L, 7910254887717195099L, 3836881802794822270L, 8311228008842563790L, 730509642500215940L, -650400159804944995L,
|
||||
-5124223765383482859L, 3579688877020158541L, 8591780283260295173L, 5028082178778891827L, -498814760953987530L, -2709709455026140056L, 5487541034902828271L, 8530400576707172340L, -7604535187505054453L, -869656751120750718L,
|
||||
4656569414526204412L, 491061932033469878L, 8035458231926703496L, 137019260109594401L, 7421708309958176805L, 8223709417363553275L, 5401705824239018731L, -7162608250562934562L, 5308870500428712900L, -5508949737295341638L,
|
||||
1376856236535589493L, -5655908917112005032L, -7100674984259216372L, 1332977380922036690L, 3015788518022419172L, -6718854486329987908L, 6396540069380292132L, 2034188120276215631L, -1655134238111203034L, -509741179510489141L,
|
||||
3623665942510192329L, -9164935270648710301L, 1765784450088366494L, 5837777785993897047L, 1564973338399864744L, -2605395199060435761L, 4964475598524693274L, -5312043978489901415L, 6706291041494563888L, -789946623649963734L,
|
||||
-8091303779971721549L, 7456716478970921562L, -335263357675197259L, -8515348892102079999L, -7048796562806032069L, -233028078259189719L, 284725780453796946L, -3832073186324226638L, -4921235094493811069L, -5089093504863659344L,
|
||||
-5607539644671350465L, -8911681616096439592L, -4743899514573401058L, -7664321526450198170L, -4599281686566632149L, 2560491659082246267L, 8971180328015050686L, 2265540171276805379L, 6093561527083620308L, 12169565841013306L,
|
||||
9128413284208255679L, -4178722056535276608L, -8960148414521589626L, -4216952774774654326L, -5374970407177951367L, -6668788646589711127L, -2946910590031425822L, -8674853389405194592L, -7535980417822448849L, -6115357923114297461L,
|
||||
-8065837346967928004L, -7487037274649424496L, -2061373546992596293L, -5783192355322733388L, 7153300451507295513L, -8779488031786375734L, 2187906506867626476L, 5612681432830855607L, -4653220181978985551L, 4688837593722596333L,
|
||||
-3815667051463559517L, -1779743783662362556L, -3650491565905270770L, -4529053496248414107L, -4021111997381021802L, -4350414089199835873L
|
||||
};
|
||||
|
||||
private long a;
|
||||
|
||||
private long b;
|
||||
|
||||
private long c;
|
||||
|
||||
private long byteCount;
|
||||
|
||||
private byte[] Buffer = new byte[8];
|
||||
|
||||
private int bOff;
|
||||
|
||||
private long[] x = new long[8];
|
||||
|
||||
private int xOff;
|
||||
|
||||
public string AlgorithmName => "Tiger";
|
||||
|
||||
public TigerDigest()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public TigerDigest(TigerDigest t)
|
||||
{
|
||||
Reset(t);
|
||||
}
|
||||
|
||||
public int GetDigestSize()
|
||||
{
|
||||
return 24;
|
||||
}
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
private void ProcessWord(byte[] b, int off)
|
||||
{
|
||||
x[xOff++] = ((long)(b[off + 7] & 0xFF) << 56) | ((long)(b[off + 6] & 0xFF) << 48) | ((long)(b[off + 5] & 0xFF) << 40) | ((long)(b[off + 4] & 0xFF) << 32) | ((long)(b[off + 3] & 0xFF) << 24) | ((long)(b[off + 2] & 0xFF) << 16) | ((long)(b[off + 1] & 0xFF) << 8) | (uint)(b[off] & 0xFF);
|
||||
if (xOff == x.Length)
|
||||
{
|
||||
ProcessBlock();
|
||||
}
|
||||
bOff = 0;
|
||||
}
|
||||
|
||||
public void Update(byte input)
|
||||
{
|
||||
Buffer[bOff++] = input;
|
||||
if (bOff == Buffer.Length)
|
||||
{
|
||||
ProcessWord(Buffer, 0);
|
||||
}
|
||||
byteCount++;
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] input, int inOff, int length)
|
||||
{
|
||||
while (bOff != 0 && length > 0)
|
||||
{
|
||||
Update(input[inOff]);
|
||||
inOff++;
|
||||
length--;
|
||||
}
|
||||
while (length > 8)
|
||||
{
|
||||
ProcessWord(input, inOff);
|
||||
inOff += 8;
|
||||
length -= 8;
|
||||
byteCount += 8L;
|
||||
}
|
||||
while (length > 0)
|
||||
{
|
||||
Update(input[inOff]);
|
||||
inOff++;
|
||||
length--;
|
||||
}
|
||||
}
|
||||
|
||||
private void RoundABC(long x, long mul)
|
||||
{
|
||||
c ^= x;
|
||||
a -= t1[(int)c & 0xFF] ^ t2[(int)(c >> 16) & 0xFF] ^ t3[(int)(c >> 32) & 0xFF] ^ t4[(int)(c >> 48) & 0xFF];
|
||||
b += t4[(int)(c >> 8) & 0xFF] ^ t3[(int)(c >> 24) & 0xFF] ^ t2[(int)(c >> 40) & 0xFF] ^ t1[(int)(c >> 56) & 0xFF];
|
||||
b *= mul;
|
||||
}
|
||||
|
||||
private void RoundBCA(long x, long mul)
|
||||
{
|
||||
a ^= x;
|
||||
b -= t1[(int)a & 0xFF] ^ t2[(int)(a >> 16) & 0xFF] ^ t3[(int)(a >> 32) & 0xFF] ^ t4[(int)(a >> 48) & 0xFF];
|
||||
c += t4[(int)(a >> 8) & 0xFF] ^ t3[(int)(a >> 24) & 0xFF] ^ t2[(int)(a >> 40) & 0xFF] ^ t1[(int)(a >> 56) & 0xFF];
|
||||
c *= mul;
|
||||
}
|
||||
|
||||
private void RoundCAB(long x, long mul)
|
||||
{
|
||||
b ^= x;
|
||||
c -= t1[(int)b & 0xFF] ^ t2[(int)(b >> 16) & 0xFF] ^ t3[(int)(b >> 32) & 0xFF] ^ t4[(int)(b >> 48) & 0xFF];
|
||||
a += t4[(int)(b >> 8) & 0xFF] ^ t3[(int)(b >> 24) & 0xFF] ^ t2[(int)(b >> 40) & 0xFF] ^ t1[(int)(b >> 56) & 0xFF];
|
||||
a *= mul;
|
||||
}
|
||||
|
||||
private void KeySchedule()
|
||||
{
|
||||
long[] array;
|
||||
(array = x)[0] = array[0] - (x[7] ^ -6510615555426900571L);
|
||||
(array = x)[1] = array[1] ^ x[0];
|
||||
(array = x)[2] = array[2] + x[1];
|
||||
(array = x)[3] = array[3] - (x[2] ^ (~x[1] << 19));
|
||||
(array = x)[4] = array[4] ^ x[3];
|
||||
(array = x)[5] = array[5] + x[4];
|
||||
(array = x)[6] = array[6] - (x[5] ^ (~x[4] >>> 23));
|
||||
(array = x)[7] = array[7] ^ x[6];
|
||||
(array = x)[0] = array[0] + x[7];
|
||||
(array = x)[1] = array[1] - (x[0] ^ (~x[7] << 19));
|
||||
(array = x)[2] = array[2] ^ x[1];
|
||||
(array = x)[3] = array[3] + x[2];
|
||||
(array = x)[4] = array[4] - (x[3] ^ (~x[2] >>> 23));
|
||||
(array = x)[5] = array[5] ^ x[4];
|
||||
(array = x)[6] = array[6] + x[5];
|
||||
(array = x)[7] = array[7] - (x[6] ^ 0x123456789ABCDEFL);
|
||||
}
|
||||
|
||||
private void ProcessBlock()
|
||||
{
|
||||
long num = a;
|
||||
long num2 = b;
|
||||
long num3 = c;
|
||||
RoundABC(x[0], 5L);
|
||||
RoundBCA(x[1], 5L);
|
||||
RoundCAB(x[2], 5L);
|
||||
RoundABC(x[3], 5L);
|
||||
RoundBCA(x[4], 5L);
|
||||
RoundCAB(x[5], 5L);
|
||||
RoundABC(x[6], 5L);
|
||||
RoundBCA(x[7], 5L);
|
||||
KeySchedule();
|
||||
RoundCAB(x[0], 7L);
|
||||
RoundABC(x[1], 7L);
|
||||
RoundBCA(x[2], 7L);
|
||||
RoundCAB(x[3], 7L);
|
||||
RoundABC(x[4], 7L);
|
||||
RoundBCA(x[5], 7L);
|
||||
RoundCAB(x[6], 7L);
|
||||
RoundABC(x[7], 7L);
|
||||
KeySchedule();
|
||||
RoundBCA(x[0], 9L);
|
||||
RoundCAB(x[1], 9L);
|
||||
RoundABC(x[2], 9L);
|
||||
RoundBCA(x[3], 9L);
|
||||
RoundCAB(x[4], 9L);
|
||||
RoundABC(x[5], 9L);
|
||||
RoundBCA(x[6], 9L);
|
||||
RoundCAB(x[7], 9L);
|
||||
a ^= num;
|
||||
b -= num2;
|
||||
c += num3;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != x.Length; i++)
|
||||
{
|
||||
x[i] = 0L;
|
||||
}
|
||||
}
|
||||
|
||||
private void UnpackWord(long r, byte[] output, int outOff)
|
||||
{
|
||||
output[outOff + 7] = (byte)(r >> 56);
|
||||
output[outOff + 6] = (byte)(r >> 48);
|
||||
output[outOff + 5] = (byte)(r >> 40);
|
||||
output[outOff + 4] = (byte)(r >> 32);
|
||||
output[outOff + 3] = (byte)(r >> 24);
|
||||
output[outOff + 2] = (byte)(r >> 16);
|
||||
output[outOff + 1] = (byte)(r >> 8);
|
||||
output[outOff] = (byte)r;
|
||||
}
|
||||
|
||||
private void ProcessLength(long bitLength)
|
||||
{
|
||||
x[7] = bitLength;
|
||||
}
|
||||
|
||||
private void Finish()
|
||||
{
|
||||
long bitLength = byteCount << 3;
|
||||
Update(1);
|
||||
while (bOff != 0)
|
||||
{
|
||||
Update(0);
|
||||
}
|
||||
ProcessLength(bitLength);
|
||||
ProcessBlock();
|
||||
}
|
||||
|
||||
public int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
Finish();
|
||||
UnpackWord(a, output, outOff);
|
||||
UnpackWord(b, output, outOff + 8);
|
||||
UnpackWord(c, output, outOff + 16);
|
||||
Reset();
|
||||
return 24;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
a = 81985529216486895L;
|
||||
b = -81985529216486896L;
|
||||
c = -1110518062304271993L;
|
||||
xOff = 0;
|
||||
for (int i = 0; i != x.Length; i++)
|
||||
{
|
||||
x[i] = 0L;
|
||||
}
|
||||
bOff = 0;
|
||||
for (int j = 0; j != Buffer.Length; j++)
|
||||
{
|
||||
Buffer[j] = 0;
|
||||
}
|
||||
byteCount = 0L;
|
||||
}
|
||||
|
||||
public IMemoable Copy()
|
||||
{
|
||||
return new TigerDigest(this);
|
||||
}
|
||||
|
||||
public void Reset(IMemoable other)
|
||||
{
|
||||
TigerDigest tigerDigest = (TigerDigest)other;
|
||||
a = tigerDigest.a;
|
||||
b = tigerDigest.b;
|
||||
c = tigerDigest.c;
|
||||
Array.Copy(tigerDigest.x, 0, x, 0, tigerDigest.x.Length);
|
||||
xOff = tigerDigest.xOff;
|
||||
Array.Copy(tigerDigest.Buffer, 0, Buffer, 0, tigerDigest.Buffer.Length);
|
||||
bOff = tigerDigest.bOff;
|
||||
byteCount = tigerDigest.byteCount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,387 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
public sealed class WhirlpoolDigest : IDigest, IMemoable
|
||||
{
|
||||
private const int BYTE_LENGTH = 64;
|
||||
|
||||
private const int DIGEST_LENGTH_BYTES = 64;
|
||||
|
||||
private const int ROUNDS = 10;
|
||||
|
||||
private const int REDUCTION_POLYNOMIAL = 285;
|
||||
|
||||
private const int BITCOUNT_ARRAY_SIZE = 32;
|
||||
|
||||
private static readonly int[] SBOX;
|
||||
|
||||
private static readonly long[] C0;
|
||||
|
||||
private static readonly long[] C1;
|
||||
|
||||
private static readonly long[] C2;
|
||||
|
||||
private static readonly long[] C3;
|
||||
|
||||
private static readonly long[] C4;
|
||||
|
||||
private static readonly long[] C5;
|
||||
|
||||
private static readonly long[] C6;
|
||||
|
||||
private static readonly long[] C7;
|
||||
|
||||
private readonly long[] _rc = new long[11];
|
||||
|
||||
private static readonly short[] EIGHT;
|
||||
|
||||
private byte[] _buffer = new byte[64];
|
||||
|
||||
private int _bufferPos;
|
||||
|
||||
private short[] _bitCount = new short[32];
|
||||
|
||||
private long[] _hash = new long[8];
|
||||
|
||||
private long[] _K = new long[8];
|
||||
|
||||
private long[] _L = new long[8];
|
||||
|
||||
private long[] _block = new long[8];
|
||||
|
||||
private long[] _state = new long[8];
|
||||
|
||||
public string AlgorithmName => "Whirlpool";
|
||||
|
||||
static WhirlpoolDigest()
|
||||
{
|
||||
SBOX = new int[256]
|
||||
{
|
||||
24, 35, 198, 232, 135, 184, 1, 79, 54, 166,
|
||||
210, 245, 121, 111, 145, 82, 96, 188, 155, 142,
|
||||
163, 12, 123, 53, 29, 224, 215, 194, 46, 75,
|
||||
254, 87, 21, 119, 55, 229, 159, 240, 74, 218,
|
||||
88, 201, 41, 10, 177, 160, 107, 133, 189, 93,
|
||||
16, 244, 203, 62, 5, 103, 228, 39, 65, 139,
|
||||
167, 125, 149, 216, 251, 238, 124, 102, 221, 23,
|
||||
71, 158, 202, 45, 191, 7, 173, 90, 131, 51,
|
||||
99, 2, 170, 113, 200, 25, 73, 217, 242, 227,
|
||||
91, 136, 154, 38, 50, 176, 233, 15, 213, 128,
|
||||
190, 205, 52, 72, 255, 122, 144, 95, 32, 104,
|
||||
26, 174, 180, 84, 147, 34, 100, 241, 115, 18,
|
||||
64, 8, 195, 236, 219, 161, 141, 61, 151, 0,
|
||||
207, 43, 118, 130, 214, 27, 181, 175, 106, 80,
|
||||
69, 243, 48, 239, 63, 85, 162, 234, 101, 186,
|
||||
47, 192, 222, 28, 253, 77, 146, 117, 6, 138,
|
||||
178, 230, 14, 31, 98, 212, 168, 150, 249, 197,
|
||||
37, 89, 132, 114, 57, 76, 94, 120, 56, 140,
|
||||
209, 165, 226, 97, 179, 33, 156, 30, 67, 199,
|
||||
252, 4, 81, 153, 109, 13, 250, 223, 126, 36,
|
||||
59, 171, 206, 17, 143, 78, 183, 235, 60, 129,
|
||||
148, 247, 185, 19, 44, 211, 231, 110, 196, 3,
|
||||
86, 68, 127, 169, 42, 187, 193, 83, 220, 11,
|
||||
157, 108, 49, 116, 246, 70, 172, 137, 20, 225,
|
||||
22, 58, 105, 9, 112, 182, 208, 237, 204, 66,
|
||||
152, 164, 40, 92, 248, 134
|
||||
};
|
||||
C0 = new long[256];
|
||||
C1 = new long[256];
|
||||
C2 = new long[256];
|
||||
C3 = new long[256];
|
||||
C4 = new long[256];
|
||||
C5 = new long[256];
|
||||
C6 = new long[256];
|
||||
C7 = new long[256];
|
||||
EIGHT = new short[32];
|
||||
EIGHT[31] = 8;
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
int num = SBOX[i];
|
||||
int num2 = maskWithReductionPolynomial(num << 1);
|
||||
int num3 = maskWithReductionPolynomial(num2 << 1);
|
||||
int num4 = num3 ^ num;
|
||||
int num5 = maskWithReductionPolynomial(num3 << 1);
|
||||
int num6 = num5 ^ num;
|
||||
C0[i] = packIntoLong(num, num, num3, num, num5, num4, num2, num6);
|
||||
C1[i] = packIntoLong(num6, num, num, num3, num, num5, num4, num2);
|
||||
C2[i] = packIntoLong(num2, num6, num, num, num3, num, num5, num4);
|
||||
C3[i] = packIntoLong(num4, num2, num6, num, num, num3, num, num5);
|
||||
C4[i] = packIntoLong(num5, num4, num2, num6, num, num, num3, num);
|
||||
C5[i] = packIntoLong(num, num5, num4, num2, num6, num, num, num3);
|
||||
C6[i] = packIntoLong(num3, num, num5, num4, num2, num6, num, num);
|
||||
C7[i] = packIntoLong(num, num3, num, num5, num4, num2, num6, num);
|
||||
}
|
||||
}
|
||||
|
||||
public WhirlpoolDigest()
|
||||
{
|
||||
_rc[0] = 0L;
|
||||
for (int i = 1; i <= 10; i++)
|
||||
{
|
||||
int num = 8 * (i - 1);
|
||||
_rc[i] = (C0[num] & -72057594037927936L) ^ (C1[num + 1] & 0xFF000000000000L) ^ (C2[num + 2] & 0xFF0000000000L) ^ (C3[num + 3] & 0xFF00000000L) ^ (C4[num + 4] & 0xFF000000u) ^ (C5[num + 5] & 0xFF0000) ^ (C6[num + 6] & 0xFF00) ^ (C7[num + 7] & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
private static long packIntoLong(int b7, int b6, int b5, int b4, int b3, int b2, int b1, int b0)
|
||||
{
|
||||
return ((long)b7 << 56) ^ ((long)b6 << 48) ^ ((long)b5 << 40) ^ ((long)b4 << 32) ^ ((long)b3 << 24) ^ ((long)b2 << 16) ^ ((long)b1 << 8) ^ b0;
|
||||
}
|
||||
|
||||
private static int maskWithReductionPolynomial(int input)
|
||||
{
|
||||
int num = input;
|
||||
if ((long)num >= 256L)
|
||||
{
|
||||
num ^= 0x11D;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public WhirlpoolDigest(WhirlpoolDigest originalDigest)
|
||||
{
|
||||
Reset(originalDigest);
|
||||
}
|
||||
|
||||
public int GetDigestSize()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
public int DoFinal(byte[] output, int outOff)
|
||||
{
|
||||
finish();
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
convertLongToByteArray(_hash[i], output, outOff + i * 8);
|
||||
}
|
||||
Reset();
|
||||
return GetDigestSize();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_bufferPos = 0;
|
||||
Array.Clear(_bitCount, 0, _bitCount.Length);
|
||||
Array.Clear(_buffer, 0, _buffer.Length);
|
||||
Array.Clear(_hash, 0, _hash.Length);
|
||||
Array.Clear(_K, 0, _K.Length);
|
||||
Array.Clear(_L, 0, _L.Length);
|
||||
Array.Clear(_block, 0, _block.Length);
|
||||
Array.Clear(_state, 0, _state.Length);
|
||||
}
|
||||
|
||||
private void processFilledBuffer()
|
||||
{
|
||||
for (int i = 0; i < _state.Length; i++)
|
||||
{
|
||||
_block[i] = bytesToLongFromBuffer(_buffer, i * 8);
|
||||
}
|
||||
processBlock();
|
||||
_bufferPos = 0;
|
||||
Array.Clear(_buffer, 0, _buffer.Length);
|
||||
}
|
||||
|
||||
private static long bytesToLongFromBuffer(byte[] buffer, int startPos)
|
||||
{
|
||||
return (long)((((ulong)buffer[startPos] & 0xFFuL) << 56) | (((ulong)buffer[startPos + 1] & 0xFFuL) << 48) | (((ulong)buffer[startPos + 2] & 0xFFuL) << 40) | (((ulong)buffer[startPos + 3] & 0xFFuL) << 32) | (((ulong)buffer[startPos + 4] & 0xFFuL) << 24) | (((ulong)buffer[startPos + 5] & 0xFFuL) << 16) | (((ulong)buffer[startPos + 6] & 0xFFuL) << 8) | ((ulong)buffer[startPos + 7] & 0xFFuL));
|
||||
}
|
||||
|
||||
private static void convertLongToByteArray(long inputLong, byte[] outputArray, int offSet)
|
||||
{
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
outputArray[offSet + i] = (byte)((inputLong >> 56 - i * 8) & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
private void processBlock()
|
||||
{
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
_state[i] = _block[i] ^ (_K[i] = _hash[i]);
|
||||
}
|
||||
for (int j = 1; j <= 10; j++)
|
||||
{
|
||||
long[] l;
|
||||
for (int k = 0; k < 8; k++)
|
||||
{
|
||||
_L[k] = 0L;
|
||||
long[] array = (l = _L);
|
||||
int num = k;
|
||||
nint num2 = num;
|
||||
array[num] = l[num2] ^ C0[(int)(_K[k & 7] >> 56) & 0xFF];
|
||||
long[] array2 = (l = _L);
|
||||
int num3 = k;
|
||||
num2 = num3;
|
||||
array2[num3] = l[num2] ^ C1[(int)(_K[(k - 1) & 7] >> 48) & 0xFF];
|
||||
long[] array3 = (l = _L);
|
||||
int num4 = k;
|
||||
num2 = num4;
|
||||
array3[num4] = l[num2] ^ C2[(int)(_K[(k - 2) & 7] >> 40) & 0xFF];
|
||||
long[] array4 = (l = _L);
|
||||
int num5 = k;
|
||||
num2 = num5;
|
||||
array4[num5] = l[num2] ^ C3[(int)(_K[(k - 3) & 7] >> 32) & 0xFF];
|
||||
long[] array5 = (l = _L);
|
||||
int num6 = k;
|
||||
num2 = num6;
|
||||
array5[num6] = l[num2] ^ C4[(int)(_K[(k - 4) & 7] >> 24) & 0xFF];
|
||||
long[] array6 = (l = _L);
|
||||
int num7 = k;
|
||||
num2 = num7;
|
||||
array6[num7] = l[num2] ^ C5[(int)(_K[(k - 5) & 7] >> 16) & 0xFF];
|
||||
long[] array7 = (l = _L);
|
||||
int num8 = k;
|
||||
num2 = num8;
|
||||
array7[num8] = l[num2] ^ C6[(int)(_K[(k - 6) & 7] >> 8) & 0xFF];
|
||||
long[] array8 = (l = _L);
|
||||
int num9 = k;
|
||||
num2 = num9;
|
||||
array8[num9] = l[num2] ^ C7[(int)_K[(k - 7) & 7] & 0xFF];
|
||||
}
|
||||
Array.Copy(_L, 0, _K, 0, _K.Length);
|
||||
(l = _K)[0] = l[0] ^ _rc[j];
|
||||
for (int m = 0; m < 8; m++)
|
||||
{
|
||||
_L[m] = _K[m];
|
||||
long[] array9 = (l = _L);
|
||||
int num10 = m;
|
||||
nint num2 = num10;
|
||||
array9[num10] = l[num2] ^ C0[(int)(_state[m & 7] >> 56) & 0xFF];
|
||||
long[] array10 = (l = _L);
|
||||
int num11 = m;
|
||||
num2 = num11;
|
||||
array10[num11] = l[num2] ^ C1[(int)(_state[(m - 1) & 7] >> 48) & 0xFF];
|
||||
long[] array11 = (l = _L);
|
||||
int num12 = m;
|
||||
num2 = num12;
|
||||
array11[num12] = l[num2] ^ C2[(int)(_state[(m - 2) & 7] >> 40) & 0xFF];
|
||||
long[] array12 = (l = _L);
|
||||
int num13 = m;
|
||||
num2 = num13;
|
||||
array12[num13] = l[num2] ^ C3[(int)(_state[(m - 3) & 7] >> 32) & 0xFF];
|
||||
long[] array13 = (l = _L);
|
||||
int num14 = m;
|
||||
num2 = num14;
|
||||
array13[num14] = l[num2] ^ C4[(int)(_state[(m - 4) & 7] >> 24) & 0xFF];
|
||||
long[] array14 = (l = _L);
|
||||
int num15 = m;
|
||||
num2 = num15;
|
||||
array14[num15] = l[num2] ^ C5[(int)(_state[(m - 5) & 7] >> 16) & 0xFF];
|
||||
long[] array15 = (l = _L);
|
||||
int num16 = m;
|
||||
num2 = num16;
|
||||
array15[num16] = l[num2] ^ C6[(int)(_state[(m - 6) & 7] >> 8) & 0xFF];
|
||||
long[] array16 = (l = _L);
|
||||
int num17 = m;
|
||||
num2 = num17;
|
||||
array16[num17] = l[num2] ^ C7[(int)_state[(m - 7) & 7] & 0xFF];
|
||||
}
|
||||
Array.Copy(_L, 0, _state, 0, _state.Length);
|
||||
}
|
||||
for (int n = 0; n < 8; n++)
|
||||
{
|
||||
long[] l;
|
||||
long[] array17 = (l = _hash);
|
||||
int num18 = n;
|
||||
nint num2 = num18;
|
||||
array17[num18] = l[num2] ^ (_state[n] ^ _block[n]);
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(byte input)
|
||||
{
|
||||
_buffer[_bufferPos] = input;
|
||||
_bufferPos++;
|
||||
if (_bufferPos == _buffer.Length)
|
||||
{
|
||||
processFilledBuffer();
|
||||
}
|
||||
increment();
|
||||
}
|
||||
|
||||
private void increment()
|
||||
{
|
||||
int num = 0;
|
||||
for (int num2 = _bitCount.Length - 1; num2 >= 0; num2--)
|
||||
{
|
||||
int num3 = (_bitCount[num2] & 0xFF) + EIGHT[num2] + num;
|
||||
num = num3 >> 8;
|
||||
_bitCount[num2] = (short)(num3 & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
public void BlockUpdate(byte[] input, int inOff, int length)
|
||||
{
|
||||
while (length > 0)
|
||||
{
|
||||
Update(input[inOff]);
|
||||
inOff++;
|
||||
length--;
|
||||
}
|
||||
}
|
||||
|
||||
private void finish()
|
||||
{
|
||||
byte[] array = copyBitLength();
|
||||
byte[] buffer;
|
||||
byte[] array2 = (buffer = _buffer);
|
||||
int num = _bufferPos++;
|
||||
nint num2 = num;
|
||||
array2[num] = (byte)(buffer[num2] | 0x80);
|
||||
if (_bufferPos == _buffer.Length)
|
||||
{
|
||||
processFilledBuffer();
|
||||
}
|
||||
if (_bufferPos > 32)
|
||||
{
|
||||
while (_bufferPos != 0)
|
||||
{
|
||||
Update(0);
|
||||
}
|
||||
}
|
||||
while (_bufferPos <= 32)
|
||||
{
|
||||
Update(0);
|
||||
}
|
||||
Array.Copy(array, 0, _buffer, 32, array.Length);
|
||||
processFilledBuffer();
|
||||
}
|
||||
|
||||
private byte[] copyBitLength()
|
||||
{
|
||||
byte[] array = new byte[32];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = (byte)(_bitCount[i] & 0xFF);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public int GetByteLength()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
public IMemoable Copy()
|
||||
{
|
||||
return new WhirlpoolDigest(this);
|
||||
}
|
||||
|
||||
public void Reset(IMemoable other)
|
||||
{
|
||||
WhirlpoolDigest whirlpoolDigest = (WhirlpoolDigest)other;
|
||||
Array.Copy(whirlpoolDigest._rc, 0, _rc, 0, _rc.Length);
|
||||
Array.Copy(whirlpoolDigest._buffer, 0, _buffer, 0, _buffer.Length);
|
||||
_bufferPos = whirlpoolDigest._bufferPos;
|
||||
Array.Copy(whirlpoolDigest._bitCount, 0, _bitCount, 0, _bitCount.Length);
|
||||
Array.Copy(whirlpoolDigest._hash, 0, _hash, 0, _hash.Length);
|
||||
Array.Copy(whirlpoolDigest._K, 0, _K, 0, _K.Length);
|
||||
Array.Copy(whirlpoolDigest._L, 0, _L, 0, _L.Length);
|
||||
Array.Copy(whirlpoolDigest._block, 0, _block, 0, _block.Length);
|
||||
Array.Copy(whirlpoolDigest._state, 0, _state, 0, _state.Length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user