Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
0004  *
0005  * Copyright (C) 2013,2018 Advanced Micro Devices, Inc.
0006  *
0007  * Author: Tom Lendacky <thomas.lendacky@amd.com>
0008  * Author: Gary R Hook <gary.hook@amd.com>
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/sched.h>
0013 #include <linux/delay.h>
0014 #include <linux/scatterlist.h>
0015 #include <linux/crypto.h>
0016 #include <crypto/algapi.h>
0017 #include <crypto/hash.h>
0018 #include <crypto/hmac.h>
0019 #include <crypto/internal/hash.h>
0020 #include <crypto/sha1.h>
0021 #include <crypto/sha2.h>
0022 #include <crypto/scatterwalk.h>
0023 #include <linux/string.h>
0024 
0025 #include "ccp-crypto.h"
0026 
0027 static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
0028 {
0029     struct ahash_request *req = ahash_request_cast(async_req);
0030     struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
0031     struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
0032     unsigned int digest_size = crypto_ahash_digestsize(tfm);
0033 
0034     if (ret)
0035         goto e_free;
0036 
0037     if (rctx->hash_rem) {
0038         /* Save remaining data to buffer */
0039         unsigned int offset = rctx->nbytes - rctx->hash_rem;
0040 
0041         scatterwalk_map_and_copy(rctx->buf, rctx->src,
0042                      offset, rctx->hash_rem, 0);
0043         rctx->buf_count = rctx->hash_rem;
0044     } else {
0045         rctx->buf_count = 0;
0046     }
0047 
0048     /* Update result area if supplied */
0049     if (req->result && rctx->final)
0050         memcpy(req->result, rctx->ctx, digest_size);
0051 
0052 e_free:
0053     sg_free_table(&rctx->data_sg);
0054 
0055     return ret;
0056 }
0057 
0058 static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
0059                  unsigned int final)
0060 {
0061     struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
0062     struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
0063     struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
0064     struct scatterlist *sg;
0065     unsigned int block_size =
0066         crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
0067     unsigned int sg_count;
0068     gfp_t gfp;
0069     u64 len;
0070     int ret;
0071 
0072     len = (u64)rctx->buf_count + (u64)nbytes;
0073 
0074     if (!final && (len <= block_size)) {
0075         scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
0076                      0, nbytes, 0);
0077         rctx->buf_count += nbytes;
0078 
0079         return 0;
0080     }
0081 
0082     rctx->src = req->src;
0083     rctx->nbytes = nbytes;
0084 
0085     rctx->final = final;
0086     rctx->hash_rem = final ? 0 : len & (block_size - 1);
0087     rctx->hash_cnt = len - rctx->hash_rem;
0088     if (!final && !rctx->hash_rem) {
0089         /* CCP can't do zero length final, so keep some data around */
0090         rctx->hash_cnt -= block_size;
0091         rctx->hash_rem = block_size;
0092     }
0093 
0094     /* Initialize the context scatterlist */
0095     sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
0096 
0097     sg = NULL;
0098     if (rctx->buf_count && nbytes) {
0099         /* Build the data scatterlist table - allocate enough entries
0100          * for both data pieces (buffer and input data)
0101          */
0102         gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
0103             GFP_KERNEL : GFP_ATOMIC;
0104         sg_count = sg_nents(req->src) + 1;
0105         ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
0106         if (ret)
0107             return ret;
0108 
0109         sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
0110         sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
0111         if (!sg) {
0112             ret = -EINVAL;
0113             goto e_free;
0114         }
0115         sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
0116         if (!sg) {
0117             ret = -EINVAL;
0118             goto e_free;
0119         }
0120         sg_mark_end(sg);
0121 
0122         sg = rctx->data_sg.sgl;
0123     } else if (rctx->buf_count) {
0124         sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
0125 
0126         sg = &rctx->buf_sg;
0127     } else if (nbytes) {
0128         sg = req->src;
0129     }
0130 
0131     rctx->msg_bits += (rctx->hash_cnt << 3);    /* Total in bits */
0132 
0133     memset(&rctx->cmd, 0, sizeof(rctx->cmd));
0134     INIT_LIST_HEAD(&rctx->cmd.entry);
0135     rctx->cmd.engine = CCP_ENGINE_SHA;
0136     rctx->cmd.u.sha.type = rctx->type;
0137     rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
0138 
0139     switch (rctx->type) {
0140     case CCP_SHA_TYPE_1:
0141         rctx->cmd.u.sha.ctx_len = SHA1_DIGEST_SIZE;
0142         break;
0143     case CCP_SHA_TYPE_224:
0144         rctx->cmd.u.sha.ctx_len = SHA224_DIGEST_SIZE;
0145         break;
0146     case CCP_SHA_TYPE_256:
0147         rctx->cmd.u.sha.ctx_len = SHA256_DIGEST_SIZE;
0148         break;
0149     case CCP_SHA_TYPE_384:
0150         rctx->cmd.u.sha.ctx_len = SHA384_DIGEST_SIZE;
0151         break;
0152     case CCP_SHA_TYPE_512:
0153         rctx->cmd.u.sha.ctx_len = SHA512_DIGEST_SIZE;
0154         break;
0155     default:
0156         /* Should never get here */
0157         break;
0158     }
0159 
0160     rctx->cmd.u.sha.src = sg;
0161     rctx->cmd.u.sha.src_len = rctx->hash_cnt;
0162     rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
0163         &ctx->u.sha.opad_sg : NULL;
0164     rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
0165         ctx->u.sha.opad_count : 0;
0166     rctx->cmd.u.sha.first = rctx->first;
0167     rctx->cmd.u.sha.final = rctx->final;
0168     rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
0169 
0170     rctx->first = 0;
0171 
0172     ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
0173 
0174     return ret;
0175 
0176 e_free:
0177     sg_free_table(&rctx->data_sg);
0178 
0179     return ret;
0180 }
0181 
0182 static int ccp_sha_init(struct ahash_request *req)
0183 {
0184     struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
0185     struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
0186     struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
0187     struct ccp_crypto_ahash_alg *alg =
0188         ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
0189     unsigned int block_size =
0190         crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
0191 
0192     memset(rctx, 0, sizeof(*rctx));
0193 
0194     rctx->type = alg->type;
0195     rctx->first = 1;
0196 
0197     if (ctx->u.sha.key_len) {
0198         /* Buffer the HMAC key for first update */
0199         memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
0200         rctx->buf_count = block_size;
0201     }
0202 
0203     return 0;
0204 }
0205 
0206 static int ccp_sha_update(struct ahash_request *req)
0207 {
0208     return ccp_do_sha_update(req, req->nbytes, 0);
0209 }
0210 
0211 static int ccp_sha_final(struct ahash_request *req)
0212 {
0213     return ccp_do_sha_update(req, 0, 1);
0214 }
0215 
0216 static int ccp_sha_finup(struct ahash_request *req)
0217 {
0218     return ccp_do_sha_update(req, req->nbytes, 1);
0219 }
0220 
0221 static int ccp_sha_digest(struct ahash_request *req)
0222 {
0223     int ret;
0224 
0225     ret = ccp_sha_init(req);
0226     if (ret)
0227         return ret;
0228 
0229     return ccp_sha_finup(req);
0230 }
0231 
0232 static int ccp_sha_export(struct ahash_request *req, void *out)
0233 {
0234     struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
0235     struct ccp_sha_exp_ctx state;
0236 
0237     /* Don't let anything leak to 'out' */
0238     memset(&state, 0, sizeof(state));
0239 
0240     state.type = rctx->type;
0241     state.msg_bits = rctx->msg_bits;
0242     state.first = rctx->first;
0243     memcpy(state.ctx, rctx->ctx, sizeof(state.ctx));
0244     state.buf_count = rctx->buf_count;
0245     memcpy(state.buf, rctx->buf, sizeof(state.buf));
0246 
0247     /* 'out' may not be aligned so memcpy from local variable */
0248     memcpy(out, &state, sizeof(state));
0249 
0250     return 0;
0251 }
0252 
0253 static int ccp_sha_import(struct ahash_request *req, const void *in)
0254 {
0255     struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
0256     struct ccp_sha_exp_ctx state;
0257 
0258     /* 'in' may not be aligned so memcpy to local variable */
0259     memcpy(&state, in, sizeof(state));
0260 
0261     memset(rctx, 0, sizeof(*rctx));
0262     rctx->type = state.type;
0263     rctx->msg_bits = state.msg_bits;
0264     rctx->first = state.first;
0265     memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx));
0266     rctx->buf_count = state.buf_count;
0267     memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
0268 
0269     return 0;
0270 }
0271 
0272 static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
0273               unsigned int key_len)
0274 {
0275     struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
0276     struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
0277     unsigned int block_size = crypto_shash_blocksize(shash);
0278     unsigned int digest_size = crypto_shash_digestsize(shash);
0279     int i, ret;
0280 
0281     /* Set to zero until complete */
0282     ctx->u.sha.key_len = 0;
0283 
0284     /* Clear key area to provide zero padding for keys smaller
0285      * than the block size
0286      */
0287     memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
0288 
0289     if (key_len > block_size) {
0290         /* Must hash the input key */
0291         ret = crypto_shash_tfm_digest(shash, key, key_len,
0292                           ctx->u.sha.key);
0293         if (ret)
0294             return -EINVAL;
0295 
0296         key_len = digest_size;
0297     } else {
0298         memcpy(ctx->u.sha.key, key, key_len);
0299     }
0300 
0301     for (i = 0; i < block_size; i++) {
0302         ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ HMAC_IPAD_VALUE;
0303         ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ HMAC_OPAD_VALUE;
0304     }
0305 
0306     sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
0307     ctx->u.sha.opad_count = block_size;
0308 
0309     ctx->u.sha.key_len = key_len;
0310 
0311     return 0;
0312 }
0313 
0314 static int ccp_sha_cra_init(struct crypto_tfm *tfm)
0315 {
0316     struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
0317     struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
0318 
0319     ctx->complete = ccp_sha_complete;
0320     ctx->u.sha.key_len = 0;
0321 
0322     crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
0323 
0324     return 0;
0325 }
0326 
0327 static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
0328 {
0329 }
0330 
0331 static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
0332 {
0333     struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
0334     struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
0335     struct crypto_shash *hmac_tfm;
0336 
0337     hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
0338     if (IS_ERR(hmac_tfm)) {
0339         pr_warn("could not load driver %s need for HMAC support\n",
0340             alg->child_alg);
0341         return PTR_ERR(hmac_tfm);
0342     }
0343 
0344     ctx->u.sha.hmac_tfm = hmac_tfm;
0345 
0346     return ccp_sha_cra_init(tfm);
0347 }
0348 
0349 static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
0350 {
0351     struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
0352 
0353     if (ctx->u.sha.hmac_tfm)
0354         crypto_free_shash(ctx->u.sha.hmac_tfm);
0355 
0356     ccp_sha_cra_exit(tfm);
0357 }
0358 
0359 struct ccp_sha_def {
0360     unsigned int version;
0361     const char *name;
0362     const char *drv_name;
0363     enum ccp_sha_type type;
0364     u32 digest_size;
0365     u32 block_size;
0366 };
0367 
0368 static struct ccp_sha_def sha_algs[] = {
0369     {
0370         .version    = CCP_VERSION(3, 0),
0371         .name       = "sha1",
0372         .drv_name   = "sha1-ccp",
0373         .type       = CCP_SHA_TYPE_1,
0374         .digest_size    = SHA1_DIGEST_SIZE,
0375         .block_size = SHA1_BLOCK_SIZE,
0376     },
0377     {
0378         .version    = CCP_VERSION(3, 0),
0379         .name       = "sha224",
0380         .drv_name   = "sha224-ccp",
0381         .type       = CCP_SHA_TYPE_224,
0382         .digest_size    = SHA224_DIGEST_SIZE,
0383         .block_size = SHA224_BLOCK_SIZE,
0384     },
0385     {
0386         .version    = CCP_VERSION(3, 0),
0387         .name       = "sha256",
0388         .drv_name   = "sha256-ccp",
0389         .type       = CCP_SHA_TYPE_256,
0390         .digest_size    = SHA256_DIGEST_SIZE,
0391         .block_size = SHA256_BLOCK_SIZE,
0392     },
0393     {
0394         .version    = CCP_VERSION(5, 0),
0395         .name       = "sha384",
0396         .drv_name   = "sha384-ccp",
0397         .type       = CCP_SHA_TYPE_384,
0398         .digest_size    = SHA384_DIGEST_SIZE,
0399         .block_size = SHA384_BLOCK_SIZE,
0400     },
0401     {
0402         .version    = CCP_VERSION(5, 0),
0403         .name       = "sha512",
0404         .drv_name   = "sha512-ccp",
0405         .type       = CCP_SHA_TYPE_512,
0406         .digest_size    = SHA512_DIGEST_SIZE,
0407         .block_size = SHA512_BLOCK_SIZE,
0408     },
0409 };
0410 
0411 static int ccp_register_hmac_alg(struct list_head *head,
0412                  const struct ccp_sha_def *def,
0413                  const struct ccp_crypto_ahash_alg *base_alg)
0414 {
0415     struct ccp_crypto_ahash_alg *ccp_alg;
0416     struct ahash_alg *alg;
0417     struct hash_alg_common *halg;
0418     struct crypto_alg *base;
0419     int ret;
0420 
0421     ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
0422     if (!ccp_alg)
0423         return -ENOMEM;
0424 
0425     /* Copy the base algorithm and only change what's necessary */
0426     *ccp_alg = *base_alg;
0427     INIT_LIST_HEAD(&ccp_alg->entry);
0428 
0429     strscpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
0430 
0431     alg = &ccp_alg->alg;
0432     alg->setkey = ccp_sha_setkey;
0433 
0434     halg = &alg->halg;
0435 
0436     base = &halg->base;
0437     snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
0438     snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
0439          def->drv_name);
0440     base->cra_init = ccp_hmac_sha_cra_init;
0441     base->cra_exit = ccp_hmac_sha_cra_exit;
0442 
0443     ret = crypto_register_ahash(alg);
0444     if (ret) {
0445         pr_err("%s ahash algorithm registration error (%d)\n",
0446                base->cra_name, ret);
0447         kfree(ccp_alg);
0448         return ret;
0449     }
0450 
0451     list_add(&ccp_alg->entry, head);
0452 
0453     return ret;
0454 }
0455 
0456 static int ccp_register_sha_alg(struct list_head *head,
0457                 const struct ccp_sha_def *def)
0458 {
0459     struct ccp_crypto_ahash_alg *ccp_alg;
0460     struct ahash_alg *alg;
0461     struct hash_alg_common *halg;
0462     struct crypto_alg *base;
0463     int ret;
0464 
0465     ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
0466     if (!ccp_alg)
0467         return -ENOMEM;
0468 
0469     INIT_LIST_HEAD(&ccp_alg->entry);
0470 
0471     ccp_alg->type = def->type;
0472 
0473     alg = &ccp_alg->alg;
0474     alg->init = ccp_sha_init;
0475     alg->update = ccp_sha_update;
0476     alg->final = ccp_sha_final;
0477     alg->finup = ccp_sha_finup;
0478     alg->digest = ccp_sha_digest;
0479     alg->export = ccp_sha_export;
0480     alg->import = ccp_sha_import;
0481 
0482     halg = &alg->halg;
0483     halg->digestsize = def->digest_size;
0484     halg->statesize = sizeof(struct ccp_sha_exp_ctx);
0485 
0486     base = &halg->base;
0487     snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
0488     snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
0489          def->drv_name);
0490     base->cra_flags = CRYPTO_ALG_ASYNC |
0491               CRYPTO_ALG_ALLOCATES_MEMORY |
0492               CRYPTO_ALG_KERN_DRIVER_ONLY |
0493               CRYPTO_ALG_NEED_FALLBACK;
0494     base->cra_blocksize = def->block_size;
0495     base->cra_ctxsize = sizeof(struct ccp_ctx);
0496     base->cra_priority = CCP_CRA_PRIORITY;
0497     base->cra_init = ccp_sha_cra_init;
0498     base->cra_exit = ccp_sha_cra_exit;
0499     base->cra_module = THIS_MODULE;
0500 
0501     ret = crypto_register_ahash(alg);
0502     if (ret) {
0503         pr_err("%s ahash algorithm registration error (%d)\n",
0504                base->cra_name, ret);
0505         kfree(ccp_alg);
0506         return ret;
0507     }
0508 
0509     list_add(&ccp_alg->entry, head);
0510 
0511     ret = ccp_register_hmac_alg(head, def, ccp_alg);
0512 
0513     return ret;
0514 }
0515 
0516 int ccp_register_sha_algs(struct list_head *head)
0517 {
0518     int i, ret;
0519     unsigned int ccpversion = ccp_version();
0520 
0521     for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
0522         if (sha_algs[i].version > ccpversion)
0523             continue;
0524         ret = ccp_register_sha_alg(head, &sha_algs[i]);
0525         if (ret)
0526             return ret;
0527     }
0528 
0529     return 0;
0530 }