modified: myjupyterlab.sh
[GalaxyCodeBases.git] / etc / Windows / vlmcsd_old_vancepym / crypto_windows.c
blobd844396921a2745e4e68b08ac6353b646d43e02f
1 /*
2 * crypto_windows.c
3 */
4 #ifndef CONFIG
5 #define CONFIG "config.h"
6 #endif // CONFIG
7 #include CONFIG
9 #ifdef _CRYPTO_WINDOWS
11 #if !_WIN32 && !__CYGWIN__
12 #error You cannot use Windows CryptoAPI on non-Windows platforms
13 #else // _WIN32 || __CYGWIN__
15 #include "crypto_windows.h"
18 typedef struct _HMAC_KEYBLOB
20 BLOBHEADER hdr;
21 DWORD dwKeySize;
22 BYTE KeyData[16];
23 } HMAC_KEYBLOB;
27 * MingW and Cygwin define NULL as ((void*)0) (Posix standard) which you can't assign to
28 * non-pointer types without compiler warning. Thus we use the following
30 #define NULLHANDLE 0
31 #define NULLFLAGS 0
34 static HCRYPTPROV hRsaAesProvider = 0; // Needs to be initialized just once per process
38 static int_fast8_t AcquireCryptContext()
40 if (!hRsaAesProvider)
42 return CryptAcquireContextW
44 &hRsaAesProvider, // Provider handle
45 NULL, // No key container name
46 NULL, // Default provider
47 PROV_RSA_AES, // Provides SHA and AES
48 CRYPT_VERIFYCONTEXT // We don't need access to persistent keys
52 return TRUE;
56 int_fast8_t Sha256(BYTE* restrict data, DWORD DataSize, BYTE* restrict hash)
58 HCRYPTHASH hHash = 0;
59 DWORD HashSize = 32;
61 int_fast8_t success =
62 AcquireCryptContext() &&
64 CryptCreateHash
66 hRsaAesProvider,// Provider handle
67 CALG_SHA_256, // Algorithm
68 NULLHANDLE, // SHA256 requires no key
69 NULLFLAGS, // Use default flags
70 &hHash // Handle for hashing
71 ) &&
73 CryptHashData
75 hHash, // Handle
76 data, // data to hash
77 DataSize, // size of data
78 NULLFLAGS // Use default flags
79 ) &&
81 CryptGetHashParam
83 hHash, // Handle
84 HP_HASHVAL, // what you actually want to get (the resulting hash)
85 hash, // data to retrieve
86 &HashSize, // size of data
87 NULLFLAGS // currently reserved (as of this writing)
90 if (hHash) CryptDestroyHash(hHash);
92 return success;
96 int_fast8_t Sha256Hmac(const BYTE* key, BYTE* restrict data, DWORD len, BYTE* restrict hmac)
98 # ifndef USE_THREADS // In fork() mode thread-safety is not required
99 static
100 # endif
101 HMAC_KEYBLOB hmackeyblob = {
102 // Type, Version, Algorithm
103 { PLAINTEXTKEYBLOB, CUR_BLOB_VERSION, 0, CALG_RC2 },
104 // Key length
108 HCRYPTKEY hKey = NULLHANDLE;
109 HCRYPTHASH hHmacHash = NULLHANDLE;
110 HMAC_INFO HmacInfo = { 0 };
111 DWORD dwHmacSize = 32;
113 HmacInfo.HashAlgid = CALG_SHA_256;
114 memcpy(hmackeyblob.KeyData, key, sizeof(hmackeyblob.KeyData));
116 BOOL success =
117 AcquireCryptContext() &&
119 CryptImportKey
121 hRsaAesProvider, // provider handle
122 (PBYTE)&hmackeyblob, // the actual key MS blob format
123 sizeof(HMAC_KEYBLOB), // size of the entire blob
124 NULLHANDLE, // password/key for the key store (none required here)
125 NULLFLAGS, // default flags
126 &hKey // key handle to retrieve (must be kept until you finish hashing)
127 ) &&
129 CryptCreateHash
131 hRsaAesProvider, // provider handle
132 CALG_HMAC, // the actual key MS blob format
133 hKey, // size of the entire blob
134 NULLFLAGS, // password/key for the key store (none required here)
135 &hHmacHash // default flags
136 ) && // key handle to retrieve (must be kept until you finish hashing)
138 CryptSetHashParam
140 hHmacHash, // hash handle
141 HP_HMAC_INFO, // parameter you want to set
142 (PBYTE)&HmacInfo, // the HMAC parameters (SHA256 with default ipad and opad)
143 NULLFLAGS // flags are reserved up to Windows 8.1
144 ) &&
146 CryptHashData
148 hHmacHash, // hash handle
149 data, // Pointer to data you want to hash
150 len, // data length
151 NULLFLAGS // default flags
152 ) &&
154 CryptGetHashParam
156 hHmacHash, // hash handle
157 HP_HASHVAL, // what you actually want to get (the resulting HMAC)
158 hmac, // data to retrieve
159 &dwHmacSize, // size of data
160 NULLFLAGS // currently reserved (as of this writing)
163 if (hKey) CryptDestroyKey(hKey);
164 if (hHmacHash) CryptDestroyHash(hHmacHash);
166 return success;
169 #endif // _WIN32 || __CYGWIN__
170 #endif // _CRYPTO_WINDOWS