Home | History | Annotate | Line # | Download | only in opencrypto
ocryptodev.c revision 1.10
      1  1.10  riastrad /*	$NetBSD: ocryptodev.c,v 1.10 2017/07/28 14:16:29 riastradh Exp $ */
      2   1.1    darran /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
      3   1.1    darran /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
      4   1.1    darran 
      5   1.1    darran /*-
      6   1.1    darran  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      7   1.1    darran  * All rights reserved.
      8   1.1    darran  *
      9   1.1    darran  * This code is derived from software contributed to The NetBSD Foundation
     10   1.1    darran  * by Coyote Point Systems, Inc.
     11   1.1    darran  *
     12   1.1    darran  * Redistribution and use in source and binary forms, with or without
     13   1.1    darran  * modification, are permitted provided that the following conditions
     14   1.1    darran  * are met:
     15   1.1    darran  * 1. Redistributions of source code must retain the above copyright
     16   1.1    darran  *    notice, this list of conditions and the following disclaimer.
     17   1.1    darran  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1    darran  *    notice, this list of conditions and the following disclaimer in the
     19   1.1    darran  *    documentation and/or other materials provided with the distribution.
     20   1.1    darran  *
     21   1.1    darran  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22   1.1    darran  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23   1.1    darran  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24   1.1    darran  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25   1.1    darran  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   1.1    darran  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.1    darran  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.1    darran  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.1    darran  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.1    darran  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.1    darran  * POSSIBILITY OF SUCH DAMAGE.
     32   1.1    darran  */
     33   1.1    darran 
     34   1.1    darran /*
     35   1.1    darran  * Copyright (c) 2001 Theo de Raadt
     36   1.1    darran  *
     37   1.1    darran  * Redistribution and use in source and binary forms, with or without
     38   1.1    darran  * modification, are permitted provided that the following conditions
     39   1.1    darran  * are met:
     40   1.1    darran  *
     41   1.1    darran  * 1. Redistributions of source code must retain the above copyright
     42   1.1    darran  *   notice, this list of conditions and the following disclaimer.
     43   1.1    darran  * 2. Redistributions in binary form must reproduce the above copyright
     44   1.1    darran  *   notice, this list of conditions and the following disclaimer in the
     45   1.1    darran  *   documentation and/or other materials provided with the distribution.
     46   1.1    darran  * 3. The name of the author may not be used to endorse or promote products
     47   1.1    darran  *   derived from this software without specific prior written permission.
     48   1.1    darran  *
     49   1.1    darran  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     50   1.1    darran  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     51   1.1    darran  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     52   1.1    darran  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     53   1.1    darran  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54   1.1    darran  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55   1.1    darran  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56   1.1    darran  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57   1.1    darran  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58   1.1    darran  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59   1.1    darran  *
     60   1.1    darran  * Effort sponsored in part by the Defense Advanced Research Projects
     61   1.1    darran  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     62   1.1    darran  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     63   1.1    darran  *
     64   1.1    darran  */
     65   1.1    darran 
     66   1.1    darran /*
     67   1.1    darran  * Implement backward compatibility IOCTLs in this module.
     68   1.1    darran  *
     69   1.1    darran  */
     70   1.1    darran 
     71   1.1    darran #include <sys/cdefs.h>
     72  1.10  riastrad __KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.10 2017/07/28 14:16:29 riastradh Exp $");
     73   1.1    darran 
     74   1.1    darran #include <sys/param.h>
     75   1.1    darran #include <sys/systm.h>
     76   1.1    darran #include <sys/kmem.h>
     77   1.1    darran #include <sys/malloc.h>
     78   1.1    darran #include <sys/mbuf.h>
     79   1.1    darran #include <sys/pool.h>
     80   1.1    darran #include <sys/sysctl.h>
     81   1.1    darran #include <sys/file.h>
     82   1.1    darran #include <sys/filedesc.h>
     83   1.1    darran #include <sys/errno.h>
     84   1.1    darran #include <sys/md5.h>
     85   1.1    darran #include <sys/sha1.h>
     86   1.1    darran #include <sys/conf.h>
     87   1.1    darran #include <sys/device.h>
     88   1.1    darran #include <sys/kauth.h>
     89   1.1    darran #include <sys/select.h>
     90   1.1    darran #include <sys/poll.h>
     91   1.1    darran #include <sys/atomic.h>
     92   1.1    darran 
     93   1.5  pgoyette #ifdef _KERNEL_OPT
     94   1.1    darran #include "opt_ocf.h"
     95   1.5  pgoyette #endif
     96   1.5  pgoyette 
     97   1.1    darran #include <opencrypto/cryptodev.h>
     98   1.3  drochner #include <opencrypto/cryptodev_internal.h>
     99   1.1    darran #include <opencrypto/ocryptodev.h>
    100   1.1    darran #include <opencrypto/xform.h>
    101   1.1    darran 
    102   1.1    darran static int	ocryptodev_op(struct csession *, struct ocrypt_op *,
    103   1.1    darran 		    struct lwp *);
    104   1.1    darran static int	ocryptodev_mop(struct fcrypt *, struct ocrypt_n_op *, int,
    105   1.1    darran 		    struct lwp *);
    106   1.1    darran static int	ocryptodev_session(struct fcrypt *, struct osession_op *);
    107   1.1    darran static int	ocryptodev_msession(struct fcrypt *, struct osession_n_op *, int);
    108   1.1    darran 
    109   1.1    darran int
    110   1.1    darran ocryptof_ioctl(struct file *fp, u_long cmd, void *data)
    111   1.1    darran {
    112   1.6      matt 	struct fcrypt *fcr = fp->f_fcrypt;
    113   1.1    darran 	struct csession *cse;
    114   1.1    darran 	struct osession_op *osop;
    115   1.1    darran 	struct osession_n_op *osnop;
    116   1.1    darran 	struct ocrypt_op *ocop;
    117   1.1    darran 	struct ocrypt_mop *omop;
    118   1.1    darran 	struct ocrypt_n_op *ocnop;
    119   1.1    darran 	struct ocrypt_sgop *osgop;
    120   1.1    darran 
    121   1.1    darran 	int error = 0;
    122   1.1    darran 
    123   1.1    darran 	switch (cmd) {
    124   1.1    darran 	case OCIOCGSESSION:
    125   1.1    darran 		osop = (struct osession_op *)data;
    126   1.1    darran 		error = ocryptodev_session(fcr, osop);
    127   1.1    darran 		break;
    128   1.1    darran 	case CIOCNGSESSION:
    129   1.1    darran 		osgop = (struct ocrypt_sgop *)data;
    130   1.1    darran 		osnop = kmem_alloc((osgop->count *
    131   1.1    darran 				  sizeof(struct osession_n_op)), KM_SLEEP);
    132   1.1    darran 		error = copyin(osgop->sessions, osnop, osgop->count *
    133   1.1    darran 			       sizeof(struct osession_n_op));
    134   1.1    darran 		if (error) {
    135   1.1    darran 			goto mbail;
    136   1.1    darran 		}
    137   1.1    darran 
    138   1.1    darran 		error = ocryptodev_msession(fcr, osnop, osgop->count);
    139   1.1    darran 		if (error) {
    140   1.1    darran 			goto mbail;
    141   1.1    darran 		}
    142   1.1    darran 
    143   1.1    darran 		error = copyout(osnop, osgop->sessions, osgop->count *
    144   1.1    darran 		    sizeof(struct osession_n_op));
    145   1.1    darran mbail:
    146   1.1    darran 		kmem_free(osnop, osgop->count * sizeof(struct osession_n_op));
    147   1.1    darran 		break;
    148   1.1    darran 	case OCIOCCRYPT:
    149   1.8  knakahar 		mutex_enter(&cryptodev_mtx);
    150   1.1    darran 		ocop = (struct ocrypt_op *)data;
    151   1.1    darran 		cse = cryptodev_csefind(fcr, ocop->ses);
    152   1.8  knakahar 		mutex_exit(&cryptodev_mtx);
    153   1.1    darran 		if (cse == NULL) {
    154   1.7  knakahar 			DPRINTF("csefind failed\n");
    155   1.1    darran 			return EINVAL;
    156   1.1    darran 		}
    157   1.1    darran 		error = ocryptodev_op(cse, ocop, curlwp);
    158   1.7  knakahar 		DPRINTF("ocryptodev_op error = %d\n", error);
    159   1.1    darran 		break;
    160   1.1    darran 	case OCIOCNCRYPTM:
    161   1.1    darran 		omop = (struct ocrypt_mop *)data;
    162  1.10  riastrad 		if ((omop->count <= 0) ||
    163  1.10  riastrad 		    (SIZE_MAX/sizeof(struct ocrypt_n_op) <= omop->count)) {
    164  1.10  riastrad 			error = EINVAL;
    165  1.10  riastrad 			break;
    166  1.10  riastrad 		}
    167   1.1    darran 		ocnop = kmem_alloc((omop->count * sizeof(struct ocrypt_n_op)),
    168   1.1    darran 		    KM_SLEEP);
    169   1.1    darran 		error = copyin(omop->reqs, ocnop,
    170   1.1    darran 		    (omop->count * sizeof(struct ocrypt_n_op)));
    171   1.1    darran 		if(!error) {
    172   1.1    darran 			error = ocryptodev_mop(fcr, ocnop, omop->count, curlwp);
    173   1.1    darran 			if (!error) {
    174   1.1    darran 				error = copyout(ocnop, omop->reqs,
    175   1.1    darran 				    (omop->count * sizeof(struct ocrypt_n_op)));
    176   1.1    darran 			}
    177   1.1    darran 		}
    178   1.1    darran 		kmem_free(ocnop, (omop->count * sizeof(struct ocrypt_n_op)));
    179   1.1    darran 		break;
    180   1.1    darran 	default:
    181   1.7  knakahar 		DPRINTF("invalid ioctl cmd 0x%lx\n", cmd);
    182   1.1    darran 		return EINVAL;
    183   1.1    darran 	}
    184   1.1    darran 	return error;
    185   1.1    darran }
    186   1.1    darran 
    187   1.1    darran 
    188   1.1    darran static int
    189   1.1    darran ocryptodev_op(struct csession *cse, struct ocrypt_op *ocop, struct lwp *l)
    190   1.1    darran {
    191   1.1    darran 	struct crypt_op cop;
    192   1.1    darran 
    193   1.1    darran 	cop.ses = ocop->ses;
    194   1.1    darran 	cop.op = ocop->op;
    195   1.1    darran 	cop.flags = ocop->flags;
    196   1.1    darran 	cop.len = ocop->len;
    197   1.1    darran 	cop.src = ocop->src;
    198   1.1    darran 	cop.dst = ocop->dst;
    199   1.1    darran 	cop.mac = ocop->mac;
    200   1.1    darran 	cop.iv = ocop->iv;
    201   1.1    darran 	cop.dst_len = 0;
    202   1.1    darran 
    203   1.1    darran 	return cryptodev_op(cse, &cop, l);
    204   1.1    darran };
    205   1.1    darran 
    206   1.1    darran static int
    207   1.1    darran ocryptodev_mop(struct fcrypt *fcr,
    208   1.1    darran               struct ocrypt_n_op *ocnop,
    209   1.1    darran               int count, struct lwp *l)
    210   1.1    darran {
    211   1.1    darran 	int res;
    212   1.1    darran 
    213   1.1    darran 	struct crypt_n_op cnop;
    214   1.1    darran 
    215   1.1    darran 	cnop.ses = ocnop->ses;
    216   1.1    darran 	cnop.op = ocnop->op;
    217   1.1    darran 	cnop.flags = ocnop->flags;
    218   1.1    darran 	cnop.len = ocnop->len;
    219   1.1    darran 	cnop.reqid = ocnop->reqid;
    220   1.1    darran 	cnop.status = ocnop->status;
    221   1.1    darran 	cnop.opaque = ocnop->opaque;
    222   1.1    darran 	cnop.keylen = ocnop->keylen;
    223   1.1    darran 	cnop.key = ocnop->key;
    224   1.1    darran 	cnop.mackeylen = ocnop->mackeylen;
    225   1.1    darran 	cnop.mackey = ocnop->mackey;
    226   1.1    darran 	cnop.src = ocnop->src;
    227   1.1    darran 	cnop.dst = ocnop->dst;
    228   1.1    darran 	cnop.mac = ocnop->mac;
    229   1.1    darran 	cnop.iv = ocnop->iv;
    230   1.1    darran 	cnop.dst_len = 0;
    231   1.1    darran 	res = cryptodev_mop(fcr, &cnop, count, l);
    232   1.1    darran 	ocnop->reqid = cnop.reqid;
    233   1.1    darran 	ocnop->status = cnop.status;
    234   1.1    darran 
    235   1.1    darran 	return res;
    236   1.1    darran };
    237   1.1    darran 
    238   1.1    darran 
    239   1.1    darran static int
    240   1.1    darran ocryptodev_session(struct fcrypt *fcr, struct osession_op *osop)
    241   1.1    darran {
    242   1.1    darran 	struct session_op sop;
    243   1.1    darran 	int res;
    244   1.1    darran 
    245   1.1    darran 	sop.cipher = osop->cipher;
    246   1.1    darran 	sop.mac = osop->mac;
    247   1.1    darran 	sop.comp_alg = 0;
    248   1.1    darran 	sop.keylen = osop->keylen;
    249   1.1    darran 	sop.key = osop->key;
    250   1.1    darran 	sop.mackeylen = osop->mackeylen;
    251   1.1    darran 	sop.mackey = osop->mackey;
    252   1.1    darran 	res = cryptodev_session(fcr, &sop);
    253   1.9  riastrad 	if (res)
    254   1.9  riastrad 		return res;
    255   1.1    darran 	osop->ses = sop.ses;
    256   1.9  riastrad 	return 0;
    257   1.1    darran 
    258   1.1    darran }
    259   1.1    darran 
    260   1.1    darran static int
    261   1.1    darran ocryptodev_msession(struct fcrypt *fcr, struct osession_n_op *osn_ops,
    262   1.1    darran 		   int count)
    263   1.1    darran {
    264   1.1    darran 	int i;
    265   1.1    darran 
    266   1.1    darran 	for (i = 0; i < count; i++, osn_ops++) {
    267   1.1    darran 		struct osession_op os_op;
    268   1.1    darran 		os_op.cipher =		osn_ops->cipher;
    269   1.1    darran 		os_op.mac =		osn_ops->mac;
    270   1.1    darran 		os_op.keylen =		osn_ops->keylen;
    271   1.1    darran 		os_op.key =		osn_ops->key;
    272   1.1    darran 		os_op.mackeylen =	osn_ops->mackeylen;
    273   1.1    darran 		os_op.mackey =		osn_ops->mackey;
    274   1.1    darran 
    275   1.1    darran 		osn_ops->status = ocryptodev_session(fcr, &os_op);
    276   1.1    darran 		osn_ops->ses =		os_op.ses;
    277   1.1    darran 	}
    278   1.1    darran 
    279   1.1    darran 	return 0;
    280   1.1    darran }
    281