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