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