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