1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Accelerated GHASH implementation with ARMv8 vmull.p64 instructions. 4 * 5 * Copyright (C) 2015 - 2018 Linaro Ltd. <ard.biesheuvel@linaro.org> 6 */ 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> 14 #include <crypto/internal/simd.h> 15 #include <crypto/gf128mul.h> 16 #include <linux/cpufeature.h> 17 #include <linux/crypto.h> 18 #include <linux/module.h> 19 20 MODULE_DESCRIPTION("GHASH secure hash using ARMv8 Crypto Extensions"); 21 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); 22 MODULE_LICENSE("GPL v2"); 23 MODULE_ALIAS_CRYPTO("ghash"); 24 25 #define GHASH_BLOCK_SIZE 16 26 #define GHASH_DIGEST_SIZE 16 27 28 struct ghash_key { 29 u64 h[2]; 30 u64 h2[2]; 31 u64 h3[2]; 32 u64 h4[2]; 33 }; 34 35 struct ghash_desc_ctx { 36 u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)]; 37 u8 buf[GHASH_BLOCK_SIZE]; 38 u32 count; 39 }; 40 41 struct ghash_async_ctx { 42 struct cryptd_ahash *cryptd_tfm; 43 }; 44 45 asmlinkage void pmull_ghash_update_p64(int blocks, u64 dg[], const char *src, 46 struct ghash_key const *k, 47 const char *head); 48 49 asmlinkage void pmull_ghash_update_p8(int blocks, u64 dg[], const char *src, 50 struct ghash_key const *k, 51 const char *head); 52 53 static void (*pmull_ghash_update)(int blocks, u64 dg[], const char *src, 54 struct ghash_key const *k, 55 const char *head); 56 57 static 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 65 static 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 100 static 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 120 static 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 131 static int ghash_setkey(struct crypto_shash *tfm, 132 const u8 *inkey, unsigned int keylen) 133 { 134 struct ghash_key *key = crypto_shash_ctx(tfm); 135 be128 h, k; 136 137 if (keylen != GHASH_BLOCK_SIZE) { 138 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); 139 return -EINVAL; 140 } 141 142 memcpy(&k, inkey, GHASH_BLOCK_SIZE); 143 ghash_reflect(key->h, &k); 144 145 h = k; 146 gf128mul_lle(&h, &k); 147 ghash_reflect(key->h2, &h); 148 149 gf128mul_lle(&h, &k); 150 ghash_reflect(key->h3, &h); 151 152 gf128mul_lle(&h, &k); 153 ghash_reflect(key->h4, &h); 154 155 return 0; 156 } 157 158 static 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 = { 166 .cra_name = "__ghash", 167 .cra_driver_name = "__driver-ghash-ce", 168 .cra_priority = 0, 169 .cra_flags = CRYPTO_ALG_INTERNAL, 170 .cra_blocksize = GHASH_BLOCK_SIZE, 171 .cra_ctxsize = sizeof(struct ghash_key), 172 .cra_module = THIS_MODULE, 173 }, 174 }; 175 176 static 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; 182 struct shash_desc *desc = cryptd_shash_desc(cryptd_req); 183 struct crypto_shash *child = cryptd_ahash_child(cryptd_tfm); 184 185 desc->tfm = child; 186 return crypto_shash_init(desc); 187 } 188 189 static int ghash_async_update(struct ahash_request *req) 190 { 191 struct ahash_request *cryptd_req = ahash_request_ctx(req); 192 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; 195 196 if (!crypto_simd_usable() || 197 (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) { 198 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 207 static int ghash_async_final(struct ahash_request *req) 208 { 209 struct ahash_request *cryptd_req = ahash_request_ctx(req); 210 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; 213 214 if (!crypto_simd_usable() || 215 (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) { 216 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 225 static 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 232 if (!crypto_simd_usable() || 233 (in_atomic() && cryptd_ahash_queued(cryptd_tfm))) { 234 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; 242 return shash_ahash_digest(req, desc); 243 } 244 } 245 246 static 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); 254 255 return crypto_shash_import(desc, in); 256 } 257 258 static 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 266 static 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 283 static 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 288 cryptd_tfm = cryptd_alloc_ahash("__driver-ghash-ce", 289 CRYPTO_ALG_INTERNAL, 290 CRYPTO_ALG_INTERNAL); 291 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 301 static 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 308 static 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, 314 .import = ghash_async_import, 315 .export = ghash_async_export, 316 .halg.digestsize = GHASH_DIGEST_SIZE, 317 .halg.statesize = sizeof(struct ghash_desc_ctx), 318 .halg.base = { 319 .cra_name = "ghash", 320 .cra_driver_name = "ghash-ce", 321 .cra_priority = 300, 322 .cra_flags = CRYPTO_ALG_ASYNC, 323 .cra_blocksize = GHASH_BLOCK_SIZE, 324 .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 331 static int __init ghash_ce_mod_init(void) 332 { 333 int err; 334 335 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 343 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 352 err_shash: 353 crypto_unregister_shash(&ghash_alg); 354 return err; 355 } 356 357 static void __exit ghash_ce_mod_exit(void) 358 { 359 crypto_unregister_ahash(&ghash_async_alg); 360 crypto_unregister_shash(&ghash_alg); 361 } 362 363 module_init(ghash_ce_mod_init); 364 module_exit(ghash_ce_mod_exit); 365