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 */
35 #define MAX_BLKSZ 4096
38 #define HDR_OFFSET_SYS 31744 /* 512 * (63 -1) */
42 #define MAX_KFILE_SZ 1048576 /* 1 MB */
43 #define MAX_KEYFILES 256
44 #define HDR_OFFSET_HIDDEN 65536
45 #define BACKUP_HDR_HIDDEN_OFFSET_END 65536
46 #define BACKUP_HDR_OFFSET_END 131072
48 #define VOL_RSVD_BYTES_START (256*512) /* Reserved bytes at vol. start */
49 #define VOL_RSVD_BYTES_END (256*512) /* Reserved bytes at vol. end */
50 #define MIN_VOL_BYTES (VOL_RSVD_BYTES_START + VOL_RSVD_BYTES_END)
52 #define MAX_CIPHER_CHAINS 64
53 #define DEFAULT_RETRIES 3
54 #define ERASE_BUFFER_SIZE 4*1024*1024 /* 4 MB */
56 /* TrueCrypt Volume flags */
57 #define TC_VOLFLAG_SYSTEM 0x01 /* system encryption */
58 #define TC_VOLFLAG_INPLACE 0x02 /* non-system in-place-encrypted volume */
60 #define LOG_BUFFER_SZ 1024
67 #if defined(__DragonFly__)
69 #elif defined(__linux__)
70 #include <uuid/uuid.h>
73 struct pbkdf_prf_algo {
78 struct tc_crypto_algo {
80 const char *dm_crypt_str;
85 struct tc_cipher_chain {
86 struct tc_crypto_algo *cipher;
88 char dm_key[MAX_KEYSZ*2 + 1];
90 struct tc_cipher_chain *prev;
91 struct tc_cipher_chain *next;
95 unsigned char salt[SALT_LEN]; /* Salt for PBKDF */
96 unsigned char enc[448]; /* Encrypted part of the header */
97 } __attribute__((__packed__));
100 char tc_str[4]; /* ASCII string "TRUE" */
101 uint16_t tc_ver; /* Volume header format version */
103 uint32_t crc_keys; /* CRC32 of the key section */
104 uint64_t vol_ctime; /* Volume creation time */
105 uint64_t hdr_ctime; /* Header creation time */
106 uint64_t sz_hidvol; /* Size of hidden volume (set to zero
107 in non-hidden volumes) */
108 uint64_t sz_vol; /* Size of volume */
109 uint64_t off_mk_scope; /* Byte offset of the start of the
111 uint64_t sz_mk_scope; /* Size of the encrypted area within
112 the master key scope */
113 uint32_t flags; /* Flag bits
114 (bit 0: system encryption;
115 bit 1: non-system in-place-encrypted volume;
116 bits 2–31 are reserved) */
117 uint32_t sec_sz; /* Sector size (in bytes) */
118 unsigned char unused3[120];
119 uint32_t crc_dhdr; /* CRC32 of dec. header (except keys) */
120 unsigned char keys[256];
121 } __attribute__((__packed__));
125 struct tchdr_dec *hdr;
126 struct tc_cipher_chain *cipher_chain;
127 struct pbkdf_prf_algo *pbkdf_prf;
128 char key[MAX_KEYSZ*2 + 1];
129 off_t start; /* Logical volume offset in table */
130 size_t size; /* Volume size */
132 off_t skip; /* IV offset */
133 off_t offset; /* Block offset */
135 /* Populated by dm_setup */
139 void *read_to_safe_mem(const char *file, off_t offset, size_t *sz);
140 int get_random(unsigned char *buf, size_t len);
141 int secure_erase(const char *dev, size_t bytes, size_t blksz);
142 int get_disk_info(const char *dev, size_t *blocks, size_t *bsize);
143 int write_to_disk(const char *dev, off_t offset, size_t blksz, void *mem,
145 int read_passphrase(const char *prompt, char *pass, size_t passlen,
148 int tc_crypto_init(void);
149 int tc_cipher_chain_populate_keys(struct tc_cipher_chain *cipher_chain,
151 int tc_cipher_chain_free_keys(struct tc_cipher_chain *cipher_chain);
152 int tc_encrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
154 unsigned char *in, int in_len, unsigned char *out);
155 int tc_decrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
157 unsigned char *in, int in_len, unsigned char *out);
159 /* The following two are platform dependent */
160 int syscrypt(struct tc_crypto_algo *cipher, unsigned char *key, size_t klen,
161 unsigned char *iv, unsigned char *in, unsigned char *out, size_t len,
163 int pbkdf2(struct pbkdf_prf_algo *hash, const char *pass, int passlen,
164 const unsigned char *salt, int saltlen,
165 int keylen, unsigned char *out);
167 int apply_keyfiles(unsigned char *pass, size_t pass_memsz, const char *keyfiles[],
170 struct tchdr_enc *create_hdr(unsigned char *pass, int passlen,
171 struct pbkdf_prf_algo *prf_algo, struct tc_cipher_chain *cipher_chain,
172 size_t sec_sz, size_t total_blocks,
173 off_t offset, size_t blocks, int hidden,
174 struct tchdr_enc **backup_hdr);
175 struct tchdr_dec *decrypt_hdr(struct tchdr_enc *ehdr,
176 struct tc_cipher_chain *cipher_chain, unsigned char *key);
177 int verify_hdr(struct tchdr_dec *hdr);
179 void *_alloc_safe_mem(size_t req_sz, const char *file, int line);
180 void _free_safe_mem(void *mem, const char *file, int line);
181 void check_and_purge_safe_mem(void);
183 struct tc_crypto_algo *check_cipher(const char *cipher, int quiet);
184 struct tc_cipher_chain *check_cipher_chain(char *cipher_chain, int quiet);
185 struct pbkdf_prf_algo *check_prf_algo(char *algo, int quiet);
187 int tc_play_init(void);
188 void tc_log(int err, const char *fmt, ...);
189 void print_info(struct tcplay_info *info);
190 int adjust_info(struct tcplay_info *info, struct tcplay_info *hinfo);
191 int process_hdr(const char *dev, unsigned char *pass, int passlen,
192 struct tchdr_enc *ehdr, struct tcplay_info **pinfo);
193 int create_volume(const char *dev, int hidden, const char *keyfiles[],
194 int nkeyfiles, const char *h_keyfiles[], int n_hkeyfiles,
195 struct pbkdf_prf_algo *prf_algo, struct tc_cipher_chain *cipher_chain,
196 struct pbkdf_prf_algo *h_prf_algo, struct tc_cipher_chain *h_cipher_chain,
197 char *passphrase, char *h_passphrase, size_t hidden_bytes_in,
199 int info_volume(const char *device, int sflag, const char *sys_dev,
200 int protect_hidden, const char *keyfiles[], int nkeyfiles,
201 const char *h_keyfiles[], int n_hkeyfiles,
202 char *passphrase, char *passphrase_hidden, int interactive, int retries,
204 int map_volume(const char *map_name, const char *device, int sflag,
205 const char *sys_dev, int protect_hidden, const char *keyfiles[],
206 int nkeyfiles, const char *h_keyfiles[], int n_hkeyfiles,
207 char *passphrase, char *passphrase_hidden, int interactive, int retries,
209 int dm_setup(const char *mapname, struct tcplay_info *info);
210 int dm_teardown(const char *mapname, const char *device);
212 typedef void(*summary_fn_t)(void);
214 extern int tc_internal_verbose;
215 extern char tc_internal_log_buffer[];
216 extern summary_fn_t summary_fn;
218 #define alloc_safe_mem(x) \
219 _alloc_safe_mem(x, __FILE__, __LINE__)
221 #define free_safe_mem(x) \
222 _free_safe_mem(x, __FILE__, __LINE__)
224 #define __unused __attribute__((__unused__))