162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci /* Algorithms supported by virtio crypto device 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Authors: Gonglei <arei.gonglei@huawei.com> 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD. 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <crypto/engine.h> 1062306a36Sopenharmony_ci#include <crypto/internal/skcipher.h> 1162306a36Sopenharmony_ci#include <crypto/scatterwalk.h> 1262306a36Sopenharmony_ci#include <linux/err.h> 1362306a36Sopenharmony_ci#include <linux/scatterlist.h> 1462306a36Sopenharmony_ci#include <uapi/linux/virtio_crypto.h> 1562306a36Sopenharmony_ci#include "virtio_crypto_common.h" 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_cistruct virtio_crypto_skcipher_ctx { 1962306a36Sopenharmony_ci struct virtio_crypto *vcrypto; 2062306a36Sopenharmony_ci struct crypto_skcipher *tfm; 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci struct virtio_crypto_sym_session_info enc_sess_info; 2362306a36Sopenharmony_ci struct virtio_crypto_sym_session_info dec_sess_info; 2462306a36Sopenharmony_ci}; 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_cistruct virtio_crypto_sym_request { 2762306a36Sopenharmony_ci struct virtio_crypto_request base; 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci /* Cipher or aead */ 3062306a36Sopenharmony_ci uint32_t type; 3162306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *skcipher_ctx; 3262306a36Sopenharmony_ci struct skcipher_request *skcipher_req; 3362306a36Sopenharmony_ci uint8_t *iv; 3462306a36Sopenharmony_ci /* Encryption? */ 3562306a36Sopenharmony_ci bool encrypt; 3662306a36Sopenharmony_ci}; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_cistruct virtio_crypto_algo { 3962306a36Sopenharmony_ci uint32_t algonum; 4062306a36Sopenharmony_ci uint32_t service; 4162306a36Sopenharmony_ci unsigned int active_devs; 4262306a36Sopenharmony_ci struct skcipher_engine_alg algo; 4362306a36Sopenharmony_ci}; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci/* 4662306a36Sopenharmony_ci * The algs_lock protects the below global virtio_crypto_active_devs 4762306a36Sopenharmony_ci * and crypto algorithms registion. 4862306a36Sopenharmony_ci */ 4962306a36Sopenharmony_cistatic DEFINE_MUTEX(algs_lock); 5062306a36Sopenharmony_cistatic void virtio_crypto_skcipher_finalize_req( 5162306a36Sopenharmony_ci struct virtio_crypto_sym_request *vc_sym_req, 5262306a36Sopenharmony_ci struct skcipher_request *req, 5362306a36Sopenharmony_ci int err); 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_cistatic void virtio_crypto_dataq_sym_callback 5662306a36Sopenharmony_ci (struct virtio_crypto_request *vc_req, int len) 5762306a36Sopenharmony_ci{ 5862306a36Sopenharmony_ci struct virtio_crypto_sym_request *vc_sym_req = 5962306a36Sopenharmony_ci container_of(vc_req, struct virtio_crypto_sym_request, base); 6062306a36Sopenharmony_ci struct skcipher_request *ablk_req; 6162306a36Sopenharmony_ci int error; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci /* Finish the encrypt or decrypt process */ 6462306a36Sopenharmony_ci if (vc_sym_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) { 6562306a36Sopenharmony_ci switch (vc_req->status) { 6662306a36Sopenharmony_ci case VIRTIO_CRYPTO_OK: 6762306a36Sopenharmony_ci error = 0; 6862306a36Sopenharmony_ci break; 6962306a36Sopenharmony_ci case VIRTIO_CRYPTO_INVSESS: 7062306a36Sopenharmony_ci case VIRTIO_CRYPTO_ERR: 7162306a36Sopenharmony_ci error = -EINVAL; 7262306a36Sopenharmony_ci break; 7362306a36Sopenharmony_ci case VIRTIO_CRYPTO_BADMSG: 7462306a36Sopenharmony_ci error = -EBADMSG; 7562306a36Sopenharmony_ci break; 7662306a36Sopenharmony_ci default: 7762306a36Sopenharmony_ci error = -EIO; 7862306a36Sopenharmony_ci break; 7962306a36Sopenharmony_ci } 8062306a36Sopenharmony_ci ablk_req = vc_sym_req->skcipher_req; 8162306a36Sopenharmony_ci virtio_crypto_skcipher_finalize_req(vc_sym_req, 8262306a36Sopenharmony_ci ablk_req, error); 8362306a36Sopenharmony_ci } 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_cistatic u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci u64 total = 0; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci for (total = 0; sg; sg = sg_next(sg)) 9162306a36Sopenharmony_ci total += sg->length; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci return total; 9462306a36Sopenharmony_ci} 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_cistatic int 9762306a36Sopenharmony_civirtio_crypto_alg_validate_key(int key_len, uint32_t *alg) 9862306a36Sopenharmony_ci{ 9962306a36Sopenharmony_ci switch (key_len) { 10062306a36Sopenharmony_ci case AES_KEYSIZE_128: 10162306a36Sopenharmony_ci case AES_KEYSIZE_192: 10262306a36Sopenharmony_ci case AES_KEYSIZE_256: 10362306a36Sopenharmony_ci *alg = VIRTIO_CRYPTO_CIPHER_AES_CBC; 10462306a36Sopenharmony_ci break; 10562306a36Sopenharmony_ci default: 10662306a36Sopenharmony_ci return -EINVAL; 10762306a36Sopenharmony_ci } 10862306a36Sopenharmony_ci return 0; 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_cistatic int virtio_crypto_alg_skcipher_init_session( 11262306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *ctx, 11362306a36Sopenharmony_ci uint32_t alg, const uint8_t *key, 11462306a36Sopenharmony_ci unsigned int keylen, 11562306a36Sopenharmony_ci int encrypt) 11662306a36Sopenharmony_ci{ 11762306a36Sopenharmony_ci struct scatterlist outhdr, key_sg, inhdr, *sgs[3]; 11862306a36Sopenharmony_ci struct virtio_crypto *vcrypto = ctx->vcrypto; 11962306a36Sopenharmony_ci int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT; 12062306a36Sopenharmony_ci int err; 12162306a36Sopenharmony_ci unsigned int num_out = 0, num_in = 0; 12262306a36Sopenharmony_ci struct virtio_crypto_op_ctrl_req *ctrl; 12362306a36Sopenharmony_ci struct virtio_crypto_session_input *input; 12462306a36Sopenharmony_ci struct virtio_crypto_sym_create_session_req *sym_create_session; 12562306a36Sopenharmony_ci struct virtio_crypto_ctrl_request *vc_ctrl_req; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci /* 12862306a36Sopenharmony_ci * Avoid to do DMA from the stack, switch to using 12962306a36Sopenharmony_ci * dynamically-allocated for the key 13062306a36Sopenharmony_ci */ 13162306a36Sopenharmony_ci uint8_t *cipher_key = kmemdup(key, keylen, GFP_ATOMIC); 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci if (!cipher_key) 13462306a36Sopenharmony_ci return -ENOMEM; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL); 13762306a36Sopenharmony_ci if (!vc_ctrl_req) { 13862306a36Sopenharmony_ci err = -ENOMEM; 13962306a36Sopenharmony_ci goto out; 14062306a36Sopenharmony_ci } 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci /* Pad ctrl header */ 14362306a36Sopenharmony_ci ctrl = &vc_ctrl_req->ctrl; 14462306a36Sopenharmony_ci ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION); 14562306a36Sopenharmony_ci ctrl->header.algo = cpu_to_le32(alg); 14662306a36Sopenharmony_ci /* Set the default dataqueue id to 0 */ 14762306a36Sopenharmony_ci ctrl->header.queue_id = 0; 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci input = &vc_ctrl_req->input; 15062306a36Sopenharmony_ci input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR); 15162306a36Sopenharmony_ci /* Pad cipher's parameters */ 15262306a36Sopenharmony_ci sym_create_session = &ctrl->u.sym_create_session; 15362306a36Sopenharmony_ci sym_create_session->op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER); 15462306a36Sopenharmony_ci sym_create_session->u.cipher.para.algo = ctrl->header.algo; 15562306a36Sopenharmony_ci sym_create_session->u.cipher.para.keylen = cpu_to_le32(keylen); 15662306a36Sopenharmony_ci sym_create_session->u.cipher.para.op = cpu_to_le32(op); 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci sg_init_one(&outhdr, ctrl, sizeof(*ctrl)); 15962306a36Sopenharmony_ci sgs[num_out++] = &outhdr; 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci /* Set key */ 16262306a36Sopenharmony_ci sg_init_one(&key_sg, cipher_key, keylen); 16362306a36Sopenharmony_ci sgs[num_out++] = &key_sg; 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci /* Return status and session id back */ 16662306a36Sopenharmony_ci sg_init_one(&inhdr, input, sizeof(*input)); 16762306a36Sopenharmony_ci sgs[num_out + num_in++] = &inhdr; 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req); 17062306a36Sopenharmony_ci if (err < 0) 17162306a36Sopenharmony_ci goto out; 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) { 17462306a36Sopenharmony_ci pr_err("virtio_crypto: Create session failed status: %u\n", 17562306a36Sopenharmony_ci le32_to_cpu(input->status)); 17662306a36Sopenharmony_ci err = -EINVAL; 17762306a36Sopenharmony_ci goto out; 17862306a36Sopenharmony_ci } 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci if (encrypt) 18162306a36Sopenharmony_ci ctx->enc_sess_info.session_id = le64_to_cpu(input->session_id); 18262306a36Sopenharmony_ci else 18362306a36Sopenharmony_ci ctx->dec_sess_info.session_id = le64_to_cpu(input->session_id); 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci err = 0; 18662306a36Sopenharmony_ciout: 18762306a36Sopenharmony_ci kfree(vc_ctrl_req); 18862306a36Sopenharmony_ci kfree_sensitive(cipher_key); 18962306a36Sopenharmony_ci return err; 19062306a36Sopenharmony_ci} 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_cistatic int virtio_crypto_alg_skcipher_close_session( 19362306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *ctx, 19462306a36Sopenharmony_ci int encrypt) 19562306a36Sopenharmony_ci{ 19662306a36Sopenharmony_ci struct scatterlist outhdr, status_sg, *sgs[2]; 19762306a36Sopenharmony_ci struct virtio_crypto_destroy_session_req *destroy_session; 19862306a36Sopenharmony_ci struct virtio_crypto *vcrypto = ctx->vcrypto; 19962306a36Sopenharmony_ci int err; 20062306a36Sopenharmony_ci unsigned int num_out = 0, num_in = 0; 20162306a36Sopenharmony_ci struct virtio_crypto_op_ctrl_req *ctrl; 20262306a36Sopenharmony_ci struct virtio_crypto_inhdr *ctrl_status; 20362306a36Sopenharmony_ci struct virtio_crypto_ctrl_request *vc_ctrl_req; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL); 20662306a36Sopenharmony_ci if (!vc_ctrl_req) 20762306a36Sopenharmony_ci return -ENOMEM; 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci ctrl_status = &vc_ctrl_req->ctrl_status; 21062306a36Sopenharmony_ci ctrl_status->status = VIRTIO_CRYPTO_ERR; 21162306a36Sopenharmony_ci /* Pad ctrl header */ 21262306a36Sopenharmony_ci ctrl = &vc_ctrl_req->ctrl; 21362306a36Sopenharmony_ci ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION); 21462306a36Sopenharmony_ci /* Set the default virtqueue id to 0 */ 21562306a36Sopenharmony_ci ctrl->header.queue_id = 0; 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci destroy_session = &ctrl->u.destroy_session; 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci if (encrypt) 22062306a36Sopenharmony_ci destroy_session->session_id = cpu_to_le64(ctx->enc_sess_info.session_id); 22162306a36Sopenharmony_ci else 22262306a36Sopenharmony_ci destroy_session->session_id = cpu_to_le64(ctx->dec_sess_info.session_id); 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci sg_init_one(&outhdr, ctrl, sizeof(*ctrl)); 22562306a36Sopenharmony_ci sgs[num_out++] = &outhdr; 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci /* Return status and session id back */ 22862306a36Sopenharmony_ci sg_init_one(&status_sg, &ctrl_status->status, sizeof(ctrl_status->status)); 22962306a36Sopenharmony_ci sgs[num_out + num_in++] = &status_sg; 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req); 23262306a36Sopenharmony_ci if (err < 0) 23362306a36Sopenharmony_ci goto out; 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci if (ctrl_status->status != VIRTIO_CRYPTO_OK) { 23662306a36Sopenharmony_ci pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n", 23762306a36Sopenharmony_ci ctrl_status->status, destroy_session->session_id); 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci err = -EINVAL; 24062306a36Sopenharmony_ci goto out; 24162306a36Sopenharmony_ci } 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci err = 0; 24462306a36Sopenharmony_ciout: 24562306a36Sopenharmony_ci kfree(vc_ctrl_req); 24662306a36Sopenharmony_ci return err; 24762306a36Sopenharmony_ci} 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_cistatic int virtio_crypto_alg_skcipher_init_sessions( 25062306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *ctx, 25162306a36Sopenharmony_ci const uint8_t *key, unsigned int keylen) 25262306a36Sopenharmony_ci{ 25362306a36Sopenharmony_ci uint32_t alg; 25462306a36Sopenharmony_ci int ret; 25562306a36Sopenharmony_ci struct virtio_crypto *vcrypto = ctx->vcrypto; 25662306a36Sopenharmony_ci 25762306a36Sopenharmony_ci if (keylen > vcrypto->max_cipher_key_len) { 25862306a36Sopenharmony_ci pr_err("virtio_crypto: the key is too long\n"); 25962306a36Sopenharmony_ci return -EINVAL; 26062306a36Sopenharmony_ci } 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci if (virtio_crypto_alg_validate_key(keylen, &alg)) 26362306a36Sopenharmony_ci return -EINVAL; 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci /* Create encryption session */ 26662306a36Sopenharmony_ci ret = virtio_crypto_alg_skcipher_init_session(ctx, 26762306a36Sopenharmony_ci alg, key, keylen, 1); 26862306a36Sopenharmony_ci if (ret) 26962306a36Sopenharmony_ci return ret; 27062306a36Sopenharmony_ci /* Create decryption session */ 27162306a36Sopenharmony_ci ret = virtio_crypto_alg_skcipher_init_session(ctx, 27262306a36Sopenharmony_ci alg, key, keylen, 0); 27362306a36Sopenharmony_ci if (ret) { 27462306a36Sopenharmony_ci virtio_crypto_alg_skcipher_close_session(ctx, 1); 27562306a36Sopenharmony_ci return ret; 27662306a36Sopenharmony_ci } 27762306a36Sopenharmony_ci return 0; 27862306a36Sopenharmony_ci} 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci/* Note: kernel crypto API realization */ 28162306a36Sopenharmony_cistatic int virtio_crypto_skcipher_setkey(struct crypto_skcipher *tfm, 28262306a36Sopenharmony_ci const uint8_t *key, 28362306a36Sopenharmony_ci unsigned int keylen) 28462306a36Sopenharmony_ci{ 28562306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 28662306a36Sopenharmony_ci uint32_t alg; 28762306a36Sopenharmony_ci int ret; 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci ret = virtio_crypto_alg_validate_key(keylen, &alg); 29062306a36Sopenharmony_ci if (ret) 29162306a36Sopenharmony_ci return ret; 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ci if (!ctx->vcrypto) { 29462306a36Sopenharmony_ci /* New key */ 29562306a36Sopenharmony_ci int node = virtio_crypto_get_current_node(); 29662306a36Sopenharmony_ci struct virtio_crypto *vcrypto = 29762306a36Sopenharmony_ci virtcrypto_get_dev_node(node, 29862306a36Sopenharmony_ci VIRTIO_CRYPTO_SERVICE_CIPHER, alg); 29962306a36Sopenharmony_ci if (!vcrypto) { 30062306a36Sopenharmony_ci pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n"); 30162306a36Sopenharmony_ci return -ENODEV; 30262306a36Sopenharmony_ci } 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ci ctx->vcrypto = vcrypto; 30562306a36Sopenharmony_ci } else { 30662306a36Sopenharmony_ci /* Rekeying, we should close the created sessions previously */ 30762306a36Sopenharmony_ci virtio_crypto_alg_skcipher_close_session(ctx, 1); 30862306a36Sopenharmony_ci virtio_crypto_alg_skcipher_close_session(ctx, 0); 30962306a36Sopenharmony_ci } 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci ret = virtio_crypto_alg_skcipher_init_sessions(ctx, key, keylen); 31262306a36Sopenharmony_ci if (ret) { 31362306a36Sopenharmony_ci virtcrypto_dev_put(ctx->vcrypto); 31462306a36Sopenharmony_ci ctx->vcrypto = NULL; 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ci return ret; 31762306a36Sopenharmony_ci } 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ci return 0; 32062306a36Sopenharmony_ci} 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_cistatic int 32362306a36Sopenharmony_ci__virtio_crypto_skcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req, 32462306a36Sopenharmony_ci struct skcipher_request *req, 32562306a36Sopenharmony_ci struct data_queue *data_vq) 32662306a36Sopenharmony_ci{ 32762306a36Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 32862306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *ctx = vc_sym_req->skcipher_ctx; 32962306a36Sopenharmony_ci struct virtio_crypto_request *vc_req = &vc_sym_req->base; 33062306a36Sopenharmony_ci unsigned int ivsize = crypto_skcipher_ivsize(tfm); 33162306a36Sopenharmony_ci struct virtio_crypto *vcrypto = ctx->vcrypto; 33262306a36Sopenharmony_ci struct virtio_crypto_op_data_req *req_data; 33362306a36Sopenharmony_ci int src_nents, dst_nents; 33462306a36Sopenharmony_ci int err; 33562306a36Sopenharmony_ci unsigned long flags; 33662306a36Sopenharmony_ci struct scatterlist outhdr, iv_sg, status_sg, **sgs; 33762306a36Sopenharmony_ci u64 dst_len; 33862306a36Sopenharmony_ci unsigned int num_out = 0, num_in = 0; 33962306a36Sopenharmony_ci int sg_total; 34062306a36Sopenharmony_ci uint8_t *iv; 34162306a36Sopenharmony_ci struct scatterlist *sg; 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci src_nents = sg_nents_for_len(req->src, req->cryptlen); 34462306a36Sopenharmony_ci if (src_nents < 0) { 34562306a36Sopenharmony_ci pr_err("Invalid number of src SG.\n"); 34662306a36Sopenharmony_ci return src_nents; 34762306a36Sopenharmony_ci } 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ci dst_nents = sg_nents(req->dst); 35062306a36Sopenharmony_ci 35162306a36Sopenharmony_ci pr_debug("virtio_crypto: Number of sgs (src_nents: %d, dst_nents: %d)\n", 35262306a36Sopenharmony_ci src_nents, dst_nents); 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ci /* Why 3? outhdr + iv + inhdr */ 35562306a36Sopenharmony_ci sg_total = src_nents + dst_nents + 3; 35662306a36Sopenharmony_ci sgs = kcalloc_node(sg_total, sizeof(*sgs), GFP_KERNEL, 35762306a36Sopenharmony_ci dev_to_node(&vcrypto->vdev->dev)); 35862306a36Sopenharmony_ci if (!sgs) 35962306a36Sopenharmony_ci return -ENOMEM; 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_ci req_data = kzalloc_node(sizeof(*req_data), GFP_KERNEL, 36262306a36Sopenharmony_ci dev_to_node(&vcrypto->vdev->dev)); 36362306a36Sopenharmony_ci if (!req_data) { 36462306a36Sopenharmony_ci kfree(sgs); 36562306a36Sopenharmony_ci return -ENOMEM; 36662306a36Sopenharmony_ci } 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci vc_req->req_data = req_data; 36962306a36Sopenharmony_ci vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER; 37062306a36Sopenharmony_ci /* Head of operation */ 37162306a36Sopenharmony_ci if (vc_sym_req->encrypt) { 37262306a36Sopenharmony_ci req_data->header.session_id = 37362306a36Sopenharmony_ci cpu_to_le64(ctx->enc_sess_info.session_id); 37462306a36Sopenharmony_ci req_data->header.opcode = 37562306a36Sopenharmony_ci cpu_to_le32(VIRTIO_CRYPTO_CIPHER_ENCRYPT); 37662306a36Sopenharmony_ci } else { 37762306a36Sopenharmony_ci req_data->header.session_id = 37862306a36Sopenharmony_ci cpu_to_le64(ctx->dec_sess_info.session_id); 37962306a36Sopenharmony_ci req_data->header.opcode = 38062306a36Sopenharmony_ci cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT); 38162306a36Sopenharmony_ci } 38262306a36Sopenharmony_ci req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER); 38362306a36Sopenharmony_ci req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize); 38462306a36Sopenharmony_ci req_data->u.sym_req.u.cipher.para.src_data_len = 38562306a36Sopenharmony_ci cpu_to_le32(req->cryptlen); 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci dst_len = virtio_crypto_alg_sg_nents_length(req->dst); 38862306a36Sopenharmony_ci if (unlikely(dst_len > U32_MAX)) { 38962306a36Sopenharmony_ci pr_err("virtio_crypto: The dst_len is beyond U32_MAX\n"); 39062306a36Sopenharmony_ci err = -EINVAL; 39162306a36Sopenharmony_ci goto free; 39262306a36Sopenharmony_ci } 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_ci dst_len = min_t(unsigned int, req->cryptlen, dst_len); 39562306a36Sopenharmony_ci pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n", 39662306a36Sopenharmony_ci req->cryptlen, dst_len); 39762306a36Sopenharmony_ci 39862306a36Sopenharmony_ci if (unlikely(req->cryptlen + dst_len + ivsize + 39962306a36Sopenharmony_ci sizeof(vc_req->status) > vcrypto->max_size)) { 40062306a36Sopenharmony_ci pr_err("virtio_crypto: The length is too big\n"); 40162306a36Sopenharmony_ci err = -EINVAL; 40262306a36Sopenharmony_ci goto free; 40362306a36Sopenharmony_ci } 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ci req_data->u.sym_req.u.cipher.para.dst_data_len = 40662306a36Sopenharmony_ci cpu_to_le32((uint32_t)dst_len); 40762306a36Sopenharmony_ci 40862306a36Sopenharmony_ci /* Outhdr */ 40962306a36Sopenharmony_ci sg_init_one(&outhdr, req_data, sizeof(*req_data)); 41062306a36Sopenharmony_ci sgs[num_out++] = &outhdr; 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ci /* IV */ 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_ci /* 41562306a36Sopenharmony_ci * Avoid to do DMA from the stack, switch to using 41662306a36Sopenharmony_ci * dynamically-allocated for the IV 41762306a36Sopenharmony_ci */ 41862306a36Sopenharmony_ci iv = kzalloc_node(ivsize, GFP_ATOMIC, 41962306a36Sopenharmony_ci dev_to_node(&vcrypto->vdev->dev)); 42062306a36Sopenharmony_ci if (!iv) { 42162306a36Sopenharmony_ci err = -ENOMEM; 42262306a36Sopenharmony_ci goto free; 42362306a36Sopenharmony_ci } 42462306a36Sopenharmony_ci memcpy(iv, req->iv, ivsize); 42562306a36Sopenharmony_ci if (!vc_sym_req->encrypt) 42662306a36Sopenharmony_ci scatterwalk_map_and_copy(req->iv, req->src, 42762306a36Sopenharmony_ci req->cryptlen - AES_BLOCK_SIZE, 42862306a36Sopenharmony_ci AES_BLOCK_SIZE, 0); 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci sg_init_one(&iv_sg, iv, ivsize); 43162306a36Sopenharmony_ci sgs[num_out++] = &iv_sg; 43262306a36Sopenharmony_ci vc_sym_req->iv = iv; 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci /* Source data */ 43562306a36Sopenharmony_ci for (sg = req->src; src_nents; sg = sg_next(sg), src_nents--) 43662306a36Sopenharmony_ci sgs[num_out++] = sg; 43762306a36Sopenharmony_ci 43862306a36Sopenharmony_ci /* Destination data */ 43962306a36Sopenharmony_ci for (sg = req->dst; sg; sg = sg_next(sg)) 44062306a36Sopenharmony_ci sgs[num_out + num_in++] = sg; 44162306a36Sopenharmony_ci 44262306a36Sopenharmony_ci /* Status */ 44362306a36Sopenharmony_ci sg_init_one(&status_sg, &vc_req->status, sizeof(vc_req->status)); 44462306a36Sopenharmony_ci sgs[num_out + num_in++] = &status_sg; 44562306a36Sopenharmony_ci 44662306a36Sopenharmony_ci vc_req->sgs = sgs; 44762306a36Sopenharmony_ci 44862306a36Sopenharmony_ci spin_lock_irqsave(&data_vq->lock, flags); 44962306a36Sopenharmony_ci err = virtqueue_add_sgs(data_vq->vq, sgs, num_out, 45062306a36Sopenharmony_ci num_in, vc_req, GFP_ATOMIC); 45162306a36Sopenharmony_ci virtqueue_kick(data_vq->vq); 45262306a36Sopenharmony_ci spin_unlock_irqrestore(&data_vq->lock, flags); 45362306a36Sopenharmony_ci if (unlikely(err < 0)) 45462306a36Sopenharmony_ci goto free_iv; 45562306a36Sopenharmony_ci 45662306a36Sopenharmony_ci return 0; 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_cifree_iv: 45962306a36Sopenharmony_ci kfree_sensitive(iv); 46062306a36Sopenharmony_cifree: 46162306a36Sopenharmony_ci kfree_sensitive(req_data); 46262306a36Sopenharmony_ci kfree(sgs); 46362306a36Sopenharmony_ci return err; 46462306a36Sopenharmony_ci} 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_cistatic int virtio_crypto_skcipher_encrypt(struct skcipher_request *req) 46762306a36Sopenharmony_ci{ 46862306a36Sopenharmony_ci struct crypto_skcipher *atfm = crypto_skcipher_reqtfm(req); 46962306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(atfm); 47062306a36Sopenharmony_ci struct virtio_crypto_sym_request *vc_sym_req = 47162306a36Sopenharmony_ci skcipher_request_ctx(req); 47262306a36Sopenharmony_ci struct virtio_crypto_request *vc_req = &vc_sym_req->base; 47362306a36Sopenharmony_ci struct virtio_crypto *vcrypto = ctx->vcrypto; 47462306a36Sopenharmony_ci /* Use the first data virtqueue as default */ 47562306a36Sopenharmony_ci struct data_queue *data_vq = &vcrypto->data_vq[0]; 47662306a36Sopenharmony_ci 47762306a36Sopenharmony_ci if (!req->cryptlen) 47862306a36Sopenharmony_ci return 0; 47962306a36Sopenharmony_ci if (req->cryptlen % AES_BLOCK_SIZE) 48062306a36Sopenharmony_ci return -EINVAL; 48162306a36Sopenharmony_ci 48262306a36Sopenharmony_ci vc_req->dataq = data_vq; 48362306a36Sopenharmony_ci vc_req->alg_cb = virtio_crypto_dataq_sym_callback; 48462306a36Sopenharmony_ci vc_sym_req->skcipher_ctx = ctx; 48562306a36Sopenharmony_ci vc_sym_req->skcipher_req = req; 48662306a36Sopenharmony_ci vc_sym_req->encrypt = true; 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_ci return crypto_transfer_skcipher_request_to_engine(data_vq->engine, req); 48962306a36Sopenharmony_ci} 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_cistatic int virtio_crypto_skcipher_decrypt(struct skcipher_request *req) 49262306a36Sopenharmony_ci{ 49362306a36Sopenharmony_ci struct crypto_skcipher *atfm = crypto_skcipher_reqtfm(req); 49462306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(atfm); 49562306a36Sopenharmony_ci struct virtio_crypto_sym_request *vc_sym_req = 49662306a36Sopenharmony_ci skcipher_request_ctx(req); 49762306a36Sopenharmony_ci struct virtio_crypto_request *vc_req = &vc_sym_req->base; 49862306a36Sopenharmony_ci struct virtio_crypto *vcrypto = ctx->vcrypto; 49962306a36Sopenharmony_ci /* Use the first data virtqueue as default */ 50062306a36Sopenharmony_ci struct data_queue *data_vq = &vcrypto->data_vq[0]; 50162306a36Sopenharmony_ci 50262306a36Sopenharmony_ci if (!req->cryptlen) 50362306a36Sopenharmony_ci return 0; 50462306a36Sopenharmony_ci if (req->cryptlen % AES_BLOCK_SIZE) 50562306a36Sopenharmony_ci return -EINVAL; 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_ci vc_req->dataq = data_vq; 50862306a36Sopenharmony_ci vc_req->alg_cb = virtio_crypto_dataq_sym_callback; 50962306a36Sopenharmony_ci vc_sym_req->skcipher_ctx = ctx; 51062306a36Sopenharmony_ci vc_sym_req->skcipher_req = req; 51162306a36Sopenharmony_ci vc_sym_req->encrypt = false; 51262306a36Sopenharmony_ci 51362306a36Sopenharmony_ci return crypto_transfer_skcipher_request_to_engine(data_vq->engine, req); 51462306a36Sopenharmony_ci} 51562306a36Sopenharmony_ci 51662306a36Sopenharmony_cistatic int virtio_crypto_skcipher_init(struct crypto_skcipher *tfm) 51762306a36Sopenharmony_ci{ 51862306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci crypto_skcipher_set_reqsize(tfm, sizeof(struct virtio_crypto_sym_request)); 52162306a36Sopenharmony_ci ctx->tfm = tfm; 52262306a36Sopenharmony_ci 52362306a36Sopenharmony_ci return 0; 52462306a36Sopenharmony_ci} 52562306a36Sopenharmony_ci 52662306a36Sopenharmony_cistatic void virtio_crypto_skcipher_exit(struct crypto_skcipher *tfm) 52762306a36Sopenharmony_ci{ 52862306a36Sopenharmony_ci struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci if (!ctx->vcrypto) 53162306a36Sopenharmony_ci return; 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci virtio_crypto_alg_skcipher_close_session(ctx, 1); 53462306a36Sopenharmony_ci virtio_crypto_alg_skcipher_close_session(ctx, 0); 53562306a36Sopenharmony_ci virtcrypto_dev_put(ctx->vcrypto); 53662306a36Sopenharmony_ci ctx->vcrypto = NULL; 53762306a36Sopenharmony_ci} 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ciint virtio_crypto_skcipher_crypt_req( 54062306a36Sopenharmony_ci struct crypto_engine *engine, void *vreq) 54162306a36Sopenharmony_ci{ 54262306a36Sopenharmony_ci struct skcipher_request *req = container_of(vreq, struct skcipher_request, base); 54362306a36Sopenharmony_ci struct virtio_crypto_sym_request *vc_sym_req = 54462306a36Sopenharmony_ci skcipher_request_ctx(req); 54562306a36Sopenharmony_ci struct virtio_crypto_request *vc_req = &vc_sym_req->base; 54662306a36Sopenharmony_ci struct data_queue *data_vq = vc_req->dataq; 54762306a36Sopenharmony_ci int ret; 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci ret = __virtio_crypto_skcipher_do_req(vc_sym_req, req, data_vq); 55062306a36Sopenharmony_ci if (ret < 0) 55162306a36Sopenharmony_ci return ret; 55262306a36Sopenharmony_ci 55362306a36Sopenharmony_ci virtqueue_kick(data_vq->vq); 55462306a36Sopenharmony_ci 55562306a36Sopenharmony_ci return 0; 55662306a36Sopenharmony_ci} 55762306a36Sopenharmony_ci 55862306a36Sopenharmony_cistatic void virtio_crypto_skcipher_finalize_req( 55962306a36Sopenharmony_ci struct virtio_crypto_sym_request *vc_sym_req, 56062306a36Sopenharmony_ci struct skcipher_request *req, 56162306a36Sopenharmony_ci int err) 56262306a36Sopenharmony_ci{ 56362306a36Sopenharmony_ci if (vc_sym_req->encrypt) 56462306a36Sopenharmony_ci scatterwalk_map_and_copy(req->iv, req->dst, 56562306a36Sopenharmony_ci req->cryptlen - AES_BLOCK_SIZE, 56662306a36Sopenharmony_ci AES_BLOCK_SIZE, 0); 56762306a36Sopenharmony_ci kfree_sensitive(vc_sym_req->iv); 56862306a36Sopenharmony_ci virtcrypto_clear_request(&vc_sym_req->base); 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_ci crypto_finalize_skcipher_request(vc_sym_req->base.dataq->engine, 57162306a36Sopenharmony_ci req, err); 57262306a36Sopenharmony_ci} 57362306a36Sopenharmony_ci 57462306a36Sopenharmony_cistatic struct virtio_crypto_algo virtio_crypto_algs[] = { { 57562306a36Sopenharmony_ci .algonum = VIRTIO_CRYPTO_CIPHER_AES_CBC, 57662306a36Sopenharmony_ci .service = VIRTIO_CRYPTO_SERVICE_CIPHER, 57762306a36Sopenharmony_ci .algo.base = { 57862306a36Sopenharmony_ci .base.cra_name = "cbc(aes)", 57962306a36Sopenharmony_ci .base.cra_driver_name = "virtio_crypto_aes_cbc", 58062306a36Sopenharmony_ci .base.cra_priority = 150, 58162306a36Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_ASYNC | 58262306a36Sopenharmony_ci CRYPTO_ALG_ALLOCATES_MEMORY, 58362306a36Sopenharmony_ci .base.cra_blocksize = AES_BLOCK_SIZE, 58462306a36Sopenharmony_ci .base.cra_ctxsize = sizeof(struct virtio_crypto_skcipher_ctx), 58562306a36Sopenharmony_ci .base.cra_module = THIS_MODULE, 58662306a36Sopenharmony_ci .init = virtio_crypto_skcipher_init, 58762306a36Sopenharmony_ci .exit = virtio_crypto_skcipher_exit, 58862306a36Sopenharmony_ci .setkey = virtio_crypto_skcipher_setkey, 58962306a36Sopenharmony_ci .decrypt = virtio_crypto_skcipher_decrypt, 59062306a36Sopenharmony_ci .encrypt = virtio_crypto_skcipher_encrypt, 59162306a36Sopenharmony_ci .min_keysize = AES_MIN_KEY_SIZE, 59262306a36Sopenharmony_ci .max_keysize = AES_MAX_KEY_SIZE, 59362306a36Sopenharmony_ci .ivsize = AES_BLOCK_SIZE, 59462306a36Sopenharmony_ci }, 59562306a36Sopenharmony_ci .algo.op = { 59662306a36Sopenharmony_ci .do_one_request = virtio_crypto_skcipher_crypt_req, 59762306a36Sopenharmony_ci }, 59862306a36Sopenharmony_ci} }; 59962306a36Sopenharmony_ci 60062306a36Sopenharmony_ciint virtio_crypto_skcipher_algs_register(struct virtio_crypto *vcrypto) 60162306a36Sopenharmony_ci{ 60262306a36Sopenharmony_ci int ret = 0; 60362306a36Sopenharmony_ci int i = 0; 60462306a36Sopenharmony_ci 60562306a36Sopenharmony_ci mutex_lock(&algs_lock); 60662306a36Sopenharmony_ci 60762306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) { 60862306a36Sopenharmony_ci 60962306a36Sopenharmony_ci uint32_t service = virtio_crypto_algs[i].service; 61062306a36Sopenharmony_ci uint32_t algonum = virtio_crypto_algs[i].algonum; 61162306a36Sopenharmony_ci 61262306a36Sopenharmony_ci if (!virtcrypto_algo_is_supported(vcrypto, service, algonum)) 61362306a36Sopenharmony_ci continue; 61462306a36Sopenharmony_ci 61562306a36Sopenharmony_ci if (virtio_crypto_algs[i].active_devs == 0) { 61662306a36Sopenharmony_ci ret = crypto_engine_register_skcipher(&virtio_crypto_algs[i].algo); 61762306a36Sopenharmony_ci if (ret) 61862306a36Sopenharmony_ci goto unlock; 61962306a36Sopenharmony_ci } 62062306a36Sopenharmony_ci 62162306a36Sopenharmony_ci virtio_crypto_algs[i].active_devs++; 62262306a36Sopenharmony_ci dev_info(&vcrypto->vdev->dev, "Registered algo %s\n", 62362306a36Sopenharmony_ci virtio_crypto_algs[i].algo.base.base.cra_name); 62462306a36Sopenharmony_ci } 62562306a36Sopenharmony_ci 62662306a36Sopenharmony_ciunlock: 62762306a36Sopenharmony_ci mutex_unlock(&algs_lock); 62862306a36Sopenharmony_ci return ret; 62962306a36Sopenharmony_ci} 63062306a36Sopenharmony_ci 63162306a36Sopenharmony_civoid virtio_crypto_skcipher_algs_unregister(struct virtio_crypto *vcrypto) 63262306a36Sopenharmony_ci{ 63362306a36Sopenharmony_ci int i = 0; 63462306a36Sopenharmony_ci 63562306a36Sopenharmony_ci mutex_lock(&algs_lock); 63662306a36Sopenharmony_ci 63762306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) { 63862306a36Sopenharmony_ci 63962306a36Sopenharmony_ci uint32_t service = virtio_crypto_algs[i].service; 64062306a36Sopenharmony_ci uint32_t algonum = virtio_crypto_algs[i].algonum; 64162306a36Sopenharmony_ci 64262306a36Sopenharmony_ci if (virtio_crypto_algs[i].active_devs == 0 || 64362306a36Sopenharmony_ci !virtcrypto_algo_is_supported(vcrypto, service, algonum)) 64462306a36Sopenharmony_ci continue; 64562306a36Sopenharmony_ci 64662306a36Sopenharmony_ci if (virtio_crypto_algs[i].active_devs == 1) 64762306a36Sopenharmony_ci crypto_engine_unregister_skcipher(&virtio_crypto_algs[i].algo); 64862306a36Sopenharmony_ci 64962306a36Sopenharmony_ci virtio_crypto_algs[i].active_devs--; 65062306a36Sopenharmony_ci } 65162306a36Sopenharmony_ci 65262306a36Sopenharmony_ci mutex_unlock(&algs_lock); 65362306a36Sopenharmony_ci} 654