Skip to content

Commit 9093cad

Browse files
committed
update file enc
1 parent 3a21b86 commit 9093cad

2 files changed

Lines changed: 42 additions & 40 deletions

File tree

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ The File encryption will auto append the AEAD tag after the file for integrity p
2424

2525
**compile**
2626
```bash
27-
#For x86
2827
gcc -O3 -march=native -I code/ code/HiAE.c app/file_enc.c -o FileHiAE
29-
#For armv8
30-
gcc -O3 -march=armv8-a+crypto -I code/ code/HiAE.c app/file_enc.c -o FileHiAE
3128
```
3229

3330
**Usage**
31+
3432
```bash
3533
./FileHiAE <encrypt/decrypt> <input_file> <output_file> <key: 32 chars> <iv: 16 chars> [buffer_size: Default 65536]
3634
```

app/file_enc.c

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ size_t encrypt_file(const char* input_file, const char* output_file, const char*
1818
return 0;
1919
}
2020

21+
if (strlen(key) != 32 || strlen(iv) != 16) {
22+
printf("Error: Key and IV must be 32 and 16 characters long respectively\n");
23+
fclose(in);
24+
fclose(out);
25+
return 0;
26+
}
27+
28+
if (buffer_size % 16 != 0) {
29+
printf("Error: Buffer size must be a multiple of 16\n");
30+
fclose(in);
31+
fclose(out);
32+
return 0;
33+
}
34+
2135
uint8_t* buffer = (uint8_t*)malloc(buffer_size);
2236
if (buffer == NULL) {
2337
printf("Error: Could not allocate buffer\n");
@@ -49,26 +63,19 @@ size_t encrypt_file(const char* input_file, const char* output_file, const char*
4963
fseek(in, 0, SEEK_SET);
5064
fseek(out, 0, SEEK_SET);
5165
size_t read_size = 0;
52-
size_t pad = 0;
5366
while (read_size < total_size) {
5467
size_t read = fread(buffer, 1, buffer_size, in);
5568
read_size += read;
5669
if (read_size > total_size) {
5770
read -= read_size - total_size;
5871
//pad to 16x bytes
5972
}
60-
if (read & 15) {
61-
pad = 16 - read & 15;
62-
memset(buffer + read, 0, pad);
63-
}
64-
HiAE_stream_encrypt(state, cipher, buffer, read + pad);
65-
fwrite(cipher, 1, read + pad, out);
73+
HiAE_stream_encrypt(state, cipher, buffer, read);
74+
fwrite(cipher, 1, read, out);
6675
}
6776

6877
HiAE_stream_finalize(state, 0, total_size, tag);
6978
fwrite(tag, 1, 16, out);
70-
uint8_t pad_u8 = pad;
71-
fwrite(&pad_u8, 1, 1, out);
7279

7380
free(buffer);
7481
free(cipher);
@@ -109,17 +116,18 @@ size_t decrypt_file(const char* input_file, const char* output_file, const char*
109116
return 0;
110117
}
111118

112-
uint8_t tag_pad[17];
113-
fseek(in, -17, SEEK_END);
114-
size_t tag_len = fread(tag_pad, 1, 17, in);
115-
if (tag_len != 17) {
119+
uint8_t tag[16];
120+
fseek(in, -16, SEEK_END);
121+
size_t tag_len = fread(tag, 1, 16, in);
122+
if (tag_len != 16) {
116123
printf("Error: Could not read tag\n");
117124
free(buffer);
118125
free(plain);
119126
fclose(in);
120127
fclose(out);
121128
return 0;
122129
}
130+
123131
fseek(in, 0, SEEK_SET);
124132

125133
uint8_t key_data[32];
@@ -132,38 +140,38 @@ size_t decrypt_file(const char* input_file, const char* output_file, const char*
132140

133141
//read in[0, -16] into buffer, decrypt, write to out, dont read last 16 bytes
134142
fseek(in, 0, SEEK_END);
135-
size_t total_size = ftell(in) - 17;
143+
size_t total_size = ftell(in);
136144
fseek(in, 0, SEEK_SET);
137145
fseek(out, 0, SEEK_SET);
138146

139-
if (total_size % 16) {
140-
printf("Error: file length not match!\n");
147+
if (total_size < 16) {
148+
printf("Error: File Destroyed by tag\n");
141149
free(buffer);
142150
free(plain);
143151
fclose(in);
144152
fclose(out);
145153
return 0;
146154
}
147155

156+
total_size -= 16;
157+
148158
size_t read_size = 0;
149-
while (read_size < total_size) {
159+
while (read_size + buffer_size < total_size) {
150160
size_t read = fread(buffer, 1, buffer_size, in);
151161
read_size += read;
152-
if (read_size > total_size) {
153-
read -= read_size - total_size;
154-
HiAE_stream_decrypt(state, plain, buffer, read);
155-
fwrite(plain, 1, read - tag_pad[16], out);
156-
}
157-
else {
158-
HiAE_stream_decrypt(state, plain, buffer, read);
159-
fwrite(plain, 1, read, out);
160-
}
162+
HiAE_stream_decrypt(state, plain, buffer, read);
163+
fwrite(plain, 1, read, out);
164+
}
165+
if (read_size < total_size) {
166+
size_t read = fread(buffer, 1, total_size - read_size, in);
167+
HiAE_stream_decrypt(state, plain, buffer, read);
168+
fwrite(plain, 1, read, out);
161169
}
162170

163171
uint8_t tag_check[16];
164-
HiAE_stream_finalize(state, 0, total_size - tag_pad[16], tag_check);
172+
HiAE_stream_finalize(state, 0, total_size, tag_check);
165173

166-
if (memcmp(tag_pad, tag_check, 16) != 0) {
174+
if (memcmp(tag, tag_check, 16) != 0) {
167175
printf("error: Tag mismatch!\n");
168176
}
169177
else {
@@ -180,7 +188,7 @@ size_t decrypt_file(const char* input_file, const char* output_file, const char*
180188

181189
int main(int argc, char** argv) {
182190
if (argc < 5) {
183-
printf("Usage: %s <encrypt/decrypt> <input_file> <output_file> <key> <iv> [buffer_size]\n", argv[0]);
191+
printf("Usage: %s <encrypt/decrypt> <input_file> <output_file> <key> <iv> [buffer_size (MB)]\n", argv[0]);
184192
return 1;
185193
}
186194

@@ -194,19 +202,15 @@ int main(int argc, char** argv) {
194202
return 1;
195203
}
196204

197-
size_t buffer_size = 65536;
205+
size_t buffer_size = 4 * 1024 * 1024; // default buffer size is 4MB
198206

199207
if (argc > 6) {
200208
size_t buffer_size_temp = atoi(argv[6]);
201-
if (buffer_size_temp < 16) {
202-
printf("Error: Buffer size must be at least 16\n");
203-
return 1;
204-
}
205-
if (buffer_size_temp % 16 != 0) {
206-
printf("Error: Buffer size must be a multiple of 16\n");
209+
if (buffer_size_temp < 1) {
210+
printf("Error: Buffer size must be greater than 0\n");
207211
return 1;
208212
}
209-
buffer_size = buffer_size_temp;
213+
buffer_size = buffer_size_temp * 1024 * 1024; // convert MB to bytes
210214
}
211215

212216
if (strcmp(argv[1], "encrypt") == 0) {

0 commit comments

Comments
 (0)