]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/arm64/crypto/crc32-ce-glue.c
Merge tag 'rodata-v4.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees...
[karo-tx-linux.git] / arch / arm64 / crypto / crc32-ce-glue.c
1 /*
2  * Accelerated CRC32(C) using arm64 NEON and Crypto Extensions instructions
3  *
4  * Copyright (C) 2016 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 <linux/cpufeature.h>
12 #include <linux/crc32.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17
18 #include <crypto/internal/hash.h>
19
20 #include <asm/hwcap.h>
21 #include <asm/neon.h>
22 #include <asm/unaligned.h>
23
24 #define PMULL_MIN_LEN           64L     /* minimum size of buffer
25                                          * for crc32_pmull_le_16 */
26 #define SCALE_F                 16L     /* size of NEON register */
27
28 asmlinkage u32 crc32_pmull_le(const u8 buf[], u64 len, u32 init_crc);
29 asmlinkage u32 crc32_armv8_le(u32 init_crc, const u8 buf[], size_t len);
30
31 asmlinkage u32 crc32c_pmull_le(const u8 buf[], u64 len, u32 init_crc);
32 asmlinkage u32 crc32c_armv8_le(u32 init_crc, const u8 buf[], size_t len);
33
34 static u32 (*fallback_crc32)(u32 init_crc, const u8 buf[], size_t len);
35 static u32 (*fallback_crc32c)(u32 init_crc, const u8 buf[], size_t len);
36
37 static int crc32_pmull_cra_init(struct crypto_tfm *tfm)
38 {
39         u32 *key = crypto_tfm_ctx(tfm);
40
41         *key = 0;
42         return 0;
43 }
44
45 static int crc32c_pmull_cra_init(struct crypto_tfm *tfm)
46 {
47         u32 *key = crypto_tfm_ctx(tfm);
48
49         *key = ~0;
50         return 0;
51 }
52
53 static int crc32_pmull_setkey(struct crypto_shash *hash, const u8 *key,
54                               unsigned int keylen)
55 {
56         u32 *mctx = crypto_shash_ctx(hash);
57
58         if (keylen != sizeof(u32)) {
59                 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
60                 return -EINVAL;
61         }
62         *mctx = le32_to_cpup((__le32 *)key);
63         return 0;
64 }
65
66 static int crc32_pmull_init(struct shash_desc *desc)
67 {
68         u32 *mctx = crypto_shash_ctx(desc->tfm);
69         u32 *crc = shash_desc_ctx(desc);
70
71         *crc = *mctx;
72         return 0;
73 }
74
75 static int crc32_pmull_update(struct shash_desc *desc, const u8 *data,
76                          unsigned int length)
77 {
78         u32 *crc = shash_desc_ctx(desc);
79         unsigned int l;
80
81         if ((u64)data % SCALE_F) {
82                 l = min_t(u32, length, SCALE_F - ((u64)data % SCALE_F));
83
84                 *crc = fallback_crc32(*crc, data, l);
85
86                 data += l;
87                 length -= l;
88         }
89
90         if (length >= PMULL_MIN_LEN) {
91                 l = round_down(length, SCALE_F);
92
93                 kernel_neon_begin_partial(10);
94                 *crc = crc32_pmull_le(data, l, *crc);
95                 kernel_neon_end();
96
97                 data += l;
98                 length -= l;
99         }
100
101         if (length > 0)
102                 *crc = fallback_crc32(*crc, data, length);
103
104         return 0;
105 }
106
107 static int crc32c_pmull_update(struct shash_desc *desc, const u8 *data,
108                          unsigned int length)
109 {
110         u32 *crc = shash_desc_ctx(desc);
111         unsigned int l;
112
113         if ((u64)data % SCALE_F) {
114                 l = min_t(u32, length, SCALE_F - ((u64)data % SCALE_F));
115
116                 *crc = fallback_crc32c(*crc, data, l);
117
118                 data += l;
119                 length -= l;
120         }
121
122         if (length >= PMULL_MIN_LEN) {
123                 l = round_down(length, SCALE_F);
124
125                 kernel_neon_begin_partial(10);
126                 *crc = crc32c_pmull_le(data, l, *crc);
127                 kernel_neon_end();
128
129                 data += l;
130                 length -= l;
131         }
132
133         if (length > 0) {
134                 *crc = fallback_crc32c(*crc, data, length);
135         }
136
137         return 0;
138 }
139
140 static int crc32_pmull_final(struct shash_desc *desc, u8 *out)
141 {
142         u32 *crc = shash_desc_ctx(desc);
143
144         put_unaligned_le32(*crc, out);
145         return 0;
146 }
147
148 static int crc32c_pmull_final(struct shash_desc *desc, u8 *out)
149 {
150         u32 *crc = shash_desc_ctx(desc);
151
152         put_unaligned_le32(~*crc, out);
153         return 0;
154 }
155
156 static struct shash_alg crc32_pmull_algs[] = { {
157         .setkey                 = crc32_pmull_setkey,
158         .init                   = crc32_pmull_init,
159         .update                 = crc32_pmull_update,
160         .final                  = crc32_pmull_final,
161         .descsize               = sizeof(u32),
162         .digestsize             = sizeof(u32),
163
164         .base.cra_ctxsize       = sizeof(u32),
165         .base.cra_init          = crc32_pmull_cra_init,
166         .base.cra_name          = "crc32",
167         .base.cra_driver_name   = "crc32-arm64-ce",
168         .base.cra_priority      = 200,
169         .base.cra_blocksize     = 1,
170         .base.cra_module        = THIS_MODULE,
171 }, {
172         .setkey                 = crc32_pmull_setkey,
173         .init                   = crc32_pmull_init,
174         .update                 = crc32c_pmull_update,
175         .final                  = crc32c_pmull_final,
176         .descsize               = sizeof(u32),
177         .digestsize             = sizeof(u32),
178
179         .base.cra_ctxsize       = sizeof(u32),
180         .base.cra_init          = crc32c_pmull_cra_init,
181         .base.cra_name          = "crc32c",
182         .base.cra_driver_name   = "crc32c-arm64-ce",
183         .base.cra_priority      = 200,
184         .base.cra_blocksize     = 1,
185         .base.cra_module        = THIS_MODULE,
186 } };
187
188 static int __init crc32_pmull_mod_init(void)
189 {
190         if (elf_hwcap & HWCAP_CRC32) {
191                 fallback_crc32 = crc32_armv8_le;
192                 fallback_crc32c = crc32c_armv8_le;
193         } else {
194                 fallback_crc32 = crc32_le;
195                 fallback_crc32c = __crc32c_le;
196         }
197
198         return crypto_register_shashes(crc32_pmull_algs,
199                                        ARRAY_SIZE(crc32_pmull_algs));
200 }
201
202 static void __exit crc32_pmull_mod_exit(void)
203 {
204         crypto_unregister_shashes(crc32_pmull_algs,
205                                   ARRAY_SIZE(crc32_pmull_algs));
206 }
207
208 module_cpu_feature_match(PMULL, crc32_pmull_mod_init);
209 module_exit(crc32_pmull_mod_exit);
210
211 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
212 MODULE_LICENSE("GPL v2");