• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * sha1-ce-glue.c - SHA-1 secure hash 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/sha.h>
16 #include <crypto/sha1_base.h>
17 #include <linux/cpufeature.h>
18 #include <linux/crypto.h>
19 #include <linux/module.h>
20 
21 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
22 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
23 MODULE_LICENSE("GPL v2");
24 
25 struct sha1_ce_state {
26 	struct sha1_state	sst;
27 	u32			finalize;
28 };
29 
30 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
31 				  int blocks);
32 #ifdef CONFIG_CFI_CLANG
__cfi_sha1_ce_transform(struct sha1_state * sst,u8 const * src,int blocks)33 static inline void __cfi_sha1_ce_transform(struct sha1_state *sst,
34 					   u8 const *src, int blocks)
35 {
36 	sha1_ce_transform((struct sha1_ce_state *)sst, src, blocks);
37 }
38 #define sha1_ce_transform __cfi_sha1_ce_transform
39 #endif
40 
41 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
42 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
43 
sha1_ce_update(struct shash_desc * desc,const u8 * data,unsigned int len)44 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
45 			  unsigned int len)
46 {
47 	struct sha1_ce_state *sctx = shash_desc_ctx(desc);
48 
49 	if (!may_use_simd())
50 		return crypto_sha1_update(desc, data, len);
51 
52 	sctx->finalize = 0;
53 	kernel_neon_begin();
54 	sha1_base_do_update(desc, data, len,
55 			    (sha1_block_fn *)sha1_ce_transform);
56 	kernel_neon_end();
57 
58 	return 0;
59 }
60 
sha1_ce_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)61 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
62 			 unsigned int len, u8 *out)
63 {
64 	struct sha1_ce_state *sctx = shash_desc_ctx(desc);
65 	bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
66 
67 	if (!may_use_simd())
68 		return crypto_sha1_finup(desc, data, len, out);
69 
70 	/*
71 	 * Allow the asm code to perform the finalization if there is no
72 	 * partial data and the input is a round multiple of the block size.
73 	 */
74 	sctx->finalize = finalize;
75 
76 	kernel_neon_begin();
77 	sha1_base_do_update(desc, data, len,
78 			    (sha1_block_fn *)sha1_ce_transform);
79 	if (!finalize)
80 		sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
81 	kernel_neon_end();
82 	return sha1_base_finish(desc, out);
83 }
84 
sha1_ce_final(struct shash_desc * desc,u8 * out)85 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
86 {
87 	struct sha1_ce_state *sctx = shash_desc_ctx(desc);
88 
89 	if (!may_use_simd())
90 		return crypto_sha1_finup(desc, NULL, 0, out);
91 
92 	sctx->finalize = 0;
93 	kernel_neon_begin();
94 	sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
95 	kernel_neon_end();
96 	return sha1_base_finish(desc, out);
97 }
98 
99 static struct shash_alg alg = {
100 	.init			= sha1_base_init,
101 	.update			= sha1_ce_update,
102 	.final			= sha1_ce_final,
103 	.finup			= sha1_ce_finup,
104 	.descsize		= sizeof(struct sha1_ce_state),
105 	.digestsize		= SHA1_DIGEST_SIZE,
106 	.base			= {
107 		.cra_name		= "sha1",
108 		.cra_driver_name	= "sha1-ce",
109 		.cra_priority		= 200,
110 		.cra_flags		= CRYPTO_ALG_TYPE_SHASH,
111 		.cra_blocksize		= SHA1_BLOCK_SIZE,
112 		.cra_module		= THIS_MODULE,
113 	}
114 };
115 
sha1_ce_mod_init(void)116 static int __init sha1_ce_mod_init(void)
117 {
118 	return crypto_register_shash(&alg);
119 }
120 
sha1_ce_mod_fini(void)121 static void __exit sha1_ce_mod_fini(void)
122 {
123 	crypto_unregister_shash(&alg);
124 }
125 
126 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
127 module_exit(sha1_ce_mod_fini);
128