18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cryptographic API.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * s390 implementation of the SHA256 and SHA224 Secure Hash Algorithm.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * s390 Version:
88c2ecf20Sopenharmony_ci *   Copyright IBM Corp. 2005, 2011
98c2ecf20Sopenharmony_ci *   Author(s): Jan Glauber (jang@de.ibm.com)
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/cpufeature.h>
158c2ecf20Sopenharmony_ci#include <crypto/sha.h>
168c2ecf20Sopenharmony_ci#include <asm/cpacf.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "sha.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic int s390_sha256_init(struct shash_desc *desc)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	sctx->state[0] = SHA256_H0;
258c2ecf20Sopenharmony_ci	sctx->state[1] = SHA256_H1;
268c2ecf20Sopenharmony_ci	sctx->state[2] = SHA256_H2;
278c2ecf20Sopenharmony_ci	sctx->state[3] = SHA256_H3;
288c2ecf20Sopenharmony_ci	sctx->state[4] = SHA256_H4;
298c2ecf20Sopenharmony_ci	sctx->state[5] = SHA256_H5;
308c2ecf20Sopenharmony_ci	sctx->state[6] = SHA256_H6;
318c2ecf20Sopenharmony_ci	sctx->state[7] = SHA256_H7;
328c2ecf20Sopenharmony_ci	sctx->count = 0;
338c2ecf20Sopenharmony_ci	sctx->func = CPACF_KIMD_SHA_256;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	return 0;
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int sha256_export(struct shash_desc *desc, void *out)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
418c2ecf20Sopenharmony_ci	struct sha256_state *octx = out;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	octx->count = sctx->count;
448c2ecf20Sopenharmony_ci	memcpy(octx->state, sctx->state, sizeof(octx->state));
458c2ecf20Sopenharmony_ci	memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
468c2ecf20Sopenharmony_ci	return 0;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int sha256_import(struct shash_desc *desc, const void *in)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
528c2ecf20Sopenharmony_ci	const struct sha256_state *ictx = in;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	sctx->count = ictx->count;
558c2ecf20Sopenharmony_ci	memcpy(sctx->state, ictx->state, sizeof(ictx->state));
568c2ecf20Sopenharmony_ci	memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
578c2ecf20Sopenharmony_ci	sctx->func = CPACF_KIMD_SHA_256;
588c2ecf20Sopenharmony_ci	return 0;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic struct shash_alg sha256_alg = {
628c2ecf20Sopenharmony_ci	.digestsize	=	SHA256_DIGEST_SIZE,
638c2ecf20Sopenharmony_ci	.init		=	s390_sha256_init,
648c2ecf20Sopenharmony_ci	.update		=	s390_sha_update,
658c2ecf20Sopenharmony_ci	.final		=	s390_sha_final,
668c2ecf20Sopenharmony_ci	.export		=	sha256_export,
678c2ecf20Sopenharmony_ci	.import		=	sha256_import,
688c2ecf20Sopenharmony_ci	.descsize	=	sizeof(struct s390_sha_ctx),
698c2ecf20Sopenharmony_ci	.statesize	=	sizeof(struct sha256_state),
708c2ecf20Sopenharmony_ci	.base		=	{
718c2ecf20Sopenharmony_ci		.cra_name	=	"sha256",
728c2ecf20Sopenharmony_ci		.cra_driver_name=	"sha256-s390",
738c2ecf20Sopenharmony_ci		.cra_priority	=	300,
748c2ecf20Sopenharmony_ci		.cra_blocksize	=	SHA256_BLOCK_SIZE,
758c2ecf20Sopenharmony_ci		.cra_module	=	THIS_MODULE,
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic int s390_sha224_init(struct shash_desc *desc)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	sctx->state[0] = SHA224_H0;
848c2ecf20Sopenharmony_ci	sctx->state[1] = SHA224_H1;
858c2ecf20Sopenharmony_ci	sctx->state[2] = SHA224_H2;
868c2ecf20Sopenharmony_ci	sctx->state[3] = SHA224_H3;
878c2ecf20Sopenharmony_ci	sctx->state[4] = SHA224_H4;
888c2ecf20Sopenharmony_ci	sctx->state[5] = SHA224_H5;
898c2ecf20Sopenharmony_ci	sctx->state[6] = SHA224_H6;
908c2ecf20Sopenharmony_ci	sctx->state[7] = SHA224_H7;
918c2ecf20Sopenharmony_ci	sctx->count = 0;
928c2ecf20Sopenharmony_ci	sctx->func = CPACF_KIMD_SHA_256;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return 0;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic struct shash_alg sha224_alg = {
988c2ecf20Sopenharmony_ci	.digestsize	=	SHA224_DIGEST_SIZE,
998c2ecf20Sopenharmony_ci	.init		=	s390_sha224_init,
1008c2ecf20Sopenharmony_ci	.update		=	s390_sha_update,
1018c2ecf20Sopenharmony_ci	.final		=	s390_sha_final,
1028c2ecf20Sopenharmony_ci	.export		=	sha256_export,
1038c2ecf20Sopenharmony_ci	.import		=	sha256_import,
1048c2ecf20Sopenharmony_ci	.descsize	=	sizeof(struct s390_sha_ctx),
1058c2ecf20Sopenharmony_ci	.statesize	=	sizeof(struct sha256_state),
1068c2ecf20Sopenharmony_ci	.base		=	{
1078c2ecf20Sopenharmony_ci		.cra_name	=	"sha224",
1088c2ecf20Sopenharmony_ci		.cra_driver_name=	"sha224-s390",
1098c2ecf20Sopenharmony_ci		.cra_priority	=	300,
1108c2ecf20Sopenharmony_ci		.cra_blocksize	=	SHA224_BLOCK_SIZE,
1118c2ecf20Sopenharmony_ci		.cra_module	=	THIS_MODULE,
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int __init sha256_s390_init(void)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	int ret;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_256))
1208c2ecf20Sopenharmony_ci		return -ENODEV;
1218c2ecf20Sopenharmony_ci	ret = crypto_register_shash(&sha256_alg);
1228c2ecf20Sopenharmony_ci	if (ret < 0)
1238c2ecf20Sopenharmony_ci		goto out;
1248c2ecf20Sopenharmony_ci	ret = crypto_register_shash(&sha224_alg);
1258c2ecf20Sopenharmony_ci	if (ret < 0)
1268c2ecf20Sopenharmony_ci		crypto_unregister_shash(&sha256_alg);
1278c2ecf20Sopenharmony_ciout:
1288c2ecf20Sopenharmony_ci	return ret;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic void __exit sha256_s390_fini(void)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	crypto_unregister_shash(&sha224_alg);
1348c2ecf20Sopenharmony_ci	crypto_unregister_shash(&sha256_alg);
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cimodule_cpu_feature_match(MSA, sha256_s390_init);
1388c2ecf20Sopenharmony_cimodule_exit(sha256_s390_fini);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("sha256");
1418c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("sha224");
1428c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1438c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SHA256 and SHA224 Secure Hash Algorithm");
144