^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Linux/arm64 port of the OpenSSL SHA256 implementation for AArch64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2016 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 <asm/hwcap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <asm/neon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <crypto/internal/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <crypto/sha256_base.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash for arm64");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) MODULE_AUTHOR("Andy Polyakov <appro@openssl.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) MODULE_ALIAS_CRYPTO("sha224");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) MODULE_ALIAS_CRYPTO("sha256");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) asmlinkage void sha256_block_data_order(u32 *digest, const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned int num_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) EXPORT_SYMBOL(sha256_block_data_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static void __sha256_block_data_order(struct sha256_state *sst, u8 const *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) sha256_block_data_order(sst->state, src, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) asmlinkage void sha256_block_neon(u32 *digest, const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned int num_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static void __sha256_block_neon(struct sha256_state *sst, u8 const *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) sha256_block_neon(sst->state, src, blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static int crypto_sha256_arm64_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return sha256_base_do_update(desc, data, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) __sha256_block_data_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int crypto_sha256_arm64_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) sha256_base_do_update(desc, data, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) __sha256_block_data_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) sha256_base_do_finalize(desc, __sha256_block_data_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return sha256_base_finish(desc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static int crypto_sha256_arm64_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return crypto_sha256_arm64_finup(desc, NULL, 0, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct shash_alg algs[] = { {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .digestsize = SHA256_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .init = sha256_base_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .update = crypto_sha256_arm64_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .final = crypto_sha256_arm64_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .finup = crypto_sha256_arm64_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .descsize = sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .base.cra_name = "sha256",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .base.cra_driver_name = "sha256-arm64",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .base.cra_priority = 125,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .base.cra_blocksize = SHA256_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .digestsize = SHA224_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .init = sha224_base_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .update = crypto_sha256_arm64_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .final = crypto_sha256_arm64_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .finup = crypto_sha256_arm64_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .descsize = sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .base.cra_name = "sha224",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .base.cra_driver_name = "sha224-arm64",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .base.cra_priority = 125,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .base.cra_blocksize = SHA224_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static int sha256_update_neon(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct sha256_state *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!crypto_simd_usable())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return sha256_base_do_update(desc, data, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) __sha256_block_data_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unsigned int chunk = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * Don't hog the CPU for the entire time it takes to process all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * input when running on a preemptible kernel, but process the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * data block by block instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (IS_ENABLED(CONFIG_PREEMPTION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) chunk + sctx->count % SHA256_BLOCK_SIZE > SHA256_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) chunk = SHA256_BLOCK_SIZE -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) sctx->count % SHA256_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kernel_neon_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) sha256_base_do_update(desc, data, chunk, __sha256_block_neon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) kernel_neon_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) data += chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) len -= chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int sha256_finup_neon(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!crypto_simd_usable()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) sha256_base_do_update(desc, data, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) __sha256_block_data_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) sha256_base_do_finalize(desc, __sha256_block_data_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) sha256_update_neon(desc, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) kernel_neon_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) sha256_base_do_finalize(desc, __sha256_block_neon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) kernel_neon_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return sha256_base_finish(desc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int sha256_final_neon(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return sha256_finup_neon(desc, NULL, 0, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static struct shash_alg neon_algs[] = { {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .digestsize = SHA256_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .init = sha256_base_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .update = sha256_update_neon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) .final = sha256_final_neon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .finup = sha256_finup_neon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) .descsize = sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .base.cra_name = "sha256",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .base.cra_driver_name = "sha256-arm64-neon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .base.cra_priority = 150,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .base.cra_blocksize = SHA256_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .digestsize = SHA224_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .init = sha224_base_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .update = sha256_update_neon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .final = sha256_final_neon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .finup = sha256_finup_neon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .descsize = sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .base.cra_name = "sha224",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .base.cra_driver_name = "sha224-arm64-neon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .base.cra_priority = 150,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .base.cra_blocksize = SHA224_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) } };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int __init sha256_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int ret = crypto_register_shashes(algs, ARRAY_SIZE(algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (cpu_have_named_feature(ASIMD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ret = crypto_register_shashes(neon_algs, ARRAY_SIZE(neon_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static void __exit sha256_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (cpu_have_named_feature(ASIMD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) crypto_unregister_shashes(neon_algs, ARRAY_SIZE(neon_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) module_init(sha256_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) module_exit(sha256_mod_fini);