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