1ea993de1Szhenwei pi // SPDX-License-Identifier: GPL-2.0-or-later
2ea993de1Szhenwei pi /* Algorithms supported by virtio crypto device
3ea993de1Szhenwei pi *
4ea993de1Szhenwei pi * Authors: Gonglei <arei.gonglei@huawei.com>
5ea993de1Szhenwei pi *
6ea993de1Szhenwei pi * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
7ea993de1Szhenwei pi */
8ea993de1Szhenwei pi
9*7a2673d7SHerbert Xu #include <crypto/engine.h>
10ea993de1Szhenwei pi #include <crypto/internal/skcipher.h>
11ea993de1Szhenwei pi #include <crypto/scatterwalk.h>
12*7a2673d7SHerbert Xu #include <linux/err.h>
13*7a2673d7SHerbert Xu #include <linux/scatterlist.h>
14ea993de1Szhenwei pi #include <uapi/linux/virtio_crypto.h>
15ea993de1Szhenwei pi #include "virtio_crypto_common.h"
16ea993de1Szhenwei pi
17ea993de1Szhenwei pi
18ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx {
19ea993de1Szhenwei pi struct virtio_crypto *vcrypto;
20ea993de1Szhenwei pi struct crypto_skcipher *tfm;
21ea993de1Szhenwei pi
22ea993de1Szhenwei pi struct virtio_crypto_sym_session_info enc_sess_info;
23ea993de1Szhenwei pi struct virtio_crypto_sym_session_info dec_sess_info;
24ea993de1Szhenwei pi };
25ea993de1Szhenwei pi
26ea993de1Szhenwei pi struct virtio_crypto_sym_request {
27ea993de1Szhenwei pi struct virtio_crypto_request base;
28ea993de1Szhenwei pi
29ea993de1Szhenwei pi /* Cipher or aead */
30ea993de1Szhenwei pi uint32_t type;
31ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *skcipher_ctx;
32ea993de1Szhenwei pi struct skcipher_request *skcipher_req;
33ea993de1Szhenwei pi uint8_t *iv;
34ea993de1Szhenwei pi /* Encryption? */
35ea993de1Szhenwei pi bool encrypt;
36ea993de1Szhenwei pi };
37ea993de1Szhenwei pi
38ea993de1Szhenwei pi struct virtio_crypto_algo {
39ea993de1Szhenwei pi uint32_t algonum;
40ea993de1Szhenwei pi uint32_t service;
41ea993de1Szhenwei pi unsigned int active_devs;
42*7a2673d7SHerbert Xu struct skcipher_engine_alg algo;
43ea993de1Szhenwei pi };
44ea993de1Szhenwei pi
45ea993de1Szhenwei pi /*
46ea993de1Szhenwei pi * The algs_lock protects the below global virtio_crypto_active_devs
47ea993de1Szhenwei pi * and crypto algorithms registion.
48ea993de1Szhenwei pi */
49ea993de1Szhenwei pi static DEFINE_MUTEX(algs_lock);
50ea993de1Szhenwei pi static void virtio_crypto_skcipher_finalize_req(
51ea993de1Szhenwei pi struct virtio_crypto_sym_request *vc_sym_req,
52ea993de1Szhenwei pi struct skcipher_request *req,
53ea993de1Szhenwei pi int err);
54ea993de1Szhenwei pi
virtio_crypto_dataq_sym_callback(struct virtio_crypto_request * vc_req,int len)55ea993de1Szhenwei pi static void virtio_crypto_dataq_sym_callback
56ea993de1Szhenwei pi (struct virtio_crypto_request *vc_req, int len)
57ea993de1Szhenwei pi {
58ea993de1Szhenwei pi struct virtio_crypto_sym_request *vc_sym_req =
59ea993de1Szhenwei pi container_of(vc_req, struct virtio_crypto_sym_request, base);
60ea993de1Szhenwei pi struct skcipher_request *ablk_req;
61ea993de1Szhenwei pi int error;
62ea993de1Szhenwei pi
63ea993de1Szhenwei pi /* Finish the encrypt or decrypt process */
64ea993de1Szhenwei pi if (vc_sym_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
65ea993de1Szhenwei pi switch (vc_req->status) {
66ea993de1Szhenwei pi case VIRTIO_CRYPTO_OK:
67ea993de1Szhenwei pi error = 0;
68ea993de1Szhenwei pi break;
69ea993de1Szhenwei pi case VIRTIO_CRYPTO_INVSESS:
70ea993de1Szhenwei pi case VIRTIO_CRYPTO_ERR:
71ea993de1Szhenwei pi error = -EINVAL;
72ea993de1Szhenwei pi break;
73ea993de1Szhenwei pi case VIRTIO_CRYPTO_BADMSG:
74ea993de1Szhenwei pi error = -EBADMSG;
75ea993de1Szhenwei pi break;
76ea993de1Szhenwei pi default:
77ea993de1Szhenwei pi error = -EIO;
78ea993de1Szhenwei pi break;
79ea993de1Szhenwei pi }
80ea993de1Szhenwei pi ablk_req = vc_sym_req->skcipher_req;
81ea993de1Szhenwei pi virtio_crypto_skcipher_finalize_req(vc_sym_req,
82ea993de1Szhenwei pi ablk_req, error);
83ea993de1Szhenwei pi }
84ea993de1Szhenwei pi }
85ea993de1Szhenwei pi
virtio_crypto_alg_sg_nents_length(struct scatterlist * sg)86ea993de1Szhenwei pi static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg)
87ea993de1Szhenwei pi {
88ea993de1Szhenwei pi u64 total = 0;
89ea993de1Szhenwei pi
90ea993de1Szhenwei pi for (total = 0; sg; sg = sg_next(sg))
91ea993de1Szhenwei pi total += sg->length;
92ea993de1Szhenwei pi
93ea993de1Szhenwei pi return total;
94ea993de1Szhenwei pi }
95ea993de1Szhenwei pi
96ea993de1Szhenwei pi static int
virtio_crypto_alg_validate_key(int key_len,uint32_t * alg)97ea993de1Szhenwei pi virtio_crypto_alg_validate_key(int key_len, uint32_t *alg)
98ea993de1Szhenwei pi {
99ea993de1Szhenwei pi switch (key_len) {
100ea993de1Szhenwei pi case AES_KEYSIZE_128:
101ea993de1Szhenwei pi case AES_KEYSIZE_192:
102ea993de1Szhenwei pi case AES_KEYSIZE_256:
103ea993de1Szhenwei pi *alg = VIRTIO_CRYPTO_CIPHER_AES_CBC;
104ea993de1Szhenwei pi break;
105ea993de1Szhenwei pi default:
106ea993de1Szhenwei pi return -EINVAL;
107ea993de1Szhenwei pi }
108ea993de1Szhenwei pi return 0;
109ea993de1Szhenwei pi }
110ea993de1Szhenwei pi
virtio_crypto_alg_skcipher_init_session(struct virtio_crypto_skcipher_ctx * ctx,uint32_t alg,const uint8_t * key,unsigned int keylen,int encrypt)111ea993de1Szhenwei pi static int virtio_crypto_alg_skcipher_init_session(
112ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *ctx,
113ea993de1Szhenwei pi uint32_t alg, const uint8_t *key,
114ea993de1Szhenwei pi unsigned int keylen,
115ea993de1Szhenwei pi int encrypt)
116ea993de1Szhenwei pi {
117ea993de1Szhenwei pi struct scatterlist outhdr, key_sg, inhdr, *sgs[3];
118ea993de1Szhenwei pi struct virtio_crypto *vcrypto = ctx->vcrypto;
119ea993de1Szhenwei pi int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT;
120ea993de1Szhenwei pi int err;
121ea993de1Szhenwei pi unsigned int num_out = 0, num_in = 0;
1226fd763d1Szhenwei pi struct virtio_crypto_op_ctrl_req *ctrl;
1236fd763d1Szhenwei pi struct virtio_crypto_session_input *input;
1246fd763d1Szhenwei pi struct virtio_crypto_sym_create_session_req *sym_create_session;
1250756ad15Szhenwei pi struct virtio_crypto_ctrl_request *vc_ctrl_req;
126ea993de1Szhenwei pi
127ea993de1Szhenwei pi /*
128ea993de1Szhenwei pi * Avoid to do DMA from the stack, switch to using
129ea993de1Szhenwei pi * dynamically-allocated for the key
130ea993de1Szhenwei pi */
131ea993de1Szhenwei pi uint8_t *cipher_key = kmemdup(key, keylen, GFP_ATOMIC);
132ea993de1Szhenwei pi
133ea993de1Szhenwei pi if (!cipher_key)
134ea993de1Szhenwei pi return -ENOMEM;
135ea993de1Szhenwei pi
1360756ad15Szhenwei pi vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
1370756ad15Szhenwei pi if (!vc_ctrl_req) {
1380756ad15Szhenwei pi err = -ENOMEM;
1390756ad15Szhenwei pi goto out;
1400756ad15Szhenwei pi }
1410756ad15Szhenwei pi
142ea993de1Szhenwei pi /* Pad ctrl header */
1430756ad15Szhenwei pi ctrl = &vc_ctrl_req->ctrl;
1446fd763d1Szhenwei pi ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
1456fd763d1Szhenwei pi ctrl->header.algo = cpu_to_le32(alg);
146ea993de1Szhenwei pi /* Set the default dataqueue id to 0 */
1476fd763d1Szhenwei pi ctrl->header.queue_id = 0;
148ea993de1Szhenwei pi
1490756ad15Szhenwei pi input = &vc_ctrl_req->input;
1506fd763d1Szhenwei pi input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
151ea993de1Szhenwei pi /* Pad cipher's parameters */
1526fd763d1Szhenwei pi sym_create_session = &ctrl->u.sym_create_session;
1536fd763d1Szhenwei pi sym_create_session->op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
1546fd763d1Szhenwei pi sym_create_session->u.cipher.para.algo = ctrl->header.algo;
1556fd763d1Szhenwei pi sym_create_session->u.cipher.para.keylen = cpu_to_le32(keylen);
1566fd763d1Szhenwei pi sym_create_session->u.cipher.para.op = cpu_to_le32(op);
157ea993de1Szhenwei pi
1586fd763d1Szhenwei pi sg_init_one(&outhdr, ctrl, sizeof(*ctrl));
159ea993de1Szhenwei pi sgs[num_out++] = &outhdr;
160ea993de1Szhenwei pi
161ea993de1Szhenwei pi /* Set key */
162ea993de1Szhenwei pi sg_init_one(&key_sg, cipher_key, keylen);
163ea993de1Szhenwei pi sgs[num_out++] = &key_sg;
164ea993de1Szhenwei pi
165ea993de1Szhenwei pi /* Return status and session id back */
1666fd763d1Szhenwei pi sg_init_one(&inhdr, input, sizeof(*input));
167ea993de1Szhenwei pi sgs[num_out + num_in++] = &inhdr;
168ea993de1Szhenwei pi
169977231e8Szhenwei pi err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
170977231e8Szhenwei pi if (err < 0)
1710756ad15Szhenwei pi goto out;
172ea993de1Szhenwei pi
1736fd763d1Szhenwei pi if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
174ea993de1Szhenwei pi pr_err("virtio_crypto: Create session failed status: %u\n",
1756fd763d1Szhenwei pi le32_to_cpu(input->status));
1760756ad15Szhenwei pi err = -EINVAL;
1770756ad15Szhenwei pi goto out;
178ea993de1Szhenwei pi }
179ea993de1Szhenwei pi
180ea993de1Szhenwei pi if (encrypt)
1816fd763d1Szhenwei pi ctx->enc_sess_info.session_id = le64_to_cpu(input->session_id);
182ea993de1Szhenwei pi else
1836fd763d1Szhenwei pi ctx->dec_sess_info.session_id = le64_to_cpu(input->session_id);
184ea993de1Szhenwei pi
1850756ad15Szhenwei pi err = 0;
1860756ad15Szhenwei pi out:
1870756ad15Szhenwei pi kfree(vc_ctrl_req);
188ea993de1Szhenwei pi kfree_sensitive(cipher_key);
1890756ad15Szhenwei pi return err;
190ea993de1Szhenwei pi }
191ea993de1Szhenwei pi
virtio_crypto_alg_skcipher_close_session(struct virtio_crypto_skcipher_ctx * ctx,int encrypt)192ea993de1Szhenwei pi static int virtio_crypto_alg_skcipher_close_session(
193ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *ctx,
194ea993de1Szhenwei pi int encrypt)
195ea993de1Szhenwei pi {
196ea993de1Szhenwei pi struct scatterlist outhdr, status_sg, *sgs[2];
197ea993de1Szhenwei pi struct virtio_crypto_destroy_session_req *destroy_session;
198ea993de1Szhenwei pi struct virtio_crypto *vcrypto = ctx->vcrypto;
199ea993de1Szhenwei pi int err;
200ea993de1Szhenwei pi unsigned int num_out = 0, num_in = 0;
2016fd763d1Szhenwei pi struct virtio_crypto_op_ctrl_req *ctrl;
2026fd763d1Szhenwei pi struct virtio_crypto_inhdr *ctrl_status;
2030756ad15Szhenwei pi struct virtio_crypto_ctrl_request *vc_ctrl_req;
204ea993de1Szhenwei pi
2050756ad15Szhenwei pi vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
2060756ad15Szhenwei pi if (!vc_ctrl_req)
2070756ad15Szhenwei pi return -ENOMEM;
2080756ad15Szhenwei pi
2090756ad15Szhenwei pi ctrl_status = &vc_ctrl_req->ctrl_status;
2106fd763d1Szhenwei pi ctrl_status->status = VIRTIO_CRYPTO_ERR;
211ea993de1Szhenwei pi /* Pad ctrl header */
2120756ad15Szhenwei pi ctrl = &vc_ctrl_req->ctrl;
2136fd763d1Szhenwei pi ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
214ea993de1Szhenwei pi /* Set the default virtqueue id to 0 */
2156fd763d1Szhenwei pi ctrl->header.queue_id = 0;
216ea993de1Szhenwei pi
2176fd763d1Szhenwei pi destroy_session = &ctrl->u.destroy_session;
218ea993de1Szhenwei pi
219ea993de1Szhenwei pi if (encrypt)
2206fd763d1Szhenwei pi destroy_session->session_id = cpu_to_le64(ctx->enc_sess_info.session_id);
221ea993de1Szhenwei pi else
2226fd763d1Szhenwei pi destroy_session->session_id = cpu_to_le64(ctx->dec_sess_info.session_id);
223ea993de1Szhenwei pi
2246fd763d1Szhenwei pi sg_init_one(&outhdr, ctrl, sizeof(*ctrl));
225ea993de1Szhenwei pi sgs[num_out++] = &outhdr;
226ea993de1Szhenwei pi
227ea993de1Szhenwei pi /* Return status and session id back */
2286fd763d1Szhenwei pi sg_init_one(&status_sg, &ctrl_status->status, sizeof(ctrl_status->status));
229ea993de1Szhenwei pi sgs[num_out + num_in++] = &status_sg;
230ea993de1Szhenwei pi
231977231e8Szhenwei pi err = virtio_crypto_ctrl_vq_request(vcrypto, sgs, num_out, num_in, vc_ctrl_req);
232977231e8Szhenwei pi if (err < 0)
2330756ad15Szhenwei pi goto out;
234ea993de1Szhenwei pi
2356fd763d1Szhenwei pi if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
236ea993de1Szhenwei pi pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
2376fd763d1Szhenwei pi ctrl_status->status, destroy_session->session_id);
238ea993de1Szhenwei pi
239b1d65f71SWei Yongjun err = -EINVAL;
240b1d65f71SWei Yongjun goto out;
241ea993de1Szhenwei pi }
242ea993de1Szhenwei pi
2430756ad15Szhenwei pi err = 0;
2440756ad15Szhenwei pi out:
2450756ad15Szhenwei pi kfree(vc_ctrl_req);
2460756ad15Szhenwei pi return err;
247ea993de1Szhenwei pi }
248ea993de1Szhenwei pi
virtio_crypto_alg_skcipher_init_sessions(struct virtio_crypto_skcipher_ctx * ctx,const uint8_t * key,unsigned int keylen)249ea993de1Szhenwei pi static int virtio_crypto_alg_skcipher_init_sessions(
250ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *ctx,
251ea993de1Szhenwei pi const uint8_t *key, unsigned int keylen)
252ea993de1Szhenwei pi {
253ea993de1Szhenwei pi uint32_t alg;
254ea993de1Szhenwei pi int ret;
255ea993de1Szhenwei pi struct virtio_crypto *vcrypto = ctx->vcrypto;
256ea993de1Szhenwei pi
257ea993de1Szhenwei pi if (keylen > vcrypto->max_cipher_key_len) {
258ea993de1Szhenwei pi pr_err("virtio_crypto: the key is too long\n");
259ea993de1Szhenwei pi return -EINVAL;
260ea993de1Szhenwei pi }
261ea993de1Szhenwei pi
262ea993de1Szhenwei pi if (virtio_crypto_alg_validate_key(keylen, &alg))
263ea993de1Szhenwei pi return -EINVAL;
264ea993de1Szhenwei pi
265ea993de1Szhenwei pi /* Create encryption session */
266ea993de1Szhenwei pi ret = virtio_crypto_alg_skcipher_init_session(ctx,
267ea993de1Szhenwei pi alg, key, keylen, 1);
268ea993de1Szhenwei pi if (ret)
269ea993de1Szhenwei pi return ret;
270ea993de1Szhenwei pi /* Create decryption session */
271ea993de1Szhenwei pi ret = virtio_crypto_alg_skcipher_init_session(ctx,
272ea993de1Szhenwei pi alg, key, keylen, 0);
273ea993de1Szhenwei pi if (ret) {
274ea993de1Szhenwei pi virtio_crypto_alg_skcipher_close_session(ctx, 1);
275ea993de1Szhenwei pi return ret;
276ea993de1Szhenwei pi }
277ea993de1Szhenwei pi return 0;
278ea993de1Szhenwei pi }
279ea993de1Szhenwei pi
280ea993de1Szhenwei pi /* Note: kernel crypto API realization */
virtio_crypto_skcipher_setkey(struct crypto_skcipher * tfm,const uint8_t * key,unsigned int keylen)281ea993de1Szhenwei pi static int virtio_crypto_skcipher_setkey(struct crypto_skcipher *tfm,
282ea993de1Szhenwei pi const uint8_t *key,
283ea993de1Szhenwei pi unsigned int keylen)
284ea993de1Szhenwei pi {
285ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
286ea993de1Szhenwei pi uint32_t alg;
287ea993de1Szhenwei pi int ret;
288ea993de1Szhenwei pi
289ea993de1Szhenwei pi ret = virtio_crypto_alg_validate_key(keylen, &alg);
290ea993de1Szhenwei pi if (ret)
291ea993de1Szhenwei pi return ret;
292ea993de1Szhenwei pi
293ea993de1Szhenwei pi if (!ctx->vcrypto) {
294ea993de1Szhenwei pi /* New key */
295ea993de1Szhenwei pi int node = virtio_crypto_get_current_node();
296ea993de1Szhenwei pi struct virtio_crypto *vcrypto =
297ea993de1Szhenwei pi virtcrypto_get_dev_node(node,
298ea993de1Szhenwei pi VIRTIO_CRYPTO_SERVICE_CIPHER, alg);
299ea993de1Szhenwei pi if (!vcrypto) {
300ea993de1Szhenwei pi pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n");
301ea993de1Szhenwei pi return -ENODEV;
302ea993de1Szhenwei pi }
303ea993de1Szhenwei pi
304ea993de1Szhenwei pi ctx->vcrypto = vcrypto;
305ea993de1Szhenwei pi } else {
306ea993de1Szhenwei pi /* Rekeying, we should close the created sessions previously */
307ea993de1Szhenwei pi virtio_crypto_alg_skcipher_close_session(ctx, 1);
308ea993de1Szhenwei pi virtio_crypto_alg_skcipher_close_session(ctx, 0);
309ea993de1Szhenwei pi }
310ea993de1Szhenwei pi
311ea993de1Szhenwei pi ret = virtio_crypto_alg_skcipher_init_sessions(ctx, key, keylen);
312ea993de1Szhenwei pi if (ret) {
313ea993de1Szhenwei pi virtcrypto_dev_put(ctx->vcrypto);
314ea993de1Szhenwei pi ctx->vcrypto = NULL;
315ea993de1Szhenwei pi
316ea993de1Szhenwei pi return ret;
317ea993de1Szhenwei pi }
318ea993de1Szhenwei pi
319ea993de1Szhenwei pi return 0;
320ea993de1Szhenwei pi }
321ea993de1Szhenwei pi
322ea993de1Szhenwei pi static int
__virtio_crypto_skcipher_do_req(struct virtio_crypto_sym_request * vc_sym_req,struct skcipher_request * req,struct data_queue * data_vq)323ea993de1Szhenwei pi __virtio_crypto_skcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req,
324ea993de1Szhenwei pi struct skcipher_request *req,
325ea993de1Szhenwei pi struct data_queue *data_vq)
326ea993de1Szhenwei pi {
327ea993de1Szhenwei pi struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
328ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *ctx = vc_sym_req->skcipher_ctx;
329ea993de1Szhenwei pi struct virtio_crypto_request *vc_req = &vc_sym_req->base;
330ea993de1Szhenwei pi unsigned int ivsize = crypto_skcipher_ivsize(tfm);
331ea993de1Szhenwei pi struct virtio_crypto *vcrypto = ctx->vcrypto;
332ea993de1Szhenwei pi struct virtio_crypto_op_data_req *req_data;
333ea993de1Szhenwei pi int src_nents, dst_nents;
334ea993de1Szhenwei pi int err;
335ea993de1Szhenwei pi unsigned long flags;
336ea993de1Szhenwei pi struct scatterlist outhdr, iv_sg, status_sg, **sgs;
337ea993de1Szhenwei pi u64 dst_len;
338ea993de1Szhenwei pi unsigned int num_out = 0, num_in = 0;
339ea993de1Szhenwei pi int sg_total;
340ea993de1Szhenwei pi uint8_t *iv;
341ea993de1Szhenwei pi struct scatterlist *sg;
342ea993de1Szhenwei pi
343ea993de1Szhenwei pi src_nents = sg_nents_for_len(req->src, req->cryptlen);
344ea993de1Szhenwei pi if (src_nents < 0) {
345ea993de1Szhenwei pi pr_err("Invalid number of src SG.\n");
346ea993de1Szhenwei pi return src_nents;
347ea993de1Szhenwei pi }
348ea993de1Szhenwei pi
349ea993de1Szhenwei pi dst_nents = sg_nents(req->dst);
350ea993de1Szhenwei pi
351ea993de1Szhenwei pi pr_debug("virtio_crypto: Number of sgs (src_nents: %d, dst_nents: %d)\n",
352ea993de1Szhenwei pi src_nents, dst_nents);
353ea993de1Szhenwei pi
354ea993de1Szhenwei pi /* Why 3? outhdr + iv + inhdr */
355ea993de1Szhenwei pi sg_total = src_nents + dst_nents + 3;
356ea993de1Szhenwei pi sgs = kcalloc_node(sg_total, sizeof(*sgs), GFP_KERNEL,
357ea993de1Szhenwei pi dev_to_node(&vcrypto->vdev->dev));
358ea993de1Szhenwei pi if (!sgs)
359ea993de1Szhenwei pi return -ENOMEM;
360ea993de1Szhenwei pi
361ea993de1Szhenwei pi req_data = kzalloc_node(sizeof(*req_data), GFP_KERNEL,
362ea993de1Szhenwei pi dev_to_node(&vcrypto->vdev->dev));
363ea993de1Szhenwei pi if (!req_data) {
364ea993de1Szhenwei pi kfree(sgs);
365ea993de1Szhenwei pi return -ENOMEM;
366ea993de1Szhenwei pi }
367ea993de1Szhenwei pi
368ea993de1Szhenwei pi vc_req->req_data = req_data;
369ea993de1Szhenwei pi vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
370ea993de1Szhenwei pi /* Head of operation */
371ea993de1Szhenwei pi if (vc_sym_req->encrypt) {
372ea993de1Szhenwei pi req_data->header.session_id =
373ea993de1Szhenwei pi cpu_to_le64(ctx->enc_sess_info.session_id);
374ea993de1Szhenwei pi req_data->header.opcode =
375ea993de1Szhenwei pi cpu_to_le32(VIRTIO_CRYPTO_CIPHER_ENCRYPT);
376ea993de1Szhenwei pi } else {
377ea993de1Szhenwei pi req_data->header.session_id =
378ea993de1Szhenwei pi cpu_to_le64(ctx->dec_sess_info.session_id);
379ea993de1Szhenwei pi req_data->header.opcode =
380ea993de1Szhenwei pi cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT);
381ea993de1Szhenwei pi }
382ea993de1Szhenwei pi req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
383ea993de1Szhenwei pi req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize);
384ea993de1Szhenwei pi req_data->u.sym_req.u.cipher.para.src_data_len =
385ea993de1Szhenwei pi cpu_to_le32(req->cryptlen);
386ea993de1Szhenwei pi
387ea993de1Szhenwei pi dst_len = virtio_crypto_alg_sg_nents_length(req->dst);
388ea993de1Szhenwei pi if (unlikely(dst_len > U32_MAX)) {
389ea993de1Szhenwei pi pr_err("virtio_crypto: The dst_len is beyond U32_MAX\n");
390ea993de1Szhenwei pi err = -EINVAL;
391ea993de1Szhenwei pi goto free;
392ea993de1Szhenwei pi }
393ea993de1Szhenwei pi
394ea993de1Szhenwei pi dst_len = min_t(unsigned int, req->cryptlen, dst_len);
395ea993de1Szhenwei pi pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n",
396ea993de1Szhenwei pi req->cryptlen, dst_len);
397ea993de1Szhenwei pi
398ea993de1Szhenwei pi if (unlikely(req->cryptlen + dst_len + ivsize +
399ea993de1Szhenwei pi sizeof(vc_req->status) > vcrypto->max_size)) {
400ea993de1Szhenwei pi pr_err("virtio_crypto: The length is too big\n");
401ea993de1Szhenwei pi err = -EINVAL;
402ea993de1Szhenwei pi goto free;
403ea993de1Szhenwei pi }
404ea993de1Szhenwei pi
405ea993de1Szhenwei pi req_data->u.sym_req.u.cipher.para.dst_data_len =
406ea993de1Szhenwei pi cpu_to_le32((uint32_t)dst_len);
407ea993de1Szhenwei pi
408ea993de1Szhenwei pi /* Outhdr */
409ea993de1Szhenwei pi sg_init_one(&outhdr, req_data, sizeof(*req_data));
410ea993de1Szhenwei pi sgs[num_out++] = &outhdr;
411ea993de1Szhenwei pi
412ea993de1Szhenwei pi /* IV */
413ea993de1Szhenwei pi
414ea993de1Szhenwei pi /*
415ea993de1Szhenwei pi * Avoid to do DMA from the stack, switch to using
416ea993de1Szhenwei pi * dynamically-allocated for the IV
417ea993de1Szhenwei pi */
418ea993de1Szhenwei pi iv = kzalloc_node(ivsize, GFP_ATOMIC,
419ea993de1Szhenwei pi dev_to_node(&vcrypto->vdev->dev));
420ea993de1Szhenwei pi if (!iv) {
421ea993de1Szhenwei pi err = -ENOMEM;
422ea993de1Szhenwei pi goto free;
423ea993de1Szhenwei pi }
424ea993de1Szhenwei pi memcpy(iv, req->iv, ivsize);
425ea993de1Szhenwei pi if (!vc_sym_req->encrypt)
426ea993de1Szhenwei pi scatterwalk_map_and_copy(req->iv, req->src,
427ea993de1Szhenwei pi req->cryptlen - AES_BLOCK_SIZE,
428ea993de1Szhenwei pi AES_BLOCK_SIZE, 0);
429ea993de1Szhenwei pi
430ea993de1Szhenwei pi sg_init_one(&iv_sg, iv, ivsize);
431ea993de1Szhenwei pi sgs[num_out++] = &iv_sg;
432ea993de1Szhenwei pi vc_sym_req->iv = iv;
433ea993de1Szhenwei pi
434ea993de1Szhenwei pi /* Source data */
435ea993de1Szhenwei pi for (sg = req->src; src_nents; sg = sg_next(sg), src_nents--)
436ea993de1Szhenwei pi sgs[num_out++] = sg;
437ea993de1Szhenwei pi
438ea993de1Szhenwei pi /* Destination data */
439ea993de1Szhenwei pi for (sg = req->dst; sg; sg = sg_next(sg))
440ea993de1Szhenwei pi sgs[num_out + num_in++] = sg;
441ea993de1Szhenwei pi
442ea993de1Szhenwei pi /* Status */
443ea993de1Szhenwei pi sg_init_one(&status_sg, &vc_req->status, sizeof(vc_req->status));
444ea993de1Szhenwei pi sgs[num_out + num_in++] = &status_sg;
445ea993de1Szhenwei pi
446ea993de1Szhenwei pi vc_req->sgs = sgs;
447ea993de1Szhenwei pi
448ea993de1Szhenwei pi spin_lock_irqsave(&data_vq->lock, flags);
449ea993de1Szhenwei pi err = virtqueue_add_sgs(data_vq->vq, sgs, num_out,
450ea993de1Szhenwei pi num_in, vc_req, GFP_ATOMIC);
451ea993de1Szhenwei pi virtqueue_kick(data_vq->vq);
452ea993de1Szhenwei pi spin_unlock_irqrestore(&data_vq->lock, flags);
453ea993de1Szhenwei pi if (unlikely(err < 0))
454ea993de1Szhenwei pi goto free_iv;
455ea993de1Szhenwei pi
456ea993de1Szhenwei pi return 0;
457ea993de1Szhenwei pi
458ea993de1Szhenwei pi free_iv:
459ea993de1Szhenwei pi kfree_sensitive(iv);
460ea993de1Szhenwei pi free:
461ea993de1Szhenwei pi kfree_sensitive(req_data);
462ea993de1Szhenwei pi kfree(sgs);
463ea993de1Szhenwei pi return err;
464ea993de1Szhenwei pi }
465ea993de1Szhenwei pi
virtio_crypto_skcipher_encrypt(struct skcipher_request * req)466ea993de1Szhenwei pi static int virtio_crypto_skcipher_encrypt(struct skcipher_request *req)
467ea993de1Szhenwei pi {
468ea993de1Szhenwei pi struct crypto_skcipher *atfm = crypto_skcipher_reqtfm(req);
469ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(atfm);
470ea993de1Szhenwei pi struct virtio_crypto_sym_request *vc_sym_req =
471ea993de1Szhenwei pi skcipher_request_ctx(req);
472ea993de1Szhenwei pi struct virtio_crypto_request *vc_req = &vc_sym_req->base;
473ea993de1Szhenwei pi struct virtio_crypto *vcrypto = ctx->vcrypto;
474ea993de1Szhenwei pi /* Use the first data virtqueue as default */
475ea993de1Szhenwei pi struct data_queue *data_vq = &vcrypto->data_vq[0];
476ea993de1Szhenwei pi
477ea993de1Szhenwei pi if (!req->cryptlen)
478ea993de1Szhenwei pi return 0;
479ea993de1Szhenwei pi if (req->cryptlen % AES_BLOCK_SIZE)
480ea993de1Szhenwei pi return -EINVAL;
481ea993de1Szhenwei pi
482ea993de1Szhenwei pi vc_req->dataq = data_vq;
483ea993de1Szhenwei pi vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
484ea993de1Szhenwei pi vc_sym_req->skcipher_ctx = ctx;
485ea993de1Szhenwei pi vc_sym_req->skcipher_req = req;
486ea993de1Szhenwei pi vc_sym_req->encrypt = true;
487ea993de1Szhenwei pi
488ea993de1Szhenwei pi return crypto_transfer_skcipher_request_to_engine(data_vq->engine, req);
489ea993de1Szhenwei pi }
490ea993de1Szhenwei pi
virtio_crypto_skcipher_decrypt(struct skcipher_request * req)491ea993de1Szhenwei pi static int virtio_crypto_skcipher_decrypt(struct skcipher_request *req)
492ea993de1Szhenwei pi {
493ea993de1Szhenwei pi struct crypto_skcipher *atfm = crypto_skcipher_reqtfm(req);
494ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(atfm);
495ea993de1Szhenwei pi struct virtio_crypto_sym_request *vc_sym_req =
496ea993de1Szhenwei pi skcipher_request_ctx(req);
497ea993de1Szhenwei pi struct virtio_crypto_request *vc_req = &vc_sym_req->base;
498ea993de1Szhenwei pi struct virtio_crypto *vcrypto = ctx->vcrypto;
499ea993de1Szhenwei pi /* Use the first data virtqueue as default */
500ea993de1Szhenwei pi struct data_queue *data_vq = &vcrypto->data_vq[0];
501ea993de1Szhenwei pi
502ea993de1Szhenwei pi if (!req->cryptlen)
503ea993de1Szhenwei pi return 0;
504ea993de1Szhenwei pi if (req->cryptlen % AES_BLOCK_SIZE)
505ea993de1Szhenwei pi return -EINVAL;
506ea993de1Szhenwei pi
507ea993de1Szhenwei pi vc_req->dataq = data_vq;
508ea993de1Szhenwei pi vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
509ea993de1Szhenwei pi vc_sym_req->skcipher_ctx = ctx;
510ea993de1Szhenwei pi vc_sym_req->skcipher_req = req;
511ea993de1Szhenwei pi vc_sym_req->encrypt = false;
512ea993de1Szhenwei pi
513ea993de1Szhenwei pi return crypto_transfer_skcipher_request_to_engine(data_vq->engine, req);
514ea993de1Szhenwei pi }
515ea993de1Szhenwei pi
virtio_crypto_skcipher_init(struct crypto_skcipher * tfm)516ea993de1Szhenwei pi static int virtio_crypto_skcipher_init(struct crypto_skcipher *tfm)
517ea993de1Szhenwei pi {
518ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
519ea993de1Szhenwei pi
520ea993de1Szhenwei pi crypto_skcipher_set_reqsize(tfm, sizeof(struct virtio_crypto_sym_request));
521ea993de1Szhenwei pi ctx->tfm = tfm;
522ea993de1Szhenwei pi
523ea993de1Szhenwei pi return 0;
524ea993de1Szhenwei pi }
525ea993de1Szhenwei pi
virtio_crypto_skcipher_exit(struct crypto_skcipher * tfm)526ea993de1Szhenwei pi static void virtio_crypto_skcipher_exit(struct crypto_skcipher *tfm)
527ea993de1Szhenwei pi {
528ea993de1Szhenwei pi struct virtio_crypto_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
529ea993de1Szhenwei pi
530ea993de1Szhenwei pi if (!ctx->vcrypto)
531ea993de1Szhenwei pi return;
532ea993de1Szhenwei pi
533ea993de1Szhenwei pi virtio_crypto_alg_skcipher_close_session(ctx, 1);
534ea993de1Szhenwei pi virtio_crypto_alg_skcipher_close_session(ctx, 0);
535ea993de1Szhenwei pi virtcrypto_dev_put(ctx->vcrypto);
536ea993de1Szhenwei pi ctx->vcrypto = NULL;
537ea993de1Szhenwei pi }
538ea993de1Szhenwei pi
virtio_crypto_skcipher_crypt_req(struct crypto_engine * engine,void * vreq)539ea993de1Szhenwei pi int virtio_crypto_skcipher_crypt_req(
540ea993de1Szhenwei pi struct crypto_engine *engine, void *vreq)
541ea993de1Szhenwei pi {
542ea993de1Szhenwei pi struct skcipher_request *req = container_of(vreq, struct skcipher_request, base);
543ea993de1Szhenwei pi struct virtio_crypto_sym_request *vc_sym_req =
544ea993de1Szhenwei pi skcipher_request_ctx(req);
545ea993de1Szhenwei pi struct virtio_crypto_request *vc_req = &vc_sym_req->base;
546ea993de1Szhenwei pi struct data_queue *data_vq = vc_req->dataq;
547ea993de1Szhenwei pi int ret;
548ea993de1Szhenwei pi
549ea993de1Szhenwei pi ret = __virtio_crypto_skcipher_do_req(vc_sym_req, req, data_vq);
550ea993de1Szhenwei pi if (ret < 0)
551ea993de1Szhenwei pi return ret;
552ea993de1Szhenwei pi
553ea993de1Szhenwei pi virtqueue_kick(data_vq->vq);
554ea993de1Szhenwei pi
555ea993de1Szhenwei pi return 0;
556ea993de1Szhenwei pi }
557ea993de1Szhenwei pi
virtio_crypto_skcipher_finalize_req(struct virtio_crypto_sym_request * vc_sym_req,struct skcipher_request * req,int err)558ea993de1Szhenwei pi static void virtio_crypto_skcipher_finalize_req(
559ea993de1Szhenwei pi struct virtio_crypto_sym_request *vc_sym_req,
560ea993de1Szhenwei pi struct skcipher_request *req,
561ea993de1Szhenwei pi int err)
562ea993de1Szhenwei pi {
563ea993de1Szhenwei pi if (vc_sym_req->encrypt)
564ea993de1Szhenwei pi scatterwalk_map_and_copy(req->iv, req->dst,
565ea993de1Szhenwei pi req->cryptlen - AES_BLOCK_SIZE,
566ea993de1Szhenwei pi AES_BLOCK_SIZE, 0);
567ea993de1Szhenwei pi kfree_sensitive(vc_sym_req->iv);
568ea993de1Szhenwei pi virtcrypto_clear_request(&vc_sym_req->base);
569ea993de1Szhenwei pi
570ea993de1Szhenwei pi crypto_finalize_skcipher_request(vc_sym_req->base.dataq->engine,
571ea993de1Szhenwei pi req, err);
572ea993de1Szhenwei pi }
573ea993de1Szhenwei pi
574ea993de1Szhenwei pi static struct virtio_crypto_algo virtio_crypto_algs[] = { {
575ea993de1Szhenwei pi .algonum = VIRTIO_CRYPTO_CIPHER_AES_CBC,
576ea993de1Szhenwei pi .service = VIRTIO_CRYPTO_SERVICE_CIPHER,
577*7a2673d7SHerbert Xu .algo.base = {
578ea993de1Szhenwei pi .base.cra_name = "cbc(aes)",
579ea993de1Szhenwei pi .base.cra_driver_name = "virtio_crypto_aes_cbc",
580ea993de1Szhenwei pi .base.cra_priority = 150,
581ea993de1Szhenwei pi .base.cra_flags = CRYPTO_ALG_ASYNC |
582ea993de1Szhenwei pi CRYPTO_ALG_ALLOCATES_MEMORY,
583ea993de1Szhenwei pi .base.cra_blocksize = AES_BLOCK_SIZE,
584ea993de1Szhenwei pi .base.cra_ctxsize = sizeof(struct virtio_crypto_skcipher_ctx),
585ea993de1Szhenwei pi .base.cra_module = THIS_MODULE,
586ea993de1Szhenwei pi .init = virtio_crypto_skcipher_init,
587ea993de1Szhenwei pi .exit = virtio_crypto_skcipher_exit,
588ea993de1Szhenwei pi .setkey = virtio_crypto_skcipher_setkey,
589ea993de1Szhenwei pi .decrypt = virtio_crypto_skcipher_decrypt,
590ea993de1Szhenwei pi .encrypt = virtio_crypto_skcipher_encrypt,
591ea993de1Szhenwei pi .min_keysize = AES_MIN_KEY_SIZE,
592ea993de1Szhenwei pi .max_keysize = AES_MAX_KEY_SIZE,
593ea993de1Szhenwei pi .ivsize = AES_BLOCK_SIZE,
594ea993de1Szhenwei pi },
595*7a2673d7SHerbert Xu .algo.op = {
596*7a2673d7SHerbert Xu .do_one_request = virtio_crypto_skcipher_crypt_req,
597*7a2673d7SHerbert Xu },
598ea993de1Szhenwei pi } };
599ea993de1Szhenwei pi
virtio_crypto_skcipher_algs_register(struct virtio_crypto * vcrypto)600ea993de1Szhenwei pi int virtio_crypto_skcipher_algs_register(struct virtio_crypto *vcrypto)
601ea993de1Szhenwei pi {
602ea993de1Szhenwei pi int ret = 0;
603ea993de1Szhenwei pi int i = 0;
604ea993de1Szhenwei pi
605ea993de1Szhenwei pi mutex_lock(&algs_lock);
606ea993de1Szhenwei pi
607ea993de1Szhenwei pi for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) {
608ea993de1Szhenwei pi
609ea993de1Szhenwei pi uint32_t service = virtio_crypto_algs[i].service;
610ea993de1Szhenwei pi uint32_t algonum = virtio_crypto_algs[i].algonum;
611ea993de1Szhenwei pi
612ea993de1Szhenwei pi if (!virtcrypto_algo_is_supported(vcrypto, service, algonum))
613ea993de1Szhenwei pi continue;
614ea993de1Szhenwei pi
615ea993de1Szhenwei pi if (virtio_crypto_algs[i].active_devs == 0) {
616*7a2673d7SHerbert Xu ret = crypto_engine_register_skcipher(&virtio_crypto_algs[i].algo);
617ea993de1Szhenwei pi if (ret)
618ea993de1Szhenwei pi goto unlock;
619ea993de1Szhenwei pi }
620ea993de1Szhenwei pi
621ea993de1Szhenwei pi virtio_crypto_algs[i].active_devs++;
622ea993de1Szhenwei pi dev_info(&vcrypto->vdev->dev, "Registered algo %s\n",
623*7a2673d7SHerbert Xu virtio_crypto_algs[i].algo.base.base.cra_name);
624ea993de1Szhenwei pi }
625ea993de1Szhenwei pi
626ea993de1Szhenwei pi unlock:
627ea993de1Szhenwei pi mutex_unlock(&algs_lock);
628ea993de1Szhenwei pi return ret;
629ea993de1Szhenwei pi }
630ea993de1Szhenwei pi
virtio_crypto_skcipher_algs_unregister(struct virtio_crypto * vcrypto)631ea993de1Szhenwei pi void virtio_crypto_skcipher_algs_unregister(struct virtio_crypto *vcrypto)
632ea993de1Szhenwei pi {
633ea993de1Szhenwei pi int i = 0;
634ea993de1Szhenwei pi
635ea993de1Szhenwei pi mutex_lock(&algs_lock);
636ea993de1Szhenwei pi
637ea993de1Szhenwei pi for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) {
638ea993de1Szhenwei pi
639ea993de1Szhenwei pi uint32_t service = virtio_crypto_algs[i].service;
640ea993de1Szhenwei pi uint32_t algonum = virtio_crypto_algs[i].algonum;
641ea993de1Szhenwei pi
642ea993de1Szhenwei pi if (virtio_crypto_algs[i].active_devs == 0 ||
643ea993de1Szhenwei pi !virtcrypto_algo_is_supported(vcrypto, service, algonum))
644ea993de1Szhenwei pi continue;
645ea993de1Szhenwei pi
646ea993de1Szhenwei pi if (virtio_crypto_algs[i].active_devs == 1)
647*7a2673d7SHerbert Xu crypto_engine_unregister_skcipher(&virtio_crypto_algs[i].algo);
648ea993de1Szhenwei pi
649ea993de1Szhenwei pi virtio_crypto_algs[i].active_devs--;
650ea993de1Szhenwei pi }
651ea993de1Szhenwei pi
652ea993de1Szhenwei pi mutex_unlock(&algs_lock);
653ea993de1Szhenwei pi }
654