• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Shared crypto simd helpers
3  *
4  * Copyright (c) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
5  * Copyright (c) 2016 Herbert Xu <herbert@gondor.apana.org.au>
6  *
7  * Based on aesni-intel_glue.c by:
8  *  Copyright (C) 2008, Intel Corp.
9  *    Author: Huang Ying <ying.huang@intel.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
24  * USA
25  *
26  */
27 
28 #include <crypto/cryptd.h>
29 #include <crypto/internal/simd.h>
30 #include <crypto/internal/skcipher.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/preempt.h>
34 #include <asm/simd.h>
35 
36 struct simd_skcipher_alg {
37 	const char *ialg_name;
38 	struct skcipher_alg alg;
39 };
40 
41 struct simd_skcipher_ctx {
42 	struct cryptd_skcipher *cryptd_tfm;
43 };
44 
simd_skcipher_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int key_len)45 static int simd_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
46 				unsigned int key_len)
47 {
48 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
49 	struct crypto_skcipher *child = &ctx->cryptd_tfm->base;
50 	int err;
51 
52 	crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
53 	crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(tfm) &
54 					 CRYPTO_TFM_REQ_MASK);
55 	err = crypto_skcipher_setkey(child, key, key_len);
56 	crypto_skcipher_set_flags(tfm, crypto_skcipher_get_flags(child) &
57 				       CRYPTO_TFM_RES_MASK);
58 	return err;
59 }
60 
simd_skcipher_encrypt(struct skcipher_request * req)61 static int simd_skcipher_encrypt(struct skcipher_request *req)
62 {
63 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
64 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
65 	struct skcipher_request *subreq;
66 	struct crypto_skcipher *child;
67 
68 	subreq = skcipher_request_ctx(req);
69 	*subreq = *req;
70 
71 	if (!may_use_simd() ||
72 	    (in_atomic() && cryptd_skcipher_queued(ctx->cryptd_tfm)))
73 		child = &ctx->cryptd_tfm->base;
74 	else
75 		child = cryptd_skcipher_child(ctx->cryptd_tfm);
76 
77 	skcipher_request_set_tfm(subreq, child);
78 
79 	return crypto_skcipher_encrypt(subreq);
80 }
81 
simd_skcipher_decrypt(struct skcipher_request * req)82 static int simd_skcipher_decrypt(struct skcipher_request *req)
83 {
84 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
85 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
86 	struct skcipher_request *subreq;
87 	struct crypto_skcipher *child;
88 
89 	subreq = skcipher_request_ctx(req);
90 	*subreq = *req;
91 
92 	if (!may_use_simd() ||
93 	    (in_atomic() && cryptd_skcipher_queued(ctx->cryptd_tfm)))
94 		child = &ctx->cryptd_tfm->base;
95 	else
96 		child = cryptd_skcipher_child(ctx->cryptd_tfm);
97 
98 	skcipher_request_set_tfm(subreq, child);
99 
100 	return crypto_skcipher_decrypt(subreq);
101 }
102 
simd_skcipher_exit(struct crypto_skcipher * tfm)103 static void simd_skcipher_exit(struct crypto_skcipher *tfm)
104 {
105 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
106 
107 	cryptd_free_skcipher(ctx->cryptd_tfm);
108 }
109 
simd_skcipher_init(struct crypto_skcipher * tfm)110 static int simd_skcipher_init(struct crypto_skcipher *tfm)
111 {
112 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
113 	struct cryptd_skcipher *cryptd_tfm;
114 	struct simd_skcipher_alg *salg;
115 	struct skcipher_alg *alg;
116 	unsigned reqsize;
117 
118 	alg = crypto_skcipher_alg(tfm);
119 	salg = container_of(alg, struct simd_skcipher_alg, alg);
120 
121 	cryptd_tfm = cryptd_alloc_skcipher(salg->ialg_name,
122 					   CRYPTO_ALG_INTERNAL,
123 					   CRYPTO_ALG_INTERNAL);
124 	if (IS_ERR(cryptd_tfm))
125 		return PTR_ERR(cryptd_tfm);
126 
127 	ctx->cryptd_tfm = cryptd_tfm;
128 
129 	reqsize = crypto_skcipher_reqsize(cryptd_skcipher_child(cryptd_tfm));
130 	reqsize = max(reqsize, crypto_skcipher_reqsize(&cryptd_tfm->base));
131 	reqsize += sizeof(struct skcipher_request);
132 
133 	crypto_skcipher_set_reqsize(tfm, reqsize);
134 
135 	return 0;
136 }
137 
simd_skcipher_create_compat(const char * algname,const char * drvname,const char * basename)138 struct simd_skcipher_alg *simd_skcipher_create_compat(const char *algname,
139 						      const char *drvname,
140 						      const char *basename)
141 {
142 	struct simd_skcipher_alg *salg;
143 	struct crypto_skcipher *tfm;
144 	struct skcipher_alg *ialg;
145 	struct skcipher_alg *alg;
146 	int err;
147 
148 	tfm = crypto_alloc_skcipher(basename, CRYPTO_ALG_INTERNAL,
149 				    CRYPTO_ALG_INTERNAL | CRYPTO_ALG_ASYNC);
150 	if (IS_ERR(tfm))
151 		return ERR_CAST(tfm);
152 
153 	ialg = crypto_skcipher_alg(tfm);
154 
155 	salg = kzalloc(sizeof(*salg), GFP_KERNEL);
156 	if (!salg) {
157 		salg = ERR_PTR(-ENOMEM);
158 		goto out_put_tfm;
159 	}
160 
161 	salg->ialg_name = basename;
162 	alg = &salg->alg;
163 
164 	err = -ENAMETOOLONG;
165 	if (snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", algname) >=
166 	    CRYPTO_MAX_ALG_NAME)
167 		goto out_free_salg;
168 
169 	if (snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
170 		     drvname) >= CRYPTO_MAX_ALG_NAME)
171 		goto out_free_salg;
172 
173 	alg->base.cra_flags = CRYPTO_ALG_ASYNC;
174 	alg->base.cra_priority = ialg->base.cra_priority;
175 	alg->base.cra_blocksize = ialg->base.cra_blocksize;
176 	alg->base.cra_alignmask = ialg->base.cra_alignmask;
177 	alg->base.cra_module = ialg->base.cra_module;
178 	alg->base.cra_ctxsize = sizeof(struct simd_skcipher_ctx);
179 
180 	alg->ivsize = ialg->ivsize;
181 	alg->chunksize = ialg->chunksize;
182 	alg->min_keysize = ialg->min_keysize;
183 	alg->max_keysize = ialg->max_keysize;
184 
185 	alg->init = simd_skcipher_init;
186 	alg->exit = simd_skcipher_exit;
187 
188 	alg->setkey = simd_skcipher_setkey;
189 	alg->encrypt = simd_skcipher_encrypt;
190 	alg->decrypt = simd_skcipher_decrypt;
191 
192 	err = crypto_register_skcipher(alg);
193 	if (err)
194 		goto out_free_salg;
195 
196 out_put_tfm:
197 	crypto_free_skcipher(tfm);
198 	return salg;
199 
200 out_free_salg:
201 	kfree(salg);
202 	salg = ERR_PTR(err);
203 	goto out_put_tfm;
204 }
205 EXPORT_SYMBOL_GPL(simd_skcipher_create_compat);
206 
simd_skcipher_create(const char * algname,const char * basename)207 struct simd_skcipher_alg *simd_skcipher_create(const char *algname,
208 					       const char *basename)
209 {
210 	char drvname[CRYPTO_MAX_ALG_NAME];
211 
212 	if (snprintf(drvname, CRYPTO_MAX_ALG_NAME, "simd-%s", basename) >=
213 	    CRYPTO_MAX_ALG_NAME)
214 		return ERR_PTR(-ENAMETOOLONG);
215 
216 	return simd_skcipher_create_compat(algname, drvname, basename);
217 }
218 EXPORT_SYMBOL_GPL(simd_skcipher_create);
219 
simd_skcipher_free(struct simd_skcipher_alg * salg)220 void simd_skcipher_free(struct simd_skcipher_alg *salg)
221 {
222 	crypto_unregister_skcipher(&salg->alg);
223 	kfree(salg);
224 }
225 EXPORT_SYMBOL_GPL(simd_skcipher_free);
226 
227 MODULE_LICENSE("GPL");
228