-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfiles.c
More file actions
204 lines (177 loc) · 4.52 KB
/
files.c
File metadata and controls
204 lines (177 loc) · 4.52 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
/* SSH Agent File Encryption Utility (safeu)
* Copyright (C) Jack Whitham 2016-2017
*
* https://github.com/jwhitham/safeu/
* https://www.jwhitham.org/
*
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <openssl/rand.h>
#include "libsafeu.h"
#include "files.h"
#define MAX_PASSWORD_SIZE (256 - 64)
#define AC "safeu: "
static int read_file (const char * src_fname, char ** file_in_data, unsigned * file_in_size)
{
FILE * fd = NULL;
size_t size = 0;
fd = fopen (src_fname, "rb");
(* file_in_data) = NULL;
if (!fd) {
perror (AC "unable to open input file");
goto error;
}
fseek (fd, 0, SEEK_END);
size = ftell (fd);
if (size > (1 << 30)) {
perror (AC "input file is too large");
goto error;
}
(* file_in_size) = size;
(* file_in_data) = malloc (size);
if ((* file_in_data) == NULL) {
perror (AC "unable to allocate memory for input file");
goto error;
}
fseek (fd, 0, SEEK_SET);
if (size != 0) {
if (fread ((* file_in_data), size, 1, fd) != 1) {
perror (AC "unable to read data from input file");
goto error;
}
}
fclose (fd);
return 1;
error:
if (fd) {
fclose (fd);
}
free ((* file_in_data));
(* file_in_data) = NULL;
(* file_in_size) = 0;
return 0;
}
static int write_file (const char * dest_fname, const char * file_out_data, unsigned file_out_size)
{
FILE * fd = NULL;
fd = fopen (dest_fname, "wb");
if (!fd) {
perror (AC "unable to open output file");
goto error;
}
if (file_out_size != 0) {
if (fwrite (file_out_data, file_out_size, 1, fd) != 1) {
perror (AC "unable to write data to output file");
goto error;
}
}
fclose (fd);
return 1;
error:
if (fd) {
fclose (fd);
}
return 0;
}
int safeu_encrypt_a_file (struct t_safeu_struct * ac,
const char * src_fname, const char * dest_fname)
{
int ok = 1;
char * file_in_data = NULL;
char * file_out_data = NULL;
unsigned file_in_size = 0;
unsigned file_out_size = 0;
ok = ok && read_file (src_fname, &file_in_data, &file_in_size);
ok = ok && safeu_encrypt_block (ac, file_in_data, file_in_size, &file_out_data, &file_out_size);
ok = ok && write_file (dest_fname, file_out_data, file_out_size);
free (file_in_data);
free (file_out_data);
return ok;
}
int safeu_decrypt_a_file (struct t_safeu_struct * ac,
const char * src_fname, const char * dest_fname)
{
int ok = 1;
char * file_in_data = NULL;
char * file_out_data = NULL;
unsigned file_in_size = 0;
unsigned file_out_size = 0;
ok = ok && read_file (src_fname, &file_in_data, &file_in_size);
ok = ok && safeu_decrypt_block (ac, file_in_data, file_in_size, &file_out_data, &file_out_size);
ok = ok && write_file (dest_fname, file_out_data, file_out_size);
free (file_in_data);
free (file_out_data);
return ok;
}
static unsigned get_number_of_identities (struct t_safeu_struct * ac)
{
unsigned i;
for (i = 0; safeu_get_fingerprint (ac, i); i++) {}
return i;
}
static void test_file_encryption (struct t_safeu_struct * ac, uint64_t size)
{
const char * test1 = "test1.tmp";
const char * test2 = "test2.tmp";
const char * test3 = "test3.tmp";
char * plaintext = calloc (1, size + 1);
char * check = NULL;
unsigned check_size = 0;
printf ("test file with size %d: %d %d\n",
(int) size, (int) size % CIPHER_BLOCK_SIZE,
(int) size - FILE_BLOCK_SIZE);
if (!plaintext) {
abort ();
}
RAND_bytes ((uint8_t *) plaintext, size);
if (!write_file (test1, plaintext, size)) {
abort ();
}
if (!safeu_encrypt_a_file (ac, test1, test2)) {
abort ();
}
if (!safeu_decrypt_a_file (ac, test2, test3)) {
abort ();
}
if (!read_file (test3, &check, &check_size)) {
abort ();
}
if (check_size != size) {
fputs (AC "readback size error\n", stderr);
abort ();
}
if (memcmp (check, plaintext, size) != 0) {
fputs (AC "readback data error\n", stderr);
abort ();
}
free (plaintext);
free (check);
unlink (test1);
unlink (test2);
unlink (test3);
}
void safeu_files_test (struct t_safeu_struct * ac)
{
uint32_t i;
if (get_number_of_identities (ac) == 0) {
fputs (AC "no identities: cannot run tests\n", stderr);
abort ();
}
for (i = 0; i < (CIPHER_BLOCK_SIZE + 2); i++) {
test_file_encryption (ac, i);
}
for (i = 0; i < (CIPHER_BLOCK_SIZE + 2); i++) {
test_file_encryption (ac, i + (FILE_BLOCK_SIZE / 2) - 1);
}
for (i = 0; i < ((CIPHER_BLOCK_SIZE + 2) * 2); i++) {
test_file_encryption (ac, FILE_BLOCK_SIZE + i - CIPHER_BLOCK_SIZE);
}
for (i = 0; i < 3; i++) {
test_file_encryption (ac, FILE_BLOCK_SIZE * 2 + i - 1);
}
}
/* ex: set tabstop=4 noexpandtab shiftwidth=4: */