2 * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/crypto.h>
17 #include <linux/highmem.h>
18 #include <linux/kthread.h>
19 #include <linux/pagemap.h>
20 #include <linux/slab.h>
24 static struct crypto_hash *pohmelfs_init_hash(struct pohmelfs_sb *psb)
27 struct crypto_hash *hash;
29 hash = crypto_alloc_hash(psb->hash_string, 0, CRYPTO_ALG_ASYNC);
32 dprintk("%s: idx: %u: failed to allocate hash '%s', err: %d.\n",
33 __func__, psb->idx, psb->hash_string, err);
37 psb->crypto_attached_size = crypto_hash_digestsize(hash);
39 if (!psb->hash_keysize)
42 err = crypto_hash_setkey(hash, psb->hash_key, psb->hash_keysize);
44 dprintk("%s: idx: %u: failed to set key for hash '%s', err: %d.\n",
45 __func__, psb->idx, psb->hash_string, err);
52 crypto_free_hash(hash);
57 static struct crypto_ablkcipher *pohmelfs_init_cipher(struct pohmelfs_sb *psb)
60 struct crypto_ablkcipher *cipher;
62 if (!psb->cipher_keysize)
65 cipher = crypto_alloc_ablkcipher(psb->cipher_string, 0, 0);
67 err = PTR_ERR(cipher);
68 dprintk("%s: idx: %u: failed to allocate cipher '%s', err: %d.\n",
69 __func__, psb->idx, psb->cipher_string, err);
73 crypto_ablkcipher_clear_flags(cipher, ~0);
75 err = crypto_ablkcipher_setkey(cipher, psb->cipher_key, psb->cipher_keysize);
77 dprintk("%s: idx: %u: failed to set key for cipher '%s', err: %d.\n",
78 __func__, psb->idx, psb->cipher_string, err);
85 crypto_free_ablkcipher(cipher);
90 int pohmelfs_crypto_engine_init(struct pohmelfs_crypto_engine *e, struct pohmelfs_sb *psb)
97 e->data = kmalloc(e->size, GFP_KERNEL);
103 if (psb->hash_string) {
104 e->hash = pohmelfs_init_hash(psb);
105 if (IS_ERR(e->hash)) {
106 err = PTR_ERR(e->hash);
112 if (psb->cipher_string) {
113 e->cipher = pohmelfs_init_cipher(psb);
114 if (IS_ERR(e->cipher)) {
115 err = PTR_ERR(e->cipher);
117 goto err_out_free_hash;
124 crypto_free_hash(e->hash);
131 void pohmelfs_crypto_engine_exit(struct pohmelfs_crypto_engine *e)
134 crypto_free_hash(e->hash);
136 crypto_free_ablkcipher(e->cipher);
140 static void pohmelfs_crypto_complete(struct crypto_async_request *req, int err)
142 struct pohmelfs_crypto_completion *c = req->data;
144 if (err == -EINPROGRESS)
147 dprintk("%s: req: %p, err: %d.\n", __func__, req, err);
149 complete(&c->complete);
152 static int pohmelfs_crypto_process(struct ablkcipher_request *req,
153 struct scatterlist *sg_dst, struct scatterlist *sg_src,
154 void *iv, int enc, unsigned long timeout)
156 struct pohmelfs_crypto_completion complete;
159 init_completion(&complete.complete);
160 complete.error = -EINPROGRESS;
162 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
163 pohmelfs_crypto_complete, &complete);
165 ablkcipher_request_set_crypt(req, sg_src, sg_dst, sg_src->length, iv);
168 err = crypto_ablkcipher_encrypt(req);
170 err = crypto_ablkcipher_decrypt(req);
175 err = wait_for_completion_interruptible_timeout(&complete.complete,
180 err = complete.error;
189 int pohmelfs_crypto_process_input_data(struct pohmelfs_crypto_engine *e, u64 cmd_iv,
190 void *data, struct page *page, unsigned int size)
193 struct scatterlist sg;
195 if (!e->cipher && !e->hash)
198 dprintk("%s: eng: %p, iv: %llx, data: %p, page: %p/%lu, size: %u.\n",
199 __func__, e, cmd_iv, data, page, (page)?page->index:0, size);
202 sg_init_one(&sg, data, size);
204 sg_init_table(&sg, 1);
205 sg_set_page(&sg, page, size, 0);
209 struct ablkcipher_request *req = e->data + crypto_hash_digestsize(e->hash);
212 memset(iv, 0, sizeof(iv));
213 memcpy(iv, &cmd_iv, sizeof(cmd_iv));
215 ablkcipher_request_set_tfm(req, e->cipher);
217 err = pohmelfs_crypto_process(req, &sg, &sg, iv, 0, e->timeout);
223 struct hash_desc desc;
224 void *dst = e->data + e->size/2;
229 err = crypto_hash_init(&desc);
233 err = crypto_hash_update(&desc, &sg, size);
237 err = crypto_hash_final(&desc, dst);
241 err = !!memcmp(dst, e->data, crypto_hash_digestsize(e->hash));
244 #ifdef CONFIG_POHMELFS_DEBUG
246 unsigned char *recv = e->data, *calc = dst;
248 dprintk("%s: eng: %p, hash: %p, cipher: %p: iv : %llx, hash mismatch (recv/calc): ",
249 __func__, e, e->hash, e->cipher, cmd_iv);
250 for (i=0; i<crypto_hash_digestsize(e->hash); ++i) {
252 dprintka("%02x ", recv[i]);
253 if (recv[i] != calc[i]) {
254 dprintka("| calc byte: %02x.\n", calc[i]);
258 dprintka("%02x/%02x ", recv[i], calc[i]);
265 dprintk("%s: eng: %p, hash: %p, cipher: %p: hashes matched.\n",
266 __func__, e, e->hash, e->cipher);
270 dprintk("%s: eng: %p, size: %u, hash: %p, cipher: %p: completed.\n",
271 __func__, e, e->size, e->hash, e->cipher);
276 dprintk("%s: eng: %p, hash: %p, cipher: %p: err: %d.\n",
277 __func__, e, e->hash, e->cipher, err);
281 static int pohmelfs_trans_iter(struct netfs_trans *t, struct pohmelfs_crypto_engine *e,
282 int (* iterator) (struct pohmelfs_crypto_engine *e,
283 struct scatterlist *dst,
284 struct scatterlist *src))
286 void *data = t->iovec.iov_base + sizeof(struct netfs_cmd) + t->psb->crypto_attached_size;
287 unsigned int size = t->iovec.iov_len - sizeof(struct netfs_cmd) - t->psb->crypto_attached_size;
288 struct netfs_cmd *cmd = data;
289 unsigned int sz, pages = t->attached_pages, i, csize, cmd_cmd, dpage_idx;
290 struct scatterlist sg_src, sg_dst;
295 cmd_cmd = __be16_to_cpu(cmd->cmd);
296 csize = __be32_to_cpu(cmd->size);
297 cmd->iv = __cpu_to_be64(e->iv);
299 if (cmd_cmd == NETFS_READ_PAGES || cmd_cmd == NETFS_READ_PAGE)
300 csize = __be16_to_cpu(cmd->ext);
302 sz = csize + __be16_to_cpu(cmd->cpad) + sizeof(struct netfs_cmd);
304 dprintk("%s: size: %u, sz: %u, cmd_size: %u, cmd_cpad: %u.\n",
305 __func__, size, sz, __be32_to_cpu(cmd->size), __be16_to_cpu(cmd->cpad));
310 sg_init_one(&sg_src, cmd->data, sz - sizeof(struct netfs_cmd));
311 sg_init_one(&sg_dst, cmd->data, sz - sizeof(struct netfs_cmd));
313 err = iterator(e, &sg_dst, &sg_src);
322 for (i=0; i<t->page_num; ++i) {
323 struct page *page = t->pages[i];
324 struct page *dpage = e->pages[dpage_idx];
329 sg_init_table(&sg_src, 1);
330 sg_init_table(&sg_dst, 1);
331 sg_set_page(&sg_src, page, page_private(page), 0);
332 sg_set_page(&sg_dst, dpage, page_private(page), 0);
334 err = iterator(e, &sg_dst, &sg_src);
347 static int pohmelfs_encrypt_iterator(struct pohmelfs_crypto_engine *e,
348 struct scatterlist *sg_dst, struct scatterlist *sg_src)
350 struct ablkcipher_request *req = e->data;
353 memset(iv, 0, sizeof(iv));
355 memcpy(iv, &e->iv, sizeof(e->iv));
357 return pohmelfs_crypto_process(req, sg_dst, sg_src, iv, 1, e->timeout);
360 static int pohmelfs_encrypt(struct pohmelfs_crypto_thread *tc)
362 struct netfs_trans *t = tc->trans;
363 struct pohmelfs_crypto_engine *e = &tc->eng;
364 struct ablkcipher_request *req = e->data;
366 memset(req, 0, sizeof(struct ablkcipher_request));
367 ablkcipher_request_set_tfm(req, e->cipher);
369 e->iv = pohmelfs_gen_iv(t);
371 return pohmelfs_trans_iter(t, e, pohmelfs_encrypt_iterator);
374 static int pohmelfs_hash_iterator(struct pohmelfs_crypto_engine *e,
375 struct scatterlist *sg_dst, struct scatterlist *sg_src)
377 return crypto_hash_update(e->data, sg_src, sg_src->length);
380 static int pohmelfs_hash(struct pohmelfs_crypto_thread *tc)
382 struct pohmelfs_crypto_engine *e = &tc->eng;
383 struct hash_desc *desc = e->data;
384 unsigned char *dst = tc->trans->iovec.iov_base + sizeof(struct netfs_cmd);
390 err = crypto_hash_init(desc);
394 err = pohmelfs_trans_iter(tc->trans, e, pohmelfs_hash_iterator);
398 err = crypto_hash_final(desc, dst);
404 dprintk("%s: ", __func__);
405 for (i=0; i<tc->psb->crypto_attached_size; ++i)
406 dprintka("%02x ", dst[i]);
413 static void pohmelfs_crypto_pages_free(struct pohmelfs_crypto_engine *e)
417 for (i=0; i<e->page_num; ++i)
418 __free_page(e->pages[i]);
422 static int pohmelfs_crypto_pages_alloc(struct pohmelfs_crypto_engine *e, struct pohmelfs_sb *psb)
426 e->pages = kmalloc(psb->trans_max_pages * sizeof(struct page *), GFP_KERNEL);
430 for (i=0; i<psb->trans_max_pages; ++i) {
431 e->pages[i] = alloc_page(GFP_KERNEL);
447 static void pohmelfs_sys_crypto_exit_one(struct pohmelfs_crypto_thread *t)
449 struct pohmelfs_sb *psb = t->psb;
452 kthread_stop(t->thread);
454 mutex_lock(&psb->crypto_thread_lock);
455 list_del(&t->thread_entry);
456 psb->crypto_thread_num--;
457 mutex_unlock(&psb->crypto_thread_lock);
459 pohmelfs_crypto_engine_exit(&t->eng);
460 pohmelfs_crypto_pages_free(&t->eng);
464 static int pohmelfs_crypto_finish(struct netfs_trans *t, struct pohmelfs_sb *psb, int err)
466 struct netfs_cmd *cmd = t->iovec.iov_base;
467 netfs_convert_cmd(cmd);
470 err = netfs_trans_finish_send(t, psb);
478 void pohmelfs_crypto_thread_make_ready(struct pohmelfs_crypto_thread *th)
480 struct pohmelfs_sb *psb = th->psb;
485 mutex_lock(&psb->crypto_thread_lock);
486 list_move_tail(&th->thread_entry, &psb->crypto_ready_list);
487 mutex_unlock(&psb->crypto_thread_lock);
491 static int pohmelfs_crypto_thread_trans(struct pohmelfs_crypto_thread *t)
493 struct netfs_trans *trans;
500 err = pohmelfs_hash(t);
506 err = pohmelfs_encrypt(t);
509 trans->eng = &t->eng;
517 pohmelfs_crypto_thread_make_ready(t);
519 pohmelfs_crypto_finish(trans, t->psb, err);
523 static int pohmelfs_crypto_thread_page(struct pohmelfs_crypto_thread *t)
525 struct pohmelfs_crypto_engine *e = &t->eng;
526 struct page *page = t->page;
529 WARN_ON(!PageChecked(page));
531 err = pohmelfs_crypto_process_input_data(e, e->iv, NULL, page, t->size);
533 SetPageUptodate(page);
537 page_cache_release(page);
539 pohmelfs_crypto_thread_make_ready(t);
544 static int pohmelfs_crypto_thread_func(void *data)
546 struct pohmelfs_crypto_thread *t = data;
548 while (!kthread_should_stop()) {
549 wait_event_interruptible(t->wait, kthread_should_stop() ||
550 t->trans || t->page);
552 if (kthread_should_stop())
555 if (!t->trans && !t->page)
558 dprintk("%s: thread: %p, trans: %p, page: %p.\n",
559 __func__, t, t->trans, t->page);
562 pohmelfs_crypto_thread_trans(t);
564 pohmelfs_crypto_thread_page(t);
570 static void pohmelfs_crypto_flush(struct pohmelfs_sb *psb, struct list_head *head)
572 while (!list_empty(head)) {
573 struct pohmelfs_crypto_thread *t = NULL;
575 mutex_lock(&psb->crypto_thread_lock);
576 if (!list_empty(head)) {
577 t = list_first_entry(head, struct pohmelfs_crypto_thread, thread_entry);
578 list_del_init(&t->thread_entry);
580 mutex_unlock(&psb->crypto_thread_lock);
583 pohmelfs_sys_crypto_exit_one(t);
587 static void pohmelfs_sys_crypto_exit(struct pohmelfs_sb *psb)
589 while (!list_empty(&psb->crypto_active_list) || !list_empty(&psb->crypto_ready_list)) {
590 dprintk("%s: crypto_thread_num: %u.\n", __func__, psb->crypto_thread_num);
591 pohmelfs_crypto_flush(psb, &psb->crypto_active_list);
592 pohmelfs_crypto_flush(psb, &psb->crypto_ready_list);
596 static int pohmelfs_sys_crypto_init(struct pohmelfs_sb *psb)
599 struct pohmelfs_crypto_thread *t;
600 struct pohmelfs_config *c;
601 struct netfs_state *st;
604 list_for_each_entry(c, &psb->state_list, config_entry) {
607 err = pohmelfs_crypto_engine_init(&st->eng, psb);
611 dprintk("%s: st: %p, eng: %p, hash: %p, cipher: %p.\n",
612 __func__, st, &st->eng, &st->eng.hash, &st->eng.cipher);
615 for (i=0; i<psb->crypto_thread_num; ++i) {
617 t = kzalloc(sizeof(struct pohmelfs_crypto_thread), GFP_KERNEL);
619 goto err_out_free_state_engines;
621 init_waitqueue_head(&t->wait);
627 err = pohmelfs_crypto_engine_init(&t->eng, psb);
629 goto err_out_free_state_engines;
631 err = pohmelfs_crypto_pages_alloc(&t->eng, psb);
635 t->thread = kthread_run(pohmelfs_crypto_thread_func, t,
636 "pohmelfs-crypto-%d-%d", psb->idx, i);
637 if (IS_ERR(t->thread)) {
638 err = PTR_ERR(t->thread);
644 psb->crypto_align_size = crypto_ablkcipher_blocksize(t->eng.cipher);
646 mutex_lock(&psb->crypto_thread_lock);
647 list_add_tail(&t->thread_entry, &psb->crypto_ready_list);
648 mutex_unlock(&psb->crypto_thread_lock);
651 psb->crypto_thread_num = i;
655 pohmelfs_sys_crypto_exit_one(t);
656 err_out_free_state_engines:
657 list_for_each_entry(c, &psb->state_list, config_entry) {
659 pohmelfs_crypto_engine_exit(&st->eng);
662 pohmelfs_sys_crypto_exit(psb);
666 void pohmelfs_crypto_exit(struct pohmelfs_sb *psb)
668 pohmelfs_sys_crypto_exit(psb);
670 kfree(psb->hash_string);
671 kfree(psb->cipher_string);
674 static int pohmelfs_crypt_init_complete(struct page **pages, unsigned int page_num,
675 void *private, int err)
677 struct pohmelfs_sb *psb = private;
680 dprintk("%s: err: %d.\n", __func__, err);
687 static int pohmelfs_crypto_init_handshake(struct pohmelfs_sb *psb)
689 struct netfs_trans *t;
690 struct netfs_crypto_capabilities *cap;
691 struct netfs_cmd *cmd;
693 int err = -ENOMEM, size;
695 size = sizeof(struct netfs_crypto_capabilities) +
696 psb->cipher_strlen + psb->hash_strlen + 2; /* 0 bytes */
698 t = netfs_trans_alloc(psb, size, 0, 0);
702 t->complete = pohmelfs_crypt_init_complete;
705 cmd = netfs_trans_current(t);
706 cap = (struct netfs_crypto_capabilities *)(cmd + 1);
707 str = (char *)(cap + 1);
709 cmd->cmd = NETFS_CAPABILITIES;
710 cmd->id = POHMELFS_CRYPTO_CAPABILITIES;
716 netfs_convert_cmd(cmd);
717 netfs_trans_update(cmd, t, size);
719 cap->hash_strlen = psb->hash_strlen;
720 if (cap->hash_strlen) {
721 sprintf(str, "%s", psb->hash_string);
722 str += cap->hash_strlen;
725 cap->cipher_strlen = psb->cipher_strlen;
726 cap->cipher_keysize = psb->cipher_keysize;
727 if (cap->cipher_strlen)
728 sprintf(str, "%s", psb->cipher_string);
730 netfs_convert_crypto_capabilities(cap);
733 err = netfs_trans_finish(t, psb);
737 err = wait_event_interruptible_timeout(psb->wait, (psb->flags != ~0),
738 psb->wait_on_page_timeout);
745 psb->perform_crypto = 1;
749 * At this point NETFS_CAPABILITIES response command
750 * should setup superblock in a way, which is acceptible
751 * for both client and server, so if server refuses connection,
752 * it will send error in transaction response.
764 int pohmelfs_crypto_init(struct pohmelfs_sb *psb)
768 if (!psb->cipher_string && !psb->hash_string)
771 err = pohmelfs_crypto_init_handshake(psb);
775 err = pohmelfs_sys_crypto_init(psb);
782 static int pohmelfs_crypto_thread_get(struct pohmelfs_sb *psb,
783 int (* action)(struct pohmelfs_crypto_thread *t, void *data), void *data)
785 struct pohmelfs_crypto_thread *t = NULL;
789 err = wait_event_interruptible_timeout(psb->wait,
790 !list_empty(&psb->crypto_ready_list),
791 psb->wait_on_page_timeout);
795 mutex_lock(&psb->crypto_thread_lock);
796 if (!list_empty(&psb->crypto_ready_list)) {
797 t = list_entry(psb->crypto_ready_list.prev,
798 struct pohmelfs_crypto_thread,
801 list_move_tail(&t->thread_entry,
802 &psb->crypto_active_list);
808 mutex_unlock(&psb->crypto_thread_lock);
814 static int pohmelfs_trans_crypt_action(struct pohmelfs_crypto_thread *t, void *data)
816 struct netfs_trans *trans = data;
818 netfs_trans_get(trans);
821 dprintk("%s: t: %p, gen: %u, thread: %p.\n", __func__, trans, trans->gen, t);
825 int pohmelfs_trans_crypt(struct netfs_trans *trans, struct pohmelfs_sb *psb)
827 if ((!psb->hash_string && !psb->cipher_string) || !psb->perform_crypto) {
828 netfs_trans_get(trans);
829 return pohmelfs_crypto_finish(trans, psb, 0);
832 return pohmelfs_crypto_thread_get(psb, pohmelfs_trans_crypt_action, trans);
835 struct pohmelfs_crypto_input_action_data
838 struct pohmelfs_crypto_engine *e;
843 static int pohmelfs_crypt_input_page_action(struct pohmelfs_crypto_thread *t, void *data)
845 struct pohmelfs_crypto_input_action_data *act = data;
847 memcpy(t->eng.data, act->e->data, t->psb->crypto_attached_size);
856 int pohmelfs_crypto_process_input_page(struct pohmelfs_crypto_engine *e,
857 struct page *page, unsigned int size, u64 iv)
859 struct inode *inode = page->mapping->host;
860 struct pohmelfs_crypto_input_action_data act;
868 err = pohmelfs_crypto_thread_get(POHMELFS_SB(inode->i_sb),
869 pohmelfs_crypt_input_page_action, &act);
876 SetPageUptodate(page);
877 page_cache_release(page);