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

Cache object: 81fcb80cad78c606a75d16598dd2c077


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