2 * Accelerated GHASH implementation with ARMv8 vmull.p64 instructions.
4 * Copyright (C) 2015 Linaro Ltd. <ard.biesheuvel@linaro.org>
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.
11 #include <asm/hwcap.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>
22 MODULE_DESCRIPTION("GHASH secure hash using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
26 #define GHASH_BLOCK_SIZE 16
27 #define GHASH_DIGEST_SIZE 16
34 struct ghash_desc_ctx {
35 u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
36 u8 buf[GHASH_BLOCK_SIZE];
40 struct ghash_async_ctx {
41 struct cryptd_ahash *cryptd_tfm;
44 asmlinkage void pmull_ghash_update(int blocks, u64 dg[], const char *src,
45 struct ghash_key const *k, const char *head);
47 static int ghash_init(struct shash_desc *desc)
49 struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
51 *ctx = (struct ghash_desc_ctx){};
55 static int ghash_update(struct shash_desc *desc, const u8 *src,
58 struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
59 unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
63 if ((partial + len) >= GHASH_BLOCK_SIZE) {
64 struct ghash_key *key = crypto_shash_ctx(desc->tfm);
68 int p = GHASH_BLOCK_SIZE - partial;
70 memcpy(ctx->buf + partial, src, p);
75 blocks = len / GHASH_BLOCK_SIZE;
76 len %= GHASH_BLOCK_SIZE;
79 pmull_ghash_update(blocks, ctx->digest, src, key,
80 partial ? ctx->buf : NULL);
82 src += blocks * GHASH_BLOCK_SIZE;
86 memcpy(ctx->buf + partial, src, len);
90 static int ghash_final(struct shash_desc *desc, u8 *dst)
92 struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
93 unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
96 struct ghash_key *key = crypto_shash_ctx(desc->tfm);
98 memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
100 pmull_ghash_update(1, ctx->digest, ctx->buf, key, NULL);
103 put_unaligned_be64(ctx->digest[1], dst);
104 put_unaligned_be64(ctx->digest[0], dst + 8);
106 *ctx = (struct ghash_desc_ctx){};
110 static int ghash_setkey(struct crypto_shash *tfm,
111 const u8 *inkey, unsigned int keylen)
113 struct ghash_key *key = crypto_shash_ctx(tfm);
116 if (keylen != GHASH_BLOCK_SIZE) {
117 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
121 /* perform multiplication by 'x' in GF(2^128) */
122 b = get_unaligned_be64(inkey);
123 a = get_unaligned_be64(inkey + 8);
125 key->a = (a << 1) | (b >> 63);
126 key->b = (b << 1) | (a >> 63);
129 key->b ^= 0xc200000000000000UL;
134 static struct shash_alg ghash_alg = {
135 .digestsize = GHASH_DIGEST_SIZE,
137 .update = ghash_update,
138 .final = ghash_final,
139 .setkey = ghash_setkey,
140 .descsize = sizeof(struct ghash_desc_ctx),
142 .cra_name = "__ghash",
143 .cra_driver_name = "__driver-ghash-ce",
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,
152 static int ghash_async_init(struct ahash_request *req)
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);
162 desc->flags = req->base.flags;
163 return crypto_shash_init(desc);
166 static int ghash_async_update(struct ahash_request *req)
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;
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);
179 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
180 return shash_ahash_update(req, desc);
184 static int ghash_async_final(struct ahash_request *req)
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;
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);
197 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
198 return crypto_shash_final(desc, req->result);
202 static int ghash_async_digest(struct ahash_request *req)
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;
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);
215 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
216 struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm);
219 desc->flags = req->base.flags;
220 return shash_ahash_digest(req, desc);
224 static int ghash_async_import(struct ahash_request *req, const void *in)
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);
231 desc->tfm = cryptd_ahash_child(ctx->cryptd_tfm);
232 desc->flags = req->base.flags;
234 return crypto_shash_import(desc, in);
237 static int ghash_async_export(struct ahash_request *req, void *out)
239 struct ahash_request *cryptd_req = ahash_request_ctx(req);
240 struct shash_desc *desc = cryptd_shash_desc(cryptd_req);
242 return crypto_shash_export(desc, out);
245 static int ghash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
248 struct ghash_async_ctx *ctx = crypto_ahash_ctx(tfm);
249 struct crypto_ahash *child = &ctx->cryptd_tfm->base;
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);
262 static int ghash_async_init_tfm(struct crypto_tfm *tfm)
264 struct cryptd_ahash *cryptd_tfm;
265 struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
267 cryptd_tfm = cryptd_alloc_ahash("__driver-ghash-ce",
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));
280 static void ghash_async_exit_tfm(struct crypto_tfm *tfm)
282 struct ghash_async_ctx *ctx = crypto_tfm_ctx(tfm);
284 cryptd_free_ahash(ctx->cryptd_tfm);
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),
299 .cra_driver_name = "ghash-ce",
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,
311 static int __init ghash_ce_mod_init(void)
315 err = crypto_register_shash(&ghash_alg);
318 err = crypto_register_ahash(&ghash_async_alg);
325 crypto_unregister_shash(&ghash_alg);
329 static void __exit ghash_ce_mod_exit(void)
331 crypto_unregister_ahash(&ghash_async_alg);
332 crypto_unregister_shash(&ghash_alg);
335 module_cpu_feature_match(PMULL, ghash_ce_mod_init);
336 module_exit(ghash_ce_mod_exit);