blob: 6e5d2f149b89f7ac3f217e1672ab60d100c86323 [file] [log] [blame]
Ard Biesheuvela62b01c2013-09-20 09:55:40 +02001/*
2 * Shared async block cipher helpers
3 *
4 * Copyright (c) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
5 *
6 * Based on aesni-intel_glue.c by:
7 * Copyright (C) 2008, Intel Corp.
8 * Author: Huang Ying <ying.huang@intel.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
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.
19 *
20 * You should have received a copy of the GNU General Public License
Martin Kepplinger1af39da2017-11-14 10:25:15 +010021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Ard Biesheuvela62b01c2013-09-20 09:55:40 +020022 *
23 */
24
25#include <linux/kernel.h>
26#include <linux/crypto.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/hardirq.h>
30#include <crypto/algapi.h>
31#include <crypto/cryptd.h>
32#include <crypto/ablk_helper.h>
33#include <asm/simd.h>
34
35int ablk_set_key(struct crypto_ablkcipher *tfm, const u8 *key,
36 unsigned int key_len)
37{
38 struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
39 struct crypto_ablkcipher *child = &ctx->cryptd_tfm->base;
40 int err;
41
42 crypto_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
43 crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(tfm)
44 & CRYPTO_TFM_REQ_MASK);
45 err = crypto_ablkcipher_setkey(child, key, key_len);
46 crypto_ablkcipher_set_flags(tfm, crypto_ablkcipher_get_flags(child)
47 & CRYPTO_TFM_RES_MASK);
48 return err;
49}
50EXPORT_SYMBOL_GPL(ablk_set_key);
51
52int __ablk_encrypt(struct ablkcipher_request *req)
53{
54 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
55 struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
56 struct blkcipher_desc desc;
57
58 desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm);
59 desc.info = req->info;
60 desc.flags = 0;
61
62 return crypto_blkcipher_crt(desc.tfm)->encrypt(
63 &desc, req->dst, req->src, req->nbytes);
64}
65EXPORT_SYMBOL_GPL(__ablk_encrypt);
66
67int ablk_encrypt(struct ablkcipher_request *req)
68{
69 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
70 struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
71
Herbert Xu88407a32016-06-21 16:55:15 +080072 if (!may_use_simd() ||
73 (in_atomic() && cryptd_ablkcipher_queued(ctx->cryptd_tfm))) {
Ard Biesheuvela62b01c2013-09-20 09:55:40 +020074 struct ablkcipher_request *cryptd_req =
75 ablkcipher_request_ctx(req);
76
kbuild test robotea493d32013-09-24 08:21:29 +080077 *cryptd_req = *req;
Ard Biesheuvela62b01c2013-09-20 09:55:40 +020078 ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
79
80 return crypto_ablkcipher_encrypt(cryptd_req);
81 } else {
82 return __ablk_encrypt(req);
83 }
84}
85EXPORT_SYMBOL_GPL(ablk_encrypt);
86
87int ablk_decrypt(struct ablkcipher_request *req)
88{
89 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
90 struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
91
Herbert Xu88407a32016-06-21 16:55:15 +080092 if (!may_use_simd() ||
93 (in_atomic() && cryptd_ablkcipher_queued(ctx->cryptd_tfm))) {
Ard Biesheuvela62b01c2013-09-20 09:55:40 +020094 struct ablkcipher_request *cryptd_req =
95 ablkcipher_request_ctx(req);
96
kbuild test robotea493d32013-09-24 08:21:29 +080097 *cryptd_req = *req;
Ard Biesheuvela62b01c2013-09-20 09:55:40 +020098 ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
99
100 return crypto_ablkcipher_decrypt(cryptd_req);
101 } else {
102 struct blkcipher_desc desc;
103
104 desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm);
105 desc.info = req->info;
106 desc.flags = 0;
107
108 return crypto_blkcipher_crt(desc.tfm)->decrypt(
109 &desc, req->dst, req->src, req->nbytes);
110 }
111}
112EXPORT_SYMBOL_GPL(ablk_decrypt);
113
114void ablk_exit(struct crypto_tfm *tfm)
115{
116 struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm);
117
118 cryptd_free_ablkcipher(ctx->cryptd_tfm);
119}
120EXPORT_SYMBOL_GPL(ablk_exit);
121
122int ablk_init_common(struct crypto_tfm *tfm, const char *drv_name)
123{
124 struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm);
125 struct cryptd_ablkcipher *cryptd_tfm;
126
Stephan Mueller466a7b92015-03-30 21:57:06 +0200127 cryptd_tfm = cryptd_alloc_ablkcipher(drv_name, CRYPTO_ALG_INTERNAL,
128 CRYPTO_ALG_INTERNAL);
Ard Biesheuvela62b01c2013-09-20 09:55:40 +0200129 if (IS_ERR(cryptd_tfm))
130 return PTR_ERR(cryptd_tfm);
131
132 ctx->cryptd_tfm = cryptd_tfm;
133 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request) +
134 crypto_ablkcipher_reqsize(&cryptd_tfm->base);
135
136 return 0;
137}
138EXPORT_SYMBOL_GPL(ablk_init_common);
139
140int ablk_init(struct crypto_tfm *tfm)
141{
142 char drv_name[CRYPTO_MAX_ALG_NAME];
143
144 snprintf(drv_name, sizeof(drv_name), "__driver-%s",
145 crypto_tfm_alg_driver_name(tfm));
146
147 return ablk_init_common(tfm, drv_name);
148}
149EXPORT_SYMBOL_GPL(ablk_init);
150
151MODULE_LICENSE("GPL");