-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
220 lines (164 loc) · 4.46 KB
/
main.c
File metadata and controls
220 lines (164 loc) · 4.46 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
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
static uint16_t const c_crcTable[] = {
0x0000, 0xCC01, 0xD801, 0x1400,
0xF001, 0x3C00, 0x2800, 0xE401,
0xA001, 0x6C00, 0x7800, 0xB401,
0x5000, 0x9C01, 0x8801, 0x4400
};
static uint8_t ReverseByte(uint8_t value)
{
size_t index;
uint8_t result;
result = 0;
for ( index = 0 ; index < 8 ; index++ ) {
result <<= 1;
if (value & 0x01)
result |= 0x01;
value >>= 1;
}
return result;
}
static uint16_t CalcCrc(const uint8_t *data, size_t size)
{
size_t index;
uint8_t rev;
uint16_t result;
result = 0;
for ( index = 0 ; index < size ; index++ ) {
rev = ReverseByte(data[index]);
result = (result >> 4) ^ c_crcTable[result & 0x0F];
result ^= c_crcTable[rev & 0x0F];
result = (result >> 4) ^ c_crcTable[result & 0x0F];
result ^= c_crcTable[rev >> 4];
}
return result;
}
static int Encrypt(const uint8_t *key, const uint8_t *src, size_t size, uint8_t *dest)
{
int ret;
EVP_CIPHER_CTX *ctx;
int len;
ctx = 0;
ret = -1;
do {
ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
ret = -1;
break;
}
ret = EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), 0, key, 0);
if (ret != 1) {
ret = -1;
break;
}
ret = EVP_CIPHER_CTX_set_padding(ctx, 0);
if (ret != 1) {
ret = -1;
break;
}
ret = EVP_EncryptUpdate(ctx, dest, &len, src, size);
if (ret != 1) {
ret = -1;
break;
}
if (len < 0 || (size_t)len != size) {
ret = -1;
break;
}
ret = 0;
} while (0);
if (ctx)
EVP_CIPHER_CTX_free(ctx);
if (ret < 0) {
printf("encryption failed\n");
return -1;
}
return 0;
}
static int WriteFile(const char *filename, const uint8_t *data, size_t size)
{
int ret;
FILE *file;
size_t nwrite;
file = fopen(filename, "wb");
if (!file) {
printf("failed to open output file\n");
return -1;
}
nwrite = fwrite(data, 1, size, file);
if (nwrite != size) {
printf("failed to write to output file\n");
fclose(file);
return -1;
}
ret = fclose(file);
if (ret != 0) {
printf("failed to finalize writing output file\n");
return -1;
}
return 0;
}
static int Worker(const char *filename, const char *key, const char *password)
{
static size_t const c_hdrSize = 48;
static size_t const c_payloadSize = 64;
static size_t const c_bufSize = c_hdrSize + c_payloadSize;
int ret;
size_t len;
uint16_t crc;
uint8_t aesKey0[16];
uint8_t aesKey1[16];
uint8_t buf[c_bufSize];
uint8_t encBuf[c_payloadSize];
size_t index;
len = strlen(key);
if (len != 16) {
printf("invalid key length\n");
return -1;
}
len = strlen(password);
if (len < 1 || len > 16) {
printf("invalid password length\n");
return -1;
}
memset(aesKey1, 0, sizeof(aesKey1));
memmove(aesKey1 + 16 - len, password, len);
memmove(buf, "\xFF\x00//the//owls//are//not//what//they//seem//\x00\x00\x00\x00\xFF", 48);
crc = CalcCrc(buf, c_hdrSize);
for ( index = 0 ; index < 8 ; index++ ) {
buf[c_hdrSize + index * 2 + 0] = crc >> 8;
buf[c_hdrSize + index * 2 + 1] = crc & 0xFF;
}
memmove(aesKey0, buf + c_hdrSize, 16);
memmove(buf + c_hdrSize + 16, key, 16);
memmove(buf + c_hdrSize + 32, "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
memmove(buf + c_hdrSize + 48, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 16);
memmove(encBuf, buf, c_hdrSize);
ret = Encrypt(aesKey0, buf + c_hdrSize, c_payloadSize, encBuf);
if (ret < 0)
return ret;
ret = Encrypt(aesKey1, encBuf, c_payloadSize, buf + c_hdrSize);
if (ret < 0)
return ret;
ret = WriteFile(filename, buf, c_bufSize);
if (ret < 0)
return ret;
return 0;
}
int main(int argc, char *argv[])
{
int ret;
if (argc != 4) {
printf("usage: bekgen <filename> <encryption_key> <password>\n");
return 1;
}
ret = Worker(argv[1], argv[2], argv[3]);
if (ret < 0)
return 1;
printf("finished successfully\n");
return 0;
}