2 * Driver for /dev/crypto device (aka CryptoDev)
4 * Copyright (c) 2004 Michal Ludvig <mludvig@logix.net.nz>, SuSE Labs
5 * Copyright (c) 2009,2010,2011 Nikos Mavrogiannopoulos <nmav@gnutls.org>
6 * Copyright (c) 2010 Phil Sutter
8 * This file is part of linux cryptodev.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * Device /dev/crypto provides an interface for
28 * accessing kernel CryptoAPI algorithms (ciphers,
29 * hashes) from userspace programs.
31 * /dev/crypto interface was originally introduced in
32 * OpenBSD and this module attempts to keep the API.
36 #include <crypto/hash.h>
37 #include <linux/crypto.h>
39 #include <linux/highmem.h>
40 #include <linux/ioctl.h>
41 #include <linux/random.h>
42 #include <linux/syscalls.h>
43 #include <linux/pagemap.h>
44 #include <linux/poll.h>
45 #include <linux/uaccess.h>
46 #include <crypto/cryptodev.h>
47 #include <linux/scatterlist.h>
48 #include "cryptodev_int.h"
52 MODULE_AUTHOR("Nikos Mavrogiannopoulos <nmav@gnutls.org>");
53 MODULE_DESCRIPTION("CryptoDev driver");
54 MODULE_LICENSE("GPL");
56 /* ====== Compile-time config ====== */
58 /* Default (pre-allocated) and maximum size of the job queue.
59 * These are free, pending and done items all together. */
60 #define DEF_COP_RINGSIZE 16
61 #define MAX_COP_RINGSIZE 64
63 /* ====== Module parameters ====== */
65 int cryptodev_verbosity
;
66 module_param(cryptodev_verbosity
, int, 0644);
67 MODULE_PARM_DESC(cryptodev_verbosity
, "0: normal, 1: verbose, 2: debug");
69 /* ====== CryptoAPI ====== */
70 struct todo_list_item
{
71 struct list_head __hook
;
72 struct kernel_crypt_op kcop
;
77 struct list_head list
;
83 struct locked_list free
, todo
, done
;
85 struct work_struct cryptask
;
86 wait_queue_head_t user_waiter
;
89 #define FILL_SG(sg, ptr, len) \
91 (sg)->page = virt_to_page(ptr); \
92 (sg)->offset = offset_in_page(ptr); \
94 (sg)->dma_address = 0; \
97 /* cryptodev's own workqueue, keeps crypto tasks from disturbing the force */
98 static struct workqueue_struct
*cryptodev_wq
;
100 /* Prepare session for future use. */
102 crypto_create_session(struct fcrypt
*fcr
, struct session_op
*sop
)
104 struct csession
*ses_new
= NULL
, *ses_ptr
;
106 const char *alg_name
= NULL
;
107 const char *hash_name
= NULL
;
108 int hmac_mode
= 1, stream
= 0, aead
= 0;
110 /* Does the request make sense? */
111 if (unlikely(!sop
->cipher
&& !sop
->mac
)) {
112 dprintk(1, KERN_DEBUG
, "Both 'cipher' and 'mac' unset.\n");
116 switch (sop
->cipher
) {
120 alg_name
= "cbc(des)";
122 case CRYPTO_3DES_CBC
:
123 alg_name
= "cbc(des3_ede)";
126 alg_name
= "cbc(blowfish)";
129 alg_name
= "cbc(aes)";
132 alg_name
= "ecb(aes)";
134 case CRYPTO_CAMELLIA_CBC
:
135 alg_name
= "cbc(camellia)";
138 alg_name
= "ctr(aes)";
142 alg_name
= "gcm(aes)";
147 alg_name
= "ecb(cipher_null)";
151 dprintk(1, KERN_DEBUG
, "bad cipher: %d\n", sop
->cipher
);
158 case CRYPTO_MD5_HMAC
:
159 hash_name
= "hmac(md5)";
161 case CRYPTO_RIPEMD160_HMAC
:
162 hash_name
= "hmac(rmd160)";
164 case CRYPTO_SHA1_HMAC
:
165 hash_name
= "hmac(sha1)";
167 case CRYPTO_SHA2_256_HMAC
:
168 hash_name
= "hmac(sha256)";
170 case CRYPTO_SHA2_384_HMAC
:
171 hash_name
= "hmac(sha384)";
173 case CRYPTO_SHA2_512_HMAC
:
174 hash_name
= "hmac(sha512)";
182 case CRYPTO_RIPEMD160
:
183 hash_name
= "rmd160";
190 case CRYPTO_SHA2_256
:
191 hash_name
= "sha256";
194 case CRYPTO_SHA2_384
:
195 hash_name
= "sha384";
198 case CRYPTO_SHA2_512
:
199 hash_name
= "sha512";
203 dprintk(1, KERN_DEBUG
, "bad mac: %d\n", sop
->mac
);
207 /* Create a session and put it to the list. */
208 ses_new
= kzalloc(sizeof(*ses_new
), GFP_KERNEL
);
212 /* Set-up crypto transform. */
214 uint8_t keyp
[CRYPTO_CIPHER_MAX_KEY_LEN
];
216 if (unlikely(sop
->keylen
> CRYPTO_CIPHER_MAX_KEY_LEN
)) {
217 dprintk(1, KERN_DEBUG
,
218 "Setting key failed for %s-%zu.\n",
219 alg_name
, (size_t)sop
->keylen
*8);
224 if (unlikely(copy_from_user(keyp
, sop
->key
, sop
->keylen
))) {
229 ret
= cryptodev_cipher_init(&ses_new
->cdata
, alg_name
, keyp
,
230 sop
->keylen
, stream
, aead
);
232 dprintk(1, KERN_DEBUG
,
233 "Failed to load cipher for %s\n", alg_name
);
239 if (hash_name
&& aead
== 0) {
240 uint8_t keyp
[CRYPTO_HMAC_MAX_KEY_LEN
];
242 if (unlikely(sop
->mackeylen
> CRYPTO_HMAC_MAX_KEY_LEN
)) {
243 dprintk(1, KERN_DEBUG
,
244 "Setting key failed for %s-%zu.\n",
245 alg_name
, (size_t)sop
->mackeylen
*8);
250 if (sop
->mackey
&& unlikely(copy_from_user(keyp
, sop
->mackey
,
256 ret
= cryptodev_hash_init(&ses_new
->hdata
, hash_name
, hmac_mode
,
257 keyp
, sop
->mackeylen
);
259 dprintk(1, KERN_DEBUG
, "Failed to load hash for %s\n", hash_name
);
265 ses_new
->alignmask
= max(ses_new
->cdata
.alignmask
,
266 ses_new
->hdata
.alignmask
);
267 dprintk(2, KERN_DEBUG
, "got alignmask %d\n", ses_new
->alignmask
);
269 ses_new
->array_size
= DEFAULT_PREALLOC_PAGES
;
270 dprintk(2, KERN_DEBUG
, "preallocating for %d user pages\n",
271 ses_new
->array_size
);
272 ses_new
->pages
= kzalloc(ses_new
->array_size
*
273 sizeof(struct page
*), GFP_KERNEL
);
274 ses_new
->sg
= kzalloc(ses_new
->array_size
*
275 sizeof(struct scatterlist
), GFP_KERNEL
);
276 if (ses_new
->sg
== NULL
|| ses_new
->pages
== NULL
) {
277 dprintk(0, KERN_DEBUG
, "Memory error\n");
282 /* put the new session to the list */
283 get_random_bytes(&ses_new
->sid
, sizeof(ses_new
->sid
));
284 mutex_init(&ses_new
->sem
);
286 mutex_lock(&fcr
->sem
);
288 list_for_each_entry(ses_ptr
, &fcr
->list
, entry
) {
289 /* Check for duplicate SID */
290 if (unlikely(ses_new
->sid
== ses_ptr
->sid
)) {
291 get_random_bytes(&ses_new
->sid
, sizeof(ses_new
->sid
));
292 /* Unless we have a broken RNG this
293 shouldn't loop forever... ;-) */
298 list_add(&ses_new
->entry
, &fcr
->list
);
299 mutex_unlock(&fcr
->sem
);
301 /* Fill in some values for the user. */
302 sop
->ses
= ses_new
->sid
;
307 cryptodev_cipher_deinit(&ses_new
->cdata
);
309 kfree(ses_new
->pages
);
317 /* Everything that needs to be done when remowing a session. */
319 crypto_destroy_session(struct csession
*ses_ptr
)
321 if (!mutex_trylock(&ses_ptr
->sem
)) {
322 dprintk(2, KERN_DEBUG
, "Waiting for semaphore of sid=0x%08X\n",
324 mutex_lock(&ses_ptr
->sem
);
326 dprintk(2, KERN_DEBUG
, "Removed session 0x%08X\n", ses_ptr
->sid
);
327 cryptodev_cipher_deinit(&ses_ptr
->cdata
);
328 cryptodev_hash_deinit(&ses_ptr
->hdata
);
329 dprintk(2, KERN_DEBUG
, "freeing space for %d user pages\n",
330 ses_ptr
->array_size
);
331 kfree(ses_ptr
->pages
);
333 mutex_unlock(&ses_ptr
->sem
);
337 /* Look up a session by ID and remove. */
339 crypto_finish_session(struct fcrypt
*fcr
, uint32_t sid
)
341 struct csession
*tmp
, *ses_ptr
;
342 struct list_head
*head
;
345 mutex_lock(&fcr
->sem
);
347 list_for_each_entry_safe(ses_ptr
, tmp
, head
, entry
) {
348 if (ses_ptr
->sid
== sid
) {
349 list_del(&ses_ptr
->entry
);
350 crypto_destroy_session(ses_ptr
);
355 if (unlikely(!ses_ptr
)) {
356 dprintk(1, KERN_ERR
, "Session with sid=0x%08X not found!\n",
360 mutex_unlock(&fcr
->sem
);
365 /* Remove all sessions when closing the file */
367 crypto_finish_all_sessions(struct fcrypt
*fcr
)
369 struct csession
*tmp
, *ses_ptr
;
370 struct list_head
*head
;
372 mutex_lock(&fcr
->sem
);
375 list_for_each_entry_safe(ses_ptr
, tmp
, head
, entry
) {
376 list_del(&ses_ptr
->entry
);
377 crypto_destroy_session(ses_ptr
);
379 mutex_unlock(&fcr
->sem
);
384 /* Look up session by session ID. The returned session is locked. */
386 crypto_get_session_by_sid(struct fcrypt
*fcr
, uint32_t sid
)
388 struct csession
*ses_ptr
, *retval
= NULL
;
390 if (unlikely(fcr
== NULL
))
393 mutex_lock(&fcr
->sem
);
394 list_for_each_entry(ses_ptr
, &fcr
->list
, entry
) {
395 if (ses_ptr
->sid
== sid
) {
396 mutex_lock(&ses_ptr
->sem
);
401 mutex_unlock(&fcr
->sem
);
406 static void cryptask_routine(struct work_struct
*work
)
408 struct crypt_priv
*pcr
= container_of(work
, struct crypt_priv
, cryptask
);
409 struct todo_list_item
*item
;
412 /* fetch all pending jobs into the temporary list */
413 mutex_lock(&pcr
->todo
.lock
);
414 list_cut_position(&tmp
, &pcr
->todo
.list
, pcr
->todo
.list
.prev
);
415 mutex_unlock(&pcr
->todo
.lock
);
417 /* handle each job locklessly */
418 list_for_each_entry(item
, &tmp
, __hook
) {
419 item
->result
= crypto_run(&pcr
->fcrypt
, &item
->kcop
);
420 if (unlikely(item
->result
))
421 dprintk(0, KERN_ERR
, "crypto_run() failed: %d\n",
425 /* push all handled jobs to the done list at once */
426 mutex_lock(&pcr
->done
.lock
);
427 list_splice_tail(&tmp
, &pcr
->done
.list
);
428 mutex_unlock(&pcr
->done
.lock
);
430 /* wake for POLLIN */
431 wake_up_interruptible(&pcr
->user_waiter
);
434 /* ====== /dev/crypto ====== */
437 cryptodev_open(struct inode
*inode
, struct file
*filp
)
439 struct todo_list_item
*tmp
;
440 struct crypt_priv
*pcr
;
443 pcr
= kmalloc(sizeof(*pcr
), GFP_KERNEL
);
447 memset(pcr
, 0, sizeof(*pcr
));
448 mutex_init(&pcr
->fcrypt
.sem
);
449 INIT_LIST_HEAD(&pcr
->fcrypt
.list
);
451 INIT_LIST_HEAD(&pcr
->free
.list
);
452 INIT_LIST_HEAD(&pcr
->todo
.list
);
453 INIT_LIST_HEAD(&pcr
->done
.list
);
454 INIT_WORK(&pcr
->cryptask
, cryptask_routine
);
455 mutex_init(&pcr
->free
.lock
);
456 mutex_init(&pcr
->todo
.lock
);
457 mutex_init(&pcr
->done
.lock
);
458 init_waitqueue_head(&pcr
->user_waiter
);
460 for (i
= 0; i
< DEF_COP_RINGSIZE
; i
++) {
461 tmp
= kzalloc(sizeof(struct todo_list_item
), GFP_KERNEL
);
465 dprintk(2, KERN_DEBUG
, "allocated new item at %lx\n",
467 list_add(&tmp
->__hook
, &pcr
->free
.list
);
470 filp
->private_data
= pcr
;
471 dprintk(2, KERN_DEBUG
,
472 "Cryptodev handle initialised, %d elements in queue\n",
478 cryptodev_release(struct inode
*inode
, struct file
*filp
)
480 struct crypt_priv
*pcr
= filp
->private_data
;
481 struct todo_list_item
*item
, *item_safe
;
487 cancel_work_sync(&pcr
->cryptask
);
489 mutex_destroy(&pcr
->todo
.lock
);
490 mutex_destroy(&pcr
->done
.lock
);
491 mutex_destroy(&pcr
->free
.lock
);
493 list_splice_tail(&pcr
->todo
.list
, &pcr
->free
.list
);
494 list_splice_tail(&pcr
->done
.list
, &pcr
->free
.list
);
496 list_for_each_entry_safe(item
, item_safe
, &pcr
->free
.list
, __hook
) {
497 dprintk(2, KERN_DEBUG
, "freeing item at %lx\n",
498 (unsigned long)item
);
499 list_del(&item
->__hook
);
504 if (items_freed
!= pcr
->itemcount
) {
506 "freed %d items, but %d should exist!\n",
507 items_freed
, pcr
->itemcount
);
510 crypto_finish_all_sessions(&pcr
->fcrypt
);
512 filp
->private_data
= NULL
;
514 dprintk(2, KERN_DEBUG
,
515 "Cryptodev handle deinitialised, %d elements freed\n",
521 clonefd(struct file
*filp
)
524 ret
= get_unused_fd();
527 fd_install(ret
, filp
);
533 /* enqueue a job for asynchronous completion
536 * -EBUSY when there are no free queue slots left
537 * (and the number of slots has reached it MAX_COP_RINGSIZE)
538 * -EFAULT when there was a memory allocation error
540 static int crypto_async_run(struct crypt_priv
*pcr
, struct kernel_crypt_op
*kcop
)
542 struct todo_list_item
*item
= NULL
;
544 mutex_lock(&pcr
->free
.lock
);
545 if (likely(!list_empty(&pcr
->free
.list
))) {
546 item
= list_first_entry(&pcr
->free
.list
,
547 struct todo_list_item
, __hook
);
548 list_del(&item
->__hook
);
549 } else if (pcr
->itemcount
< MAX_COP_RINGSIZE
) {
552 mutex_unlock(&pcr
->free
.lock
);
555 mutex_unlock(&pcr
->free
.lock
);
557 if (unlikely(!item
)) {
558 item
= kzalloc(sizeof(struct todo_list_item
), GFP_KERNEL
);
561 dprintk(1, KERN_INFO
, "increased item count to %d\n",
565 memcpy(&item
->kcop
, kcop
, sizeof(struct kernel_crypt_op
));
567 mutex_lock(&pcr
->todo
.lock
);
568 list_add_tail(&item
->__hook
, &pcr
->todo
.list
);
569 mutex_unlock(&pcr
->todo
.lock
);
571 queue_work(cryptodev_wq
, &pcr
->cryptask
);
575 /* get the first completed job from the "done" queue
578 * -EBUSY if no completed jobs are ready (yet)
579 * the return value of crypto_run() otherwise */
580 static int crypto_async_fetch(struct crypt_priv
*pcr
,
581 struct kernel_crypt_op
*kcop
)
583 struct todo_list_item
*item
;
586 mutex_lock(&pcr
->done
.lock
);
587 if (list_empty(&pcr
->done
.list
)) {
588 mutex_unlock(&pcr
->done
.lock
);
591 item
= list_first_entry(&pcr
->done
.list
, struct todo_list_item
, __hook
);
592 list_del(&item
->__hook
);
593 mutex_unlock(&pcr
->done
.lock
);
595 memcpy(kcop
, &item
->kcop
, sizeof(struct kernel_crypt_op
));
596 retval
= item
->result
;
598 mutex_lock(&pcr
->free
.lock
);
599 list_add_tail(&item
->__hook
, &pcr
->free
.list
);
600 mutex_unlock(&pcr
->free
.lock
);
602 /* wake for POLLOUT */
603 wake_up_interruptible(&pcr
->user_waiter
);
608 /* this function has to be called from process context */
609 static int fill_kcop_from_cop(struct kernel_crypt_op
*kcop
, struct fcrypt
*fcr
)
611 struct crypt_op
*cop
= &kcop
->cop
;
612 struct csession
*ses_ptr
;
615 /* this also enters ses_ptr->sem */
616 ses_ptr
= crypto_get_session_by_sid(fcr
, cop
->ses
);
617 if (unlikely(!ses_ptr
)) {
618 dprintk(1, KERN_ERR
, "invalid session ID=0x%08X\n", cop
->ses
);
621 kcop
->ivlen
= cop
->iv
? ses_ptr
->cdata
.ivsize
: 0;
622 kcop
->digestsize
= 0; /* will be updated during operation */
624 crypto_put_session(ses_ptr
);
626 kcop
->task
= current
;
627 kcop
->mm
= current
->mm
;
630 rc
= copy_from_user(kcop
->iv
, cop
->iv
, kcop
->ivlen
);
633 "error copying IV (%d bytes), copy_from_user returned %d for address %lx\n",
634 kcop
->ivlen
, rc
, (unsigned long)cop
->iv
);
642 /* this function has to be called from process context */
643 static int fill_cop_from_kcop(struct kernel_crypt_op
*kcop
, struct fcrypt
*fcr
)
647 if (kcop
->digestsize
) {
648 ret
= copy_to_user(kcop
->cop
.mac
,
649 kcop
->hash_output
, kcop
->digestsize
);
653 if (kcop
->ivlen
&& kcop
->cop
.flags
& COP_FLAG_WRITE_IV
) {
654 ret
= copy_to_user(kcop
->cop
.iv
,
655 kcop
->iv
, kcop
->ivlen
);
662 static int kcop_from_user(struct kernel_crypt_op
*kcop
,
663 struct fcrypt
*fcr
, void __user
*arg
)
665 if (unlikely(copy_from_user(&kcop
->cop
, arg
, sizeof(kcop
->cop
))))
668 return fill_kcop_from_cop(kcop
, fcr
);
671 static int kcop_to_user(struct kernel_crypt_op
*kcop
,
672 struct fcrypt
*fcr
, void __user
*arg
)
676 ret
= fill_cop_from_kcop(kcop
, fcr
);
678 dprintk(1, KERN_ERR
, "Error in fill_cop_from_kcop\n");
682 if (unlikely(copy_to_user(arg
, &kcop
->cop
, sizeof(kcop
->cop
)))) {
683 dprintk(1, KERN_ERR
, "Cannot copy to userspace\n");
689 static inline void tfm_info_to_alg_info(struct alg_info
*dst
, struct crypto_tfm
*tfm
)
691 snprintf(dst
->cra_name
, CRYPTODEV_MAX_ALG_NAME
,
692 "%s", crypto_tfm_alg_name(tfm
));
693 snprintf(dst
->cra_driver_name
, CRYPTODEV_MAX_ALG_NAME
,
694 "%s", crypto_tfm_alg_driver_name(tfm
));
697 static unsigned int is_known_accelerated(struct crypto_tfm
*tfm
)
699 const char* name
= crypto_tfm_alg_driver_name(tfm
);
702 return 1; /* assume accelerated */
704 if (strstr(name
, "-talitos"))
706 else if (strncmp(name
, "mv-", 3) == 0)
708 else if (strstr(name
, "geode"))
710 else if (strstr(name
, "hifn"))
712 else if (strstr(name
, "-ixp4xx"))
714 else if (strstr(name
, "-omap"))
716 else if (strstr(name
, "-picoxcell"))
718 else if (strstr(name
, "-s5p"))
720 else if (strstr(name
, "-ppc4xx"))
722 else if (strstr(name
, "-caam"))
724 else if (strstr(name
, "-n2"))
730 static int get_session_info(struct fcrypt
*fcr
, struct session_info_op
*siop
)
732 struct csession
*ses_ptr
;
733 struct crypto_tfm
*tfm
;
735 /* this also enters ses_ptr->sem */
736 ses_ptr
= crypto_get_session_by_sid(fcr
, siop
->ses
);
737 if (unlikely(!ses_ptr
)) {
738 dprintk(1, KERN_ERR
, "invalid session ID=0x%08X\n", siop
->ses
);
744 if (ses_ptr
->cdata
.init
) {
745 if (ses_ptr
->cdata
.aead
== 0) {
746 tfm
= crypto_ablkcipher_tfm(ses_ptr
->cdata
.async
.s
);
748 tfm
= crypto_aead_tfm(ses_ptr
->cdata
.async
.as
);
750 tfm_info_to_alg_info(&siop
->cipher_info
, tfm
);
751 #ifdef CRYPTO_ALG_KERN_DRIVER_ONLY
752 if (tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_KERN_DRIVER_ONLY
)
753 siop
->flags
|= SIOP_FLAG_KERNEL_DRIVER_ONLY
;
755 if (is_known_accelerated(tfm
))
756 siop
->flags
|= SIOP_FLAG_KERNEL_DRIVER_ONLY
;
759 if (ses_ptr
->hdata
.init
) {
760 tfm
= crypto_ahash_tfm(ses_ptr
->hdata
.async
.s
);
761 tfm_info_to_alg_info(&siop
->hash_info
, tfm
);
762 #ifdef CRYPTO_ALG_KERN_DRIVER_ONLY
763 if (tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_KERN_DRIVER_ONLY
)
764 siop
->flags
|= SIOP_FLAG_KERNEL_DRIVER_ONLY
;
766 if (is_known_accelerated(tfm
))
767 siop
->flags
|= SIOP_FLAG_KERNEL_DRIVER_ONLY
;
771 siop
->alignmask
= ses_ptr
->alignmask
;
773 crypto_put_session(ses_ptr
);
778 cryptodev_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg_
)
780 void __user
*arg
= (void __user
*)arg_
;
782 struct session_op sop
;
783 struct kernel_crypt_op kcop
;
784 struct kernel_crypt_auth_op kcaop
;
785 struct crypt_priv
*pcr
= filp
->private_data
;
787 struct session_info_op siop
;
798 return put_user(0, p
);
801 ret
= put_user(fd
, p
);
808 if (unlikely(copy_from_user(&sop
, arg
, sizeof(sop
))))
811 ret
= crypto_create_session(fcr
, &sop
);
814 ret
= copy_to_user(arg
, &sop
, sizeof(sop
));
816 crypto_finish_session(fcr
, sop
.ses
);
821 ret
= get_user(ses
, (uint32_t __user
*)arg
);
824 ret
= crypto_finish_session(fcr
, ses
);
827 if (unlikely(copy_from_user(&siop
, arg
, sizeof(siop
))))
830 ret
= get_session_info(fcr
, &siop
);
833 return copy_to_user(arg
, &siop
, sizeof(siop
));
835 if (unlikely(ret
= kcop_from_user(&kcop
, fcr
, arg
))) {
836 dprintk(1, KERN_WARNING
, "Error copying from user\n");
840 ret
= crypto_run(fcr
, &kcop
);
842 dprintk(1, KERN_WARNING
, "Error in crypto_run\n");
846 return kcop_to_user(&kcop
, fcr
, arg
);
848 if (unlikely(ret
= kcaop_from_user(&kcaop
, fcr
, arg
))) {
849 dprintk(1, KERN_WARNING
, "Error copying from user\n");
853 ret
= crypto_auth_run(fcr
, &kcaop
);
855 dprintk(1, KERN_WARNING
, "Error in crypto_auth_run\n");
858 return kcaop_to_user(&kcaop
, fcr
, arg
);
860 if (unlikely(ret
= kcop_from_user(&kcop
, fcr
, arg
)))
863 return crypto_async_run(pcr
, &kcop
);
865 ret
= crypto_async_fetch(pcr
, &kcop
);
869 return kcop_to_user(&kcop
, fcr
, arg
);
875 /* compatibility code for 32bit userlands */
879 compat_to_session_op(struct compat_session_op
*compat
, struct session_op
*sop
)
881 sop
->cipher
= compat
->cipher
;
882 sop
->mac
= compat
->mac
;
883 sop
->keylen
= compat
->keylen
;
885 sop
->key
= compat_ptr(compat
->key
);
886 sop
->mackeylen
= compat
->mackeylen
;
887 sop
->mackey
= compat_ptr(compat
->mackey
);
888 sop
->ses
= compat
->ses
;
892 session_op_to_compat(struct session_op
*sop
, struct compat_session_op
*compat
)
894 compat
->cipher
= sop
->cipher
;
895 compat
->mac
= sop
->mac
;
896 compat
->keylen
= sop
->keylen
;
898 compat
->key
= ptr_to_compat(sop
->key
);
899 compat
->mackeylen
= sop
->mackeylen
;
900 compat
->mackey
= ptr_to_compat(sop
->mackey
);
901 compat
->ses
= sop
->ses
;
905 compat_to_crypt_op(struct compat_crypt_op
*compat
, struct crypt_op
*cop
)
907 cop
->ses
= compat
->ses
;
908 cop
->op
= compat
->op
;
909 cop
->flags
= compat
->flags
;
910 cop
->len
= compat
->len
;
912 cop
->src
= compat_ptr(compat
->src
);
913 cop
->dst
= compat_ptr(compat
->dst
);
914 cop
->mac
= compat_ptr(compat
->mac
);
915 cop
->iv
= compat_ptr(compat
->iv
);
919 crypt_op_to_compat(struct crypt_op
*cop
, struct compat_crypt_op
*compat
)
921 compat
->ses
= cop
->ses
;
922 compat
->op
= cop
->op
;
923 compat
->flags
= cop
->flags
;
924 compat
->len
= cop
->len
;
926 compat
->src
= ptr_to_compat(cop
->src
);
927 compat
->dst
= ptr_to_compat(cop
->dst
);
928 compat
->mac
= ptr_to_compat(cop
->mac
);
929 compat
->iv
= ptr_to_compat(cop
->iv
);
932 static int compat_kcop_from_user(struct kernel_crypt_op
*kcop
,
933 struct fcrypt
*fcr
, void __user
*arg
)
935 struct compat_crypt_op compat_cop
;
937 if (unlikely(copy_from_user(&compat_cop
, arg
, sizeof(compat_cop
))))
939 compat_to_crypt_op(&compat_cop
, &kcop
->cop
);
941 return fill_kcop_from_cop(kcop
, fcr
);
944 static int compat_kcop_to_user(struct kernel_crypt_op
*kcop
,
945 struct fcrypt
*fcr
, void __user
*arg
)
948 struct compat_crypt_op compat_cop
;
950 ret
= fill_cop_from_kcop(kcop
, fcr
);
952 dprintk(1, KERN_WARNING
, "Error in fill_cop_from_kcop\n");
955 crypt_op_to_compat(&kcop
->cop
, &compat_cop
);
957 if (unlikely(copy_to_user(arg
, &compat_cop
, sizeof(compat_cop
)))) {
958 dprintk(1, KERN_WARNING
, "Error copying to user\n");
965 cryptodev_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg_
)
967 void __user
*arg
= (void __user
*)arg_
;
968 struct crypt_priv
*pcr
= file
->private_data
;
970 struct session_op sop
;
971 struct compat_session_op compat_sop
;
972 struct kernel_crypt_op kcop
;
985 return cryptodev_ioctl(file
, cmd
, arg_
);
987 case COMPAT_CIOCGSESSION
:
988 if (unlikely(copy_from_user(&compat_sop
, arg
,
989 sizeof(compat_sop
))))
991 compat_to_session_op(&compat_sop
, &sop
);
993 ret
= crypto_create_session(fcr
, &sop
);
997 session_op_to_compat(&sop
, &compat_sop
);
998 ret
= copy_to_user(arg
, &compat_sop
, sizeof(compat_sop
));
1000 crypto_finish_session(fcr
, sop
.ses
);
1005 case COMPAT_CIOCCRYPT
:
1006 ret
= compat_kcop_from_user(&kcop
, fcr
, arg
);
1010 ret
= crypto_run(fcr
, &kcop
);
1014 return compat_kcop_to_user(&kcop
, fcr
, arg
);
1015 case COMPAT_CIOCASYNCCRYPT
:
1016 if (unlikely(ret
= compat_kcop_from_user(&kcop
, fcr
, arg
)))
1019 return crypto_async_run(pcr
, &kcop
);
1020 case COMPAT_CIOCASYNCFETCH
:
1021 ret
= crypto_async_fetch(pcr
, &kcop
);
1025 return compat_kcop_to_user(&kcop
, fcr
, arg
);
1032 #endif /* CONFIG_COMPAT */
1034 static unsigned int cryptodev_poll(struct file
*file
, poll_table
*wait
)
1036 struct crypt_priv
*pcr
= file
->private_data
;
1039 poll_wait(file
, &pcr
->user_waiter
, wait
);
1041 if (!list_empty_careful(&pcr
->done
.list
))
1042 ret
|= POLLIN
| POLLRDNORM
;
1043 if (!list_empty_careful(&pcr
->free
.list
) || pcr
->itemcount
< MAX_COP_RINGSIZE
)
1044 ret
|= POLLOUT
| POLLWRNORM
;
1049 static const struct file_operations cryptodev_fops
= {
1050 .owner
= THIS_MODULE
,
1051 .open
= cryptodev_open
,
1052 .release
= cryptodev_release
,
1053 .unlocked_ioctl
= cryptodev_ioctl
,
1054 #ifdef CONFIG_COMPAT
1055 .compat_ioctl
= cryptodev_compat_ioctl
,
1056 #endif /* CONFIG_COMPAT */
1057 .poll
= cryptodev_poll
,
1060 static struct miscdevice cryptodev
= {
1061 .minor
= MISC_DYNAMIC_MINOR
,
1063 .fops
= &cryptodev_fops
,
1064 .mode
= S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
,
1068 cryptodev_register(void)
1072 rc
= misc_register(&cryptodev
);
1074 printk(KERN_ERR PFX
"registration of /dev/crypto failed\n");
1082 cryptodev_deregister(void)
1084 misc_deregister(&cryptodev
);
1087 /* ====== Module init/exit ====== */
1088 static int __init
init_cryptodev(void)
1092 cryptodev_wq
= create_workqueue("cryptodev_queue");
1093 if (unlikely(!cryptodev_wq
)) {
1094 printk(KERN_ERR PFX
"failed to allocate the cryptodev workqueue\n");
1098 rc
= cryptodev_register();
1100 destroy_workqueue(cryptodev_wq
);
1104 printk(KERN_INFO PFX
"driver %s loaded.\n", VERSION
);
1109 static void __exit
exit_cryptodev(void)
1111 flush_workqueue(cryptodev_wq
);
1112 destroy_workqueue(cryptodev_wq
);
1114 cryptodev_deregister();
1115 printk(KERN_INFO PFX
"driver unloaded.\n");
1118 module_init(init_cryptodev
);
1119 module_exit(exit_cryptodev
);