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
54 for (dummy_chain
= cipher_chain
;
56 dummy_chain
= dummy_chain
->next
) {
57 total_key_bytes
+= dummy_chain
->cipher
->klen
;
61 * Now we need to get prepare the keys, as the keys are in
62 * forward order with respect to the cipher cascade, but
63 * the actual decryption is in reverse cipher cascade order.
66 for (dummy_chain
= cipher_chain
;
68 dummy_chain
= dummy_chain
->next
) {
69 dummy_chain
->key
= alloc_safe_mem(dummy_chain
->cipher
->klen
);
70 if (dummy_chain
->key
== NULL
) {
71 tc_log(1, "tc_decrypt: Could not allocate key "
76 /* XXX: here we assume XTS operation! */
77 memcpy(dummy_chain
->key
,
78 key
+ used_key_bytes
/2,
79 dummy_chain
->cipher
->klen
/2);
80 memcpy(dummy_chain
->key
+ dummy_chain
->cipher
->klen
/2,
81 key
+ (total_key_bytes
/2) + used_key_bytes
/2,
82 dummy_chain
->cipher
->klen
/2);
84 /* Remember how many key bytes we've seen */
85 used_key_bytes
+= dummy_chain
->cipher
->klen
;
92 tc_cipher_chain_free_keys(struct tc_cipher_chain
*cipher_chain
)
94 for (; cipher_chain
!= NULL
; cipher_chain
= cipher_chain
->next
) {
95 if (cipher_chain
->key
!= NULL
) {
96 free_safe_mem(cipher_chain
->key
);
97 cipher_chain
->key
= NULL
;
105 tc_encrypt(struct tc_cipher_chain
*cipher_chain
, unsigned char *key
,
107 unsigned char *in
, int in_len
, unsigned char *out
)
109 struct tc_cipher_chain
*chain_start
;
112 chain_start
= cipher_chain
;
114 if ((err
= tc_cipher_chain_populate_keys(cipher_chain
, key
)))
118 printf("tc_encrypt: starting chain\n");
122 * Now process the actual decryption, in forward cascade order.
125 cipher_chain
!= NULL
;
126 cipher_chain
= cipher_chain
->next
) {
128 printf("tc_encrypt: Currently using cipher %s\n",
129 cipher_chain
->cipher
->name
);
132 err
= syscrypt(cipher_chain
->cipher
, cipher_chain
->key
,
133 cipher_chain
->cipher
->klen
, iv
, in
, out
, in_len
, 1);
135 /* Deallocate this key, since we won't need it anymore */
136 free_safe_mem(cipher_chain
->key
);
137 cipher_chain
->key
= NULL
;
140 tc_cipher_chain_free_keys(chain_start
);
144 /* Set next input buffer as current output buffer */
148 tc_cipher_chain_free_keys(chain_start
);
154 tc_decrypt(struct tc_cipher_chain
*cipher_chain
, unsigned char *key
,
156 unsigned char *in
, int in_len
, unsigned char *out
)
158 struct tc_cipher_chain
*chain_start
;
161 chain_start
= cipher_chain
;
163 if ((err
= tc_cipher_chain_populate_keys(cipher_chain
, key
)))
167 printf("tc_decrypt: starting chain!\n");
171 * Now process the actual decryption, in reverse cascade order; so
172 * first find the last element in the chain.
174 for (; cipher_chain
->next
!= NULL
; cipher_chain
= cipher_chain
->next
)
177 cipher_chain
!= NULL
;
178 cipher_chain
= cipher_chain
->prev
) {
180 printf("tc_decrypt: Currently using cipher %s\n",
181 cipher_chain
->cipher
->name
);
184 err
= syscrypt(cipher_chain
->cipher
, cipher_chain
->key
,
185 cipher_chain
->cipher
->klen
, iv
, in
, out
, in_len
, 0);
187 /* Deallocate this key, since we won't need it anymore */
188 free_safe_mem(cipher_chain
->key
);
189 cipher_chain
->key
= NULL
;
192 tc_cipher_chain_free_keys(chain_start
);
196 /* Set next input buffer as current output buffer */
200 tc_cipher_chain_free_keys(chain_start
);
206 apply_keyfiles(unsigned char *pass
, size_t pass_memsz
, const char *keyfiles
[],
210 unsigned char *kpool
;
211 unsigned char *kdata
;
216 if (pass_memsz
< MAX_PASSSZ
) {
217 tc_log(1, "Not enough memory for password manipluation\n");
221 pl
= strlen((char *)pass
);
222 memset(pass
+pl
, 0, MAX_PASSSZ
-pl
);
224 if ((kpool
= alloc_safe_mem(KPOOL_SZ
)) == NULL
) {
225 tc_log(1, "Error allocating memory for keyfile pool\n");
229 memset(kpool
, 0, KPOOL_SZ
);
231 for (k
= 0; k
< nkeyfiles
; k
++) {
233 printf("Loading keyfile %s into kpool\n", keyfiles
[k
]);
237 kdata_sz
= MAX_KFILE_SZ
;
239 if ((kdata
= read_to_safe_mem(keyfiles
[k
], 0, &kdata_sz
)) == NULL
) {
240 tc_log(1, "Error reading keyfile %s content\n",
242 free_safe_mem(kpool
);
246 for (i
= 0; i
< kdata_sz
; i
++) {
247 crc
= crc32_intermediate(crc
, kdata
[i
]);
249 kpool
[kpool_idx
++] += (unsigned char)(crc
>> 24);
250 kpool
[kpool_idx
++] += (unsigned char)(crc
>> 16);
251 kpool
[kpool_idx
++] += (unsigned char)(crc
>> 8);
252 kpool
[kpool_idx
++] += (unsigned char)(crc
);
255 if (kpool_idx
== KPOOL_SZ
)
259 free_safe_mem(kdata
);
263 printf("Applying kpool to passphrase\n");
265 /* Apply keyfile pool to passphrase */
266 for (i
= 0; i
< KPOOL_SZ
; i
++)
269 free_safe_mem(kpool
);