2 * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 /* Version of tcplay specified during build (CMakeLists.txt, Makefile.classic) */
35 #define MAX_BLKSZ 4096
38 #define HDR_OFFSET_SYS 31744 /* 512 * (63 -1) */
41 #define PASS_BUFSZ 256
43 #define MAX_KFILE_SZ 1048576 /* 1 MB */
44 #define MAX_KEYFILES 256
45 #define HDR_OFFSET_HIDDEN 65536
46 #define BACKUP_HDR_HIDDEN_OFFSET_END 65536
47 #define BACKUP_HDR_OFFSET_END 131072
49 #define VOL_RSVD_BYTES_START (256*512) /* Reserved bytes at vol. start */
50 #define VOL_RSVD_BYTES_END (256*512) /* Reserved bytes at vol. end */
51 #define MIN_VOL_BYTES (VOL_RSVD_BYTES_START + VOL_RSVD_BYTES_END)
53 #define MAX_CIPHER_CHAINS 64
54 #define DEFAULT_RETRIES 3
55 #define ERASE_BUFFER_SIZE 4*1024*1024 /* 4 MB */
57 /* TrueCrypt Volume flags */
58 #define TC_VOLFLAG_SYSTEM 0x01 /* system encryption */
59 #define TC_VOLFLAG_INPLACE 0x02 /* non-system in-place-encrypted volume */
61 #define TC_VOLFLAG_SET(f, x) ((f & TC_VOLFLAG_##x) == TC_VOLFLAG_##x)
63 #define LOG_BUFFER_SZ 1024
68 #define TC_FLAG_SYS 0x0001
69 #define TC_FLAG_FDE 0x0002
70 #define TC_FLAG_BACKUP 0x0004
71 #define TC_FLAG_ONLY_RESTORE 0x0008
72 #define TC_FLAG_ALLOW_TRIM 0x0010
73 #define TC_FLAG_SAVE_TO_FILE 0x0020
74 #define TC_FLAG_HDR_FROM_FILE 0x0040
75 #define TC_FLAG_H_HDR_FROM_FILE 0x0080
77 #define TC_FLAG_SET(f, x) ((f & TC_FLAG_##x) == TC_FLAG_##x)
82 #if defined(__DragonFly__)
84 #elif defined(__linux__)
85 #include <uuid/uuid.h>
89 typedef uint64_t disksz_t
;
90 #define DISKSZ_FMT PRIu64
93 struct pbkdf_prf_algo
{
98 struct tc_crypto_algo
{
100 const char *dm_crypt_str
;
105 struct tc_cipher_chain
{
106 struct tc_crypto_algo
*cipher
;
108 char dm_key
[MAX_KEYSZ
*2 + 1];
110 struct tc_cipher_chain
*prev
;
111 struct tc_cipher_chain
*next
;
115 unsigned char salt
[SALT_LEN
]; /* Salt for PBKDF */
116 unsigned char enc
[448]; /* Encrypted part of the header */
117 } __attribute__((__packed__
));
120 char tc_str
[4]; /* ASCII string "TRUE" */
121 uint16_t tc_ver
; /* Volume header format version */
123 uint32_t crc_keys
; /* CRC32 of the key section */
124 uint64_t vol_ctime
; /* Volume creation time */
125 uint64_t hdr_ctime
; /* Header creation time */
126 uint64_t sz_hidvol
; /* Size of hidden volume (set to zero
127 in non-hidden volumes) */
128 uint64_t sz_vol
; /* Size of volume */
129 uint64_t off_mk_scope
; /* Byte offset of the start of the
131 uint64_t sz_mk_scope
; /* Size of the encrypted area within
132 the master key scope */
133 uint32_t flags
; /* Flag bits
134 (bit 0: system encryption;
135 bit 1: non-system in-place-encrypted volume;
136 bits 2–31 are reserved) */
137 uint32_t sec_sz
; /* Sector size (in bytes) */
138 unsigned char unused3
[120];
139 uint32_t crc_dhdr
; /* CRC32 of dec. header (except keys) */
140 unsigned char keys
[256];
141 } __attribute__((__packed__
));
145 struct tchdr_dec
*hdr
;
146 struct tc_cipher_chain
*cipher_chain
;
147 struct pbkdf_prf_algo
*pbkdf_prf
;
148 char key
[MAX_KEYSZ
*2 + 1];
155 off_t start
; /* Logical volume offset in table (in blk_sz blocks) */
156 disksz_t size
; /* Volume size (in blk_sz blocks) */
158 off_t skip
; /* IV offset (in blk_sz blocks) */
159 off_t offset
; /* Block offset (in blk_sz blocks) */
161 /* Populated by dm_setup */
167 #define INFO_TO_DM_BLOCKS(info, memb) \
168 (((info)->memb * (uint64_t)((info)->blk_sz))/512)
170 struct tcplay_dm_table
{
171 char device
[PATH_MAX
]; /* Underlying device */
172 char target
[256]; /* DM Target type */
173 off_t start
; /* Logical volume offset in table */
174 disksz_t size
; /* Volume size */
176 char cipher
[256]; /* Cipher */
177 off_t skip
; /* IV offset */
178 off_t offset
; /* Block offset */
182 typedef int (*tc_state_change_fn
)(void *, const char *, int);
185 /* (Mostly) common options */
187 const char *keyfiles
[MAX_KEYFILES
];
189 const char *h_keyfiles
[MAX_KEYFILES
];
191 struct pbkdf_prf_algo
*prf_algo
;
192 struct tc_cipher_chain
*cipher_chain
;
193 struct pbkdf_prf_algo
*h_prf_algo
;
194 struct tc_cipher_chain
*h_cipher_chain
;
195 const char *passphrase
;
196 const char *h_passphrase
;
198 int weak_keys_and_salt
;
200 /* Options for create */
202 disksz_t hidden_size_bytes
;
203 int secure_erase
; /* XXX: default to 1! */
205 /* Options for map, info_mapped */
206 const char *map_name
;
208 /* Options for info, map, modify */
212 int retries
; /* XXX: default to DEFAULT_RETRIES */
215 const char *hdr_file_in
;
216 const char *h_hdr_file_in
;
218 /* Options for modify only */
219 struct pbkdf_prf_algo
*new_prf_algo
;
220 const char *new_passphrase
;
221 const char *hdr_file_out
;
222 const char *new_keyfiles
[MAX_KEYFILES
];
226 tc_state_change_fn state_change_fn
;
230 struct tcplay_opts
*opts_init(void);
231 int opts_add_keyfile(struct tcplay_opts
*opts
, const char *keyfile
);
232 int opts_add_keyfile_hidden(struct tcplay_opts
*opts
, const char *keyfile
);
233 int opts_add_keyfile_new(struct tcplay_opts
*opts
, const char *keyfile
);
234 void opts_free(struct tcplay_opts
*opts
);
235 void opts_clear_keyfile(struct tcplay_opts
*opts
);
236 void opts_clear_keyfile_hidden(struct tcplay_opts
*opts
);
237 void opts_clear_keyfile_new(struct tcplay_opts
*opts
);
239 void *read_to_safe_mem(const char *file
, off_t offset
, size_t *sz
);
240 int get_random(unsigned char *buf
, size_t len
, int weak
);
241 int secure_erase(const char *dev
, disksz_t bytes
, size_t blksz
);
242 int get_disk_info(const char *dev
, disksz_t
*blocks
, size_t *bsize
);
243 int write_to_disk(const char *dev
, off_t offset
, size_t blksz
, void *mem
,
245 int write_to_file(const char *file
, void *mem
, size_t bytes
);
246 int read_passphrase(const char *prompt
, char *pass
, size_t passlen
,
247 size_t bufsz
, time_t timeout
);
248 float get_random_read_progress(void);
249 float get_secure_erase_progress(void);
252 int tc_crypto_init(void);
253 int tc_cipher_chain_populate_keys(struct tc_cipher_chain
*cipher_chain
,
255 int tc_cipher_chain_free_keys(struct tc_cipher_chain
*cipher_chain
);
256 int tc_encrypt(struct tc_cipher_chain
*cipher_chain
, unsigned char *key
,
258 unsigned char *in
, int in_len
, unsigned char *out
);
259 int tc_decrypt(struct tc_cipher_chain
*cipher_chain
, unsigned char *key
,
261 unsigned char *in
, int in_len
, unsigned char *out
);
263 /* The following two are platform dependent */
264 int syscrypt(struct tc_crypto_algo
*cipher
, unsigned char *key
, size_t klen
,
265 unsigned char *iv
, unsigned char *in
, unsigned char *out
, size_t len
,
267 int pbkdf2(struct pbkdf_prf_algo
*hash
, const char *pass
, int passlen
,
268 const unsigned char *salt
, int saltlen
,
269 int keylen
, unsigned char *out
);
271 int apply_keyfiles(unsigned char *pass
, size_t pass_memsz
, const char *keyfiles
[],
274 struct tchdr_enc
*create_hdr(unsigned char *pass
, int passlen
,
275 struct pbkdf_prf_algo
*prf_algo
, struct tc_cipher_chain
*cipher_chain
,
276 size_t sec_sz
, disksz_t total_blocks
,
277 off_t offset
, disksz_t blocks
, int hidden
, int weak
,
278 struct tchdr_enc
**backup_hdr
);
279 struct tchdr_dec
*decrypt_hdr(struct tchdr_enc
*ehdr
,
280 struct tc_cipher_chain
*cipher_chain
, unsigned char *key
);
281 int verify_hdr(struct tchdr_dec
*hdr
);
282 struct tchdr_enc
*copy_reencrypt_hdr(unsigned char *pass
, int passlen
,
283 struct pbkdf_prf_algo
*prf_algo
, int weak
, struct tcplay_info
*info
,
284 struct tchdr_enc
**backup_hdr
);
286 void *_alloc_safe_mem(size_t req_sz
, const char *file
, int line
);
287 void *_strdup_safe_mem(const char *in
, const char *file
, int line
);
288 void _free_safe_mem(void *mem
, const char *file
, int line
);
289 void check_and_purge_safe_mem(void);
291 struct tc_crypto_algo
*check_cipher(const char *cipher
, int quiet
);
292 struct tc_cipher_chain
*check_cipher_chain(const char *cipher_chain
, int quiet
);
293 struct pbkdf_prf_algo
*check_prf_algo(const char *algo
, int quiet
);
295 int tc_play_init(void);
296 void tc_log(int err
, const char *fmt
, ...);
297 int tc_cipher_chain_klen(struct tc_cipher_chain
*chain
);
298 int tc_cipher_chain_length(struct tc_cipher_chain
*chain
);
299 char *tc_cipher_chain_sprint(char *buf
, size_t bufsz
,
300 struct tc_cipher_chain
*chain
);
301 int free_info(struct tcplay_info
*info
);
302 void print_info(struct tcplay_info
*info
);
303 int adjust_info(struct tcplay_info
*info
, struct tcplay_info
*hinfo
);
304 int process_hdr(const char *dev
, int flags
, unsigned char *pass
, int passlen
,
305 struct tchdr_enc
*ehdr
, struct tcplay_info
**pinfo
);
306 int create_volume(struct tcplay_opts
*opts
);
307 struct tcplay_info
*info_map_common(struct tcplay_opts
*opts
,
308 char *passphrase_out
);
309 int info_mapped_volume(struct tcplay_opts
*opts
);
310 int info_volume(struct tcplay_opts
*opts
);
311 int map_volume(struct tcplay_opts
*opts
);
312 int modify_volume(struct tcplay_opts
*opts
);
313 int dm_setup(const char *mapname
, struct tcplay_info
*info
);
314 int dm_teardown(const char *mapname
, const char *device
);
315 struct tcplay_info
*dm_info_map(const char *map_name
);
317 typedef void(*summary_fn_t
)(void);
319 extern int tc_internal_verbose
;
320 extern char tc_internal_log_buffer
[];
321 extern summary_fn_t summary_fn
;
322 extern struct pbkdf_prf_algo pbkdf_prf_algos
[];
323 extern struct tc_cipher_chain
*tc_cipher_chains
[MAX_CIPHER_CHAINS
];
325 #define STATE_UNKNOWN 0
326 #define STATE_GET_RANDOM 1
327 #define STATE_ERASE 2
329 extern int tc_internal_state
;
331 #define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
334 #define alloc_safe_mem(x) \
335 _alloc_safe_mem(x, __FILE__, __LINE__)
337 #define strdup_safe_mem(x) \
338 _strdup_safe_mem(x, __FILE__, __LINE__)
340 #define free_safe_mem(x) \
341 _free_safe_mem(__DECONST(void *, x), __FILE__, __LINE__)
343 #define __unused __attribute__((__unused__))