Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
006d0624 AB |
2 | /* |
3 | * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions | |
4 | * | |
5 | * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org> | |
006d0624 AB |
6 | */ |
7 | ||
8 | #include <crypto/internal/hash.h> | |
99680c5e | 9 | #include <crypto/internal/simd.h> |
a24d22b2 | 10 | #include <crypto/sha2.h> |
9205b949 | 11 | #include <crypto/sha256_base.h> |
a83ff88b | 12 | #include <linux/cpufeature.h> |
006d0624 AB |
13 | #include <linux/crypto.h> |
14 | #include <linux/module.h> | |
15 | ||
16 | #include <asm/hwcap.h> | |
17 | #include <asm/simd.h> | |
18 | #include <asm/neon.h> | |
19 | #include <asm/unaligned.h> | |
20 | ||
9205b949 AB |
21 | #include "sha256_glue.h" |
22 | ||
006d0624 AB |
23 | MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions"); |
24 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); | |
25 | MODULE_LICENSE("GPL v2"); | |
26 | ||
9205b949 AB |
27 | asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src, |
28 | int blocks); | |
006d0624 | 29 | |
9205b949 AB |
30 | static int sha2_ce_update(struct shash_desc *desc, const u8 *data, |
31 | unsigned int len) | |
006d0624 AB |
32 | { |
33 | struct sha256_state *sctx = shash_desc_ctx(desc); | |
34 | ||
99680c5e | 35 | if (!crypto_simd_usable() || |
9205b949 AB |
36 | (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE) |
37 | return crypto_sha256_arm_update(desc, data, len); | |
006d0624 | 38 | |
9205b949 AB |
39 | kernel_neon_begin(); |
40 | sha256_base_do_update(desc, data, len, | |
41 | (sha256_block_fn *)sha2_ce_transform); | |
42 | kernel_neon_end(); | |
006d0624 | 43 | |
006d0624 AB |
44 | return 0; |
45 | } | |
46 | ||
9205b949 AB |
47 | static int sha2_ce_finup(struct shash_desc *desc, const u8 *data, |
48 | unsigned int len, u8 *out) | |
006d0624 | 49 | { |
99680c5e | 50 | if (!crypto_simd_usable()) |
9205b949 | 51 | return crypto_sha256_arm_finup(desc, data, len, out); |
006d0624 | 52 | |
9205b949 | 53 | kernel_neon_begin(); |
006d0624 | 54 | if (len) |
9205b949 AB |
55 | sha256_base_do_update(desc, data, len, |
56 | (sha256_block_fn *)sha2_ce_transform); | |
57 | sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform); | |
58 | kernel_neon_end(); | |
006d0624 | 59 | |
9205b949 | 60 | return sha256_base_finish(desc, out); |
006d0624 AB |
61 | } |
62 | ||
9205b949 | 63 | static int sha2_ce_final(struct shash_desc *desc, u8 *out) |
006d0624 | 64 | { |
9205b949 | 65 | return sha2_ce_finup(desc, NULL, 0, out); |
006d0624 AB |
66 | } |
67 | ||
68 | static struct shash_alg algs[] = { { | |
9205b949 AB |
69 | .init = sha224_base_init, |
70 | .update = sha2_ce_update, | |
71 | .final = sha2_ce_final, | |
72 | .finup = sha2_ce_finup, | |
006d0624 AB |
73 | .descsize = sizeof(struct sha256_state), |
74 | .digestsize = SHA224_DIGEST_SIZE, | |
006d0624 AB |
75 | .base = { |
76 | .cra_name = "sha224", | |
77 | .cra_driver_name = "sha224-ce", | |
f2f770d7 | 78 | .cra_priority = 300, |
006d0624 AB |
79 | .cra_blocksize = SHA256_BLOCK_SIZE, |
80 | .cra_module = THIS_MODULE, | |
81 | } | |
82 | }, { | |
9205b949 AB |
83 | .init = sha256_base_init, |
84 | .update = sha2_ce_update, | |
85 | .final = sha2_ce_final, | |
86 | .finup = sha2_ce_finup, | |
006d0624 AB |
87 | .descsize = sizeof(struct sha256_state), |
88 | .digestsize = SHA256_DIGEST_SIZE, | |
006d0624 AB |
89 | .base = { |
90 | .cra_name = "sha256", | |
91 | .cra_driver_name = "sha256-ce", | |
f2f770d7 | 92 | .cra_priority = 300, |
006d0624 AB |
93 | .cra_blocksize = SHA256_BLOCK_SIZE, |
94 | .cra_module = THIS_MODULE, | |
95 | } | |
96 | } }; | |
97 | ||
98 | static int __init sha2_ce_mod_init(void) | |
99 | { | |
006d0624 AB |
100 | return crypto_register_shashes(algs, ARRAY_SIZE(algs)); |
101 | } | |
102 | ||
103 | static void __exit sha2_ce_mod_fini(void) | |
104 | { | |
105 | crypto_unregister_shashes(algs, ARRAY_SIZE(algs)); | |
106 | } | |
107 | ||
a83ff88b | 108 | module_cpu_feature_match(SHA2, sha2_ce_mod_init); |
006d0624 | 109 | module_exit(sha2_ce_mod_fini); |