xref: /openbmc/linux/arch/arm/crypto/sha256_glue.c (revision 217188d9)
1 /*
2  * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
3  * using optimized ARM assembler and NEON instructions.
4  *
5  * Copyright © 2015 Google Inc.
6  *
7  * This file is based on sha256_ssse3_glue.c:
8  *   Copyright (C) 2013 Intel Corporation
9  *   Author: Tim Chen <tim.c.chen@linux.intel.com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation; either version 2 of the License, or (at your option)
14  * any later version.
15  *
16  */
17 
18 #include <crypto/internal/hash.h>
19 #include <linux/crypto.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/mm.h>
23 #include <linux/cryptohash.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <crypto/sha.h>
27 #include <crypto/sha256_base.h>
28 #include <asm/simd.h>
29 #include <asm/neon.h>
30 
31 #include "sha256_glue.h"
32 
33 asmlinkage void sha256_block_data_order(u32 *digest, const void *data,
34 					unsigned int num_blks);
35 
36 int crypto_sha256_arm_update(struct shash_desc *desc, const u8 *data,
37 			     unsigned int len)
38 {
39 	/* make sure casting to sha256_block_fn() is safe */
40 	BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0);
41 
42 	return sha256_base_do_update(desc, data, len,
43 				(sha256_block_fn *)sha256_block_data_order);
44 }
45 EXPORT_SYMBOL(crypto_sha256_arm_update);
46 
47 static int sha256_final(struct shash_desc *desc, u8 *out)
48 {
49 	sha256_base_do_finalize(desc,
50 				(sha256_block_fn *)sha256_block_data_order);
51 	return sha256_base_finish(desc, out);
52 }
53 
54 int crypto_sha256_arm_finup(struct shash_desc *desc, const u8 *data,
55 			    unsigned int len, u8 *out)
56 {
57 	sha256_base_do_update(desc, data, len,
58 			      (sha256_block_fn *)sha256_block_data_order);
59 	return sha256_final(desc, out);
60 }
61 EXPORT_SYMBOL(crypto_sha256_arm_finup);
62 
63 static struct shash_alg algs[] = { {
64 	.digestsize	=	SHA256_DIGEST_SIZE,
65 	.init		=	sha256_base_init,
66 	.update		=	crypto_sha256_arm_update,
67 	.final		=	sha256_final,
68 	.finup		=	crypto_sha256_arm_finup,
69 	.descsize	=	sizeof(struct sha256_state),
70 	.base		=	{
71 		.cra_name	=	"sha256",
72 		.cra_driver_name =	"sha256-asm",
73 		.cra_priority	=	150,
74 		.cra_blocksize	=	SHA256_BLOCK_SIZE,
75 		.cra_module	=	THIS_MODULE,
76 	}
77 }, {
78 	.digestsize	=	SHA224_DIGEST_SIZE,
79 	.init		=	sha224_base_init,
80 	.update		=	crypto_sha256_arm_update,
81 	.final		=	sha256_final,
82 	.finup		=	crypto_sha256_arm_finup,
83 	.descsize	=	sizeof(struct sha256_state),
84 	.base		=	{
85 		.cra_name	=	"sha224",
86 		.cra_driver_name =	"sha224-asm",
87 		.cra_priority	=	150,
88 		.cra_blocksize	=	SHA224_BLOCK_SIZE,
89 		.cra_module	=	THIS_MODULE,
90 	}
91 } };
92 
93 static int __init sha256_mod_init(void)
94 {
95 	int res = crypto_register_shashes(algs, ARRAY_SIZE(algs));
96 
97 	if (res < 0)
98 		return res;
99 
100 	if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon()) {
101 		res = crypto_register_shashes(sha256_neon_algs,
102 					      ARRAY_SIZE(sha256_neon_algs));
103 
104 		if (res < 0)
105 			crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
106 	}
107 
108 	return res;
109 }
110 
111 static void __exit sha256_mod_fini(void)
112 {
113 	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
114 
115 	if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon())
116 		crypto_unregister_shashes(sha256_neon_algs,
117 					  ARRAY_SIZE(sha256_neon_algs));
118 }
119 
120 module_init(sha256_mod_init);
121 module_exit(sha256_mod_fini);
122 
123 MODULE_LICENSE("GPL");
124 MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm (ARM), including NEON");
125 
126 MODULE_ALIAS_CRYPTO("sha256");
127