blob: b7d30b6cf49cf560c4a194a4b45f1cd734c0cede [file] [log] [blame]
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +01001/*
2 * Accelerated GHASH implementation with ARMv8 vmull.p64 instructions.
3 *
Ard Biesheuvel00227e32018-08-23 15:48:51 +01004 * Copyright (C) 2015 - 2018 Linaro Ltd. <ard.biesheuvel@linaro.org>
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +01005 *
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>
Ard Biesheuvelc9d9f602017-05-21 10:23:37 +000018#include <linux/cpufeature.h>
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +010019#include <linux/crypto.h>
20#include <linux/module.h>
21
22MODULE_DESCRIPTION("GHASH secure hash using ARMv8 Crypto Extensions");
23MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24MODULE_LICENSE("GPL v2");
Ard Biesheuvel3759ee02017-07-24 11:28:17 +010025MODULE_ALIAS_CRYPTO("ghash");
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +010026
27#define GHASH_BLOCK_SIZE 16
28#define GHASH_DIGEST_SIZE 16
29
30struct ghash_key {
Ard Biesheuvel00227e32018-08-23 15:48:51 +010031 u64 h[2];
32 u64 h2[2];
33 u64 h3[2];
34 u64 h4[2];
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +010035};
36
37struct ghash_desc_ctx {
38 u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
39 u8 buf[GHASH_BLOCK_SIZE];
40 u32 count;
41};
42
43struct ghash_async_ctx {
44 struct cryptd_ahash *cryptd_tfm;
45};
46
Ard Biesheuvel3759ee02017-07-24 11:28:17 +010047asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src,
48 struct ghash_key const *k,
49 const char *head);
50
51asmlinkage void pmull_ghash_update_p8(int blocks, u64 dg[], const char *src,
52 struct ghash_key const *k,
53 const char *head);
54
55static void (*pmull_ghash_update)(int blocks, u64 dg[], const char *src,
56 struct ghash_key const *k,
57 const char *head);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +010058
59static int ghash_init(struct shash_desc *desc)
60{
61 struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
62
63 *ctx = (struct ghash_desc_ctx){};
64 return 0;
65}
66
67static int ghash_update(struct shash_desc *desc, const u8 *src,
68 unsigned int len)
69{
70 struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
71 unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
72
73 ctx->count += len;
74
75 if ((partial + len) >= GHASH_BLOCK_SIZE) {
76 struct ghash_key *key = crypto_shash_ctx(desc->tfm);
77 int blocks;
78
79 if (partial) {
80 int p = GHASH_BLOCK_SIZE - partial;
81
82 memcpy(ctx->buf + partial, src, p);
83 src += p;
84 len -= p;
85 }
86
87 blocks = len / GHASH_BLOCK_SIZE;
88 len %= GHASH_BLOCK_SIZE;
89
90 kernel_neon_begin();
91 pmull_ghash_update(blocks, ctx->digest, src, key,
92 partial ? ctx->buf : NULL);
93 kernel_neon_end();
94 src += blocks * GHASH_BLOCK_SIZE;
95 partial = 0;
96 }
97 if (len)
98 memcpy(ctx->buf + partial, src, len);
99 return 0;
100}
101
102static int ghash_final(struct shash_desc *desc, u8 *dst)
103{
104 struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
105 unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
106
107 if (partial) {
108 struct ghash_key *key = crypto_shash_ctx(desc->tfm);
109
110 memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
111 kernel_neon_begin();
112 pmull_ghash_update(1, ctx->digest, ctx->buf, key, NULL);
113 kernel_neon_end();
114 }
115 put_unaligned_be64(ctx->digest[1], dst);
116 put_unaligned_be64(ctx->digest[0], dst + 8);
117
118 *ctx = (struct ghash_desc_ctx){};
119 return 0;
120}
121
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100122static void ghash_reflect(u64 h[], const be128 *k)
123{
124 u64 carry = be64_to_cpu(k->a) >> 63;
125
126 h[0] = (be64_to_cpu(k->b) << 1) | carry;
127 h[1] = (be64_to_cpu(k->a) << 1) | (be64_to_cpu(k->b) >> 63);
128
129 if (carry)
130 h[1] ^= 0xc200000000000000UL;
131}
132
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100133static int ghash_setkey(struct crypto_shash *tfm,
134 const u8 *inkey, unsigned int keylen)
135{
136 struct ghash_key *key = crypto_shash_ctx(tfm);
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100137 be128 h, k;
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100138
139 if (keylen != GHASH_BLOCK_SIZE) {
140 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
141 return -EINVAL;
142 }
143
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100144 memcpy(&k, inkey, GHASH_BLOCK_SIZE);
145 ghash_reflect(key->h, &k);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100146
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100147 h = k;
148 gf128mul_lle(&h, &k);
149 ghash_reflect(key->h2, &h);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100150
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100151 gf128mul_lle(&h, &k);
152 ghash_reflect(key->h3, &h);
153
154 gf128mul_lle(&h, &k);
155 ghash_reflect(key->h4, &h);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100156
157 return 0;
158}
159
160static struct shash_alg ghash_alg = {
161 .digestsize = GHASH_DIGEST_SIZE,
162 .init = ghash_init,
163 .update = ghash_update,
164 .final = ghash_final,
165 .setkey = ghash_setkey,
166 .descsize = sizeof(struct ghash_desc_ctx),
167 .base = {
Ard Biesheuvel71f89912016-09-05 12:58:44 +0100168 .cra_name = "__ghash",
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100169 .cra_driver_name = "__driver-ghash-ce",
170 .cra_priority = 0,
Eric Biggerse50944e2018-06-30 15:16:11 -0700171 .cra_flags = CRYPTO_ALG_INTERNAL,
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100172 .cra_blocksize = GHASH_BLOCK_SIZE,
173 .cra_ctxsize = sizeof(struct ghash_key),
174 .cra_module = THIS_MODULE,
175 },
176};
177
178static int ghash_async_init(struct ahash_request *req)
179{
180 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
181 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
182 struct ahash_request *cryptd_req = ahash_request_ctx(req);
183 struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
Herbert Xu820573e2016-06-21 16:55:17 +0800184 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
185 struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100186
Herbert Xu820573e2016-06-21 16:55:17 +0800187 desc->tfm = child;
188 desc->flags = req->base.flags;
189 return crypto_shash_init(desc);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100190}
191
192static int ghash_async_update(struct ahash_request *req)
193{
194 struct ahash_request *cryptd_req = ahash_request_ctx(req);
Herbert Xu820573e2016-06-21 16:55:17 +0800195 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
196 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
197 struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100198
Herbert Xu820573e2016-06-21 16:55:17 +0800199 if (!may_use_simd() ||
200 (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100201 memcpy(cryptd_req, req, sizeof(*req));
202 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
203 return crypto_ahash_update(cryptd_req);
204 } else {
205 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
206 return shash_ahash_update(req, desc);
207 }
208}
209
210static int ghash_async_final(struct ahash_request *req)
211{
212 struct ahash_request *cryptd_req = ahash_request_ctx(req);
Herbert Xu820573e2016-06-21 16:55:17 +0800213 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
214 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
215 struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100216
Herbert Xu820573e2016-06-21 16:55:17 +0800217 if (!may_use_simd() ||
218 (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100219 memcpy(cryptd_req, req, sizeof(*req));
220 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
221 return crypto_ahash_final(cryptd_req);
222 } else {
223 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
224 return crypto_shash_final(desc, req->result);
225 }
226}
227
228static int ghash_async_digest(struct ahash_request *req)
229{
230 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
231 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
232 struct ahash_request *cryptd_req = ahash_request_ctx(req);
233 struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
234
Herbert Xu820573e2016-06-21 16:55:17 +0800235 if (!may_use_simd() ||
236 (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100237 memcpy(cryptd_req, req, sizeof(*req));
238 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
239 return crypto_ahash_digest(cryptd_req);
240 } else {
241 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
242 struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
243
244 desc->tfm = child;
245 desc->flags = req->base.flags;
246 return shash_ahash_digest(req, desc);
247 }
248}
249
Ard Biesheuveled4767d2016-09-01 14:25:42 +0100250static int ghash_async_import(struct ahash_request *req, const void *in)
251{
252 struct ahash_request *cryptd_req = ahash_request_ctx(req);
253 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
254 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
255 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
256
257 desc->tfm = cryptd_ahash_child(ctx->cryptd_tfm);
258 desc->flags = req->base.flags;
259
260 return crypto_shash_import(desc, in);
261}
262
263static int ghash_async_export(struct ahash_request *req, void *out)
264{
265 struct ahash_request *cryptd_req = ahash_request_ctx(req);
266 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
267
268 return crypto_shash_export(desc, out);
269}
270
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100271static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
272 unsigned int keylen)
273{
274 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
275 struct crypto_ahash *child = &ctx->cryptd_tfm->base;
276 int err;
277
278 crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
279 crypto_ahash_set_flags(child, crypto_ahash_get_flags(tfm)
280 & CRYPTO_TFM_REQ_MASK);
281 err = crypto_ahash_setkey(child, key, keylen);
282 crypto_ahash_set_flags(tfm, crypto_ahash_get_flags(child)
283 & CRYPTO_TFM_RES_MASK);
284
285 return err;
286}
287
288static int ghash_async_init_tfm(struct crypto_tfm *tfm)
289{
290 struct cryptd_ahash *cryptd_tfm;
291 struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
292
Stephan Mueller4b3f4e32015-03-30 22:02:36 +0200293 cryptd_tfm = cryptd_alloc_ahash("__driver-ghash-ce",
294 CRYPTO_ALG_INTERNAL,
295 CRYPTO_ALG_INTERNAL);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100296 if (IS_ERR(cryptd_tfm))
297 return PTR_ERR(cryptd_tfm);
298 ctx->cryptd_tfm = cryptd_tfm;
299 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
300 sizeof(struct ahash_request) +
301 crypto_ahash_reqsize(&cryptd_tfm->base));
302
303 return 0;
304}
305
306static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
307{
308 struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
309
310 cryptd_free_ahash(ctx->cryptd_tfm);
311}
312
313static struct ahash_alg ghash_async_alg = {
314 .init = ghash_async_init,
315 .update = ghash_async_update,
316 .final = ghash_async_final,
317 .setkey = ghash_async_setkey,
318 .digest = ghash_async_digest,
Ard Biesheuveled4767d2016-09-01 14:25:42 +0100319 .import = ghash_async_import,
320 .export = ghash_async_export,
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100321 .halg.digestsize = GHASH_DIGEST_SIZE,
Ard Biesheuveled4767d2016-09-01 14:25:42 +0100322 .halg.statesize = sizeof(struct ghash_desc_ctx),
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100323 .halg.base = {
324 .cra_name = "ghash",
325 .cra_driver_name = "ghash-ce",
326 .cra_priority = 300,
Eric Biggers6a38f622018-06-30 15:16:12 -0700327 .cra_flags = CRYPTO_ALG_ASYNC,
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100328 .cra_blocksize = GHASH_BLOCK_SIZE,
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100329 .cra_ctxsize = sizeof(struct ghash_async_ctx),
330 .cra_module = THIS_MODULE,
331 .cra_init = ghash_async_init_tfm,
332 .cra_exit = ghash_async_exit_tfm,
333 },
334};
335
336static int __init ghash_ce_mod_init(void)
337{
338 int err;
339
Ard Biesheuvel3759ee02017-07-24 11:28:17 +0100340 if (!(elf_hwcap & HWCAP_NEON))
341 return -ENODEV;
342
343 if (elf_hwcap2 & HWCAP2_PMULL)
344 pmull_ghash_update = pmull_ghash_update_p64;
345 else
346 pmull_ghash_update = pmull_ghash_update_p8;
347
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100348 err = crypto_register_shash(&ghash_alg);
349 if (err)
350 return err;
351 err = crypto_register_ahash(&ghash_async_alg);
352 if (err)
353 goto err_shash;
354
355 return 0;
356
357err_shash:
358 crypto_unregister_shash(&ghash_alg);
359 return err;
360}
361
362static void __exit ghash_ce_mod_exit(void)
363{
364 crypto_unregister_ahash(&ghash_async_alg);
365 crypto_unregister_shash(&ghash_alg);
366}
367
Ard Biesheuvel3759ee02017-07-24 11:28:17 +0100368module_init(ghash_ce_mod_init);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100369module_exit(ghash_ce_mod_exit);