The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ 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  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: dea95564c5675b26cb7d154a2a8cd335


[ 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.