cgd_crypto.c revision 1.12 1 1.12 riastrad /* $NetBSD: cgd_crypto.c,v 1.12 2013/06/24 04:21:20 riastradh Exp $ */
2 1.1 elric
3 1.1 elric /*-
4 1.1 elric * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 elric * All rights reserved.
6 1.1 elric *
7 1.1 elric * This code is derived from software contributed to The NetBSD Foundation
8 1.1 elric * by Roland C. Dowdeswell.
9 1.1 elric *
10 1.1 elric * Redistribution and use in source and binary forms, with or without
11 1.1 elric * modification, are permitted provided that the following conditions
12 1.1 elric * are met:
13 1.1 elric * 1. Redistributions of source code must retain the above copyright
14 1.1 elric * notice, this list of conditions and the following disclaimer.
15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 elric * notice, this list of conditions and the following disclaimer in the
17 1.1 elric * documentation and/or other materials provided with the distribution.
18 1.1 elric *
19 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 elric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 elric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 elric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 elric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 elric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 elric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 elric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 elric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 elric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 elric * POSSIBILITY OF SUCH DAMAGE.
30 1.1 elric */
31 1.1 elric
32 1.1 elric /*
33 1.1 elric * Crypto Framework For cgd.c
34 1.1 elric *
35 1.1 elric * This framework is temporary and awaits a more complete
36 1.1 elric * kernel wide crypto implementation.
37 1.1 elric */
38 1.1 elric
39 1.1 elric #include <sys/cdefs.h>
40 1.12 riastrad __KERNEL_RCSID(0, "$NetBSD: cgd_crypto.c,v 1.12 2013/06/24 04:21:20 riastradh Exp $");
41 1.1 elric
42 1.1 elric #include <sys/param.h>
43 1.1 elric #include <sys/systm.h>
44 1.1 elric #include <sys/malloc.h>
45 1.1 elric
46 1.1 elric #include <dev/cgd_crypto.h>
47 1.1 elric
48 1.11 christos #include <crypto/rijndael/rijndael-api-fst.h>
49 1.11 christos #include <crypto/des/des.h>
50 1.11 christos #include <crypto/blowfish/blowfish.h>
51 1.11 christos
52 1.1 elric #ifdef DIAGNOSTIC
53 1.6 christos #define DIAGPANIC(x) panic x
54 1.1 elric #else
55 1.1 elric #define DIAGPANIC(x)
56 1.1 elric #endif
57 1.1 elric
58 1.1 elric /*
59 1.1 elric * The general framework provides only one generic function.
60 1.1 elric * It takes the name of an algorith and returns a struct cryptfuncs *
61 1.1 elric * for it. It is up to the initialisation routines of the algorithm
62 1.1 elric * to check key size and block size.
63 1.1 elric */
64 1.1 elric
65 1.11 christos static cfunc_init cgd_cipher_aes_init;
66 1.11 christos static cfunc_destroy cgd_cipher_aes_destroy;
67 1.11 christos static cfunc_cipher cgd_cipher_aes_cbc;
68 1.11 christos
69 1.11 christos static cfunc_init cgd_cipher_3des_init;
70 1.11 christos static cfunc_destroy cgd_cipher_3des_destroy;
71 1.11 christos static cfunc_cipher cgd_cipher_3des_cbc;
72 1.11 christos
73 1.11 christos static cfunc_init cgd_cipher_bf_init;
74 1.11 christos static cfunc_destroy cgd_cipher_bf_destroy;
75 1.11 christos static cfunc_cipher cgd_cipher_bf_cbc;
76 1.11 christos
77 1.11 christos static const struct cryptfuncs cf[] = {
78 1.11 christos {
79 1.11 christos .cf_name = "aes-cbc",
80 1.11 christos .cf_init = cgd_cipher_aes_init,
81 1.11 christos .cf_destroy = cgd_cipher_aes_destroy,
82 1.11 christos .cf_cipher = cgd_cipher_aes_cbc,
83 1.11 christos },
84 1.11 christos {
85 1.11 christos .cf_name = "3des-cbc",
86 1.11 christos .cf_init = cgd_cipher_3des_init,
87 1.11 christos .cf_destroy = cgd_cipher_3des_destroy,
88 1.11 christos .cf_cipher = cgd_cipher_3des_cbc,
89 1.11 christos },
90 1.11 christos {
91 1.11 christos .cf_name = "blowfish-cbc",
92 1.11 christos .cf_init = cgd_cipher_bf_init,
93 1.11 christos .cf_destroy = cgd_cipher_bf_destroy,
94 1.11 christos .cf_cipher = cgd_cipher_bf_cbc,
95 1.11 christos },
96 1.11 christos };
97 1.11 christos const struct cryptfuncs *
98 1.6 christos cryptfuncs_find(const char *alg)
99 1.1 elric {
100 1.1 elric
101 1.11 christos for (size_t i = 0; i < __arraycount(cf); i++)
102 1.11 christos if (strcmp(cf[i].cf_name, alg) == 0)
103 1.11 christos return &cf[i];
104 1.11 christos
105 1.1 elric return NULL;
106 1.1 elric }
107 1.1 elric
108 1.7 cbiere typedef void (*cipher_func)(void *, void *, const void *, size_t);
109 1.1 elric
110 1.11 christos static void
111 1.1 elric cgd_cipher_uio_cbc(void *privdata, cipher_func cipher,
112 1.1 elric struct uio *dstuio, struct uio *srcuio);
113 1.1 elric
114 1.1 elric /*
115 1.1 elric * cgd_cipher_uio_cbc takes a simple cbc cipher and iterates
116 1.1 elric * it over two struct uio's. It presumes that the cipher function
117 1.1 elric * that is passed to it keeps the IV state between calls.
118 1.1 elric *
119 1.1 elric * We assume that the caller has ensured that each segment is evenly
120 1.1 elric * divisible by the block size, which for the cgd is a valid assumption.
121 1.1 elric * If we were to make this code more generic, we might need to take care
122 1.1 elric * of this case, either by issuing an error or copying the data.
123 1.1 elric */
124 1.1 elric
125 1.11 christos static void
126 1.1 elric cgd_cipher_uio_cbc(void *privdata, cipher_func cipher,
127 1.6 christos struct uio *dstuio, struct uio *srcuio)
128 1.1 elric {
129 1.1 elric struct iovec *dst;
130 1.1 elric struct iovec *src;
131 1.1 elric int dstnum;
132 1.1 elric int dstoff = 0;
133 1.1 elric int srcnum;
134 1.1 elric int srcoff = 0;
135 1.1 elric
136 1.1 elric dst = dstuio->uio_iov;
137 1.1 elric dstnum = dstuio->uio_iovcnt;
138 1.1 elric src = srcuio->uio_iov;
139 1.1 elric srcnum = srcuio->uio_iovcnt;
140 1.1 elric for (;;) {
141 1.1 elric int l = MIN(dst->iov_len - dstoff, src->iov_len - srcoff);
142 1.1 elric u_int8_t *d = (u_int8_t *)dst->iov_base + dstoff;
143 1.1 elric u_int8_t *s = (u_int8_t *)src->iov_base + srcoff;
144 1.1 elric
145 1.1 elric cipher(privdata, d, s, l);
146 1.1 elric
147 1.1 elric dstoff += l;
148 1.1 elric srcoff += l;
149 1.1 elric /*
150 1.1 elric * We assume that {dst,src} == {dst,src}->iov_len,
151 1.1 elric * because it should not be possible for it not to be.
152 1.1 elric */
153 1.1 elric if (dstoff == dst->iov_len) {
154 1.1 elric dstoff = 0;
155 1.1 elric dstnum--;
156 1.1 elric dst++;
157 1.1 elric }
158 1.1 elric if (srcoff == src->iov_len) {
159 1.1 elric srcoff = 0;
160 1.1 elric srcnum--;
161 1.1 elric src++;
162 1.1 elric }
163 1.1 elric if (!srcnum || !dstnum)
164 1.1 elric break;
165 1.1 elric }
166 1.1 elric }
167 1.1 elric
168 1.1 elric /*
169 1.1 elric * AES Framework
170 1.1 elric */
171 1.1 elric
172 1.1 elric /*
173 1.1 elric * NOTE: we do not store the blocksize in here, because it is not
174 1.1 elric * variable [yet], we hardcode the blocksize to 16 (128 bits).
175 1.1 elric */
176 1.1 elric
177 1.1 elric struct aes_privdata {
178 1.1 elric keyInstance ap_enckey;
179 1.1 elric keyInstance ap_deckey;
180 1.1 elric };
181 1.1 elric
182 1.1 elric struct aes_encdata {
183 1.1 elric keyInstance *ae_key; /* key for this direction */
184 1.1 elric u_int8_t ae_iv[16]; /* Initialization Vector */
185 1.1 elric };
186 1.1 elric
187 1.11 christos static void *
188 1.7 cbiere cgd_cipher_aes_init(size_t keylen, const void *key, size_t *blocksize)
189 1.1 elric {
190 1.1 elric struct aes_privdata *ap;
191 1.1 elric
192 1.1 elric if (!blocksize)
193 1.1 elric return NULL;
194 1.1 elric if (keylen != 128 && keylen != 192 && keylen != 256)
195 1.1 elric return NULL;
196 1.6 christos if (*blocksize == (size_t)-1)
197 1.1 elric *blocksize = 128;
198 1.1 elric if (*blocksize != 128)
199 1.1 elric return NULL;
200 1.1 elric ap = malloc(sizeof(*ap), M_DEVBUF, 0);
201 1.1 elric if (!ap)
202 1.1 elric return NULL;
203 1.1 elric rijndael_makeKey(&ap->ap_enckey, DIR_ENCRYPT, keylen, key);
204 1.1 elric rijndael_makeKey(&ap->ap_deckey, DIR_DECRYPT, keylen, key);
205 1.6 christos return ap;
206 1.1 elric }
207 1.1 elric
208 1.11 christos static void
209 1.6 christos cgd_cipher_aes_destroy(void *data)
210 1.1 elric {
211 1.6 christos struct aes_privdata *apd = data;
212 1.1 elric
213 1.12 riastrad explicit_memset(apd, 0, sizeof(*apd));
214 1.1 elric free(apd, M_DEVBUF);
215 1.1 elric }
216 1.1 elric
217 1.11 christos static void
218 1.7 cbiere aes_cbc_enc_int(void *privdata, void *dst, const void *src, size_t len)
219 1.1 elric {
220 1.6 christos struct aes_encdata *ae = privdata;
221 1.1 elric cipherInstance cipher;
222 1.1 elric
223 1.1 elric rijndael_cipherInit(&cipher, MODE_CBC, ae->ae_iv);
224 1.1 elric rijndael_blockEncrypt(&cipher, ae->ae_key, src, len * 8, dst);
225 1.6 christos (void)memcpy(ae->ae_iv, (u_int8_t *)dst + (len - 16), 16);
226 1.1 elric }
227 1.1 elric
228 1.11 christos static void
229 1.7 cbiere aes_cbc_dec_int(void *privdata, void *dst, const void *src, size_t len)
230 1.1 elric {
231 1.6 christos struct aes_encdata *ae = privdata;
232 1.1 elric cipherInstance cipher;
233 1.1 elric
234 1.1 elric rijndael_cipherInit(&cipher, MODE_CBC, ae->ae_iv);
235 1.1 elric rijndael_blockDecrypt(&cipher, ae->ae_key, src, len * 8, dst);
236 1.7 cbiere (void)memcpy(ae->ae_iv, (const u_int8_t *)src + (len - 16), 16);
237 1.1 elric }
238 1.1 elric
239 1.11 christos static void
240 1.6 christos cgd_cipher_aes_cbc(void *privdata, struct uio *dstuio,
241 1.6 christos struct uio *srcuio, void *iv, int dir)
242 1.1 elric {
243 1.6 christos struct aes_privdata *apd = privdata;
244 1.1 elric struct aes_encdata encd;
245 1.1 elric
246 1.6 christos (void)memcpy(encd.ae_iv, iv, 16);
247 1.1 elric switch (dir) {
248 1.1 elric case CGD_CIPHER_ENCRYPT:
249 1.1 elric encd.ae_key = &apd->ap_enckey;
250 1.1 elric cgd_cipher_uio_cbc(&encd, aes_cbc_enc_int, dstuio, srcuio);
251 1.1 elric break;
252 1.1 elric case CGD_CIPHER_DECRYPT:
253 1.1 elric encd.ae_key = &apd->ap_deckey;
254 1.1 elric cgd_cipher_uio_cbc(&encd, aes_cbc_dec_int, dstuio, srcuio);
255 1.1 elric break;
256 1.1 elric default:
257 1.8 perry DIAGPANIC(("%s: unrecognised direction %d", __func__, dir));
258 1.1 elric }
259 1.1 elric }
260 1.1 elric
261 1.1 elric /*
262 1.1 elric * 3DES Framework
263 1.1 elric */
264 1.1 elric
265 1.1 elric struct c3des_privdata {
266 1.1 elric des_key_schedule cp_key1;
267 1.1 elric des_key_schedule cp_key2;
268 1.1 elric des_key_schedule cp_key3;
269 1.1 elric };
270 1.1 elric
271 1.1 elric struct c3des_encdata {
272 1.1 elric des_key_schedule *ce_key1;
273 1.1 elric des_key_schedule *ce_key2;
274 1.1 elric des_key_schedule *ce_key3;
275 1.1 elric u_int8_t ce_iv[8];
276 1.1 elric };
277 1.1 elric
278 1.11 christos static void *
279 1.7 cbiere cgd_cipher_3des_init(size_t keylen, const void *key, size_t *blocksize)
280 1.1 elric {
281 1.1 elric struct c3des_privdata *cp;
282 1.1 elric int error = 0;
283 1.7 cbiere des_cblock *block;
284 1.1 elric
285 1.1 elric if (!blocksize)
286 1.1 elric return NULL;
287 1.6 christos if (*blocksize == (size_t)-1)
288 1.1 elric *blocksize = 64;
289 1.1 elric if (keylen != (DES_KEY_SZ * 3 * 8) || *blocksize != 64)
290 1.1 elric return NULL;
291 1.1 elric cp = malloc(sizeof(*cp), M_DEVBUF, 0);
292 1.1 elric if (!cp)
293 1.1 elric return NULL;
294 1.7 cbiere block = __UNCONST(key);
295 1.7 cbiere error = des_key_sched(block, cp->cp_key1);
296 1.7 cbiere error |= des_key_sched(block + 1, cp->cp_key2);
297 1.7 cbiere error |= des_key_sched(block + 2, cp->cp_key3);
298 1.1 elric if (error) {
299 1.12 riastrad explicit_memset(cp, 0, sizeof(*cp));
300 1.1 elric free(cp, M_DEVBUF);
301 1.1 elric return NULL;
302 1.1 elric }
303 1.6 christos return cp;
304 1.1 elric }
305 1.1 elric
306 1.11 christos static void
307 1.6 christos cgd_cipher_3des_destroy(void *data)
308 1.1 elric {
309 1.6 christos struct c3des_privdata *cp = data;
310 1.1 elric
311 1.12 riastrad explicit_memset(cp, 0, sizeof(*cp));
312 1.1 elric free(cp, M_DEVBUF);
313 1.1 elric }
314 1.1 elric
315 1.1 elric static void
316 1.7 cbiere c3des_cbc_enc_int(void *privdata, void *dst, const void *src, size_t len)
317 1.1 elric {
318 1.6 christos struct c3des_encdata *ce = privdata;
319 1.1 elric
320 1.1 elric des_ede3_cbc_encrypt(src, dst, len, *ce->ce_key1, *ce->ce_key2,
321 1.1 elric *ce->ce_key3, (des_cblock *)ce->ce_iv, 1);
322 1.7 cbiere (void)memcpy(ce->ce_iv, (const u_int8_t *)dst + (len - 8), 8);
323 1.1 elric }
324 1.1 elric
325 1.1 elric static void
326 1.7 cbiere c3des_cbc_dec_int(void *privdata, void *dst, const void *src, size_t len)
327 1.1 elric {
328 1.6 christos struct c3des_encdata *ce = privdata;
329 1.1 elric
330 1.1 elric des_ede3_cbc_encrypt(src, dst, len, *ce->ce_key1, *ce->ce_key2,
331 1.1 elric *ce->ce_key3, (des_cblock *)ce->ce_iv, 0);
332 1.7 cbiere (void)memcpy(ce->ce_iv, (const u_int8_t *)src + (len - 8), 8);
333 1.1 elric }
334 1.1 elric
335 1.11 christos static void
336 1.6 christos cgd_cipher_3des_cbc(void *privdata, struct uio *dstuio,
337 1.6 christos struct uio *srcuio, void *iv, int dir)
338 1.1 elric {
339 1.6 christos struct c3des_privdata *cp = privdata;
340 1.1 elric struct c3des_encdata ce;
341 1.1 elric
342 1.6 christos (void)memcpy(ce.ce_iv, iv, 8);
343 1.1 elric ce.ce_key1 = &cp->cp_key1;
344 1.1 elric ce.ce_key2 = &cp->cp_key2;
345 1.1 elric ce.ce_key3 = &cp->cp_key3;
346 1.1 elric switch (dir) {
347 1.1 elric case CGD_CIPHER_ENCRYPT:
348 1.1 elric cgd_cipher_uio_cbc(&ce, c3des_cbc_enc_int, dstuio, srcuio);
349 1.1 elric break;
350 1.1 elric case CGD_CIPHER_DECRYPT:
351 1.1 elric cgd_cipher_uio_cbc(&ce, c3des_cbc_dec_int, dstuio, srcuio);
352 1.1 elric break;
353 1.1 elric default:
354 1.8 perry DIAGPANIC(("%s: unrecognised direction %d", __func__, dir));
355 1.1 elric }
356 1.1 elric }
357 1.1 elric
358 1.1 elric /*
359 1.1 elric * Blowfish Framework
360 1.1 elric */
361 1.1 elric
362 1.1 elric struct bf_privdata {
363 1.1 elric BF_KEY bp_key;
364 1.1 elric };
365 1.1 elric
366 1.1 elric struct bf_encdata {
367 1.1 elric BF_KEY *be_key;
368 1.1 elric u_int8_t be_iv[8];
369 1.1 elric };
370 1.1 elric
371 1.11 christos static void *
372 1.7 cbiere cgd_cipher_bf_init(size_t keylen, const void *key, size_t *blocksize)
373 1.1 elric {
374 1.1 elric struct bf_privdata *bp;
375 1.1 elric
376 1.1 elric if (!blocksize)
377 1.1 elric return NULL;
378 1.3 dan if (keylen < 40 || keylen > 448 || (keylen % 8 != 0))
379 1.1 elric return NULL;
380 1.6 christos if (*blocksize == (size_t)-1)
381 1.1 elric *blocksize = 64;
382 1.1 elric if (*blocksize != 64)
383 1.1 elric return NULL;
384 1.1 elric bp = malloc(sizeof(*bp), M_DEVBUF, 0);
385 1.1 elric if (!bp)
386 1.1 elric return NULL;
387 1.3 dan BF_set_key(&bp->bp_key, keylen / 8, key);
388 1.6 christos return bp;
389 1.1 elric }
390 1.1 elric
391 1.11 christos static void
392 1.6 christos cgd_cipher_bf_destroy(void *data)
393 1.1 elric {
394 1.6 christos struct bf_privdata *bp = data;
395 1.1 elric
396 1.12 riastrad explicit_memset(bp, 0, sizeof(*bp));
397 1.1 elric free(bp, M_DEVBUF);
398 1.1 elric }
399 1.1 elric
400 1.11 christos static void
401 1.7 cbiere bf_cbc_enc_int(void *privdata, void *dst, const void *src, size_t len)
402 1.1 elric {
403 1.6 christos struct bf_encdata *be = privdata;
404 1.1 elric
405 1.1 elric BF_cbc_encrypt(src, dst, len, be->be_key, be->be_iv, 1);
406 1.6 christos (void)memcpy(be->be_iv, (u_int8_t *)dst + (len - 8), 8);
407 1.1 elric }
408 1.1 elric
409 1.11 christos static void
410 1.7 cbiere bf_cbc_dec_int(void *privdata, void *dst, const void *src, size_t len)
411 1.1 elric {
412 1.6 christos struct bf_encdata *be = privdata;
413 1.1 elric
414 1.1 elric BF_cbc_encrypt(src, dst, len, be->be_key, be->be_iv, 0);
415 1.7 cbiere (void)memcpy(be->be_iv, (const u_int8_t *)src + (len - 8), 8);
416 1.1 elric }
417 1.1 elric
418 1.11 christos static void
419 1.6 christos cgd_cipher_bf_cbc(void *privdata, struct uio *dstuio,
420 1.6 christos struct uio *srcuio, void *iv, int dir)
421 1.1 elric {
422 1.6 christos struct bf_privdata *bp = privdata;
423 1.1 elric struct bf_encdata be;
424 1.1 elric
425 1.6 christos (void)memcpy(be.be_iv, iv, 8);
426 1.1 elric be.be_key = &bp->bp_key;
427 1.1 elric switch (dir) {
428 1.1 elric case CGD_CIPHER_ENCRYPT:
429 1.1 elric cgd_cipher_uio_cbc(&be, bf_cbc_enc_int, dstuio, srcuio);
430 1.1 elric break;
431 1.1 elric case CGD_CIPHER_DECRYPT:
432 1.1 elric cgd_cipher_uio_cbc(&be, bf_cbc_dec_int, dstuio, srcuio);
433 1.1 elric break;
434 1.1 elric default:
435 1.8 perry DIAGPANIC(("%s: unrecognised direction %d", __func__, dir));
436 1.1 elric }
437 1.1 elric
438 1.1 elric }
439