Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Interop.cs
Go to the documentation of this file.
1using System;
8
9internal static class Interop
10{
11 internal static class BCrypt
12 {
13 private struct BCRYPT_KEY_DATA_BLOB_HEADER
14 {
15 public uint dwMagic;
16
17 public uint dwVersion;
18
19 public uint cbKeyData;
20 }
21
22 internal enum NTSTATUS : uint
23 {
24 STATUS_SUCCESS = 0u,
25 STATUS_NOT_FOUND = 3221226021u,
26 STATUS_INVALID_PARAMETER = 3221225485u,
27 STATUS_NO_MEMORY = 3221225495u,
28 STATUS_AUTH_TAG_MISMATCH = 3221266434u
29 }
30
31 internal struct BCRYPT_OAEP_PADDING_INFO
32 {
33 internal IntPtr pszAlgId;
34
35 internal IntPtr pbLabel;
36
37 internal int cbLabel;
38 }
39
40 internal struct BCRYPT_PKCS1_PADDING_INFO
41 {
42 internal IntPtr pszAlgId;
43 }
44
45 internal struct BCRYPT_PSS_PADDING_INFO
46 {
47 internal IntPtr pszAlgId;
48
49 internal int cbSalt;
50 }
51
79
80 internal struct BCRYPT_RSAKEY_BLOB
81 {
82 internal KeyBlobMagicNumber Magic;
83
84 internal int BitLength;
85
86 internal int cbPublicExp;
87
88 internal int cbModulus;
89
90 internal int cbPrime1;
91
92 internal int cbPrime2;
93 }
94
95 internal struct BCRYPT_DSA_KEY_BLOB
96 {
97 internal KeyBlobMagicNumber Magic;
98
99 internal int cbKey;
100
101 internal unsafe fixed byte Count[4];
102
103 internal unsafe fixed byte Seed[20];
104
105 internal unsafe fixed byte q[20];
106 }
107
108 internal struct BCRYPT_DSA_KEY_BLOB_V2
109 {
110 internal KeyBlobMagicNumber Magic;
111
112 internal int cbKey;
113
114 internal HASHALGORITHM_ENUM hashAlgorithm;
115
116 internal DSAFIPSVERSION_ENUM standardVersion;
117
118 internal int cbSeedLength;
119
120 internal int cbGroupSize;
121
122 internal unsafe fixed byte Count[4];
123 }
124
131
133 {
136 }
137
138 internal struct BCRYPT_ECCKEY_BLOB
139 {
140 internal KeyBlobMagicNumber Magic;
141
142 internal int cbKey;
143 }
144
151
156
157 internal struct BCRYPT_ECCFULLKEY_BLOB
158 {
159 internal KeyBlobMagicNumber Magic;
160
161 internal int Version;
162
163 internal ECC_CURVE_TYPE_ENUM CurveType;
164
165 internal ECC_CURVE_ALG_ID_ENUM CurveGenerationAlgId;
166
167 internal int cbFieldLength;
168
169 internal int cbSubgroupOrder;
170
171 internal int cbCofactor;
172
173 internal int cbSeed;
174 }
175
177 {
181 KDF_HMAC_KEY = 3,
186 KDF_ALGORITHMID = 8,
187 KDF_PARTYUINFO = 9,
188 KDF_PARTYVINFO = 10,
189 KDF_SUPPPUBINFO = 11,
190 KDF_SUPPPRIVINFO = 12,
191 KDF_LABEL = 13,
192 KDF_CONTEXT = 14,
193 KDF_SALT = 15,
196 }
197
198 internal struct BCryptBuffer
199 {
200 internal int cbBuffer;
201
202 internal CngBufferDescriptors BufferType;
203
204 internal IntPtr pvBuffer;
205 }
206
207 internal struct BCryptBufferDesc
208 {
209 internal int ulVersion;
210
211 internal int cBuffers;
212
213 internal IntPtr pBuffers;
214 }
215
216 internal struct BCRYPT_ECC_PARAMETER_HEADER
217 {
218 internal int Version;
219
220 internal ECC_CURVE_TYPE_ENUM CurveType;
221
222 internal ECC_CURVE_ALG_ID_ENUM CurveGenerationAlgId;
223
224 internal int cbFieldLength;
225
226 internal int cbSubgroupOrder;
227
228 internal int cbCofactor;
229
230 internal int cbSeed;
231 }
232
233 [Flags]
235 {
236 None = 0,
238 }
239
240 [Flags]
242 {
243 None = 0,
245 }
246
247 internal static class BCryptAlgorithmCache
248 {
249 private struct Entry
250 {
251 public string HashAlgorithmId { get; private set; }
252
253 public BCryptOpenAlgorithmProviderFlags Flags { get; private set; }
254
256
257 public int HashSizeInBytes { get; private set; }
258
260 {
261 this = default(Entry);
262 HashAlgorithmId = hashAlgorithmId;
263 Flags = flags;
264 Handle = handle;
265 Unsafe.SkipInit(out int hashSizeInBytes);
266 int pcbResult;
267 NTSTATUS nTSTATUS = BCryptGetProperty(handle, "HashDigestLength", &hashSizeInBytes, 4, out pcbResult, 0);
268 if (nTSTATUS != 0)
269 {
270 throw CreateCryptographicException(nTSTATUS);
271 }
272 HashSizeInBytes = hashSizeInBytes;
273 }
274 }
275
276 private static volatile Entry[] _cache = Array.Empty<Entry>();
277
279 {
280 Entry[] array = _cache;
281 Entry[] array2 = array;
282 for (int i = 0; i < array2.Length; i++)
283 {
284 Entry entry = array2[i];
285 if (entry.HashAlgorithmId == hashAlgorithmId && entry.Flags == flags)
286 {
287 hashSizeInBytes = entry.HashSizeInBytes;
288 return entry.Handle;
289 }
290 }
292 NTSTATUS nTSTATUS = BCryptOpenAlgorithmProvider(out phAlgorithm, hashAlgorithmId, null, flags);
293 if (nTSTATUS != 0)
294 {
295 throw CreateCryptographicException(nTSTATUS);
296 }
297 Array.Resize(ref array, array.Length + 1);
298 Entry entry2 = new Entry(hashAlgorithmId, flags, phAlgorithm);
299 array[^1] = new Entry(hashAlgorithmId, flags, phAlgorithm);
300 _cache = array;
301 hashSizeInBytes = entry2.HashSizeInBytes;
302 return entry2.Handle;
303 }
304 }
305
307 {
308 int length = key.Length;
309 int num = sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + length;
310 byte[] array = new byte[num];
311 fixed (byte* ptr = array)
312 {
314 ptr2->dwMagic = 1296188491u;
315 ptr2->dwVersion = 1u;
316 ptr2->cbKeyData = (uint)length;
317 }
318 key.CopyTo(array.AsSpan(sizeof(BCRYPT_KEY_DATA_BLOB_HEADER)));
320 NTSTATUS nTSTATUS = BCryptImportKey(hAlg, IntPtr.Zero, "KeyDataBlob", out hKey, IntPtr.Zero, 0, array, num, 0);
321 if (nTSTATUS != 0)
322 {
323 throw CreateCryptographicException(nTSTATUS);
324 }
325 return hKey;
326 }
327
328 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
329 private static extern NTSTATUS BCryptImportKey(Internal.NativeCrypto.SafeAlgorithmHandle hAlgorithm, IntPtr hImportKey, string pszBlobType, out Internal.NativeCrypto.SafeKeyHandle hKey, IntPtr pbKeyObject, int cbKeyObject, byte[] pbInput, int cbInput, int dwFlags);
330
331 internal unsafe static int BCryptEncrypt(Internal.NativeCrypto.SafeKeyHandle hKey, ReadOnlySpan<byte> input, byte[] iv, Span<byte> output)
332 {
333 fixed (byte* pbInput = input)
334 {
335 fixed (byte* pbOutput = output)
336 {
337 int cbResult;
338 NTSTATUS nTSTATUS = BCryptEncrypt(hKey, pbInput, input.Length, IntPtr.Zero, iv, (iv != null) ? iv.Length : 0, pbOutput, output.Length, out cbResult, 0);
339 if (nTSTATUS != 0)
340 {
341 throw CreateCryptographicException(nTSTATUS);
342 }
343 return cbResult;
344 }
345 }
346 }
347
348 internal unsafe static int BCryptDecrypt(Internal.NativeCrypto.SafeKeyHandle hKey, ReadOnlySpan<byte> input, byte[] iv, Span<byte> output)
349 {
350 fixed (byte* pbInput = input)
351 {
352 fixed (byte* pbOutput = output)
353 {
354 int cbResult;
355 NTSTATUS nTSTATUS = BCryptDecrypt(hKey, pbInput, input.Length, IntPtr.Zero, iv, (iv != null) ? iv.Length : 0, pbOutput, output.Length, out cbResult, 0);
356 if (nTSTATUS != 0)
357 {
358 throw CreateCryptographicException(nTSTATUS);
359 }
360 return cbResult;
361 }
362 }
363 }
364
365 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
366 public unsafe static extern NTSTATUS BCryptEncrypt(Internal.NativeCrypto.SafeKeyHandle hKey, byte* pbInput, int cbInput, IntPtr paddingInfo, [In][Out] byte[] pbIV, int cbIV, byte* pbOutput, int cbOutput, out int cbResult, int dwFlags);
367
368 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
369 public unsafe static extern NTSTATUS BCryptDecrypt(Internal.NativeCrypto.SafeKeyHandle hKey, byte* pbInput, int cbInput, IntPtr paddingInfo, [In][Out] byte[] pbIV, int cbIV, byte* pbOutput, int cbOutput, out int cbResult, int dwFlags);
370
371 internal static void Emit(byte[] blob, ref int offset, byte[] value)
372 {
373 Buffer.BlockCopy(value, 0, blob, offset, value.Length);
374 offset += value.Length;
375 }
376
377 internal static void EmitByte(byte[] blob, ref int offset, byte value, int count = 1)
378 {
379 int num = offset + count;
380 for (int i = offset; i < num; i++)
381 {
382 blob[i] = value;
383 }
384 offset = num;
385 }
386
387 internal static void EmitBigEndian(byte[] blob, ref int offset, int value)
388 {
389 blob[offset++] = (byte)(value >> 24);
390 blob[offset++] = (byte)(value >> 16);
391 blob[offset++] = (byte)(value >> 8);
392 blob[offset++] = (byte)value;
393 }
394
395 internal static byte[] Consume(byte[] blob, ref int offset, int count)
396 {
397 byte[] array = new byte[count];
398 Buffer.BlockCopy(blob, offset, array, 0, count);
399 offset += count;
400 return array;
401 }
402
404 {
405 int hr = (int)(ntStatus | (NTSTATUS)16777216u);
406 return hr.ToCryptographicException();
407 }
408
409 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
410 internal static extern NTSTATUS BCryptOpenAlgorithmProvider(out Microsoft.Win32.SafeHandles.SafeBCryptAlgorithmHandle phAlgorithm, string pszAlgId, string pszImplementation, BCryptOpenAlgorithmProviderFlags dwFlags);
411
412 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
413 internal static extern NTSTATUS BCryptCloseAlgorithmProvider(IntPtr hAlgorithm, int dwFlags);
414
415 internal static NTSTATUS BCryptCreateHash(Microsoft.Win32.SafeHandles.SafeBCryptAlgorithmHandle hAlgorithm, out Microsoft.Win32.SafeHandles.SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ReadOnlySpan<byte> secret, int cbSecret, BCryptCreateHashFlags dwFlags)
416 {
417 return BCryptCreateHash(hAlgorithm, out phHash, pbHashObject, cbHashObject, ref MemoryMarshal.GetReference(secret), cbSecret, dwFlags);
418 }
419
420 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
421 private static extern NTSTATUS BCryptCreateHash(Microsoft.Win32.SafeHandles.SafeBCryptAlgorithmHandle hAlgorithm, out Microsoft.Win32.SafeHandles.SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ref byte pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags);
422
423 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
424 internal static extern NTSTATUS BCryptDestroyHash(IntPtr hHash);
425
426 internal static NTSTATUS BCryptHashData(Microsoft.Win32.SafeHandles.SafeBCryptHashHandle hHash, ReadOnlySpan<byte> pbInput, int cbInput, int dwFlags)
427 {
428 return BCryptHashData(hHash, ref MemoryMarshal.GetReference(pbInput), cbInput, dwFlags);
429 }
430
431 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
432 private static extern NTSTATUS BCryptHashData(Microsoft.Win32.SafeHandles.SafeBCryptHashHandle hHash, ref byte pbInput, int cbInput, int dwFlags);
433
434 internal static NTSTATUS BCryptFinishHash(Microsoft.Win32.SafeHandles.SafeBCryptHashHandle hHash, Span<byte> pbOutput, int cbOutput, int dwFlags)
435 {
436 return BCryptFinishHash(hHash, ref MemoryMarshal.GetReference(pbOutput), cbOutput, dwFlags);
437 }
438
439 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
440 private static extern NTSTATUS BCryptFinishHash(Microsoft.Win32.SafeHandles.SafeBCryptHashHandle hHash, ref byte pbOutput, int cbOutput, int dwFlags);
441
442 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
443 internal unsafe static extern NTSTATUS BCryptGetProperty(Microsoft.Win32.SafeHandles.SafeBCryptHandle hObject, string pszProperty, void* pbOutput, int cbOutput, out int pcbResult, int dwFlags);
444 }
445
446 internal static class Crypt32
447 {
448 internal struct CRYPT_OID_INFO
449 {
450 public int cbSize;
451
452 public IntPtr pszOID;
453
454 public IntPtr pwszName;
455
456 public OidGroup dwGroupId;
457
458 public int AlgId;
459
460 public int cbData;
461
462 public IntPtr pbData;
463
464 public string Name => Marshal.PtrToStringUni(pwszName);
465 }
466
476
477 internal static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
478 {
479 IntPtr intPtr = IntPtr.Zero;
480 try
481 {
482 intPtr = keyType switch
483 {
484 CryptOidInfoKeyType.CRYPT_OID_INFO_OID_KEY => Marshal.StringToCoTaskMemAnsi(key),
485 CryptOidInfoKeyType.CRYPT_OID_INFO_NAME_KEY => Marshal.StringToCoTaskMemUni(key),
486 _ => throw new NotSupportedException(),
487 };
489 {
490 OidGroup group2 = group | (OidGroup)(-2147483648);
491 IntPtr intPtr2 = CryptFindOIDInfo(keyType, intPtr, group2);
492 if (intPtr2 != IntPtr.Zero)
493 {
494 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr2);
495 }
496 }
497 IntPtr intPtr3 = CryptFindOIDInfo(keyType, intPtr, group);
498 if (intPtr3 != IntPtr.Zero)
499 {
500 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr3);
501 }
502 if (fallBackToAllGroups && group != 0)
503 {
504 IntPtr intPtr4 = CryptFindOIDInfo(keyType, intPtr, OidGroup.All);
505 if (intPtr4 != IntPtr.Zero)
506 {
507 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr4);
508 }
509 }
510 CRYPT_OID_INFO result = default(CRYPT_OID_INFO);
511 result.AlgId = -1;
512 return result;
513 }
514 finally
515 {
516 if (intPtr != IntPtr.Zero)
517 {
518 Marshal.FreeCoTaskMem(intPtr);
519 }
520 }
521 }
522
524 {
525 if (group != OidGroup.HashAlgorithm && group != OidGroup.EncryptionAlgorithm && group != OidGroup.PublicKeyAlgorithm && group != OidGroup.SignatureAlgorithm && group != OidGroup.Attribute && group != OidGroup.ExtensionOrAttribute)
526 {
527 return group == OidGroup.KeyDerivationFunction;
528 }
529 return true;
530 }
531
532 [DllImport("crypt32.dll", CharSet = CharSet.Unicode)]
533 private static extern IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group);
534
535 [DllImport("crypt32.dll", CharSet = CharSet.Unicode)]
536 private static extern IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, ref int pvKey, OidGroup group);
537
539 {
540 int pvKey = (int)algId;
541 IntPtr intPtr = CryptFindOIDInfo(CryptOidInfoKeyType.CRYPT_OID_INFO_ALGID_KEY, ref pvKey, OidGroup.HashAlgorithm);
542 if (intPtr != IntPtr.Zero)
543 {
544 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr);
545 }
546 CRYPT_OID_INFO result = default(CRYPT_OID_INFO);
547 result.AlgId = -1;
548 return result;
549 }
550 }
551
552 internal static class NCrypt
553 {
555 {
556 None = 0,
561 }
562
563 internal enum ErrorCode
564 {
565 ERROR_SUCCESS = 0,
566 NTE_BAD_SIGNATURE = -2146893818,
567 NTE_NOT_FOUND = -2146893807,
568 NTE_BAD_KEYSET = -2146893802,
569 NTE_INVALID_PARAMETER = -2146893785,
570 NTE_BUFFER_TOO_SMALL = -2146893784,
571 NTE_NOT_SUPPORTED = -2146893783,
572 NTE_NO_MORE_ITEMS = -2146893782,
573 E_FAIL = -2147467259,
574 STATUS_UNSUCCESSFUL = -1073741823
575 }
576
577 internal struct CRYPT_PKCS12_PBE_PARAMS
578 {
579 internal int iIterations;
580
581 internal int cbSalt;
582 }
583
584 internal struct PBE_PARAMS
585 {
586 internal CRYPT_PKCS12_PBE_PARAMS Params;
587
588 internal unsafe fixed byte rgbSalt[8];
589 }
590
591 [Flags]
593 {
594 None = 0,
596 }
597
598 internal enum BufferType
599 {
602 KdfSecretAppend = 2,
603 KdfHmacKey = 3,
604 KdfTlsLabel = 4,
605 KdfTlsSeed = 5,
606 PkcsAlgOid = 41,
607 PkcsAlgParam = 42,
608 PkcsSecret = 46
609 }
610
611 internal struct NCryptBuffer
612 {
613 public int cbBuffer;
614
615 public BufferType BufferType;
616
617 public IntPtr pvBuffer;
618 }
619
620 internal struct NCryptBufferDesc
621 {
622 public int ulVersion;
623
624 public int cBuffers;
625
626 public IntPtr pBuffers;
627 }
628
629 internal struct NCRYPT_UI_POLICY
630 {
631 public int dwVersion;
632
634
636
638
640 }
641
642 internal unsafe static ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan<byte> pbInput, int cbInput, void* pPaddingInfo, Span<byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
643 {
644 return NCryptEncrypt(hKey, ref MemoryMarshal.GetReference(pbInput), cbInput, pPaddingInfo, ref MemoryMarshal.GetReference(pbOutput), cbOutput, out pcbResult, dwFlags);
645 }
646
647 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
648 private unsafe static extern ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void* pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);
649
650 internal unsafe static ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan<byte> pbInput, int cbInput, void* pPaddingInfo, Span<byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
651 {
652 return NCryptDecrypt(hKey, ref MemoryMarshal.GetReference(pbInput), cbInput, pPaddingInfo, ref MemoryMarshal.GetReference(pbOutput), cbOutput, out pcbResult, dwFlags);
653 }
654
655 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
656 private unsafe static extern ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void* pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);
657
658 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
659 internal static extern ErrorCode NCryptOpenKey(SafeNCryptProviderHandle hProvider, out SafeNCryptKeyHandle phKey, string pszKeyName, int dwLegacyKeySpec, CngKeyOpenOptions dwFlags);
660
661 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
662 internal static extern ErrorCode NCryptImportKey(SafeNCryptProviderHandle hProvider, IntPtr hImportKey, string pszBlobType, IntPtr pParameterList, out SafeNCryptKeyHandle phKey, ref byte pbData, int cbData, int dwFlags);
663
664 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
665 internal static extern ErrorCode NCryptImportKey(SafeNCryptProviderHandle hProvider, IntPtr hImportKey, string pszBlobType, ref NCryptBufferDesc pParameterList, out SafeNCryptKeyHandle phKey, ref byte pbData, int cbData, int dwFlags);
666
667 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
668 internal static extern ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, [Out] byte[] pbOutput, int cbOutput, out int pcbResult, int dwFlags);
669
670 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
671 internal static extern ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, ref byte pbOutput, int cbOutput, out int pcbResult, int dwFlags);
672
673 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
674 internal static extern ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, ref NCryptBufferDesc pParameterList, ref byte pbOutput, int cbOutput, out int pcbResult, int dwFlags);
675
676 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
677 internal static extern ErrorCode NCryptDeleteKey(SafeNCryptKeyHandle hKey, int dwFlags);
678
679 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
680 internal static extern ErrorCode NCryptCreatePersistedKey(SafeNCryptProviderHandle hProvider, out SafeNCryptKeyHandle phKey, string pszAlgId, string pszKeyName, int dwLegacyKeySpec, CngKeyCreationOptions dwFlags);
681
682 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
683 internal static extern ErrorCode NCryptFinalizeKey(SafeNCryptKeyHandle hKey, int dwFlags);
684
685 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
686 internal static extern ErrorCode NCryptFreeObject(IntPtr hObject);
687
688 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
689 internal static extern ErrorCode NCryptOpenStorageProvider(out SafeNCryptProviderHandle phProvider, string pszProviderName, int dwFlags);
690
691 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
692 private static extern ErrorCode NCryptDeriveKey(SafeNCryptSecretHandle hSharedSecret, string pwszKDF, [In] ref NCryptBufferDesc pParameterList, [Out][MarshalAs(UnmanagedType.LPArray)] byte[] pbDerivedKey, int cbDerivedKey, out int pcbResult, SecretAgreementFlags dwFlags);
693
694 private unsafe static byte[] DeriveKeyMaterial(SafeNCryptSecretHandle secretAgreement, string kdf, string hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
695 {
696 IntPtr intPtr = IntPtr.Zero;
697 try
698 {
699 intPtr = Marshal.StringToCoTaskMemUni(hashAlgorithm);
700 Span<NCryptBuffer> span = stackalloc NCryptBuffer[4];
701 int num = 0;
702 NCryptBuffer nCryptBuffer = default(NCryptBuffer);
703 nCryptBuffer.cbBuffer = (hashAlgorithm.Length + 1) * 2;
704 nCryptBuffer.BufferType = BufferType.KdfHashAlgorithm;
705 nCryptBuffer.pvBuffer = intPtr;
706 span[num] = nCryptBuffer;
707 num++;
708 fixed (byte* ptr = hmacKey)
709 {
710 fixed (byte* ptr2 = secretPrepend)
711 {
712 fixed (byte* ptr3 = secretAppend)
713 {
714 if (ptr != null)
715 {
716 NCryptBuffer nCryptBuffer2 = default(NCryptBuffer);
717 nCryptBuffer2.cbBuffer = hmacKey.Length;
718 nCryptBuffer2.BufferType = BufferType.KdfHmacKey;
719 nCryptBuffer2.pvBuffer = new IntPtr(ptr);
720 span[num] = nCryptBuffer2;
721 num++;
722 }
723 if (ptr2 != null)
724 {
725 NCryptBuffer nCryptBuffer3 = default(NCryptBuffer);
726 nCryptBuffer3.cbBuffer = secretPrepend.Length;
727 nCryptBuffer3.BufferType = BufferType.KdfSecretPrepend;
728 nCryptBuffer3.pvBuffer = new IntPtr(ptr2);
729 span[num] = nCryptBuffer3;
730 num++;
731 }
732 if (ptr3 != null)
733 {
734 NCryptBuffer nCryptBuffer4 = default(NCryptBuffer);
735 nCryptBuffer4.cbBuffer = secretAppend.Length;
736 nCryptBuffer4.BufferType = BufferType.KdfSecretAppend;
737 nCryptBuffer4.pvBuffer = new IntPtr(ptr3);
738 span[num] = nCryptBuffer4;
739 num++;
740 }
741 return DeriveKeyMaterial(secretAgreement, kdf, span.Slice(0, num), flags);
742 }
743 }
744 }
745 }
746 finally
747 {
748 if (intPtr != IntPtr.Zero)
749 {
750 Marshal.FreeCoTaskMem(intPtr);
751 }
752 }
753 }
754
755 private unsafe static byte[] DeriveKeyMaterial(SafeNCryptSecretHandle secretAgreement, string kdf, ReadOnlySpan<NCryptBuffer> parameters, SecretAgreementFlags flags)
756 {
757 fixed (NCryptBuffer* value = &MemoryMarshal.GetReference(parameters))
758 {
759 NCryptBufferDesc pParameterList = default(NCryptBufferDesc);
760 pParameterList.ulVersion = 0;
761 pParameterList.cBuffers = parameters.Length;
762 pParameterList.pBuffers = new IntPtr(value);
763 ErrorCode errorCode = NCryptDeriveKey(secretAgreement, kdf, ref pParameterList, null, 0, out var pcbResult, flags);
764 if (errorCode != 0 && errorCode != ErrorCode.NTE_BUFFER_TOO_SMALL)
765 {
766 throw errorCode.ToCryptographicException();
767 }
768 byte[] array = new byte[pcbResult];
769 errorCode = NCryptDeriveKey(secretAgreement, kdf, ref pParameterList, array, array.Length, out pcbResult, flags);
770 if (errorCode != 0)
771 {
772 throw errorCode.ToCryptographicException();
773 }
774 Array.Resize(ref array, Math.Min(pcbResult, array.Length));
775 return array;
776 }
777 }
778
779 internal static byte[] DeriveKeyMaterialHash(SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
780 {
781 return DeriveKeyMaterial(secretAgreement, "HASH", hashAlgorithm, null, secretPrepend, secretAppend, flags);
782 }
783
784 internal static byte[] DeriveKeyMaterialHmac(SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
785 {
786 return DeriveKeyMaterial(secretAgreement, "HMAC", hashAlgorithm, hmacKey, secretPrepend, secretAppend, flags);
787 }
788
789 internal unsafe static byte[] DeriveKeyMaterialTls(SafeNCryptSecretHandle secretAgreement, byte[] label, byte[] seed, SecretAgreementFlags flags)
790 {
791 Span<NCryptBuffer> span = stackalloc NCryptBuffer[2];
792 fixed (byte* value = label)
793 {
794 fixed (byte* value2 = seed)
795 {
796 NCryptBuffer nCryptBuffer = default(NCryptBuffer);
797 nCryptBuffer.cbBuffer = label.Length;
798 nCryptBuffer.BufferType = BufferType.KdfTlsLabel;
799 nCryptBuffer.pvBuffer = new IntPtr(value);
800 span[0] = nCryptBuffer;
801 NCryptBuffer nCryptBuffer2 = default(NCryptBuffer);
802 nCryptBuffer2.cbBuffer = seed.Length;
803 nCryptBuffer2.BufferType = BufferType.KdfTlsSeed;
804 nCryptBuffer2.pvBuffer = new IntPtr(value2);
805 span[1] = nCryptBuffer2;
806 return DeriveKeyMaterial(secretAgreement, "TLS_PRF", span, flags);
807 }
808 }
809 }
810
811 [DllImport("ncrypt.dll")]
812 private static extern ErrorCode NCryptSecretAgreement(SafeNCryptKeyHandle hPrivKey, SafeNCryptKeyHandle hPubKey, out SafeNCryptSecretHandle phSecret, int dwFlags);
813
815 {
816 SafeNCryptSecretHandle phSecret;
817 ErrorCode errorCode = NCryptSecretAgreement(privateKey, otherPartyPublicKey, out phSecret, 0);
818 if (errorCode != 0)
819 {
820 throw errorCode.ToCryptographicException();
821 }
822 return phSecret;
823 }
824
825 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
826 internal unsafe static extern ErrorCode NCryptGetProperty(SafeNCryptHandle hObject, string pszProperty, [Out] void* pbOutput, int cbOutput, out int pcbResult, CngPropertyOptions dwFlags);
827
828 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
829 internal unsafe static extern ErrorCode NCryptSetProperty(SafeNCryptHandle hObject, string pszProperty, [In] void* pbInput, int cbInput, CngPropertyOptions dwFlags);
830
831 internal unsafe static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string pszProperty, ref int result)
832 {
833 fixed (int* pbOutput = &result)
834 {
835 int pcbResult;
836 return NCryptGetProperty(hObject, pszProperty, pbOutput, 4, out pcbResult, CngPropertyOptions.None);
837 }
838 }
839
840 internal unsafe static ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan<byte> pbHashValue, int cbHashValue, Span<byte> pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags)
841 {
842 return NCryptSignHash(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, out pcbResult, dwFlags);
843 }
844
845 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
846 private unsafe static extern ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags);
847
848 internal unsafe static ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan<byte> pbHashValue, int cbHashValue, ReadOnlySpan<byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags)
849 {
850 return NCryptVerifySignature(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, dwFlags);
851 }
852
853 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
854 private unsafe static extern ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);
855 }
856
857 internal static class Kernel32
858 {
859 [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", ExactSpelling = true, SetLastError = true)]
860 private unsafe static extern int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void* lpBuffer, int nSize, IntPtr arguments);
861
862 internal static string GetMessage(int errorCode)
863 {
864 return GetMessage(errorCode, IntPtr.Zero);
865 }
866
867 internal unsafe static string GetMessage(int errorCode, IntPtr moduleHandle)
868 {
869 int num = 12800;
870 if (moduleHandle != IntPtr.Zero)
871 {
872 num |= 0x800;
873 }
874 Span<char> span = stackalloc char[256];
875 fixed (char* lpBuffer = span)
876 {
877 int num2 = FormatMessage(num, moduleHandle, (uint)errorCode, 0, lpBuffer, span.Length, IntPtr.Zero);
878 if (num2 > 0)
879 {
880 return GetAndTrimString(span.Slice(0, num2));
881 }
882 }
883 if (Marshal.GetLastWin32Error() == 122)
884 {
885 IntPtr intPtr = default(IntPtr);
886 try
887 {
888 int num3 = FormatMessage(num | 0x100, moduleHandle, (uint)errorCode, 0, &intPtr, 0, IntPtr.Zero);
889 if (num3 > 0)
890 {
891 return GetAndTrimString(new Span<char>((void*)intPtr, num3));
892 }
893 }
894 finally
895 {
896 Marshal.FreeHGlobal(intPtr);
897 }
898 }
899 return $"Unknown error (0x{errorCode:x})";
900 }
901
902 private static string GetAndTrimString(Span<char> buffer)
903 {
904 int num = buffer.Length;
905 while (num > 0 && buffer[num - 1] <= ' ')
906 {
907 num--;
908 }
909 return buffer.Slice(0, num).ToString();
910 }
911 }
912}
static volatile Entry[] _cache
Definition Interop.cs:328
static Microsoft.Win32.SafeHandles.SafeBCryptAlgorithmHandle GetCachedBCryptAlgorithmHandle(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, out int hashSizeInBytes)
Definition Interop.cs:278
static NTSTATUS BCryptDestroyHash(IntPtr hHash)
static unsafe int BCryptEncrypt(SafeKeyHandle hKey, ReadOnlySpan< byte > input, byte[] iv, Span< byte > output)
Definition Interop.cs:386
static unsafe Internal.NativeCrypto.SafeKeyHandle BCryptImportKey(Internal.NativeCrypto.SafeAlgorithmHandle hAlg, ReadOnlySpan< byte > key)
Definition Interop.cs:306
static Exception CreateCryptographicException(NTSTATUS ntStatus)
Definition Interop.cs:461
static unsafe NTSTATUS BCryptGetProperty(Microsoft.Win32.SafeHandles.SafeBCryptHandle hObject, string pszProperty, void *pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static unsafe NTSTATUS BCryptEncrypt(Internal.NativeCrypto.SafeKeyHandle hKey, byte *pbInput, int cbInput, IntPtr paddingInfo, [In][Out] byte[] pbIV, int cbIV, byte *pbOutput, int cbOutput, out int cbResult, int dwFlags)
static NTSTATUS BCryptOpenAlgorithmProvider(out Microsoft.Win32.SafeHandles.SafeBCryptAlgorithmHandle phAlgorithm, string pszAlgId, string pszImplementation, BCryptOpenAlgorithmProviderFlags dwFlags)
static NTSTATUS BCryptHashData(Microsoft.Win32.SafeHandles.SafeBCryptHashHandle hHash, ref byte pbInput, int cbInput, int dwFlags)
static void EmitBigEndian(byte[] blob, ref int offset, int value)
Definition Interop.cs:387
static NTSTATUS BCryptCreateHash(Microsoft.Win32.SafeHandles.SafeBCryptAlgorithmHandle hAlgorithm, out Microsoft.Win32.SafeHandles.SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ref byte pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags)
static unsafe NTSTATUS BCryptDecrypt(Internal.NativeCrypto.SafeKeyHandle hKey, byte *pbInput, int cbInput, IntPtr paddingInfo, [In][Out] byte[] pbIV, int cbIV, byte *pbOutput, int cbOutput, out int cbResult, int dwFlags)
static NTSTATUS BCryptImportKey(Internal.NativeCrypto.SafeAlgorithmHandle hAlgorithm, IntPtr hImportKey, string pszBlobType, out Internal.NativeCrypto.SafeKeyHandle hKey, IntPtr pbKeyObject, int cbKeyObject, byte[] pbInput, int cbInput, int dwFlags)
BCryptOpenAlgorithmProviderFlags
Definition Interop.cs:272
static NTSTATUS BCryptCloseAlgorithmProvider(IntPtr hAlgorithm, int dwFlags)
static void EmitByte(byte[] blob, ref int offset, byte value, int count=1)
Definition Interop.cs:377
static unsafe SafeKeyHandle BCryptImportKey(SafeAlgorithmHandle hAlg, ReadOnlySpan< byte > key)
Definition Interop.cs:361
static NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ReadOnlySpan< byte > secret, int cbSecret, BCryptCreateHashFlags dwFlags)
Definition Interop.cs:473
static NTSTATUS BCryptFinishHash(Microsoft.Win32.SafeHandles.SafeBCryptHashHandle hHash, Span< byte > pbOutput, int cbOutput, int dwFlags)
Definition Interop.cs:434
static unsafe int BCryptDecrypt(Internal.NativeCrypto.SafeKeyHandle hKey, ReadOnlySpan< byte > input, byte[] iv, Span< byte > output)
Definition Interop.cs:348
static byte[] Consume(byte[] blob, ref int offset, int count)
Definition Interop.cs:395
static unsafe int BCryptEncrypt(Internal.NativeCrypto.SafeKeyHandle hKey, ReadOnlySpan< byte > input, byte[] iv, Span< byte > output)
Definition Interop.cs:331
static NTSTATUS BCryptOpenAlgorithmProvider(out SafeBCryptAlgorithmHandle phAlgorithm, string pszAlgId, string pszImplementation, BCryptOpenAlgorithmProviderFlags dwFlags)
static NTSTATUS BCryptHashData(Microsoft.Win32.SafeHandles.SafeBCryptHashHandle hHash, ReadOnlySpan< byte > pbInput, int cbInput, int dwFlags)
Definition Interop.cs:426
static unsafe int BCryptDecrypt(SafeKeyHandle hKey, ReadOnlySpan< byte > input, byte[] iv, Span< byte > output)
Definition Interop.cs:403
static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span< byte > pbOutput, int cbOutput, int dwFlags)
Definition Interop.cs:516
static unsafe NTSTATUS BCryptGetProperty(SafeBCryptHandle hObject, string pszProperty, void *pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan< byte > pbInput, int cbInput, int dwFlags)
Definition Interop.cs:508
static NTSTATUS BCryptCreateHash(Microsoft.Win32.SafeHandles.SafeBCryptAlgorithmHandle hAlgorithm, out Microsoft.Win32.SafeHandles.SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ReadOnlySpan< byte > secret, int cbSecret, BCryptCreateHashFlags dwFlags)
Definition Interop.cs:415
static void Emit(byte[] blob, ref int offset, byte[] value)
Definition Interop.cs:371
static NTSTATUS BCryptFinishHash(Microsoft.Win32.SafeHandles.SafeBCryptHashHandle hHash, ref byte pbOutput, int cbOutput, int dwFlags)
static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
Definition Interop.cs:477
static IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, ref int pvKey, OidGroup group)
static IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group)
static bool OidGroupWillNotUseActiveDirectory(OidGroup group)
Definition Interop.cs:900
static CRYPT_OID_INFO FindAlgIdOidInfo(BCrypt.ECC_CURVE_ALG_ID_ENUM algId)
Definition Interop.cs:538
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
Definition Interop.cs:867
static void SetLastError(int errorCode)
static string GetMessage(int errorCode)
Definition Interop.cs:862
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static string GetAndTrimString(Span< char > buffer)
Definition Interop.cs:153
static unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan< byte > pbInput, int cbInput, void *pPaddingInfo, Span< byte > pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
Definition Interop.cs:650
static ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, [Out] byte[] pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, ref NCryptBufferDesc pParameterList, ref byte pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static unsafe ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string pszProperty, ref int result)
Definition Interop.cs:831
static byte[] DeriveKeyMaterialHmac(SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
Definition Interop.cs:784
static unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags)
static unsafe byte[] DeriveKeyMaterial(SafeNCryptSecretHandle secretAgreement, string kdf, string hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
Definition Interop.cs:694
static unsafe byte[] DeriveKeyMaterialTls(SafeNCryptSecretHandle secretAgreement, byte[] label, byte[] seed, SecretAgreementFlags flags)
Definition Interop.cs:789
static unsafe ErrorCode NCryptSetProperty(SafeNCryptHandle hObject, string pszProperty, [In] void *pbInput, int cbInput, CngPropertyOptions dwFlags)
static unsafe ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void *pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
static ErrorCode NCryptImportKey(SafeNCryptProviderHandle hProvider, IntPtr hImportKey, string pszBlobType, IntPtr pParameterList, out SafeNCryptKeyHandle phKey, ref byte pbData, int cbData, int dwFlags)
static unsafe ErrorCode NCryptGetProperty(SafeNCryptHandle hObject, string pszProperty, [Out] void *pbOutput, int cbOutput, out int pcbResult, CngPropertyOptions dwFlags)
static ErrorCode NCryptSecretAgreement(SafeNCryptKeyHandle hPrivKey, SafeNCryptKeyHandle hPubKey, out SafeNCryptSecretHandle phSecret, int dwFlags)
static ErrorCode NCryptOpenStorageProvider(out SafeNCryptProviderHandle phProvider, string pszProviderName, int dwFlags)
static byte[] DeriveKeyMaterialHash(SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
Definition Interop.cs:779
static ErrorCode NCryptCreatePersistedKey(SafeNCryptProviderHandle hProvider, out SafeNCryptKeyHandle phKey, string pszAlgId, string pszKeyName, int dwLegacyKeySpec, CngKeyCreationOptions dwFlags)
static unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void *pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
static unsafe ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan< byte > pbInput, int cbInput, void *pPaddingInfo, Span< byte > pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
Definition Interop.cs:642
static SafeNCryptSecretHandle DeriveSecretAgreement(SafeNCryptKeyHandle privateKey, SafeNCryptKeyHandle otherPartyPublicKey)
Definition Interop.cs:814
static ErrorCode NCryptDeleteKey(SafeNCryptKeyHandle hKey, int dwFlags)
static ErrorCode NCryptOpenKey(SafeNCryptProviderHandle hProvider, out SafeNCryptKeyHandle phKey, string pszKeyName, int dwLegacyKeySpec, CngKeyOpenOptions dwFlags)
static ErrorCode NCryptImportKey(SafeNCryptProviderHandle hProvider, IntPtr hImportKey, string pszBlobType, ref NCryptBufferDesc pParameterList, out SafeNCryptKeyHandle phKey, ref byte pbData, int cbData, int dwFlags)
static ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, ref byte pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags)
static ErrorCode NCryptFinalizeKey(SafeNCryptKeyHandle hKey, int dwFlags)
static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan< byte > pbHashValue, int cbHashValue, ReadOnlySpan< byte > pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags)
Definition Interop.cs:848
static ErrorCode NCryptFreeObject(IntPtr hObject)
static unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void *pPaddingInfo, ReadOnlySpan< byte > pbHashValue, int cbHashValue, Span< byte > pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags)
Definition Interop.cs:840
static unsafe byte[] DeriveKeyMaterial(SafeNCryptSecretHandle secretAgreement, string kdf, ReadOnlySpan< NCryptBuffer > parameters, SecretAgreementFlags flags)
Definition Interop.cs:755
static ErrorCode NCryptDeriveKey(SafeNCryptSecretHandle hSharedSecret, string pwszKDF, [In] ref NCryptBufferDesc pParameterList, [Out][MarshalAs(UnmanagedType.LPArray)] byte[] pbDerivedKey, int cbDerivedKey, out int pcbResult, SecretAgreementFlags dwFlags)
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static unsafe IntPtr StringToCoTaskMemUni(string? s)
Definition Marshal.cs:1327
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static void FreeCoTaskMem(IntPtr ptr)
Definition Marshal.cs:1712
static unsafe? string PtrToStringUni(IntPtr ptr)
Definition Marshal.cs:652
static ? object PtrToStructure(IntPtr ptr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type structureType)
Definition Marshal.cs:1164
static unsafe IntPtr StringToCoTaskMemAnsi(string? s)
Definition Marshal.cs:1362
DSAFIPSVERSION_ENUM standardVersion
Definition Interop.cs:116
unsafe fixed byte q[20]
Definition Interop.cs:105
unsafe fixed byte Count[4]
Definition Interop.cs:101
unsafe fixed byte Seed[20]
Definition Interop.cs:103
ECC_CURVE_ALG_ID_ENUM CurveGenerationAlgId
Definition Interop.cs:165
ECC_CURVE_ALG_ID_ENUM CurveGenerationAlgId
Definition Interop.cs:222
KeyBlobMagicNumber Magic
Definition Interop.cs:82
unsafe Entry(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, Microsoft.Win32.SafeHandles.SafeBCryptAlgorithmHandle handle)
Definition Interop.cs:259
SafeBCryptAlgorithmHandle Handle
Definition Interop.cs:307
CngBufferDescriptors BufferType
Definition Interop.cs:202
CngUIProtectionLevels dwFlags
Definition Interop.cs:633
unsafe fixed byte rgbSalt[8]
Definition Interop.cs:611
CRYPT_PKCS12_PBE_PARAMS Params
Definition Interop.cs:609
static readonly IntPtr Zero
Definition IntPtr.cs:18
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70