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