key derivation service that pinvokes into CommonCrypto for PBKDF2 key

This commit is contained in:
Kyle Spearrin 2016-08-01 00:06:12 -04:00
parent eab691664e
commit fc07844bb6
6 changed files with 6177 additions and 315 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,7 @@
namespace Bit.App.Abstractions
{
public interface IKeyDerivationService
{
byte[] DeriveKey(string password, string salt);
}
}

View file

@ -41,6 +41,7 @@
<Compile Include="Abstractions\Services\IAppIdService.cs" />
<Compile Include="Abstractions\Services\IAuthService.cs" />
<Compile Include="Abstractions\Services\IClipboardService.cs" />
<Compile Include="Abstractions\Services\IKeyDerivationService.cs" />
<Compile Include="Abstractions\Services\IReflectionService.cs" />
<Compile Include="Abstractions\Services\ISiteService.cs" />
<Compile Include="Abstractions\Services\IFolderService.cs" />

View file

@ -12,6 +12,8 @@ namespace Bit.iOS
// This is the main entry point of the application.
static void Main(string[] args)
{
ObjCRuntime.Dlfcn.dlopen(ObjCRuntime.Constants.libSystemLibrary, 0);
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");

View file

@ -0,0 +1,35 @@
using Bit.App.Abstractions;
using Foundation;
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Bit.iOS.Services
{
public class KeyDerivationService : IKeyDerivationService
{
private const uint PBKDFAlgorithm = 2; // PBKDF2
private const uint PseudoRandomAlgorithm = 3; // SHA256
private const uint Rounds = 5000;
public byte[] DeriveKey(string password, string salt)
{
var passwordData = NSData.FromArray(Encoding.UTF8.GetBytes(password));
var saltData = NSData.FromArray(Encoding.UTF8.GetBytes(salt));
var keyData = new NSMutableData();
keyData.Length = 32;
var result = CCKeyCerivationPBKDF(PBKDFAlgorithm, passwordData.Bytes, passwordData.Length, saltData.Bytes,
saltData.Length, PseudoRandomAlgorithm, Rounds, keyData.MutableBytes, keyData.Length);
byte[] keyBytes = new byte[keyData.Length];
Marshal.Copy(keyData.Bytes, keyBytes, 0, Convert.ToInt32(keyData.Length));
return keyBytes;
}
// ref: http://opensource.apple.com//source/CommonCrypto/CommonCrypto-55010/CommonCrypto/CommonKeyDerivation.h
[DllImport(ObjCRuntime.Constants.libSystemLibrary, EntryPoint = "CCKeyDerivationPBKDF")]
public extern static int CCKeyCerivationPBKDF(uint algorithm, IntPtr password, nuint passwordLen,
IntPtr salt, nuint saltLen, uint prf, nuint rounds, IntPtr derivedKey, nuint derivedKeyLength);
}
}

View file

@ -140,6 +140,7 @@
<Compile Include="Services\ClipboardService.cs" />
<Compile Include="Main.cs" />
<Compile Include="AppDelegate.cs" />
<Compile Include="Services\KeyDerivationService.cs" />
<Compile Include="Services\ReflectionService.cs" />
<None Include="app.config" />
<None Include="Entitlements.plist" />