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:
parent
eab691664e
commit
fc07844bb6
6446
src/Android/Resources/Resource.Designer.cs
generated
6446
src/Android/Resources/Resource.Designer.cs
generated
File diff suppressed because it is too large
Load Diff
7
src/App/Abstractions/Services/IKeyDerivationService.cs
Normal file
7
src/App/Abstractions/Services/IKeyDerivationService.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace Bit.App.Abstractions
|
||||||
|
{
|
||||||
|
public interface IKeyDerivationService
|
||||||
|
{
|
||||||
|
byte[] DeriveKey(string password, string salt);
|
||||||
|
}
|
||||||
|
}
|
@ -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" />
|
||||||
|
@ -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");
|
||||||
|
35
src/iOS/Services/KeyDerivationService.cs
Normal file
35
src/iOS/Services/KeyDerivationService.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -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" />
|
||||||
|
Loading…
Reference in New Issue
Block a user