GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / arm64 / crypto / sha512-ce-glue.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * sha512-ce-glue.c - SHA-384/SHA-512 using ARMv8 Crypto Extensions
4  *
5  * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <asm/neon.h>
13 #include <asm/simd.h>
14 #include <asm/unaligned.h>
15 #include <crypto/internal/hash.h>
16 #include <crypto/sha.h>
17 #include <crypto/sha512_base.h>
18 #include <linux/cpufeature.h>
19 #include <linux/crypto.h>
20 #include <linux/module.h>
21
22 MODULE_DESCRIPTION("SHA-384/SHA-512 secure hash using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
25 MODULE_ALIAS_CRYPTO("sha384");
26 MODULE_ALIAS_CRYPTO("sha512");
27
28 asmlinkage void sha512_ce_transform(struct sha512_state *sst, u8 const *src,
29                                     int blocks);
30
31 asmlinkage void sha512_block_data_order(u64 *digest, u8 const *src, int blocks);
32
33 static int sha512_ce_update(struct shash_desc *desc, const u8 *data,
34                             unsigned int len)
35 {
36         if (!may_use_simd())
37                 return sha512_base_do_update(desc, data, len,
38                                 (sha512_block_fn *)sha512_block_data_order);
39
40         kernel_neon_begin();
41         sha512_base_do_update(desc, data, len,
42                               (sha512_block_fn *)sha512_ce_transform);
43         kernel_neon_end();
44
45         return 0;
46 }
47
48 static int sha512_ce_finup(struct shash_desc *desc, const u8 *data,
49                            unsigned int len, u8 *out)
50 {
51         if (!may_use_simd()) {
52                 if (len)
53                         sha512_base_do_update(desc, data, len,
54                                 (sha512_block_fn *)sha512_block_data_order);
55                 sha512_base_do_finalize(desc,
56                                 (sha512_block_fn *)sha512_block_data_order);
57                 return sha512_base_finish(desc, out);
58         }
59
60         kernel_neon_begin();
61         sha512_base_do_update(desc, data, len,
62                               (sha512_block_fn *)sha512_ce_transform);
63         sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
64         kernel_neon_end();
65         return sha512_base_finish(desc, out);
66 }
67
68 static int sha512_ce_final(struct shash_desc *desc, u8 *out)
69 {
70         if (!may_use_simd()) {
71                 sha512_base_do_finalize(desc,
72                                 (sha512_block_fn *)sha512_block_data_order);
73                 return sha512_base_finish(desc, out);
74         }
75
76         kernel_neon_begin();
77         sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
78         kernel_neon_end();
79         return sha512_base_finish(desc, out);
80 }
81
82 static struct shash_alg algs[] = { {
83         .init                   = sha384_base_init,
84         .update                 = sha512_ce_update,
85         .final                  = sha512_ce_final,
86         .finup                  = sha512_ce_finup,
87         .descsize               = sizeof(struct sha512_state),
88         .digestsize             = SHA384_DIGEST_SIZE,
89         .base.cra_name          = "sha384",
90         .base.cra_driver_name   = "sha384-ce",
91         .base.cra_priority      = 200,
92         .base.cra_blocksize     = SHA512_BLOCK_SIZE,
93         .base.cra_module        = THIS_MODULE,
94 }, {
95         .init                   = sha512_base_init,
96         .update                 = sha512_ce_update,
97         .final                  = sha512_ce_final,
98         .finup                  = sha512_ce_finup,
99         .descsize               = sizeof(struct sha512_state),
100         .digestsize             = SHA512_DIGEST_SIZE,
101         .base.cra_name          = "sha512",
102         .base.cra_driver_name   = "sha512-ce",
103         .base.cra_priority      = 200,
104         .base.cra_blocksize     = SHA512_BLOCK_SIZE,
105         .base.cra_module        = THIS_MODULE,
106 } };
107
108 static int __init sha512_ce_mod_init(void)
109 {
110         return crypto_register_shashes(algs, ARRAY_SIZE(algs));
111 }
112
113 static void __exit sha512_ce_mod_fini(void)
114 {
115         crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
116 }
117
118 module_cpu_feature_match(SHA512, sha512_ce_mod_init);
119 module_exit(sha512_ce_mod_fini);