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