Path: blob/master/arch/riscv/crypto/ghash-riscv64-glue.c
26424 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* GHASH using the RISC-V vector crypto extensions3*4* Copyright (C) 2023 VRULL GmbH5* Author: Heiko Stuebner <[email protected]>6*7* Copyright (C) 2023 SiFive, Inc.8* Author: Jerry Shih <[email protected]>9*/1011#include <asm/simd.h>12#include <asm/vector.h>13#include <crypto/b128ops.h>14#include <crypto/gf128mul.h>15#include <crypto/ghash.h>16#include <crypto/internal/hash.h>17#include <crypto/internal/simd.h>18#include <crypto/utils.h>19#include <linux/errno.h>20#include <linux/kernel.h>21#include <linux/module.h>22#include <linux/string.h>2324asmlinkage void ghash_zvkg(be128 *accumulator, const be128 *key, const u8 *data,25size_t len);2627struct riscv64_ghash_tfm_ctx {28be128 key;29};3031struct riscv64_ghash_desc_ctx {32be128 accumulator;33};3435static int riscv64_ghash_setkey(struct crypto_shash *tfm, const u8 *key,36unsigned int keylen)37{38struct riscv64_ghash_tfm_ctx *tctx = crypto_shash_ctx(tfm);3940if (keylen != GHASH_BLOCK_SIZE)41return -EINVAL;4243memcpy(&tctx->key, key, GHASH_BLOCK_SIZE);4445return 0;46}4748static int riscv64_ghash_init(struct shash_desc *desc)49{50struct riscv64_ghash_desc_ctx *dctx = shash_desc_ctx(desc);5152*dctx = (struct riscv64_ghash_desc_ctx){};5354return 0;55}5657static inline void58riscv64_ghash_blocks(const struct riscv64_ghash_tfm_ctx *tctx,59struct riscv64_ghash_desc_ctx *dctx,60const u8 *src, size_t srclen)61{62/* The srclen is nonzero and a multiple of 16. */63if (crypto_simd_usable()) {64kernel_vector_begin();65ghash_zvkg(&dctx->accumulator, &tctx->key, src, srclen);66kernel_vector_end();67} else {68do {69crypto_xor((u8 *)&dctx->accumulator, src,70GHASH_BLOCK_SIZE);71gf128mul_lle(&dctx->accumulator, &tctx->key);72src += GHASH_BLOCK_SIZE;73srclen -= GHASH_BLOCK_SIZE;74} while (srclen);75}76}7778static int riscv64_ghash_update(struct shash_desc *desc, const u8 *src,79unsigned int srclen)80{81const struct riscv64_ghash_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);82struct riscv64_ghash_desc_ctx *dctx = shash_desc_ctx(desc);8384riscv64_ghash_blocks(tctx, dctx, src,85round_down(srclen, GHASH_BLOCK_SIZE));86return srclen - round_down(srclen, GHASH_BLOCK_SIZE);87}8889static int riscv64_ghash_finup(struct shash_desc *desc, const u8 *src,90unsigned int len, u8 *out)91{92const struct riscv64_ghash_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);93struct riscv64_ghash_desc_ctx *dctx = shash_desc_ctx(desc);9495if (len) {96u8 buf[GHASH_BLOCK_SIZE] = {};9798memcpy(buf, src, len);99riscv64_ghash_blocks(tctx, dctx, buf, GHASH_BLOCK_SIZE);100memzero_explicit(buf, sizeof(buf));101}102103memcpy(out, &dctx->accumulator, GHASH_DIGEST_SIZE);104return 0;105}106107static struct shash_alg riscv64_ghash_alg = {108.init = riscv64_ghash_init,109.update = riscv64_ghash_update,110.finup = riscv64_ghash_finup,111.setkey = riscv64_ghash_setkey,112.descsize = sizeof(struct riscv64_ghash_desc_ctx),113.digestsize = GHASH_DIGEST_SIZE,114.base = {115.cra_blocksize = GHASH_BLOCK_SIZE,116.cra_ctxsize = sizeof(struct riscv64_ghash_tfm_ctx),117.cra_priority = 300,118.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,119.cra_name = "ghash",120.cra_driver_name = "ghash-riscv64-zvkg",121.cra_module = THIS_MODULE,122},123};124125static int __init riscv64_ghash_mod_init(void)126{127if (riscv_isa_extension_available(NULL, ZVKG) &&128riscv_vector_vlen() >= 128)129return crypto_register_shash(&riscv64_ghash_alg);130131return -ENODEV;132}133134static void __exit riscv64_ghash_mod_exit(void)135{136crypto_unregister_shash(&riscv64_ghash_alg);137}138139module_init(riscv64_ghash_mod_init);140module_exit(riscv64_ghash_mod_exit);141142MODULE_DESCRIPTION("GHASH (RISC-V accelerated)");143MODULE_AUTHOR("Heiko Stuebner <[email protected]>");144MODULE_LICENSE("GPL");145MODULE_ALIAS_CRYPTO("ghash");146147148