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
29 #include <sys/types.h>
30 #include <sys/param.h>
43 tc_cipher_chain_populate_keys(struct tc_cipher_chain
*cipher_chain
,
46 int total_key_bytes
, used_key_bytes
;
47 struct tc_cipher_chain
*dummy_chain
;
50 * We need to determine the total key bytes as the key locations
53 total_key_bytes
= tc_cipher_chain_klen(cipher_chain
);
56 * Now we need to get prepare the keys, as the keys are in
57 * forward order with respect to the cipher cascade, but
58 * the actual decryption is in reverse cipher cascade order.
61 for (dummy_chain
= cipher_chain
;
63 dummy_chain
= dummy_chain
->next
) {
64 dummy_chain
->key
= alloc_safe_mem(dummy_chain
->cipher
->klen
);
65 if (dummy_chain
->key
== NULL
) {
66 tc_log(1, "tc_decrypt: Could not allocate key "
71 /* XXX: here we assume XTS operation! */
72 memcpy(dummy_chain
->key
,
73 key
+ used_key_bytes
/2,
74 dummy_chain
->cipher
->klen
/2);
75 memcpy(dummy_chain
->key
+ dummy_chain
->cipher
->klen
/2,
76 key
+ (total_key_bytes
/2) + used_key_bytes
/2,
77 dummy_chain
->cipher
->klen
/2);
79 /* Remember how many key bytes we've seen */
80 used_key_bytes
+= dummy_chain
->cipher
->klen
;
87 tc_cipher_chain_free_keys(struct tc_cipher_chain
*cipher_chain
)
89 for (; cipher_chain
!= NULL
; cipher_chain
= cipher_chain
->next
) {
90 if (cipher_chain
->key
!= NULL
) {
91 free_safe_mem(cipher_chain
->key
);
92 cipher_chain
->key
= NULL
;
100 tc_encrypt(struct tc_cipher_chain
*cipher_chain
, unsigned char *key
,
102 unsigned char *in
, int in_len
, unsigned char *out
)
104 struct tc_cipher_chain
*chain_start
;
107 chain_start
= cipher_chain
;
109 if ((err
= tc_cipher_chain_populate_keys(cipher_chain
, key
)))
113 printf("tc_encrypt: starting chain\n");
117 * Now process the actual decryption, in forward cascade order.
120 cipher_chain
!= NULL
;
121 cipher_chain
= cipher_chain
->next
) {
123 printf("tc_encrypt: Currently using cipher %s\n",
124 cipher_chain
->cipher
->name
);
127 err
= syscrypt(cipher_chain
->cipher
, cipher_chain
->key
,
128 cipher_chain
->cipher
->klen
, iv
, in
, out
, in_len
, 1);
130 /* Deallocate this key, since we won't need it anymore */
131 free_safe_mem(cipher_chain
->key
);
132 cipher_chain
->key
= NULL
;
135 tc_cipher_chain_free_keys(chain_start
);
139 /* Set next input buffer as current output buffer */
143 tc_cipher_chain_free_keys(chain_start
);
149 tc_decrypt(struct tc_cipher_chain
*cipher_chain
, unsigned char *key
,
151 unsigned char *in
, int in_len
, unsigned char *out
)
153 struct tc_cipher_chain
*chain_start
;
156 chain_start
= cipher_chain
;
158 if ((err
= tc_cipher_chain_populate_keys(cipher_chain
, key
)))
162 printf("tc_decrypt: starting chain!\n");
166 * Now process the actual decryption, in reverse cascade order; so
167 * first find the last element in the chain.
169 for (; cipher_chain
->next
!= NULL
; cipher_chain
= cipher_chain
->next
)
172 cipher_chain
!= NULL
;
173 cipher_chain
= cipher_chain
->prev
) {
175 printf("tc_decrypt: Currently using cipher %s\n",
176 cipher_chain
->cipher
->name
);
179 err
= syscrypt(cipher_chain
->cipher
, cipher_chain
->key
,
180 cipher_chain
->cipher
->klen
, iv
, in
, out
, in_len
, 0);
182 /* Deallocate this key, since we won't need it anymore */
183 free_safe_mem(cipher_chain
->key
);
184 cipher_chain
->key
= NULL
;
187 tc_cipher_chain_free_keys(chain_start
);
191 /* Set next input buffer as current output buffer */
195 tc_cipher_chain_free_keys(chain_start
);
201 apply_keyfiles(unsigned char *pass
, size_t pass_memsz
, const char *keyfiles
[],
205 unsigned char *kpool
;
206 unsigned char *kdata
;
211 if (pass_memsz
< MAX_PASSSZ
) {
212 tc_log(1, "Not enough memory for password manipluation\n");
216 pl
= strlen((char *)pass
);
217 memset(pass
+pl
, 0, MAX_PASSSZ
-pl
);
219 if ((kpool
= alloc_safe_mem(KPOOL_SZ
)) == NULL
) {
220 tc_log(1, "Error allocating memory for keyfile pool\n");
224 memset(kpool
, 0, KPOOL_SZ
);
226 for (k
= 0; k
< nkeyfiles
; k
++) {
228 printf("Loading keyfile %s into kpool\n", keyfiles
[k
]);
232 kdata_sz
= MAX_KFILE_SZ
;
234 if ((kdata
= read_to_safe_mem(keyfiles
[k
], 0, &kdata_sz
)) == NULL
) {
235 tc_log(1, "Error reading keyfile %s content\n",
237 free_safe_mem(kpool
);
241 for (i
= 0; i
< kdata_sz
; i
++) {
242 crc
= crc32_intermediate(crc
, kdata
[i
]);
244 kpool
[kpool_idx
++] += (unsigned char)(crc
>> 24);
245 kpool
[kpool_idx
++] += (unsigned char)(crc
>> 16);
246 kpool
[kpool_idx
++] += (unsigned char)(crc
>> 8);
247 kpool
[kpool_idx
++] += (unsigned char)(crc
);
250 if (kpool_idx
== KPOOL_SZ
)
254 free_safe_mem(kdata
);
258 printf("Applying kpool to passphrase\n");
260 /* Apply keyfile pool to passphrase */
261 for (i
= 0; i
< KPOOL_SZ
; i
++)
264 free_safe_mem(kpool
);