^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * NHPoly1305 - ε-almost-∆-universal hash function for Adiantum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * (SSE2 accelerated version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright 2018 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/internal/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/nhpoly1305.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) asmlinkage void nh_sse2(const u32 *key, const u8 *message, size_t message_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) u8 hash[NH_HASH_BYTES]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* wrapper to avoid indirect call to assembly, which doesn't work with CFI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static void _nh_sse2(const u32 *key, const u8 *message, size_t message_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) __le64 hash[NH_NUM_PASSES])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) nh_sse2(key, message, message_len, (u8 *)hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int nhpoly1305_sse2_update(struct shash_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) const u8 *src, unsigned int srclen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (srclen < 64 || !crypto_simd_usable())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return crypto_nhpoly1305_update(desc, src, srclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned int n = min_t(unsigned int, srclen, SZ_4K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) kernel_fpu_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) crypto_nhpoly1305_update_helper(desc, src, n, _nh_sse2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) kernel_fpu_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) src += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) srclen -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) } while (srclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return 0;
^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 struct shash_alg nhpoly1305_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .base.cra_name = "nhpoly1305",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .base.cra_driver_name = "nhpoly1305-sse2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .base.cra_priority = 200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .base.cra_ctxsize = sizeof(struct nhpoly1305_key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .digestsize = POLY1305_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .init = crypto_nhpoly1305_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .update = nhpoly1305_sse2_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .final = crypto_nhpoly1305_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .setkey = crypto_nhpoly1305_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .descsize = sizeof(struct nhpoly1305_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int __init nhpoly1305_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!boot_cpu_has(X86_FEATURE_XMM2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return crypto_register_shash(&nhpoly1305_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static void __exit nhpoly1305_mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) crypto_unregister_shash(&nhpoly1305_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) module_init(nhpoly1305_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) module_exit(nhpoly1305_mod_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function (SSE2-accelerated)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) MODULE_ALIAS_CRYPTO("nhpoly1305");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) MODULE_ALIAS_CRYPTO("nhpoly1305-sse2");