aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/crypto/virtio/virtio_crypto_algs.c
diff options
context:
space:
mode:
authorZeng, Xin <xin.zeng@intel.com>2017-06-23 11:31:19 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2017-07-18 05:50:51 -0400
commitd31e712302c7d5ea47bdb5c1108ed1e33bf8681d (patch)
tree406ec11b112210ce34a11c874052d42c91ba90e4 /drivers/crypto/virtio/virtio_crypto_algs.c
parent41cdf7a45389e01991ee31e3301ed83cb3e3f7dc (diff)
crypto: virtio - Refacotor virtio_crypto driver for new virito crypto services
In current virtio crypto device driver, some common data structures and implementations that should be used by other virtio crypto algorithms (e.g. asymmetric crypto algorithms) introduce symmetric crypto algorithms specific implementations. This patch refactors these pieces of code so that they can be reused by other virtio crypto algorithms. Acked-by: Gonglei <arei.gonglei@huawei.com> Signed-off-by: Xin Zeng <xin.zeng@intel.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'drivers/crypto/virtio/virtio_crypto_algs.c')
-rw-r--r--drivers/crypto/virtio/virtio_crypto_algs.c109
1 files changed, 87 insertions, 22 deletions
diff --git a/drivers/crypto/virtio/virtio_crypto_algs.c b/drivers/crypto/virtio/virtio_crypto_algs.c
index 49defda4e03d..5035b0dc1e40 100644
--- a/drivers/crypto/virtio/virtio_crypto_algs.c
+++ b/drivers/crypto/virtio/virtio_crypto_algs.c
@@ -27,12 +27,68 @@
27#include <uapi/linux/virtio_crypto.h> 27#include <uapi/linux/virtio_crypto.h>
28#include "virtio_crypto_common.h" 28#include "virtio_crypto_common.h"
29 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
30/* 51/*
31 * The algs_lock protects the below global virtio_crypto_active_devs 52 * The algs_lock protects the below global virtio_crypto_active_devs
32 * and crypto algorithms registion. 53 * and crypto algorithms registion.
33 */ 54 */
34static DEFINE_MUTEX(algs_lock); 55static DEFINE_MUTEX(algs_lock);
35static unsigned int virtio_crypto_active_devs; 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}
36 92
37static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg) 93static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg)
38{ 94{
@@ -286,13 +342,14 @@ static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
286} 342}
287 343
288static int 344static int
289__virtio_crypto_ablkcipher_do_req(struct virtio_crypto_request *vc_req, 345__virtio_crypto_ablkcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req,
290 struct ablkcipher_request *req, 346 struct ablkcipher_request *req,
291 struct data_queue *data_vq) 347 struct data_queue *data_vq)
292{ 348{
293 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); 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;
294 unsigned int ivsize = crypto_ablkcipher_ivsize(tfm); 352 unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
295 struct virtio_crypto_ablkcipher_ctx *ctx = vc_req->ablkcipher_ctx;
296 struct virtio_crypto *vcrypto = ctx->vcrypto; 353 struct virtio_crypto *vcrypto = ctx->vcrypto;
297 struct virtio_crypto_op_data_req *req_data; 354 struct virtio_crypto_op_data_req *req_data;
298 int src_nents, dst_nents; 355 int src_nents, dst_nents;
@@ -326,9 +383,9 @@ __virtio_crypto_ablkcipher_do_req(struct virtio_crypto_request *vc_req,
326 } 383 }
327 384
328 vc_req->req_data = req_data; 385 vc_req->req_data = req_data;
329 vc_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER; 386 vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
330 /* Head of operation */ 387 /* Head of operation */
331 if (vc_req->encrypt) { 388 if (vc_sym_req->encrypt) {
332 req_data->header.session_id = 389 req_data->header.session_id =
333 cpu_to_le64(ctx->enc_sess_info.session_id); 390 cpu_to_le64(ctx->enc_sess_info.session_id);
334 req_data->header.opcode = 391 req_data->header.opcode =
@@ -383,7 +440,7 @@ __virtio_crypto_ablkcipher_do_req(struct virtio_crypto_request *vc_req,
383 memcpy(iv, req->info, ivsize); 440 memcpy(iv, req->info, ivsize);
384 sg_init_one(&iv_sg, iv, ivsize); 441 sg_init_one(&iv_sg, iv, ivsize);
385 sgs[num_out++] = &iv_sg; 442 sgs[num_out++] = &iv_sg;
386 vc_req->iv = iv; 443 vc_sym_req->iv = iv;
387 444
388 /* Source data */ 445 /* Source data */
389 for (i = 0; i < src_nents; i++) 446 for (i = 0; i < src_nents; i++)
@@ -421,15 +478,18 @@ static int virtio_crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
421{ 478{
422 struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req); 479 struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
423 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm); 480 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
424 struct virtio_crypto_request *vc_req = ablkcipher_request_ctx(req); 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;
425 struct virtio_crypto *vcrypto = ctx->vcrypto; 484 struct virtio_crypto *vcrypto = ctx->vcrypto;
426 /* Use the first data virtqueue as default */ 485 /* Use the first data virtqueue as default */
427 struct data_queue *data_vq = &vcrypto->data_vq[0]; 486 struct data_queue *data_vq = &vcrypto->data_vq[0];
428 487
429 vc_req->ablkcipher_ctx = ctx;
430 vc_req->ablkcipher_req = req;
431 vc_req->encrypt = true;
432 vc_req->dataq = data_vq; 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;
433 493
434 return crypto_transfer_cipher_request_to_engine(data_vq->engine, req); 494 return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
435} 495}
@@ -438,16 +498,18 @@ static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
438{ 498{
439 struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req); 499 struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
440 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm); 500 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
441 struct virtio_crypto_request *vc_req = ablkcipher_request_ctx(req); 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;
442 struct virtio_crypto *vcrypto = ctx->vcrypto; 504 struct virtio_crypto *vcrypto = ctx->vcrypto;
443 /* Use the first data virtqueue as default */ 505 /* Use the first data virtqueue as default */
444 struct data_queue *data_vq = &vcrypto->data_vq[0]; 506 struct data_queue *data_vq = &vcrypto->data_vq[0];
445 507
446 vc_req->ablkcipher_ctx = ctx;
447 vc_req->ablkcipher_req = req;
448
449 vc_req->encrypt = false;
450 vc_req->dataq = data_vq; 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;
451 513
452 return crypto_transfer_cipher_request_to_engine(data_vq->engine, req); 514 return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
453} 515}
@@ -456,7 +518,7 @@ static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm)
456{ 518{
457 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm); 519 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
458 520
459 tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_request); 521 tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_sym_request);
460 ctx->tfm = tfm; 522 ctx->tfm = tfm;
461 523
462 return 0; 524 return 0;
@@ -479,11 +541,13 @@ int virtio_crypto_ablkcipher_crypt_req(
479 struct crypto_engine *engine, 541 struct crypto_engine *engine,
480 struct ablkcipher_request *req) 542 struct ablkcipher_request *req)
481{ 543{
482 struct virtio_crypto_request *vc_req = ablkcipher_request_ctx(req); 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;
483 struct data_queue *data_vq = vc_req->dataq; 547 struct data_queue *data_vq = vc_req->dataq;
484 int ret; 548 int ret;
485 549
486 ret = __virtio_crypto_ablkcipher_do_req(vc_req, req, data_vq); 550 ret = __virtio_crypto_ablkcipher_do_req(vc_sym_req, req, data_vq);
487 if (ret < 0) 551 if (ret < 0)
488 return ret; 552 return ret;
489 553
@@ -492,14 +556,15 @@ int virtio_crypto_ablkcipher_crypt_req(
492 return 0; 556 return 0;
493} 557}
494 558
495void virtio_crypto_ablkcipher_finalize_req( 559static void virtio_crypto_ablkcipher_finalize_req(
496 struct virtio_crypto_request *vc_req, 560 struct virtio_crypto_sym_request *vc_sym_req,
497 struct ablkcipher_request *req, 561 struct ablkcipher_request *req,
498 int err) 562 int err)
499{ 563{
500 crypto_finalize_cipher_request(vc_req->dataq->engine, req, err); 564 crypto_finalize_cipher_request(vc_sym_req->base.dataq->engine,
501 565 req, err);
502 virtcrypto_clear_request(vc_req); 566 kzfree(vc_sym_req->iv);
567 virtcrypto_clear_request(&vc_sym_req->base);
503} 568}
504 569
505static struct crypto_alg virtio_crypto_algs[] = { { 570static struct crypto_alg virtio_crypto_algs[] = { {