This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit dad0b4e9a35d4b2f92c16a7eab1605b50eab054b Author: makejian <[email protected]> AuthorDate: Mon Nov 23 11:36:29 2026 +0800 drivers/virtio: add asymmetric support to virtio-crypto Handle the cryptodev key operations on the AKCIPHER service: RSA encrypt/decrypt/sign/verify, ECDSA sign/verify/genkey and ECDH key generation and shared-secret derivation. Each operation opens a short lived AKCIPHER session on the control queue, runs one data-queue request and releases the session, so no asymmetric state is cached in the driver. RSA keys are exported to DER with the ASN.1 helpers, ECDSA and ECDH pass raw field elements sized by the curve table. The driver registers itself for every cryptodev key operation through crypto_kregister() alongside the symmetric algorithms. Signed-off-by: makejian <[email protected]> --- drivers/virtio/virtio-crypto.c | 852 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 852 insertions(+) diff --git a/drivers/virtio/virtio-crypto.c b/drivers/virtio/virtio-crypto.c index 8f506ab6bc9..1c1730aa1fa 100644 --- a/drivers/virtio/virtio-crypto.c +++ b/drivers/virtio/virtio-crypto.c @@ -25,6 +25,7 @@ ****************************************************************************/ #include <debug.h> +#include <inttypes.h> #include <crypto/virtio_crypto.h> #include <nuttx/spinlock.h> @@ -37,6 +38,8 @@ * Pre-processor Definitions ****************************************************************************/ +#define VIRTIO_CRYPTO_RSA_KEYBITS_MAX 8192 + /* Largest number of virtqueue slots a single request uses. Bounded by the * AEAD path: header, iv, src, aad, dst, inhdr. */ @@ -125,6 +128,7 @@ struct virtio_crypto_s static int virtio_crypto_newsession(FAR uint32_t *sid, FAR struct cryptoini *cri); static int virtio_crypto_process(FAR struct cryptop *crp); +static int virtio_crypto_kprocess(FAR struct cryptkop *krp); static int virtio_crypto_freesession(uint64_t tid); static int virtio_crypto_fill_crtlq_request(FAR struct cryptoini *cri, @@ -859,6 +863,837 @@ static int virtio_crypto_process(FAR struct cryptop *crp) return -ENOTSUP; } } + +static int virtio_crypto_rsa_get_session(FAR struct virtio_crypto_s *vcrypto, + FAR struct cryptkop *krp, + FAR uint32_t *sid) +{ + struct virtio_crypto_create_session_input_s input; + struct virtio_crypto_op_ctrl_req_s ctrl; + struct virtio_crypto_req_s req; + FAR struct virtio_device *vdev = vcrypto->vdev; + FAR unsigned char *keybuf; + FAR struct virtqueue *vq; + FAR struct crparam *n; + FAR struct crparam *e; + FAR struct crparam *d; + FAR struct crparam *p; + FAR struct crparam *q; + FAR struct crparam *dp; + FAR struct crparam *dq; + FAR struct crparam *u; + uint32_t keybufsize; + uint32_t keybits; + ssize_t keylen; + bool is_private; + int ret; + + vq = vdev->vrings_info[vcrypto->max_data_queues].vq; + + is_private = krp->krp_keytype == CRYPTO_KEY_TYPE_PRIVATE; + keybits = krp->krp_param[1].crp_nbits; + if (keybits == 0 || keybits > VIRTIO_CRYPTO_RSA_KEYBITS_MAX) + { + return -EINVAL; + } + + keybufsize = VIRTIO_CRYPTO_RSA_DER_MAX_BYTES(keybits, is_private); + + /* Private RSA-8192 DER is ~5.7 KB, too large for the stack. */ + + keybuf = kmm_malloc(keybufsize); + if (keybuf == NULL) + { + return -ENOMEM; + } + + /* Pad ctrl header for creating a new session */ + + memset(&ctrl, 0, sizeof(ctrl)); + ctrl.header.algo = virtio_crypto_asym_get_alg(krp->krp_op); + ctrl.header.opcode = VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION; + ctrl.op_flf.akcipher.algo = ctrl.header.algo; + ctrl.op_flf.akcipher.key_type = is_private ? + VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE : + VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC; + ctrl.op_flf.akcipher.algo_flf.rsa.padding_algo = krp->krp_padding; + ctrl.op_flf.akcipher.algo_flf.rsa.hash_algo = krp->krp_hash; + + if (ctrl.op_flf.akcipher.key_type == + VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE) + { + n = &krp->krp_param[1]; + e = &krp->krp_param[2]; + d = &krp->krp_param[3]; + p = &krp->krp_param[4]; + q = &krp->krp_param[5]; + dp = &krp->krp_param[6]; + dq = &krp->krp_param[7]; + u = &krp->krp_param[8]; + keylen = virtio_crypto_write_rsa_key_der( + (FAR unsigned char *)n->crp_p, n->crp_nbits / 8, + (FAR unsigned char *)e->crp_p, e->crp_nbits / 8, + (FAR unsigned char *)d->crp_p, d->crp_nbits / 8, + (FAR unsigned char *)p->crp_p, p->crp_nbits / 8, + (FAR unsigned char *)q->crp_p, q->crp_nbits / 8, + (FAR unsigned char *)dp->crp_p, dp->crp_nbits / 8, + (FAR unsigned char *)dq->crp_p, dq->crp_nbits / 8, + (FAR unsigned char *)u->crp_p, u->crp_nbits / 8, + keybuf, keybufsize); + } + else + { + n = &krp->krp_param[1]; + e = &krp->krp_param[2]; + keylen = virtio_crypto_write_rsa_pubkey_der( + (FAR unsigned char *)n->crp_p, n->crp_nbits / 8, + (FAR unsigned char *)e->crp_p, e->crp_nbits / 8, + keybuf, keybufsize); + } + + if (keylen < 0) + { + ret = keylen; + goto out; + } + + ctrl.op_flf.akcipher.key_len = keylen; + + input.status = VIRTIO_CRYPTO_ERR; + + virtio_crypto_req_init(&req, vdev); + virtio_crypto_req_out(&req, &ctrl, sizeof(ctrl)); + + /* keybufsize is worst-case; send only the tail actually filled in. */ + + virtio_crypto_req_out(&req, keybuf + keybufsize - keylen, keylen); + virtio_crypto_req_in(&req, &input, sizeof(input)); + + ret = virtio_crypto_req_map(&req); + if (ret < 0) + { + goto out; + } + + ret = virtio_crypto_send_recv(vcrypto, vq, req.vb, req.n_out, req.n_in); + + virtio_crypto_req_unmap(&req); + if (ret < 0) + { + goto out; + } + + if (input.status != VIRTIO_CRYPTO_OK) + { + vrterr("virtio_crypto_s: Create session failed status: %u\n", + input.status); + ret = virtio_crypto_get_errcode(input.status); + } + else + { + *sid = input.session_id; + ret = OK; + } + +out: + kmm_free(keybuf); + return ret; +} + +static int +virtio_crypto_ecdsa_get_session(FAR struct virtio_crypto_s *vcrypto, + FAR struct cryptkop *krp, + FAR uint32_t *sid) +{ + struct virtio_crypto_create_session_input_s input; + struct virtio_crypto_op_ctrl_req_s ctrl; + struct virtio_crypto_req_s req; + FAR struct virtio_device *vdev = vcrypto->vdev; + FAR struct virtqueue *vq; + FAR uint8_t *keybuf = NULL; + uint32_t keylen = 0; + int32_t curve_id; + int curve_len; + int ret; + + vq = vdev->vrings_info[vcrypto->max_data_queues].vq; + + curve_id = virtio_crypto_ecdsa_get_curve(krp->krp_op); + if (curve_id < 0) + { + return curve_id; + } + + curve_len = virtio_crypto_curve_get_len(curve_id); + if (curve_len < 0) + { + return curve_len; + } + + switch (krp->krp_optype) + { + case CRYPTO_OP_SIGN: + keylen = krp->krp_param[0].crp_nbits / 8; + break; + case CRYPTO_OP_VERIFY: + keylen = krp->krp_param[0].crp_nbits / 8 + + krp->krp_param[1].crp_nbits / 8; + break; + case CRYPTO_OP_GENKEY: + + /* Key generation has no input key; session carries no key. */ + + keylen = 0; + break; + default: + return -EINVAL; + } + + /* Bound keylen against the curve's field length before allocating. */ + + if (keylen > (uint32_t)curve_len * 2) + { + return -EINVAL; + } + + if (keylen > 0) + { + keybuf = kmm_malloc(keylen); + if (keybuf == NULL) + { + return -ENOMEM; + } + } + + memset(&ctrl, 0, sizeof(ctrl)); + ctrl.header.algo = VIRTIO_CRYPTO_AKCIPHER_ECDSA; + ctrl.header.opcode = VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION; + ctrl.op_flf.akcipher.algo = VIRTIO_CRYPTO_AKCIPHER_ECDSA; + ctrl.op_flf.akcipher.key_type = + krp->krp_keytype == CRYPTO_KEY_TYPE_PRIVATE ? + VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE : + VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC; + ctrl.op_flf.akcipher.algo_flf.ecdsa.curve_id = curve_id; + ctrl.op_flf.akcipher.key_len = keylen; + + if (krp->krp_optype == CRYPTO_OP_SIGN) + { + memcpy(keybuf, krp->krp_param[0].crp_p, keylen); + } + else if (krp->krp_optype == CRYPTO_OP_VERIFY) + { + memcpy(keybuf, krp->krp_param[0].crp_p, + krp->krp_param[0].crp_nbits / 8); + memcpy(keybuf + krp->krp_param[0].crp_nbits / 8, + krp->krp_param[1].crp_p, + krp->krp_param[1].crp_nbits / 8); + } + + input.status = VIRTIO_CRYPTO_ERR; + + virtio_crypto_req_init(&req, vdev); + virtio_crypto_req_out(&req, &ctrl, sizeof(ctrl)); + if (keylen > 0) + { + virtio_crypto_req_out(&req, keybuf, keylen); + } + + virtio_crypto_req_in(&req, &input, sizeof(input)); + + ret = virtio_crypto_req_map(&req); + if (ret < 0) + { + goto out; + } + + ret = virtio_crypto_send_recv(vcrypto, vq, req.vb, req.n_out, req.n_in); + + virtio_crypto_req_unmap(&req); + if (ret < 0) + { + goto out; + } + + if (input.status != VIRTIO_CRYPTO_OK) + { + ret = virtio_crypto_get_errcode(input.status); + goto out; + } + + *sid = input.session_id; + ret = OK; + +out: + kmm_free(keybuf); + return ret; +} + +static int +virtio_crypto_ecdsa_process(FAR struct virtio_crypto_s *vcrypto, + FAR struct cryptkop *krp, + uint32_t session_id) +{ + struct virtio_crypto_op_data_req_s data; + struct virtio_crypto_inhdr_s inhdr; + struct virtio_crypto_req_s req; + FAR struct virtio_device *vdev = vcrypto->vdev; + FAR struct virtqueue *vq = vdev->vrings_info[0].vq; + FAR uint8_t *src = NULL; + FAR uint8_t *dst = NULL; + uint32_t src_len = 0; + uint32_t dst_len = 0; + uint32_t rlen; + uint32_t slen; + uint32_t mlen; + uint32_t dlen; + uint32_t xlen; + uint32_t ylen; + int32_t curve_id; + int curve_len; + int ret; + + curve_id = virtio_crypto_ecdsa_get_curve(krp->krp_op); + if (curve_id < 0) + { + return curve_id; + } + + curve_len = virtio_crypto_curve_get_len(curve_id); + if (curve_len < 0) + { + return curve_len; + } + + switch (krp->krp_optype) + { + case CRYPTO_OP_SIGN: + src_len = krp->krp_param[1].crp_nbits / 8; + dst_len = krp->krp_param[2].crp_nbits / 8 + + krp->krp_param[3].crp_nbits / 8; + break; + case CRYPTO_OP_VERIFY: + src_len = krp->krp_param[3].crp_nbits / 8 + + krp->krp_param[4].crp_nbits / 8; + dst_len = krp->krp_param[5].crp_nbits / 8; + break; + case CRYPTO_OP_GENKEY: + src_len = 0; + dst_len = krp->krp_param[0].crp_nbits / 8 + + krp->krp_param[1].crp_nbits / 8 + + krp->krp_param[2].crp_nbits / 8; + break; + default: + return -EINVAL; + } + + /* Bound src_len/dst_len against the curve's field length before + * allocating. A signature/shared-key request touches at most a + * handful of curve-sized components (r||s, d||x||y, ...), so 4x + * the field length is a generous but still meaningful upper bound. + */ + + if (src_len > (uint32_t)curve_len * 4 || + dst_len > (uint32_t)curve_len * 4) + { + return -EINVAL; + } + + if (src_len > 0) + { + src = kmm_malloc(src_len); + if (src == NULL) + { + return -ENOMEM; + } + } + + if (dst_len > 0) + { + dst = kmm_malloc(dst_len); + if (dst == NULL) + { + ret = -ENOMEM; + goto out; + } + } + + memset(&data, 0, sizeof(data)); + data.header.algo = VIRTIO_CRYPTO_AKCIPHER_ECDSA; + data.header.session_id = session_id & 0xffffffff; + data.header.flag = krp->krp_flags; + inhdr.status = VIRTIO_CRYPTO_ERR; + + switch (krp->krp_optype) + { + case CRYPTO_OP_SIGN: + data.header.opcode = VIRTIO_CRYPTO_AKCIPHER_SIGN; + data.op_flf.akcipher.src_data_len = src_len; + data.op_flf.akcipher.dst_data_len = dst_len; + memcpy(src, krp->krp_param[1].crp_p, src_len); + break; + case CRYPTO_OP_VERIFY: + rlen = krp->krp_param[3].crp_nbits / 8; + slen = krp->krp_param[4].crp_nbits / 8; + mlen = krp->krp_param[5].crp_nbits / 8; + data.header.opcode = VIRTIO_CRYPTO_AKCIPHER_VERIFY; + data.op_flf.akcipher.src_data_len = rlen + slen; + data.op_flf.akcipher.dst_data_len = mlen; + memcpy(src, krp->krp_param[3].crp_p, rlen); + memcpy(src + rlen, krp->krp_param[4].crp_p, slen); + memcpy(dst, krp->krp_param[5].crp_p, mlen); + break; + case CRYPTO_OP_GENKEY: + data.header.opcode = VIRTIO_CRYPTO_AKCIPHER_GENKEY; + data.op_flf.akcipher.src_data_len = 0; + data.op_flf.akcipher.dst_data_len = dst_len; + break; + default: + break; + } + + virtio_crypto_req_init(&req, vdev); + virtio_crypto_req_out(&req, &data, sizeof(data)); + if (src_len > 0) + { + virtio_crypto_req_out(&req, src, src_len); + } + + if (dst_len > 0) + { + /* VERIFY feeds dst in; SIGN/GENKEY get their result written back. */ + + if (krp->krp_optype == CRYPTO_OP_VERIFY) + { + virtio_crypto_req_out(&req, dst, dst_len); + } + else + { + virtio_crypto_req_in(&req, dst, dst_len); + } + } + + virtio_crypto_req_in(&req, &inhdr, sizeof(inhdr)); + + ret = virtio_crypto_req_map(&req); + if (ret < 0) + { + goto out; + } + + ret = virtio_crypto_send_recv(vcrypto, vq, req.vb, req.n_out, req.n_in); + + virtio_crypto_req_unmap(&req); + if (ret < 0) + { + goto out; + } + + if (inhdr.status != VIRTIO_CRYPTO_OK) + { + ret = virtio_crypto_get_errcode(inhdr.status); + goto out; + } + + switch (krp->krp_optype) + { + case CRYPTO_OP_SIGN: + rlen = krp->krp_param[2].crp_nbits / 8; + slen = krp->krp_param[3].crp_nbits / 8; + memcpy(krp->krp_param[2].crp_p, dst, rlen); + memcpy(krp->krp_param[3].crp_p, dst + rlen, slen); + break; + case CRYPTO_OP_GENKEY: + dlen = krp->krp_param[0].crp_nbits / 8; + xlen = krp->krp_param[1].crp_nbits / 8; + ylen = krp->krp_param[2].crp_nbits / 8; + memcpy(krp->krp_param[0].crp_p, dst, dlen); + memcpy(krp->krp_param[1].crp_p, dst + dlen, xlen); + memcpy(krp->krp_param[2].crp_p, dst + dlen + xlen, ylen); + break; + default: + break; + } + + ret = OK; + +out: + kmm_free(src); + kmm_free(dst); + return ret; +} + +static int +virtio_crypto_ecdh_get_session(FAR struct virtio_crypto_s *vcrypto, + FAR struct cryptkop *krp, + FAR uint32_t *sid) +{ + struct virtio_crypto_create_session_input_s input; + struct virtio_crypto_op_ctrl_req_s ctrl; + struct virtio_crypto_req_s req; + FAR struct virtio_device *vdev = vcrypto->vdev; + FAR struct virtqueue *vq; + int32_t curve_id; + int ret; + + vq = vdev->vrings_info[vcrypto->max_data_queues].vq; + + curve_id = virtio_crypto_ecdsa_get_curve(krp->krp_op); + if (curve_id < 0) + { + return curve_id; + } + + /* ECDH keys travel with each request; the session only fixes the curve */ + + memset(&ctrl, 0, sizeof(ctrl)); + ctrl.header.algo = VIRTIO_CRYPTO_AKCIPHER_ECDH; + ctrl.header.opcode = VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION; + ctrl.op_flf.akcipher.algo = VIRTIO_CRYPTO_AKCIPHER_ECDH; + ctrl.op_flf.akcipher.key_type = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE; + ctrl.op_flf.akcipher.algo_flf.ecdsa.curve_id = curve_id; + input.status = VIRTIO_CRYPTO_ERR; + + virtio_crypto_req_init(&req, vdev); + virtio_crypto_req_out(&req, &ctrl, sizeof(ctrl)); + virtio_crypto_req_in(&req, &input, sizeof(input)); + + ret = virtio_crypto_req_map(&req); + if (ret < 0) + { + return ret; + } + + ret = virtio_crypto_send_recv(vcrypto, vq, req.vb, req.n_out, req.n_in); + + virtio_crypto_req_unmap(&req); + if (ret < 0) + { + return ret; + } + + if (input.status != VIRTIO_CRYPTO_OK) + { + return virtio_crypto_get_errcode(input.status); + } + + *sid = input.session_id; + return OK; +} + +static int +virtio_crypto_ecdh_process(FAR struct virtio_crypto_s *vcrypto, + FAR struct cryptkop *krp, + uint32_t session_id) +{ + struct virtio_crypto_op_data_req_s data; + struct virtio_crypto_inhdr_s inhdr; + struct virtio_crypto_req_s req; + FAR struct virtio_device *vdev = vcrypto->vdev; + FAR struct virtqueue *vq = vdev->vrings_info[0].vq; + FAR struct crparam *d = &krp->krp_param[0]; + FAR struct crparam *x = &krp->krp_param[1]; + FAR struct crparam *y = &krp->krp_param[2]; + FAR uint8_t *buf; + uint32_t dlen = d->crp_nbits / 8; + uint32_t xlen = x->crp_nbits / 8; + uint32_t ylen = y->crp_nbits / 8; + uint32_t klen = 0; + int curve_len; + int ret; + + curve_len = virtio_crypto_curve_get_len( + virtio_crypto_ecdsa_get_curve(krp->krp_op)); + if (curve_len < 0) + { + return curve_len; + } + + if (krp->krp_op == CRK_ECDH_COMPUTE_SHARED) + { + klen = krp->krp_param[3].crp_nbits / 8; + if (klen != curve_len) + { + return -EINVAL; + } + } + + if (dlen != curve_len || xlen != curve_len || ylen != curve_len) + { + return -EINVAL; + } + + /* d || x || y: GEN_PUBLIC output, COMPUTE_SHARED input */ + + buf = kmm_malloc(dlen + xlen + ylen); + if (buf == NULL) + { + return -ENOMEM; + } + + memset(&data, 0, sizeof(data)); + data.header.algo = VIRTIO_CRYPTO_AKCIPHER_ECDH; + data.header.session_id = session_id & 0xffffffff; + data.header.flag = krp->krp_flags; + inhdr.status = VIRTIO_CRYPTO_ERR; + + virtio_crypto_req_init(&req, vdev); + virtio_crypto_req_out(&req, &data, sizeof(data)); + + switch (krp->krp_op) + { + case CRK_ECDH_GEN_PUBLIC: + data.header.opcode = VIRTIO_CRYPTO_AKCIPHER_GENKEY; + data.op_flf.akcipher.dst_data_len = dlen + xlen + ylen; + virtio_crypto_req_in(&req, buf, dlen + xlen + ylen); + break; + case CRK_ECDH_COMPUTE_SHARED: + memcpy(buf, d->crp_p, dlen); + memcpy(buf + dlen, x->crp_p, xlen); + memcpy(buf + dlen + xlen, y->crp_p, ylen); + data.header.opcode = VIRTIO_CRYPTO_AKCIPHER_DERIVE; + data.op_flf.akcipher.src_data_len = dlen + xlen + ylen; + data.op_flf.akcipher.dst_data_len = klen; + virtio_crypto_req_out(&req, buf, dlen + xlen + ylen); + virtio_crypto_req_in(&req, krp->krp_param[3].crp_p, klen); + break; + default: + ret = -EINVAL; + goto out; + } + + virtio_crypto_req_in(&req, &inhdr, sizeof(inhdr)); + + ret = virtio_crypto_req_map(&req); + if (ret < 0) + { + goto out; + } + + ret = virtio_crypto_send_recv(vcrypto, vq, req.vb, req.n_out, req.n_in); + + virtio_crypto_req_unmap(&req); + if (ret < 0) + { + goto out; + } + + if (inhdr.status != VIRTIO_CRYPTO_OK) + { + ret = virtio_crypto_get_errcode(inhdr.status); + goto out; + } + + if (krp->krp_op == CRK_ECDH_GEN_PUBLIC) + { + memcpy(d->crp_p, buf, dlen); + memcpy(x->crp_p, buf + dlen, xlen); + memcpy(y->crp_p, buf + dlen + xlen, ylen); + } + +out: + kmm_free(buf); + return ret; +} + +static int +virtio_crypto_asym_get_session(FAR struct virtio_crypto_s *vcrypto, + FAR struct cryptkop *krp, + FAR uint32_t *sid) +{ + switch (virtio_crypto_asym_get_alg(krp->krp_op)) + { + case VIRTIO_CRYPTO_AKCIPHER_RSA: + return virtio_crypto_rsa_get_session(vcrypto, krp, sid); + case VIRTIO_CRYPTO_AKCIPHER_ECDSA: + return virtio_crypto_ecdsa_get_session(vcrypto, krp, sid); + case VIRTIO_CRYPTO_AKCIPHER_ECDH: + return virtio_crypto_ecdh_get_session(vcrypto, krp, sid); + } + + return -EINVAL; +} + +static void +virtio_crypto_asym_free_session(FAR struct virtio_crypto_s *vcrypto, + FAR struct cryptkop *krp, + uint32_t session_id) +{ + FAR struct virtio_device *vdev = vcrypto->vdev; + FAR struct virtqueue *vq; + struct virtio_crypto_destroy_session_input_s input; + struct virtio_crypto_op_ctrl_req_s ctrl; + struct virtio_crypto_req_s req; + int ret; + + vq = vdev->vrings_info[vcrypto->max_data_queues].vq; + + memset(&ctrl, 0, sizeof(ctrl)); + ctrl.header.opcode = VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION; + ctrl.op_flf.destroy.session_id = session_id; + input.status = VIRTIO_CRYPTO_ERR; + + virtio_crypto_req_init(&req, vdev); + virtio_crypto_req_out(&req, &ctrl, sizeof(ctrl)); + virtio_crypto_req_in(&req, &input, sizeof(input)); + + if (virtio_crypto_req_map(&req) < 0) + { + return; + } + + ret = virtio_crypto_send_recv(vcrypto, vq, req.vb, req.n_out, req.n_in); + + virtio_crypto_req_unmap(&req); + if (ret == 0 && input.status != VIRTIO_CRYPTO_OK) + { + vrterr("virtio_crypto_s: Close session failed status: %u," + "session_id: 0x%lu\n", input.status, + ctrl.op_flf.destroy.session_id); + } +} + +static int virtio_crypto_rsa_process(FAR struct virtio_crypto_s *vcrypto, + FAR struct cryptkop *krp, + uint32_t session_id) +{ + FAR struct virtio_device *vdev = vcrypto->vdev; + FAR struct virtqueue *vq = vdev->vrings_info[0].vq; + FAR struct crparam *src_param; + struct virtio_crypto_op_data_req_s data; + struct virtio_crypto_inhdr_s inhdr; + struct virtio_crypto_req_s req; + FAR struct crparam *dst_param; + int iparams = krp->krp_iparams; + int oparams = krp->krp_oparams; + uint32_t src_len; + uint32_t dst_len; + int ret; + + src_param = &krp->krp_param[0]; + dst_param = &krp->krp_param[iparams + oparams - 1]; + src_len = src_param->crp_nbits / 8; + dst_len = dst_param->crp_nbits / 8; + + memset(&data, 0, sizeof(data)); + data.header.session_id = session_id; + data.header.algo = virtio_crypto_asym_get_alg(krp->krp_op); + + switch (krp->krp_optype) + { + case CRYPTO_OP_DECRYPT: + data.header.opcode = VIRTIO_CRYPTO_AKCIPHER_DECRYPT; + break; + case CRYPTO_OP_ENCRYPT: + data.header.opcode = VIRTIO_CRYPTO_AKCIPHER_ENCRYPT; + break; + case CRYPTO_OP_SIGN: + data.header.opcode = VIRTIO_CRYPTO_AKCIPHER_SIGN; + break; + case CRYPTO_OP_VERIFY: + data.header.opcode = VIRTIO_CRYPTO_AKCIPHER_VERIFY; + break; + default: + return -EINVAL; + } + + data.op_flf.akcipher.src_data_len = src_len; + data.op_flf.akcipher.dst_data_len = dst_len; + inhdr.status = VIRTIO_CRYPTO_ERR; + + virtio_crypto_req_init(&req, vdev); + virtio_crypto_req_out(&req, &data, sizeof(data)); + virtio_crypto_req_out(&req, src_param->crp_p, src_len); + if (krp->krp_optype == CRYPTO_OP_VERIFY) + { + virtio_crypto_req_out(&req, dst_param->crp_p, dst_len); + } + else + { + virtio_crypto_req_in(&req, dst_param->crp_p, dst_len); + } + + virtio_crypto_req_in(&req, &inhdr, sizeof(inhdr)); + + ret = virtio_crypto_req_map(&req); + if (ret < 0) + { + return ret; + } + + ret = virtio_crypto_send_recv(vcrypto, vq, req.vb, req.n_out, req.n_in); + + virtio_crypto_req_unmap(&req); + + if (ret == 0 && inhdr.status != VIRTIO_CRYPTO_OK) + { + vrterr("virtio_crypto_s: Akcipher process failed status: %u," + "session_id: 0x%" PRIx32 "\n", inhdr.status, session_id); + ret = virtio_crypto_get_errcode(inhdr.status); + } + + return ret; +} + +static int virtio_crypto_asym_process(FAR struct virtio_crypto_s *vcrypto, + FAR struct cryptkop *krp, + uint32_t session_id) +{ + switch (virtio_crypto_asym_get_alg(krp->krp_op)) + { + case VIRTIO_CRYPTO_AKCIPHER_RSA: + return virtio_crypto_rsa_process(vcrypto, krp, session_id); + case VIRTIO_CRYPTO_AKCIPHER_ECDSA: + return virtio_crypto_ecdsa_process(vcrypto, krp, session_id); + case VIRTIO_CRYPTO_AKCIPHER_ECDH: + return virtio_crypto_ecdh_process(vcrypto, krp, session_id); + } + + return -EINVAL; +} + +static int virtio_crypto_kprocess(FAR struct cryptkop *krp) +{ + FAR struct virtio_crypto_s *vcrypto; + uint32_t hid; + uint32_t sid = 0; + int ret; + + if (krp == NULL) + { + vrterr("Virtio Crypto kprocess: Invalid parameters\n"); + return -EINVAL; + } + + hid = (uint32_t)krp->krp_hid; + vcrypto = crypto_driver_get_priv(hid); + if (vcrypto == NULL) + { + vrterr("Virtio Crypto kprocess: Invalid context\n"); + return -EINVAL; + } + + /* create session */ + + ret = virtio_crypto_asym_get_session(vcrypto, krp, &sid); + if (ret < 0) + { + return ret; + } + + /* process akcipher data */ + + ret = virtio_crypto_asym_process(vcrypto, krp, sid); + if (ret < 0) + { + vrterr("Virtio Crypto kprocess failed, ret = %d\n", ret); + } + + /* free session */ + + virtio_crypto_asym_free_session(vcrypto, krp, sid); + return ret; +} + static void virtio_crypto_notify(FAR struct virtqueue *vq) { FAR struct virtio_crypto_s *vcrypto = vq->vq_dev->priv; @@ -916,6 +1751,7 @@ static int virtio_crypto_probe(FAR struct virtio_device *vdev) { FAR struct virtio_crypto_s *vcrypto; int algs[CRYPTO_ALGORITHM_MAX + 1]; + int kalgs[CRK_ALGORITHM_MAX + 1]; int ret; int i; @@ -1013,6 +1849,22 @@ static int virtio_crypto_probe(FAR struct virtio_device *vdev) goto err; } + for (i = 0; i <= CRK_ALGORITHM_MAX; i++) + { + kalgs[i] = CRYPTO_ALG_FLAG_SUPPORTED; + } + + ret = crypto_kregister(vcrypto->dev_id, kalgs, virtio_crypto_kprocess); + if (ret < 0) + { + for (i = 0; i <= CRYPTO_ALGORITHM_MAX; i++) + { + crypto_unregister(vcrypto->dev_id, i); + } + + goto err; + } + return ret; err:
