Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002  /* Asymmetric algorithms supported by virtio crypto device
0003   *
0004   * Authors: zhenwei pi <pizhenwei@bytedance.com>
0005   *          lei he <helei.sig11@bytedance.com>
0006   *
0007   * Copyright 2022 Bytedance CO., LTD.
0008   */
0009 
0010 #include <linux/mpi.h>
0011 #include <linux/scatterlist.h>
0012 #include <crypto/algapi.h>
0013 #include <crypto/internal/akcipher.h>
0014 #include <crypto/internal/rsa.h>
0015 #include <linux/err.h>
0016 #include <crypto/scatterwalk.h>
0017 #include <linux/atomic.h>
0018 
0019 #include <uapi/linux/virtio_crypto.h>
0020 #include "virtio_crypto_common.h"
0021 
0022 struct virtio_crypto_rsa_ctx {
0023     MPI n;
0024 };
0025 
0026 struct virtio_crypto_akcipher_ctx {
0027     struct crypto_engine_ctx enginectx;
0028     struct virtio_crypto *vcrypto;
0029     struct crypto_akcipher *tfm;
0030     bool session_valid;
0031     __u64 session_id;
0032     union {
0033         struct virtio_crypto_rsa_ctx rsa_ctx;
0034     };
0035 };
0036 
0037 struct virtio_crypto_akcipher_request {
0038     struct virtio_crypto_request base;
0039     struct virtio_crypto_akcipher_ctx *akcipher_ctx;
0040     struct akcipher_request *akcipher_req;
0041     void *src_buf;
0042     void *dst_buf;
0043     uint32_t opcode;
0044 };
0045 
0046 struct virtio_crypto_akcipher_algo {
0047     uint32_t algonum;
0048     uint32_t service;
0049     unsigned int active_devs;
0050     struct akcipher_alg algo;
0051 };
0052 
0053 static DEFINE_MUTEX(algs_lock);
0054 
0055 static void virtio_crypto_akcipher_finalize_req(
0056     struct virtio_crypto_akcipher_request *vc_akcipher_req,
0057     struct akcipher_request *req, int err)
0058 {
0059     kfree(vc_akcipher_req->src_buf);
0060     kfree(vc_akcipher_req->dst_buf);
0061     vc_akcipher_req->src_buf = NULL;
0062     vc_akcipher_req->dst_buf = NULL;
0063     virtcrypto_clear_request(&vc_akcipher_req->base);
0064 
0065     crypto_finalize_akcipher_request(vc_akcipher_req->base.dataq->engine, req, err);
0066 }
0067 
0068 static void virtio_crypto_dataq_akcipher_callback(struct virtio_crypto_request *vc_req, int len)
0069 {
0070     struct virtio_crypto_akcipher_request *vc_akcipher_req =
0071         container_of(vc_req, struct virtio_crypto_akcipher_request, base);
0072     struct akcipher_request *akcipher_req;
0073     int error;
0074 
0075     switch (vc_req->status) {
0076     case VIRTIO_CRYPTO_OK:
0077         error = 0;
0078         break;
0079     case VIRTIO_CRYPTO_INVSESS:
0080     case VIRTIO_CRYPTO_ERR:
0081         error = -EINVAL;
0082         break;
0083     case VIRTIO_CRYPTO_BADMSG:
0084         error = -EBADMSG;
0085         break;
0086 
0087     case VIRTIO_CRYPTO_KEY_REJECTED:
0088         error = -EKEYREJECTED;
0089         break;
0090 
0091     default:
0092         error = -EIO;
0093         break;
0094     }
0095 
0096     akcipher_req = vc_akcipher_req->akcipher_req;
0097     if (vc_akcipher_req->opcode != VIRTIO_CRYPTO_AKCIPHER_VERIFY) {
0098         /* actuall length maybe less than dst buffer */
0099         akcipher_req->dst_len = len - sizeof(vc_req->status);
0100         sg_copy_from_buffer(akcipher_req->dst, sg_nents(akcipher_req->dst),
0101                     vc_akcipher_req->dst_buf, akcipher_req->dst_len);
0102     }
0103     virtio_crypto_akcipher_finalize_req(vc_akcipher_req, akcipher_req, error);
0104 }
0105 
0106 static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher_ctx *ctx,
0107         struct virtio_crypto_ctrl_header *header, void *para,
0108         const uint8_t *key, unsigned int keylen)
0109 {
0110     struct scatterlist outhdr_sg, key_sg, inhdr_sg, *sgs[3];
0111     struct virtio_crypto *vcrypto = ctx->vcrypto;
0112     uint8_t *pkey;
0113     int err;
0114     unsigned int num_out = 0, num_in = 0;
0115     struct virtio_crypto_op_ctrl_req *ctrl;
0116     struct virtio_crypto_session_input *input;
0117     struct virtio_crypto_ctrl_request *vc_ctrl_req;
0118 
0119     pkey = kmemdup(key, keylen, GFP_ATOMIC);
0120     if (!pkey)
0121         return -ENOMEM;
0122 
0123     vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
0124     if (!vc_ctrl_req) {
0125         err = -ENOMEM;
0126         goto out;
0127     }
0128 
0129     ctrl = &vc_ctrl_req->ctrl;
0130     memcpy(&ctrl->header, header, sizeof(ctrl->header));
0131     memcpy(&ctrl->u, para, sizeof(ctrl->u));
0132     input = &vc_ctrl_req->input;
0133     input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
0134 
0135     sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
0136     sgs[num_out++] = &outhdr_sg;
0137 
0138     sg_init_one(&key_sg, pkey, keylen);
0139     sgs[num_out++] = &key_sg;
0140 
0141     sg_init_one(&inhdr_sg, input, sizeof(*input));
0142     sgs[num_out + num_in++] = &inhdr_sg;
0143 
0144     err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
0145     if (err < 0)
0146         goto out;
0147 
0148     if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
0149         pr_err("virtio_crypto: Create session failed status: %u\n",
0150             le32_to_cpu(input->status));
0151         err = -EINVAL;
0152         goto out;
0153     }
0154 
0155     ctx->session_id = le64_to_cpu(input->session_id);
0156     ctx->session_valid = true;
0157     err = 0;
0158 
0159 out:
0160     kfree(vc_ctrl_req);
0161     kfree_sensitive(pkey);
0162 
0163     return err;
0164 }
0165 
0166 static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akcipher_ctx *ctx)
0167 {
0168     struct scatterlist outhdr_sg, inhdr_sg, *sgs[2];
0169     struct virtio_crypto_destroy_session_req *destroy_session;
0170     struct virtio_crypto *vcrypto = ctx->vcrypto;
0171     unsigned int num_out = 0, num_in = 0;
0172     int err;
0173     struct virtio_crypto_op_ctrl_req *ctrl;
0174     struct virtio_crypto_inhdr *ctrl_status;
0175     struct virtio_crypto_ctrl_request *vc_ctrl_req;
0176 
0177     if (!ctx->session_valid)
0178         return 0;
0179 
0180     vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
0181     if (!vc_ctrl_req)
0182         return -ENOMEM;
0183 
0184     ctrl_status = &vc_ctrl_req->ctrl_status;
0185     ctrl_status->status = VIRTIO_CRYPTO_ERR;
0186     ctrl = &vc_ctrl_req->ctrl;
0187     ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION);
0188     ctrl->header.queue_id = 0;
0189 
0190     destroy_session = &ctrl->u.destroy_session;
0191     destroy_session->session_id = cpu_to_le64(ctx->session_id);
0192 
0193     sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
0194     sgs[num_out++] = &outhdr_sg;
0195 
0196     sg_init_one(&inhdr_sg, &ctrl_status->status, sizeof(ctrl_status->status));
0197     sgs[num_out + num_in++] = &inhdr_sg;
0198 
0199     err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
0200     if (err < 0)
0201         goto out;
0202 
0203     if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
0204         pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
0205             ctrl_status->status, destroy_session->session_id);
0206         err = -EINVAL;
0207         goto out;
0208     }
0209 
0210     err = 0;
0211     ctx->session_valid = false;
0212 
0213 out:
0214     kfree(vc_ctrl_req);
0215 
0216     return err;
0217 }
0218 
0219 static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request *vc_akcipher_req,
0220         struct akcipher_request *req, struct data_queue *data_vq)
0221 {
0222     struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx;
0223     struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
0224     struct virtio_crypto *vcrypto = ctx->vcrypto;
0225     struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
0226     struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
0227     void *src_buf = NULL, *dst_buf = NULL;
0228     unsigned int num_out = 0, num_in = 0;
0229     int node = dev_to_node(&vcrypto->vdev->dev);
0230     unsigned long flags;
0231     int ret = -ENOMEM;
0232     bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
0233     unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;
0234 
0235     /* out header */
0236     sg_init_one(&outhdr_sg, req_data, sizeof(*req_data));
0237     sgs[num_out++] = &outhdr_sg;
0238 
0239     /* src data */
0240     src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
0241     if (!src_buf)
0242         goto err;
0243 
0244     if (verify) {
0245         /* for verify operation, both src and dst data work as OUT direction */
0246         sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
0247         sg_init_one(&srcdata_sg, src_buf, src_len);
0248         sgs[num_out++] = &srcdata_sg;
0249     } else {
0250         sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
0251         sg_init_one(&srcdata_sg, src_buf, src_len);
0252         sgs[num_out++] = &srcdata_sg;
0253 
0254         /* dst data */
0255         dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
0256         if (!dst_buf)
0257             goto err;
0258 
0259         sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
0260         sgs[num_out + num_in++] = &dstdata_sg;
0261     }
0262 
0263     vc_akcipher_req->src_buf = src_buf;
0264     vc_akcipher_req->dst_buf = dst_buf;
0265 
0266     /* in header */
0267     sg_init_one(&inhdr_sg, &vc_req->status, sizeof(vc_req->status));
0268     sgs[num_out + num_in++] = &inhdr_sg;
0269 
0270     spin_lock_irqsave(&data_vq->lock, flags);
0271     ret = virtqueue_add_sgs(data_vq->vq, sgs, num_out, num_in, vc_req, GFP_ATOMIC);
0272     virtqueue_kick(data_vq->vq);
0273     spin_unlock_irqrestore(&data_vq->lock, flags);
0274     if (ret)
0275         goto err;
0276 
0277     return 0;
0278 
0279 err:
0280     kfree(src_buf);
0281     kfree(dst_buf);
0282 
0283     return -ENOMEM;
0284 }
0285 
0286 static int virtio_crypto_rsa_do_req(struct crypto_engine *engine, void *vreq)
0287 {
0288     struct akcipher_request *req = container_of(vreq, struct akcipher_request, base);
0289     struct virtio_crypto_akcipher_request *vc_akcipher_req = akcipher_request_ctx(req);
0290     struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
0291     struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx;
0292     struct virtio_crypto *vcrypto = ctx->vcrypto;
0293     struct data_queue *data_vq = vc_req->dataq;
0294     struct virtio_crypto_op_header *header;
0295     struct virtio_crypto_akcipher_data_req *akcipher_req;
0296     int ret;
0297 
0298     vc_req->sgs = NULL;
0299     vc_req->req_data = kzalloc_node(sizeof(*vc_req->req_data),
0300         GFP_KERNEL, dev_to_node(&vcrypto->vdev->dev));
0301     if (!vc_req->req_data)
0302         return -ENOMEM;
0303 
0304     /* build request header */
0305     header = &vc_req->req_data->header;
0306     header->opcode = cpu_to_le32(vc_akcipher_req->opcode);
0307     header->algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA);
0308     header->session_id = cpu_to_le64(ctx->session_id);
0309 
0310     /* build request akcipher data */
0311     akcipher_req = &vc_req->req_data->u.akcipher_req;
0312     akcipher_req->para.src_data_len = cpu_to_le32(req->src_len);
0313     akcipher_req->para.dst_data_len = cpu_to_le32(req->dst_len);
0314 
0315     ret = __virtio_crypto_akcipher_do_req(vc_akcipher_req, req, data_vq);
0316     if (ret < 0) {
0317         kfree_sensitive(vc_req->req_data);
0318         vc_req->req_data = NULL;
0319         return ret;
0320     }
0321 
0322     return 0;
0323 }
0324 
0325 static int virtio_crypto_rsa_req(struct akcipher_request *req, uint32_t opcode)
0326 {
0327     struct crypto_akcipher *atfm = crypto_akcipher_reqtfm(req);
0328     struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(atfm);
0329     struct virtio_crypto_akcipher_request *vc_akcipher_req = akcipher_request_ctx(req);
0330     struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
0331     struct virtio_crypto *vcrypto = ctx->vcrypto;
0332     /* Use the first data virtqueue as default */
0333     struct data_queue *data_vq = &vcrypto->data_vq[0];
0334 
0335     vc_req->dataq = data_vq;
0336     vc_req->alg_cb = virtio_crypto_dataq_akcipher_callback;
0337     vc_akcipher_req->akcipher_ctx = ctx;
0338     vc_akcipher_req->akcipher_req = req;
0339     vc_akcipher_req->opcode = opcode;
0340 
0341     return crypto_transfer_akcipher_request_to_engine(data_vq->engine, req);
0342 }
0343 
0344 static int virtio_crypto_rsa_encrypt(struct akcipher_request *req)
0345 {
0346     return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_ENCRYPT);
0347 }
0348 
0349 static int virtio_crypto_rsa_decrypt(struct akcipher_request *req)
0350 {
0351     return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_DECRYPT);
0352 }
0353 
0354 static int virtio_crypto_rsa_sign(struct akcipher_request *req)
0355 {
0356     return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_SIGN);
0357 }
0358 
0359 static int virtio_crypto_rsa_verify(struct akcipher_request *req)
0360 {
0361     return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_VERIFY);
0362 }
0363 
0364 static int virtio_crypto_rsa_set_key(struct crypto_akcipher *tfm,
0365                      const void *key,
0366                      unsigned int keylen,
0367                      bool private,
0368                      int padding_algo,
0369                      int hash_algo)
0370 {
0371     struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
0372     struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx;
0373     struct virtio_crypto *vcrypto;
0374     struct virtio_crypto_ctrl_header header;
0375     struct virtio_crypto_akcipher_session_para para;
0376     struct rsa_key rsa_key = {0};
0377     int node = virtio_crypto_get_current_node();
0378     uint32_t keytype;
0379     int ret;
0380 
0381     /* mpi_free will test n, just free it. */
0382     mpi_free(rsa_ctx->n);
0383     rsa_ctx->n = NULL;
0384 
0385     if (private) {
0386         keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE;
0387         ret = rsa_parse_priv_key(&rsa_key, key, keylen);
0388     } else {
0389         keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC;
0390         ret = rsa_parse_pub_key(&rsa_key, key, keylen);
0391     }
0392 
0393     if (ret)
0394         return ret;
0395 
0396     rsa_ctx->n = mpi_read_raw_data(rsa_key.n, rsa_key.n_sz);
0397     if (!rsa_ctx->n)
0398         return -ENOMEM;
0399 
0400     if (!ctx->vcrypto) {
0401         vcrypto = virtcrypto_get_dev_node(node, VIRTIO_CRYPTO_SERVICE_AKCIPHER,
0402                         VIRTIO_CRYPTO_AKCIPHER_RSA);
0403         if (!vcrypto) {
0404             pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n");
0405             return -ENODEV;
0406         }
0407 
0408         ctx->vcrypto = vcrypto;
0409     } else {
0410         virtio_crypto_alg_akcipher_close_session(ctx);
0411     }
0412 
0413     /* set ctrl header */
0414     header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION);
0415     header.algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA);
0416     header.queue_id = 0;
0417 
0418     /* set RSA para */
0419     para.algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA);
0420     para.keytype = cpu_to_le32(keytype);
0421     para.keylen = cpu_to_le32(keylen);
0422     para.u.rsa.padding_algo = cpu_to_le32(padding_algo);
0423     para.u.rsa.hash_algo = cpu_to_le32(hash_algo);
0424 
0425     return virtio_crypto_alg_akcipher_init_session(ctx, &header, &para, key, keylen);
0426 }
0427 
0428 static int virtio_crypto_rsa_raw_set_priv_key(struct crypto_akcipher *tfm,
0429                           const void *key,
0430                           unsigned int keylen)
0431 {
0432     return virtio_crypto_rsa_set_key(tfm, key, keylen, 1,
0433                      VIRTIO_CRYPTO_RSA_RAW_PADDING,
0434                      VIRTIO_CRYPTO_RSA_NO_HASH);
0435 }
0436 
0437 
0438 static int virtio_crypto_p1pad_rsa_sha1_set_priv_key(struct crypto_akcipher *tfm,
0439                              const void *key,
0440                              unsigned int keylen)
0441 {
0442     return virtio_crypto_rsa_set_key(tfm, key, keylen, 1,
0443                      VIRTIO_CRYPTO_RSA_PKCS1_PADDING,
0444                      VIRTIO_CRYPTO_RSA_SHA1);
0445 }
0446 
0447 static int virtio_crypto_rsa_raw_set_pub_key(struct crypto_akcipher *tfm,
0448                          const void *key,
0449                          unsigned int keylen)
0450 {
0451     return virtio_crypto_rsa_set_key(tfm, key, keylen, 0,
0452                      VIRTIO_CRYPTO_RSA_RAW_PADDING,
0453                      VIRTIO_CRYPTO_RSA_NO_HASH);
0454 }
0455 
0456 static int virtio_crypto_p1pad_rsa_sha1_set_pub_key(struct crypto_akcipher *tfm,
0457                             const void *key,
0458                             unsigned int keylen)
0459 {
0460     return virtio_crypto_rsa_set_key(tfm, key, keylen, 0,
0461                      VIRTIO_CRYPTO_RSA_PKCS1_PADDING,
0462                      VIRTIO_CRYPTO_RSA_SHA1);
0463 }
0464 
0465 static unsigned int virtio_crypto_rsa_max_size(struct crypto_akcipher *tfm)
0466 {
0467     struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
0468     struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx;
0469 
0470     return mpi_get_size(rsa_ctx->n);
0471 }
0472 
0473 static int virtio_crypto_rsa_init_tfm(struct crypto_akcipher *tfm)
0474 {
0475     struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
0476 
0477     ctx->tfm = tfm;
0478     ctx->enginectx.op.do_one_request = virtio_crypto_rsa_do_req;
0479     ctx->enginectx.op.prepare_request = NULL;
0480     ctx->enginectx.op.unprepare_request = NULL;
0481 
0482     return 0;
0483 }
0484 
0485 static void virtio_crypto_rsa_exit_tfm(struct crypto_akcipher *tfm)
0486 {
0487     struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm);
0488     struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx;
0489 
0490     virtio_crypto_alg_akcipher_close_session(ctx);
0491     virtcrypto_dev_put(ctx->vcrypto);
0492     mpi_free(rsa_ctx->n);
0493     rsa_ctx->n = NULL;
0494 }
0495 
0496 static struct virtio_crypto_akcipher_algo virtio_crypto_akcipher_algs[] = {
0497     {
0498         .algonum = VIRTIO_CRYPTO_AKCIPHER_RSA,
0499         .service = VIRTIO_CRYPTO_SERVICE_AKCIPHER,
0500         .algo = {
0501             .encrypt = virtio_crypto_rsa_encrypt,
0502             .decrypt = virtio_crypto_rsa_decrypt,
0503             .set_pub_key = virtio_crypto_rsa_raw_set_pub_key,
0504             .set_priv_key = virtio_crypto_rsa_raw_set_priv_key,
0505             .max_size = virtio_crypto_rsa_max_size,
0506             .init = virtio_crypto_rsa_init_tfm,
0507             .exit = virtio_crypto_rsa_exit_tfm,
0508             .reqsize = sizeof(struct virtio_crypto_akcipher_request),
0509             .base = {
0510                 .cra_name = "rsa",
0511                 .cra_driver_name = "virtio-crypto-rsa",
0512                 .cra_priority = 150,
0513                 .cra_module = THIS_MODULE,
0514                 .cra_ctxsize = sizeof(struct virtio_crypto_akcipher_ctx),
0515             },
0516         },
0517     },
0518     {
0519         .algonum = VIRTIO_CRYPTO_AKCIPHER_RSA,
0520         .service = VIRTIO_CRYPTO_SERVICE_AKCIPHER,
0521         .algo = {
0522             .encrypt = virtio_crypto_rsa_encrypt,
0523             .decrypt = virtio_crypto_rsa_decrypt,
0524             .sign = virtio_crypto_rsa_sign,
0525             .verify = virtio_crypto_rsa_verify,
0526             .set_pub_key = virtio_crypto_p1pad_rsa_sha1_set_pub_key,
0527             .set_priv_key = virtio_crypto_p1pad_rsa_sha1_set_priv_key,
0528             .max_size = virtio_crypto_rsa_max_size,
0529             .init = virtio_crypto_rsa_init_tfm,
0530             .exit = virtio_crypto_rsa_exit_tfm,
0531             .reqsize = sizeof(struct virtio_crypto_akcipher_request),
0532             .base = {
0533                 .cra_name = "pkcs1pad(rsa,sha1)",
0534                 .cra_driver_name = "virtio-pkcs1-rsa-with-sha1",
0535                 .cra_priority = 150,
0536                 .cra_module = THIS_MODULE,
0537                 .cra_ctxsize = sizeof(struct virtio_crypto_akcipher_ctx),
0538             },
0539         },
0540     },
0541 };
0542 
0543 int virtio_crypto_akcipher_algs_register(struct virtio_crypto *vcrypto)
0544 {
0545     int ret = 0;
0546     int i = 0;
0547 
0548     mutex_lock(&algs_lock);
0549 
0550     for (i = 0; i < ARRAY_SIZE(virtio_crypto_akcipher_algs); i++) {
0551         uint32_t service = virtio_crypto_akcipher_algs[i].service;
0552         uint32_t algonum = virtio_crypto_akcipher_algs[i].algonum;
0553 
0554         if (!virtcrypto_algo_is_supported(vcrypto, service, algonum))
0555             continue;
0556 
0557         if (virtio_crypto_akcipher_algs[i].active_devs == 0) {
0558             ret = crypto_register_akcipher(&virtio_crypto_akcipher_algs[i].algo);
0559             if (ret)
0560                 goto unlock;
0561         }
0562 
0563         virtio_crypto_akcipher_algs[i].active_devs++;
0564         dev_info(&vcrypto->vdev->dev, "Registered akcipher algo %s\n",
0565              virtio_crypto_akcipher_algs[i].algo.base.cra_name);
0566     }
0567 
0568 unlock:
0569     mutex_unlock(&algs_lock);
0570     return ret;
0571 }
0572 
0573 void virtio_crypto_akcipher_algs_unregister(struct virtio_crypto *vcrypto)
0574 {
0575     int i = 0;
0576 
0577     mutex_lock(&algs_lock);
0578 
0579     for (i = 0; i < ARRAY_SIZE(virtio_crypto_akcipher_algs); i++) {
0580         uint32_t service = virtio_crypto_akcipher_algs[i].service;
0581         uint32_t algonum = virtio_crypto_akcipher_algs[i].algonum;
0582 
0583         if (virtio_crypto_akcipher_algs[i].active_devs == 0 ||
0584             !virtcrypto_algo_is_supported(vcrypto, service, algonum))
0585             continue;
0586 
0587         if (virtio_crypto_akcipher_algs[i].active_devs == 1)
0588             crypto_unregister_akcipher(&virtio_crypto_akcipher_algs[i].algo);
0589 
0590         virtio_crypto_akcipher_algs[i].active_devs--;
0591     }
0592 
0593     mutex_unlock(&algs_lock);
0594 }