blob: 52d472a050e6aea9b8e887bfe96c9e8faa726f2a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +01002/*
3 * Accelerated GHASH implementation with ARMv8 vmull.p64 instructions.
4 *
Ard Biesheuvel00227e32018-08-23 15:48:51 +01005 * Copyright (C) 2015 - 2018 Linaro Ltd. <ard.biesheuvel@linaro.org>
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +01006 */
7
8#include <asm/hwcap.h>
9#include <asm/neon.h>
10#include <asm/simd.h>
11#include <asm/unaligned.h>
12#include <crypto/cryptd.h>
13#include <crypto/internal/hash.h>
Eric Biggers99680c52019-03-12 22:12:49 -070014#include <crypto/internal/simd.h>
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +010015#include <crypto/gf128mul.h>
Ard Biesheuvelc9d9f602017-05-21 10:23:37 +000016#include <linux/cpufeature.h>
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +010017#include <linux/crypto.h>
18#include <linux/module.h>
19
20MODULE_DESCRIPTION("GHASH secure hash using ARMv8 Crypto Extensions");
21MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
22MODULE_LICENSE("GPL v2");
Ard Biesheuvel3759ee02017-07-24 11:28:17 +010023MODULE_ALIAS_CRYPTO("ghash");
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +010024
25#define GHASH_BLOCK_SIZE 16
26#define GHASH_DIGEST_SIZE 16
27
28struct ghash_key {
Ard Biesheuvel00227e32018-08-23 15:48:51 +010029 u64 h[2];
30 u64 h2[2];
31 u64 h3[2];
32 u64 h4[2];
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +010033};
34
35struct ghash_desc_ctx {
36 u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
37 u8 buf[GHASH_BLOCK_SIZE];
38 u32 count;
39};
40
41struct ghash_async_ctx {
42 struct cryptd_ahash *cryptd_tfm;
43};
44
Ard Biesheuvel3759ee02017-07-24 11:28:17 +010045asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src,
46 struct ghash_key const *k,
47 const char *head);
48
49asmlinkage void pmull_ghash_update_p8(int blocks, u64 dg[], const char *src,
50 struct ghash_key const *k,
51 const char *head);
52
53static void (*pmull_ghash_update)(int blocks, u64 dg[], const char *src,
54 struct ghash_key const *k,
55 const char *head);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +010056
57static int ghash_init(struct shash_desc *desc)
58{
59 struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
60
61 *ctx = (struct ghash_desc_ctx){};
62 return 0;
63}
64
65static int ghash_update(struct shash_desc *desc, const u8 *src,
66 unsigned int len)
67{
68 struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
69 unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
70
71 ctx->count += len;
72
73 if ((partial + len) >= GHASH_BLOCK_SIZE) {
74 struct ghash_key *key = crypto_shash_ctx(desc->tfm);
75 int blocks;
76
77 if (partial) {
78 int p = GHASH_BLOCK_SIZE - partial;
79
80 memcpy(ctx->buf + partial, src, p);
81 src += p;
82 len -= p;
83 }
84
85 blocks = len / GHASH_BLOCK_SIZE;
86 len %= GHASH_BLOCK_SIZE;
87
88 kernel_neon_begin();
89 pmull_ghash_update(blocks, ctx->digest, src, key,
90 partial ? ctx->buf : NULL);
91 kernel_neon_end();
92 src += blocks * GHASH_BLOCK_SIZE;
93 partial = 0;
94 }
95 if (len)
96 memcpy(ctx->buf + partial, src, len);
97 return 0;
98}
99
100static int ghash_final(struct shash_desc *desc, u8 *dst)
101{
102 struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
103 unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
104
105 if (partial) {
106 struct ghash_key *key = crypto_shash_ctx(desc->tfm);
107
108 memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
109 kernel_neon_begin();
110 pmull_ghash_update(1, ctx->digest, ctx->buf, key, NULL);
111 kernel_neon_end();
112 }
113 put_unaligned_be64(ctx->digest[1], dst);
114 put_unaligned_be64(ctx->digest[0], dst + 8);
115
116 *ctx = (struct ghash_desc_ctx){};
117 return 0;
118}
119
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100120static void ghash_reflect(u64 h[], const be128 *k)
121{
122 u64 carry = be64_to_cpu(k->a) >> 63;
123
124 h[0] = (be64_to_cpu(k->b) << 1) | carry;
125 h[1] = (be64_to_cpu(k->a) << 1) | (be64_to_cpu(k->b) >> 63);
126
127 if (carry)
128 h[1] ^= 0xc200000000000000UL;
129}
130
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100131static int ghash_setkey(struct crypto_shash *tfm,
132 const u8 *inkey, unsigned int keylen)
133{
134 struct ghash_key *key = crypto_shash_ctx(tfm);
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100135 be128 h, k;
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100136
137 if (keylen != GHASH_BLOCK_SIZE) {
138 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
139 return -EINVAL;
140 }
141
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100142 memcpy(&k, inkey, GHASH_BLOCK_SIZE);
143 ghash_reflect(key->h, &k);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100144
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100145 h = k;
146 gf128mul_lle(&h, &k);
147 ghash_reflect(key->h2, &h);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100148
Ard Biesheuvel00227e32018-08-23 15:48:51 +0100149 gf128mul_lle(&h, &k);
150 ghash_reflect(key->h3, &h);
151
152 gf128mul_lle(&h, &k);
153 ghash_reflect(key->h4, &h);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100154
155 return 0;
156}
157
158static struct shash_alg ghash_alg = {
159 .digestsize = GHASH_DIGEST_SIZE,
160 .init = ghash_init,
161 .update = ghash_update,
162 .final = ghash_final,
163 .setkey = ghash_setkey,
164 .descsize = sizeof(struct ghash_desc_ctx),
165 .base = {
Ard Biesheuvel71f89912016-09-05 12:58:44 +0100166 .cra_name = "__ghash",
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100167 .cra_driver_name = "__driver-ghash-ce",
168 .cra_priority = 0,
Eric Biggerse50944e2018-06-30 15:16:11 -0700169 .cra_flags = CRYPTO_ALG_INTERNAL,
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100170 .cra_blocksize = GHASH_BLOCK_SIZE,
171 .cra_ctxsize = sizeof(struct ghash_key),
172 .cra_module = THIS_MODULE,
173 },
174};
175
176static int ghash_async_init(struct ahash_request *req)
177{
178 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
179 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
180 struct ahash_request *cryptd_req = ahash_request_ctx(req);
181 struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
Herbert Xu820573e2016-06-21 16:55:17 +0800182 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
183 struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100184
Herbert Xu820573e2016-06-21 16:55:17 +0800185 desc->tfm = child;
Herbert Xu820573e2016-06-21 16:55:17 +0800186 return crypto_shash_init(desc);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100187}
188
189static int ghash_async_update(struct ahash_request *req)
190{
191 struct ahash_request *cryptd_req = ahash_request_ctx(req);
Herbert Xu820573e2016-06-21 16:55:17 +0800192 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
193 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
194 struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100195
Eric Biggers99680c52019-03-12 22:12:49 -0700196 if (!crypto_simd_usable() ||
Herbert Xu820573e2016-06-21 16:55:17 +0800197 (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100198 memcpy(cryptd_req, req, sizeof(*req));
199 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
200 return crypto_ahash_update(cryptd_req);
201 } else {
202 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
203 return shash_ahash_update(req, desc);
204 }
205}
206
207static int ghash_async_final(struct ahash_request *req)
208{
209 struct ahash_request *cryptd_req = ahash_request_ctx(req);
Herbert Xu820573e2016-06-21 16:55:17 +0800210 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
211 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
212 struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100213
Eric Biggers99680c52019-03-12 22:12:49 -0700214 if (!crypto_simd_usable() ||
Herbert Xu820573e2016-06-21 16:55:17 +0800215 (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100216 memcpy(cryptd_req, req, sizeof(*req));
217 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
218 return crypto_ahash_final(cryptd_req);
219 } else {
220 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
221 return crypto_shash_final(desc, req->result);
222 }
223}
224
225static int ghash_async_digest(struct ahash_request *req)
226{
227 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
228 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
229 struct ahash_request *cryptd_req = ahash_request_ctx(req);
230 struct cryptd_ahash *cryptd_tfm = ctx->cryptd_tfm;
231
Eric Biggers99680c52019-03-12 22:12:49 -0700232 if (!crypto_simd_usable() ||
Herbert Xu820573e2016-06-21 16:55:17 +0800233 (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) {
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100234 memcpy(cryptd_req, req, sizeof(*req));
235 ahash_request_set_tfm(cryptd_req, &cryptd_tfm->base);
236 return crypto_ahash_digest(cryptd_req);
237 } else {
238 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
239 struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
240
241 desc->tfm = child;
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100242 return shash_ahash_digest(req, desc);
243 }
244}
245
Ard Biesheuveled4767d2016-09-01 14:25:42 +0100246static int ghash_async_import(struct ahash_request *req, const void *in)
247{
248 struct ahash_request *cryptd_req = ahash_request_ctx(req);
249 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
250 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
251 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
252
253 desc->tfm = cryptd_ahash_child(ctx->cryptd_tfm);
Ard Biesheuveled4767d2016-09-01 14:25:42 +0100254
255 return crypto_shash_import(desc, in);
256}
257
258static int ghash_async_export(struct ahash_request *req, void *out)
259{
260 struct ahash_request *cryptd_req = ahash_request_ctx(req);
261 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
262
263 return crypto_shash_export(desc, out);
264}
265
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100266static 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
280 return err;
281}
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 Mueller4b3f4e32015-03-30 22:02:36 +0200288 cryptd_tfm = cryptd_alloc_ahash("__driver-ghash-ce",
289 CRYPTO_ALG_INTERNAL,
290 CRYPTO_ALG_INTERNAL);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100291 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,
Ard Biesheuveled4767d2016-09-01 14:25:42 +0100314 .import = ghash_async_import,
315 .export = ghash_async_export,
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100316 .halg.digestsize = GHASH_DIGEST_SIZE,
Ard Biesheuveled4767d2016-09-01 14:25:42 +0100317 .halg.statesize = sizeof(struct ghash_desc_ctx),
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100318 .halg.base = {
319 .cra_name = "ghash",
320 .cra_driver_name = "ghash-ce",
321 .cra_priority = 300,
Eric Biggers6a38f622018-06-30 15:16:12 -0700322 .cra_flags = CRYPTO_ALG_ASYNC,
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100323 .cra_blocksize = GHASH_BLOCK_SIZE,
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100324 .cra_ctxsize = sizeof(struct ghash_async_ctx),
325 .cra_module = THIS_MODULE,
326 .cra_init = ghash_async_init_tfm,
327 .cra_exit = ghash_async_exit_tfm,
328 },
329};
330
331static int __init ghash_ce_mod_init(void)
332{
333 int err;
334
Ard Biesheuvel3759ee02017-07-24 11:28:17 +0100335 if (!(elf_hwcap & HWCAP_NEON))
336 return -ENODEV;
337
338 if (elf_hwcap2 & HWCAP2_PMULL)
339 pmull_ghash_update = pmull_ghash_update_p64;
340 else
341 pmull_ghash_update = pmull_ghash_update_p8;
342
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100343 err = crypto_register_shash(&ghash_alg);
344 if (err)
345 return err;
346 err = crypto_register_ahash(&ghash_async_alg);
347 if (err)
348 goto err_shash;
349
350 return 0;
351
352err_shash:
353 crypto_unregister_shash(&ghash_alg);
354 return err;
355}
356
357static void __exit ghash_ce_mod_exit(void)
358{
359 crypto_unregister_ahash(&ghash_async_alg);
360 crypto_unregister_shash(&ghash_alg);
361}
362
Ard Biesheuvel3759ee02017-07-24 11:28:17 +0100363module_init(ghash_ce_mod_init);
Ard Biesheuvelf1e866b2015-03-10 09:47:48 +0100364module_exit(ghash_ce_mod_exit);