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