blob: 356ca9397a8669fcc685cdfd7ee8b29f781b2794 [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);
31
Ard Biesheuvel0a953c02017-04-26 17:11:32 +010032const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state,
33 sst.count);
34const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state,
35 finalize);
36
Ard Biesheuvel03802f62015-04-09 12:55:45 +020037static int sha256_ce_update(struct shash_desc *desc, const u8 *data,
38 unsigned int len)
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010039{
Ard Biesheuvel03802f62015-04-09 12:55:45 +020040 struct sha256_ce_state *sctx = shash_desc_ctx(desc);
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010041
Ard Biesheuvel03802f62015-04-09 12:55:45 +020042 sctx->finalize = 0;
43 kernel_neon_begin_partial(28);
44 sha256_base_do_update(desc, data, len,
45 (sha256_block_fn *)sha2_ce_transform);
46 kernel_neon_end();
47
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010048 return 0;
49}
50
Ard Biesheuvel03802f62015-04-09 12:55:45 +020051static int sha256_ce_finup(struct shash_desc *desc, const u8 *data,
52 unsigned int len, u8 *out)
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010053{
Ard Biesheuvel03802f62015-04-09 12:55:45 +020054 struct sha256_ce_state *sctx = shash_desc_ctx(desc);
Elena Petrova86d0b192019-05-28 15:35:06 +010055 bool finalize = !sctx->sst.count && !(len % SHA256_BLOCK_SIZE) && len;
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010056
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010057 /*
Ard Biesheuvel03802f62015-04-09 12:55:45 +020058 * Allow the asm code to perform the finalization if there is no
59 * partial data and the input is a round multiple of the block size.
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010060 */
Ard Biesheuvel03802f62015-04-09 12:55:45 +020061 sctx->finalize = finalize;
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010062
63 kernel_neon_begin_partial(28);
Ard Biesheuvel03802f62015-04-09 12:55:45 +020064 sha256_base_do_update(desc, data, len,
65 (sha256_block_fn *)sha2_ce_transform);
66 if (!finalize)
67 sha256_base_do_finalize(desc,
68 (sha256_block_fn *)sha2_ce_transform);
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010069 kernel_neon_end();
Ard Biesheuvel03802f62015-04-09 12:55:45 +020070 return sha256_base_finish(desc, out);
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010071}
72
Ard Biesheuvel03802f62015-04-09 12:55:45 +020073static int sha256_ce_final(struct shash_desc *desc, u8 *out)
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010074{
Ard Biesheuvelec59a652015-05-06 15:54:32 +020075 struct sha256_ce_state *sctx = shash_desc_ctx(desc);
76
77 sctx->finalize = 0;
Ard Biesheuvel03802f62015-04-09 12:55:45 +020078 kernel_neon_begin_partial(28);
79 sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
80 kernel_neon_end();
81 return sha256_base_finish(desc, out);
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010082}
83
84static struct shash_alg algs[] = { {
Ard Biesheuvel03802f62015-04-09 12:55:45 +020085 .init = sha224_base_init,
86 .update = sha256_ce_update,
87 .final = sha256_ce_final,
88 .finup = sha256_ce_finup,
89 .descsize = sizeof(struct sha256_ce_state),
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010090 .digestsize = SHA224_DIGEST_SIZE,
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +010091 .base = {
92 .cra_name = "sha224",
93 .cra_driver_name = "sha224-ce",
94 .cra_priority = 200,
95 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
96 .cra_blocksize = SHA256_BLOCK_SIZE,
97 .cra_module = THIS_MODULE,
98 }
99}, {
Ard Biesheuvel03802f62015-04-09 12:55:45 +0200100 .init = sha256_base_init,
101 .update = sha256_ce_update,
102 .final = sha256_ce_final,
103 .finup = sha256_ce_finup,
104 .descsize = sizeof(struct sha256_ce_state),
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +0100105 .digestsize = SHA256_DIGEST_SIZE,
Ard Biesheuvel6ba6c742014-03-20 15:35:40 +0100106 .base = {
107 .cra_name = "sha256",
108 .cra_driver_name = "sha256-ce",
109 .cra_priority = 200,
110 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
111 .cra_blocksize = SHA256_BLOCK_SIZE,
112 .cra_module = THIS_MODULE,
113 }
114} };
115
116static int __init sha2_ce_mod_init(void)
117{
118 return crypto_register_shashes(algs, ARRAY_SIZE(algs));
119}
120
121static void __exit sha2_ce_mod_fini(void)
122{
123 crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
124}
125
126module_cpu_feature_match(SHA2, sha2_ce_mod_init);
127module_exit(sha2_ce_mod_fini);