cryptodev.c revision 1.31 1 1.31 tls /* $NetBSD: cryptodev.c,v 1.31 2008/02/01 04:52:35 tls Exp $ */
2 1.4 jonathan /* $FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $ */
3 1.1 jonathan /* $OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $ */
4 1.1 jonathan
5 1.1 jonathan /*
6 1.1 jonathan * Copyright (c) 2001 Theo de Raadt
7 1.1 jonathan *
8 1.1 jonathan * Redistribution and use in source and binary forms, with or without
9 1.1 jonathan * modification, are permitted provided that the following conditions
10 1.1 jonathan * are met:
11 1.1 jonathan *
12 1.1 jonathan * 1. Redistributions of source code must retain the above copyright
13 1.1 jonathan * notice, this list of conditions and the following disclaimer.
14 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jonathan * notice, this list of conditions and the following disclaimer in the
16 1.1 jonathan * documentation and/or other materials provided with the distribution.
17 1.1 jonathan * 3. The name of the author may not be used to endorse or promote products
18 1.1 jonathan * derived from this software without specific prior written permission.
19 1.1 jonathan *
20 1.1 jonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 jonathan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 jonathan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 jonathan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 jonathan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 jonathan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 jonathan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 jonathan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 jonathan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 jonathan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 jonathan *
31 1.1 jonathan * Effort sponsored in part by the Defense Advanced Research Projects
32 1.1 jonathan * Agency (DARPA) and Air Force Research Laboratory, Air Force
33 1.1 jonathan * Materiel Command, USAF, under agreement number F30602-01-2-0537.
34 1.1 jonathan *
35 1.1 jonathan */
36 1.1 jonathan
37 1.1 jonathan #include <sys/cdefs.h>
38 1.31 tls __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.31 2008/02/01 04:52:35 tls Exp $");
39 1.1 jonathan
40 1.1 jonathan #include <sys/param.h>
41 1.1 jonathan #include <sys/systm.h>
42 1.1 jonathan #include <sys/malloc.h>
43 1.1 jonathan #include <sys/mbuf.h>
44 1.29 tls #include <sys/pool.h>
45 1.1 jonathan #include <sys/sysctl.h>
46 1.1 jonathan #include <sys/file.h>
47 1.1 jonathan #include <sys/filedesc.h>
48 1.1 jonathan #include <sys/errno.h>
49 1.2 jonathan #include <sys/md5.h>
50 1.1 jonathan #include <sys/sha1.h>
51 1.1 jonathan #include <sys/conf.h>
52 1.1 jonathan #include <sys/device.h>
53 1.21 elad #include <sys/kauth.h>
54 1.1 jonathan
55 1.1 jonathan #include <opencrypto/cryptodev.h>
56 1.1 jonathan #include <opencrypto/xform.h>
57 1.1 jonathan
58 1.1 jonathan #define splcrypto splnet
59 1.18 christos #ifdef CRYPTO_DEBUG
60 1.18 christos #define DPRINTF(a) uprintf a
61 1.18 christos #else
62 1.18 christos #define DPRINTF(a)
63 1.18 christos #endif
64 1.1 jonathan
65 1.1 jonathan struct csession {
66 1.1 jonathan TAILQ_ENTRY(csession) next;
67 1.1 jonathan u_int64_t sid;
68 1.1 jonathan u_int32_t ses;
69 1.1 jonathan
70 1.1 jonathan u_int32_t cipher;
71 1.1 jonathan struct enc_xform *txform;
72 1.1 jonathan u_int32_t mac;
73 1.1 jonathan struct auth_hash *thash;
74 1.1 jonathan
75 1.26 christos void * key;
76 1.1 jonathan int keylen;
77 1.1 jonathan u_char tmp_iv[EALG_MAX_BLOCK_LEN];
78 1.1 jonathan
79 1.26 christos void * mackey;
80 1.1 jonathan int mackeylen;
81 1.1 jonathan u_char tmp_mac[CRYPTO_MAX_MAC_LEN];
82 1.1 jonathan
83 1.29 tls struct iovec iovec[1]; /* user requests never have more */
84 1.1 jonathan struct uio uio;
85 1.1 jonathan int error;
86 1.1 jonathan };
87 1.1 jonathan
88 1.1 jonathan struct fcrypt {
89 1.1 jonathan TAILQ_HEAD(csessionlist, csession) csessions;
90 1.1 jonathan int sesn;
91 1.1 jonathan };
92 1.1 jonathan
93 1.29 tls /* For our fixed-size allocations */
94 1.29 tls struct pool fcrpl;
95 1.29 tls struct pool csepl;
96 1.1 jonathan
97 1.1 jonathan /* Declaration of master device (fd-cloning/ctxt-allocating) entrypoints */
98 1.16 christos static int cryptoopen(dev_t dev, int flag, int mode, struct lwp *l);
99 1.1 jonathan static int cryptoread(dev_t dev, struct uio *uio, int ioflag);
100 1.1 jonathan static int cryptowrite(dev_t dev, struct uio *uio, int ioflag);
101 1.16 christos static int cryptoselect(dev_t dev, int rw, struct lwp *l);
102 1.1 jonathan
103 1.1 jonathan /* Declaration of cloned-device (per-ctxt) entrypoints */
104 1.21 elad static int cryptof_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
105 1.21 elad static int cryptof_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
106 1.16 christos static int cryptof_ioctl(struct file *, u_long, void*, struct lwp *l);
107 1.16 christos static int cryptof_close(struct file *, struct lwp *);
108 1.1 jonathan
109 1.12 christos static const struct fileops cryptofops = {
110 1.1 jonathan cryptof_read,
111 1.1 jonathan cryptof_write,
112 1.1 jonathan cryptof_ioctl,
113 1.12 christos fnullop_fcntl,
114 1.12 christos fnullop_poll,
115 1.12 christos fbadop_stat,
116 1.1 jonathan cryptof_close,
117 1.12 christos fnullop_kqfilter
118 1.1 jonathan };
119 1.1 jonathan
120 1.1 jonathan static struct csession *csefind(struct fcrypt *, u_int);
121 1.1 jonathan static int csedelete(struct fcrypt *, struct csession *);
122 1.1 jonathan static struct csession *cseadd(struct fcrypt *, struct csession *);
123 1.26 christos static struct csession *csecreate(struct fcrypt *, u_int64_t, void *, u_int64_t,
124 1.26 christos void *, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
125 1.1 jonathan struct auth_hash *);
126 1.1 jonathan static int csefree(struct csession *);
127 1.1 jonathan
128 1.16 christos static int cryptodev_op(struct csession *, struct crypt_op *, struct lwp *);
129 1.1 jonathan static int cryptodev_key(struct crypt_kop *);
130 1.1 jonathan int cryptodev_dokey(struct crypt_kop *kop, struct crparam kvp[]);
131 1.1 jonathan
132 1.1 jonathan static int cryptodev_cb(void *);
133 1.1 jonathan static int cryptodevkey_cb(void *);
134 1.1 jonathan
135 1.9 jonathan /*
136 1.9 jonathan * sysctl-able control variables for /dev/crypto now defined in crypto.c:
137 1.9 jonathan * crypto_usercrypto, crypto_userasmcrypto, crypto_devallowsoft.
138 1.9 jonathan */
139 1.1 jonathan
140 1.1 jonathan /* ARGSUSED */
141 1.1 jonathan int
142 1.25 christos cryptof_read(struct file *fp, off_t *poff,
143 1.25 christos struct uio *uio, kauth_cred_t cred, int flags)
144 1.1 jonathan {
145 1.1 jonathan return (EIO);
146 1.1 jonathan }
147 1.1 jonathan
148 1.1 jonathan /* ARGSUSED */
149 1.1 jonathan int
150 1.25 christos cryptof_write(struct file *fp, off_t *poff,
151 1.25 christos struct uio *uio, kauth_cred_t cred, int flags)
152 1.1 jonathan {
153 1.1 jonathan return (EIO);
154 1.1 jonathan }
155 1.1 jonathan
156 1.1 jonathan /* ARGSUSED */
157 1.1 jonathan int
158 1.16 christos cryptof_ioctl(struct file *fp, u_long cmd, void* data, struct lwp *l)
159 1.1 jonathan {
160 1.1 jonathan struct cryptoini cria, crie;
161 1.1 jonathan struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
162 1.1 jonathan struct csession *cse;
163 1.1 jonathan struct session_op *sop;
164 1.1 jonathan struct crypt_op *cop;
165 1.1 jonathan struct enc_xform *txform = NULL;
166 1.1 jonathan struct auth_hash *thash = NULL;
167 1.1 jonathan u_int64_t sid;
168 1.1 jonathan u_int32_t ses;
169 1.1 jonathan int error = 0;
170 1.1 jonathan
171 1.29 tls /* backwards compatibility */
172 1.29 tls struct file *criofp;
173 1.29 tls struct fcrypt *criofcr;
174 1.29 tls int criofd;
175 1.29 tls
176 1.29 tls switch (cmd) {
177 1.29 tls case CRIOGET: /* XXX deprecated, remove after 5.0 */
178 1.29 tls if ((error = falloc(l, &criofp, &criofd)) != 0)
179 1.29 tls return error;
180 1.29 tls criofcr = pool_get(&fcrpl, PR_WAITOK);
181 1.29 tls TAILQ_INIT(&criofcr->csessions);
182 1.29 tls /*
183 1.29 tls * Don't ever return session 0, to allow detection of
184 1.29 tls * failed creation attempts with multi-create ioctl.
185 1.29 tls */
186 1.29 tls criofcr->sesn = 1;
187 1.29 tls (void)fdclone(l, criofp, criofd, (FREAD|FWRITE),
188 1.29 tls &cryptofops, criofcr);
189 1.29 tls *(u_int32_t *)data = criofd;
190 1.29 tls return error;
191 1.29 tls break;
192 1.1 jonathan case CIOCGSESSION:
193 1.1 jonathan sop = (struct session_op *)data;
194 1.1 jonathan switch (sop->cipher) {
195 1.1 jonathan case 0:
196 1.1 jonathan break;
197 1.1 jonathan case CRYPTO_DES_CBC:
198 1.1 jonathan txform = &enc_xform_des;
199 1.1 jonathan break;
200 1.1 jonathan case CRYPTO_3DES_CBC:
201 1.1 jonathan txform = &enc_xform_3des;
202 1.1 jonathan break;
203 1.1 jonathan case CRYPTO_BLF_CBC:
204 1.1 jonathan txform = &enc_xform_blf;
205 1.1 jonathan break;
206 1.1 jonathan case CRYPTO_CAST_CBC:
207 1.1 jonathan txform = &enc_xform_cast5;
208 1.1 jonathan break;
209 1.1 jonathan case CRYPTO_SKIPJACK_CBC:
210 1.1 jonathan txform = &enc_xform_skipjack;
211 1.1 jonathan break;
212 1.1 jonathan case CRYPTO_AES_CBC:
213 1.1 jonathan txform = &enc_xform_rijndael128;
214 1.1 jonathan break;
215 1.1 jonathan case CRYPTO_NULL_CBC:
216 1.1 jonathan txform = &enc_xform_null;
217 1.1 jonathan break;
218 1.1 jonathan case CRYPTO_ARC4:
219 1.1 jonathan txform = &enc_xform_arc4;
220 1.1 jonathan break;
221 1.1 jonathan default:
222 1.18 christos DPRINTF(("Invalid cipher %d\n", sop->cipher));
223 1.1 jonathan return (EINVAL);
224 1.1 jonathan }
225 1.1 jonathan
226 1.1 jonathan switch (sop->mac) {
227 1.1 jonathan case 0:
228 1.1 jonathan break;
229 1.1 jonathan case CRYPTO_MD5_HMAC:
230 1.1 jonathan thash = &auth_hash_hmac_md5_96;
231 1.1 jonathan break;
232 1.1 jonathan case CRYPTO_SHA1_HMAC:
233 1.1 jonathan thash = &auth_hash_hmac_sha1_96;
234 1.1 jonathan break;
235 1.1 jonathan case CRYPTO_SHA2_HMAC:
236 1.1 jonathan if (sop->mackeylen == auth_hash_hmac_sha2_256.keysize)
237 1.1 jonathan thash = &auth_hash_hmac_sha2_256;
238 1.1 jonathan else if (sop->mackeylen == auth_hash_hmac_sha2_384.keysize)
239 1.1 jonathan thash = &auth_hash_hmac_sha2_384;
240 1.1 jonathan else if (sop->mackeylen == auth_hash_hmac_sha2_512.keysize)
241 1.1 jonathan thash = &auth_hash_hmac_sha2_512;
242 1.18 christos else {
243 1.18 christos DPRINTF(("Invalid mackeylen %d\n",
244 1.18 christos sop->mackeylen));
245 1.1 jonathan return (EINVAL);
246 1.18 christos }
247 1.1 jonathan break;
248 1.1 jonathan case CRYPTO_RIPEMD160_HMAC:
249 1.1 jonathan thash = &auth_hash_hmac_ripemd_160_96;
250 1.1 jonathan break;
251 1.1 jonathan case CRYPTO_MD5:
252 1.1 jonathan thash = &auth_hash_md5;
253 1.1 jonathan break;
254 1.1 jonathan case CRYPTO_SHA1:
255 1.1 jonathan thash = &auth_hash_sha1;
256 1.1 jonathan break;
257 1.1 jonathan case CRYPTO_NULL_HMAC:
258 1.1 jonathan thash = &auth_hash_null;
259 1.1 jonathan break;
260 1.1 jonathan default:
261 1.18 christos DPRINTF(("Invalid mac %d\n", sop->mac));
262 1.1 jonathan return (EINVAL);
263 1.1 jonathan }
264 1.1 jonathan
265 1.1 jonathan bzero(&crie, sizeof(crie));
266 1.1 jonathan bzero(&cria, sizeof(cria));
267 1.1 jonathan
268 1.1 jonathan if (txform) {
269 1.1 jonathan crie.cri_alg = txform->type;
270 1.1 jonathan crie.cri_klen = sop->keylen * 8;
271 1.1 jonathan if (sop->keylen > txform->maxkey ||
272 1.1 jonathan sop->keylen < txform->minkey) {
273 1.18 christos DPRINTF(("keylen %d not in [%d,%d]\n",
274 1.18 christos sop->keylen, txform->minkey,
275 1.18 christos txform->maxkey));
276 1.1 jonathan error = EINVAL;
277 1.1 jonathan goto bail;
278 1.1 jonathan }
279 1.1 jonathan
280 1.19 christos crie.cri_key = malloc(crie.cri_klen / 8, M_XDATA,
281 1.19 christos M_WAITOK);
282 1.1 jonathan if ((error = copyin(sop->key, crie.cri_key,
283 1.1 jonathan crie.cri_klen / 8)))
284 1.1 jonathan goto bail;
285 1.1 jonathan if (thash)
286 1.1 jonathan crie.cri_next = &cria;
287 1.1 jonathan }
288 1.1 jonathan
289 1.1 jonathan if (thash) {
290 1.1 jonathan cria.cri_alg = thash->type;
291 1.1 jonathan cria.cri_klen = sop->mackeylen * 8;
292 1.1 jonathan if (sop->mackeylen != thash->keysize) {
293 1.18 christos DPRINTF(("mackeylen %d != keysize %d\n",
294 1.18 christos sop->mackeylen, thash->keysize));
295 1.1 jonathan error = EINVAL;
296 1.1 jonathan goto bail;
297 1.1 jonathan }
298 1.1 jonathan
299 1.1 jonathan if (cria.cri_klen) {
300 1.19 christos cria.cri_key = malloc(cria.cri_klen / 8,
301 1.19 christos M_XDATA, M_WAITOK);
302 1.1 jonathan if ((error = copyin(sop->mackey, cria.cri_key,
303 1.1 jonathan cria.cri_klen / 8)))
304 1.1 jonathan goto bail;
305 1.1 jonathan }
306 1.1 jonathan }
307 1.1 jonathan
308 1.1 jonathan error = crypto_newsession(&sid, (txform ? &crie : &cria),
309 1.30 tls crypto_devallowsoft);
310 1.1 jonathan if (error) {
311 1.18 christos DPRINTF(("SIOCSESSION violates kernel parameters %d\n",
312 1.18 christos error));
313 1.1 jonathan goto bail;
314 1.1 jonathan }
315 1.1 jonathan
316 1.1 jonathan cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
317 1.1 jonathan cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform,
318 1.1 jonathan thash);
319 1.1 jonathan
320 1.1 jonathan if (cse == NULL) {
321 1.18 christos DPRINTF(("csecreate failed\n"));
322 1.1 jonathan crypto_freesession(sid);
323 1.1 jonathan error = EINVAL;
324 1.1 jonathan goto bail;
325 1.1 jonathan }
326 1.1 jonathan sop->ses = cse->ses;
327 1.1 jonathan
328 1.1 jonathan bail:
329 1.1 jonathan if (error) {
330 1.1 jonathan if (crie.cri_key)
331 1.1 jonathan FREE(crie.cri_key, M_XDATA);
332 1.1 jonathan if (cria.cri_key)
333 1.1 jonathan FREE(cria.cri_key, M_XDATA);
334 1.1 jonathan }
335 1.1 jonathan break;
336 1.1 jonathan case CIOCFSESSION:
337 1.1 jonathan ses = *(u_int32_t *)data;
338 1.1 jonathan cse = csefind(fcr, ses);
339 1.1 jonathan if (cse == NULL)
340 1.1 jonathan return (EINVAL);
341 1.1 jonathan csedelete(fcr, cse);
342 1.1 jonathan error = csefree(cse);
343 1.1 jonathan break;
344 1.1 jonathan case CIOCCRYPT:
345 1.1 jonathan cop = (struct crypt_op *)data;
346 1.1 jonathan cse = csefind(fcr, cop->ses);
347 1.18 christos if (cse == NULL) {
348 1.18 christos DPRINTF(("csefind failed\n"));
349 1.1 jonathan return (EINVAL);
350 1.18 christos }
351 1.16 christos error = cryptodev_op(cse, cop, l);
352 1.1 jonathan break;
353 1.1 jonathan case CIOCKEY:
354 1.1 jonathan error = cryptodev_key((struct crypt_kop *)data);
355 1.1 jonathan break;
356 1.1 jonathan case CIOCASYMFEAT:
357 1.1 jonathan error = crypto_getfeat((int *)data);
358 1.1 jonathan break;
359 1.1 jonathan default:
360 1.18 christos DPRINTF(("invalid ioctl cmd %ld\n", cmd));
361 1.1 jonathan error = EINVAL;
362 1.1 jonathan }
363 1.1 jonathan return (error);
364 1.1 jonathan }
365 1.1 jonathan
366 1.1 jonathan static int
367 1.25 christos cryptodev_op(struct csession *cse, struct crypt_op *cop, struct lwp *l)
368 1.1 jonathan {
369 1.1 jonathan struct cryptop *crp = NULL;
370 1.1 jonathan struct cryptodesc *crde = NULL, *crda = NULL;
371 1.29 tls int error, s;
372 1.1 jonathan
373 1.1 jonathan if (cop->len > 256*1024-4)
374 1.1 jonathan return (E2BIG);
375 1.1 jonathan
376 1.13 jonathan if (cse->txform) {
377 1.13 jonathan if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
378 1.13 jonathan return (EINVAL);
379 1.13 jonathan }
380 1.1 jonathan
381 1.1 jonathan bzero(&cse->uio, sizeof(cse->uio));
382 1.1 jonathan cse->uio.uio_iovcnt = 1;
383 1.1 jonathan cse->uio.uio_resid = 0;
384 1.1 jonathan cse->uio.uio_rw = UIO_WRITE;
385 1.1 jonathan cse->uio.uio_iov = cse->iovec;
386 1.17 yamt UIO_SETUP_SYSSPACE(&cse->uio);
387 1.29 tls memset(&cse->iovec, 0, sizeof(cse->iovec));
388 1.1 jonathan cse->uio.uio_iov[0].iov_len = cop->len;
389 1.1 jonathan cse->uio.uio_iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
390 1.29 tls cse->uio.uio_resid = cse->uio.uio_iov[0].iov_len;
391 1.1 jonathan
392 1.1 jonathan crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
393 1.1 jonathan if (crp == NULL) {
394 1.1 jonathan error = ENOMEM;
395 1.1 jonathan goto bail;
396 1.1 jonathan }
397 1.1 jonathan
398 1.1 jonathan if (cse->thash) {
399 1.1 jonathan crda = crp->crp_desc;
400 1.1 jonathan if (cse->txform)
401 1.1 jonathan crde = crda->crd_next;
402 1.1 jonathan } else {
403 1.1 jonathan if (cse->txform)
404 1.1 jonathan crde = crp->crp_desc;
405 1.1 jonathan else {
406 1.1 jonathan error = EINVAL;
407 1.1 jonathan goto bail;
408 1.1 jonathan }
409 1.1 jonathan }
410 1.1 jonathan
411 1.1 jonathan if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
412 1.1 jonathan goto bail;
413 1.1 jonathan
414 1.1 jonathan if (crda) {
415 1.1 jonathan crda->crd_skip = 0;
416 1.1 jonathan crda->crd_len = cop->len;
417 1.1 jonathan crda->crd_inject = 0; /* ??? */
418 1.1 jonathan
419 1.1 jonathan crda->crd_alg = cse->mac;
420 1.1 jonathan crda->crd_key = cse->mackey;
421 1.1 jonathan crda->crd_klen = cse->mackeylen * 8;
422 1.1 jonathan }
423 1.1 jonathan
424 1.1 jonathan if (crde) {
425 1.1 jonathan if (cop->op == COP_ENCRYPT)
426 1.1 jonathan crde->crd_flags |= CRD_F_ENCRYPT;
427 1.1 jonathan else
428 1.1 jonathan crde->crd_flags &= ~CRD_F_ENCRYPT;
429 1.1 jonathan crde->crd_len = cop->len;
430 1.1 jonathan crde->crd_inject = 0;
431 1.1 jonathan
432 1.1 jonathan crde->crd_alg = cse->cipher;
433 1.1 jonathan crde->crd_key = cse->key;
434 1.1 jonathan crde->crd_klen = cse->keylen * 8;
435 1.1 jonathan }
436 1.1 jonathan
437 1.1 jonathan crp->crp_ilen = cop->len;
438 1.1 jonathan crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
439 1.1 jonathan | (cop->flags & COP_F_BATCH);
440 1.26 christos crp->crp_buf = (void *)&cse->uio;
441 1.1 jonathan crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
442 1.1 jonathan crp->crp_sid = cse->sid;
443 1.1 jonathan crp->crp_opaque = (void *)cse;
444 1.1 jonathan
445 1.1 jonathan if (cop->iv) {
446 1.1 jonathan if (crde == NULL) {
447 1.1 jonathan error = EINVAL;
448 1.1 jonathan goto bail;
449 1.1 jonathan }
450 1.1 jonathan if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
451 1.1 jonathan error = EINVAL;
452 1.1 jonathan goto bail;
453 1.1 jonathan }
454 1.1 jonathan if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
455 1.1 jonathan goto bail;
456 1.1 jonathan bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
457 1.1 jonathan crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
458 1.1 jonathan crde->crd_skip = 0;
459 1.1 jonathan } else if (crde) {
460 1.20 christos if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
461 1.20 christos crde->crd_skip = 0;
462 1.20 christos } else {
463 1.20 christos crde->crd_flags |= CRD_F_IV_PRESENT;
464 1.20 christos crde->crd_skip = cse->txform->blocksize;
465 1.20 christos crde->crd_len -= cse->txform->blocksize;
466 1.20 christos }
467 1.1 jonathan }
468 1.1 jonathan
469 1.1 jonathan if (cop->mac) {
470 1.1 jonathan if (crda == NULL) {
471 1.1 jonathan error = EINVAL;
472 1.1 jonathan goto bail;
473 1.1 jonathan }
474 1.1 jonathan crp->crp_mac=cse->tmp_mac;
475 1.1 jonathan }
476 1.1 jonathan
477 1.1 jonathan s = splcrypto(); /* NB: only needed with CRYPTO_F_CBIMM */
478 1.1 jonathan error = crypto_dispatch(crp);
479 1.4 jonathan if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
480 1.1 jonathan error = tsleep(crp, PSOCK, "crydev", 0);
481 1.1 jonathan splx(s);
482 1.1 jonathan if (error) {
483 1.1 jonathan goto bail;
484 1.1 jonathan }
485 1.1 jonathan
486 1.1 jonathan if (crp->crp_etype != 0) {
487 1.1 jonathan error = crp->crp_etype;
488 1.1 jonathan goto bail;
489 1.1 jonathan }
490 1.1 jonathan
491 1.1 jonathan if (cse->error) {
492 1.1 jonathan error = cse->error;
493 1.1 jonathan goto bail;
494 1.1 jonathan }
495 1.1 jonathan
496 1.1 jonathan if (cop->dst &&
497 1.1 jonathan (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
498 1.1 jonathan goto bail;
499 1.1 jonathan
500 1.1 jonathan if (cop->mac &&
501 1.1 jonathan (error = copyout(crp->crp_mac, cop->mac, cse->thash->authsize)))
502 1.1 jonathan goto bail;
503 1.1 jonathan
504 1.1 jonathan bail:
505 1.1 jonathan if (crp)
506 1.1 jonathan crypto_freereq(crp);
507 1.1 jonathan if (cse->uio.uio_iov[0].iov_base)
508 1.1 jonathan free(cse->uio.uio_iov[0].iov_base, M_XDATA);
509 1.1 jonathan
510 1.1 jonathan return (error);
511 1.1 jonathan }
512 1.1 jonathan
513 1.1 jonathan static int
514 1.1 jonathan cryptodev_cb(void *op)
515 1.1 jonathan {
516 1.1 jonathan struct cryptop *crp = (struct cryptop *) op;
517 1.1 jonathan struct csession *cse = (struct csession *)crp->crp_opaque;
518 1.1 jonathan
519 1.1 jonathan cse->error = crp->crp_etype;
520 1.1 jonathan if (crp->crp_etype == EAGAIN)
521 1.1 jonathan return crypto_dispatch(crp);
522 1.1 jonathan wakeup_one(crp);
523 1.1 jonathan return (0);
524 1.1 jonathan }
525 1.1 jonathan
526 1.1 jonathan static int
527 1.1 jonathan cryptodevkey_cb(void *op)
528 1.1 jonathan {
529 1.1 jonathan struct cryptkop *krp = (struct cryptkop *) op;
530 1.1 jonathan
531 1.1 jonathan wakeup_one(krp);
532 1.1 jonathan return (0);
533 1.1 jonathan }
534 1.1 jonathan
535 1.1 jonathan static int
536 1.1 jonathan cryptodev_key(struct crypt_kop *kop)
537 1.1 jonathan {
538 1.1 jonathan struct cryptkop *krp = NULL;
539 1.1 jonathan int error = EINVAL;
540 1.1 jonathan int in, out, size, i;
541 1.1 jonathan
542 1.1 jonathan if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
543 1.1 jonathan return (EFBIG);
544 1.1 jonathan }
545 1.1 jonathan
546 1.1 jonathan in = kop->crk_iparams;
547 1.1 jonathan out = kop->crk_oparams;
548 1.1 jonathan switch (kop->crk_op) {
549 1.1 jonathan case CRK_MOD_EXP:
550 1.1 jonathan if (in == 3 && out == 1)
551 1.1 jonathan break;
552 1.1 jonathan return (EINVAL);
553 1.1 jonathan case CRK_MOD_EXP_CRT:
554 1.1 jonathan if (in == 6 && out == 1)
555 1.1 jonathan break;
556 1.1 jonathan return (EINVAL);
557 1.1 jonathan case CRK_DSA_SIGN:
558 1.1 jonathan if (in == 5 && out == 2)
559 1.1 jonathan break;
560 1.1 jonathan return (EINVAL);
561 1.1 jonathan case CRK_DSA_VERIFY:
562 1.1 jonathan if (in == 7 && out == 0)
563 1.1 jonathan break;
564 1.1 jonathan return (EINVAL);
565 1.1 jonathan case CRK_DH_COMPUTE_KEY:
566 1.1 jonathan if (in == 3 && out == 1)
567 1.1 jonathan break;
568 1.1 jonathan return (EINVAL);
569 1.27 tls case CRK_MOD_ADD:
570 1.27 tls if (in == 3 && out == 1)
571 1.27 tls break;
572 1.27 tls return (EINVAL);
573 1.27 tls case CRK_MOD_ADDINV:
574 1.27 tls if (in == 2 && out == 1)
575 1.27 tls break;
576 1.27 tls return (EINVAL);
577 1.27 tls case CRK_MOD_SUB:
578 1.27 tls if (in == 3 && out == 1)
579 1.27 tls break;
580 1.27 tls return (EINVAL);
581 1.27 tls case CRK_MOD_MULT:
582 1.27 tls if (in == 3 && out == 1)
583 1.27 tls break;
584 1.27 tls return (EINVAL);
585 1.27 tls case CRK_MOD_MULTINV:
586 1.27 tls if (in == 2 && out == 1)
587 1.27 tls break;
588 1.27 tls return (EINVAL);
589 1.27 tls case CRK_MOD:
590 1.27 tls if (in == 2 && out == 1)
591 1.27 tls break;
592 1.27 tls return (EINVAL);
593 1.1 jonathan default:
594 1.1 jonathan return (EINVAL);
595 1.1 jonathan }
596 1.1 jonathan
597 1.1 jonathan krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK);
598 1.1 jonathan if (!krp)
599 1.1 jonathan return (ENOMEM);
600 1.1 jonathan bzero(krp, sizeof *krp);
601 1.1 jonathan krp->krp_op = kop->crk_op;
602 1.1 jonathan krp->krp_status = kop->crk_status;
603 1.1 jonathan krp->krp_iparams = kop->crk_iparams;
604 1.1 jonathan krp->krp_oparams = kop->crk_oparams;
605 1.1 jonathan krp->krp_status = 0;
606 1.1 jonathan krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
607 1.1 jonathan
608 1.1 jonathan for (i = 0; i < CRK_MAXPARAM; i++)
609 1.1 jonathan krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
610 1.1 jonathan for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
611 1.1 jonathan size = (krp->krp_param[i].crp_nbits + 7) / 8;
612 1.1 jonathan if (size == 0)
613 1.1 jonathan continue;
614 1.19 christos krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
615 1.1 jonathan if (i >= krp->krp_iparams)
616 1.1 jonathan continue;
617 1.1 jonathan error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
618 1.1 jonathan if (error)
619 1.1 jonathan goto fail;
620 1.1 jonathan }
621 1.1 jonathan
622 1.1 jonathan error = crypto_kdispatch(krp);
623 1.1 jonathan if (error == 0)
624 1.1 jonathan error = tsleep(krp, PSOCK, "crydev", 0);
625 1.1 jonathan if (error)
626 1.1 jonathan goto fail;
627 1.1 jonathan
628 1.1 jonathan if (krp->krp_status != 0) {
629 1.1 jonathan error = krp->krp_status;
630 1.1 jonathan goto fail;
631 1.1 jonathan }
632 1.1 jonathan
633 1.1 jonathan for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
634 1.1 jonathan size = (krp->krp_param[i].crp_nbits + 7) / 8;
635 1.1 jonathan if (size == 0)
636 1.1 jonathan continue;
637 1.1 jonathan error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
638 1.1 jonathan if (error)
639 1.1 jonathan goto fail;
640 1.1 jonathan }
641 1.1 jonathan
642 1.1 jonathan fail:
643 1.1 jonathan if (krp) {
644 1.1 jonathan kop->crk_status = krp->krp_status;
645 1.1 jonathan for (i = 0; i < CRK_MAXPARAM; i++) {
646 1.1 jonathan if (krp->krp_param[i].crp_p)
647 1.1 jonathan FREE(krp->krp_param[i].crp_p, M_XDATA);
648 1.1 jonathan }
649 1.1 jonathan free(krp, M_XDATA);
650 1.1 jonathan }
651 1.1 jonathan return (error);
652 1.1 jonathan }
653 1.1 jonathan
654 1.1 jonathan /* ARGSUSED */
655 1.1 jonathan static int
656 1.25 christos cryptof_close(struct file *fp, struct lwp *l)
657 1.1 jonathan {
658 1.1 jonathan struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
659 1.1 jonathan struct csession *cse;
660 1.1 jonathan
661 1.1 jonathan while ((cse = TAILQ_FIRST(&fcr->csessions))) {
662 1.1 jonathan TAILQ_REMOVE(&fcr->csessions, cse, next);
663 1.1 jonathan (void)csefree(cse);
664 1.1 jonathan }
665 1.29 tls pool_put(&fcrpl, fcr);
666 1.1 jonathan
667 1.1 jonathan fp->f_data = NULL;
668 1.1 jonathan
669 1.1 jonathan return 0;
670 1.1 jonathan }
671 1.1 jonathan
672 1.1 jonathan static struct csession *
673 1.1 jonathan csefind(struct fcrypt *fcr, u_int ses)
674 1.1 jonathan {
675 1.1 jonathan struct csession *cse;
676 1.1 jonathan
677 1.1 jonathan TAILQ_FOREACH(cse, &fcr->csessions, next)
678 1.1 jonathan if (cse->ses == ses)
679 1.1 jonathan return (cse);
680 1.1 jonathan return (NULL);
681 1.1 jonathan }
682 1.1 jonathan
683 1.1 jonathan static int
684 1.1 jonathan csedelete(struct fcrypt *fcr, struct csession *cse_del)
685 1.1 jonathan {
686 1.1 jonathan struct csession *cse;
687 1.1 jonathan
688 1.1 jonathan TAILQ_FOREACH(cse, &fcr->csessions, next) {
689 1.1 jonathan if (cse == cse_del) {
690 1.1 jonathan TAILQ_REMOVE(&fcr->csessions, cse, next);
691 1.1 jonathan return (1);
692 1.1 jonathan }
693 1.1 jonathan }
694 1.1 jonathan return (0);
695 1.1 jonathan }
696 1.1 jonathan
697 1.1 jonathan static struct csession *
698 1.1 jonathan cseadd(struct fcrypt *fcr, struct csession *cse)
699 1.1 jonathan {
700 1.1 jonathan TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
701 1.1 jonathan cse->ses = fcr->sesn++;
702 1.1 jonathan return (cse);
703 1.1 jonathan }
704 1.1 jonathan
705 1.1 jonathan static struct csession *
706 1.26 christos csecreate(struct fcrypt *fcr, u_int64_t sid, void *key, u_int64_t keylen,
707 1.26 christos void *mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
708 1.1 jonathan struct enc_xform *txform, struct auth_hash *thash)
709 1.1 jonathan {
710 1.1 jonathan struct csession *cse;
711 1.1 jonathan
712 1.28 tls /* Don't let the session ID wrap! */
713 1.28 tls if (fcr->sesn + 1 == 0)
714 1.28 tls return NULL;
715 1.28 tls
716 1.29 tls cse = pool_get(&csepl, PR_NOWAIT);
717 1.1 jonathan if (cse == NULL)
718 1.1 jonathan return NULL;
719 1.1 jonathan cse->key = key;
720 1.1 jonathan cse->keylen = keylen/8;
721 1.1 jonathan cse->mackey = mackey;
722 1.1 jonathan cse->mackeylen = mackeylen/8;
723 1.1 jonathan cse->sid = sid;
724 1.1 jonathan cse->cipher = cipher;
725 1.1 jonathan cse->mac = mac;
726 1.1 jonathan cse->txform = txform;
727 1.1 jonathan cse->thash = thash;
728 1.28 tls if (cseadd(fcr, cse))
729 1.28 tls return (cse);
730 1.28 tls else {
731 1.28 tls FREE(cse, M_XDATA);
732 1.28 tls return NULL;
733 1.28 tls }
734 1.1 jonathan }
735 1.1 jonathan
736 1.1 jonathan static int
737 1.1 jonathan csefree(struct csession *cse)
738 1.1 jonathan {
739 1.1 jonathan int error;
740 1.1 jonathan
741 1.1 jonathan error = crypto_freesession(cse->sid);
742 1.1 jonathan if (cse->key)
743 1.1 jonathan FREE(cse->key, M_XDATA);
744 1.1 jonathan if (cse->mackey)
745 1.1 jonathan FREE(cse->mackey, M_XDATA);
746 1.29 tls pool_put(&csepl, cse);
747 1.1 jonathan return (error);
748 1.1 jonathan }
749 1.1 jonathan
750 1.1 jonathan static int
751 1.25 christos cryptoopen(dev_t dev, int flag, int mode,
752 1.25 christos struct lwp *l)
753 1.1 jonathan {
754 1.29 tls struct file *fp;
755 1.29 tls struct fcrypt *fcr;
756 1.29 tls int fd, error;
757 1.29 tls
758 1.1 jonathan if (crypto_usercrypto == 0)
759 1.1 jonathan return (ENXIO);
760 1.29 tls
761 1.29 tls if ((error = falloc(l, &fp, &fd)) != 0)
762 1.29 tls return error;
763 1.29 tls
764 1.29 tls fcr = pool_get(&fcrpl, PR_WAITOK);
765 1.29 tls TAILQ_INIT(&fcr->csessions);
766 1.29 tls /*
767 1.29 tls * Don't ever return session 0, to allow detection of
768 1.29 tls * failed creation attempts with multi-create ioctl.
769 1.29 tls */
770 1.29 tls fcr->sesn = 1;
771 1.29 tls return fdclone(l, fp, fd, flag, &cryptofops, fcr);
772 1.1 jonathan }
773 1.1 jonathan
774 1.1 jonathan static int
775 1.25 christos cryptoread(dev_t dev, struct uio *uio, int ioflag)
776 1.1 jonathan {
777 1.1 jonathan return (EIO);
778 1.1 jonathan }
779 1.1 jonathan
780 1.1 jonathan static int
781 1.25 christos cryptowrite(dev_t dev, struct uio *uio, int ioflag)
782 1.1 jonathan {
783 1.1 jonathan return (EIO);
784 1.1 jonathan }
785 1.1 jonathan
786 1.1 jonathan int
787 1.25 christos cryptoselect(dev_t dev, int rw, struct lwp *l)
788 1.1 jonathan {
789 1.1 jonathan return (0);
790 1.1 jonathan }
791 1.1 jonathan
792 1.1 jonathan /*static*/
793 1.1 jonathan struct cdevsw crypto_cdevsw = {
794 1.1 jonathan /* open */ cryptoopen,
795 1.29 tls /* close */ noclose,
796 1.1 jonathan /* read */ cryptoread,
797 1.1 jonathan /* write */ cryptowrite,
798 1.29 tls /* ioctl */ noioctl,
799 1.1 jonathan /* ttstop?*/ nostop,
800 1.1 jonathan /* ??*/ notty,
801 1.1 jonathan /* poll */ cryptoselect /*nopoll*/,
802 1.1 jonathan /* mmap */ nommap,
803 1.1 jonathan /* kqfilter */ nokqfilter,
804 1.23 christos /* type */ D_OTHER,
805 1.1 jonathan };
806 1.1 jonathan
807 1.15 thorpej /*
808 1.15 thorpej * Pseudo-device initialization routine for /dev/crypto
809 1.15 thorpej */
810 1.15 thorpej void cryptoattach(int);
811 1.15 thorpej
812 1.15 thorpej void
813 1.25 christos cryptoattach(int num)
814 1.15 thorpej {
815 1.29 tls pool_init(&fcrpl, sizeof(struct fcrypt), 0, 0, 0, "fcrpl",
816 1.29 tls NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
817 1.29 tls pool_init(&csepl, sizeof(struct csession), 0, 0, 0, "csepl",
818 1.29 tls NULL, IPL_NET); /* XXX IPL_NET ("splcrypto") */
819 1.29 tls
820 1.29 tls /*
821 1.29 tls * Preallocate space for 64 users, with 5 sessions each.
822 1.29 tls * (consider that a TLS protocol session requires at least
823 1.29 tls * 3DES, MD5, and SHA1 (both hashes are used in the PRF) for
824 1.29 tls * the negotiation, plus HMAC_SHA1 for the actual SSL records,
825 1.29 tls * consuming one session here for each algorithm.
826 1.29 tls */
827 1.29 tls pool_prime(&fcrpl, 64);
828 1.29 tls pool_prime(&csepl, 64 * 5);
829 1.15 thorpej }
830