More robust kadm5 server handle init and cleanup
[heimdal.git] / lib / hcrypto / evp-w32.c
blob7d14d1f4f30979265ed64771ca28865d43b04894
1 /*
2 * Copyright (c) 2015, Secure Endpoints Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Windows fallback provider: decides whether to use hcrypto or
33 * wincng depending on whether bcrypt.dll is available (i.e. it
34 * is runtime compatible back to XP, but will use the native
35 * crypto APIs from Vista onwards).
38 #include <config.h>
39 #include <roken.h>
41 #include <assert.h>
43 #include <evp.h>
44 #include <evp-w32.h>
45 #include <evp-hcrypto.h>
47 #include <evp-wincng.h>
49 static LONG wincng_available = -1;
51 static __inline int
52 wincng_check_availability(void)
54 if (wincng_available == -1) {
55 char szBCryptDllPath[MAX_PATH];
56 UINT cbBCryptDllPath;
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);
66 InterlockedCompareExchangeRelease(&wincng_available,
67 !!hBCryptDll, -1);
68 if (hBCryptDll)
69 FreeLibrary(hBCryptDll);
73 return wincng_available == 1;
76 BOOL WINAPI
77 _hc_w32crypto_DllMain(HINSTANCE hinstDLL,
78 DWORD fdwReason,
79 LPVOID lpvReserved)
81 if (fdwReason == DLL_PROCESS_DETACH) {
83 * Don't bother cleaning up on process exit, only on
84 * FreeLibrary() (in which case lpvReserved will be NULL).
86 if (lpvReserved == NULL)
87 _hc_wincng_cleanup();
90 return TRUE;
93 #define EVP_W32CRYPTO_PROVIDER(type, name) \
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; \
105 #define EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(type, name) \
107 const type *hc_EVP_w32crypto_ ##name (void) \
109 return hc_EVP_hcrypto_ ##name (); \
112 EVP_W32CRYPTO_PROVIDER(EVP_MD, md2)
113 EVP_W32CRYPTO_PROVIDER(EVP_MD, md4)
114 EVP_W32CRYPTO_PROVIDER(EVP_MD, md5)
115 EVP_W32CRYPTO_PROVIDER(EVP_MD, sha1)
116 EVP_W32CRYPTO_PROVIDER(EVP_MD, sha256)
117 EVP_W32CRYPTO_PROVIDER(EVP_MD, sha384)
118 EVP_W32CRYPTO_PROVIDER(EVP_MD, sha512)
120 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_cbc)
121 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_40_cbc)
122 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_64_cbc)
124 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc4)
125 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc4_40)
127 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, des_cbc)
128 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, des_ede3_cbc)
130 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_128_cbc)
131 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_192_cbc)
132 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_256_cbc)
134 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_128_cfb8)
135 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_192_cfb8)
136 EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_256_cfb8)
138 EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_128_cbc)
139 EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_192_cbc)
140 EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_256_cbc)