1 // SPDX-License-Identifier: GPL-2.0-only
3 * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
5 * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
10 #include <linux/unaligned.h>
11 #include <crypto/internal/hash.h>
12 #include <crypto/internal/simd.h>
13 #include <crypto/sha2.h>
14 #include <crypto/sha256_base.h>
15 #include <linux/cpufeature.h>
16 #include <linux/crypto.h>
17 #include <linux/module.h>
19 MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
20 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
21 MODULE_LICENSE("GPL v2");
22 MODULE_ALIAS_CRYPTO("sha224");
23 MODULE_ALIAS_CRYPTO("sha256");
25 struct sha256_ce_state
{
26 struct sha256_state sst
;
30 extern const u32 sha256_ce_offsetof_count
;
31 extern const u32 sha256_ce_offsetof_finalize
;
33 asmlinkage
int __sha256_ce_transform(struct sha256_ce_state
*sst
, u8
const *src
,
36 static void sha256_ce_transform(struct sha256_state
*sst
, u8
const *src
,
43 rem
= __sha256_ce_transform(container_of(sst
,
44 struct sha256_ce_state
,
47 src
+= (blocks
- rem
) * SHA256_BLOCK_SIZE
;
52 const u32 sha256_ce_offsetof_count
= offsetof(struct sha256_ce_state
,
54 const u32 sha256_ce_offsetof_finalize
= offsetof(struct sha256_ce_state
,
57 asmlinkage
void sha256_block_data_order(u32
*digest
, u8
const *src
, int blocks
);
59 static void sha256_arm64_transform(struct sha256_state
*sst
, u8
const *src
,
62 sha256_block_data_order(sst
->state
, src
, blocks
);
65 static int sha256_ce_update(struct shash_desc
*desc
, const u8
*data
,
68 struct sha256_ce_state
*sctx
= shash_desc_ctx(desc
);
70 if (!crypto_simd_usable())
71 return sha256_base_do_update(desc
, data
, len
,
72 sha256_arm64_transform
);
75 sha256_base_do_update(desc
, data
, len
, sha256_ce_transform
);
80 static int sha256_ce_finup(struct shash_desc
*desc
, const u8
*data
,
81 unsigned int len
, u8
*out
)
83 struct sha256_ce_state
*sctx
= shash_desc_ctx(desc
);
84 bool finalize
= !sctx
->sst
.count
&& !(len
% SHA256_BLOCK_SIZE
) && len
;
86 if (!crypto_simd_usable()) {
88 sha256_base_do_update(desc
, data
, len
,
89 sha256_arm64_transform
);
90 sha256_base_do_finalize(desc
, sha256_arm64_transform
);
91 return sha256_base_finish(desc
, out
);
95 * Allow the asm code to perform the finalization if there is no
96 * partial data and the input is a round multiple of the block size.
98 sctx
->finalize
= finalize
;
100 sha256_base_do_update(desc
, data
, len
, sha256_ce_transform
);
102 sha256_base_do_finalize(desc
, sha256_ce_transform
);
103 return sha256_base_finish(desc
, out
);
106 static int sha256_ce_final(struct shash_desc
*desc
, u8
*out
)
108 struct sha256_ce_state
*sctx
= shash_desc_ctx(desc
);
110 if (!crypto_simd_usable()) {
111 sha256_base_do_finalize(desc
, sha256_arm64_transform
);
112 return sha256_base_finish(desc
, out
);
116 sha256_base_do_finalize(desc
, sha256_ce_transform
);
117 return sha256_base_finish(desc
, out
);
120 static int sha256_ce_digest(struct shash_desc
*desc
, const u8
*data
,
121 unsigned int len
, u8
*out
)
123 sha256_base_init(desc
);
124 return sha256_ce_finup(desc
, data
, len
, out
);
127 static int sha256_ce_export(struct shash_desc
*desc
, void *out
)
129 struct sha256_ce_state
*sctx
= shash_desc_ctx(desc
);
131 memcpy(out
, &sctx
->sst
, sizeof(struct sha256_state
));
135 static int sha256_ce_import(struct shash_desc
*desc
, const void *in
)
137 struct sha256_ce_state
*sctx
= shash_desc_ctx(desc
);
139 memcpy(&sctx
->sst
, in
, sizeof(struct sha256_state
));
144 static struct shash_alg algs
[] = { {
145 .init
= sha224_base_init
,
146 .update
= sha256_ce_update
,
147 .final
= sha256_ce_final
,
148 .finup
= sha256_ce_finup
,
149 .export
= sha256_ce_export
,
150 .import
= sha256_ce_import
,
151 .descsize
= sizeof(struct sha256_ce_state
),
152 .statesize
= sizeof(struct sha256_state
),
153 .digestsize
= SHA224_DIGEST_SIZE
,
155 .cra_name
= "sha224",
156 .cra_driver_name
= "sha224-ce",
158 .cra_blocksize
= SHA256_BLOCK_SIZE
,
159 .cra_module
= THIS_MODULE
,
162 .init
= sha256_base_init
,
163 .update
= sha256_ce_update
,
164 .final
= sha256_ce_final
,
165 .finup
= sha256_ce_finup
,
166 .digest
= sha256_ce_digest
,
167 .export
= sha256_ce_export
,
168 .import
= sha256_ce_import
,
169 .descsize
= sizeof(struct sha256_ce_state
),
170 .statesize
= sizeof(struct sha256_state
),
171 .digestsize
= SHA256_DIGEST_SIZE
,
173 .cra_name
= "sha256",
174 .cra_driver_name
= "sha256-ce",
176 .cra_blocksize
= SHA256_BLOCK_SIZE
,
177 .cra_module
= THIS_MODULE
,
181 static int __init
sha2_ce_mod_init(void)
183 return crypto_register_shashes(algs
, ARRAY_SIZE(algs
));
186 static void __exit
sha2_ce_mod_fini(void)
188 crypto_unregister_shashes(algs
, ARRAY_SIZE(algs
));
191 module_cpu_feature_match(SHA2
, sha2_ce_mod_init
);
192 module_exit(sha2_ce_mod_fini
);