-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaokex.c
171 lines (142 loc) · 4.68 KB
/
aokex.c
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
/*
* AOChat -- library for talking with the Anarchy Online chat servers
*
* Copyright (c) 2002-2005, Oskari Saarenmaa <[email protected]>
*
* This file is under the 2-clause BSD license.
* See the file `LICENSE` for details.
*
*/
/*
* based on the PHP class I wrote some time ago.. this C-port is
* quite unfinished, the only thing it can do at the moment is generate
* the required login keys (onesided Diffie-Hellman key exchange with
* TEA encrypted message body)
*
* DH: http://www.rsasecurity.com/rsalabs/faq/3-6-1.html
* TEA: http://www.ftp.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include "aokex.h"
static char * aokex_cipher(char *, char *, int);
static void aokex_tea_encipher(AoUInt32[4], AoUInt32[4]);
char *
aokex_login_key(char *serverseed,
char *username,
char *password)
{
char *dh_str_x, *dh_str_k, *challenge, *plaintext, *ciphertext, *ret;
size_t clen, plen;
AoUInt32 cookie[2];
const char
dh_str_y[] =
"9c32cc23d559ca90fc31be72df817d0e124769e809f936bc"
"14360ff4bed758f260a0d596584eacbbc2b88bdd41041616"
"3e11dbf62173393fbc0c6fefb2d855f1a03dec8e9f105bba"
"d91b3437d8eb73fe2f44159597aa4053cf788d2f9d7012fb"
"8d7c4ce3876f7d6cd5d0c31754f4cd96166708641958de54"
"a6def5657b9f2e92",
dh_str_p[] =
"eca2e8c85d863dcdc26a429a71a9815ad052f6139669dd65"
"9f98ae159d313d13c6bf2838e10a69b6478b64a24bd054ba"
"8248e8fa778703b418408249440b2c1edd28853e240d8a7e"
"49540b76d120d3b1ad2878b1b99490eb4a2a5e84caa8a91c"
"ecbdb1aa7c816e8be343246f80c637abc653b893fd91686c"
"f8d32d6cfe5f2a6f",
dh_str_g[] = "5";
const int
dh_exponent = 192;
aokex_math_t *ctx = aokex_math_init();
/* diffie-hellman */
aokex_math_dh_init(ctx, dh_str_g, dh_str_p);
dh_str_x = aokex_math_dh_x(ctx, dh_exponent);
dh_str_k = aokex_math_dh_k(ctx, dh_str_y);
/* session cookie */
cookie[0] = aokex_math_random32(ctx);
cookie[1] = aokex_math_random32(ctx);
/* uninit math */
aokex_math_uninit(ctx);
/* challenge = username '|' serverseed '|' password */
/* payload = cookie[8] challenge-length[4] challenge */
clen = strlen(username) + 1 + strlen(serverseed) + 1 + strlen(password);
plen = 8+4+clen;
plen = ((plen+7)/8)*8; /* round up to 8 */
plaintext = (char *)aokex_malloc(plen+1); /* make room for a null */
/* insert cookie */
memcpy(plaintext+0, &cookie[0], 4);
memcpy(plaintext+4, &cookie[1], 4);
/* pack the content-length in network byte order */
plaintext[ 8] = (clen>>24) & 0xff;
plaintext[ 9] = (clen>>16) & 0xff;
plaintext[10] = (clen>> 8) & 0xff;
plaintext[11] = (clen>> 0) & 0xff;
/* insert the challenge in our plaintext string */
snprintf(plaintext+12, plen-12, "%s|%s|%s",
username, serverseed, password);
memset(plaintext+12+clen, 0, plen-clen-12);
/* 'ciphertext' is a newly allocated string */
ciphertext = aokex_cipher(dh_str_k, plaintext, plen);
/* the return value is dh_str_x '-' ciphertext */
ret = (char *)aokex_malloc(strlen(dh_str_x)+1+strlen(ciphertext)+1);
strcpy(ret, dh_str_x);
strcat(ret, "-");
strcat(ret, ciphertext);
/* free any memory we allocated */
aokex_free(dh_str_x, 0);
aokex_free(dh_str_k, 0);
aokex_free(plaintext, 0);
aokex_free(ciphertext, 0);
return ret;
}
static char *
aokex_cipher(char *key,
char *str,
int len)
{
AoUInt32 i, cycle[4], akey[4];
char *text=(char *)aokex_malloc(len*2+1), *p=text;
/* Zero the cycle */
memset(cycle, 0, sizeof cycle);
/* Convert the hexadecimal key to binary */
sscanf(key, "%08x%08x%08x%08x", &akey[0], &akey[1], &akey[2], &akey[3]);
akey[0] = htonl(akey[0]);
akey[1] = htonl(akey[1]);
akey[2] = htonl(akey[2]);
akey[3] = htonl(akey[3]);
for(i=0; i<len/4;)
{
cycle[0] = ((str[4*i+3] << 24) | (str[4*i+2] << 16) |
(str[4*i+1] << 8) | (str[4*i+0] << 0)) ^ cycle[2];
i++;
cycle[1] = ((str[4*i+3] << 24) | (str[4*i+2] << 16) |
(str[4*i+1] << 8) | (str[4*i+0] << 0)) ^ cycle[3];
i++;
aokex_tea_encipher(cycle, akey);
sprintf(p, "%08x%08x", htonl(cycle[2]), htonl(cycle[3]));
p += 16;
}
return text;
}
static void
aokex_tea_encipher(AoUInt32 cycle[4],
AoUInt32 key[4])
{
AoUInt32 a = cycle[0],
b = cycle[1],
sum = 0,
delta = 0x9e3779b9,
i = 32;
while(i--)
{
sum += delta;
a += ((b << 4 & 0xfffffff0) + key[0]) ^ (b + sum) ^ ((b >> 5 & 0x7ffffff) + key[1]);
b += ((a << 4 & 0xfffffff0) + key[2]) ^ (a + sum) ^ ((a >> 5 & 0x7ffffff) + key[3]);
}
cycle[2] = a;
cycle[3] = b;
}