1 /*	$NetBSD: ocryptodev.c,v 1.6 2014/09/05 09:23:40 matt 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.6 2014/09/05 09:23:40 matt Exp $");
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kmem.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/pool.h>
80 #include <sys/sysctl.h>
81 #include <sys/file.h>
82 #include <sys/filedesc.h>
83 #include <sys/errno.h>
84 #include <sys/md5.h>
85 #include <sys/sha1.h>
86 #include <sys/conf.h>
87 #include <sys/device.h>
88 #include <sys/kauth.h>
89 #include <sys/select.h>
90 #include <sys/poll.h>
91 #include <sys/atomic.h>
92 
93 #ifdef _KERNEL_OPT
94 #include "opt_ocf.h"
95 #endif
96 
97 #include <opencrypto/cryptodev.h>
98 #include <opencrypto/cryptodev_internal.h>
99 #include <opencrypto/ocryptodev.h>
100 #include <opencrypto/xform.h>
101 
102 static int	ocryptodev_op(struct csession *, struct ocrypt_op *,
103 		    struct lwp *);
104 static int	ocryptodev_mop(struct fcrypt *, struct ocrypt_n_op *, int,
105 		    struct lwp *);
106 static int	ocryptodev_session(struct fcrypt *, struct osession_op *);
107 static int	ocryptodev_msession(struct fcrypt *, struct osession_n_op *, int);
108 
109 int
ocryptof_ioctl(struct file * fp,u_long cmd,void * data)110 ocryptof_ioctl(struct file *fp, u_long cmd, void *data)
111 {
112 	struct fcrypt *fcr = fp->f_fcrypt;
113 	struct csession *cse;
114 	struct osession_op *osop;
115 	struct osession_n_op *osnop;
116 	struct ocrypt_op *ocop;
117 	struct ocrypt_mop *omop;
118 	struct ocrypt_n_op *ocnop;
119 	struct ocrypt_sgop *osgop;
120 
121 	int error = 0;
122 
123 	switch (cmd) {
124 	case OCIOCGSESSION:
125 		osop = (struct osession_op *)data;
126 		error = ocryptodev_session(fcr, osop);
127 		break;
128 	case CIOCNGSESSION:
129 		osgop = (struct ocrypt_sgop *)data;
130 		osnop = kmem_alloc((osgop->count *
131 				  sizeof(struct osession_n_op)), KM_SLEEP);
132 		error = copyin(osgop->sessions, osnop, osgop->count *
133 			       sizeof(struct osession_n_op));
134 		if (error) {
135 			goto mbail;
136 		}
137 
138 		error = ocryptodev_msession(fcr, osnop, osgop->count);
139 		if (error) {
140 			goto mbail;
141 		}
142 
143 		error = copyout(osnop, osgop->sessions, osgop->count *
144 		    sizeof(struct osession_n_op));
145 mbail:
146 		kmem_free(osnop, osgop->count * sizeof(struct osession_n_op));
147 		break;
148 	case OCIOCCRYPT:
149 		mutex_enter(&crypto_mtx);
150 		ocop = (struct ocrypt_op *)data;
151 		cse = cryptodev_csefind(fcr, ocop->ses);
152 		mutex_exit(&crypto_mtx);
153 		if (cse == NULL) {
154 			DPRINTF(("csefind failed\n"));
155 			return EINVAL;
156 		}
157 		error = ocryptodev_op(cse, ocop, curlwp);
158 		DPRINTF(("ocryptodev_op error = %d\n", error));
159 		break;
160 	case OCIOCNCRYPTM:
161 		omop = (struct ocrypt_mop *)data;
162 		ocnop = kmem_alloc((omop->count * sizeof(struct ocrypt_n_op)),
163 		    KM_SLEEP);
164 		error = copyin(omop->reqs, ocnop,
165 		    (omop->count * sizeof(struct ocrypt_n_op)));
166 		if(!error) {
167 			error = ocryptodev_mop(fcr, ocnop, omop->count, curlwp);
168 			if (!error) {
169 				error = copyout(ocnop, omop->reqs,
170 				    (omop->count * sizeof(struct ocrypt_n_op)));
171 			}
172 		}
173 		kmem_free(ocnop, (omop->count * sizeof(struct ocrypt_n_op)));
174 		break;
175 	default:
176 		DPRINTF(("invalid ioctl cmd 0x%lx\n", cmd));
177 		return EINVAL;
178 	}
179 	return error;
180 }
181 
182 
183 static int
ocryptodev_op(struct csession * cse,struct ocrypt_op * ocop,struct lwp * l)184 ocryptodev_op(struct csession *cse, struct ocrypt_op *ocop, struct lwp *l)
185 {
186 	struct crypt_op cop;
187 
188 	cop.ses = ocop->ses;
189 	cop.op = ocop->op;
190 	cop.flags = ocop->flags;
191 	cop.len = ocop->len;
192 	cop.src = ocop->src;
193 	cop.dst = ocop->dst;
194 	cop.mac = ocop->mac;
195 	cop.iv = ocop->iv;
196 	cop.dst_len = 0;
197 
198 	return cryptodev_op(cse, &cop, l);
199 };
200 
201 static int
ocryptodev_mop(struct fcrypt * fcr,struct ocrypt_n_op * ocnop,int count,struct lwp * l)202 ocryptodev_mop(struct fcrypt *fcr,
203               struct ocrypt_n_op *ocnop,
204               int count, struct lwp *l)
205 {
206 	int res;
207 
208 	struct crypt_n_op cnop;
209 
210 	cnop.ses = ocnop->ses;
211 	cnop.op = ocnop->op;
212 	cnop.flags = ocnop->flags;
213 	cnop.len = ocnop->len;
214 	cnop.reqid = ocnop->reqid;
215 	cnop.status = ocnop->status;
216 	cnop.opaque = ocnop->opaque;
217 	cnop.keylen = ocnop->keylen;
218 	cnop.key = ocnop->key;
219 	cnop.mackeylen = ocnop->mackeylen;
220 	cnop.mackey = ocnop->mackey;
221 	cnop.src = ocnop->src;
222 	cnop.dst = ocnop->dst;
223 	cnop.mac = ocnop->mac;
224 	cnop.iv = ocnop->iv;
225 	cnop.dst_len = 0;
226 	res = cryptodev_mop(fcr, &cnop, count, l);
227 	ocnop->reqid = cnop.reqid;
228 	ocnop->status = cnop.status;
229 
230 	return res;
231 };
232 
233 
234 static int
ocryptodev_session(struct fcrypt * fcr,struct osession_op * osop)235 ocryptodev_session(struct fcrypt *fcr, struct osession_op *osop)
236 {
237 	struct session_op sop;
238 	int res;
239 
240 	sop.cipher = osop->cipher;
241 	sop.mac = osop->mac;
242 	sop.comp_alg = 0;
243 	sop.keylen = osop->keylen;
244 	sop.key = osop->key;
245 	sop.mackeylen = osop->mackeylen;
246 	sop.mackey = osop->mackey;
247 	res = cryptodev_session(fcr, &sop);
248 	osop->ses = sop.ses;
249 	return res;
250 
251 }
252 
253 static int
ocryptodev_msession(struct fcrypt * fcr,struct osession_n_op * osn_ops,int count)254 ocryptodev_msession(struct fcrypt *fcr, struct osession_n_op *osn_ops,
255 		   int count)
256 {
257 	int i;
258 
259 	for (i = 0; i < count; i++, osn_ops++) {
260 		struct osession_op os_op;
261 		os_op.cipher =		osn_ops->cipher;
262 		os_op.mac =		osn_ops->mac;
263 		os_op.keylen =		osn_ops->keylen;
264 		os_op.key =		osn_ops->key;
265 		os_op.mackeylen =	osn_ops->mackeylen;
266 		os_op.mackey =		osn_ops->mackey;
267 
268 		osn_ops->status = ocryptodev_session(fcr, &os_op);
269 		osn_ops->ses =		os_op.ses;
270 	}
271 
272 	return 0;
273 }
274