^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * sha512-neon-glue.c - accelerated SHA-384/512 for ARM NEON
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <crypto/internal/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/sha512_base.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/neon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "sha512.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) MODULE_ALIAS_CRYPTO("sha384-neon");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) MODULE_ALIAS_CRYPTO("sha512-neon");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) asmlinkage void sha512_block_data_order_neon(u64 *state, u8 const *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int sha512_neon_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct sha512_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (!crypto_simd_usable() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) (sctx->count[0] % SHA512_BLOCK_SIZE) + len < SHA512_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return sha512_arm_update(desc, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) kernel_neon_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) sha512_base_do_update(desc, data, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) (sha512_block_fn *)sha512_block_data_order_neon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) kernel_neon_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int sha512_neon_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (!crypto_simd_usable())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return sha512_arm_finup(desc, data, len, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) kernel_neon_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sha512_base_do_update(desc, data, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) (sha512_block_fn *)sha512_block_data_order_neon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) sha512_base_do_finalize(desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) (sha512_block_fn *)sha512_block_data_order_neon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) kernel_neon_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return sha512_base_finish(desc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int sha512_neon_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return sha512_neon_finup(desc, NULL, 0, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct shash_alg sha512_neon_algs[] = { {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .init = sha384_base_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .update = sha512_neon_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .final = sha512_neon_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .finup = sha512_neon_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .descsize = sizeof(struct sha512_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .digestsize = SHA384_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .cra_name = "sha384",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .cra_driver_name = "sha384-neon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .cra_priority = 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .cra_blocksize = SHA384_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .init = sha512_base_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .update = sha512_neon_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .final = sha512_neon_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .finup = sha512_neon_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .descsize = sizeof(struct sha512_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .digestsize = SHA512_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .cra_name = "sha512",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .cra_driver_name = "sha512-neon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .cra_priority = 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .cra_blocksize = SHA512_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) } };