1 /*
2  * sha512-neon-glue.c - accelerated SHA-384/512 for ARM NEON
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/sha.h>
13 #include <crypto/sha512_base.h>
14 #include <linux/crypto.h>
15 #include <linux/module.h>
16 
17 #include <asm/simd.h>
18 #include <asm/neon.h>
19 
20 #include "sha512.h"
21 
22 MODULE_ALIAS_CRYPTO("sha384-neon");
23 MODULE_ALIAS_CRYPTO("sha512-neon");
24 
25 asmlinkage void sha512_block_data_order_neon(u64 *state, u8 const *src,
26 					     int blocks);
27 
28 static int sha512_neon_update(struct shash_desc *desc, const u8 *data,
29 			      unsigned int len)
30 {
31 	struct sha512_state *sctx = shash_desc_ctx(desc);
32 
33 	if (!may_use_simd() ||
34 	    (sctx->count[0] % SHA512_BLOCK_SIZE) + len < SHA512_BLOCK_SIZE)
35 		return sha512_arm_update(desc, data, len);
36 
37 	kernel_neon_begin();
38 	sha512_base_do_update(desc, data, len,
39 		(sha512_block_fn *)sha512_block_data_order_neon);
40 	kernel_neon_end();
41 
42 	return 0;
43 }
44 
45 static int sha512_neon_finup(struct shash_desc *desc, const u8 *data,
46 			     unsigned int len, u8 *out)
47 {
48 	if (!may_use_simd())
49 		return sha512_arm_finup(desc, data, len, out);
50 
51 	kernel_neon_begin();
52 	if (len)
53 		sha512_base_do_update(desc, data, len,
54 			(sha512_block_fn *)sha512_block_data_order_neon);
55 	sha512_base_do_finalize(desc,
56 		(sha512_block_fn *)sha512_block_data_order_neon);
57 	kernel_neon_end();
58 
59 	return sha512_base_finish(desc, out);
60 }
61 
62 static int sha512_neon_final(struct shash_desc *desc, u8 *out)
63 {
64 	return sha512_neon_finup(desc, NULL, 0, out);
65 }
66 
67 struct shash_alg sha512_neon_algs[] = { {
68 	.init			= sha384_base_init,
69 	.update			= sha512_neon_update,
70 	.final			= sha512_neon_final,
71 	.finup			= sha512_neon_finup,
72 	.descsize		= sizeof(struct sha512_state),
73 	.digestsize		= SHA384_DIGEST_SIZE,
74 	.base			= {
75 		.cra_name		= "sha384",
76 		.cra_driver_name	= "sha384-neon",
77 		.cra_priority		= 300,
78 		.cra_blocksize		= SHA384_BLOCK_SIZE,
79 		.cra_module		= THIS_MODULE,
80 
81 	}
82 },  {
83 	.init			= sha512_base_init,
84 	.update			= sha512_neon_update,
85 	.final			= sha512_neon_final,
86 	.finup			= sha512_neon_finup,
87 	.descsize		= sizeof(struct sha512_state),
88 	.digestsize		= SHA512_DIGEST_SIZE,
89 	.base			= {
90 		.cra_name		= "sha512",
91 		.cra_driver_name	= "sha512-neon",
92 		.cra_priority		= 300,
93 		.cra_blocksize		= SHA512_BLOCK_SIZE,
94 		.cra_module		= THIS_MODULE,
95 	}
96 } };
97