crypt.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2014 Antony Polukhin
  3. * Copyright 2015 Andrey Semashev
  4. *
  5. * Distributed under the Boost Software License, Version 1.0.
  6. * See http://www.boost.org/LICENSE_1_0.txt
  7. */
  8. #ifndef BOOST_WINAPI_CRYPT_HPP_INCLUDED_
  9. #define BOOST_WINAPI_CRYPT_HPP_INCLUDED_
  10. #include <boost/winapi/basic_types.hpp>
  11. #if defined( BOOST_USE_WINDOWS_H )
  12. // This header is not always included as part of windows.h
  13. #include <wincrypt.h>
  14. #endif
  15. #ifdef BOOST_HAS_PRAGMA_ONCE
  16. #pragma once
  17. #endif
  18. #if !defined( BOOST_USE_WINDOWS_H )
  19. namespace boost { namespace winapi {
  20. typedef ULONG_PTR_ HCRYPTPROV_;
  21. }}
  22. extern "C" {
  23. #if BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  24. #if !defined( BOOST_NO_ANSI_APIS )
  25. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  26. CryptEnumProvidersA(
  27. boost::winapi::DWORD_ dwIndex,
  28. boost::winapi::DWORD_ *pdwReserved,
  29. boost::winapi::DWORD_ dwFlags,
  30. boost::winapi::DWORD_ *pdwProvType,
  31. boost::winapi::LPSTR_ szProvName,
  32. boost::winapi::DWORD_ *pcbProvName);
  33. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  34. CryptAcquireContextA(
  35. boost::winapi::HCRYPTPROV_ *phProv,
  36. boost::winapi::LPCSTR_ pszContainer,
  37. boost::winapi::LPCSTR_ pszProvider,
  38. boost::winapi::DWORD_ dwProvType,
  39. boost::winapi::DWORD_ dwFlags);
  40. #endif // !defined( BOOST_NO_ANSI_APIS )
  41. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  42. CryptEnumProvidersW(
  43. boost::winapi::DWORD_ dwIndex,
  44. boost::winapi::DWORD_ *pdwReserved,
  45. boost::winapi::DWORD_ dwFlags,
  46. boost::winapi::DWORD_ *pdwProvType,
  47. boost::winapi::LPWSTR_ szProvName,
  48. boost::winapi::DWORD_ *pcbProvName);
  49. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  50. CryptAcquireContextW(
  51. boost::winapi::HCRYPTPROV_ *phProv,
  52. boost::winapi::LPCWSTR_ szContainer,
  53. boost::winapi::LPCWSTR_ szProvider,
  54. boost::winapi::DWORD_ dwProvType,
  55. boost::winapi::DWORD_ dwFlags);
  56. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  57. CryptGenRandom(
  58. boost::winapi::HCRYPTPROV_ hProv,
  59. boost::winapi::DWORD_ dwLen,
  60. boost::winapi::BYTE_ *pbBuffer);
  61. #endif // BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  62. #if BOOST_WINAPI_PARTITION_APP_SYSTEM
  63. #if defined(_MSC_VER) && (_MSC_VER+0) >= 1500 && (_MSC_VER+0) < 1900 && BOOST_USE_NTDDI_VERSION < BOOST_WINAPI_NTDDI_WINXP
  64. // Standalone MS Windows SDK 6.0A and later until 10.0 provide a different declaration of CryptReleaseContext for Windows 2000 and older.
  65. // This is not the case for (a) MinGW and MinGW-w64, (b) MSVC 7.1 and 8, which are shipped with their own Windows SDK,
  66. // and (c) MSVC 14.0 and later, which are used with Windows SDK 10.
  67. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  68. CryptReleaseContext(
  69. boost::winapi::HCRYPTPROV_ hProv,
  70. boost::winapi::ULONG_PTR_ dwFlags);
  71. #else
  72. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  73. CryptReleaseContext(
  74. boost::winapi::HCRYPTPROV_ hProv,
  75. boost::winapi::DWORD_ dwFlags);
  76. #endif
  77. #endif // BOOST_WINAPI_PARTITION_APP_SYSTEM
  78. }
  79. #endif // !defined( BOOST_USE_WINDOWS_H )
  80. namespace boost {
  81. namespace winapi {
  82. #if defined( BOOST_USE_WINDOWS_H )
  83. typedef ::HCRYPTPROV HCRYPTPROV_;
  84. #if BOOST_WINAPI_PARTITION_APP_SYSTEM
  85. BOOST_CONSTEXPR_OR_CONST DWORD_ PROV_RSA_FULL_ = PROV_RSA_FULL;
  86. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_VERIFYCONTEXT_ = CRYPT_VERIFYCONTEXT;
  87. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_NEWKEYSET_ = CRYPT_NEWKEYSET;
  88. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_DELETEKEYSET_ = CRYPT_DELETEKEYSET;
  89. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_MACHINE_KEYSET_ = CRYPT_MACHINE_KEYSET;
  90. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_SILENT_ = CRYPT_SILENT;
  91. #endif
  92. #else
  93. #if BOOST_WINAPI_PARTITION_APP_SYSTEM
  94. BOOST_CONSTEXPR_OR_CONST DWORD_ PROV_RSA_FULL_ = 1;
  95. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_VERIFYCONTEXT_ = 0xF0000000;
  96. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_NEWKEYSET_ = 8;
  97. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_DELETEKEYSET_ = 16;
  98. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_MACHINE_KEYSET_ = 32;
  99. BOOST_CONSTEXPR_OR_CONST DWORD_ CRYPT_SILENT_ = 64;
  100. #endif
  101. #endif
  102. #if BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  103. #if !defined( BOOST_NO_ANSI_APIS )
  104. using ::CryptEnumProvidersA;
  105. using ::CryptAcquireContextA;
  106. #endif
  107. using ::CryptEnumProvidersW;
  108. using ::CryptAcquireContextW;
  109. using ::CryptGenRandom;
  110. #if !defined( BOOST_NO_ANSI_APIS )
  111. BOOST_FORCEINLINE BOOL_ crypt_enum_providers(
  112. DWORD_ dwIndex,
  113. DWORD_ *pdwReserved,
  114. DWORD_ dwFlags,
  115. DWORD_ *pdwProvType,
  116. LPSTR_ szProvName,
  117. DWORD_ *pcbProvName)
  118. {
  119. return ::CryptEnumProvidersA(dwIndex, pdwReserved, dwFlags, pdwProvType, szProvName, pcbProvName);
  120. }
  121. BOOST_FORCEINLINE BOOL_ crypt_acquire_context(
  122. HCRYPTPROV_ *phProv,
  123. LPCSTR_ pszContainer,
  124. LPCSTR_ pszProvider,
  125. DWORD_ dwProvType,
  126. DWORD_ dwFlags)
  127. {
  128. return ::CryptAcquireContextA(phProv, pszContainer, pszProvider, dwProvType, dwFlags);
  129. }
  130. #endif
  131. BOOST_FORCEINLINE BOOL_ crypt_enum_providers(
  132. DWORD_ dwIndex,
  133. DWORD_ *pdwReserved,
  134. DWORD_ dwFlags,
  135. DWORD_ *pdwProvType,
  136. LPWSTR_ szProvName,
  137. DWORD_ *pcbProvName)
  138. {
  139. return ::CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, szProvName, pcbProvName);
  140. }
  141. BOOST_FORCEINLINE BOOL_ crypt_acquire_context(
  142. HCRYPTPROV_ *phProv,
  143. LPCWSTR_ szContainer,
  144. LPCWSTR_ szProvider,
  145. DWORD_ dwProvType,
  146. DWORD_ dwFlags)
  147. {
  148. return ::CryptAcquireContextW(phProv, szContainer, szProvider, dwProvType, dwFlags);
  149. }
  150. #endif // BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  151. #if BOOST_WINAPI_PARTITION_APP_SYSTEM
  152. BOOST_FORCEINLINE BOOL_ CryptReleaseContext(HCRYPTPROV_ hProv, DWORD_ dwFlags)
  153. {
  154. return ::CryptReleaseContext(hProv, dwFlags);
  155. }
  156. #endif
  157. }
  158. }
  159. #endif // BOOST_WINAPI_CRYPT_HPP_INCLUDED_