summaryrefslogtreecommitdiff
path: root/plugins/Db3x_mmap/src/dbcrypt.cpp
blob: 42907f46d129573bea4bbdff95a48a34a1d79446 (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
/*

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

Copyright 2012-13 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 "commonheaders.h"

int CDb3Base::InitCrypt()
{
	CRYPTO_PROVIDER *pProvider;

	DBVARIANT dbv = { 0 };
	dbv.type = DBVT_ASCIIZ;
	DBCONTACTGETSETTING dbcgs = { "CryptoEngine", "Provider", &dbv };
	if (GetContactSettingStr(NULL, &dbcgs)) {
		CRYPTO_PROVIDER **ppProvs;
		int iNumProvs;
		Crypto_EnumProviders(&iNumProvs, &ppProvs);
		if (iNumProvs == 0)
			return 1;

		pProvider = ppProvs[0];  //!!!!!!!!!!!!!!!!!!
		
		DBCONTACTWRITESETTING dbcws = { "CryptoEngine", "Provider" };
		dbcws.value.type = DBVT_ASCIIZ;
		dbcws.value.pszVal = pProvider->pszName;
		WriteContactSetting(NULL, &dbcws);
	}
	else {
		pProvider = Crypto_GetProvider(dbv.pszVal);
		FreeVariant(&dbv);
		if (pProvider == NULL)
			return 2;
	}

	if ((m_crypto = pProvider->pFactory()) == NULL)
		return 3;

	size_t iKeyLength = m_crypto->getKeyLength();

	dbv.type = DBVT_BLOB;
	dbcgs.szSetting = "StoredKey";
	if (GetContactSetting(NULL, &dbcgs)) {
		m_crypto->generateKey(); // unencrypted key
		
		BYTE *pKey = (BYTE*)_alloca(iKeyLength);
		m_crypto->getKey(pKey, iKeyLength);

		DBCONTACTWRITESETTING dbcws = { "CryptoEngine", "StoredKey" };
		dbcws.value.type = DBVT_BLOB;
		dbcws.value.cpbVal = (WORD)iKeyLength;
		dbcws.value.pbVal = pKey;
		WriteContactSetting(NULL, &dbcws);

		memset(pKey, 0, iKeyLength);
	}
	else {
		if (dbv.cpbVal != (WORD)iKeyLength)
			return 4;

		m_crypto->setKey(dbv.pbVal, iKeyLength);
		FreeVariant(&dbv);
	}
	return 0;
}

void CDb3Base::EncodeCopyMemory(void *dst, void *src, size_t size)
{
	MoveMemory(dst, src, size);
}

void CDb3Base::DecodeCopyMemory(void *dst, void *src, size_t size)
{
	MoveMemory(dst, src, size);
}

void CDb3Base::EncodeDBWrite(DWORD ofs, void *src, int size)
{
	DBWrite(ofs, src, size);
}

void CDb3Base::DecodeDBWrite(DWORD ofs, void *src, int size)
{
	DBWrite(ofs, src, size);
}