| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* Algorithms supported by virtio crypto device |
|---|
| 2 | 3 | * |
|---|
| 3 | 4 | * Authors: Gonglei <arei.gonglei@huawei.com> |
|---|
| 4 | 5 | * |
|---|
| 5 | 6 | * 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 | 7 | */ |
|---|
| 20 | 8 | |
|---|
| 21 | 9 | #include <linux/scatterlist.h> |
|---|
| 22 | 10 | #include <crypto/algapi.h> |
|---|
| 11 | +#include <crypto/internal/skcipher.h> |
|---|
| 23 | 12 | #include <linux/err.h> |
|---|
| 24 | 13 | #include <crypto/scatterwalk.h> |
|---|
| 25 | 14 | #include <linux/atomic.h> |
|---|
| .. | .. |
|---|
| 28 | 17 | #include "virtio_crypto_common.h" |
|---|
| 29 | 18 | |
|---|
| 30 | 19 | |
|---|
| 31 | | -struct virtio_crypto_ablkcipher_ctx { |
|---|
| 20 | +struct virtio_crypto_skcipher_ctx { |
|---|
| 32 | 21 | struct crypto_engine_ctx enginectx; |
|---|
| 33 | 22 | struct virtio_crypto *vcrypto; |
|---|
| 34 | | - struct crypto_tfm *tfm; |
|---|
| 23 | + struct crypto_skcipher *tfm; |
|---|
| 35 | 24 | |
|---|
| 36 | 25 | struct virtio_crypto_sym_session_info enc_sess_info; |
|---|
| 37 | 26 | struct virtio_crypto_sym_session_info dec_sess_info; |
|---|
| .. | .. |
|---|
| 42 | 31 | |
|---|
| 43 | 32 | /* Cipher or aead */ |
|---|
| 44 | 33 | uint32_t type; |
|---|
| 45 | | - struct virtio_crypto_ablkcipher_ctx *ablkcipher_ctx; |
|---|
| 46 | | - struct ablkcipher_request *ablkcipher_req; |
|---|
| 34 | + struct virtio_crypto_skcipher_ctx *skcipher_ctx; |
|---|
| 35 | + struct skcipher_request *skcipher_req; |
|---|
| 47 | 36 | uint8_t *iv; |
|---|
| 48 | 37 | /* Encryption? */ |
|---|
| 49 | 38 | bool encrypt; |
|---|
| .. | .. |
|---|
| 53 | 42 | uint32_t algonum; |
|---|
| 54 | 43 | uint32_t service; |
|---|
| 55 | 44 | unsigned int active_devs; |
|---|
| 56 | | - struct crypto_alg algo; |
|---|
| 45 | + struct skcipher_alg algo; |
|---|
| 57 | 46 | }; |
|---|
| 58 | 47 | |
|---|
| 59 | 48 | /* |
|---|
| .. | .. |
|---|
| 61 | 50 | * and crypto algorithms registion. |
|---|
| 62 | 51 | */ |
|---|
| 63 | 52 | static DEFINE_MUTEX(algs_lock); |
|---|
| 64 | | -static void virtio_crypto_ablkcipher_finalize_req( |
|---|
| 53 | +static void virtio_crypto_skcipher_finalize_req( |
|---|
| 65 | 54 | struct virtio_crypto_sym_request *vc_sym_req, |
|---|
| 66 | | - struct ablkcipher_request *req, |
|---|
| 55 | + struct skcipher_request *req, |
|---|
| 67 | 56 | int err); |
|---|
| 68 | 57 | |
|---|
| 69 | 58 | static void virtio_crypto_dataq_sym_callback |
|---|
| .. | .. |
|---|
| 71 | 60 | { |
|---|
| 72 | 61 | struct virtio_crypto_sym_request *vc_sym_req = |
|---|
| 73 | 62 | container_of(vc_req, struct virtio_crypto_sym_request, base); |
|---|
| 74 | | - struct ablkcipher_request *ablk_req; |
|---|
| 63 | + struct skcipher_request *ablk_req; |
|---|
| 75 | 64 | int error; |
|---|
| 76 | 65 | |
|---|
| 77 | 66 | /* Finish the encrypt or decrypt process */ |
|---|
| .. | .. |
|---|
| 91 | 80 | error = -EIO; |
|---|
| 92 | 81 | break; |
|---|
| 93 | 82 | } |
|---|
| 94 | | - ablk_req = vc_sym_req->ablkcipher_req; |
|---|
| 95 | | - virtio_crypto_ablkcipher_finalize_req(vc_sym_req, |
|---|
| 83 | + ablk_req = vc_sym_req->skcipher_req; |
|---|
| 84 | + virtio_crypto_skcipher_finalize_req(vc_sym_req, |
|---|
| 96 | 85 | ablk_req, error); |
|---|
| 97 | 86 | } |
|---|
| 98 | 87 | } |
|---|
| .. | .. |
|---|
| 122 | 111 | return 0; |
|---|
| 123 | 112 | } |
|---|
| 124 | 113 | |
|---|
| 125 | | -static int virtio_crypto_alg_ablkcipher_init_session( |
|---|
| 126 | | - struct virtio_crypto_ablkcipher_ctx *ctx, |
|---|
| 114 | +static int virtio_crypto_alg_skcipher_init_session( |
|---|
| 115 | + struct virtio_crypto_skcipher_ctx *ctx, |
|---|
| 127 | 116 | uint32_t alg, const uint8_t *key, |
|---|
| 128 | 117 | unsigned int keylen, |
|---|
| 129 | 118 | int encrypt) |
|---|
| .. | .. |
|---|
| 139 | 128 | * Avoid to do DMA from the stack, switch to using |
|---|
| 140 | 129 | * dynamically-allocated for the key |
|---|
| 141 | 130 | */ |
|---|
| 142 | | - uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC); |
|---|
| 131 | + uint8_t *cipher_key = kmemdup(key, keylen, GFP_ATOMIC); |
|---|
| 143 | 132 | |
|---|
| 144 | 133 | if (!cipher_key) |
|---|
| 145 | 134 | return -ENOMEM; |
|---|
| 146 | | - |
|---|
| 147 | | - memcpy(cipher_key, key, keylen); |
|---|
| 148 | 135 | |
|---|
| 149 | 136 | spin_lock(&vcrypto->ctrl_lock); |
|---|
| 150 | 137 | /* Pad ctrl header */ |
|---|
| .. | .. |
|---|
| 180 | 167 | num_in, vcrypto, GFP_ATOMIC); |
|---|
| 181 | 168 | if (err < 0) { |
|---|
| 182 | 169 | spin_unlock(&vcrypto->ctrl_lock); |
|---|
| 183 | | - kzfree(cipher_key); |
|---|
| 170 | + kfree_sensitive(cipher_key); |
|---|
| 184 | 171 | return err; |
|---|
| 185 | 172 | } |
|---|
| 186 | 173 | virtqueue_kick(vcrypto->ctrl_vq); |
|---|
| .. | .. |
|---|
| 197 | 184 | spin_unlock(&vcrypto->ctrl_lock); |
|---|
| 198 | 185 | pr_err("virtio_crypto: Create session failed status: %u\n", |
|---|
| 199 | 186 | le32_to_cpu(vcrypto->input.status)); |
|---|
| 200 | | - kzfree(cipher_key); |
|---|
| 187 | + kfree_sensitive(cipher_key); |
|---|
| 201 | 188 | return -EINVAL; |
|---|
| 202 | 189 | } |
|---|
| 203 | 190 | |
|---|
| .. | .. |
|---|
| 210 | 197 | |
|---|
| 211 | 198 | spin_unlock(&vcrypto->ctrl_lock); |
|---|
| 212 | 199 | |
|---|
| 213 | | - kzfree(cipher_key); |
|---|
| 200 | + kfree_sensitive(cipher_key); |
|---|
| 214 | 201 | return 0; |
|---|
| 215 | 202 | } |
|---|
| 216 | 203 | |
|---|
| 217 | | -static int virtio_crypto_alg_ablkcipher_close_session( |
|---|
| 218 | | - struct virtio_crypto_ablkcipher_ctx *ctx, |
|---|
| 204 | +static int virtio_crypto_alg_skcipher_close_session( |
|---|
| 205 | + struct virtio_crypto_skcipher_ctx *ctx, |
|---|
| 219 | 206 | int encrypt) |
|---|
| 220 | 207 | { |
|---|
| 221 | 208 | struct scatterlist outhdr, status_sg, *sgs[2]; |
|---|
| .. | .. |
|---|
| 275 | 262 | return 0; |
|---|
| 276 | 263 | } |
|---|
| 277 | 264 | |
|---|
| 278 | | -static int virtio_crypto_alg_ablkcipher_init_sessions( |
|---|
| 279 | | - struct virtio_crypto_ablkcipher_ctx *ctx, |
|---|
| 265 | +static int virtio_crypto_alg_skcipher_init_sessions( |
|---|
| 266 | + struct virtio_crypto_skcipher_ctx *ctx, |
|---|
| 280 | 267 | const uint8_t *key, unsigned int keylen) |
|---|
| 281 | 268 | { |
|---|
| 282 | 269 | uint32_t alg; |
|---|
| .. | .. |
|---|
| 285 | 272 | |
|---|
| 286 | 273 | if (keylen > vcrypto->max_cipher_key_len) { |
|---|
| 287 | 274 | pr_err("virtio_crypto: the key is too long\n"); |
|---|
| 288 | | - goto bad_key; |
|---|
| 275 | + return -EINVAL; |
|---|
| 289 | 276 | } |
|---|
| 290 | 277 | |
|---|
| 291 | 278 | if (virtio_crypto_alg_validate_key(keylen, &alg)) |
|---|
| 292 | | - goto bad_key; |
|---|
| 279 | + return -EINVAL; |
|---|
| 293 | 280 | |
|---|
| 294 | 281 | /* Create encryption session */ |
|---|
| 295 | | - ret = virtio_crypto_alg_ablkcipher_init_session(ctx, |
|---|
| 282 | + ret = virtio_crypto_alg_skcipher_init_session(ctx, |
|---|
| 296 | 283 | alg, key, keylen, 1); |
|---|
| 297 | 284 | if (ret) |
|---|
| 298 | 285 | return ret; |
|---|
| 299 | 286 | /* Create decryption session */ |
|---|
| 300 | | - ret = virtio_crypto_alg_ablkcipher_init_session(ctx, |
|---|
| 287 | + ret = virtio_crypto_alg_skcipher_init_session(ctx, |
|---|
| 301 | 288 | alg, key, keylen, 0); |
|---|
| 302 | 289 | if (ret) { |
|---|
| 303 | | - virtio_crypto_alg_ablkcipher_close_session(ctx, 1); |
|---|
| 290 | + virtio_crypto_alg_skcipher_close_session(ctx, 1); |
|---|
| 304 | 291 | return ret; |
|---|
| 305 | 292 | } |
|---|
| 306 | 293 | return 0; |
|---|
| 307 | | - |
|---|
| 308 | | -bad_key: |
|---|
| 309 | | - crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
|---|
| 310 | | - return -EINVAL; |
|---|
| 311 | 294 | } |
|---|
| 312 | 295 | |
|---|
| 313 | 296 | /* Note: kernel crypto API realization */ |
|---|
| 314 | | -static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, |
|---|
| 297 | +static int virtio_crypto_skcipher_setkey(struct crypto_skcipher *tfm, |
|---|
| 315 | 298 | const uint8_t *key, |
|---|
| 316 | 299 | unsigned int keylen) |
|---|
| 317 | 300 | { |
|---|
| 318 | | - struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
|---|
| 301 | + struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 319 | 302 | uint32_t alg; |
|---|
| 320 | 303 | int ret; |
|---|
| 321 | 304 | |
|---|
| .. | .. |
|---|
| 337 | 320 | ctx->vcrypto = vcrypto; |
|---|
| 338 | 321 | } else { |
|---|
| 339 | 322 | /* Rekeying, we should close the created sessions previously */ |
|---|
| 340 | | - virtio_crypto_alg_ablkcipher_close_session(ctx, 1); |
|---|
| 341 | | - virtio_crypto_alg_ablkcipher_close_session(ctx, 0); |
|---|
| 323 | + virtio_crypto_alg_skcipher_close_session(ctx, 1); |
|---|
| 324 | + virtio_crypto_alg_skcipher_close_session(ctx, 0); |
|---|
| 342 | 325 | } |
|---|
| 343 | 326 | |
|---|
| 344 | | - ret = virtio_crypto_alg_ablkcipher_init_sessions(ctx, key, keylen); |
|---|
| 327 | + ret = virtio_crypto_alg_skcipher_init_sessions(ctx, key, keylen); |
|---|
| 345 | 328 | if (ret) { |
|---|
| 346 | 329 | virtcrypto_dev_put(ctx->vcrypto); |
|---|
| 347 | 330 | ctx->vcrypto = NULL; |
|---|
| .. | .. |
|---|
| 353 | 336 | } |
|---|
| 354 | 337 | |
|---|
| 355 | 338 | static int |
|---|
| 356 | | -__virtio_crypto_ablkcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req, |
|---|
| 357 | | - struct ablkcipher_request *req, |
|---|
| 339 | +__virtio_crypto_skcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req, |
|---|
| 340 | + struct skcipher_request *req, |
|---|
| 358 | 341 | struct data_queue *data_vq) |
|---|
| 359 | 342 | { |
|---|
| 360 | | - struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); |
|---|
| 361 | | - struct virtio_crypto_ablkcipher_ctx *ctx = vc_sym_req->ablkcipher_ctx; |
|---|
| 343 | + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| 344 | + struct virtio_crypto_skcipher_ctx *ctx = vc_sym_req->skcipher_ctx; |
|---|
| 362 | 345 | struct virtio_crypto_request *vc_req = &vc_sym_req->base; |
|---|
| 363 | | - unsigned int ivsize = crypto_ablkcipher_ivsize(tfm); |
|---|
| 346 | + unsigned int ivsize = crypto_skcipher_ivsize(tfm); |
|---|
| 364 | 347 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
|---|
| 365 | 348 | struct virtio_crypto_op_data_req *req_data; |
|---|
| 366 | 349 | int src_nents, dst_nents; |
|---|
| .. | .. |
|---|
| 373 | 356 | uint8_t *iv; |
|---|
| 374 | 357 | struct scatterlist *sg; |
|---|
| 375 | 358 | |
|---|
| 376 | | - src_nents = sg_nents_for_len(req->src, req->nbytes); |
|---|
| 359 | + src_nents = sg_nents_for_len(req->src, req->cryptlen); |
|---|
| 377 | 360 | if (src_nents < 0) { |
|---|
| 378 | 361 | pr_err("Invalid number of src SG.\n"); |
|---|
| 379 | 362 | return src_nents; |
|---|
| .. | .. |
|---|
| 409 | 392 | } else { |
|---|
| 410 | 393 | req_data->header.session_id = |
|---|
| 411 | 394 | cpu_to_le64(ctx->dec_sess_info.session_id); |
|---|
| 412 | | - req_data->header.opcode = |
|---|
| 395 | + req_data->header.opcode = |
|---|
| 413 | 396 | cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT); |
|---|
| 414 | 397 | } |
|---|
| 415 | 398 | req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER); |
|---|
| 416 | 399 | req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize); |
|---|
| 417 | 400 | req_data->u.sym_req.u.cipher.para.src_data_len = |
|---|
| 418 | | - cpu_to_le32(req->nbytes); |
|---|
| 401 | + cpu_to_le32(req->cryptlen); |
|---|
| 419 | 402 | |
|---|
| 420 | 403 | dst_len = virtio_crypto_alg_sg_nents_length(req->dst); |
|---|
| 421 | 404 | if (unlikely(dst_len > U32_MAX)) { |
|---|
| .. | .. |
|---|
| 424 | 407 | goto free; |
|---|
| 425 | 408 | } |
|---|
| 426 | 409 | |
|---|
| 427 | | - dst_len = min_t(unsigned int, req->nbytes, dst_len); |
|---|
| 410 | + dst_len = min_t(unsigned int, req->cryptlen, dst_len); |
|---|
| 428 | 411 | pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n", |
|---|
| 429 | | - req->nbytes, dst_len); |
|---|
| 412 | + req->cryptlen, dst_len); |
|---|
| 430 | 413 | |
|---|
| 431 | | - if (unlikely(req->nbytes + dst_len + ivsize + |
|---|
| 414 | + if (unlikely(req->cryptlen + dst_len + ivsize + |
|---|
| 432 | 415 | sizeof(vc_req->status) > vcrypto->max_size)) { |
|---|
| 433 | 416 | pr_err("virtio_crypto: The length is too big\n"); |
|---|
| 434 | 417 | err = -EINVAL; |
|---|
| .. | .. |
|---|
| 454 | 437 | err = -ENOMEM; |
|---|
| 455 | 438 | goto free; |
|---|
| 456 | 439 | } |
|---|
| 457 | | - memcpy(iv, req->info, ivsize); |
|---|
| 440 | + memcpy(iv, req->iv, ivsize); |
|---|
| 458 | 441 | if (!vc_sym_req->encrypt) |
|---|
| 459 | | - scatterwalk_map_and_copy(req->info, req->src, |
|---|
| 460 | | - req->nbytes - AES_BLOCK_SIZE, |
|---|
| 442 | + scatterwalk_map_and_copy(req->iv, req->src, |
|---|
| 443 | + req->cryptlen - AES_BLOCK_SIZE, |
|---|
| 461 | 444 | AES_BLOCK_SIZE, 0); |
|---|
| 462 | 445 | |
|---|
| 463 | 446 | sg_init_one(&iv_sg, iv, ivsize); |
|---|
| .. | .. |
|---|
| 489 | 472 | return 0; |
|---|
| 490 | 473 | |
|---|
| 491 | 474 | free_iv: |
|---|
| 492 | | - kzfree(iv); |
|---|
| 475 | + kfree_sensitive(iv); |
|---|
| 493 | 476 | free: |
|---|
| 494 | | - kzfree(req_data); |
|---|
| 477 | + kfree_sensitive(req_data); |
|---|
| 495 | 478 | kfree(sgs); |
|---|
| 496 | 479 | return err; |
|---|
| 497 | 480 | } |
|---|
| 498 | 481 | |
|---|
| 499 | | -static int virtio_crypto_ablkcipher_encrypt(struct ablkcipher_request *req) |
|---|
| 482 | +static int virtio_crypto_skcipher_encrypt(struct skcipher_request *req) |
|---|
| 500 | 483 | { |
|---|
| 501 | | - struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req); |
|---|
| 502 | | - struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm); |
|---|
| 484 | + struct crypto_skcipher *atfm = crypto_skcipher_reqtfm(req); |
|---|
| 485 | + struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(atfm); |
|---|
| 503 | 486 | struct virtio_crypto_sym_request *vc_sym_req = |
|---|
| 504 | | - ablkcipher_request_ctx(req); |
|---|
| 487 | + skcipher_request_ctx(req); |
|---|
| 505 | 488 | struct virtio_crypto_request *vc_req = &vc_sym_req->base; |
|---|
| 506 | 489 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
|---|
| 507 | 490 | /* Use the first data virtqueue as default */ |
|---|
| 508 | 491 | struct data_queue *data_vq = &vcrypto->data_vq[0]; |
|---|
| 509 | 492 | |
|---|
| 510 | | - if (!req->nbytes) |
|---|
| 493 | + if (!req->cryptlen) |
|---|
| 511 | 494 | return 0; |
|---|
| 512 | | - if (req->nbytes % AES_BLOCK_SIZE) |
|---|
| 495 | + if (req->cryptlen % AES_BLOCK_SIZE) |
|---|
| 513 | 496 | return -EINVAL; |
|---|
| 514 | 497 | |
|---|
| 515 | 498 | vc_req->dataq = data_vq; |
|---|
| 516 | 499 | vc_req->alg_cb = virtio_crypto_dataq_sym_callback; |
|---|
| 517 | | - vc_sym_req->ablkcipher_ctx = ctx; |
|---|
| 518 | | - vc_sym_req->ablkcipher_req = req; |
|---|
| 500 | + vc_sym_req->skcipher_ctx = ctx; |
|---|
| 501 | + vc_sym_req->skcipher_req = req; |
|---|
| 519 | 502 | vc_sym_req->encrypt = true; |
|---|
| 520 | 503 | |
|---|
| 521 | | - return crypto_transfer_ablkcipher_request_to_engine(data_vq->engine, req); |
|---|
| 504 | + return crypto_transfer_skcipher_request_to_engine(data_vq->engine, req); |
|---|
| 522 | 505 | } |
|---|
| 523 | 506 | |
|---|
| 524 | | -static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req) |
|---|
| 507 | +static int virtio_crypto_skcipher_decrypt(struct skcipher_request *req) |
|---|
| 525 | 508 | { |
|---|
| 526 | | - struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req); |
|---|
| 527 | | - struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm); |
|---|
| 509 | + struct crypto_skcipher *atfm = crypto_skcipher_reqtfm(req); |
|---|
| 510 | + struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(atfm); |
|---|
| 528 | 511 | struct virtio_crypto_sym_request *vc_sym_req = |
|---|
| 529 | | - ablkcipher_request_ctx(req); |
|---|
| 512 | + skcipher_request_ctx(req); |
|---|
| 530 | 513 | struct virtio_crypto_request *vc_req = &vc_sym_req->base; |
|---|
| 531 | 514 | struct virtio_crypto *vcrypto = ctx->vcrypto; |
|---|
| 532 | 515 | /* Use the first data virtqueue as default */ |
|---|
| 533 | 516 | struct data_queue *data_vq = &vcrypto->data_vq[0]; |
|---|
| 534 | 517 | |
|---|
| 535 | | - if (!req->nbytes) |
|---|
| 518 | + if (!req->cryptlen) |
|---|
| 536 | 519 | return 0; |
|---|
| 537 | | - if (req->nbytes % AES_BLOCK_SIZE) |
|---|
| 520 | + if (req->cryptlen % AES_BLOCK_SIZE) |
|---|
| 538 | 521 | return -EINVAL; |
|---|
| 539 | 522 | |
|---|
| 540 | 523 | vc_req->dataq = data_vq; |
|---|
| 541 | 524 | vc_req->alg_cb = virtio_crypto_dataq_sym_callback; |
|---|
| 542 | | - vc_sym_req->ablkcipher_ctx = ctx; |
|---|
| 543 | | - vc_sym_req->ablkcipher_req = req; |
|---|
| 525 | + vc_sym_req->skcipher_ctx = ctx; |
|---|
| 526 | + vc_sym_req->skcipher_req = req; |
|---|
| 544 | 527 | vc_sym_req->encrypt = false; |
|---|
| 545 | 528 | |
|---|
| 546 | | - return crypto_transfer_ablkcipher_request_to_engine(data_vq->engine, req); |
|---|
| 529 | + return crypto_transfer_skcipher_request_to_engine(data_vq->engine, req); |
|---|
| 547 | 530 | } |
|---|
| 548 | 531 | |
|---|
| 549 | | -static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm) |
|---|
| 532 | +static int virtio_crypto_skcipher_init(struct crypto_skcipher *tfm) |
|---|
| 550 | 533 | { |
|---|
| 551 | | - struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm); |
|---|
| 534 | + struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 552 | 535 | |
|---|
| 553 | | - tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_sym_request); |
|---|
| 536 | + crypto_skcipher_set_reqsize(tfm, sizeof(struct virtio_crypto_sym_request)); |
|---|
| 554 | 537 | ctx->tfm = tfm; |
|---|
| 555 | 538 | |
|---|
| 556 | | - ctx->enginectx.op.do_one_request = virtio_crypto_ablkcipher_crypt_req; |
|---|
| 539 | + ctx->enginectx.op.do_one_request = virtio_crypto_skcipher_crypt_req; |
|---|
| 557 | 540 | ctx->enginectx.op.prepare_request = NULL; |
|---|
| 558 | 541 | ctx->enginectx.op.unprepare_request = NULL; |
|---|
| 559 | 542 | return 0; |
|---|
| 560 | 543 | } |
|---|
| 561 | 544 | |
|---|
| 562 | | -static void virtio_crypto_ablkcipher_exit(struct crypto_tfm *tfm) |
|---|
| 545 | +static void virtio_crypto_skcipher_exit(struct crypto_skcipher *tfm) |
|---|
| 563 | 546 | { |
|---|
| 564 | | - struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm); |
|---|
| 547 | + struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 565 | 548 | |
|---|
| 566 | 549 | if (!ctx->vcrypto) |
|---|
| 567 | 550 | return; |
|---|
| 568 | 551 | |
|---|
| 569 | | - virtio_crypto_alg_ablkcipher_close_session(ctx, 1); |
|---|
| 570 | | - virtio_crypto_alg_ablkcipher_close_session(ctx, 0); |
|---|
| 552 | + virtio_crypto_alg_skcipher_close_session(ctx, 1); |
|---|
| 553 | + virtio_crypto_alg_skcipher_close_session(ctx, 0); |
|---|
| 571 | 554 | virtcrypto_dev_put(ctx->vcrypto); |
|---|
| 572 | 555 | ctx->vcrypto = NULL; |
|---|
| 573 | 556 | } |
|---|
| 574 | 557 | |
|---|
| 575 | | -int virtio_crypto_ablkcipher_crypt_req( |
|---|
| 558 | +int virtio_crypto_skcipher_crypt_req( |
|---|
| 576 | 559 | struct crypto_engine *engine, void *vreq) |
|---|
| 577 | 560 | { |
|---|
| 578 | | - struct ablkcipher_request *req = container_of(vreq, struct ablkcipher_request, base); |
|---|
| 561 | + struct skcipher_request *req = container_of(vreq, struct skcipher_request, base); |
|---|
| 579 | 562 | struct virtio_crypto_sym_request *vc_sym_req = |
|---|
| 580 | | - ablkcipher_request_ctx(req); |
|---|
| 563 | + skcipher_request_ctx(req); |
|---|
| 581 | 564 | struct virtio_crypto_request *vc_req = &vc_sym_req->base; |
|---|
| 582 | 565 | struct data_queue *data_vq = vc_req->dataq; |
|---|
| 583 | 566 | int ret; |
|---|
| 584 | 567 | |
|---|
| 585 | | - ret = __virtio_crypto_ablkcipher_do_req(vc_sym_req, req, data_vq); |
|---|
| 568 | + ret = __virtio_crypto_skcipher_do_req(vc_sym_req, req, data_vq); |
|---|
| 586 | 569 | if (ret < 0) |
|---|
| 587 | 570 | return ret; |
|---|
| 588 | 571 | |
|---|
| .. | .. |
|---|
| 591 | 574 | return 0; |
|---|
| 592 | 575 | } |
|---|
| 593 | 576 | |
|---|
| 594 | | -static void virtio_crypto_ablkcipher_finalize_req( |
|---|
| 577 | +static void virtio_crypto_skcipher_finalize_req( |
|---|
| 595 | 578 | struct virtio_crypto_sym_request *vc_sym_req, |
|---|
| 596 | | - struct ablkcipher_request *req, |
|---|
| 579 | + struct skcipher_request *req, |
|---|
| 597 | 580 | int err) |
|---|
| 598 | 581 | { |
|---|
| 599 | 582 | if (vc_sym_req->encrypt) |
|---|
| 600 | | - scatterwalk_map_and_copy(req->info, req->dst, |
|---|
| 601 | | - req->nbytes - AES_BLOCK_SIZE, |
|---|
| 583 | + scatterwalk_map_and_copy(req->iv, req->dst, |
|---|
| 584 | + req->cryptlen - AES_BLOCK_SIZE, |
|---|
| 602 | 585 | AES_BLOCK_SIZE, 0); |
|---|
| 603 | | - kzfree(vc_sym_req->iv); |
|---|
| 586 | + kfree_sensitive(vc_sym_req->iv); |
|---|
| 604 | 587 | virtcrypto_clear_request(&vc_sym_req->base); |
|---|
| 605 | 588 | |
|---|
| 606 | | - crypto_finalize_ablkcipher_request(vc_sym_req->base.dataq->engine, |
|---|
| 589 | + crypto_finalize_skcipher_request(vc_sym_req->base.dataq->engine, |
|---|
| 607 | 590 | req, err); |
|---|
| 608 | 591 | } |
|---|
| 609 | 592 | |
|---|
| .. | .. |
|---|
| 611 | 594 | .algonum = VIRTIO_CRYPTO_CIPHER_AES_CBC, |
|---|
| 612 | 595 | .service = VIRTIO_CRYPTO_SERVICE_CIPHER, |
|---|
| 613 | 596 | .algo = { |
|---|
| 614 | | - .cra_name = "cbc(aes)", |
|---|
| 615 | | - .cra_driver_name = "virtio_crypto_aes_cbc", |
|---|
| 616 | | - .cra_priority = 150, |
|---|
| 617 | | - .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
|---|
| 618 | | - .cra_blocksize = AES_BLOCK_SIZE, |
|---|
| 619 | | - .cra_ctxsize = sizeof(struct virtio_crypto_ablkcipher_ctx), |
|---|
| 620 | | - .cra_alignmask = 0, |
|---|
| 621 | | - .cra_module = THIS_MODULE, |
|---|
| 622 | | - .cra_type = &crypto_ablkcipher_type, |
|---|
| 623 | | - .cra_init = virtio_crypto_ablkcipher_init, |
|---|
| 624 | | - .cra_exit = virtio_crypto_ablkcipher_exit, |
|---|
| 625 | | - .cra_u = { |
|---|
| 626 | | - .ablkcipher = { |
|---|
| 627 | | - .setkey = virtio_crypto_ablkcipher_setkey, |
|---|
| 628 | | - .decrypt = virtio_crypto_ablkcipher_decrypt, |
|---|
| 629 | | - .encrypt = virtio_crypto_ablkcipher_encrypt, |
|---|
| 630 | | - .min_keysize = AES_MIN_KEY_SIZE, |
|---|
| 631 | | - .max_keysize = AES_MAX_KEY_SIZE, |
|---|
| 632 | | - .ivsize = AES_BLOCK_SIZE, |
|---|
| 633 | | - }, |
|---|
| 634 | | - }, |
|---|
| 597 | + .base.cra_name = "cbc(aes)", |
|---|
| 598 | + .base.cra_driver_name = "virtio_crypto_aes_cbc", |
|---|
| 599 | + .base.cra_priority = 150, |
|---|
| 600 | + .base.cra_flags = CRYPTO_ALG_ASYNC | |
|---|
| 601 | + CRYPTO_ALG_ALLOCATES_MEMORY, |
|---|
| 602 | + .base.cra_blocksize = AES_BLOCK_SIZE, |
|---|
| 603 | + .base.cra_ctxsize = sizeof(struct virtio_crypto_skcipher_ctx), |
|---|
| 604 | + .base.cra_module = THIS_MODULE, |
|---|
| 605 | + .init = virtio_crypto_skcipher_init, |
|---|
| 606 | + .exit = virtio_crypto_skcipher_exit, |
|---|
| 607 | + .setkey = virtio_crypto_skcipher_setkey, |
|---|
| 608 | + .decrypt = virtio_crypto_skcipher_decrypt, |
|---|
| 609 | + .encrypt = virtio_crypto_skcipher_encrypt, |
|---|
| 610 | + .min_keysize = AES_MIN_KEY_SIZE, |
|---|
| 611 | + .max_keysize = AES_MAX_KEY_SIZE, |
|---|
| 612 | + .ivsize = AES_BLOCK_SIZE, |
|---|
| 635 | 613 | }, |
|---|
| 636 | 614 | } }; |
|---|
| 637 | 615 | |
|---|
| .. | .. |
|---|
| 651 | 629 | continue; |
|---|
| 652 | 630 | |
|---|
| 653 | 631 | if (virtio_crypto_algs[i].active_devs == 0) { |
|---|
| 654 | | - ret = crypto_register_alg(&virtio_crypto_algs[i].algo); |
|---|
| 632 | + ret = crypto_register_skcipher(&virtio_crypto_algs[i].algo); |
|---|
| 655 | 633 | if (ret) |
|---|
| 656 | 634 | goto unlock; |
|---|
| 657 | 635 | } |
|---|
| 658 | 636 | |
|---|
| 659 | 637 | virtio_crypto_algs[i].active_devs++; |
|---|
| 660 | 638 | dev_info(&vcrypto->vdev->dev, "Registered algo %s\n", |
|---|
| 661 | | - virtio_crypto_algs[i].algo.cra_name); |
|---|
| 639 | + virtio_crypto_algs[i].algo.base.cra_name); |
|---|
| 662 | 640 | } |
|---|
| 663 | 641 | |
|---|
| 664 | 642 | unlock: |
|---|
| .. | .. |
|---|
| 682 | 660 | continue; |
|---|
| 683 | 661 | |
|---|
| 684 | 662 | if (virtio_crypto_algs[i].active_devs == 1) |
|---|
| 685 | | - crypto_unregister_alg(&virtio_crypto_algs[i].algo); |
|---|
| 663 | + crypto_unregister_skcipher(&virtio_crypto_algs[i].algo); |
|---|
| 686 | 664 | |
|---|
| 687 | 665 | virtio_crypto_algs[i].active_devs--; |
|---|
| 688 | 666 | } |
|---|