Commit | Line | Data |
---|---|---|
2c98833a AB |
1 | /* |
2 | * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions | |
3 | * | |
4 | * Copyright (C) 2014 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/unaligned.h> | |
13 | #include <crypto/internal/hash.h> | |
14 | #include <crypto/sha.h> | |
07eb54d3 | 15 | #include <crypto/sha1_base.h> |
2c98833a AB |
16 | #include <linux/cpufeature.h> |
17 | #include <linux/crypto.h> | |
18 | #include <linux/module.h> | |
19 | ||
07eb54d3 AB |
20 | #define ASM_EXPORT(sym, val) \ |
21 | asm(".globl " #sym "; .set " #sym ", %0" :: "I"(val)); | |
22 | ||
2c98833a AB |
23 | MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions"); |
24 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); | |
25 | MODULE_LICENSE("GPL v2"); | |
26 | ||
07eb54d3 AB |
27 | struct sha1_ce_state { |
28 | struct sha1_state sst; | |
29 | u32 finalize; | |
30 | }; | |
2c98833a | 31 | |
07eb54d3 AB |
32 | asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src, |
33 | int blocks); | |
2c98833a | 34 | |
07eb54d3 AB |
35 | static int sha1_ce_update(struct shash_desc *desc, const u8 *data, |
36 | unsigned int len) | |
2c98833a | 37 | { |
07eb54d3 | 38 | struct sha1_ce_state *sctx = shash_desc_ctx(desc); |
2c98833a | 39 | |
07eb54d3 AB |
40 | sctx->finalize = 0; |
41 | kernel_neon_begin_partial(16); | |
42 | sha1_base_do_update(desc, data, len, | |
43 | (sha1_block_fn *)sha1_ce_transform); | |
44 | kernel_neon_end(); | |
2c98833a | 45 | |
2c98833a AB |
46 | return 0; |
47 | } | |
48 | ||
07eb54d3 AB |
49 | static int sha1_ce_finup(struct shash_desc *desc, const u8 *data, |
50 | unsigned int len, u8 *out) | |
2c98833a | 51 | { |
07eb54d3 AB |
52 | struct sha1_ce_state *sctx = shash_desc_ctx(desc); |
53 | bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE); | |
2c98833a | 54 | |
07eb54d3 AB |
55 | ASM_EXPORT(sha1_ce_offsetof_count, |
56 | offsetof(struct sha1_ce_state, sst.count)); | |
57 | ASM_EXPORT(sha1_ce_offsetof_finalize, | |
58 | offsetof(struct sha1_ce_state, finalize)); | |
2c98833a AB |
59 | |
60 | /* | |
07eb54d3 AB |
61 | * Allow the asm code to perform the finalization if there is no |
62 | * partial data and the input is a round multiple of the block size. | |
2c98833a | 63 | */ |
07eb54d3 | 64 | sctx->finalize = finalize; |
2c98833a AB |
65 | |
66 | kernel_neon_begin_partial(16); | |
07eb54d3 AB |
67 | sha1_base_do_update(desc, data, len, |
68 | (sha1_block_fn *)sha1_ce_transform); | |
69 | if (!finalize) | |
70 | sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); | |
2c98833a | 71 | kernel_neon_end(); |
07eb54d3 | 72 | return sha1_base_finish(desc, out); |
2c98833a AB |
73 | } |
74 | ||
07eb54d3 | 75 | static int sha1_ce_final(struct shash_desc *desc, u8 *out) |
2c98833a | 76 | { |
bf7883eb AB |
77 | struct sha1_ce_state *sctx = shash_desc_ctx(desc); |
78 | ||
79 | sctx->finalize = 0; | |
07eb54d3 AB |
80 | kernel_neon_begin_partial(16); |
81 | sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); | |
82 | kernel_neon_end(); | |
83 | return sha1_base_finish(desc, out); | |
2c98833a AB |
84 | } |
85 | ||
86 | static struct shash_alg alg = { | |
07eb54d3 AB |
87 | .init = sha1_base_init, |
88 | .update = sha1_ce_update, | |
89 | .final = sha1_ce_final, | |
90 | .finup = sha1_ce_finup, | |
91 | .descsize = sizeof(struct sha1_ce_state), | |
2c98833a | 92 | .digestsize = SHA1_DIGEST_SIZE, |
2c98833a AB |
93 | .base = { |
94 | .cra_name = "sha1", | |
95 | .cra_driver_name = "sha1-ce", | |
96 | .cra_priority = 200, | |
97 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, | |
98 | .cra_blocksize = SHA1_BLOCK_SIZE, | |
99 | .cra_module = THIS_MODULE, | |
100 | } | |
101 | }; | |
102 | ||
103 | static int __init sha1_ce_mod_init(void) | |
104 | { | |
105 | return crypto_register_shash(&alg); | |
106 | } | |
107 | ||
108 | static void __exit sha1_ce_mod_fini(void) | |
109 | { | |
110 | crypto_unregister_shash(&alg); | |
111 | } | |
112 | ||
113 | module_cpu_feature_match(SHA1, sha1_ce_mod_init); | |
114 | module_exit(sha1_ce_mod_fini); |