2 * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
4 * Copyright (C) 2014 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.
12 #include <asm/unaligned.h>
13 #include <crypto/internal/hash.h>
14 #include <crypto/sha.h>
15 #include <crypto/sha1_base.h>
16 #include <linux/cpufeature.h>
17 #include <linux/crypto.h>
18 #include <linux/module.h>
20 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
21 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
22 MODULE_LICENSE("GPL v2");
24 struct sha1_ce_state {
25 struct sha1_state sst;
29 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
31 #ifdef CONFIG_CFI_CLANG
32 static inline void __cfi_sha1_ce_transform(struct sha1_state *sst,
33 u8 const *src, int blocks)
35 sha1_ce_transform((struct sha1_ce_state *)sst, src, blocks);
37 #define sha1_ce_transform __cfi_sha1_ce_transform
40 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
41 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
43 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
44 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
46 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
49 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
52 kernel_neon_begin_partial(16);
53 sha1_base_do_update(desc, data, len,
54 (sha1_block_fn *)sha1_ce_transform);
60 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
61 unsigned int len, u8 *out)
63 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
64 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
67 * Allow the asm code to perform the finalization if there is no
68 * partial data and the input is a round multiple of the block size.
70 sctx->finalize = finalize;
72 kernel_neon_begin_partial(16);
73 sha1_base_do_update(desc, data, len,
74 (sha1_block_fn *)sha1_ce_transform);
76 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
78 return sha1_base_finish(desc, out);
81 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
83 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
86 kernel_neon_begin_partial(16);
87 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
89 return sha1_base_finish(desc, out);
92 static struct shash_alg alg = {
93 .init = sha1_base_init,
94 .update = sha1_ce_update,
95 .final = sha1_ce_final,
96 .finup = sha1_ce_finup,
97 .descsize = sizeof(struct sha1_ce_state),
98 .digestsize = SHA1_DIGEST_SIZE,
101 .cra_driver_name = "sha1-ce",
103 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
104 .cra_blocksize = SHA1_BLOCK_SIZE,
105 .cra_module = THIS_MODULE,
109 static int __init sha1_ce_mod_init(void)
111 return crypto_register_shash(&alg);
114 static void __exit sha1_ce_mod_fini(void)
116 crypto_unregister_shash(&alg);
119 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
120 module_exit(sha1_ce_mod_fini);