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

Cache object: 47c10d9ab17ce8f4a24312de0a140909


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