xref: /openbmc/linux/arch/s390/crypto/sha256_s390.c (revision 2359ccdd)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Cryptographic API.
4  *
5  * s390 implementation of the SHA256 and SHA224 Secure Hash Algorithm.
6  *
7  * s390 Version:
8  *   Copyright IBM Corp. 2005, 2011
9  *   Author(s): Jan Glauber (jang@de.ibm.com)
10  */
11 #include <crypto/internal/hash.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/cpufeature.h>
15 #include <crypto/sha.h>
16 #include <asm/cpacf.h>
17 
18 #include "sha.h"
19 
20 static int sha256_init(struct shash_desc *desc)
21 {
22 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
23 
24 	sctx->state[0] = SHA256_H0;
25 	sctx->state[1] = SHA256_H1;
26 	sctx->state[2] = SHA256_H2;
27 	sctx->state[3] = SHA256_H3;
28 	sctx->state[4] = SHA256_H4;
29 	sctx->state[5] = SHA256_H5;
30 	sctx->state[6] = SHA256_H6;
31 	sctx->state[7] = SHA256_H7;
32 	sctx->count = 0;
33 	sctx->func = CPACF_KIMD_SHA_256;
34 
35 	return 0;
36 }
37 
38 static int sha256_export(struct shash_desc *desc, void *out)
39 {
40 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
41 	struct sha256_state *octx = out;
42 
43 	octx->count = sctx->count;
44 	memcpy(octx->state, sctx->state, sizeof(octx->state));
45 	memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
46 	return 0;
47 }
48 
49 static int sha256_import(struct shash_desc *desc, const void *in)
50 {
51 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
52 	const struct sha256_state *ictx = in;
53 
54 	sctx->count = ictx->count;
55 	memcpy(sctx->state, ictx->state, sizeof(ictx->state));
56 	memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
57 	sctx->func = CPACF_KIMD_SHA_256;
58 	return 0;
59 }
60 
61 static struct shash_alg sha256_alg = {
62 	.digestsize	=	SHA256_DIGEST_SIZE,
63 	.init		=	sha256_init,
64 	.update		=	s390_sha_update,
65 	.final		=	s390_sha_final,
66 	.export		=	sha256_export,
67 	.import		=	sha256_import,
68 	.descsize	=	sizeof(struct s390_sha_ctx),
69 	.statesize	=	sizeof(struct sha256_state),
70 	.base		=	{
71 		.cra_name	=	"sha256",
72 		.cra_driver_name=	"sha256-s390",
73 		.cra_priority	=	300,
74 		.cra_flags	=	CRYPTO_ALG_TYPE_SHASH,
75 		.cra_blocksize	=	SHA256_BLOCK_SIZE,
76 		.cra_module	=	THIS_MODULE,
77 	}
78 };
79 
80 static int sha224_init(struct shash_desc *desc)
81 {
82 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
83 
84 	sctx->state[0] = SHA224_H0;
85 	sctx->state[1] = SHA224_H1;
86 	sctx->state[2] = SHA224_H2;
87 	sctx->state[3] = SHA224_H3;
88 	sctx->state[4] = SHA224_H4;
89 	sctx->state[5] = SHA224_H5;
90 	sctx->state[6] = SHA224_H6;
91 	sctx->state[7] = SHA224_H7;
92 	sctx->count = 0;
93 	sctx->func = CPACF_KIMD_SHA_256;
94 
95 	return 0;
96 }
97 
98 static struct shash_alg sha224_alg = {
99 	.digestsize	=	SHA224_DIGEST_SIZE,
100 	.init		=	sha224_init,
101 	.update		=	s390_sha_update,
102 	.final		=	s390_sha_final,
103 	.export		=	sha256_export,
104 	.import		=	sha256_import,
105 	.descsize	=	sizeof(struct s390_sha_ctx),
106 	.statesize	=	sizeof(struct sha256_state),
107 	.base		=	{
108 		.cra_name	=	"sha224",
109 		.cra_driver_name=	"sha224-s390",
110 		.cra_priority	=	300,
111 		.cra_flags	=	CRYPTO_ALG_TYPE_SHASH,
112 		.cra_blocksize	=	SHA224_BLOCK_SIZE,
113 		.cra_module	=	THIS_MODULE,
114 	}
115 };
116 
117 static int __init sha256_s390_init(void)
118 {
119 	int ret;
120 
121 	if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_256))
122 		return -EOPNOTSUPP;
123 	ret = crypto_register_shash(&sha256_alg);
124 	if (ret < 0)
125 		goto out;
126 	ret = crypto_register_shash(&sha224_alg);
127 	if (ret < 0)
128 		crypto_unregister_shash(&sha256_alg);
129 out:
130 	return ret;
131 }
132 
133 static void __exit sha256_s390_fini(void)
134 {
135 	crypto_unregister_shash(&sha224_alg);
136 	crypto_unregister_shash(&sha256_alg);
137 }
138 
139 module_cpu_feature_match(MSA, sha256_s390_init);
140 module_exit(sha256_s390_fini);
141 
142 MODULE_ALIAS_CRYPTO("sha256");
143 MODULE_ALIAS_CRYPTO("sha224");
144 MODULE_LICENSE("GPL");
145 MODULE_DESCRIPTION("SHA256 and SHA224 Secure Hash Algorithm");
146