20 #include <winpr/config.h>
22 #include <winpr/crypto.h>
141 #include <winpr/crt.h>
142 #include <winpr/collections.h>
144 static wListDictionary* g_ProtectedMemoryBlocks = NULL;
146 BOOL CryptProtectMemory(LPVOID pData, DWORD cbData, DWORD dwFlags)
148 BYTE* pCipherText = NULL;
151 WINPR_CIPHER_CTX* enc = NULL;
152 BYTE randomKey[256] = { 0 };
155 if (dwFlags != CRYPTPROTECTMEMORY_SAME_PROCESS)
158 if (!g_ProtectedMemoryBlocks)
160 g_ProtectedMemoryBlocks = ListDictionary_New(TRUE);
162 if (!g_ProtectedMemoryBlocks)
171 pMemBlock->pData = pData;
172 pMemBlock->cbData = cbData;
173 pMemBlock->dwFlags = dwFlags;
175 winpr_RAND(pMemBlock->salt, 8);
176 winpr_RAND(randomKey,
sizeof(randomKey));
178 winpr_Cipher_BytesToKey(WINPR_CIPHER_AES_256_CBC, WINPR_MD_SHA1, pMemBlock->salt, randomKey,
179 sizeof(randomKey), 4, pMemBlock->key, pMemBlock->iv);
181 SecureZeroMemory(randomKey,
sizeof(randomKey));
183 cbOut = pMemBlock->cbData + 16 - 1;
184 pCipherText = (BYTE*)calloc(1, cbOut);
189 if ((enc = winpr_Cipher_NewEx(WINPR_CIPHER_AES_256_CBC, WINPR_ENCRYPT, pMemBlock->key,
190 sizeof(pMemBlock->key), pMemBlock->iv,
sizeof(pMemBlock->iv))) ==
193 if (!winpr_Cipher_Update(enc, pMemBlock->pData, pMemBlock->cbData, pCipherText, &cbOut))
195 if (!winpr_Cipher_Final(enc, pCipherText + cbOut, &cbFinal))
197 winpr_Cipher_Free(enc);
199 CopyMemory(pMemBlock->pData, pCipherText, pMemBlock->cbData);
202 return ListDictionary_Add(g_ProtectedMemoryBlocks, pData, pMemBlock);
206 winpr_Cipher_Free(enc);
211 BOOL CryptUnprotectMemory(LPVOID pData, DWORD cbData, DWORD dwFlags)
213 BYTE* pPlainText = NULL;
216 WINPR_CIPHER_CTX* dec = NULL;
219 if (dwFlags != CRYPTPROTECTMEMORY_SAME_PROCESS)
222 if (!g_ProtectedMemoryBlocks)
231 cbOut = pMemBlock->cbData + 16 - 1;
233 pPlainText = (BYTE*)malloc(cbOut);
238 if ((dec = winpr_Cipher_NewEx(WINPR_CIPHER_AES_256_CBC, WINPR_DECRYPT, pMemBlock->key,
239 sizeof(pMemBlock->key), pMemBlock->iv,
sizeof(pMemBlock->iv))) ==
242 if (!winpr_Cipher_Update(dec, pMemBlock->pData, pMemBlock->cbData, pPlainText, &cbOut))
244 if (!winpr_Cipher_Final(dec, pPlainText + cbOut, &cbFinal))
246 winpr_Cipher_Free(dec);
248 CopyMemory(pMemBlock->pData, pPlainText, pMemBlock->cbData);
249 SecureZeroMemory(pPlainText, pMemBlock->cbData);
252 ListDictionary_Remove(g_ProtectedMemoryBlocks, pData);
261 winpr_Cipher_Free(dec);
265 BOOL CryptProtectData(
DATA_BLOB* pDataIn, LPCWSTR szDataDescr,
DATA_BLOB* pOptionalEntropy,
272 BOOL CryptUnprotectData(
DATA_BLOB* pDataIn, LPWSTR* ppszDataDescr,
DATA_BLOB* pOptionalEntropy,
279 BOOL CryptStringToBinaryW(LPCWSTR pszString, DWORD cchString, DWORD dwFlags, BYTE* pbBinary,
280 DWORD* pcbBinary, DWORD* pdwSkip, DWORD* pdwFlags)
285 BOOL CryptStringToBinaryA(LPCSTR pszString, DWORD cchString, DWORD dwFlags, BYTE* pbBinary,
286 DWORD* pcbBinary, DWORD* pdwSkip, DWORD* pdwFlags)
291 BOOL CryptBinaryToStringW(CONST BYTE* pbBinary, DWORD cbBinary, DWORD dwFlags, LPWSTR pszString,
297 BOOL CryptBinaryToStringA(CONST BYTE* pbBinary, DWORD cbBinary, DWORD dwFlags, LPSTR pszString,