1 /*
2  * Accelerated GHASH implementation with Intel PCLMULQDQ-NI
3  * instructions. This file contains glue code.
4  *
5  * Copyright (c) 2009 Intel Corp.
6  *   Author: Huang Ying <ying.huang@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published
10  * by the Free Software Foundation.
11  */
12 
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/crypto.h>
18 #include <crypto/algapi.h>
19 #include <crypto/cryptd.h>
20 #include <crypto/gf128mul.h>
21 #include <crypto/internal/hash.h>
22 #include <asm/fpu/api.h>
23 #include <asm/cpu_device_id.h>
24 
25 #define GHASH_BLOCK_SIZE	16
26 #define GHASH_DIGEST_SIZE	16
27 
28 void clmul_ghash_mul(char *dst, const u128 *shash);
29 
30 void clmul_ghash_update(char *dst, const char *src, unsigned int srclen,
31 			const u128 *shash);
32 
33 struct ghash_async_ctx {
34 	struct cryptd_ahash *cryptd_tfm;
35 };
36 
37 struct ghash_ctx {
38 	u128 shash;
39 };
40 
41 struct ghash_desc_ctx {
42 	u8 buffer[GHASH_BLOCK_SIZE];
43 	u32 bytes;
44 };
45 
46 static int ghash_init(struct shash_desc *desc)
47 {
48 	struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
49 
50 	memset(dctx, 0, sizeof(*dctx));
51 
52 	return 0;
53 }
54 
55 static int ghash_setkey(struct crypto_shash *tfm,
56 			const u8 *key, unsigned int keylen)
57 {
58 	struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
59 	be128 *x = (be128 *)key;
60 	u64 a, b;
61 
62 	if (keylen != GHASH_BLOCK_SIZE) {
63 		crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
64 		return -EINVAL;
65 	}
66 
67 	/* perform multiplication by 'x' in GF(2^128) */
68 	a = be64_to_cpu(x->a);
69 	b = be64_to_cpu(x->b);
70 
71 	ctx->shash.a = (b << 1) | (a >> 63);
72 	ctx->shash.b = (a << 1) | (b >> 63);
73 
74 	if (a >> 63)
75 		ctx->shash.b ^= ((u64)0xc2) << 56;
76 
77 	return 0;
78 }
79 
80 static int ghash_update(struct shash_desc *desc,
81 			 const u8 *src, unsigned int srclen)
82 {
83 	struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
84 	struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
85 	u8 *dst = dctx->buffer;
86 
87 	kernel_fpu_begin();
88 	if (dctx->bytes) {
89 		int n = min(srclen, dctx->bytes);
90 		u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
91 
92 		dctx->bytes -= n;
93 		srclen -= n;
94 
95 		while (n--)
96 			*pos++ ^= *src++;
97 
98 		if (!dctx->bytes)
99 			clmul_ghash_mul(dst, &ctx->shash);
100 	}
101 
102 	clmul_ghash_update(dst, src, srclen, &ctx->shash);
103 	kernel_fpu_end();
104 
105 	if (srclen & 0xf) {
106 		src += srclen - (srclen & 0xf);
107 		srclen &= 0xf;
108 		dctx->bytes = GHASH_BLOCK_SIZE - srclen;
109 		while (srclen--)
110 			*dst++ ^= *src++;
111 	}
112 
113 	return 0;
114 }
115 
116 static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
117 {
118 	u8 *dst = dctx->buffer;
119 
120 	if (dctx->bytes) {
121 		u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
122 
123 		while (dctx->bytes--)
124 			*tmp++ ^= 0;
125 
126 		kernel_fpu_begin();
127 		clmul_ghash_mul(dst, &ctx->shash);
128 		kernel_fpu_end();
129 	}
130 
131 	dctx->bytes = 0;
132 }
133 
134 static int ghash_final(struct shash_desc *desc, u8 *dst)
135 {
136 	struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
137 	struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
138 	u8 *buf = dctx->buffer;
139 
140 	ghash_flush(ctx, dctx);
141 	memcpy(dst, buf, GHASH_BLOCK_SIZE);
142 
143 	return 0;
144 }
145 
146 static struct shash_alg ghash_alg = {
147 	.digestsize	= GHASH_DIGEST_SIZE,
148 	.init		= ghash_init,
149 	.update		= ghash_update,
150 	.final		= ghash_final,
151 	.setkey		= ghash_setkey,
152 	.descsize	= sizeof(struct ghash_desc_ctx),
153 	.base		= {
154 		.cra_name		= "__ghash",
155 		.cra_driver_name	= "__ghash-pclmulqdqni",
156 		.cra_priority		= 0,
157 		.cra_flags		= CRYPTO_ALG_TYPE_SHASH |
158 					  CRYPTO_ALG_INTERNAL,
159 		.cra_blocksize		= GHASH_BLOCK_SIZE,
160 		.cra_ctxsize		= sizeof(struct ghash_ctx),
161 		.cra_module		= THIS_MODULE,
162 	},
163 };
164 
165 static int ghash_async_init(struct ahash_request *req)
166 {
167 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
168 	struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
169 	struct ahash_request *cryptd_req = ahash_request_ctx(req);
170 	struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
171 
172 	if (!irq_fpu_usable()) {
173 		memcpy(cryptd_req, req, sizeof(*req));
174 		ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
175 		return crypto_ahash_init(cryptd_req);
176 	} else {
177 		struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
178 		struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
179 
180 		desc->tfm = child;
181 		desc->flags = req->base.flags;
182 		return crypto_shash_init(desc);
183 	}
184 }
185 
186 static int ghash_async_update(struct ahash_request *req)
187 {
188 	struct ahash_request *cryptd_req = ahash_request_ctx(req);
189 
190 	if (!irq_fpu_usable()) {
191 		struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
192 		struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
193 		struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
194 
195 		memcpy(cryptd_req, req, sizeof(*req));
196 		ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
197 		return crypto_ahash_update(cryptd_req);
198 	} else {
199 		struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
200 		return shash_ahash_update(req, desc);
201 	}
202 }
203 
204 static int ghash_async_final(struct ahash_request *req)
205 {
206 	struct ahash_request *cryptd_req = ahash_request_ctx(req);
207 
208 	if (!irq_fpu_usable()) {
209 		struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
210 		struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
211 		struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
212 
213 		memcpy(cryptd_req, req, sizeof(*req));
214 		ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
215 		return crypto_ahash_final(cryptd_req);
216 	} else {
217 		struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
218 		return crypto_shash_final(desc, req->result);
219 	}
220 }
221 
222 static int ghash_async_digest(struct ahash_request *req)
223 {
224 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
225 	struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
226 	struct ahash_request *cryptd_req = ahash_request_ctx(req);
227 	struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
228 
229 	if (!irq_fpu_usable()) {
230 		memcpy(cryptd_req, req, sizeof(*req));
231 		ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
232 		return crypto_ahash_digest(cryptd_req);
233 	} else {
234 		struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
235 		struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
236 
237 		desc->tfm = child;
238 		desc->flags = req->base.flags;
239 		return shash_ahash_digest(req, desc);
240 	}
241 }
242 
243 static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
244 			      unsigned int keylen)
245 {
246 	struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
247 	struct crypto_ahash *child = &ctx->cryptd_tfm->base;
248 	int err;
249 
250 	crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
251 	crypto_ahash_set_flags(child, crypto_ahash_get_flags(tfm)
252 			       & CRYPTO_TFM_REQ_MASK);
253 	err = crypto_ahash_setkey(child, key, keylen);
254 	crypto_ahash_set_flags(tfm, crypto_ahash_get_flags(child)
255 			       & CRYPTO_TFM_RES_MASK);
256 
257 	return err;
258 }
259 
260 static int ghash_async_init_tfm(struct crypto_tfm *tfm)
261 {
262 	struct cryptd_ahash *cryptd_tfm;
263 	struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
264 
265 	cryptd_tfm = cryptd_alloc_ahash("__ghash-pclmulqdqni",
266 					CRYPTO_ALG_INTERNAL,
267 					CRYPTO_ALG_INTERNAL);
268 	if (IS_ERR(cryptd_tfm))
269 		return PTR_ERR(cryptd_tfm);
270 	ctx->cryptd_tfm = cryptd_tfm;
271 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
272 				 sizeof(struct ahash_request) +
273 				 crypto_ahash_reqsize(&cryptd_tfm->base));
274 
275 	return 0;
276 }
277 
278 static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
279 {
280 	struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
281 
282 	cryptd_free_ahash(ctx->cryptd_tfm);
283 }
284 
285 static struct ahash_alg ghash_async_alg = {
286 	.init		= ghash_async_init,
287 	.update		= ghash_async_update,
288 	.final		= ghash_async_final,
289 	.setkey		= ghash_async_setkey,
290 	.digest		= ghash_async_digest,
291 	.halg = {
292 		.digestsize	= GHASH_DIGEST_SIZE,
293 		.base = {
294 			.cra_name		= "ghash",
295 			.cra_driver_name	= "ghash-clmulni",
296 			.cra_priority		= 400,
297 			.cra_ctxsize		= sizeof(struct ghash_async_ctx),
298 			.cra_flags		= CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
299 			.cra_blocksize		= GHASH_BLOCK_SIZE,
300 			.cra_type		= &crypto_ahash_type,
301 			.cra_module		= THIS_MODULE,
302 			.cra_init		= ghash_async_init_tfm,
303 			.cra_exit		= ghash_async_exit_tfm,
304 		},
305 	},
306 };
307 
308 static const struct x86_cpu_id pcmul_cpu_id[] = {
309 	X86_FEATURE_MATCH(X86_FEATURE_PCLMULQDQ), /* Pickle-Mickle-Duck */
310 	{}
311 };
312 MODULE_DEVICE_TABLE(x86cpu, pcmul_cpu_id);
313 
314 static int __init ghash_pclmulqdqni_mod_init(void)
315 {
316 	int err;
317 
318 	if (!x86_match_cpu(pcmul_cpu_id))
319 		return -ENODEV;
320 
321 	err = crypto_register_shash(&ghash_alg);
322 	if (err)
323 		goto err_out;
324 	err = crypto_register_ahash(&ghash_async_alg);
325 	if (err)
326 		goto err_shash;
327 
328 	return 0;
329 
330 err_shash:
331 	crypto_unregister_shash(&ghash_alg);
332 err_out:
333 	return err;
334 }
335 
336 static void __exit ghash_pclmulqdqni_mod_exit(void)
337 {
338 	crypto_unregister_ahash(&ghash_async_alg);
339 	crypto_unregister_shash(&ghash_alg);
340 }
341 
342 module_init(ghash_pclmulqdqni_mod_init);
343 module_exit(ghash_pclmulqdqni_mod_exit);
344 
345 MODULE_LICENSE("GPL");
346 MODULE_DESCRIPTION("GHASH Message Digest Algorithm, "
347 		   "acclerated by PCLMULQDQ-NI");
348 MODULE_ALIAS_CRYPTO("ghash");
349