5 #define CONFIG "config.h"
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
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
34 static HCRYPTPROV hRsaAesProvider
= 0; // Needs to be initialized just once per process
38 static int_fast8_t AcquireCryptContext()
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
56 int_fast8_t Sha256(BYTE
* restrict data
, DWORD DataSize
, BYTE
* restrict hash
)
62 AcquireCryptContext() &&
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
77 DataSize
, // size of data
78 NULLFLAGS
// Use default flags
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
);
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
101 HMAC_KEYBLOB hmackeyblob
= {
102 // Type, Version, Algorithm
103 { PLAINTEXTKEYBLOB
, CUR_BLOB_VERSION
, 0, CALG_RC2
},
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
));
117 AcquireCryptContext() &&
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)
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)
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
148 hHmacHash
, // hash handle
149 data
, // Pointer to data you want to hash
151 NULLFLAGS
// default flags
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
);
169 #endif // _WIN32 || __CYGWIN__
170 #endif // _CRYPTO_WINDOWS