xref: /openbmc/linux/arch/arm64/crypto/sha2-ce-glue.c (revision 0c94efab)
1 /*
2  * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
3  *
4  * Copyright (C) 2014 - 2017 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/simd.h>
13 #include <asm/unaligned.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/internal/simd.h>
16 #include <crypto/sha.h>
17 #include <crypto/sha256_base.h>
18 #include <linux/cpufeature.h>
19 #include <linux/crypto.h>
20 #include <linux/module.h>
21 
22 MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
25 
26 struct sha256_ce_state {
27 	struct sha256_state	sst;
28 	u32			finalize;
29 };
30 
31 asmlinkage void sha2_ce_transform(struct sha256_ce_state *sst, u8 const *src,
32 				  int blocks);
33 
34 const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state,
35 					      sst.count);
36 const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state,
37 						 finalize);
38 
39 asmlinkage void sha256_block_data_order(u32 *digest, u8 const *src, int blocks);
40 
41 static int sha256_ce_update(struct shash_desc *desc, const u8 *data,
42 			    unsigned int len)
43 {
44 	struct sha256_ce_state *sctx = shash_desc_ctx(desc);
45 
46 	if (!crypto_simd_usable())
47 		return sha256_base_do_update(desc, data, len,
48 				(sha256_block_fn *)sha256_block_data_order);
49 
50 	sctx->finalize = 0;
51 	kernel_neon_begin();
52 	sha256_base_do_update(desc, data, len,
53 			      (sha256_block_fn *)sha2_ce_transform);
54 	kernel_neon_end();
55 
56 	return 0;
57 }
58 
59 static int sha256_ce_finup(struct shash_desc *desc, const u8 *data,
60 			   unsigned int len, u8 *out)
61 {
62 	struct sha256_ce_state *sctx = shash_desc_ctx(desc);
63 	bool finalize = !sctx->sst.count && !(len % SHA256_BLOCK_SIZE);
64 
65 	if (!crypto_simd_usable()) {
66 		if (len)
67 			sha256_base_do_update(desc, data, len,
68 				(sha256_block_fn *)sha256_block_data_order);
69 		sha256_base_do_finalize(desc,
70 				(sha256_block_fn *)sha256_block_data_order);
71 		return sha256_base_finish(desc, out);
72 	}
73 
74 	/*
75 	 * Allow the asm code to perform the finalization if there is no
76 	 * partial data and the input is a round multiple of the block size.
77 	 */
78 	sctx->finalize = finalize;
79 
80 	kernel_neon_begin();
81 	sha256_base_do_update(desc, data, len,
82 			      (sha256_block_fn *)sha2_ce_transform);
83 	if (!finalize)
84 		sha256_base_do_finalize(desc,
85 					(sha256_block_fn *)sha2_ce_transform);
86 	kernel_neon_end();
87 	return sha256_base_finish(desc, out);
88 }
89 
90 static int sha256_ce_final(struct shash_desc *desc, u8 *out)
91 {
92 	struct sha256_ce_state *sctx = shash_desc_ctx(desc);
93 
94 	if (!crypto_simd_usable()) {
95 		sha256_base_do_finalize(desc,
96 				(sha256_block_fn *)sha256_block_data_order);
97 		return sha256_base_finish(desc, out);
98 	}
99 
100 	sctx->finalize = 0;
101 	kernel_neon_begin();
102 	sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
103 	kernel_neon_end();
104 	return sha256_base_finish(desc, out);
105 }
106 
107 static struct shash_alg algs[] = { {
108 	.init			= sha224_base_init,
109 	.update			= sha256_ce_update,
110 	.final			= sha256_ce_final,
111 	.finup			= sha256_ce_finup,
112 	.descsize		= sizeof(struct sha256_ce_state),
113 	.digestsize		= SHA224_DIGEST_SIZE,
114 	.base			= {
115 		.cra_name		= "sha224",
116 		.cra_driver_name	= "sha224-ce",
117 		.cra_priority		= 200,
118 		.cra_blocksize		= SHA256_BLOCK_SIZE,
119 		.cra_module		= THIS_MODULE,
120 	}
121 }, {
122 	.init			= sha256_base_init,
123 	.update			= sha256_ce_update,
124 	.final			= sha256_ce_final,
125 	.finup			= sha256_ce_finup,
126 	.descsize		= sizeof(struct sha256_ce_state),
127 	.digestsize		= SHA256_DIGEST_SIZE,
128 	.base			= {
129 		.cra_name		= "sha256",
130 		.cra_driver_name	= "sha256-ce",
131 		.cra_priority		= 200,
132 		.cra_blocksize		= SHA256_BLOCK_SIZE,
133 		.cra_module		= THIS_MODULE,
134 	}
135 } };
136 
137 static int __init sha2_ce_mod_init(void)
138 {
139 	return crypto_register_shashes(algs, ARRAY_SIZE(algs));
140 }
141 
142 static void __exit sha2_ce_mod_fini(void)
143 {
144 	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
145 }
146 
147 module_cpu_feature_match(SHA2, sha2_ce_mod_init);
148 module_exit(sha2_ce_mod_fini);
149