2 * Accelerated CRC32(C) using ARM CRC, NEON and Crypto Extensions instructions
4 * Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org>
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.
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>
18 #include <crypto/internal/hash.h>
19 #include <crypto/internal/simd.h>
21 #include <asm/hwcap.h>
24 #include <asm/unaligned.h>
26 #define PMULL_MIN_LEN 64L /* minimum size of buffer
27 * for crc32_pmull_le_16 */
28 #define SCALE_F 16L /* size of NEON register */
30 asmlinkage u32
crc32_pmull_le(const u8 buf
[], u32 len
, u32 init_crc
);
31 asmlinkage u32
crc32_armv8_le(u32 init_crc
, const u8 buf
[], u32 len
);
33 asmlinkage u32
crc32c_pmull_le(const u8 buf
[], u32 len
, u32 init_crc
);
34 asmlinkage u32
crc32c_armv8_le(u32 init_crc
, const u8 buf
[], u32 len
);
36 static u32 (*fallback_crc32
)(u32 init_crc
, const u8 buf
[], u32 len
);
37 static u32 (*fallback_crc32c
)(u32 init_crc
, const u8 buf
[], u32 len
);
39 static int crc32_cra_init(struct crypto_tfm
*tfm
)
41 u32
*key
= crypto_tfm_ctx(tfm
);
47 static int crc32c_cra_init(struct crypto_tfm
*tfm
)
49 u32
*key
= crypto_tfm_ctx(tfm
);
55 static int crc32_setkey(struct crypto_shash
*hash
, const u8
*key
,
58 u32
*mctx
= crypto_shash_ctx(hash
);
60 if (keylen
!= sizeof(u32
)) {
61 crypto_shash_set_flags(hash
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
64 *mctx
= le32_to_cpup((__le32
*)key
);
68 static int crc32_init(struct shash_desc
*desc
)
70 u32
*mctx
= crypto_shash_ctx(desc
->tfm
);
71 u32
*crc
= shash_desc_ctx(desc
);
77 static int crc32_update(struct shash_desc
*desc
, const u8
*data
,
80 u32
*crc
= shash_desc_ctx(desc
);
82 *crc
= crc32_armv8_le(*crc
, data
, length
);
86 static int crc32c_update(struct shash_desc
*desc
, const u8
*data
,
89 u32
*crc
= shash_desc_ctx(desc
);
91 *crc
= crc32c_armv8_le(*crc
, data
, length
);
95 static int crc32_final(struct shash_desc
*desc
, u8
*out
)
97 u32
*crc
= shash_desc_ctx(desc
);
99 put_unaligned_le32(*crc
, out
);
103 static int crc32c_final(struct shash_desc
*desc
, u8
*out
)
105 u32
*crc
= shash_desc_ctx(desc
);
107 put_unaligned_le32(~*crc
, out
);
111 static int crc32_pmull_update(struct shash_desc
*desc
, const u8
*data
,
114 u32
*crc
= shash_desc_ctx(desc
);
117 if (crypto_simd_usable()) {
118 if ((u32
)data
% SCALE_F
) {
119 l
= min_t(u32
, length
, SCALE_F
- ((u32
)data
% SCALE_F
));
121 *crc
= fallback_crc32(*crc
, data
, l
);
127 if (length
>= PMULL_MIN_LEN
) {
128 l
= round_down(length
, SCALE_F
);
131 *crc
= crc32_pmull_le(data
, l
, *crc
);
140 *crc
= fallback_crc32(*crc
, data
, length
);
145 static int crc32c_pmull_update(struct shash_desc
*desc
, const u8
*data
,
148 u32
*crc
= shash_desc_ctx(desc
);
151 if (crypto_simd_usable()) {
152 if ((u32
)data
% SCALE_F
) {
153 l
= min_t(u32
, length
, SCALE_F
- ((u32
)data
% SCALE_F
));
155 *crc
= fallback_crc32c(*crc
, data
, l
);
161 if (length
>= PMULL_MIN_LEN
) {
162 l
= round_down(length
, SCALE_F
);
165 *crc
= crc32c_pmull_le(data
, l
, *crc
);
174 *crc
= fallback_crc32c(*crc
, data
, length
);
179 static struct shash_alg crc32_pmull_algs
[] = { {
180 .setkey
= crc32_setkey
,
182 .update
= crc32_update
,
183 .final
= crc32_final
,
184 .descsize
= sizeof(u32
),
185 .digestsize
= sizeof(u32
),
187 .base
.cra_ctxsize
= sizeof(u32
),
188 .base
.cra_init
= crc32_cra_init
,
189 .base
.cra_name
= "crc32",
190 .base
.cra_driver_name
= "crc32-arm-ce",
191 .base
.cra_priority
= 200,
192 .base
.cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
193 .base
.cra_blocksize
= 1,
194 .base
.cra_module
= THIS_MODULE
,
196 .setkey
= crc32_setkey
,
198 .update
= crc32c_update
,
199 .final
= crc32c_final
,
200 .descsize
= sizeof(u32
),
201 .digestsize
= sizeof(u32
),
203 .base
.cra_ctxsize
= sizeof(u32
),
204 .base
.cra_init
= crc32c_cra_init
,
205 .base
.cra_name
= "crc32c",
206 .base
.cra_driver_name
= "crc32c-arm-ce",
207 .base
.cra_priority
= 200,
208 .base
.cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
209 .base
.cra_blocksize
= 1,
210 .base
.cra_module
= THIS_MODULE
,
213 static int __init
crc32_pmull_mod_init(void)
215 if (elf_hwcap2
& HWCAP2_PMULL
) {
216 crc32_pmull_algs
[0].update
= crc32_pmull_update
;
217 crc32_pmull_algs
[1].update
= crc32c_pmull_update
;
219 if (elf_hwcap2
& HWCAP2_CRC32
) {
220 fallback_crc32
= crc32_armv8_le
;
221 fallback_crc32c
= crc32c_armv8_le
;
223 fallback_crc32
= crc32_le
;
224 fallback_crc32c
= __crc32c_le
;
226 } else if (!(elf_hwcap2
& HWCAP2_CRC32
)) {
230 return crypto_register_shashes(crc32_pmull_algs
,
231 ARRAY_SIZE(crc32_pmull_algs
));
234 static void __exit
crc32_pmull_mod_exit(void)
236 crypto_unregister_shashes(crc32_pmull_algs
,
237 ARRAY_SIZE(crc32_pmull_algs
));
240 static const struct cpu_feature __maybe_unused crc32_cpu_feature
[] = {
241 { cpu_feature(CRC32
) }, { cpu_feature(PMULL
) }, { }
243 MODULE_DEVICE_TABLE(cpu
, crc32_cpu_feature
);
245 module_init(crc32_pmull_mod_init
);
246 module_exit(crc32_pmull_mod_exit
);
248 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
249 MODULE_LICENSE("GPL v2");
250 MODULE_ALIAS_CRYPTO("crc32");
251 MODULE_ALIAS_CRYPTO("crc32c");