summaryrefslogtreecommitdiff
path: root/plugins/Kuzne4ikCrypt/src/utils.cpp
blob: c7bf59fbfdaf8c0599881a16fab9e4dc2d313b58 (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
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (C) 2012-17 Miranda NG project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "stdafx.h"

bool getRandomBytes(BYTE *buf, size_t bufLen)
{
	// try to use Intel hardware randomizer first
	HCRYPTPROV hProvider = NULL;
	if (::CryptAcquireContext(&hProvider, NULL, L"Intel Hardware Cryptographic Service Provider", PROV_INTEL_SEC, 0) ||
		 ::CryptAcquireContext(&hProvider, NULL, MS_STRONG_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) ||
		 ::CryptAcquireContext(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
	{
		::CryptGenRandom(hProvider, DWORD(bufLen), buf);
		::CryptReleaseContext(hProvider, 0);
	}
	// no luck? try to use Windows NT RTL
	else {
		typedef BOOL(WINAPI *pfnGetRandom)(PVOID RandomBuffer, ULONG RandomBufferLength);
		pfnGetRandom fnGetRandom = (pfnGetRandom)GetProcAddress(GetModuleHandleA("advapi32.dll"), "SystemFunction036");
		if (fnGetRandom == NULL)
			return false;

		fnGetRandom(buf, DWORD(bufLen));
	}
	return true;
}

void slow_hash(const void *buf, size_t bufLen, BYTE* tmpHash)
{
	mir_sha256_hash(buf, bufLen, tmpHash);

	for (int i = 0; i < 50000; i++)
		mir_sha256_hash(tmpHash, 32, tmpHash);
}