Home | History | Annotate | Line # | Download | only in opencrypto
ocryptodev.c revision 1.11.4.1
      1  1.11.4.1  christos /*	$NetBSD: ocryptodev.c,v 1.11.4.1 2019/06/10 22:09:49 christos 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.11.4.1  christos __KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.11.4.1 2019/06/10 22:09:49 christos 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.11.4.1  christos #include <sys/compat_stub.h>
     93  1.11.4.1  christos #include <sys/module.h>
     94       1.1    darran 
     95       1.5  pgoyette #ifdef _KERNEL_OPT
     96       1.1    darran #include "opt_ocf.h"
     97       1.5  pgoyette #endif
     98       1.5  pgoyette 
     99       1.1    darran #include <opencrypto/cryptodev.h>
    100       1.3  drochner #include <opencrypto/cryptodev_internal.h>
    101       1.1    darran #include <opencrypto/ocryptodev.h>
    102       1.1    darran #include <opencrypto/xform.h>
    103       1.1    darran 
    104       1.1    darran static int	ocryptodev_op(struct csession *, struct ocrypt_op *,
    105       1.1    darran 		    struct lwp *);
    106       1.1    darran static int	ocryptodev_mop(struct fcrypt *, struct ocrypt_n_op *, int,
    107       1.1    darran 		    struct lwp *);
    108       1.1    darran static int	ocryptodev_session(struct fcrypt *, struct osession_op *);
    109  1.11.4.1  christos static int	ocryptodev_msession(struct fcrypt *, struct osession_n_op *,
    110  1.11.4.1  christos 		    int);
    111       1.1    darran 
    112       1.1    darran int
    113       1.1    darran ocryptof_ioctl(struct file *fp, u_long cmd, void *data)
    114       1.1    darran {
    115       1.6      matt 	struct fcrypt *fcr = fp->f_fcrypt;
    116       1.1    darran 	struct csession *cse;
    117       1.1    darran 	struct osession_op *osop;
    118       1.1    darran 	struct osession_n_op *osnop;
    119       1.1    darran 	struct ocrypt_op *ocop;
    120       1.1    darran 	struct ocrypt_mop *omop;
    121       1.1    darran 	struct ocrypt_n_op *ocnop;
    122       1.1    darran 	struct ocrypt_sgop *osgop;
    123       1.1    darran 
    124       1.1    darran 	int error = 0;
    125       1.1    darran 
    126       1.1    darran 	switch (cmd) {
    127       1.1    darran 	case OCIOCGSESSION:
    128       1.1    darran 		osop = (struct osession_op *)data;
    129       1.1    darran 		error = ocryptodev_session(fcr, osop);
    130       1.1    darran 		break;
    131       1.1    darran 	case CIOCNGSESSION:
    132       1.1    darran 		osgop = (struct ocrypt_sgop *)data;
    133      1.11  riastrad 		if ((osgop->count <= 0) ||
    134      1.11  riastrad 		    (SIZE_MAX/sizeof(struct osession_n_op) < osgop->count)) {
    135      1.11  riastrad 			error = EINVAL;
    136      1.11  riastrad 			break;
    137      1.11  riastrad 		}
    138       1.1    darran 		osnop = kmem_alloc((osgop->count *
    139       1.1    darran 				  sizeof(struct osession_n_op)), KM_SLEEP);
    140       1.1    darran 		error = copyin(osgop->sessions, osnop, osgop->count *
    141       1.1    darran 			       sizeof(struct osession_n_op));
    142       1.1    darran 		if (error) {
    143       1.1    darran 			goto mbail;
    144       1.1    darran 		}
    145       1.1    darran 
    146       1.1    darran 		error = ocryptodev_msession(fcr, osnop, osgop->count);
    147       1.1    darran 		if (error) {
    148       1.1    darran 			goto mbail;
    149       1.1    darran 		}
    150       1.1    darran 
    151       1.1    darran 		error = copyout(osnop, osgop->sessions, osgop->count *
    152       1.1    darran 		    sizeof(struct osession_n_op));
    153       1.1    darran mbail:
    154       1.1    darran 		kmem_free(osnop, osgop->count * sizeof(struct osession_n_op));
    155       1.1    darran 		break;
    156       1.1    darran 	case OCIOCCRYPT:
    157       1.8  knakahar 		mutex_enter(&cryptodev_mtx);
    158       1.1    darran 		ocop = (struct ocrypt_op *)data;
    159       1.1    darran 		cse = cryptodev_csefind(fcr, ocop->ses);
    160       1.8  knakahar 		mutex_exit(&cryptodev_mtx);
    161       1.1    darran 		if (cse == NULL) {
    162       1.7  knakahar 			DPRINTF("csefind failed\n");
    163       1.1    darran 			return EINVAL;
    164       1.1    darran 		}
    165       1.1    darran 		error = ocryptodev_op(cse, ocop, curlwp);
    166       1.7  knakahar 		DPRINTF("ocryptodev_op error = %d\n", error);
    167       1.1    darran 		break;
    168       1.1    darran 	case OCIOCNCRYPTM:
    169       1.1    darran 		omop = (struct ocrypt_mop *)data;
    170      1.10  riastrad 		if ((omop->count <= 0) ||
    171      1.10  riastrad 		    (SIZE_MAX/sizeof(struct ocrypt_n_op) <= omop->count)) {
    172      1.10  riastrad 			error = EINVAL;
    173      1.10  riastrad 			break;
    174      1.10  riastrad 		}
    175       1.1    darran 		ocnop = kmem_alloc((omop->count * sizeof(struct ocrypt_n_op)),
    176       1.1    darran 		    KM_SLEEP);
    177       1.1    darran 		error = copyin(omop->reqs, ocnop,
    178       1.1    darran 		    (omop->count * sizeof(struct ocrypt_n_op)));
    179       1.1    darran 		if(!error) {
    180  1.11.4.1  christos 			error = ocryptodev_mop(fcr, ocnop, omop->count,
    181  1.11.4.1  christos 			    curlwp);
    182       1.1    darran 			if (!error) {
    183       1.1    darran 				error = copyout(ocnop, omop->reqs,
    184       1.1    darran 				    (omop->count * sizeof(struct ocrypt_n_op)));
    185       1.1    darran 			}
    186       1.1    darran 		}
    187       1.1    darran 		kmem_free(ocnop, (omop->count * sizeof(struct ocrypt_n_op)));
    188       1.1    darran 		break;
    189       1.1    darran 	default:
    190       1.7  knakahar 		DPRINTF("invalid ioctl cmd 0x%lx\n", cmd);
    191       1.1    darran 		return EINVAL;
    192       1.1    darran 	}
    193       1.1    darran 	return error;
    194       1.1    darran }
    195       1.1    darran 
    196       1.1    darran 
    197       1.1    darran static int
    198       1.1    darran ocryptodev_op(struct csession *cse, struct ocrypt_op *ocop, struct lwp *l)
    199       1.1    darran {
    200       1.1    darran 	struct crypt_op cop;
    201       1.1    darran 
    202       1.1    darran 	cop.ses = ocop->ses;
    203       1.1    darran 	cop.op = ocop->op;
    204       1.1    darran 	cop.flags = ocop->flags;
    205       1.1    darran 	cop.len = ocop->len;
    206       1.1    darran 	cop.src = ocop->src;
    207       1.1    darran 	cop.dst = ocop->dst;
    208       1.1    darran 	cop.mac = ocop->mac;
    209       1.1    darran 	cop.iv = ocop->iv;
    210       1.1    darran 	cop.dst_len = 0;
    211       1.1    darran 
    212       1.1    darran 	return cryptodev_op(cse, &cop, l);
    213       1.1    darran };
    214       1.1    darran 
    215       1.1    darran static int
    216  1.11.4.1  christos ocryptodev_mop(struct fcrypt *fcr, struct ocrypt_n_op *ocnop, int count,
    217  1.11.4.1  christos     struct lwp *l)
    218       1.1    darran {
    219       1.1    darran 	int res;
    220       1.1    darran 
    221       1.1    darran 	struct crypt_n_op cnop;
    222       1.1    darran 
    223       1.1    darran 	cnop.ses = ocnop->ses;
    224       1.1    darran 	cnop.op = ocnop->op;
    225       1.1    darran 	cnop.flags = ocnop->flags;
    226       1.1    darran 	cnop.len = ocnop->len;
    227       1.1    darran 	cnop.reqid = ocnop->reqid;
    228       1.1    darran 	cnop.status = ocnop->status;
    229       1.1    darran 	cnop.opaque = ocnop->opaque;
    230       1.1    darran 	cnop.keylen = ocnop->keylen;
    231       1.1    darran 	cnop.key = ocnop->key;
    232       1.1    darran 	cnop.mackeylen = ocnop->mackeylen;
    233       1.1    darran 	cnop.mackey = ocnop->mackey;
    234       1.1    darran 	cnop.src = ocnop->src;
    235       1.1    darran 	cnop.dst = ocnop->dst;
    236       1.1    darran 	cnop.mac = ocnop->mac;
    237       1.1    darran 	cnop.iv = ocnop->iv;
    238       1.1    darran 	cnop.dst_len = 0;
    239       1.1    darran 	res = cryptodev_mop(fcr, &cnop, count, l);
    240       1.1    darran 	ocnop->reqid = cnop.reqid;
    241       1.1    darran 	ocnop->status = cnop.status;
    242       1.1    darran 
    243       1.1    darran 	return res;
    244       1.1    darran };
    245       1.1    darran 
    246       1.1    darran 
    247       1.1    darran static int
    248  1.11.4.1  christos ocryptodev_session(struct fcrypt *fcr, struct osession_op *osop)
    249       1.1    darran {
    250       1.1    darran 	struct session_op sop;
    251       1.1    darran 	int res;
    252       1.1    darran 
    253       1.1    darran 	sop.cipher = osop->cipher;
    254       1.1    darran 	sop.mac = osop->mac;
    255       1.1    darran 	sop.comp_alg = 0;
    256       1.1    darran 	sop.keylen = osop->keylen;
    257       1.1    darran 	sop.key = osop->key;
    258       1.1    darran 	sop.mackeylen = osop->mackeylen;
    259       1.1    darran 	sop.mackey = osop->mackey;
    260       1.1    darran 	res = cryptodev_session(fcr, &sop);
    261       1.9  riastrad 	if (res)
    262       1.9  riastrad 		return res;
    263       1.1    darran 	osop->ses = sop.ses;
    264       1.9  riastrad 	return 0;
    265       1.1    darran 
    266       1.1    darran }
    267       1.1    darran 
    268       1.1    darran static int
    269       1.1    darran ocryptodev_msession(struct fcrypt *fcr, struct osession_n_op *osn_ops,
    270       1.1    darran 		   int count)
    271       1.1    darran {
    272       1.1    darran 	int i;
    273       1.1    darran 
    274       1.1    darran 	for (i = 0; i < count; i++, osn_ops++) {
    275       1.1    darran 		struct osession_op os_op;
    276       1.1    darran 		os_op.cipher =		osn_ops->cipher;
    277       1.1    darran 		os_op.mac =		osn_ops->mac;
    278       1.1    darran 		os_op.keylen =		osn_ops->keylen;
    279       1.1    darran 		os_op.key =		osn_ops->key;
    280       1.1    darran 		os_op.mackeylen =	osn_ops->mackeylen;
    281       1.1    darran 		os_op.mackey =		osn_ops->mackey;
    282       1.1    darran 
    283       1.1    darran 		osn_ops->status = ocryptodev_session(fcr, &os_op);
    284       1.1    darran 		osn_ops->ses =		os_op.ses;
    285       1.1    darran 	}
    286       1.1    darran 
    287       1.1    darran 	return 0;
    288       1.1    darran }
    289  1.11.4.1  christos 
    290  1.11.4.1  christos static void
    291  1.11.4.1  christos crypto_50_init(void)
    292  1.11.4.1  christos {
    293  1.11.4.1  christos 
    294  1.11.4.1  christos 	MODULE_HOOK_SET(ocryptof_50_hook, "cryp50", ocryptof_ioctl);
    295  1.11.4.1  christos }
    296  1.11.4.1  christos 
    297  1.11.4.1  christos static void
    298  1.11.4.1  christos crypto_50_fini(void)
    299  1.11.4.1  christos {
    300  1.11.4.1  christos 
    301  1.11.4.1  christos 	MODULE_HOOK_UNSET(ocryptof_50_hook);
    302  1.11.4.1  christos }
    303  1.11.4.1  christos 
    304  1.11.4.1  christos MODULE(MODULE_CLASS_EXEC, compat_crypto_50, "crypto,compat_50");
    305  1.11.4.1  christos 
    306  1.11.4.1  christos static int
    307  1.11.4.1  christos compat_crypto_50_modcmd(modcmd_t cmd, void *arg)
    308  1.11.4.1  christos {
    309  1.11.4.1  christos 
    310  1.11.4.1  christos 	switch (cmd) {
    311  1.11.4.1  christos 	case MODULE_CMD_INIT:
    312  1.11.4.1  christos 		crypto_50_init();
    313  1.11.4.1  christos 		return 0;
    314  1.11.4.1  christos 	case MODULE_CMD_FINI:
    315  1.11.4.1  christos 		crypto_50_fini();
    316  1.11.4.1  christos 		return 0;
    317  1.11.4.1  christos 	default:
    318  1.11.4.1  christos 		return ENOTTY;
    319  1.11.4.1  christos 	}
    320  1.11.4.1  christos }
    321  1.11.4.1  christos 
    322