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

Commit f7dcabb

Browse files
committed
Remove SetLastError = true from BCrypt methods
They don't use it. They use their `NTSTATUS` result code instead.
1 parent 4a25649 commit f7dcabb

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/BCrypt/BCrypt.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static partial class BCrypt
2323
/// </param>
2424
/// <param name="dwFlags">A set of flags that modify the behavior of this function.</param>
2525
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
26-
[DllImport(nameof(BCrypt), SetLastError = true)]
26+
[DllImport(nameof(BCrypt))]
2727
public static extern unsafe NTSTATUS BCryptEnumAlgorithms(
2828
AlgorithmOperations dwAlgOperations,
2929
out int pAlgCount,
@@ -63,7 +63,7 @@ public static extern unsafe NTSTATUS BCryptEnumAlgorithms(
6363
/// <returns>
6464
/// Returns a status code that indicates the success or failure of the function.
6565
/// </returns>
66-
[DllImport(nameof(BCrypt), SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true)]
66+
[DllImport(nameof(BCrypt), CharSet = CharSet.Unicode, ExactSpelling = true)]
6767
public static extern NTSTATUS BCryptOpenAlgorithmProvider(
6868
out SafeAlgorithmHandle phAlgorithm,
6969
string pszAlgId,
@@ -98,7 +98,7 @@ public static extern NTSTATUS BCryptOpenAlgorithmProvider(
9898
/// </param>
9999
/// <param name="dwFlags">Flags that modify the behavior of the function.</param>
100100
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
101-
[DllImport(nameof(BCrypt), SetLastError = true)]
101+
[DllImport(nameof(BCrypt))]
102102
public static extern unsafe NTSTATUS BCryptCreateHash(
103103
SafeAlgorithmHandle hAlgorithm,
104104
out SafeHashHandle phHash,
@@ -145,7 +145,7 @@ public static extern unsafe NTSTATUS BCryptCreateHash(
145145
/// Multi-hash objects are always reusable and always behave as if the <see cref="BCryptCreateHashFlags.BCRYPT_HASH_REUSABLE_FLAG"/> was passed. This flag is supported here for consistency.
146146
/// </param>
147147
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
148-
[DllImport(nameof(BCrypt), SetLastError = true)]
148+
[DllImport(nameof(BCrypt))]
149149
public static extern unsafe NTSTATUS BCryptCreateMultiHash(
150150
SafeAlgorithmHandle hAlgorithm,
151151
out SafeHashHandle phHash,
@@ -195,7 +195,7 @@ public static extern unsafe NTSTATUS BCryptCreateMultiHash(
195195
/// <remarks>
196196
/// The <paramref name="pbInput"/> and <paramref name="pbOutput"/> parameters can point to the same buffer. In this case, this function will perform the encryption in place. It is possible that the encrypted data size will be larger than the unencrypted data size, so the buffer must be large enough to hold the encrypted data.
197197
/// </remarks>
198-
[DllImport(nameof(BCrypt), SetLastError = true)]
198+
[DllImport(nameof(BCrypt))]
199199
public static unsafe extern NTSTATUS BCryptEncrypt(
200200
SafeKeyHandle hKey,
201201
byte* pbInput,
@@ -246,7 +246,7 @@ public static unsafe extern NTSTATUS BCryptEncrypt(
246246
/// 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="hKey"/> parameter.
247247
/// </param>
248248
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
249-
[DllImport(nameof(BCrypt), SetLastError = true)]
249+
[DllImport(nameof(BCrypt))]
250250
public static unsafe extern NTSTATUS BCryptDecrypt(
251251
SafeKeyHandle hKey,
252252
byte* pbInput,
@@ -275,7 +275,7 @@ public static unsafe extern NTSTATUS BCryptDecrypt(
275275
/// To combine more than one buffer into the hash or MAC, you can call this function multiple times, passing a different buffer each time. To obtain the hash or MAC value, call the <see cref="BCryptFinishHash(SafeHashHandle, byte*, int, BCryptFinishHashFlags)"/> function.
276276
/// After the <see cref="BCryptFinishHash(SafeHashHandle, byte*, int, BCryptFinishHashFlags)"/> function has been called for a specified handle, that handle cannot be reused.
277277
/// </remarks>
278-
[DllImport(nameof(BCrypt), SetLastError = true)]
278+
[DllImport(nameof(BCrypt))]
279279
public static extern unsafe NTSTATUS BCryptHashData(
280280
SafeHashHandle hHash,
281281
byte* pbInput,
@@ -305,7 +305,7 @@ public static extern unsafe NTSTATUS BCryptHashData(
305305
/// Each element of the pOperations array contains instructions for a particular computation to be performed on a single element of the multi-object state. The functional behavior of BCryptProcessMultiOperations is equivalent to performing, for each element in the multi-object state, the computations specified in the operations array for that element, one at a time, in order.
306306
/// The relative order of two operations that operate on different elements of the array is not guaranteed.If an output buffer overlaps an input or output buffer the result is not deterministic.
307307
/// </remarks>
308-
[DllImport(nameof(BCrypt), SetLastError = true)]
308+
[DllImport(nameof(BCrypt))]
309309
public static extern unsafe NTSTATUS BCryptProcessMultiOperations(
310310
SafeHashHandle hHash,
311311
BCRYPT_MULTI_OPERATION_TYPE operationType,
@@ -328,7 +328,7 @@ public static extern unsafe NTSTATUS BCryptProcessMultiOperations(
328328
/// </param>
329329
/// <param name="dwFlags">A set of flags that modify the behavior of this function.</param>
330330
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
331-
[DllImport(nameof(BCrypt), SetLastError = true)]
331+
[DllImport(nameof(BCrypt))]
332332
public static extern unsafe NTSTATUS BCryptFinishHash(
333333
SafeHashHandle hHash,
334334
byte* pbOutput,
@@ -366,7 +366,7 @@ public static extern unsafe NTSTATUS BCryptFinishHash(
366366
/// <remarks>
367367
/// To later verify that the signature is valid, call the <see cref="BCryptVerifySignature(SafeKeyHandle, void*, byte*, int, byte*, int, BCryptSignHashFlags)"/> function with an identical key and an identical hash of the original data.
368368
/// </remarks>
369-
[DllImport(nameof(BCrypt), SetLastError = true)]
369+
[DllImport(nameof(BCrypt))]
370370
public static unsafe extern NTSTATUS BCryptSignHash(
371371
SafeKeyHandle hKey,
372372
void* pPaddingInfo,
@@ -407,7 +407,7 @@ public static unsafe extern NTSTATUS BCryptSignHash(
407407
/// Returns a status code that indicates the success or failure of the function.
408408
/// In particular, an invalid signature will produce a <see cref="NTSTATUS.Code.STATUS_INVALID_SIGNATURE"/> result.
409409
/// </returns>
410-
[DllImport(nameof(BCrypt), SetLastError = true)]
410+
[DllImport(nameof(BCrypt))]
411411
public static unsafe extern NTSTATUS BCryptVerifySignature(
412412
SafeKeyHandle hKey,
413413
void* pPaddingInfo,
@@ -430,7 +430,7 @@ public static unsafe extern NTSTATUS BCryptVerifySignature(
430430
/// function to set its properties; however, the key cannot be used until the
431431
/// BCryptFinalizeKeyPair function is called.
432432
/// </remarks>
433-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true)]
433+
[DllImport(nameof(BCrypt), ExactSpelling = true)]
434434
public static extern NTSTATUS BCryptGenerateKeyPair(
435435
SafeAlgorithmHandle hAlgorithm,
436436
out SafeKeyHandle phKey,
@@ -464,7 +464,7 @@ public static extern NTSTATUS BCryptGenerateKeyPair(
464464
/// </param>
465465
/// <param name="flags">A set of flags that modify the behavior of this function. No flags are currently defined, so this parameter should be zero.</param>
466466
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
467-
[DllImport(nameof(BCrypt), SetLastError = true)]
467+
[DllImport(nameof(BCrypt))]
468468
public static extern unsafe NTSTATUS BCryptGenerateSymmetricKey(
469469
SafeAlgorithmHandle hAlgorithm,
470470
out SafeKeyHandle phKey,
@@ -485,7 +485,7 @@ public static extern unsafe NTSTATUS BCryptGenerateSymmetricKey(
485485
/// After this function has been called, the BCryptSetProperty function
486486
/// can no longer be used for this key.
487487
/// </remarks>
488-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true)]
488+
[DllImport(nameof(BCrypt), ExactSpelling = true)]
489489
public static extern NTSTATUS BCryptFinalizeKeyPair(
490490
SafeKeyHandle hKey,
491491
BCryptFinalizeKeyPairFlags dwFlags = BCryptFinalizeKeyPairFlags.None);
@@ -526,7 +526,7 @@ public static extern NTSTATUS BCryptFinalizeKeyPair(
526526
/// </param>
527527
/// <param name="dwFlags">A set of flags that modify the behavior of this function.</param>
528528
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
529-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
529+
[DllImport(nameof(BCrypt), ExactSpelling = true, CharSet = CharSet.Unicode)]
530530
public static extern unsafe NTSTATUS BCryptImportKey(
531531
SafeAlgorithmHandle hAlgorithm,
532532
SafeKeyHandle hImportKey,
@@ -549,7 +549,7 @@ public static extern unsafe NTSTATUS BCryptImportKey(
549549
/// <param name="cbInput">The size, in bytes, of the <paramref name="pbInput"/> buffer.</param>
550550
/// <param name="dwFlags">A set of flags that modify the behavior of this function. This can be zero or the following value: BCRYPT_NO_KEY_VALIDATION</param>
551551
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
552-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
552+
[DllImport(nameof(BCrypt), ExactSpelling = true, CharSet = CharSet.Unicode)]
553553
public static extern unsafe NTSTATUS BCryptImportKeyPair(
554554
SafeAlgorithmHandle hAlgorithm,
555555
SafeKeyHandle hImportKey,
@@ -586,7 +586,7 @@ public static extern unsafe NTSTATUS BCryptImportKeyPair(
586586
/// </param>
587587
/// <param name="dwFlags">A set of flags that modify the behavior of this function. </param>
588588
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
589-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
589+
[DllImport(nameof(BCrypt), ExactSpelling = true, CharSet = CharSet.Unicode)]
590590
public static extern unsafe NTSTATUS BCryptExportKey(
591591
SafeKeyHandle hKey,
592592
SafeKeyHandle hExportKey,
@@ -614,7 +614,7 @@ public static extern unsafe NTSTATUS BCryptExportKey(
614614
/// </param>
615615
/// <param name="flags">A set of flags that modify the behavior of this function. No flags are defined for this function.</param>
616616
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
617-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true)]
617+
[DllImport(nameof(BCrypt), ExactSpelling = true)]
618618
public static extern NTSTATUS BCryptSecretAgreement(
619619
SafeKeyHandle privateKey,
620620
SafeKeyHandle publicKey,
@@ -656,7 +656,7 @@ public static extern NTSTATUS BCryptSecretAgreement(
656656
/// <returns>
657657
/// Returns a status code that indicates the success or failure of the function.
658658
/// </returns>
659-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
659+
[DllImport(nameof(BCrypt), ExactSpelling = true, CharSet = CharSet.Unicode)]
660660
public static extern unsafe NTSTATUS BCryptDeriveKey(
661661
SafeSecretHandle sharedSecret,
662662
string keyDerivationFunction,
@@ -677,7 +677,7 @@ public static extern unsafe NTSTATUS BCryptDeriveKey(
677677
/// <param name="cbInput">The size, in bytes, of the <paramref name="pbInput"/> buffer.</param>
678678
/// <param name="dwFlags">A set of flags that modify the behavior of this function. No flags are defined for this function.</param>
679679
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
680-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
680+
[DllImport(nameof(BCrypt), ExactSpelling = true, CharSet = CharSet.Unicode)]
681681
public static extern unsafe NTSTATUS BCryptSetProperty(
682682
SafeHandle hObject,
683683
string pszProperty,
@@ -696,7 +696,7 @@ public static extern unsafe NTSTATUS BCryptSetProperty(
696696
/// <param name="cbInput">The size, in bytes, of the <paramref name="pbInput"/> buffer.</param>
697697
/// <param name="dwFlags">A set of flags that modify the behavior of this function. No flags are defined for this function.</param>
698698
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
699-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
699+
[DllImport(nameof(BCrypt), ExactSpelling = true, CharSet = CharSet.Unicode)]
700700
public static extern NTSTATUS BCryptSetProperty(
701701
SafeHandle hObject,
702702
string pszProperty,
@@ -714,7 +714,7 @@ public static extern NTSTATUS BCryptSetProperty(
714714
/// <param name="resultSize">A pointer to a ULONG variable that receives the number of bytes that were copied to the pbOutput buffer. If the <paramref name="output"/> parameter is NULL, this function will place the required size, in bytes, in the location pointed to by this parameter.</param>
715715
/// <param name="flags">A set of flags that modify the behavior of this function. No flags are defined for this function.</param>
716716
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
717-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
717+
[DllImport(nameof(BCrypt), ExactSpelling = true, CharSet = CharSet.Unicode)]
718718
public static extern unsafe NTSTATUS BCryptGetProperty(
719719
SafeHandle hObject,
720720
string property,
@@ -737,7 +737,7 @@ public static extern unsafe NTSTATUS BCryptGetProperty(
737737
/// </param>
738738
/// <param name="flags">A set of flags that modify the behavior of this function. </param>
739739
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
740-
[DllImport(nameof(BCrypt), SetLastError = true)]
740+
[DllImport(nameof(BCrypt))]
741741
public static extern unsafe NTSTATUS BCryptGenRandom(
742742
SafeAlgorithmHandle hAlgorithm,
743743
byte* pbBuffer,
@@ -748,7 +748,7 @@ public static extern unsafe NTSTATUS BCryptGenRandom(
748748
/// The BCryptFreeBuffer function is used to free memory that was allocated by one of the CNG functions.
749749
/// </summary>
750750
/// <param name="pvBuffer">A pointer to the memory buffer to be freed.</param>
751-
[DllImport(nameof(BCrypt), SetLastError = true)]
751+
[DllImport(nameof(BCrypt))]
752752
public static extern unsafe void BCryptFreeBuffer(void* pvBuffer);
753753

754754
/// <summary>
@@ -757,7 +757,7 @@ public static extern unsafe NTSTATUS BCryptGenRandom(
757757
/// <param name="algorithmHandle">A handle that represents the algorithm provider to close. This handle is obtained by calling the BCryptOpenAlgorithmProvider function.</param>
758758
/// <param name="flags">A set of flags that modify the behavior of this function. No flags are defined for this function.</param>
759759
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
760-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true)]
760+
[DllImport(nameof(BCrypt), ExactSpelling = true)]
761761
private static extern NTSTATUS BCryptCloseAlgorithmProvider(
762762
IntPtr algorithmHandle,
763763
BCryptCloseAlgorithmProviderFlags flags = BCryptCloseAlgorithmProviderFlags.None);
@@ -767,15 +767,15 @@ private static extern NTSTATUS BCryptCloseAlgorithmProvider(
767767
/// </summary>
768768
/// <param name="hHash">The handle of the hash or MAC object to destroy. This handle is obtained by using the <see cref="BCryptCreateHash(SafeAlgorithmHandle, out SafeHashHandle, byte*, int, byte*, int, BCryptCreateHashFlags)"/> function.</param>
769769
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
770-
[DllImport(nameof(BCrypt), SetLastError = true)]
770+
[DllImport(nameof(BCrypt))]
771771
private static extern NTSTATUS BCryptDestroyHash(IntPtr hHash);
772772

773773
/// <summary>
774774
/// Destroys a key.
775775
/// </summary>
776776
/// <param name="hKey">The handle of the key to destroy.</param>
777777
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
778-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true)]
778+
[DllImport(nameof(BCrypt), ExactSpelling = true)]
779779
private static extern NTSTATUS BCryptDestroyKey(
780780
IntPtr hKey);
781781

@@ -784,7 +784,7 @@ private static extern NTSTATUS BCryptDestroyKey(
784784
/// </summary>
785785
/// <param name="hSecret">The handle of the secret to destroy.</param>
786786
/// <returns>Returns a status code that indicates the success or failure of the function.</returns>
787-
[DllImport(nameof(BCrypt), SetLastError = true, ExactSpelling = true)]
787+
[DllImport(nameof(BCrypt), ExactSpelling = true)]
788788
private static extern NTSTATUS BCryptDestroySecret(
789789
IntPtr hSecret);
790790
}

0 commit comments

Comments
 (0)