blob: 10f266d462d6088e221edd203f9d758ce81efc4b [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Gongleidbaf0622016-12-15 10:03:16 +08002 /* Algorithms supported by virtio crypto device
3 *
4 * Authors: Gonglei <arei.gonglei@huawei.com>
5 *
6 * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
Gongleidbaf0622016-12-15 10:03:16 +08007 */
8
9#include <linux/scatterlist.h>
10#include <crypto/algapi.h>
11#include <linux/err.h>
12#include <crypto/scatterwalk.h>
13#include <linux/atomic.h>
14
15#include <uapi/linux/virtio_crypto.h>
16#include "virtio_crypto_common.h"
17
Zeng, Xind31e7122017-06-23 11:31:19 -040018
19struct virtio_crypto_ablkcipher_ctx {
Corentin LABBE671893752018-01-26 20:15:32 +010020 struct crypto_engine_ctx enginectx;
Zeng, Xind31e7122017-06-23 11:31:19 -040021 struct virtio_crypto *vcrypto;
22 struct crypto_tfm *tfm;
23
24 struct virtio_crypto_sym_session_info enc_sess_info;
25 struct virtio_crypto_sym_session_info dec_sess_info;
26};
27
28struct virtio_crypto_sym_request {
29 struct virtio_crypto_request base;
30
31 /* Cipher or aead */
32 uint32_t type;
33 struct virtio_crypto_ablkcipher_ctx *ablkcipher_ctx;
34 struct ablkcipher_request *ablkcipher_req;
35 uint8_t *iv;
36 /* Encryption? */
37 bool encrypt;
38};
39
Farhan Alid0d859b2018-06-19 11:41:34 -040040struct virtio_crypto_algo {
41 uint32_t algonum;
42 uint32_t service;
43 unsigned int active_devs;
44 struct crypto_alg algo;
45};
46
Gongleidbaf0622016-12-15 10:03:16 +080047/*
48 * The algs_lock protects the below global virtio_crypto_active_devs
49 * and crypto algorithms registion.
50 */
51static DEFINE_MUTEX(algs_lock);
Zeng, Xind31e7122017-06-23 11:31:19 -040052static void virtio_crypto_ablkcipher_finalize_req(
53 struct virtio_crypto_sym_request *vc_sym_req,
54 struct ablkcipher_request *req,
55 int err);
56
57static void virtio_crypto_dataq_sym_callback
58 (struct virtio_crypto_request *vc_req, int len)
59{
60 struct virtio_crypto_sym_request *vc_sym_req =
61 container_of(vc_req, struct virtio_crypto_sym_request, base);
62 struct ablkcipher_request *ablk_req;
63 int error;
64
65 /* Finish the encrypt or decrypt process */
66 if (vc_sym_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
67 switch (vc_req->status) {
68 case VIRTIO_CRYPTO_OK:
69 error = 0;
70 break;
71 case VIRTIO_CRYPTO_INVSESS:
72 case VIRTIO_CRYPTO_ERR:
73 error = -EINVAL;
74 break;
75 case VIRTIO_CRYPTO_BADMSG:
76 error = -EBADMSG;
77 break;
78 default:
79 error = -EIO;
80 break;
81 }
82 ablk_req = vc_sym_req->ablkcipher_req;
83 virtio_crypto_ablkcipher_finalize_req(vc_sym_req,
84 ablk_req, error);
85 }
86}
Gongleidbaf0622016-12-15 10:03:16 +080087
88static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg)
89{
90 u64 total = 0;
91
92 for (total = 0; sg; sg = sg_next(sg))
93 total += sg->length;
94
95 return total;
96}
97
98static int
99virtio_crypto_alg_validate_key(int key_len, uint32_t *alg)
100{
101 switch (key_len) {
102 case AES_KEYSIZE_128:
103 case AES_KEYSIZE_192:
104 case AES_KEYSIZE_256:
105 *alg = VIRTIO_CRYPTO_CIPHER_AES_CBC;
106 break;
107 default:
108 pr_err("virtio_crypto: Unsupported key length: %d\n",
109 key_len);
110 return -EINVAL;
111 }
112 return 0;
113}
114
115static int virtio_crypto_alg_ablkcipher_init_session(
116 struct virtio_crypto_ablkcipher_ctx *ctx,
117 uint32_t alg, const uint8_t *key,
118 unsigned int keylen,
119 int encrypt)
120{
121 struct scatterlist outhdr, key_sg, inhdr, *sgs[3];
122 unsigned int tmp;
123 struct virtio_crypto *vcrypto = ctx->vcrypto;
124 int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT;
125 int err;
126 unsigned int num_out = 0, num_in = 0;
127
128 /*
129 * Avoid to do DMA from the stack, switch to using
130 * dynamically-allocated for the key
131 */
132 uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC);
133
134 if (!cipher_key)
135 return -ENOMEM;
136
137 memcpy(cipher_key, key, keylen);
138
139 spin_lock(&vcrypto->ctrl_lock);
140 /* Pad ctrl header */
141 vcrypto->ctrl.header.opcode =
142 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
143 vcrypto->ctrl.header.algo = cpu_to_le32(alg);
144 /* Set the default dataqueue id to 0 */
145 vcrypto->ctrl.header.queue_id = 0;
146
147 vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
148 /* Pad cipher's parameters */
149 vcrypto->ctrl.u.sym_create_session.op_type =
150 cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
151 vcrypto->ctrl.u.sym_create_session.u.cipher.para.algo =
152 vcrypto->ctrl.header.algo;
153 vcrypto->ctrl.u.sym_create_session.u.cipher.para.keylen =
154 cpu_to_le32(keylen);
155 vcrypto->ctrl.u.sym_create_session.u.cipher.para.op =
156 cpu_to_le32(op);
157
158 sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
159 sgs[num_out++] = &outhdr;
160
161 /* Set key */
162 sg_init_one(&key_sg, cipher_key, keylen);
163 sgs[num_out++] = &key_sg;
164
165 /* Return status and session id back */
166 sg_init_one(&inhdr, &vcrypto->input, sizeof(vcrypto->input));
167 sgs[num_out + num_in++] = &inhdr;
168
169 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
170 num_in, vcrypto, GFP_ATOMIC);
171 if (err < 0) {
172 spin_unlock(&vcrypto->ctrl_lock);
173 kzfree(cipher_key);
174 return err;
175 }
176 virtqueue_kick(vcrypto->ctrl_vq);
177
178 /*
179 * Trapping into the hypervisor, so the request should be
180 * handled immediately.
181 */
182 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
183 !virtqueue_is_broken(vcrypto->ctrl_vq))
184 cpu_relax();
185
186 if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) {
187 spin_unlock(&vcrypto->ctrl_lock);
188 pr_err("virtio_crypto: Create session failed status: %u\n",
189 le32_to_cpu(vcrypto->input.status));
190 kzfree(cipher_key);
191 return -EINVAL;
192 }
193
194 if (encrypt)
195 ctx->enc_sess_info.session_id =
196 le64_to_cpu(vcrypto->input.session_id);
197 else
198 ctx->dec_sess_info.session_id =
199 le64_to_cpu(vcrypto->input.session_id);
200
201 spin_unlock(&vcrypto->ctrl_lock);
202
203 kzfree(cipher_key);
204 return 0;
205}
206
207static int virtio_crypto_alg_ablkcipher_close_session(
208 struct virtio_crypto_ablkcipher_ctx *ctx,
209 int encrypt)
210{
211 struct scatterlist outhdr, status_sg, *sgs[2];
212 unsigned int tmp;
213 struct virtio_crypto_destroy_session_req *destroy_session;
214 struct virtio_crypto *vcrypto = ctx->vcrypto;
215 int err;
216 unsigned int num_out = 0, num_in = 0;
217
218 spin_lock(&vcrypto->ctrl_lock);
219 vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR;
220 /* Pad ctrl header */
221 vcrypto->ctrl.header.opcode =
222 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
223 /* Set the default virtqueue id to 0 */
224 vcrypto->ctrl.header.queue_id = 0;
225
226 destroy_session = &vcrypto->ctrl.u.destroy_session;
227
228 if (encrypt)
229 destroy_session->session_id =
230 cpu_to_le64(ctx->enc_sess_info.session_id);
231 else
232 destroy_session->session_id =
233 cpu_to_le64(ctx->dec_sess_info.session_id);
234
235 sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
236 sgs[num_out++] = &outhdr;
237
238 /* Return status and session id back */
239 sg_init_one(&status_sg, &vcrypto->ctrl_status.status,
240 sizeof(vcrypto->ctrl_status.status));
241 sgs[num_out + num_in++] = &status_sg;
242
243 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
244 num_in, vcrypto, GFP_ATOMIC);
245 if (err < 0) {
246 spin_unlock(&vcrypto->ctrl_lock);
247 return err;
248 }
249 virtqueue_kick(vcrypto->ctrl_vq);
250
251 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
252 !virtqueue_is_broken(vcrypto->ctrl_vq))
253 cpu_relax();
254
255 if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) {
256 spin_unlock(&vcrypto->ctrl_lock);
257 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
258 vcrypto->ctrl_status.status,
259 destroy_session->session_id);
260
261 return -EINVAL;
262 }
263 spin_unlock(&vcrypto->ctrl_lock);
264
265 return 0;
266}
267
268static int virtio_crypto_alg_ablkcipher_init_sessions(
269 struct virtio_crypto_ablkcipher_ctx *ctx,
270 const uint8_t *key, unsigned int keylen)
271{
272 uint32_t alg;
273 int ret;
274 struct virtio_crypto *vcrypto = ctx->vcrypto;
275
276 if (keylen > vcrypto->max_cipher_key_len) {
277 pr_err("virtio_crypto: the key is too long\n");
278 goto bad_key;
279 }
280
281 if (virtio_crypto_alg_validate_key(keylen, &alg))
282 goto bad_key;
283
284 /* Create encryption session */
285 ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
286 alg, key, keylen, 1);
287 if (ret)
288 return ret;
289 /* Create decryption session */
290 ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
291 alg, key, keylen, 0);
292 if (ret) {
293 virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
294 return ret;
295 }
296 return 0;
297
298bad_key:
299 crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
300 return -EINVAL;
301}
302
303/* Note: kernel crypto API realization */
304static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
305 const uint8_t *key,
306 unsigned int keylen)
307{
308 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(tfm);
Farhan Alid0d859b2018-06-19 11:41:34 -0400309 uint32_t alg;
Gongleidbaf0622016-12-15 10:03:16 +0800310 int ret;
311
Farhan Alid0d859b2018-06-19 11:41:34 -0400312 ret = virtio_crypto_alg_validate_key(keylen, &alg);
313 if (ret)
314 return ret;
315
Gongleidbaf0622016-12-15 10:03:16 +0800316 if (!ctx->vcrypto) {
317 /* New key */
318 int node = virtio_crypto_get_current_node();
319 struct virtio_crypto *vcrypto =
Farhan Alid0d859b2018-06-19 11:41:34 -0400320 virtcrypto_get_dev_node(node,
321 VIRTIO_CRYPTO_SERVICE_CIPHER, alg);
Gongleidbaf0622016-12-15 10:03:16 +0800322 if (!vcrypto) {
Farhan Alid0d859b2018-06-19 11:41:34 -0400323 pr_err("virtio_crypto: Could not find a virtio device in the system or unsupported algo\n");
Gongleidbaf0622016-12-15 10:03:16 +0800324 return -ENODEV;
325 }
326
327 ctx->vcrypto = vcrypto;
328 } else {
329 /* Rekeying, we should close the created sessions previously */
330 virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
331 virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
332 }
333
334 ret = virtio_crypto_alg_ablkcipher_init_sessions(ctx, key, keylen);
335 if (ret) {
336 virtcrypto_dev_put(ctx->vcrypto);
337 ctx->vcrypto = NULL;
338
339 return ret;
340 }
341
342 return 0;
343}
344
345static int
Zeng, Xind31e7122017-06-23 11:31:19 -0400346__virtio_crypto_ablkcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req,
Gongleidbaf0622016-12-15 10:03:16 +0800347 struct ablkcipher_request *req,
Gonglei \(Arei\)d79b5d02016-12-27 14:49:07 +0800348 struct data_queue *data_vq)
Gongleidbaf0622016-12-15 10:03:16 +0800349{
350 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
Zeng, Xind31e7122017-06-23 11:31:19 -0400351 struct virtio_crypto_ablkcipher_ctx *ctx = vc_sym_req->ablkcipher_ctx;
352 struct virtio_crypto_request *vc_req = &vc_sym_req->base;
Gongleidbaf0622016-12-15 10:03:16 +0800353 unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
Gongleidbaf0622016-12-15 10:03:16 +0800354 struct virtio_crypto *vcrypto = ctx->vcrypto;
355 struct virtio_crypto_op_data_req *req_data;
356 int src_nents, dst_nents;
357 int err;
358 unsigned long flags;
359 struct scatterlist outhdr, iv_sg, status_sg, **sgs;
360 int i;
361 u64 dst_len;
362 unsigned int num_out = 0, num_in = 0;
363 int sg_total;
364 uint8_t *iv;
365
366 src_nents = sg_nents_for_len(req->src, req->nbytes);
367 dst_nents = sg_nents(req->dst);
368
369 pr_debug("virtio_crypto: Number of sgs (src_nents: %d, dst_nents: %d)\n",
370 src_nents, dst_nents);
371
372 /* Why 3? outhdr + iv + inhdr */
373 sg_total = src_nents + dst_nents + 3;
Jia-Ju Baif6adeef2018-07-23 16:43:46 +0800374 sgs = kcalloc_node(sg_total, sizeof(*sgs), GFP_KERNEL,
Gongleidbaf0622016-12-15 10:03:16 +0800375 dev_to_node(&vcrypto->vdev->dev));
376 if (!sgs)
377 return -ENOMEM;
378
Jia-Ju Baif6adeef2018-07-23 16:43:46 +0800379 req_data = kzalloc_node(sizeof(*req_data), GFP_KERNEL,
Gongleidbaf0622016-12-15 10:03:16 +0800380 dev_to_node(&vcrypto->vdev->dev));
381 if (!req_data) {
382 kfree(sgs);
383 return -ENOMEM;
384 }
385
386 vc_req->req_data = req_data;
Zeng, Xind31e7122017-06-23 11:31:19 -0400387 vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
Gongleidbaf0622016-12-15 10:03:16 +0800388 /* Head of operation */
Zeng, Xind31e7122017-06-23 11:31:19 -0400389 if (vc_sym_req->encrypt) {
Gongleidbaf0622016-12-15 10:03:16 +0800390 req_data->header.session_id =
391 cpu_to_le64(ctx->enc_sess_info.session_id);
392 req_data->header.opcode =
393 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_ENCRYPT);
394 } else {
395 req_data->header.session_id =
396 cpu_to_le64(ctx->dec_sess_info.session_id);
Colin Ian King1bb64d82018-12-30 13:46:21 +0000397 req_data->header.opcode =
Gongleidbaf0622016-12-15 10:03:16 +0800398 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT);
399 }
400 req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
401 req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize);
402 req_data->u.sym_req.u.cipher.para.src_data_len =
403 cpu_to_le32(req->nbytes);
404
405 dst_len = virtio_crypto_alg_sg_nents_length(req->dst);
406 if (unlikely(dst_len > U32_MAX)) {
407 pr_err("virtio_crypto: The dst_len is beyond U32_MAX\n");
408 err = -EINVAL;
409 goto free;
410 }
411
412 pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n",
413 req->nbytes, dst_len);
414
415 if (unlikely(req->nbytes + dst_len + ivsize +
416 sizeof(vc_req->status) > vcrypto->max_size)) {
417 pr_err("virtio_crypto: The length is too big\n");
418 err = -EINVAL;
419 goto free;
420 }
421
422 req_data->u.sym_req.u.cipher.para.dst_data_len =
423 cpu_to_le32((uint32_t)dst_len);
424
425 /* Outhdr */
426 sg_init_one(&outhdr, req_data, sizeof(*req_data));
427 sgs[num_out++] = &outhdr;
428
429 /* IV */
430
431 /*
432 * Avoid to do DMA from the stack, switch to using
433 * dynamically-allocated for the IV
434 */
435 iv = kzalloc_node(ivsize, GFP_ATOMIC,
436 dev_to_node(&vcrypto->vdev->dev));
437 if (!iv) {
438 err = -ENOMEM;
439 goto free;
440 }
441 memcpy(iv, req->info, ivsize);
442 sg_init_one(&iv_sg, iv, ivsize);
443 sgs[num_out++] = &iv_sg;
Zeng, Xind31e7122017-06-23 11:31:19 -0400444 vc_sym_req->iv = iv;
Gongleidbaf0622016-12-15 10:03:16 +0800445
446 /* Source data */
447 for (i = 0; i < src_nents; i++)
448 sgs[num_out++] = &req->src[i];
449
450 /* Destination data */
451 for (i = 0; i < dst_nents; i++)
452 sgs[num_out + num_in++] = &req->dst[i];
453
454 /* Status */
455 sg_init_one(&status_sg, &vc_req->status, sizeof(vc_req->status));
456 sgs[num_out + num_in++] = &status_sg;
457
458 vc_req->sgs = sgs;
459
460 spin_lock_irqsave(&data_vq->lock, flags);
461 err = virtqueue_add_sgs(data_vq->vq, sgs, num_out,
462 num_in, vc_req, GFP_ATOMIC);
463 virtqueue_kick(data_vq->vq);
464 spin_unlock_irqrestore(&data_vq->lock, flags);
465 if (unlikely(err < 0))
466 goto free_iv;
467
468 return 0;
469
470free_iv:
471 kzfree(iv);
472free:
473 kzfree(req_data);
474 kfree(sgs);
475 return err;
476}
477
478static int virtio_crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
479{
480 struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
481 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
Zeng, Xind31e7122017-06-23 11:31:19 -0400482 struct virtio_crypto_sym_request *vc_sym_req =
483 ablkcipher_request_ctx(req);
484 struct virtio_crypto_request *vc_req = &vc_sym_req->base;
Gongleidbaf0622016-12-15 10:03:16 +0800485 struct virtio_crypto *vcrypto = ctx->vcrypto;
Gongleidbaf0622016-12-15 10:03:16 +0800486 /* Use the first data virtqueue as default */
487 struct data_queue *data_vq = &vcrypto->data_vq[0];
488
Gonglei \(Arei\)d79b5d02016-12-27 14:49:07 +0800489 vc_req->dataq = data_vq;
Zeng, Xind31e7122017-06-23 11:31:19 -0400490 vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
491 vc_sym_req->ablkcipher_ctx = ctx;
492 vc_sym_req->ablkcipher_req = req;
493 vc_sym_req->encrypt = true;
Gongleidbaf0622016-12-15 10:03:16 +0800494
Corentin LABBE671893752018-01-26 20:15:32 +0100495 return crypto_transfer_ablkcipher_request_to_engine(data_vq->engine, req);
Gongleidbaf0622016-12-15 10:03:16 +0800496}
497
498static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
499{
500 struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
501 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
Zeng, Xind31e7122017-06-23 11:31:19 -0400502 struct virtio_crypto_sym_request *vc_sym_req =
503 ablkcipher_request_ctx(req);
504 struct virtio_crypto_request *vc_req = &vc_sym_req->base;
Gongleidbaf0622016-12-15 10:03:16 +0800505 struct virtio_crypto *vcrypto = ctx->vcrypto;
Gongleidbaf0622016-12-15 10:03:16 +0800506 /* Use the first data virtqueue as default */
507 struct data_queue *data_vq = &vcrypto->data_vq[0];
508
Gonglei \(Arei\)d79b5d02016-12-27 14:49:07 +0800509 vc_req->dataq = data_vq;
Zeng, Xind31e7122017-06-23 11:31:19 -0400510 vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
511 vc_sym_req->ablkcipher_ctx = ctx;
512 vc_sym_req->ablkcipher_req = req;
513 vc_sym_req->encrypt = false;
Gongleidbaf0622016-12-15 10:03:16 +0800514
Corentin LABBE671893752018-01-26 20:15:32 +0100515 return crypto_transfer_ablkcipher_request_to_engine(data_vq->engine, req);
Gongleidbaf0622016-12-15 10:03:16 +0800516}
517
518static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm)
519{
520 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
521
Zeng, Xind31e7122017-06-23 11:31:19 -0400522 tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_sym_request);
Gongleidbaf0622016-12-15 10:03:16 +0800523 ctx->tfm = tfm;
524
Corentin LABBE671893752018-01-26 20:15:32 +0100525 ctx->enginectx.op.do_one_request = virtio_crypto_ablkcipher_crypt_req;
526 ctx->enginectx.op.prepare_request = NULL;
527 ctx->enginectx.op.unprepare_request = NULL;
Gongleidbaf0622016-12-15 10:03:16 +0800528 return 0;
529}
530
531static void virtio_crypto_ablkcipher_exit(struct crypto_tfm *tfm)
532{
533 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
534
535 if (!ctx->vcrypto)
536 return;
537
538 virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
539 virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
540 virtcrypto_dev_put(ctx->vcrypto);
541 ctx->vcrypto = NULL;
542}
543
Gonglei \(Arei\)d79b5d02016-12-27 14:49:07 +0800544int virtio_crypto_ablkcipher_crypt_req(
Corentin LABBE671893752018-01-26 20:15:32 +0100545 struct crypto_engine *engine, void *vreq)
Gonglei \(Arei\)d79b5d02016-12-27 14:49:07 +0800546{
Corentin LABBE671893752018-01-26 20:15:32 +0100547 struct ablkcipher_request *req = container_of(vreq, struct ablkcipher_request, base);
Zeng, Xind31e7122017-06-23 11:31:19 -0400548 struct virtio_crypto_sym_request *vc_sym_req =
549 ablkcipher_request_ctx(req);
550 struct virtio_crypto_request *vc_req = &vc_sym_req->base;
Gonglei \(Arei\)d79b5d02016-12-27 14:49:07 +0800551 struct data_queue *data_vq = vc_req->dataq;
552 int ret;
553
Zeng, Xind31e7122017-06-23 11:31:19 -0400554 ret = __virtio_crypto_ablkcipher_do_req(vc_sym_req, req, data_vq);
Gonglei \(Arei\)d79b5d02016-12-27 14:49:07 +0800555 if (ret < 0)
556 return ret;
557
558 virtqueue_kick(data_vq->vq);
559
560 return 0;
561}
562
Zeng, Xind31e7122017-06-23 11:31:19 -0400563static void virtio_crypto_ablkcipher_finalize_req(
564 struct virtio_crypto_sym_request *vc_sym_req,
Gonglei \(Arei\)d79b5d02016-12-27 14:49:07 +0800565 struct ablkcipher_request *req,
566 int err)
567{
Corentin LABBE671893752018-01-26 20:15:32 +0100568 crypto_finalize_ablkcipher_request(vc_sym_req->base.dataq->engine,
569 req, err);
Zeng, Xind31e7122017-06-23 11:31:19 -0400570 kzfree(vc_sym_req->iv);
571 virtcrypto_clear_request(&vc_sym_req->base);
Gonglei \(Arei\)d79b5d02016-12-27 14:49:07 +0800572}
573
Farhan Alid0d859b2018-06-19 11:41:34 -0400574static struct virtio_crypto_algo virtio_crypto_algs[] = { {
575 .algonum = VIRTIO_CRYPTO_CIPHER_AES_CBC,
576 .service = VIRTIO_CRYPTO_SERVICE_CIPHER,
577 .algo = {
578 .cra_name = "cbc(aes)",
579 .cra_driver_name = "virtio_crypto_aes_cbc",
580 .cra_priority = 150,
581 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
582 .cra_blocksize = AES_BLOCK_SIZE,
583 .cra_ctxsize = sizeof(struct virtio_crypto_ablkcipher_ctx),
584 .cra_alignmask = 0,
585 .cra_module = THIS_MODULE,
586 .cra_type = &crypto_ablkcipher_type,
587 .cra_init = virtio_crypto_ablkcipher_init,
588 .cra_exit = virtio_crypto_ablkcipher_exit,
589 .cra_u = {
590 .ablkcipher = {
591 .setkey = virtio_crypto_ablkcipher_setkey,
592 .decrypt = virtio_crypto_ablkcipher_decrypt,
593 .encrypt = virtio_crypto_ablkcipher_encrypt,
594 .min_keysize = AES_MIN_KEY_SIZE,
595 .max_keysize = AES_MAX_KEY_SIZE,
596 .ivsize = AES_BLOCK_SIZE,
597 },
Gongleidbaf0622016-12-15 10:03:16 +0800598 },
599 },
600} };
601
Farhan Alid0d859b2018-06-19 11:41:34 -0400602int virtio_crypto_algs_register(struct virtio_crypto *vcrypto)
Gongleidbaf0622016-12-15 10:03:16 +0800603{
604 int ret = 0;
Farhan Alid0d859b2018-06-19 11:41:34 -0400605 int i = 0;
Gongleidbaf0622016-12-15 10:03:16 +0800606
607 mutex_lock(&algs_lock);
Gongleidbaf0622016-12-15 10:03:16 +0800608
Farhan Alid0d859b2018-06-19 11:41:34 -0400609 for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) {
610
611 uint32_t service = virtio_crypto_algs[i].service;
612 uint32_t algonum = virtio_crypto_algs[i].algonum;
613
614 if (!virtcrypto_algo_is_supported(vcrypto, service, algonum))
615 continue;
616
617 if (virtio_crypto_algs[i].active_devs == 0) {
618 ret = crypto_register_alg(&virtio_crypto_algs[i].algo);
619 if (ret)
620 goto unlock;
621 }
622
623 virtio_crypto_algs[i].active_devs++;
624 dev_info(&vcrypto->vdev->dev, "Registered algo %s\n",
625 virtio_crypto_algs[i].algo.cra_name);
626 }
Gongleidbaf0622016-12-15 10:03:16 +0800627
628unlock:
629 mutex_unlock(&algs_lock);
630 return ret;
631}
632
Farhan Alid0d859b2018-06-19 11:41:34 -0400633void virtio_crypto_algs_unregister(struct virtio_crypto *vcrypto)
Gongleidbaf0622016-12-15 10:03:16 +0800634{
Farhan Alid0d859b2018-06-19 11:41:34 -0400635 int i = 0;
636
Gongleidbaf0622016-12-15 10:03:16 +0800637 mutex_lock(&algs_lock);
Gongleidbaf0622016-12-15 10:03:16 +0800638
Farhan Alid0d859b2018-06-19 11:41:34 -0400639 for (i = 0; i < ARRAY_SIZE(virtio_crypto_algs); i++) {
Gongleidbaf0622016-12-15 10:03:16 +0800640
Farhan Alid0d859b2018-06-19 11:41:34 -0400641 uint32_t service = virtio_crypto_algs[i].service;
642 uint32_t algonum = virtio_crypto_algs[i].algonum;
643
644 if (virtio_crypto_algs[i].active_devs == 0 ||
645 !virtcrypto_algo_is_supported(vcrypto, service, algonum))
646 continue;
647
648 if (virtio_crypto_algs[i].active_devs == 1)
649 crypto_unregister_alg(&virtio_crypto_algs[i].algo);
650
651 virtio_crypto_algs[i].active_devs--;
652 }
653
Gongleidbaf0622016-12-15 10:03:16 +0800654 mutex_unlock(&algs_lock);
655}