]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/arm/crypto/ghash-ce-glue.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[karo-tx-linux.git] / arch / arm / crypto / ghash-ce-glue.c
1 /*
2  * Accelerated GHASH implementation with ARMv8 vmull.p64 instructions.
3  *
4  * Copyright (C) 2015 Linaro Ltd. <ard.biesheuvel@linaro.org>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation.
9  */
10
11 #include <asm/hwcap.h>
12 #include <asm/neon.h>
13 #include <asm/simd.h>
14 #include <asm/unaligned.h>
15 #include <crypto/cryptd.h>
16 #include <crypto/internal/hash.h>
17 #include <crypto/gf128mul.h>
18 #include <linux/cpufeature.h>
19 #include <linux/crypto.h>
20 #include <linux/module.h>
21
22 MODULE_DESCRIPTION("GHASH secure hash using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
25
26 #define GHASH_BLOCK_SIZE        16
27 #define GHASH_DIGEST_SIZE       16
28
29 struct ghash_key {
30         u64     a;
31         u64     b;
32 };
33
34 struct ghash_desc_ctx {
35         u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
36         u8 buf[GHASH_BLOCK_SIZE];
37         u32 count;
38 };
39
40 struct ghash_async_ctx {
41         struct cryptd_ahash *cryptd_tfm;
42 };
43
44 asmlinkage void pmull_ghash_update(int blocks, u64 dg[], const char *src,
45                                    struct ghash_key const *k, const char *head);
46
47 static int ghash_init(struct shash_desc *desc)
48 {
49         struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
50
51         *ctx = (struct ghash_desc_ctx){};
52         return 0;
53 }
54
55 static int ghash_update(struct shash_desc *desc, const u8 *src,
56                         unsigned int len)
57 {
58         struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
59         unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
60
61         ctx->count += len;
62
63         if ((partial + len) >= GHASH_BLOCK_SIZE) {
64                 struct ghash_key *key = crypto_shash_ctx(desc->tfm);
65                 int blocks;
66
67                 if (partial) {
68                         int p = GHASH_BLOCK_SIZE - partial;
69
70                         memcpy(ctx->buf + partial, src, p);
71                         src += p;
72                         len -= p;
73                 }
74
75                 blocks = len / GHASH_BLOCK_SIZE;
76                 len %= GHASH_BLOCK_SIZE;
77
78                 kernel_neon_begin();
79                 pmull_ghash_update(blocks, ctx->digest, src, key,
80                                    partial ? ctx->buf : NULL);
81                 kernel_neon_end();
82                 src += blocks * GHASH_BLOCK_SIZE;
83                 partial = 0;
84         }
85         if (len)
86                 memcpy(ctx->buf + partial, src, len);
87         return 0;
88 }
89
90 static int ghash_final(struct shash_desc *desc, u8 *dst)
91 {
92         struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
93         unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
94
95         if (partial) {
96                 struct ghash_key *key = crypto_shash_ctx(desc->tfm);
97
98                 memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
99                 kernel_neon_begin();
100                 pmull_ghash_update(1, ctx->digest, ctx->buf, key, NULL);
101                 kernel_neon_end();
102         }
103         put_unaligned_be64(ctx->digest[1], dst);
104         put_unaligned_be64(ctx->digest[0], dst + 8);
105
106         *ctx = (struct ghash_desc_ctx){};
107         return 0;
108 }
109
110 static int ghash_setkey(struct crypto_shash *tfm,
111                         const u8 *inkey, unsigned int keylen)
112 {
113         struct ghash_key *key = crypto_shash_ctx(tfm);
114         u64 a, b;
115
116         if (keylen != GHASH_BLOCK_SIZE) {
117                 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
118                 return -EINVAL;
119         }
120
121         /* perform multiplication by 'x' in GF(2^128) */
122         b = get_unaligned_be64(inkey);
123         a = get_unaligned_be64(inkey + 8);
124
125         key->a = (a << 1) | (b >> 63);
126         key->b = (b << 1) | (a >> 63);
127
128         if (b >> 63)
129                 key->b ^= 0xc200000000000000UL;
130
131         return 0;
132 }
133
134 static struct shash_alg ghash_alg = {
135         .digestsize             = GHASH_DIGEST_SIZE,
136         .init                   = ghash_init,
137         .update                 = ghash_update,
138         .final                  = ghash_final,
139         .setkey                 = ghash_setkey,
140         .descsize               = sizeof(struct ghash_desc_ctx),
141         .base                   = {
142                 .cra_name       = "__ghash",
143                 .cra_driver_name = "__driver-ghash-ce",
144                 .cra_priority   = 0,
145                 .cra_flags      = CRYPTO_ALG_TYPE_SHASH | CRYPTO_ALG_INTERNAL,
146                 .cra_blocksize  = GHASH_BLOCK_SIZE,
147                 .cra_ctxsize    = sizeof(struct ghash_key),
148                 .cra_module     = THIS_MODULE,
149         },
150 };
151
152 static int ghash_async_init(struct ahash_request *req)
153 {
154         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
155         struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
156         struct ahash_request *cryptd_req = ahash_request_ctx(req);
157         struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
158         struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
159         struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
160
161         desc->tfm = child;
162         desc->flags = req->base.flags;
163         return crypto_shash_init(desc);
164 }
165
166 static int ghash_async_update(struct ahash_request *req)
167 {
168         struct ahash_request *cryptd_req = ahash_request_ctx(req);
169         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
170         struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
171         struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
172
173         if (!may_use_simd() ||
174             (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
175                 memcpy(cryptd_req, req, sizeof(*req));
176                 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
177                 return crypto_ahash_update(cryptd_req);
178         } else {
179                 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
180                 return shash_ahash_update(req, desc);
181         }
182 }
183
184 static int ghash_async_final(struct ahash_request *req)
185 {
186         struct ahash_request *cryptd_req = ahash_request_ctx(req);
187         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
188         struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
189         struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
190
191         if (!may_use_simd() ||
192             (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
193                 memcpy(cryptd_req, req, sizeof(*req));
194                 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
195                 return crypto_ahash_final(cryptd_req);
196         } else {
197                 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
198                 return crypto_shash_final(desc, req->result);
199         }
200 }
201
202 static int ghash_async_digest(struct ahash_request *req)
203 {
204         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
205         struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
206         struct ahash_request *cryptd_req = ahash_request_ctx(req);
207         struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
208
209         if (!may_use_simd() ||
210             (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
211                 memcpy(cryptd_req, req, sizeof(*req));
212                 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
213                 return crypto_ahash_digest(cryptd_req);
214         } else {
215                 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
216                 struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
217
218                 desc->tfm = child;
219                 desc->flags = req->base.flags;
220                 return shash_ahash_digest(req, desc);
221         }
222 }
223
224 static int ghash_async_import(struct ahash_request *req, const void *in)
225 {
226         struct ahash_request *cryptd_req = ahash_request_ctx(req);
227         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
228         struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
229         struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
230
231         desc->tfm = cryptd_ahash_child(ctx->cryptd_tfm);
232         desc->flags = req->base.flags;
233
234         return crypto_shash_import(desc, in);
235 }
236
237 static int ghash_async_export(struct ahash_request *req, void *out)
238 {
239         struct ahash_request *cryptd_req = ahash_request_ctx(req);
240         struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
241
242         return crypto_shash_export(desc, out);
243 }
244
245 static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
246                               unsigned int keylen)
247 {
248         struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
249         struct crypto_ahash *child = &ctx->cryptd_tfm->base;
250         int err;
251
252         crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
253         crypto_ahash_set_flags(child, crypto_ahash_get_flags(tfm)
254                                & CRYPTO_TFM_REQ_MASK);
255         err = crypto_ahash_setkey(child, key, keylen);
256         crypto_ahash_set_flags(tfm, crypto_ahash_get_flags(child)
257                                & CRYPTO_TFM_RES_MASK);
258
259         return err;
260 }
261
262 static int ghash_async_init_tfm(struct crypto_tfm *tfm)
263 {
264         struct cryptd_ahash *cryptd_tfm;
265         struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
266
267         cryptd_tfm = cryptd_alloc_ahash("__driver-ghash-ce",
268                                         CRYPTO_ALG_INTERNAL,
269                                         CRYPTO_ALG_INTERNAL);
270         if (IS_ERR(cryptd_tfm))
271                 return PTR_ERR(cryptd_tfm);
272         ctx->cryptd_tfm = cryptd_tfm;
273         crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
274                                  sizeof(struct ahash_request) +
275                                  crypto_ahash_reqsize(&cryptd_tfm->base));
276
277         return 0;
278 }
279
280 static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
281 {
282         struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
283
284         cryptd_free_ahash(ctx->cryptd_tfm);
285 }
286
287 static struct ahash_alg ghash_async_alg = {
288         .init                   = ghash_async_init,
289         .update                 = ghash_async_update,
290         .final                  = ghash_async_final,
291         .setkey                 = ghash_async_setkey,
292         .digest                 = ghash_async_digest,
293         .import                 = ghash_async_import,
294         .export                 = ghash_async_export,
295         .halg.digestsize        = GHASH_DIGEST_SIZE,
296         .halg.statesize         = sizeof(struct ghash_desc_ctx),
297         .halg.base              = {
298                 .cra_name       = "ghash",
299                 .cra_driver_name = "ghash-ce",
300                 .cra_priority   = 300,
301                 .cra_flags      = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
302                 .cra_blocksize  = GHASH_BLOCK_SIZE,
303                 .cra_type       = &crypto_ahash_type,
304                 .cra_ctxsize    = sizeof(struct ghash_async_ctx),
305                 .cra_module     = THIS_MODULE,
306                 .cra_init       = ghash_async_init_tfm,
307                 .cra_exit       = ghash_async_exit_tfm,
308         },
309 };
310
311 static int __init ghash_ce_mod_init(void)
312 {
313         int err;
314
315         err = crypto_register_shash(&ghash_alg);
316         if (err)
317                 return err;
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         return err;
327 }
328
329 static void __exit ghash_ce_mod_exit(void)
330 {
331         crypto_unregister_ahash(&ghash_async_alg);
332         crypto_unregister_shash(&ghash_alg);
333 }
334
335 module_cpu_feature_match(PMULL, ghash_ce_mod_init);
336 module_exit(ghash_ce_mod_exit);