summaryrefslogtreecommitdiff
path: root/src/encryption.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/encryption.c')
-rw-r--r--src/encryption.c182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/encryption.c b/src/encryption.c
new file mode 100644
index 0000000..6539b1a
--- /dev/null
+++ b/src/encryption.c
@@ -0,0 +1,182 @@
1/***
2 * ENCRYPTION
3 *
4 * Makes files unreadable, kinda.
5*/
6
7#include "encryption.h"
8
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <stdlib.h>
12#include <stdint.h>
13#include <string.h>
14#include <unistd.h>
15#include <error.h>
16#include <errno.h>
17#include <fcntl.h>
18
19#include <stdio.h>
20
21// Encrypt a file (OVERWRITES FILE!) using a passphrase
22size_t passenc(int fd, const char *passphrase) {
23 struct stat sb;
24 void *filebuf = NULL;
25 ssize_t cur_len = 0;
26 ssize_t bytesread = 0;
27 int esave;
28
29
30 // Set up file buffer
31 if(fstat(fd, &sb) < 0) {
32 error(0, errno, "[passenc] Could not stat file descriptor \"%d\"", fd);
33 return -1;
34 }
35 filebuf = calloc(sb.st_size, 1);
36 if(filebuf == NULL) {
37 error(0, errno, "[passenc] Could not allocate buffer for reading file");
38 return -1;
39 }
40
41 while((bytesread = read(fd, ((uint8_t*)filebuf) + cur_len, sb.st_size - cur_len)) > 0) {
42 esave = errno;
43 cur_len += bytesread;
44 if(cur_len > sb.st_size) {
45 error(0, ERANGE, "[passenc] Reading overran the proper file size somehow");
46 free(filebuf);
47 return -1;
48 }
49 }
50 if(bytesread < 0) {
51 error(0, esave, "[passenc] Ran into a read error");
52 free(filebuf);
53 return -1;
54 }
55
56
57 // Do the "encryption"
58 size_t phraselen = strlen(passphrase);
59 for(off_t i = 0; i < sb.st_size; i++) {
60 ((uint8_t*)filebuf)[i] ^= passphrase[i % phraselen];
61 }
62
63
64 // Write "encrypted" data to file
65 if(lseek(fd, 0, SEEK_SET) < 0) {
66 error(0, errno, "[passenc] Could not seek to beginning of file");
67 free(filebuf);
68 return -1;
69 }
70
71 bytesread = 0;
72 cur_len = 0;
73 while((bytesread = write(fd, ((uint8_t*)filebuf) + cur_len, sb.st_size - cur_len)) > 0) {
74 esave = errno;
75 cur_len += bytesread;
76 if(cur_len > sb.st_size) {
77 error(0, ERANGE, "[passenc] writing overran the proper file size somehow");
78 free(filebuf);
79 return -1;
80 }
81 }
82 if(bytesread < 0) {
83 error(0, esave, "[passenc] Ran into a write error");
84 free(filebuf);
85 return -1;
86 }
87
88 free(filebuf);
89 return cur_len;
90}
91
92// Encrypt a file one block at a time (Overwrites file)
93size_t passencblock(int fd, const char *passphrase) {
94 struct stat sb;
95 void *filebuf = NULL;
96 ssize_t cur_len = 0, totalwritten = 0;
97 ssize_t bytesread = 0, usebytes = 0;
98 int esave;
99 off_t offset = 0;
100
101
102 // Set up file buffer
103 if(fstat(fd, &sb) < 0) {
104 error(0, errno, "[passenc] Could not stat file descriptor \"%d\"", fd);
105 return -1;
106 }
107 filebuf = calloc(sb.st_blksize, 1);
108 if(filebuf == NULL) {
109 error(0, errno, "[passenc] Could not allocate buffer for reading file");
110 return -1;
111 }
112
113 // Read 1 block
114 // Do the encryption
115 // Overwrite the block with the encryption
116 // Repeat until EOF
117
118 while(offset < sb.st_size) {
119 // Read a block
120 bytesread = 0;
121 cur_len = 0;
122 while((bytesread = read(fd, ((char*)filebuf) + cur_len, sb.st_blksize - cur_len)) > 0) {
123 esave = errno;
124 cur_len += bytesread;
125 if(cur_len > sb.st_blksize) {
126 error(0, ERANGE, "[passenc] Read too many bytes somehow");
127 free(filebuf);
128 return -1;
129 }
130 }
131 if(bytesread < 0) {
132 error(0, esave, "[passenc] Ran into a read error");
133 free(filebuf);
134 return -1;
135 }
136 usebytes = cur_len;
137
138 // Do the encryption
139 size_t phraselen = strlen(passphrase);
140 for(off_t i = 0; i < usebytes; i++) {
141 ((char*)filebuf)[i] ^= passphrase[i % phraselen];
142 }
143
144 // Seek to the right spot
145 if(lseek(fd, offset, SEEK_SET) < 0) {
146 error(0, errno, "[passenc] Could not seek to proper offset");
147 free(filebuf);
148 return -1;
149 }
150 offset += sb.st_blksize;
151
152 // Write to the file
153 bytesread = 0;
154 cur_len = 0;
155 while((bytesread = write(fd, ((char*)filebuf) + cur_len, usebytes - cur_len)) > 0) {
156 esave = errno;
157 cur_len += bytesread;
158 if(cur_len > sb.st_blksize) {
159 error(0, ERANGE, "[passenc] writing overran the block size somehow");
160 free(filebuf);
161 return -1;
162 }
163 }
164 if(bytesread < 0) {
165 error(0, esave, "[passenc] Ran into a write error");
166 free(filebuf);
167 return -1;
168 }
169 totalwritten += cur_len;
170
171 }
172
173 free(filebuf);
174 return totalwritten;
175}
176
177int main() {
178 int fd = open("test.txt", O_RDWR);
179 passencblock(fd, "we do a little trolling");
180
181 return 0;
182} \ No newline at end of file