]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - arch/x86/crypto/nhpoly1305-avx2-glue.c
crypto: arm64/nhpoly1305 - implement ->digest
[thirdparty/kernel/linux.git] / arch / x86 / crypto / nhpoly1305-avx2-glue.c
CommitLineData
0f961f9f
EB
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>
f2abe0d7 10#include <crypto/internal/simd.h>
0f961f9f
EB
11#include <crypto/nhpoly1305.h>
12#include <linux/module.h>
0c3dc787 13#include <linux/sizes.h>
f2abe0d7 14#include <asm/simd.h>
0f961f9f
EB
15
16asmlinkage void nh_avx2(const u32 *key, const u8 *message, size_t message_len,
0f8bc4bd 17 __le64 hash[NH_NUM_PASSES]);
0f961f9f
EB
18
19static int nhpoly1305_avx2_update(struct shash_desc *desc,
20 const u8 *src, unsigned int srclen)
21{
f2abe0d7 22 if (srclen < 64 || !crypto_simd_usable())
0f961f9f
EB
23 return crypto_nhpoly1305_update(desc, src, srclen);
24
25 do {
a9a8ba90 26 unsigned int n = min_t(unsigned int, srclen, SZ_4K);
0f961f9f
EB
27
28 kernel_fpu_begin();
0f8bc4bd 29 crypto_nhpoly1305_update_helper(desc, src, n, nh_avx2);
0f961f9f
EB
30 kernel_fpu_end();
31 src += n;
32 srclen -= n;
33 } while (srclen);
34 return 0;
35}
36
37static struct shash_alg nhpoly1305_alg = {
38 .base.cra_name = "nhpoly1305",
39 .base.cra_driver_name = "nhpoly1305-avx2",
40 .base.cra_priority = 300,
41 .base.cra_ctxsize = sizeof(struct nhpoly1305_key),
42 .base.cra_module = THIS_MODULE,
43 .digestsize = POLY1305_DIGEST_SIZE,
44 .init = crypto_nhpoly1305_init,
45 .update = nhpoly1305_avx2_update,
46 .final = crypto_nhpoly1305_final,
47 .setkey = crypto_nhpoly1305_setkey,
48 .descsize = sizeof(struct nhpoly1305_state),
49};
50
51static int __init nhpoly1305_mod_init(void)
52{
53 if (!boot_cpu_has(X86_FEATURE_AVX2) ||
54 !boot_cpu_has(X86_FEATURE_OSXSAVE))
55 return -ENODEV;
56
57 return crypto_register_shash(&nhpoly1305_alg);
58}
59
60static void __exit nhpoly1305_mod_exit(void)
61{
62 crypto_unregister_shash(&nhpoly1305_alg);
63}
64
65module_init(nhpoly1305_mod_init);
66module_exit(nhpoly1305_mod_exit);
67
68MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function (AVX2-accelerated)");
69MODULE_LICENSE("GPL v2");
70MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
71MODULE_ALIAS_CRYPTO("nhpoly1305");
72MODULE_ALIAS_CRYPTO("nhpoly1305-avx2");