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