blob: a69321a7778393f059fc43c4ead83ab1e0cd8d4e [file] [log] [blame]
Huang Ying0e1227d2009-10-19 11:53:06 +09001/*
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
Randy Dunlap52f6c5a2010-12-15 17:58:57 +080013#include <linux/err.h>
Huang Ying0e1227d2009-10-19 11:53:06 +090014#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>
Ingo Molnardf6b35f2015-04-24 02:46:00 +020022#include <asm/fpu/api.h>
Andi Kleen3bd391f2012-01-26 00:09:06 +010023#include <asm/cpu_device_id.h>
Huang Ying0e1227d2009-10-19 11:53:06 +090024
25#define GHASH_BLOCK_SIZE 16
26#define GHASH_DIGEST_SIZE 16
27
Herbert Xu0ea48142014-04-04 20:24:03 +080028void clmul_ghash_mul(char *dst, const u128 *shash);
Huang Ying0e1227d2009-10-19 11:53:06 +090029
30void clmul_ghash_update(char *dst, const char *src, unsigned int srclen,
Herbert Xu0ea48142014-04-04 20:24:03 +080031 const u128 *shash);
Huang Ying0e1227d2009-10-19 11:53:06 +090032
Huang Ying0e1227d2009-10-19 11:53:06 +090033struct ghash_async_ctx {
34 struct cryptd_ahash *cryptd_tfm;
35};
36
37struct ghash_ctx {
Herbert Xu0ea48142014-04-04 20:24:03 +080038 u128 shash;
Huang Ying0e1227d2009-10-19 11:53:06 +090039};
40
41struct ghash_desc_ctx {
42 u8 buffer[GHASH_BLOCK_SIZE];
43 u32 bytes;
44};
45
46static 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
55static int ghash_setkey(struct crypto_shash *tfm,
56 const u8 *key, unsigned int keylen)
57{
58 struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
Ard Biesheuvel8ceee722014-03-27 18:14:40 +010059 be128 *x = (be128 *)key;
60 u64 a, b;
Huang Ying0e1227d2009-10-19 11:53:06 +090061
62 if (keylen != GHASH_BLOCK_SIZE) {
63 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
64 return -EINVAL;
65 }
66
Ard Biesheuvel8ceee722014-03-27 18:14:40 +010067 /* perform multiplication by 'x' in GF(2^128) */
68 a = be64_to_cpu(x->a);
69 b = be64_to_cpu(x->b);
70
Herbert Xu0ea48142014-04-04 20:24:03 +080071 ctx->shash.a = (b << 1) | (a >> 63);
72 ctx->shash.b = (a << 1) | (b >> 63);
Ard Biesheuvel8ceee722014-03-27 18:14:40 +010073
74 if (a >> 63)
Herbert Xu0ea48142014-04-04 20:24:03 +080075 ctx->shash.b ^= ((u64)0xc2) << 56;
Huang Ying0e1227d2009-10-19 11:53:06 +090076
77 return 0;
78}
79
80static 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
116static 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
134static 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
146static 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,
Stephan Mueller6a9b52b2015-03-30 22:01:49 +0200157 .cra_flags = CRYPTO_ALG_TYPE_SHASH |
158 CRYPTO_ALG_INTERNAL,
Huang Ying0e1227d2009-10-19 11:53:06 +0900159 .cra_blocksize = GHASH_BLOCK_SIZE,
160 .cra_ctxsize = sizeof(struct ghash_ctx),
161 .cra_module = THIS_MODULE,
Huang Ying0e1227d2009-10-19 11:53:06 +0900162 },
163};
164
165static 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
Huang Ying01dd9582009-11-03 10:55:20 -0500172 if (!irq_fpu_usable()) {
Huang Ying0e1227d2009-10-19 11:53:06 +0900173 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
186static int ghash_async_update(struct ahash_request *req)
187{
188 struct ahash_request *cryptd_req = ahash_request_ctx(req);
189
Huang Ying01dd9582009-11-03 10:55:20 -0500190 if (!irq_fpu_usable()) {
Huang Ying0e1227d2009-10-19 11:53:06 +0900191 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
204static int ghash_async_final(struct ahash_request *req)
205{
206 struct ahash_request *cryptd_req = ahash_request_ctx(req);
207
Huang Ying01dd9582009-11-03 10:55:20 -0500208 if (!irq_fpu_usable()) {
Huang Ying0e1227d2009-10-19 11:53:06 +0900209 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
Wang, Rui Y3a020a72015-11-29 22:45:33 +0800222static int ghash_async_import(struct ahash_request *req, const void *in)
223{
224 struct ahash_request *cryptd_req = ahash_request_ctx(req);
225 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
226 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
227
228 ghash_async_init(req);
229 memcpy(dctx, in, sizeof(*dctx));
230 return 0;
231
232}
233
234static int ghash_async_export(struct ahash_request *req, void *out)
235{
236 struct ahash_request *cryptd_req = ahash_request_ctx(req);
237 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
238 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
239
240 memcpy(out, dctx, sizeof(*dctx));
241 return 0;
242
243}
244
Huang Ying0e1227d2009-10-19 11:53:06 +0900245static int ghash_async_digest(struct ahash_request *req)
246{
247 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
248 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
249 struct ahash_request *cryptd_req = ahash_request_ctx(req);
250 struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
251
Huang Ying01dd9582009-11-03 10:55:20 -0500252 if (!irq_fpu_usable()) {
Huang Ying0e1227d2009-10-19 11:53:06 +0900253 memcpy(cryptd_req, req, sizeof(*req));
254 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
255 return crypto_ahash_digest(cryptd_req);
256 } else {
257 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
258 struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
259
260 desc->tfm = child;
261 desc->flags = req->base.flags;
262 return shash_ahash_digest(req, desc);
263 }
264}
265
266static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
267 unsigned int keylen)
268{
269 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
270 struct crypto_ahash *child = &ctx->cryptd_tfm->base;
271 int err;
272
273 crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
274 crypto_ahash_set_flags(child, crypto_ahash_get_flags(tfm)
275 & CRYPTO_TFM_REQ_MASK);
276 err = crypto_ahash_setkey(child, key, keylen);
277 crypto_ahash_set_flags(tfm, crypto_ahash_get_flags(child)
278 & CRYPTO_TFM_RES_MASK);
279
Gustavo F. Padovanc3e73e72011-05-26 13:29:33 +1000280 return err;
Huang Ying0e1227d2009-10-19 11:53:06 +0900281}
282
283static int ghash_async_init_tfm(struct crypto_tfm *tfm)
284{
285 struct cryptd_ahash *cryptd_tfm;
286 struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
287
Stephan Mueller6a9b52b2015-03-30 22:01:49 +0200288 cryptd_tfm = cryptd_alloc_ahash("__ghash-pclmulqdqni",
289 CRYPTO_ALG_INTERNAL,
290 CRYPTO_ALG_INTERNAL);
Huang Ying0e1227d2009-10-19 11:53:06 +0900291 if (IS_ERR(cryptd_tfm))
292 return PTR_ERR(cryptd_tfm);
293 ctx->cryptd_tfm = cryptd_tfm;
294 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
295 sizeof(struct ahash_request) +
296 crypto_ahash_reqsize(&cryptd_tfm->base));
297
298 return 0;
299}
300
301static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
302{
303 struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
304
305 cryptd_free_ahash(ctx->cryptd_tfm);
306}
307
308static struct ahash_alg ghash_async_alg = {
309 .init = ghash_async_init,
310 .update = ghash_async_update,
311 .final = ghash_async_final,
312 .setkey = ghash_async_setkey,
313 .digest = ghash_async_digest,
Wang, Rui Y3a020a72015-11-29 22:45:33 +0800314 .export = ghash_async_export,
315 .import = ghash_async_import,
Huang Ying0e1227d2009-10-19 11:53:06 +0900316 .halg = {
317 .digestsize = GHASH_DIGEST_SIZE,
Wang, Rui Y3a020a72015-11-29 22:45:33 +0800318 .statesize = sizeof(struct ghash_desc_ctx),
Huang Ying0e1227d2009-10-19 11:53:06 +0900319 .base = {
320 .cra_name = "ghash",
321 .cra_driver_name = "ghash-clmulni",
322 .cra_priority = 400,
Andrey Ryabinin71c6da82015-09-03 14:32:01 +0300323 .cra_ctxsize = sizeof(struct ghash_async_ctx),
Huang Ying0e1227d2009-10-19 11:53:06 +0900324 .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
325 .cra_blocksize = GHASH_BLOCK_SIZE,
326 .cra_type = &crypto_ahash_type,
327 .cra_module = THIS_MODULE,
Huang Ying0e1227d2009-10-19 11:53:06 +0900328 .cra_init = ghash_async_init_tfm,
329 .cra_exit = ghash_async_exit_tfm,
330 },
331 },
332};
333
Andi Kleen3bd391f2012-01-26 00:09:06 +0100334static const struct x86_cpu_id pcmul_cpu_id[] = {
335 X86_FEATURE_MATCH(X86_FEATURE_PCLMULQDQ), /* Pickle-Mickle-Duck */
336 {}
337};
338MODULE_DEVICE_TABLE(x86cpu, pcmul_cpu_id);
339
Huang Ying0e1227d2009-10-19 11:53:06 +0900340static int __init ghash_pclmulqdqni_mod_init(void)
341{
342 int err;
343
Andi Kleen3bd391f2012-01-26 00:09:06 +0100344 if (!x86_match_cpu(pcmul_cpu_id))
Huang Ying0e1227d2009-10-19 11:53:06 +0900345 return -ENODEV;
Huang Ying0e1227d2009-10-19 11:53:06 +0900346
347 err = crypto_register_shash(&ghash_alg);
348 if (err)
349 goto err_out;
350 err = crypto_register_ahash(&ghash_async_alg);
351 if (err)
352 goto err_shash;
353
354 return 0;
355
356err_shash:
357 crypto_unregister_shash(&ghash_alg);
358err_out:
359 return err;
360}
361
362static void __exit ghash_pclmulqdqni_mod_exit(void)
363{
364 crypto_unregister_ahash(&ghash_async_alg);
365 crypto_unregister_shash(&ghash_alg);
366}
367
368module_init(ghash_pclmulqdqni_mod_init);
369module_exit(ghash_pclmulqdqni_mod_exit);
370
371MODULE_LICENSE("GPL");
372MODULE_DESCRIPTION("GHASH Message Digest Algorithm, "
373 "acclerated by PCLMULQDQ-NI");
Kees Cook5d26a102014-11-20 17:05:53 -0800374MODULE_ALIAS_CRYPTO("ghash");