^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * s390 implementation of the SHA256 and SHA224 Secure Hash Algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * s390 Version:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright IBM Corp. 2005, 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author(s): Jan Glauber (jang@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/cpacf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "sha.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int s390_sha256_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) sctx->state[0] = SHA256_H0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) sctx->state[1] = SHA256_H1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) sctx->state[2] = SHA256_H2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) sctx->state[3] = SHA256_H3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) sctx->state[4] = SHA256_H4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) sctx->state[5] = SHA256_H5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) sctx->state[6] = SHA256_H6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) sctx->state[7] = SHA256_H7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) sctx->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) sctx->func = CPACF_KIMD_SHA_256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int sha256_export(struct shash_desc *desc, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct sha256_state *octx = out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) octx->count = sctx->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) memcpy(octx->state, sctx->state, sizeof(octx->state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int sha256_import(struct shash_desc *desc, const void *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) const struct sha256_state *ictx = in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) sctx->count = ictx->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) memcpy(sctx->state, ictx->state, sizeof(ictx->state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) sctx->func = CPACF_KIMD_SHA_256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static struct shash_alg sha256_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .digestsize = SHA256_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .init = s390_sha256_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .update = s390_sha_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .final = s390_sha_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .export = sha256_export,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .import = sha256_import,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .descsize = sizeof(struct s390_sha_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .statesize = sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .cra_name = "sha256",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .cra_driver_name= "sha256-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .cra_priority = 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .cra_blocksize = SHA256_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int s390_sha224_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) sctx->state[0] = SHA224_H0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) sctx->state[1] = SHA224_H1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) sctx->state[2] = SHA224_H2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) sctx->state[3] = SHA224_H3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) sctx->state[4] = SHA224_H4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) sctx->state[5] = SHA224_H5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) sctx->state[6] = SHA224_H6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) sctx->state[7] = SHA224_H7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) sctx->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) sctx->func = CPACF_KIMD_SHA_256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static struct shash_alg sha224_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .digestsize = SHA224_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .init = s390_sha224_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .update = s390_sha_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .final = s390_sha_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .export = sha256_export,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .import = sha256_import,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .descsize = sizeof(struct s390_sha_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .statesize = sizeof(struct sha256_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .cra_name = "sha224",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .cra_driver_name= "sha224-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .cra_priority = 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .cra_blocksize = SHA224_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int __init sha256_s390_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_256))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ret = crypto_register_shash(&sha256_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ret = crypto_register_shash(&sha224_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) crypto_unregister_shash(&sha256_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static void __exit sha256_s390_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) crypto_unregister_shash(&sha224_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) crypto_unregister_shash(&sha256_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) module_cpu_feature_match(MSA, sha256_s390_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) module_exit(sha256_s390_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) MODULE_ALIAS_CRYPTO("sha256");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) MODULE_ALIAS_CRYPTO("sha224");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) MODULE_DESCRIPTION("SHA256 and SHA224 Secure Hash Algorithm");