sha.c includes correct header.
[cryptodev-linux.git] / ioctl.c
blob5b8169dea1065366f926dc3658f732f94f111be9
1 /*
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
22 * Foundation, Inc.,
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>
38 #include <linux/mm.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"
49 #include "zc.h"
50 #include "version.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;
73 int result;
76 struct locked_list {
77 struct list_head list;
78 struct mutex lock;
81 struct crypt_priv {
82 struct fcrypt fcrypt;
83 struct locked_list free, todo, done;
84 int itemcount;
85 struct work_struct cryptask;
86 wait_queue_head_t user_waiter;
89 #define FILL_SG(sg, ptr, len) \
90 do { \
91 (sg)->page = virt_to_page(ptr); \
92 (sg)->offset = offset_in_page(ptr); \
93 (sg)->length = len; \
94 (sg)->dma_address = 0; \
95 } while (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. */
101 static int
102 crypto_create_session(struct fcrypt *fcr, struct session_op *sop)
104 struct csession *ses_new = NULL, *ses_ptr;
105 int ret = 0;
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");
113 return -EINVAL;
116 switch (sop->cipher) {
117 case 0:
118 break;
119 case CRYPTO_DES_CBC:
120 alg_name = "cbc(des)";
121 break;
122 case CRYPTO_3DES_CBC:
123 alg_name = "cbc(des3_ede)";
124 break;
125 case CRYPTO_BLF_CBC:
126 alg_name = "cbc(blowfish)";
127 break;
128 case CRYPTO_AES_CBC:
129 alg_name = "cbc(aes)";
130 break;
131 case CRYPTO_AES_ECB:
132 alg_name = "ecb(aes)";
133 break;
134 case CRYPTO_CAMELLIA_CBC:
135 alg_name = "cbc(camelia)";
136 break;
137 case CRYPTO_AES_CTR:
138 alg_name = "ctr(aes)";
139 stream = 1;
140 break;
141 case CRYPTO_AES_GCM:
142 alg_name = "gcm(aes)";
143 stream = 1;
144 aead = 1;
145 break;
146 case CRYPTO_NULL:
147 alg_name = "ecb(cipher_null)";
148 stream = 1;
149 break;
150 default:
151 dprintk(1, KERN_DEBUG, "%s: bad cipher: %d\n", __func__,
152 sop->cipher);
153 return -EINVAL;
156 switch (sop->mac) {
157 case 0:
158 break;
159 case CRYPTO_MD5_HMAC:
160 hash_name = "hmac(md5)";
161 break;
162 case CRYPTO_RIPEMD160_HMAC:
163 hash_name = "hmac(rmd160)";
164 break;
165 case CRYPTO_SHA1_HMAC:
166 hash_name = "hmac(sha1)";
167 break;
168 case CRYPTO_SHA2_256_HMAC:
169 hash_name = "hmac(sha256)";
170 break;
171 case CRYPTO_SHA2_384_HMAC:
172 hash_name = "hmac(sha384)";
173 break;
174 case CRYPTO_SHA2_512_HMAC:
175 hash_name = "hmac(sha512)";
176 break;
178 /* non-hmac cases */
179 case CRYPTO_MD5:
180 hash_name = "md5";
181 hmac_mode = 0;
182 break;
183 case CRYPTO_RIPEMD160:
184 hash_name = "rmd160";
185 hmac_mode = 0;
186 break;
187 case CRYPTO_SHA1:
188 hash_name = "sha1";
189 hmac_mode = 0;
190 break;
191 case CRYPTO_SHA2_256:
192 hash_name = "sha256";
193 hmac_mode = 0;
194 break;
195 case CRYPTO_SHA2_384:
196 hash_name = "sha384";
197 hmac_mode = 0;
198 break;
199 case CRYPTO_SHA2_512:
200 hash_name = "sha512";
201 hmac_mode = 0;
202 break;
203 default:
204 dprintk(1, KERN_DEBUG, "%s: bad mac: %d\n", __func__,
205 sop->mac);
206 return -EINVAL;
209 /* Create a session and put it to the list. */
210 ses_new = kzalloc(sizeof(*ses_new), GFP_KERNEL);
211 if (!ses_new)
212 return -ENOMEM;
214 /* Set-up crypto transform. */
215 if (alg_name) {
216 uint8_t keyp[CRYPTO_CIPHER_MAX_KEY_LEN];
218 if (unlikely(sop->keylen > CRYPTO_CIPHER_MAX_KEY_LEN)) {
219 dprintk(1, KERN_DEBUG,
220 "Setting key failed for %s-%zu.\n",
221 alg_name, (size_t)sop->keylen*8);
222 ret = -EINVAL;
223 goto error_cipher;
226 if (unlikely(copy_from_user(keyp, sop->key, sop->keylen))) {
227 ret = -EFAULT;
228 goto error_cipher;
231 ret = cryptodev_cipher_init(&ses_new->cdata, alg_name, keyp,
232 sop->keylen, stream, aead);
233 if (ret < 0) {
234 dprintk(1, KERN_DEBUG,
235 "%s: Failed to load cipher for %s\n",
236 __func__, alg_name);
237 ret = -EINVAL;
238 goto error_cipher;
242 if (hash_name && aead == 0) {
243 uint8_t keyp[CRYPTO_HMAC_MAX_KEY_LEN];
245 if (unlikely(sop->mackeylen > CRYPTO_HMAC_MAX_KEY_LEN)) {
246 dprintk(1, KERN_DEBUG,
247 "Setting key failed for %s-%zu.\n",
248 alg_name, (size_t)sop->mackeylen*8);
249 ret = -EINVAL;
250 goto error_hash;
253 if (sop->mackey && unlikely(copy_from_user(keyp, sop->mackey,
254 sop->mackeylen))) {
255 ret = -EFAULT;
256 goto error_hash;
259 ret = cryptodev_hash_init(&ses_new->hdata, hash_name, hmac_mode,
260 keyp, sop->mackeylen);
261 if (ret != 0) {
262 dprintk(1, KERN_DEBUG,
263 "%s: Failed to load hash for %s\n",
264 __func__, hash_name);
265 ret = -EINVAL;
266 goto error_hash;
270 ses_new->alignmask = max(ses_new->cdata.alignmask,
271 ses_new->hdata.alignmask);
272 dprintk(2, KERN_DEBUG, "%s: got alignmask %d\n", __func__, ses_new->alignmask);
274 ses_new->array_size = DEFAULT_PREALLOC_PAGES;
275 dprintk(2, KERN_DEBUG, "%s: preallocating for %d user pages\n",
276 __func__, ses_new->array_size);
277 ses_new->pages = kzalloc(ses_new->array_size *
278 sizeof(struct page *), GFP_KERNEL);
279 ses_new->sg = kzalloc(ses_new->array_size *
280 sizeof(struct scatterlist), GFP_KERNEL);
281 if (ses_new->sg == NULL || ses_new->pages == NULL) {
282 dprintk(0, KERN_DEBUG, "Memory error\n");
283 ret = -ENOMEM;
284 goto error_hash;
287 /* put the new session to the list */
288 get_random_bytes(&ses_new->sid, sizeof(ses_new->sid));
289 mutex_init(&ses_new->sem);
291 mutex_lock(&fcr->sem);
292 restart:
293 list_for_each_entry(ses_ptr, &fcr->list, entry) {
294 /* Check for duplicate SID */
295 if (unlikely(ses_new->sid == ses_ptr->sid)) {
296 get_random_bytes(&ses_new->sid, sizeof(ses_new->sid));
297 /* Unless we have a broken RNG this
298 shouldn't loop forever... ;-) */
299 goto restart;
303 list_add(&ses_new->entry, &fcr->list);
304 mutex_unlock(&fcr->sem);
306 /* Fill in some values for the user. */
307 sop->ses = ses_new->sid;
309 return 0;
311 error_hash:
312 cryptodev_cipher_deinit(&ses_new->cdata);
313 kfree(ses_new->sg);
314 kfree(ses_new->pages);
315 error_cipher:
316 kfree(ses_new);
318 return ret;
322 /* Everything that needs to be done when remowing a session. */
323 static inline void
324 crypto_destroy_session(struct csession *ses_ptr)
326 if (!mutex_trylock(&ses_ptr->sem)) {
327 dprintk(2, KERN_DEBUG, "Waiting for semaphore of sid=0x%08X\n",
328 ses_ptr->sid);
329 mutex_lock(&ses_ptr->sem);
331 dprintk(2, KERN_DEBUG, "Removed session 0x%08X\n", ses_ptr->sid);
332 cryptodev_cipher_deinit(&ses_ptr->cdata);
333 cryptodev_hash_deinit(&ses_ptr->hdata);
334 dprintk(2, KERN_DEBUG, "%s: freeing space for %d user pages\n",
335 __func__, ses_ptr->array_size);
336 kfree(ses_ptr->pages);
337 kfree(ses_ptr->sg);
338 mutex_unlock(&ses_ptr->sem);
339 kfree(ses_ptr);
342 /* Look up a session by ID and remove. */
343 static int
344 crypto_finish_session(struct fcrypt *fcr, uint32_t sid)
346 struct csession *tmp, *ses_ptr;
347 struct list_head *head;
348 int ret = 0;
350 mutex_lock(&fcr->sem);
351 head = &fcr->list;
352 list_for_each_entry_safe(ses_ptr, tmp, head, entry) {
353 if (ses_ptr->sid == sid) {
354 list_del(&ses_ptr->entry);
355 crypto_destroy_session(ses_ptr);
356 break;
360 if (unlikely(!ses_ptr)) {
361 dprintk(1, KERN_ERR, "Session with sid=0x%08X not found!\n",
362 sid);
363 ret = -ENOENT;
365 mutex_unlock(&fcr->sem);
367 return ret;
370 /* Remove all sessions when closing the file */
371 static int
372 crypto_finish_all_sessions(struct fcrypt *fcr)
374 struct csession *tmp, *ses_ptr;
375 struct list_head *head;
377 mutex_lock(&fcr->sem);
379 head = &fcr->list;
380 list_for_each_entry_safe(ses_ptr, tmp, head, entry) {
381 list_del(&ses_ptr->entry);
382 crypto_destroy_session(ses_ptr);
384 mutex_unlock(&fcr->sem);
386 return 0;
389 /* Look up session by session ID. The returned session is locked. */
390 struct csession *
391 crypto_get_session_by_sid(struct fcrypt *fcr, uint32_t sid)
393 struct csession *ses_ptr, *retval = NULL;
395 if (unlikely(fcr == NULL))
396 return NULL;
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);
402 retval = ses_ptr;
403 break;
406 mutex_unlock(&fcr->sem);
408 return retval;
411 static void cryptask_routine(struct work_struct *work)
413 struct crypt_priv *pcr = container_of(work, struct crypt_priv, cryptask);
414 struct todo_list_item *item;
415 LIST_HEAD(tmp);
417 /* fetch all pending jobs into the temporary list */
418 mutex_lock(&pcr->todo.lock);
419 list_cut_position(&tmp, &pcr->todo.list, pcr->todo.list.prev);
420 mutex_unlock(&pcr->todo.lock);
422 /* handle each job locklessly */
423 list_for_each_entry(item, &tmp, __hook) {
424 item->result = crypto_run(&pcr->fcrypt, &item->kcop);
425 if (unlikely(item->result))
426 dprintk(0, KERN_ERR, "%s: crypto_run() failed: %d\n",
427 __func__, item->result);
430 /* push all handled jobs to the done list at once */
431 mutex_lock(&pcr->done.lock);
432 list_splice_tail(&tmp, &pcr->done.list);
433 mutex_unlock(&pcr->done.lock);
435 /* wake for POLLIN */
436 wake_up_interruptible(&pcr->user_waiter);
439 /* ====== /dev/crypto ====== */
441 static int
442 cryptodev_open(struct inode *inode, struct file *filp)
444 struct todo_list_item *tmp;
445 struct crypt_priv *pcr;
446 int i;
448 pcr = kmalloc(sizeof(*pcr), GFP_KERNEL);
449 if (!pcr)
450 return -ENOMEM;
452 memset(pcr, 0, sizeof(*pcr));
453 mutex_init(&pcr->fcrypt.sem);
454 INIT_LIST_HEAD(&pcr->fcrypt.list);
456 INIT_LIST_HEAD(&pcr->free.list);
457 INIT_LIST_HEAD(&pcr->todo.list);
458 INIT_LIST_HEAD(&pcr->done.list);
459 INIT_WORK(&pcr->cryptask, cryptask_routine);
460 mutex_init(&pcr->free.lock);
461 mutex_init(&pcr->todo.lock);
462 mutex_init(&pcr->done.lock);
463 init_waitqueue_head(&pcr->user_waiter);
465 for (i = 0; i < DEF_COP_RINGSIZE; i++) {
466 tmp = kzalloc(sizeof(struct todo_list_item), GFP_KERNEL);
467 pcr->itemcount++;
468 dprintk(2, KERN_DEBUG, "%s: allocated new item at %lx\n",
469 __func__, (unsigned long)tmp);
470 list_add(&tmp->__hook, &pcr->free.list);
473 filp->private_data = pcr;
474 dprintk(2, KERN_DEBUG,
475 "Cryptodev handle initialised, %d elements in queue\n",
476 DEF_COP_RINGSIZE);
477 return 0;
480 static int
481 cryptodev_release(struct inode *inode, struct file *filp)
483 struct crypt_priv *pcr = filp->private_data;
484 struct todo_list_item *item, *item_safe;
485 int items_freed = 0;
487 if (!pcr)
488 return 0;
490 cancel_work_sync(&pcr->cryptask);
492 mutex_destroy(&pcr->todo.lock);
493 mutex_destroy(&pcr->done.lock);
494 mutex_destroy(&pcr->free.lock);
496 list_splice_tail(&pcr->todo.list, &pcr->free.list);
497 list_splice_tail(&pcr->done.list, &pcr->free.list);
499 list_for_each_entry_safe(item, item_safe, &pcr->free.list, __hook) {
500 dprintk(2, KERN_DEBUG, "%s: freeing item at %lx\n",
501 __func__, (unsigned long)item);
502 list_del(&item->__hook);
503 kfree(item);
504 items_freed++;
507 if (items_freed != pcr->itemcount) {
508 dprintk(0, KERN_ERR,
509 "%s: freed %d items, but %d should exist!\n",
510 __func__, items_freed, pcr->itemcount);
513 crypto_finish_all_sessions(&pcr->fcrypt);
514 kfree(pcr);
515 filp->private_data = NULL;
517 dprintk(2, KERN_DEBUG,
518 "Cryptodev handle deinitialised, %d elements freed\n",
519 items_freed);
520 return 0;
523 static int
524 clonefd(struct file *filp)
526 int ret;
527 ret = get_unused_fd();
528 if (ret >= 0) {
529 get_file(filp);
530 fd_install(ret, filp);
533 return ret;
536 /* enqueue a job for asynchronous completion
538 * returns:
539 * -EBUSY when there are no free queue slots left
540 * (and the number of slots has reached it MAX_COP_RINGSIZE)
541 * -EFAULT when there was a memory allocation error
542 * 0 on success */
543 static int crypto_async_run(struct crypt_priv *pcr, struct kernel_crypt_op *kcop)
545 struct todo_list_item *item = NULL;
547 mutex_lock(&pcr->free.lock);
548 if (likely(!list_empty(&pcr->free.list))) {
549 item = list_first_entry(&pcr->free.list,
550 struct todo_list_item, __hook);
551 list_del(&item->__hook);
552 } else if (pcr->itemcount < MAX_COP_RINGSIZE) {
553 pcr->itemcount++;
554 } else {
555 mutex_unlock(&pcr->free.lock);
556 return -EBUSY;
558 mutex_unlock(&pcr->free.lock);
560 if (unlikely(!item)) {
561 item = kzalloc(sizeof(struct todo_list_item), GFP_KERNEL);
562 if (unlikely(!item))
563 return -EFAULT;
564 dprintk(1, KERN_INFO, "%s: increased item count to %d\n",
565 __func__, pcr->itemcount);
568 memcpy(&item->kcop, kcop, sizeof(struct kernel_crypt_op));
570 mutex_lock(&pcr->todo.lock);
571 list_add_tail(&item->__hook, &pcr->todo.list);
572 mutex_unlock(&pcr->todo.lock);
574 queue_work(cryptodev_wq, &pcr->cryptask);
575 return 0;
578 /* get the first completed job from the "done" queue
580 * returns:
581 * -EBUSY if no completed jobs are ready (yet)
582 * the return value of crypto_run() otherwise */
583 static int crypto_async_fetch(struct crypt_priv *pcr,
584 struct kernel_crypt_op *kcop)
586 struct todo_list_item *item;
587 int retval;
589 mutex_lock(&pcr->done.lock);
590 if (list_empty(&pcr->done.list)) {
591 mutex_unlock(&pcr->done.lock);
592 return -EBUSY;
594 item = list_first_entry(&pcr->done.list, struct todo_list_item, __hook);
595 list_del(&item->__hook);
596 mutex_unlock(&pcr->done.lock);
598 memcpy(kcop, &item->kcop, sizeof(struct kernel_crypt_op));
599 retval = item->result;
601 mutex_lock(&pcr->free.lock);
602 list_add_tail(&item->__hook, &pcr->free.list);
603 mutex_unlock(&pcr->free.lock);
605 /* wake for POLLOUT */
606 wake_up_interruptible(&pcr->user_waiter);
608 return retval;
611 /* this function has to be called from process context */
612 static int fill_kcop_from_cop(struct kernel_crypt_op *kcop, struct fcrypt *fcr)
614 struct crypt_op *cop = &kcop->cop;
615 struct csession *ses_ptr;
616 int rc;
618 /* this also enters ses_ptr->sem */
619 ses_ptr = crypto_get_session_by_sid(fcr, cop->ses);
620 if (unlikely(!ses_ptr)) {
621 dprintk(1, KERN_ERR, "invalid session ID=0x%08X\n", cop->ses);
622 return -EINVAL;
624 kcop->ivlen = cop->iv ? ses_ptr->cdata.ivsize : 0;
625 kcop->digestsize = 0; /* will be updated during operation */
627 crypto_put_session(ses_ptr);
629 kcop->task = current;
630 kcop->mm = current->mm;
632 if (cop->iv) {
633 rc = copy_from_user(kcop->iv, cop->iv, kcop->ivlen);
634 if (unlikely(rc)) {
635 dprintk(1, KERN_ERR,
636 "error copying IV (%d bytes), copy_from_user returned %d for address %lx\n",
637 kcop->ivlen, rc, (unsigned long)cop->iv);
638 return -EFAULT;
642 return 0;
645 /* this function has to be called from process context */
646 static int fill_cop_from_kcop(struct kernel_crypt_op *kcop, struct fcrypt *fcr)
648 int ret;
650 if (kcop->digestsize) {
651 ret = copy_to_user(kcop->cop.mac,
652 kcop->hash_output, kcop->digestsize);
653 if (unlikely(ret))
654 return -EFAULT;
656 if (kcop->ivlen && kcop->cop.flags & COP_FLAG_WRITE_IV) {
657 ret = copy_to_user(kcop->cop.iv,
658 kcop->iv, kcop->ivlen);
659 if (unlikely(ret))
660 return -EFAULT;
662 return 0;
665 static int kcop_from_user(struct kernel_crypt_op *kcop,
666 struct fcrypt *fcr, void __user *arg)
668 if (unlikely(copy_from_user(&kcop->cop, arg, sizeof(kcop->cop))))
669 return -EFAULT;
671 return fill_kcop_from_cop(kcop, fcr);
674 static int kcop_to_user(struct kernel_crypt_op *kcop,
675 struct fcrypt *fcr, void __user *arg)
677 int ret;
679 ret = fill_cop_from_kcop(kcop, fcr);
680 if (unlikely(ret))
681 return ret;
683 if (unlikely(copy_to_user(arg, &kcop->cop, sizeof(kcop->cop))))
684 return -EFAULT;
685 return 0;
688 static inline void tfm_info_to_alg_info(struct alg_info *dst, struct crypto_tfm *tfm)
690 snprintf(dst->cra_name, CRYPTODEV_MAX_ALG_NAME,
691 "%s", crypto_tfm_alg_name(tfm));
692 snprintf(dst->cra_driver_name, CRYPTODEV_MAX_ALG_NAME,
693 "%s", crypto_tfm_alg_driver_name(tfm));
696 static int get_session_info(struct fcrypt *fcr, struct session_info_op *siop)
698 struct csession *ses_ptr;
700 /* this also enters ses_ptr->sem */
701 ses_ptr = crypto_get_session_by_sid(fcr, siop->ses);
702 if (unlikely(!ses_ptr)) {
703 dprintk(1, KERN_ERR, "invalid session ID=0x%08X\n", siop->ses);
704 return -EINVAL;
707 if (ses_ptr->cdata.init) {
708 if (ses_ptr->cdata.aead == 0)
709 tfm_info_to_alg_info(&siop->cipher_info,
710 crypto_ablkcipher_tfm(ses_ptr->cdata.async.s));
711 else
712 tfm_info_to_alg_info(&siop->cipher_info,
713 crypto_aead_tfm(ses_ptr->cdata.async.as));
715 if (ses_ptr->hdata.init) {
716 tfm_info_to_alg_info(&siop->hash_info,
717 crypto_ahash_tfm(ses_ptr->hdata.async.s));
720 siop->alignmask = ses_ptr->alignmask;
722 crypto_put_session(ses_ptr);
723 return 0;
726 static long
727 cryptodev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg_)
729 void __user *arg = (void __user *)arg_;
730 int __user *p = arg;
731 struct session_op sop;
732 struct kernel_crypt_op kcop;
733 struct kernel_crypt_auth_op kcaop;
734 struct crypt_priv *pcr = filp->private_data;
735 struct fcrypt *fcr;
736 struct session_info_op siop;
737 uint32_t ses;
738 int ret, fd;
740 if (unlikely(!pcr))
741 BUG();
743 fcr = &pcr->fcrypt;
745 switch (cmd) {
746 case CIOCASYMFEAT:
747 return put_user(0, p);
748 case CRIOGET:
749 fd = clonefd(filp);
750 ret = put_user(fd, p);
751 if (unlikely(ret)) {
752 sys_close(fd);
753 return ret;
755 return ret;
756 case CIOCGSESSION:
757 if (unlikely(copy_from_user(&sop, arg, sizeof(sop))))
758 return -EFAULT;
760 ret = crypto_create_session(fcr, &sop);
761 if (unlikely(ret))
762 return ret;
763 ret = copy_to_user(arg, &sop, sizeof(sop));
764 if (unlikely(ret)) {
765 crypto_finish_session(fcr, sop.ses);
766 return -EFAULT;
768 return ret;
769 case CIOCFSESSION:
770 ret = get_user(ses, (uint32_t __user *)arg);
771 if (unlikely(ret))
772 return ret;
773 ret = crypto_finish_session(fcr, ses);
774 return ret;
775 case CIOCGSESSINFO:
776 if (unlikely(copy_from_user(&siop, arg, sizeof(siop))))
777 return -EFAULT;
779 ret = get_session_info(fcr, &siop);
780 if (unlikely(ret))
781 return ret;
782 return copy_to_user(arg, &siop, sizeof(siop));
783 case CIOCCRYPT:
784 if (unlikely(ret = kcop_from_user(&kcop, fcr, arg))) {
785 dprintk(1, KERN_WARNING, "Error copying from user");
786 return ret;
789 ret = crypto_run(fcr, &kcop);
790 if (unlikely(ret)) {
791 dprintk(1, KERN_WARNING, "Error in crypto_run");
792 return ret;
795 return kcop_to_user(&kcop, fcr, arg);
796 case CIOCAUTHCRYPT:
797 if (unlikely(ret = kcaop_from_user(&kcaop, fcr, arg))) {
798 dprintk(1, KERN_WARNING, "Error copying from user");
799 return ret;
802 ret = crypto_auth_run(fcr, &kcaop);
803 if (unlikely(ret)) {
804 dprintk(1, KERN_WARNING, "Error in crypto_auth_run");
805 return ret;
807 return kcaop_to_user(&kcaop, fcr, arg);
808 case CIOCASYNCCRYPT:
809 if (unlikely(ret = kcop_from_user(&kcop, fcr, arg)))
810 return ret;
812 return crypto_async_run(pcr, &kcop);
813 case CIOCASYNCFETCH:
814 ret = crypto_async_fetch(pcr, &kcop);
815 if (unlikely(ret))
816 return ret;
818 return kcop_to_user(&kcop, fcr, arg);
819 default:
820 return -EINVAL;
824 /* compatibility code for 32bit userlands */
825 #ifdef CONFIG_COMPAT
827 static inline void
828 compat_to_session_op(struct compat_session_op *compat, struct session_op *sop)
830 sop->cipher = compat->cipher;
831 sop->mac = compat->mac;
832 sop->keylen = compat->keylen;
834 sop->key = compat_ptr(compat->key);
835 sop->mackeylen = compat->mackeylen;
836 sop->mackey = compat_ptr(compat->mackey);
837 sop->ses = compat->ses;
840 static inline void
841 session_op_to_compat(struct session_op *sop, struct compat_session_op *compat)
843 compat->cipher = sop->cipher;
844 compat->mac = sop->mac;
845 compat->keylen = sop->keylen;
847 compat->key = ptr_to_compat(sop->key);
848 compat->mackeylen = sop->mackeylen;
849 compat->mackey = ptr_to_compat(sop->mackey);
850 compat->ses = sop->ses;
853 static inline void
854 compat_to_crypt_op(struct compat_crypt_op *compat, struct crypt_op *cop)
856 cop->ses = compat->ses;
857 cop->op = compat->op;
858 cop->flags = compat->flags;
859 cop->len = compat->len;
861 cop->src = compat_ptr(compat->src);
862 cop->dst = compat_ptr(compat->dst);
863 cop->mac = compat_ptr(compat->mac);
864 cop->iv = compat_ptr(compat->iv);
867 static inline void
868 crypt_op_to_compat(struct crypt_op *cop, struct compat_crypt_op *compat)
870 compat->ses = cop->ses;
871 compat->op = cop->op;
872 compat->flags = cop->flags;
873 compat->len = cop->len;
875 compat->src = ptr_to_compat(cop->src);
876 compat->dst = ptr_to_compat(cop->dst);
877 compat->mac = ptr_to_compat(cop->mac);
878 compat->iv = ptr_to_compat(cop->iv);
881 static int compat_kcop_from_user(struct kernel_crypt_op *kcop,
882 struct fcrypt *fcr, void __user *arg)
884 struct compat_crypt_op compat_cop;
886 if (unlikely(copy_from_user(&compat_cop, arg, sizeof(compat_cop))))
887 return -EFAULT;
888 compat_to_crypt_op(&compat_cop, &kcop->cop);
890 return fill_kcop_from_cop(kcop, fcr);
893 static int compat_kcop_to_user(struct kernel_crypt_op *kcop,
894 struct fcrypt *fcr, void __user *arg)
896 int ret;
897 struct compat_crypt_op compat_cop;
899 ret = fill_cop_from_kcop(kcop, fcr);
900 if (unlikely(ret)) {
901 dprintk(1, KERN_WARNING, "Error in fill_cop_from_kcop");
902 return ret;
904 crypt_op_to_compat(&kcop->cop, &compat_cop);
906 if (unlikely(copy_to_user(arg, &compat_cop, sizeof(compat_cop)))) {
907 dprintk(1, KERN_WARNING, "Error copying to user");
908 return -EFAULT;
910 return 0;
913 static long
914 cryptodev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg_)
916 void __user *arg = (void __user *)arg_;
917 struct crypt_priv *pcr = file->private_data;
918 struct fcrypt *fcr;
919 struct session_op sop;
920 struct compat_session_op compat_sop;
921 struct kernel_crypt_op kcop;
922 int ret;
924 if (unlikely(!pcr))
925 BUG();
927 fcr = &pcr->fcrypt;
929 switch (cmd) {
930 case CIOCASYMFEAT:
931 case CRIOGET:
932 case CIOCFSESSION:
933 case CIOCGSESSINFO:
934 return cryptodev_ioctl(file, cmd, arg_);
936 case COMPAT_CIOCGSESSION:
937 if (unlikely(copy_from_user(&compat_sop, arg,
938 sizeof(compat_sop))))
939 return -EFAULT;
940 compat_to_session_op(&compat_sop, &sop);
942 ret = crypto_create_session(fcr, &sop);
943 if (unlikely(ret))
944 return ret;
946 session_op_to_compat(&sop, &compat_sop);
947 ret = copy_to_user(arg, &compat_sop, sizeof(compat_sop));
948 if (unlikely(ret)) {
949 crypto_finish_session(fcr, sop.ses);
950 return -EFAULT;
952 return ret;
954 case COMPAT_CIOCCRYPT:
955 ret = compat_kcop_from_user(&kcop, fcr, arg);
956 if (unlikely(ret))
957 return ret;
959 ret = crypto_run(fcr, &kcop);
960 if (unlikely(ret))
961 return ret;
963 return compat_kcop_to_user(&kcop, fcr, arg);
964 case COMPAT_CIOCASYNCCRYPT:
965 if (unlikely(ret = compat_kcop_from_user(&kcop, fcr, arg)))
966 return ret;
968 return crypto_async_run(pcr, &kcop);
969 case COMPAT_CIOCASYNCFETCH:
970 ret = crypto_async_fetch(pcr, &kcop);
971 if (unlikely(ret))
972 return ret;
974 return compat_kcop_to_user(&kcop, fcr, arg);
976 default:
977 return -EINVAL;
981 #endif /* CONFIG_COMPAT */
983 static unsigned int cryptodev_poll(struct file *file, poll_table *wait)
985 struct crypt_priv *pcr = file->private_data;
986 int ret = 0;
988 poll_wait(file, &pcr->user_waiter, wait);
990 if (!list_empty_careful(&pcr->done.list))
991 ret |= POLLIN | POLLRDNORM;
992 if (!list_empty_careful(&pcr->free.list) || pcr->itemcount < MAX_COP_RINGSIZE)
993 ret |= POLLOUT | POLLWRNORM;
995 return ret;
998 static const struct file_operations cryptodev_fops = {
999 .owner = THIS_MODULE,
1000 .open = cryptodev_open,
1001 .release = cryptodev_release,
1002 .unlocked_ioctl = cryptodev_ioctl,
1003 #ifdef CONFIG_COMPAT
1004 .compat_ioctl = cryptodev_compat_ioctl,
1005 #endif /* CONFIG_COMPAT */
1006 .poll = cryptodev_poll,
1009 static struct miscdevice cryptodev = {
1010 .minor = MISC_DYNAMIC_MINOR,
1011 .name = "crypto",
1012 .fops = &cryptodev_fops,
1015 static int __init
1016 cryptodev_register(void)
1018 int rc;
1020 rc = misc_register(&cryptodev);
1021 if (unlikely(rc)) {
1022 printk(KERN_ERR PFX "registration of /dev/crypto failed\n");
1023 return rc;
1026 return 0;
1029 static void __exit
1030 cryptodev_deregister(void)
1032 misc_deregister(&cryptodev);
1035 /* ====== Module init/exit ====== */
1036 static int __init init_cryptodev(void)
1038 int rc;
1040 cryptodev_wq = create_workqueue("cryptodev_queue");
1041 if (unlikely(!cryptodev_wq)) {
1042 printk(KERN_ERR PFX "failed to allocate the cryptodev workqueue\n");
1043 return -EFAULT;
1046 rc = cryptodev_register();
1047 if (unlikely(rc)) {
1048 destroy_workqueue(cryptodev_wq);
1049 return rc;
1052 printk(KERN_INFO PFX "driver %s loaded.\n", VERSION);
1054 return 0;
1057 static void __exit exit_cryptodev(void)
1059 flush_workqueue(cryptodev_wq);
1060 destroy_workqueue(cryptodev_wq);
1062 cryptodev_deregister();
1063 printk(KERN_INFO PFX "driver unloaded.\n");
1066 module_init(init_cryptodev);
1067 module_exit(exit_cryptodev);