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 {
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
32 {
33 internal IntPtr pszAlgId;
34
35 internal IntPtr pbLabel;
36
37 internal int cbLabel;
38 }
39
41 {
42 internal IntPtr pszAlgId;
43 }
44
46 {
47 internal IntPtr pszAlgId;
48
49 internal int cbSalt;
50 }
51
79
80 internal struct BCRYPT_RSAKEY_BLOB
81 {
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 {
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
109 {
111
112 internal int cbKey;
113
115
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 {
141
142 internal int cbKey;
143 }
144
151
156
158 {
160
161 internal int Version;
162
164
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
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
217 {
218 internal int Version;
219
221
223
224 internal int cbFieldLength;
225
226 internal int cbSubgroupOrder;
227
228 internal int cbCofactor;
229
230 internal int cbSeed;
231 }
232
234 {
235 private int cbSize;
236
237 private uint dwInfoVersion;
238
239 internal unsafe byte* pbNonce;
240
241 internal int cbNonce;
242
243 internal unsafe byte* pbAuthData;
244
245 internal int cbAuthData;
246
247 internal unsafe byte* pbTag;
248
249 internal int cbTag;
250
251 internal unsafe byte* pbMacContext;
252
253 internal int cbMacContext;
254
255 internal int cbAAD;
256
257 internal ulong cbData;
258
259 internal uint dwFlags;
260
262 {
264 result.cbSize = sizeof(BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO);
265 result.dwInfoVersion = 1u;
266 return result;
267 }
268 }
269
270 [Flags]
272 {
273 None = 0,
275 }
276
277 [Flags]
279 {
280 None = 0,
282 }
283
298
299 internal static class BCryptAlgorithmCache
300 {
301 private struct Entry
302 {
303 public string HashAlgorithmId { get; private set; }
304
305 public BCryptOpenAlgorithmProviderFlags Flags { get; private set; }
306
307 public SafeBCryptAlgorithmHandle Handle { get; private set; }
308
309 public int HashSizeInBytes { get; private set; }
310
311 public unsafe Entry(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, SafeBCryptAlgorithmHandle handle)
312 {
313 this = default(Entry);
314 HashAlgorithmId = hashAlgorithmId;
315 Flags = flags;
316 Handle = handle;
317 Unsafe.SkipInit(out int hashSizeInBytes);
318 int pcbResult;
319 NTSTATUS nTSTATUS = BCryptGetProperty(handle, "HashDigestLength", &hashSizeInBytes, 4, out pcbResult, 0);
320 if (nTSTATUS != 0)
321 {
322 throw CreateCryptographicException(nTSTATUS);
323 }
324 HashSizeInBytes = hashSizeInBytes;
325 }
326 }
327
328 private static volatile Entry[] _cache = Array.Empty<Entry>();
329
330 public static SafeBCryptAlgorithmHandle GetCachedBCryptAlgorithmHandle(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, out int hashSizeInBytes)
331 {
332 Entry[] array = _cache;
333 Entry[] array2 = array;
334 for (int i = 0; i < array2.Length; i++)
335 {
336 Entry entry = array2[i];
337 if (entry.HashAlgorithmId == hashAlgorithmId && entry.Flags == flags)
338 {
339 hashSizeInBytes = entry.HashSizeInBytes;
340 return entry.Handle;
341 }
342 }
343 SafeBCryptAlgorithmHandle phAlgorithm;
344 NTSTATUS nTSTATUS = BCryptOpenAlgorithmProvider(out phAlgorithm, hashAlgorithmId, null, flags);
345 if (nTSTATUS != 0)
346 {
347 throw CreateCryptographicException(nTSTATUS);
348 }
349 Array.Resize(ref array, array.Length + 1);
350 Entry entry2 = new Entry(hashAlgorithmId, flags, phAlgorithm);
351 array[^1] = new Entry(hashAlgorithmId, flags, phAlgorithm);
352 _cache = array;
353 hashSizeInBytes = entry2.HashSizeInBytes;
354 return entry2.Handle;
355 }
356 }
357
359
360
362 {
363 int length = key.Length;
364 int num = sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + length;
365 byte[] array = new byte[num];
366 fixed (byte* ptr = array)
367 {
369 ptr2->dwMagic = 1296188491u;
370 ptr2->dwVersion = 1u;
371 ptr2->cbKeyData = (uint)length;
372 }
373 key.CopyTo(array.AsSpan(sizeof(BCRYPT_KEY_DATA_BLOB_HEADER)));
374 SafeKeyHandle hKey;
375 NTSTATUS nTSTATUS = BCryptImportKey(hAlg, IntPtr.Zero, "KeyDataBlob", out hKey, IntPtr.Zero, 0, array, num, 0);
376 if (nTSTATUS != 0)
377 {
378 throw CreateCryptographicException(nTSTATUS);
379 }
380 return hKey;
381 }
382
383 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
384 private static extern NTSTATUS BCryptImportKey(SafeAlgorithmHandle hAlgorithm, IntPtr hImportKey, string pszBlobType, out SafeKeyHandle hKey, IntPtr pbKeyObject, int cbKeyObject, byte[] pbInput, int cbInput, int dwFlags);
385
386 internal unsafe static int BCryptEncrypt(SafeKeyHandle hKey, ReadOnlySpan<byte> input, byte[] iv, Span<byte> output)
387 {
388 fixed (byte* pbInput = input)
389 {
390 fixed (byte* pbOutput = output)
391 {
392 int cbResult;
393 NTSTATUS nTSTATUS = BCryptEncrypt(hKey, pbInput, input.Length, IntPtr.Zero, iv, (iv != null) ? iv.Length : 0, pbOutput, output.Length, out cbResult, 0);
394 if (nTSTATUS != 0)
395 {
396 throw CreateCryptographicException(nTSTATUS);
397 }
398 return cbResult;
399 }
400 }
401 }
402
403 internal unsafe static int BCryptDecrypt(SafeKeyHandle hKey, ReadOnlySpan<byte> input, byte[] iv, Span<byte> output)
404 {
405 fixed (byte* pbInput = input)
406 {
407 fixed (byte* pbOutput = output)
408 {
409 int cbResult;
410 NTSTATUS nTSTATUS = BCryptDecrypt(hKey, pbInput, input.Length, IntPtr.Zero, iv, (iv != null) ? iv.Length : 0, pbOutput, output.Length, out cbResult, 0);
411 if (nTSTATUS != 0)
412 {
413 throw CreateCryptographicException(nTSTATUS);
414 }
415 return cbResult;
416 }
417 }
418 }
419
420 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
421 public unsafe static extern NTSTATUS BCryptEncrypt(SafeKeyHandle hKey, byte* pbInput, int cbInput, IntPtr paddingInfo, [In][Out] byte[] pbIV, int cbIV, byte* pbOutput, int cbOutput, out int cbResult, int dwFlags);
422
423 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
424 public unsafe static extern NTSTATUS BCryptDecrypt(SafeKeyHandle hKey, byte* pbInput, int cbInput, IntPtr paddingInfo, [In][Out] byte[] pbIV, int cbIV, byte* pbOutput, int cbOutput, out int cbResult, int dwFlags);
425
426 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
427 internal unsafe static extern NTSTATUS BCryptGenRandom(IntPtr hAlgorithm, byte* pbBuffer, int cbBuffer, int dwFlags);
428
429 internal static void Emit(byte[] blob, ref int offset, byte[] value)
430 {
431 Buffer.BlockCopy(value, 0, blob, offset, value.Length);
432 offset += value.Length;
433 }
434
435 internal static void EmitByte(byte[] blob, ref int offset, byte value, int count = 1)
436 {
437 int num = offset + count;
438 for (int i = offset; i < num; i++)
439 {
440 blob[i] = value;
441 }
442 offset = num;
443 }
444
445 internal static void EmitBigEndian(byte[] blob, ref int offset, int value)
446 {
447 blob[offset++] = (byte)(value >> 24);
448 blob[offset++] = (byte)(value >> 16);
449 blob[offset++] = (byte)(value >> 8);
450 blob[offset++] = (byte)value;
451 }
452
453 internal static byte[] Consume(byte[] blob, ref int offset, int count)
454 {
455 byte[] array = new byte[count];
456 Buffer.BlockCopy(blob, offset, array, 0, count);
457 offset += count;
458 return array;
459 }
460
462 {
463 int hr = (int)(ntStatus | (NTSTATUS)16777216u);
464 return hr.ToCryptographicException();
465 }
466
467 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
468 internal static extern NTSTATUS BCryptOpenAlgorithmProvider(out SafeBCryptAlgorithmHandle phAlgorithm, string pszAlgId, string pszImplementation, BCryptOpenAlgorithmProviderFlags dwFlags);
469
470 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
471 internal static extern NTSTATUS BCryptCloseAlgorithmProvider(IntPtr hAlgorithm, int dwFlags);
472
473 internal static NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ReadOnlySpan<byte> secret, int cbSecret, BCryptCreateHashFlags dwFlags)
474 {
475 return BCryptCreateHash(hAlgorithm, out phHash, pbHashObject, cbHashObject, ref MemoryMarshal.GetReference(secret), cbSecret, dwFlags);
476 }
477
478 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
479 private static extern NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ref byte pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags);
480
481 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
482 internal static extern NTSTATUS BCryptDestroyHash(IntPtr hHash);
483
485 {
486 SafeBCryptHashHandle phNewHash;
487 NTSTATUS nTSTATUS = BCryptDuplicateHash(hHash, out phNewHash, IntPtr.Zero, 0, 0);
488 if (nTSTATUS != 0)
489 {
490 phNewHash.Dispose();
491 throw CreateCryptographicException(nTSTATUS);
492 }
493 return phNewHash;
494 }
495
496 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
497 private static extern NTSTATUS BCryptDuplicateHash(SafeBCryptHashHandle hHash, out SafeBCryptHashHandle phNewHash, IntPtr pbHashObject, int cbHashObject, int dwFlags);
498
499 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
500 internal unsafe static extern NTSTATUS BCryptHash(nuint hAlgorithm, byte* pbSecret, int cbSecret, byte* pbInput, int cbInput, byte* pbOutput, int cbOutput);
501
502 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
503 internal unsafe static extern NTSTATUS BCryptDeriveKeyPBKDF2(SafeBCryptAlgorithmHandle hPrf, byte* pbPassword, int cbPassword, byte* pbSalt, int cbSalt, ulong cIterations, byte* pbDerivedKey, int cbDerivedKey, uint dwFlags);
504
505 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
506 internal static extern NTSTATUS BCryptDestroyKey(IntPtr hKey);
507
508 internal static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan<byte> pbInput, int cbInput, int dwFlags)
509 {
510 return BCryptHashData(hHash, ref MemoryMarshal.GetReference(pbInput), cbInput, dwFlags);
511 }
512
513 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
514 private static extern NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ref byte pbInput, int cbInput, int dwFlags);
515
516 internal static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span<byte> pbOutput, int cbOutput, int dwFlags)
517 {
518 return BCryptFinishHash(hHash, ref MemoryMarshal.GetReference(pbOutput), cbOutput, dwFlags);
519 }
520
521 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
522 private static extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, ref byte pbOutput, int cbOutput, int dwFlags);
523
524 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
525 internal unsafe static extern NTSTATUS BCryptGetProperty(SafeBCryptHandle hObject, string pszProperty, void* pbOutput, int cbOutput, out int pcbResult, int dwFlags);
526
527 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
528 internal unsafe static extern NTSTATUS BCryptGenerateSymmetricKey(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptKeyHandle phKey, IntPtr pbKeyObject, int cbKeyObject, byte* pbSecret, int cbSecret, uint dwFlags);
529
530 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
531 internal unsafe static extern NTSTATUS BCryptGenerateSymmetricKey(nuint hAlgorithm, out SafeBCryptKeyHandle phKey, IntPtr pbKeyObject, int cbKeyObject, byte* pbSecret, int cbSecret, uint dwFlags);
532
533 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
534 internal unsafe static extern NTSTATUS BCryptKeyDerivation(SafeBCryptKeyHandle hKey, BCryptBufferDesc* pParameterList, byte* pbDerivedKey, int cbDerivedKey, out uint pcbResult, int dwFlags);
535 }
536
537 internal static class NCrypt
538 {
539 [Flags]
541 {
542 None = 0,
544 }
545
546 internal enum BufferType
547 {
550 KdfSecretAppend = 2,
551 KdfHmacKey = 3,
552 KdfTlsLabel = 4,
553 KdfTlsSeed = 5,
554 PkcsAlgOid = 41,
555 PkcsAlgParam = 42,
556 PkcsSecret = 46
557 }
558
559 internal struct NCryptBuffer
560 {
561 public int cbBuffer;
562
564
566 }
567
568 internal struct NCryptBufferDesc
569 {
570 public int ulVersion;
571
572 public int cBuffers;
573
575 }
576
578 {
579 None = 0,
584 }
585
586 internal enum ErrorCode
587 {
588 ERROR_SUCCESS = 0,
589 NTE_BAD_SIGNATURE = -2146893818,
590 NTE_NOT_FOUND = -2146893807,
591 NTE_BAD_KEYSET = -2146893802,
592 NTE_INVALID_PARAMETER = -2146893785,
593 NTE_BUFFER_TOO_SMALL = -2146893784,
594 NTE_NOT_SUPPORTED = -2146893783,
595 NTE_NO_MORE_ITEMS = -2146893782,
596 E_FAIL = -2147467259,
597 STATUS_UNSUCCESSFUL = -1073741823
598 }
599
601 {
602 internal int iIterations;
603
604 internal int cbSalt;
605 }
606
607 internal struct PBE_PARAMS
608 {
610
611 internal unsafe fixed byte rgbSalt[8];
612 }
613
614 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
615 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);
616
617 private unsafe static byte[] DeriveKeyMaterial(SafeNCryptSecretHandle secretAgreement, string kdf, string hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
618 {
619 IntPtr intPtr = IntPtr.Zero;
620 try
621 {
622 intPtr = Marshal.StringToCoTaskMemUni(hashAlgorithm);
623 Span<NCryptBuffer> span = stackalloc NCryptBuffer[4];
624 int num = 0;
625 NCryptBuffer nCryptBuffer = default(NCryptBuffer);
626 nCryptBuffer.cbBuffer = (hashAlgorithm.Length + 1) * 2;
627 nCryptBuffer.BufferType = BufferType.KdfHashAlgorithm;
628 nCryptBuffer.pvBuffer = intPtr;
629 span[num] = nCryptBuffer;
630 num++;
631 fixed (byte* ptr = hmacKey)
632 {
633 fixed (byte* ptr2 = secretPrepend)
634 {
635 fixed (byte* ptr3 = secretAppend)
636 {
637 if (ptr != null)
638 {
639 NCryptBuffer nCryptBuffer2 = default(NCryptBuffer);
640 nCryptBuffer2.cbBuffer = hmacKey.Length;
641 nCryptBuffer2.BufferType = BufferType.KdfHmacKey;
642 nCryptBuffer2.pvBuffer = new IntPtr(ptr);
643 span[num] = nCryptBuffer2;
644 num++;
645 }
646 if (ptr2 != null)
647 {
648 NCryptBuffer nCryptBuffer3 = default(NCryptBuffer);
649 nCryptBuffer3.cbBuffer = secretPrepend.Length;
650 nCryptBuffer3.BufferType = BufferType.KdfSecretPrepend;
651 nCryptBuffer3.pvBuffer = new IntPtr(ptr2);
652 span[num] = nCryptBuffer3;
653 num++;
654 }
655 if (ptr3 != null)
656 {
657 NCryptBuffer nCryptBuffer4 = default(NCryptBuffer);
658 nCryptBuffer4.cbBuffer = secretAppend.Length;
659 nCryptBuffer4.BufferType = BufferType.KdfSecretAppend;
660 nCryptBuffer4.pvBuffer = new IntPtr(ptr3);
661 span[num] = nCryptBuffer4;
662 num++;
663 }
664 return DeriveKeyMaterial(secretAgreement, kdf, span.Slice(0, num), flags);
665 }
666 }
667 }
668 }
669 finally
670 {
671 if (intPtr != IntPtr.Zero)
672 {
673 Marshal.FreeCoTaskMem(intPtr);
674 }
675 }
676 }
677
678 private unsafe static byte[] DeriveKeyMaterial(SafeNCryptSecretHandle secretAgreement, string kdf, ReadOnlySpan<NCryptBuffer> parameters, SecretAgreementFlags flags)
679 {
680 fixed (NCryptBuffer* value = &MemoryMarshal.GetReference(parameters))
681 {
682 NCryptBufferDesc pParameterList = default(NCryptBufferDesc);
683 pParameterList.ulVersion = 0;
684 pParameterList.cBuffers = parameters.Length;
685 pParameterList.pBuffers = new IntPtr(value);
686 ErrorCode errorCode = NCryptDeriveKey(secretAgreement, kdf, ref pParameterList, null, 0, out var pcbResult, flags);
687 if (errorCode != 0 && errorCode != ErrorCode.NTE_BUFFER_TOO_SMALL)
688 {
689 throw errorCode.ToCryptographicException();
690 }
691 byte[] array = new byte[pcbResult];
692 errorCode = NCryptDeriveKey(secretAgreement, kdf, ref pParameterList, array, array.Length, out pcbResult, flags);
693 if (errorCode != 0)
694 {
695 throw errorCode.ToCryptographicException();
696 }
697 Array.Resize(ref array, Math.Min(pcbResult, array.Length));
698 return array;
699 }
700 }
701
702 internal static byte[] DeriveKeyMaterialHash(SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
703 {
704 return DeriveKeyMaterial(secretAgreement, "HASH", hashAlgorithm, null, secretPrepend, secretAppend, flags);
705 }
706
707 internal static byte[] DeriveKeyMaterialHmac(SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
708 {
709 return DeriveKeyMaterial(secretAgreement, "HMAC", hashAlgorithm, hmacKey, secretPrepend, secretAppend, flags);
710 }
711
712 internal unsafe static byte[] DeriveKeyMaterialTls(SafeNCryptSecretHandle secretAgreement, byte[] label, byte[] seed, SecretAgreementFlags flags)
713 {
714 Span<NCryptBuffer> span = stackalloc NCryptBuffer[2];
715 fixed (byte* value = label)
716 {
717 fixed (byte* value2 = seed)
718 {
719 NCryptBuffer nCryptBuffer = default(NCryptBuffer);
720 nCryptBuffer.cbBuffer = label.Length;
721 nCryptBuffer.BufferType = BufferType.KdfTlsLabel;
722 nCryptBuffer.pvBuffer = new IntPtr(value);
723 span[0] = nCryptBuffer;
724 NCryptBuffer nCryptBuffer2 = default(NCryptBuffer);
725 nCryptBuffer2.cbBuffer = seed.Length;
726 nCryptBuffer2.BufferType = BufferType.KdfTlsSeed;
727 nCryptBuffer2.pvBuffer = new IntPtr(value2);
728 span[1] = nCryptBuffer2;
729 return DeriveKeyMaterial(secretAgreement, "TLS_PRF", span, flags);
730 }
731 }
732 }
733
734 [DllImport("ncrypt.dll")]
735 private static extern ErrorCode NCryptSecretAgreement(SafeNCryptKeyHandle hPrivKey, SafeNCryptKeyHandle hPubKey, out SafeNCryptSecretHandle phSecret, int dwFlags);
736
738 {
739 SafeNCryptSecretHandle phSecret;
740 ErrorCode errorCode = NCryptSecretAgreement(privateKey, otherPartyPublicKey, out phSecret, 0);
741 if (errorCode != 0)
742 {
743 throw errorCode.ToCryptographicException();
744 }
745 return phSecret;
746 }
747
748 internal unsafe static ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan<byte> pbInput, int cbInput, void* pPaddingInfo, Span<byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
749 {
750 return NCryptEncrypt(hKey, ref MemoryMarshal.GetReference(pbInput), cbInput, pPaddingInfo, ref MemoryMarshal.GetReference(pbOutput), cbOutput, out pcbResult, dwFlags);
751 }
752
753 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
754 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);
755
756 internal unsafe static ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan<byte> pbInput, int cbInput, void* pPaddingInfo, Span<byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags)
757 {
758 return NCryptDecrypt(hKey, ref MemoryMarshal.GetReference(pbInput), cbInput, pPaddingInfo, ref MemoryMarshal.GetReference(pbOutput), cbOutput, out pcbResult, dwFlags);
759 }
760
761 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
762 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);
763
764 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
765 internal static extern ErrorCode NCryptImportKey(SafeNCryptProviderHandle hProvider, IntPtr hImportKey, string pszBlobType, IntPtr pParameterList, out SafeNCryptKeyHandle phKey, ref byte pbData, int cbData, int dwFlags);
766
767 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
768 internal static extern ErrorCode NCryptImportKey(SafeNCryptProviderHandle hProvider, IntPtr hImportKey, string pszBlobType, ref NCryptBufferDesc pParameterList, out SafeNCryptKeyHandle phKey, ref byte pbData, int cbData, int dwFlags);
769
770 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
771 internal static extern ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, [Out] byte[] pbOutput, int cbOutput, out int pcbResult, int dwFlags);
772
773 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
774 internal static extern ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, IntPtr pParameterList, ref byte pbOutput, int cbOutput, out int pcbResult, int dwFlags);
775
776 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
777 internal static extern ErrorCode NCryptExportKey(SafeNCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, ref NCryptBufferDesc pParameterList, ref byte pbOutput, int cbOutput, out int pcbResult, int dwFlags);
778
779 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
780 internal static extern ErrorCode NCryptCreatePersistedKey(SafeNCryptProviderHandle hProvider, out SafeNCryptKeyHandle phKey, string pszAlgId, string pszKeyName, int dwLegacyKeySpec, CngKeyCreationOptions dwFlags);
781
782 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
783 internal static extern ErrorCode NCryptFinalizeKey(SafeNCryptKeyHandle hKey, int dwFlags);
784
785 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
786 internal static extern ErrorCode NCryptFreeObject(IntPtr hObject);
787
788 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
789 internal static extern ErrorCode NCryptOpenStorageProvider(out SafeNCryptProviderHandle phProvider, string pszProviderName, int dwFlags);
790
791 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
792 internal unsafe static extern ErrorCode NCryptGetProperty(SafeNCryptHandle hObject, string pszProperty, [Out] void* pbOutput, int cbOutput, out int pcbResult, CngPropertyOptions dwFlags);
793
794 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
795 internal unsafe static extern ErrorCode NCryptSetProperty(SafeNCryptHandle hObject, string pszProperty, [In] void* pbInput, int cbInput, CngPropertyOptions dwFlags);
796
797 internal unsafe static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string pszProperty, ref int result)
798 {
799 fixed (int* pbOutput = &result)
800 {
801 int pcbResult;
802 return NCryptGetProperty(hObject, pszProperty, pbOutput, 4, out pcbResult, CngPropertyOptions.None);
803 }
804 }
805
806 internal unsafe static ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan<byte> pbHashValue, int cbHashValue, Span<byte> pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags)
807 {
808 return NCryptSignHash(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, out pcbResult, dwFlags);
809 }
810
811 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
812 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);
813
814 internal unsafe static ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan<byte> pbHashValue, int cbHashValue, ReadOnlySpan<byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags)
815 {
816 return NCryptVerifySignature(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, dwFlags);
817 }
818
819 [DllImport("ncrypt.dll", CharSet = CharSet.Unicode)]
820 private unsafe static extern ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);
821 }
822
823 internal static class Crypt32
824 {
825 internal struct CRYPT_OID_INFO
826 {
827 public int cbSize;
828
830
832
834
835 public int AlgId;
836
837 public int cbData;
838
840
842 }
843
853
854 internal static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
855 {
856 IntPtr intPtr = IntPtr.Zero;
857 try
858 {
859 intPtr = keyType switch
860 {
861 CryptOidInfoKeyType.CRYPT_OID_INFO_OID_KEY => Marshal.StringToCoTaskMemAnsi(key),
862 CryptOidInfoKeyType.CRYPT_OID_INFO_NAME_KEY => Marshal.StringToCoTaskMemUni(key),
863 _ => throw new NotSupportedException(),
864 };
866 {
867 OidGroup group2 = group | (OidGroup)(-2147483648);
868 IntPtr intPtr2 = CryptFindOIDInfo(keyType, intPtr, group2);
869 if (intPtr2 != IntPtr.Zero)
870 {
871 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr2);
872 }
873 }
874 IntPtr intPtr3 = CryptFindOIDInfo(keyType, intPtr, group);
875 if (intPtr3 != IntPtr.Zero)
876 {
877 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr3);
878 }
879 if (fallBackToAllGroups && group != 0)
880 {
881 IntPtr intPtr4 = CryptFindOIDInfo(keyType, intPtr, OidGroup.All);
882 if (intPtr4 != IntPtr.Zero)
883 {
884 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr4);
885 }
886 }
887 CRYPT_OID_INFO result = default(CRYPT_OID_INFO);
888 result.AlgId = -1;
889 return result;
890 }
891 finally
892 {
893 if (intPtr != IntPtr.Zero)
894 {
895 Marshal.FreeCoTaskMem(intPtr);
896 }
897 }
898 }
899
901 {
902 if (group != OidGroup.HashAlgorithm && group != OidGroup.EncryptionAlgorithm && group != OidGroup.PublicKeyAlgorithm && group != OidGroup.SignatureAlgorithm && group != OidGroup.Attribute && group != OidGroup.ExtensionOrAttribute)
903 {
904 return group == OidGroup.KeyDerivationFunction;
905 }
906 return true;
907 }
908
909 [DllImport("crypt32.dll", CharSet = CharSet.Unicode)]
910 private static extern IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group);
911
912 [DllImport("crypt32.dll", CharSet = CharSet.Unicode)]
913 private static extern IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, ref int pvKey, OidGroup group);
914
916 {
917 int pvKey = (int)algId;
918 IntPtr intPtr = CryptFindOIDInfo(CryptOidInfoKeyType.CRYPT_OID_INFO_ALGID_KEY, ref pvKey, OidGroup.HashAlgorithm);
919 if (intPtr != IntPtr.Zero)
920 {
921 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr);
922 }
923 CRYPT_OID_INFO result = default(CRYPT_OID_INFO);
924 result.AlgId = -1;
925 return result;
926 }
927 }
928
929 internal static class Kernel32
930 {
931 [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", ExactSpelling = true, SetLastError = true)]
932 private unsafe static extern int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void* lpBuffer, int nSize, IntPtr arguments);
933
934 internal static string GetMessage(int errorCode)
935 {
936 return GetMessage(errorCode, IntPtr.Zero);
937 }
938
939 internal unsafe static string GetMessage(int errorCode, IntPtr moduleHandle)
940 {
941 int num = 12800;
942 if (moduleHandle != IntPtr.Zero)
943 {
944 num |= 0x800;
945 }
946 Span<char> span = stackalloc char[256];
947 fixed (char* lpBuffer = span)
948 {
949 int num2 = FormatMessage(num, moduleHandle, (uint)errorCode, 0, lpBuffer, span.Length, IntPtr.Zero);
950 if (num2 > 0)
951 {
952 return GetAndTrimString(span.Slice(0, num2));
953 }
954 }
955 if (Marshal.GetLastWin32Error() == 122)
956 {
957 IntPtr intPtr = default(IntPtr);
958 try
959 {
960 int num3 = FormatMessage(num | 0x100, moduleHandle, (uint)errorCode, 0, &intPtr, 0, IntPtr.Zero);
961 if (num3 > 0)
962 {
963 return GetAndTrimString(new Span<char>((void*)intPtr, num3));
964 }
965 }
966 finally
967 {
968 Marshal.FreeHGlobal(intPtr);
969 }
970 }
971 return $"Unknown error (0x{errorCode:x})";
972 }
973
974 private static string GetAndTrimString(Span<char> buffer)
975 {
976 int num = buffer.Length;
977 while (num > 0 && buffer[num - 1] <= ' ')
978 {
979 num--;
980 }
981 return buffer.Slice(0, num).ToString();
982 }
983 }
984}
static SafeBCryptAlgorithmHandle GetCachedBCryptAlgorithmHandle(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, out int hashSizeInBytes)
Definition Interop.cs:330
static volatile Entry[] _cache
Definition Interop.cs:328
static NTSTATUS BCryptDestroyHash(IntPtr hHash)
static unsafe NTSTATUS BCryptGenerateSymmetricKey(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptKeyHandle phKey, IntPtr pbKeyObject, int cbKeyObject, byte *pbSecret, int cbSecret, uint dwFlags)
static unsafe int BCryptEncrypt(SafeKeyHandle hKey, ReadOnlySpan< byte > input, byte[] iv, Span< byte > output)
Definition Interop.cs:386
static Exception CreateCryptographicException(NTSTATUS ntStatus)
Definition Interop.cs:461
static NTSTATUS BCryptDuplicateHash(SafeBCryptHashHandle hHash, out SafeBCryptHashHandle phNewHash, IntPtr pbHashObject, int cbHashObject, int dwFlags)
static NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ref byte pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags)
static void EmitBigEndian(byte[] blob, ref int offset, int value)
Definition Interop.cs:445
static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, ref byte pbOutput, int cbOutput, int dwFlags)
static bool PseudoHandlesSupported
Definition Interop.cs:358
static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ref 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:435
static unsafe SafeKeyHandle BCryptImportKey(SafeAlgorithmHandle hAlg, ReadOnlySpan< byte > key)
Definition Interop.cs:361
static NTSTATUS BCryptDestroyKey(IntPtr hKey)
static NTSTATUS BCryptImportKey(SafeAlgorithmHandle hAlgorithm, IntPtr hImportKey, string pszBlobType, out SafeKeyHandle hKey, IntPtr pbKeyObject, int cbKeyObject, byte[] pbInput, int cbInput, int dwFlags)
static unsafe NTSTATUS BCryptEncrypt(SafeKeyHandle hKey, byte *pbInput, int cbInput, IntPtr paddingInfo, [In][Out] byte[] pbIV, int cbIV, byte *pbOutput, int cbOutput, out int cbResult, int dwFlags)
static unsafe NTSTATUS BCryptHash(nuint hAlgorithm, byte *pbSecret, int cbSecret, byte *pbInput, int cbInput, byte *pbOutput, int cbOutput)
static NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ReadOnlySpan< byte > secret, int cbSecret, BCryptCreateHashFlags dwFlags)
Definition Interop.cs:473
static byte[] Consume(byte[] blob, ref int offset, int count)
Definition Interop.cs:453
static unsafe NTSTATUS BCryptKeyDerivation(SafeBCryptKeyHandle hKey, BCryptBufferDesc *pParameterList, byte *pbDerivedKey, int cbDerivedKey, out uint pcbResult, int dwFlags)
static unsafe NTSTATUS BCryptGenerateSymmetricKey(nuint hAlgorithm, out SafeBCryptKeyHandle phKey, IntPtr pbKeyObject, int cbKeyObject, byte *pbSecret, int cbSecret, uint dwFlags)
static SafeBCryptHashHandle BCryptDuplicateHash(SafeBCryptHashHandle hHash)
Definition Interop.cs:484
static NTSTATUS BCryptOpenAlgorithmProvider(out SafeBCryptAlgorithmHandle phAlgorithm, string pszAlgId, string pszImplementation, BCryptOpenAlgorithmProviderFlags dwFlags)
static unsafe int BCryptDecrypt(SafeKeyHandle hKey, ReadOnlySpan< byte > input, byte[] iv, Span< byte > output)
Definition Interop.cs:403
static unsafe NTSTATUS BCryptDecrypt(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 BCryptFinishHash(SafeBCryptHashHandle hHash, Span< byte > pbOutput, int cbOutput, int dwFlags)
Definition Interop.cs:516
static unsafe NTSTATUS BCryptDeriveKeyPBKDF2(SafeBCryptAlgorithmHandle hPrf, byte *pbPassword, int cbPassword, byte *pbSalt, int cbSalt, ulong cIterations, byte *pbDerivedKey, int cbDerivedKey, uint dwFlags)
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 unsafe NTSTATUS BCryptGenRandom(IntPtr hAlgorithm, byte *pbBuffer, int cbBuffer, int dwFlags)
static void Emit(byte[] blob, ref int offset, byte[] value)
Definition Interop.cs:429
static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
Definition Interop.cs:854
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:915
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
Definition Interop.cs:939
static void SetLastError(int errorCode)
static string GetMessage(int errorCode)
Definition Interop.cs:934
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:756
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:797
static byte[] DeriveKeyMaterialHmac(SafeNCryptSecretHandle secretAgreement, string hashAlgorithm, byte[] hmacKey, byte[] secretPrepend, byte[] secretAppend, SecretAgreementFlags flags)
Definition Interop.cs:707
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:617
static unsafe byte[] DeriveKeyMaterialTls(SafeNCryptSecretHandle secretAgreement, byte[] label, byte[] seed, SecretAgreementFlags flags)
Definition Interop.cs:712
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:702
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:748
static SafeNCryptSecretHandle DeriveSecretAgreement(SafeNCryptKeyHandle privateKey, SafeNCryptKeyHandle otherPartyPublicKey)
Definition Interop.cs:737
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:814
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:806
static unsafe byte[] DeriveKeyMaterial(SafeNCryptSecretHandle secretAgreement, string kdf, ReadOnlySpan< NCryptBuffer > parameters, SecretAgreementFlags flags)
Definition Interop.cs:678
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 bool IsWindowsVersionAtLeast(int major, int minor=0, int build=0, int revision=0)
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
static unsafe BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO Create()
Definition Interop.cs:261
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
SafeBCryptAlgorithmHandle Handle
Definition Interop.cs:307
unsafe Entry(string hashAlgorithmId, BCryptOpenAlgorithmProviderFlags flags, SafeBCryptAlgorithmHandle handle)
Definition Interop.cs:311
BCryptOpenAlgorithmProviderFlags Flags
Definition Interop.cs:305
CngBufferDescriptors BufferType
Definition Interop.cs:202
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