18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cryptographic API.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * s390 implementation of the SHA1 Secure Hash Algorithm.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Derived from cryptoapi implementation, adapted for in-place
88c2ecf20Sopenharmony_ci * scatterlist interface.  Originally based on the public domain
98c2ecf20Sopenharmony_ci * implementation written by Steve Reid.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * s390 Version:
128c2ecf20Sopenharmony_ci *   Copyright IBM Corp. 2003, 2007
138c2ecf20Sopenharmony_ci *   Author(s): Thomas Spatzier
148c2ecf20Sopenharmony_ci *		Jan Glauber (jan.glauber@de.ibm.com)
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * Derived from "crypto/sha1_generic.c"
178c2ecf20Sopenharmony_ci *   Copyright (c) Alan Smithee.
188c2ecf20Sopenharmony_ci *   Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
198c2ecf20Sopenharmony_ci *   Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
228c2ecf20Sopenharmony_ci#include <linux/init.h>
238c2ecf20Sopenharmony_ci#include <linux/module.h>
248c2ecf20Sopenharmony_ci#include <linux/cpufeature.h>
258c2ecf20Sopenharmony_ci#include <crypto/sha.h>
268c2ecf20Sopenharmony_ci#include <asm/cpacf.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include "sha.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic int s390_sha1_init(struct shash_desc *desc)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	sctx->state[0] = SHA1_H0;
358c2ecf20Sopenharmony_ci	sctx->state[1] = SHA1_H1;
368c2ecf20Sopenharmony_ci	sctx->state[2] = SHA1_H2;
378c2ecf20Sopenharmony_ci	sctx->state[3] = SHA1_H3;
388c2ecf20Sopenharmony_ci	sctx->state[4] = SHA1_H4;
398c2ecf20Sopenharmony_ci	sctx->count = 0;
408c2ecf20Sopenharmony_ci	sctx->func = CPACF_KIMD_SHA_1;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	return 0;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int s390_sha1_export(struct shash_desc *desc, void *out)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
488c2ecf20Sopenharmony_ci	struct sha1_state *octx = out;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	octx->count = sctx->count;
518c2ecf20Sopenharmony_ci	memcpy(octx->state, sctx->state, sizeof(octx->state));
528c2ecf20Sopenharmony_ci	memcpy(octx->buffer, sctx->buf, sizeof(octx->buffer));
538c2ecf20Sopenharmony_ci	return 0;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int s390_sha1_import(struct shash_desc *desc, const void *in)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
598c2ecf20Sopenharmony_ci	const struct sha1_state *ictx = in;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	sctx->count = ictx->count;
628c2ecf20Sopenharmony_ci	memcpy(sctx->state, ictx->state, sizeof(ictx->state));
638c2ecf20Sopenharmony_ci	memcpy(sctx->buf, ictx->buffer, sizeof(ictx->buffer));
648c2ecf20Sopenharmony_ci	sctx->func = CPACF_KIMD_SHA_1;
658c2ecf20Sopenharmony_ci	return 0;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic struct shash_alg alg = {
698c2ecf20Sopenharmony_ci	.digestsize	=	SHA1_DIGEST_SIZE,
708c2ecf20Sopenharmony_ci	.init		=	s390_sha1_init,
718c2ecf20Sopenharmony_ci	.update		=	s390_sha_update,
728c2ecf20Sopenharmony_ci	.final		=	s390_sha_final,
738c2ecf20Sopenharmony_ci	.export		=	s390_sha1_export,
748c2ecf20Sopenharmony_ci	.import		=	s390_sha1_import,
758c2ecf20Sopenharmony_ci	.descsize	=	sizeof(struct s390_sha_ctx),
768c2ecf20Sopenharmony_ci	.statesize	=	sizeof(struct sha1_state),
778c2ecf20Sopenharmony_ci	.base		=	{
788c2ecf20Sopenharmony_ci		.cra_name	=	"sha1",
798c2ecf20Sopenharmony_ci		.cra_driver_name=	"sha1-s390",
808c2ecf20Sopenharmony_ci		.cra_priority	=	300,
818c2ecf20Sopenharmony_ci		.cra_blocksize	=	SHA1_BLOCK_SIZE,
828c2ecf20Sopenharmony_ci		.cra_module	=	THIS_MODULE,
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int __init sha1_s390_init(void)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_1))
898c2ecf20Sopenharmony_ci		return -ENODEV;
908c2ecf20Sopenharmony_ci	return crypto_register_shash(&alg);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic void __exit sha1_s390_fini(void)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	crypto_unregister_shash(&alg);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cimodule_cpu_feature_match(MSA, sha1_s390_init);
998c2ecf20Sopenharmony_cimodule_exit(sha1_s390_fini);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("sha1");
1028c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1038c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
104