-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e.c
More file actions
423 lines (373 loc) · 12 KB
/
e2e.c
File metadata and controls
423 lines (373 loc) · 12 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* e2e.dll - mIRC End-to-End Encryption DLL
*
* Platform: Win32 (x86) - mIRC is 32-bit only
* Dependencies: libsodium
* Encryption: XChaCha20-Poly1305 (AEAD)
*
* Build: Visual Studio, /MT (static CRT), Win32
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string.h>
#include <sodium.h>
static int safe_copy(char *dst, size_t dst_sz, const char *src)
{
size_t n = strnlen(src, dst_sz);
if (n >= dst_sz) {
if (dst_sz > 0) dst[0] = '\0';
return 0;
}
memcpy(dst, src, n);
dst[n] = '\0';
return 1;
}
static INIT_ONCE g_log_once = INIT_ONCE_STATIC_INIT;
static SRWLOCK g_log_lock = SRWLOCK_INIT;
static char g_log_path[MAX_PATH];
static BOOL CALLBACK e2e_log_init(PINIT_ONCE once, PVOID param, PVOID *context)
{
char path[MAX_PATH];
DWORD len = GetModuleFileNameA(NULL, path, (DWORD)sizeof(path));
if (len == 0 || len >= sizeof(path)) {
g_log_path[0] = '\0';
return TRUE;
}
for (DWORD i = len; i > 0; i--) {
if (path[i - 1] == '\\' || path[i - 1] == '/') {
path[i - 1] = '\0';
break;
}
}
snprintf(g_log_path, sizeof(g_log_path), "%s\\e2e.logs", path);
return TRUE;
}
static void e2e_log_line(const char *level, const char *msg)
{
InitOnceExecuteOnce(&g_log_once, e2e_log_init, NULL, NULL);
if (g_log_path[0] == '\0') return;
SYSTEMTIME st;
GetLocalTime(&st);
char line[1024];
int n = snprintf(line, sizeof(line),
"%04d-%02d-%02d %02d:%02d:%02d [%s] %s\r\n",
(int)st.wYear, (int)st.wMonth, (int)st.wDay,
(int)st.wHour, (int)st.wMinute, (int)st.wSecond,
level ? level : "INFO", msg ? msg : "");
if (n <= 0) return;
AcquireSRWLockExclusive(&g_log_lock);
HANDLE h = CreateFileA(g_log_path, FILE_APPEND_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (h != INVALID_HANDLE_VALUE) {
DWORD written = 0;
WriteFile(h, line, (DWORD)strlen(line), &written, NULL);
CloseHandle(h);
}
ReleaseSRWLockExclusive(&g_log_lock);
}
static int e2e_return_error(char *data, const char *msg)
{
e2e_log_line("ERROR", msg);
safe_copy(data, 900, msg);
return 3;
}
/* ============================================================================
* DLL Entry Point
* ============================================================================ */
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
// Initialize libsodium
if (sodium_init() < 0) {
return FALSE; // Failed to initialize
}
}
return TRUE;
}
/* ============================================================================
* mIRC LoadDll - Called when DLL is first loaded
* ============================================================================ */
typedef struct {
DWORD mVersion;
HWND mHwnd;
BOOL mKeep;
BOOL mUnicode;
DWORD mBeta;
DWORD mBytes;
} LOADINFO;
__declspec(dllexport)
void __stdcall LoadDll(LOADINFO *loadinfo)
{
loadinfo->mKeep = TRUE; // Keep DLL loaded
loadinfo->mUnicode = FALSE; // Use ANSI, not Unicode
}
/* ============================================================================
* Helper Functions
* ============================================================================ */
/**
* Base64 encode using libsodium
* @param in Input binary data
* @param inlen Input length
* @param out Output buffer (must be large enough)
* @param outlen Output buffer size
*/
void b64_encode(const unsigned char *in, size_t inlen, char *out, size_t outlen)
{
sodium_bin2base64(
out,
outlen,
in,
inlen,
sodium_base64_VARIANT_ORIGINAL
);
}
/**
* Base64 decode using libsodium
* @param in Input base64 string
* @param out Output buffer
* @param outlen Output buffer size
* @return Number of bytes decoded, or -1 on error
*/
int b64_decode(const char *in, unsigned char *out, size_t outlen)
{
size_t bin_len;
if (sodium_base642bin(
out,
outlen,
in,
strlen(in),
NULL,
&bin_len,
NULL,
sodium_base64_VARIANT_ORIGINAL) != 0) {
return -1;
}
return (int)bin_len;
}
/* ============================================================================
* mIRC DLL Exports
*
* mIRC expects this signature (from official documentation):
* int __stdcall procname(HWND mWnd, HWND aWnd, TCHAR *data, TCHAR *parms,
* BOOL show, BOOL nopause);
*
* Parametri:
* mWnd - mIRC main window handle
* aWnd - mIRC script window handle
* data - Output buffer (return data to mIRC)
* parms - Input parameters from mIRC
* show - FALSE if . prefix (quiet), TRUE otherwise
* nopause - TRUE if mIRC is in critical routine
*
* Return values:
* 0 - /halt processing
* 1 - continue processing
* 2 - filled data with command for mIRC to perform
* 3 - filled data with result for $dll() to return
* ============================================================================ */
/**
* E2EEncrypt - Encrypts plaintext (TEST VERSION)
*
* Currently uses a random key to test DLL communication.
*
* mIRC usage: $dll(e2e.dll, E2EEncrypt, plaintext message)
*
* Output format: +E2E1 <base64_ciphertext>
*
* @note This is a TEST version - key exchange is not implemented yet.
*/
__declspec(dllexport)
int __stdcall E2EEncrypt(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)
{
char plaintext[1024];
// INPUT is passed in the 'data' parameter.
if (!safe_copy(plaintext, sizeof(plaintext), data)) {
return e2e_return_error(data, "ERROR: Input too long");
}
// Check if empty
if (strlen(plaintext) == 0) {
return e2e_return_error(data, "ERROR: Empty plaintext");
}
// Crypto buffers
unsigned char key[crypto_aead_xchacha20poly1305_ietf_KEYBYTES];
unsigned char nonce[crypto_aead_xchacha20poly1305_ietf_NPUBBYTES];
unsigned char ciphertext[1200];
unsigned long long clen;
// Generate random key and nonce (TEST - later from KX)
randombytes_buf(key, sizeof(key));
randombytes_buf(nonce, sizeof(nonce));
// Encrypt with XChaCha20-Poly1305
size_t pt_len = strlen(plaintext);
if (pt_len > sizeof(ciphertext) - crypto_aead_xchacha20poly1305_ietf_ABYTES) {
return e2e_return_error(data, "ERROR: Input too long");
}
int result = crypto_aead_xchacha20poly1305_ietf_encrypt(
ciphertext,
&clen,
(unsigned char *)plaintext,
pt_len,
NULL, // No additional authenticated data yet
0,
NULL, // Not used (secret nonce)
nonce,
key
);
if (result != 0) {
return e2e_return_error(data, "ERROR: Encryption failed");
}
// Prepare output with nonce + ciphertext combined
unsigned char combined[1250];
memcpy(combined, nonce, sizeof(nonce));
memcpy(combined + sizeof(nonce), ciphertext, clen);
// Base64 encode
char b64[1800];
size_t combined_len = sizeof(nonce) + (size_t)clen;
if (sodium_base64_ENCODED_LEN(combined_len, sodium_base64_VARIANT_ORIGINAL) >= sizeof(b64)) {
return e2e_return_error(data, "ERROR: Output too large");
}
b64_encode(combined, combined_len, b64, sizeof(b64));
// Format output: +E2E1 <base64>
if (snprintf(data, 900, "+E2E1 %s", b64) >= 900) {
return e2e_return_error(data, "ERROR: Output too large");
}
return 3; // Replace command with result
}
/**
* E2EDecrypt - Decrypts ciphertext (TEST VERSION)
*
* mIRC usage: $dll(e2e.dll, E2EDecrypt, +E2E1 <base64>)
*
* @note TEST version - uses a random key (WILL NOT WORK in real usage!)
* This only demonstrates the format. Real Decrypt comes with KX.
*/
__declspec(dllexport)
int __stdcall E2EDecrypt(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)
{
char input[2048];
// INPUT is in 'data'.
if (!safe_copy(input, sizeof(input), data)) {
return e2e_return_error(data, "ERROR: Input too long");
}
// Check for +E2E1 prefix
if (strncmp(input, "+E2E1 ", 6) != 0) {
return e2e_return_error(data, "ERROR: Invalid E2E format");
}
// Extract base64 part
char *b64 = input + 6;
// Decode base64
unsigned char combined[1250];
int combined_len = b64_decode(b64, combined, sizeof(combined));
if (combined_len < 0) {
return e2e_return_error(data, "ERROR: Invalid base64");
}
if (combined_len < crypto_aead_xchacha20poly1305_ietf_NPUBBYTES) {
return e2e_return_error(data, "ERROR: Invalid ciphertext length");
}
// Extract nonce and ciphertext
unsigned char nonce[crypto_aead_xchacha20poly1305_ietf_NPUBBYTES];
memcpy(nonce, combined, sizeof(nonce));
unsigned char *ciphertext = combined + sizeof(nonce);
size_t clen = combined_len - sizeof(nonce);
// Generate same random key (TEST - later from KX)
// NOTE: This will NOT work because the key is random each time.
// This only shows format - real Decrypt uses saved keys.
unsigned char key[crypto_aead_xchacha20poly1305_ietf_KEYBYTES];
randombytes_buf(key, sizeof(key));
// Decrypt
unsigned char plaintext[1024];
unsigned long long plen;
int result = crypto_aead_xchacha20poly1305_ietf_decrypt(
plaintext,
&plen,
NULL,
ciphertext,
clen,
NULL, // No AAD yet
0,
nonce,
key
);
if (result != 0) {
return e2e_return_error(data, "ERROR: Decryption failed (wrong key or corrupted)");
}
// Null-terminate and return
if (plen >= 900) {
return e2e_return_error(data, "ERROR: Output too large");
}
plaintext[plen] = '\0';
memcpy(data, plaintext, (size_t)plen + 1);
return 3;
}
/**
* Version - Returns DLL version
*
* mIRC usage: $dll(e2e.dll, Version, 0)
*/
__declspec(dllexport)
int __stdcall Version(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)
{
snprintf(data, 900, "e2e.dll v0.1 TEST (libsodium %s)", sodium_version_string());
return 3;
}
/**
* SelfTest - Basic safety checks
*
* mIRC usage: $dll(e2e.dll, SelfTest, 0)
*/
__declspec(dllexport)
int __stdcall SelfTest(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)
{
unsigned char key[32];
char b64[128];
randombytes_buf(key, sizeof(key));
if (sodium_base64_ENCODED_LEN(sizeof(key), sodium_base64_VARIANT_URLSAFE_NO_PADDING) >= sizeof(b64)) {
return e2e_return_error(data, "ERROR: SelfTest failed");
}
sodium_bin2base64(b64, sizeof(b64), key, sizeof(key),
sodium_base64_VARIANT_URLSAFE_NO_PADDING);
snprintf(data, 900, "OK");
return 3;
}
/**
* Test - Checks whether DLL works
*
* mIRC usage: $dll(e2e.dll, Test, hello)
*/
__declspec(dllexport)
int __stdcall Test(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)
{
// INPUT is in 'data', not in 'parms'!
char input[900];
if (!safe_copy(input, sizeof(input), data)) {
return e2e_return_error(data, "ERROR: Input too long");
}
snprintf(data, 900, "RECEIVED: %s", input); // Return output
return 3;
}
/**
* Debug - Shows ALL parameters sent by mIRC
*
* mIRC usage: $dll(e2e.dll, Debug, test)
*/
__declspec(dllexport)
int __stdcall Debug(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)
{
snprintf(data, 900, "mWnd=%p aWnd=%p data=%p parms=%p show=%d nopause=%d str='%s' len=%d",
mWnd, aWnd, data, parms, show, nopause,
parms ? parms : "NULL", parms ? (int)strlen(parms) : -1);
return 3;
}
/**
* Enc - Same as Test, just another name
*
* mIRC usage: $dll(e2e.dll, Enc, hello)
*/
__declspec(dllexport)
int __stdcall Enc(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)
{
snprintf(data, 900, "Enc OK - received: %s", parms);
return 3;
}