1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-25 12:05:59 +01:00

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\IAppIdService.cs" />
<Compile Include="Abstractions\Services\IAuthService.cs" /> <Compile Include="Abstractions\Services\IAuthService.cs" />
<Compile Include="Abstractions\Services\IClipboardService.cs" /> <Compile Include="Abstractions\Services\IClipboardService.cs" />
<Compile Include="Abstractions\Services\IKeyDerivationService.cs" />
<Compile Include="Abstractions\Services\IReflectionService.cs" /> <Compile Include="Abstractions\Services\IReflectionService.cs" />
<Compile Include="Abstractions\Services\ISiteService.cs" /> <Compile Include="Abstractions\Services\ISiteService.cs" />
<Compile Include="Abstractions\Services\IFolderService.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. // This is the main entry point of the application.
static void Main(string[] args) static void Main(string[] args)
{ {
ObjCRuntime.Dlfcn.dlopen(ObjCRuntime.Constants.libSystemLibrary, 0);
// if you want to use a different Application Delegate class from "AppDelegate" // if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here. // you can specify it here.
UIApplication.Main(args, null, "AppDelegate"); 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="Services\ClipboardService.cs" />
<Compile Include="Main.cs" /> <Compile Include="Main.cs" />
<Compile Include="AppDelegate.cs" /> <Compile Include="AppDelegate.cs" />
<Compile Include="Services\KeyDerivationService.cs" />
<Compile Include="Services\ReflectionService.cs" /> <Compile Include="Services\ReflectionService.cs" />
<None Include="app.config" /> <None Include="app.config" />
<None Include="Entitlements.plist" /> <None Include="Entitlements.plist" />