Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Cryptographic API.
0004  *
0005  * s390 implementation of the SHA512 and SHA38 Secure Hash Algorithm.
0006  *
0007  * Copyright IBM Corp. 2007
0008  * Author(s): Jan Glauber (jang@de.ibm.com)
0009  */
0010 #include <crypto/internal/hash.h>
0011 #include <crypto/sha2.h>
0012 #include <linux/errno.h>
0013 #include <linux/init.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/cpufeature.h>
0017 #include <asm/cpacf.h>
0018 
0019 #include "sha.h"
0020 
0021 static int sha512_init(struct shash_desc *desc)
0022 {
0023     struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
0024 
0025     *(__u64 *)&ctx->state[0] = SHA512_H0;
0026     *(__u64 *)&ctx->state[2] = SHA512_H1;
0027     *(__u64 *)&ctx->state[4] = SHA512_H2;
0028     *(__u64 *)&ctx->state[6] = SHA512_H3;
0029     *(__u64 *)&ctx->state[8] = SHA512_H4;
0030     *(__u64 *)&ctx->state[10] = SHA512_H5;
0031     *(__u64 *)&ctx->state[12] = SHA512_H6;
0032     *(__u64 *)&ctx->state[14] = SHA512_H7;
0033     ctx->count = 0;
0034     ctx->func = CPACF_KIMD_SHA_512;
0035 
0036     return 0;
0037 }
0038 
0039 static int sha512_export(struct shash_desc *desc, void *out)
0040 {
0041     struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
0042     struct sha512_state *octx = out;
0043 
0044     octx->count[0] = sctx->count;
0045     octx->count[1] = 0;
0046     memcpy(octx->state, sctx->state, sizeof(octx->state));
0047     memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
0048     return 0;
0049 }
0050 
0051 static int sha512_import(struct shash_desc *desc, const void *in)
0052 {
0053     struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
0054     const struct sha512_state *ictx = in;
0055 
0056     if (unlikely(ictx->count[1]))
0057         return -ERANGE;
0058     sctx->count = ictx->count[0];
0059 
0060     memcpy(sctx->state, ictx->state, sizeof(ictx->state));
0061     memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
0062     sctx->func = CPACF_KIMD_SHA_512;
0063     return 0;
0064 }
0065 
0066 static struct shash_alg sha512_alg = {
0067     .digestsize =   SHA512_DIGEST_SIZE,
0068     .init       =   sha512_init,
0069     .update     =   s390_sha_update,
0070     .final      =   s390_sha_final,
0071     .export     =   sha512_export,
0072     .import     =   sha512_import,
0073     .descsize   =   sizeof(struct s390_sha_ctx),
0074     .statesize  =   sizeof(struct sha512_state),
0075     .base       =   {
0076         .cra_name   =   "sha512",
0077         .cra_driver_name=   "sha512-s390",
0078         .cra_priority   =   300,
0079         .cra_blocksize  =   SHA512_BLOCK_SIZE,
0080         .cra_module =   THIS_MODULE,
0081     }
0082 };
0083 
0084 MODULE_ALIAS_CRYPTO("sha512");
0085 
0086 static int sha384_init(struct shash_desc *desc)
0087 {
0088     struct s390_sha_ctx *ctx = shash_desc_ctx(desc);
0089 
0090     *(__u64 *)&ctx->state[0] = SHA384_H0;
0091     *(__u64 *)&ctx->state[2] = SHA384_H1;
0092     *(__u64 *)&ctx->state[4] = SHA384_H2;
0093     *(__u64 *)&ctx->state[6] = SHA384_H3;
0094     *(__u64 *)&ctx->state[8] = SHA384_H4;
0095     *(__u64 *)&ctx->state[10] = SHA384_H5;
0096     *(__u64 *)&ctx->state[12] = SHA384_H6;
0097     *(__u64 *)&ctx->state[14] = SHA384_H7;
0098     ctx->count = 0;
0099     ctx->func = CPACF_KIMD_SHA_512;
0100 
0101     return 0;
0102 }
0103 
0104 static struct shash_alg sha384_alg = {
0105     .digestsize =   SHA384_DIGEST_SIZE,
0106     .init       =   sha384_init,
0107     .update     =   s390_sha_update,
0108     .final      =   s390_sha_final,
0109     .export     =   sha512_export,
0110     .import     =   sha512_import,
0111     .descsize   =   sizeof(struct s390_sha_ctx),
0112     .statesize  =   sizeof(struct sha512_state),
0113     .base       =   {
0114         .cra_name   =   "sha384",
0115         .cra_driver_name=   "sha384-s390",
0116         .cra_priority   =   300,
0117         .cra_blocksize  =   SHA384_BLOCK_SIZE,
0118         .cra_ctxsize    =   sizeof(struct s390_sha_ctx),
0119         .cra_module =   THIS_MODULE,
0120     }
0121 };
0122 
0123 MODULE_ALIAS_CRYPTO("sha384");
0124 
0125 static int __init init(void)
0126 {
0127     int ret;
0128 
0129     if (!cpacf_query_func(CPACF_KIMD, CPACF_KIMD_SHA_512))
0130         return -ENODEV;
0131     if ((ret = crypto_register_shash(&sha512_alg)) < 0)
0132         goto out;
0133     if ((ret = crypto_register_shash(&sha384_alg)) < 0)
0134         crypto_unregister_shash(&sha512_alg);
0135 out:
0136     return ret;
0137 }
0138 
0139 static void __exit fini(void)
0140 {
0141     crypto_unregister_shash(&sha512_alg);
0142     crypto_unregister_shash(&sha384_alg);
0143 }
0144 
0145 module_cpu_feature_match(S390_CPU_FEATURE_MSA, init);
0146 module_exit(fini);
0147 
0148 MODULE_LICENSE("GPL");
0149 MODULE_DESCRIPTION("SHA512 and SHA-384 Secure Hash Algorithm");