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