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 <linux/crypto.h>
37 #include <linux/highmem.h>
38 #include <linux/ioctl.h>
39 #include <linux/random.h>
40 #include <linux/syscalls.h>
41 #include <linux/pagemap.h>
42 #include <linux/uaccess.h>
43 #include "cryptodev.h"
44 #include <linux/scatterlist.h>
45 #include "cryptodev_int.h"
48 MODULE_AUTHOR("Nikos Mavrogiannopoulos <nmav@gnutls.org>");
49 MODULE_DESCRIPTION("CryptoDev driver");
50 MODULE_LICENSE("GPL");
52 /* ====== Compile-time config ====== */
54 #define CRYPTODEV_STATS
56 /* ====== Module parameters ====== */
58 int cryptodev_verbosity
;
59 module_param(cryptodev_verbosity
, int, 0644);
60 MODULE_PARM_DESC(cryptodev_verbosity
, "0: normal, 1: verbose, 2: debug");
62 #ifdef CRYPTODEV_STATS
63 static int enable_stats
;
64 module_param(enable_stats
, int, 0644);
65 MODULE_PARM_DESC(enable_stats
, "collect statictics about cryptodev usage");
68 /* ====== CryptoAPI ====== */
70 struct list_head list
;
78 #define FILL_SG(sg, ptr, len) \
80 (sg)->page = virt_to_page(ptr); \
81 (sg)->offset = offset_in_page(ptr); \
83 (sg)->dma_address = 0; \
87 struct list_head entry
;
89 struct cipher_data cdata
;
90 struct hash_data hdata
;
92 #ifdef CRYPTODEV_STATS
93 #if !((COP_ENCRYPT < 2) && (COP_DECRYPT < 2))
94 #error Struct csession.stat uses COP_{ENCRYPT,DECRYPT} as indices. Do something!
96 unsigned long long stat
[2];
97 size_t stat_max_size
, stat_count
;
101 struct scatterlist
*sg
;
104 /* Prepare session for future use. */
106 crypto_create_session(struct fcrypt
*fcr
, struct session_op
*sop
)
108 struct csession
*ses_new
= NULL
, *ses_ptr
;
110 const char *alg_name
= NULL
;
111 const char *hash_name
= NULL
;
114 /* Does the request make sense? */
115 if (unlikely(!sop
->cipher
&& !sop
->mac
)) {
116 dprintk(1, KERN_DEBUG
, "Both 'cipher' and 'mac' unset.\n");
120 switch (sop
->cipher
) {
124 alg_name
= "cbc(des)";
126 case CRYPTO_3DES_CBC
:
127 alg_name
= "cbc(des3_ede)";
130 alg_name
= "cbc(blowfish)";
133 alg_name
= "cbc(aes)";
135 case CRYPTO_CAMELLIA_CBC
:
136 alg_name
= "cbc(camelia)";
139 alg_name
= "ctr(aes)";
142 alg_name
= "ecb(cipher_null)";
145 dprintk(1, KERN_DEBUG
, "%s: bad cipher: %d\n", __func__
,
153 case CRYPTO_MD5_HMAC
:
154 hash_name
= "hmac(md5)";
156 case CRYPTO_RIPEMD160_HMAC
:
157 hash_name
= "hmac(rmd160)";
159 case CRYPTO_SHA1_HMAC
:
160 hash_name
= "hmac(sha1)";
162 case CRYPTO_SHA2_256_HMAC
:
163 hash_name
= "hmac(sha256)";
165 case CRYPTO_SHA2_384_HMAC
:
166 hash_name
= "hmac(sha384)";
168 case CRYPTO_SHA2_512_HMAC
:
169 hash_name
= "hmac(sha512)";
177 case CRYPTO_RIPEMD160
:
178 hash_name
= "rmd160";
185 case CRYPTO_SHA2_256
:
186 hash_name
= "sha256";
189 case CRYPTO_SHA2_384
:
190 hash_name
= "sha384";
193 case CRYPTO_SHA2_512
:
194 hash_name
= "sha512";
199 dprintk(1, KERN_DEBUG
, "%s: bad mac: %d\n", __func__
,
204 /* Create a session and put it to the list. */
205 ses_new
= kzalloc(sizeof(*ses_new
), GFP_KERNEL
);
209 /* Set-up crypto transform. */
211 uint8_t keyp
[CRYPTO_CIPHER_MAX_KEY_LEN
];
213 if (unlikely(sop
->keylen
> CRYPTO_CIPHER_MAX_KEY_LEN
)) {
214 dprintk(1, KERN_DEBUG
,
215 "Setting key failed for %s-%zu.\n",
216 alg_name
, (size_t)sop
->keylen
*8);
221 if (unlikely(copy_from_user(keyp
, sop
->key
, sop
->keylen
))) {
226 ret
= cryptodev_cipher_init(&ses_new
->cdata
, alg_name
, keyp
,
229 dprintk(1, KERN_DEBUG
,
230 "%s: Failed to load cipher for %s\n",
238 uint8_t keyp
[CRYPTO_HMAC_MAX_KEY_LEN
];
240 if (unlikely(sop
->mackeylen
> CRYPTO_HMAC_MAX_KEY_LEN
)) {
241 dprintk(1, KERN_DEBUG
,
242 "Setting key failed for %s-%zu.\n",
243 alg_name
, (size_t)sop
->mackeylen
*8);
248 if (unlikely(copy_from_user(keyp
, sop
->mackey
,
254 ret
= cryptodev_hash_init(&ses_new
->hdata
, hash_name
, hmac_mode
,
255 keyp
, sop
->mackeylen
);
257 dprintk(1, KERN_DEBUG
,
258 "%s: Failed to load hash for %s\n",
259 __func__
, hash_name
);
265 ses_new
->array_size
= DEFAULT_PREALLOC_PAGES
;
266 dprintk(2, KERN_DEBUG
, "%s: preallocating for %d user pages\n",
267 __func__
, ses_new
->array_size
);
268 ses_new
->pages
= kzalloc(ses_new
->array_size
*
269 sizeof(struct page
*), GFP_KERNEL
);
270 ses_new
->sg
= kzalloc(ses_new
->array_size
*
271 sizeof(struct scatterlist
), GFP_KERNEL
);
272 if (ses_new
->sg
== NULL
|| ses_new
->pages
== NULL
) {
273 dprintk(0, KERN_DEBUG
, "Memory error\n");
278 /* put the new session to the list */
279 get_random_bytes(&ses_new
->sid
, sizeof(ses_new
->sid
));
280 mutex_init(&ses_new
->sem
);
282 mutex_lock(&fcr
->sem
);
284 list_for_each_entry(ses_ptr
, &fcr
->list
, entry
) {
285 /* Check for duplicate SID */
286 if (unlikely(ses_new
->sid
== ses_ptr
->sid
)) {
287 get_random_bytes(&ses_new
->sid
, sizeof(ses_new
->sid
));
288 /* Unless we have a broken RNG this
289 shouldn't loop forever... ;-) */
294 list_add(&ses_new
->entry
, &fcr
->list
);
295 mutex_unlock(&fcr
->sem
);
297 /* Fill in some values for the user. */
298 sop
->ses
= ses_new
->sid
;
303 cryptodev_cipher_deinit(&ses_new
->cdata
);
305 kfree(ses_new
->pages
);
313 /* Everything that needs to be done when remowing a session. */
315 crypto_destroy_session(struct csession
*ses_ptr
)
317 if (!mutex_trylock(&ses_ptr
->sem
)) {
318 dprintk(2, KERN_DEBUG
, "Waiting for semaphore of sid=0x%08X\n",
320 mutex_lock(&ses_ptr
->sem
);
322 dprintk(2, KERN_DEBUG
, "Removed session 0x%08X\n", ses_ptr
->sid
);
323 #if defined(CRYPTODEV_STATS)
325 dprintk(2, KERN_DEBUG
,
326 "Usage in Bytes: enc=%llu, dec=%llu, \
327 max=%zu, avg=%lu, cnt=%zu\n",
328 ses_ptr
->stat
[COP_ENCRYPT
], ses_ptr
->stat
[COP_DECRYPT
],
329 ses_ptr
->stat_max_size
, ses_ptr
->stat_count
> 0
330 ? ((unsigned long)(ses_ptr
->stat
[COP_ENCRYPT
]+
331 ses_ptr
->stat
[COP_DECRYPT
]) /
332 ses_ptr
->stat_count
) : 0,
333 ses_ptr
->stat_count
);
335 cryptodev_cipher_deinit(&ses_ptr
->cdata
);
336 cryptodev_hash_deinit(&ses_ptr
->hdata
);
337 dprintk(2, KERN_DEBUG
, "%s: freeing space for %d user pages\n",
338 __func__
, ses_ptr
->array_size
);
339 kfree(ses_ptr
->pages
);
341 mutex_unlock(&ses_ptr
->sem
);
345 /* Look up a session by ID and remove. */
347 crypto_finish_session(struct fcrypt
*fcr
, uint32_t sid
)
349 struct csession
*tmp
, *ses_ptr
;
350 struct list_head
*head
;
353 mutex_lock(&fcr
->sem
);
355 list_for_each_entry_safe(ses_ptr
, tmp
, head
, entry
) {
356 if (ses_ptr
->sid
== sid
) {
357 list_del(&ses_ptr
->entry
);
358 crypto_destroy_session(ses_ptr
);
363 if (unlikely(!ses_ptr
)) {
364 dprintk(1, KERN_ERR
, "Session with sid=0x%08X not found!\n",
368 mutex_unlock(&fcr
->sem
);
373 /* Remove all sessions when closing the file */
375 crypto_finish_all_sessions(struct fcrypt
*fcr
)
377 struct csession
*tmp
, *ses_ptr
;
378 struct list_head
*head
;
380 mutex_lock(&fcr
->sem
);
383 list_for_each_entry_safe(ses_ptr
, tmp
, head
, entry
) {
384 list_del(&ses_ptr
->entry
);
385 crypto_destroy_session(ses_ptr
);
387 mutex_unlock(&fcr
->sem
);
392 /* Look up session by session ID. The returned session is locked. */
393 static struct csession
*
394 crypto_get_session_by_sid(struct fcrypt
*fcr
, uint32_t sid
)
396 struct csession
*ses_ptr
;
398 mutex_lock(&fcr
->sem
);
399 list_for_each_entry(ses_ptr
, &fcr
->list
, entry
) {
400 if (ses_ptr
->sid
== sid
) {
401 mutex_lock(&ses_ptr
->sem
);
405 mutex_unlock(&fcr
->sem
);
411 hash_n_crypt(struct csession
*ses_ptr
, struct crypt_op
*cop
,
412 struct scatterlist
*src_sg
, struct scatterlist
*dst_sg
,
417 /* Always hash before encryption and after decryption. Maybe
418 * we should introduce a flag to switch... TBD later on.
420 if (cop
->op
== COP_ENCRYPT
) {
421 if (ses_ptr
->hdata
.init
!= 0) {
422 ret
= cryptodev_hash_update(&ses_ptr
->hdata
,
427 if (ses_ptr
->cdata
.init
!= 0) {
428 ret
= cryptodev_cipher_encrypt(&ses_ptr
->cdata
,
429 src_sg
, dst_sg
, len
);
435 if (ses_ptr
->cdata
.init
!= 0) {
436 ret
= cryptodev_cipher_decrypt(&ses_ptr
->cdata
,
437 src_sg
, dst_sg
, len
);
443 if (ses_ptr
->hdata
.init
!= 0) {
444 ret
= cryptodev_hash_update(&ses_ptr
->hdata
,
452 dprintk(0, KERN_ERR
, "CryptoAPI failure: %d\n", ret
);
457 /* This is the main crypto function - feed it with plaintext
458 and get a ciphertext (or vice versa :-) */
460 __crypto_run_std(struct csession
*ses_ptr
, struct crypt_op
*cop
)
463 char __user
*src
, *dst
;
464 struct scatterlist sg
;
465 size_t nbytes
, bufsize
;
469 data
= (char *)__get_free_page(GFP_KERNEL
);
474 bufsize
= PAGE_SIZE
< nbytes
? PAGE_SIZE
: nbytes
;
480 size_t current_len
= nbytes
> bufsize
? bufsize
: nbytes
;
482 if (unlikely(copy_from_user(data
, src
, current_len
))) {
487 sg_init_one(&sg
, data
, current_len
);
489 ret
= hash_n_crypt(ses_ptr
, cop
, &sg
, &sg
, current_len
);
494 if (ses_ptr
->cdata
.init
!= 0) {
495 if (unlikely(copy_to_user(dst
, data
, current_len
))) {
502 nbytes
-= current_len
;
506 free_page((unsigned long)data
);
510 void release_user_pages(struct page
**pg
, int pagecount
)
512 while (pagecount
--) {
513 if (!PageReserved(pg
[pagecount
]))
514 SetPageDirty(pg
[pagecount
]);
515 page_cache_release(pg
[pagecount
]);
519 /* offset of buf in it's first page */
520 #define PAGEOFFSET(buf) ((unsigned long)buf & ~PAGE_MASK)
522 /* fetch the pages addr resides in into pg and initialise sg with them */
523 int __get_userbuf(uint8_t __user
*addr
, uint32_t len
, int write
,
524 int pgcount
, struct page
**pg
, struct scatterlist
*sg
)
526 int ret
, pglen
, i
= 0;
527 struct scatterlist
*sgp
;
529 down_write(¤t
->mm
->mmap_sem
);
530 ret
= get_user_pages(current
, current
->mm
,
531 (unsigned long)addr
, pgcount
, write
, 0, pg
, NULL
);
532 up_write(¤t
->mm
->mmap_sem
);
536 sg_init_table(sg
, pgcount
);
538 pglen
= min((ptrdiff_t)(PAGE_SIZE
- PAGEOFFSET(addr
)), (ptrdiff_t)len
);
539 sg_set_page(sg
, pg
[i
++], pglen
, PAGEOFFSET(addr
));
542 for (sgp
= sg_next(sg
); len
; sgp
= sg_next(sgp
)) {
543 pglen
= min((uint32_t)PAGE_SIZE
, len
);
544 sg_set_page(sgp
, pg
[i
++], pglen
, 0);
547 sg_mark_end(sg_last(sg
, pgcount
));
551 /* make cop->src and cop->dst available in scatterlists */
552 static int get_userbuf(struct csession
*ses
,
553 struct crypt_op
*cop
, struct scatterlist
**src_sg
,
554 struct scatterlist
**dst_sg
, int *tot_pages
)
556 int src_pagecount
, dst_pagecount
= 0, pagecount
, write_src
= 1;
558 if (cop
->src
== NULL
)
561 src_pagecount
= PAGECOUNT(cop
->src
, cop
->len
);
562 if (!ses
->cdata
.init
) { /* hashing only */
564 } else if (cop
->src
!= cop
->dst
) { /* non-in-situ transformation */
565 if (cop
->dst
== NULL
)
568 dst_pagecount
= PAGECOUNT(cop
->dst
, cop
->len
);
571 (*tot_pages
) = pagecount
= src_pagecount
+ dst_pagecount
;
573 if (pagecount
> ses
->array_size
) {
574 struct scatterlist
*sg
;
578 for (array_size
= ses
->array_size
; array_size
< pagecount
;
582 dprintk(2, KERN_DEBUG
, "%s: reallocating to %d elements\n",
583 __func__
, array_size
);
584 pages
= krealloc(ses
->pages
, array_size
* sizeof(struct page
*),
589 sg
= krealloc(ses
->sg
, array_size
* sizeof(struct scatterlist
),
594 ses
->array_size
= array_size
;
597 if (__get_userbuf(cop
->src
, cop
->len
, write_src
,
598 src_pagecount
, ses
->pages
, ses
->sg
)) {
600 "failed to get user pages for data input\n");
603 (*src_sg
) = (*dst_sg
) = ses
->sg
;
606 (*dst_sg
) = ses
->sg
+ src_pagecount
;
608 if (__get_userbuf(cop
->dst
, cop
->len
, 1, dst_pagecount
,
609 ses
->pages
+ src_pagecount
, *dst_sg
)) {
611 "failed to get user pages for data output\n");
612 release_user_pages(ses
->pages
, src_pagecount
);
619 /* This is the main crypto function - zero-copy edition */
621 __crypto_run_zc(struct csession
*ses_ptr
, struct crypt_op
*cop
)
623 struct scatterlist
*src_sg
, *dst_sg
;
624 int ret
= 0, pagecount
;
626 ret
= get_userbuf(ses_ptr
, cop
, &src_sg
, &dst_sg
, &pagecount
);
628 dprintk(1, KERN_ERR
, "Error getting user pages. \
629 Falling back to non zero copy.\n");
630 return __crypto_run_std(ses_ptr
, cop
);
633 ret
= hash_n_crypt(ses_ptr
, cop
, src_sg
, dst_sg
, cop
->len
);
635 release_user_pages(ses_ptr
->pages
, pagecount
);
639 static int crypto_run(struct fcrypt
*fcr
, struct crypt_op
*cop
)
641 struct csession
*ses_ptr
;
642 uint8_t hash_output
[AALG_MAX_RESULT_LEN
];
645 if (unlikely(cop
->op
!= COP_ENCRYPT
&& cop
->op
!= COP_DECRYPT
)) {
646 dprintk(1, KERN_DEBUG
, "invalid operation op=%u\n", cop
->op
);
650 /* this also enters ses_ptr->sem */
651 ses_ptr
= crypto_get_session_by_sid(fcr
, cop
->ses
);
652 if (unlikely(!ses_ptr
)) {
653 dprintk(1, KERN_ERR
, "invalid session ID=0x%08X\n", cop
->ses
);
657 if (ses_ptr
->hdata
.init
!= 0 && !(cop
->flags
& COP_FLAG_UPDATE
) &&
658 !(cop
->flags
& COP_FLAG_FINAL
)) {
659 ret
= cryptodev_hash_reset(&ses_ptr
->hdata
);
662 "error in cryptodev_hash_reset()\n");
667 if (ses_ptr
->cdata
.init
!= 0) {
668 int blocksize
= ses_ptr
->cdata
.blocksize
;
670 if (unlikely(cop
->len
% blocksize
)) {
672 "data size (%u) isn't a multiple \
673 of block size (%u)\n",
674 cop
->len
, blocksize
);
680 uint8_t iv
[EALG_MAX_BLOCK_LEN
];
682 ret
= copy_from_user(iv
, cop
->iv
,
683 min((int)sizeof(iv
), (ses_ptr
->cdata
.ivsize
)));
686 "error copying IV (%d bytes)\n",
688 (ses_ptr
->cdata
.ivsize
)));
693 cryptodev_cipher_set_iv(&ses_ptr
->cdata
, iv
,
694 ses_ptr
->cdata
.ivsize
);
699 ret
= __crypto_run_zc(ses_ptr
, cop
);
704 if (ses_ptr
->hdata
.init
!= 0 &&
705 ((cop
->flags
& COP_FLAG_FINAL
) ||
706 (!(cop
->flags
& COP_FLAG_UPDATE
) || cop
->len
== 0))) {
707 ret
= cryptodev_hash_final(&ses_ptr
->hdata
, hash_output
);
709 dprintk(0, KERN_ERR
, "CryptoAPI failure: %d\n", ret
);
713 if (unlikely(copy_to_user(cop
->mac
, hash_output
,
714 ses_ptr
->hdata
.digestsize
))) {
720 #if defined(CRYPTODEV_STATS)
722 /* this is safe - we check cop->op at the function entry */
723 ses_ptr
->stat
[cop
->op
] += cop
->len
;
724 if (ses_ptr
->stat_max_size
< cop
->len
)
725 ses_ptr
->stat_max_size
= cop
->len
;
726 ses_ptr
->stat_count
++;
731 mutex_unlock(&ses_ptr
->sem
);
735 /* ====== /dev/crypto ====== */
738 cryptodev_open(struct inode
*inode
, struct file
*filp
)
740 struct crypt_priv
*pcr
;
742 pcr
= kmalloc(sizeof(*pcr
), GFP_KERNEL
);
746 memset(pcr
, 0, sizeof(*pcr
));
747 mutex_init(&pcr
->fcrypt
.sem
);
748 INIT_LIST_HEAD(&pcr
->fcrypt
.list
);
750 filp
->private_data
= pcr
;
755 cryptodev_release(struct inode
*inode
, struct file
*filp
)
757 struct crypt_priv
*pcr
= filp
->private_data
;
760 crypto_finish_all_sessions(&pcr
->fcrypt
);
762 filp
->private_data
= NULL
;
769 clonefd(struct file
*filp
)
772 ret
= get_unused_fd();
775 fd_install(ret
, filp
);
782 cryptodev_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg_
)
784 void __user
*arg
= (void __user
*)arg_
;
786 struct session_op sop
;
788 struct crypt_priv
*pcr
= filp
->private_data
;
800 return put_user(0, p
);
803 ret
= put_user(fd
, p
);
810 if (unlikely(copy_from_user(&sop
, arg
, sizeof(sop
))))
813 ret
= crypto_create_session(fcr
, &sop
);
816 ret
= copy_to_user(arg
, &sop
, sizeof(sop
));
818 crypto_finish_session(fcr
, sop
.ses
);
823 ret
= get_user(ses
, (uint32_t __user
*)arg
);
826 ret
= crypto_finish_session(fcr
, ses
);
829 if (unlikely(copy_from_user(&cop
, arg
, sizeof(cop
))))
832 ret
= crypto_run(fcr
, &cop
);
835 if (unlikely(copy_to_user(arg
, &cop
, sizeof(cop
))))
844 /* compatibility code for 32bit userlands */
848 compat_to_session_op(struct compat_session_op
*compat
, struct session_op
*sop
)
850 sop
->cipher
= compat
->cipher
;
851 sop
->mac
= compat
->mac
;
852 sop
->keylen
= compat
->keylen
;
854 sop
->key
= compat_ptr(compat
->key
);
855 sop
->mackeylen
= compat
->mackeylen
;
856 sop
->mackey
= compat_ptr(compat
->mackey
);
857 sop
->ses
= compat
->ses
;
861 session_op_to_compat(struct session_op
*sop
, struct compat_session_op
*compat
)
863 compat
->cipher
= sop
->cipher
;
864 compat
->mac
= sop
->mac
;
865 compat
->keylen
= sop
->keylen
;
867 compat
->key
= ptr_to_compat(sop
->key
);
868 compat
->mackeylen
= sop
->mackeylen
;
869 compat
->mackey
= ptr_to_compat(sop
->mackey
);
870 compat
->ses
= sop
->ses
;
874 compat_to_crypt_op(struct compat_crypt_op
*compat
, struct crypt_op
*cop
)
876 cop
->ses
= compat
->ses
;
877 cop
->op
= compat
->op
;
878 cop
->flags
= compat
->flags
;
879 cop
->len
= compat
->len
;
881 cop
->src
= compat_ptr(compat
->src
);
882 cop
->dst
= compat_ptr(compat
->dst
);
883 cop
->mac
= compat_ptr(compat
->mac
);
884 cop
->iv
= compat_ptr(compat
->iv
);
888 crypt_op_to_compat(struct crypt_op
*cop
, struct compat_crypt_op
*compat
)
890 compat
->ses
= cop
->ses
;
891 compat
->op
= cop
->op
;
892 compat
->flags
= cop
->flags
;
893 compat
->len
= cop
->len
;
895 compat
->src
= ptr_to_compat(cop
->src
);
896 compat
->dst
= ptr_to_compat(cop
->dst
);
897 compat
->mac
= ptr_to_compat(cop
->mac
);
898 compat
->iv
= ptr_to_compat(cop
->iv
);
902 cryptodev_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg_
)
904 void __user
*arg
= (void __user
*)arg_
;
905 struct fcrypt
*fcr
= file
->private_data
;
906 struct session_op sop
;
907 struct compat_session_op compat_sop
;
909 struct compat_crypt_op compat_cop
;
919 return cryptodev_ioctl(file
, cmd
, arg_
);
921 case COMPAT_CIOCGSESSION
:
922 if (unlikely(copy_from_user(&compat_sop
, arg
,
923 sizeof(compat_sop
))))
925 compat_to_session_op(&compat_sop
, &sop
);
927 ret
= crypto_create_session(fcr
, &sop
);
931 session_op_to_compat(&sop
, &compat_sop
);
932 ret
= copy_to_user(arg
, &compat_sop
, sizeof(compat_sop
));
934 crypto_finish_session(fcr
, sop
.ses
);
939 case COMPAT_CIOCCRYPT
:
940 if (unlikely(copy_from_user(&compat_cop
, arg
,
941 sizeof(compat_cop
))))
944 compat_to_crypt_op(&compat_cop
, &cop
);
946 ret
= crypto_run(fcr
, &cop
);
950 crypt_op_to_compat(&cop
, &compat_cop
);
951 if (unlikely(copy_to_user(arg
, &compat_cop
,
952 sizeof(compat_cop
))))
961 #endif /* CONFIG_COMPAT */
963 static const struct file_operations cryptodev_fops
= {
964 .owner
= THIS_MODULE
,
965 .open
= cryptodev_open
,
966 .release
= cryptodev_release
,
967 .unlocked_ioctl
= cryptodev_ioctl
,
969 .compat_ioctl
= cryptodev_compat_ioctl
,
970 #endif /* CONFIG_COMPAT */
973 static struct miscdevice cryptodev
= {
974 .minor
= MISC_DYNAMIC_MINOR
,
976 .fops
= &cryptodev_fops
,
980 cryptodev_register(void)
984 rc
= misc_register(&cryptodev
);
986 printk(KERN_ERR PFX
"registration of /dev/crypto failed\n");
994 cryptodev_deregister(void)
996 misc_deregister(&cryptodev
);
999 /* ====== Module init/exit ====== */
1000 static int __init
init_cryptodev(void)
1004 rc
= cryptodev_register();
1008 printk(KERN_INFO PFX
"driver %s loaded.\n", VERSION
);
1013 static void __exit
exit_cryptodev(void)
1015 cryptodev_deregister();
1016 printk(KERN_INFO PFX
"driver unloaded.\n");
1019 module_init(init_cryptodev
);
1020 module_exit(exit_cryptodev
);