summaryrefslogtreecommitdiff
path: root/sys/crypto/cryptodev.c
blob: de4e9a759678ecb9e74b9d75cdb5692bf5778c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*	$OpenBSD: cryptodev.c,v 1.3 2001/05/13 16:52:33 jason Exp $	*/

/*
 * Copyright (c) 2001 Theo de Raadt
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/sysctl.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/errno.h>
#include <sys/md5k.h>
#include <dev/rndvar.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <crypto/sha1.h>
#include <crypto/rmd160.h>
#include <crypto/cast.h>
#include <crypto/skipjack.h>
#include <crypto/blf.h>
#include <crypto/crypto.h>
#include <crypto/xform.h>
#include <crypto/cryptodev.h>

struct csession {
	TAILQ_ENTRY(csession) next;
	u_int64_t	sid;
	u_int32_t	ses;

	u_int32_t	cipher;
	caddr_t		key;
	int		keylen;
	u_char		tmp_iv[16];		/* XXX MAX_IV_SIZE */

	u_int32_t	mac;
	caddr_t		mackey;
	int		mackeylen;
	u_char		tmp_mac[16];		/* XXX MAX_MAC_SIZE */

	struct criov	criov;
	int		error;
};

struct fcrypt {
	TAILQ_HEAD(csessionlist, csession) csessions;
	int		sesn;
};

void	cryptoattach __P((int));

int     cryptoopen __P((dev_t, int, int, struct proc *));
int     cryptoclose __P((dev_t, int, int, struct proc *));
int     cryptoread __P((dev_t, struct uio *, int));
int     cryptowrite __P((dev_t, struct uio *, int));
int     cryptoioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
int     cryptoselect __P((dev_t, int, struct proc *));

int	cryptof_read(struct file *, off_t *, struct uio *, struct ucred *);
int	cryptof_write(struct file *, off_t *, struct uio *, struct ucred *);
int	cryptof_ioctl(struct file *, u_long, caddr_t, struct proc *p);
int	cryptof_select(struct file *, int, struct proc *);
int	cryptof_kqfilter(struct file *, struct knote *);
int	cryptof_close(struct file *, struct proc *);

static struct fileops cryptofops = {
    cryptof_read, cryptof_write, cryptof_ioctl,
    cryptof_select, cryptof_kqfilter, cryptof_close };

struct	csession *csefind(struct fcrypt *, u_int);
int	csedelete(struct fcrypt *, struct csession *);
struct	csession *cseadd(struct fcrypt *, struct csession *);
struct	csession *csecreate(struct fcrypt *, u_int64_t, caddr_t, caddr_t);
void	csefree(struct csession *);

int	crypto_op(struct csession *, struct crypt_op *);

/* ARGSUSED */
int
cryptof_read(fp, poff, uio, cred)
	struct file *fp;
	off_t *poff;
	struct uio *uio;
	struct ucred *cred;
{
	return (EIO);
}

/* ARGSUSED */
int
cryptof_write(fp, poff, uio, cred)
	struct file *fp;
	off_t *poff;
	struct uio *uio;
	struct ucred *cred;
{
	return (EIO);
}

/* ARGSUSED */
int
cryptof_ioctl(fp, cmd, data, p)
	struct file *fp;
	u_long cmd;
	caddr_t data;
	struct proc *p;
{
	struct cryptoini cria, crie;
	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
	struct csession *cse;
	struct session_op *sop;
	struct crypt_op *cop;
	struct enc_xform *txform = NULL;
	struct auth_hash *thash = NULL;
	u_int64_t sid;
	u_int32_t ses;
	int error = 0;

	switch (cmd) {
	case CIOCGSESSION:
		sop = (struct session_op *)data;
		switch (sop->cipher) {
		case 0:
			break;
		case CRYPTO_DES_CBC:
			txform = &enc_xform_des;
			break;
		case CRYPTO_3DES_CBC:
			txform = &enc_xform_3des;
			break;
		case CRYPTO_BLF_CBC:
			txform = &enc_xform_blf;
			break;
		case CRYPTO_CAST_CBC:
			txform = &enc_xform_cast5;
			break;
		case CRYPTO_SKIPJACK_CBC:
			txform = &enc_xform_skipjack;
			break;
		case CRYPTO_AES_CBC:
			txform = &enc_xform_rijndael128;
			break;
		default:
			return (EINVAL);
		}

		switch (sop->mac) {
		case 0:
			break;
		case CRYPTO_MD5_HMAC:
			thash = &auth_hash_hmac_md5_96;
			break;
		case CRYPTO_SHA1_HMAC:
			thash = &auth_hash_hmac_sha1_96;
			break;
		case CRYPTO_RIPEMD160_HMAC:
			thash = &auth_hash_hmac_ripemd_160_96;
			break;
		default:
			return (EINVAL);
		}

		bzero(&crie, sizeof(crie));
		bzero(&cria, sizeof(cria));

		if (txform) {
			crie.cri_alg = txform->type;
			crie.cri_klen = sop->keylen * 8;	/* XXX range check */

			MALLOC(crie.cri_key, u_int8_t *,
			    crie.cri_klen / 8, M_XDATA, M_WAITOK);
			if ((error = copyin(sop->key, crie.cri_key,
			    crie.cri_klen / 8)))
				goto bail;
			if (thash)
				crie.cri_next = &cria;
		}

		if (thash) {
			cria.cri_alg = thash->type;
			cria.cri_klen = sop->mackeylen * 8;	/* XXX range check */

			MALLOC(cria.cri_key, u_int8_t *,
			    cria.cri_klen / 8, M_XDATA, M_WAITOK);
			if ((error = copyin(sop->mackey, cria.cri_key,
			    cria.cri_klen / 8)))
				goto bail;
		}

		error = crypto_newsession(&sid, (txform ? &crie : &cria), 1);

bail:
		if (error) {
			if (crie.cri_key)
				FREE(crie.cri_key, M_XDATA);
			if (cria.cri_key)
				FREE(cria.cri_key, M_XDATA);
			return (error);
		}

		cse = csecreate(fcr, sid, crie.cri_key, cria.cri_key);
		sop->ses = cse->ses;
		break;
	case CIOCFSESSION:
		ses = *(u_int32_t *)data;
		cse = csefind(fcr, ses);
		if (cse == NULL)
			return (EINVAL);
		error = crypto_freesession(cse->sid);
		csedelete(fcr, cse);
		csefree(cse);
		break;
	case CIOCCRYPT:
		cse = csefind(fcr, cop->ses);
		if (cse == NULL)
			return (EINVAL);
		error = crypto_op(cse, cop);
		break;
	default:
		error = EINVAL;
	}
	return (error);
}

int	cryptodev_cb(void *);


int
crypto_op(struct csession *cse, struct crypt_op *cop)
{
	struct cryptop *crp = NULL;
	struct cryptodesc *crde = NULL, *crda = NULL;
	int error;

	if (cop->len > 64*1024)
		return (E2BIG);

	bzero(&cse->criov, sizeof(cse->criov));
	cse->criov.niov = 1;
	cse->criov.iov[0].iov_len = cop->len;
	cse->criov.iov[0].iov_base = malloc(cop->len, M_XDATA, M_WAITOK);
	if (cse->criov.iov[0].iov_base == NULL)
		return (ENOMEM);

	crp = crypto_getreq((cse->cipher > 0) + (cse->mac > 0));
	if (crp == NULL)
		goto bail;

	if (cse->mac) {
		crda = crp->crp_desc;
		if (cse->cipher)
			crde = crda->crd_next;
	} else {
		if (cse->cipher)
			crde = crp->crp_desc;
		else {
			error = EINVAL;
			goto bail;
		}
	}

	if ((error = copyin(cop->src, cse->criov.iov[0].iov_base, cop->len)))
		goto bail;

	if (crda) {
		crda->crd_skip = 0;
		crda->crd_len = cop->len;
		crda->crd_inject = 0;	/* ??? */

		crda->crd_alg = cse->mac;
		crda->crd_key = cse->mackey;
		crda->crd_klen = cse->mackeylen * 8;
	}

	if (crde) {
		crde->crd_skip = 0;
		crde->crd_len = cop->len;
		crde->crd_inject = 0;	/* ??? */

		crde->crd_alg = cse->cipher;
		crde->crd_key = cse->key;
		crde->crd_klen = cse->keylen * 8;
	}

	crp->crp_ilen = cop->len;
	crp->crp_flags = CRYPTO_F_IOV;
	crp->crp_buf = (caddr_t)&cse->criov;
	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
	crp->crp_sid = cse->sid;
	crp->crp_opaque = (void *)cse;

	if (cop->iv) {
		if (crde == NULL) {
			error = EINVAL;
			goto bail;
		}
		if ((error = copyin(cop->iv, cse->tmp_iv, 8)))	/* XXX sop->iv_size? */
			goto bail;
	}

	if (cop->mac) {
		if (crda == NULL) {
			error = EINVAL;
			goto bail;
		}
		if ((error = copyin(cop->mac, cse->tmp_mac, 16)))	/* XXX */
			goto bail;
		crp->crp_mac_trunc_len = 16;	/* XXX */
	}

	crypto_dispatch(crp);
	error = tsleep(cse, PSOCK, "crydev", 0);
	if (error) {
		/// XXX can this happen?  if so, how do we recover?
		goto bail;
	}

	if (cse->error)
		goto bail;

	if ((error = copyout(cse->criov.iov[0].iov_base, cop->dst, cop->len)))
		goto bail;

	if (cop->mac &&
	    (error = copyout(crp->crp_mac, cop->mac, crp->crp_mac_trunc_len)))
		goto bail;

bail:
	if (crp)
		crypto_freereq(crp);
	if (cse->criov.iov[0].iov_base)
		free(cse->criov.iov[0].iov_base, M_XDATA);

	return (error);
}

int
cryptodev_cb(void *op)
{
	struct cryptop *crp = (struct cryptop *) op;
	struct csession *cse = (struct csession *)crp->crp_opaque;

	cse->error = crp->crp_etype;
	if (crp->crp_etype == EAGAIN)
		return crypto_dispatch(crp);
	wakeup(cse);
	return (0);
}



/* ARGSUSED */
int
cryptof_select(fp, which, p)
	struct file *fp;
	int which;
	struct proc *p;
{
	return (0);
}

/* ARGSUSED */
int
cryptof_kqfilter(fp, kn)
	struct file *fp;
	struct knote *kn;
{
	return (0);
}

/* ARGSUSED */
int
cryptof_close(fp, p)
	struct file *fp;
	struct proc *p;
{
	struct fcrypt *fcr = (struct fcrypt *)fp->f_data;
	struct csession *cse;

	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
		TAILQ_REMOVE(&fcr->csessions, cse, next);
		csefree(cse);
	}
	FREE(fcr, M_XDATA);
	fp->f_data = NULL;
	return 0;
}

void
cryptoattach(int n)
{
}

int
cryptoopen(dev, flag, mode, p)
	dev_t   dev;
	int     flag;
	int     mode;
	struct proc *p;
{
	return (0);
}

int
cryptoclose(dev, flag, mode, p)
	dev_t   dev;
	int     flag;
	int     mode;
	struct proc *p;
{
	return (0);
}

int
cryptoread(dev, uio, ioflag)
	dev_t   dev;
	struct uio *uio;
	int     ioflag;
{
	return (EIO);
}

int
cryptowrite(dev, uio, ioflag)
	dev_t   dev;
	struct uio *uio;
	int     ioflag;
{
	return (EIO);
}

int
cryptoioctl(dev, cmd, data, flag, p)
	dev_t   dev;
	u_long  cmd;
	caddr_t data;
	int     flag;
	struct proc *p;
{
	struct file *f;
	struct fcrypt *fcr;
	int fd, error;

	switch (cmd) {
	case CRIOGET:
		MALLOC(fcr, struct fcrypt *,
		    sizeof(struct fcrypt), M_XDATA, M_WAITOK);
		TAILQ_INIT(&fcr->csessions);

		error = falloc(p, &f, &fd);

		if (error) {
			FREE(fcr, M_XDATA);
			return (error);
		}
		f->f_flag = FREAD | FWRITE;
		f->f_type = DTYPE_PIPE;
		f->f_ops = &cryptofops;
		f->f_data = (caddr_t)fcr;
		*(u_int32_t *)data = fd;
		break;
	default:
		error = EINVAL;
		break;
	}
	return (error);
}

int
cryptoselect(dev, rw, p)
	dev_t   dev;
	int     rw;
	struct proc *p;
{
	return (0);
}

struct csession *
csefind(struct fcrypt *fcr, u_int ses)
{
	struct csession *cse;

	TAILQ_FOREACH(cse, &fcr->csessions, next)
		if (cse->ses == ses)
			return (cse);
	return (NULL);
}

int
csedelete(struct fcrypt *fcr, struct csession *cse_del)
{
	struct csession *cse;

	TAILQ_FOREACH(cse, &fcr->csessions, next) {
		if (cse == cse_del) {
			TAILQ_REMOVE(&fcr->csessions, cse, next);
			return (1);
		}
	}
	return (0);
}
	
struct csession *
cseadd(struct fcrypt *fcr, struct csession *cse)
{
	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
	cse->ses = fcr->sesn++;
	return (cse);
}

struct csession *
csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, caddr_t mackey)
{
	struct csession *cse;

	MALLOC(cse, struct csession *, sizeof(struct csession),
	    M_XDATA, M_NOWAIT);
	cse->key = key;
	cse->mackey = mackey;
	cse->sid = sid;
	cseadd(fcr, cse);
	return (cse);
}

void
csefree(struct csession *cse)
{
	if (cse->key)
		FREE(cse->key, M_XDATA);
	if (cse->mackey)
		FREE(cse->mackey, M_XDATA);
	(void) crypto_freesession(cse->sid);
	FREE(cse, M_XDATA);
}