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