Skip to content

Commit

Permalink
Merge pull request #12 from yaruson/develop
Browse files Browse the repository at this point in the history
Performance optimizations
  • Loading branch information
Nikolay Kim committed May 30, 2015
2 parents 5f0a9a8 + c3efe7f commit 11fb339
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 27 deletions.
6 changes: 6 additions & 0 deletions GostPlugin/GostECB_Test/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>

<supportedRuntime version="v2.0.50727"/></startup>
</configuration>
60 changes: 60 additions & 0 deletions GostPlugin/GostECB_Test/GostECB_Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8F8F172C-B111-4349-BD61-46279EF49555}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GostECB_Test</RootNamespace>
<AssemblyName>GostECB_Test</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GostPlugin\GostPlugin.csproj">
<Project>{6bfa5b86-80ec-497e-97c4-7f114db72699}</Project>
<Name>GostPlugin</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
34 changes: 34 additions & 0 deletions GostPlugin/GostECB_Test/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using GostPlugin;
using System;
using System.Diagnostics;

namespace GostECB_Test
{
class Program
{
static void Main(string[] args)
{
var data = new byte[8];
var key = new byte[32];
var enc = new byte[8];

var r = new Random();
r.NextBytes(key);

var ecb = new GostECB(key, GostECB.SBox_Test);

Stopwatch sw = new Stopwatch();

sw.Start();
for (int i = 0; i < 1048576; i++)
{
r.NextBytes(data);
enc = ecb.Process(data);
}
sw.Stop();
Console.WriteLine("Speed {0:F2} MB/s", 8192.0 / sw.ElapsedMilliseconds);
Console.ReadKey();

}
}
}
36 changes: 36 additions & 0 deletions GostPlugin/GostECB_Test/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("GostECB_Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("GostECB_Test")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8f8f172c-b111-4349-bd61-46279ef49555")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
17 changes: 10 additions & 7 deletions GostPlugin/GostPlugin.sln
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio 14
VisualStudioVersion = 14.0.22823.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GostPlugin", "GostPlugin\GostPlugin.csproj", "{6BFA5B86-80EC-497E-97C4-7F114DB72699}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GostPluginTest", "GostPluginTest\GostPluginTest.csproj", "{F4763372-04BC-4E1A-87A3-435BE6C6A428}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GostECB_Test", "GostECB_Test\GostECB_Test.csproj", "{8F8F172C-B111-4349-BD61-46279EF49555}"
EndProject
Global
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Expand All @@ -17,10 +20,10 @@ Global
{6BFA5B86-80EC-497E-97C4-7F114DB72699}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BFA5B86-80EC-497E-97C4-7F114DB72699}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BFA5B86-80EC-497E-97C4-7F114DB72699}.Release|Any CPU.Build.0 = Release|Any CPU
{F4763372-04BC-4E1A-87A3-435BE6C6A428}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F4763372-04BC-4E1A-87A3-435BE6C6A428}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4763372-04BC-4E1A-87A3-435BE6C6A428}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4763372-04BC-4E1A-87A3-435BE6C6A428}.Release|Any CPU.Build.0 = Release|Any CPU
{8F8F172C-B111-4349-BD61-46279EF49555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8F8F172C-B111-4349-BD61-46279EF49555}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F8F172C-B111-4349-BD61-46279EF49555}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8F8F172C-B111-4349-BD61-46279EF49555}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions GostPlugin/GostPlugin/GostCryptoTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GostPlugin
{
public sealed class GostCryptoTransform : ICryptoTransform
{
private GostECB _ecbTransform = new GostECB();
private GostECB _ecbTransform = null;
private readonly byte[] _key = new byte[GostECB.KeyLength];
private byte[] _state = new byte[GostECB.BlockSize];
private bool _encrypt;
Expand All @@ -22,6 +22,7 @@ public GostCryptoTransform(byte[] key, byte[] iv, bool encrypt)
Debug.Assert(key.Length == GostECB.KeyLength, "Key must be 256-bit long");
Debug.Assert(iv.Length == GostECB.KeyLength, "Initialization Vector must be 64-bit long");

_ecbTransform = new GostECB(key);
Array.Copy(key, _key, GostECB.KeyLength);
Array.Copy(iv, _state, GostECB.BlockSize);
_encrypt = encrypt;
Expand Down Expand Up @@ -70,7 +71,7 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b

Array.Copy(inputBuffer, inputOffset, dataBlock, 0, inputCount);

gamma = _ecbTransform.Process(_state, _key);
gamma = _ecbTransform.Process(_state);
result = XOr(dataBlock, gamma);

Array.Copy(result, 0, outputBuffer, outputOffset, inputCount);
Expand Down
53 changes: 40 additions & 13 deletions GostPlugin/GostPlugin/GostECB.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace GostPlugin
{
Expand Down Expand Up @@ -30,14 +31,37 @@ public class GostECB
new byte[] { 0x0b, 0x0a, 0x0f, 0x05, 0x00, 0x0c, 0x0e, 0x08, 0x06, 0x02, 0x03, 0x09, 0x01, 0x07, 0x0d, 0x04 }
};

[StructLayout(LayoutKind.Explicit)]
private struct UnionSBoxOffsetIndex
{
[FieldOffset(0)]
public uint data;

[FieldOffset(0)]
public byte i0;

[FieldOffset(1)]
public byte i1;

[FieldOffset(2)]
public byte i2;

[FieldOffset(3)]
public byte i3;
}

UnionSBoxOffsetIndex _offset = new UnionSBoxOffsetIndex();

private uint[][] _sBox32;
private uint[] _subKeys;

public GostECB(byte[][] sBox = null)
public GostECB(byte[] key, byte[][] sBox = null)
{
Convert_sBox(sBox == null ? GostECB.SBox_CryptoProA : sBox);
ConvertSBox(sBox == null ? GostECB.SBox_CryptoProA : sBox);
_subKeys = GetSubKeys(key);
}

private void Convert_sBox(byte[][] _sBox)
private void ConvertSBox(byte[][] _sBox)
{
_sBox32 = new uint[4][];

Expand All @@ -52,23 +76,26 @@ private void Convert_sBox(byte[][] _sBox)
}
}

public byte[] Process(byte[] data, byte[] key)
public byte[] Process(byte[] data)
{
Debug.Assert(data.Length == BlockSize, "BlockSize must be 64-bit long");
Debug.Assert(key.Length == KeyLength, "Key must be 256-bit long");

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

var subKeys = GetSubKeys(key);
var result = new byte[8];

for (int i = 0; i < 32; i++)
{
var keyIndex = (i < 24) ? i % 8 : 7 - (i % 8);

/*
var subKey = subKeys[keyIndex];
var fValue = F(a, subKey);
var round = b ^ fValue;
*/

var round = b ^ F(a, _subKeys[keyIndex]);

b = a;
a = round;
Expand All @@ -83,17 +110,17 @@ public byte[] Process(byte[] data, byte[] key)

private uint F(uint block, uint subKey)
{
block = block + subKey;
block =
_sBox32[0][(block & 0x000000ff) >> 0] ^
_sBox32[1][(block & 0x0000ff00) >> 8] ^
_sBox32[2][(block & 0x00ff0000) >> 16] ^
_sBox32[3][(block & 0xff000000) >> 24];
return block;
_offset.data = block + subKey;
return
_sBox32[0][_offset.i0] ^
_sBox32[1][_offset.i1] ^
_sBox32[2][_offset.i2] ^
_sBox32[3][_offset.i3];
}

private uint[] GetSubKeys(byte[] key)
{
Debug.Assert(key.Length == KeyLength, "Key must be 256-bit long");
var subKeys = new uint[8];
for (int i = 0; i < 8; i++)
subKeys[i] = (uint)BitConverter.ToUInt32(key, i * 4);
Expand Down
5 changes: 2 additions & 3 deletions GostPlugin/GostPlugin/GostPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<ItemGroup>
<Reference Include="KeePass, Version=2.29.0.17893, Culture=neutral, PublicKeyToken=fed2ed7716aecf5c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\Program Files (x86)\KeePass Password Safe 2\KeePass.exe</HintPath>
<Reference Include="KeePass">
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\KeePass Password Safe 2\KeePass.exe</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions GostPlugin/GostPlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
//
// You can specify all the values or you can default the Build and Revision Numbers by using the '*'
// as shown below: [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.4.0.0")]
[assembly: AssemblyFileVersion("1.4.0.0")]

0 comments on commit 11fb339

Please sign in to comment.