xref: /linux/arch/s390/crypto/sha512_s390.c (revision a3b5b88ea9bc9da00bd89d94738bf7181b2516f7)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Cryptographic API.
4  *
5  * s390 implementation of the SHA512 and SHA38 Secure Hash Algorithm.
6  *
7  * Copyright IBM Corp. 2007
8  * Author(s): Jan Glauber (jang@de.ibm.com)
9  */
10 #include <asm/cpacf.h>
11 #include <crypto/internal/hash.h>
12 #include <crypto/sha2.h>
13 #include <linux/cpufeature.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 
18 #include "sha.h"
19 
sha512_init(struct shash_desc * desc)20 static int sha512_init(struct shash_desc *desc)
21 {
22 	struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
23 
24 	ctx->sha512.state[0] = SHA512_H0;
25 	ctx->sha512.state[1] = SHA512_H1;
26 	ctx->sha512.state[2] = SHA512_H2;
27 	ctx->sha512.state[3] = SHA512_H3;
28 	ctx->sha512.state[4] = SHA512_H4;
29 	ctx->sha512.state[5] = SHA512_H5;
30 	ctx->sha512.state[6] = SHA512_H6;
31 	ctx->sha512.state[7] = SHA512_H7;
32 	ctx->count = 0;
33 	ctx->sha512.count_hi = 0;
34 	ctx->func = CPACF_KIMD_SHA_512;
35 	ctx->first_message_part = 0;
36 
37 	return 0;
38 }
39 
sha512_export(struct shash_desc * desc,void * out)40 static int sha512_export(struct shash_desc *desc, void *out)
41 {
42 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
43 	struct sha512_state *octx = out;
44 
45 	octx->count[0] = sctx->count;
46 	octx->count[1] = sctx->sha512.count_hi;
47 	memcpy(octx->state, sctx->state, sizeof(octx->state));
48 	return 0;
49 }
50 
sha512_import(struct shash_desc * desc,const void * in)51 static int sha512_import(struct shash_desc *desc, const void *in)
52 {
53 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
54 	const struct sha512_state *ictx = in;
55 
56 	sctx->count = ictx->count[0];
57 	sctx->sha512.count_hi = ictx->count[1];
58 
59 	memcpy(sctx->state, ictx->state, sizeof(ictx->state));
60 	sctx->func = CPACF_KIMD_SHA_512;
61 	sctx->first_message_part = 0;
62 	return 0;
63 }
64 
65 static struct shash_alg sha512_alg = {
66 	.digestsize	=	SHA512_DIGEST_SIZE,
67 	.init		=	sha512_init,
68 	.update		=	s390_sha_update_blocks,
69 	.finup		=	s390_sha_finup,
70 	.export		=	sha512_export,
71 	.import		=	sha512_import,
72 	.descsize	=	sizeof(struct s390_sha_ctx),
73 	.statesize	=	SHA512_STATE_SIZE,
74 	.base		=	{
75 		.cra_name	=	"sha512",
76 		.cra_driver_name=	"sha512-s390",
77 		.cra_priority	=	300,
78 		.cra_flags	=	CRYPTO_AHASH_ALG_BLOCK_ONLY |
79 					CRYPTO_AHASH_ALG_FINUP_MAX,
80 		.cra_blocksize	=	SHA512_BLOCK_SIZE,
81 		.cra_module	=	THIS_MODULE,
82 	}
83 };
84 
85 MODULE_ALIAS_CRYPTO("sha512");
86 
sha384_init(struct shash_desc * desc)87 static int sha384_init(struct shash_desc *desc)
88 {
89 	struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
90 
91 	ctx->sha512.state[0] = SHA384_H0;
92 	ctx->sha512.state[1] = SHA384_H1;
93 	ctx->sha512.state[2] = SHA384_H2;
94 	ctx->sha512.state[3] = SHA384_H3;
95 	ctx->sha512.state[4] = SHA384_H4;
96 	ctx->sha512.state[5] = SHA384_H5;
97 	ctx->sha512.state[6] = SHA384_H6;
98 	ctx->sha512.state[7] = SHA384_H7;
99 	ctx->count = 0;
100 	ctx->sha512.count_hi = 0;
101 	ctx->func = CPACF_KIMD_SHA_512;
102 	ctx->first_message_part = 0;
103 
104 	return 0;
105 }
106 
107 static struct shash_alg sha384_alg = {
108 	.digestsize	=	SHA384_DIGEST_SIZE,
109 	.init		=	sha384_init,
110 	.update		=	s390_sha_update_blocks,
111 	.finup		=	s390_sha_finup,
112 	.export		=	sha512_export,
113 	.import		=	sha512_import,
114 	.descsize	=	sizeof(struct s390_sha_ctx),
115 	.statesize	=	SHA512_STATE_SIZE,
116 	.base		=	{
117 		.cra_name	=	"sha384",
118 		.cra_driver_name=	"sha384-s390",
119 		.cra_priority	=	300,
120 		.cra_blocksize	=	SHA384_BLOCK_SIZE,
121 		.cra_flags	=	CRYPTO_AHASH_ALG_BLOCK_ONLY |
122 					CRYPTO_AHASH_ALG_FINUP_MAX,
123 		.cra_ctxsize	=	sizeof(struct s390_sha_ctx),
124 		.cra_module	=	THIS_MODULE,
125 	}
126 };
127 
128 MODULE_ALIAS_CRYPTO("sha384");
129 
init(void)130 static int __init init(void)
131 {
132 	int ret;
133 
134 	if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_512))
135 		return -ENODEV;
136 	if ((ret = crypto_register_shash(&sha512_alg)) < 0)
137 		goto out;
138 	if ((ret = crypto_register_shash(&sha384_alg)) < 0)
139 		crypto_unregister_shash(&sha512_alg);
140 out:
141 	return ret;
142 }
143 
fini(void)144 static void __exit fini(void)
145 {
146 	crypto_unregister_shash(&sha512_alg);
147 	crypto_unregister_shash(&sha384_alg);
148 }
149 
150 module_cpu_feature_match(S390_CPU_FEATURE_MSA, init);
151 module_exit(fini);
152 
153 MODULE_LICENSE("GPL");
154 MODULE_DESCRIPTION("SHA512 and SHA-384 Secure Hash Algorithm");
155