xref: /openbmc/linux/arch/arm/crypto/sha2-ce-glue.c (revision 1a59d1b8)
1 /*
2  * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
3  *
4  * Copyright (C) 2015 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 <crypto/internal/hash.h>
12 #include <crypto/internal/simd.h>
13 #include <crypto/sha.h>
14 #include <crypto/sha256_base.h>
15 #include <linux/cpufeature.h>
16 #include <linux/crypto.h>
17 #include <linux/module.h>
18 
19 #include <asm/hwcap.h>
20 #include <asm/simd.h>
21 #include <asm/neon.h>
22 #include <asm/unaligned.h>
23 
24 #include "sha256_glue.h"
25 
26 MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
27 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
28 MODULE_LICENSE("GPL v2");
29 
30 asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src,
31 				  int blocks);
32 
33 static int sha2_ce_update(struct shash_desc *desc, const u8 *data,
34 			  unsigned int len)
35 {
36 	struct sha256_state *sctx = shash_desc_ctx(desc);
37 
38 	if (!crypto_simd_usable() ||
39 	    (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
40 		return crypto_sha256_arm_update(desc, data, len);
41 
42 	kernel_neon_begin();
43 	sha256_base_do_update(desc, data, len,
44 			      (sha256_block_fn *)sha2_ce_transform);
45 	kernel_neon_end();
46 
47 	return 0;
48 }
49 
50 static int sha2_ce_finup(struct shash_desc *desc, const u8 *data,
51 			 unsigned int len, u8 *out)
52 {
53 	if (!crypto_simd_usable())
54 		return crypto_sha256_arm_finup(desc, data, len, out);
55 
56 	kernel_neon_begin();
57 	if (len)
58 		sha256_base_do_update(desc, data, len,
59 				      (sha256_block_fn *)sha2_ce_transform);
60 	sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
61 	kernel_neon_end();
62 
63 	return sha256_base_finish(desc, out);
64 }
65 
66 static int sha2_ce_final(struct shash_desc *desc, u8 *out)
67 {
68 	return sha2_ce_finup(desc, NULL, 0, out);
69 }
70 
71 static struct shash_alg algs[] = { {
72 	.init			= sha224_base_init,
73 	.update			= sha2_ce_update,
74 	.final			= sha2_ce_final,
75 	.finup			= sha2_ce_finup,
76 	.descsize		= sizeof(struct sha256_state),
77 	.digestsize		= SHA224_DIGEST_SIZE,
78 	.base			= {
79 		.cra_name		= "sha224",
80 		.cra_driver_name	= "sha224-ce",
81 		.cra_priority		= 300,
82 		.cra_blocksize		= SHA256_BLOCK_SIZE,
83 		.cra_module		= THIS_MODULE,
84 	}
85 }, {
86 	.init			= sha256_base_init,
87 	.update			= sha2_ce_update,
88 	.final			= sha2_ce_final,
89 	.finup			= sha2_ce_finup,
90 	.descsize		= sizeof(struct sha256_state),
91 	.digestsize		= SHA256_DIGEST_SIZE,
92 	.base			= {
93 		.cra_name		= "sha256",
94 		.cra_driver_name	= "sha256-ce",
95 		.cra_priority		= 300,
96 		.cra_blocksize		= SHA256_BLOCK_SIZE,
97 		.cra_module		= THIS_MODULE,
98 	}
99 } };
100 
101 static int __init sha2_ce_mod_init(void)
102 {
103 	return crypto_register_shashes(algs, ARRAY_SIZE(algs));
104 }
105 
106 static void __exit sha2_ce_mod_fini(void)
107 {
108 	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
109 }
110 
111 module_cpu_feature_match(SHA2, sha2_ce_mod_init);
112 module_exit(sha2_ce_mod_fini);
113