Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Cryptographic API.
0004  *
0005  * s390 implementation of the SHA256 and SHA224 Secure Hash Algorithm.
0006  *
0007  * s390 Version:
0008  *   Copyright IBM Corp. 2019
0009  *   Author(s): Joerg Schmidbauer (jschmidb@de.ibm.com)
0010  */
0011 #include <crypto/internal/hash.h>
0012 #include <linux/init.h>
0013 #include <linux/module.h>
0014 #include <linux/cpufeature.h>
0015 #include <crypto/sha3.h>
0016 #include <asm/cpacf.h>
0017 
0018 #include "sha.h"
0019 
0020 static int sha3_256_init(struct shash_desc *desc)
0021 {
0022     struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
0023 
0024     memset(sctx->state, 0, sizeof(sctx->state));
0025     sctx->count = 0;
0026     sctx->func = CPACF_KIMD_SHA3_256;
0027 
0028     return 0;
0029 }
0030 
0031 static int sha3_256_export(struct shash_desc *desc, void *out)
0032 {
0033     struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
0034     struct sha3_state *octx = out;
0035 
0036     octx->rsiz = sctx->count;
0037     memcpy(octx->st, sctx->state, sizeof(octx->st));
0038     memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
0039 
0040     return 0;
0041 }
0042 
0043 static int sha3_256_import(struct shash_desc *desc, const void *in)
0044 {
0045     struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
0046     const struct sha3_state *ictx = in;
0047 
0048     sctx->count = ictx->rsiz;
0049     memcpy(sctx->state, ictx->st, sizeof(ictx->st));
0050     memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
0051     sctx->func = CPACF_KIMD_SHA3_256;
0052 
0053     return 0;
0054 }
0055 
0056 static int sha3_224_import(struct shash_desc *desc, const void *in)
0057 {
0058     struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
0059     const struct sha3_state *ictx = in;
0060 
0061     sctx->count = ictx->rsiz;
0062     memcpy(sctx->state, ictx->st, sizeof(ictx->st));
0063     memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
0064     sctx->func = CPACF_KIMD_SHA3_224;
0065 
0066     return 0;
0067 }
0068 
0069 static struct shash_alg sha3_256_alg = {
0070     .digestsize =   SHA3_256_DIGEST_SIZE,      /* = 32 */
0071     .init       =   sha3_256_init,
0072     .update     =   s390_sha_update,
0073     .final      =   s390_sha_final,
0074     .export     =   sha3_256_export,
0075     .import     =   sha3_256_import,
0076     .descsize   =   sizeof(struct s390_sha_ctx),
0077     .statesize  =   sizeof(struct sha3_state),
0078     .base       =   {
0079         .cra_name    =  "sha3-256",
0080         .cra_driver_name =  "sha3-256-s390",
0081         .cra_priority    =  300,
0082         .cra_blocksize   =  SHA3_256_BLOCK_SIZE,
0083         .cra_module  =  THIS_MODULE,
0084     }
0085 };
0086 
0087 static int sha3_224_init(struct shash_desc *desc)
0088 {
0089     struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
0090 
0091     memset(sctx->state, 0, sizeof(sctx->state));
0092     sctx->count = 0;
0093     sctx->func = CPACF_KIMD_SHA3_224;
0094 
0095     return 0;
0096 }
0097 
0098 static struct shash_alg sha3_224_alg = {
0099     .digestsize =   SHA3_224_DIGEST_SIZE,
0100     .init       =   sha3_224_init,
0101     .update     =   s390_sha_update,
0102     .final      =   s390_sha_final,
0103     .export     =   sha3_256_export, /* same as for 256 */
0104     .import     =   sha3_224_import, /* function code different! */
0105     .descsize   =   sizeof(struct s390_sha_ctx),
0106     .statesize  =   sizeof(struct sha3_state),
0107     .base       =   {
0108         .cra_name    =  "sha3-224",
0109         .cra_driver_name =  "sha3-224-s390",
0110         .cra_priority    =  300,
0111         .cra_blocksize   =  SHA3_224_BLOCK_SIZE,
0112         .cra_module  =  THIS_MODULE,
0113     }
0114 };
0115 
0116 static int __init sha3_256_s390_init(void)
0117 {
0118     int ret;
0119 
0120     if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA3_256))
0121         return -ENODEV;
0122 
0123     ret = crypto_register_shash(&sha3_256_alg);
0124     if (ret < 0)
0125         goto out;
0126 
0127     ret = crypto_register_shash(&sha3_224_alg);
0128     if (ret < 0)
0129         crypto_unregister_shash(&sha3_256_alg);
0130 out:
0131     return ret;
0132 }
0133 
0134 static void __exit sha3_256_s390_fini(void)
0135 {
0136     crypto_unregister_shash(&sha3_224_alg);
0137     crypto_unregister_shash(&sha3_256_alg);
0138 }
0139 
0140 module_cpu_feature_match(S390_CPU_FEATURE_MSA, sha3_256_s390_init);
0141 module_exit(sha3_256_s390_fini);
0142 
0143 MODULE_ALIAS_CRYPTO("sha3-256");
0144 MODULE_ALIAS_CRYPTO("sha3-224");
0145 MODULE_LICENSE("GPL");
0146 MODULE_DESCRIPTION("SHA3-256 and SHA3-224 Secure Hash Algorithm");