Skip to content
Newer
Older
100644 244 lines (203 sloc) 6.22 KB
65808bd @bwalex separate out bits; implement gcrypt backend
authored Jul 20, 2011
1 /*
2 * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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
14 * distribution.
15 *
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
27 * SUCH DAMAGE.
28 */
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <inttypes.h>
32
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <string.h>
7d3bd9d @bwalex big cleanup; Makefile is now gnu make
authored Jul 20, 2011
37 #include <stdio.h>
65808bd @bwalex separate out bits; implement gcrypt backend
authored Jul 20, 2011
38
39 #include "crc32.h"
40 #include "tcplay.h"
41
42 int
43 tc_cipher_chain_populate_keys(struct tc_cipher_chain *cipher_chain,
44 unsigned char *key)
45 {
46 int total_key_bytes, used_key_bytes;
47 struct tc_cipher_chain *dummy_chain;
48
49 /*
50 * We need to determine the total key bytes as the key locations
51 * depend on it.
52 */
53 total_key_bytes = 0;
54 for (dummy_chain = cipher_chain;
55 dummy_chain != NULL;
56 dummy_chain = dummy_chain->next) {
57 total_key_bytes += dummy_chain->cipher->klen;
58 }
59
60 /*
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.
64 */
65 used_key_bytes = 0;
66 for (dummy_chain = cipher_chain;
67 dummy_chain != NULL;
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 "
72 "memory\n");
73 return ENOMEM;
74 }
75
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);
83
84 /* Remember how many key bytes we've seen */
85 used_key_bytes += dummy_chain->cipher->klen;
86 }
87
88 return 0;
89 }
90
91 int
92 tc_encrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
93 unsigned char *iv,
94 unsigned char *in, int in_len, unsigned char *out)
95 {
96 int err;
97
98 if ((err = tc_cipher_chain_populate_keys(cipher_chain, key)))
99 return err;
100
101 #ifdef DEBUG
102 printf("tc_encrypt: starting chain\n");
103 #endif
104
105 /*
106 * Now process the actual decryption, in forward cascade order.
107 */
108 for (;
109 cipher_chain != NULL;
110 cipher_chain = cipher_chain->next) {
111 #ifdef DEBUG
112 printf("tc_encrypt: Currently using cipher %s\n",
113 cipher_chain->cipher->name);
114 #endif
115
116 err = syscrypt(cipher_chain->cipher, cipher_chain->key,
117 cipher_chain->cipher->klen, iv, in, out, in_len, 1);
118
119 /* Deallocate this key, since we won't need it anymore */
120 free_safe_mem(cipher_chain->key);
121
122 if (err != 0)
123 return err;
124
125 /* Set next input buffer as current output buffer */
126 in = out;
127 }
128
129 return 0;
130 }
131
132 int
133 tc_decrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
134 unsigned char *iv,
135 unsigned char *in, int in_len, unsigned char *out)
136 {
137 int err;
138
139 if ((err = tc_cipher_chain_populate_keys(cipher_chain, key)))
140 return err;
141
142 #ifdef DEBUG
143 printf("tc_decrypt: starting chain!\n");
144 #endif
145
146 /*
147 * Now process the actual decryption, in reverse cascade order; so
148 * first find the last element in the chain.
149 */
150 for (; cipher_chain->next != NULL; cipher_chain = cipher_chain->next)
151 ;
152 for (;
153 cipher_chain != NULL;
154 cipher_chain = cipher_chain->prev) {
155 #ifdef DEBUG
156 printf("tc_decrypt: Currently using cipher %s\n",
157 cipher_chain->cipher->name);
158 #endif
159
160 err = syscrypt(cipher_chain->cipher, cipher_chain->key,
161 cipher_chain->cipher->klen, iv, in, out, in_len, 0);
162
163 /* Deallocate this key, since we won't need it anymore */
164 free_safe_mem(cipher_chain->key);
165
166 if (err != 0)
167 return err;
168
169 /* Set next input buffer as current output buffer */
170 in = out;
171 }
172
173 return 0;
174 }
175
176 int
177 apply_keyfiles(unsigned char *pass, size_t pass_memsz, const char *keyfiles[],
178 int nkeyfiles)
179 {
180 int pl, k;
181 unsigned char *kpool;
182 unsigned char *kdata;
183 int kpool_idx;
184 size_t i, kdata_sz;
185 uint32_t crc;
186
187 if (pass_memsz < MAX_PASSSZ) {
188 tc_log(1, "Not enough memory for password manipluation\n");
189 return ENOMEM;
190 }
191
6d60f48 @bwalex make compile on linux, update Makefile
authored Jul 20, 2011
192 pl = strlen((char *)pass);
65808bd @bwalex separate out bits; implement gcrypt backend
authored Jul 20, 2011
193 memset(pass+pl, 0, MAX_PASSSZ-pl);
194
195 if ((kpool = alloc_safe_mem(KPOOL_SZ)) == NULL) {
196 tc_log(1, "Error allocating memory for keyfile pool\n");
197 return ENOMEM;
198 }
199
200 memset(kpool, 0, KPOOL_SZ);
201
202 for (k = 0; k < nkeyfiles; k++) {
203 #ifdef DEBUG
204 printf("Loading keyfile %s into kpool\n", keyfiles[k]);
205 #endif
206 kpool_idx = 0;
207 crc = ~0U;
208 kdata_sz = MAX_KFILE_SZ;
209
210 if ((kdata = read_to_safe_mem(keyfiles[k], 0, &kdata_sz)) == NULL) {
211 tc_log(1, "Error reading keyfile %s content\n",
212 keyfiles[k]);
213 free_safe_mem(kpool);
214 return EIO;
215 }
216
217 for (i = 0; i < kdata_sz; i++) {
218 crc = crc32_intermediate(crc, kdata[i]);
219
220 kpool[kpool_idx++] += (unsigned char)(crc >> 24);
221 kpool[kpool_idx++] += (unsigned char)(crc >> 16);
222 kpool[kpool_idx++] += (unsigned char)(crc >> 8);
223 kpool[kpool_idx++] += (unsigned char)(crc);
224
225 /* Wrap around */
226 if (kpool_idx == KPOOL_SZ)
227 kpool_idx = 0;
228 }
229
230 free_safe_mem(kdata);
231 }
232
233 #ifdef DEBUG
234 printf("Applying kpool to passphrase\n");
235 #endif
236 /* Apply keyfile pool to passphrase */
237 for (i = 0; i < KPOOL_SZ; i++)
238 pass[i] += kpool[i];
239
240 free_safe_mem(kpool);
241
242 return 0;
243 }
Something went wrong with that request. Please try again.