1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
4  * using NEON instructions.
5  *
6  * Copyright © 2015 Google Inc.
7  *
8  * This file is based on sha512_neon_glue.c:
9  *   Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
10  */
11 
12 #include <crypto/internal/hash.h>
13 #include <crypto/internal/simd.h>
14 #include <linux/types.h>
15 #include <linux/string.h>
16 #include <crypto/sha2.h>
17 #include <crypto/sha256_base.h>
18 #include <asm/byteorder.h>
19 #include <asm/simd.h>
20 #include <asm/neon.h>
21 
22 #include "sha256_glue.h"
23 
24 asmlinkage void sha256_block_data_order_neon(u32 *digest, const void *data,
25 					     unsigned int num_blks);
26 
27 static int crypto_sha256_neon_update(struct shash_desc *desc, const u8 *data,
28 				     unsigned int len)
29 {
30 	struct sha256_state *sctx = shash_desc_ctx(desc);
31 
32 	if (!crypto_simd_usable() ||
33 	    (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
34 		return crypto_sha256_arm_update(desc, data, len);
35 
36 	kernel_neon_begin();
37 	sha256_base_do_update(desc, data, len,
38 			(sha256_block_fn *)sha256_block_data_order_neon);
39 	kernel_neon_end();
40 
41 	return 0;
42 }
43 
44 static int crypto_sha256_neon_finup(struct shash_desc *desc, const u8 *data,
45 				    unsigned int len, u8 *out)
46 {
47 	if (!crypto_simd_usable())
48 		return crypto_sha256_arm_finup(desc, data, len, out);
49 
50 	kernel_neon_begin();
51 	if (len)
52 		sha256_base_do_update(desc, data, len,
53 			(sha256_block_fn *)sha256_block_data_order_neon);
54 	sha256_base_do_finalize(desc,
55 			(sha256_block_fn *)sha256_block_data_order_neon);
56 	kernel_neon_end();
57 
58 	return sha256_base_finish(desc, out);
59 }
60 
61 static int crypto_sha256_neon_final(struct shash_desc *desc, u8 *out)
62 {
63 	return crypto_sha256_neon_finup(desc, NULL, 0, out);
64 }
65 
66 struct shash_alg sha256_neon_algs[] = { {
67 	.digestsize	=	SHA256_DIGEST_SIZE,
68 	.init		=	sha256_base_init,
69 	.update		=	crypto_sha256_neon_update,
70 	.final		=	crypto_sha256_neon_final,
71 	.finup		=	crypto_sha256_neon_finup,
72 	.descsize	=	sizeof(struct sha256_state),
73 	.base		=	{
74 		.cra_name	=	"sha256",
75 		.cra_driver_name =	"sha256-neon",
76 		.cra_priority	=	250,
77 		.cra_blocksize	=	SHA256_BLOCK_SIZE,
78 		.cra_module	=	THIS_MODULE,
79 	}
80 }, {
81 	.digestsize	=	SHA224_DIGEST_SIZE,
82 	.init		=	sha224_base_init,
83 	.update		=	crypto_sha256_neon_update,
84 	.final		=	crypto_sha256_neon_final,
85 	.finup		=	crypto_sha256_neon_finup,
86 	.descsize	=	sizeof(struct sha256_state),
87 	.base		=	{
88 		.cra_name	=	"sha224",
89 		.cra_driver_name =	"sha224-neon",
90 		.cra_priority	=	250,
91 		.cra_blocksize	=	SHA224_BLOCK_SIZE,
92 		.cra_module	=	THIS_MODULE,
93 	}
94 } };
95