-
Notifications
You must be signed in to change notification settings - Fork 5k
Use BCrypt for ephemeral RSA on Windows #76277
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
205 changes: 205 additions & 0 deletions
205
src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptEncryptDecrypt.RSA.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,205 @@ | ||
// 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 static partial class Interop | ||
{ | ||
internal static partial class BCrypt | ||
{ | ||
[Flags] | ||
private enum BCryptEncryptFlags : uint | ||
{ | ||
BCRYPT_PAD_PKCS1 = 2, | ||
BCRYPT_PAD_OAEP = 4, | ||
} | ||
|
||
[LibraryImport(Libraries.BCrypt)] | ||
private static unsafe partial NTSTATUS BCryptEncrypt( | ||
SafeBCryptKeyHandle hKey, | ||
byte* pbInput, | ||
int cbInput, | ||
void* paddingInfo, | ||
byte* pbIV, | ||
int cbIV, | ||
byte* pbOutput, | ||
int cbOutput, | ||
out int cbResult, | ||
BCryptEncryptFlags dwFlags); | ||
|
||
[LibraryImport(Libraries.BCrypt)] | ||
private static unsafe partial NTSTATUS BCryptDecrypt( | ||
SafeBCryptKeyHandle hKey, | ||
byte* pbInput, | ||
int cbInput, | ||
void* paddingInfo, | ||
byte* pbIV, | ||
int cbIV, | ||
byte* pbOutput, | ||
int cbOutput, | ||
out int cbResult, | ||
BCryptEncryptFlags dwFlags); | ||
|
||
private static unsafe int BCryptEncryptRsa( | ||
SafeBCryptKeyHandle key, | ||
ReadOnlySpan<byte> source, | ||
Span<byte> destination, | ||
void* pPaddingInfo, | ||
BCryptEncryptFlags dwFlags) | ||
{ | ||
// BCryptEncrypt does not accept null/0, only non-null/0. | ||
Span<byte> notNull = stackalloc byte[1]; | ||
scoped ReadOnlySpan<byte> effectiveSource; | ||
|
||
if (source.IsEmpty) | ||
{ | ||
effectiveSource = notNull.Slice(1); | ||
} | ||
else | ||
{ | ||
effectiveSource = source; | ||
} | ||
|
||
NTSTATUS status; | ||
int written; | ||
|
||
fixed (byte* pSource = &MemoryMarshal.GetReference(effectiveSource)) | ||
fixed (byte* pDest = &MemoryMarshal.GetReference(destination)) | ||
{ | ||
status = BCryptEncrypt( | ||
key, | ||
pSource, | ||
source.Length, | ||
pPaddingInfo, | ||
null, | ||
0, | ||
pDest, | ||
destination.Length, | ||
out written, | ||
dwFlags); | ||
} | ||
|
||
if (status != NTSTATUS.STATUS_SUCCESS) | ||
{ | ||
throw CreateCryptographicException(status); | ||
} | ||
|
||
return written; | ||
} | ||
|
||
private static unsafe bool BCryptDecryptRsa( | ||
SafeBCryptKeyHandle key, | ||
ReadOnlySpan<byte> source, | ||
Span<byte> destination, | ||
void* pPaddingInfo, | ||
BCryptEncryptFlags dwFlags, | ||
out int bytesWritten) | ||
{ | ||
NTSTATUS status; | ||
int written; | ||
|
||
fixed (byte* pSource = &MemoryMarshal.GetReference(source)) | ||
fixed (byte* pDest = &MemoryMarshal.GetReference(destination)) | ||
{ | ||
status = BCryptDecrypt( | ||
key, | ||
pSource, | ||
source.Length, | ||
pPaddingInfo, | ||
null, | ||
0, | ||
pDest, | ||
destination.Length, | ||
out written, | ||
dwFlags); | ||
} | ||
|
||
if (status == NTSTATUS.STATUS_SUCCESS) | ||
{ | ||
bytesWritten = written; | ||
return true; | ||
} | ||
|
||
if (status == NTSTATUS.STATUS_BUFFER_TOO_SMALL) | ||
{ | ||
bytesWritten = 0; | ||
return false; | ||
} | ||
|
||
throw CreateCryptographicException(status); | ||
} | ||
|
||
internal static unsafe int BCryptEncryptPkcs1( | ||
SafeBCryptKeyHandle key, | ||
ReadOnlySpan<byte> source, | ||
Span<byte> destination) | ||
{ | ||
return BCryptEncryptRsa( | ||
key, | ||
source, | ||
destination, | ||
null, | ||
BCryptEncryptFlags.BCRYPT_PAD_PKCS1); | ||
} | ||
|
||
internal static unsafe int BCryptEncryptOaep( | ||
SafeBCryptKeyHandle key, | ||
ReadOnlySpan<byte> source, | ||
Span<byte> destination, | ||
string? hashAlgorithmName) | ||
{ | ||
fixed (char* pHashAlg = hashAlgorithmName) | ||
{ | ||
BCRYPT_OAEP_PADDING_INFO paddingInfo = default; | ||
paddingInfo.pszAlgId = (IntPtr)pHashAlg; | ||
|
||
return BCryptEncryptRsa( | ||
key, | ||
source, | ||
destination, | ||
&paddingInfo, | ||
BCryptEncryptFlags.BCRYPT_PAD_OAEP); | ||
} | ||
} | ||
|
||
internal static unsafe bool BCryptDecryptPkcs1( | ||
SafeBCryptKeyHandle key, | ||
ReadOnlySpan<byte> source, | ||
Span<byte> destination, | ||
out int bytesWritten) | ||
{ | ||
return BCryptDecryptRsa( | ||
key, | ||
source, | ||
destination, | ||
null, | ||
BCryptEncryptFlags.BCRYPT_PAD_PKCS1, | ||
out bytesWritten); | ||
} | ||
|
||
internal static unsafe bool BCryptDecryptOaep( | ||
SafeBCryptKeyHandle key, | ||
ReadOnlySpan<byte> source, | ||
Span<byte> destination, | ||
string? hashAlgorithmName, | ||
out int bytesWritten) | ||
{ | ||
fixed (char* pHashAlg = hashAlgorithmName) | ||
{ | ||
BCRYPT_OAEP_PADDING_INFO paddingInfo = default; | ||
paddingInfo.pszAlgId = (IntPtr)pHashAlg; | ||
|
||
return BCryptDecryptRsa( | ||
key, | ||
source, | ||
destination, | ||
&paddingInfo, | ||
BCryptEncryptFlags.BCRYPT_PAD_OAEP, | ||
out bytesWritten); | ||
} | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptFinalizeKey.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,28 @@ | ||
// 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 static partial class Interop | ||
{ | ||
internal static partial class BCrypt | ||
{ | ||
[LibraryImport(Libraries.BCrypt)] | ||
private static unsafe partial NTSTATUS BCryptFinalizeKeyPair( | ||
SafeBCryptKeyHandle hKey, | ||
uint dwFlags); | ||
|
||
internal static void BCryptFinalizeKeyPair(SafeBCryptKeyHandle key) | ||
{ | ||
NTSTATUS status = BCryptFinalizeKeyPair(key, 0); | ||
|
||
if (status != NTSTATUS.STATUS_SUCCESS) | ||
{ | ||
throw CreateCryptographicException(status); | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/libraries/Common/src/Interop/Windows/BCrypt/Interop.BCryptGenerateKeyPair.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,38 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.InteropServices; | ||
|
||
using Microsoft.Win32.SafeHandles; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class BCrypt | ||
{ | ||
[LibraryImport(Libraries.BCrypt)] | ||
private static unsafe partial NTSTATUS BCryptGenerateKeyPair( | ||
SafeBCryptAlgorithmHandle hAlgorithm, | ||
out SafeBCryptKeyHandle phKey, | ||
int dwLength, | ||
vcsjones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint dwFlags); | ||
|
||
internal static SafeBCryptKeyHandle BCryptGenerateKeyPair( | ||
SafeBCryptAlgorithmHandle hAlgorithm, | ||
int keyLength) | ||
{ | ||
NTSTATUS status = BCryptGenerateKeyPair( | ||
hAlgorithm, | ||
out SafeBCryptKeyHandle hKey, | ||
keyLength, | ||
0); | ||
|
||
if (status != NTSTATUS.STATUS_SUCCESS) | ||
{ | ||
hKey.Dispose(); | ||
throw CreateCryptographicException(status); | ||
} | ||
|
||
return hKey; | ||
} | ||
} | ||
} |
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.