GNU Linux-libre 4.19.207-gnu1
[releases.git] / arch / arm64 / crypto / sha2-ce-glue.c
1 /*
2  * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
3  *
4  * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <asm/neon.h>
12 #include <asm/simd.h>
13 #include <asm/unaligned.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/sha.h>
16 #include <crypto/sha256_base.h>
17 #include <linux/cpufeature.h>
18 #include <linux/crypto.h>
19 #include <linux/module.h>
20
21 MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
22 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
23 MODULE_LICENSE("GPL v2");
24 MODULE_ALIAS_CRYPTO("sha224");
25 MODULE_ALIAS_CRYPTO("sha256");
26
27 struct sha256_ce_state {
28         struct sha256_state     sst;
29         u32                     finalize;
30 };
31
32 asmlinkage void sha2_ce_transform(struct sha256_ce_state *sst, u8 const *src,
33                                   int blocks);
34
35 const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state,
36                                               sst.count);
37 const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state,
38                                                  finalize);
39
40 asmlinkage void sha256_block_data_order(u32 *digest, u8 const *src, int blocks);
41
42 static int sha256_ce_update(struct shash_desc *desc, const u8 *data,
43                             unsigned int len)
44 {
45         struct sha256_ce_state *sctx = shash_desc_ctx(desc);
46
47         if (!may_use_simd())
48                 return sha256_base_do_update(desc, data, len,
49                                 (sha256_block_fn *)sha256_block_data_order);
50
51         sctx->finalize = 0;
52         kernel_neon_begin();
53         sha256_base_do_update(desc, data, len,
54                               (sha256_block_fn *)sha2_ce_transform);
55         kernel_neon_end();
56
57         return 0;
58 }
59
60 static int sha256_ce_finup(struct shash_desc *desc, const u8 *data,
61                            unsigned int len, u8 *out)
62 {
63         struct sha256_ce_state *sctx = shash_desc_ctx(desc);
64         bool finalize = !sctx->sst.count && !(len % SHA256_BLOCK_SIZE) && len;
65
66         if (!may_use_simd()) {
67                 if (len)
68                         sha256_base_do_update(desc, data, len,
69                                 (sha256_block_fn *)sha256_block_data_order);
70                 sha256_base_do_finalize(desc,
71                                 (sha256_block_fn *)sha256_block_data_order);
72                 return sha256_base_finish(desc, out);
73         }
74
75         /*
76          * Allow the asm code to perform the finalization if there is no
77          * partial data and the input is a round multiple of the block size.
78          */
79         sctx->finalize = finalize;
80
81         kernel_neon_begin();
82         sha256_base_do_update(desc, data, len,
83                               (sha256_block_fn *)sha2_ce_transform);
84         if (!finalize)
85                 sha256_base_do_finalize(desc,
86                                         (sha256_block_fn *)sha2_ce_transform);
87         kernel_neon_end();
88         return sha256_base_finish(desc, out);
89 }
90
91 static int sha256_ce_final(struct shash_desc *desc, u8 *out)
92 {
93         struct sha256_ce_state *sctx = shash_desc_ctx(desc);
94
95         if (!may_use_simd()) {
96                 sha256_base_do_finalize(desc,
97                                 (sha256_block_fn *)sha256_block_data_order);
98                 return sha256_base_finish(desc, out);
99         }
100
101         sctx->finalize = 0;
102         kernel_neon_begin();
103         sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
104         kernel_neon_end();
105         return sha256_base_finish(desc, out);
106 }
107
108 static struct shash_alg algs[] = { {
109         .init                   = sha224_base_init,
110         .update                 = sha256_ce_update,
111         .final                  = sha256_ce_final,
112         .finup                  = sha256_ce_finup,
113         .descsize               = sizeof(struct sha256_ce_state),
114         .digestsize             = SHA224_DIGEST_SIZE,
115         .base                   = {
116                 .cra_name               = "sha224",
117                 .cra_driver_name        = "sha224-ce",
118                 .cra_priority           = 200,
119                 .cra_blocksize          = SHA256_BLOCK_SIZE,
120                 .cra_module             = THIS_MODULE,
121         }
122 }, {
123         .init                   = sha256_base_init,
124         .update                 = sha256_ce_update,
125         .final                  = sha256_ce_final,
126         .finup                  = sha256_ce_finup,
127         .descsize               = sizeof(struct sha256_ce_state),
128         .digestsize             = SHA256_DIGEST_SIZE,
129         .base                   = {
130                 .cra_name               = "sha256",
131                 .cra_driver_name        = "sha256-ce",
132                 .cra_priority           = 200,
133                 .cra_blocksize          = SHA256_BLOCK_SIZE,
134                 .cra_module             = THIS_MODULE,
135         }
136 } };
137
138 static int __init sha2_ce_mod_init(void)
139 {
140         return crypto_register_shashes(algs, ARRAY_SIZE(algs));
141 }
142
143 static void __exit sha2_ce_mod_fini(void)
144 {
145         crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
146 }
147
148 module_cpu_feature_match(SHA2, sha2_ce_mod_init);
149 module_exit(sha2_ce_mod_fini);