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 Nikos Mavrogiannopoulos <nmav@gnutls.org>
7 * This file is part of linux cryptodev.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * Device /dev/crypto provides an interface for
27 * accessing kernel CryptoAPI algorithms (ciphers,
28 * hashes) from userspace programs.
30 * /dev/crypto interface was originally introduced in
31 * OpenBSD and this module attempts to keep the API.
35 #include <crypto/hash.h>
36 #include <linux/crypto.h>
38 #include <linux/highmem.h>
39 #include <linux/ioctl.h>
40 #include <linux/random.h>
41 #include <linux/syscalls.h>
42 #include <linux/pagemap.h>
43 #include <linux/poll.h>
44 #include <linux/uaccess.h>
45 #include <crypto/cryptodev.h>
46 #include <linux/scatterlist.h>
47 #include "cryptodev_int.h"
51 MODULE_AUTHOR("Nikos Mavrogiannopoulos <nmav@gnutls.org>");
52 MODULE_DESCRIPTION("CryptoDev driver");
53 MODULE_LICENSE("GPL");
55 /* ====== Compile-time config ====== */
57 /* Default (pre-allocated) and maximum size of the job queue.
58 * These are free, pending and done items all together. */
59 #define DEF_COP_RINGSIZE 16
60 #define MAX_COP_RINGSIZE 64
62 /* ====== Module parameters ====== */
64 int cryptodev_verbosity
;
65 module_param(cryptodev_verbosity
, int, 0644);
66 MODULE_PARM_DESC(cryptodev_verbosity
, "0: normal, 1: verbose, 2: debug");
68 /* ====== CryptoAPI ====== */
69 struct todo_list_item
{
70 struct list_head __hook
;
71 struct kernel_crypt_op kcop
;
76 struct list_head list
;
82 struct locked_list free
, todo
, done
;
84 struct work_struct cryptask
;
85 wait_queue_head_t user_waiter
;
88 #define FILL_SG(sg, ptr, len) \
90 (sg)->page = virt_to_page(ptr); \
91 (sg)->offset = offset_in_page(ptr); \
93 (sg)->dma_address = 0; \
96 /* cryptodev's own workqueue, keeps crypto tasks from disturbing the force */
97 static struct workqueue_struct
*cryptodev_wq
;
99 /* Prepare session for future use. */
101 crypto_create_session(struct fcrypt
*fcr
, struct session_op
*sop
)
103 struct csession
*ses_new
= NULL
, *ses_ptr
;
105 const char *alg_name
= NULL
;
106 const char *hash_name
= NULL
;
107 int hmac_mode
= 1, stream
= 0, aead
= 0;
109 /* Does the request make sense? */
110 if (unlikely(!sop
->cipher
&& !sop
->mac
)) {
111 dprintk(1, KERN_DEBUG
, "Both 'cipher' and 'mac' unset.\n");
115 switch (sop
->cipher
) {
119 alg_name
= "cbc(des)";
121 case CRYPTO_3DES_CBC
:
122 alg_name
= "cbc(des3_ede)";
125 alg_name
= "cbc(blowfish)";
128 alg_name
= "cbc(aes)";
131 alg_name
= "ecb(aes)";
133 case CRYPTO_CAMELLIA_CBC
:
134 alg_name
= "cbc(camelia)";
137 alg_name
= "ctr(aes)";
141 alg_name
= "gcm(aes)";
146 alg_name
= "ecb(cipher_null)";
150 dprintk(1, KERN_DEBUG
, "%s: bad cipher: %d\n", __func__
,
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
, "%s: bad mac: %d\n", __func__
,
208 /* Create a session and put it to the list. */
209 ses_new
= kzalloc(sizeof(*ses_new
), GFP_KERNEL
);
213 /* Set-up crypto transform. */
215 uint8_t keyp
[CRYPTO_CIPHER_MAX_KEY_LEN
];
217 if (unlikely(sop
->keylen
> CRYPTO_CIPHER_MAX_KEY_LEN
)) {
218 dprintk(1, KERN_DEBUG
,
219 "Setting key failed for %s-%zu.\n",
220 alg_name
, (size_t)sop
->keylen
*8);
225 if (unlikely(copy_from_user(keyp
, sop
->key
, sop
->keylen
))) {
230 ret
= cryptodev_cipher_init(&ses_new
->cdata
, alg_name
, keyp
,
231 sop
->keylen
, stream
, aead
);
233 dprintk(1, KERN_DEBUG
,
234 "%s: Failed to load cipher for %s\n",
241 if (hash_name
&& aead
== 0) {
242 uint8_t keyp
[CRYPTO_HMAC_MAX_KEY_LEN
];
244 if (unlikely(sop
->mackeylen
> CRYPTO_HMAC_MAX_KEY_LEN
)) {
245 dprintk(1, KERN_DEBUG
,
246 "Setting key failed for %s-%zu.\n",
247 alg_name
, (size_t)sop
->mackeylen
*8);
252 if (sop
->mackey
&& unlikely(copy_from_user(keyp
, sop
->mackey
,
258 ret
= cryptodev_hash_init(&ses_new
->hdata
, hash_name
, hmac_mode
,
259 keyp
, sop
->mackeylen
);
261 dprintk(1, KERN_DEBUG
,
262 "%s: Failed to load hash for %s\n",
263 __func__
, hash_name
);
269 ses_new
->alignmask
= max(ses_new
->cdata
.alignmask
,
270 ses_new
->hdata
.alignmask
);
271 dprintk(2, KERN_DEBUG
, "%s: got alignmask %d\n", __func__
, ses_new
->alignmask
);
273 ses_new
->array_size
= DEFAULT_PREALLOC_PAGES
;
274 dprintk(2, KERN_DEBUG
, "%s: preallocating for %d user pages\n",
275 __func__
, ses_new
->array_size
);
276 ses_new
->pages
= kzalloc(ses_new
->array_size
*
277 sizeof(struct page
*), GFP_KERNEL
);
278 ses_new
->sg
= kzalloc(ses_new
->array_size
*
279 sizeof(struct scatterlist
), GFP_KERNEL
);
280 if (ses_new
->sg
== NULL
|| ses_new
->pages
== NULL
) {
281 dprintk(0, KERN_DEBUG
, "Memory error\n");
286 /* put the new session to the list */
287 get_random_bytes(&ses_new
->sid
, sizeof(ses_new
->sid
));
288 mutex_init(&ses_new
->sem
);
290 mutex_lock(&fcr
->sem
);
292 list_for_each_entry(ses_ptr
, &fcr
->list
, entry
) {
293 /* Check for duplicate SID */
294 if (unlikely(ses_new
->sid
== ses_ptr
->sid
)) {
295 get_random_bytes(&ses_new
->sid
, sizeof(ses_new
->sid
));
296 /* Unless we have a broken RNG this
297 shouldn't loop forever... ;-) */
302 list_add(&ses_new
->entry
, &fcr
->list
);
303 mutex_unlock(&fcr
->sem
);
305 /* Fill in some values for the user. */
306 sop
->ses
= ses_new
->sid
;
311 cryptodev_cipher_deinit(&ses_new
->cdata
);
313 kfree(ses_new
->pages
);
321 /* Everything that needs to be done when remowing a session. */
323 crypto_destroy_session(struct csession
*ses_ptr
)
325 if (!mutex_trylock(&ses_ptr
->sem
)) {
326 dprintk(2, KERN_DEBUG
, "Waiting for semaphore of sid=0x%08X\n",
328 mutex_lock(&ses_ptr
->sem
);
330 dprintk(2, KERN_DEBUG
, "Removed session 0x%08X\n", ses_ptr
->sid
);
331 cryptodev_cipher_deinit(&ses_ptr
->cdata
);
332 cryptodev_hash_deinit(&ses_ptr
->hdata
);
333 dprintk(2, KERN_DEBUG
, "%s: freeing space for %d user pages\n",
334 __func__
, ses_ptr
->array_size
);
335 kfree(ses_ptr
->pages
);
337 mutex_unlock(&ses_ptr
->sem
);
341 /* Look up a session by ID and remove. */
343 crypto_finish_session(struct fcrypt
*fcr
, uint32_t sid
)
345 struct csession
*tmp
, *ses_ptr
;
346 struct list_head
*head
;
349 mutex_lock(&fcr
->sem
);
351 list_for_each_entry_safe(ses_ptr
, tmp
, head
, entry
) {
352 if (ses_ptr
->sid
== sid
) {
353 list_del(&ses_ptr
->entry
);
354 crypto_destroy_session(ses_ptr
);
359 if (unlikely(!ses_ptr
)) {
360 dprintk(1, KERN_ERR
, "Session with sid=0x%08X not found!\n",
364 mutex_unlock(&fcr
->sem
);
369 /* Remove all sessions when closing the file */
371 crypto_finish_all_sessions(struct fcrypt
*fcr
)
373 struct csession
*tmp
, *ses_ptr
;
374 struct list_head
*head
;
376 mutex_lock(&fcr
->sem
);
379 list_for_each_entry_safe(ses_ptr
, tmp
, head
, entry
) {
380 list_del(&ses_ptr
->entry
);
381 crypto_destroy_session(ses_ptr
);
383 mutex_unlock(&fcr
->sem
);
388 /* Look up session by session ID. The returned session is locked. */
390 crypto_get_session_by_sid(struct fcrypt
*fcr
, uint32_t sid
)
392 struct csession
*ses_ptr
, *retval
= 0;
394 mutex_lock(&fcr
->sem
);
395 list_for_each_entry(ses_ptr
, &fcr
->list
, entry
) {
396 if (ses_ptr
->sid
== sid
) {
397 mutex_lock(&ses_ptr
->sem
);
402 mutex_unlock(&fcr
->sem
);
407 static void cryptask_routine(struct work_struct
*work
)
409 struct crypt_priv
*pcr
= container_of(work
, struct crypt_priv
, cryptask
);
410 struct todo_list_item
*item
;
413 /* fetch all pending jobs into the temporary list */
414 mutex_lock(&pcr
->todo
.lock
);
415 list_cut_position(&tmp
, &pcr
->todo
.list
, pcr
->todo
.list
.prev
);
416 mutex_unlock(&pcr
->todo
.lock
);
418 /* handle each job locklessly */
419 list_for_each_entry(item
, &tmp
, __hook
) {
420 item
->result
= crypto_run(&pcr
->fcrypt
, &item
->kcop
);
421 if (unlikely(item
->result
))
422 dprintk(0, KERN_ERR
, "%s: crypto_run() failed: %d\n",
423 __func__
, item
->result
);
426 /* push all handled jobs to the done list at once */
427 mutex_lock(&pcr
->done
.lock
);
428 list_splice_tail(&tmp
, &pcr
->done
.list
);
429 mutex_unlock(&pcr
->done
.lock
);
431 /* wake for POLLIN */
432 wake_up_interruptible(&pcr
->user_waiter
);
435 /* ====== /dev/crypto ====== */
438 cryptodev_open(struct inode
*inode
, struct file
*filp
)
440 struct todo_list_item
*tmp
;
441 struct crypt_priv
*pcr
;
444 pcr
= kmalloc(sizeof(*pcr
), GFP_KERNEL
);
448 memset(pcr
, 0, sizeof(*pcr
));
449 mutex_init(&pcr
->fcrypt
.sem
);
450 INIT_LIST_HEAD(&pcr
->fcrypt
.list
);
452 INIT_LIST_HEAD(&pcr
->free
.list
);
453 INIT_LIST_HEAD(&pcr
->todo
.list
);
454 INIT_LIST_HEAD(&pcr
->done
.list
);
455 INIT_WORK(&pcr
->cryptask
, cryptask_routine
);
456 mutex_init(&pcr
->free
.lock
);
457 mutex_init(&pcr
->todo
.lock
);
458 mutex_init(&pcr
->done
.lock
);
459 init_waitqueue_head(&pcr
->user_waiter
);
461 for (i
= 0; i
< DEF_COP_RINGSIZE
; i
++) {
462 tmp
= kzalloc(sizeof(struct todo_list_item
), GFP_KERNEL
);
464 dprintk(2, KERN_DEBUG
, "%s: allocated new item at %lx\n",
465 __func__
, (unsigned long)tmp
);
466 list_add(&tmp
->__hook
, &pcr
->free
.list
);
469 filp
->private_data
= pcr
;
470 dprintk(2, KERN_DEBUG
,
471 "Cryptodev handle initialised, %d elements in queue\n",
477 cryptodev_release(struct inode
*inode
, struct file
*filp
)
479 struct crypt_priv
*pcr
= filp
->private_data
;
480 struct todo_list_item
*item
, *item_safe
;
486 cancel_work_sync(&pcr
->cryptask
);
488 mutex_destroy(&pcr
->todo
.lock
);
489 mutex_destroy(&pcr
->done
.lock
);
490 mutex_destroy(&pcr
->free
.lock
);
492 list_splice_tail(&pcr
->todo
.list
, &pcr
->free
.list
);
493 list_splice_tail(&pcr
->done
.list
, &pcr
->free
.list
);
495 list_for_each_entry_safe(item
, item_safe
, &pcr
->free
.list
, __hook
) {
496 dprintk(2, KERN_DEBUG
, "%s: freeing item at %lx\n",
497 __func__
, (unsigned long)item
);
498 list_del(&item
->__hook
);
503 if (items_freed
!= pcr
->itemcount
) {
505 "%s: freed %d items, but %d should exist!\n",
506 __func__
, items_freed
, pcr
->itemcount
);
509 crypto_finish_all_sessions(&pcr
->fcrypt
);
511 filp
->private_data
= NULL
;
513 dprintk(2, KERN_DEBUG
,
514 "Cryptodev handle deinitialised, %d elements freed\n",
520 clonefd(struct file
*filp
)
523 ret
= get_unused_fd();
526 fd_install(ret
, filp
);
532 /* enqueue a job for asynchronous completion
535 * -EBUSY when there are no free queue slots left
536 * (and the number of slots has reached it MAX_COP_RINGSIZE)
537 * -EFAULT when there was a memory allocation error
539 static int crypto_async_run(struct crypt_priv
*pcr
, struct kernel_crypt_op
*kcop
)
541 struct todo_list_item
*item
= NULL
;
543 mutex_lock(&pcr
->free
.lock
);
544 if (likely(!list_empty(&pcr
->free
.list
))) {
545 item
= list_first_entry(&pcr
->free
.list
,
546 struct todo_list_item
, __hook
);
547 list_del(&item
->__hook
);
548 } else if (pcr
->itemcount
< MAX_COP_RINGSIZE
) {
551 mutex_unlock(&pcr
->free
.lock
);
554 mutex_unlock(&pcr
->free
.lock
);
556 if (unlikely(!item
)) {
557 item
= kzalloc(sizeof(struct todo_list_item
), GFP_KERNEL
);
560 dprintk(1, KERN_INFO
, "%s: increased item count to %d\n",
561 __func__
, pcr
->itemcount
);
564 memcpy(&item
->kcop
, kcop
, sizeof(struct kernel_crypt_op
));
566 mutex_lock(&pcr
->todo
.lock
);
567 list_add_tail(&item
->__hook
, &pcr
->todo
.list
);
568 mutex_unlock(&pcr
->todo
.lock
);
570 queue_work(cryptodev_wq
, &pcr
->cryptask
);
574 /* get the first completed job from the "done" queue
577 * -EBUSY if no completed jobs are ready (yet)
578 * the return value of crypto_run() otherwise */
579 static int crypto_async_fetch(struct crypt_priv
*pcr
,
580 struct kernel_crypt_op
*kcop
)
582 struct todo_list_item
*item
;
585 mutex_lock(&pcr
->done
.lock
);
586 if (list_empty(&pcr
->done
.list
)) {
587 mutex_unlock(&pcr
->done
.lock
);
590 item
= list_first_entry(&pcr
->done
.list
, struct todo_list_item
, __hook
);
591 list_del(&item
->__hook
);
592 mutex_unlock(&pcr
->done
.lock
);
594 memcpy(kcop
, &item
->kcop
, sizeof(struct kernel_crypt_op
));
595 retval
= item
->result
;
597 mutex_lock(&pcr
->free
.lock
);
598 list_add_tail(&item
->__hook
, &pcr
->free
.list
);
599 mutex_unlock(&pcr
->free
.lock
);
601 /* wake for POLLOUT */
602 wake_up_interruptible(&pcr
->user_waiter
);
607 /* this function has to be called from process context */
608 static int fill_kcop_from_cop(struct kernel_crypt_op
*kcop
, struct fcrypt
*fcr
)
610 struct crypt_op
*cop
= &kcop
->cop
;
611 struct csession
*ses_ptr
;
614 /* this also enters ses_ptr->sem */
615 ses_ptr
= crypto_get_session_by_sid(fcr
, cop
->ses
);
616 if (unlikely(!ses_ptr
)) {
617 dprintk(1, KERN_ERR
, "invalid session ID=0x%08X\n", cop
->ses
);
620 kcop
->ivlen
= cop
->iv
? ses_ptr
->cdata
.ivsize
: 0;
621 kcop
->digestsize
= 0; /* will be updated during operation */
623 crypto_put_session(ses_ptr
);
625 kcop
->task
= current
;
626 kcop
->mm
= current
->mm
;
629 rc
= copy_from_user(kcop
->iv
, cop
->iv
, kcop
->ivlen
);
632 "error copying IV (%d bytes), copy_from_user returned %d for address %lx\n",
633 kcop
->ivlen
, rc
, (unsigned long)cop
->iv
);
641 /* this function has to be called from process context */
642 static int fill_cop_from_kcop(struct kernel_crypt_op
*kcop
, struct fcrypt
*fcr
)
646 if (kcop
->digestsize
) {
647 ret
= copy_to_user(kcop
->cop
.mac
,
648 kcop
->hash_output
, kcop
->digestsize
);
652 if (kcop
->ivlen
&& kcop
->cop
.flags
& COP_FLAG_WRITE_IV
) {
653 ret
= copy_to_user(kcop
->cop
.iv
,
654 kcop
->iv
, kcop
->ivlen
);
661 static int kcop_from_user(struct kernel_crypt_op
*kcop
,
662 struct fcrypt
*fcr
, void __user
*arg
)
664 if (unlikely(copy_from_user(&kcop
->cop
, arg
, sizeof(kcop
->cop
))))
667 return fill_kcop_from_cop(kcop
, fcr
);
670 static int kcop_to_user(struct kernel_crypt_op
*kcop
,
671 struct fcrypt
*fcr
, void __user
*arg
)
675 ret
= fill_cop_from_kcop(kcop
, fcr
);
679 if (unlikely(copy_to_user(arg
, &kcop
->cop
, sizeof(kcop
->cop
))))
684 static inline void tfm_info_to_alg_info(struct alg_info
*dst
, struct crypto_tfm
*tfm
)
686 snprintf(dst
->cra_name
, CRYPTODEV_MAX_ALG_NAME
,
687 "%s", crypto_tfm_alg_name(tfm
));
688 snprintf(dst
->cra_driver_name
, CRYPTODEV_MAX_ALG_NAME
,
689 "%s", crypto_tfm_alg_driver_name(tfm
));
692 static int get_session_info(struct fcrypt
*fcr
, struct session_info_op
*siop
)
694 struct csession
*ses_ptr
;
696 /* this also enters ses_ptr->sem */
697 ses_ptr
= crypto_get_session_by_sid(fcr
, siop
->ses
);
698 if (unlikely(!ses_ptr
)) {
699 dprintk(1, KERN_ERR
, "invalid session ID=0x%08X\n", siop
->ses
);
703 if (ses_ptr
->cdata
.init
) {
704 tfm_info_to_alg_info(&siop
->cipher_info
,
705 crypto_ablkcipher_tfm(ses_ptr
->cdata
.async
.s
));
707 if (ses_ptr
->hdata
.init
) {
708 tfm_info_to_alg_info(&siop
->hash_info
,
709 crypto_ahash_tfm(ses_ptr
->hdata
.async
.s
));
712 siop
->alignmask
= ses_ptr
->alignmask
;
714 crypto_put_session(ses_ptr
);
719 cryptodev_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg_
)
721 void __user
*arg
= (void __user
*)arg_
;
723 struct session_op sop
;
724 struct kernel_crypt_op kcop
;
725 struct kernel_crypt_auth_op kcaop
;
726 struct crypt_priv
*pcr
= filp
->private_data
;
728 struct session_info_op siop
;
739 return put_user(0, p
);
742 ret
= put_user(fd
, p
);
749 if (unlikely(copy_from_user(&sop
, arg
, sizeof(sop
))))
752 ret
= crypto_create_session(fcr
, &sop
);
755 ret
= copy_to_user(arg
, &sop
, sizeof(sop
));
757 crypto_finish_session(fcr
, sop
.ses
);
762 ret
= get_user(ses
, (uint32_t __user
*)arg
);
765 ret
= crypto_finish_session(fcr
, ses
);
768 if (unlikely(copy_from_user(&siop
, arg
, sizeof(siop
))))
771 ret
= get_session_info(fcr
, &siop
);
774 return copy_to_user(arg
, &siop
, sizeof(siop
));
776 if (unlikely(ret
= kcop_from_user(&kcop
, fcr
, arg
))) {
777 dprintk(1, KERN_WARNING
, "Error copying from user");
781 ret
= crypto_run(fcr
, &kcop
);
783 dprintk(1, KERN_WARNING
, "Error in crypto_run");
787 return kcop_to_user(&kcop
, fcr
, arg
);
789 if (unlikely(ret
= kcaop_from_user(&kcaop
, fcr
, arg
))) {
790 dprintk(1, KERN_WARNING
, "Error copying from user");
794 ret
= crypto_auth_run(fcr
, &kcaop
);
796 dprintk(1, KERN_WARNING
, "Error in crypto_auth_run");
799 return kcaop_to_user(&kcaop
, fcr
, arg
);
801 if (unlikely(ret
= kcop_from_user(&kcop
, fcr
, arg
)))
804 return crypto_async_run(pcr
, &kcop
);
806 ret
= crypto_async_fetch(pcr
, &kcop
);
810 return kcop_to_user(&kcop
, fcr
, arg
);
816 /* compatibility code for 32bit userlands */
820 compat_to_session_op(struct compat_session_op
*compat
, struct session_op
*sop
)
822 sop
->cipher
= compat
->cipher
;
823 sop
->mac
= compat
->mac
;
824 sop
->keylen
= compat
->keylen
;
826 sop
->key
= compat_ptr(compat
->key
);
827 sop
->mackeylen
= compat
->mackeylen
;
828 sop
->mackey
= compat_ptr(compat
->mackey
);
829 sop
->ses
= compat
->ses
;
833 session_op_to_compat(struct session_op
*sop
, struct compat_session_op
*compat
)
835 compat
->cipher
= sop
->cipher
;
836 compat
->mac
= sop
->mac
;
837 compat
->keylen
= sop
->keylen
;
839 compat
->key
= ptr_to_compat(sop
->key
);
840 compat
->mackeylen
= sop
->mackeylen
;
841 compat
->mackey
= ptr_to_compat(sop
->mackey
);
842 compat
->ses
= sop
->ses
;
846 compat_to_crypt_op(struct compat_crypt_op
*compat
, struct crypt_op
*cop
)
848 cop
->ses
= compat
->ses
;
849 cop
->op
= compat
->op
;
850 cop
->flags
= compat
->flags
;
851 cop
->len
= compat
->len
;
853 cop
->src
= compat_ptr(compat
->src
);
854 cop
->dst
= compat_ptr(compat
->dst
);
855 cop
->mac
= compat_ptr(compat
->mac
);
856 cop
->iv
= compat_ptr(compat
->iv
);
860 crypt_op_to_compat(struct crypt_op
*cop
, struct compat_crypt_op
*compat
)
862 compat
->ses
= cop
->ses
;
863 compat
->op
= cop
->op
;
864 compat
->flags
= cop
->flags
;
865 compat
->len
= cop
->len
;
867 compat
->src
= ptr_to_compat(cop
->src
);
868 compat
->dst
= ptr_to_compat(cop
->dst
);
869 compat
->mac
= ptr_to_compat(cop
->mac
);
870 compat
->iv
= ptr_to_compat(cop
->iv
);
873 static int compat_kcop_from_user(struct kernel_crypt_op
*kcop
,
874 struct fcrypt
*fcr
, void __user
*arg
)
876 struct compat_crypt_op compat_cop
;
878 if (unlikely(copy_from_user(&compat_cop
, arg
, sizeof(compat_cop
))))
880 compat_to_crypt_op(&compat_cop
, &kcop
->cop
);
882 return fill_kcop_from_cop(kcop
, fcr
);
885 static int compat_kcop_to_user(struct kernel_crypt_op
*kcop
,
886 struct fcrypt
*fcr
, void __user
*arg
)
889 struct compat_crypt_op compat_cop
;
891 ret
= fill_cop_from_kcop(kcop
, fcr
);
893 dprintk(1, KERN_WARNING
, "Error in fill_cop_from_kcop");
896 crypt_op_to_compat(&kcop
->cop
, &compat_cop
);
898 if (unlikely(copy_to_user(arg
, &compat_cop
, sizeof(compat_cop
)))) {
899 dprintk(1, KERN_WARNING
, "Error copying to user");
906 cryptodev_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg_
)
908 void __user
*arg
= (void __user
*)arg_
;
909 struct crypt_priv
*pcr
= file
->private_data
;
911 struct session_op sop
;
912 struct compat_session_op compat_sop
;
913 struct kernel_crypt_op kcop
;
926 return cryptodev_ioctl(file
, cmd
, arg_
);
928 case COMPAT_CIOCGSESSION
:
929 if (unlikely(copy_from_user(&compat_sop
, arg
,
930 sizeof(compat_sop
))))
932 compat_to_session_op(&compat_sop
, &sop
);
934 ret
= crypto_create_session(fcr
, &sop
);
938 session_op_to_compat(&sop
, &compat_sop
);
939 ret
= copy_to_user(arg
, &compat_sop
, sizeof(compat_sop
));
941 crypto_finish_session(fcr
, sop
.ses
);
946 case COMPAT_CIOCCRYPT
:
947 ret
= compat_kcop_from_user(&kcop
, fcr
, arg
);
951 ret
= crypto_run(fcr
, &kcop
);
955 return compat_kcop_to_user(&kcop
, fcr
, arg
);
956 case COMPAT_CIOCASYNCCRYPT
:
957 if (unlikely(ret
= compat_kcop_from_user(&kcop
, fcr
, arg
)))
960 return crypto_async_run(pcr
, &kcop
);
961 case COMPAT_CIOCASYNCFETCH
:
962 ret
= crypto_async_fetch(pcr
, &kcop
);
966 return compat_kcop_to_user(&kcop
, fcr
, arg
);
973 #endif /* CONFIG_COMPAT */
975 static unsigned int cryptodev_poll(struct file
*file
, poll_table
*wait
)
977 struct crypt_priv
*pcr
= file
->private_data
;
980 poll_wait(file
, &pcr
->user_waiter
, wait
);
982 if (!list_empty_careful(&pcr
->done
.list
))
983 ret
|= POLLIN
| POLLRDNORM
;
984 if (!list_empty_careful(&pcr
->free
.list
) || pcr
->itemcount
< MAX_COP_RINGSIZE
)
985 ret
|= POLLOUT
| POLLWRNORM
;
990 static const struct file_operations cryptodev_fops
= {
991 .owner
= THIS_MODULE
,
992 .open
= cryptodev_open
,
993 .release
= cryptodev_release
,
994 .unlocked_ioctl
= cryptodev_ioctl
,
996 .compat_ioctl
= cryptodev_compat_ioctl
,
997 #endif /* CONFIG_COMPAT */
998 .poll
= cryptodev_poll
,
1001 static struct miscdevice cryptodev
= {
1002 .minor
= MISC_DYNAMIC_MINOR
,
1004 .fops
= &cryptodev_fops
,
1008 cryptodev_register(void)
1012 rc
= misc_register(&cryptodev
);
1014 printk(KERN_ERR PFX
"registration of /dev/crypto failed\n");
1022 cryptodev_deregister(void)
1024 misc_deregister(&cryptodev
);
1027 /* ====== Module init/exit ====== */
1028 static int __init
init_cryptodev(void)
1032 cryptodev_wq
= create_workqueue("cryptodev_queue");
1033 if (unlikely(!cryptodev_wq
)) {
1034 printk(KERN_ERR PFX
"failed to allocate the cryptodev workqueue\n");
1038 rc
= cryptodev_register();
1040 destroy_workqueue(cryptodev_wq
);
1044 printk(KERN_INFO PFX
"driver %s loaded.\n", VERSION
);
1049 static void __exit
exit_cryptodev(void)
1051 flush_workqueue(cryptodev_wq
);
1052 destroy_workqueue(cryptodev_wq
);
1054 cryptodev_deregister();
1055 printk(KERN_INFO PFX
"driver unloaded.\n");
1058 module_init(init_cryptodev
);
1059 module_exit(exit_cryptodev
);