1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Glue code for the SHA1 Secure Hash Algorithm assembler implementation using
4  * ARM NEON instructions.
5  *
6  * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
7  *
8  * This file is based on sha1_generic.c and sha1_ssse3_glue.c:
9  *  Copyright (c) Alan Smithee.
10  *  Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
11  *  Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
12  *  Copyright (c) Mathias Krause <minipli@googlemail.com>
13  *  Copyright (c) Chandramouli Narayanan <mouli@linux.intel.com>
14  */
15 
16 #include <crypto/internal/hash.h>
17 #include <crypto/internal/simd.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/mm.h>
21 #include <linux/types.h>
22 #include <crypto/sha1.h>
23 #include <crypto/sha1_base.h>
24 #include <asm/neon.h>
25 #include <asm/simd.h>
26 
27 #include "sha1.h"
28 
29 asmlinkage void sha1_transform_neon(void *state_h, const char *data,
30 				    unsigned int rounds);
31 
sha1_neon_update(struct shash_desc * desc,const u8 * data,unsigned int len)32 static int sha1_neon_update(struct shash_desc *desc, const u8 *data,
33 			  unsigned int len)
34 {
35 	struct sha1_state *sctx = shash_desc_ctx(desc);
36 
37 	if (!crypto_simd_usable() ||
38 	    (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE)
39 		return sha1_update_arm(desc, data, len);
40 
41 	kernel_neon_begin();
42 	sha1_base_do_update(desc, data, len,
43 			    (sha1_block_fn *)sha1_transform_neon);
44 	kernel_neon_end();
45 
46 	return 0;
47 }
48 
sha1_neon_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)49 static int sha1_neon_finup(struct shash_desc *desc, const u8 *data,
50 			   unsigned int len, u8 *out)
51 {
52 	if (!crypto_simd_usable())
53 		return sha1_finup_arm(desc, data, len, out);
54 
55 	kernel_neon_begin();
56 	if (len)
57 		sha1_base_do_update(desc, data, len,
58 				    (sha1_block_fn *)sha1_transform_neon);
59 	sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_transform_neon);
60 	kernel_neon_end();
61 
62 	return sha1_base_finish(desc, out);
63 }
64 
sha1_neon_final(struct shash_desc * desc,u8 * out)65 static int sha1_neon_final(struct shash_desc *desc, u8 *out)
66 {
67 	return sha1_neon_finup(desc, NULL, 0, out);
68 }
69 
70 static struct shash_alg alg = {
71 	.digestsize	=	SHA1_DIGEST_SIZE,
72 	.init		=	sha1_base_init,
73 	.update		=	sha1_neon_update,
74 	.final		=	sha1_neon_final,
75 	.finup		=	sha1_neon_finup,
76 	.descsize	=	sizeof(struct sha1_state),
77 	.base		=	{
78 		.cra_name		= "sha1",
79 		.cra_driver_name	= "sha1-neon",
80 		.cra_priority		= 250,
81 		.cra_blocksize		= SHA1_BLOCK_SIZE,
82 		.cra_module		= THIS_MODULE,
83 	}
84 };
85 
sha1_neon_mod_init(void)86 static int __init sha1_neon_mod_init(void)
87 {
88 	if (!cpu_has_neon())
89 		return -ENODEV;
90 
91 	return crypto_register_shash(&alg);
92 }
93 
sha1_neon_mod_fini(void)94 static void __exit sha1_neon_mod_fini(void)
95 {
96 	crypto_unregister_shash(&alg);
97 }
98 
99 module_init(sha1_neon_mod_init);
100 module_exit(sha1_neon_mod_fini);
101 
102 MODULE_LICENSE("GPL");
103 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, NEON accelerated");
104 MODULE_ALIAS_CRYPTO("sha1");
105