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