-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbc-file.c
217 lines (182 loc) · 4.66 KB
/
cbc-file.c
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
/*
* This is an implementation that intends to provide a simple executable to
* encrypt and decrypt an arbitrary file.
*
* The file is encrypted with ChaCha20+Poly1305 and the key is derived from the
* password input and a salt.
*
* File format:
* <encryption type: 1 byte> <salt: 32 bytes> <iv: 32 bytes> <aad: 32 bytes> <tag: 16 bytes> <file>
*/
#define _DEFAULT_SOURCE /* getentropy() */
#define _XOPEN_SOURCE 600 /* fdopen() */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <bearssl.h>
#include <argon2.h>
#define KEY_LENGTH 32
#define SALT_LENGTH 32
#define IV_LENGTH 32 /* should be 12 bytes, but keep 32 for backwards compat and possible future changes */
#define AAD_LENGTH 32
#define TAG_LENGTH 16
#define FINAL_LENGTH (1 + SALT_LENGTH + IV_LENGTH + AAD_LENGTH + TAG_LENGTH)
#define TIME_COST 32 /* 32 computations */
#define MEM_COST (1 << 16) /* 2^16 bytes */
#define PARALLEL 4 /* number of threads/lanes */
#define PASS_LENGTH 32
enum encryption_type {
CHACHA20POLY1305 = 1,
};
const enum encryption_type default_enctype = CHACHA20POLY1305;
/* constant time */
static int ct_memcmp(const void *va, const void *vb, size_t l)
{
const unsigned char *a = va, *b = vb;
unsigned char rv = 0;
for (size_t i=0; i<l; i++) rv |= a[i] ^ b[i];
return rv;
}
static void usage(void)
{
fputs("Usage: cbd-file lock|unlock <file>\n", stderr);
exit(1);
}
static int read_password(uint8_t *key, const uint8_t *salt)
{
int rv = -1;
fprintf(stderr, "input password (up to %d): ", KEY_LENGTH);
struct termios old, t;
tcgetattr(STDIN_FILENO, &old);
t = old;
// only disable echo, keep other attributes
t.c_lflag = old.c_lflag & ~(ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &t);
char pass[PASS_LENGTH];
if (fgets(pass, PASS_LENGTH, stdin) == NULL) {
// echo should always be turned off at exit
goto end;
}
rv = 0;
argon2id_hash_raw(
TIME_COST, MEM_COST, PARALLEL,
pass, strlen(pass), salt, SALT_LENGTH,
key, KEY_LENGTH);
end:
// restore previous attributes
tcsetattr(STDIN_FILENO, TCSANOW, &old);
return rv;
}
static off_t file_size(int fd)
{
struct stat s = { 0 };
int rv = fstat(fd, &s);
if (rv != 0) {
perror("fstat()");
exit(100);
}
return s.st_size;
}
static ssize_t open_file_for_read(char *name, uint8_t **buffer)
{
int f = open(name, O_RDONLY | O_CLOEXEC);
if (f < 0) {
perror("open()");
return -1;
}
off_t size = file_size(f);
*buffer = calloc(1, size);
if (buffer == NULL) {
perror("calloc()");
return -1;
}
FILE *file = fdopen(f, "r");
if (file == NULL) {
perror("fdopen()");
return -1;
}
if (fread(*buffer, 1, size, file) != (size_t)size) {
fputs("mismatched file size and bytes read!\n", stderr);
return -1;
}
fclose(file);
return size;
}
int main(int argc, char **argv)
{
if (argc < 3) {
usage();
}
uint8_t *buffer;
ssize_t bytes;
uint8_t enctype;
uint8_t key[KEY_LENGTH] = { 0 };
uint8_t iv[IV_LENGTH];
uint8_t aad[AAD_LENGTH];
uint8_t salt[SALT_LENGTH];
uint8_t tag[TAG_LENGTH];
if (strcmp(argv[1], "lock") == 0) {
// locking code
bytes = open_file_for_read(argv[2], &buffer);
if (bytes < 0) {
return 1;
}
if (getentropy(iv, IV_LENGTH) < 0
|| getentropy(aad, AAD_LENGTH) < 0
|| getentropy(salt, SALT_LENGTH) < 0) {
perror("getentropy()");
return 1;
}
if (read_password(key, salt) < 0) {
return 1;
}
br_poly1305_ctmul_run(key, iv, buffer, bytes, aad, AAD_LENGTH, tag, br_chacha20_ct_run, 1);
enctype = default_enctype;
fwrite(&enctype, 1, 1, stdout);
fwrite(salt, 1, SALT_LENGTH, stdout);
fwrite(iv, 1, IV_LENGTH, stdout);
fwrite(aad, 1, AAD_LENGTH, stdout);
fwrite(tag, 1, TAG_LENGTH, stdout);
fwrite(buffer, 1, bytes, stdout);
} else if (strcmp(argv[1], "unlock") == 0) {
// unlocking code
bytes = open_file_for_read(argv[2], &buffer);
if (bytes < FINAL_LENGTH) {
return 1;
}
memcpy(&enctype, buffer, 1);
buffer += 1;
if (enctype != default_enctype) {
fputs("wrong encryption mode!\n", stderr);
return 1;
}
memcpy(salt, buffer, SALT_LENGTH);
buffer += SALT_LENGTH;
memcpy(iv, buffer, IV_LENGTH);
buffer += IV_LENGTH;
memcpy(aad, buffer, AAD_LENGTH);
buffer += AAD_LENGTH;
memcpy(tag, buffer, TAG_LENGTH);
buffer += TAG_LENGTH;
bytes -= FINAL_LENGTH;
if (read_password(key, salt) < 0) {
return 1;
}
uint8_t new_tag[TAG_LENGTH];
br_poly1305_ctmul_run(key, iv, buffer, bytes, aad, AAD_LENGTH, new_tag, br_chacha20_ct_run, 0);
if (ct_memcmp(tag, new_tag, TAG_LENGTH)) {
fputs("bad tag!\n", stderr);
return 1;
}
fwrite(buffer, 1, bytes, stdout);
} else {
usage();
}
return 0;
}