[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/opencrypto/cryptodev.c

Version: -  FREEBSD  -  FREEBSD8  -  FREEBSD7  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

    1 /*      $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $  */
    2 
    3 /*-
    4  * Copyright (c) 2001 Theo de Raadt
    5  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  *
   11  * 1. Redistributions of source code must retain the above copyright
   12  *   notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *   notice, this list of conditions and the following disclaimer in the
   15  *   documentation and/or other materials provided with the distribution.
   16  * 3. The name of the author may not be used to endorse or promote products
   17  *   derived from this software without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   29  *
   30  * Effort sponsored in part by the Defense Advanced Research Projects
   31  * Agency (DARPA) and Air Force Research Laboratory, Air Force
   32  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD$");
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/malloc.h>
   41 #include <sys/mbuf.h>
   42 #include <sys/lock.h>
   43 #include <sys/mutex.h>
   44 #include <sys/sysctl.h>
   45 #include <sys/file.h>
   46 #include <sys/filedesc.h>
   47 #include <sys/errno.h>
   48 #include <sys/uio.h>
   49 #include <sys/random.h>
   50 #include <sys/conf.h>
   51 #include <sys/kernel.h>
   52 #include <sys/module.h>
   53 #include <sys/fcntl.h>
   54 #include <sys/bus.h>
   55 
   56 #include <opencrypto/cryptodev.h>
   57 #include <opencrypto/xform.h>
   58 
   59 struct csession {
   60         TAILQ_ENTRY(csession) next;
   61         u_int64_t       sid;
   62         u_int32_t       ses;
   63         struct mtx      lock;           /* for op submission */
   64 
   65         u_int32_t       cipher;
   66         struct enc_xform *txform;
   67         u_int32_t       mac;
   68         struct auth_hash *thash;
   69 
   70         caddr_t         key;
   71         int             keylen;
   72         u_char          tmp_iv[EALG_MAX_BLOCK_LEN];
   73 
   74         caddr_t         mackey;
   75         int             mackeylen;
   76 
   77         struct iovec    iovec;
   78         struct uio      uio;
   79         int             error;
   80 };
   81 
   82 struct fcrypt {
   83         TAILQ_HEAD(csessionlist, csession) csessions;
   84         int             sesn;
   85 };
   86 
   87 static  int cryptof_rw(struct file *fp, struct uio *uio,
   88                     struct ucred *cred, int flags, struct thread *);
   89 static  int cryptof_truncate(struct file *, off_t, struct ucred *,
   90                     struct thread *);
   91 static  int cryptof_ioctl(struct file *, u_long, void *,
   92                     struct ucred *, struct thread *);
   93 static  int cryptof_poll(struct file *, int, struct ucred *, struct thread *);
   94 static  int cryptof_kqfilter(struct file *, struct knote *);
   95 static  int cryptof_stat(struct file *, struct stat *,
   96                     struct ucred *, struct thread *);
   97 static  int cryptof_close(struct file *, struct thread *);
   98 
   99 static struct fileops cryptofops = {
  100     .fo_read = cryptof_rw,
  101     .fo_write = cryptof_rw,
  102     .fo_truncate = cryptof_truncate,
  103     .fo_ioctl = cryptof_ioctl,
  104     .fo_poll = cryptof_poll,
  105     .fo_kqfilter = cryptof_kqfilter,
  106     .fo_stat = cryptof_stat,
  107     .fo_close = cryptof_close
  108 };
  109 
  110 static struct csession *csefind(struct fcrypt *, u_int);
  111 static int csedelete(struct fcrypt *, struct csession *);
  112 static struct csession *cseadd(struct fcrypt *, struct csession *);
  113 static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
  114     u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
  115     struct auth_hash *);
  116 static int csefree(struct csession *);
  117 
  118 static  int cryptodev_op(struct csession *, struct crypt_op *,
  119                         struct ucred *, struct thread *td);
  120 static  int cryptodev_key(struct crypt_kop *);
  121 static  int cryptodev_find(struct crypt_find_op *);
  122 
  123 static int
  124 cryptof_rw(
  125         struct file *fp,
  126         struct uio *uio,
  127         struct ucred *active_cred,
  128         int flags,
  129         struct thread *td)
  130 {
  131 
  132         return (EIO);
  133 }
  134 
  135 static int
  136 cryptof_truncate(
  137         struct file *fp,
  138         off_t length,
  139         struct ucred *active_cred,
  140         struct thread *td)
  141 {
  142 
  143         return (EINVAL);
  144 }
  145 
  146 /*
  147  * Check a crypto identifier to see if it requested
  148  * a software device/driver.  This can be done either
  149  * by device name/class or through search constraints.
  150  */
  151 static int
  152 checkforsoftware(int crid)
  153 {
  154         if (crid & CRYPTOCAP_F_SOFTWARE)
  155                 return EINVAL;          /* XXX */
  156         if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
  157             (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
  158                 return EINVAL;          /* XXX */
  159         return 0;
  160 }
  161 
  162 /* ARGSUSED */
  163 static int
  164 cryptof_ioctl(
  165         struct file *fp,
  166         u_long cmd,
  167         void *data,
  168         struct ucred *active_cred,
  169         struct thread *td)
  170 {
  171 #define SES2(p) ((struct session2_op *)p)
  172         struct cryptoini cria, crie;
  173         struct fcrypt *fcr = fp->f_data;
  174         struct csession *cse;
  175         struct session_op *sop;
  176         struct crypt_op *cop;
  177         struct enc_xform *txform = NULL;
  178         struct auth_hash *thash = NULL;
  179         struct crypt_kop *kop;
  180         u_int64_t sid;
  181         u_int32_t ses;
  182         int error = 0, crid;
  183 
  184         switch (cmd) {
  185         case CIOCGSESSION:
  186         case CIOCGSESSION2:
  187                 sop = (struct session_op *)data;
  188                 switch (sop->cipher) {
  189                 case 0:
  190                         break;
  191                 case CRYPTO_DES_CBC:
  192                         txform = &enc_xform_des;
  193                         break;
  194                 case CRYPTO_3DES_CBC:
  195                         txform = &enc_xform_3des;
  196                         break;
  197                 case CRYPTO_BLF_CBC:
  198                         txform = &enc_xform_blf;
  199                         break;
  200                 case CRYPTO_CAST_CBC:
  201                         txform = &enc_xform_cast5;
  202                         break;
  203                 case CRYPTO_SKIPJACK_CBC:
  204                         txform = &enc_xform_skipjack;
  205                         break;
  206                 case CRYPTO_AES_CBC:
  207                         txform = &enc_xform_rijndael128;
  208                         break;
  209                 case CRYPTO_NULL_CBC:
  210                         txform = &enc_xform_null;
  211                         break;
  212                 case CRYPTO_ARC4:
  213                         txform = &enc_xform_arc4;
  214                         break;
  215                 case CRYPTO_CAMELLIA_CBC:
  216                         txform = &enc_xform_camellia;
  217                         break;
  218                 default:
  219                         return (EINVAL);
  220                 }
  221 
  222                 switch (sop->mac) {
  223                 case 0:
  224                         break;
  225                 case CRYPTO_MD5_HMAC:
  226                         thash = &auth_hash_hmac_md5;
  227                         break;
  228                 case CRYPTO_SHA1_HMAC:
  229                         thash = &auth_hash_hmac_sha1;
  230                         break;
  231                 case CRYPTO_SHA2_256_HMAC:
  232                         thash = &auth_hash_hmac_sha2_256;
  233                         break;
  234                 case CRYPTO_SHA2_384_HMAC:
  235                         thash = &auth_hash_hmac_sha2_384;
  236                         break;
  237                 case CRYPTO_SHA2_512_HMAC:
  238                         thash = &auth_hash_hmac_sha2_512;
  239                         break;
  240                 case CRYPTO_RIPEMD160_HMAC:
  241                         thash = &auth_hash_hmac_ripemd_160;
  242                         break;
  243 #ifdef notdef
  244                 case CRYPTO_MD5:
  245                         thash = &auth_hash_md5;
  246                         break;
  247                 case CRYPTO_SHA1:
  248                         thash = &auth_hash_sha1;
  249                         break;
  250 #endif
  251                 case CRYPTO_NULL_HMAC:
  252                         thash = &auth_hash_null;
  253                         break;
  254                 default:
  255                         return (EINVAL);
  256                 }
  257 
  258                 bzero(&crie, sizeof(crie));
  259                 bzero(&cria, sizeof(cria));
  260 
  261                 if (txform) {
  262                         crie.cri_alg = txform->type;
  263                         crie.cri_klen = sop->keylen * 8;
  264                         if (sop->keylen > txform->maxkey ||
  265                             sop->keylen < txform->minkey) {
  266                                 error = EINVAL;
  267                                 goto bail;
  268                         }
  269 
  270                         crie.cri_key = malloc(crie.cri_klen / 8,
  271                             M_XDATA, M_WAITOK);
  272                         if ((error = copyin(sop->key, crie.cri_key,
  273                             crie.cri_klen / 8)))
  274                                 goto bail;
  275                         if (thash)
  276                                 crie.cri_next = &cria;
  277                 }
  278 
  279                 if (thash) {
  280                         cria.cri_alg = thash->type;
  281                         cria.cri_klen = sop->mackeylen * 8;
  282                         if (sop->mackeylen != thash->keysize) {
  283                                 error = EINVAL;
  284                                 goto bail;
  285                         }
  286 
  287                         if (cria.cri_klen) {
  288                                 cria.cri_key = malloc(cria.cri_klen / 8,
  289                                     M_XDATA, M_WAITOK);
  290                                 if ((error = copyin(sop->mackey, cria.cri_key,
  291                                     cria.cri_klen / 8)))
  292                                         goto bail;
  293                         }
  294                 }
  295 
  296                 /* NB: CIOGSESSION2 has the crid */
  297                 if (cmd == CIOCGSESSION2) {
  298                         crid = SES2(sop)->crid;
  299                         error = checkforsoftware(crid);
  300                         if (error)
  301                                 goto bail;
  302                 } else
  303                         crid = CRYPTOCAP_F_HARDWARE;
  304                 error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
  305                 if (error)
  306                         goto bail;
  307 
  308                 cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
  309                     cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
  310                     thash);
  311 
  312                 if (cse == NULL) {
  313                         crypto_freesession(sid);
  314                         error = EINVAL;
  315                         goto bail;
  316                 }
  317                 sop->ses = cse->ses;
  318                 if (cmd == CIOCGSESSION2) {
  319                         /* return hardware/driver id */
  320                         SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
  321                 }
  322 bail:
  323                 if (error) {
  324                         if (crie.cri_key)
  325                                 free(crie.cri_key, M_XDATA);
  326                         if (cria.cri_key)
  327                                 free(cria.cri_key, M_XDATA);
  328                 }
  329                 break;
  330         case CIOCFSESSION:
  331                 ses = *(u_int32_t *)data;
  332                 cse = csefind(fcr, ses);
  333                 if (cse == NULL)
  334                         return (EINVAL);
  335                 csedelete(fcr, cse);
  336                 error = csefree(cse);
  337                 break;
  338         case CIOCCRYPT:
  339                 cop = (struct crypt_op *)data;
  340                 cse = csefind(fcr, cop->ses);
  341                 if (cse == NULL)
  342                         return (EINVAL);
  343                 error = cryptodev_op(cse, cop, active_cred, td);
  344                 break;
  345         case CIOCKEY:
  346         case CIOCKEY2:
  347                 if (!crypto_userasymcrypto)
  348                         return (EPERM);         /* XXX compat? */
  349                 mtx_lock(&Giant);
  350                 kop = (struct crypt_kop *)data;
  351                 if (cmd == CIOCKEY) {
  352                         /* NB: crypto core enforces s/w driver use */
  353                         kop->crk_crid =
  354                             CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
  355                 }
  356                 error = cryptodev_key(kop);
  357                 mtx_unlock(&Giant);
  358                 break;
  359         case CIOCASYMFEAT:
  360                 if (!crypto_userasymcrypto) {
  361                         /*
  362                          * NB: if user asym crypto operations are
  363                          * not permitted return "no algorithms"
  364                          * so well-behaved applications will just
  365                          * fallback to doing them in software.
  366                          */
  367                         *(int *)data = 0;
  368                 } else
  369                         error = crypto_getfeat((int *)data);
  370                 break;
  371         case CIOCFINDDEV:
  372                 error = cryptodev_find((struct crypt_find_op *)data);
  373                 break;
  374         default:
  375                 error = EINVAL;
  376                 break;
  377         }
  378         return (error);
  379 #undef SES2
  380 }
  381 
  382 static int cryptodev_cb(void *);
  383 
  384 
  385 static int
  386 cryptodev_op(
  387         struct csession *cse,
  388         struct crypt_op *cop,
  389         struct ucred *active_cred,
  390         struct thread *td)
  391 {
  392         struct cryptop *crp = NULL;
  393         struct cryptodesc *crde = NULL, *crda = NULL;
  394         int error;
  395 
  396         if (cop->len > 256*1024-4)
  397                 return (E2BIG);
  398 
  399         if (cse->txform) {
  400                 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
  401                         return (EINVAL);
  402         }
  403 
  404         cse->uio.uio_iov = &cse->iovec;
  405         cse->uio.uio_iovcnt = 1;
  406         cse->uio.uio_offset = 0;
  407         cse->uio.uio_resid = cop->len;
  408         cse->uio.uio_segflg = UIO_SYSSPACE;
  409         cse->uio.uio_rw = UIO_WRITE;
  410         cse->uio.uio_td = td;
  411         cse->uio.uio_iov[0].iov_len = cop->len;
  412         if (cse->thash) {
  413                 cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
  414                 cse->uio.uio_resid += cse->thash->hashsize;
  415         }
  416         cse->uio.uio_iov[0].iov_base = malloc(cse->uio.uio_iov[0].iov_len,
  417             M_XDATA, M_WAITOK);
  418 
  419         crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
  420         if (crp == NULL) {
  421                 error = ENOMEM;
  422                 goto bail;
  423         }
  424 
  425         if (cse->thash) {
  426                 crda = crp->crp_desc;
  427                 if (cse->txform)
  428                         crde = crda->crd_next;
  429         } else {
  430                 if (cse->txform)
  431                         crde = crp->crp_desc;
  432                 else {
  433                         error = EINVAL;
  434                         goto bail;
  435                 }
  436         }
  437 
  438         if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
  439                 goto bail;
  440 
  441         if (crda) {
  442                 crda->crd_skip = 0;
  443                 crda->crd_len = cop->len;
  444                 crda->crd_inject = cop->len;
  445 
  446                 crda->crd_alg = cse->mac;
  447                 crda->crd_key = cse->mackey;
  448                 crda->crd_klen = cse->mackeylen * 8;
  449         }
  450 
  451         if (crde) {
  452                 if (cop->op == COP_ENCRYPT)
  453                         crde->crd_flags |= CRD_F_ENCRYPT;
  454                 else
  455                         crde->crd_flags &= ~CRD_F_ENCRYPT;
  456                 crde->crd_len = cop->len;
  457                 crde->crd_inject = 0;
  458 
  459                 crde->crd_alg = cse->cipher;
  460                 crde->crd_key = cse->key;
  461                 crde->crd_klen = cse->keylen * 8;
  462         }
  463 
  464         crp->crp_ilen = cop->len;
  465         crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
  466                        | (cop->flags & COP_F_BATCH);
  467         crp->crp_buf = (caddr_t)&cse->uio;
  468         crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
  469         crp->crp_sid = cse->sid;
  470         crp->crp_opaque = (void *)cse;
  471 
  472         if (cop->iv) {
  473                 if (crde == NULL) {
  474                         error = EINVAL;
  475                         goto bail;
  476                 }
  477                 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
  478                         error = EINVAL;
  479                         goto bail;
  480                 }
  481                 if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
  482                         goto bail;
  483                 bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
  484                 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
  485                 crde->crd_skip = 0;
  486         } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
  487                 crde->crd_skip = 0;
  488         } else if (crde) {
  489                 crde->crd_flags |= CRD_F_IV_PRESENT;
  490                 crde->crd_skip = cse->txform->blocksize;
  491                 crde->crd_len -= cse->txform->blocksize;
  492         }
  493 
  494         if (cop->mac && crda == NULL) {
  495                 error = EINVAL;
  496                 goto bail;
  497         }
  498 
  499 again:
  500         /*
  501          * Let the dispatch run unlocked, then, interlock against the
  502          * callback before checking if the operation completed and going
  503          * to sleep.  This insures drivers don't inherit our lock which
  504          * results in a lock order reversal between crypto_dispatch forced
  505          * entry and the crypto_done callback into us.
  506          */
  507         error = crypto_dispatch(crp);
  508         mtx_lock(&cse->lock);
  509         if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
  510                 error = msleep(crp, &cse->lock, PWAIT, "crydev", 0);
  511         mtx_unlock(&cse->lock);
  512 
  513         if (error != 0)
  514                 goto bail;
  515 
  516         if (crp->crp_etype == EAGAIN) {
  517                 crp->crp_etype = 0;
  518                 crp->crp_flags &= ~CRYPTO_F_DONE;
  519                 goto again;
  520         }
  521 
  522         if (crp->crp_etype != 0) {
  523                 error = crp->crp_etype;
  524                 goto bail;
  525         }
  526 
  527         if (cse->error) {
  528                 error = cse->error;
  529                 goto bail;
  530         }
  531 
  532         if (cop->dst &&
  533             (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
  534                 goto bail;
  535 
  536         if (cop->mac &&
  537             (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
  538             cop->mac, cse->thash->hashsize)))
  539                 goto bail;
  540 
  541 bail:
  542         if (crp)
  543                 crypto_freereq(crp);
  544         if (cse->uio.uio_iov[0].iov_base)
  545                 free(cse->uio.uio_iov[0].iov_base, M_XDATA);
  546 
  547         return (error);
  548 }
  549 
  550 static int
  551 cryptodev_cb(void *op)
  552 {
  553         struct cryptop *crp = (struct cryptop *) op;
  554         struct csession *cse = (struct csession *)crp->crp_opaque;
  555 
  556         mtx_lock(&cse->lock);
  557         cse->error = crp->crp_etype;
  558         wakeup_one(crp);
  559         mtx_unlock(&cse->lock);
  560         return (0);
  561 }
  562 
  563 static int
  564 cryptodevkey_cb(void *op)
  565 {
  566         struct cryptkop *krp = (struct cryptkop *) op;
  567 
  568         wakeup_one(krp);
  569         return (0);
  570 }
  571 
  572 static int
  573 cryptodev_key(struct crypt_kop *kop)
  574 {
  575         struct cryptkop *krp = NULL;
  576         int error = EINVAL;
  577         int in, out, size, i;
  578 
  579         if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
  580                 return (EFBIG);
  581         }
  582 
  583         in = kop->crk_iparams;
  584         out = kop->crk_oparams;
  585         switch (kop->crk_op) {
  586         case CRK_MOD_EXP:
  587                 if (in == 3 && out == 1)
  588                         break;
  589                 return (EINVAL);
  590         case CRK_MOD_EXP_CRT:
  591                 if (in == 6 && out == 1)
  592                         break;
  593                 return (EINVAL);
  594         case CRK_DSA_SIGN:
  595                 if (in == 5 && out == 2)
  596                         break;
  597                 return (EINVAL);
  598         case CRK_DSA_VERIFY:
  599                 if (in == 7 && out == 0)
  600                         break;
  601                 return (EINVAL);
  602         case CRK_DH_COMPUTE_KEY:
  603                 if (in == 3 && out == 1)
  604                         break;
  605                 return (EINVAL);
  606         default:
  607                 return (EINVAL);
  608         }
  609 
  610         krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
  611         if (!krp)
  612                 return (ENOMEM);
  613         krp->krp_op = kop->crk_op;
  614         krp->krp_status = kop->crk_status;
  615         krp->krp_iparams = kop->crk_iparams;
  616         krp->krp_oparams = kop->crk_oparams;
  617         krp->krp_crid = kop->crk_crid;
  618         krp->krp_status = 0;
  619         krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
  620 
  621         for (i = 0; i < CRK_MAXPARAM; i++) {
  622                 if (kop->crk_param[i].crp_nbits > 65536)
  623                         /* Limit is the same as in OpenBSD */
  624                         goto fail;
  625                 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
  626         }
  627         for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
  628                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
  629                 if (size == 0)
  630                         continue;
  631                 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
  632                 if (i >= krp->krp_iparams)
  633                         continue;
  634                 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
  635                 if (error)
  636                         goto fail;
  637         }
  638 
  639         error = crypto_kdispatch(krp);
  640         if (error)
  641                 goto fail;
  642         error = tsleep(krp, PSOCK, "crydev", 0);
  643         if (error) {
  644                 /* XXX can this happen?  if so, how do we recover? */
  645                 goto fail;
  646         }
  647         
  648         kop->crk_crid = krp->krp_crid;          /* device that did the work */
  649         if (krp->krp_status != 0) {
  650                 error = krp->krp_status;
  651                 goto fail;
  652         }
  653 
  654         for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
  655                 size = (krp->krp_param[i].crp_nbits + 7) / 8;
  656                 if (size == 0)
  657                         continue;
  658                 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
  659                 if (error)
  660                         goto fail;
  661         }
  662 
  663 fail:
  664         if (krp) {
  665                 kop->crk_status = krp->krp_status;
  666                 for (i = 0; i < CRK_MAXPARAM; i++) {
  667                         if (krp->krp_param[i].crp_p)
  668                                 free(krp->krp_param[i].crp_p, M_XDATA);
  669                 }
  670                 free(krp, M_XDATA);
  671         }
  672         return (error);
  673 }
  674 
  675 static int
  676 cryptodev_find(struct crypt_find_op *find)
  677 {
  678         device_t dev;
  679 
  680         if (find->crid != -1) {
  681                 dev = crypto_find_device_byhid(find->crid);
  682                 if (dev == NULL)
  683                         return (ENOENT);
  684                 strlcpy(find->name, device_get_nameunit(dev),
  685                     sizeof(find->name));
  686         } else {
  687                 find->crid = crypto_find_driver(find->name);
  688                 if (find->crid == -1)
  689                         return (ENOENT);
  690         }
  691         return (0);
  692 }
  693 
  694 /* ARGSUSED */
  695 static int
  696 cryptof_poll(
  697         struct file *fp,
  698         int events,
  699         struct ucred *active_cred,
  700         struct thread *td)
  701 {
  702 
  703         return (0);
  704 }
  705 
  706 /* ARGSUSED */
  707 static int
  708 cryptof_kqfilter(struct file *fp, struct knote *kn)
  709 {
  710 
  711         return (0);
  712 }
  713 
  714 /* ARGSUSED */
  715 static int
  716 cryptof_stat(
  717         struct file *fp,
  718         struct stat *sb,
  719         struct ucred *active_cred,
  720         struct thread *td)
  721 {
  722 
  723         return (EOPNOTSUPP);
  724 }
  725 
  726 /* ARGSUSED */
  727 static int
  728 cryptof_close(struct file *fp, struct thread *td)
  729 {
  730         struct fcrypt *fcr = fp->f_data;
  731         struct csession *cse;
  732 
  733         while ((cse = TAILQ_FIRST(&fcr->csessions))) {
  734                 TAILQ_REMOVE(&fcr->csessions, cse, next);
  735                 (void)csefree(cse);
  736         }
  737         free(fcr, M_XDATA);
  738         fp->f_data = NULL;
  739         return 0;
  740 }
  741 
  742 static struct csession *
  743 csefind(struct fcrypt *fcr, u_int ses)
  744 {
  745         struct csession *cse;
  746 
  747         TAILQ_FOREACH(cse, &fcr->csessions, next)
  748                 if (cse->ses == ses)
  749                         return (cse);
  750         return (NULL);
  751 }
  752 
  753 static int
  754 csedelete(struct fcrypt *fcr, struct csession *cse_del)
  755 {
  756         struct csession *cse;
  757 
  758         TAILQ_FOREACH(cse, &fcr->csessions, next) {
  759                 if (cse == cse_del) {
  760                         TAILQ_REMOVE(&fcr->csessions, cse, next);
  761                         return (1);
  762                 }
  763         }
  764         return (0);
  765 }
  766         
  767 static struct csession *
  768 cseadd(struct fcrypt *fcr, struct csession *cse)
  769 {
  770         TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
  771         cse->ses = fcr->sesn++;
  772         return (cse);
  773 }
  774 
  775 struct csession *
  776 csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
  777     caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
  778     struct enc_xform *txform, struct auth_hash *thash)
  779 {
  780         struct csession *cse;
  781 
  782 #ifdef INVARIANTS
  783         /* NB: required when mtx_init is built with INVARIANTS */
  784         cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
  785 #else
  786         cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT);
  787 #endif
  788         if (cse == NULL)
  789                 return NULL;
  790         mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
  791         cse->key = key;
  792         cse->keylen = keylen/8;
  793         cse->mackey = mackey;
  794         cse->mackeylen = mackeylen/8;
  795         cse->sid = sid;
  796         cse->cipher = cipher;
  797         cse->mac = mac;
  798         cse->txform = txform;
  799         cse->thash = thash;
  800         cseadd(fcr, cse);
  801         return (cse);
  802 }
  803 
  804 static int
  805 csefree(struct csession *cse)
  806 {
  807         int error;
  808 
  809         error = crypto_freesession(cse->sid);
  810         mtx_destroy(&cse->lock);
  811         if (cse->key)
  812                 free(cse->key, M_XDATA);
  813         if (cse->mackey)
  814                 free(cse->mackey, M_XDATA);
  815         free(cse, M_XDATA);
  816         return (error);
  817 }
  818 
  819 static int
  820 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
  821 {
  822         return (0);
  823 }
  824 
  825 static int
  826 cryptoread(struct cdev *dev, struct uio *uio, int ioflag)
  827 {
  828         return (EIO);
  829 }
  830 
  831 static int
  832 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag)
  833 {
  834         return (EIO);
  835 }
  836 
  837 static int
  838 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  839 {
  840         struct file *f;
  841         struct fcrypt *fcr;
  842         int fd, error;
  843 
  844         switch (cmd) {
  845         case CRIOGET:
  846                 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
  847                 TAILQ_INIT(&fcr->csessions);
  848                 fcr->sesn = 0;
  849 
  850                 error = falloc(td, &f, &fd);
  851 
  852                 if (error) {
  853                         free(fcr, M_XDATA);
  854                         return (error);
  855                 }
  856                 /* falloc automatically provides an extra reference to 'f'. */
  857                 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
  858                 *(u_int32_t *)data = fd;
  859                 fdrop(f, td);
  860                 break;
  861         case CRIOFINDDEV:
  862                 error = cryptodev_find((struct crypt_find_op *)data);
  863                 break;
  864         case CRIOASYMFEAT:
  865                 error = crypto_getfeat((int *)data);
  866                 break;
  867         default:
  868                 error = EINVAL;
  869                 break;
  870         }
  871         return (error);
  872 }
  873 
  874 static struct cdevsw crypto_cdevsw = {
  875         .d_version =    D_VERSION,
  876         .d_flags =      D_NEEDGIANT,
  877         .d_open =       cryptoopen,
  878         .d_read =       cryptoread,
  879         .d_write =      cryptowrite,
  880         .d_ioctl =      cryptoioctl,
  881         .d_name =       "crypto",
  882 };
  883 static struct cdev *crypto_dev;
  884 
  885 /*
  886  * Initialization code, both for static and dynamic loading.
  887  */
  888 static int
  889 cryptodev_modevent(module_t mod, int type, void *unused)
  890 {
  891         switch (type) {
  892         case MOD_LOAD:
  893                 if (bootverbose)
  894                         printf("crypto: <crypto device>\n");
  895                 crypto_dev = make_dev(&crypto_cdevsw, 0, 
  896                                       UID_ROOT, GID_WHEEL, 0666,
  897                                       "crypto");
  898                 return 0;
  899         case MOD_UNLOAD:
  900                 /*XXX disallow if active sessions */
  901                 destroy_dev(crypto_dev);
  902                 return 0;
  903         }
  904         return EINVAL;
  905 }
  906 
  907 static moduledata_t cryptodev_mod = {
  908         "cryptodev",
  909         cryptodev_modevent,
  910         0
  911 };
  912 MODULE_VERSION(cryptodev, 1);
  913 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  914 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
  915 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);

Cache object: fd99e99e6fb22f90319ee01b00e13f03


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.