No empty .Rs/.Re
[netbsd-mini2440.git] / sys / opencrypto / ocryptodev.c
blob734147408cba1f546832ce8c738bb39370cf92e8
1 /* $NetBSD: ocryptodev.c,v 1.1 2009/03/25 01:26:13 darran 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 $ */
5 /*-
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Coyote Point Systems, Inc.
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.
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.
35 * Copyright (c) 2001 Theo de Raadt
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
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.
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.
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.
67 * Implement backward compatibility IOCTLs in this module.
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.1 2009/03/25 01:26:13 darran Exp $");
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>
93 #include "opt_ocf.h"
94 #include <opencrypto/cryptodev.h>
95 #include <opencrypto/ocryptodev.h>
96 #include <opencrypto/xform.h>
98 static int ocryptodev_op(struct csession *, struct ocrypt_op *,
99 struct lwp *);
100 static int ocryptodev_mop(struct fcrypt *, struct ocrypt_n_op *, int,
101 struct lwp *);
102 static int ocryptodev_session(struct fcrypt *, struct osession_op *);
103 static int ocryptodev_msession(struct fcrypt *, struct osession_n_op *, int);
106 ocryptof_ioctl(struct file *fp, u_long cmd, void *data)
108 struct fcrypt *fcr = fp->f_data;
109 struct csession *cse;
110 struct osession_op *osop;
111 struct osession_n_op *osnop;
112 struct ocrypt_op *ocop;
113 struct ocrypt_mop *omop;
114 struct ocrypt_n_op *ocnop;
115 struct ocrypt_sgop *osgop;
117 int error = 0;
119 switch (cmd) {
120 case OCIOCGSESSION:
121 osop = (struct osession_op *)data;
122 error = ocryptodev_session(fcr, osop);
123 break;
124 case CIOCNGSESSION:
125 osgop = (struct ocrypt_sgop *)data;
126 osnop = kmem_alloc((osgop->count *
127 sizeof(struct osession_n_op)), KM_SLEEP);
128 error = copyin(osgop->sessions, osnop, osgop->count *
129 sizeof(struct osession_n_op));
130 if (error) {
131 goto mbail;
134 error = ocryptodev_msession(fcr, osnop, osgop->count);
135 if (error) {
136 goto mbail;
139 error = copyout(osnop, osgop->sessions, osgop->count *
140 sizeof(struct osession_n_op));
141 mbail:
142 kmem_free(osnop, osgop->count * sizeof(struct osession_n_op));
143 break;
144 case OCIOCCRYPT:
145 mutex_spin_enter(&crypto_mtx);
146 ocop = (struct ocrypt_op *)data;
147 cse = cryptodev_csefind(fcr, ocop->ses);
148 mutex_spin_exit(&crypto_mtx);
149 if (cse == NULL) {
150 DPRINTF(("csefind failed\n"));
151 return EINVAL;
153 error = ocryptodev_op(cse, ocop, curlwp);
154 DPRINTF(("ocryptodev_op error = %d\n", error));
155 break;
156 case OCIOCNCRYPTM:
157 omop = (struct ocrypt_mop *)data;
158 ocnop = kmem_alloc((omop->count * sizeof(struct ocrypt_n_op)),
159 KM_SLEEP);
160 error = copyin(omop->reqs, ocnop,
161 (omop->count * sizeof(struct ocrypt_n_op)));
162 if(!error) {
163 error = ocryptodev_mop(fcr, ocnop, omop->count, curlwp);
164 if (!error) {
165 error = copyout(ocnop, omop->reqs,
166 (omop->count * sizeof(struct ocrypt_n_op)));
169 kmem_free(ocnop, (omop->count * sizeof(struct ocrypt_n_op)));
170 break;
171 default:
172 DPRINTF(("invalid ioctl cmd 0x%lx\n", cmd));
173 return EINVAL;
175 return error;
179 static int
180 ocryptodev_op(struct csession *cse, struct ocrypt_op *ocop, struct lwp *l)
182 struct crypt_op cop;
184 cop.ses = ocop->ses;
185 cop.op = ocop->op;
186 cop.flags = ocop->flags;
187 cop.len = ocop->len;
188 cop.src = ocop->src;
189 cop.dst = ocop->dst;
190 cop.mac = ocop->mac;
191 cop.iv = ocop->iv;
192 cop.dst_len = 0;
194 return cryptodev_op(cse, &cop, l);
197 static int
198 ocryptodev_mop(struct fcrypt *fcr,
199 struct ocrypt_n_op *ocnop,
200 int count, struct lwp *l)
202 int res;
204 struct crypt_n_op cnop;
206 cnop.ses = ocnop->ses;
207 cnop.op = ocnop->op;
208 cnop.flags = ocnop->flags;
209 cnop.len = ocnop->len;
210 cnop.reqid = ocnop->reqid;
211 cnop.status = ocnop->status;
212 cnop.opaque = ocnop->opaque;
213 cnop.keylen = ocnop->keylen;
214 cnop.key = ocnop->key;
215 cnop.mackeylen = ocnop->mackeylen;
216 cnop.mackey = ocnop->mackey;
217 cnop.src = ocnop->src;
218 cnop.dst = ocnop->dst;
219 cnop.mac = ocnop->mac;
220 cnop.iv = ocnop->iv;
221 cnop.dst_len = 0;
222 res = cryptodev_mop(fcr, &cnop, count, l);
223 ocnop->reqid = cnop.reqid;
224 ocnop->status = cnop.status;
226 return res;
230 static int
231 ocryptodev_session(struct fcrypt *fcr, struct osession_op *osop)
233 struct session_op sop;
234 int res;
236 sop.cipher = osop->cipher;
237 sop.mac = osop->mac;
238 sop.comp_alg = 0;
239 sop.keylen = osop->keylen;
240 sop.key = osop->key;
241 sop.mackeylen = osop->mackeylen;
242 sop.mackey = osop->mackey;
243 res = cryptodev_session(fcr, &sop);
244 osop->ses = sop.ses;
245 return res;
249 static int
250 ocryptodev_msession(struct fcrypt *fcr, struct osession_n_op *osn_ops,
251 int count)
253 int i;
255 for (i = 0; i < count; i++, osn_ops++) {
256 struct osession_op os_op;
257 os_op.cipher = osn_ops->cipher;
258 os_op.mac = osn_ops->mac;
259 os_op.keylen = osn_ops->keylen;
260 os_op.key = osn_ops->key;
261 os_op.mackeylen = osn_ops->mackeylen;
262 os_op.mackey = osn_ops->mackey;
264 osn_ops->status = ocryptodev_session(fcr, &os_op);
265 osn_ops->ses = os_op.ses;
268 return 0;