1 /* Algorithms supported by virtio crypto device
3 * Authors: Gonglei <arei.gonglei@huawei.com>
5 * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <linux/scatterlist.h>
22 #include <crypto/algapi.h>
23 #include <linux/err.h>
24 #include <crypto/scatterwalk.h>
25 #include <linux/atomic.h>
27 #include <uapi/linux/virtio_crypto.h>
28 #include "virtio_crypto_common.h"
31 struct virtio_crypto_ablkcipher_ctx {
32 struct virtio_crypto *vcrypto;
33 struct crypto_tfm *tfm;
35 struct virtio_crypto_sym_session_info enc_sess_info;
36 struct virtio_crypto_sym_session_info dec_sess_info;
39 struct virtio_crypto_sym_request {
40 struct virtio_crypto_request base;
44 struct virtio_crypto_ablkcipher_ctx *ablkcipher_ctx;
45 struct ablkcipher_request *ablkcipher_req;
52 * The algs_lock protects the below global virtio_crypto_active_devs
53 * and crypto algorithms registion.
55 static DEFINE_MUTEX(algs_lock);
56 static unsigned int virtio_crypto_active_devs;
57 static void virtio_crypto_ablkcipher_finalize_req(
58 struct virtio_crypto_sym_request *vc_sym_req,
59 struct ablkcipher_request *req,
62 static void virtio_crypto_dataq_sym_callback
63 (struct virtio_crypto_request *vc_req, int len)
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;
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:
76 case VIRTIO_CRYPTO_INVSESS:
77 case VIRTIO_CRYPTO_ERR:
80 case VIRTIO_CRYPTO_BADMSG:
87 ablk_req = vc_sym_req->ablkcipher_req;
88 virtio_crypto_ablkcipher_finalize_req(vc_sym_req,
93 static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg)
97 for (total = 0; sg; sg = sg_next(sg))
104 virtio_crypto_alg_validate_key(int key_len, uint32_t *alg)
107 case AES_KEYSIZE_128:
108 case AES_KEYSIZE_192:
109 case AES_KEYSIZE_256:
110 *alg = VIRTIO_CRYPTO_CIPHER_AES_CBC;
118 static int virtio_crypto_alg_ablkcipher_init_session(
119 struct virtio_crypto_ablkcipher_ctx *ctx,
120 uint32_t alg, const uint8_t *key,
124 struct scatterlist outhdr, key_sg, inhdr, *sgs[3];
126 struct virtio_crypto *vcrypto = ctx->vcrypto;
127 int op = encrypt ? VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT;
129 unsigned int num_out = 0, num_in = 0;
132 * Avoid to do DMA from the stack, switch to using
133 * dynamically-allocated for the key
135 uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC);
140 memcpy(cipher_key, key, keylen);
142 spin_lock(&vcrypto->ctrl_lock);
143 /* Pad ctrl header */
144 vcrypto->ctrl.header.opcode =
145 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
146 vcrypto->ctrl.header.algo = cpu_to_le32(alg);
147 /* Set the default dataqueue id to 0 */
148 vcrypto->ctrl.header.queue_id = 0;
150 vcrypto->input.status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
151 /* Pad cipher's parameters */
152 vcrypto->ctrl.u.sym_create_session.op_type =
153 cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
154 vcrypto->ctrl.u.sym_create_session.u.cipher.para.algo =
155 vcrypto->ctrl.header.algo;
156 vcrypto->ctrl.u.sym_create_session.u.cipher.para.keylen =
158 vcrypto->ctrl.u.sym_create_session.u.cipher.para.op =
161 sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
162 sgs[num_out++] = &outhdr;
165 sg_init_one(&key_sg, cipher_key, keylen);
166 sgs[num_out++] = &key_sg;
168 /* Return status and session id back */
169 sg_init_one(&inhdr, &vcrypto->input, sizeof(vcrypto->input));
170 sgs[num_out + num_in++] = &inhdr;
172 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
173 num_in, vcrypto, GFP_ATOMIC);
175 spin_unlock(&vcrypto->ctrl_lock);
179 virtqueue_kick(vcrypto->ctrl_vq);
182 * Trapping into the hypervisor, so the request should be
183 * handled immediately.
185 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
186 !virtqueue_is_broken(vcrypto->ctrl_vq))
189 if (le32_to_cpu(vcrypto->input.status) != VIRTIO_CRYPTO_OK) {
190 spin_unlock(&vcrypto->ctrl_lock);
191 pr_err("virtio_crypto: Create session failed status: %u\n",
192 le32_to_cpu(vcrypto->input.status));
198 ctx->enc_sess_info.session_id =
199 le64_to_cpu(vcrypto->input.session_id);
201 ctx->dec_sess_info.session_id =
202 le64_to_cpu(vcrypto->input.session_id);
204 spin_unlock(&vcrypto->ctrl_lock);
210 static int virtio_crypto_alg_ablkcipher_close_session(
211 struct virtio_crypto_ablkcipher_ctx *ctx,
214 struct scatterlist outhdr, status_sg, *sgs[2];
216 struct virtio_crypto_destroy_session_req *destroy_session;
217 struct virtio_crypto *vcrypto = ctx->vcrypto;
219 unsigned int num_out = 0, num_in = 0;
221 spin_lock(&vcrypto->ctrl_lock);
222 vcrypto->ctrl_status.status = VIRTIO_CRYPTO_ERR;
223 /* Pad ctrl header */
224 vcrypto->ctrl.header.opcode =
225 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
226 /* Set the default virtqueue id to 0 */
227 vcrypto->ctrl.header.queue_id = 0;
229 destroy_session = &vcrypto->ctrl.u.destroy_session;
232 destroy_session->session_id =
233 cpu_to_le64(ctx->enc_sess_info.session_id);
235 destroy_session->session_id =
236 cpu_to_le64(ctx->dec_sess_info.session_id);
238 sg_init_one(&outhdr, &vcrypto->ctrl, sizeof(vcrypto->ctrl));
239 sgs[num_out++] = &outhdr;
241 /* Return status and session id back */
242 sg_init_one(&status_sg, &vcrypto->ctrl_status.status,
243 sizeof(vcrypto->ctrl_status.status));
244 sgs[num_out + num_in++] = &status_sg;
246 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
247 num_in, vcrypto, GFP_ATOMIC);
249 spin_unlock(&vcrypto->ctrl_lock);
252 virtqueue_kick(vcrypto->ctrl_vq);
254 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
255 !virtqueue_is_broken(vcrypto->ctrl_vq))
258 if (vcrypto->ctrl_status.status != VIRTIO_CRYPTO_OK) {
259 spin_unlock(&vcrypto->ctrl_lock);
260 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
261 vcrypto->ctrl_status.status,
262 destroy_session->session_id);
266 spin_unlock(&vcrypto->ctrl_lock);
271 static int virtio_crypto_alg_ablkcipher_init_sessions(
272 struct virtio_crypto_ablkcipher_ctx *ctx,
273 const uint8_t *key, unsigned int keylen)
277 struct virtio_crypto *vcrypto = ctx->vcrypto;
279 if (keylen > vcrypto->max_cipher_key_len) {
280 pr_err("virtio_crypto: the key is too long\n");
284 if (virtio_crypto_alg_validate_key(keylen, &alg))
287 /* Create encryption session */
288 ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
289 alg, key, keylen, 1);
292 /* Create decryption session */
293 ret = virtio_crypto_alg_ablkcipher_init_session(ctx,
294 alg, key, keylen, 0);
296 virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
302 crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
306 /* Note: kernel crypto API realization */
307 static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
311 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(tfm);
316 int node = virtio_crypto_get_current_node();
317 struct virtio_crypto *vcrypto =
318 virtcrypto_get_dev_node(node);
320 pr_err("virtio_crypto: Could not find a virtio device in the system");
324 ctx->vcrypto = vcrypto;
326 /* Rekeying, we should close the created sessions previously */
327 virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
328 virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
331 ret = virtio_crypto_alg_ablkcipher_init_sessions(ctx, key, keylen);
333 virtcrypto_dev_put(ctx->vcrypto);
343 __virtio_crypto_ablkcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req,
344 struct ablkcipher_request *req,
345 struct data_queue *data_vq)
347 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
348 struct virtio_crypto_ablkcipher_ctx *ctx = vc_sym_req->ablkcipher_ctx;
349 struct virtio_crypto_request *vc_req = &vc_sym_req->base;
350 unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
351 struct virtio_crypto *vcrypto = ctx->vcrypto;
352 struct virtio_crypto_op_data_req *req_data;
353 int src_nents, dst_nents;
356 struct scatterlist outhdr, iv_sg, status_sg, **sgs;
358 unsigned int num_out = 0, num_in = 0;
361 struct scatterlist *sg;
363 src_nents = sg_nents_for_len(req->src, req->nbytes);
365 pr_err("Invalid number of src SG.\n");
369 dst_nents = sg_nents(req->dst);
371 pr_debug("virtio_crypto: Number of sgs (src_nents: %d, dst_nents: %d)\n",
372 src_nents, dst_nents);
374 /* Why 3? outhdr + iv + inhdr */
375 sg_total = src_nents + dst_nents + 3;
376 sgs = kzalloc_node(sg_total * sizeof(*sgs), GFP_ATOMIC,
377 dev_to_node(&vcrypto->vdev->dev));
381 req_data = kzalloc_node(sizeof(*req_data), GFP_ATOMIC,
382 dev_to_node(&vcrypto->vdev->dev));
388 vc_req->req_data = req_data;
389 vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
390 /* Head of operation */
391 if (vc_sym_req->encrypt) {
392 req_data->header.session_id =
393 cpu_to_le64(ctx->enc_sess_info.session_id);
394 req_data->header.opcode =
395 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_ENCRYPT);
397 req_data->header.session_id =
398 cpu_to_le64(ctx->dec_sess_info.session_id);
399 req_data->header.opcode =
400 cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DECRYPT);
402 req_data->u.sym_req.op_type = cpu_to_le32(VIRTIO_CRYPTO_SYM_OP_CIPHER);
403 req_data->u.sym_req.u.cipher.para.iv_len = cpu_to_le32(ivsize);
404 req_data->u.sym_req.u.cipher.para.src_data_len =
405 cpu_to_le32(req->nbytes);
407 dst_len = virtio_crypto_alg_sg_nents_length(req->dst);
408 if (unlikely(dst_len > U32_MAX)) {
409 pr_err("virtio_crypto: The dst_len is beyond U32_MAX\n");
414 dst_len = min_t(unsigned int, req->nbytes, dst_len);
415 pr_debug("virtio_crypto: src_len: %u, dst_len: %llu\n",
416 req->nbytes, dst_len);
418 if (unlikely(req->nbytes + dst_len + ivsize +
419 sizeof(vc_req->status) > vcrypto->max_size)) {
420 pr_err("virtio_crypto: The length is too big\n");
425 req_data->u.sym_req.u.cipher.para.dst_data_len =
426 cpu_to_le32((uint32_t)dst_len);
429 sg_init_one(&outhdr, req_data, sizeof(*req_data));
430 sgs[num_out++] = &outhdr;
435 * Avoid to do DMA from the stack, switch to using
436 * dynamically-allocated for the IV
438 iv = kzalloc_node(ivsize, GFP_ATOMIC,
439 dev_to_node(&vcrypto->vdev->dev));
444 memcpy(iv, req->info, ivsize);
445 sg_init_one(&iv_sg, iv, ivsize);
446 sgs[num_out++] = &iv_sg;
450 for (sg = req->src; src_nents; sg = sg_next(sg), src_nents--)
453 /* Destination data */
454 for (sg = req->dst; sg; sg = sg_next(sg))
455 sgs[num_out + num_in++] = sg;
458 sg_init_one(&status_sg, &vc_req->status, sizeof(vc_req->status));
459 sgs[num_out + num_in++] = &status_sg;
463 spin_lock_irqsave(&data_vq->lock, flags);
464 err = virtqueue_add_sgs(data_vq->vq, sgs, num_out,
465 num_in, vc_req, GFP_ATOMIC);
466 virtqueue_kick(data_vq->vq);
467 spin_unlock_irqrestore(&data_vq->lock, flags);
468 if (unlikely(err < 0))
481 static int virtio_crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
483 struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
484 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
485 struct virtio_crypto_sym_request *vc_sym_req =
486 ablkcipher_request_ctx(req);
487 struct virtio_crypto_request *vc_req = &vc_sym_req->base;
488 struct virtio_crypto *vcrypto = ctx->vcrypto;
489 /* Use the first data virtqueue as default */
490 struct data_queue *data_vq = &vcrypto->data_vq[0];
494 if (req->nbytes % AES_BLOCK_SIZE)
497 vc_req->dataq = data_vq;
498 vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
499 vc_sym_req->ablkcipher_ctx = ctx;
500 vc_sym_req->ablkcipher_req = req;
501 vc_sym_req->encrypt = true;
503 return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
506 static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
508 struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
509 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
510 struct virtio_crypto_sym_request *vc_sym_req =
511 ablkcipher_request_ctx(req);
512 struct virtio_crypto_request *vc_req = &vc_sym_req->base;
513 struct virtio_crypto *vcrypto = ctx->vcrypto;
514 /* Use the first data virtqueue as default */
515 struct data_queue *data_vq = &vcrypto->data_vq[0];
519 if (req->nbytes % AES_BLOCK_SIZE)
522 vc_req->dataq = data_vq;
523 vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
524 vc_sym_req->ablkcipher_ctx = ctx;
525 vc_sym_req->ablkcipher_req = req;
526 vc_sym_req->encrypt = false;
528 return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
531 static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm)
533 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
535 tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_sym_request);
541 static void virtio_crypto_ablkcipher_exit(struct crypto_tfm *tfm)
543 struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
548 virtio_crypto_alg_ablkcipher_close_session(ctx, 1);
549 virtio_crypto_alg_ablkcipher_close_session(ctx, 0);
550 virtcrypto_dev_put(ctx->vcrypto);
554 int virtio_crypto_ablkcipher_crypt_req(
555 struct crypto_engine *engine,
556 struct ablkcipher_request *req)
558 struct virtio_crypto_sym_request *vc_sym_req =
559 ablkcipher_request_ctx(req);
560 struct virtio_crypto_request *vc_req = &vc_sym_req->base;
561 struct data_queue *data_vq = vc_req->dataq;
564 ret = __virtio_crypto_ablkcipher_do_req(vc_sym_req, req, data_vq);
568 virtqueue_kick(data_vq->vq);
573 static void virtio_crypto_ablkcipher_finalize_req(
574 struct virtio_crypto_sym_request *vc_sym_req,
575 struct ablkcipher_request *req,
578 kzfree(vc_sym_req->iv);
579 virtcrypto_clear_request(&vc_sym_req->base);
581 crypto_finalize_cipher_request(vc_sym_req->base.dataq->engine,
585 static struct crypto_alg virtio_crypto_algs[] = { {
586 .cra_name = "cbc(aes)",
587 .cra_driver_name = "virtio_crypto_aes_cbc",
589 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
590 .cra_blocksize = AES_BLOCK_SIZE,
591 .cra_ctxsize = sizeof(struct virtio_crypto_ablkcipher_ctx),
593 .cra_module = THIS_MODULE,
594 .cra_type = &crypto_ablkcipher_type,
595 .cra_init = virtio_crypto_ablkcipher_init,
596 .cra_exit = virtio_crypto_ablkcipher_exit,
599 .setkey = virtio_crypto_ablkcipher_setkey,
600 .decrypt = virtio_crypto_ablkcipher_decrypt,
601 .encrypt = virtio_crypto_ablkcipher_encrypt,
602 .min_keysize = AES_MIN_KEY_SIZE,
603 .max_keysize = AES_MAX_KEY_SIZE,
604 .ivsize = AES_BLOCK_SIZE,
609 int virtio_crypto_algs_register(void)
613 mutex_lock(&algs_lock);
614 if (++virtio_crypto_active_devs != 1)
617 ret = crypto_register_algs(virtio_crypto_algs,
618 ARRAY_SIZE(virtio_crypto_algs));
620 virtio_crypto_active_devs--;
623 mutex_unlock(&algs_lock);
627 void virtio_crypto_algs_unregister(void)
629 mutex_lock(&algs_lock);
630 if (--virtio_crypto_active_devs != 0)
633 crypto_unregister_algs(virtio_crypto_algs,
634 ARRAY_SIZE(virtio_crypto_algs));
637 mutex_unlock(&algs_lock);