1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Asymmetric algorithms supported by virtio crypto device 3 * 4 * Authors: zhenwei pi <pizhenwei@bytedance.com> 5 * lei he <helei.sig11@bytedance.com> 6 * 7 * Copyright 2022 Bytedance CO., LTD. 8 */ 9 10 #include <linux/mpi.h> 11 #include <linux/scatterlist.h> 12 #include <crypto/algapi.h> 13 #include <crypto/internal/akcipher.h> 14 #include <crypto/internal/rsa.h> 15 #include <linux/err.h> 16 #include <crypto/scatterwalk.h> 17 #include <linux/atomic.h> 18 19 #include <uapi/linux/virtio_crypto.h> 20 #include "virtio_crypto_common.h" 21 22 struct virtio_crypto_rsa_ctx { 23 MPI n; 24 }; 25 26 struct virtio_crypto_akcipher_ctx { 27 struct crypto_engine_ctx enginectx; 28 struct virtio_crypto *vcrypto; 29 struct crypto_akcipher *tfm; 30 bool session_valid; 31 __u64 session_id; 32 union { 33 struct virtio_crypto_rsa_ctx rsa_ctx; 34 }; 35 }; 36 37 struct virtio_crypto_akcipher_request { 38 struct virtio_crypto_request base; 39 struct virtio_crypto_akcipher_ctx *akcipher_ctx; 40 struct akcipher_request *akcipher_req; 41 void *src_buf; 42 void *dst_buf; 43 uint32_t opcode; 44 }; 45 46 struct virtio_crypto_akcipher_algo { 47 uint32_t algonum; 48 uint32_t service; 49 unsigned int active_devs; 50 struct akcipher_alg algo; 51 }; 52 53 static DEFINE_MUTEX(algs_lock); 54 55 static void virtio_crypto_akcipher_finalize_req( 56 struct virtio_crypto_akcipher_request *vc_akcipher_req, 57 struct akcipher_request *req, int err) 58 { 59 kfree(vc_akcipher_req->src_buf); 60 kfree(vc_akcipher_req->dst_buf); 61 vc_akcipher_req->src_buf = NULL; 62 vc_akcipher_req->dst_buf = NULL; 63 virtcrypto_clear_request(&vc_akcipher_req->base); 64 65 crypto_finalize_akcipher_request(vc_akcipher_req->base.dataq->engine, req, err); 66 } 67 68 static void virtio_crypto_dataq_akcipher_callback(struct virtio_crypto_request *vc_req, int len) 69 { 70 struct virtio_crypto_akcipher_request *vc_akcipher_req = 71 container_of(vc_req, struct virtio_crypto_akcipher_request, base); 72 struct akcipher_request *akcipher_req; 73 int error; 74 75 switch (vc_req->status) { 76 case VIRTIO_CRYPTO_OK: 77 error = 0; 78 break; 79 case VIRTIO_CRYPTO_INVSESS: 80 case VIRTIO_CRYPTO_ERR: 81 error = -EINVAL; 82 break; 83 case VIRTIO_CRYPTO_BADMSG: 84 error = -EBADMSG; 85 break; 86 87 case VIRTIO_CRYPTO_KEY_REJECTED: 88 error = -EKEYREJECTED; 89 break; 90 91 default: 92 error = -EIO; 93 break; 94 } 95 96 akcipher_req = vc_akcipher_req->akcipher_req; 97 if (vc_akcipher_req->opcode != VIRTIO_CRYPTO_AKCIPHER_VERIFY) { 98 /* actuall length maybe less than dst buffer */ 99 akcipher_req->dst_len = len - sizeof(vc_req->status); 100 sg_copy_from_buffer(akcipher_req->dst, sg_nents(akcipher_req->dst), 101 vc_akcipher_req->dst_buf, akcipher_req->dst_len); 102 } 103 virtio_crypto_akcipher_finalize_req(vc_akcipher_req, akcipher_req, error); 104 } 105 106 static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher_ctx *ctx, 107 struct virtio_crypto_ctrl_header *header, void *para, 108 const uint8_t *key, unsigned int keylen) 109 { 110 struct scatterlist outhdr_sg, key_sg, inhdr_sg, *sgs[3]; 111 struct virtio_crypto *vcrypto = ctx->vcrypto; 112 uint8_t *pkey; 113 int err; 114 unsigned int num_out = 0, num_in = 0; 115 struct virtio_crypto_op_ctrl_req *ctrl; 116 struct virtio_crypto_session_input *input; 117 struct virtio_crypto_ctrl_request *vc_ctrl_req; 118 119 pkey = kmemdup(key, keylen, GFP_KERNEL); 120 if (!pkey) 121 return -ENOMEM; 122 123 vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL); 124 if (!vc_ctrl_req) { 125 err = -ENOMEM; 126 goto out; 127 } 128 129 ctrl = &vc_ctrl_req->ctrl; 130 memcpy(&ctrl->header, header, sizeof(ctrl->header)); 131 memcpy(&ctrl->u, para, sizeof(ctrl->u)); 132 input = &vc_ctrl_req->input; 133 input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR); 134 135 sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl)); 136 sgs[num_out++] = &outhdr_sg; 137 138 sg_init_one(&key_sg, pkey, keylen); 139 sgs[num_out++] = &key_sg; 140 141 sg_init_one(&inhdr_sg, input, sizeof(*input)); 142 sgs[num_out + num_in++] = &inhdr_sg; 143 144 err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req); 145 if (err < 0) 146 goto out; 147 148 if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) { 149 pr_err("virtio_crypto: Create session failed status: %u\n", 150 le32_to_cpu(input->status)); 151 err = -EINVAL; 152 goto out; 153 } 154 155 ctx->session_id = le64_to_cpu(input->session_id); 156 ctx->session_valid = true; 157 err = 0; 158 159 out: 160 kfree(vc_ctrl_req); 161 kfree_sensitive(pkey); 162 163 return err; 164 } 165 166 static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akcipher_ctx *ctx) 167 { 168 struct scatterlist outhdr_sg, inhdr_sg, *sgs[2]; 169 struct virtio_crypto_destroy_session_req *destroy_session; 170 struct virtio_crypto *vcrypto = ctx->vcrypto; 171 unsigned int num_out = 0, num_in = 0; 172 int err; 173 struct virtio_crypto_op_ctrl_req *ctrl; 174 struct virtio_crypto_inhdr *ctrl_status; 175 struct virtio_crypto_ctrl_request *vc_ctrl_req; 176 177 if (!ctx->session_valid) 178 return 0; 179 180 vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL); 181 if (!vc_ctrl_req) 182 return -ENOMEM; 183 184 ctrl_status = &vc_ctrl_req->ctrl_status; 185 ctrl_status->status = VIRTIO_CRYPTO_ERR; 186 ctrl = &vc_ctrl_req->ctrl; 187 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION); 188 ctrl->header.queue_id = 0; 189 190 destroy_session = &ctrl->u.destroy_session; 191 destroy_session->session_id = cpu_to_le64(ctx->session_id); 192 193 sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl)); 194 sgs[num_out++] = &outhdr_sg; 195 196 sg_init_one(&inhdr_sg, &ctrl_status->status, sizeof(ctrl_status->status)); 197 sgs[num_out + num_in++] = &inhdr_sg; 198 199 err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req); 200 if (err < 0) 201 goto out; 202 203 if (ctrl_status->status != VIRTIO_CRYPTO_OK) { 204 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n", 205 ctrl_status->status, destroy_session->session_id); 206 err = -EINVAL; 207 goto out; 208 } 209 210 err = 0; 211 ctx->session_valid = false; 212 213 out: 214 kfree(vc_ctrl_req); 215 216 return err; 217 } 218 219 static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request *vc_akcipher_req, 220 struct akcipher_request *req, struct data_queue *data_vq) 221 { 222 struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx; 223 struct virtio_crypto_request *vc_req = &vc_akcipher_req->base; 224 struct virtio_crypto *vcrypto = ctx->vcrypto; 225 struct virtio_crypto_op_data_req *req_data = vc_req->req_data; 226 struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg; 227 void *src_buf = NULL, *dst_buf = NULL; 228 unsigned int num_out = 0, num_in = 0; 229 int node = dev_to_node(&vcrypto->vdev->dev); 230 unsigned long flags; 231 int ret = -ENOMEM; 232 bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY; 233 unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len; 234 235 /* out header */ 236 sg_init_one(&outhdr_sg, req_data, sizeof(*req_data)); 237 sgs[num_out++] = &outhdr_sg; 238 239 /* src data */ 240 src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node); 241 if (!src_buf) 242 goto err; 243 244 if (verify) { 245 /* for verify operation, both src and dst data work as OUT direction */ 246 sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len); 247 sg_init_one(&srcdata_sg, src_buf, src_len); 248 sgs[num_out++] = &srcdata_sg; 249 } else { 250 sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len); 251 sg_init_one(&srcdata_sg, src_buf, src_len); 252 sgs[num_out++] = &srcdata_sg; 253 254 /* dst data */ 255 dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node); 256 if (!dst_buf) 257 goto err; 258 259 sg_init_one(&dstdata_sg, dst_buf, req->dst_len); 260 sgs[num_out + num_in++] = &dstdata_sg; 261 } 262 263 vc_akcipher_req->src_buf = src_buf; 264 vc_akcipher_req->dst_buf = dst_buf; 265 266 /* in header */ 267 sg_init_one(&inhdr_sg, &vc_req->status, sizeof(vc_req->status)); 268 sgs[num_out + num_in++] = &inhdr_sg; 269 270 spin_lock_irqsave(&data_vq->lock, flags); 271 ret = virtqueue_add_sgs(data_vq->vq, sgs, num_out, num_in, vc_req, GFP_ATOMIC); 272 virtqueue_kick(data_vq->vq); 273 spin_unlock_irqrestore(&data_vq->lock, flags); 274 if (ret) 275 goto err; 276 277 return 0; 278 279 err: 280 kfree(src_buf); 281 kfree(dst_buf); 282 283 return -ENOMEM; 284 } 285 286 static int virtio_crypto_rsa_do_req(struct crypto_engine *engine, void *vreq) 287 { 288 struct akcipher_request *req = container_of(vreq, struct akcipher_request, base); 289 struct virtio_crypto_akcipher_request *vc_akcipher_req = akcipher_request_ctx(req); 290 struct virtio_crypto_request *vc_req = &vc_akcipher_req->base; 291 struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx; 292 struct virtio_crypto *vcrypto = ctx->vcrypto; 293 struct data_queue *data_vq = vc_req->dataq; 294 struct virtio_crypto_op_header *header; 295 struct virtio_crypto_akcipher_data_req *akcipher_req; 296 int ret; 297 298 vc_req->sgs = NULL; 299 vc_req->req_data = kzalloc_node(sizeof(*vc_req->req_data), 300 GFP_KERNEL, dev_to_node(&vcrypto->vdev->dev)); 301 if (!vc_req->req_data) 302 return -ENOMEM; 303 304 /* build request header */ 305 header = &vc_req->req_data->header; 306 header->opcode = cpu_to_le32(vc_akcipher_req->opcode); 307 header->algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA); 308 header->session_id = cpu_to_le64(ctx->session_id); 309 310 /* build request akcipher data */ 311 akcipher_req = &vc_req->req_data->u.akcipher_req; 312 akcipher_req->para.src_data_len = cpu_to_le32(req->src_len); 313 akcipher_req->para.dst_data_len = cpu_to_le32(req->dst_len); 314 315 ret = __virtio_crypto_akcipher_do_req(vc_akcipher_req, req, data_vq); 316 if (ret < 0) { 317 kfree_sensitive(vc_req->req_data); 318 vc_req->req_data = NULL; 319 return ret; 320 } 321 322 return 0; 323 } 324 325 static int virtio_crypto_rsa_req(struct akcipher_request *req, uint32_t opcode) 326 { 327 struct crypto_akcipher *atfm = crypto_akcipher_reqtfm(req); 328 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(atfm); 329 struct virtio_crypto_akcipher_request *vc_akcipher_req = akcipher_request_ctx(req); 330 struct virtio_crypto_request *vc_req = &vc_akcipher_req->base; 331 struct virtio_crypto *vcrypto = ctx->vcrypto; 332 /* Use the first data virtqueue as default */ 333 struct data_queue *data_vq = &vcrypto->data_vq[0]; 334 335 vc_req->dataq = data_vq; 336 vc_req->alg_cb = virtio_crypto_dataq_akcipher_callback; 337 vc_akcipher_req->akcipher_ctx = ctx; 338 vc_akcipher_req->akcipher_req = req; 339 vc_akcipher_req->opcode = opcode; 340 341 return crypto_transfer_akcipher_request_to_engine(data_vq->engine, req); 342 } 343 344 static int virtio_crypto_rsa_encrypt(struct akcipher_request *req) 345 { 346 return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_ENCRYPT); 347 } 348 349 static int virtio_crypto_rsa_decrypt(struct akcipher_request *req) 350 { 351 return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_DECRYPT); 352 } 353 354 static int virtio_crypto_rsa_sign(struct akcipher_request *req) 355 { 356 return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_SIGN); 357 } 358 359 static int virtio_crypto_rsa_verify(struct akcipher_request *req) 360 { 361 return virtio_crypto_rsa_req(req, VIRTIO_CRYPTO_AKCIPHER_VERIFY); 362 } 363 364 static int virtio_crypto_rsa_set_key(struct crypto_akcipher *tfm, 365 const void *key, 366 unsigned int keylen, 367 bool private, 368 int padding_algo, 369 int hash_algo) 370 { 371 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm); 372 struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx; 373 struct virtio_crypto *vcrypto; 374 struct virtio_crypto_ctrl_header header; 375 struct virtio_crypto_akcipher_session_para para; 376 struct rsa_key rsa_key = {0}; 377 int node = virtio_crypto_get_current_node(); 378 uint32_t keytype; 379 int ret; 380 381 /* mpi_free will test n, just free it. */ 382 mpi_free(rsa_ctx->n); 383 rsa_ctx->n = NULL; 384 385 if (private) { 386 keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE; 387 ret = rsa_parse_priv_key(&rsa_key, key, keylen); 388 } else { 389 keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC; 390 ret = rsa_parse_pub_key(&rsa_key, key, keylen); 391 } 392 393 if (ret) 394 return ret; 395 396 rsa_ctx->n = mpi_read_raw_data(rsa_key.n, rsa_key.n_sz); 397 if (!rsa_ctx->n) 398 return -ENOMEM; 399 400 if (!ctx->vcrypto) { 401 vcrypto = virtcrypto_get_dev_node(node, VIRTIO_CRYPTO_SERVICE_AKCIPHER, 402 VIRTIO_CRYPTO_AKCIPHER_RSA); 403 if (!vcrypto) { 404 pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n"); 405 return -ENODEV; 406 } 407 408 ctx->vcrypto = vcrypto; 409 } else { 410 virtio_crypto_alg_akcipher_close_session(ctx); 411 } 412 413 /* set ctrl header */ 414 header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION); 415 header.algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA); 416 header.queue_id = 0; 417 418 /* set RSA para */ 419 para.algo = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_RSA); 420 para.keytype = cpu_to_le32(keytype); 421 para.keylen = cpu_to_le32(keylen); 422 para.u.rsa.padding_algo = cpu_to_le32(padding_algo); 423 para.u.rsa.hash_algo = cpu_to_le32(hash_algo); 424 425 return virtio_crypto_alg_akcipher_init_session(ctx, &header, ¶, key, keylen); 426 } 427 428 static int virtio_crypto_rsa_raw_set_priv_key(struct crypto_akcipher *tfm, 429 const void *key, 430 unsigned int keylen) 431 { 432 return virtio_crypto_rsa_set_key(tfm, key, keylen, 1, 433 VIRTIO_CRYPTO_RSA_RAW_PADDING, 434 VIRTIO_CRYPTO_RSA_NO_HASH); 435 } 436 437 438 static int virtio_crypto_p1pad_rsa_sha1_set_priv_key(struct crypto_akcipher *tfm, 439 const void *key, 440 unsigned int keylen) 441 { 442 return virtio_crypto_rsa_set_key(tfm, key, keylen, 1, 443 VIRTIO_CRYPTO_RSA_PKCS1_PADDING, 444 VIRTIO_CRYPTO_RSA_SHA1); 445 } 446 447 static int virtio_crypto_rsa_raw_set_pub_key(struct crypto_akcipher *tfm, 448 const void *key, 449 unsigned int keylen) 450 { 451 return virtio_crypto_rsa_set_key(tfm, key, keylen, 0, 452 VIRTIO_CRYPTO_RSA_RAW_PADDING, 453 VIRTIO_CRYPTO_RSA_NO_HASH); 454 } 455 456 static int virtio_crypto_p1pad_rsa_sha1_set_pub_key(struct crypto_akcipher *tfm, 457 const void *key, 458 unsigned int keylen) 459 { 460 return virtio_crypto_rsa_set_key(tfm, key, keylen, 0, 461 VIRTIO_CRYPTO_RSA_PKCS1_PADDING, 462 VIRTIO_CRYPTO_RSA_SHA1); 463 } 464 465 static unsigned int virtio_crypto_rsa_max_size(struct crypto_akcipher *tfm) 466 { 467 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm); 468 struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx; 469 470 return mpi_get_size(rsa_ctx->n); 471 } 472 473 static int virtio_crypto_rsa_init_tfm(struct crypto_akcipher *tfm) 474 { 475 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm); 476 477 ctx->tfm = tfm; 478 ctx->enginectx.op.do_one_request = virtio_crypto_rsa_do_req; 479 ctx->enginectx.op.prepare_request = NULL; 480 ctx->enginectx.op.unprepare_request = NULL; 481 482 akcipher_set_reqsize(tfm, 483 sizeof(struct virtio_crypto_akcipher_request)); 484 485 return 0; 486 } 487 488 static void virtio_crypto_rsa_exit_tfm(struct crypto_akcipher *tfm) 489 { 490 struct virtio_crypto_akcipher_ctx *ctx = akcipher_tfm_ctx(tfm); 491 struct virtio_crypto_rsa_ctx *rsa_ctx = &ctx->rsa_ctx; 492 493 virtio_crypto_alg_akcipher_close_session(ctx); 494 virtcrypto_dev_put(ctx->vcrypto); 495 mpi_free(rsa_ctx->n); 496 rsa_ctx->n = NULL; 497 } 498 499 static struct virtio_crypto_akcipher_algo virtio_crypto_akcipher_algs[] = { 500 { 501 .algonum = VIRTIO_CRYPTO_AKCIPHER_RSA, 502 .service = VIRTIO_CRYPTO_SERVICE_AKCIPHER, 503 .algo = { 504 .encrypt = virtio_crypto_rsa_encrypt, 505 .decrypt = virtio_crypto_rsa_decrypt, 506 .set_pub_key = virtio_crypto_rsa_raw_set_pub_key, 507 .set_priv_key = virtio_crypto_rsa_raw_set_priv_key, 508 .max_size = virtio_crypto_rsa_max_size, 509 .init = virtio_crypto_rsa_init_tfm, 510 .exit = virtio_crypto_rsa_exit_tfm, 511 .base = { 512 .cra_name = "rsa", 513 .cra_driver_name = "virtio-crypto-rsa", 514 .cra_priority = 150, 515 .cra_module = THIS_MODULE, 516 .cra_ctxsize = sizeof(struct virtio_crypto_akcipher_ctx), 517 }, 518 }, 519 }, 520 { 521 .algonum = VIRTIO_CRYPTO_AKCIPHER_RSA, 522 .service = VIRTIO_CRYPTO_SERVICE_AKCIPHER, 523 .algo = { 524 .encrypt = virtio_crypto_rsa_encrypt, 525 .decrypt = virtio_crypto_rsa_decrypt, 526 .sign = virtio_crypto_rsa_sign, 527 .verify = virtio_crypto_rsa_verify, 528 .set_pub_key = virtio_crypto_p1pad_rsa_sha1_set_pub_key, 529 .set_priv_key = virtio_crypto_p1pad_rsa_sha1_set_priv_key, 530 .max_size = virtio_crypto_rsa_max_size, 531 .init = virtio_crypto_rsa_init_tfm, 532 .exit = virtio_crypto_rsa_exit_tfm, 533 .base = { 534 .cra_name = "pkcs1pad(rsa,sha1)", 535 .cra_driver_name = "virtio-pkcs1-rsa-with-sha1", 536 .cra_priority = 150, 537 .cra_module = THIS_MODULE, 538 .cra_ctxsize = sizeof(struct virtio_crypto_akcipher_ctx), 539 }, 540 }, 541 }, 542 }; 543 544 int virtio_crypto_akcipher_algs_register(struct virtio_crypto *vcrypto) 545 { 546 int ret = 0; 547 int i = 0; 548 549 mutex_lock(&algs_lock); 550 551 for (i = 0; i < ARRAY_SIZE(virtio_crypto_akcipher_algs); i++) { 552 uint32_t service = virtio_crypto_akcipher_algs[i].service; 553 uint32_t algonum = virtio_crypto_akcipher_algs[i].algonum; 554 555 if (!virtcrypto_algo_is_supported(vcrypto, service, algonum)) 556 continue; 557 558 if (virtio_crypto_akcipher_algs[i].active_devs == 0) { 559 ret = crypto_register_akcipher(&virtio_crypto_akcipher_algs[i].algo); 560 if (ret) 561 goto unlock; 562 } 563 564 virtio_crypto_akcipher_algs[i].active_devs++; 565 dev_info(&vcrypto->vdev->dev, "Registered akcipher algo %s\n", 566 virtio_crypto_akcipher_algs[i].algo.base.cra_name); 567 } 568 569 unlock: 570 mutex_unlock(&algs_lock); 571 return ret; 572 } 573 574 void virtio_crypto_akcipher_algs_unregister(struct virtio_crypto *vcrypto) 575 { 576 int i = 0; 577 578 mutex_lock(&algs_lock); 579 580 for (i = 0; i < ARRAY_SIZE(virtio_crypto_akcipher_algs); i++) { 581 uint32_t service = virtio_crypto_akcipher_algs[i].service; 582 uint32_t algonum = virtio_crypto_akcipher_algs[i].algonum; 583 584 if (virtio_crypto_akcipher_algs[i].active_devs == 0 || 585 !virtcrypto_algo_is_supported(vcrypto, service, algonum)) 586 continue; 587 588 if (virtio_crypto_akcipher_algs[i].active_devs == 1) 589 crypto_unregister_akcipher(&virtio_crypto_akcipher_algs[i].algo); 590 591 virtio_crypto_akcipher_algs[i].active_devs--; 592 } 593 594 mutex_unlock(&algs_lock); 595 } 596