cryptosoft.c revision 1.46 1 1.46 riastrad /* $NetBSD: cryptosoft.c,v 1.46 2014/07/02 18:58:42 riastradh Exp $ */
2 1.1 jonathan /* $FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $ */
3 1.1 jonathan /* $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $ */
4 1.1 jonathan
5 1.1 jonathan /*
6 1.1 jonathan * The author of this code is Angelos D. Keromytis (angelos (at) cis.upenn.edu)
7 1.1 jonathan *
8 1.1 jonathan * This code was written by Angelos D. Keromytis in Athens, Greece, in
9 1.1 jonathan * February 2000. Network Security Technologies Inc. (NSTI) kindly
10 1.1 jonathan * supported the development of this code.
11 1.1 jonathan *
12 1.1 jonathan * Copyright (c) 2000, 2001 Angelos D. Keromytis
13 1.1 jonathan *
14 1.1 jonathan * Permission to use, copy, and modify this software with or without fee
15 1.1 jonathan * is hereby granted, provided that this entire notice is included in
16 1.1 jonathan * all source code copies of any software which is or includes a copy or
17 1.1 jonathan * modification of this software.
18 1.1 jonathan *
19 1.1 jonathan * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
20 1.1 jonathan * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
21 1.1 jonathan * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
22 1.1 jonathan * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
23 1.1 jonathan * PURPOSE.
24 1.1 jonathan */
25 1.1 jonathan
26 1.1 jonathan #include <sys/cdefs.h>
27 1.46 riastrad __KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.46 2014/07/02 18:58:42 riastradh Exp $");
28 1.1 jonathan
29 1.1 jonathan #include <sys/param.h>
30 1.1 jonathan #include <sys/systm.h>
31 1.1 jonathan #include <sys/malloc.h>
32 1.1 jonathan #include <sys/mbuf.h>
33 1.1 jonathan #include <sys/sysctl.h>
34 1.1 jonathan #include <sys/errno.h>
35 1.41 christos #include <sys/cprng.h>
36 1.44 pgoyette #include <sys/module.h>
37 1.44 pgoyette #include <sys/device.h>
38 1.5 jonathan
39 1.44 pgoyette #ifdef _KERNEL_OPT
40 1.20 tls #include "opt_ocf.h"
41 1.44 pgoyette #endif
42 1.44 pgoyette
43 1.1 jonathan #include <opencrypto/cryptodev.h>
44 1.1 jonathan #include <opencrypto/cryptosoft.h>
45 1.1 jonathan #include <opencrypto/xform.h>
46 1.1 jonathan
47 1.10 thorpej #include <opencrypto/cryptosoft_xform.c>
48 1.1 jonathan
49 1.10 thorpej union authctx {
50 1.10 thorpej MD5_CTX md5ctx;
51 1.10 thorpej SHA1_CTX sha1ctx;
52 1.10 thorpej RMD160_CTX rmd160ctx;
53 1.10 thorpej SHA256_CTX sha256ctx;
54 1.10 thorpej SHA384_CTX sha384ctx;
55 1.10 thorpej SHA512_CTX sha512ctx;
56 1.36 drochner aesxcbc_ctx aesxcbcctx;
57 1.37 drochner AES_GMAC_CTX aesgmacctx;
58 1.1 jonathan };
59 1.1 jonathan
60 1.1 jonathan struct swcr_data **swcr_sessions = NULL;
61 1.1 jonathan u_int32_t swcr_sesnum = 0;
62 1.1 jonathan int32_t swcr_id = -1;
63 1.1 jonathan
64 1.1 jonathan #define COPYBACK(x, a, b, c, d) \
65 1.1 jonathan (x) == CRYPTO_BUF_MBUF ? m_copyback((struct mbuf *)a,b,c,d) \
66 1.1 jonathan : cuio_copyback((struct uio *)a,b,c,d)
67 1.1 jonathan #define COPYDATA(x, a, b, c, d) \
68 1.1 jonathan (x) == CRYPTO_BUF_MBUF ? m_copydata((struct mbuf *)a,b,c,d) \
69 1.1 jonathan : cuio_copydata((struct uio *)a,b,c,d)
70 1.1 jonathan
71 1.27 drochner static int swcr_encdec(struct cryptodesc *, const struct swcr_data *, void *, int);
72 1.27 drochner static int swcr_compdec(struct cryptodesc *, const struct swcr_data *, void *, int, int *);
73 1.37 drochner static int swcr_combined(struct cryptop *, int);
74 1.1 jonathan static int swcr_process(void *, struct cryptop *, int);
75 1.1 jonathan static int swcr_newsession(void *, u_int32_t *, struct cryptoini *);
76 1.1 jonathan static int swcr_freesession(void *, u_int64_t);
77 1.1 jonathan
78 1.1 jonathan /*
79 1.1 jonathan * Apply a symmetric encryption/decryption algorithm.
80 1.1 jonathan */
81 1.1 jonathan static int
82 1.27 drochner swcr_encdec(struct cryptodesc *crd, const struct swcr_data *sw, void *bufv,
83 1.1 jonathan int outtype)
84 1.1 jonathan {
85 1.17 christos char *buf = bufv;
86 1.1 jonathan unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN], *idat;
87 1.1 jonathan unsigned char *ivp, piv[EALG_MAX_BLOCK_LEN];
88 1.10 thorpej const struct swcr_enc_xform *exf;
89 1.32 drochner int i, k, j, blks, ivlen;
90 1.1 jonathan int count, ind;
91 1.1 jonathan
92 1.1 jonathan exf = sw->sw_exf;
93 1.10 thorpej blks = exf->enc_xform->blocksize;
94 1.32 drochner ivlen = exf->enc_xform->ivsize;
95 1.32 drochner KASSERT(exf->reinit ? ivlen <= blks : ivlen == blks);
96 1.1 jonathan
97 1.1 jonathan /* Check for non-padded data */
98 1.1 jonathan if (crd->crd_len % blks)
99 1.1 jonathan return EINVAL;
100 1.1 jonathan
101 1.1 jonathan /* Initialize the IV */
102 1.1 jonathan if (crd->crd_flags & CRD_F_ENCRYPT) {
103 1.1 jonathan /* IV explicitly provided ? */
104 1.34 drochner if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
105 1.32 drochner memcpy(iv, crd->crd_iv, ivlen);
106 1.34 drochner if (exf->reinit)
107 1.34 drochner exf->reinit(sw->sw_kschedule, iv, 0);
108 1.34 drochner } else if (exf->reinit) {
109 1.34 drochner exf->reinit(sw->sw_kschedule, 0, iv);
110 1.34 drochner } else {
111 1.1 jonathan /* Get random IV */
112 1.1 jonathan for (i = 0;
113 1.31 drochner i + sizeof (u_int32_t) <= EALG_MAX_BLOCK_LEN;
114 1.1 jonathan i += sizeof (u_int32_t)) {
115 1.39 tls u_int32_t temp = cprng_fast32();
116 1.1 jonathan
117 1.25 tsutsui memcpy(iv + i, &temp, sizeof(u_int32_t));
118 1.1 jonathan }
119 1.1 jonathan /*
120 1.1 jonathan * What if the block size is not a multiple
121 1.1 jonathan * of sizeof (u_int32_t), which is the size of
122 1.1 jonathan * what arc4random() returns ?
123 1.1 jonathan */
124 1.1 jonathan if (EALG_MAX_BLOCK_LEN % sizeof (u_int32_t) != 0) {
125 1.39 tls u_int32_t temp = cprng_fast32();
126 1.1 jonathan
127 1.1 jonathan bcopy (&temp, iv + i,
128 1.1 jonathan EALG_MAX_BLOCK_LEN - i);
129 1.1 jonathan }
130 1.1 jonathan }
131 1.1 jonathan
132 1.1 jonathan /* Do we need to write the IV */
133 1.1 jonathan if (!(crd->crd_flags & CRD_F_IV_PRESENT)) {
134 1.32 drochner COPYBACK(outtype, buf, crd->crd_inject, ivlen, iv);
135 1.1 jonathan }
136 1.1 jonathan
137 1.1 jonathan } else { /* Decryption */
138 1.1 jonathan /* IV explicitly provided ? */
139 1.1 jonathan if (crd->crd_flags & CRD_F_IV_EXPLICIT)
140 1.32 drochner memcpy(iv, crd->crd_iv, ivlen);
141 1.1 jonathan else {
142 1.1 jonathan /* Get IV off buf */
143 1.32 drochner COPYDATA(outtype, buf, crd->crd_inject, ivlen, iv);
144 1.1 jonathan }
145 1.34 drochner if (exf->reinit)
146 1.34 drochner exf->reinit(sw->sw_kschedule, iv, 0);
147 1.1 jonathan }
148 1.1 jonathan
149 1.1 jonathan ivp = iv;
150 1.1 jonathan
151 1.1 jonathan if (outtype == CRYPTO_BUF_CONTIG) {
152 1.32 drochner if (exf->reinit) {
153 1.32 drochner for (i = crd->crd_skip;
154 1.32 drochner i < crd->crd_skip + crd->crd_len; i += blks) {
155 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
156 1.32 drochner exf->encrypt(sw->sw_kschedule, buf + i);
157 1.32 drochner } else {
158 1.32 drochner exf->decrypt(sw->sw_kschedule, buf + i);
159 1.32 drochner }
160 1.32 drochner }
161 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
162 1.1 jonathan for (i = crd->crd_skip;
163 1.1 jonathan i < crd->crd_skip + crd->crd_len; i += blks) {
164 1.1 jonathan /* XOR with the IV/previous block, as appropriate. */
165 1.1 jonathan if (i == crd->crd_skip)
166 1.1 jonathan for (k = 0; k < blks; k++)
167 1.1 jonathan buf[i + k] ^= ivp[k];
168 1.1 jonathan else
169 1.1 jonathan for (k = 0; k < blks; k++)
170 1.1 jonathan buf[i + k] ^= buf[i + k - blks];
171 1.1 jonathan exf->encrypt(sw->sw_kschedule, buf + i);
172 1.1 jonathan }
173 1.1 jonathan } else { /* Decrypt */
174 1.1 jonathan /*
175 1.1 jonathan * Start at the end, so we don't need to keep the encrypted
176 1.1 jonathan * block as the IV for the next block.
177 1.1 jonathan */
178 1.1 jonathan for (i = crd->crd_skip + crd->crd_len - blks;
179 1.1 jonathan i >= crd->crd_skip; i -= blks) {
180 1.1 jonathan exf->decrypt(sw->sw_kschedule, buf + i);
181 1.1 jonathan
182 1.1 jonathan /* XOR with the IV/previous block, as appropriate */
183 1.1 jonathan if (i == crd->crd_skip)
184 1.1 jonathan for (k = 0; k < blks; k++)
185 1.1 jonathan buf[i + k] ^= ivp[k];
186 1.1 jonathan else
187 1.1 jonathan for (k = 0; k < blks; k++)
188 1.1 jonathan buf[i + k] ^= buf[i + k - blks];
189 1.1 jonathan }
190 1.1 jonathan }
191 1.1 jonathan
192 1.1 jonathan return 0;
193 1.1 jonathan } else if (outtype == CRYPTO_BUF_MBUF) {
194 1.1 jonathan struct mbuf *m = (struct mbuf *) buf;
195 1.1 jonathan
196 1.1 jonathan /* Find beginning of data */
197 1.1 jonathan m = m_getptr(m, crd->crd_skip, &k);
198 1.1 jonathan if (m == NULL)
199 1.1 jonathan return EINVAL;
200 1.1 jonathan
201 1.1 jonathan i = crd->crd_len;
202 1.1 jonathan
203 1.1 jonathan while (i > 0) {
204 1.1 jonathan /*
205 1.1 jonathan * If there's insufficient data at the end of
206 1.1 jonathan * an mbuf, we have to do some copying.
207 1.1 jonathan */
208 1.1 jonathan if (m->m_len < k + blks && m->m_len != k) {
209 1.1 jonathan m_copydata(m, k, blks, blk);
210 1.1 jonathan
211 1.1 jonathan /* Actual encryption/decryption */
212 1.32 drochner if (exf->reinit) {
213 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
214 1.32 drochner exf->encrypt(sw->sw_kschedule,
215 1.32 drochner blk);
216 1.32 drochner } else {
217 1.32 drochner exf->decrypt(sw->sw_kschedule,
218 1.32 drochner blk);
219 1.32 drochner }
220 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
221 1.1 jonathan /* XOR with previous block */
222 1.1 jonathan for (j = 0; j < blks; j++)
223 1.1 jonathan blk[j] ^= ivp[j];
224 1.1 jonathan
225 1.1 jonathan exf->encrypt(sw->sw_kschedule, blk);
226 1.1 jonathan
227 1.1 jonathan /*
228 1.1 jonathan * Keep encrypted block for XOR'ing
229 1.1 jonathan * with next block
230 1.1 jonathan */
231 1.25 tsutsui memcpy(iv, blk, blks);
232 1.1 jonathan ivp = iv;
233 1.1 jonathan } else { /* decrypt */
234 1.1 jonathan /*
235 1.1 jonathan * Keep encrypted block for XOR'ing
236 1.1 jonathan * with next block
237 1.1 jonathan */
238 1.1 jonathan if (ivp == iv)
239 1.25 tsutsui memcpy(piv, blk, blks);
240 1.1 jonathan else
241 1.25 tsutsui memcpy(iv, blk, blks);
242 1.1 jonathan
243 1.1 jonathan exf->decrypt(sw->sw_kschedule, blk);
244 1.1 jonathan
245 1.1 jonathan /* XOR with previous block */
246 1.1 jonathan for (j = 0; j < blks; j++)
247 1.1 jonathan blk[j] ^= ivp[j];
248 1.1 jonathan
249 1.1 jonathan if (ivp == iv)
250 1.25 tsutsui memcpy(iv, piv, blks);
251 1.1 jonathan else
252 1.1 jonathan ivp = iv;
253 1.1 jonathan }
254 1.1 jonathan
255 1.1 jonathan /* Copy back decrypted block */
256 1.1 jonathan m_copyback(m, k, blks, blk);
257 1.1 jonathan
258 1.1 jonathan /* Advance pointer */
259 1.1 jonathan m = m_getptr(m, k + blks, &k);
260 1.1 jonathan if (m == NULL)
261 1.1 jonathan return EINVAL;
262 1.1 jonathan
263 1.1 jonathan i -= blks;
264 1.1 jonathan
265 1.1 jonathan /* Could be done... */
266 1.1 jonathan if (i == 0)
267 1.1 jonathan break;
268 1.1 jonathan }
269 1.1 jonathan
270 1.1 jonathan /* Skip possibly empty mbufs */
271 1.1 jonathan if (k == m->m_len) {
272 1.1 jonathan for (m = m->m_next; m && m->m_len == 0;
273 1.1 jonathan m = m->m_next)
274 1.1 jonathan ;
275 1.1 jonathan k = 0;
276 1.1 jonathan }
277 1.1 jonathan
278 1.1 jonathan /* Sanity check */
279 1.1 jonathan if (m == NULL)
280 1.1 jonathan return EINVAL;
281 1.1 jonathan
282 1.1 jonathan /*
283 1.1 jonathan * Warning: idat may point to garbage here, but
284 1.1 jonathan * we only use it in the while() loop, only if
285 1.1 jonathan * there are indeed enough data.
286 1.1 jonathan */
287 1.1 jonathan idat = mtod(m, unsigned char *) + k;
288 1.1 jonathan
289 1.1 jonathan while (m->m_len >= k + blks && i > 0) {
290 1.32 drochner if (exf->reinit) {
291 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
292 1.32 drochner exf->encrypt(sw->sw_kschedule,
293 1.32 drochner idat);
294 1.32 drochner } else {
295 1.32 drochner exf->decrypt(sw->sw_kschedule,
296 1.32 drochner idat);
297 1.32 drochner }
298 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
299 1.1 jonathan /* XOR with previous block/IV */
300 1.1 jonathan for (j = 0; j < blks; j++)
301 1.1 jonathan idat[j] ^= ivp[j];
302 1.1 jonathan
303 1.1 jonathan exf->encrypt(sw->sw_kschedule, idat);
304 1.1 jonathan ivp = idat;
305 1.1 jonathan } else { /* decrypt */
306 1.1 jonathan /*
307 1.1 jonathan * Keep encrypted block to be used
308 1.1 jonathan * in next block's processing.
309 1.1 jonathan */
310 1.1 jonathan if (ivp == iv)
311 1.25 tsutsui memcpy(piv, idat, blks);
312 1.1 jonathan else
313 1.25 tsutsui memcpy(iv, idat, blks);
314 1.1 jonathan
315 1.1 jonathan exf->decrypt(sw->sw_kschedule, idat);
316 1.1 jonathan
317 1.1 jonathan /* XOR with previous block/IV */
318 1.1 jonathan for (j = 0; j < blks; j++)
319 1.1 jonathan idat[j] ^= ivp[j];
320 1.1 jonathan
321 1.1 jonathan if (ivp == iv)
322 1.25 tsutsui memcpy(iv, piv, blks);
323 1.1 jonathan else
324 1.1 jonathan ivp = iv;
325 1.1 jonathan }
326 1.1 jonathan
327 1.1 jonathan idat += blks;
328 1.1 jonathan k += blks;
329 1.1 jonathan i -= blks;
330 1.1 jonathan }
331 1.1 jonathan }
332 1.1 jonathan
333 1.1 jonathan return 0; /* Done with mbuf encryption/decryption */
334 1.1 jonathan } else if (outtype == CRYPTO_BUF_IOV) {
335 1.1 jonathan struct uio *uio = (struct uio *) buf;
336 1.1 jonathan
337 1.1 jonathan /* Find beginning of data */
338 1.1 jonathan count = crd->crd_skip;
339 1.1 jonathan ind = cuio_getptr(uio, count, &k);
340 1.1 jonathan if (ind == -1)
341 1.1 jonathan return EINVAL;
342 1.1 jonathan
343 1.1 jonathan i = crd->crd_len;
344 1.1 jonathan
345 1.1 jonathan while (i > 0) {
346 1.1 jonathan /*
347 1.1 jonathan * If there's insufficient data at the end,
348 1.1 jonathan * we have to do some copying.
349 1.1 jonathan */
350 1.1 jonathan if (uio->uio_iov[ind].iov_len < k + blks &&
351 1.1 jonathan uio->uio_iov[ind].iov_len != k) {
352 1.1 jonathan cuio_copydata(uio, k, blks, blk);
353 1.1 jonathan
354 1.1 jonathan /* Actual encryption/decryption */
355 1.32 drochner if (exf->reinit) {
356 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
357 1.32 drochner exf->encrypt(sw->sw_kschedule,
358 1.32 drochner blk);
359 1.32 drochner } else {
360 1.32 drochner exf->decrypt(sw->sw_kschedule,
361 1.32 drochner blk);
362 1.32 drochner }
363 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
364 1.1 jonathan /* XOR with previous block */
365 1.1 jonathan for (j = 0; j < blks; j++)
366 1.1 jonathan blk[j] ^= ivp[j];
367 1.1 jonathan
368 1.1 jonathan exf->encrypt(sw->sw_kschedule, blk);
369 1.1 jonathan
370 1.1 jonathan /*
371 1.1 jonathan * Keep encrypted block for XOR'ing
372 1.1 jonathan * with next block
373 1.1 jonathan */
374 1.25 tsutsui memcpy(iv, blk, blks);
375 1.1 jonathan ivp = iv;
376 1.1 jonathan } else { /* decrypt */
377 1.1 jonathan /*
378 1.1 jonathan * Keep encrypted block for XOR'ing
379 1.1 jonathan * with next block
380 1.1 jonathan */
381 1.1 jonathan if (ivp == iv)
382 1.25 tsutsui memcpy(piv, blk, blks);
383 1.1 jonathan else
384 1.25 tsutsui memcpy(iv, blk, blks);
385 1.1 jonathan
386 1.1 jonathan exf->decrypt(sw->sw_kschedule, blk);
387 1.1 jonathan
388 1.1 jonathan /* XOR with previous block */
389 1.1 jonathan for (j = 0; j < blks; j++)
390 1.1 jonathan blk[j] ^= ivp[j];
391 1.1 jonathan
392 1.1 jonathan if (ivp == iv)
393 1.25 tsutsui memcpy(iv, piv, blks);
394 1.1 jonathan else
395 1.1 jonathan ivp = iv;
396 1.1 jonathan }
397 1.1 jonathan
398 1.1 jonathan /* Copy back decrypted block */
399 1.1 jonathan cuio_copyback(uio, k, blks, blk);
400 1.1 jonathan
401 1.1 jonathan count += blks;
402 1.1 jonathan
403 1.1 jonathan /* Advance pointer */
404 1.1 jonathan ind = cuio_getptr(uio, count, &k);
405 1.1 jonathan if (ind == -1)
406 1.1 jonathan return (EINVAL);
407 1.1 jonathan
408 1.1 jonathan i -= blks;
409 1.1 jonathan
410 1.1 jonathan /* Could be done... */
411 1.1 jonathan if (i == 0)
412 1.1 jonathan break;
413 1.1 jonathan }
414 1.1 jonathan
415 1.1 jonathan /*
416 1.1 jonathan * Warning: idat may point to garbage here, but
417 1.1 jonathan * we only use it in the while() loop, only if
418 1.1 jonathan * there are indeed enough data.
419 1.1 jonathan */
420 1.17 christos idat = ((char *)uio->uio_iov[ind].iov_base) + k;
421 1.1 jonathan
422 1.1 jonathan while (uio->uio_iov[ind].iov_len >= k + blks &&
423 1.1 jonathan i > 0) {
424 1.32 drochner if (exf->reinit) {
425 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
426 1.32 drochner exf->encrypt(sw->sw_kschedule,
427 1.32 drochner idat);
428 1.32 drochner } else {
429 1.32 drochner exf->decrypt(sw->sw_kschedule,
430 1.32 drochner idat);
431 1.32 drochner }
432 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
433 1.1 jonathan /* XOR with previous block/IV */
434 1.1 jonathan for (j = 0; j < blks; j++)
435 1.1 jonathan idat[j] ^= ivp[j];
436 1.1 jonathan
437 1.1 jonathan exf->encrypt(sw->sw_kschedule, idat);
438 1.1 jonathan ivp = idat;
439 1.1 jonathan } else { /* decrypt */
440 1.1 jonathan /*
441 1.1 jonathan * Keep encrypted block to be used
442 1.1 jonathan * in next block's processing.
443 1.1 jonathan */
444 1.1 jonathan if (ivp == iv)
445 1.25 tsutsui memcpy(piv, idat, blks);
446 1.1 jonathan else
447 1.25 tsutsui memcpy(iv, idat, blks);
448 1.1 jonathan
449 1.1 jonathan exf->decrypt(sw->sw_kschedule, idat);
450 1.1 jonathan
451 1.1 jonathan /* XOR with previous block/IV */
452 1.1 jonathan for (j = 0; j < blks; j++)
453 1.1 jonathan idat[j] ^= ivp[j];
454 1.1 jonathan
455 1.1 jonathan if (ivp == iv)
456 1.25 tsutsui memcpy(iv, piv, blks);
457 1.1 jonathan else
458 1.1 jonathan ivp = iv;
459 1.1 jonathan }
460 1.1 jonathan
461 1.1 jonathan idat += blks;
462 1.1 jonathan count += blks;
463 1.1 jonathan k += blks;
464 1.1 jonathan i -= blks;
465 1.1 jonathan }
466 1.1 jonathan }
467 1.1 jonathan return 0; /* Done with mbuf encryption/decryption */
468 1.1 jonathan }
469 1.1 jonathan
470 1.1 jonathan /* Unreachable */
471 1.1 jonathan return EINVAL;
472 1.1 jonathan }
473 1.1 jonathan
474 1.1 jonathan /*
475 1.1 jonathan * Compute keyed-hash authenticator.
476 1.1 jonathan */
477 1.16 daniel int
478 1.1 jonathan swcr_authcompute(struct cryptop *crp, struct cryptodesc *crd,
479 1.27 drochner const struct swcr_data *sw, void *buf, int outtype)
480 1.1 jonathan {
481 1.1 jonathan unsigned char aalg[AALG_MAX_RESULT_LEN];
482 1.10 thorpej const struct swcr_auth_hash *axf;
483 1.1 jonathan union authctx ctx;
484 1.1 jonathan int err;
485 1.1 jonathan
486 1.1 jonathan if (sw->sw_ictx == 0)
487 1.1 jonathan return EINVAL;
488 1.1 jonathan
489 1.1 jonathan axf = sw->sw_axf;
490 1.1 jonathan
491 1.35 drochner memcpy(&ctx, sw->sw_ictx, axf->ctxsize);
492 1.1 jonathan
493 1.1 jonathan switch (outtype) {
494 1.1 jonathan case CRYPTO_BUF_CONTIG:
495 1.17 christos axf->Update(&ctx, (char *)buf + crd->crd_skip, crd->crd_len);
496 1.1 jonathan break;
497 1.1 jonathan case CRYPTO_BUF_MBUF:
498 1.1 jonathan err = m_apply((struct mbuf *) buf, crd->crd_skip, crd->crd_len,
499 1.17 christos (int (*)(void*, void *, unsigned int)) axf->Update,
500 1.17 christos (void *) &ctx);
501 1.1 jonathan if (err)
502 1.1 jonathan return err;
503 1.1 jonathan break;
504 1.1 jonathan case CRYPTO_BUF_IOV:
505 1.2 jonathan err = cuio_apply((struct uio *) buf, crd->crd_skip,
506 1.2 jonathan crd->crd_len,
507 1.17 christos (int (*)(void *, void *, unsigned int)) axf->Update,
508 1.17 christos (void *) &ctx);
509 1.2 jonathan if (err) {
510 1.2 jonathan return err;
511 1.2 jonathan }
512 1.2 jonathan break;
513 1.1 jonathan default:
514 1.1 jonathan return EINVAL;
515 1.1 jonathan }
516 1.1 jonathan
517 1.1 jonathan switch (sw->sw_alg) {
518 1.1 jonathan case CRYPTO_MD5_HMAC:
519 1.19 tls case CRYPTO_MD5_HMAC_96:
520 1.1 jonathan case CRYPTO_SHA1_HMAC:
521 1.19 tls case CRYPTO_SHA1_HMAC_96:
522 1.29 drochner case CRYPTO_SHA2_256_HMAC:
523 1.29 drochner case CRYPTO_SHA2_384_HMAC:
524 1.29 drochner case CRYPTO_SHA2_512_HMAC:
525 1.1 jonathan case CRYPTO_RIPEMD160_HMAC:
526 1.19 tls case CRYPTO_RIPEMD160_HMAC_96:
527 1.1 jonathan if (sw->sw_octx == NULL)
528 1.1 jonathan return EINVAL;
529 1.1 jonathan
530 1.1 jonathan axf->Final(aalg, &ctx);
531 1.35 drochner memcpy(&ctx, sw->sw_octx, axf->ctxsize);
532 1.10 thorpej axf->Update(&ctx, aalg, axf->auth_hash->hashsize);
533 1.1 jonathan axf->Final(aalg, &ctx);
534 1.1 jonathan break;
535 1.1 jonathan
536 1.1 jonathan case CRYPTO_MD5_KPDK:
537 1.1 jonathan case CRYPTO_SHA1_KPDK:
538 1.1 jonathan if (sw->sw_octx == NULL)
539 1.1 jonathan return EINVAL;
540 1.1 jonathan
541 1.1 jonathan axf->Update(&ctx, sw->sw_octx, sw->sw_klen);
542 1.1 jonathan axf->Final(aalg, &ctx);
543 1.1 jonathan break;
544 1.1 jonathan
545 1.1 jonathan case CRYPTO_NULL_HMAC:
546 1.1 jonathan case CRYPTO_MD5:
547 1.1 jonathan case CRYPTO_SHA1:
548 1.36 drochner case CRYPTO_AES_XCBC_MAC_96:
549 1.1 jonathan axf->Final(aalg, &ctx);
550 1.1 jonathan break;
551 1.1 jonathan }
552 1.1 jonathan
553 1.1 jonathan /* Inject the authentication data */
554 1.2 jonathan switch (outtype) {
555 1.2 jonathan case CRYPTO_BUF_CONTIG:
556 1.17 christos (void)memcpy((char *)buf + crd->crd_inject, aalg,
557 1.17 christos axf->auth_hash->authsize);
558 1.2 jonathan break;
559 1.2 jonathan case CRYPTO_BUF_MBUF:
560 1.1 jonathan m_copyback((struct mbuf *) buf, crd->crd_inject,
561 1.10 thorpej axf->auth_hash->authsize, aalg);
562 1.2 jonathan break;
563 1.2 jonathan case CRYPTO_BUF_IOV:
564 1.25 tsutsui memcpy(crp->crp_mac, aalg, axf->auth_hash->authsize);
565 1.2 jonathan break;
566 1.2 jonathan default:
567 1.2 jonathan return EINVAL;
568 1.2 jonathan }
569 1.1 jonathan return 0;
570 1.1 jonathan }
571 1.1 jonathan
572 1.1 jonathan /*
573 1.37 drochner * Apply a combined encryption-authentication transformation
574 1.37 drochner */
575 1.37 drochner static int
576 1.37 drochner swcr_combined(struct cryptop *crp, int outtype)
577 1.37 drochner {
578 1.37 drochner uint32_t blkbuf[howmany(EALG_MAX_BLOCK_LEN, sizeof(uint32_t))];
579 1.37 drochner u_char *blk = (u_char *)blkbuf;
580 1.37 drochner u_char aalg[AALG_MAX_RESULT_LEN];
581 1.37 drochner u_char iv[EALG_MAX_BLOCK_LEN];
582 1.37 drochner union authctx ctx;
583 1.37 drochner struct cryptodesc *crd, *crda = NULL, *crde = NULL;
584 1.37 drochner struct swcr_data *sw, *swa, *swe = NULL;
585 1.37 drochner const struct swcr_auth_hash *axf = NULL;
586 1.37 drochner const struct swcr_enc_xform *exf = NULL;
587 1.37 drochner void *buf = (void *)crp->crp_buf;
588 1.37 drochner uint32_t *blkp;
589 1.37 drochner int i, blksz = 0, ivlen = 0, len;
590 1.37 drochner
591 1.37 drochner for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
592 1.37 drochner for (sw = swcr_sessions[crp->crp_sid & 0xffffffff];
593 1.37 drochner sw && sw->sw_alg != crd->crd_alg;
594 1.37 drochner sw = sw->sw_next)
595 1.37 drochner ;
596 1.37 drochner if (sw == NULL)
597 1.37 drochner return (EINVAL);
598 1.37 drochner
599 1.37 drochner switch (sw->sw_alg) {
600 1.37 drochner case CRYPTO_AES_GCM_16:
601 1.37 drochner case CRYPTO_AES_GMAC:
602 1.37 drochner swe = sw;
603 1.37 drochner crde = crd;
604 1.37 drochner exf = swe->sw_exf;
605 1.37 drochner ivlen = exf->enc_xform->ivsize;
606 1.37 drochner break;
607 1.37 drochner case CRYPTO_AES_128_GMAC:
608 1.37 drochner case CRYPTO_AES_192_GMAC:
609 1.37 drochner case CRYPTO_AES_256_GMAC:
610 1.37 drochner swa = sw;
611 1.37 drochner crda = crd;
612 1.37 drochner axf = swa->sw_axf;
613 1.37 drochner if (swa->sw_ictx == 0)
614 1.37 drochner return (EINVAL);
615 1.37 drochner memcpy(&ctx, swa->sw_ictx, axf->ctxsize);
616 1.37 drochner blksz = axf->auth_hash->blocksize;
617 1.37 drochner break;
618 1.37 drochner default:
619 1.37 drochner return (EINVAL);
620 1.37 drochner }
621 1.37 drochner }
622 1.37 drochner if (crde == NULL || crda == NULL)
623 1.37 drochner return (EINVAL);
624 1.37 drochner if (outtype == CRYPTO_BUF_CONTIG)
625 1.37 drochner return (EINVAL);
626 1.37 drochner
627 1.37 drochner /* Initialize the IV */
628 1.37 drochner if (crde->crd_flags & CRD_F_ENCRYPT) {
629 1.37 drochner /* IV explicitly provided ? */
630 1.37 drochner if (crde->crd_flags & CRD_F_IV_EXPLICIT) {
631 1.37 drochner memcpy(iv, crde->crd_iv, ivlen);
632 1.37 drochner if (exf->reinit)
633 1.37 drochner exf->reinit(swe->sw_kschedule, iv, 0);
634 1.37 drochner } else if (exf->reinit)
635 1.37 drochner exf->reinit(swe->sw_kschedule, 0, iv);
636 1.37 drochner else
637 1.39 tls cprng_fast(iv, ivlen);
638 1.37 drochner
639 1.37 drochner /* Do we need to write the IV */
640 1.37 drochner if (!(crde->crd_flags & CRD_F_IV_PRESENT))
641 1.37 drochner COPYBACK(outtype, buf, crde->crd_inject, ivlen, iv);
642 1.37 drochner
643 1.37 drochner } else { /* Decryption */
644 1.37 drochner /* IV explicitly provided ? */
645 1.37 drochner if (crde->crd_flags & CRD_F_IV_EXPLICIT)
646 1.37 drochner memcpy(iv, crde->crd_iv, ivlen);
647 1.37 drochner else {
648 1.37 drochner /* Get IV off buf */
649 1.37 drochner COPYDATA(outtype, buf, crde->crd_inject, ivlen, iv);
650 1.37 drochner }
651 1.37 drochner if (exf->reinit)
652 1.37 drochner exf->reinit(swe->sw_kschedule, iv, 0);
653 1.37 drochner }
654 1.37 drochner
655 1.37 drochner /* Supply MAC with IV */
656 1.37 drochner if (axf->Reinit)
657 1.37 drochner axf->Reinit(&ctx, iv, ivlen);
658 1.37 drochner
659 1.37 drochner /* Supply MAC with AAD */
660 1.37 drochner for (i = 0; i < crda->crd_len; i += blksz) {
661 1.37 drochner len = MIN(crda->crd_len - i, blksz);
662 1.37 drochner COPYDATA(outtype, buf, crda->crd_skip + i, len, blk);
663 1.37 drochner axf->Update(&ctx, blk, len);
664 1.37 drochner }
665 1.37 drochner
666 1.37 drochner /* Do encryption/decryption with MAC */
667 1.37 drochner for (i = 0; i < crde->crd_len; i += blksz) {
668 1.37 drochner len = MIN(crde->crd_len - i, blksz);
669 1.37 drochner if (len < blksz)
670 1.37 drochner memset(blk, 0, blksz);
671 1.37 drochner COPYDATA(outtype, buf, crde->crd_skip + i, len, blk);
672 1.37 drochner if (crde->crd_flags & CRD_F_ENCRYPT) {
673 1.37 drochner exf->encrypt(swe->sw_kschedule, blk);
674 1.37 drochner axf->Update(&ctx, blk, len);
675 1.37 drochner } else {
676 1.37 drochner axf->Update(&ctx, blk, len);
677 1.37 drochner exf->decrypt(swe->sw_kschedule, blk);
678 1.37 drochner }
679 1.37 drochner COPYBACK(outtype, buf, crde->crd_skip + i, len, blk);
680 1.37 drochner }
681 1.37 drochner
682 1.37 drochner /* Do any required special finalization */
683 1.37 drochner switch (crda->crd_alg) {
684 1.37 drochner case CRYPTO_AES_128_GMAC:
685 1.37 drochner case CRYPTO_AES_192_GMAC:
686 1.37 drochner case CRYPTO_AES_256_GMAC:
687 1.37 drochner /* length block */
688 1.37 drochner memset(blk, 0, blksz);
689 1.37 drochner blkp = (uint32_t *)blk + 1;
690 1.37 drochner *blkp = htobe32(crda->crd_len * 8);
691 1.37 drochner blkp = (uint32_t *)blk + 3;
692 1.37 drochner *blkp = htobe32(crde->crd_len * 8);
693 1.37 drochner axf->Update(&ctx, blk, blksz);
694 1.37 drochner break;
695 1.37 drochner }
696 1.37 drochner
697 1.37 drochner /* Finalize MAC */
698 1.37 drochner axf->Final(aalg, &ctx);
699 1.37 drochner
700 1.37 drochner /* Inject the authentication data */
701 1.37 drochner if (outtype == CRYPTO_BUF_MBUF)
702 1.37 drochner COPYBACK(outtype, buf, crda->crd_inject, axf->auth_hash->authsize, aalg);
703 1.37 drochner else
704 1.37 drochner memcpy(crp->crp_mac, aalg, axf->auth_hash->authsize);
705 1.37 drochner
706 1.37 drochner return (0);
707 1.37 drochner }
708 1.37 drochner
709 1.37 drochner /*
710 1.1 jonathan * Apply a compression/decompression algorithm
711 1.1 jonathan */
712 1.1 jonathan static int
713 1.27 drochner swcr_compdec(struct cryptodesc *crd, const struct swcr_data *sw,
714 1.27 drochner void *buf, int outtype, int *res_size)
715 1.1 jonathan {
716 1.1 jonathan u_int8_t *data, *out;
717 1.10 thorpej const struct swcr_comp_algo *cxf;
718 1.1 jonathan int adj;
719 1.1 jonathan u_int32_t result;
720 1.1 jonathan
721 1.1 jonathan cxf = sw->sw_cxf;
722 1.1 jonathan
723 1.1 jonathan /* We must handle the whole buffer of data in one time
724 1.1 jonathan * then if there is not all the data in the mbuf, we must
725 1.1 jonathan * copy in a buffer.
726 1.1 jonathan */
727 1.1 jonathan
728 1.12 christos data = malloc(crd->crd_len, M_CRYPTO_DATA, M_NOWAIT);
729 1.1 jonathan if (data == NULL)
730 1.1 jonathan return (EINVAL);
731 1.1 jonathan COPYDATA(outtype, buf, crd->crd_skip, crd->crd_len, data);
732 1.1 jonathan
733 1.1 jonathan if (crd->crd_flags & CRD_F_COMP)
734 1.1 jonathan result = cxf->compress(data, crd->crd_len, &out);
735 1.1 jonathan else
736 1.28 drochner result = cxf->decompress(data, crd->crd_len, &out,
737 1.28 drochner *res_size);
738 1.1 jonathan
739 1.21 cegger free(data, M_CRYPTO_DATA);
740 1.1 jonathan if (result == 0)
741 1.1 jonathan return EINVAL;
742 1.1 jonathan
743 1.1 jonathan /* Copy back the (de)compressed data. m_copyback is
744 1.1 jonathan * extending the mbuf as necessary.
745 1.1 jonathan */
746 1.27 drochner *res_size = (int)result;
747 1.1 jonathan /* Check the compressed size when doing compression */
748 1.28 drochner if (crd->crd_flags & CRD_F_COMP &&
749 1.28 drochner sw->sw_alg == CRYPTO_DEFLATE_COMP_NOGROW &&
750 1.28 drochner result >= crd->crd_len) {
751 1.1 jonathan /* Compression was useless, we lost time */
752 1.21 cegger free(out, M_CRYPTO_DATA);
753 1.1 jonathan return 0;
754 1.1 jonathan }
755 1.1 jonathan
756 1.1 jonathan COPYBACK(outtype, buf, crd->crd_skip, result, out);
757 1.1 jonathan if (result < crd->crd_len) {
758 1.1 jonathan adj = result - crd->crd_len;
759 1.1 jonathan if (outtype == CRYPTO_BUF_MBUF) {
760 1.1 jonathan adj = result - crd->crd_len;
761 1.1 jonathan m_adj((struct mbuf *)buf, adj);
762 1.1 jonathan }
763 1.24 darran /* Don't adjust the iov_len, it breaks the kmem_free */
764 1.1 jonathan }
765 1.21 cegger free(out, M_CRYPTO_DATA);
766 1.1 jonathan return 0;
767 1.1 jonathan }
768 1.1 jonathan
769 1.1 jonathan /*
770 1.1 jonathan * Generate a new software session.
771 1.1 jonathan */
772 1.1 jonathan static int
773 1.15 christos swcr_newsession(void *arg, u_int32_t *sid, struct cryptoini *cri)
774 1.1 jonathan {
775 1.1 jonathan struct swcr_data **swd;
776 1.10 thorpej const struct swcr_auth_hash *axf;
777 1.10 thorpej const struct swcr_enc_xform *txf;
778 1.10 thorpej const struct swcr_comp_algo *cxf;
779 1.1 jonathan u_int32_t i;
780 1.1 jonathan int k, error;
781 1.1 jonathan
782 1.1 jonathan if (sid == NULL || cri == NULL)
783 1.1 jonathan return EINVAL;
784 1.1 jonathan
785 1.1 jonathan if (swcr_sessions) {
786 1.1 jonathan for (i = 1; i < swcr_sesnum; i++)
787 1.1 jonathan if (swcr_sessions[i] == NULL)
788 1.1 jonathan break;
789 1.1 jonathan } else
790 1.1 jonathan i = 1; /* NB: to silence compiler warning */
791 1.1 jonathan
792 1.1 jonathan if (swcr_sessions == NULL || i == swcr_sesnum) {
793 1.1 jonathan if (swcr_sessions == NULL) {
794 1.1 jonathan i = 1; /* We leave swcr_sessions[0] empty */
795 1.1 jonathan swcr_sesnum = CRYPTO_SW_SESSIONS;
796 1.1 jonathan } else
797 1.1 jonathan swcr_sesnum *= 2;
798 1.1 jonathan
799 1.1 jonathan swd = malloc(swcr_sesnum * sizeof(struct swcr_data *),
800 1.1 jonathan M_CRYPTO_DATA, M_NOWAIT);
801 1.1 jonathan if (swd == NULL) {
802 1.1 jonathan /* Reset session number */
803 1.1 jonathan if (swcr_sesnum == CRYPTO_SW_SESSIONS)
804 1.1 jonathan swcr_sesnum = 0;
805 1.1 jonathan else
806 1.1 jonathan swcr_sesnum /= 2;
807 1.1 jonathan return ENOBUFS;
808 1.1 jonathan }
809 1.1 jonathan
810 1.22 cegger memset(swd, 0, swcr_sesnum * sizeof(struct swcr_data *));
811 1.1 jonathan
812 1.1 jonathan /* Copy existing sessions */
813 1.1 jonathan if (swcr_sessions) {
814 1.25 tsutsui memcpy(swd, swcr_sessions,
815 1.1 jonathan (swcr_sesnum / 2) * sizeof(struct swcr_data *));
816 1.1 jonathan free(swcr_sessions, M_CRYPTO_DATA);
817 1.1 jonathan }
818 1.1 jonathan
819 1.1 jonathan swcr_sessions = swd;
820 1.1 jonathan }
821 1.1 jonathan
822 1.1 jonathan swd = &swcr_sessions[i];
823 1.1 jonathan *sid = i;
824 1.1 jonathan
825 1.1 jonathan while (cri) {
826 1.13 dsl *swd = malloc(sizeof **swd, M_CRYPTO_DATA, M_NOWAIT);
827 1.1 jonathan if (*swd == NULL) {
828 1.1 jonathan swcr_freesession(NULL, i);
829 1.1 jonathan return ENOBUFS;
830 1.1 jonathan }
831 1.22 cegger memset(*swd, 0, sizeof(struct swcr_data));
832 1.1 jonathan
833 1.1 jonathan switch (cri->cri_alg) {
834 1.1 jonathan case CRYPTO_DES_CBC:
835 1.10 thorpej txf = &swcr_enc_xform_des;
836 1.1 jonathan goto enccommon;
837 1.1 jonathan case CRYPTO_3DES_CBC:
838 1.10 thorpej txf = &swcr_enc_xform_3des;
839 1.1 jonathan goto enccommon;
840 1.1 jonathan case CRYPTO_BLF_CBC:
841 1.10 thorpej txf = &swcr_enc_xform_blf;
842 1.1 jonathan goto enccommon;
843 1.1 jonathan case CRYPTO_CAST_CBC:
844 1.10 thorpej txf = &swcr_enc_xform_cast5;
845 1.1 jonathan goto enccommon;
846 1.1 jonathan case CRYPTO_SKIPJACK_CBC:
847 1.10 thorpej txf = &swcr_enc_xform_skipjack;
848 1.1 jonathan goto enccommon;
849 1.1 jonathan case CRYPTO_RIJNDAEL128_CBC:
850 1.10 thorpej txf = &swcr_enc_xform_rijndael128;
851 1.1 jonathan goto enccommon;
852 1.30 drochner case CRYPTO_CAMELLIA_CBC:
853 1.30 drochner txf = &swcr_enc_xform_camellia;
854 1.30 drochner goto enccommon;
855 1.33 drochner case CRYPTO_AES_CTR:
856 1.33 drochner txf = &swcr_enc_xform_aes_ctr;
857 1.33 drochner goto enccommon;
858 1.37 drochner case CRYPTO_AES_GCM_16:
859 1.37 drochner txf = &swcr_enc_xform_aes_gcm;
860 1.37 drochner goto enccommon;
861 1.38 drochner case CRYPTO_AES_GMAC:
862 1.38 drochner txf = &swcr_enc_xform_aes_gmac;
863 1.38 drochner goto enccommon;
864 1.1 jonathan case CRYPTO_NULL_CBC:
865 1.10 thorpej txf = &swcr_enc_xform_null;
866 1.1 jonathan goto enccommon;
867 1.1 jonathan enccommon:
868 1.1 jonathan error = txf->setkey(&((*swd)->sw_kschedule),
869 1.1 jonathan cri->cri_key, cri->cri_klen / 8);
870 1.1 jonathan if (error) {
871 1.1 jonathan swcr_freesession(NULL, i);
872 1.1 jonathan return error;
873 1.1 jonathan }
874 1.1 jonathan (*swd)->sw_exf = txf;
875 1.1 jonathan break;
876 1.1 jonathan
877 1.1 jonathan case CRYPTO_MD5_HMAC:
878 1.19 tls axf = &swcr_auth_hash_hmac_md5;
879 1.19 tls goto authcommon;
880 1.19 tls case CRYPTO_MD5_HMAC_96:
881 1.10 thorpej axf = &swcr_auth_hash_hmac_md5_96;
882 1.1 jonathan goto authcommon;
883 1.1 jonathan case CRYPTO_SHA1_HMAC:
884 1.19 tls axf = &swcr_auth_hash_hmac_sha1;
885 1.19 tls goto authcommon;
886 1.19 tls case CRYPTO_SHA1_HMAC_96:
887 1.10 thorpej axf = &swcr_auth_hash_hmac_sha1_96;
888 1.1 jonathan goto authcommon;
889 1.29 drochner case CRYPTO_SHA2_256_HMAC:
890 1.29 drochner axf = &swcr_auth_hash_hmac_sha2_256;
891 1.29 drochner goto authcommon;
892 1.29 drochner case CRYPTO_SHA2_384_HMAC:
893 1.29 drochner axf = &swcr_auth_hash_hmac_sha2_384;
894 1.29 drochner goto authcommon;
895 1.29 drochner case CRYPTO_SHA2_512_HMAC:
896 1.29 drochner axf = &swcr_auth_hash_hmac_sha2_512;
897 1.1 jonathan goto authcommon;
898 1.1 jonathan case CRYPTO_NULL_HMAC:
899 1.10 thorpej axf = &swcr_auth_hash_null;
900 1.1 jonathan goto authcommon;
901 1.1 jonathan case CRYPTO_RIPEMD160_HMAC:
902 1.19 tls axf = &swcr_auth_hash_hmac_ripemd_160;
903 1.19 tls goto authcommon;
904 1.19 tls case CRYPTO_RIPEMD160_HMAC_96:
905 1.10 thorpej axf = &swcr_auth_hash_hmac_ripemd_160_96;
906 1.19 tls goto authcommon; /* leave this for safety */
907 1.1 jonathan authcommon:
908 1.35 drochner (*swd)->sw_ictx = malloc(axf->ctxsize,
909 1.10 thorpej M_CRYPTO_DATA, M_NOWAIT);
910 1.1 jonathan if ((*swd)->sw_ictx == NULL) {
911 1.1 jonathan swcr_freesession(NULL, i);
912 1.1 jonathan return ENOBUFS;
913 1.1 jonathan }
914 1.1 jonathan
915 1.35 drochner (*swd)->sw_octx = malloc(axf->ctxsize,
916 1.10 thorpej M_CRYPTO_DATA, M_NOWAIT);
917 1.1 jonathan if ((*swd)->sw_octx == NULL) {
918 1.1 jonathan swcr_freesession(NULL, i);
919 1.1 jonathan return ENOBUFS;
920 1.1 jonathan }
921 1.1 jonathan
922 1.1 jonathan for (k = 0; k < cri->cri_klen / 8; k++)
923 1.1 jonathan cri->cri_key[k] ^= HMAC_IPAD_VAL;
924 1.1 jonathan
925 1.1 jonathan axf->Init((*swd)->sw_ictx);
926 1.1 jonathan axf->Update((*swd)->sw_ictx, cri->cri_key,
927 1.1 jonathan cri->cri_klen / 8);
928 1.1 jonathan axf->Update((*swd)->sw_ictx, hmac_ipad_buffer,
929 1.29 drochner axf->auth_hash->blocksize - (cri->cri_klen / 8));
930 1.1 jonathan
931 1.1 jonathan for (k = 0; k < cri->cri_klen / 8; k++)
932 1.1 jonathan cri->cri_key[k] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
933 1.1 jonathan
934 1.1 jonathan axf->Init((*swd)->sw_octx);
935 1.1 jonathan axf->Update((*swd)->sw_octx, cri->cri_key,
936 1.1 jonathan cri->cri_klen / 8);
937 1.1 jonathan axf->Update((*swd)->sw_octx, hmac_opad_buffer,
938 1.29 drochner axf->auth_hash->blocksize - (cri->cri_klen / 8));
939 1.1 jonathan
940 1.1 jonathan for (k = 0; k < cri->cri_klen / 8; k++)
941 1.1 jonathan cri->cri_key[k] ^= HMAC_OPAD_VAL;
942 1.1 jonathan (*swd)->sw_axf = axf;
943 1.1 jonathan break;
944 1.1 jonathan
945 1.1 jonathan case CRYPTO_MD5_KPDK:
946 1.10 thorpej axf = &swcr_auth_hash_key_md5;
947 1.1 jonathan goto auth2common;
948 1.1 jonathan
949 1.1 jonathan case CRYPTO_SHA1_KPDK:
950 1.10 thorpej axf = &swcr_auth_hash_key_sha1;
951 1.1 jonathan auth2common:
952 1.35 drochner (*swd)->sw_ictx = malloc(axf->ctxsize,
953 1.10 thorpej M_CRYPTO_DATA, M_NOWAIT);
954 1.1 jonathan if ((*swd)->sw_ictx == NULL) {
955 1.1 jonathan swcr_freesession(NULL, i);
956 1.1 jonathan return ENOBUFS;
957 1.1 jonathan }
958 1.1 jonathan
959 1.1 jonathan /* Store the key so we can "append" it to the payload */
960 1.1 jonathan (*swd)->sw_octx = malloc(cri->cri_klen / 8, M_CRYPTO_DATA,
961 1.1 jonathan M_NOWAIT);
962 1.1 jonathan if ((*swd)->sw_octx == NULL) {
963 1.1 jonathan swcr_freesession(NULL, i);
964 1.1 jonathan return ENOBUFS;
965 1.1 jonathan }
966 1.1 jonathan
967 1.1 jonathan (*swd)->sw_klen = cri->cri_klen / 8;
968 1.25 tsutsui memcpy((*swd)->sw_octx, cri->cri_key, cri->cri_klen / 8);
969 1.1 jonathan axf->Init((*swd)->sw_ictx);
970 1.1 jonathan axf->Update((*swd)->sw_ictx, cri->cri_key,
971 1.1 jonathan cri->cri_klen / 8);
972 1.1 jonathan axf->Final(NULL, (*swd)->sw_ictx);
973 1.1 jonathan (*swd)->sw_axf = axf;
974 1.1 jonathan break;
975 1.1 jonathan
976 1.1 jonathan case CRYPTO_MD5:
977 1.10 thorpej axf = &swcr_auth_hash_md5;
978 1.1 jonathan goto auth3common;
979 1.1 jonathan
980 1.1 jonathan case CRYPTO_SHA1:
981 1.10 thorpej axf = &swcr_auth_hash_sha1;
982 1.1 jonathan auth3common:
983 1.35 drochner (*swd)->sw_ictx = malloc(axf->ctxsize,
984 1.10 thorpej M_CRYPTO_DATA, M_NOWAIT);
985 1.1 jonathan if ((*swd)->sw_ictx == NULL) {
986 1.1 jonathan swcr_freesession(NULL, i);
987 1.1 jonathan return ENOBUFS;
988 1.1 jonathan }
989 1.1 jonathan
990 1.1 jonathan axf->Init((*swd)->sw_ictx);
991 1.1 jonathan (*swd)->sw_axf = axf;
992 1.1 jonathan break;
993 1.1 jonathan
994 1.36 drochner case CRYPTO_AES_XCBC_MAC_96:
995 1.36 drochner axf = &swcr_auth_hash_aes_xcbc_mac;
996 1.37 drochner goto auth4common;
997 1.37 drochner case CRYPTO_AES_128_GMAC:
998 1.37 drochner axf = &swcr_auth_hash_gmac_aes_128;
999 1.37 drochner goto auth4common;
1000 1.37 drochner case CRYPTO_AES_192_GMAC:
1001 1.37 drochner axf = &swcr_auth_hash_gmac_aes_192;
1002 1.37 drochner goto auth4common;
1003 1.37 drochner case CRYPTO_AES_256_GMAC:
1004 1.37 drochner axf = &swcr_auth_hash_gmac_aes_256;
1005 1.37 drochner auth4common:
1006 1.36 drochner (*swd)->sw_ictx = malloc(axf->ctxsize,
1007 1.36 drochner M_CRYPTO_DATA, M_NOWAIT);
1008 1.36 drochner if ((*swd)->sw_ictx == NULL) {
1009 1.36 drochner swcr_freesession(NULL, i);
1010 1.36 drochner return ENOBUFS;
1011 1.36 drochner }
1012 1.36 drochner axf->Init((*swd)->sw_ictx);
1013 1.36 drochner axf->Setkey((*swd)->sw_ictx,
1014 1.36 drochner cri->cri_key, cri->cri_klen / 8);
1015 1.36 drochner (*swd)->sw_axf = axf;
1016 1.36 drochner break;
1017 1.36 drochner
1018 1.1 jonathan case CRYPTO_DEFLATE_COMP:
1019 1.10 thorpej cxf = &swcr_comp_algo_deflate;
1020 1.1 jonathan (*swd)->sw_cxf = cxf;
1021 1.1 jonathan break;
1022 1.24 darran
1023 1.28 drochner case CRYPTO_DEFLATE_COMP_NOGROW:
1024 1.28 drochner cxf = &swcr_comp_algo_deflate_nogrow;
1025 1.28 drochner (*swd)->sw_cxf = cxf;
1026 1.28 drochner break;
1027 1.28 drochner
1028 1.24 darran case CRYPTO_GZIP_COMP:
1029 1.24 darran cxf = &swcr_comp_algo_gzip;
1030 1.24 darran (*swd)->sw_cxf = cxf;
1031 1.24 darran break;
1032 1.1 jonathan default:
1033 1.1 jonathan swcr_freesession(NULL, i);
1034 1.1 jonathan return EINVAL;
1035 1.1 jonathan }
1036 1.1 jonathan
1037 1.1 jonathan (*swd)->sw_alg = cri->cri_alg;
1038 1.1 jonathan cri = cri->cri_next;
1039 1.1 jonathan swd = &((*swd)->sw_next);
1040 1.1 jonathan }
1041 1.1 jonathan return 0;
1042 1.1 jonathan }
1043 1.1 jonathan
1044 1.1 jonathan /*
1045 1.1 jonathan * Free a session.
1046 1.1 jonathan */
1047 1.1 jonathan static int
1048 1.15 christos swcr_freesession(void *arg, u_int64_t tid)
1049 1.1 jonathan {
1050 1.1 jonathan struct swcr_data *swd;
1051 1.10 thorpej const struct swcr_enc_xform *txf;
1052 1.10 thorpej const struct swcr_auth_hash *axf;
1053 1.1 jonathan u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
1054 1.1 jonathan
1055 1.1 jonathan if (sid > swcr_sesnum || swcr_sessions == NULL ||
1056 1.1 jonathan swcr_sessions[sid] == NULL)
1057 1.1 jonathan return EINVAL;
1058 1.1 jonathan
1059 1.1 jonathan /* Silently accept and return */
1060 1.1 jonathan if (sid == 0)
1061 1.1 jonathan return 0;
1062 1.1 jonathan
1063 1.1 jonathan while ((swd = swcr_sessions[sid]) != NULL) {
1064 1.1 jonathan swcr_sessions[sid] = swd->sw_next;
1065 1.1 jonathan
1066 1.1 jonathan switch (swd->sw_alg) {
1067 1.1 jonathan case CRYPTO_DES_CBC:
1068 1.1 jonathan case CRYPTO_3DES_CBC:
1069 1.1 jonathan case CRYPTO_BLF_CBC:
1070 1.1 jonathan case CRYPTO_CAST_CBC:
1071 1.1 jonathan case CRYPTO_SKIPJACK_CBC:
1072 1.1 jonathan case CRYPTO_RIJNDAEL128_CBC:
1073 1.30 drochner case CRYPTO_CAMELLIA_CBC:
1074 1.33 drochner case CRYPTO_AES_CTR:
1075 1.37 drochner case CRYPTO_AES_GCM_16:
1076 1.38 drochner case CRYPTO_AES_GMAC:
1077 1.1 jonathan case CRYPTO_NULL_CBC:
1078 1.1 jonathan txf = swd->sw_exf;
1079 1.1 jonathan
1080 1.1 jonathan if (swd->sw_kschedule)
1081 1.1 jonathan txf->zerokey(&(swd->sw_kschedule));
1082 1.1 jonathan break;
1083 1.1 jonathan
1084 1.1 jonathan case CRYPTO_MD5_HMAC:
1085 1.19 tls case CRYPTO_MD5_HMAC_96:
1086 1.1 jonathan case CRYPTO_SHA1_HMAC:
1087 1.19 tls case CRYPTO_SHA1_HMAC_96:
1088 1.29 drochner case CRYPTO_SHA2_256_HMAC:
1089 1.29 drochner case CRYPTO_SHA2_384_HMAC:
1090 1.29 drochner case CRYPTO_SHA2_512_HMAC:
1091 1.1 jonathan case CRYPTO_RIPEMD160_HMAC:
1092 1.19 tls case CRYPTO_RIPEMD160_HMAC_96:
1093 1.1 jonathan case CRYPTO_NULL_HMAC:
1094 1.1 jonathan axf = swd->sw_axf;
1095 1.1 jonathan
1096 1.1 jonathan if (swd->sw_ictx) {
1097 1.42 riastrad explicit_memset(swd->sw_ictx, 0, axf->ctxsize);
1098 1.1 jonathan free(swd->sw_ictx, M_CRYPTO_DATA);
1099 1.1 jonathan }
1100 1.1 jonathan if (swd->sw_octx) {
1101 1.42 riastrad explicit_memset(swd->sw_octx, 0, axf->ctxsize);
1102 1.1 jonathan free(swd->sw_octx, M_CRYPTO_DATA);
1103 1.1 jonathan }
1104 1.1 jonathan break;
1105 1.1 jonathan
1106 1.1 jonathan case CRYPTO_MD5_KPDK:
1107 1.1 jonathan case CRYPTO_SHA1_KPDK:
1108 1.1 jonathan axf = swd->sw_axf;
1109 1.1 jonathan
1110 1.1 jonathan if (swd->sw_ictx) {
1111 1.42 riastrad explicit_memset(swd->sw_ictx, 0, axf->ctxsize);
1112 1.1 jonathan free(swd->sw_ictx, M_CRYPTO_DATA);
1113 1.1 jonathan }
1114 1.1 jonathan if (swd->sw_octx) {
1115 1.42 riastrad explicit_memset(swd->sw_octx, 0, swd->sw_klen);
1116 1.1 jonathan free(swd->sw_octx, M_CRYPTO_DATA);
1117 1.1 jonathan }
1118 1.1 jonathan break;
1119 1.1 jonathan
1120 1.1 jonathan case CRYPTO_MD5:
1121 1.1 jonathan case CRYPTO_SHA1:
1122 1.36 drochner case CRYPTO_AES_XCBC_MAC_96:
1123 1.37 drochner case CRYPTO_AES_128_GMAC:
1124 1.37 drochner case CRYPTO_AES_192_GMAC:
1125 1.37 drochner case CRYPTO_AES_256_GMAC:
1126 1.1 jonathan axf = swd->sw_axf;
1127 1.1 jonathan
1128 1.40 drochner if (swd->sw_ictx) {
1129 1.42 riastrad explicit_memset(swd->sw_ictx, 0, axf->ctxsize);
1130 1.1 jonathan free(swd->sw_ictx, M_CRYPTO_DATA);
1131 1.40 drochner }
1132 1.1 jonathan break;
1133 1.1 jonathan
1134 1.1 jonathan case CRYPTO_DEFLATE_COMP:
1135 1.28 drochner case CRYPTO_DEFLATE_COMP_NOGROW:
1136 1.24 darran case CRYPTO_GZIP_COMP:
1137 1.1 jonathan break;
1138 1.1 jonathan }
1139 1.1 jonathan
1140 1.21 cegger free(swd, M_CRYPTO_DATA);
1141 1.1 jonathan }
1142 1.1 jonathan return 0;
1143 1.1 jonathan }
1144 1.1 jonathan
1145 1.1 jonathan /*
1146 1.1 jonathan * Process a software request.
1147 1.1 jonathan */
1148 1.1 jonathan static int
1149 1.15 christos swcr_process(void *arg, struct cryptop *crp, int hint)
1150 1.1 jonathan {
1151 1.1 jonathan struct cryptodesc *crd;
1152 1.1 jonathan struct swcr_data *sw;
1153 1.1 jonathan u_int32_t lid;
1154 1.1 jonathan int type;
1155 1.1 jonathan
1156 1.1 jonathan /* Sanity check */
1157 1.1 jonathan if (crp == NULL)
1158 1.1 jonathan return EINVAL;
1159 1.1 jonathan
1160 1.1 jonathan if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
1161 1.1 jonathan crp->crp_etype = EINVAL;
1162 1.1 jonathan goto done;
1163 1.1 jonathan }
1164 1.1 jonathan
1165 1.1 jonathan lid = crp->crp_sid & 0xffffffff;
1166 1.1 jonathan if (lid >= swcr_sesnum || lid == 0 || swcr_sessions[lid] == NULL) {
1167 1.1 jonathan crp->crp_etype = ENOENT;
1168 1.1 jonathan goto done;
1169 1.1 jonathan }
1170 1.1 jonathan
1171 1.1 jonathan if (crp->crp_flags & CRYPTO_F_IMBUF) {
1172 1.1 jonathan type = CRYPTO_BUF_MBUF;
1173 1.1 jonathan } else if (crp->crp_flags & CRYPTO_F_IOV) {
1174 1.1 jonathan type = CRYPTO_BUF_IOV;
1175 1.1 jonathan } else {
1176 1.1 jonathan type = CRYPTO_BUF_CONTIG;
1177 1.1 jonathan }
1178 1.1 jonathan
1179 1.1 jonathan /* Go through crypto descriptors, processing as we go */
1180 1.1 jonathan for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
1181 1.1 jonathan /*
1182 1.1 jonathan * Find the crypto context.
1183 1.1 jonathan *
1184 1.1 jonathan * XXX Note that the logic here prevents us from having
1185 1.1 jonathan * XXX the same algorithm multiple times in a session
1186 1.1 jonathan * XXX (or rather, we can but it won't give us the right
1187 1.1 jonathan * XXX results). To do that, we'd need some way of differentiating
1188 1.1 jonathan * XXX between the various instances of an algorithm (so we can
1189 1.1 jonathan * XXX locate the correct crypto context).
1190 1.1 jonathan */
1191 1.1 jonathan for (sw = swcr_sessions[lid];
1192 1.1 jonathan sw && sw->sw_alg != crd->crd_alg;
1193 1.1 jonathan sw = sw->sw_next)
1194 1.1 jonathan ;
1195 1.1 jonathan
1196 1.1 jonathan /* No such context ? */
1197 1.1 jonathan if (sw == NULL) {
1198 1.1 jonathan crp->crp_etype = EINVAL;
1199 1.1 jonathan goto done;
1200 1.1 jonathan }
1201 1.1 jonathan
1202 1.1 jonathan switch (sw->sw_alg) {
1203 1.1 jonathan case CRYPTO_DES_CBC:
1204 1.1 jonathan case CRYPTO_3DES_CBC:
1205 1.1 jonathan case CRYPTO_BLF_CBC:
1206 1.1 jonathan case CRYPTO_CAST_CBC:
1207 1.1 jonathan case CRYPTO_SKIPJACK_CBC:
1208 1.1 jonathan case CRYPTO_RIJNDAEL128_CBC:
1209 1.30 drochner case CRYPTO_CAMELLIA_CBC:
1210 1.33 drochner case CRYPTO_AES_CTR:
1211 1.1 jonathan if ((crp->crp_etype = swcr_encdec(crd, sw,
1212 1.1 jonathan crp->crp_buf, type)) != 0)
1213 1.1 jonathan goto done;
1214 1.1 jonathan break;
1215 1.1 jonathan case CRYPTO_NULL_CBC:
1216 1.1 jonathan crp->crp_etype = 0;
1217 1.1 jonathan break;
1218 1.1 jonathan case CRYPTO_MD5_HMAC:
1219 1.19 tls case CRYPTO_MD5_HMAC_96:
1220 1.1 jonathan case CRYPTO_SHA1_HMAC:
1221 1.19 tls case CRYPTO_SHA1_HMAC_96:
1222 1.29 drochner case CRYPTO_SHA2_256_HMAC:
1223 1.29 drochner case CRYPTO_SHA2_384_HMAC:
1224 1.29 drochner case CRYPTO_SHA2_512_HMAC:
1225 1.1 jonathan case CRYPTO_RIPEMD160_HMAC:
1226 1.19 tls case CRYPTO_RIPEMD160_HMAC_96:
1227 1.1 jonathan case CRYPTO_NULL_HMAC:
1228 1.1 jonathan case CRYPTO_MD5_KPDK:
1229 1.1 jonathan case CRYPTO_SHA1_KPDK:
1230 1.1 jonathan case CRYPTO_MD5:
1231 1.1 jonathan case CRYPTO_SHA1:
1232 1.36 drochner case CRYPTO_AES_XCBC_MAC_96:
1233 1.1 jonathan if ((crp->crp_etype = swcr_authcompute(crp, crd, sw,
1234 1.1 jonathan crp->crp_buf, type)) != 0)
1235 1.1 jonathan goto done;
1236 1.1 jonathan break;
1237 1.1 jonathan
1238 1.37 drochner case CRYPTO_AES_GCM_16:
1239 1.37 drochner case CRYPTO_AES_GMAC:
1240 1.37 drochner case CRYPTO_AES_128_GMAC:
1241 1.37 drochner case CRYPTO_AES_192_GMAC:
1242 1.37 drochner case CRYPTO_AES_256_GMAC:
1243 1.37 drochner crp->crp_etype = swcr_combined(crp, type);
1244 1.37 drochner goto done;
1245 1.37 drochner
1246 1.1 jonathan case CRYPTO_DEFLATE_COMP:
1247 1.28 drochner case CRYPTO_DEFLATE_COMP_NOGROW:
1248 1.24 darran case CRYPTO_GZIP_COMP:
1249 1.24 darran DPRINTF(("swcr_process: compdec for %d\n", sw->sw_alg));
1250 1.9 perry if ((crp->crp_etype = swcr_compdec(crd, sw,
1251 1.27 drochner crp->crp_buf, type, &crp->crp_olen)) != 0)
1252 1.1 jonathan goto done;
1253 1.1 jonathan break;
1254 1.1 jonathan
1255 1.1 jonathan default:
1256 1.1 jonathan /* Unknown/unsupported algorithm */
1257 1.1 jonathan crp->crp_etype = EINVAL;
1258 1.1 jonathan goto done;
1259 1.1 jonathan }
1260 1.1 jonathan }
1261 1.1 jonathan
1262 1.1 jonathan done:
1263 1.26 jakllsch DPRINTF(("request %p done\n", crp));
1264 1.1 jonathan crypto_done(crp);
1265 1.1 jonathan return 0;
1266 1.1 jonathan }
1267 1.1 jonathan
1268 1.10 thorpej static void
1269 1.1 jonathan swcr_init(void)
1270 1.1 jonathan {
1271 1.1 jonathan swcr_id = crypto_get_driverid(CRYPTOCAP_F_SOFTWARE);
1272 1.1 jonathan if (swcr_id < 0) {
1273 1.1 jonathan /* This should never happen */
1274 1.1 jonathan panic("Software crypto device cannot initialize!");
1275 1.1 jonathan }
1276 1.1 jonathan
1277 1.1 jonathan crypto_register(swcr_id, CRYPTO_DES_CBC,
1278 1.1 jonathan 0, 0, swcr_newsession, swcr_freesession, swcr_process, NULL);
1279 1.1 jonathan #define REGISTER(alg) \
1280 1.1 jonathan crypto_register(swcr_id, alg, 0, 0, NULL, NULL, NULL, NULL)
1281 1.1 jonathan
1282 1.1 jonathan REGISTER(CRYPTO_3DES_CBC);
1283 1.1 jonathan REGISTER(CRYPTO_BLF_CBC);
1284 1.1 jonathan REGISTER(CRYPTO_CAST_CBC);
1285 1.1 jonathan REGISTER(CRYPTO_SKIPJACK_CBC);
1286 1.30 drochner REGISTER(CRYPTO_CAMELLIA_CBC);
1287 1.33 drochner REGISTER(CRYPTO_AES_CTR);
1288 1.37 drochner REGISTER(CRYPTO_AES_GCM_16);
1289 1.37 drochner REGISTER(CRYPTO_AES_GMAC);
1290 1.1 jonathan REGISTER(CRYPTO_NULL_CBC);
1291 1.1 jonathan REGISTER(CRYPTO_MD5_HMAC);
1292 1.19 tls REGISTER(CRYPTO_MD5_HMAC_96);
1293 1.1 jonathan REGISTER(CRYPTO_SHA1_HMAC);
1294 1.19 tls REGISTER(CRYPTO_SHA1_HMAC_96);
1295 1.29 drochner REGISTER(CRYPTO_SHA2_256_HMAC);
1296 1.29 drochner REGISTER(CRYPTO_SHA2_384_HMAC);
1297 1.29 drochner REGISTER(CRYPTO_SHA2_512_HMAC);
1298 1.1 jonathan REGISTER(CRYPTO_RIPEMD160_HMAC);
1299 1.19 tls REGISTER(CRYPTO_RIPEMD160_HMAC_96);
1300 1.1 jonathan REGISTER(CRYPTO_NULL_HMAC);
1301 1.1 jonathan REGISTER(CRYPTO_MD5_KPDK);
1302 1.1 jonathan REGISTER(CRYPTO_SHA1_KPDK);
1303 1.1 jonathan REGISTER(CRYPTO_MD5);
1304 1.1 jonathan REGISTER(CRYPTO_SHA1);
1305 1.36 drochner REGISTER(CRYPTO_AES_XCBC_MAC_96);
1306 1.37 drochner REGISTER(CRYPTO_AES_128_GMAC);
1307 1.37 drochner REGISTER(CRYPTO_AES_192_GMAC);
1308 1.37 drochner REGISTER(CRYPTO_AES_256_GMAC);
1309 1.1 jonathan REGISTER(CRYPTO_RIJNDAEL128_CBC);
1310 1.1 jonathan REGISTER(CRYPTO_DEFLATE_COMP);
1311 1.28 drochner REGISTER(CRYPTO_DEFLATE_COMP_NOGROW);
1312 1.24 darran REGISTER(CRYPTO_GZIP_COMP);
1313 1.1 jonathan #undef REGISTER
1314 1.1 jonathan }
1315 1.1 jonathan
1316 1.10 thorpej
1317 1.10 thorpej /*
1318 1.10 thorpej * Pseudo-device init routine for software crypto.
1319 1.10 thorpej */
1320 1.11 thorpej void swcryptoattach(int);
1321 1.10 thorpej
1322 1.10 thorpej void
1323 1.15 christos swcryptoattach(int num)
1324 1.10 thorpej {
1325 1.10 thorpej
1326 1.10 thorpej swcr_init();
1327 1.10 thorpej }
1328 1.44 pgoyette
1329 1.44 pgoyette void swcrypto_attach(device_t, device_t, void *);
1330 1.44 pgoyette
1331 1.44 pgoyette void
1332 1.44 pgoyette swcrypto_attach(device_t parent, device_t self, void *opaque)
1333 1.44 pgoyette {
1334 1.44 pgoyette
1335 1.44 pgoyette swcr_init();
1336 1.45 christos
1337 1.45 christos if (!pmf_device_register(self, NULL, NULL))
1338 1.45 christos aprint_error_dev(self, "couldn't establish power handler\n");
1339 1.44 pgoyette }
1340 1.44 pgoyette
1341 1.44 pgoyette int swcrypto_detach(device_t, int);
1342 1.44 pgoyette
1343 1.44 pgoyette int
1344 1.44 pgoyette swcrypto_detach(device_t self, int flag)
1345 1.44 pgoyette {
1346 1.46 riastrad pmf_device_deregister(self);
1347 1.44 pgoyette if (swcr_id >= 0)
1348 1.44 pgoyette crypto_unregister_all(swcr_id);
1349 1.44 pgoyette return 0;
1350 1.44 pgoyette }
1351 1.44 pgoyette
1352 1.44 pgoyette int swcrypto_match(device_t, cfdata_t, void *);
1353 1.44 pgoyette
1354 1.44 pgoyette int
1355 1.44 pgoyette swcrypto_match(device_t parent, cfdata_t data, void *opaque)
1356 1.44 pgoyette {
1357 1.44 pgoyette
1358 1.44 pgoyette return 1;
1359 1.44 pgoyette }
1360 1.44 pgoyette
1361 1.44 pgoyette MODULE(MODULE_CLASS_DRIVER, swcrypto,
1362 1.44 pgoyette "opencrypto,zlib,blowfish,des,cast128,camellia,skipjack");
1363 1.44 pgoyette
1364 1.44 pgoyette CFDRIVER_DECL(swcrypto, DV_DULL, NULL);
1365 1.44 pgoyette
1366 1.44 pgoyette CFATTACH_DECL2_NEW(swcrypto, 0, swcrypto_match, swcrypto_attach,
1367 1.44 pgoyette swcrypto_detach, NULL, NULL, NULL);
1368 1.44 pgoyette
1369 1.44 pgoyette static int swcryptoloc[] = { -1, -1 };
1370 1.44 pgoyette
1371 1.44 pgoyette static struct cfdata swcrypto_cfdata[] = {
1372 1.44 pgoyette {
1373 1.44 pgoyette .cf_name = "swcrypto",
1374 1.44 pgoyette .cf_atname = "swcrypto",
1375 1.44 pgoyette .cf_unit = 0,
1376 1.44 pgoyette .cf_fstate = 0,
1377 1.44 pgoyette .cf_loc = swcryptoloc,
1378 1.44 pgoyette .cf_flags = 0,
1379 1.44 pgoyette .cf_pspec = NULL,
1380 1.44 pgoyette },
1381 1.44 pgoyette { NULL, NULL, 0, 0, NULL, 0, NULL }
1382 1.44 pgoyette };
1383 1.44 pgoyette
1384 1.44 pgoyette static int
1385 1.44 pgoyette swcrypto_modcmd(modcmd_t cmd, void *arg)
1386 1.44 pgoyette {
1387 1.44 pgoyette int error;
1388 1.44 pgoyette
1389 1.44 pgoyette switch (cmd) {
1390 1.44 pgoyette case MODULE_CMD_INIT:
1391 1.44 pgoyette error = config_cfdriver_attach(&swcrypto_cd);
1392 1.44 pgoyette if (error) {
1393 1.44 pgoyette return error;
1394 1.44 pgoyette }
1395 1.44 pgoyette
1396 1.44 pgoyette error = config_cfattach_attach(swcrypto_cd.cd_name,
1397 1.44 pgoyette &swcrypto_ca);
1398 1.44 pgoyette if (error) {
1399 1.44 pgoyette config_cfdriver_detach(&swcrypto_cd);
1400 1.44 pgoyette aprint_error("%s: unable to register cfattach\n",
1401 1.44 pgoyette swcrypto_cd.cd_name);
1402 1.44 pgoyette
1403 1.44 pgoyette return error;
1404 1.44 pgoyette }
1405 1.44 pgoyette
1406 1.44 pgoyette error = config_cfdata_attach(swcrypto_cfdata, 1);
1407 1.44 pgoyette if (error) {
1408 1.44 pgoyette config_cfattach_detach(swcrypto_cd.cd_name,
1409 1.44 pgoyette &swcrypto_ca);
1410 1.44 pgoyette config_cfdriver_detach(&swcrypto_cd);
1411 1.44 pgoyette aprint_error("%s: unable to register cfdata\n",
1412 1.44 pgoyette swcrypto_cd.cd_name);
1413 1.44 pgoyette
1414 1.44 pgoyette return error;
1415 1.44 pgoyette }
1416 1.44 pgoyette
1417 1.44 pgoyette (void)config_attach_pseudo(swcrypto_cfdata);
1418 1.44 pgoyette
1419 1.44 pgoyette return 0;
1420 1.44 pgoyette case MODULE_CMD_FINI:
1421 1.44 pgoyette error = config_cfdata_detach(swcrypto_cfdata);
1422 1.44 pgoyette if (error) {
1423 1.44 pgoyette return error;
1424 1.44 pgoyette }
1425 1.44 pgoyette
1426 1.44 pgoyette config_cfattach_detach(swcrypto_cd.cd_name, &swcrypto_ca);
1427 1.44 pgoyette config_cfdriver_detach(&swcrypto_cd);
1428 1.44 pgoyette
1429 1.44 pgoyette return 0;
1430 1.44 pgoyette default:
1431 1.44 pgoyette return ENOTTY;
1432 1.44 pgoyette }
1433 1.44 pgoyette }
1434