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 4af8c370f4a74964ac6a170086c4904e26632fe6 Author: makejian <[email protected]> AuthorDate: Mon Nov 23 15:58:10 2026 +0800 crypto: add rpmsg crypto client driver Add the client side of the rpmsg crypto transport. The client core registers itself as a crypto driver for every symmetric algorithm and forwards crypto_newsession(), crypto_invoke() and crypto_freesession() requests to the server core over an rpmsg endpoint named "rpmsg-crypto": - Session create and destroy are mapped to the virtio crypto control request format; the session id returned by the remote core is kept as the per-slot tid. - Cipher, hash, MAC and AEAD operations are chunked into the rpmsg buffer size, streamed over CRD_F_UPDATE messages when the data does not fit, and support the CRYPTO_F_CBIMM asynchronous completion path through the response handler. - A CRYPTO_F_CANCEL request is forwarded as a fire-and-forget cancel message carrying the session tid and reqid. - CONFIG_CRYPTO_RPMSG_SESSIONS_NUM session slots are pre-allocated at registration time and never reallocated, so the semaphore embedded in a slot cannot move while a response handler holds its cookie. - CONFIG_CRYPTO_RPMSG_CONNECT_TIMEOUT_MS bounds the wait for the remote endpoint to bind. The wire protocol matches the remote-core crypto offload protocol in use on Xiaomi Vela. Signed-off-by: makejian <[email protected]> --- drivers/crypto/CMakeLists.txt | 4 + drivers/crypto/Kconfig | 33 ++ drivers/crypto/Make.defs | 4 + drivers/crypto/rpmsg_crypto.c | 1309 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1350 insertions(+) diff --git a/drivers/crypto/CMakeLists.txt b/drivers/crypto/CMakeLists.txt index 3c93183635e..9f2d913632a 100644 --- a/drivers/crypto/CMakeLists.txt +++ b/drivers/crypto/CMakeLists.txt @@ -27,3 +27,7 @@ endif() if(CONFIG_DEV_RNG90) target_sources(drivers PRIVATE rng90.c) endif() + +if(CONFIG_CRYPTO_RPMSG) + target_sources(drivers PRIVATE rpmsg_crypto.c) +endif() diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig index fbc357dae62..855861cbb4a 100644 --- a/drivers/crypto/Kconfig +++ b/drivers/crypto/Kconfig @@ -95,6 +95,39 @@ endchoice # /dev/urandom algorithm endif # DEV_URANDOM +config CRYPTO_RPMSG + bool "RPMSG Crypto Driver Client Support" + default n + depends on RPMSG + depends on CRYPTO + select CRYPTO_BACKENDS + ---help--- + Offload the NuttX crypto API to a remote core over rpmsg. The + client core registers the transport as a crypto driver and + forwards session and operation requests to the server core. + +config CRYPTO_RPMSG_SESSIONS_NUM + int "Number of pre-allocated rpmsg crypto session slots" + default 8 + depends on CRYPTO_RPMSG + ---help--- + Number of session slots pre-allocated at registration time for + the rpmsg crypto client. The slot array has a fixed size and is + never reallocated, because relocating it would move the embedded + per-session semaphore and dangle any in-flight waiter and response + handler cookie (use-after-free). Set this to the maximum number of + concurrent crypto sessions plus in-flight async operations. When + all slots are busy, new session requests fail with -ENOMEM. + +config CRYPTO_RPMSG_CONNECT_TIMEOUT_MS + int "Timeout (ms) waiting for rpmsg crypto server connection" + default -1 + depends on CRYPTO_RPMSG + ---help--- + Maximum time in milliseconds to wait for the remote rpmsg crypto + server endpoint to bind before returning -ENOTCONN. A value of -1 + means wait indefinitely (original behaviour). + menuconfig DEV_SE05X bool "Enable secure element (SE05X)" depends on I2C diff --git a/drivers/crypto/Make.defs b/drivers/crypto/Make.defs index e243bd916fa..708bf1791c0 100644 --- a/drivers/crypto/Make.defs +++ b/drivers/crypto/Make.defs @@ -35,6 +35,10 @@ ifeq ($(CONFIG_DEV_RNG90),y) CSRCS += rng90.c endif +ifeq ($(CONFIG_CRYPTO_RPMSG),y) + CSRCS += rpmsg_crypto.c +endif + # Include crypto device driver build support DEPPATH += --dep-path crypto diff --git a/drivers/crypto/rpmsg_crypto.c b/drivers/crypto/rpmsg_crypto.c new file mode 100644 index 00000000000..4bf56c0ce26 --- /dev/null +++ b/drivers/crypto/rpmsg_crypto.c @@ -0,0 +1,1309 @@ +/**************************************************************************** + * drivers/crypto/rpmsg_crypto.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <debug.h> +#include <inttypes.h> +#include <stdio.h> +#include <sys/param.h> + +#include <crypto/cryptodev.h> +#include <crypto/virtio_crypto.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mutex.h> +#include <nuttx/nuttx.h> +#include <nuttx/rpmsg/rpmsg.h> +#include <nuttx/crypto/crypto.h> + +#include "rpmsg_crypto.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct rpmsg_crypto_session_s +{ + bool used; /* The session is used */ + sem_t sem; /* Semaphore used for rpmsg */ + uint64_t tid; /* The session id */ + int result; /* The return value of the remote call */ + FAR void *data; /* The return data buffer of the remote call */ + uint32_t offset; /* CBIMM: accumulated output offset */ +}; + +struct rpmsg_crypto_s +{ + struct rpmsg_endpoint ept; /* Rpmsg endpoint */ + FAR const char *remotecpu; /* The server cpu name */ + sem_t wait; /* Wait sem, used for preventing any + * operation until the connection + * between two cpu established. + */ + mutex_t lock; /* Protect session alloc/free */ + uint8_t devid; /* The device id registered */ + uint32_t sesnum; /* The number of sessions */ + FAR struct rpmsg_crypto_session_s + *sessions; /* The session data slots */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rpmsg_crypto_newsession_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsg_crypto_process_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsg_crypto_freesession_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Rpmsg crypto response handler table */ + +static const rpmsg_ept_cb g_rpmsg_crypto_handler[] = +{ + [RPMSG_CRYPTO_NEWSESSION] = rpmsg_crypto_newsession_handler, + [RPMSG_CRYPTO_PROCESS] = rpmsg_crypto_process_handler, + [RPMSG_CRYPTO_FREESESSION] = rpmsg_crypto_freesession_handler, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsg_crypto_get_tx_payload_buffer + * + * Description: + * Get the rpmsg crypto tx payload, the buffer is from the rpmsg + * share memory that can be accessed by local and remote cpu. + * + * Parameters: + * priv - The rpmsg-crypto handle + * len - The got memory size + * + * Returned Values: + * NULL - failure + * not NULL - success + * + ****************************************************************************/ + +static FAR void * +rpmsg_crypto_get_tx_payload_buffer(FAR struct rpmsg_crypto_s *priv, + FAR uint32_t *len) +{ + int sval = 0; + + nxsem_get_value(&priv->wait, &sval); + if (sval <= 0) + { +#if CONFIG_CRYPTO_RPMSG_CONNECT_TIMEOUT_MS < 0 + if (rpmsg_wait(&priv->ept, &priv->wait) < 0) +#else + if (rpmsg_tickwait(&priv->ept, &priv->wait, + MSEC2TICK( + CONFIG_CRYPTO_RPMSG_CONNECT_TIMEOUT_MS)) < 0) +#endif + { + return NULL; + } + + rpmsg_post(&priv->ept, &priv->wait); + } + + return rpmsg_get_tx_payload_buffer(&priv->ept, len, true); +} + +/**************************************************************************** + * Name: rpmsg_crypto_send_recv + * + * Description: + * Send and receive the rpmsg crypto data reserved by + * rpmsg_get_tx_payload_buffer(). + * + * Parameters: + * priv - rpmsg crypto handle + * command - the command, RPMSG_CRYPTO_NEWSESSION, RPMSG_CRYPTO_PROCESS, + * RPMSG_CRYPTO_FREESESSION + * msg - the message header + * len - length of the payload + * data - the data + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsg_crypto_send_recv(FAR struct rpmsg_crypto_s *priv, + FAR struct rpmsg_crypto_session_s *session, + FAR struct rpmsg_crypto_header_s *msg, + uint32_t command, uint32_t reqid, + int len, bool waiting) +{ + int ret; + + msg->command = command; + msg->result = -ENXIO; + msg->cookie = (uintptr_t)session; + msg->reqid = reqid; + + ret = rpmsg_send_nocopy(&priv->ept, msg, len); + if (ret < 0) + { + rpmsg_release_tx_buffer(&priv->ept, msg); + return ret; + } + + if (waiting && rpmsg_wait(&priv->ept, &session->sem) >= 0) + { + ret = session->result; + } + + return ret; +} + +/**************************************************************************** + * Name: rpmsg_crypto_cancel + * + * Description: + * Send a fire-and-forget cancel for an in-flight op. It needs no session + * and expects no response, so it does not take a session slot or wait: it + * just grabs a tx buffer and sends. A symmetric cancel carries the session + * (tid) for sid-based routing on the server; an asymmetric cancel carries + * the op. Matched by the reqid in the header; backends that cannot abort + * ignore it. + * + ****************************************************************************/ + +static int rpmsg_crypto_cancel(FAR struct rpmsg_crypto_s *rdev, + uint32_t reqid, uint64_t tid, uint32_t op) +{ + FAR struct rpmsg_crypto_cancel_s *msg; + uint32_t space; + int ret; + + if (rdev == NULL) + { + return -EINVAL; + } + + msg = rpmsg_crypto_get_tx_payload_buffer(rdev, &space); + if (msg == NULL) + { + return -ENOMEM; + } + + memset(msg, 0, sizeof(*msg)); + msg->header.command = RPMSG_CRYPTO_CANCEL; + msg->header.reqid = reqid; + msg->header.tid = tid; + msg->op = op; + + ret = rpmsg_send_nocopy(&rdev->ept, msg, sizeof(*msg)); + if (ret < 0) + { + rpmsg_release_tx_buffer(&rdev->ept, msg); + } + + return ret; +} + +static int rpmsg_crypto_get_session(FAR struct rpmsg_crypto_s *rdev) +{ + uint32_t i; + + nxmutex_lock(&rdev->lock); + + for (i = 0; i < rdev->sesnum; i++) + { + if (!rdev->sessions[i].used) + { + break; + } + } + + if (i == rdev->sesnum) + { + nxmutex_unlock(&rdev->lock); + return -ENOMEM; + } + + rdev->sessions[i].used = true; + rdev->sessions[i].data = NULL; + nxsem_init(&rdev->sessions[i].sem, 0, 0); + nxmutex_unlock(&rdev->lock); + return i; +} + +static int rpmsg_crypto_put_session(FAR struct rpmsg_crypto_s *rdev, + uint32_t sid) +{ + if (sid >= rdev->sesnum) + { + return -EINVAL; + } + + nxmutex_lock(&rdev->lock); + nxsem_destroy(&rdev->sessions[sid].sem); + rdev->sessions[sid].used = false; + nxmutex_unlock(&rdev->lock); + return OK; +} + +static int +rpmsg_crypto_fill_session_request(FAR struct cryptoini *cri, + FAR struct rpmsg_crypto_newsession_s *msg) +{ + FAR struct virtio_crypto_op_ctrl_req_s *ctrl = &msg->ctrl; + int service = virtio_crypto_get_service(cri->cri_alg); + int algo = virtio_crypto_get_alg(cri->cri_alg); + + if (service < 0) + { + return service; + } + + if (algo < 0) + { + return algo; + } + + memset(ctrl, 0, sizeof(*ctrl)); + + /* Pad ctrl header for creating a new session */ + + ctrl->header.algo = algo; + if (cri->cri_flags & CRD_F_KEYID) + { + ctrl->header.flag |= VIRTIO_CRYPTO_KEYID; + } + + switch (service) + { + case VIRTIO_CRYPTO_SERVICE_CIPHER: + ctrl->header.opcode = VIRTIO_CRYPTO_CIPHER_CREATE_SESSION; + ctrl->op_flf.sym.op_type = VIRTIO_CRYPTO_SYM_OP_CIPHER; + ctrl->op_flf.sym.op_flf.cipher.algo = algo; + ctrl->op_flf.sym.op_flf.cipher.key_len = cri->cri_klen / 8; + ctrl->op_flf.sym.op_flf.cipher.op = + cri->cri_op == COP_ENCRYPT ? + VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT; + break; + case VIRTIO_CRYPTO_SERVICE_HASH: + ctrl->header.opcode = VIRTIO_CRYPTO_HASH_CREATE_SESSION; + ctrl->op_flf.hash.algo = algo; + ctrl->op_flf.hash.hash_result_len = + virtio_crypto_get_hash_result_len(algo); + break; + case VIRTIO_CRYPTO_SERVICE_MAC: + ctrl->header.opcode = VIRTIO_CRYPTO_MAC_CREATE_SESSION; + ctrl->op_flf.mac.algo = algo; + ctrl->op_flf.mac.hash_result_len = + virtio_crypto_get_hash_result_len(algo); + ctrl->op_flf.mac.auth_key_len = cri->cri_klen / 8; + break; + case VIRTIO_CRYPTO_SERVICE_AEAD: + ctrl->header.opcode = VIRTIO_CRYPTO_AEAD_CREATE_SESSION; + ctrl->op_flf.aead.algo = algo; + ctrl->op_flf.aead.key_len = cri->cri_klen / 8; + ctrl->op_flf.aead.tag_len = virtio_crypto_get_tag_len(service); + ctrl->op_flf.aead.op = cri->cri_op == COP_ENCRYPT ? + VIRTIO_CRYPTO_OP_ENCRYPT : VIRTIO_CRYPTO_OP_DECRYPT; + break; + default: + return -EINVAL; + } + + return OK; +} + +static int rpmsg_crypto_newsession(FAR uint32_t *sid, + FAR struct cryptoini *cri) +{ + FAR struct rpmsg_crypto_s *rdev; + FAR struct rpmsg_crypto_newsession_s *msg; + uint32_t len = sizeof(*msg); + uint32_t space; + int ret; + + if (sid == NULL || cri == NULL) + { + return -EINVAL; + } + + rdev = (FAR struct rpmsg_crypto_s *)crypto_driver_get_priv(*sid); + if (rdev == NULL) + { + rpmsgerr("Invalid context\n"); + return -EINVAL; + } + + ret = rpmsg_crypto_get_session(rdev); + if (ret < 0) + { + return ret; + } + + *sid = ret; + msg = rpmsg_crypto_get_tx_payload_buffer(rdev, &space); + if (msg == NULL) + { + ret = -ENOMEM; + goto fail; + } + + /* create newsession msg */ + + ret = rpmsg_crypto_fill_session_request(cri, msg); + if (ret < 0) + { + rpmsgerr("Invalid parameters in newsession\n"); + rpmsg_release_tx_buffer(&rdev->ept, msg); + goto fail; + } + + if (cri->cri_klen > 0) + { + memcpy(msg->buf, cri->cri_key, cri->cri_klen / 8); + len += cri->cri_klen / 8; + } + + ret = rpmsg_crypto_send_recv(rdev, &rdev->sessions[*sid], + &msg->header, RPMSG_CRYPTO_NEWSESSION, 0, + len, true); + if (ret < 0) + { + goto fail; + } + + return rdev->sessions[*sid].result; + +fail: + rpmsg_crypto_put_session(rdev, *sid); + return ret; +} + +static int rpmsg_crypto_fill_data_request( + FAR struct cryptop *crp, + FAR struct rpmsg_crypto_process_s *msg) +{ + FAR struct virtio_crypto_op_data_req_s *data = &msg->data; + FAR struct virtio_crypto_op_header_s *header = &data->header; + FAR struct cryptodesc *crda = crp->crp_desc; + FAR struct cryptodesc *crde = crda->crd_next; + int service = virtio_crypto_get_service(crda->crd_alg); + int algo = virtio_crypto_get_alg(crda->crd_alg); + + if (service < 0) + { + return service; + } + + if (algo < 0) + { + return algo; + } + + memset(data, 0, sizeof(struct virtio_crypto_op_data_req_s)); + header->algo = algo; + switch (service) + { + case VIRTIO_CRYPTO_SERVICE_CIPHER: + header->opcode = crda->crd_flags & CRD_F_ENCRYPT ? + VIRTIO_CRYPTO_CIPHER_ENCRYPT : VIRTIO_CRYPTO_CIPHER_DECRYPT; + data->op_flf.sym.op_type = VIRTIO_CRYPTO_SYM_OP_CIPHER; + data->op_flf.sym.op_type_flf.cipher.src_data_len = crp->crp_ilen; + data->op_flf.sym.op_type_flf.cipher.dst_data_len = crp->crp_olen; + crp->crp_opaque = crp->crp_dst; + break; + case VIRTIO_CRYPTO_SERVICE_HASH: + header->opcode = VIRTIO_CRYPTO_HASH; + data->op_flf.hash.src_data_len = crp->crp_ilen; + break; + case VIRTIO_CRYPTO_SERVICE_MAC: + header->opcode = VIRTIO_CRYPTO_MAC; + data->op_flf.mac.hdr.src_data_len = crp->crp_ilen; + data->op_flf.mac.hdr.iterations = crp->crp_iter; + break; + case VIRTIO_CRYPTO_SERVICE_AEAD: + header->opcode = crde->crd_flags & CRD_F_ENCRYPT ? + VIRTIO_CRYPTO_AEAD_ENCRYPT : VIRTIO_CRYPTO_AEAD_DECRYPT; + data->op_flf.aead.iv_len = crp->crp_ivlen; + data->op_flf.aead.tag_len = virtio_crypto_get_tag_len(service); + break; + default: + return -ENOTSUP; + } + + header->flag = crp->crp_flags; + return OK; +} + +static FAR struct rpmsg_crypto_process_s * +rpmsg_crypto_get_process_msg(FAR struct rpmsg_crypto_s * rdev, + FAR struct rpmsg_crypto_process_s *src, + FAR uint32_t *space) +{ + FAR struct rpmsg_crypto_process_s *msg; + + msg = rpmsg_crypto_get_tx_payload_buffer(rdev, space); + if (msg == NULL) + { + return NULL; + } + + if (*space <= sizeof(*msg)) + { + rpmsg_release_tx_buffer(&rdev->ept, msg); + return NULL; + } + + msg->header = src->header; + msg->data = src->data; + return msg; +} + +static int +rpmsg_crypto_process_endec(FAR struct rpmsg_crypto_s *rdev, + FAR struct rpmsg_crypto_process_s *info, + FAR struct rpmsg_crypto_session_s *session, + FAR struct cryptop *crp) +{ + FAR struct rpmsg_crypto_process_s *msg; + FAR struct rpmsg_crypto_process_s *rsp; + FAR char *buf; + uint32_t header_size; + uint32_t data_size; + uint32_t offset = 0; + uint32_t space; + bool need_iv = !(crp->crp_desc->crd_flags & CRD_F_UPDATE); + bool async = crp->crp_flags & CRYPTO_F_CBIMM; + int remaining = crp->crp_ilen; + int blksize = virtio_crypto_get_blocksize(crp->crp_desc->crd_alg); + int ret; + + if (blksize < 0) + { + return blksize; + } + + if (crp->crp_dst == NULL) + { + return -EINVAL; + } + + while (remaining > 0) + { + msg = rpmsg_crypto_get_process_msg(rdev, info, &space); + if (msg == NULL) + { + return -ENOMEM; + } + + buf = msg->buf; + header_size = sizeof(*msg); + data_size = space - sizeof(*msg); + if (remaining == crp->crp_ilen && need_iv) + { + if (data_size <= crp->crp_ivlen) + { + rpmsg_release_tx_buffer(&rdev->ept, msg); + return -ENOMEM; + } + + msg->data.op_flf.sym.op_type_flf.cipher.iv_len = crp->crp_ivlen; + memcpy(buf, crp->crp_iv, crp->crp_ivlen); + header_size += crp->crp_ivlen; + data_size -= crp->crp_ivlen; + buf += crp->crp_ivlen; + } + else + { + msg->data.op_flf.sym.op_type_flf.cipher.iv_len = 0; + } + + data_size = MIN(data_size, remaining); + if (data_size < remaining) + { + data_size = ALIGN_DOWN(data_size, blksize); + } + + if (data_size == 0) + { + rpmsg_release_tx_buffer(&rdev->ept, msg); + return -ENOMEM; + } + + memcpy(buf, crp->crp_buf + offset, data_size); + ret = rpmsg_crypto_send_recv(rdev, session, &msg->header, + RPMSG_CRYPTO_PROCESS, crp->crp_reqid, + header_size + data_size, !async); + if (ret < 0) + { + return ret; + } + + if (!async && session->data != NULL) + { + rsp = session->data; + memcpy(crp->crp_dst + offset, rsp->buf, data_size); + rpmsg_release_rx_buffer(&rdev->ept, rsp); + session->data = NULL; + } + + remaining -= data_size; + offset += data_size; + } + + return async ? OK : session->result; +} + +static int +rpmsg_crypto_process_hash_mac(FAR struct rpmsg_crypto_s *rdev, + FAR struct rpmsg_crypto_process_s *info, + FAR struct rpmsg_crypto_session_s *session, + FAR struct cryptop *crp) +{ + FAR struct rpmsg_crypto_process_s *msg; + FAR struct rpmsg_crypto_process_s *rsp; + uint32_t flags = crp->crp_desc->crd_flags; + uint32_t offset = 0; + uint32_t data_size; + uint32_t space; + bool async = crp->crp_flags & CRYPTO_F_CBIMM; + int service = virtio_crypto_get_service(crp->crp_desc->crd_alg); + int remaining = crp->crp_ilen; + int ret; + + do + { + msg = rpmsg_crypto_get_process_msg(rdev, info, &space); + if (msg == NULL) + { + return -ENOMEM; + } + + data_size = MIN(space - sizeof(*msg), remaining); + if (data_size > 0) + { + memcpy(msg->buf, crp->crp_buf + offset, data_size); + } + + offset += data_size; + remaining -= data_size; + if (remaining == 0 && !(flags & CRD_F_UPDATE)) + { + if (service == VIRTIO_CRYPTO_SERVICE_HASH) + { + msg->data.op_flf.hash.hash_result_len = crp->crp_olen; + } + else if (service == VIRTIO_CRYPTO_SERVICE_MAC) + { + msg->data.op_flf.mac.hdr.hash_result_len = crp->crp_olen; + } + } + + ret = rpmsg_crypto_send_recv(rdev, session, &msg->header, + RPMSG_CRYPTO_PROCESS, crp->crp_reqid, + sizeof(*msg) + data_size, + remaining == 0 && + !(flags & CRD_F_UPDATE) && !async); + if (ret < 0) + { + return ret; + } + + if (!async && session->data != NULL) + { + rsp = session->data; + memcpy(crp->crp_mac, rsp->buf, crp->crp_olen); + rpmsg_release_rx_buffer(&rdev->ept, rsp); + session->data = NULL; + } + } + while (remaining > 0); + + return async ? OK : session->result; +} + +static int +rpmsg_crypto_process_aead_update_aad( + FAR struct rpmsg_crypto_s *rdev, + FAR struct rpmsg_crypto_process_s *info, + FAR struct rpmsg_crypto_session_s *session, + FAR struct cryptop *crp) +{ + FAR struct rpmsg_crypto_process_s *msg; + uint32_t flags = crp->crp_desc->crd_flags; + uint32_t remaining = crp->crp_aadlen; + uint32_t ivlen = crp->crp_ivlen; + uint32_t offset = 0; + uint32_t datalen; + uint32_t space; + uint32_t len; + FAR char *buf; + int ret; + + do + { + msg = rpmsg_crypto_get_process_msg(rdev, info, &space); + if (msg == NULL) + { + return -ENOMEM; + } + + buf = msg->buf; + len = sizeof(*msg); + msg->data.header.flag = flags; + msg->data.op_flf.aead.dst_data_len = crp->crp_olen; + + /* First packet includes IV (ivlen > 0), then reset to 0 */ + + msg->data.op_flf.aead.iv_len = ivlen; + if (ivlen > 0) + { + memcpy(buf, crp->crp_iv, ivlen); + len += ivlen; + buf += ivlen; + ivlen = 0; + } + + /* Copy AAD data */ + + if (space <= len) + { + rpmsg_release_tx_buffer(&rdev->ept, msg); + return -ENOMEM; + } + + datalen = MIN(space - len, remaining); + if (datalen > 0) + { + memcpy(buf, crp->crp_aad + offset, datalen); + len += datalen; + offset += datalen; + remaining -= datalen; + } + + msg->data.op_flf.aead.aad_len = datalen; + msg->data.op_flf.aead.src_data_len = 0; + + /* Send packet without waiting - AAD produces no output */ + + ret = rpmsg_crypto_send_recv(rdev, session, &msg->header, + RPMSG_CRYPTO_PROCESS, crp->crp_reqid, + len, false); + if (ret < 0) + { + return ret; + } + } + while (remaining > 0); + + return session->result; +} + +static int +rpmsg_crypto_process_aead_update(FAR struct rpmsg_crypto_s *rdev, + FAR struct rpmsg_crypto_process_s *info, + FAR struct rpmsg_crypto_session_s *session, + FAR struct cryptop *crp) +{ + FAR struct rpmsg_crypto_process_s *msg; + FAR struct rpmsg_crypto_process_s *rsp; + FAR char *buf; + uint32_t remaining = crp->crp_ilen; + uint32_t flags = crp->crp_desc->crd_flags; + uint32_t offset = 0; + uint32_t datalen; + uint32_t space; + uint32_t len; + bool async = crp->crp_flags & CRYPTO_F_CBIMM; + int blksize; + int ret; + + blksize = virtio_crypto_get_blocksize(crp->crp_desc->crd_next->crd_alg); + if (blksize < 0) + { + return blksize; + } + + /* Process data in packets if needed */ + + while (remaining > 0) + { + msg = rpmsg_crypto_get_process_msg(rdev, info, &space); + if (msg == NULL) + { + return -ENOMEM; + } + + buf = msg->buf; + len = sizeof(*msg); + msg->data.header.flag = flags; + msg->data.op_flf.aead.dst_data_len = crp->crp_olen; + msg->data.op_flf.aead.aad_len = 0; + + /* Copy data */ + + if (space <= len) + { + rpmsg_release_tx_buffer(&rdev->ept, msg); + return -ENOMEM; + } + + datalen = MIN(space - len, remaining); + if (datalen < remaining) + { + datalen = ALIGN_DOWN(datalen, blksize); + } + + if (datalen == 0) + { + rpmsg_release_tx_buffer(&rdev->ept, msg); + return -ENOMEM; + } + + memcpy(buf, crp->crp_buf + offset, datalen); + len += datalen; + offset += datalen; + remaining -= datalen; + + msg->data.op_flf.aead.src_data_len = datalen; + + ret = rpmsg_crypto_send_recv(rdev, session, &msg->header, + RPMSG_CRYPTO_PROCESS, crp->crp_reqid, + len, !async); + if (ret < 0) + { + return ret; + } + + if (!async && session->data != NULL) + { + rsp = session->data; + memcpy(crp->crp_dst + offset - datalen, rsp->buf, datalen); + rpmsg_release_rx_buffer(&rdev->ept, rsp); + session->data = NULL; + } + } + + return async ? OK : session->result; +} + +static int +rpmsg_crypto_process_aead_final(FAR struct rpmsg_crypto_s *rdev, + FAR struct rpmsg_crypto_process_s *info, + FAR struct rpmsg_crypto_session_s *session, + FAR struct cryptop *crp) +{ + FAR struct rpmsg_crypto_process_s *msg; + FAR struct rpmsg_crypto_process_s *rsp; + bool async = crp->crp_flags & CRYPTO_F_CBIMM; + uint32_t flags = crp->crp_desc->crd_flags; + uint32_t space; + int ret; + + msg = rpmsg_crypto_get_process_msg(rdev, info, &space); + if (msg == NULL) + { + return -ENOMEM; + } + + /* Pass the flags to server for proper handling */ + + msg->data.header.flag = flags; + msg->data.op_flf.aead.dst_data_len = 0; + msg->data.op_flf.aead.iv_len = 0; + msg->data.op_flf.aead.aad_len = 0; + msg->data.op_flf.aead.src_data_len = 0; + + /* Both encrypt and decrypt receive tag from server */ + + ret = rpmsg_crypto_send_recv(rdev, session, &msg->header, + RPMSG_CRYPTO_PROCESS, crp->crp_reqid, + sizeof(*msg), !async); + if (ret < 0) + { + return ret; + } + + if (!async && session->data != NULL) + { + rsp = session->data; + memcpy(crp->crp_mac, rsp->buf, crp->crp_olen); + rpmsg_release_rx_buffer(&rdev->ept, rsp); + session->data = NULL; + } + + return async ? OK : session->result; +} + +static int +rpmsg_crypto_process_aead(FAR struct rpmsg_crypto_s *rdev, + FAR struct rpmsg_crypto_process_s *info, + FAR struct rpmsg_crypto_session_s *session, + FAR struct cryptop *crp) +{ + uint32_t flags = crp->crp_desc->crd_flags; + + if (flags & CRD_F_UPDATE_AAD) + { + return rpmsg_crypto_process_aead_update_aad(rdev, info, session, crp); + } + else if (flags & CRD_F_UPDATE) + { + return rpmsg_crypto_process_aead_update(rdev, info, session, crp); + } + else + { + return rpmsg_crypto_process_aead_final(rdev, info, session, crp); + } +} + +static int rpmsg_crypto_process(FAR struct cryptop *crp) +{ + FAR struct rpmsg_crypto_s *rdev; + FAR struct rpmsg_crypto_session_s *session; + struct rpmsg_crypto_process_s info; + uint32_t hid = (uint32_t)(crp->crp_sid >> 32); + uint32_t sid = (uint32_t)crp->crp_sid; + FAR void *opaque = crp->crp_opaque; + int ret; + + memset(&info, 0, sizeof(info)); + rdev = (FAR struct rpmsg_crypto_s *)crypto_driver_get_priv(hid); + if (rdev == NULL) + { + rpmsgerr("Invalid context\n"); + return -EINVAL; + } + + if (sid >= rdev->sesnum || !rdev->sessions[sid].used) + { + rpmsgerr("Invalid sid\n"); + return -EINVAL; + } + + session = &rdev->sessions[sid]; + + if (crp->crp_flags & CRYPTO_F_CANCEL) + { + return rpmsg_crypto_cancel(rdev, crp->crp_reqid, + session->tid, 0); + } + + ret = rpmsg_crypto_fill_data_request(crp, &info); + if (ret < 0) + { + rpmsgerr("Failed to fill data request\n"); + return ret; + } + + if (crp->crp_flags & CRYPTO_F_CBIMM) + { + session->data = opaque; + session->offset = 0; + } + else + { + session->data = NULL; + } + + info.header.tid = session->tid; + session->result = 0; + if (info.data.header.opcode == VIRTIO_CRYPTO_CIPHER_ENCRYPT || + info.data.header.opcode == VIRTIO_CRYPTO_CIPHER_DECRYPT) + { + ret = rpmsg_crypto_process_endec(rdev, &info, session, crp); + } + else if (info.data.header.opcode == VIRTIO_CRYPTO_HASH || + info.data.header.opcode == VIRTIO_CRYPTO_MAC) + { + ret = rpmsg_crypto_process_hash_mac(rdev, &info, session, crp); + } + else if (info.data.header.opcode == VIRTIO_CRYPTO_AEAD_ENCRYPT || + info.data.header.opcode == VIRTIO_CRYPTO_AEAD_DECRYPT) + { + ret = rpmsg_crypto_process_aead(rdev, &info, session, crp); + } + else + { + return -ENOTSUP; + } + + crp->crp_etype = session->result; + return ret == session->result ? 0 : ret; +} + +static int rpmsg_crypto_freesession(uint64_t tid) +{ + FAR struct rpmsg_crypto_freesession_s *msg; + FAR struct rpmsg_crypto_session_s *session; + FAR struct rpmsg_crypto_s *rdev; + uint32_t hid = (uint32_t)(tid >> 32); + uint32_t sid = (uint32_t)tid; + uint32_t space; + int ret; + + rdev = (FAR struct rpmsg_crypto_s *)crypto_driver_get_priv(hid); + if (rdev == NULL) + { + rpmsgerr("Invalid context\n"); + return -EINVAL; + } + + if (sid >= rdev->sesnum) + { + rpmsgerr("Invalid sid\n"); + return -EINVAL; + } + + session = &rdev->sessions[sid]; + msg = rpmsg_crypto_get_tx_payload_buffer(rdev, &space); + if (msg == NULL) + { + return -ENOMEM; + } + + msg->ctrl.header.opcode = VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION; + msg->header.tid = session->tid; + ret = rpmsg_crypto_send_recv(rdev, session, &msg->header, + RPMSG_CRYPTO_FREESESSION, 0, + sizeof(*msg), true); + rpmsg_crypto_put_session(rdev, sid); + return ret; +} + +static int rpmsg_crypto_newsession_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsg_crypto_header_s *header = data; + FAR struct rpmsg_crypto_session_s *session = + (FAR struct rpmsg_crypto_session_s *)(uintptr_t)header->cookie; + + session->result = header->result; + if (session->result >= 0) + { + session->tid = header->tid; + } + + return rpmsg_post(ept, &session->sem); +} + +static int rpmsg_crypto_process_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsg_crypto_header_s *header = data; + FAR struct rpmsg_crypto_session_s *session = + (FAR struct rpmsg_crypto_session_s *)(uintptr_t)header->cookie; + FAR struct rpmsg_crypto_process_s *msg = data; + FAR struct cryptop *crp = session->data; + + session->result = header->result; + if (crp != NULL && (crp->crp_flags & CRYPTO_F_CBIMM)) + { + crp->crp_etype = header->result; + if (session->result == 0) + { + len -= sizeof(*msg); + if (crp->crp_dst && len > 0) + { + memcpy(crp->crp_dst + session->offset, msg->buf, len); + } + else if (crp->crp_mac && len > 0) + { + memcpy(crp->crp_mac, msg->buf, len); + } + + session->offset += len; + } + + if (session->result != 0 || session->offset >= crp->crp_olen) + { + session->data = NULL; + if (crp->crp_callback) + { + crp->crp_callback(crp); + } + } + + return OK; + } + + if (session->result == 0) + { + rpmsg_hold_rx_buffer(ept, data); + session->data = data; + } + else + { + session->data = NULL; + } + + return rpmsg_post(ept, &session->sem); +} + +static int rpmsg_crypto_freesession_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsg_crypto_header_s *header = data; + FAR struct rpmsg_crypto_session_s *session = + (FAR struct rpmsg_crypto_session_s *)(uintptr_t)header->cookie; + + session->result = header->result; + return rpmsg_post(ept, &session->sem); +} + +/**************************************************************************** + * Name: rpmsg_crypto_ept_cb + * + * Description: + * Rpmsg crypto end point callback function, this function will be called + * when receive the remote cpu message. + * + * Parameters: + * ept - The rpmsg-crypto end point + * data - The received data + * len - The received data length + * src - unknown + * priv - unknown + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsg_crypto_ept_cb(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, uint32_t src, + FAR void *priv) +{ + FAR struct rpmsg_crypto_header_s *header = data; + uint32_t command = header->command; + + if (command < nitems(g_rpmsg_crypto_handler)) + { + return g_rpmsg_crypto_handler[command](ept, data, len, src, priv); + } + + rpmsgerr("Failed to find command handler\n"); + return -EINVAL; +} + +/**************************************************************************** + * Name: rpmsg_crypto_ns_bound + * + * Description: + * Rpmsg crypto end point service bound callback function , called when + * remote end point address is received. + * + * Parameters: + * ept - The rpmsg-crypto end point + * + * Returned Values: + * None + * + ****************************************************************************/ + +static void rpmsg_crypto_ns_bound(FAR struct rpmsg_endpoint *ept) +{ + FAR struct rpmsg_crypto_s *priv = ept->priv; + + rpmsg_post(&priv->ept, &priv->wait); +} + +/**************************************************************************** + * Name: rpmsg_crypto_created + * + * Description: + * Rpmsg crypto create function, this function will be called by rptun to + * create a rpmsg-crypto end point. + * + * Parameters: + * rdev - The rpmsg-crypto end point + * priv_ - Rpmsg-crypto handle + * + * Returned Values: + * None + * + ****************************************************************************/ + +static void rpmsg_crypto_created(FAR struct rpmsg_device *rdev, + FAR void *priv_) +{ + FAR struct rpmsg_crypto_s *priv = priv_; + + if (strcmp(priv->remotecpu, rpmsg_get_cpuname(rdev)) == 0) + { + priv->ept.priv = priv; + priv->ept.ns_bound_cb = rpmsg_crypto_ns_bound; + rpmsg_set_priority(&priv->ept, RPMSG_PRIO_RT); + rpmsg_create_ept(&priv->ept, rdev, RPMSG_CRYPTO_NAME_PREFIX, + RPMSG_ADDR_ANY, RPMSG_ADDR_ANY, + rpmsg_crypto_ept_cb, NULL); + } +} + +/**************************************************************************** + * Name: rpmsg_crypto_destroy + * + * Description: + * Rpmsg crypto destroy function, this function will be called by rptun to + * destroy rpmsg-crypto end point. + * + * Parameters: + * rdev - The rpmsg-crypto end point + * priv_ - Rpmsg-device handle + * + * Returned Values: + * None + * + ****************************************************************************/ + +static void rpmsg_crypto_destroy(FAR struct rpmsg_device *rdev, + FAR void *priv_) +{ + FAR struct rpmsg_crypto_s *priv = priv_; + + if (strcmp(priv->remotecpu, rpmsg_get_cpuname(rdev)) == 0) + { + rpmsg_destroy_ept(&priv->ept); + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsg_crypto_register + * + * Description: + * Rpmsg-crypto client initialize function, the client cpu should call + * this function in the board initialize process. + * + * Parameters: + * remotecpu - the server cpu name + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +int rpmsg_crypto_register(FAR const char *remotecpu) +{ + FAR struct rpmsg_crypto_s *rdev; + int algs[CRYPTO_ALGORITHM_MAX + 1]; + int ret; + int i; + + /* Arguments check */ + + if (remotecpu == NULL) + { + return -EINVAL; + } + + /* Initialize the rpmsg crypto */ + + rdev = kmm_zalloc(sizeof(struct rpmsg_crypto_s)); + if (rdev == NULL) + { + rpmsgerr("rpmsg crypto failed to allocate memory\n"); + return -ENOMEM; + } + + /* Pre-allocate session slots to avoid realloc during concurrent use */ + + rdev->sessions = kmm_zalloc(CONFIG_CRYPTO_RPMSG_SESSIONS_NUM * + sizeof(struct rpmsg_crypto_session_s)); + if (rdev->sessions == NULL) + { + kmm_free(rdev); + return -ENOMEM; + } + + rdev->sesnum = CONFIG_CRYPTO_RPMSG_SESSIONS_NUM; + rdev->remotecpu = remotecpu; + nxsem_init(&rdev->wait, 0, 0); + nxmutex_init(&rdev->lock); + + /* Register the rpmsg callback */ + + ret = rpmsg_register_callback(rdev, + rpmsg_crypto_created, + rpmsg_crypto_destroy, + NULL, + NULL); + if (ret < 0) + { + rpmsgerr("rpmsg register callback failed, ret=%d\n", ret); + goto fail_alloc; + } + + /* Register NuttX driver for cipher */ + + rdev->devid = crypto_get_driverid(CRYPTOCAP_F_REMOTE); + + crypto_driver_set_priv(rdev->devid, rdev); + + for (i = 0; i < CRYPTO_ALGORITHM_MAX + 1; i++) + { + algs[i] = CRYPTO_ALG_FLAG_SUPPORTED; + } + + ret = crypto_register(rdev->devid, algs, rpmsg_crypto_newsession, + rpmsg_crypto_freesession, rpmsg_crypto_process); + + if (ret < 0) + { + rpmsgerr("rpmsg crypto cipher register failed, ret=%d\n", ret); + goto fail_register; + } + + return ret; + +fail_register: + rpmsg_unregister_callback(rdev, + rpmsg_crypto_created, + rpmsg_crypto_destroy, + NULL, + NULL); +fail_alloc: + nxmutex_destroy(&rdev->lock); + nxsem_destroy(&rdev->wait); + kmm_free(rdev->sessions); + kmm_free(rdev); + return ret; +}
