• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <crypto/internal/hash.h>
11 #include <crypto/sha2.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/cpufeature.h>
17 #include <asm/cpacf.h>
18 
19 #include "sha.h"
20 
sha512_init(struct shash_desc * desc)21 static int sha512_init(struct shash_desc *desc)
22 {
23 	struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
24 
25 	*(__u64 *)&ctx->state[0] = SHA512_H0;
26 	*(__u64 *)&ctx->state[2] = SHA512_H1;
27 	*(__u64 *)&ctx->state[4] = SHA512_H2;
28 	*(__u64 *)&ctx->state[6] = SHA512_H3;
29 	*(__u64 *)&ctx->state[8] = SHA512_H4;
30 	*(__u64 *)&ctx->state[10] = SHA512_H5;
31 	*(__u64 *)&ctx->state[12] = SHA512_H6;
32 	*(__u64 *)&ctx->state[14] = SHA512_H7;
33 	ctx->count = 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] = 0;
47 	memcpy(octx->state, sctx->state, sizeof(octx->state));
48 	memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
49 	return 0;
50 }
51 
sha512_import(struct shash_desc * desc,const void * in)52 static int sha512_import(struct shash_desc *desc, const void *in)
53 {
54 	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
55 	const struct sha512_state *ictx = in;
56 
57 	if (unlikely(ictx->count[1]))
58 		return -ERANGE;
59 	sctx->count = ictx->count[0];
60 
61 	memcpy(sctx->state, ictx->state, sizeof(ictx->state));
62 	memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
63 	sctx->func = CPACF_KIMD_SHA_512;
64 	sctx->first_message_part = 0;
65 	return 0;
66 }
67 
68 static struct shash_alg sha512_alg = {
69 	.digestsize	=	SHA512_DIGEST_SIZE,
70 	.init		=	sha512_init,
71 	.update		=	s390_sha_update,
72 	.final		=	s390_sha_final,
73 	.export		=	sha512_export,
74 	.import		=	sha512_import,
75 	.descsize	=	sizeof(struct s390_sha_ctx),
76 	.statesize	=	sizeof(struct sha512_state),
77 	.base		=	{
78 		.cra_name	=	"sha512",
79 		.cra_driver_name=	"sha512-s390",
80 		.cra_priority	=	300,
81 		.cra_blocksize	=	SHA512_BLOCK_SIZE,
82 		.cra_module	=	THIS_MODULE,
83 	}
84 };
85 
86 MODULE_ALIAS_CRYPTO("sha512");
87 
sha384_init(struct shash_desc * desc)88 static int sha384_init(struct shash_desc *desc)
89 {
90 	struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
91 
92 	*(__u64 *)&ctx->state[0] = SHA384_H0;
93 	*(__u64 *)&ctx->state[2] = SHA384_H1;
94 	*(__u64 *)&ctx->state[4] = SHA384_H2;
95 	*(__u64 *)&ctx->state[6] = SHA384_H3;
96 	*(__u64 *)&ctx->state[8] = SHA384_H4;
97 	*(__u64 *)&ctx->state[10] = SHA384_H5;
98 	*(__u64 *)&ctx->state[12] = SHA384_H6;
99 	*(__u64 *)&ctx->state[14] = SHA384_H7;
100 	ctx->count = 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,
111 	.final		=	s390_sha_final,
112 	.export		=	sha512_export,
113 	.import		=	sha512_import,
114 	.descsize	=	sizeof(struct s390_sha_ctx),
115 	.statesize	=	sizeof(struct sha512_state),
116 	.base		=	{
117 		.cra_name	=	"sha384",
118 		.cra_driver_name=	"sha384-s390",
119 		.cra_priority	=	300,
120 		.cra_blocksize	=	SHA384_BLOCK_SIZE,
121 		.cra_ctxsize	=	sizeof(struct s390_sha_ctx),
122 		.cra_module	=	THIS_MODULE,
123 	}
124 };
125 
126 MODULE_ALIAS_CRYPTO("sha384");
127 
init(void)128 static int __init init(void)
129 {
130 	int ret;
131 
132 	if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_512))
133 		return -ENODEV;
134 	if ((ret = crypto_register_shash(&sha512_alg)) < 0)
135 		goto out;
136 	if ((ret = crypto_register_shash(&sha384_alg)) < 0)
137 		crypto_unregister_shash(&sha512_alg);
138 out:
139 	return ret;
140 }
141 
fini(void)142 static void __exit fini(void)
143 {
144 	crypto_unregister_shash(&sha512_alg);
145 	crypto_unregister_shash(&sha384_alg);
146 }
147 
148 module_cpu_feature_match(S390_CPU_FEATURE_MSA, init);
149 module_exit(fini);
150 
151 MODULE_LICENSE("GPL");
152 MODULE_DESCRIPTION("SHA512 and SHA-384 Secure Hash Algorithm");
153