xref: /linux/arch/s390/crypto/sha3_512_s390.c (revision fb7399cf2d0b33825b8039f95c45395c7deba25c)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Cryptographic API.
4  *
5  * s390 implementation of the SHA512 and SHA384 Secure Hash Algorithm.
6  *
7  * Copyright IBM Corp. 2019
8  * Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com)
9  */
10 #include <asm/cpacf.h>
11 #include <crypto/internal/hash.h>
12 #include <crypto/sha3.h>
13 #include <linux/cpufeature.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/string.h>
18 
19 #include "sha.h"
20 
21 static int sha3_512_init(struct shash_desc *desc)
22 {
23 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
24 
25 	sctx->first_message_part = test_facility(86);
26 	if (!sctx->first_message_part)
27 		memset(sctx->state, 0, sizeof(sctx->state));
28 	sctx->count = 0;
29 	sctx->func = CPACF_KIMD_SHA3_512;
30 
31 	return 0;
32 }
33 
34 static int sha3_512_export(struct shash_desc *desc, void *out)
35 {
36 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
37 	struct sha3_state *octx = out;
38 
39 
40 	if (sctx->first_message_part) {
41 		memset(sctx->state, 0, sizeof(sctx->state));
42 		sctx->first_message_part = 0;
43 	}
44 	memcpy(octx->st, sctx->state, sizeof(octx->st));
45 	return 0;
46 }
47 
48 static int sha3_512_import(struct shash_desc *desc, const void *in)
49 {
50 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
51 	const struct sha3_state *ictx = in;
52 
53 	sctx->count = 0;
54 	memcpy(sctx->state, ictx->st, sizeof(ictx->st));
55 	sctx->first_message_part = 0;
56 	sctx->func = CPACF_KIMD_SHA3_512;
57 
58 	return 0;
59 }
60 
61 static int sha3_384_import(struct shash_desc *desc, const void *in)
62 {
63 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
64 
65 	sha3_512_import(desc, in);
66 	sctx->func = CPACF_KIMD_SHA3_384;
67 	return 0;
68 }
69 
70 static struct shash_alg sha3_512_alg = {
71 	.digestsize	=	SHA3_512_DIGEST_SIZE,
72 	.init		=	sha3_512_init,
73 	.update		=	s390_sha_update_blocks,
74 	.finup		=	s390_sha_finup,
75 	.export		=	sha3_512_export,
76 	.import		=	sha3_512_import,
77 	.descsize	=	S390_SHA_CTX_SIZE,
78 	.statesize	=	SHA3_STATE_SIZE,
79 	.base		=	{
80 		.cra_name	 =	"sha3-512",
81 		.cra_driver_name =	"sha3-512-s390",
82 		.cra_priority	 =	300,
83 		.cra_flags	 =	CRYPTO_AHASH_ALG_BLOCK_ONLY,
84 		.cra_blocksize	 =	SHA3_512_BLOCK_SIZE,
85 		.cra_module	 =	THIS_MODULE,
86 	}
87 };
88 
89 MODULE_ALIAS_CRYPTO("sha3-512");
90 
91 static int sha3_384_init(struct shash_desc *desc)
92 {
93 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
94 
95 	sha3_512_init(desc);
96 	sctx->func = CPACF_KIMD_SHA3_384;
97 	return 0;
98 }
99 
100 static struct shash_alg sha3_384_alg = {
101 	.digestsize	=	SHA3_384_DIGEST_SIZE,
102 	.init		=	sha3_384_init,
103 	.update		=	s390_sha_update_blocks,
104 	.finup		=	s390_sha_finup,
105 	.export		=	sha3_512_export, /* same as for 512 */
106 	.import		=	sha3_384_import, /* function code different! */
107 	.descsize	=	S390_SHA_CTX_SIZE,
108 	.statesize	=	SHA3_STATE_SIZE,
109 	.base		=	{
110 		.cra_name	 =	"sha3-384",
111 		.cra_driver_name =	"sha3-384-s390",
112 		.cra_priority	 =	300,
113 		.cra_flags	 =	CRYPTO_AHASH_ALG_BLOCK_ONLY,
114 		.cra_blocksize	 =	SHA3_384_BLOCK_SIZE,
115 		.cra_ctxsize	 =	sizeof(struct s390_sha_ctx),
116 		.cra_module	 =	THIS_MODULE,
117 	}
118 };
119 
120 MODULE_ALIAS_CRYPTO("sha3-384");
121 
122 static int __init init(void)
123 {
124 	int ret;
125 
126 	if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA3_512))
127 		return -ENODEV;
128 	ret = crypto_register_shash(&sha3_512_alg);
129 	if (ret < 0)
130 		goto out;
131 	ret = crypto_register_shash(&sha3_384_alg);
132 	if (ret < 0)
133 		crypto_unregister_shash(&sha3_512_alg);
134 out:
135 	return ret;
136 }
137 
138 static void __exit fini(void)
139 {
140 	crypto_unregister_shash(&sha3_512_alg);
141 	crypto_unregister_shash(&sha3_384_alg);
142 }
143 
144 module_cpu_feature_match(S390_CPU_FEATURE_MSA, init);
145 module_exit(fini);
146 
147 MODULE_LICENSE("GPL");
148 MODULE_DESCRIPTION("SHA3-512 and SHA3-384 Secure Hash Algorithm");
149