GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / crypto / virtio / virtio_crypto_algs.c
1  /* Algorithms supported by virtio crypto device
2   *
3   * Authors: Gonglei <arei.gonglei@huawei.com>
4   *
5   * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
6   *
7   * This program is free software; you can redistribute it and/or modify
8   * it under the terms of the GNU General Public License as published by
9   * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, see <http://www.gnu.org/licenses/>.
19   */
20
21 #include <linux/scatterlist.h>
22 #include <crypto/algapi.h>
23 #include <linux/err.h>
24 #include <crypto/scatterwalk.h>
25 #include <linux/atomic.h>
26
27 #include <uapi/linux/virtio_crypto.h>
28 #include "virtio_crypto_common.h"
29
30
31 struct virtio_crypto_ablkcipher_ctx {
32         struct virtio_crypto *vcrypto;
33         struct crypto_tfm *tfm;
34
35         struct virtio_crypto_sym_session_info enc_sess_info;
36         struct virtio_crypto_sym_session_info dec_sess_info;
37 };
38
39 struct virtio_crypto_sym_request {
40         struct virtio_crypto_request base;
41
42         /* Cipher or aead */
43         uint32_t type;
44         struct virtio_crypto_ablkcipher_ctx *ablkcipher_ctx;
45         struct ablkcipher_request *ablkcipher_req;
46         uint8_t *iv;
47         /* Encryption? */
48         bool encrypt;
49 };
50
51 /*
52  * The algs_lock protects the below global virtio_crypto_active_devs
53  * and crypto algorithms registion.
54  */
55 static DEFINE_MUTEX(algs_lock);
56 static unsigned int virtio_crypto_active_devs;
57 static void virtio_crypto_ablkcipher_finalize_req(
58         struct virtio_crypto_sym_request *vc_sym_req,
59         struct ablkcipher_request *req,
60         int err);
61
62 static void virtio_crypto_dataq_sym_callback
63                 (struct virtio_crypto_request *vc_req, int len)
64 {
65         struct virtio_crypto_sym_request *vc_sym_req =
66                 container_of(vc_req, struct virtio_crypto_sym_request, base);
67         struct ablkcipher_request *ablk_req;
68         int error;
69
70         /* Finish the encrypt or decrypt process */
71         if (vc_sym_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
72                 switch (vc_req->status) {
73                 case VIRTIO_CRYPTO_OK:
74                         error = 0;
75                         break;
76                 case VIRTIO_CRYPTO_INVSESS:
77                 case VIRTIO_CRYPTO_ERR:
78                         error = -EINVAL;
79                         break;
80                 case VIRTIO_CRYPTO_BADMSG:
81                         error = -EBADMSG;
82                         break;
83                 default:
84                         error = -EIO;
85                         break;
86                 }
87                 ablk_req = vc_sym_req->ablkcipher_req;
88                 virtio_crypto_ablkcipher_finalize_req(vc_sym_req,
89                                                         ablk_req, error);
90         }
91 }
92
93 static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg)
94 {
95         u64 total = 0;
96
97         for (total = 0; sg; sg = sg_next(sg))
98                 total += sg->length;
99
100         return total;
101 }
102
103 static int
104 virtio_crypto_alg_validate_key(int key_len, uint32_t *alg)
105 {
106         switch (key_len) {
107         case AES_KEYSIZE_128:
108         case AES_KEYSIZE_192:
109         case AES_KEYSIZE_256:
110                 *alg = VIRTIO_CRYPTO_CIPHER_AES_CBC;
111                 break;
112         default:
113                 return -EINVAL;
114         }
115         return 0;
116 }
117
118 static int virtio_crypto_alg_ablkcipher_init_session(
119                 struct virtio_crypto_ablkcipher_ctx *ctx,
120                 uint32_t alg, const uint8_t *key,
121                 unsigned int keylen,
122                 int encrypt)
123 {
124         struct scatterlist outhdr, key_sg, inhdr, *sgs[3];
125         unsigned int tmp;
126         struct virtio_crypto *vcrypto = ctx->vcrypto;
127         int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT;
128         int err;
129         unsigned int num_out = 0, num_in = 0;
130
131         /*
132          * Avoid to do DMA from the stack, switch to using
133          * dynamically-allocated for the key
134          */
135         uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC);
136
137         if (!cipher_key)
138                 return -ENOMEM;
139
140         memcpy(cipher_key, key, keylen);
141
142         spin_lock(&vcrypto->ctrl_lock);
143         /* Pad ctrl header */
144         vcrypto->ctrl.header.opcode =
145                 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
146         vcrypto->ctrl.header.algo = cpu_to_le32(alg);
147         /* Set the default dataqueue id to 0 */
148         vcrypto->ctrl.header.queue_id = 0;
149
150         vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
151         /* Pad cipher's parameters */
152         vcrypto->ctrl.u.sym_create_session.op_type =
153                 cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
154         vcrypto->ctrl.u.sym_create_session.u.cipher.para.algo =
155                 vcrypto->ctrl.header.algo;
156         vcrypto->ctrl.u.sym_create_session.u.cipher.para.keylen =
157                 cpu_to_le32(keylen);
158         vcrypto->ctrl.u.sym_create_session.u.cipher.para.op =
159                 cpu_to_le32(op);
160
161         sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
162         sgs[num_out++] = &outhdr;
163
164         /* Set key */
165         sg_init_one(&key_sg, cipher_key, keylen);
166         sgs[num_out++] = &key_sg;
167
168         /* Return status and session id back */
169         sg_init_one(&inhdr, &vcrypto->input, sizeof(vcrypto->input));
170         sgs[num_out + num_in++] = &inhdr;
171
172         err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
173                                 num_in, vcrypto, GFP_ATOMIC);
174         if (err < 0) {
175                 spin_unlock(&vcrypto->ctrl_lock);
176                 kzfree(cipher_key);
177                 return err;
178         }
179         virtqueue_kick(vcrypto->ctrl_vq);
180
181         /*
182          * Trapping into the hypervisor, so the request should be
183          * handled immediately.
184          */
185         while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
186                !virtqueue_is_broken(vcrypto->ctrl_vq))
187                 cpu_relax();
188
189         if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) {
190                 spin_unlock(&vcrypto->ctrl_lock);
191                 pr_err("virtio_crypto: Create session failed status: %u\n",
192                         le32_to_cpu(vcrypto->input.status));
193                 kzfree(cipher_key);
194                 return -EINVAL;
195         }
196
197         if (encrypt)
198                 ctx->enc_sess_info.session_id =
199                         le64_to_cpu(vcrypto->input.session_id);
200         else
201                 ctx->dec_sess_info.session_id =
202                         le64_to_cpu(vcrypto->input.session_id);
203
204         spin_unlock(&vcrypto->ctrl_lock);
205
206         kzfree(cipher_key);
207         return 0;
208 }
209
210 static int virtio_crypto_alg_ablkcipher_close_session(
211                 struct virtio_crypto_ablkcipher_ctx *ctx,
212                 int encrypt)
213 {
214         struct scatterlist outhdr, status_sg, *sgs[2];
215         unsigned int tmp;
216         struct virtio_crypto_destroy_session_req *destroy_session;
217         struct virtio_crypto *vcrypto = ctx->vcrypto;
218         int err;
219         unsigned int num_out = 0, num_in = 0;
220
221         spin_lock(&vcrypto->ctrl_lock);
222         vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR;
223         /* Pad ctrl header */
224         vcrypto->ctrl.header.opcode =
225                 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
226         /* Set the default virtqueue id to 0 */
227         vcrypto->ctrl.header.queue_id = 0;
228
229         destroy_session = &vcrypto->ctrl.u.destroy_session;
230
231         if (encrypt)
232                 destroy_session->session_id =
233                         cpu_to_le64(ctx->enc_sess_info.session_id);
234         else
235                 destroy_session->session_id =
236                         cpu_to_le64(ctx->dec_sess_info.session_id);
237
238         sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
239         sgs[num_out++] = &outhdr;
240
241         /* Return status and session id back */
242         sg_init_one(&status_sg, &vcrypto->ctrl_status.status,
243                 sizeof(vcrypto->ctrl_status.status));
244         sgs[num_out + num_in++] = &status_sg;
245
246         err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
247                         num_in, vcrypto, GFP_ATOMIC);
248         if (err < 0) {
249                 spin_unlock(&vcrypto->ctrl_lock);
250                 return err;
251         }
252         virtqueue_kick(vcrypto->ctrl_vq);
253
254         while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
255                !virtqueue_is_broken(vcrypto->ctrl_vq))
256                 cpu_relax();
257
258         if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) {
259                 spin_unlock(&vcrypto->ctrl_lock);
260                 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
261                         vcrypto->ctrl_status.status,
262                         destroy_session->session_id);
263
264                 return -EINVAL;
265         }
266         spin_unlock(&vcrypto->ctrl_lock);
267
268         return 0;
269 }
270
271 static int virtio_crypto_alg_ablkcipher_init_sessions(
272                 struct virtio_crypto_ablkcipher_ctx *ctx,
273                 const uint8_t *key, unsigned int keylen)
274 {
275         uint32_t alg;
276         int ret;
277         struct virtio_crypto *vcrypto = ctx->vcrypto;
278
279         if (keylen > vcrypto->max_cipher_key_len) {
280                 pr_err("virtio_crypto: the key is too long\n");
281                 goto bad_key;
282         }
283
284         if (virtio_crypto_alg_validate_key(keylen, &alg))
285                 goto bad_key;
286
287         /* Create encryption session */
288         ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
289                         alg, key, keylen, 1);
290         if (ret)
291                 return ret;
292         /* Create decryption session */
293         ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
294                         alg, key, keylen, 0);
295         if (ret) {
296                 virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
297                 return ret;
298         }
299         return 0;
300
301 bad_key:
302         crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
303         return -EINVAL;
304 }
305
306 /* Note: kernel crypto API realization */
307 static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
308                                          const uint8_t *key,
309                                          unsigned int keylen)
310 {
311         struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(tfm);
312         int ret;
313
314         if (!ctx->vcrypto) {
315                 /* New key */
316                 int node = virtio_crypto_get_current_node();
317                 struct virtio_crypto *vcrypto =
318                                       virtcrypto_get_dev_node(node);
319                 if (!vcrypto) {
320                         pr_err("virtio_crypto: Could not find a virtio device in the system");
321                         return -ENODEV;
322                 }
323
324                 ctx->vcrypto = vcrypto;
325         } else {
326                 /* Rekeying, we should close the created sessions previously */
327                 virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
328                 virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
329         }
330
331         ret = virtio_crypto_alg_ablkcipher_init_sessions(ctx, key, keylen);
332         if (ret) {
333                 virtcrypto_dev_put(ctx->vcrypto);
334                 ctx->vcrypto = NULL;
335
336                 return ret;
337         }
338
339         return 0;
340 }
341
342 static int
343 __virtio_crypto_ablkcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req,
344                 struct ablkcipher_request *req,
345                 struct data_queue *data_vq)
346 {
347         struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
348         struct virtio_crypto_ablkcipher_ctx *ctx = vc_sym_req->ablkcipher_ctx;
349         struct virtio_crypto_request *vc_req = &vc_sym_req->base;
350         unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
351         struct virtio_crypto *vcrypto = ctx->vcrypto;
352         struct virtio_crypto_op_data_req *req_data;
353         int src_nents, dst_nents;
354         int err;
355         unsigned long flags;
356         struct scatterlist outhdr, iv_sg, status_sg, **sgs;
357         u64 dst_len;
358         unsigned int num_out = 0, num_in = 0;
359         int sg_total;
360         uint8_t *iv;
361         struct scatterlist *sg;
362
363         src_nents = sg_nents_for_len(req->src, req->nbytes);
364         if (src_nents < 0) {
365                 pr_err("Invalid number of src SG.\n");
366                 return src_nents;
367         }
368
369         dst_nents = sg_nents(req->dst);
370
371         pr_debug("virtio_crypto: Number of sgs (src_nents: %d, dst_nents: %d)\n",
372                         src_nents, dst_nents);
373
374         /* Why 3?  outhdr + iv + inhdr */
375         sg_total = src_nents + dst_nents + 3;
376         sgs = kzalloc_node(sg_total * sizeof(*sgs), GFP_ATOMIC,
377                                 dev_to_node(&vcrypto->vdev->dev));
378         if (!sgs)
379                 return -ENOMEM;
380
381         req_data = kzalloc_node(sizeof(*req_data), GFP_ATOMIC,
382                                 dev_to_node(&vcrypto->vdev->dev));
383         if (!req_data) {
384                 kfree(sgs);
385                 return -ENOMEM;
386         }
387
388         vc_req->req_data = req_data;
389         vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
390         /* Head of operation */
391         if (vc_sym_req->encrypt) {
392                 req_data->header.session_id =
393                         cpu_to_le64(ctx->enc_sess_info.session_id);
394                 req_data->header.opcode =
395                         cpu_to_le32(VIRTIO_CRYPTO_CIPHER_ENCRYPT);
396         } else {
397                 req_data->header.session_id =
398                         cpu_to_le64(ctx->dec_sess_info.session_id);
399             req_data->header.opcode =
400                         cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT);
401         }
402         req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
403         req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize);
404         req_data->u.sym_req.u.cipher.para.src_data_len =
405                         cpu_to_le32(req->nbytes);
406
407         dst_len = virtio_crypto_alg_sg_nents_length(req->dst);
408         if (unlikely(dst_len > U32_MAX)) {
409                 pr_err("virtio_crypto: The dst_len is beyond U32_MAX\n");
410                 err = -EINVAL;
411                 goto free;
412         }
413
414         dst_len = min_t(unsigned int, req->nbytes, dst_len);
415         pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n",
416                         req->nbytes, dst_len);
417
418         if (unlikely(req->nbytes + dst_len + ivsize +
419                 sizeof(vc_req->status) > vcrypto->max_size)) {
420                 pr_err("virtio_crypto: The length is too big\n");
421                 err = -EINVAL;
422                 goto free;
423         }
424
425         req_data->u.sym_req.u.cipher.para.dst_data_len =
426                         cpu_to_le32((uint32_t)dst_len);
427
428         /* Outhdr */
429         sg_init_one(&outhdr, req_data, sizeof(*req_data));
430         sgs[num_out++] = &outhdr;
431
432         /* IV */
433
434         /*
435          * Avoid to do DMA from the stack, switch to using
436          * dynamically-allocated for the IV
437          */
438         iv = kzalloc_node(ivsize, GFP_ATOMIC,
439                                 dev_to_node(&vcrypto->vdev->dev));
440         if (!iv) {
441                 err = -ENOMEM;
442                 goto free;
443         }
444         memcpy(iv, req->info, ivsize);
445         sg_init_one(&iv_sg, iv, ivsize);
446         sgs[num_out++] = &iv_sg;
447         vc_sym_req->iv = iv;
448
449         /* Source data */
450         for (sg = req->src; src_nents; sg = sg_next(sg), src_nents--)
451                 sgs[num_out++] = sg;
452
453         /* Destination data */
454         for (sg = req->dst; sg; sg = sg_next(sg))
455                 sgs[num_out + num_in++] = sg;
456
457         /* Status */
458         sg_init_one(&status_sg, &vc_req->status, sizeof(vc_req->status));
459         sgs[num_out + num_in++] = &status_sg;
460
461         vc_req->sgs = sgs;
462
463         spin_lock_irqsave(&data_vq->lock, flags);
464         err = virtqueue_add_sgs(data_vq->vq, sgs, num_out,
465                                 num_in, vc_req, GFP_ATOMIC);
466         virtqueue_kick(data_vq->vq);
467         spin_unlock_irqrestore(&data_vq->lock, flags);
468         if (unlikely(err < 0))
469                 goto free_iv;
470
471         return 0;
472
473 free_iv:
474         kzfree(iv);
475 free:
476         kzfree(req_data);
477         kfree(sgs);
478         return err;
479 }
480
481 static int virtio_crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
482 {
483         struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
484         struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
485         struct virtio_crypto_sym_request *vc_sym_req =
486                                 ablkcipher_request_ctx(req);
487         struct virtio_crypto_request *vc_req = &vc_sym_req->base;
488         struct virtio_crypto *vcrypto = ctx->vcrypto;
489         /* Use the first data virtqueue as default */
490         struct data_queue *data_vq = &vcrypto->data_vq[0];
491
492         if (!req->nbytes)
493                 return 0;
494         if (req->nbytes % AES_BLOCK_SIZE)
495                 return -EINVAL;
496
497         vc_req->dataq = data_vq;
498         vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
499         vc_sym_req->ablkcipher_ctx = ctx;
500         vc_sym_req->ablkcipher_req = req;
501         vc_sym_req->encrypt = true;
502
503         return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
504 }
505
506 static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
507 {
508         struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
509         struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
510         struct virtio_crypto_sym_request *vc_sym_req =
511                                 ablkcipher_request_ctx(req);
512         struct virtio_crypto_request *vc_req = &vc_sym_req->base;
513         struct virtio_crypto *vcrypto = ctx->vcrypto;
514         /* Use the first data virtqueue as default */
515         struct data_queue *data_vq = &vcrypto->data_vq[0];
516
517         if (!req->nbytes)
518                 return 0;
519         if (req->nbytes % AES_BLOCK_SIZE)
520                 return -EINVAL;
521
522         vc_req->dataq = data_vq;
523         vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
524         vc_sym_req->ablkcipher_ctx = ctx;
525         vc_sym_req->ablkcipher_req = req;
526         vc_sym_req->encrypt = false;
527
528         return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
529 }
530
531 static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm)
532 {
533         struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
534
535         tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_sym_request);
536         ctx->tfm = tfm;
537
538         return 0;
539 }
540
541 static void virtio_crypto_ablkcipher_exit(struct crypto_tfm *tfm)
542 {
543         struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
544
545         if (!ctx->vcrypto)
546                 return;
547
548         virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
549         virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
550         virtcrypto_dev_put(ctx->vcrypto);
551         ctx->vcrypto = NULL;
552 }
553
554 int virtio_crypto_ablkcipher_crypt_req(
555         struct crypto_engine *engine,
556         struct ablkcipher_request *req)
557 {
558         struct virtio_crypto_sym_request *vc_sym_req =
559                                 ablkcipher_request_ctx(req);
560         struct virtio_crypto_request *vc_req = &vc_sym_req->base;
561         struct data_queue *data_vq = vc_req->dataq;
562         int ret;
563
564         ret = __virtio_crypto_ablkcipher_do_req(vc_sym_req, req, data_vq);
565         if (ret < 0)
566                 return ret;
567
568         virtqueue_kick(data_vq->vq);
569
570         return 0;
571 }
572
573 static void virtio_crypto_ablkcipher_finalize_req(
574         struct virtio_crypto_sym_request *vc_sym_req,
575         struct ablkcipher_request *req,
576         int err)
577 {
578         kzfree(vc_sym_req->iv);
579         virtcrypto_clear_request(&vc_sym_req->base);
580
581         crypto_finalize_cipher_request(vc_sym_req->base.dataq->engine,
582                                            req, err);
583 }
584
585 static struct crypto_alg virtio_crypto_algs[] = { {
586         .cra_name = "cbc(aes)",
587         .cra_driver_name = "virtio_crypto_aes_cbc",
588         .cra_priority = 150,
589         .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
590         .cra_blocksize = AES_BLOCK_SIZE,
591         .cra_ctxsize  = sizeof(struct virtio_crypto_ablkcipher_ctx),
592         .cra_alignmask = 0,
593         .cra_module = THIS_MODULE,
594         .cra_type = &crypto_ablkcipher_type,
595         .cra_init = virtio_crypto_ablkcipher_init,
596         .cra_exit = virtio_crypto_ablkcipher_exit,
597         .cra_u = {
598            .ablkcipher = {
599                         .setkey = virtio_crypto_ablkcipher_setkey,
600                         .decrypt = virtio_crypto_ablkcipher_decrypt,
601                         .encrypt = virtio_crypto_ablkcipher_encrypt,
602                         .min_keysize = AES_MIN_KEY_SIZE,
603                         .max_keysize = AES_MAX_KEY_SIZE,
604                         .ivsize = AES_BLOCK_SIZE,
605                 },
606         },
607 } };
608
609 int virtio_crypto_algs_register(void)
610 {
611         int ret = 0;
612
613         mutex_lock(&algs_lock);
614         if (++virtio_crypto_active_devs != 1)
615                 goto unlock;
616
617         ret = crypto_register_algs(virtio_crypto_algs,
618                         ARRAY_SIZE(virtio_crypto_algs));
619         if (ret)
620                 virtio_crypto_active_devs--;
621
622 unlock:
623         mutex_unlock(&algs_lock);
624         return ret;
625 }
626
627 void virtio_crypto_algs_unregister(void)
628 {
629         mutex_lock(&algs_lock);
630         if (--virtio_crypto_active_devs != 0)
631                 goto unlock;
632
633         crypto_unregister_algs(virtio_crypto_algs,
634                         ARRAY_SIZE(virtio_crypto_algs));
635
636 unlock:
637         mutex_unlock(&algs_lock);
638 }