GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / arm64 / crypto / sha1-ce-glue.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
4  *
5  * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
6  */
7
8 #include <asm/neon.h>
9 #include <asm/simd.h>
10 #include <asm/unaligned.h>
11 #include <crypto/internal/hash.h>
12 #include <crypto/internal/simd.h>
13 #include <crypto/sha.h>
14 #include <crypto/sha1_base.h>
15 #include <linux/cpufeature.h>
16 #include <linux/crypto.h>
17 #include <linux/module.h>
18
19 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
20 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
21 MODULE_LICENSE("GPL v2");
22 MODULE_ALIAS_CRYPTO("sha1");
23
24 struct sha1_ce_state {
25         struct sha1_state       sst;
26         u32                     finalize;
27 };
28
29 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
30                                   int blocks);
31
32 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
33 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
34
35 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
36                           unsigned int len)
37 {
38         struct sha1_ce_state *sctx = shash_desc_ctx(desc);
39
40         if (!crypto_simd_usable())
41                 return crypto_sha1_update(desc, data, len);
42
43         sctx->finalize = 0;
44         kernel_neon_begin();
45         sha1_base_do_update(desc, data, len,
46                             (sha1_block_fn *)sha1_ce_transform);
47         kernel_neon_end();
48
49         return 0;
50 }
51
52 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
53                          unsigned int len, u8 *out)
54 {
55         struct sha1_ce_state *sctx = shash_desc_ctx(desc);
56         bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
57
58         if (!crypto_simd_usable())
59                 return crypto_sha1_finup(desc, data, len, out);
60
61         /*
62          * Allow the asm code to perform the finalization if there is no
63          * partial data and the input is a round multiple of the block size.
64          */
65         sctx->finalize = finalize;
66
67         kernel_neon_begin();
68         sha1_base_do_update(desc, data, len,
69                             (sha1_block_fn *)sha1_ce_transform);
70         if (!finalize)
71                 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
72         kernel_neon_end();
73         return sha1_base_finish(desc, out);
74 }
75
76 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
77 {
78         struct sha1_ce_state *sctx = shash_desc_ctx(desc);
79
80         if (!crypto_simd_usable())
81                 return crypto_sha1_finup(desc, NULL, 0, out);
82
83         sctx->finalize = 0;
84         kernel_neon_begin();
85         sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
86         kernel_neon_end();
87         return sha1_base_finish(desc, out);
88 }
89
90 static struct shash_alg alg = {
91         .init                   = sha1_base_init,
92         .update                 = sha1_ce_update,
93         .final                  = sha1_ce_final,
94         .finup                  = sha1_ce_finup,
95         .descsize               = sizeof(struct sha1_ce_state),
96         .digestsize             = SHA1_DIGEST_SIZE,
97         .base                   = {
98                 .cra_name               = "sha1",
99                 .cra_driver_name        = "sha1-ce",
100                 .cra_priority           = 200,
101                 .cra_blocksize          = SHA1_BLOCK_SIZE,
102                 .cra_module             = THIS_MODULE,
103         }
104 };
105
106 static int __init sha1_ce_mod_init(void)
107 {
108         return crypto_register_shash(&alg);
109 }
110
111 static void __exit sha1_ce_mod_fini(void)
112 {
113         crypto_unregister_shash(&alg);
114 }
115
116 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
117 module_exit(sha1_ce_mod_fini);