1 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
4 * Linux port done by David McCullough <david_mccullough@mcafee.com>
5 * Copyright (C) 2006-2010 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; */
81 /* u_int16_t ctxsize; */
85 struct list_head list
;
89 wait_queue_head_t waitq
;
97 u_char tmp_iv
[EALG_MAX_BLOCK_LEN
];
102 struct csession_info info
;
110 struct list_head csessions
;
114 static struct csession
*csefind(struct fcrypt
*, u_int
);
115 static int csedelete(struct fcrypt
*, struct csession
*);
116 static struct csession
*cseadd(struct fcrypt
*, struct csession
*);
117 static struct csession
*csecreate(struct fcrypt
*, u_int64_t
,
118 struct cryptoini
*crie
, struct cryptoini
*cria
, struct csession_info
*);
119 static int csefree(struct csession
*);
121 static int cryptodev_op(struct csession
*, struct crypt_op
*);
122 static int cryptodev_key(struct crypt_kop
*);
123 static int cryptodev_find(struct crypt_find_op
*);
125 static int cryptodev_cb(void *);
126 static int cryptodev_open(struct inode
*inode
, struct file
*filp
);
129 * Check a crypto identifier to see if it requested
130 * a valid crid and it's capabilities match.
135 int hid
= crid
& ~(CRYPTOCAP_F_SOFTWARE
| CRYPTOCAP_F_HARDWARE
);
136 int typ
= crid
& (CRYPTOCAP_F_SOFTWARE
| CRYPTOCAP_F_HARDWARE
);
139 /* if the user hasn't selected a driver, then just call newsession */
140 if (hid
== 0 && typ
!= 0)
143 caps
= crypto_getcaps(hid
);
145 /* didn't find anything with capabilities */
147 dprintk("%s: hid=%x typ=%x not matched\n", __FUNCTION__
, hid
, typ
);
151 /* the user didn't specify SW or HW, so the driver is ok */
155 /* if the type specified didn't match */
156 if (typ
!= (caps
& (CRYPTOCAP_F_SOFTWARE
| CRYPTOCAP_F_HARDWARE
))) {
157 dprintk("%s: hid=%x typ=%x caps=%x not matched\n", __FUNCTION__
,
166 cryptodev_op(struct csession
*cse
, struct crypt_op
*cop
)
168 struct cryptop
*crp
= NULL
;
169 struct cryptodesc
*crde
= NULL
, *crda
= NULL
;
172 dprintk("%s()\n", __FUNCTION__
);
173 if (cop
->len
> CRYPTO_MAX_DATA_LEN
) {
174 dprintk("%s: %d > %d\n", __FUNCTION__
, cop
->len
, CRYPTO_MAX_DATA_LEN
);
178 if (cse
->info
.blocksize
&& (cop
->len
% cse
->info
.blocksize
) != 0) {
179 dprintk("%s: blocksize=%d len=%d\n", __FUNCTION__
, cse
->info
.blocksize
,
184 cse
->uio
.uio_iov
= &cse
->iovec
;
185 cse
->uio
.uio_iovcnt
= 1;
186 cse
->uio
.uio_offset
= 0;
188 cse
->uio
.uio_resid
= cop
->len
;
189 cse
->uio
.uio_segflg
= UIO_SYSSPACE
;
190 cse
->uio
.uio_rw
= UIO_WRITE
;
191 cse
->uio
.uio_td
= td
;
193 cse
->uio
.uio_iov
[0].iov_len
= cop
->len
;
194 if (cse
->info
.authsize
)
195 cse
->uio
.uio_iov
[0].iov_len
+= cse
->info
.authsize
;
196 cse
->uio
.uio_iov
[0].iov_base
= kmalloc(cse
->uio
.uio_iov
[0].iov_len
,
199 if (cse
->uio
.uio_iov
[0].iov_base
== NULL
) {
200 dprintk("%s: iov_base kmalloc(%d) failed\n", __FUNCTION__
,
201 (int)cse
->uio
.uio_iov
[0].iov_len
);
205 crp
= crypto_getreq((cse
->info
.blocksize
!= 0) + (cse
->info
.authsize
!= 0));
207 dprintk("%s: ENOMEM\n", __FUNCTION__
);
212 if (cse
->info
.authsize
&& cse
->info
.blocksize
) {
213 if (cop
->op
== COP_ENCRYPT
) {
214 crde
= crp
->crp_desc
;
215 crda
= crde
->crd_next
;
217 crda
= crp
->crp_desc
;
218 crde
= crda
->crd_next
;
220 } else if (cse
->info
.authsize
) {
221 crda
= crp
->crp_desc
;
222 } else if (cse
->info
.blocksize
) {
223 crde
= crp
->crp_desc
;
225 dprintk("%s: bad request\n", __FUNCTION__
);
230 if ((error
= copy_from_user(cse
->uio
.uio_iov
[0].iov_base
, cop
->src
,
232 dprintk("%s: bad copy\n", __FUNCTION__
);
238 crda
->crd_len
= cop
->len
;
239 crda
->crd_inject
= cop
->len
;
241 crda
->crd_alg
= cse
->mac
;
242 crda
->crd_key
= cse
->mackey
;
243 crda
->crd_klen
= cse
->mackeylen
* 8;
247 if (cop
->op
== COP_ENCRYPT
)
248 crde
->crd_flags
|= CRD_F_ENCRYPT
;
250 crde
->crd_flags
&= ~CRD_F_ENCRYPT
;
251 crde
->crd_len
= cop
->len
;
252 crde
->crd_inject
= 0;
254 crde
->crd_alg
= cse
->cipher
;
255 crde
->crd_key
= cse
->key
;
256 crde
->crd_klen
= cse
->keylen
* 8;
259 crp
->crp_ilen
= cse
->uio
.uio_iov
[0].iov_len
;
260 crp
->crp_flags
= CRYPTO_F_IOV
| CRYPTO_F_CBIMM
261 | (cop
->flags
& COP_F_BATCH
);
262 crp
->crp_buf
= (caddr_t
)&cse
->uio
;
263 crp
->crp_callback
= (int (*) (struct cryptop
*)) cryptodev_cb
;
264 crp
->crp_sid
= cse
->sid
;
265 crp
->crp_opaque
= (void *)cse
;
270 dprintk("%s no crde\n", __FUNCTION__
);
273 if (cse
->cipher
== CRYPTO_ARC4
) { /* XXX use flag? */
275 dprintk("%s arc4 with IV\n", __FUNCTION__
);
278 if ((error
= copy_from_user(cse
->tmp_iv
, cop
->iv
,
279 cse
->info
.blocksize
))) {
280 dprintk("%s bad iv copy\n", __FUNCTION__
);
283 memcpy(crde
->crd_iv
, cse
->tmp_iv
, cse
->info
.blocksize
);
284 crde
->crd_flags
|= CRD_F_IV_EXPLICIT
| CRD_F_IV_PRESENT
;
286 } else if (cse
->cipher
== CRYPTO_ARC4
) { /* XXX use flag? */
289 crde
->crd_flags
|= CRD_F_IV_PRESENT
;
290 crde
->crd_skip
= cse
->info
.blocksize
;
291 crde
->crd_len
-= cse
->info
.blocksize
;
294 if (cop
->mac
&& crda
== NULL
) {
296 dprintk("%s no crda\n", __FUNCTION__
);
301 * Let the dispatch run unlocked, then, interlock against the
302 * callback before checking if the operation completed and going
303 * to sleep. This insures drivers don't inherit our lock which
304 * results in a lock order reversal between crypto_dispatch forced
305 * entry and the crypto_done callback into us.
307 error
= crypto_dispatch(crp
);
309 dprintk("%s error in crypto_dispatch\n", __FUNCTION__
);
313 dprintk("%s about to WAIT\n", __FUNCTION__
);
315 * we really need to wait for driver to complete to maintain
316 * state, luckily interrupts will be remembered
319 error
= wait_event_interruptible(crp
->crp_waitq
,
320 ((crp
->crp_flags
& CRYPTO_F_DONE
) != 0));
322 * we can't break out of this loop or we will leave behind
323 * a huge mess, however, staying here means if your driver
324 * is broken user applications can hang and not be killed.
325 * The solution, fix your driver :-)
331 } while ((crp
->crp_flags
& CRYPTO_F_DONE
) == 0);
332 dprintk("%s finished WAITING error=%d\n", __FUNCTION__
, error
);
334 if (crp
->crp_etype
!= 0) {
335 error
= crp
->crp_etype
;
336 dprintk("%s error in crp processing\n", __FUNCTION__
);
342 dprintk("%s error in cse processing\n", __FUNCTION__
);
346 if (cop
->dst
&& (error
= copy_to_user(cop
->dst
,
347 cse
->uio
.uio_iov
[0].iov_base
, cop
->len
))) {
348 dprintk("%s bad dst copy\n", __FUNCTION__
);
353 (error
=copy_to_user(cop
->mac
,
354 (caddr_t
)cse
->uio
.uio_iov
[0].iov_base
+ cop
->len
,
355 cse
->info
.authsize
))) {
356 dprintk("%s bad mac copy\n", __FUNCTION__
);
363 if (cse
->uio
.uio_iov
[0].iov_base
)
364 kfree(cse
->uio
.uio_iov
[0].iov_base
);
370 cryptodev_cb(void *op
)
372 struct cryptop
*crp
= (struct cryptop
*) op
;
373 struct csession
*cse
= (struct csession
*)crp
->crp_opaque
;
376 dprintk("%s()\n", __FUNCTION__
);
377 error
= crp
->crp_etype
;
378 if (error
== EAGAIN
) {
379 crp
->crp_flags
&= ~CRYPTO_F_DONE
;
382 * DAVIDM I am fairly sure that we should turn this into a batch
383 * request to stop bad karma/lockup, revisit
385 crp
->crp_flags
|= CRYPTO_F_BATCH
;
387 return crypto_dispatch(crp
);
389 if (error
!= 0 || (crp
->crp_flags
& CRYPTO_F_DONE
)) {
391 wake_up_interruptible(&crp
->crp_waitq
);
397 cryptodevkey_cb(void *op
)
399 struct cryptkop
*krp
= (struct cryptkop
*) op
;
400 dprintk("%s()\n", __FUNCTION__
);
401 wake_up_interruptible(&krp
->krp_waitq
);
406 cryptodev_key(struct crypt_kop
*kop
)
408 struct cryptkop
*krp
= NULL
;
410 int in
, out
, size
, i
;
412 dprintk("%s()\n", __FUNCTION__
);
413 if (kop
->crk_iparams
+ kop
->crk_oparams
> CRK_MAXPARAM
) {
414 dprintk("%s params too big\n", __FUNCTION__
);
418 in
= kop
->crk_iparams
;
419 out
= kop
->crk_oparams
;
420 switch (kop
->crk_op
) {
422 if (in
== 3 && out
== 1)
425 case CRK_MOD_EXP_CRT
:
426 if (in
== 6 && out
== 1)
430 if (in
== 5 && out
== 2)
434 if (in
== 7 && out
== 0)
437 case CRK_DH_COMPUTE_KEY
:
438 if (in
== 3 && out
== 1)
445 krp
= (struct cryptkop
*)kmalloc(sizeof *krp
, GFP_KERNEL
);
448 bzero(krp
, sizeof *krp
);
449 krp
->krp_op
= kop
->crk_op
;
450 krp
->krp_status
= kop
->crk_status
;
451 krp
->krp_iparams
= kop
->crk_iparams
;
452 krp
->krp_oparams
= kop
->crk_oparams
;
453 krp
->krp_crid
= kop
->crk_crid
;
455 krp
->krp_flags
= CRYPTO_KF_CBIMM
;
456 krp
->krp_callback
= (int (*) (struct cryptkop
*)) cryptodevkey_cb
;
457 init_waitqueue_head(&krp
->krp_waitq
);
459 for (i
= 0; i
< CRK_MAXPARAM
; i
++)
460 krp
->krp_param
[i
].crp_nbits
= kop
->crk_param
[i
].crp_nbits
;
461 for (i
= 0; i
< krp
->krp_iparams
+ krp
->krp_oparams
; i
++) {
462 size
= (krp
->krp_param
[i
].crp_nbits
+ 7) / 8;
465 krp
->krp_param
[i
].crp_p
= (caddr_t
) kmalloc(size
, GFP_KERNEL
);
466 if (i
>= krp
->krp_iparams
)
468 error
= copy_from_user(krp
->krp_param
[i
].crp_p
,
469 kop
->crk_param
[i
].crp_p
, size
);
474 error
= crypto_kdispatch(krp
);
479 error
= wait_event_interruptible(krp
->krp_waitq
,
480 ((krp
->krp_flags
& CRYPTO_KF_DONE
) != 0));
482 * we can't break out of this loop or we will leave behind
483 * a huge mess, however, staying here means if your driver
484 * is broken user applications can hang and not be killed.
485 * The solution, fix your driver :-)
491 } while ((krp
->krp_flags
& CRYPTO_KF_DONE
) == 0);
493 dprintk("%s finished WAITING error=%d\n", __FUNCTION__
, error
);
495 kop
->crk_crid
= krp
->krp_crid
; /* device that did the work */
496 if (krp
->krp_status
!= 0) {
497 error
= krp
->krp_status
;
501 for (i
= krp
->krp_iparams
; i
< krp
->krp_iparams
+ krp
->krp_oparams
; i
++) {
502 size
= (krp
->krp_param
[i
].crp_nbits
+ 7) / 8;
505 error
= copy_to_user(kop
->crk_param
[i
].crp_p
, krp
->krp_param
[i
].crp_p
,
513 kop
->crk_status
= krp
->krp_status
;
514 for (i
= 0; i
< CRK_MAXPARAM
; i
++) {
515 if (krp
->krp_param
[i
].crp_p
)
516 kfree(krp
->krp_param
[i
].crp_p
);
524 cryptodev_find(struct crypt_find_op
*find
)
528 if (find
->crid
!= -1) {
529 dev
= crypto_find_device_byhid(find
->crid
);
532 strlcpy(find
->name
, device_get_nameunit(dev
),
535 find
->crid
= crypto_find_driver(find
->name
);
536 if (find
->crid
== -1)
542 static struct csession
*
543 csefind(struct fcrypt
*fcr
, u_int ses
)
545 struct csession
*cse
;
547 dprintk("%s()\n", __FUNCTION__
);
548 list_for_each_entry(cse
, &fcr
->csessions
, list
)
555 csedelete(struct fcrypt
*fcr
, struct csession
*cse_del
)
557 struct csession
*cse
;
559 dprintk("%s()\n", __FUNCTION__
);
560 list_for_each_entry(cse
, &fcr
->csessions
, list
) {
561 if (cse
== cse_del
) {
562 list_del(&cse
->list
);
569 static struct csession
*
570 cseadd(struct fcrypt
*fcr
, struct csession
*cse
)
572 dprintk("%s()\n", __FUNCTION__
);
573 list_add_tail(&cse
->list
, &fcr
->csessions
);
574 cse
->ses
= fcr
->sesn
++;
578 static struct csession
*
579 csecreate(struct fcrypt
*fcr
, u_int64_t sid
, struct cryptoini
*crie
,
580 struct cryptoini
*cria
, struct csession_info
*info
)
582 struct csession
*cse
;
584 dprintk("%s()\n", __FUNCTION__
);
585 cse
= (struct csession
*) kmalloc(sizeof(struct csession
), GFP_KERNEL
);
588 memset(cse
, 0, sizeof(struct csession
));
590 INIT_LIST_HEAD(&cse
->list
);
591 init_waitqueue_head(&cse
->waitq
);
593 cse
->key
= crie
->cri_key
;
594 cse
->keylen
= crie
->cri_klen
/8;
595 cse
->mackey
= cria
->cri_key
;
596 cse
->mackeylen
= cria
->cri_klen
/8;
598 cse
->cipher
= crie
->cri_alg
;
599 cse
->mac
= cria
->cri_alg
;
606 csefree(struct csession
*cse
)
610 dprintk("%s()\n", __FUNCTION__
);
611 error
= crypto_freesession(cse
->sid
);
627 struct cryptoini cria
, crie
;
628 struct fcrypt
*fcr
= filp
->private_data
;
629 struct csession
*cse
;
630 struct csession_info info
;
631 struct session2_op sop
;
633 struct crypt_kop kop
;
634 struct crypt_find_op fop
;
637 int feat
, fd
, error
= 0, crid
;
640 dprintk("%s(cmd=%x arg=%lx)\n", __FUNCTION__
, cmd
, arg
);
645 dprintk("%s(CRIOGET)\n", __FUNCTION__
);
648 for (fd
= 0; fd
< files_fdtable(current
->files
)->max_fds
; fd
++)
649 if (files_fdtable(current
->files
)->fd
[fd
] == filp
)
653 put_user(fd
, (int *) arg
);
654 return IS_ERR_VALUE(fd
) ? fd
: 0;
657 #define CIOCGSESSSTR (cmd == CIOCGSESSION ? "CIOCGSESSION" : "CIOCGSESSION2")
660 dprintk("%s(%s)\n", __FUNCTION__
, CIOCGSESSSTR
);
661 memset(&crie
, 0, sizeof(crie
));
662 memset(&cria
, 0, sizeof(cria
));
663 memset(&info
, 0, sizeof(info
));
664 memset(&sop
, 0, sizeof(sop
));
666 if (copy_from_user(&sop
, (void*)arg
, (cmd
== CIOCGSESSION
) ?
667 sizeof(struct session_op
) : sizeof(sop
))) {
668 dprintk("%s(%s) - bad copy\n", __FUNCTION__
, CIOCGSESSSTR
);
673 switch (sop
.cipher
) {
675 dprintk("%s(%s) - no cipher\n", __FUNCTION__
, CIOCGSESSSTR
);
677 case CRYPTO_NULL_CBC
:
678 info
.blocksize
= NULL_BLOCK_LEN
;
679 info
.minkey
= NULL_MIN_KEY_LEN
;
680 info
.maxkey
= NULL_MAX_KEY_LEN
;
683 info
.blocksize
= DES_BLOCK_LEN
;
684 info
.minkey
= DES_MIN_KEY_LEN
;
685 info
.maxkey
= DES_MAX_KEY_LEN
;
687 case CRYPTO_3DES_CBC
:
688 info
.blocksize
= DES3_BLOCK_LEN
;
689 info
.minkey
= DES3_MIN_KEY_LEN
;
690 info
.maxkey
= DES3_MAX_KEY_LEN
;
693 info
.blocksize
= BLOWFISH_BLOCK_LEN
;
694 info
.minkey
= BLOWFISH_MIN_KEY_LEN
;
695 info
.maxkey
= BLOWFISH_MAX_KEY_LEN
;
697 case CRYPTO_CAST_CBC
:
698 info
.blocksize
= CAST128_BLOCK_LEN
;
699 info
.minkey
= CAST128_MIN_KEY_LEN
;
700 info
.maxkey
= CAST128_MAX_KEY_LEN
;
702 case CRYPTO_SKIPJACK_CBC
:
703 info
.blocksize
= SKIPJACK_BLOCK_LEN
;
704 info
.minkey
= SKIPJACK_MIN_KEY_LEN
;
705 info
.maxkey
= SKIPJACK_MAX_KEY_LEN
;
708 info
.blocksize
= AES_BLOCK_LEN
;
709 info
.minkey
= AES_MIN_KEY_LEN
;
710 info
.maxkey
= AES_MAX_KEY_LEN
;
713 info
.blocksize
= ARC4_BLOCK_LEN
;
714 info
.minkey
= ARC4_MIN_KEY_LEN
;
715 info
.maxkey
= ARC4_MAX_KEY_LEN
;
717 case CRYPTO_CAMELLIA_CBC
:
718 info
.blocksize
= CAMELLIA_BLOCK_LEN
;
719 info
.minkey
= CAMELLIA_MIN_KEY_LEN
;
720 info
.maxkey
= CAMELLIA_MAX_KEY_LEN
;
723 dprintk("%s(%s) - bad cipher\n", __FUNCTION__
, CIOCGSESSSTR
);
730 dprintk("%s(%s) - no mac\n", __FUNCTION__
, CIOCGSESSSTR
);
732 case CRYPTO_NULL_HMAC
:
733 info
.authsize
= NULL_HASH_LEN
;
736 info
.authsize
= MD5_HASH_LEN
;
739 info
.authsize
= SHA1_HASH_LEN
;
741 case CRYPTO_SHA2_256
:
742 info
.authsize
= SHA2_256_HASH_LEN
;
744 case CRYPTO_SHA2_384
:
745 info
.authsize
= SHA2_384_HASH_LEN
;
747 case CRYPTO_SHA2_512
:
748 info
.authsize
= SHA2_512_HASH_LEN
;
750 case CRYPTO_RIPEMD160
:
751 info
.authsize
= RIPEMD160_HASH_LEN
;
753 case CRYPTO_MD5_HMAC
:
754 info
.authsize
= MD5_HASH_LEN
;
757 case CRYPTO_SHA1_HMAC
:
758 info
.authsize
= SHA1_HASH_LEN
;
761 case CRYPTO_SHA2_256_HMAC
:
762 info
.authsize
= SHA2_256_HASH_LEN
;
765 case CRYPTO_SHA2_384_HMAC
:
766 info
.authsize
= SHA2_384_HASH_LEN
;
769 case CRYPTO_SHA2_512_HMAC
:
770 info
.authsize
= SHA2_512_HASH_LEN
;
773 case CRYPTO_RIPEMD160_HMAC
:
774 info
.authsize
= RIPEMD160_HASH_LEN
;
778 dprintk("%s(%s) - bad mac\n", __FUNCTION__
, CIOCGSESSSTR
);
783 if (info
.blocksize
) {
784 crie
.cri_alg
= sop
.cipher
;
785 crie
.cri_klen
= sop
.keylen
* 8;
786 if ((info
.maxkey
&& sop
.keylen
> info
.maxkey
) ||
787 sop
.keylen
< info
.minkey
) {
788 dprintk("%s(%s) - bad key\n", __FUNCTION__
, CIOCGSESSSTR
);
793 crie
.cri_key
= (u_int8_t
*) kmalloc(crie
.cri_klen
/8+1, GFP_KERNEL
);
794 if (copy_from_user(crie
.cri_key
, sop
.key
,
796 dprintk("%s(%s) - bad copy\n", __FUNCTION__
, CIOCGSESSSTR
);
801 crie
.cri_next
= &cria
;
805 cria
.cri_alg
= sop
.mac
;
806 cria
.cri_klen
= sop
.mackeylen
* 8;
807 if (info
.authkey
&& sop
.mackeylen
!= info
.authkey
) {
808 dprintk("%s(%s) - mackeylen %d != %d\n", __FUNCTION__
,
809 CIOCGSESSSTR
, sop
.mackeylen
, info
.authkey
);
815 cria
.cri_key
= (u_int8_t
*) kmalloc(cria
.cri_klen
/8,GFP_KERNEL
);
816 if (copy_from_user(cria
.cri_key
, sop
.mackey
,
817 cria
.cri_klen
/ 8)) {
818 dprintk("%s(%s) - bad copy\n", __FUNCTION__
, CIOCGSESSSTR
);
825 /* NB: CIOGSESSION2 has the crid */
826 if (cmd
== CIOCGSESSION2
) {
828 error
= checkcrid(crid
);
830 dprintk("%s(%s) - checkcrid %x\n", __FUNCTION__
,
831 CIOCGSESSSTR
, error
);
835 /* allow either HW or SW to be used */
836 crid
= CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
;
838 error
= crypto_newsession(&sid
, (info
.blocksize
? &crie
: &cria
), crid
);
840 dprintk("%s(%s) - newsession %d\n",__FUNCTION__
,CIOCGSESSSTR
,error
);
844 cse
= csecreate(fcr
, sid
, &crie
, &cria
, &info
);
846 crypto_freesession(sid
);
848 dprintk("%s(%s) - csecreate failed\n", __FUNCTION__
, CIOCGSESSSTR
);
853 if (cmd
== CIOCGSESSION2
) {
854 /* return hardware/driver id */
855 sop
.crid
= CRYPTO_SESID2HID(cse
->sid
);
858 if (copy_to_user((void*)arg
, &sop
, (cmd
== CIOCGSESSION
) ?
859 sizeof(struct session_op
) : sizeof(sop
))) {
860 dprintk("%s(%s) - bad copy\n", __FUNCTION__
, CIOCGSESSSTR
);
865 dprintk("%s(%s) - bail %d\n", __FUNCTION__
, CIOCGSESSSTR
, error
);
873 dprintk("%s(CIOCFSESSION)\n", __FUNCTION__
);
874 get_user(ses
, (uint32_t*)arg
);
875 cse
= csefind(fcr
, ses
);
878 dprintk("%s(CIOCFSESSION) - Fail %d\n", __FUNCTION__
, error
);
882 error
= csefree(cse
);
885 dprintk("%s(CIOCCRYPT)\n", __FUNCTION__
);
886 if(copy_from_user(&cop
, (void*)arg
, sizeof(cop
))) {
887 dprintk("%s(CIOCCRYPT) - bad copy\n", __FUNCTION__
);
891 cse
= csefind(fcr
, cop
.ses
);
894 dprintk("%s(CIOCCRYPT) - Fail %d\n", __FUNCTION__
, error
);
897 error
= cryptodev_op(cse
, &cop
);
898 if(copy_to_user((void*)arg
, &cop
, sizeof(cop
))) {
899 dprintk("%s(CIOCCRYPT) - bad return copy\n", __FUNCTION__
);
906 dprintk("%s(CIOCKEY)\n", __FUNCTION__
);
907 if (!crypto_userasymcrypto
)
908 return (EPERM
); /* XXX compat? */
909 if(copy_from_user(&kop
, (void*)arg
, sizeof(kop
))) {
910 dprintk("%s(CIOCKEY) - bad copy\n", __FUNCTION__
);
914 if (cmd
== CIOCKEY
) {
915 /* NB: crypto core enforces s/w driver use */
917 CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
;
919 error
= cryptodev_key(&kop
);
920 if(copy_to_user((void*)arg
, &kop
, sizeof(kop
))) {
921 dprintk("%s(CIOCGKEY) - bad return copy\n", __FUNCTION__
);
927 dprintk("%s(CIOCASYMFEAT)\n", __FUNCTION__
);
928 if (!crypto_userasymcrypto
) {
930 * NB: if user asym crypto operations are
931 * not permitted return "no algorithms"
932 * so well-behaved applications will just
933 * fallback to doing them in software.
937 error
= crypto_getfeat(&feat
);
939 error
= copy_to_user((void*)arg
, &feat
, sizeof(feat
));
943 if (copy_from_user(&fop
, (void*)arg
, sizeof(fop
))) {
944 dprintk("%s(CIOCFINDDEV) - bad copy\n", __FUNCTION__
);
948 error
= cryptodev_find(&fop
);
949 if (copy_to_user((void*)arg
, &fop
, sizeof(fop
))) {
950 dprintk("%s(CIOCFINDDEV) - bad return copy\n", __FUNCTION__
);
956 dprintk("%s(unknown ioctl 0x%x)\n", __FUNCTION__
, cmd
);
963 #ifdef HAVE_UNLOCKED_IOCTL
965 cryptodev_unlocked_ioctl(
970 return cryptodev_ioctl(NULL
, filp
, cmd
, arg
);
975 cryptodev_open(struct inode
*inode
, struct file
*filp
)
979 dprintk("%s()\n", __FUNCTION__
);
980 if (filp
->private_data
) {
981 printk("cryptodev: Private data already exists !\n");
985 fcr
= kmalloc(sizeof(*fcr
), GFP_KERNEL
);
987 dprintk("%s() - malloc failed\n", __FUNCTION__
);
990 memset(fcr
, 0, sizeof(*fcr
));
992 INIT_LIST_HEAD(&fcr
->csessions
);
993 filp
->private_data
= fcr
;
998 cryptodev_release(struct inode
*inode
, struct file
*filp
)
1000 struct fcrypt
*fcr
= filp
->private_data
;
1001 struct csession
*cse
, *tmp
;
1003 dprintk("%s()\n", __FUNCTION__
);
1005 printk("cryptodev: No private data on release\n");
1009 list_for_each_entry_safe(cse
, tmp
, &fcr
->csessions
, list
) {
1010 list_del(&cse
->list
);
1013 filp
->private_data
= NULL
;
1018 static struct file_operations cryptodev_fops
= {
1019 .owner
= THIS_MODULE
,
1020 .open
= cryptodev_open
,
1021 .release
= cryptodev_release
,
1022 .ioctl
= cryptodev_ioctl
,
1023 #ifdef HAVE_UNLOCKED_IOCTL
1024 .unlocked_ioctl
= cryptodev_unlocked_ioctl
,
1028 static struct miscdevice cryptodev
= {
1029 .minor
= CRYPTODEV_MINOR
,
1031 .fops
= &cryptodev_fops
,
1035 cryptodev_init(void)
1039 dprintk("%s(%p)\n", __FUNCTION__
, cryptodev_init
);
1040 rc
= misc_register(&cryptodev
);
1042 printk(KERN_ERR
"cryptodev: registration of /dev/crypto failed\n");
1050 cryptodev_exit(void)
1052 dprintk("%s()\n", __FUNCTION__
);
1053 misc_deregister(&cryptodev
);
1056 module_init(cryptodev_init
);
1057 module_exit(cryptodev_exit
);
1059 MODULE_LICENSE("BSD");
1060 MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
1061 MODULE_DESCRIPTION("Cryptodev (user interface to OCF)");