Please help with C++ DLL for use with Delphi . . .

Hi All,

I've got a problem, I'm trying to implement the MSN protocol V15.
I'm doing this in Delphi, all is well, except for the encryption cypher part.
I've got an example in C++, but I can't port it to Delphi.
So I thought maybe someone could help me turn it in to a DLL so I can use it from Delphi.

All help would be greatly appreciated,
Smokey Mc. Pot

This is the example I've got:

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "stdafx.h"
#include "wincrypt.h"

#pragma comment(lib, "Crypt32.lib")

BOOL HMACHash(HCRYPTPROV hProvider, HCRYPTKEY hKey, ALG_ID HashAlgId, BYTE * pBytesOut, DWORD dwLengthOut, BYTE * pBytesIn1, DWORD dwLengthIn1, BYTE * pBytesIn2, DWORD dwLengthIn2);
BOOL DeriveLoginKey(BYTE * key, DWORD dwKeySize, CString magic, BYTE * pOutBytes, DWORD dwOutLen);
CString GenerateLoginBlob(CString key, CString challenge);

const BYTE cKeyStdHeader[] = {0x08,0x02,0x00,0x00,0x03,0x66,0x00,0x00,0x18,0x00,0x00,0x00};
#define STDKEYHDRSIZE 12
typedef struct tagMSGRUSRKEY
{
	ULONG uStructHeaderSize;
	ULONG uCryptMode;
	ULONG uCipherType;
	ULONG uHashType;
	ULONG uIVLen;
	ULONG uHashLen;
	ULONG uCipherLen;
	BYTE aIVBytes[8];
	BYTE aHashBytes[20];
	BYTE aCipherBytes[72];

	tagMSGRUSRKEY()
	{
		uStructHeaderSize = 28;
		uCryptMode = CRYPT_MODE_CBC;
		uCipherType = CALG_3DES;
		uHashType = CALG_SHA1;
		uIVLen = sizeof (aIVBytes);
		uHashLen = sizeof(aHashBytes);
		uCipherLen = sizeof(aCipherBytes);
	}
}MSGUSRKEY;

BOOL HMACHash(HCRYPTPROV hProvider, HCRYPTKEY hKey, ALG_ID HashAlgId, BYTE * pBytesOut, DWORD dwLengthOut, BYTE * pBytesIn1, DWORD dwLengthIn1, BYTE * pBytesIn2, DWORD dwLengthIn2)
{
	HCRYPTHASH hHash;
	BOOL bResult = FALSE;

	if ( CryptCreateHash(hProvider,CALG_HMAC,hKey,0,&hHash) )
	{
		HMAC_INFO hmcinfo;
		ZeroMemory(&hmcinfo, sizeof(HMAC_INFO));
		hmcinfo.HashAlgid = HashAlgId;
		CryptSetHashParam(hHash,HP_HMAC_INFO,(BYTE*)&hmcinfo,0);

		if (CryptHashData(hHash,pBytesIn1,dwLengthIn1,0))
		{
			if (dwLengthIn2 != 0 && pBytesIn2 != 0)
			{
				CryptHashData(hHash,pBytesIn2,dwLengthIn2,0);
			}
			DWORD dwHashSize;
			DWORD dwParamSize = 4;
			if ( CryptGetHashParam(hHash,HP_HASHSIZE,(BYTE*)&dwHashSize,&dwParamSize,0) )
			{
				if ( dwHashSize <=dwLengthOut)
				{
					dwParamSize = dwLengthOut;
					if ( CryptGetHashParam(hHash,HP_HASHVAL,pBytesOut,&dwParamSize,0) )
					{
						bResult = TRUE;
					}
				}
			}
		}
		CryptDestroyHash(hHash);
	}
	return bResult;
}

BOOL DeriveLoginKey(BYTE * key, DWORD dwKeySize, CString magic, BYTE * pOutBytes, DWORD dwOutLen)
{
	HCRYPTPROV hProvider;
	BOOL bRet = FALSE;
	if ( CryptAcquireContext(&hProvider,0,0,PROV_RSA_FULL,0) )
	{
		HCRYPTKEY hCryptKey;

		BYTE * pImportKey = new BYTE[ dwKeySize+STDKEYHDRSIZE ];
		memcpy(pImportKey,cKeyStdHeader,STDKEYHDRSIZE);
		memcpy(pImportKey+STDKEYHDRSIZE,key,dwKeySize);
		((DWORD*)pImportKey)[2]=dwKeySize;
		if (CryptImportKey(hProvider,pImportKey,dwKeySize+STDKEYHDRSIZE,0,CRYPT_SF,&hCryptKey))
		{
			BYTE bHash1[20];
			BYTE bHash2[20];
			BYTE bHash3[20];
			BYTE bHash4[20];
			HMACHash(hProvider,hCryptKey,CALG_SHA1,bHash1,20,(BYTE*)(LPCSTR)(LPCTSTR)magic,magic.GetLength(),0,0);
			HMACHash(hProvider,hCryptKey,CALG_SHA1,bHash2,20,bHash1,20,(BYTE*)(LPCSTR)(LPCTSTR)magic,magic.GetLength());
			HMACHash(hProvider,hCryptKey,CALG_SHA1,bHash3,20,bHash1,20,0,0);
			HMACHash(hProvider,hCryptKey,CALG_SHA1,bHash4,20,bHash3,20,(BYTE*)(LPCSTR)(LPCTSTR)magic,magic.GetLength());

			if ( dwOutLen>=24)
			{
				memcpy(pOutBytes,bHash2,20);
				memcpy(pOutBytes+20,bHash4,4);
				bRet = TRUE;
			}
			CryptDestroyKey(hCryptKey);
		}
		delete[] pImportKey;

		CryptReleaseContext(hProvider,0);
	}

	return bRet;
}

CString GenerateLoginBlob(CString key, CString challenge)
{
	BYTE key1[24] = {0};
	BYTE key2[24] = {0};
	BYTE key3[24] = {0};
	BYTE hash[20] = {0};
	BYTE randomdata[8] = {0};
	CString szRet ="";
	DWORD dwBase64Size;

	CryptStringToBinary(key,0,CRYPT_STRING_BASE64,0,&dwBase64Size,0,0);
	ASSERT(dwBase64Size<=24);
	if (dwBase64Size>24)
		return "";

	dwBase64Size = 24;
	CryptStringToBinary(key,0,CRYPT_STRING_BASE64,key1,&dwBase64Size,0,0);
	DeriveLoginKey(key1,24,"WS-SecureConversationSESSION KEY HASH",key2,24);
	DeriveLoginKey(key1,24,"WS-SecureConversationSESSION KEY ENCRYPTION",key3,24);

	HCRYPTPROV hProvider;
	if ( CryptAcquireContext(&hProvider,0,0,PROV_RSA_FULL,0) )
	{
		HCRYPTKEY hCryptKey;
		HCRYPTKEY hCryptKey2;

		BYTE * pImportKey = new BYTE[ 24+STDKEYHDRSIZE ];
		memcpy(pImportKey,cKeyStdHeader,STDKEYHDRSIZE);
		memcpy(pImportKey+STDKEYHDRSIZE,key2,24);

		CryptImportKey(hProvider,pImportKey,24+STDKEYHDRSIZE,0,CRYPT_SF,&hCryptKey2);

		memcpy(pImportKey+STDKEYHDRSIZE,key3,24);
		if ( CryptImportKey(hProvider,pImportKey,24+STDKEYHDRSIZE,0,CRYPT_SF,&hCryptKey) )
		{
			HCRYPTKEY hKeyDupe1;
			HCRYPTKEY hKeyDupe2;
			HCRYPTHASH hHash;

			CryptDuplicateKey(hCryptKey,0,0,&hKeyDupe1);
			DWORD dwMode = CRYPT_MODE_CBC;
			CryptSetKeyParam(hKeyDupe1,KP_MODE,(BYTE*)&dwMode,0);
			if (CryptCreateHash(hProvider,CALG_HMAC,hCryptKey2,0,&hHash))
			{
				HMAC_INFO hmcinfo;
				ZeroMemory(&hmcinfo, sizeof(HMAC_INFO));
				hmcinfo.HashAlgid = CALG_SHA1;
				CryptSetHashParam(hHash,HP_HMAC_INFO,(BYTE*)&hmcinfo,0);

				DWORD dwDataLen = challenge.GetLength();
				CryptDuplicateKey(hKeyDupe1,0,0,&hKeyDupe2);
				CryptEncrypt(hKeyDupe2,0,TRUE,0,0,&dwDataLen,0);
				CryptDestroyKey(hKeyDupe2);

				if ( dwDataLen > 0)
				{
					CryptGenRandom(hProvider,8,randomdata);
					CryptSetKeyParam(hKeyDupe1,KP_IV,randomdata,0);

					BYTE * pEncryptBytes = new BYTE[dwDataLen];
					ZeroMemory(pEncryptBytes,dwDataLen);
					memcpy(pEncryptBytes,(LPCSTR)(LPCTSTR)challenge,challenge.GetLength());

					DWORD dwData = challenge.GetLength();
					if (CryptEncrypt(hKeyDupe1,hHash,TRUE,0,pEncryptBytes,&dwData,dwDataLen))
					{
						ASSERT(dwData == 72); // The size of the encryption *should* always be 72. If it's not you'll need to fix it.
						dwData = 20;
						CryptGetHashParam(hHash,HP_HASHVAL,hash,&dwData,0);

						MSGUSRKEY usrkey;
						memcpy(usrkey.aIVBytes, randomdata,8);
						memcpy(usrkey.aHashBytes, hash,20);
						memcpy(usrkey.aCipherBytes , pEncryptBytes,72);

						CryptBinaryToString((BYTE*)&usrkey,sizeof(MSGUSRKEY),CRYPT_STRING_BASE64, 0,&dwBase64Size);
						CryptBinaryToString((BYTE*)&usrkey,sizeof(MSGUSRKEY),CRYPT_STRING_BASE64, szRet.GetBuffer(dwBase64Size),&dwBase64Size);
						szRet.ReleaseBuffer();

						szRet.Replace("\r\n","");
					}
					delete[] pEncryptBytes;
				}
				CryptDestroyHash(hHash);
			}
			CryptDestroyKey(hKeyDupe1);

			CryptDestroyKey(hCryptKey);
		}
		delete[] pImportKey;
		if ( hCryptKey2 )
			CryptDestroyKey(hCryptKey2);
		CryptReleaseContext(hProvider,0);
	}
    return szRet;
}
Hi,

If I understand your problem the links below give a help. The first shows how you can use a DLL write in C/C++ and use in the Delphi. And the second shows you create and using DLL in the Delphi.

http://www.drbob42.com/delphi/headconv.htm
http://delphi.about.com/od/windowsshellapi/a/dll_basics.htm

I want that help you.

Regards,
Topic archived. No new replies allowed.