1 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
4 * Linux port done by David McCullough <david_mccullough@securecomputing.com>
5 * Copyright (C) 2006-2007 David McCullough
6 * Copyright (C) 2004-2005 Intel Corporation.
7 * The license and original author are listed below.
9 * Copyright (c) 2001 Theo de Raadt
10 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Effort sponsored in part by the Defense Advanced Research Projects
36 * Agency (DARPA) and Air Force Research Laboratory, Air Force
37 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
39 __FBSDID("$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.34 2007/05/09 19:37:02 gnn Exp $");
42 #ifndef AUTOCONF_INCLUDED
43 #include <linux/config.h>
45 #include <linux/types.h>
46 #include <linux/time.h>
47 #include <linux/delay.h>
48 #include <linux/list.h>
49 #include <linux/init.h>
50 #include <linux/sched.h>
51 #include <linux/unistd.h>
52 #include <linux/module.h>
53 #include <linux/wait.h>
54 #include <linux/slab.h>
56 #include <linux/dcache.h>
57 #include <linux/file.h>
58 #include <linux/mount.h>
59 #include <linux/miscdevice.h>
60 #include <linux/version.h>
61 #include <asm/uaccess.h>
63 #include <cryptodev.h>
66 extern asmlinkage
long sys_dup(unsigned int fildes
);
68 #define debug cryptodev_debug
69 int cryptodev_debug
= 0;
70 module_param(cryptodev_debug
, int, 0644);
71 MODULE_PARM_DESC(cryptodev_debug
, "Enable cryptodev debug");
73 struct csession_info
{
75 u_int16_t minkey
, maxkey
;
78 /* u_int16_t hashsize; */
80 /* u_int16_t ctxsize; */
84 struct list_head list
;
88 wait_queue_head_t waitq
;
96 u_char tmp_iv
[EALG_MAX_BLOCK_LEN
];
101 struct csession_info info
;
109 struct list_head csessions
;
113 static struct csession
*csefind(struct fcrypt
*, u_int
);
114 static int csedelete(struct fcrypt
*, struct csession
*);
115 static struct csession
*cseadd(struct fcrypt
*, struct csession
*);
116 static struct csession
*csecreate(struct fcrypt
*, u_int64_t
,
117 struct cryptoini
*crie
, struct cryptoini
*cria
, struct csession_info
*);
118 static int csefree(struct csession
*);
120 static int cryptodev_op(struct csession
*, struct crypt_op
*);
121 static int cryptodev_key(struct crypt_kop
*);
122 static int cryptodev_find(struct crypt_find_op
*);
124 static int cryptodev_cb(void *);
125 static int cryptodev_open(struct inode
*inode
, struct file
*filp
);
128 * Check a crypto identifier to see if it requested
129 * a valid crid and it's capabilities match.
134 int hid
= crid
& ~(CRYPTOCAP_F_SOFTWARE
| CRYPTOCAP_F_HARDWARE
);
135 int typ
= crid
& (CRYPTOCAP_F_SOFTWARE
| CRYPTOCAP_F_HARDWARE
);
138 /* if the user hasn't selected a driver, then just call newsession */
139 if (hid
== 0 && typ
!= 0)
142 caps
= crypto_getcaps(hid
);
144 /* didn't find anything with capabilities */
146 dprintk("%s: hid=%x typ=%x not matched\n", __FUNCTION__
, hid
, typ
);
150 /* the user didn't specify SW or HW, so the driver is ok */
154 /* if the type specified didn't match */
155 if (typ
!= (caps
& (CRYPTOCAP_F_SOFTWARE
| CRYPTOCAP_F_HARDWARE
))) {
156 dprintk("%s: hid=%x typ=%x caps=%x not matched\n", __FUNCTION__
,
165 cryptodev_op(struct csession
*cse
, struct crypt_op
*cop
)
167 struct cryptop
*crp
= NULL
;
168 struct cryptodesc
*crde
= NULL
, *crda
= NULL
;
171 dprintk("%s()\n", __FUNCTION__
);
172 if (cop
->len
> CRYPTO_MAX_DATA_LEN
) {
173 dprintk("%s: %d > %d\n", __FUNCTION__
, cop
->len
, CRYPTO_MAX_DATA_LEN
);
177 if (cse
->info
.blocksize
&& (cop
->len
% cse
->info
.blocksize
) != 0) {
178 dprintk("%s: blocksize=%d len=%d\n", __FUNCTION__
, cse
->info
.blocksize
,
183 cse
->uio
.uio_iov
= &cse
->iovec
;
184 cse
->uio
.uio_iovcnt
= 1;
185 cse
->uio
.uio_offset
= 0;
187 cse
->uio
.uio_resid
= cop
->len
;
188 cse
->uio
.uio_segflg
= UIO_SYSSPACE
;
189 cse
->uio
.uio_rw
= UIO_WRITE
;
190 cse
->uio
.uio_td
= td
;
192 cse
->uio
.uio_iov
[0].iov_len
= cop
->len
;
193 if (cse
->info
.authsize
)
194 cse
->uio
.uio_iov
[0].iov_len
+= cse
->info
.authsize
;
195 cse
->uio
.uio_iov
[0].iov_base
= kmalloc(cse
->uio
.uio_iov
[0].iov_len
,
198 if (cse
->uio
.uio_iov
[0].iov_base
== NULL
) {
199 dprintk("%s: iov_base kmalloc(%d) failed\n", __FUNCTION__
,
200 cse
->uio
.uio_iov
[0].iov_len
);
204 crp
= crypto_getreq((cse
->info
.blocksize
!= 0) + (cse
->info
.authsize
!= 0));
206 dprintk("%s: ENOMEM\n", __FUNCTION__
);
211 if (cse
->info
.authsize
) {
212 crda
= crp
->crp_desc
;
213 if (cse
->info
.blocksize
)
214 crde
= crda
->crd_next
;
216 if (cse
->info
.blocksize
)
217 crde
= crp
->crp_desc
;
219 dprintk("%s: bad request\n", __FUNCTION__
);
225 if ((error
= copy_from_user(cse
->uio
.uio_iov
[0].iov_base
, cop
->src
,
227 dprintk("%s: bad copy\n", __FUNCTION__
);
233 crda
->crd_len
= cop
->len
;
234 crda
->crd_inject
= cop
->len
;
236 crda
->crd_alg
= cse
->mac
;
237 crda
->crd_key
= cse
->mackey
;
238 crda
->crd_klen
= cse
->mackeylen
* 8;
242 if (cop
->op
== COP_ENCRYPT
)
243 crde
->crd_flags
|= CRD_F_ENCRYPT
;
245 crde
->crd_flags
&= ~CRD_F_ENCRYPT
;
246 crde
->crd_len
= cop
->len
;
247 crde
->crd_inject
= 0;
249 crde
->crd_alg
= cse
->cipher
;
250 crde
->crd_key
= cse
->key
;
251 crde
->crd_klen
= cse
->keylen
* 8;
254 crp
->crp_ilen
= cse
->uio
.uio_iov
[0].iov_len
;
255 crp
->crp_flags
= CRYPTO_F_IOV
| CRYPTO_F_CBIMM
256 | (cop
->flags
& COP_F_BATCH
);
257 crp
->crp_buf
= (caddr_t
)&cse
->uio
;
258 crp
->crp_callback
= (int (*) (struct cryptop
*)) cryptodev_cb
;
259 crp
->crp_sid
= cse
->sid
;
260 crp
->crp_opaque
= (void *)cse
;
265 dprintk("%s no crde\n", __FUNCTION__
);
268 if (cse
->cipher
== CRYPTO_ARC4
) { /* XXX use flag? */
270 dprintk("%s arc4 with IV\n", __FUNCTION__
);
273 if ((error
= copy_from_user(cse
->tmp_iv
, cop
->iv
,
274 cse
->info
.blocksize
))) {
275 dprintk("%s bad iv copy\n", __FUNCTION__
);
278 memcpy(crde
->crd_iv
, cse
->tmp_iv
, cse
->info
.blocksize
);
279 crde
->crd_flags
|= CRD_F_IV_EXPLICIT
| CRD_F_IV_PRESENT
;
281 } else if (cse
->cipher
== CRYPTO_ARC4
) { /* XXX use flag? */
284 crde
->crd_flags
|= CRD_F_IV_PRESENT
;
285 crde
->crd_skip
= cse
->info
.blocksize
;
286 crde
->crd_len
-= cse
->info
.blocksize
;
289 if (cop
->mac
&& crda
== NULL
) {
291 dprintk("%s no crda\n", __FUNCTION__
);
296 * Let the dispatch run unlocked, then, interlock against the
297 * callback before checking if the operation completed and going
298 * to sleep. This insures drivers don't inherit our lock which
299 * results in a lock order reversal between crypto_dispatch forced
300 * entry and the crypto_done callback into us.
302 error
= crypto_dispatch(crp
);
304 dprintk("%s about to WAIT\n", __FUNCTION__
);
306 * we really need to wait for driver to complete to maintain
307 * state, luckily interrupts will be remembered
310 error
= wait_event_interruptible(crp
->crp_waitq
,
311 ((crp
->crp_flags
& CRYPTO_F_DONE
) != 0));
313 * we can't break out of this loop or we will leave behind
314 * a huge mess, however, staying here means if your driver
315 * is broken user applications can hang and not be killed.
316 * The solution, fix your driver :-)
322 } while ((crp
->crp_flags
& CRYPTO_F_DONE
) == 0);
323 dprintk("%s finished WAITING error=%d\n", __FUNCTION__
, error
);
326 if (crp
->crp_etype
!= 0) {
327 error
= crp
->crp_etype
;
328 dprintk("%s error in crp processing\n", __FUNCTION__
);
334 dprintk("%s error in cse processing\n", __FUNCTION__
);
338 if (cop
->dst
&& (error
= copy_to_user(cop
->dst
,
339 cse
->uio
.uio_iov
[0].iov_base
, cop
->len
))) {
340 dprintk("%s bad dst copy\n", __FUNCTION__
);
345 (error
=copy_to_user(cop
->mac
,
346 (caddr_t
)cse
->uio
.uio_iov
[0].iov_base
+ cop
->len
,
347 cse
->info
.authsize
))) {
348 dprintk("%s bad mac copy\n", __FUNCTION__
);
355 if (cse
->uio
.uio_iov
[0].iov_base
)
356 kfree(cse
->uio
.uio_iov
[0].iov_base
);
362 cryptodev_cb(void *op
)
364 struct cryptop
*crp
= (struct cryptop
*) op
;
365 struct csession
*cse
= (struct csession
*)crp
->crp_opaque
;
368 dprintk("%s()\n", __FUNCTION__
);
369 error
= crp
->crp_etype
;
370 if (error
== EAGAIN
) {
371 crp
->crp_flags
&= ~CRYPTO_F_DONE
;
374 * DAVIDM I am fairly sure that we should turn this into a batch
375 * request to stop bad karma/lockup, revisit
377 crp
->crp_flags
|= CRYPTO_F_BATCH
;
379 return crypto_dispatch(crp
);
381 if (error
!= 0 || (crp
->crp_flags
& CRYPTO_F_DONE
)) {
383 wake_up_interruptible(&crp
->crp_waitq
);
389 cryptodevkey_cb(void *op
)
391 struct cryptkop
*krp
= (struct cryptkop
*) op
;
392 dprintk("%s()\n", __FUNCTION__
);
393 wake_up_interruptible(&krp
->krp_waitq
);
398 cryptodev_key(struct crypt_kop
*kop
)
400 struct cryptkop
*krp
= NULL
;
402 int in
, out
, size
, i
;
404 dprintk("%s()\n", __FUNCTION__
);
405 if (kop
->crk_iparams
+ kop
->crk_oparams
> CRK_MAXPARAM
) {
406 dprintk("%s params too big\n", __FUNCTION__
);
410 in
= kop
->crk_iparams
;
411 out
= kop
->crk_oparams
;
412 switch (kop
->crk_op
) {
414 if (in
== 3 && out
== 1)
417 case CRK_MOD_EXP_CRT
:
418 if (in
== 6 && out
== 1)
422 if (in
== 5 && out
== 2)
426 if (in
== 7 && out
== 0)
429 case CRK_DH_COMPUTE_KEY
:
430 if (in
== 3 && out
== 1)
437 krp
= (struct cryptkop
*)kmalloc(sizeof *krp
, GFP_KERNEL
);
440 bzero(krp
, sizeof *krp
);
441 krp
->krp_op
= kop
->crk_op
;
442 krp
->krp_status
= kop
->crk_status
;
443 krp
->krp_iparams
= kop
->crk_iparams
;
444 krp
->krp_oparams
= kop
->crk_oparams
;
445 krp
->krp_crid
= kop
->crk_crid
;
447 krp
->krp_flags
= CRYPTO_KF_CBIMM
;
448 krp
->krp_callback
= (int (*) (struct cryptkop
*)) cryptodevkey_cb
;
449 init_waitqueue_head(&krp
->krp_waitq
);
451 for (i
= 0; i
< CRK_MAXPARAM
; i
++)
452 krp
->krp_param
[i
].crp_nbits
= kop
->crk_param
[i
].crp_nbits
;
453 for (i
= 0; i
< krp
->krp_iparams
+ krp
->krp_oparams
; i
++) {
454 size
= (krp
->krp_param
[i
].crp_nbits
+ 7) / 8;
457 krp
->krp_param
[i
].crp_p
= (caddr_t
) kmalloc(size
, GFP_KERNEL
);
458 if (i
>= krp
->krp_iparams
)
460 error
= copy_from_user(krp
->krp_param
[i
].crp_p
,
461 kop
->crk_param
[i
].crp_p
, size
);
466 error
= crypto_kdispatch(krp
);
471 error
= wait_event_interruptible(krp
->krp_waitq
,
472 ((krp
->krp_flags
& CRYPTO_KF_DONE
) != 0));
474 * we can't break out of this loop or we will leave behind
475 * a huge mess, however, staying here means if your driver
476 * is broken user applications can hang and not be killed.
477 * The solution, fix your driver :-)
483 } while ((krp
->krp_flags
& CRYPTO_KF_DONE
) == 0);
485 dprintk("%s finished WAITING error=%d\n", __FUNCTION__
, error
);
487 kop
->crk_crid
= krp
->krp_crid
; /* device that did the work */
488 if (krp
->krp_status
!= 0) {
489 error
= krp
->krp_status
;
493 for (i
= krp
->krp_iparams
; i
< krp
->krp_iparams
+ krp
->krp_oparams
; i
++) {
494 size
= (krp
->krp_param
[i
].crp_nbits
+ 7) / 8;
497 error
= copy_to_user(kop
->crk_param
[i
].crp_p
, krp
->krp_param
[i
].crp_p
,
505 kop
->crk_status
= krp
->krp_status
;
506 for (i
= 0; i
< CRK_MAXPARAM
; i
++) {
507 if (krp
->krp_param
[i
].crp_p
)
508 kfree(krp
->krp_param
[i
].crp_p
);
516 cryptodev_find(struct crypt_find_op
*find
)
520 if (find
->crid
!= -1) {
521 dev
= crypto_find_device_byhid(find
->crid
);
524 strlcpy(find
->name
, device_get_nameunit(dev
),
527 find
->crid
= crypto_find_driver(find
->name
);
528 if (find
->crid
== -1)
534 static struct csession
*
535 csefind(struct fcrypt
*fcr
, u_int ses
)
537 struct csession
*cse
;
539 dprintk("%s()\n", __FUNCTION__
);
540 list_for_each_entry(cse
, &fcr
->csessions
, list
)
547 csedelete(struct fcrypt
*fcr
, struct csession
*cse_del
)
549 struct csession
*cse
;
551 dprintk("%s()\n", __FUNCTION__
);
552 list_for_each_entry(cse
, &fcr
->csessions
, list
) {
553 if (cse
== cse_del
) {
554 list_del(&cse
->list
);
561 static struct csession
*
562 cseadd(struct fcrypt
*fcr
, struct csession
*cse
)
564 dprintk("%s()\n", __FUNCTION__
);
565 list_add_tail(&cse
->list
, &fcr
->csessions
);
566 cse
->ses
= fcr
->sesn
++;
570 static struct csession
*
571 csecreate(struct fcrypt
*fcr
, u_int64_t sid
, struct cryptoini
*crie
,
572 struct cryptoini
*cria
, struct csession_info
*info
)
574 struct csession
*cse
;
576 dprintk("%s()\n", __FUNCTION__
);
577 cse
= (struct csession
*) kmalloc(sizeof(struct csession
), GFP_KERNEL
);
580 memset(cse
, 0, sizeof(struct csession
));
582 INIT_LIST_HEAD(&cse
->list
);
583 init_waitqueue_head(&cse
->waitq
);
585 cse
->key
= crie
->cri_key
;
586 cse
->keylen
= crie
->cri_klen
/8;
587 cse
->mackey
= cria
->cri_key
;
588 cse
->mackeylen
= cria
->cri_klen
/8;
590 cse
->cipher
= crie
->cri_alg
;
591 cse
->mac
= cria
->cri_alg
;
598 csefree(struct csession
*cse
)
602 dprintk("%s()\n", __FUNCTION__
);
603 error
= crypto_freesession(cse
->sid
);
619 struct cryptoini cria
, crie
;
620 struct fcrypt
*fcr
= filp
->private_data
;
621 struct csession
*cse
;
622 struct csession_info info
;
623 struct session2_op sop
;
625 struct crypt_kop kop
;
626 struct crypt_find_op fop
;
629 int feat
, fd
, error
= 0, crid
;
632 dprintk("%s(cmd=%x arg=%lx)\n", __FUNCTION__
, cmd
, arg
);
637 dprintk("%s(CRIOGET)\n", __FUNCTION__
);
640 for (fd
= 0; fd
< files_fdtable(current
->files
)->max_fds
; fd
++)
641 if (files_fdtable(current
->files
)->fd
[fd
] == filp
)
645 put_user(fd
, (int *) arg
);
646 return IS_ERR_VALUE(fd
) ? fd
: 0;
649 #define CIOCGSESSSTR (cmd == CIOCGSESSION ? "CIOCGSESSION" : "CIOCGSESSION2")
652 dprintk("%s(%s)\n", __FUNCTION__
, CIOCGSESSSTR
);
653 memset(&crie
, 0, sizeof(crie
));
654 memset(&cria
, 0, sizeof(cria
));
655 memset(&info
, 0, sizeof(info
));
656 memset(&sop
, 0, sizeof(sop
));
658 if (copy_from_user(&sop
, (void*)arg
, (cmd
== CIOCGSESSION
) ?
659 sizeof(struct session_op
) : sizeof(sop
))) {
660 dprintk("%s(%s) - bad copy\n", __FUNCTION__
, CIOCGSESSSTR
);
665 switch (sop
.cipher
) {
667 dprintk("%s(%s) - no cipher\n", __FUNCTION__
, CIOCGSESSSTR
);
669 case CRYPTO_NULL_CBC
:
670 info
.blocksize
= NULL_BLOCK_LEN
;
671 info
.minkey
= NULL_MIN_KEY_LEN
;
672 info
.maxkey
= NULL_MAX_KEY_LEN
;
675 info
.blocksize
= DES_BLOCK_LEN
;
676 info
.minkey
= DES_MIN_KEY_LEN
;
677 info
.maxkey
= DES_MAX_KEY_LEN
;
679 case CRYPTO_3DES_CBC
:
680 info
.blocksize
= DES3_BLOCK_LEN
;
681 info
.minkey
= DES3_MIN_KEY_LEN
;
682 info
.maxkey
= DES3_MAX_KEY_LEN
;
685 info
.blocksize
= BLOWFISH_BLOCK_LEN
;
686 info
.minkey
= BLOWFISH_MIN_KEY_LEN
;
687 info
.maxkey
= BLOWFISH_MAX_KEY_LEN
;
689 case CRYPTO_CAST_CBC
:
690 info
.blocksize
= CAST128_BLOCK_LEN
;
691 info
.minkey
= CAST128_MIN_KEY_LEN
;
692 info
.maxkey
= CAST128_MAX_KEY_LEN
;
694 case CRYPTO_SKIPJACK_CBC
:
695 info
.blocksize
= SKIPJACK_BLOCK_LEN
;
696 info
.minkey
= SKIPJACK_MIN_KEY_LEN
;
697 info
.maxkey
= SKIPJACK_MAX_KEY_LEN
;
700 info
.blocksize
= AES_BLOCK_LEN
;
701 info
.minkey
= AES_MIN_KEY_LEN
;
702 info
.maxkey
= AES_MAX_KEY_LEN
;
705 info
.blocksize
= ARC4_BLOCK_LEN
;
706 info
.minkey
= ARC4_MIN_KEY_LEN
;
707 info
.maxkey
= ARC4_MAX_KEY_LEN
;
709 case CRYPTO_CAMELLIA_CBC
:
710 info
.blocksize
= CAMELLIA_BLOCK_LEN
;
711 info
.minkey
= CAMELLIA_MIN_KEY_LEN
;
712 info
.maxkey
= CAMELLIA_MAX_KEY_LEN
;
715 dprintk("%s(%s) - bad cipher\n", __FUNCTION__
, CIOCGSESSSTR
);
722 dprintk("%s(%s) - no mac\n", __FUNCTION__
, CIOCGSESSSTR
);
724 case CRYPTO_NULL_HMAC
:
725 info
.authsize
= NULL_HASH_LEN
;
728 info
.authsize
= MD5_HASH_LEN
;
731 info
.authsize
= SHA1_HASH_LEN
;
733 case CRYPTO_SHA2_256
:
734 info
.authsize
= SHA2_256_HASH_LEN
;
736 case CRYPTO_SHA2_384
:
737 info
.authsize
= SHA2_384_HASH_LEN
;
739 case CRYPTO_SHA2_512
:
740 info
.authsize
= SHA2_512_HASH_LEN
;
742 case CRYPTO_RIPEMD160
:
743 info
.authsize
= RIPEMD160_HASH_LEN
;
745 case CRYPTO_MD5_HMAC
:
746 info
.authsize
= MD5_HASH_LEN
;
748 case CRYPTO_SHA1_HMAC
:
749 info
.authsize
= SHA1_HASH_LEN
;
751 case CRYPTO_SHA2_256_HMAC
:
752 info
.authsize
= SHA2_256_HASH_LEN
;
754 case CRYPTO_SHA2_384_HMAC
:
755 info
.authsize
= SHA2_384_HASH_LEN
;
757 case CRYPTO_SHA2_512_HMAC
:
758 info
.authsize
= SHA2_512_HASH_LEN
;
760 case CRYPTO_RIPEMD160_HMAC
:
761 info
.authsize
= RIPEMD160_HASH_LEN
;
764 dprintk("%s(%s) - bad mac\n", __FUNCTION__
, CIOCGSESSSTR
);
769 if (info
.blocksize
) {
770 crie
.cri_alg
= sop
.cipher
;
771 crie
.cri_klen
= sop
.keylen
* 8;
772 if ((info
.maxkey
&& sop
.keylen
> info
.maxkey
) ||
773 sop
.keylen
< info
.minkey
) {
774 dprintk("%s(%s) - bad key\n", __FUNCTION__
, CIOCGSESSSTR
);
779 crie
.cri_key
= (u_int8_t
*) kmalloc(crie
.cri_klen
/8+1, GFP_KERNEL
);
780 if (copy_from_user(crie
.cri_key
, sop
.key
,
782 dprintk("%s(%s) - bad copy\n", __FUNCTION__
, CIOCGSESSSTR
);
787 crie
.cri_next
= &cria
;
791 cria
.cri_alg
= sop
.mac
;
792 cria
.cri_klen
= sop
.mackeylen
* 8;
793 if ((info
.maxkey
&& sop
.mackeylen
> info
.maxkey
) ||
794 sop
.keylen
< info
.minkey
) {
795 dprintk("%s(%s) - mackeylen %d\n", __FUNCTION__
, CIOCGSESSSTR
,
802 cria
.cri_key
= (u_int8_t
*) kmalloc(cria
.cri_klen
/8,GFP_KERNEL
);
803 if (copy_from_user(cria
.cri_key
, sop
.mackey
,
804 cria
.cri_klen
/ 8)) {
805 dprintk("%s(%s) - bad copy\n", __FUNCTION__
, CIOCGSESSSTR
);
812 /* NB: CIOGSESSION2 has the crid */
813 if (cmd
== CIOCGSESSION2
) {
815 error
= checkcrid(crid
);
817 dprintk("%s(%s) - checkcrid %x\n", __FUNCTION__
,
818 CIOCGSESSSTR
, error
);
822 /* allow either HW or SW to be used */
823 crid
= CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
;
825 error
= crypto_newsession(&sid
, (info
.blocksize
? &crie
: &cria
), crid
);
827 dprintk("%s(%s) - newsession %d\n",__FUNCTION__
,CIOCGSESSSTR
,error
);
831 cse
= csecreate(fcr
, sid
, &crie
, &cria
, &info
);
833 crypto_freesession(sid
);
835 dprintk("%s(%s) - csecreate failed\n", __FUNCTION__
, CIOCGSESSSTR
);
840 if (cmd
== CIOCGSESSION2
) {
841 /* return hardware/driver id */
842 sop
.crid
= CRYPTO_SESID2HID(cse
->sid
);
845 if (copy_to_user((void*)arg
, &sop
, (cmd
== CIOCGSESSION
) ?
846 sizeof(struct session_op
) : sizeof(sop
))) {
847 dprintk("%s(%s) - bad copy\n", __FUNCTION__
, CIOCGSESSSTR
);
852 dprintk("%s(%s) - bail %d\n", __FUNCTION__
, CIOCGSESSSTR
, error
);
860 dprintk("%s(CIOCFSESSION)\n", __FUNCTION__
);
861 get_user(ses
, (uint32_t*)arg
);
862 cse
= csefind(fcr
, ses
);
865 dprintk("%s(CIOCFSESSION) - Fail %d\n", __FUNCTION__
, error
);
869 error
= csefree(cse
);
872 dprintk("%s(CIOCCRYPT)\n", __FUNCTION__
);
873 if(copy_from_user(&cop
, (void*)arg
, sizeof(cop
))) {
874 dprintk("%s(CIOCCRYPT) - bad copy\n", __FUNCTION__
);
878 cse
= csefind(fcr
, cop
.ses
);
881 dprintk("%s(CIOCCRYPT) - Fail %d\n", __FUNCTION__
, error
);
884 error
= cryptodev_op(cse
, &cop
);
885 if(copy_to_user((void*)arg
, &cop
, sizeof(cop
))) {
886 dprintk("%s(CIOCCRYPT) - bad return copy\n", __FUNCTION__
);
893 dprintk("%s(CIOCKEY)\n", __FUNCTION__
);
894 if (!crypto_userasymcrypto
)
895 return (EPERM
); /* XXX compat? */
896 if(copy_from_user(&kop
, (void*)arg
, sizeof(kop
))) {
897 dprintk("%s(CIOCKEY) - bad copy\n", __FUNCTION__
);
901 if (cmd
== CIOCKEY
) {
902 /* NB: crypto core enforces s/w driver use */
904 CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
;
906 error
= cryptodev_key(&kop
);
907 if(copy_to_user((void*)arg
, &kop
, sizeof(kop
))) {
908 dprintk("%s(CIOCGKEY) - bad return copy\n", __FUNCTION__
);
914 dprintk("%s(CIOCASYMFEAT)\n", __FUNCTION__
);
915 if (!crypto_userasymcrypto
) {
917 * NB: if user asym crypto operations are
918 * not permitted return "no algorithms"
919 * so well-behaved applications will just
920 * fallback to doing them in software.
924 error
= crypto_getfeat(&feat
);
926 error
= copy_to_user((void*)arg
, &feat
, sizeof(feat
));
930 if (copy_from_user(&fop
, (void*)arg
, sizeof(fop
))) {
931 dprintk("%s(CIOCFINDDEV) - bad copy\n", __FUNCTION__
);
935 error
= cryptodev_find(&fop
);
936 if (copy_to_user((void*)arg
, &fop
, sizeof(fop
))) {
937 dprintk("%s(CIOCFINDDEV) - bad return copy\n", __FUNCTION__
);
943 dprintk("%s(unknown ioctl 0x%x)\n", __FUNCTION__
, cmd
);
950 #ifdef HAVE_UNLOCKED_IOCTL
952 cryptodev_unlocked_ioctl(
957 return cryptodev_ioctl(NULL
, filp
, cmd
, arg
);
962 cryptodev_open(struct inode
*inode
, struct file
*filp
)
966 dprintk("%s()\n", __FUNCTION__
);
967 if (filp
->private_data
) {
968 printk("cryptodev: Private data already exists !\n");
972 fcr
= kmalloc(sizeof(*fcr
), GFP_KERNEL
);
974 dprintk("%s() - malloc failed\n", __FUNCTION__
);
977 memset(fcr
, 0, sizeof(*fcr
));
979 INIT_LIST_HEAD(&fcr
->csessions
);
980 filp
->private_data
= fcr
;
985 cryptodev_release(struct inode
*inode
, struct file
*filp
)
987 struct fcrypt
*fcr
= filp
->private_data
;
988 struct csession
*cse
, *tmp
;
990 dprintk("%s()\n", __FUNCTION__
);
992 printk("cryptodev: No private data on release\n");
996 list_for_each_entry_safe(cse
, tmp
, &fcr
->csessions
, list
) {
997 list_del(&cse
->list
);
1000 filp
->private_data
= NULL
;
1005 static struct file_operations cryptodev_fops
= {
1006 .owner
= THIS_MODULE
,
1007 .open
= cryptodev_open
,
1008 .release
= cryptodev_release
,
1009 .ioctl
= cryptodev_ioctl
,
1010 #ifdef HAVE_UNLOCKED_IOCTL
1011 .unlocked_ioctl
= cryptodev_unlocked_ioctl
,
1015 static struct miscdevice cryptodev
= {
1016 .minor
= CRYPTODEV_MINOR
,
1018 .fops
= &cryptodev_fops
,
1022 cryptodev_init(void)
1026 dprintk("%s(%p)\n", __FUNCTION__
, cryptodev_init
);
1027 rc
= misc_register(&cryptodev
);
1029 printk(KERN_ERR
"cryptodev: registration of /dev/crypto failed\n");
1037 cryptodev_exit(void)
1039 dprintk("%s()\n", __FUNCTION__
);
1040 misc_deregister(&cryptodev
);
1043 module_init(cryptodev_init
);
1044 module_exit(cryptodev_exit
);
1046 MODULE_LICENSE("BSD");
1047 MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
1048 MODULE_DESCRIPTION("Cryptodev (user interface to OCF)");