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