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"
50 MODULE_AUTHOR("Nikos Mavrogiannopoulos <nmav@gnutls.org>");
51 MODULE_DESCRIPTION("CryptoDev driver");
52 MODULE_LICENSE("GPL");
54 /* ====== Compile-time config ====== */
56 #define CRYPTODEV_STATS
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 #ifdef CRYPTODEV_STATS
70 static int enable_stats
;
71 module_param(enable_stats
, int, 0644);
72 MODULE_PARM_DESC(enable_stats
, "collect statictics about cryptodev usage");
75 /* ====== CryptoAPI ====== */
77 struct list_head list
;
81 struct todo_list_item
{
82 struct list_head __hook
;
83 struct kernel_crypt_op kcop
;
88 struct list_head list
;
94 struct locked_list free
, todo
, done
;
96 struct work_struct cryptask
;
97 wait_queue_head_t user_waiter
;
100 #define FILL_SG(sg, ptr, len) \
102 (sg)->page = virt_to_page(ptr); \
103 (sg)->offset = offset_in_page(ptr); \
104 (sg)->length = len; \
105 (sg)->dma_address = 0; \
109 struct list_head entry
;
111 struct cipher_data cdata
;
112 struct hash_data hdata
;
115 #ifdef CRYPTODEV_STATS
116 #if !((COP_ENCRYPT < 2) && (COP_DECRYPT < 2))
117 #error Struct csession.stat uses COP_{ENCRYPT,DECRYPT} as indices. Do something!
119 unsigned long long stat
[2];
120 size_t stat_max_size
, stat_count
;
124 struct scatterlist
*sg
;
127 /* cryptodev's own workqueue, keeps crypto tasks from disturbing the force */
128 static struct workqueue_struct
*cryptodev_wq
;
130 /* Prepare session for future use. */
132 crypto_create_session(struct fcrypt
*fcr
, struct session_op
*sop
)
134 struct csession
*ses_new
= NULL
, *ses_ptr
;
136 const char *alg_name
= NULL
;
137 const char *hash_name
= NULL
;
140 /* Does the request make sense? */
141 if (unlikely(!sop
->cipher
&& !sop
->mac
)) {
142 dprintk(1, KERN_DEBUG
, "Both 'cipher' and 'mac' unset.\n");
146 switch (sop
->cipher
) {
150 alg_name
= "cbc(des)";
152 case CRYPTO_3DES_CBC
:
153 alg_name
= "cbc(des3_ede)";
156 alg_name
= "cbc(blowfish)";
159 alg_name
= "cbc(aes)";
162 alg_name
= "ecb(aes)";
164 case CRYPTO_CAMELLIA_CBC
:
165 alg_name
= "cbc(camelia)";
168 alg_name
= "ctr(aes)";
171 alg_name
= "ecb(cipher_null)";
174 dprintk(1, KERN_DEBUG
, "%s: bad cipher: %d\n", __func__
,
182 case CRYPTO_MD5_HMAC
:
183 hash_name
= "hmac(md5)";
185 case CRYPTO_RIPEMD160_HMAC
:
186 hash_name
= "hmac(rmd160)";
188 case CRYPTO_SHA1_HMAC
:
189 hash_name
= "hmac(sha1)";
191 case CRYPTO_SHA2_256_HMAC
:
192 hash_name
= "hmac(sha256)";
194 case CRYPTO_SHA2_384_HMAC
:
195 hash_name
= "hmac(sha384)";
197 case CRYPTO_SHA2_512_HMAC
:
198 hash_name
= "hmac(sha512)";
206 case CRYPTO_RIPEMD160
:
207 hash_name
= "rmd160";
214 case CRYPTO_SHA2_256
:
215 hash_name
= "sha256";
218 case CRYPTO_SHA2_384
:
219 hash_name
= "sha384";
222 case CRYPTO_SHA2_512
:
223 hash_name
= "sha512";
228 dprintk(1, KERN_DEBUG
, "%s: bad mac: %d\n", __func__
,
233 /* Create a session and put it to the list. */
234 ses_new
= kzalloc(sizeof(*ses_new
), GFP_KERNEL
);
238 /* Set-up crypto transform. */
240 uint8_t keyp
[CRYPTO_CIPHER_MAX_KEY_LEN
];
242 if (unlikely(sop
->keylen
> CRYPTO_CIPHER_MAX_KEY_LEN
)) {
243 dprintk(1, KERN_DEBUG
,
244 "Setting key failed for %s-%zu.\n",
245 alg_name
, (size_t)sop
->keylen
*8);
250 if (unlikely(copy_from_user(keyp
, sop
->key
, sop
->keylen
))) {
255 ret
= cryptodev_cipher_init(&ses_new
->cdata
, alg_name
, keyp
,
258 dprintk(1, KERN_DEBUG
,
259 "%s: Failed to load cipher for %s\n",
267 uint8_t keyp
[CRYPTO_HMAC_MAX_KEY_LEN
];
269 if (unlikely(sop
->mackeylen
> CRYPTO_HMAC_MAX_KEY_LEN
)) {
270 dprintk(1, KERN_DEBUG
,
271 "Setting key failed for %s-%zu.\n",
272 alg_name
, (size_t)sop
->mackeylen
*8);
277 if (sop
->mackey
&& unlikely(copy_from_user(keyp
, sop
->mackey
,
283 ret
= cryptodev_hash_init(&ses_new
->hdata
, hash_name
, hmac_mode
,
284 keyp
, sop
->mackeylen
);
286 dprintk(1, KERN_DEBUG
,
287 "%s: Failed to load hash for %s\n",
288 __func__
, hash_name
);
294 ses_new
->alignmask
= max(ses_new
->cdata
.alignmask
,
295 ses_new
->hdata
.alignmask
);
296 dprintk(2, KERN_DEBUG
, "%s: got alignmask %d\n", __func__
, ses_new
->alignmask
);
298 ses_new
->array_size
= DEFAULT_PREALLOC_PAGES
;
299 dprintk(2, KERN_DEBUG
, "%s: preallocating for %d user pages\n",
300 __func__
, ses_new
->array_size
);
301 ses_new
->pages
= kzalloc(ses_new
->array_size
*
302 sizeof(struct page
*), GFP_KERNEL
);
303 ses_new
->sg
= kzalloc(ses_new
->array_size
*
304 sizeof(struct scatterlist
), GFP_KERNEL
);
305 if (ses_new
->sg
== NULL
|| ses_new
->pages
== NULL
) {
306 dprintk(0, KERN_DEBUG
, "Memory error\n");
311 /* put the new session to the list */
312 get_random_bytes(&ses_new
->sid
, sizeof(ses_new
->sid
));
313 mutex_init(&ses_new
->sem
);
315 mutex_lock(&fcr
->sem
);
317 list_for_each_entry(ses_ptr
, &fcr
->list
, entry
) {
318 /* Check for duplicate SID */
319 if (unlikely(ses_new
->sid
== ses_ptr
->sid
)) {
320 get_random_bytes(&ses_new
->sid
, sizeof(ses_new
->sid
));
321 /* Unless we have a broken RNG this
322 shouldn't loop forever... ;-) */
327 list_add(&ses_new
->entry
, &fcr
->list
);
328 mutex_unlock(&fcr
->sem
);
330 /* Fill in some values for the user. */
331 sop
->ses
= ses_new
->sid
;
336 cryptodev_cipher_deinit(&ses_new
->cdata
);
338 kfree(ses_new
->pages
);
346 /* Everything that needs to be done when remowing a session. */
348 crypto_destroy_session(struct csession
*ses_ptr
)
350 if (!mutex_trylock(&ses_ptr
->sem
)) {
351 dprintk(2, KERN_DEBUG
, "Waiting for semaphore of sid=0x%08X\n",
353 mutex_lock(&ses_ptr
->sem
);
355 dprintk(2, KERN_DEBUG
, "Removed session 0x%08X\n", ses_ptr
->sid
);
356 #if defined(CRYPTODEV_STATS)
358 dprintk(2, KERN_DEBUG
,
359 "Usage in Bytes: enc=%llu, dec=%llu, "
360 "max=%zu, avg=%lu, cnt=%zu\n",
361 ses_ptr
->stat
[COP_ENCRYPT
], ses_ptr
->stat
[COP_DECRYPT
],
362 ses_ptr
->stat_max_size
, ses_ptr
->stat_count
> 0
363 ? ((unsigned long)(ses_ptr
->stat
[COP_ENCRYPT
]+
364 ses_ptr
->stat
[COP_DECRYPT
]) /
365 ses_ptr
->stat_count
) : 0,
366 ses_ptr
->stat_count
);
368 cryptodev_cipher_deinit(&ses_ptr
->cdata
);
369 cryptodev_hash_deinit(&ses_ptr
->hdata
);
370 dprintk(2, KERN_DEBUG
, "%s: freeing space for %d user pages\n",
371 __func__
, ses_ptr
->array_size
);
372 kfree(ses_ptr
->pages
);
374 mutex_unlock(&ses_ptr
->sem
);
378 /* Look up a session by ID and remove. */
380 crypto_finish_session(struct fcrypt
*fcr
, uint32_t sid
)
382 struct csession
*tmp
, *ses_ptr
;
383 struct list_head
*head
;
386 mutex_lock(&fcr
->sem
);
388 list_for_each_entry_safe(ses_ptr
, tmp
, head
, entry
) {
389 if (ses_ptr
->sid
== sid
) {
390 list_del(&ses_ptr
->entry
);
391 crypto_destroy_session(ses_ptr
);
396 if (unlikely(!ses_ptr
)) {
397 dprintk(1, KERN_ERR
, "Session with sid=0x%08X not found!\n",
401 mutex_unlock(&fcr
->sem
);
406 /* Remove all sessions when closing the file */
408 crypto_finish_all_sessions(struct fcrypt
*fcr
)
410 struct csession
*tmp
, *ses_ptr
;
411 struct list_head
*head
;
413 mutex_lock(&fcr
->sem
);
416 list_for_each_entry_safe(ses_ptr
, tmp
, head
, entry
) {
417 list_del(&ses_ptr
->entry
);
418 crypto_destroy_session(ses_ptr
);
420 mutex_unlock(&fcr
->sem
);
425 /* Look up session by session ID. The returned session is locked. */
426 static struct csession
*
427 crypto_get_session_by_sid(struct fcrypt
*fcr
, uint32_t sid
)
429 struct csession
*ses_ptr
, *retval
= 0;
431 mutex_lock(&fcr
->sem
);
432 list_for_each_entry(ses_ptr
, &fcr
->list
, entry
) {
433 if (ses_ptr
->sid
== sid
) {
434 mutex_lock(&ses_ptr
->sem
);
439 mutex_unlock(&fcr
->sem
);
445 hash_n_crypt(struct csession
*ses_ptr
, struct crypt_op
*cop
,
446 struct scatterlist
*src_sg
, struct scatterlist
*dst_sg
,
451 /* Always hash before encryption and after decryption. Maybe
452 * we should introduce a flag to switch... TBD later on.
454 if (cop
->op
== COP_ENCRYPT
) {
455 if (ses_ptr
->hdata
.init
!= 0) {
456 ret
= cryptodev_hash_update(&ses_ptr
->hdata
,
461 if (ses_ptr
->cdata
.init
!= 0) {
462 ret
= cryptodev_cipher_encrypt(&ses_ptr
->cdata
,
463 src_sg
, dst_sg
, len
);
469 if (ses_ptr
->cdata
.init
!= 0) {
470 ret
= cryptodev_cipher_decrypt(&ses_ptr
->cdata
,
471 src_sg
, dst_sg
, len
);
477 if (ses_ptr
->hdata
.init
!= 0) {
478 ret
= cryptodev_hash_update(&ses_ptr
->hdata
,
486 dprintk(0, KERN_ERR
, "CryptoAPI failure: %d\n", ret
);
491 /* This is the main crypto function - feed it with plaintext
492 and get a ciphertext (or vice versa :-) */
494 __crypto_run_std(struct csession
*ses_ptr
, struct crypt_op
*cop
)
497 char __user
*src
, *dst
;
498 struct scatterlist sg
;
499 size_t nbytes
, bufsize
;
503 data
= (char *)__get_free_page(GFP_KERNEL
);
508 bufsize
= PAGE_SIZE
< nbytes
? PAGE_SIZE
: nbytes
;
514 size_t current_len
= nbytes
> bufsize
? bufsize
: nbytes
;
516 if (unlikely(copy_from_user(data
, src
, current_len
))) {
521 sg_init_one(&sg
, data
, current_len
);
523 ret
= hash_n_crypt(ses_ptr
, cop
, &sg
, &sg
, current_len
);
528 if (ses_ptr
->cdata
.init
!= 0) {
529 if (unlikely(copy_to_user(dst
, data
, current_len
))) {
536 nbytes
-= current_len
;
540 free_page((unsigned long)data
);
544 void release_user_pages(struct page
**pg
, int pagecount
)
546 while (pagecount
--) {
547 if (!PageReserved(pg
[pagecount
]))
548 SetPageDirty(pg
[pagecount
]);
549 page_cache_release(pg
[pagecount
]);
553 /* offset of buf in it's first page */
554 #define PAGEOFFSET(buf) ((unsigned long)buf & ~PAGE_MASK)
556 /* fetch the pages addr resides in into pg and initialise sg with them */
557 int __get_userbuf(uint8_t __user
*addr
, uint32_t len
, int write
,
558 int pgcount
, struct page
**pg
, struct scatterlist
*sg
,
559 struct task_struct
*task
, struct mm_struct
*mm
)
561 int ret
, pglen
, i
= 0;
562 struct scatterlist
*sgp
;
564 down_write(&mm
->mmap_sem
);
565 ret
= get_user_pages(task
, mm
,
566 (unsigned long)addr
, pgcount
, write
, 0, pg
, NULL
);
567 up_write(&mm
->mmap_sem
);
571 sg_init_table(sg
, pgcount
);
573 pglen
= min((ptrdiff_t)(PAGE_SIZE
- PAGEOFFSET(addr
)), (ptrdiff_t)len
);
574 sg_set_page(sg
, pg
[i
++], pglen
, PAGEOFFSET(addr
));
577 for (sgp
= sg_next(sg
); len
; sgp
= sg_next(sgp
)) {
578 pglen
= min((uint32_t)PAGE_SIZE
, len
);
579 sg_set_page(sgp
, pg
[i
++], pglen
, 0);
582 sg_mark_end(sg_last(sg
, pgcount
));
586 /* make cop->src and cop->dst available in scatterlists */
587 static int get_userbuf(struct csession
*ses
, struct kernel_crypt_op
*kcop
,
588 struct scatterlist
**src_sg
, struct scatterlist
**dst_sg
,
591 int src_pagecount
, dst_pagecount
= 0, pagecount
, write_src
= 1;
592 struct crypt_op
*cop
= &kcop
->cop
;
595 if (cop
->src
== NULL
)
598 if (ses
->alignmask
&& !IS_ALIGNED((unsigned long)cop
->src
, ses
->alignmask
)) {
599 dprintk(2, KERN_WARNING
, "%s: careful - source address %lx is not %d byte aligned\n",
600 __func__
, (unsigned long)cop
->src
, ses
->alignmask
+ 1);
603 src_pagecount
= PAGECOUNT(cop
->src
, cop
->len
);
604 if (!ses
->cdata
.init
) { /* hashing only */
606 } else if (cop
->src
!= cop
->dst
) { /* non-in-situ transformation */
607 if (cop
->dst
== NULL
)
610 dst_pagecount
= PAGECOUNT(cop
->dst
, cop
->len
);
613 if (ses
->alignmask
&& !IS_ALIGNED((unsigned long)cop
->dst
, ses
->alignmask
)) {
614 dprintk(2, KERN_WARNING
, "%s: careful - destination address %lx is not %d byte aligned\n",
615 __func__
, (unsigned long)cop
->dst
, ses
->alignmask
+ 1);
619 (*tot_pages
) = pagecount
= src_pagecount
+ dst_pagecount
;
621 if (pagecount
> ses
->array_size
) {
622 struct scatterlist
*sg
;
626 for (array_size
= ses
->array_size
; array_size
< pagecount
;
630 dprintk(2, KERN_DEBUG
, "%s: reallocating to %d elements\n",
631 __func__
, array_size
);
632 pages
= krealloc(ses
->pages
, array_size
* sizeof(struct page
*),
634 if (unlikely(!pages
))
637 sg
= krealloc(ses
->sg
, array_size
* sizeof(struct scatterlist
),
642 ses
->array_size
= array_size
;
645 rc
= __get_userbuf(cop
->src
, cop
->len
, write_src
, src_pagecount
,
646 ses
->pages
, ses
->sg
, kcop
->task
, kcop
->mm
);
649 "failed to get user pages for data input\n");
652 (*src_sg
) = (*dst_sg
) = ses
->sg
;
657 (*dst_sg
) = ses
->sg
+ src_pagecount
;
659 rc
= __get_userbuf(cop
->dst
, cop
->len
, 1, dst_pagecount
,
660 ses
->pages
+ src_pagecount
, *dst_sg
,
661 kcop
->task
, kcop
->mm
);
664 "failed to get user pages for data output\n");
665 release_user_pages(ses
->pages
, src_pagecount
);
671 /* This is the main crypto function - zero-copy edition */
673 __crypto_run_zc(struct csession
*ses_ptr
, struct kernel_crypt_op
*kcop
)
675 struct scatterlist
*src_sg
, *dst_sg
;
676 struct crypt_op
*cop
= &kcop
->cop
;
677 int ret
= 0, pagecount
;
679 ret
= get_userbuf(ses_ptr
, kcop
, &src_sg
, &dst_sg
, &pagecount
);
681 dprintk(1, KERN_ERR
, "Error getting user pages. "
682 "Falling back to non zero copy.\n");
683 return __crypto_run_std(ses_ptr
, cop
);
686 ret
= hash_n_crypt(ses_ptr
, cop
, src_sg
, dst_sg
, cop
->len
);
688 release_user_pages(ses_ptr
->pages
, pagecount
);
692 static int crypto_run(struct fcrypt
*fcr
, struct kernel_crypt_op
*kcop
)
694 struct csession
*ses_ptr
;
695 struct crypt_op
*cop
= &kcop
->cop
;
698 if (unlikely(cop
->op
!= COP_ENCRYPT
&& cop
->op
!= COP_DECRYPT
)) {
699 dprintk(1, KERN_DEBUG
, "invalid operation op=%u\n", cop
->op
);
703 /* this also enters ses_ptr->sem */
704 ses_ptr
= crypto_get_session_by_sid(fcr
, cop
->ses
);
705 if (unlikely(!ses_ptr
)) {
706 dprintk(1, KERN_ERR
, "invalid session ID=0x%08X\n", cop
->ses
);
710 if (ses_ptr
->hdata
.init
!= 0 && !(cop
->flags
& (COP_FLAG_UPDATE
| COP_FLAG_FINAL
))) {
711 ret
= cryptodev_hash_reset(&ses_ptr
->hdata
);
714 "error in cryptodev_hash_reset()\n");
719 if (ses_ptr
->cdata
.init
!= 0) {
720 int blocksize
= ses_ptr
->cdata
.blocksize
;
722 if (unlikely(cop
->len
% blocksize
)) {
724 "data size (%u) isn't a multiple "
725 "of block size (%u)\n",
726 cop
->len
, blocksize
);
731 cryptodev_cipher_set_iv(&ses_ptr
->cdata
, kcop
->iv
,
732 min(ses_ptr
->cdata
.ivsize
, kcop
->ivlen
));
735 if (likely(cop
->len
)) {
736 if (cop
->flags
& COP_FLAG_NO_ZC
)
737 ret
= __crypto_run_std(ses_ptr
, &kcop
->cop
);
739 ret
= __crypto_run_zc(ses_ptr
, kcop
);
744 if (ses_ptr
->cdata
.init
!= 0) {
745 cryptodev_cipher_get_iv(&ses_ptr
->cdata
, kcop
->iv
,
746 min(ses_ptr
->cdata
.ivsize
, kcop
->ivlen
));
749 if (ses_ptr
->hdata
.init
!= 0 &&
750 ((cop
->flags
& COP_FLAG_FINAL
) ||
751 (!(cop
->flags
& COP_FLAG_UPDATE
) || cop
->len
== 0))) {
753 ret
= cryptodev_hash_final(&ses_ptr
->hdata
, kcop
->hash_output
);
755 dprintk(0, KERN_ERR
, "CryptoAPI failure: %d\n", ret
);
758 kcop
->digestsize
= ses_ptr
->hdata
.digestsize
;
761 #if defined(CRYPTODEV_STATS)
763 /* this is safe - we check cop->op at the function entry */
764 ses_ptr
->stat
[cop
->op
] += cop
->len
;
765 if (ses_ptr
->stat_max_size
< cop
->len
)
766 ses_ptr
->stat_max_size
= cop
->len
;
767 ses_ptr
->stat_count
++;
772 mutex_unlock(&ses_ptr
->sem
);
776 static void cryptask_routine(struct work_struct
*work
)
778 struct crypt_priv
*pcr
= container_of(work
, struct crypt_priv
, cryptask
);
779 struct todo_list_item
*item
;
782 /* fetch all pending jobs into the temporary list */
783 mutex_lock(&pcr
->todo
.lock
);
784 list_cut_position(&tmp
, &pcr
->todo
.list
, pcr
->todo
.list
.prev
);
785 mutex_unlock(&pcr
->todo
.lock
);
787 /* handle each job locklessly */
788 list_for_each_entry(item
, &tmp
, __hook
) {
789 item
->result
= crypto_run(&pcr
->fcrypt
, &item
->kcop
);
790 if (unlikely(item
->result
))
791 dprintk(0, KERN_ERR
, "%s: crypto_run() failed: %d\n",
792 __func__
, item
->result
);
795 /* push all handled jobs to the done list at once */
796 mutex_lock(&pcr
->done
.lock
);
797 list_splice_tail(&tmp
, &pcr
->done
.list
);
798 mutex_unlock(&pcr
->done
.lock
);
800 /* wake for POLLIN */
801 wake_up_interruptible(&pcr
->user_waiter
);
804 /* ====== /dev/crypto ====== */
807 cryptodev_open(struct inode
*inode
, struct file
*filp
)
809 struct todo_list_item
*tmp
;
810 struct crypt_priv
*pcr
;
813 pcr
= kmalloc(sizeof(*pcr
), GFP_KERNEL
);
817 memset(pcr
, 0, sizeof(*pcr
));
818 mutex_init(&pcr
->fcrypt
.sem
);
819 INIT_LIST_HEAD(&pcr
->fcrypt
.list
);
821 INIT_LIST_HEAD(&pcr
->free
.list
);
822 INIT_LIST_HEAD(&pcr
->todo
.list
);
823 INIT_LIST_HEAD(&pcr
->done
.list
);
824 INIT_WORK(&pcr
->cryptask
, cryptask_routine
);
825 mutex_init(&pcr
->free
.lock
);
826 mutex_init(&pcr
->todo
.lock
);
827 mutex_init(&pcr
->done
.lock
);
828 init_waitqueue_head(&pcr
->user_waiter
);
830 for (i
= 0; i
< DEF_COP_RINGSIZE
; i
++) {
831 tmp
= kzalloc(sizeof(struct todo_list_item
), GFP_KERNEL
);
833 dprintk(2, KERN_DEBUG
, "%s: allocated new item at %lx\n",
834 __func__
, (unsigned long)tmp
);
835 list_add(&tmp
->__hook
, &pcr
->free
.list
);
838 filp
->private_data
= pcr
;
839 dprintk(2, KERN_DEBUG
,
840 "Cryptodev handle initialised, %d elements in queue\n",
846 cryptodev_release(struct inode
*inode
, struct file
*filp
)
848 struct crypt_priv
*pcr
= filp
->private_data
;
849 struct todo_list_item
*item
, *item_safe
;
855 cancel_work_sync(&pcr
->cryptask
);
857 mutex_destroy(&pcr
->todo
.lock
);
858 mutex_destroy(&pcr
->done
.lock
);
859 mutex_destroy(&pcr
->free
.lock
);
861 list_splice_tail(&pcr
->todo
.list
, &pcr
->free
.list
);
862 list_splice_tail(&pcr
->done
.list
, &pcr
->free
.list
);
864 list_for_each_entry_safe(item
, item_safe
, &pcr
->free
.list
, __hook
) {
865 dprintk(2, KERN_DEBUG
, "%s: freeing item at %lx\n",
866 __func__
, (unsigned long)item
);
867 list_del(&item
->__hook
);
872 if (items_freed
!= pcr
->itemcount
) {
874 "%s: freed %d items, but %d should exist!\n",
875 __func__
, items_freed
, pcr
->itemcount
);
878 crypto_finish_all_sessions(&pcr
->fcrypt
);
880 filp
->private_data
= NULL
;
882 dprintk(2, KERN_DEBUG
,
883 "Cryptodev handle deinitialised, %d elements freed\n",
889 clonefd(struct file
*filp
)
892 ret
= get_unused_fd();
895 fd_install(ret
, filp
);
901 /* enqueue a job for asynchronous completion
904 * -EBUSY when there are no free queue slots left
905 * (and the number of slots has reached it MAX_COP_RINGSIZE)
906 * -EFAULT when there was a memory allocation error
908 static int crypto_async_run(struct crypt_priv
*pcr
, struct kernel_crypt_op
*kcop
)
910 struct todo_list_item
*item
= NULL
;
912 mutex_lock(&pcr
->free
.lock
);
913 if (likely(!list_empty(&pcr
->free
.list
))) {
914 item
= list_first_entry(&pcr
->free
.list
,
915 struct todo_list_item
, __hook
);
916 list_del(&item
->__hook
);
917 } else if (pcr
->itemcount
< MAX_COP_RINGSIZE
) {
920 mutex_unlock(&pcr
->free
.lock
);
923 mutex_unlock(&pcr
->free
.lock
);
925 if (unlikely(!item
)) {
926 item
= kzalloc(sizeof(struct todo_list_item
), GFP_KERNEL
);
929 dprintk(1, KERN_INFO
, "%s: increased item count to %d\n",
930 __func__
, pcr
->itemcount
);
933 memcpy(&item
->kcop
, kcop
, sizeof(struct kernel_crypt_op
));
935 mutex_lock(&pcr
->todo
.lock
);
936 list_add_tail(&item
->__hook
, &pcr
->todo
.list
);
937 mutex_unlock(&pcr
->todo
.lock
);
939 queue_work(cryptodev_wq
, &pcr
->cryptask
);
943 /* get the first completed job from the "done" queue
946 * -EBUSY if no completed jobs are ready (yet)
947 * the return value of crypto_run() otherwise */
948 static int crypto_async_fetch(struct crypt_priv
*pcr
,
949 struct kernel_crypt_op
*kcop
)
951 struct todo_list_item
*item
;
954 mutex_lock(&pcr
->done
.lock
);
955 if (list_empty(&pcr
->done
.list
)) {
956 mutex_unlock(&pcr
->done
.lock
);
959 item
= list_first_entry(&pcr
->done
.list
, struct todo_list_item
, __hook
);
960 list_del(&item
->__hook
);
961 mutex_unlock(&pcr
->done
.lock
);
963 memcpy(kcop
, &item
->kcop
, sizeof(struct kernel_crypt_op
));
964 retval
= item
->result
;
966 mutex_lock(&pcr
->free
.lock
);
967 list_add_tail(&item
->__hook
, &pcr
->free
.list
);
968 mutex_unlock(&pcr
->free
.lock
);
970 /* wake for POLLOUT */
971 wake_up_interruptible(&pcr
->user_waiter
);
976 /* this function has to be called from process context */
977 static int fill_kcop_from_cop(struct kernel_crypt_op
*kcop
, struct fcrypt
*fcr
)
979 struct crypt_op
*cop
= &kcop
->cop
;
980 struct csession
*ses_ptr
;
983 /* this also enters ses_ptr->sem */
984 ses_ptr
= crypto_get_session_by_sid(fcr
, cop
->ses
);
985 if (unlikely(!ses_ptr
)) {
986 dprintk(1, KERN_ERR
, "invalid session ID=0x%08X\n", cop
->ses
);
989 kcop
->ivlen
= cop
->iv
? ses_ptr
->cdata
.ivsize
: 0;
990 kcop
->digestsize
= 0; /* will be updated during operation */
992 mutex_unlock(&ses_ptr
->sem
);
994 kcop
->task
= current
;
995 kcop
->mm
= current
->mm
;
998 rc
= copy_from_user(kcop
->iv
, cop
->iv
, kcop
->ivlen
);
1000 dprintk(1, KERN_ERR
,
1001 "error copying IV (%d bytes), copy_from_user returned %d for address %lx\n",
1002 kcop
->ivlen
, rc
, (unsigned long)cop
->iv
);
1010 /* this function has to be called from process context */
1011 static int fill_cop_from_kcop(struct kernel_crypt_op
*kcop
, struct fcrypt
*fcr
)
1015 if (kcop
->digestsize
) {
1016 ret
= copy_to_user(kcop
->cop
.mac
,
1017 kcop
->hash_output
, kcop
->digestsize
);
1021 if (kcop
->ivlen
&& kcop
->cop
.flags
& COP_FLAG_WRITE_IV
) {
1022 ret
= copy_to_user(kcop
->cop
.iv
,
1023 kcop
->iv
, kcop
->ivlen
);
1030 static int kcop_from_user(struct kernel_crypt_op
*kcop
,
1031 struct fcrypt
*fcr
, void __user
*arg
)
1033 if (unlikely(copy_from_user(&kcop
->cop
, arg
, sizeof(kcop
->cop
))))
1036 return fill_kcop_from_cop(kcop
, fcr
);
1039 static int kcop_to_user(struct kernel_crypt_op
*kcop
,
1040 struct fcrypt
*fcr
, void __user
*arg
)
1044 ret
= fill_cop_from_kcop(kcop
, fcr
);
1048 if (unlikely(copy_to_user(arg
, &kcop
->cop
, sizeof(kcop
->cop
))))
1053 static inline void tfm_info_to_alg_info(struct alg_info
*dst
, struct crypto_tfm
*tfm
)
1055 snprintf(dst
->cra_name
, CRYPTODEV_MAX_ALG_NAME
,
1056 "%s", crypto_tfm_alg_name(tfm
));
1057 snprintf(dst
->cra_driver_name
, CRYPTODEV_MAX_ALG_NAME
,
1058 "%s", crypto_tfm_alg_driver_name(tfm
));
1061 static int get_session_info(struct fcrypt
*fcr
, struct session_info_op
*siop
)
1063 struct csession
*ses_ptr
;
1065 /* this also enters ses_ptr->sem */
1066 ses_ptr
= crypto_get_session_by_sid(fcr
, siop
->ses
);
1067 if (unlikely(!ses_ptr
)) {
1068 dprintk(1, KERN_ERR
, "invalid session ID=0x%08X\n", siop
->ses
);
1072 if (ses_ptr
->cdata
.init
) {
1073 tfm_info_to_alg_info(&siop
->cipher_info
,
1074 crypto_ablkcipher_tfm(ses_ptr
->cdata
.async
.s
));
1076 if (ses_ptr
->hdata
.init
) {
1077 tfm_info_to_alg_info(&siop
->hash_info
,
1078 crypto_ahash_tfm(ses_ptr
->hdata
.async
.s
));
1081 siop
->alignmask
= ses_ptr
->alignmask
;
1083 mutex_unlock(&ses_ptr
->sem
);
1088 cryptodev_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg_
)
1090 void __user
*arg
= (void __user
*)arg_
;
1091 int __user
*p
= arg
;
1092 struct session_op sop
;
1093 struct kernel_crypt_op kcop
;
1094 struct crypt_priv
*pcr
= filp
->private_data
;
1096 struct session_info_op siop
;
1107 return put_user(0, p
);
1110 ret
= put_user(fd
, p
);
1111 if (unlikely(ret
)) {
1117 if (unlikely(copy_from_user(&sop
, arg
, sizeof(sop
))))
1120 ret
= crypto_create_session(fcr
, &sop
);
1123 ret
= copy_to_user(arg
, &sop
, sizeof(sop
));
1124 if (unlikely(ret
)) {
1125 crypto_finish_session(fcr
, sop
.ses
);
1130 ret
= get_user(ses
, (uint32_t __user
*)arg
);
1133 ret
= crypto_finish_session(fcr
, ses
);
1136 if (unlikely(copy_from_user(&siop
, arg
, sizeof(siop
))))
1139 ret
= get_session_info(fcr
, &siop
);
1142 return copy_to_user(arg
, &siop
, sizeof(siop
));
1144 if (unlikely(ret
= kcop_from_user(&kcop
, fcr
, arg
)))
1147 ret
= crypto_run(fcr
, &kcop
);
1151 return kcop_to_user(&kcop
, fcr
, arg
);
1152 case CIOCASYNCCRYPT
:
1153 if (unlikely(ret
= kcop_from_user(&kcop
, fcr
, arg
)))
1156 return crypto_async_run(pcr
, &kcop
);
1157 case CIOCASYNCFETCH
:
1158 ret
= crypto_async_fetch(pcr
, &kcop
);
1162 return kcop_to_user(&kcop
, fcr
, arg
);
1168 /* compatibility code for 32bit userlands */
1169 #ifdef CONFIG_COMPAT
1172 compat_to_session_op(struct compat_session_op
*compat
, struct session_op
*sop
)
1174 sop
->cipher
= compat
->cipher
;
1175 sop
->mac
= compat
->mac
;
1176 sop
->keylen
= compat
->keylen
;
1178 sop
->key
= compat_ptr(compat
->key
);
1179 sop
->mackeylen
= compat
->mackeylen
;
1180 sop
->mackey
= compat_ptr(compat
->mackey
);
1181 sop
->ses
= compat
->ses
;
1185 session_op_to_compat(struct session_op
*sop
, struct compat_session_op
*compat
)
1187 compat
->cipher
= sop
->cipher
;
1188 compat
->mac
= sop
->mac
;
1189 compat
->keylen
= sop
->keylen
;
1191 compat
->key
= ptr_to_compat(sop
->key
);
1192 compat
->mackeylen
= sop
->mackeylen
;
1193 compat
->mackey
= ptr_to_compat(sop
->mackey
);
1194 compat
->ses
= sop
->ses
;
1198 compat_to_crypt_op(struct compat_crypt_op
*compat
, struct crypt_op
*cop
)
1200 cop
->ses
= compat
->ses
;
1201 cop
->op
= compat
->op
;
1202 cop
->flags
= compat
->flags
;
1203 cop
->len
= compat
->len
;
1205 cop
->src
= compat_ptr(compat
->src
);
1206 cop
->dst
= compat_ptr(compat
->dst
);
1207 cop
->mac
= compat_ptr(compat
->mac
);
1208 cop
->iv
= compat_ptr(compat
->iv
);
1212 crypt_op_to_compat(struct crypt_op
*cop
, struct compat_crypt_op
*compat
)
1214 compat
->ses
= cop
->ses
;
1215 compat
->op
= cop
->op
;
1216 compat
->flags
= cop
->flags
;
1217 compat
->len
= cop
->len
;
1219 compat
->src
= ptr_to_compat(cop
->src
);
1220 compat
->dst
= ptr_to_compat(cop
->dst
);
1221 compat
->mac
= ptr_to_compat(cop
->mac
);
1222 compat
->iv
= ptr_to_compat(cop
->iv
);
1225 static int compat_kcop_from_user(struct kernel_crypt_op
*kcop
,
1226 struct fcrypt
*fcr
, void __user
*arg
)
1228 struct compat_crypt_op compat_cop
;
1230 if (unlikely(copy_from_user(&compat_cop
, arg
, sizeof(compat_cop
))))
1232 compat_to_crypt_op(&compat_cop
, &kcop
->cop
);
1234 return fill_kcop_from_cop(kcop
, fcr
);
1237 static int compat_kcop_to_user(struct kernel_crypt_op
*kcop
,
1238 struct fcrypt
*fcr
, void __user
*arg
)
1241 struct compat_crypt_op compat_cop
;
1243 ret
= fill_cop_from_kcop(kcop
, fcr
);
1246 crypt_op_to_compat(&kcop
->cop
, &compat_cop
);
1248 if (unlikely(copy_to_user(arg
, &compat_cop
, sizeof(compat_cop
))))
1254 cryptodev_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg_
)
1256 void __user
*arg
= (void __user
*)arg_
;
1257 struct crypt_priv
*pcr
= file
->private_data
;
1259 struct session_op sop
;
1260 struct compat_session_op compat_sop
;
1261 struct kernel_crypt_op kcop
;
1274 return cryptodev_ioctl(file
, cmd
, arg_
);
1276 case COMPAT_CIOCGSESSION
:
1277 if (unlikely(copy_from_user(&compat_sop
, arg
,
1278 sizeof(compat_sop
))))
1280 compat_to_session_op(&compat_sop
, &sop
);
1282 ret
= crypto_create_session(fcr
, &sop
);
1286 session_op_to_compat(&sop
, &compat_sop
);
1287 ret
= copy_to_user(arg
, &compat_sop
, sizeof(compat_sop
));
1288 if (unlikely(ret
)) {
1289 crypto_finish_session(fcr
, sop
.ses
);
1294 case COMPAT_CIOCCRYPT
:
1295 ret
= compat_kcop_from_user(&kcop
, fcr
, arg
);
1299 ret
= crypto_run(fcr
, &kcop
);
1303 return compat_kcop_to_user(&kcop
, fcr
, arg
);
1304 case COMPAT_CIOCASYNCCRYPT
:
1305 if (unlikely(ret
= compat_kcop_from_user(&kcop
, fcr
, arg
)))
1308 return crypto_async_run(pcr
, &kcop
);
1309 case COMPAT_CIOCASYNCFETCH
:
1310 ret
= crypto_async_fetch(pcr
, &kcop
);
1314 return compat_kcop_to_user(&kcop
, fcr
, arg
);
1321 #endif /* CONFIG_COMPAT */
1323 static unsigned int cryptodev_poll(struct file
*file
, poll_table
*wait
)
1325 struct crypt_priv
*pcr
= file
->private_data
;
1328 poll_wait(file
, &pcr
->user_waiter
, wait
);
1330 if (!list_empty_careful(&pcr
->done
.list
))
1331 ret
|= POLLIN
| POLLRDNORM
;
1332 if (!list_empty_careful(&pcr
->free
.list
) || pcr
->itemcount
< MAX_COP_RINGSIZE
)
1333 ret
|= POLLOUT
| POLLWRNORM
;
1338 static const struct file_operations cryptodev_fops
= {
1339 .owner
= THIS_MODULE
,
1340 .open
= cryptodev_open
,
1341 .release
= cryptodev_release
,
1342 .unlocked_ioctl
= cryptodev_ioctl
,
1343 #ifdef CONFIG_COMPAT
1344 .compat_ioctl
= cryptodev_compat_ioctl
,
1345 #endif /* CONFIG_COMPAT */
1346 .poll
= cryptodev_poll
,
1349 static struct miscdevice cryptodev
= {
1350 .minor
= MISC_DYNAMIC_MINOR
,
1352 .fops
= &cryptodev_fops
,
1356 cryptodev_register(void)
1360 rc
= misc_register(&cryptodev
);
1362 printk(KERN_ERR PFX
"registration of /dev/crypto failed\n");
1370 cryptodev_deregister(void)
1372 misc_deregister(&cryptodev
);
1375 /* ====== Module init/exit ====== */
1376 static int __init
init_cryptodev(void)
1380 cryptodev_wq
= create_workqueue("cryptodev_queue");
1381 if (unlikely(!cryptodev_wq
)) {
1382 printk(KERN_ERR PFX
"failed to allocate the cryptodev workqueue\n");
1386 rc
= cryptodev_register();
1388 destroy_workqueue(cryptodev_wq
);
1392 printk(KERN_INFO PFX
"driver %s loaded.\n", VERSION
);
1397 static void __exit
exit_cryptodev(void)
1399 flush_workqueue(cryptodev_wq
);
1400 destroy_workqueue(cryptodev_wq
);
1402 cryptodev_deregister();
1403 printk(KERN_INFO PFX
"driver unloaded.\n");
1406 module_init(init_cryptodev
);
1407 module_exit(exit_cryptodev
);