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