linux/drivers/crypto/virtio/virtio_crypto_algs.c
<<
>>
Prefs
   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
  31struct 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
  39struct 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 */
  55static DEFINE_MUTEX(algs_lock);
  56static unsigned int virtio_crypto_active_devs;
  57static void virtio_crypto_ablkcipher_finalize_req(
  58        struct virtio_crypto_sym_request *vc_sym_req,
  59        struct ablkcipher_request *req,
  60        int err);
  61
  62static 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
  93static 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
 103static int
 104virtio_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                pr_err("virtio_crypto: Unsupported key length: %d\n",
 114                        key_len);
 115                return -EINVAL;
 116        }
 117        return 0;
 118}
 119
 120static int virtio_crypto_alg_ablkcipher_init_session(
 121                struct virtio_crypto_ablkcipher_ctx *ctx,
 122                uint32_t alg, const uint8_t *key,
 123                unsigned int keylen,
 124                int encrypt)
 125{
 126        struct scatterlist outhdr, key_sg, inhdr, *sgs[3];
 127        unsigned int tmp;
 128        struct virtio_crypto *vcrypto = ctx->vcrypto;
 129        int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT;
 130        int err;
 131        unsigned int num_out = 0, num_in = 0;
 132
 133        /*
 134         * Avoid to do DMA from the stack, switch to using
 135         * dynamically-allocated for the key
 136         */
 137        uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC);
 138
 139        if (!cipher_key)
 140                return -ENOMEM;
 141
 142        memcpy(cipher_key, key, keylen);
 143
 144        spin_lock(&vcrypto->ctrl_lock);
 145        /* Pad ctrl header */
 146        vcrypto->ctrl.header.opcode =
 147                cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
 148        vcrypto->ctrl.header.algo = cpu_to_le32(alg);
 149        /* Set the default dataqueue id to 0 */
 150        vcrypto->ctrl.header.queue_id = 0;
 151
 152        vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
 153        /* Pad cipher's parameters */
 154        vcrypto->ctrl.u.sym_create_session.op_type =
 155                cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
 156        vcrypto->ctrl.u.sym_create_session.u.cipher.para.algo =
 157                vcrypto->ctrl.header.algo;
 158        vcrypto->ctrl.u.sym_create_session.u.cipher.para.keylen =
 159                cpu_to_le32(keylen);
 160        vcrypto->ctrl.u.sym_create_session.u.cipher.para.op =
 161                cpu_to_le32(op);
 162
 163        sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
 164        sgs[num_out++] = &outhdr;
 165
 166        /* Set key */
 167        sg_init_one(&key_sg, cipher_key, keylen);
 168        sgs[num_out++] = &key_sg;
 169
 170        /* Return status and session id back */
 171        sg_init_one(&inhdr, &vcrypto->input, sizeof(vcrypto->input));
 172        sgs[num_out + num_in++] = &inhdr;
 173
 174        err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
 175                                num_in, vcrypto, GFP_ATOMIC);
 176        if (err < 0) {
 177                spin_unlock(&vcrypto->ctrl_lock);
 178                kzfree(cipher_key);
 179                return err;
 180        }
 181        virtqueue_kick(vcrypto->ctrl_vq);
 182
 183        /*
 184         * Trapping into the hypervisor, so the request should be
 185         * handled immediately.
 186         */
 187        while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
 188               !virtqueue_is_broken(vcrypto->ctrl_vq))
 189                cpu_relax();
 190
 191        if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) {
 192                spin_unlock(&vcrypto->ctrl_lock);
 193                pr_err("virtio_crypto: Create session failed status: %u\n",
 194                        le32_to_cpu(vcrypto->input.status));
 195                kzfree(cipher_key);
 196                return -EINVAL;
 197        }
 198
 199        if (encrypt)
 200                ctx->enc_sess_info.session_id =
 201                        le64_to_cpu(vcrypto->input.session_id);
 202        else
 203                ctx->dec_sess_info.session_id =
 204                        le64_to_cpu(vcrypto->input.session_id);
 205
 206        spin_unlock(&vcrypto->ctrl_lock);
 207
 208        kzfree(cipher_key);
 209        return 0;
 210}
 211
 212static int virtio_crypto_alg_ablkcipher_close_session(
 213                struct virtio_crypto_ablkcipher_ctx *ctx,
 214                int encrypt)
 215{
 216        struct scatterlist outhdr, status_sg, *sgs[2];
 217        unsigned int tmp;
 218        struct virtio_crypto_destroy_session_req *destroy_session;
 219        struct virtio_crypto *vcrypto = ctx->vcrypto;
 220        int err;
 221        unsigned int num_out = 0, num_in = 0;
 222
 223        spin_lock(&vcrypto->ctrl_lock);
 224        vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR;
 225        /* Pad ctrl header */
 226        vcrypto->ctrl.header.opcode =
 227                cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
 228        /* Set the default virtqueue id to 0 */
 229        vcrypto->ctrl.header.queue_id = 0;
 230
 231        destroy_session = &vcrypto->ctrl.u.destroy_session;
 232
 233        if (encrypt)
 234                destroy_session->session_id =
 235                        cpu_to_le64(ctx->enc_sess_info.session_id);
 236        else
 237                destroy_session->session_id =
 238                        cpu_to_le64(ctx->dec_sess_info.session_id);
 239
 240        sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
 241        sgs[num_out++] = &outhdr;
 242
 243        /* Return status and session id back */
 244        sg_init_one(&status_sg, &vcrypto->ctrl_status.status,
 245                sizeof(vcrypto->ctrl_status.status));
 246        sgs[num_out + num_in++] = &status_sg;
 247
 248        err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
 249                        num_in, vcrypto, GFP_ATOMIC);
 250        if (err < 0) {
 251                spin_unlock(&vcrypto->ctrl_lock);
 252                return err;
 253        }
 254        virtqueue_kick(vcrypto->ctrl_vq);
 255
 256        while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
 257               !virtqueue_is_broken(vcrypto->ctrl_vq))
 258                cpu_relax();
 259
 260        if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) {
 261                spin_unlock(&vcrypto->ctrl_lock);
 262                pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
 263                        vcrypto->ctrl_status.status,
 264                        destroy_session->session_id);
 265
 266                return -EINVAL;
 267        }
 268        spin_unlock(&vcrypto->ctrl_lock);
 269
 270        return 0;
 271}
 272
 273static int virtio_crypto_alg_ablkcipher_init_sessions(
 274                struct virtio_crypto_ablkcipher_ctx *ctx,
 275                const uint8_t *key, unsigned int keylen)
 276{
 277        uint32_t alg;
 278        int ret;
 279        struct virtio_crypto *vcrypto = ctx->vcrypto;
 280
 281        if (keylen > vcrypto->max_cipher_key_len) {
 282                pr_err("virtio_crypto: the key is too long\n");
 283                goto bad_key;
 284        }
 285
 286        if (virtio_crypto_alg_validate_key(keylen, &alg))
 287                goto bad_key;
 288
 289        /* Create encryption session */
 290        ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
 291                        alg, key, keylen, 1);
 292        if (ret)
 293                return ret;
 294        /* Create decryption session */
 295        ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
 296                        alg, key, keylen, 0);
 297        if (ret) {
 298                virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
 299                return ret;
 300        }
 301        return 0;
 302
 303bad_key:
 304        crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
 305        return -EINVAL;
 306}
 307
 308/* Note: kernel crypto API realization */
 309static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
 310                                         const uint8_t *key,
 311                                         unsigned int keylen)
 312{
 313        struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(tfm);
 314        int ret;
 315
 316        if (!ctx->vcrypto) {
 317                /* New key */
 318                int node = virtio_crypto_get_current_node();
 319                struct virtio_crypto *vcrypto =
 320                                      virtcrypto_get_dev_node(node);
 321                if (!vcrypto) {
 322                        pr_err("virtio_crypto: Could not find a virtio device in the system");
 323                        return -ENODEV;
 324                }
 325
 326                ctx->vcrypto = vcrypto;
 327        } else {
 328                /* Rekeying, we should close the created sessions previously */
 329                virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
 330                virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
 331        }
 332
 333        ret = virtio_crypto_alg_ablkcipher_init_sessions(ctx, key, keylen);
 334        if (ret) {
 335                virtcrypto_dev_put(ctx->vcrypto);
 336                ctx->vcrypto = NULL;
 337
 338                return ret;
 339        }
 340
 341        return 0;
 342}
 343
 344static int
 345__virtio_crypto_ablkcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req,
 346                struct ablkcipher_request *req,
 347                struct data_queue *data_vq)
 348{
 349        struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
 350        struct virtio_crypto_ablkcipher_ctx *ctx = vc_sym_req->ablkcipher_ctx;
 351        struct virtio_crypto_request *vc_req = &vc_sym_req->base;
 352        unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
 353        struct virtio_crypto *vcrypto = ctx->vcrypto;
 354        struct virtio_crypto_op_data_req *req_data;
 355        int src_nents, dst_nents;
 356        int err;
 357        unsigned long flags;
 358        struct scatterlist outhdr, iv_sg, status_sg, **sgs;
 359        int i;
 360        u64 dst_len;
 361        unsigned int num_out = 0, num_in = 0;
 362        int sg_total;
 363        uint8_t *iv;
 364
 365        src_nents = sg_nents_for_len(req->src, req->nbytes);
 366        dst_nents = sg_nents(req->dst);
 367
 368        pr_debug("virtio_crypto: Number of sgs (src_nents: %d, dst_nents: %d)\n",
 369                        src_nents, dst_nents);
 370
 371        /* Why 3?  outhdr + iv + inhdr */
 372        sg_total = src_nents + dst_nents + 3;
 373        sgs = kzalloc_node(sg_total * sizeof(*sgs), GFP_ATOMIC,
 374                                dev_to_node(&vcrypto->vdev->dev));
 375        if (!sgs)
 376                return -ENOMEM;
 377
 378        req_data = kzalloc_node(sizeof(*req_data), GFP_ATOMIC,
 379                                dev_to_node(&vcrypto->vdev->dev));
 380        if (!req_data) {
 381                kfree(sgs);
 382                return -ENOMEM;
 383        }
 384
 385        vc_req->req_data = req_data;
 386        vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
 387        /* Head of operation */
 388        if (vc_sym_req->encrypt) {
 389                req_data->header.session_id =
 390                        cpu_to_le64(ctx->enc_sess_info.session_id);
 391                req_data->header.opcode =
 392                        cpu_to_le32(VIRTIO_CRYPTO_CIPHER_ENCRYPT);
 393        } else {
 394                req_data->header.session_id =
 395                        cpu_to_le64(ctx->dec_sess_info.session_id);
 396            req_data->header.opcode =
 397                        cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT);
 398        }
 399        req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
 400        req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize);
 401        req_data->u.sym_req.u.cipher.para.src_data_len =
 402                        cpu_to_le32(req->nbytes);
 403
 404        dst_len = virtio_crypto_alg_sg_nents_length(req->dst);
 405        if (unlikely(dst_len > U32_MAX)) {
 406                pr_err("virtio_crypto: The dst_len is beyond U32_MAX\n");
 407                err = -EINVAL;
 408                goto free;
 409        }
 410
 411        pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n",
 412                        req->nbytes, dst_len);
 413
 414        if (unlikely(req->nbytes + dst_len + ivsize +
 415                sizeof(vc_req->status) > vcrypto->max_size)) {
 416                pr_err("virtio_crypto: The length is too big\n");
 417                err = -EINVAL;
 418                goto free;
 419        }
 420
 421        req_data->u.sym_req.u.cipher.para.dst_data_len =
 422                        cpu_to_le32((uint32_t)dst_len);
 423
 424        /* Outhdr */
 425        sg_init_one(&outhdr, req_data, sizeof(*req_data));
 426        sgs[num_out++] = &outhdr;
 427
 428        /* IV */
 429
 430        /*
 431         * Avoid to do DMA from the stack, switch to using
 432         * dynamically-allocated for the IV
 433         */
 434        iv = kzalloc_node(ivsize, GFP_ATOMIC,
 435                                dev_to_node(&vcrypto->vdev->dev));
 436        if (!iv) {
 437                err = -ENOMEM;
 438                goto free;
 439        }
 440        memcpy(iv, req->info, ivsize);
 441        sg_init_one(&iv_sg, iv, ivsize);
 442        sgs[num_out++] = &iv_sg;
 443        vc_sym_req->iv = iv;
 444
 445        /* Source data */
 446        for (i = 0; i < src_nents; i++)
 447                sgs[num_out++] = &req->src[i];
 448
 449        /* Destination data */
 450        for (i = 0; i < dst_nents; i++)
 451                sgs[num_out + num_in++] = &req->dst[i];
 452
 453        /* Status */
 454        sg_init_one(&status_sg, &vc_req->status, sizeof(vc_req->status));
 455        sgs[num_out + num_in++] = &status_sg;
 456
 457        vc_req->sgs = sgs;
 458
 459        spin_lock_irqsave(&data_vq->lock, flags);
 460        err = virtqueue_add_sgs(data_vq->vq, sgs, num_out,
 461                                num_in, vc_req, GFP_ATOMIC);
 462        virtqueue_kick(data_vq->vq);
 463        spin_unlock_irqrestore(&data_vq->lock, flags);
 464        if (unlikely(err < 0))
 465                goto free_iv;
 466
 467        return 0;
 468
 469free_iv:
 470        kzfree(iv);
 471free:
 472        kzfree(req_data);
 473        kfree(sgs);
 474        return err;
 475}
 476
 477static int virtio_crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
 478{
 479        struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
 480        struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
 481        struct virtio_crypto_sym_request *vc_sym_req =
 482                                ablkcipher_request_ctx(req);
 483        struct virtio_crypto_request *vc_req = &vc_sym_req->base;
 484        struct virtio_crypto *vcrypto = ctx->vcrypto;
 485        /* Use the first data virtqueue as default */
 486        struct data_queue *data_vq = &vcrypto->data_vq[0];
 487
 488        vc_req->dataq = data_vq;
 489        vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
 490        vc_sym_req->ablkcipher_ctx = ctx;
 491        vc_sym_req->ablkcipher_req = req;
 492        vc_sym_req->encrypt = true;
 493
 494        return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
 495}
 496
 497static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
 498{
 499        struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
 500        struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
 501        struct virtio_crypto_sym_request *vc_sym_req =
 502                                ablkcipher_request_ctx(req);
 503        struct virtio_crypto_request *vc_req = &vc_sym_req->base;
 504        struct virtio_crypto *vcrypto = ctx->vcrypto;
 505        /* Use the first data virtqueue as default */
 506        struct data_queue *data_vq = &vcrypto->data_vq[0];
 507
 508        vc_req->dataq = data_vq;
 509        vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
 510        vc_sym_req->ablkcipher_ctx = ctx;
 511        vc_sym_req->ablkcipher_req = req;
 512        vc_sym_req->encrypt = false;
 513
 514        return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
 515}
 516
 517static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm)
 518{
 519        struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
 520
 521        tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_sym_request);
 522        ctx->tfm = tfm;
 523
 524        return 0;
 525}
 526
 527static void virtio_crypto_ablkcipher_exit(struct crypto_tfm *tfm)
 528{
 529        struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
 530
 531        if (!ctx->vcrypto)
 532                return;
 533
 534        virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
 535        virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
 536        virtcrypto_dev_put(ctx->vcrypto);
 537        ctx->vcrypto = NULL;
 538}
 539
 540int virtio_crypto_ablkcipher_crypt_req(
 541        struct crypto_engine *engine,
 542        struct ablkcipher_request *req)
 543{
 544        struct virtio_crypto_sym_request *vc_sym_req =
 545                                ablkcipher_request_ctx(req);
 546        struct virtio_crypto_request *vc_req = &vc_sym_req->base;
 547        struct data_queue *data_vq = vc_req->dataq;
 548        int ret;
 549
 550        ret = __virtio_crypto_ablkcipher_do_req(vc_sym_req, req, data_vq);
 551        if (ret < 0)
 552                return ret;
 553
 554        virtqueue_kick(data_vq->vq);
 555
 556        return 0;
 557}
 558
 559static void virtio_crypto_ablkcipher_finalize_req(
 560        struct virtio_crypto_sym_request *vc_sym_req,
 561        struct ablkcipher_request *req,
 562        int err)
 563{
 564        crypto_finalize_cipher_request(vc_sym_req->base.dataq->engine,
 565                                        req, err);
 566        kzfree(vc_sym_req->iv);
 567        virtcrypto_clear_request(&vc_sym_req->base);
 568}
 569
 570static struct crypto_alg virtio_crypto_algs[] = { {
 571        .cra_name = "cbc(aes)",
 572        .cra_driver_name = "virtio_crypto_aes_cbc",
 573        .cra_priority = 150,
 574        .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
 575        .cra_blocksize = AES_BLOCK_SIZE,
 576        .cra_ctxsize  = sizeof(struct virtio_crypto_ablkcipher_ctx),
 577        .cra_alignmask = 0,
 578        .cra_module = THIS_MODULE,
 579        .cra_type = &crypto_ablkcipher_type,
 580        .cra_init = virtio_crypto_ablkcipher_init,
 581        .cra_exit = virtio_crypto_ablkcipher_exit,
 582        .cra_u = {
 583           .ablkcipher = {
 584                        .setkey = virtio_crypto_ablkcipher_setkey,
 585                        .decrypt = virtio_crypto_ablkcipher_decrypt,
 586                        .encrypt = virtio_crypto_ablkcipher_encrypt,
 587                        .min_keysize = AES_MIN_KEY_SIZE,
 588                        .max_keysize = AES_MAX_KEY_SIZE,
 589                        .ivsize = AES_BLOCK_SIZE,
 590                },
 591        },
 592} };
 593
 594int virtio_crypto_algs_register(void)
 595{
 596        int ret = 0;
 597
 598        mutex_lock(&algs_lock);
 599        if (++virtio_crypto_active_devs != 1)
 600                goto unlock;
 601
 602        ret = crypto_register_algs(virtio_crypto_algs,
 603                        ARRAY_SIZE(virtio_crypto_algs));
 604        if (ret)
 605                virtio_crypto_active_devs--;
 606
 607unlock:
 608        mutex_unlock(&algs_lock);
 609        return ret;
 610}
 611
 612void virtio_crypto_algs_unregister(void)
 613{
 614        mutex_lock(&algs_lock);
 615        if (--virtio_crypto_active_devs != 0)
 616                goto unlock;
 617
 618        crypto_unregister_algs(virtio_crypto_algs,
 619                        ARRAY_SIZE(virtio_crypto_algs));
 620
 621unlock:
 622        mutex_unlock(&algs_lock);
 623}
 624