-
Notifications
You must be signed in to change notification settings - Fork 5.1k
PBKDF2 one-shot #48107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
PBKDF2 one-shot #48107
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
5f15fdf
Basic implementation of one-shot PBKDF2 for all platforms.
vcsjones c77b4e7
Use HashAlgorithmName to PAL
vcsjones 1bf9d93
Use psuedo handles if available on the Windows platform.
vcsjones 22ead64
Fix compilation
vcsjones abe4c36
Undo inadvertent change in ref file.
vcsjones 859112e
Detect Windows 7 instead of relying on the NTSTATUS.
vcsjones 62d0ec2
Loosen restrictions and add deeper tests
vcsjones d8e5c2d
Fix empty password on macOS.
vcsjones 5ef000d
Permit empty salts
vcsjones d1c34a3
Fix OpenSSL 1.0 with empty values
vcsjones a873c76
Rename exported function
vcsjones eed78a4
Rename Apple exported function
vcsjones 51b77c1
Cleanup native PALs.
vcsjones 61140da
Merge remote-tracking branch 'ms/master' into 24897-pbkdf2-one-shot
vcsjones 0210441
Fix issue with not slicing the rented buffer.
vcsjones f8d82d3
Use BCryptKeyDerivation on Windows 8+.
vcsjones ff56305
Cleanup and some refactoring for Windows implementation
vcsjones c322666
Use throwing UTF8.
vcsjones 8c6d370
Change to follow approved API.
vcsjones ddfd960
Tests for overlapping input / output buffers.
vcsjones 5a2c91a
Add tests around HMAC block size boundaries.
vcsjones b82528e
Fix block sizes.
vcsjones dc72dc3
XML documentation.
vcsjones 3ec9a55
Code review feedback.
vcsjones be3ca75
Merge remote-tracking branch 'ms/master' into 24897-pbkdf2-one-shot
vcsjones 19faa41
Move high iterations test to OuterLoop.
vcsjones b98a679
Dispose of handles in error conditions
vcsjones bc02881
Rename buffer descriptors with clearer prefix
vcsjones 8f00cf6
Refactor input validation.
vcsjones b2199b1
Test for large passwords and salts.
vcsjones c78ca1a
Merge remote-tracking branch 'ms/master' into 24897-pbkdf2-one-shot
vcsjones b881b3a
Use asserts to make what should be pre-validated paths clearer.
vcsjones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...raries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Pbkdf2.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Security.Cryptography; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class AppleCrypto | ||
{ | ||
internal static unsafe void Pbkdf2( | ||
PAL_HashAlgorithm prfAlgorithm, | ||
ReadOnlySpan<byte> password, | ||
ReadOnlySpan<byte> salt, | ||
int iterations, | ||
Span<byte> destination) | ||
{ | ||
fixed (byte* pPassword = password) | ||
fixed (byte* pSalt = salt) | ||
fixed (byte* pDestination = destination) | ||
{ | ||
int ret = AppleCryptoNative_Pbkdf2( | ||
prfAlgorithm, | ||
pPassword, | ||
password.Length, | ||
pSalt, | ||
salt.Length, | ||
iterations, | ||
pDestination, | ||
destination.Length, | ||
out int ccStatus); | ||
|
||
if (ret == 0) | ||
{ | ||
throw Interop.AppleCrypto.CreateExceptionForCCError( | ||
ccStatus, | ||
Interop.AppleCrypto.CCCryptorStatus); | ||
} | ||
|
||
if (ret != 1) | ||
{ | ||
Debug.Fail($"Pbkdf2 failed with invalid input {ret}"); | ||
throw new CryptographicException(); | ||
} | ||
} | ||
} | ||
|
||
[DllImport(Libraries.AppleCryptoNative)] | ||
private static extern unsafe int AppleCryptoNative_Pbkdf2( | ||
PAL_HashAlgorithm prfAlgorithm, | ||
byte* password, | ||
int passwordLen, | ||
byte* salt, | ||
int saltLen, | ||
int iterations, | ||
byte* derivedKey, | ||
int derivedKeyLen, | ||
out int errorCode); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptAlgPseudoHandle.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class BCrypt | ||
{ | ||
// Pseudo-handles, as defined in bcrypt.h | ||
// TODO: This really should be backed by 'nuint' (see https://github.com/dotnet/roslyn/issues/44110) | ||
public enum BCryptAlgPseudoHandle : uint | ||
{ | ||
BCRYPT_MD5_ALG_HANDLE = 0x00000021, | ||
BCRYPT_SHA1_ALG_HANDLE = 0x00000031, | ||
BCRYPT_SHA256_ALG_HANDLE = 0x00000041, | ||
BCRYPT_SHA384_ALG_HANDLE = 0x00000051, | ||
BCRYPT_SHA512_ALG_HANDLE = 0x00000061, | ||
BCRYPT_PBKDF2_ALG_HANDLE = 0x00000331, | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptDeriveKeyPBKDF2.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
using Microsoft.Win32.SafeHandles; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class BCrypt | ||
{ | ||
[DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] | ||
internal static extern unsafe NTSTATUS BCryptDeriveKeyPBKDF2( | ||
SafeBCryptAlgorithmHandle hPrf, | ||
byte* pbPassword, | ||
int cbPassword, | ||
byte* pbSalt, | ||
int cbSalt, | ||
ulong cIterations, | ||
byte* pbDerivedKey, | ||
int cbDerivedKey, | ||
uint dwFlags); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptGenerateSymmetricKey.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class BCrypt | ||
{ | ||
[DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] | ||
internal static unsafe extern NTSTATUS BCryptGenerateSymmetricKey( | ||
SafeBCryptAlgorithmHandle hAlgorithm, | ||
out SafeBCryptKeyHandle phKey, | ||
IntPtr pbKeyObject, | ||
int cbKeyObject, | ||
byte* pbSecret, | ||
int cbSecret, | ||
uint dwFlags); | ||
|
||
[DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] | ||
internal static unsafe extern NTSTATUS BCryptGenerateSymmetricKey( | ||
nuint hAlgorithm, | ||
out SafeBCryptKeyHandle phKey, | ||
IntPtr pbKeyObject, | ||
int cbKeyObject, | ||
byte* pbSecret, | ||
int cbSecret, | ||
uint dwFlags); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptKeyDerivation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
using Microsoft.Win32.SafeHandles; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class BCrypt | ||
{ | ||
[DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] | ||
internal static unsafe extern NTSTATUS BCryptKeyDerivation( | ||
SafeBCryptKeyHandle hKey, | ||
BCryptBufferDesc* pParameterList, | ||
byte* pbDerivedKey, | ||
int cbDerivedKey, | ||
out uint pcbResult, | ||
int dwFlags); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keyderivation.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include "pal_keyderivation.h" | ||
|
||
#if !defined(TARGET_IOS) && !defined(TARGET_TVOS) | ||
|
||
static int32_t PrfAlgorithmFromHashAlgorithm(PAL_HashAlgorithm hashAlgorithm, CCPseudoRandomAlgorithm* algorithm) | ||
{ | ||
if (algorithm == NULL) | ||
return 0; | ||
|
||
switch (hashAlgorithm) | ||
{ | ||
case PAL_SHA1: | ||
*algorithm = kCCPRFHmacAlgSHA1; | ||
return 1; | ||
case PAL_SHA256: | ||
*algorithm = kCCPRFHmacAlgSHA256; | ||
return 1; | ||
case PAL_SHA384: | ||
*algorithm = kCCPRFHmacAlgSHA384; | ||
return 1; | ||
case PAL_SHA512: | ||
*algorithm = kCCPRFHmacAlgSHA512; | ||
return 1; | ||
default: | ||
*algorithm = 0; | ||
return 0; | ||
} | ||
} | ||
|
||
int32_t AppleCryptoNative_Pbkdf2(PAL_HashAlgorithm prfAlgorithm, | ||
const char* password, | ||
int32_t passwordLen, | ||
const uint8_t* salt, | ||
int32_t saltLen, | ||
int32_t iterations, | ||
uint8_t* derivedKey, | ||
uint32_t derivedKeyLen, | ||
int32_t* errorCode) | ||
{ | ||
if (errorCode != NULL) | ||
*errorCode = noErr; | ||
|
||
if (passwordLen < 0 || saltLen < 0 || iterations < 0 || derivedKey == NULL || | ||
derivedKeyLen < 0 || errorCode == NULL) | ||
{ | ||
return -1; | ||
} | ||
|
||
if (salt == NULL && saltLen != 0) | ||
{ | ||
return -1; | ||
} | ||
|
||
const char* empty = ""; | ||
|
||
if (password == NULL) | ||
{ | ||
if (passwordLen != 0) | ||
{ | ||
return -1; | ||
} | ||
|
||
// macOS will not accept a null password, but it will accept a zero-length | ||
// password with a valid pointer. | ||
password = empty; | ||
} | ||
|
||
CCPseudoRandomAlgorithm prf; | ||
|
||
if (!PrfAlgorithmFromHashAlgorithm(prfAlgorithm, &prf)) | ||
{ | ||
return -2; | ||
} | ||
|
||
CCStatus result = CCKeyDerivationPBKDF(kCCPBKDF2, password, passwordLen, salt, | ||
saltLen, prf, iterations, derivedKey, derivedKeyLen); | ||
*errorCode = result; | ||
return result == kCCSuccess ? 1 : 0; | ||
} | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.