blob: f08ecf431dcf8ce195099acc0c142a84e2bb29d2 [file] [log] [blame]
Ard Biesheuvel2c988332014-03-06 16:23:33 +08001/*
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>
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020015#include <crypto/sha1_base.h>
Ard Biesheuvel2c988332014-03-06 16:23:33 +080016#include <linux/cpufeature.h>
17#include <linux/crypto.h>
18#include <linux/module.h>
19
20MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
21MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
22MODULE_LICENSE("GPL v2");
23
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020024struct sha1_ce_state {
25 struct sha1_state sst;
26 u32 finalize;
27};
Ard Biesheuvel2c988332014-03-06 16:23:33 +080028
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020029asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
30 int blocks);
Sami Tolvanend6da00d2017-08-10 09:39:53 -070031#ifdef CONFIG_CFI_CLANG
32static inline void __cfi_sha1_ce_transform(struct sha1_state *sst,
33 u8 const *src, int blocks)
34{
35 sha1_ce_transform((struct sha1_ce_state *)sst, src, blocks);
36}
37#define sha1_ce_transform __cfi_sha1_ce_transform
38#endif
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020039
Ard Biesheuvele2b2ca92017-04-26 17:11:32 +010040const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
41const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
42
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020043static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
44 unsigned int len)
Ard Biesheuvel2c988332014-03-06 16:23:33 +080045{
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020046 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080047
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020048 sctx->finalize = 0;
Ard Biesheuvel2c988332014-03-06 16:23:33 +080049 kernel_neon_begin_partial(16);
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020050 sha1_base_do_update(desc, data, len,
51 (sha1_block_fn *)sha1_ce_transform);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080052 kernel_neon_end();
53
Ard Biesheuvel2c988332014-03-06 16:23:33 +080054 return 0;
55}
56
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020057static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
58 unsigned int len, u8 *out)
Ard Biesheuvel2c988332014-03-06 16:23:33 +080059{
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020060 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
61 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080062
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020063 /*
64 * Allow the asm code to perform the finalization if there is no
65 * partial data and the input is a round multiple of the block size.
66 */
67 sctx->finalize = finalize;
68
69 kernel_neon_begin_partial(16);
70 sha1_base_do_update(desc, data, len,
71 (sha1_block_fn *)sha1_ce_transform);
72 if (!finalize)
73 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
74 kernel_neon_end();
75 return sha1_base_finish(desc, out);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080076}
77
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020078static int sha1_ce_final(struct shash_desc *desc, u8 *out)
Ard Biesheuvel2c988332014-03-06 16:23:33 +080079{
Ard Biesheuvelbf7883e2015-05-06 15:54:31 +020080 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
81
82 sctx->finalize = 0;
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020083 kernel_neon_begin_partial(16);
84 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
85 kernel_neon_end();
86 return sha1_base_finish(desc, out);
Ard Biesheuvel2c988332014-03-06 16:23:33 +080087}
88
89static struct shash_alg alg = {
Ard Biesheuvel07eb54d2015-04-09 12:55:44 +020090 .init = sha1_base_init,
91 .update = sha1_ce_update,
92 .final = sha1_ce_final,
93 .finup = sha1_ce_finup,
94 .descsize = sizeof(struct sha1_ce_state),
Ard Biesheuvel2c988332014-03-06 16:23:33 +080095 .digestsize = SHA1_DIGEST_SIZE,
Ard Biesheuvel2c988332014-03-06 16:23:33 +080096 .base = {
97 .cra_name = "sha1",
98 .cra_driver_name = "sha1-ce",
99 .cra_priority = 200,
100 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
101 .cra_blocksize = SHA1_BLOCK_SIZE,
102 .cra_module = THIS_MODULE,
103 }
104};
105
106static int __init sha1_ce_mod_init(void)
107{
108 return crypto_register_shash(&alg);
109}
110
111static void __exit sha1_ce_mod_fini(void)
112{
113 crypto_unregister_shash(&alg);
114}
115
116module_cpu_feature_match(SHA1, sha1_ce_mod_init);
117module_exit(sha1_ce_mod_fini);