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