Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Make the master password decryption about 355% faster by using the Bo…
Browse files Browse the repository at this point in the history
…uncy Castle library

Tested on a Lumia 640 with a debug build for a db with 5 000 000 rounds :
- 03:38 with the previous method
- 01:01 with Bouncy Castle

Benchmarked on x86 (100 000 rounds, test run 100 times) :
- 47s with the original WinPass method
- 10s with the KeePass desktop client method
- 8s with Bouncy Castle
  • Loading branch information
mlaily committed May 5, 2016
1 parent 4e4efaf commit fa79d23
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
4 changes: 4 additions & 0 deletions KeePass.IO/KeePass.IO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Reference Include="BouncyCastle.Crypto, Version=1.8.1.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
<HintPath>..\packages\BouncyCastle.1.8.1\lib\BouncyCastle.Crypto.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="ICSharpCode.SharpZipLib.Portable, Version=0.86.0.51803, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SharpZipLib.Portable.0.86.0.0003\lib\portable-net45+netcore45+wp8+win8+wpa81+MonoTouch+MonoAndroid+Xamarin.iOS10\ICSharpCode.SharpZipLib.Portable.dll</HintPath>
<Private>True</Private>
Expand Down
21 changes: 10 additions & 11 deletions KeePass.IO/Utils/PasswordData.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Security.Cryptography;
using System.Text;
using KeePass.IO.Data;
using Windows.Security.Cryptography.Core;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Engines;

namespace KeePass.IO.Utils
{
Expand Down Expand Up @@ -44,23 +44,22 @@ public PasswordData(string password, byte[] keyFile)

public byte[] TransformKey(byte[] transformSeed, int rounds)
{
var block = BufferEx.Clone(_hash);

var cipher = new AesEngine();
cipher.Init(true, new KeyParameter(transformSeed));

var aesEcb = SymmetricKeyAlgorithmProvider
.OpenAlgorithm(SymmetricAlgorithmNames.AesEcb);
var key = aesEcb.CreateSymmetricKey(
CryptographicBuffer.CreateFromByteArray(transformSeed));

IBuffer blockBuffer = CryptographicBuffer
.CreateFromByteArray(_hash);

for (var i = 0; i < rounds; i++)
for (int i = 0; i < rounds; i++)
{
blockBuffer = CryptographicEngine
.Encrypt(key, blockBuffer, null);
cipher.ProcessBlock(block, 0, block, 0);
cipher.ProcessBlock(block, 16, block, 16);
}

byte[] block = null;
CryptographicBuffer.CopyToByteArray(blockBuffer, out block);

return BufferEx.GetHash(block);
}
}
Expand Down
1 change: 1 addition & 0 deletions KeePass.IO/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BouncyCastle" version="1.8.1" targetFramework="wp81" />
<package id="SharpZipLib.Portable" version="0.86.0.0003" targetFramework="wp81" />
</packages>

0 comments on commit fa79d23

Please sign in to comment.