blob: 42d96ddd35c317311da3c87044a8c8f1ac5898c1 [file] [log] [blame]
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +01001/*
2 * sha2-ce-glue.c - SHA-224/SHA-256 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 Biesheuvel03802f62015-04-09 12:55:45 +020015#include <crypto/sha256_base.h>
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010016#include <linux/cpufeature.h>
17#include <linux/crypto.h>
18#include <linux/module.h>
19
20MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
21MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
22MODULE_LICENSE("GPL v2");
23
Ard Biesheuvel03802f62015-04-09 12:55:45 +020024struct sha256_ce_state {
25 struct sha256_state sst;
26 u32 finalize;
27};
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010028
Ard Biesheuvel03802f62015-04-09 12:55:45 +020029asmlinkage void sha2_ce_transform(struct sha256_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_sha2_ce_transform(struct sha256_state *sst,
33 u8 const *src, int blocks)
34{
35 sha2_ce_transform((struct sha256_ce_state *)sst, src, blocks);
36}
37#define sha2_ce_transform __cfi_sha2_ce_transform
38#endif
Ard Biesheuvel03802f62015-04-09 12:55:45 +020039
Ard Biesheuvele2b2ca92017-04-26 17:11:32 +010040const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state,
41 sst.count);
42const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state,
43 finalize);
44
Ard Biesheuvel03802f62015-04-09 12:55:45 +020045static int sha256_ce_update(struct shash_desc *desc, const u8 *data,
46 unsigned int len)
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010047{
Ard Biesheuvel03802f62015-04-09 12:55:45 +020048 struct sha256_ce_state *sctx = shash_desc_ctx(desc);
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010049
Ard Biesheuvel03802f62015-04-09 12:55:45 +020050 sctx->finalize = 0;
51 kernel_neon_begin_partial(28);
52 sha256_base_do_update(desc, data, len,
53 (sha256_block_fn *)sha2_ce_transform);
54 kernel_neon_end();
55
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010056 return 0;
57}
58
Ard Biesheuvel03802f62015-04-09 12:55:45 +020059static int sha256_ce_finup(struct shash_desc *desc, const u8 *data,
60 unsigned int len, u8 *out)
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010061{
Ard Biesheuvel03802f62015-04-09 12:55:45 +020062 struct sha256_ce_state *sctx = shash_desc_ctx(desc);
Elena Petrova86d0b192019-05-28 15:35:06 +010063 bool finalize = !sctx->sst.count && !(len % SHA256_BLOCK_SIZE) && len;
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010064
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010065 /*
Ard Biesheuvel03802f62015-04-09 12:55:45 +020066 * Allow the asm code to perform the finalization if there is no
67 * partial data and the input is a round multiple of the block size.
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010068 */
Ard Biesheuvel03802f62015-04-09 12:55:45 +020069 sctx->finalize = finalize;
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010070
71 kernel_neon_begin_partial(28);
Ard Biesheuvel03802f62015-04-09 12:55:45 +020072 sha256_base_do_update(desc, data, len,
73 (sha256_block_fn *)sha2_ce_transform);
74 if (!finalize)
75 sha256_base_do_finalize(desc,
76 (sha256_block_fn *)sha2_ce_transform);
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010077 kernel_neon_end();
Ard Biesheuvel03802f62015-04-09 12:55:45 +020078 return sha256_base_finish(desc, out);
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010079}
80
Ard Biesheuvel03802f62015-04-09 12:55:45 +020081static int sha256_ce_final(struct shash_desc *desc, u8 *out)
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010082{
Ard Biesheuvelec59a652015-05-06 15:54:32 +020083 struct sha256_ce_state *sctx = shash_desc_ctx(desc);
84
85 sctx->finalize = 0;
Ard Biesheuvel03802f62015-04-09 12:55:45 +020086 kernel_neon_begin_partial(28);
87 sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
88 kernel_neon_end();
89 return sha256_base_finish(desc, out);
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010090}
91
92static struct shash_alg algs[] = { {
Ard Biesheuvel03802f62015-04-09 12:55:45 +020093 .init = sha224_base_init,
94 .update = sha256_ce_update,
95 .final = sha256_ce_final,
96 .finup = sha256_ce_finup,
97 .descsize = sizeof(struct sha256_ce_state),
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010098 .digestsize = SHA224_DIGEST_SIZE,
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010099 .base = {
100 .cra_name = "sha224",
101 .cra_driver_name = "sha224-ce",
102 .cra_priority = 200,
103 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
104 .cra_blocksize = SHA256_BLOCK_SIZE,
105 .cra_module = THIS_MODULE,
106 }
107}, {
Ard Biesheuvel03802f62015-04-09 12:55:45 +0200108 .init = sha256_base_init,
109 .update = sha256_ce_update,
110 .final = sha256_ce_final,
111 .finup = sha256_ce_finup,
112 .descsize = sizeof(struct sha256_ce_state),
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +0100113 .digestsize = SHA256_DIGEST_SIZE,
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +0100114 .base = {
115 .cra_name = "sha256",
116 .cra_driver_name = "sha256-ce",
117 .cra_priority = 200,
118 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
119 .cra_blocksize = SHA256_BLOCK_SIZE,
120 .cra_module = THIS_MODULE,
121 }
122} };
123
124static int __init sha2_ce_mod_init(void)
125{
126 return crypto_register_shashes(algs, ARRAY_SIZE(algs));
127}
128
129static void __exit sha2_ce_mod_fini(void)
130{
131 crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
132}
133
134module_cpu_feature_match(SHA2, sha2_ce_mod_init);
135module_exit(sha2_ce_mod_fini);