GNU Linux-libre 5.4.274-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/internal/simd.h>
17 #include <crypto/sha.h>
18 #include <crypto/sha512_base.h>
19 #include <linux/cpufeature.h>
20 #include <linux/crypto.h>
21 #include <linux/module.h>
22
23 MODULE_DESCRIPTION("SHA-384/SHA-512 secure hash using ARMv8 Crypto Extensions");
24 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
25 MODULE_LICENSE("GPL v2");
26 MODULE_ALIAS_CRYPTO("sha384");
27 MODULE_ALIAS_CRYPTO("sha512");
28
29 asmlinkage void sha512_ce_transform(struct sha512_state *sst, u8 const *src,
30                                     int blocks);
31
32 asmlinkage void sha512_block_data_order(u64 *digest, u8 const *src, int blocks);
33
34 static int sha512_ce_update(struct shash_desc *desc, const u8 *data,
35                             unsigned int len)
36 {
37         if (!crypto_simd_usable())
38                 return sha512_base_do_update(desc, data, len,
39                                 (sha512_block_fn *)sha512_block_data_order);
40
41         kernel_neon_begin();
42         sha512_base_do_update(desc, data, len,
43                               (sha512_block_fn *)sha512_ce_transform);
44         kernel_neon_end();
45
46         return 0;
47 }
48
49 static int sha512_ce_finup(struct shash_desc *desc, const u8 *data,
50                            unsigned int len, u8 *out)
51 {
52         if (!crypto_simd_usable()) {
53                 if (len)
54                         sha512_base_do_update(desc, data, len,
55                                 (sha512_block_fn *)sha512_block_data_order);
56                 sha512_base_do_finalize(desc,
57                                 (sha512_block_fn *)sha512_block_data_order);
58                 return sha512_base_finish(desc, out);
59         }
60
61         kernel_neon_begin();
62         sha512_base_do_update(desc, data, len,
63                               (sha512_block_fn *)sha512_ce_transform);
64         sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
65         kernel_neon_end();
66         return sha512_base_finish(desc, out);
67 }
68
69 static int sha512_ce_final(struct shash_desc *desc, u8 *out)
70 {
71         if (!crypto_simd_usable()) {
72                 sha512_base_do_finalize(desc,
73                                 (sha512_block_fn *)sha512_block_data_order);
74                 return sha512_base_finish(desc, out);
75         }
76
77         kernel_neon_begin();
78         sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
79         kernel_neon_end();
80         return sha512_base_finish(desc, out);
81 }
82
83 static struct shash_alg algs[] = { {
84         .init                   = sha384_base_init,
85         .update                 = sha512_ce_update,
86         .final                  = sha512_ce_final,
87         .finup                  = sha512_ce_finup,
88         .descsize               = sizeof(struct sha512_state),
89         .digestsize             = SHA384_DIGEST_SIZE,
90         .base.cra_name          = "sha384",
91         .base.cra_driver_name   = "sha384-ce",
92         .base.cra_priority      = 200,
93         .base.cra_blocksize     = SHA512_BLOCK_SIZE,
94         .base.cra_module        = THIS_MODULE,
95 }, {
96         .init                   = sha512_base_init,
97         .update                 = sha512_ce_update,
98         .final                  = sha512_ce_final,
99         .finup                  = sha512_ce_finup,
100         .descsize               = sizeof(struct sha512_state),
101         .digestsize             = SHA512_DIGEST_SIZE,
102         .base.cra_name          = "sha512",
103         .base.cra_driver_name   = "sha512-ce",
104         .base.cra_priority      = 200,
105         .base.cra_blocksize     = SHA512_BLOCK_SIZE,
106         .base.cra_module        = THIS_MODULE,
107 } };
108
109 static int __init sha512_ce_mod_init(void)
110 {
111         return crypto_register_shashes(algs, ARRAY_SIZE(algs));
112 }
113
114 static void __exit sha512_ce_mod_fini(void)
115 {
116         crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
117 }
118
119 module_cpu_feature_match(SHA512, sha512_ce_mod_init);
120 module_exit(sha512_ce_mod_fini);