Skip to content

Commit

Permalink
Merge pull request #1 from yaruson/develop
Browse files Browse the repository at this point in the history
First release
  • Loading branch information
Nikolay committed Apr 25, 2015
2 parents 240ec8b + 73ed81c commit c4f6c2a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
4 changes: 3 additions & 1 deletion GostPlugin/GostPlugin/GostCipherEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal class GostCipherEngine : ICipherEngine
0x9C, 0xFD, 0x52, 0xD1, 0x08, 0x22, 0xA6, 0xF7
};

private static readonly string _cipherName = "GOST 28147-89 (256-Bit Key)";

private readonly PwUuid _cipherUuid;

public GostCipherEngine()
Expand All @@ -21,7 +23,7 @@ public GostCipherEngine()

public PwUuid CipherUuid { get { return _cipherUuid; } }

public string DisplayName { get { return "GOST 28147-89 (256-Bit Key)"; } }
public string DisplayName { get { return _cipherName; } }

private Stream CreateStream(Stream sInput, bool bEncrypt, byte[] pbKey, byte[] pbIV)
{
Expand Down
28 changes: 14 additions & 14 deletions GostPlugin/GostPlugin/GostCryptoTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,32 @@ public GostCryptoTransform(byte[] key, byte[] iv, bool encrypt)
/// <returns></returns>
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
if (inputCount == 0)
{
return inputCount;
}
else
if (inputCount != 0)
{
Debug.Assert(inputCount == InputBlockSize, "Input block must be 64-bit long");
}

byte[] dataBlock = new byte[InputBlockSize];
Array.Copy(inputBuffer, inputOffset, dataBlock, 0, inputCount);
byte[] dataBlock = new byte[InputBlockSize];
Array.Copy(inputBuffer, inputOffset, dataBlock, 0, inputCount);

byte[] processed = GostECB.Process(_state, _key, GostECB.SBox_CryptoPro_A, true);
byte[] result = XOr(dataBlock, processed);
byte[] processed = GostECB.Process(_state, _key, GostECB.SBox_CryptoPro_A, true);
byte[] result = XOr(dataBlock, processed);

Array.Copy(result, 0, outputBuffer, outputOffset, inputCount);
Array.Copy(_encrypt ? result : dataBlock, _state, GostECB.BlockSize);
Array.Copy(result, 0, outputBuffer, outputOffset, inputCount);
Array.Copy(_encrypt ? result : dataBlock, _state, GostECB.BlockSize);
}

return inputCount;
}

private byte[] XOr(byte[] a, byte[] b)
{
byte[] c = new byte[a.Length];
Debug.Assert(a.Length == b.Length, "Byte arrays must be same length");

var c = new byte[a.Length];
for (int i = 0; i < a.Length; i++)
{
c[i] = (byte)(a[i] ^ b[i]);
}
return c;
}

Expand All @@ -95,7 +95,7 @@ private byte[] XOr(byte[] a, byte[] b)
/// <returns></returns>
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
byte[] outputBuffer = new byte[inputCount];
var outputBuffer = new byte[inputCount];
TransformBlock(inputBuffer, inputOffset, inputCount, outputBuffer, 0);
return outputBuffer;
}
Expand Down
18 changes: 9 additions & 9 deletions GostPlugin/GostPlugin/GostECB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ public static byte[] Process(byte[] data, byte[] key, byte[][] sBox, bool encryp
Debug.Assert(data.Length == BlockSize, "BlockSize must be 64-bit long");
Debug.Assert(key.Length == KeyLength, "Key must be 256-bit long");

uint a = BitConverter.ToUInt32(data, 0);
uint b = BitConverter.ToUInt32(data, 4);
var a = BitConverter.ToUInt32(data, 0);
var b = BitConverter.ToUInt32(data, 4);

uint[] subKeys = GetSubKeys(key);
var subKeys = GetSubKeys(key);

byte[] result = new byte[8];
var result = new byte[8];

for (int i = 0; i < 32; i++)
{
int keyIndex = GetKeyIndex(i, encrypt);
uint subKey = subKeys[keyIndex];
uint fValue = F(a, subKey, sBox);
uint round = b ^ fValue;
var keyIndex = GetKeyIndex(i, encrypt);
var subKey = subKeys[keyIndex];
var fValue = F(a, subKey, sBox);
var round = b ^ fValue;
if (i < 31)
{
b = a;
Expand Down Expand Up @@ -90,7 +90,7 @@ private static uint Substitute(uint value, byte[][] sBox)

private static uint[] GetSubKeys(byte[] key)
{
uint[] subKeys = new uint[8];
var subKeys = new uint[8];
for (int i = 0; i < 8; i++)
subKeys[i] = (uint)BitConverter.ToUInt32(key, i * 4);
return subKeys;
Expand Down

0 comments on commit c4f6c2a

Please sign in to comment.