From 8b5f9d8529ac4c66758face566fcb8f65dbcde8b Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 19 Jan 2023 17:10:49 -0500 Subject: [PATCH] Argon2id primitive --- .../Services/CryptoPrimitiveService.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/iOS.Core/Services/CryptoPrimitiveService.cs b/src/iOS.Core/Services/CryptoPrimitiveService.cs index 9d97fa647..a2101e70d 100644 --- a/src/iOS.Core/Services/CryptoPrimitiveService.cs +++ b/src/iOS.Core/Services/CryptoPrimitiveService.cs @@ -39,9 +39,31 @@ namespace Bit.iOS.Core.Services return keyBytes; } + public byte[] Argon2id(byte[] password, byte[] salt, int iterations, int memory, int parallelism) + { + // TODO: Do we need to pass this in somehow like PBKDF2 based on an algorithm + int keySize = 32; + var keyData = new NSMutableData(); + keyData.Length = keySize; + + var passwordData = NSData.FromArray(password); + var saltData = NSData.FromArray(salt); + + argon2id_hash_raw(iterations, memory, parallelism, passwordData.Bytes, passwordData.Length, + saltData.Bytes, saltData.Length, keyData.MutableBytes, keyData.Length); + + var 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")] private extern static int CCKeyCerivationPBKDF(uint algorithm, IntPtr password, nuint passwordLen, IntPtr salt, nuint saltLen, uint prf, nuint rounds, IntPtr derivedKey, nuint derivedKeyLength); + + [DllImport("__Internal", EntryPoint = "argon2id_hash_raw")] + internal static extern int argon2id_hash_raw(int timeCost, int memoryCost, int parallelism, IntPtr pwd, + int pwdlen, IntPtr salt, int saltlen, IntPtr hash, int hashlen); } }