summaryrefslogtreecommitdiffstats
path: root/third_party/heimdal/lib/hcrypto/evp-w32.c
blob: ac2c6a53e4b07444d4b57aebcc81fd044b6af2cb (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (c) 2015, Secure Endpoints Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Windows fallback provider: decides whether to use hcrypto or
 * wincng depending on whether bcrypt.dll is available (i.e. it
 * is runtime compatible back to XP, but will use the native
 * crypto APIs from Vista onwards).
 */

#include <config.h>
#include <roken.h>

#include <assert.h>

#include <evp.h>
#include <evp-w32.h>
#include <evp-hcrypto.h>

#include <evp-wincng.h>

static LONG wincng_available = -1;

static __inline int
wincng_check_availability(void)
{
    if (wincng_available == -1) {
	char szBCryptDllPath[MAX_PATH];
	UINT cbBCryptDllPath;

	cbBCryptDllPath = GetSystemDirectory(szBCryptDllPath,
					     sizeof(szBCryptDllPath));
	if (cbBCryptDllPath > 0 &&
	    cbBCryptDllPath < sizeof(szBCryptDllPath) &&
	    strncat_s(szBCryptDllPath,
		      sizeof(szBCryptDllPath), "\\bcrypt.dll", 11) == 0) {
	    HANDLE hBCryptDll = LoadLibrary(szBCryptDllPath);

	    InterlockedCompareExchangeRelease(&wincng_available,
					      !!hBCryptDll, -1);
	    if (hBCryptDll)
		FreeLibrary(hBCryptDll);
	}
    }

    return wincng_available == 1;
}

BOOL WINAPI
_hc_w32crypto_DllMain(HINSTANCE hinstDLL,
		      DWORD fdwReason,
		      LPVOID lpvReserved)
{
    if (fdwReason == DLL_PROCESS_DETACH) {
	/*
	 * Don't bother cleaning up on process exit, only on
	 * FreeLibrary() (in which case lpvReserved will be NULL).
	 */
	if (lpvReserved == NULL)
	    _hc_wincng_cleanup();
    }

    return TRUE;
}

#define EVP_W32CRYPTO_PROVIDER(type, name)		    \
							    \
    const type *hc_EVP_w32crypto_ ##name (void)		    \
    {							    \
	if (wincng_check_availability())		    \
	    return hc_EVP_wincng_ ##name ();		    \
	else if (HCRYPTO_FALLBACK)			    \
	    return hc_EVP_hcrypto_ ##name ();		    \
	else						    \
	    return NULL;				    \
    }

#define EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(type, name)  \
							    \
    const type *hc_EVP_w32crypto_ ##name (void)		    \
    {							    \
	return hc_EVP_hcrypto_ ##name ();		    \
    }

EVP_W32CRYPTO_PROVIDER(EVP_MD, md4)
EVP_W32CRYPTO_PROVIDER(EVP_MD, md5)
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha1)
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha256)
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha384)
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha512)

EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_cbc)
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_40_cbc)
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_64_cbc)

EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc4)
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc4_40)

EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, des_cbc)
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, des_ede3_cbc)

EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_128_cbc)
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_192_cbc)
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_256_cbc)

EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_128_cfb8)
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_192_cfb8)
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_256_cfb8)

EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_128_cbc)
EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_192_cbc)
EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_256_cbc)