-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
280 lines (238 loc) · 8.78 KB
/
main.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (C) 2022 Francesco Vannini
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#define _FILE_OFFSET_BITS 64
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/conf.h>
#include <openssl/err.h>
#include <stdbool.h>
#include <getopt.h>
#include <libgen.h>
#include <unistd.h>
#include <stdint.h>
#define BUFSIZE 256
#define SECSIZE 512
#define MAXKEYSIZE 32
typedef unsigned char sector_t[SECSIZE];
_Static_assert(AES_BLOCK_SIZE == 16, "AES_BLOCK_SIZE is expected to be 16 bytes");
bool ProcessSector(uint64_t sec_num, unsigned char *in_data, unsigned char *key, unsigned short key_len,
unsigned char *key_hash, unsigned char *out_data, bool encrypt_mode) {
// Encrypts the sector number using the hashed key and zeros as IV
// https://en.wikipedia.org/wiki/Disk_encryption_theory#Encrypted_salt-sector_initialization_vector_(ESSIV)
EVP_CIPHER_CTX *essiv_ctx;
if (!(essiv_ctx = EVP_CIPHER_CTX_new())) {
ERR_print_errors_fp(stderr);
return false;
}
if (!EVP_CipherInit(essiv_ctx, EVP_aes_256_cbc(), key_hash, NULL, true)) {
ERR_print_errors_fp(stderr);
return false;
}
int out_len;
static unsigned char essiv[AES_BLOCK_SIZE];
static unsigned char sec_buf[AES_BLOCK_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
memcpy(&sec_buf[0], &sec_num, sizeof(sec_num));
if (!EVP_CipherUpdate(essiv_ctx, &essiv[0], &out_len, &sec_buf[0], AES_BLOCK_SIZE)) {
ERR_print_errors_fp(stderr);
return false;
}
EVP_CIPHER_CTX_free(essiv_ctx);
if (out_len != AES_BLOCK_SIZE) {
return false;
}
// En/Decrypts the sector data using the key and the ESSIV calculated above
EVP_CIPHER_CTX *sec_ctx;
if (!(sec_ctx = EVP_CIPHER_CTX_new())) {
ERR_print_errors_fp(stderr);
return false;
}
// Dynamically select the AES cipher based on key length
int init_res;
switch (key_len) {
case 16:
init_res = EVP_CipherInit(sec_ctx, EVP_aes_128_cbc(), key, &essiv[0], encrypt_mode);
break;
case 24:
init_res = EVP_CipherInit(sec_ctx, EVP_aes_192_cbc(), key, &essiv[0], encrypt_mode);
break;
case 32:
init_res = EVP_CipherInit(sec_ctx, EVP_aes_256_cbc(), key, &essiv[0], encrypt_mode);
break;
default:
return false;
}
if (!init_res) {
ERR_print_errors_fp(stderr);
return false;
}
if (!EVP_CIPHER_CTX_set_padding(sec_ctx, 0)) {
ERR_print_errors_fp(stderr);
return false;
}
if (!EVP_CipherUpdate(sec_ctx, out_data, &out_len, in_data, SECSIZE)) {
ERR_print_errors_fp(stderr);
return false;
}
if (out_len != SECSIZE) {
return false;
}
EVP_CIPHER_CTX_free(sec_ctx);
return true;
}
void ShowHelp(char *command, int exitcode) {
fprintf(stderr, "De(En)crypts a dmcrypt-style aes-cbc-essiv:sha256 disk volume\n");
fprintf(stderr, "Usage: %s [-e] [-y] [-q] input output key\n", basename(command));
fprintf(stderr, " -e Encrypt; if not specified, default is to decrypt.\n");
fprintf(stderr, " -y Overwrite output file if it exists.\n");
fprintf(stderr, " -q Quiet output. Only print errors.\n");
fprintf(stderr, " input Input disk volume. Size in bytes must be a multiple of 512.\n");
fprintf(stderr, " output Output disk volume.\n");
fprintf(stderr, " key Key file. Must be either 16, 24 or 32 bytes long.\n");
exit(exitcode);
}
int main(int argc, char *argv[]) {
int opt;
bool encrypt_mode = false;
bool quiet = false;
bool overwrite_existing = false;
while ((opt = getopt(argc, argv, ":eqy")) != -1) {
switch (opt) {
case 'e':
encrypt_mode = true;
break;
case 'q':
quiet = true;
break;
case 'y':
overwrite_existing = true;
break;
case '?':
fprintf(stderr, "Unknown option -%c, ignored.\n", optopt);
break;
default:
ShowHelp(argv[0], EXIT_FAILURE);
}
}
// Not enough params
if (optind > argc - 3) {
ShowHelp(argv[0], EXIT_FAILURE);
}
char *in_filename = argv[optind];
char *out_filename = argv[optind + 1];
char *key_filename = argv[optind + 2];
if (!overwrite_existing) {
if (access(out_filename, F_OK) == 0) {
fprintf(stderr, "Output file %s exists but can't overwrite it, exiting.\n", out_filename);
exit(0);
}
}
FILE *key_file;
if (!(key_file = fopen(key_filename, "rb"))) {
fprintf(stderr, "Error opening key file: %s\n", strerror(errno));
exit(1);
}
unsigned short key_size;
unsigned char key[MAXKEYSIZE];
key_size = fread(key, sizeof(unsigned char), MAXKEYSIZE, key_file);
if (ferror(key_file) != 0) {
fprintf(stderr, "Error reading key file: %s\n", strerror(errno));
fclose(key_file);
exit(1);
}
fclose(key_file);
if ((key_size != 16) && (key_size != 24) && (key_size != 32)) {
fprintf(stderr, "Key length is (%d) bytes but only 16, 24 or 32 bytes keys are supported.\n", key_size);
exit(1);
}
// Open input
FILE *in_file;
if (!(in_file = fopen(in_filename, "rb"))) {
fprintf(stderr, "Error opening input file: %s\n", strerror(errno));
exit(1);
}
// Input file is supposed to be made of sectors
fseek(in_file, 0L, SEEK_END);
uint64_t in_file_len = ftello(in_file);
if ((in_file_len % SECSIZE) != 0) {
fprintf(stderr, "Error: input file length must be a multiple of 512\n");
fclose(in_file);
exit(1);
}
uint64_t sec_total = in_file_len / 512;
fseek(in_file, 0L, SEEK_SET);
FILE *out_file;
if (!(out_file = fopen(out_filename, "wb"))) {
fprintf(stderr, "Error opening output file: %s\n", strerror(errno));
exit(1);
}
if (!quiet) {
if (encrypt_mode) {
fprintf(stdout, "En");
} else {
fprintf(stdout, "De");
}
fprintf(stdout, "coding using AES-%d-CBC (based on key length)\n", key_size * 8);
}
// Get a SHA256 hash of the key
unsigned char key_hash[SHA256_DIGEST_LENGTH];
SHA256(key, key_size, &(key_hash[0]));
sector_t in_buf[BUFSIZE], out_buf[BUFSIZE];
uint64_t sec_idx = 0;
time_t raw_time;
unsigned long start_time = time(&raw_time);
unsigned long check_time = start_time + 5;
unsigned long now;
while (!feof(in_file)) {
unsigned int in_buf_len = fread(&in_buf, sizeof(sector_t), BUFSIZE, in_file);
for (unsigned int s = 0; s < in_buf_len; s++) {
if (!ProcessSector(sec_idx + s, in_buf[s], key, key_size, key_hash, out_buf[s], encrypt_mode)) {
fprintf(stderr, "Error encoding/decoding sector: %lu. Invalid key or input file corrupted.\n",
sec_idx + s);
fclose(in_file);
fclose(out_file);
exit(1);
}
}
if (fwrite(&out_buf[0], sizeof(sector_t), in_buf_len, out_file) < in_buf_len) {
fprintf(stderr, "Error writing output file: %s\n", strerror(errno));
fclose(in_file);
fclose(out_file);
exit(1);
}
sec_idx += in_buf_len;
if (!quiet) {
now = time(&raw_time);
if (now > check_time) {
check_time = now + 3;
fprintf(stdout, "Processed %" PRId64 " sectors out of %" PRId64 " (%.0f%%). Speed: %.2f MB/s\n",
sec_idx, sec_total, 100.0f / (double) sec_total * (double) sec_idx,
(double) (sec_idx * 512) / (double) (now - start_time) / 1048576.0f);
}
}
}
now = time(&raw_time);
fclose(in_file);
fclose(out_file);
if (!quiet) {
fprintf(stdout, "Processed %lu sector at %.2f MB/s\n", sec_idx,
(double) (sec_idx * 512) / (double) (now - start_time) / 1048576.0f);
}
exit(0);
}