| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#include <config.h> |
| 39 |
#include <roken.h> |
| 40 |
|
| 41 |
#include <assert.h> |
| 42 |
|
| 43 |
#include <evp.h> |
| 44 |
#include <evp-w32.h> |
| 45 |
#include <evp-hcrypto.h> |
| 46 |
|
| 47 |
#include <evp-wincng.h> |
| 48 |
|
| 49 |
static LONG wincng_available = -1; |
| 50 |
|
| 51 |
static __inline int |
| 52 |
wincng_check_availability(void) |
| 53 |
{ |
| 54 |
if (wincng_available == -1) { |
| 55 |
char szBCryptDllPath[MAX_PATH]; |
| 56 |
UINT cbBCryptDllPath; |
| 57 |
|
| 58 |
cbBCryptDllPath = GetSystemDirectory(szBCryptDllPath, |
| 59 |
sizeof(szBCryptDllPath)); |
| 60 |
if (cbBCryptDllPath > 0 && |
| 61 |
cbBCryptDllPath < sizeof(szBCryptDllPath) && |
| 62 |
strncat_s(szBCryptDllPath, |
| 63 |
sizeof(szBCryptDllPath), "\\bcrypt.dll", 11) == 0) { |
| 64 |
HANDLE hBCryptDll = LoadLibrary(szBCryptDllPath); |
| 65 |
|
| 66 |
InterlockedCompareExchangeRelease(&wincng_available, |
| 67 |
!!hBCryptDll, -1); |
| 68 |
if (hBCryptDll) |
| 69 |
FreeLibrary(hBCryptDll); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return wincng_available == 1; |
| 74 |
} |
| 75 |
|
| 76 |
BOOL WINAPI |
| 77 |
_hc_w32crypto_DllMain(HINSTANCE hinstDLL, |
| 78 |
DWORD fdwReason, |
| 79 |
LPVOID lpvReserved) |
| 80 |
{ |
| 81 |
if (fdwReason == DLL_PROCESS_DETACH) { |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if (lpvReserved == NULL) |
| 87 |
_hc_wincng_cleanup(); |
| 88 |
} |
| 89 |
|
| 90 |
return TRUE; |
| 91 |
} |
| 92 |
|
| 93 |
#define EVP_W32CRYPTO_PROVIDER(type, name) \ |
| 94 |
\ |
| 95 |
const type *hc_EVP_w32crypto_ ##name (void) \ |
| 96 |
{ \ |
| 97 |
if (wincng_check_availability()) \ |
| 98 |
return hc_EVP_wincng_ ##name (); \ |
| 99 |
else if (HCRYPTO_FALLBACK) \ |
| 100 |
return hc_EVP_hcrypto_ ##name (); \ |
| 101 |
else \ |
| 102 |
return NULL; \ |
| 103 |
} |
| 104 |
|
| 105 |
#define EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(type, name) \ |
| 106 |
\ |
| 107 |
const type *hc_EVP_w32crypto_ ##name (void) \ |
| 108 |
{ \ |
| 109 |
return hc_EVP_hcrypto_ ##name (); \ |
| 110 |
} |
| 111 |
|
| 112 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, md4) |
| 113 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, md5) |
| 114 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha1) |
| 115 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha256) |
| 116 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha384) |
| 117 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha512) |
| 118 |
|
| 119 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_cbc) |
| 120 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_40_cbc) |
| 121 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_64_cbc) |
| 122 |
|
| 123 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc4) |
| 124 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc4_40) |
| 125 |
|
| 126 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, des_cbc) |
| 127 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, des_ede3_cbc) |
| 128 |
|
| 129 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_128_cbc) |
| 130 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_192_cbc) |
| 131 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_256_cbc) |
| 132 |
|
| 133 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_128_cfb8) |
| 134 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_192_cfb8) |
| 135 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_256_cfb8) |
| 136 |
|
| 137 |
EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_128_cbc) |
| 138 |
EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_192_cbc) |
| 139 |
EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_256_cbc) |