]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/opencrypto/cryptosoft.c
Remove support for keyed MD5 and SHA1 authentication hashes.
[FreeBSD/FreeBSD.git] / sys / opencrypto / cryptosoft.c
1 /*      $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $ */
2
3 /*-
4  * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
5  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
6  *
7  * This code was written by Angelos D. Keromytis in Athens, Greece, in
8  * February 2000. Network Security Technologies Inc. (NSTI) kindly
9  * supported the development of this code.
10  *
11  * Copyright (c) 2000, 2001 Angelos D. Keromytis
12  * Copyright (c) 2014 The FreeBSD Foundation
13  * All rights reserved.
14  *
15  * Portions of this software were developed by John-Mark Gurney
16  * under sponsorship of the FreeBSD Foundation and
17  * Rubicon Communications, LLC (Netgate).
18  *
19  * Permission to use, copy, and modify this software with or without fee
20  * is hereby granted, provided that this entire notice is included in
21  * all source code copies of any software which is or includes a copy or
22  * modification of this software.
23  *
24  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
25  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
26  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
27  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
28  * PURPOSE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/module.h>
39 #include <sys/sysctl.h>
40 #include <sys/errno.h>
41 #include <sys/random.h>
42 #include <sys/kernel.h>
43 #include <sys/uio.h>
44 #include <sys/lock.h>
45 #include <sys/rwlock.h>
46 #include <sys/endian.h>
47 #include <sys/limits.h>
48 #include <sys/mutex.h>
49
50 #include <crypto/blowfish/blowfish.h>
51 #include <crypto/sha1.h>
52 #include <opencrypto/rmd160.h>
53 #include <sys/md5.h>
54
55 #include <opencrypto/cryptodev.h>
56 #include <opencrypto/xform.h>
57
58 #include <sys/kobj.h>
59 #include <sys/bus.h>
60 #include "cryptodev_if.h"
61
62 struct swcr_auth {
63         void            *sw_ictx;
64         void            *sw_octx;
65         struct auth_hash *sw_axf;
66         uint16_t        sw_mlen;
67 };
68
69 struct swcr_encdec {
70         uint8_t         *sw_kschedule;
71         struct enc_xform *sw_exf;
72 };
73
74 struct swcr_compdec {
75         struct comp_algo *sw_cxf;
76 };
77
78 struct swcr_session {
79         struct mtx      swcr_lock;
80         int     (*swcr_process)(struct swcr_session *, struct cryptop *);
81
82         struct swcr_auth swcr_auth;
83         struct swcr_encdec swcr_encdec;
84         struct swcr_compdec swcr_compdec;
85 };
86
87 static  int32_t swcr_id;
88
89 static  void swcr_freesession(device_t dev, crypto_session_t cses);
90
91 /* Used for CRYPTO_NULL_CBC. */
92 static int
93 swcr_null(struct swcr_session *ses, struct cryptop *crp)
94 {
95
96         return (0);
97 }
98
99 /*
100  * Apply a symmetric encryption/decryption algorithm.
101  */
102 static int
103 swcr_encdec(struct swcr_session *ses, struct cryptop *crp)
104 {
105         unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN];
106         unsigned char *ivp, *nivp, iv2[EALG_MAX_BLOCK_LEN];
107         const struct crypto_session_params *csp;
108         struct swcr_encdec *sw;
109         struct enc_xform *exf;
110         int i, j, k, blks, ind, count, ivlen;
111         struct uio *uio, uiolcl;
112         struct iovec iovlcl[4];
113         struct iovec *iov;
114         int iovcnt, iovalloc;
115         int error;
116         bool encrypting;
117
118         error = 0;
119
120         sw = &ses->swcr_encdec;
121         exf = sw->sw_exf;
122         blks = exf->blocksize;
123         ivlen = exf->ivsize;
124
125         /* Check for non-padded data */
126         if ((crp->crp_payload_length % blks) != 0)
127                 return EINVAL;
128
129         if (exf == &enc_xform_aes_icm &&
130             (crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
131                 return (EINVAL);
132
133         crypto_read_iv(crp, iv);
134
135         if (crp->crp_cipher_key != NULL) {
136                 if (sw->sw_kschedule)
137                         exf->zerokey(&(sw->sw_kschedule));
138
139                 csp = crypto_get_params(crp->crp_session);
140                 error = exf->setkey(&sw->sw_kschedule,
141                     crp->crp_cipher_key, csp->csp_cipher_klen);
142                 if (error)
143                         return (error);
144         }
145
146         iov = iovlcl;
147         iovcnt = nitems(iovlcl);
148         iovalloc = 0;
149         uio = &uiolcl;
150         switch (crp->crp_buf_type) {
151         case CRYPTO_BUF_MBUF:
152                 error = crypto_mbuftoiov(crp->crp_mbuf, &iov, &iovcnt,
153                     &iovalloc);
154                 if (error)
155                         return (error);
156                 uio->uio_iov = iov;
157                 uio->uio_iovcnt = iovcnt;
158                 break;
159         case CRYPTO_BUF_UIO:
160                 uio = crp->crp_uio;
161                 break;
162         case CRYPTO_BUF_CONTIG:
163                 iov[0].iov_base = crp->crp_buf;
164                 iov[0].iov_len = crp->crp_ilen;
165                 uio->uio_iov = iov;
166                 uio->uio_iovcnt = 1;
167                 break;
168         }
169
170         ivp = iv;
171
172         if (exf->reinit) {
173                 /*
174                  * xforms that provide a reinit method perform all IV
175                  * handling themselves.
176                  */
177                 exf->reinit(sw->sw_kschedule, iv);
178         }
179
180         count = crp->crp_payload_start;
181         ind = cuio_getptr(uio, count, &k);
182         if (ind == -1) {
183                 error = EINVAL;
184                 goto out;
185         }
186
187         i = crp->crp_payload_length;
188         encrypting = CRYPTO_OP_IS_ENCRYPT(crp->crp_op);
189
190         while (i > 0) {
191                 /*
192                  * If there's insufficient data at the end of
193                  * an iovec, we have to do some copying.
194                  */
195                 if (uio->uio_iov[ind].iov_len < k + blks &&
196                     uio->uio_iov[ind].iov_len != k) {
197                         cuio_copydata(uio, count, blks, blk);
198
199                         /* Actual encryption/decryption */
200                         if (exf->reinit) {
201                                 if (encrypting) {
202                                         exf->encrypt(sw->sw_kschedule,
203                                             blk);
204                                 } else {
205                                         exf->decrypt(sw->sw_kschedule,
206                                             blk);
207                                 }
208                         } else if (encrypting) {
209                                 /* XOR with previous block */
210                                 for (j = 0; j < blks; j++)
211                                         blk[j] ^= ivp[j];
212
213                                 exf->encrypt(sw->sw_kschedule, blk);
214
215                                 /*
216                                  * Keep encrypted block for XOR'ing
217                                  * with next block
218                                  */
219                                 bcopy(blk, iv, blks);
220                                 ivp = iv;
221                         } else {        /* decrypt */
222                                 /*      
223                                  * Keep encrypted block for XOR'ing
224                                  * with next block
225                                  */
226                                 nivp = (ivp == iv) ? iv2 : iv;
227                                 bcopy(blk, nivp, blks);
228
229                                 exf->decrypt(sw->sw_kschedule, blk);
230
231                                 /* XOR with previous block */
232                                 for (j = 0; j < blks; j++)
233                                         blk[j] ^= ivp[j];
234
235                                 ivp = nivp;
236                         }
237
238                         /* Copy back decrypted block */
239                         cuio_copyback(uio, count, blks, blk);
240
241                         count += blks;
242
243                         /* Advance pointer */
244                         ind = cuio_getptr(uio, count, &k);
245                         if (ind == -1) {
246                                 error = EINVAL;
247                                 goto out;
248                         }
249
250                         i -= blks;
251
252                         /* Could be done... */
253                         if (i == 0)
254                                 break;
255                 }
256
257                 while (uio->uio_iov[ind].iov_len >= k + blks && i > 0) {
258                         uint8_t *idat;
259                         size_t nb, rem;
260
261                         nb = blks;
262                         rem = MIN((size_t)i,
263                             uio->uio_iov[ind].iov_len - (size_t)k);
264                         idat = (uint8_t *)uio->uio_iov[ind].iov_base + k;
265
266                         if (exf->reinit) {
267                                 if (encrypting && exf->encrypt_multi == NULL)
268                                         exf->encrypt(sw->sw_kschedule,
269                                             idat);
270                                 else if (encrypting) {
271                                         nb = rounddown(rem, blks);
272                                         exf->encrypt_multi(sw->sw_kschedule,
273                                             idat, nb);
274                                 } else if (exf->decrypt_multi == NULL)
275                                         exf->decrypt(sw->sw_kschedule,
276                                             idat);
277                                 else {
278                                         nb = rounddown(rem, blks);
279                                         exf->decrypt_multi(sw->sw_kschedule,
280                                             idat, nb);
281                                 }
282                         } else if (encrypting) {
283                                 /* XOR with previous block/IV */
284                                 for (j = 0; j < blks; j++)
285                                         idat[j] ^= ivp[j];
286
287                                 exf->encrypt(sw->sw_kschedule, idat);
288                                 ivp = idat;
289                         } else {        /* decrypt */
290                                 /*
291                                  * Keep encrypted block to be used
292                                  * in next block's processing.
293                                  */
294                                 nivp = (ivp == iv) ? iv2 : iv;
295                                 bcopy(idat, nivp, blks);
296
297                                 exf->decrypt(sw->sw_kschedule, idat);
298
299                                 /* XOR with previous block/IV */
300                                 for (j = 0; j < blks; j++)
301                                         idat[j] ^= ivp[j];
302
303                                 ivp = nivp;
304                         }
305
306                         count += nb;
307                         k += nb;
308                         i -= nb;
309                 }
310
311                 /*
312                  * Advance to the next iov if the end of the current iov
313                  * is aligned with the end of a cipher block.
314                  * Note that the code is equivalent to calling:
315                  *      ind = cuio_getptr(uio, count, &k);
316                  */
317                 if (i > 0 && k == uio->uio_iov[ind].iov_len) {
318                         k = 0;
319                         ind++;
320                         if (ind >= uio->uio_iovcnt) {
321                                 error = EINVAL;
322                                 goto out;
323                         }
324                 }
325         }
326
327 out:
328         if (iovalloc)
329                 free(iov, M_CRYPTO_DATA);
330
331         return (error);
332 }
333
334 static void
335 swcr_authprepare(struct auth_hash *axf, struct swcr_auth *sw,
336     const uint8_t *key, int klen)
337 {
338
339         switch (axf->type) {
340         case CRYPTO_MD5_HMAC:
341         case CRYPTO_SHA1_HMAC:
342         case CRYPTO_SHA2_224_HMAC:
343         case CRYPTO_SHA2_256_HMAC:
344         case CRYPTO_SHA2_384_HMAC:
345         case CRYPTO_SHA2_512_HMAC:
346         case CRYPTO_NULL_HMAC:
347         case CRYPTO_RIPEMD160_HMAC:
348                 hmac_init_ipad(axf, key, klen, sw->sw_ictx);
349                 hmac_init_opad(axf, key, klen, sw->sw_octx);
350                 break;
351         case CRYPTO_POLY1305:
352         case CRYPTO_BLAKE2B:
353         case CRYPTO_BLAKE2S:
354                 axf->Setkey(sw->sw_ictx, key, klen);
355                 axf->Init(sw->sw_ictx);
356                 break;
357         default:
358                 panic("%s: algorithm %d doesn't use keys", __func__, axf->type);
359         }
360 }
361
362 /*
363  * Compute or verify hash.
364  */
365 static int
366 swcr_authcompute(struct swcr_session *ses, struct cryptop *crp)
367 {
368         u_char aalg[HASH_MAX_LEN];
369         u_char uaalg[HASH_MAX_LEN];
370         const struct crypto_session_params *csp;
371         struct swcr_auth *sw;
372         struct auth_hash *axf;
373         union authctx ctx;
374         int err;
375
376         sw = &ses->swcr_auth;
377
378         axf = sw->sw_axf;
379
380         if (crp->crp_auth_key != NULL) {
381                 csp = crypto_get_params(crp->crp_session);
382                 swcr_authprepare(axf, sw, crp->crp_auth_key,
383                     csp->csp_auth_klen);
384         }
385
386         bcopy(sw->sw_ictx, &ctx, axf->ctxsize);
387
388         err = crypto_apply(crp, crp->crp_aad_start, crp->crp_aad_length,
389             (int (*)(void *, void *, unsigned int))axf->Update, &ctx);
390         if (err)
391                 return err;
392
393         err = crypto_apply(crp, crp->crp_payload_start, crp->crp_payload_length,
394             (int (*)(void *, void *, unsigned int))axf->Update, &ctx);
395         if (err)
396                 return err;
397
398         switch (axf->type) {
399         case CRYPTO_SHA1:
400         case CRYPTO_SHA2_224:
401         case CRYPTO_SHA2_256:
402         case CRYPTO_SHA2_384:
403         case CRYPTO_SHA2_512:
404                 axf->Final(aalg, &ctx);
405                 break;
406
407         case CRYPTO_MD5_HMAC:
408         case CRYPTO_SHA1_HMAC:
409         case CRYPTO_SHA2_224_HMAC:
410         case CRYPTO_SHA2_256_HMAC:
411         case CRYPTO_SHA2_384_HMAC:
412         case CRYPTO_SHA2_512_HMAC:
413         case CRYPTO_RIPEMD160_HMAC:
414                 if (sw->sw_octx == NULL)
415                         return EINVAL;
416
417                 axf->Final(aalg, &ctx);
418                 bcopy(sw->sw_octx, &ctx, axf->ctxsize);
419                 axf->Update(&ctx, aalg, axf->hashsize);
420                 axf->Final(aalg, &ctx);
421                 break;
422
423         case CRYPTO_BLAKE2B:
424         case CRYPTO_BLAKE2S:
425         case CRYPTO_NULL_HMAC:
426         case CRYPTO_POLY1305:
427                 axf->Final(aalg, &ctx);
428                 break;
429         }
430
431         if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
432                 crypto_copydata(crp, crp->crp_digest_start, sw->sw_mlen, uaalg);
433                 if (timingsafe_bcmp(aalg, uaalg, sw->sw_mlen) != 0)
434                         return (EBADMSG);
435         } else {
436                 /* Inject the authentication data */
437                 crypto_copyback(crp, crp->crp_digest_start, sw->sw_mlen, aalg);
438         }
439         return (0);
440 }
441
442 CTASSERT(INT_MAX <= (1ll<<39) - 256);   /* GCM: plain text < 2^39-256 */
443 CTASSERT(INT_MAX <= (uint64_t)-1);      /* GCM: associated data <= 2^64-1 */
444
445 static int
446 swcr_gmac(struct swcr_session *ses, struct cryptop *crp)
447 {
448         uint32_t blkbuf[howmany(EALG_MAX_BLOCK_LEN, sizeof(uint32_t))];
449         u_char *blk = (u_char *)blkbuf;
450         u_char aalg[AALG_MAX_RESULT_LEN];
451         u_char uaalg[AALG_MAX_RESULT_LEN];
452         u_char iv[EALG_MAX_BLOCK_LEN];
453         union authctx ctx;
454         struct swcr_auth *swa;
455         struct auth_hash *axf;
456         uint32_t *blkp;
457         int blksz, i, ivlen, len;
458
459         swa = &ses->swcr_auth;
460         axf = swa->sw_axf;
461
462         bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
463         blksz = axf->blocksize;
464
465         /* Initialize the IV */
466         ivlen = AES_GCM_IV_LEN;
467         crypto_read_iv(crp, iv);
468
469         axf->Reinit(&ctx, iv, ivlen);
470         for (i = 0; i < crp->crp_payload_length; i += blksz) {
471                 len = MIN(crp->crp_payload_length - i, blksz);
472                 crypto_copydata(crp, crp->crp_payload_start + i, len, blk);
473                 bzero(blk + len, blksz - len);
474                 axf->Update(&ctx, blk, blksz);
475         }
476
477         /* length block */
478         bzero(blk, blksz);
479         blkp = (uint32_t *)blk + 1;
480         *blkp = htobe32(crp->crp_payload_length * 8);
481         axf->Update(&ctx, blk, blksz);
482
483         /* Finalize MAC */
484         axf->Final(aalg, &ctx);
485
486         if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
487                 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
488                     uaalg);
489                 if (timingsafe_bcmp(aalg, uaalg, swa->sw_mlen) != 0)
490                         return (EBADMSG);
491         } else {
492                 /* Inject the authentication data */
493                 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, aalg);
494         }
495         return (0);
496 }
497
498 static int
499 swcr_gcm(struct swcr_session *ses, struct cryptop *crp)
500 {
501         uint32_t blkbuf[howmany(EALG_MAX_BLOCK_LEN, sizeof(uint32_t))];
502         u_char *blk = (u_char *)blkbuf;
503         u_char aalg[AALG_MAX_RESULT_LEN];
504         u_char uaalg[AALG_MAX_RESULT_LEN];
505         u_char iv[EALG_MAX_BLOCK_LEN];
506         union authctx ctx;
507         struct swcr_auth *swa;
508         struct swcr_encdec *swe;
509         struct auth_hash *axf;
510         struct enc_xform *exf;
511         uint32_t *blkp;
512         int blksz, i, ivlen, len, r;
513
514         swa = &ses->swcr_auth;
515         axf = swa->sw_axf;
516
517         bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
518         blksz = axf->blocksize;
519         
520         swe = &ses->swcr_encdec;
521         exf = swe->sw_exf;
522
523         if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
524                 return (EINVAL);
525
526         /* Initialize the IV */
527         ivlen = AES_GCM_IV_LEN;
528         bcopy(crp->crp_iv, iv, ivlen);
529
530         /* Supply MAC with IV */
531         axf->Reinit(&ctx, iv, ivlen);
532
533         /* Supply MAC with AAD */
534         for (i = 0; i < crp->crp_aad_length; i += blksz) {
535                 len = MIN(crp->crp_aad_length - i, blksz);
536                 crypto_copydata(crp, crp->crp_aad_start + i, len, blk);
537                 bzero(blk + len, blksz - len);
538                 axf->Update(&ctx, blk, blksz);
539         }
540
541         exf->reinit(swe->sw_kschedule, iv);
542
543         /* Do encryption with MAC */
544         for (i = 0; i < crp->crp_payload_length; i += len) {
545                 len = MIN(crp->crp_payload_length - i, blksz);
546                 if (len < blksz)
547                         bzero(blk, blksz);
548                 crypto_copydata(crp, crp->crp_payload_start + i, len, blk);
549                 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
550                         exf->encrypt(swe->sw_kschedule, blk);
551                         axf->Update(&ctx, blk, len);
552                         crypto_copyback(crp, crp->crp_payload_start + i, len,
553                             blk);
554                 } else {
555                         axf->Update(&ctx, blk, len);
556                 }
557         }
558
559         /* length block */
560         bzero(blk, blksz);
561         blkp = (uint32_t *)blk + 1;
562         *blkp = htobe32(crp->crp_aad_length * 8);
563         blkp = (uint32_t *)blk + 3;
564         *blkp = htobe32(crp->crp_payload_length * 8);
565         axf->Update(&ctx, blk, blksz);
566
567         /* Finalize MAC */
568         axf->Final(aalg, &ctx);
569
570         /* Validate tag */
571         if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
572                 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
573                     uaalg);
574
575                 r = timingsafe_bcmp(aalg, uaalg, swa->sw_mlen);
576                 if (r != 0)
577                         return (EBADMSG);
578                 
579                 /* tag matches, decrypt data */
580                 for (i = 0; i < crp->crp_payload_length; i += blksz) {
581                         len = MIN(crp->crp_payload_length - i, blksz);
582                         if (len < blksz)
583                                 bzero(blk, blksz);
584                         crypto_copydata(crp, crp->crp_payload_start + i, len,
585                             blk);
586                         exf->decrypt(swe->sw_kschedule, blk);
587                         crypto_copyback(crp, crp->crp_payload_start + i, len,
588                             blk);
589                 }
590         } else {
591                 /* Inject the authentication data */
592                 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen,
593                     aalg);
594         }
595
596         return (0);
597 }
598
599 static int
600 swcr_ccm_cbc_mac(struct swcr_session *ses, struct cryptop *crp)
601 {
602         uint32_t blkbuf[howmany(EALG_MAX_BLOCK_LEN, sizeof(uint32_t))];
603         u_char *blk = (u_char *)blkbuf;
604         u_char aalg[AALG_MAX_RESULT_LEN];
605         u_char uaalg[AALG_MAX_RESULT_LEN];
606         u_char iv[EALG_MAX_BLOCK_LEN];
607         union authctx ctx;
608         struct swcr_auth *swa;
609         struct auth_hash *axf;
610         int blksz, i, ivlen, len;
611
612         swa = &ses->swcr_auth;
613         axf = swa->sw_axf;
614
615         bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
616         blksz = axf->blocksize;
617
618         /* Initialize the IV */
619         ivlen = AES_CCM_IV_LEN;
620         crypto_read_iv(crp, iv);
621
622         /*
623          * AES CCM-CBC-MAC needs to know the length of both the auth
624          * data and payload data before doing the auth computation.
625          */
626         ctx.aes_cbc_mac_ctx.authDataLength = crp->crp_payload_length;
627         ctx.aes_cbc_mac_ctx.cryptDataLength = 0;
628
629         axf->Reinit(&ctx, iv, ivlen);
630         for (i = 0; i < crp->crp_payload_length; i += blksz) {
631                 len = MIN(crp->crp_payload_length - i, blksz);
632                 crypto_copydata(crp, crp->crp_payload_start + i, len, blk);
633                 bzero(blk + len, blksz - len);
634                 axf->Update(&ctx, blk, blksz);
635         }
636
637         /* Finalize MAC */
638         axf->Final(aalg, &ctx);
639
640         if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
641                 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
642                     uaalg);
643                 if (timingsafe_bcmp(aalg, uaalg, swa->sw_mlen) != 0)
644                         return (EBADMSG);
645         } else {
646                 /* Inject the authentication data */
647                 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, aalg);
648         }
649         return (0);
650 }
651
652 static int
653 swcr_ccm(struct swcr_session *ses, struct cryptop *crp)
654 {
655         uint32_t blkbuf[howmany(EALG_MAX_BLOCK_LEN, sizeof(uint32_t))];
656         u_char *blk = (u_char *)blkbuf;
657         u_char aalg[AALG_MAX_RESULT_LEN];
658         u_char uaalg[AALG_MAX_RESULT_LEN];
659         u_char iv[EALG_MAX_BLOCK_LEN];
660         union authctx ctx;
661         struct swcr_auth *swa;
662         struct swcr_encdec *swe;
663         struct auth_hash *axf;
664         struct enc_xform *exf;
665         int blksz, i, ivlen, len, r;
666
667         swa = &ses->swcr_auth;
668         axf = swa->sw_axf;
669
670         bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
671         blksz = axf->blocksize;
672         
673         swe = &ses->swcr_encdec;
674         exf = swe->sw_exf;
675
676         if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
677                 return (EINVAL);
678
679         /* Initialize the IV */
680         ivlen = AES_CCM_IV_LEN;
681         bcopy(crp->crp_iv, iv, ivlen);
682
683         /*
684          * AES CCM-CBC-MAC needs to know the length of both the auth
685          * data and payload data before doing the auth computation.
686          */
687         ctx.aes_cbc_mac_ctx.authDataLength = crp->crp_aad_length;
688         ctx.aes_cbc_mac_ctx.cryptDataLength = crp->crp_payload_length;
689
690         /* Supply MAC with IV */
691         axf->Reinit(&ctx, iv, ivlen);
692
693         /* Supply MAC with AAD */
694         for (i = 0; i < crp->crp_aad_length; i += blksz) {
695                 len = MIN(crp->crp_aad_length - i, blksz);
696                 crypto_copydata(crp, crp->crp_aad_start + i, len, blk);
697                 bzero(blk + len, blksz - len);
698                 axf->Update(&ctx, blk, blksz);
699         }
700
701         exf->reinit(swe->sw_kschedule, iv);
702
703         /* Do encryption/decryption with MAC */
704         for (i = 0; i < crp->crp_payload_length; i += len) {
705                 len = MIN(crp->crp_payload_length - i, blksz);
706                 if (len < blksz)
707                         bzero(blk, blksz);
708                 crypto_copydata(crp, crp->crp_payload_start + i, len, blk);
709                 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
710                         axf->Update(&ctx, blk, len);
711                         exf->encrypt(swe->sw_kschedule, blk);
712                         crypto_copyback(crp, crp->crp_payload_start + i, len,
713                             blk);
714                 } else {
715                         /*
716                          * One of the problems with CCM+CBC is that
717                          * the authentication is done on the
718                          * unecncrypted data.  As a result, we have to
719                          * decrypt the data twice: once to generate
720                          * the tag and a second time after the tag is
721                          * verified.
722                          */
723                         exf->decrypt(swe->sw_kschedule, blk);
724                         axf->Update(&ctx, blk, len);
725                 }
726         }
727
728         /* Finalize MAC */
729         axf->Final(aalg, &ctx);
730
731         /* Validate tag */
732         if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
733                 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
734                     uaalg);
735
736                 r = timingsafe_bcmp(aalg, uaalg, swa->sw_mlen);
737                 if (r != 0)
738                         return (EBADMSG);
739                 
740                 /* tag matches, decrypt data */
741                 exf->reinit(swe->sw_kschedule, iv);
742                 for (i = 0; i < crp->crp_payload_length; i += blksz) {
743                         len = MIN(crp->crp_payload_length - i, blksz);
744                         if (len < blksz)
745                                 bzero(blk, blksz);
746                         crypto_copydata(crp, crp->crp_payload_start + i, len,
747                             blk);
748                         exf->decrypt(swe->sw_kschedule, blk);
749                         crypto_copyback(crp, crp->crp_payload_start + i, len,
750                             blk);
751                 }
752         } else {
753                 /* Inject the authentication data */
754                 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen,
755                     aalg);
756         }
757
758         return (0);
759 }
760
761 /*
762  * Apply a cipher and a digest to perform EtA.
763  */
764 static int
765 swcr_eta(struct swcr_session *ses, struct cryptop *crp)
766 {
767         int error;
768
769         if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
770                 error = swcr_encdec(ses, crp);
771                 if (error == 0)
772                         error = swcr_authcompute(ses, crp);
773         } else {
774                 error = swcr_authcompute(ses, crp);
775                 if (error == 0)
776                         error = swcr_encdec(ses, crp);
777         }
778         return (error);
779 }
780
781 /*
782  * Apply a compression/decompression algorithm
783  */
784 static int
785 swcr_compdec(struct swcr_session *ses, struct cryptop *crp)
786 {
787         u_int8_t *data, *out;
788         struct comp_algo *cxf;
789         int adj;
790         u_int32_t result;
791
792         cxf = ses->swcr_compdec.sw_cxf;
793
794         /* We must handle the whole buffer of data in one time
795          * then if there is not all the data in the mbuf, we must
796          * copy in a buffer.
797          */
798
799         data = malloc(crp->crp_payload_length, M_CRYPTO_DATA,  M_NOWAIT);
800         if (data == NULL)
801                 return (EINVAL);
802         crypto_copydata(crp, crp->crp_payload_start, crp->crp_payload_length,
803             data);
804
805         if (CRYPTO_OP_IS_COMPRESS(crp->crp_op))
806                 result = cxf->compress(data, crp->crp_payload_length, &out);
807         else
808                 result = cxf->decompress(data, crp->crp_payload_length, &out);
809
810         free(data, M_CRYPTO_DATA);
811         if (result == 0)
812                 return (EINVAL);
813         crp->crp_olen = result;
814
815         /* Check the compressed size when doing compression */
816         if (CRYPTO_OP_IS_COMPRESS(crp->crp_op)) {
817                 if (result >= crp->crp_payload_length) {
818                         /* Compression was useless, we lost time */
819                         free(out, M_CRYPTO_DATA);
820                         return (0);
821                 }
822         }
823
824         /* Copy back the (de)compressed data. m_copyback is
825          * extending the mbuf as necessary.
826          */
827         crypto_copyback(crp, crp->crp_payload_start, result, out);
828         if (result < crp->crp_payload_length) {
829                 switch (crp->crp_buf_type) {
830                 case CRYPTO_BUF_MBUF:
831                         adj = result - crp->crp_payload_length;
832                         m_adj(crp->crp_mbuf, adj);
833                         break;
834                 case CRYPTO_BUF_UIO: {
835                         struct uio *uio = crp->crp_uio;
836                         int ind;
837
838                         adj = crp->crp_payload_length - result;
839                         ind = uio->uio_iovcnt - 1;
840
841                         while (adj > 0 && ind >= 0) {
842                                 if (adj < uio->uio_iov[ind].iov_len) {
843                                         uio->uio_iov[ind].iov_len -= adj;
844                                         break;
845                                 }
846
847                                 adj -= uio->uio_iov[ind].iov_len;
848                                 uio->uio_iov[ind].iov_len = 0;
849                                 ind--;
850                                 uio->uio_iovcnt--;
851                         }
852                         }
853                         break;
854                 }
855         }
856         free(out, M_CRYPTO_DATA);
857         return 0;
858 }
859
860 static int
861 swcr_setup_encdec(struct swcr_session *ses,
862     const struct crypto_session_params *csp)
863 {
864         struct swcr_encdec *swe;
865         struct enc_xform *txf;
866         int error;
867
868         swe = &ses->swcr_encdec;
869         txf = crypto_cipher(csp);
870         MPASS(txf->ivsize == csp->csp_ivlen);
871         if (csp->csp_cipher_key != NULL) {
872                 error = txf->setkey(&swe->sw_kschedule,
873                     csp->csp_cipher_key, csp->csp_cipher_klen);
874                 if (error)
875                         return (error);
876         }
877         swe->sw_exf = txf;
878         return (0);
879 }
880
881 static int
882 swcr_setup_auth(struct swcr_session *ses,
883     const struct crypto_session_params *csp)
884 {
885         struct swcr_auth *swa;
886         struct auth_hash *axf;
887
888         swa = &ses->swcr_auth;
889
890         axf = crypto_auth_hash(csp);
891         swa->sw_axf = axf;
892         if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
893                 return (EINVAL);
894         if (csp->csp_auth_mlen == 0)
895                 swa->sw_mlen = axf->hashsize;
896         else
897                 swa->sw_mlen = csp->csp_auth_mlen;
898         swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
899         if (swa->sw_ictx == NULL)
900                 return (ENOBUFS);
901         
902         switch (csp->csp_auth_alg) {
903         case CRYPTO_MD5_HMAC:
904         case CRYPTO_SHA1_HMAC:
905         case CRYPTO_SHA2_224_HMAC:
906         case CRYPTO_SHA2_256_HMAC:
907         case CRYPTO_SHA2_384_HMAC:
908         case CRYPTO_SHA2_512_HMAC:
909         case CRYPTO_NULL_HMAC:
910         case CRYPTO_RIPEMD160_HMAC:
911                 swa->sw_octx = malloc(axf->ctxsize, M_CRYPTO_DATA,
912                     M_NOWAIT);
913                 if (swa->sw_octx == NULL)
914                         return (ENOBUFS);
915
916                 if (csp->csp_auth_key != NULL) {
917                         swcr_authprepare(axf, swa, csp->csp_auth_key,
918                             csp->csp_auth_klen);
919                 }
920
921                 if (csp->csp_mode == CSP_MODE_DIGEST)
922                         ses->swcr_process = swcr_authcompute;
923                 break;
924         case CRYPTO_SHA1:
925         case CRYPTO_SHA2_224:
926         case CRYPTO_SHA2_256:
927         case CRYPTO_SHA2_384:
928         case CRYPTO_SHA2_512:
929                 axf->Init(swa->sw_ictx);
930                 if (csp->csp_mode == CSP_MODE_DIGEST)
931                         ses->swcr_process = swcr_authcompute;
932                 break;
933         case CRYPTO_AES_NIST_GMAC:
934                 axf->Init(swa->sw_ictx);
935                 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
936                     csp->csp_auth_klen);
937                 if (csp->csp_mode == CSP_MODE_DIGEST)
938                         ses->swcr_process = swcr_gmac;
939                 break;
940         case CRYPTO_POLY1305:
941         case CRYPTO_BLAKE2B:
942         case CRYPTO_BLAKE2S:
943                 /*
944                  * Blake2b and Blake2s support an optional key but do
945                  * not require one.
946                  */
947                 if (csp->csp_auth_klen == 0 || csp->csp_auth_key != NULL)
948                         axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
949                             csp->csp_auth_klen);
950                 axf->Init(swa->sw_ictx);
951                 if (csp->csp_mode == CSP_MODE_DIGEST)
952                         ses->swcr_process = swcr_authcompute;
953                 break;
954         case CRYPTO_AES_CCM_CBC_MAC:
955                 axf->Init(swa->sw_ictx);
956                 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
957                     csp->csp_auth_klen);
958                 if (csp->csp_mode == CSP_MODE_DIGEST)
959                         ses->swcr_process = swcr_ccm_cbc_mac;
960                 break;
961         }
962
963         return (0);
964 }
965
966 static int
967 swcr_setup_gcm(struct swcr_session *ses,
968     const struct crypto_session_params *csp)
969 {
970         struct swcr_encdec *swe;
971         struct swcr_auth *swa;
972         struct enc_xform *txf;
973         struct auth_hash *axf;
974         int error;
975
976         if (csp->csp_ivlen != AES_GCM_IV_LEN)
977                 return (EINVAL);
978
979         /* First, setup the auth side. */
980         swa = &ses->swcr_auth;
981         switch (csp->csp_cipher_klen * 8) {
982         case 128:
983                 axf = &auth_hash_nist_gmac_aes_128;
984                 break;
985         case 192:
986                 axf = &auth_hash_nist_gmac_aes_192;
987                 break;
988         case 256:
989                 axf = &auth_hash_nist_gmac_aes_256;
990                 break;
991         default:
992                 return (EINVAL);
993         }
994         swa->sw_axf = axf;
995         if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
996                 return (EINVAL);
997         if (csp->csp_auth_mlen == 0)
998                 swa->sw_mlen = axf->hashsize;
999         else
1000                 swa->sw_mlen = csp->csp_auth_mlen;
1001         swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1002         if (swa->sw_ictx == NULL)
1003                 return (ENOBUFS);
1004         axf->Init(swa->sw_ictx);
1005         if (csp->csp_cipher_key != NULL)
1006                 axf->Setkey(swa->sw_ictx, csp->csp_cipher_key,
1007                     csp->csp_cipher_klen);
1008
1009         /* Second, setup the cipher side. */
1010         swe = &ses->swcr_encdec;
1011         txf = &enc_xform_aes_nist_gcm;
1012         if (csp->csp_cipher_key != NULL) {
1013                 error = txf->setkey(&swe->sw_kschedule,
1014                     csp->csp_cipher_key, csp->csp_cipher_klen);
1015                 if (error)
1016                         return (error);
1017         }
1018         swe->sw_exf = txf;
1019
1020         return (0);
1021 }
1022
1023 static int
1024 swcr_setup_ccm(struct swcr_session *ses,
1025     const struct crypto_session_params *csp)
1026 {
1027         struct swcr_encdec *swe;
1028         struct swcr_auth *swa;
1029         struct enc_xform *txf;
1030         struct auth_hash *axf;
1031         int error;
1032
1033         if (csp->csp_ivlen != AES_CCM_IV_LEN)
1034                 return (EINVAL);
1035
1036         /* First, setup the auth side. */
1037         swa = &ses->swcr_auth;
1038         switch (csp->csp_cipher_klen * 8) {
1039         case 128:
1040                 axf = &auth_hash_ccm_cbc_mac_128;
1041                 break;
1042         case 192:
1043                 axf = &auth_hash_ccm_cbc_mac_192;
1044                 break;
1045         case 256:
1046                 axf = &auth_hash_ccm_cbc_mac_256;
1047                 break;
1048         default:
1049                 return (EINVAL);
1050         }
1051         swa->sw_axf = axf;
1052         if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1053                 return (EINVAL);
1054         if (csp->csp_auth_mlen == 0)
1055                 swa->sw_mlen = axf->hashsize;
1056         else
1057                 swa->sw_mlen = csp->csp_auth_mlen;
1058         swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1059         if (swa->sw_ictx == NULL)
1060                 return (ENOBUFS);
1061         axf->Init(swa->sw_ictx);
1062         if (csp->csp_cipher_key != NULL)
1063                 axf->Setkey(swa->sw_ictx, csp->csp_cipher_key,
1064                     csp->csp_cipher_klen);
1065
1066         /* Second, setup the cipher side. */
1067         swe = &ses->swcr_encdec;
1068         txf = &enc_xform_ccm;
1069         if (csp->csp_cipher_key != NULL) {
1070                 error = txf->setkey(&swe->sw_kschedule,
1071                     csp->csp_cipher_key, csp->csp_cipher_klen);
1072                 if (error)
1073                         return (error);
1074         }
1075         swe->sw_exf = txf;
1076
1077         return (0);
1078 }
1079
1080 static bool
1081 swcr_auth_supported(const struct crypto_session_params *csp)
1082 {
1083         struct auth_hash *axf;
1084
1085         axf = crypto_auth_hash(csp);
1086         if (axf == NULL)
1087                 return (false);
1088         switch (csp->csp_auth_alg) {
1089         case CRYPTO_MD5_HMAC:
1090         case CRYPTO_SHA1_HMAC:
1091         case CRYPTO_SHA2_224_HMAC:
1092         case CRYPTO_SHA2_256_HMAC:
1093         case CRYPTO_SHA2_384_HMAC:
1094         case CRYPTO_SHA2_512_HMAC:
1095         case CRYPTO_NULL_HMAC:
1096         case CRYPTO_RIPEMD160_HMAC:
1097                 break;
1098         case CRYPTO_AES_NIST_GMAC:
1099                 switch (csp->csp_auth_klen * 8) {
1100                 case 128:
1101                 case 192:
1102                 case 256:
1103                         break;
1104                 default:
1105                         return (false);
1106                 }
1107                 if (csp->csp_auth_key == NULL)
1108                         return (false);
1109                 if (csp->csp_ivlen != AES_GCM_IV_LEN)
1110                         return (false);
1111                 break;
1112         case CRYPTO_POLY1305:
1113                 if (csp->csp_auth_klen != POLY1305_KEY_LEN)
1114                         return (false);
1115                 break;
1116         case CRYPTO_AES_CCM_CBC_MAC:
1117                 switch (csp->csp_auth_klen * 8) {
1118                 case 128:
1119                 case 192:
1120                 case 256:
1121                         break;
1122                 default:
1123                         return (false);
1124                 }
1125                 if (csp->csp_auth_key == NULL)
1126                         return (false);
1127                 if (csp->csp_ivlen != AES_CCM_IV_LEN)
1128                         return (false);
1129                 break;
1130         }
1131         return (true);
1132 }
1133
1134 static bool
1135 swcr_cipher_supported(const struct crypto_session_params *csp)
1136 {
1137         struct enc_xform *txf;
1138
1139         txf = crypto_cipher(csp);
1140         if (txf == NULL)
1141                 return (false);
1142         if (csp->csp_cipher_alg != CRYPTO_NULL_CBC &&
1143             txf->ivsize != csp->csp_ivlen)
1144                 return (false);
1145         return (true);
1146 }
1147
1148 static int
1149 swcr_probesession(device_t dev, const struct crypto_session_params *csp)
1150 {
1151
1152         if (csp->csp_flags != 0)
1153                 return (EINVAL);
1154         switch (csp->csp_mode) {
1155         case CSP_MODE_COMPRESS:
1156                 switch (csp->csp_cipher_alg) {
1157                 case CRYPTO_DEFLATE_COMP:
1158                         break;
1159                 default:
1160                         return (EINVAL);
1161                 }
1162                 break;
1163         case CSP_MODE_CIPHER:
1164                 switch (csp->csp_cipher_alg) {
1165                 case CRYPTO_AES_NIST_GCM_16:
1166                 case CRYPTO_AES_CCM_16:
1167                         return (EINVAL);
1168                 default:
1169                         if (!swcr_cipher_supported(csp))
1170                                 return (EINVAL);
1171                         break;
1172                 }
1173                 break;
1174         case CSP_MODE_DIGEST:
1175                 if (!swcr_auth_supported(csp))
1176                         return (EINVAL);
1177                 break;
1178         case CSP_MODE_AEAD:
1179                 switch (csp->csp_cipher_alg) {
1180                 case CRYPTO_AES_NIST_GCM_16:
1181                 case CRYPTO_AES_CCM_16:
1182                         break;
1183                 default:
1184                         return (EINVAL);
1185                 }
1186                 break;
1187         case CSP_MODE_ETA:
1188                 /* AEAD algorithms cannot be used for EtA. */
1189                 switch (csp->csp_cipher_alg) {
1190                 case CRYPTO_AES_NIST_GCM_16:
1191                 case CRYPTO_AES_CCM_16:
1192                         return (EINVAL);
1193                 }
1194                 switch (csp->csp_auth_alg) {
1195                 case CRYPTO_AES_NIST_GMAC:
1196                 case CRYPTO_AES_CCM_CBC_MAC:
1197                         return (EINVAL);
1198                 }
1199
1200                 if (!swcr_cipher_supported(csp) ||
1201                     !swcr_auth_supported(csp))
1202                         return (EINVAL);
1203                 break;
1204         default:
1205                 return (EINVAL);
1206         }
1207
1208         return (CRYPTODEV_PROBE_SOFTWARE);
1209 }
1210
1211 /*
1212  * Generate a new software session.
1213  */
1214 static int
1215 swcr_newsession(device_t dev, crypto_session_t cses,
1216     const struct crypto_session_params *csp)
1217 {
1218         struct swcr_session *ses;
1219         struct swcr_encdec *swe;
1220         struct swcr_auth *swa;
1221         struct comp_algo *cxf;
1222         int error;
1223
1224         ses = crypto_get_driver_session(cses);
1225         mtx_init(&ses->swcr_lock, "swcr session lock", NULL, MTX_DEF);
1226
1227         error = 0;
1228         swe = &ses->swcr_encdec;
1229         swa = &ses->swcr_auth;
1230         switch (csp->csp_mode) {
1231         case CSP_MODE_COMPRESS:
1232                 switch (csp->csp_cipher_alg) {
1233                 case CRYPTO_DEFLATE_COMP:
1234                         cxf = &comp_algo_deflate;
1235                         break;
1236 #ifdef INVARIANTS
1237                 default:
1238                         panic("bad compression algo");
1239 #endif
1240                 }
1241                 ses->swcr_compdec.sw_cxf = cxf;
1242                 ses->swcr_process = swcr_compdec;
1243                 break;
1244         case CSP_MODE_CIPHER:
1245                 switch (csp->csp_cipher_alg) {
1246                 case CRYPTO_NULL_CBC:
1247                         ses->swcr_process = swcr_null;
1248                         break;
1249 #ifdef INVARIANTS
1250                 case CRYPTO_AES_NIST_GCM_16:
1251                 case CRYPTO_AES_CCM_16:
1252                         panic("bad cipher algo");
1253 #endif
1254                 default:
1255                         error = swcr_setup_encdec(ses, csp);
1256                         if (error == 0)
1257                                 ses->swcr_process = swcr_encdec;
1258                 }
1259                 break;
1260         case CSP_MODE_DIGEST:
1261                 error = swcr_setup_auth(ses, csp);
1262                 break;
1263         case CSP_MODE_AEAD:
1264                 switch (csp->csp_cipher_alg) {
1265                 case CRYPTO_AES_NIST_GCM_16:
1266                         error = swcr_setup_gcm(ses, csp);
1267                         if (error == 0)
1268                                 ses->swcr_process = swcr_gcm;
1269                         break;
1270                 case CRYPTO_AES_CCM_16:
1271                         error = swcr_setup_ccm(ses, csp);
1272                         if (error == 0)
1273                                 ses->swcr_process = swcr_ccm;
1274                         break;
1275 #ifdef INVARIANTS
1276                 default:
1277                         panic("bad aead algo");
1278 #endif
1279                 }
1280                 break;
1281         case CSP_MODE_ETA:
1282 #ifdef INVARIANTS
1283                 switch (csp->csp_cipher_alg) {
1284                 case CRYPTO_AES_NIST_GCM_16:
1285                 case CRYPTO_AES_CCM_16:
1286                         panic("bad eta cipher algo");
1287                 }
1288                 switch (csp->csp_auth_alg) {
1289                 case CRYPTO_AES_NIST_GMAC:
1290                 case CRYPTO_AES_CCM_CBC_MAC:
1291                         panic("bad eta auth algo");
1292                 }
1293 #endif
1294
1295                 error = swcr_setup_auth(ses, csp);
1296                 if (error)
1297                         break;
1298                 if (csp->csp_cipher_alg == CRYPTO_NULL_CBC) {
1299                         /* Effectively degrade to digest mode. */
1300                         ses->swcr_process = swcr_authcompute;
1301                         break;
1302                 }
1303
1304                 error = swcr_setup_encdec(ses, csp);
1305                 if (error == 0)
1306                         ses->swcr_process = swcr_eta;
1307                 break;
1308         default:
1309                 error = EINVAL;
1310         }
1311
1312         if (error)
1313                 swcr_freesession(dev, cses);
1314         return (error);
1315 }
1316
1317 static void
1318 swcr_freesession(device_t dev, crypto_session_t cses)
1319 {
1320         struct swcr_session *ses;
1321         struct swcr_auth *swa;
1322         struct enc_xform *txf;
1323         struct auth_hash *axf;
1324
1325         ses = crypto_get_driver_session(cses);
1326
1327         mtx_destroy(&ses->swcr_lock);
1328
1329         txf = ses->swcr_encdec.sw_exf;
1330         if (txf != NULL) {
1331                 if (ses->swcr_encdec.sw_kschedule != NULL)
1332                         txf->zerokey(&(ses->swcr_encdec.sw_kschedule));
1333         }
1334
1335         axf = ses->swcr_auth.sw_axf;
1336         if (axf != NULL) {
1337                 swa = &ses->swcr_auth;
1338                 if (swa->sw_ictx != NULL) {
1339                         explicit_bzero(swa->sw_ictx, axf->ctxsize);
1340                         free(swa->sw_ictx, M_CRYPTO_DATA);
1341                 }
1342                 if (swa->sw_octx != NULL) {
1343                         explicit_bzero(swa->sw_octx, axf->ctxsize);
1344                         free(swa->sw_octx, M_CRYPTO_DATA);
1345                 }
1346         }
1347 }
1348
1349 /*
1350  * Process a software request.
1351  */
1352 static int
1353 swcr_process(device_t dev, struct cryptop *crp, int hint)
1354 {
1355         struct swcr_session *ses;
1356
1357         ses = crypto_get_driver_session(crp->crp_session);
1358         mtx_lock(&ses->swcr_lock);
1359
1360         crp->crp_etype = ses->swcr_process(ses, crp);
1361
1362         mtx_unlock(&ses->swcr_lock);
1363         crypto_done(crp);
1364         return (0);
1365 }
1366
1367 static void
1368 swcr_identify(driver_t *drv, device_t parent)
1369 {
1370         /* NB: order 10 is so we get attached after h/w devices */
1371         if (device_find_child(parent, "cryptosoft", -1) == NULL &&
1372             BUS_ADD_CHILD(parent, 10, "cryptosoft", 0) == 0)
1373                 panic("cryptosoft: could not attach");
1374 }
1375
1376 static int
1377 swcr_probe(device_t dev)
1378 {
1379         device_set_desc(dev, "software crypto");
1380         return (BUS_PROBE_NOWILDCARD);
1381 }
1382
1383 static int
1384 swcr_attach(device_t dev)
1385 {
1386
1387         swcr_id = crypto_get_driverid(dev, sizeof(struct swcr_session),
1388                         CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC);
1389         if (swcr_id < 0) {
1390                 device_printf(dev, "cannot initialize!");
1391                 return (ENXIO);
1392         }
1393
1394         return (0);
1395 }
1396
1397 static int
1398 swcr_detach(device_t dev)
1399 {
1400         crypto_unregister_all(swcr_id);
1401         return 0;
1402 }
1403
1404 static device_method_t swcr_methods[] = {
1405         DEVMETHOD(device_identify,      swcr_identify),
1406         DEVMETHOD(device_probe,         swcr_probe),
1407         DEVMETHOD(device_attach,        swcr_attach),
1408         DEVMETHOD(device_detach,        swcr_detach),
1409
1410         DEVMETHOD(cryptodev_probesession, swcr_probesession),
1411         DEVMETHOD(cryptodev_newsession, swcr_newsession),
1412         DEVMETHOD(cryptodev_freesession,swcr_freesession),
1413         DEVMETHOD(cryptodev_process,    swcr_process),
1414
1415         {0, 0},
1416 };
1417
1418 static driver_t swcr_driver = {
1419         "cryptosoft",
1420         swcr_methods,
1421         0,              /* NB: no softc */
1422 };
1423 static devclass_t swcr_devclass;
1424
1425 /*
1426  * NB: We explicitly reference the crypto module so we
1427  * get the necessary ordering when built as a loadable
1428  * module.  This is required because we bundle the crypto
1429  * module code together with the cryptosoft driver (otherwise
1430  * normal module dependencies would handle things).
1431  */
1432 extern int crypto_modevent(struct module *, int, void *);
1433 /* XXX where to attach */
1434 DRIVER_MODULE(cryptosoft, nexus, swcr_driver, swcr_devclass, crypto_modevent,0);
1435 MODULE_VERSION(cryptosoft, 1);
1436 MODULE_DEPEND(cryptosoft, crypto, 1, 1, 1);