forked from dsoldier/memecrypto
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
memecrypto.c
233 lines (190 loc) · 6.59 KB
/
memecrypto.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
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
#include "memecrypto.h"
#define PUBKEYDER_LEN 0x7E
static unsigned char pubkeyder[] = {
0x30, 0x7C, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05,
0x00, 0x03, 0x6B, 0x00, 0x30, 0x68, 0x02, 0x61, 0x00, 0xB6, 0x1E, 0x19, 0x20, 0x91, 0xF9, 0x0A,
0x8F, 0x76, 0xA6, 0xEA, 0xAA, 0x9A, 0x3C, 0xE5, 0x8C, 0x86, 0x3F, 0x39, 0xAE, 0x25, 0x3F, 0x03,
0x78, 0x16, 0xF5, 0x97, 0x58, 0x54, 0xE0, 0x7A, 0x9A, 0x45, 0x66, 0x01, 0xE7, 0xC9, 0x4C, 0x29,
0x75, 0x9F, 0xE1, 0x55, 0xC0, 0x64, 0xED, 0xDF, 0xA1, 0x11, 0x44, 0x3F, 0x81, 0xEF, 0x1A, 0x42,
0x8C, 0xF6, 0xCD, 0x32, 0xF9, 0xDA, 0xC9, 0xD4, 0x8E, 0x94, 0xCF, 0xB3, 0xF6, 0x90, 0x12, 0x0E,
0x8E, 0x6B, 0x91, 0x11, 0xAD, 0xDA, 0xF1, 0x1E, 0x7C, 0x96, 0x20, 0x8C, 0x37, 0xC0, 0x14, 0x3F,
0xF2, 0xBF, 0x3D, 0x7E, 0x83, 0x11, 0x41, 0xA9, 0x73, 0x02, 0x03, 0x01, 0x00, 0x01
};
void xor(unsigned char *in, unsigned char *b, int len)
{
for (int i = 0; i < len; i++)
in[i] ^= b[i];
}
void memecrypto_aes_encrypt(unsigned char *buf, unsigned char * output, unsigned char *key)
{
unsigned char temp[0x10];
unsigned char temp2[0x10];
unsigned char subkey[0x10];
unsigned char block[0x10];
for (int i = 0; i < 0x10; i++)
temp[i] = 0;
for (int i = 0; i < MEME_LEN/0x10; i++) // CBC
{
memcpy(block, buf+i*0x10, 0x10);
xor(block, temp, 0x10);
AES128_ECB_encrypt(block, key, temp);
memcpy(output+i*0x10, temp, 0x10);
}
// CMAC
xor(temp, output, 0x10);
for (int i = 0; i < 0x10; i += 2)
{
unsigned char b1 = temp[i];
unsigned char b2 = temp[i+1];
subkey[i] = (b1 << 1) | (b2 >> 7);
subkey[i+1] = (b2 << 1);
if (i + 2 < 0x10)
subkey[i+1] |= temp[i+2] >> 7;
}
if (temp[0] & 0x80)
subkey[0xF] ^= 0x87;
for (int i = 0; i < 0x10; i++)
temp[i] = 0;
for (int i = 0; i < MEME_LEN/0x10; i++) // PBC
{
memcpy(block, output + ((MEME_LEN/0x10 - i - 1) * 0x10), 0x10);
xor(block, subkey, 0x10);
AES128_ECB_encrypt(block, key, temp2);
xor(temp2, temp, 0x10);
memcpy(output + ((MEME_LEN/0x10 - i - 1) * 0x10), temp2, 0x10);
memcpy(temp, block, 0x10);
}
}
void memecrypto_aes_decrypt(unsigned char *buf, unsigned char *output, unsigned char *key)
{
unsigned char temp[0x10];
unsigned char subkey[0x10];
unsigned char block[0x10];
for (int i = 0; i < 0x10; i++)
temp[i] = 0;
for (int i = 0; i < MEME_LEN/0x10; i++) // PBC
{
memcpy(block, buf + ((MEME_LEN/0x10 - i - 1) * 0x10), 0x10);
xor(block, temp, 0x10);
AES128_ECB_decrypt(block, key, temp);
memcpy(output + ((MEME_LEN/0x10 - i - 1) * 0x10), temp, 0x10);
}
memcpy(temp, output + ((MEME_LEN/0x10 - 1) * 0x10), 0x10);
// CMAC
xor(temp, output, 0x10);
for (int i = 0; i < 0x10; i += 2)
{
unsigned char b1 = temp[i];
unsigned char b2 = temp[i+1];
subkey[i] = (b1 << 1) | (b2 >> 7);
subkey[i+1] = (b2 << 1);
if (i + 2 < 0x10)
subkey[i+1] |= temp[i+2] >> 7;
}
if (temp[0] & 0x80)
subkey[0xF] ^= 0x87;
for (int i = 0; i < MEME_LEN/0x10; i++)
{
xor(output + i * 0x10, subkey, 0x10);
}
for (int i = 0; i < 0x10; i++)
temp[i] = 0;
for (int i = 0; i < MEME_LEN/0x10; i++) // CBC
{
memcpy(block, output+i*0x10, 0x10);
AES128_ECB_decrypt(block, key, output + i*0x10);
xor(output + i*0x10, temp, 0x10);
memcpy(temp, block, 0x10);
}
}
int memecrypto_sign(unsigned char *input, unsigned char *output, int len)
{
if (len < MEME_LEN)
return 0;
unsigned char memebuf[MEME_LEN];
unsigned char hash[0x14];
sha1_ctx sha1[1];
memcpy(output, input, len - MEME_LEN);
sha1_begin(sha1);
sha1_hash(input, len - 8, sha1);
sha1_end(hash, sha1);
memcpy(input + len - 8, hash, 8); // Update SHA1 hash
sha1_begin(sha1);
sha1_hash(pubkeyder, PUBKEYDER_LEN, sha1);
if (len > MEME_LEN)
sha1_hash(input, len - MEME_LEN, sha1);
sha1_end(hash, sha1); // Hash is now aes key
memcpy(memebuf, input + (len - MEME_LEN), MEME_LEN);
memecrypto_aes_encrypt(memebuf, memebuf, hash);
memebuf[0x0] &= 0x7F;
rsa_decrypt(memebuf, output + (len - MEME_LEN));
return 1;
}
int memecrypto_verify(unsigned char *input, unsigned char *output, int len)
{
if (len < MEME_LEN)
return 0;
unsigned char memebuf_1[MEME_LEN];
unsigned char memebuf_2[MEME_LEN];
unsigned char hash[0x14];
sha1_ctx sha1[1];
memcpy(output, input, len - MEME_LEN);
sha1_begin(sha1);
sha1_hash(pubkeyder, PUBKEYDER_LEN, sha1);
if (len > MEME_LEN)
sha1_hash(input, len - MEME_LEN, sha1);
sha1_end(hash, sha1); // Hash is now aes key
rsa_encrypt(input + (len - MEME_LEN), memebuf_1);
memcpy(memebuf_2, memebuf_1, MEME_LEN);
memebuf_2[0] |= 0x80;
memecrypto_aes_decrypt(memebuf_1, memebuf_1, hash);
memecrypto_aes_decrypt(memebuf_2, memebuf_2, hash);
memcpy(output + (len - MEME_LEN), memebuf_1, MEME_LEN);
sha1_begin(sha1);
sha1_hash(output, len - 8, sha1);
sha1_end(hash, sha1);
if (!memcmp(hash, output + len - 8, 8))
return 1;
memcpy(output + (len - MEME_LEN), memebuf_2, MEME_LEN);
sha1_begin(sha1);
sha1_hash(output, len - 8, sha1);
sha1_end(hash, sha1);
if (!memcmp(hash, output + len - 8, 8))
return 1;
return 0;
}
void reverseCrypt(unsigned char input[], unsigned char output[])
{
unsigned char enc[0x60];
memcpy(enc, &input[0x20], 0x60);
unsigned char keybuf[PUBKEYDER_LEN + 0x20];
memcpy(keybuf, pubkeyder, PUBKEYDER_LEN);
memcpy(&keybuf[PUBKEYDER_LEN], input, 0x20);
unsigned char hash[0x14];
unsigned char key[0x10];
sha1(hash, keybuf, 0x20 + PUBKEYDER_LEN);
memcpy(key, hash, 0x10);
unsigned char RSA[RSA_BYTES];
rsa_encrypt(enc, RSA);
unsigned char dec[0x80];
memecrypto_aes_decrypt(RSA, dec, key);
unsigned char temp1[0x8];
unsigned char temp2[0x8];
sha1(hash, dec, 0x80);
memcpy(temp1, hash, 0x8);
memcpy(temp2, &dec[0x58], 0x8);
if (!memcmp(temp1, temp2, 0x8))
{
memcpy(output, dec, 0x80);
return;
}
RSA[0] |= 0x80;
memecrypto_aes_decrypt(RSA, dec, key);
sha1(hash, dec, 0x80);
memcpy(temp1, hash, 0x8);
memcpy(temp2, &dec[0x58], 0x8);
if (!memcmp(temp1, temp2, 0x8))
{
memcpy(output, dec, 0x80);
}
}