Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Generate Span<T> overloads where arrays would be
Browse files Browse the repository at this point in the history
Closes #444
  • Loading branch information
AArnott committed Jul 20, 2020
1 parent ad46bd5 commit 7e2f9f1
Show file tree
Hide file tree
Showing 21 changed files with 305 additions and 58 deletions.
4 changes: 4 additions & 0 deletions src/AdvApi32/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
static PInvoke.AdvApi32.CryptGetHashParam(PInvoke.AdvApi32.SafeHashHandle hHash, PInvoke.AdvApi32.CryptGetHashParamFlags dwParam, System.Span<byte> pbData, ref int pdwDataLen, uint dwFlags) -> bool
static PInvoke.AdvApi32.CryptHashData(PInvoke.AdvApi32.SafeHashHandle hHash, System.ReadOnlySpan<byte> pbData, PInvoke.AdvApi32.CryptHashDataFlags flags) -> bool
static PInvoke.AdvApi32.EnumServicesStatus(PInvoke.AdvApi32.SafeServiceHandle hSCManager, PInvoke.AdvApi32.ServiceType dwServiceType, PInvoke.AdvApi32.ServiceStateQuery dwServiceState, System.Span<byte> lpServices, ref int pcbBytesNeeded, ref int lpServicesReturned, ref int lpResumeHandle) -> bool
static PInvoke.AdvApi32.LookupPrivilegeValue(System.IntPtr lpSystemName, System.IntPtr lpName, System.IntPtr lpLuid) -> bool
static PInvoke.AdvApi32.LookupPrivilegeValue(System.ReadOnlySpan<char> lpSystemName, System.ReadOnlySpan<char> lpName, out PInvoke.User32.LUID lpLuid) -> bool
static PInvoke.AdvApi32.LookupPrivilegeValue(char[] lpSystemName, char[] lpName, out PInvoke.User32.LUID lpLuid) -> bool
static extern PInvoke.AdvApi32.LookupPrivilegeValue(char* lpSystemName, char* lpName, PInvoke.User32.LUID* lpLuid) -> bool
18 changes: 9 additions & 9 deletions src/BCrypt.Tests/BCryptFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public unsafe void EncryptDecrypt_Pointers()
Array.Copy(plainText, plainTextPadded, plainText.Length);
BCryptEncrypt(
key,
new ArraySegment<byte>(plainTextPadded),
plainTextPadded,
null,
null,
null,
Expand All @@ -192,10 +192,10 @@ public unsafe void EncryptDecrypt_Pointers()
cipherText = new byte[cipherTextLength];
BCryptEncrypt(
key,
new ArraySegment<byte>(plainTextPadded),
plainTextPadded,
null,
null,
new ArraySegment<byte>(cipherText),
cipherText,
out cipherTextLength,
BCryptEncryptFlags.None).ThrowOnError();

Expand All @@ -210,10 +210,10 @@ public unsafe void EncryptDecrypt_Pointers()
int cbDecrypted;
BCryptDecrypt(
key,
new ArraySegment<byte>(cipherText),
cipherText,
null,
null,
new ArraySegment<byte>(decryptedText),
decryptedText,
out cbDecrypted,
BCryptEncryptFlags.None).ThrowOnError();

Expand All @@ -234,10 +234,10 @@ public unsafe void EncryptDecrypt_PointerCornerCases()
int length;
BCryptEncrypt(
key,
new ArraySegment<byte>(new byte[0]),
new byte[0],
null,
default(ArraySegment<byte>),
new ArraySegment<byte>(new byte[1]),
new byte[1],
out length,
BCryptEncryptFlags.None);
}
Expand All @@ -247,10 +247,10 @@ public unsafe void EncryptDecrypt_PointerCornerCases()
int length;
BCryptEncrypt(
key,
new ArraySegment<byte>(new byte[0]),
new byte[0],
null,
null,
new ArraySegment<byte>(new byte[1]),
new byte[1],
out length,
BCryptEncryptFlags.None);
}
Expand Down
12 changes: 7 additions & 5 deletions src/BCrypt/BCrypt.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,20 +307,20 @@ public static ArraySegment<byte> BCryptExportKey(SafeKeyHandle key, SafeKeyHandl
int length;
BCryptEncrypt(
hKey,
ArraySegmentFor(pbInput),
pbInput,
pPaddingInfo,
ArraySegmentFor(pbIV),
pbIV,
null,
out length,
dwFlags).ThrowOnError();

byte[] cipherText = new byte[length];
BCryptEncrypt(
hKey,
ArraySegmentFor(pbInput),
pbInput,
pPaddingInfo,
ArraySegmentFor(pbIV),
ArraySegmentFor(cipherText),
pbIV,
cipherText,
out length,
dwFlags).ThrowOnError();

Expand Down Expand Up @@ -356,6 +356,7 @@ public static ArraySegment<byte> BCryptExportKey(SafeKeyHandle key, SafeKeyHandl
/// A set of flags that modify the behavior of this function. The allowed set of flags depends on the type of key specified by the hKey parameter.
/// </param>
/// <returns>The encrypted ciphertext.</returns>
[Obsolete("Use the overload that accepts spans instead.")]
public static unsafe NTSTATUS BCryptEncrypt(
SafeKeyHandle key,
ArraySegment<byte>? input,
Expand Down Expand Up @@ -486,6 +487,7 @@ public static ArraySegment<byte> BCryptExportKey(SafeKeyHandle key, SafeKeyHandl
/// A set of flags that modify the behavior of this function. The allowed set of flags depends on the type of key specified by the <paramref name="key"/> parameter.
/// </param>
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
[Obsolete("Use the overload that accepts spans instead.")]
public static unsafe NTSTATUS BCryptDecrypt(
SafeKeyHandle key,
ArraySegment<byte>? input,
Expand Down
22 changes: 22 additions & 0 deletions src/BCrypt/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
PInvoke.BCrypt.BCryptKeyDerivationFlags
PInvoke.BCrypt.BCryptKeyDerivationFlags.BCRYPT_CAPI_AES_FLAG = 16 -> PInvoke.BCrypt.BCryptKeyDerivationFlags
PInvoke.BCrypt.BCryptKeyDerivationFlags.None = 0 -> PInvoke.BCrypt.BCryptKeyDerivationFlags
static PInvoke.BCrypt.BCryptCreateHash(PInvoke.BCrypt.SafeAlgorithmHandle hAlgorithm, out PInvoke.BCrypt.SafeHashHandle phHash, System.Span<byte> pbHashObject, System.ReadOnlySpan<byte> pbSecret, PInvoke.BCrypt.BCryptCreateHashFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptCreateMultiHash(PInvoke.BCrypt.SafeAlgorithmHandle hAlgorithm, out PInvoke.BCrypt.SafeHashHandle phHash, int nHashes, System.Span<byte> pbHashObject, System.ReadOnlySpan<byte> pbSecret, PInvoke.BCrypt.BCryptCreateHashFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptDecrypt(PInvoke.BCrypt.SafeKeyHandle hKey, System.ReadOnlySpan<byte> pbInput, System.IntPtr pPaddingInfo, System.ReadOnlySpan<byte> pbIV, System.Span<byte> pbOutput, out int pcbResult, PInvoke.BCrypt.BCryptEncryptFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptDecrypt(PInvoke.BCrypt.SafeKeyHandle hKey, System.ReadOnlySpan<byte> pbInput, void* pPaddingInfo, System.ReadOnlySpan<byte> pbIV, System.Span<byte> pbOutput, out int pcbResult, PInvoke.BCrypt.BCryptEncryptFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptDeriveKey(PInvoke.BCrypt.SafeSecretHandle sharedSecret, string keyDerivationFunction, PInvoke.BCrypt.BCryptBufferDesc? kdfParameters, System.Span<byte> derivedKey, out int resultSize, PInvoke.BCrypt.BCryptDeriveKeyFlags flags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptEncrypt(PInvoke.BCrypt.SafeKeyHandle hKey, System.ReadOnlySpan<byte> pbInput, System.IntPtr pPaddingInfo, System.ReadOnlySpan<byte> pbIV, System.Span<byte> pbOutput, out int pcbResult, PInvoke.BCrypt.BCryptEncryptFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptEncrypt(PInvoke.BCrypt.SafeKeyHandle hKey, System.ReadOnlySpan<byte> pbInput, void* pPaddingInfo, System.ReadOnlySpan<byte> pbIV, System.Span<byte> pbOutput, out int pcbResult, PInvoke.BCrypt.BCryptEncryptFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptExportKey(PInvoke.BCrypt.SafeKeyHandle hKey, PInvoke.BCrypt.SafeKeyHandle hExportKey, string pszBlobType, System.Span<byte> pbOutput, out int pcbResult, PInvoke.BCrypt.BCryptExportKeyFlags dwFlags = PInvoke.BCrypt.BCryptExportKeyFlags.None) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptFinishHash(PInvoke.BCrypt.SafeHashHandle hHash, System.Span<byte> pbOutput, PInvoke.BCrypt.BCryptFinishHashFlags dwFlags = PInvoke.BCrypt.BCryptFinishHashFlags.None) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptGenRandom(PInvoke.BCrypt.SafeAlgorithmHandle hAlgorithm, System.Span<byte> pbBuffer, PInvoke.BCrypt.BCryptGenRandomFlags flags = PInvoke.BCrypt.BCryptGenRandomFlags.None) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptGenerateSymmetricKey(PInvoke.BCrypt.SafeAlgorithmHandle hAlgorithm, out PInvoke.BCrypt.SafeKeyHandle phKey, System.Span<byte> pbKeyObject, System.ReadOnlySpan<byte> pbSecret, PInvoke.BCrypt.BCryptGenerateSymmetricKeyFlags flags = PInvoke.BCrypt.BCryptGenerateSymmetricKeyFlags.None) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptGetProperty(System.Runtime.InteropServices.SafeHandle hObject, string property, System.Span<byte> output, out int resultSize, PInvoke.BCrypt.BCryptGetPropertyFlags flags = PInvoke.BCrypt.BCryptGetPropertyFlags.None) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptHashData(PInvoke.BCrypt.SafeHashHandle hHash, System.ReadOnlySpan<byte> pbInput, PInvoke.BCrypt.BCryptHashDataFlags dwFlags = PInvoke.BCrypt.BCryptHashDataFlags.None) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptImportKey(PInvoke.BCrypt.SafeAlgorithmHandle hAlgorithm, PInvoke.BCrypt.SafeKeyHandle hImportKey, string pszBlobType, out PInvoke.BCrypt.SafeKeyHandle phKey, System.Span<byte> pbKeyObject, System.ReadOnlySpan<byte> pbInput, PInvoke.BCrypt.BCryptImportKeyFlags dwFlags = PInvoke.BCrypt.BCryptImportKeyFlags.None) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptImportKeyPair(PInvoke.BCrypt.SafeAlgorithmHandle hAlgorithm, PInvoke.BCrypt.SafeKeyHandle hImportKey, string pszBlobType, out PInvoke.BCrypt.SafeKeyHandle phKey, System.ReadOnlySpan<byte> pbInput, PInvoke.BCrypt.BCryptImportKeyPairFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptKeyDerivation(PInvoke.BCrypt.SafeKeyHandle hKey, PInvoke.BCrypt.BCryptBufferDesc? pParameterList, System.Span<byte> pbDerivedKey, int cbDerivedKey, out int pcbResult, PInvoke.BCrypt.BCryptKeyDerivationFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptKeyDerivation(PInvoke.BCrypt.SafeKeyHandle hKey, PInvoke.BCrypt.BCryptBufferDesc? pParameterList, byte[] pbDerivedKey, int cbDerivedKey, out int pcbResult, PInvoke.BCrypt.BCryptKeyDerivationFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptKeyDerivation(PInvoke.BCrypt.SafeKeyHandle hKey, System.IntPtr pParameterList, System.IntPtr pbDerivedKey, int cbDerivedKey, out int pcbResult, PInvoke.BCrypt.BCryptKeyDerivationFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptProcessMultiOperations(PInvoke.BCrypt.SafeHashHandle hHash, PInvoke.BCrypt.BCRYPT_MULTI_OPERATION_TYPE operationType, System.ReadOnlySpan<PInvoke.BCrypt.BCRYPT_MULTI_HASH_OPERATION> pOperations, int cbOperations, int dwFlags = 0) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptSetProperty(System.Runtime.InteropServices.SafeHandle hObject, string pszProperty, System.ReadOnlySpan<byte> pbInput, PInvoke.BCrypt.BCryptSetPropertyFlags dwFlags = PInvoke.BCrypt.BCryptSetPropertyFlags.None) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptSignHash(PInvoke.BCrypt.SafeKeyHandle hKey, System.IntPtr pPaddingInfo, System.ReadOnlySpan<byte> pbInput, System.Span<byte> pbOutput, out int pcbResult, PInvoke.BCrypt.BCryptSignHashFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptSignHash(PInvoke.BCrypt.SafeKeyHandle hKey, void* pPaddingInfo, System.ReadOnlySpan<byte> pbInput, System.Span<byte> pbOutput, out int pcbResult, PInvoke.BCrypt.BCryptSignHashFlags dwFlags) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptVerifySignature(PInvoke.BCrypt.SafeKeyHandle hKey, System.IntPtr pPaddingInfo, System.ReadOnlySpan<byte> pbHash, System.ReadOnlySpan<byte> pbSignature, PInvoke.BCrypt.BCryptSignHashFlags dwFlags = PInvoke.BCrypt.BCryptSignHashFlags.None) -> PInvoke.NTSTATUS
static PInvoke.BCrypt.BCryptVerifySignature(PInvoke.BCrypt.SafeKeyHandle hKey, void* pPaddingInfo, System.ReadOnlySpan<byte> pbHash, System.ReadOnlySpan<byte> pbSignature, PInvoke.BCrypt.BCryptSignHashFlags dwFlags = PInvoke.BCrypt.BCryptSignHashFlags.None) -> PInvoke.NTSTATUS
static extern PInvoke.BCrypt.BCryptKeyDerivation(PInvoke.BCrypt.SafeKeyHandle hKey, PInvoke.BCrypt.BCryptBufferDesc* pParameterList, byte* pbDerivedKey, int cbDerivedKey, out int pcbResult, PInvoke.BCrypt.BCryptKeyDerivationFlags dwFlags) -> PInvoke.NTSTATUS
6 changes: 2 additions & 4 deletions src/CodeGeneration/CodeGeneration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\CodeGenerationAttributes\FriendlyFlags.cs">
<Link>FriendlyFlags.cs</Link>
</Compile>
<PackageReference Include="CodeGeneration.Roslyn" Version="$(CodeGenerationPackageVersion)" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CodeGeneration.Roslyn" Version="$(CodeGenerationPackageVersion)" PrivateAssets="all" />
<ProjectReference Include="..\CodeGenerationAttributes\CodeGenerationAttributes.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 7e2f9f1

Please sign in to comment.