1 // SPDX-License-Identifier: GPL-2.0+
5 * s390 implementation of the SHA256 and SHA224 Secure Hash Algorithm.
8 * Copyright IBM Corp. 2019
11 #include <crypto/internal/hash.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/cpufeature.h>
15 #include <crypto/sha3.h>
16 #include <asm/cpacf.h>
20 static int sha3_256_init(struct shash_desc *desc)
22 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
24 if (!test_facility(86)) /* msa 12 */
25 memset(sctx->state, 0, sizeof(sctx->state));
27 sctx->func = CPACF_KIMD_SHA3_256;
28 sctx->first_message_part = 1;
33 static int sha3_256_export(struct shash_desc *desc, void *out)
35 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
36 struct sha3_state *octx = out;
38 octx->rsiz = sctx->count;
39 memcpy(octx->st, sctx->state, sizeof(octx->st));
40 memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
41 octx->partial = sctx->first_message_part;
46 static int sha3_256_import(struct shash_desc *desc, const void *in)
48 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
49 const struct sha3_state *ictx = in;
51 sctx->count = ictx->rsiz;
52 memcpy(sctx->state, ictx->st, sizeof(ictx->st));
53 memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
54 sctx->first_message_part = ictx->partial;
55 sctx->func = CPACF_KIMD_SHA3_256;
60 static int sha3_224_import(struct shash_desc *desc, const void *in)
62 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
63 const struct sha3_state *ictx = in;
65 sctx->count = ictx->rsiz;
66 memcpy(sctx->state, ictx->st, sizeof(ictx->st));
67 memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
68 sctx->first_message_part = ictx->partial;
69 sctx->func = CPACF_KIMD_SHA3_224;
74 static struct shash_alg sha3_256_alg = {
75 .digestsize = SHA3_256_DIGEST_SIZE, /* = 32 */
76 .init = sha3_256_init,
77 .update = s390_sha_update,
78 .final = s390_sha_final,
79 .export = sha3_256_export,
80 .import = sha3_256_import,
81 .descsize = sizeof(struct s390_sha_ctx),
82 .statesize = sizeof(struct sha3_state),
84 .cra_name = "sha3-256",
85 .cra_driver_name = "sha3-256-s390",
87 .cra_blocksize = SHA3_256_BLOCK_SIZE,
88 .cra_module = THIS_MODULE,
92 static int sha3_224_init(struct shash_desc *desc)
94 struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
96 if (!test_facility(86)) /* msa 12 */
97 memset(sctx->state, 0, sizeof(sctx->state));
99 sctx->func = CPACF_KIMD_SHA3_224;
100 sctx->first_message_part = 1;
105 static struct shash_alg sha3_224_alg = {
106 .digestsize = SHA3_224_DIGEST_SIZE,
107 .init = sha3_224_init,
108 .update = s390_sha_update,
109 .final = s390_sha_final,
110 .export = sha3_256_export, /* same as for 256 */
111 .import = sha3_224_import, /* function code different! */
112 .descsize = sizeof(struct s390_sha_ctx),
113 .statesize = sizeof(struct sha3_state),
115 .cra_name = "sha3-224",
116 .cra_driver_name = "sha3-224-s390",
118 .cra_blocksize = SHA3_224_BLOCK_SIZE,
119 .cra_module = THIS_MODULE,
123 static int __init sha3_256_s390_init(void)
127 if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA3_256))
130 ret = crypto_register_shash(&sha3_256_alg);
134 ret = crypto_register_shash(&sha3_224_alg);
136 crypto_unregister_shash(&sha3_256_alg);
141 static void __exit sha3_256_s390_fini(void)
143 crypto_unregister_shash(&sha3_224_alg);
144 crypto_unregister_shash(&sha3_256_alg);
147 module_cpu_feature_match(S390_CPU_FEATURE_MSA, sha3_256_s390_init);
148 module_exit(sha3_256_s390_fini);
150 MODULE_ALIAS_CRYPTO("sha3-256");
151 MODULE_ALIAS_CRYPTO("sha3-224");
152 MODULE_LICENSE("GPL");
153 MODULE_DESCRIPTION("SHA3-256 and SHA3-224 Secure Hash Algorithm");