This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit d679cdb4b308c989f3314689bd7d576af63800fa Author: makejian <[email protected]> AuthorDate: Fri Apr 11 14:11:56 2025 +0800 drivers/virtio: add the virtio-crypto driver Add a frontend for the VirtIO crypto device (device id 20) and register it as a cryptodev driver. It creates sessions on the control queue and runs cipher, hash, MAC and AEAD requests on the data queue. include/crypto/virtio_crypto.h maps the cryptodev algorithm ids to the VirtIO ones, include/nuttx/virtio/virtio-crypto.h holds the VirtIO crypto protocol definitions. Signed-off-by: makejian <[email protected]> Signed-off-by: Bowen Wang <[email protected]> --- drivers/virtio/CMakeLists.txt | 4 + drivers/virtio/Kconfig | 6 + drivers/virtio/Make.defs | 4 + drivers/virtio/virtio-crypto.c | 639 ++++++++++++++++++++++++ drivers/virtio/virtio-crypto.h | 59 +++ drivers/virtio/virtio.c | 9 + include/crypto/virtio_crypto.h | 273 +++++++++++ include/nuttx/virtio/virtio-crypto.h | 910 +++++++++++++++++++++++++++++++++++ 8 files changed, 1904 insertions(+) diff --git a/drivers/virtio/CMakeLists.txt b/drivers/virtio/CMakeLists.txt index 0a3683e82f8..0be47217dc8 100644 --- a/drivers/virtio/CMakeLists.txt +++ b/drivers/virtio/CMakeLists.txt @@ -37,6 +37,10 @@ if(CONFIG_DRIVERS_VIRTIO_BLK) list(APPEND SRCS virtio-blk.c) endif() +if(CONFIG_DRIVERS_VIRTIO_CRYPTO) + list(APPEND SRCS virtio-crypto.c) +endif() + if(CONFIG_DRIVERS_VIRTIO_GPU) list(APPEND SRCS virtio-gpu.c) endif() diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 28357052cad..a3ccfa53562 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -63,6 +63,12 @@ config DRIVERS_VIRTIO_BLK default n select DRIVERS_VIRTIO +config DRIVERS_VIRTIO_CRYPTO + bool "Virtio Crypto support" + depends on CRYPTO_CRYPTODEV + default n + select DRIVERS_VIRTIO + config DRIVERS_VIRTIO_GPU bool "Virtio gpu support" default n diff --git a/drivers/virtio/Make.defs b/drivers/virtio/Make.defs index 19f0026b7a9..526d220f203 100644 --- a/drivers/virtio/Make.defs +++ b/drivers/virtio/Make.defs @@ -38,6 +38,10 @@ ifeq ($(CONFIG_DRIVERS_VIRTIO_BLK),y) CSRCS += virtio-blk.c endif +ifeq ($(CONFIG_DRIVERS_VIRTIO_CRYPTO),y) + CSRCS += virtio-crypto.c +endif + ifeq ($(CONFIG_DRIVERS_VIRTIO_GPU),y) CSRCS += virtio-gpu.c endif diff --git a/drivers/virtio/virtio-crypto.c b/drivers/virtio/virtio-crypto.c new file mode 100644 index 00000000000..5bd0365afcf --- /dev/null +++ b/drivers/virtio/virtio-crypto.c @@ -0,0 +1,639 @@ +/**************************************************************************** + * drivers/virtio/virtio-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 <crypto/virtio_crypto.h> +#include <nuttx/spinlock.h> +#include <nuttx/virtio/virtio.h> +#include <nuttx/virtio/virtio-crypto.h> + +#include "virtio-crypto.h" + +/**************************************************************************** + * Private Type Definitions + ****************************************************************************/ + +struct virtio_crypto_s +{ + FAR struct virtio_device *vdev; + + /* To protect the vq operations for the controlq and dataq */ + + spinlock_t lock; + + /* Maximum number of data queues supported by the device */ + + uint32_t max_data_queues; + + /* Number of queue currently used by the driver */ + + uint32_t curr_queue; + + /* Specifies the services mask which the device supports, + * see VIRTIO_CRYPTO_SERVICE_* + */ + + uint32_t crypto_services; + + /* Detailed algorithms mask */ + + uint32_t cipher_algo_l; + uint32_t cipher_algo_h; + uint32_t hash_algo; + uint32_t mac_algo_l; + uint32_t mac_algo_h; + uint32_t aead_algo; + uint32_t akcipher_algo; + + /* Maximum length of cipher key */ + + uint32_t max_cipher_key_len; + + /* Maximum length of authenticated key */ + + uint32_t max_auth_key_len; + + /* Maximum size of per request */ + + uint64_t max_size; + + uint8_t dev_id; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* Crypto driver operation function */ + +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_freesession(uint64_t tid); + +static int virtio_crypto_fill_crtlq_request(FAR struct cryptoini *cri, + FAR struct virtio_crypto_op_ctrl_req_s *ctrl); +static int virtio_crypto_fill_dataq_request(FAR struct cryptop *crp, + FAR struct virtio_crypto_op_data_req_s *req_data); + +static void virtio_crypto_notify(FAR struct virtqueue *vq); +static int virtio_crypto_create_vqs(FAR struct virtio_crypto_s *vcrypto); + +/* Virtio driver functions */ + +static int virtio_crypto_probe(FAR struct virtio_device *vdev); +static void virtio_crypto_remove(FAR struct virtio_device *vdev); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct virtio_driver g_virtio_crypto_driver = +{ + VIRTIO_ID_CRYPTO, /* device id */ + virtio_crypto_probe, /* probe */ + virtio_crypto_remove, /* remove */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int virtio_crypto_fill_crtlq_request( + FAR struct cryptoini *cri, + FAR struct virtio_crypto_op_ctrl_req_s *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(struct virtio_crypto_op_ctrl_req_s)); + + /* Pad ctrl header for creating a new session */ + + ctrl->header.algo = algo; + + 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 = + virtio_crypto_get_auth_key_len(algo); + break; + case VIRTIO_CRYPTO_SERVICE_AEAD: + ctrl->header.opcode = VIRTIO_CRYPTO_MAC_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_hash_result_len(algo); + 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 virtio_crypto_newsession(FAR uint32_t *sid, + FAR struct cryptoini *cri) +{ + struct virtio_crypto_create_session_input_s input; + struct virtio_crypto_op_ctrl_req_s ctrl; + FAR struct virtio_crypto_s *vcrypto; + FAR struct virtqueue *vq; + struct virtqueue_buf vb[3]; + unsigned int num_out = 0; + unsigned int num_in = 0; + sem_t sem; + int ret; + + vcrypto = (struct virtio_crypto_s *)crypto_driver_get_priv(*sid); + if (vcrypto == NULL) + { + vrterr("Virtio Crypto newsession: Invalid context\n"); + return -EINVAL; + } + + vq = vcrypto->vdev->vrings_info[vcrypto->max_data_queues].vq; + + /* Change Nuttx Crypto para into Virtio para */ + + ret = virtio_crypto_fill_crtlq_request(cri, &ctrl); + if (ret < 0) + { + vrterr("Virtio Crypto: Invalid parameters in newsession\n"); + return ret; + } + + nxsem_init(&sem, 0, 0); + + input.status = VIRTIO_CRYPTO_ERR; + + vb[num_out].buf = &ctrl; + vb[num_out++].len = sizeof(ctrl); + + if (cri->cri_klen > 0) + { + vb[num_out].buf = cri->cri_key; + vb[num_out++].len = cri->cri_klen / 8; + } + + vb[num_out + num_in].buf = &input; + vb[num_out + num_in++].len = sizeof(input); + + virtqueue_add_buffer_lock(vq, vb, num_out, num_in, &sem, + &vcrypto->lock); + virtqueue_kick_lock(vq, &vcrypto->lock); + + /* Wait for completion */ + + nxsem_wait_uninterruptible(&sem); + + nxsem_destroy(&sem); + if (input.status != VIRTIO_CRYPTO_OK) + { + vrterr("virtio_crypto_s: Create session failed status: %u\n", + input.status); + return virtio_crypto_get_errcode(input.status); + } + + *sid = input.session_id; + return ret; +} + +static int virtio_crypto_freesession(uint64_t tid) +{ + struct virtio_crypto_destroy_session_input_s input; + struct virtio_crypto_op_ctrl_req_s ctrl; + FAR struct virtio_crypto_s *vcrypto; + FAR struct virtqueue *vq; + struct virtqueue_buf vb[2]; + uint32_t hid = (uint32_t)(tid >> 32); + sem_t sem; + int ret = 0; + + vcrypto = crypto_driver_get_priv(hid); + if (vcrypto == NULL) + { + vrterr("Virtio Crypto freesession: Invalid context\n"); + return -EINVAL; + } + + vq = vcrypto->vdev->vrings_info[vcrypto->max_data_queues].vq; + + memset(&ctrl, 0, sizeof(ctrl)); + nxsem_init(&sem, 0, 0); + + /* Pad ctrl header */ + + ctrl.header.opcode = VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION; + ctrl.op_flf.destroy.session_id = tid & 0xffffffff; + + input.status = VIRTIO_CRYPTO_ERR; + + vb[0].buf = &ctrl; + vb[0].len = sizeof(ctrl); + vb[1].buf = &input; + vb[1].len = sizeof(input); + + virtqueue_add_buffer_lock(vq, vb, 1, 1, &sem, &vcrypto->lock); + virtqueue_kick_lock(vq, &vcrypto->lock); + + /* Wait for completion */ + + nxsem_wait_uninterruptible(&sem); + + nxsem_destroy(&sem); + if (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); + + ret = virtio_crypto_get_errcode(input.status); + } + + return ret; +} + +static int virtio_crypto_fill_dataq_request( + FAR struct cryptop *crp, + FAR struct virtio_crypto_op_data_req_s *req_data) +{ + FAR struct virtio_crypto_op_header_s *header = &req_data->header; + FAR struct cryptodesc *crda = crp->crp_desc; + 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(req_data, 0, sizeof(struct virtio_crypto_op_data_req_s)); + + header->algo = algo; + header->session_id = crp->crp_sid & 0xffffffff; + + switch (service) + { + case VIRTIO_CRYPTO_SERVICE_CIPHER: + header->opcode = crda->crd_flags & CRD_F_ENCRYPT ? + VIRTIO_CRYPTO_CIPHER_ENCRYPT : VIRTIO_CRYPTO_CIPHER_DECRYPT; + req_data->op_flf.sym.op_type = VIRTIO_CRYPTO_SYM_OP_CIPHER; + req_data->op_flf.sym.op_type_flf.cipher.iv_len = + virtio_crypto_get_iv_len(service, algo); + req_data->op_flf.sym.op_type_flf.cipher.src_data_len = crp->crp_ilen; + req_data->op_flf.sym.op_type_flf.cipher.dst_data_len = crp->crp_olen; + break; + case VIRTIO_CRYPTO_SERVICE_HASH: + header->opcode = VIRTIO_CRYPTO_HASH; + req_data->op_flf.hash.src_data_len = crp->crp_ilen; + req_data->op_flf.hash.hash_result_len = + virtio_crypto_get_hash_result_len(algo); + break; + case VIRTIO_CRYPTO_SERVICE_MAC: + header->opcode = VIRTIO_CRYPTO_MAC; + req_data->op_flf.mac.hdr.src_data_len = crp->crp_ilen; + req_data->op_flf.mac.hdr.hash_result_len = + virtio_crypto_get_hash_result_len(algo); + break; + case VIRTIO_CRYPTO_SERVICE_AEAD: + header->opcode = crda->crd_flags & CRD_F_ENCRYPT ? + VIRTIO_CRYPTO_AEAD_ENCRYPT : VIRTIO_CRYPTO_AEAD_DECRYPT; + req_data->op_flf.aead.iv_len = + virtio_crypto_get_iv_len(service, algo); + req_data->op_flf.aead.src_data_len = crp->crp_ilen; + req_data->op_flf.aead.dst_data_len = crp->crp_olen; + req_data->op_flf.aead.aad_len = crp->crp_aadlen; + req_data->op_flf.aead.tag_len = virtio_crypto_get_tag_len(algo); + break; + default: + return -ENOTSUP; + } + + return OK; +} + +static int virtio_crypto_process(FAR struct cryptop *crp) +{ + struct virtio_crypto_op_data_req_s req_data; + struct virtio_crypto_inhdr_s inhdr; + FAR struct virtio_crypto_s *vcrypto; + FAR struct virtqueue *vq; + struct virtqueue_buf vb[6]; + uint32_t hid = (uint32_t)(crp->crp_sid >> 32); + sem_t sem; + int num_out = 0; + int num_in = 0; + int ret; + + vcrypto = crypto_driver_get_priv(hid); + if (vcrypto == NULL) + { + vrterr("Virtio Crypto process: Invalid context\n"); + return -EINVAL; + } + + vq = vcrypto->vdev->vrings_info[0].vq; + + ret = virtio_crypto_fill_dataq_request(crp, &req_data); + if (ret < 0) + { + vrterr("Virtio Crypto: Invalid parameters in process\n"); + return ret; + } + + nxsem_init(&sem, 0, 0); + inhdr.status = VIRTIO_CRYPTO_ERR; + + vb[num_out].buf = &req_data; + vb[num_out++].len = sizeof(req_data); + + if (crp->crp_iv != NULL) + { + vb[num_out].buf = crp->crp_iv; + vb[num_out++].len = + virtio_crypto_get_iv_len( + virtio_crypto_get_service(crp->crp_desc->crd_alg), + req_data.header.algo); + } + + vb[num_out].buf = crp->crp_buf; + vb[num_out++].len = crp->crp_ilen; + + if (crp->crp_aad != NULL) + { + vb[num_out].buf = crp->crp_aad; + vb[num_out++].len = crp->crp_aadlen; + } + + vb[num_out + num_in].buf = crp->crp_dst; + vb[num_out + num_in++].len = crp->crp_olen; + vb[num_out + num_in].buf = &inhdr; + vb[num_out + num_in++].len = sizeof(inhdr); + + virtqueue_add_buffer_lock(vq, vb, num_out, num_in, &sem, + &vcrypto->lock); + virtqueue_kick_lock(vq, &vcrypto->lock); + + /* Wait for completion */ + + nxsem_wait_uninterruptible(&sem); + + nxsem_destroy(&sem); + if (inhdr.status != VIRTIO_CRYPTO_OK) + { + ret = virtio_crypto_get_errcode(inhdr.status); + } + + return ret; +} + +static void virtio_crypto_notify(FAR struct virtqueue *vq) +{ + FAR struct virtio_crypto_s *vcrypto = vq->vq_dev->priv; + FAR sem_t *sem; + + for (; ; ) + { + sem = virtqueue_get_buffer_lock(vq, 0, NULL, &vcrypto->lock); + if (sem == NULL) + { + break; + } + + nxsem_post(sem); + } +} + +static int virtio_crypto_create_vqs(struct virtio_crypto_s *vcrypto) +{ + FAR struct virtio_device *vdev = vcrypto->vdev; + FAR const char *vqnames[vcrypto->max_data_queues + 1]; + vq_callback callback[vcrypto->max_data_queues + 1]; + uint32_t i; + int ret; + + /* We expect 1 data virtqueue, followed by + * possible N-1 data queues used in multiqueue mode, + * followed by control vq. + */ + + /* Allocate/initialize parameters for data virtqueues */ + + for (i = 0; i < vcrypto->max_data_queues; i++) + { + vqnames[i] = "virtio_crypto_dataq"; + callback[i] = virtio_crypto_notify; + } + + /* Parameters for control virtqueue */ + + vqnames[vcrypto->max_data_queues] = "virtio_crypto_ctrlq"; + callback[vcrypto->max_data_queues] = virtio_crypto_notify; + + ret = virtio_create_virtqueues( + vdev, 0, vcrypto->max_data_queues + 1, vqnames, callback, NULL); + if (ret < 0) + { + vrterr("virtio_device_create_virtqueue failed, ret=%d\n", ret); + } + + return ret; +} + +static int virtio_crypto_probe(FAR struct virtio_device *vdev) +{ + FAR struct virtio_crypto_s *vcrypto; + int algs[CRYPTO_ALGORITHM_MAX + 1]; + int ret; + int i; + + vcrypto = kmm_zalloc(sizeof(struct virtio_crypto_s)); + if (vcrypto == NULL) + { + vrterr("Virtio Crypto: Probe failed to allocate memory\n"); + return -ENOMEM; + } + + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + max_dataqueues, &vcrypto->max_data_queues); + if (vcrypto->max_data_queues < 1) + { + vcrypto->max_data_queues = 1; + } + + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + max_cipher_key_len, &vcrypto->max_cipher_key_len); + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + max_auth_key_len, &vcrypto->max_auth_key_len); + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + max_size, &vcrypto->max_size); + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + crypto_services, &vcrypto->crypto_services); + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + cipher_algo_l, &vcrypto->cipher_algo_l); + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + cipher_algo_h, &vcrypto->cipher_algo_h); + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + hash_algo, &vcrypto->hash_algo); + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + mac_algo_l, &vcrypto->mac_algo_l); + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + mac_algo_h, &vcrypto->mac_algo_h); + if (vcrypto->crypto_services & (1 << VIRTIO_CRYPTO_SERVICE_AKCIPHER)) + { + virtio_read_config_member(vdev, struct virtio_crypto_config_s, + akcipher_algo, &vcrypto->akcipher_algo); + } + + vcrypto->vdev = vdev; + vdev->priv = vcrypto; + + /* Use single data queue as default */ + + vcrypto->curr_queue = 1; + + vrtinfo("max_queues: %x, max_cipher_key_len: %u,\ + max_auth_key_len: %u, max_size 0x%lx\n", + vcrypto->max_data_queues, vcrypto->max_cipher_key_len, + vcrypto->max_auth_key_len, vcrypto->max_size); + + /* Call openamp api to initialize the virtio device */ + + virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER); + virtio_negotiate_features(vdev, + (1UL << VIRTIO_CRYPTO_F_REVISION_1) | + (1UL << VIRTIO_CRYPTO_F_CIPHER_STATELESS_MODE) | + (1UL << VIRTIO_CRYPTO_F_HASH_STATELESS_MODE) | + (1UL << VIRTIO_CRYPTO_F_MAC_STATELESS_MODE) | + (1UL << VIRTIO_CRYPTO_F_AEAD_STATELESS_MODE) | + (1UL << VIRTIO_CRYPTO_F_AKCIPHER_STATELESS_MODE), + NULL); + virtio_set_status(vdev, VIRTIO_CONFIG_FEATURES_OK); + + ret = virtio_crypto_create_vqs(vcrypto); + if (ret < 0) + { + vrterr("virtio_device_create_virtqueue failed, ret=%d\n", ret); + goto err; + } + + virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK); + + spin_lock_init(&vcrypto->lock); + + /* Register NuttX driver */ + + vcrypto->dev_id = crypto_get_driverid(0); + + crypto_driver_set_priv(vcrypto->dev_id, vcrypto); + + /* All algorithms are supported by default, handled by the host. */ + + for (i = 0; i < CRYPTO_ALGORITHM_MAX; i++) + { + algs[i] = CRYPTO_ALG_FLAG_SUPPORTED; + } + + ret = crypto_register(vcrypto->dev_id, algs, virtio_crypto_newsession, + virtio_crypto_freesession, virtio_crypto_process); + if (ret < 0) + { + goto err; + } + + return ret; + +err: + virtio_delete_virtqueues(vdev); + virtio_reset_device(vdev); + kmm_free(vcrypto); + return ret; +} + +static void virtio_crypto_remove(FAR struct virtio_device *vdev) +{ + FAR struct virtio_crypto_s *vcrypto = vdev->priv; + int i; + + for (i = 0; i < CRYPTO_ALGORITHM_MAX; i++) + { + crypto_unregister(vcrypto->dev_id, i); + } + + virtio_delete_virtqueues(vdev); + virtio_reset_device(vdev); + kmm_free(vcrypto); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: virtio_register_crypto_driver + ****************************************************************************/ + +int virtio_register_crypto_driver(void) +{ + return virtio_register_driver(&g_virtio_crypto_driver); +} diff --git a/drivers/virtio/virtio-crypto.h b/drivers/virtio/virtio-crypto.h new file mode 100644 index 00000000000..ff0be0edd78 --- /dev/null +++ b/drivers/virtio/virtio-crypto.h @@ -0,0 +1,59 @@ +/**************************************************************************** + * drivers/virtio/virtio-crypto.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __DRIVERS_VIRTIO_VIRTIO_CRYPTO_H +#define __DRIVERS_VIRTIO_VIRTIO_CRYPTO_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#ifdef CONFIG_DRIVERS_VIRTIO_CRYPTO + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +int virtio_register_crypto_driver(void); + +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_DRIVERS_VIRTIO_CRYPTO */ + +#endif /* __DRIVERS_VIRTIO_VIRTIO_CRYPTO_H */ diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index cb49f9eb6d5..def7abdf3ec 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -38,6 +38,7 @@ #include <nuttx/virtio/virtio.h> #include "virtio-blk.h" +#include "virtio-crypto.h" #include "virtio-gpio.h" #include "virtio-gpu.h" #include "virtio-i2c.h" @@ -374,6 +375,14 @@ void virtio_register_drivers(void) } #endif +#ifdef CONFIG_DRIVERS_VIRTIO_CRYPTO + ret = virtio_register_crypto_driver(); + if (ret < 0) + { + vrterr("virtio_register_crypto_driver failed, ret=%d\n", ret); + } +#endif + #ifdef CONFIG_DRIVERS_VIRTIO_SPI ret = virtio_register_spi_driver(); if (ret < 0) diff --git a/include/crypto/virtio_crypto.h b/include/crypto/virtio_crypto.h new file mode 100644 index 00000000000..ab5cff6c079 --- /dev/null +++ b/include/crypto/virtio_crypto.h @@ -0,0 +1,273 @@ +/**************************************************************************** + * include/crypto/virtio_crypto.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_CRYPTO_VIRTIO_CRYPTO_H +#define __INCLUDE_CRYPTO_VIRTIO_CRYPTO_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <crypto/cryptodev.h> +#include <nuttx/virtio/virtio-crypto.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define VIRTIO_CRYPTO_MD5_HASH_LEN 16 +#define VIRTIO_CRYPTO_SHA1_HASH_LEN 20 +#define VIRTIO_CRYPTO_SHA224_HASH_LEN 28 +#define VIRTIO_CRYPTO_SHA256_HASH_LEN 32 +#define VIRTIO_CRYPTO_SHA384_HASH_LEN 48 +#define VIRTIO_CRYPTO_SHA512_HASH_LEN 64 +#define VIRTIO_CRYPTO_AESCMAC_KEY_LEN 16 +#define VIRTIO_CRYPTO_AES_IV_LEN 16 +#define VIRTIO_CRYPTO_CCM_IV_LEN 12 +#define VIRTIO_CRYPTO_CHACHAPOLY_IV_LEN 12 +#define VIRTIO_CRYPTO_DES_IV_LEN 8 +#define VIRTIO_CRYPTO_GCM_IV_LEN 12 +#define VIRTIO_CRYPTO_CCM_TAG_LEN 16 +#define VIRTIO_CRYPTO_GCM_TAG_LEN 16 +#define VIRTIO_CRYPTO_CHACHAPOLY_TAG_LEN 16 +#define VIRTIO_CRYPTO_NOUNCE_LEN 4 + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +static inline_function +int virtio_crypto_get_errcode(int verr) +{ + switch (verr) + { + case VIRTIO_CRYPTO_OK: + return OK; + case VIRTIO_CRYPTO_ERR: + return -ENOSYS; + case VIRTIO_CRYPTO_BADMSG: + return -EBADMSG; + case VIRTIO_CRYPTO_NOTSUPP: + return -ENOTSUP; + case VIRTIO_CRYPTO_INVSESS: + return -EBADF; + case VIRTIO_CRYPTO_NOSPC: + return -ENOSPC; + case VIRTIO_CRYPTO_KEY_REJECTED: + return -EKEYREJECTED; + } + + return -EINVAL; +} + +static inline_function +int virtio_crypto_get_tag_len(int valg) +{ + switch (valg) + { + case VIRTIO_CRYPTO_AEAD_GCM: + return VIRTIO_CRYPTO_GCM_TAG_LEN; + case VIRTIO_CRYPTO_AEAD_CCM: + return VIRTIO_CRYPTO_CCM_TAG_LEN; + case VIRTIO_CRYPTO_AEAD_CHACHA20_POLY1305: + return VIRTIO_CRYPTO_CHACHAPOLY_TAG_LEN; + } + + return -EINVAL; +} + +static inline_function +int virtio_crypto_get_iv_len(int vservice, int valg) +{ + if (vservice == VIRTIO_CRYPTO_SERVICE_CIPHER) + { + switch (valg) + { + case VIRTIO_CRYPTO_CIPHER_3DES_CBC: + return VIRTIO_CRYPTO_DES_IV_LEN; + case VIRTIO_CRYPTO_CIPHER_AES_CBC: + case VIRTIO_CRYPTO_CIPHER_AES_CTR: + case VIRTIO_CRYPTO_CIPHER_AES_XTS: + return VIRTIO_CRYPTO_AES_IV_LEN; + } + } + else if (vservice = VIRTIO_CRYPTO_SERVICE_AEAD) + { + switch (valg) + { + case VIRTIO_CRYPTO_AEAD_GCM: + return VIRTIO_CRYPTO_GCM_IV_LEN; + case VIRTIO_CRYPTO_AEAD_CCM: + return VIRTIO_CRYPTO_CCM_IV_LEN; + case VIRTIO_CRYPTO_AEAD_CHACHA20_POLY1305: + return VIRTIO_CRYPTO_CHACHAPOLY_IV_LEN; + } + } + + return -EINVAL; +} + +static inline_function +int virtio_crypto_get_auth_key_len(int valg) +{ + switch (valg) + { + case VIRTIO_CRYPTO_HASH_MD5: + return VIRTIO_CRYPTO_MD5_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA1: + return VIRTIO_CRYPTO_SHA1_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA_224: + return VIRTIO_CRYPTO_SHA224_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA_256: + return VIRTIO_CRYPTO_SHA256_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA_384: + return VIRTIO_CRYPTO_SHA384_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA_512: + return VIRTIO_CRYPTO_SHA512_HASH_LEN; + case VIRTIO_CRYPTO_MAC_CMAC_AES: + return VIRTIO_CRYPTO_AESCMAC_KEY_LEN; + } + + return -EINVAL; +} + +static inline_function +int virtio_crypto_get_hash_result_len(int valg) +{ + switch (valg) + { + case VIRTIO_CRYPTO_HASH_MD5: + return VIRTIO_CRYPTO_MD5_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA1: + return VIRTIO_CRYPTO_SHA1_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA_224: + return VIRTIO_CRYPTO_SHA224_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA_256: + return VIRTIO_CRYPTO_SHA256_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA_384: + return VIRTIO_CRYPTO_SHA384_HASH_LEN; + case VIRTIO_CRYPTO_HASH_SHA_512: + return VIRTIO_CRYPTO_SHA512_HASH_LEN; + } + + return -EINVAL; +} + +static inline_function +int virtio_crypto_get_service(int alg) +{ + switch (alg) + { + case CRYPTO_3DES_CBC: + case CRYPTO_BLF_CBC: + case CRYPTO_CAST_CBC: + case CRYPTO_AES_CBC: + case CRYPTO_AES_192_CBC: + case CRYPTO_AES_256_CBC: + case CRYPTO_AES_CTR: + case CRYPTO_AES_XTS: + case CRYPTO_AES_OFB: + case CRYPTO_AES_CFB_8: + case CRYPTO_AES_CFB_128: + return VIRTIO_CRYPTO_SERVICE_CIPHER; + case CRYPTO_MD5: + case CRYPTO_POLY1305: + case CRYPTO_RIPEMD160: + case CRYPTO_SHA1: + case CRYPTO_SHA2_224: + case CRYPTO_SHA2_256: + case CRYPTO_SHA2_384: + case CRYPTO_SHA2_512: + case CRYPTO_CRC32: + return VIRTIO_CRYPTO_SERVICE_HASH; + case CRYPTO_MD5_HMAC: + case CRYPTO_SHA1_HMAC: + case CRYPTO_RIPEMD160_HMAC: + case CRYPTO_SHA2_256_HMAC: + case CRYPTO_SHA2_384_HMAC: + case CRYPTO_SHA2_512_HMAC: + case CRYPTO_AES_128_GMAC: + case CRYPTO_AES_128_CMAC: + return VIRTIO_CRYPTO_SERVICE_MAC; + case CRYPTO_AES_GCM_16: + case CRYPTO_CHACHA20_POLY1305: + case CRYPTO_CHACHA20_POLY1305_MAC: + return VIRTIO_CRYPTO_SERVICE_AEAD; + } + + return -EINVAL; +} + +static inline_function +int virtio_crypto_get_alg(int alg) +{ + switch (alg) + { + case CRYPTO_3DES_CBC: + return VIRTIO_CRYPTO_CIPHER_3DES_CBC; + case CRYPTO_AES_CBC: + case CRYPTO_AES_192_CBC: + case CRYPTO_AES_256_CBC: + return VIRTIO_CRYPTO_CIPHER_AES_CBC; + case CRYPTO_AES_CTR: + return VIRTIO_CRYPTO_CIPHER_AES_CTR; + case CRYPTO_AES_XTS: + return VIRTIO_CRYPTO_CIPHER_AES_XTS; + case CRYPTO_MD5_HMAC: + return VIRTIO_CRYPTO_MAC_HMAC_MD5; + case CRYPTO_SHA1_HMAC: + return VIRTIO_CRYPTO_MAC_HMAC_SHA1; + case CRYPTO_SHA2_256_HMAC: + return VIRTIO_CRYPTO_MAC_HMAC_SHA_256; + case CRYPTO_SHA2_384_HMAC: + return VIRTIO_CRYPTO_MAC_HMAC_SHA_384; + case CRYPTO_SHA2_512_HMAC: + return VIRTIO_CRYPTO_MAC_HMAC_SHA_512; + case CRYPTO_MD5: + return VIRTIO_CRYPTO_HASH_MD5; + case CRYPTO_SHA1: + return VIRTIO_CRYPTO_HASH_SHA1; + case CRYPTO_SHA2_224: + return VIRTIO_CRYPTO_HASH_SHA_224; + case CRYPTO_SHA2_256: + return VIRTIO_CRYPTO_HASH_SHA_256; + case CRYPTO_SHA2_384: + return VIRTIO_CRYPTO_HASH_SHA_384; + case CRYPTO_SHA2_512: + return VIRTIO_CRYPTO_HASH_SHA_512; + case CRYPTO_AES_128_GMAC: + return VIRTIO_CRYPTO_MAC_GMAC_AES; + case CRYPTO_AES_CMAC: + case CRYPTO_AES_128_CMAC: + return VIRTIO_CRYPTO_MAC_CMAC_AES; + case CRYPTO_AES_GCM_16: + return VIRTIO_CRYPTO_AEAD_GCM; + case CRYPTO_CHACHA20_POLY1305: + case CRYPTO_CHACHA20_POLY1305_MAC: + return VIRTIO_CRYPTO_AEAD_CHACHA20_POLY1305; + } + + return -EINVAL; +} + +#endif /* __INCLUDE_CRYPTO_VIRTIO_CRYPTO_H */ diff --git a/include/nuttx/virtio/virtio-crypto.h b/include/nuttx/virtio/virtio-crypto.h new file mode 100644 index 00000000000..77f80eeb086 --- /dev/null +++ b/include/nuttx/virtio/virtio-crypto.h @@ -0,0 +1,910 @@ +/**************************************************************************** + * include/nuttx/virtio/virtio-crypto.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_VIRTIO_VIRTIO_CRYPTO_H +#define __INCLUDE_NUTTX_VIRTIO_VIRTIO_CRYPTO_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdint.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Crypto feature bits */ + +#define VIRTIO_CRYPTO_F_REVISION_1 0 +#define VIRTIO_CRYPTO_F_CIPHER_STATELESS_MODE 1 +#define VIRTIO_CRYPTO_F_HASH_STATELESS_MODE 2 +#define VIRTIO_CRYPTO_F_MAC_STATELESS_MODE 3 +#define VIRTIO_CRYPTO_F_AEAD_STATELESS_MODE 4 +#define VIRTIO_CRYPTO_F_AKCIPHER_STATELESS_MODE 5 + +/* CIPHER (Symmetric Key Cipher) service */ + +#define VIRTIO_CRYPTO_SERVICE_CIPHER 0 + +/* HASH service */ + +#define VIRTIO_CRYPTO_SERVICE_HASH 1 + +/* MAC (Message Authentication Codes) service */ + +#define VIRTIO_CRYPTO_SERVICE_MAC 2 + +/* AEAD (Authenticated Encryption with Associated Data) service */ + +#define VIRTIO_CRYPTO_SERVICE_AEAD 3 + +/* AKCIPHER (Asymmetric Key Cipher) service */ + +#define VIRTIO_CRYPTO_SERVICE_AKCIPHER 4 + +/* CIPHER services */ + +#define VIRTIO_CRYPTO_NO_CIPHER 0 +#define VIRTIO_CRYPTO_CIPHER_ARC4 1 +#define VIRTIO_CRYPTO_CIPHER_AES_ECB 2 +#define VIRTIO_CRYPTO_CIPHER_AES_CBC 3 +#define VIRTIO_CRYPTO_CIPHER_AES_CTR 4 +#define VIRTIO_CRYPTO_CIPHER_DES_ECB 5 +#define VIRTIO_CRYPTO_CIPHER_DES_CBC 6 +#define VIRTIO_CRYPTO_CIPHER_3DES_ECB 7 +#define VIRTIO_CRYPTO_CIPHER_3DES_CBC 8 +#define VIRTIO_CRYPTO_CIPHER_3DES_CTR 9 +#define VIRTIO_CRYPTO_CIPHER_KASUMI_F8 10 +#define VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2 11 +#define VIRTIO_CRYPTO_CIPHER_AES_F8 12 +#define VIRTIO_CRYPTO_CIPHER_AES_XTS 13 +#define VIRTIO_CRYPTO_CIPHER_ZUC_EEA3 14 + +/* HASH services */ + +#define VIRTIO_CRYPTO_NO_HASH 0 +#define VIRTIO_CRYPTO_HASH_MD5 1 +#define VIRTIO_CRYPTO_HASH_SHA1 2 +#define VIRTIO_CRYPTO_HASH_SHA_224 3 +#define VIRTIO_CRYPTO_HASH_SHA_256 4 +#define VIRTIO_CRYPTO_HASH_SHA_384 5 +#define VIRTIO_CRYPTO_HASH_SHA_512 6 +#define VIRTIO_CRYPTO_HASH_SHA3_224 7 +#define VIRTIO_CRYPTO_HASH_SHA3_256 8 +#define VIRTIO_CRYPTO_HASH_SHA3_384 9 +#define VIRTIO_CRYPTO_HASH_SHA3_512 10 +#define VIRTIO_CRYPTO_HASH_SHA3_SHAKE128 11 +#define VIRTIO_CRYPTO_HASH_SHA3_SHAKE256 12 + +/* MAC services */ + +#define VIRTIO_CRYPTO_NO_MAC 0 +#define VIRTIO_CRYPTO_MAC_HMAC_MD5 1 +#define VIRTIO_CRYPTO_MAC_HMAC_SHA1 2 +#define VIRTIO_CRYPTO_MAC_HMAC_SHA_224 3 +#define VIRTIO_CRYPTO_MAC_HMAC_SHA_256 4 +#define VIRTIO_CRYPTO_MAC_HMAC_SHA_384 5 +#define VIRTIO_CRYPTO_MAC_HMAC_SHA_512 6 +#define VIRTIO_CRYPTO_MAC_CMAC_3DES 25 +#define VIRTIO_CRYPTO_MAC_CMAC_AES 26 +#define VIRTIO_CRYPTO_MAC_KASUMI_F9 27 +#define VIRTIO_CRYPTO_MAC_SNOW3G_UIA2 28 +#define VIRTIO_CRYPTO_MAC_GMAC_AES 41 +#define VIRTIO_CRYPTO_MAC_GMAC_TWOFISH 42 +#define VIRTIO_CRYPTO_MAC_CBCMAC_AES 49 +#define VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9 50 +#define VIRTIO_CRYPTO_MAC_XCBC_AES 53 +#define VIRTIO_CRYPTO_MAC_ZUC_EIA3 54 + +/* AEAD services */ + +#define VIRTIO_CRYPTO_NO_AEAD 0 +#define VIRTIO_CRYPTO_AEAD_GCM 1 +#define VIRTIO_CRYPTO_AEAD_CCM 2 +#define VIRTIO_CRYPTO_AEAD_CHACHA20_POLY1305 3 + +/* AKCIPHER services */ + +#define VIRTIO_CRYPTO_NO_AKCIPHER 0 +#define VIRTIO_CRYPTO_AKCIPHER_RSA 1 +#define VIRTIO_CRYPTO_AKCIPHER_ECDSA 2 + +#define VIRTIO_CRYPTO_S_HW_READY (1 << 0) + +#define VIRTIO_CRYPTO_OPCODE(service, op) (((service) << 8) | (op)) + +#define VIRTIO_CRYPTO_CIPHER_CREATE_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x02) +#define VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x03) +#define VIRTIO_CRYPTO_HASH_CREATE_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_HASH, 0x02) +#define VIRTIO_CRYPTO_HASH_DESTROY_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_HASH, 0x03) +#define VIRTIO_CRYPTO_MAC_CREATE_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_MAC, 0x02) +#define VIRTIO_CRYPTO_MAC_DESTROY_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_MAC, 0x03) +#define VIRTIO_CRYPTO_AEAD_CREATE_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x02) +#define VIRTIO_CRYPTO_AEAD_DESTROY_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x03) +#define VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x04) +#define VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x05) + +#define VIRTIO_CRYPTO_CTRLQ_OP_SPEC_HDR_LEGACY 56 + +#define VIRTIO_CRYPTO_OP_ENCRYPT 1 +#define VIRTIO_CRYPTO_OP_DECRYPT 2 + +#define VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER 1 +#define VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH 2 + +/* Plain hash */ + +#define VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN 1 + +/* Authenticated hash (mac) */ + +#define VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH 2 + +/* Nested hash */ + +#define VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED 3 + +#define VIRTIO_CRYPTO_ALG_CHAIN_SESS_OP_SPEC_HDR_SIZE 16 + +#define VIRTIO_CRYPTO_SYM_SESS_OP_SPEC_HDR_SIZE 48 + +/* No operation */ + +#define VIRTIO_CRYPTO_SYM_OP_NONE 0 + + /* Cipher only operation on the data */ + +#define VIRTIO_CRYPTO_SYM_OP_CIPHER 1 + +/* Chain any cipher with any hash or mac operation. The order + * depends on the value of alg_chain_order param + */ + +#define VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING 2 + +#define VIRTIO_CRYPTO_RSA_RAW_PADDING 0 +#define VIRTIO_CRYPTO_RSA_PKCS1_PADDING 1 + +#define VIRTIO_CRYPTO_RSA_NO_HASH 0 +#define VIRTIO_CRYPTO_RSA_MD2 1 +#define VIRTIO_CRYPTO_RSA_MD3 2 +#define VIRTIO_CRYPTO_RSA_MD4 3 +#define VIRTIO_CRYPTO_RSA_MD5 4 +#define VIRTIO_CRYPTO_RSA_SHA1 5 +#define VIRTIO_CRYPTO_RSA_SHA256 6 +#define VIRTIO_CRYPTO_RSA_SHA384 7 +#define VIRTIO_CRYPTO_RSA_SHA512 8 +#define VIRTIO_CRYPTO_RSA_SHA224 9 + +#define VIRTIO_CRYPTO_CURVE_UNKNOWN 0 +#define VIRTIO_CRYPTO_CURVE_NIST_P192 1 +#define VIRTIO_CRYPTO_CURVE_NIST_P224 2 +#define VIRTIO_CRYPTO_CURVE_NIST_P256 3 +#define VIRTIO_CRYPTO_CURVE_NIST_P384 4 +#define VIRTIO_CRYPTO_CURVE_NIST_P521 5 + +#define VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC 1 +#define VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE 2 + +#define VIRTIO_CRYPTO_AKCIPHER_SESS_ALGO_SPEC_HDR_SIZE 44 + +#define VIRTIO_CRYPTO_CIPHER_ENCRYPT \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x00) +#define VIRTIO_CRYPTO_CIPHER_DECRYPT \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_CIPHER, 0x01) +#define VIRTIO_CRYPTO_HASH \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_HASH, 0x00) +#define VIRTIO_CRYPTO_MAC \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_MAC, 0x00) +#define VIRTIO_CRYPTO_AEAD_ENCRYPT \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x00) +#define VIRTIO_CRYPTO_AEAD_DECRYPT \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AEAD, 0x01) +#define VIRTIO_CRYPTO_AKCIPHER_ENCRYPT \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x00) +#define VIRTIO_CRYPTO_AKCIPHER_DECRYPT \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x01) +#define VIRTIO_CRYPTO_AKCIPHER_SIGN \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x02) +#define VIRTIO_CRYPTO_AKCIPHER_VERIFY \ + VIRTIO_CRYPTO_OPCODE(VIRTIO_CRYPTO_SERVICE_AKCIPHER, 0x03) + +#define VIRTIO_CRYPTO_FLAG_SESSION_MODE 1 + +#define VIRTIO_CRYPTO_DATAQ_OP_SPEC_HDR_LEGACY 48 + +#define VIRTIO_CRYPTO_SYM_DATA_REQ_HDR_SIZE 40 + +#define VIRTIO_CRYPTO_SYM_DATE_REQ_HDR_STATELESS_SIZE 72 + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +begin_packed_struct struct virtio_crypto_config_s +{ + uint32_t status; + uint32_t max_dataqueues; + uint32_t crypto_services; + + /* Detailed algorithms mask */ + + uint32_t cipher_algo_l; + uint32_t cipher_algo_h; + uint32_t hash_algo; + uint32_t mac_algo_l; + uint32_t mac_algo_h; + uint32_t aead_algo; + + /* Maximum length of cipher key in bytes */ + + uint32_t max_cipher_key_len; + + /* Maximum length of authenticated key in bytes */ + + uint32_t max_auth_key_len; + uint32_t akcipher_algo; + + /* Maximum size of each crypto request’s content in bytes */ + + uint64_t max_size; +} end_packed_struct; + +enum VIRTIO_CRYPTO_STATUS +{ + VIRTIO_CRYPTO_OK = 0, + VIRTIO_CRYPTO_ERR = 1, + VIRTIO_CRYPTO_BADMSG = 2, + VIRTIO_CRYPTO_NOTSUPP = 3, + VIRTIO_CRYPTO_INVSESS = 4, + VIRTIO_CRYPTO_NOSPC = 5, + VIRTIO_CRYPTO_KEY_REJECTED = 6, + VIRTIO_CRYPTO_MAX +}; + +begin_packed_struct struct virtio_crypto_ctrl_header_s +{ + uint32_t opcode; + + /* algo should be service-specific algorithms */ + + uint32_t algo; + uint32_t flag; + uint32_t reserved; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_hash_create_session_flf_s +{ + /* Device read only portion */ + + /* See VIRTIO_CRYPTO_HASH_* above */ + + uint32_t algo; + + /* hash result length */ + + uint32_t hash_result_len; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_mac_create_session_flf_s +{ + /* Device read only portion */ + + /* See VIRTIO_CRYPTO_MAC_* above */ + + uint32_t algo; + + /* hash result length */ + + uint32_t hash_result_len; + + /* length of authenticated key */ + + uint32_t auth_key_len; + uint32_t padding; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_cipher_session_flf_s +{ + /* Device read only portion */ + + /* See VIRTIO_CRYPTO_CIPHER* above */ + + uint32_t algo; + + /* length of key */ + + uint32_t key_len; + + /* encryption or decryption */ + + uint32_t op; + uint32_t padding; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_alg_chain_session_flf_s +{ + /* Device read only portion */ + + uint32_t alg_chain_order; + uint32_t hash_mode; + struct virtio_crypto_cipher_session_flf_s cipher_hdr; + + /* fixed length fields, algo specific */ + + union + { + struct virtio_crypto_hash_create_session_flf_s hash; + struct virtio_crypto_mac_create_session_flf_s mac; + uint8_t padding[VIRTIO_CRYPTO_ALG_CHAIN_SESS_OP_SPEC_HDR_SIZE]; + } algo_flf; + + /* length of the additional authenticated data (AAD) in bytes */ + + uint32_t aad_len; + uint32_t padding; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_sym_create_session_flf_s +{ + /* Device read only portion */ + + union + { + struct virtio_crypto_cipher_session_flf_s cipher; + struct virtio_crypto_alg_chain_session_flf_s chain; + uint8_t padding[VIRTIO_CRYPTO_SYM_SESS_OP_SPEC_HDR_SIZE]; + } op_flf; + + uint32_t op_type; + uint32_t padding; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_aead_create_session_flf_s +{ + /* Device read only portion */ + + /* See VIRTIO_CRYPTO_AEAD_* above */ + + uint32_t algo; + + /* length of key */ + + uint32_t key_len; + + /* Authentication tag length */ + + uint32_t tag_len; + + /* length of the additional authenticated data (AAD) in bytes */ + + uint32_t aad_len; + + /* encrypt or decrypt, See above VIRTIO_CRYPTO_OP_* */ + + uint32_t op; + uint32_t padding; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_rsa_session_para_s +{ + uint32_t padding_algo; + uint32_t hash_algo; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_ecdsa_session_para_s +{ + /* See VIRTIO_CRYPTO_CURVE_* above */ + + uint32_t curve_id; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_akcipher_create_session_flf_s +{ + /* Device read only portion */ + + /* See VIRTIO_CRYPTO_AKCIPHER_* above */ + + uint32_t algo; + uint32_t key_type; + + /* length of key */ + + uint32_t key_len; + + union + { + struct virtio_crypto_rsa_session_para_s rsa; + struct virtio_crypto_ecdsa_session_para_s ecdsa; + uint8_t padding[VIRTIO_CRYPTO_AKCIPHER_SESS_ALGO_SPEC_HDR_SIZE]; + } algo_flf; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_create_session_input_s +{ + uint64_t session_id; + uint32_t status; + uint32_t padding; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_destroy_session_flf_s +{ + /* Device read only portion */ + + uint64_t session_id; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_destroy_session_input_s +{ + /* Device write only portion */ + + uint8_t status; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_op_ctrl_req_s +{ + /* Device read only portion */ + + struct virtio_crypto_ctrl_header_s header; + + /* fixed length fields, opcode specific */ + + union + { + struct virtio_crypto_sym_create_session_flf_s sym; + struct virtio_crypto_hash_create_session_flf_s hash; + struct virtio_crypto_mac_create_session_flf_s mac; + struct virtio_crypto_aead_create_session_flf_s aead; + struct virtio_crypto_akcipher_create_session_flf_s akcipher; + struct virtio_crypto_destroy_session_flf_s destroy; + uint8_t padding[VIRTIO_CRYPTO_CTRLQ_OP_SPEC_HDR_LEGACY]; + } op_flf; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_op_header_s +{ + uint32_t opcode; + + /* algo should be service-specific algorithms */ + + uint32_t algo; + + /* session_id should be service-specific algorithms */ + + uint64_t session_id; + + /* control flag to control the request */ + + uint32_t flag; + uint32_t padding; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_inhdr_s +{ + uint8_t status; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_hash_data_flf_s +{ + /* length of source data */ + + uint32_t src_data_len; + + /* hash result length */ + + uint32_t hash_result_len; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_hash_data_flf_stateless_s +{ + struct + { + /* See VIRTIO_CRYPTO_HASH_* above */ + + uint32_t algo; + } sess_para; + + /* length of source data */ + + uint32_t src_data_len; + + /* hash result length */ + + uint32_t hash_result_len; + uint32_t reserved; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_mac_data_flf_s +{ + struct virtio_crypto_hash_data_flf_s hdr; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_mac_data_flf_stateless_s +{ + struct + { + /* See VIRTIO_CRYPTO_MAC_* above */ + + uint32_t algo; + + /* length of authenticated key */ + + uint32_t auth_key_len; + } sess_para; + + /* length of source data */ + + uint32_t src_data_len; + + /* hash result length */ + + uint32_t hash_result_len; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_cipher_data_flf_s +{ + /* Byte Length of valid IV/Counter data pointed to by the below iv data. + * + * For block ciphers in CBC or F8 mode, or for Kasumi in F8 mode, or for + * SNOW3G in UEA2 mode, this is the length of the IV (which + * must be the same as the block length of the cipher). + * For block ciphers in CTR mode, this is the length of the counter + * (which must be the same as the block length of the cipher). + */ + + uint32_t iv_len; + + /* length of source data */ + + uint32_t src_data_len; + + /* length of dst data */ + + uint32_t dst_data_len; + uint32_t padding; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_alg_chain_data_flf_s +{ + uint32_t iv_len; + + /* Length of source data */ + + uint32_t src_data_len; + + /* Length of destination data */ + + uint32_t dst_data_len; + + /* Starting point for cipher processing in source data */ + + uint32_t cipher_start_src_offset; + + /* Length of the source data that the cipher will be computed on */ + + uint32_t len_to_cipher; + + /* Starting point for hash processing in source data */ + + uint32_t hash_start_src_offset; + + /* Length of the source data that the hash will be computed on */ + + uint32_t len_to_hash; + + /* Length of the additional auth data */ + + uint32_t aad_len; + + /* Length of the hash result */ + + uint32_t hash_result_len; + uint32_t reserved; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_sym_data_flf_s +{ + /* Device read only portion */ + + union + { + struct virtio_crypto_cipher_data_flf_s cipher; + struct virtio_crypto_alg_chain_data_flf_s chain; + uint8_t padding[VIRTIO_CRYPTO_SYM_DATA_REQ_HDR_SIZE]; + } op_type_flf; + + /* See above VIRTIO_CRYPTO_SYM_OP_* */ + + uint32_t op_type; + uint32_t padding; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_cipher_data_flf_stateless_s +{ + struct + { + /* See VIRTIO_CRYPTO_CIPHER* above */ + + uint32_t algo; + + /* length of key */ + + uint32_t key_len; + + /* See VIRTIO_CRYPTO_OP_* above */ + + uint32_t op; + } sess_para; + + /* Byte Length of valid IV/Counter data pointed to by the below iv data. + */ + + uint32_t iv_len; + + /* length of source data */ + + uint32_t src_data_len; + + /* length of destination data */ + + uint32_t dst_data_len; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_alg_chain_data_flf_stateless_s +{ + struct + { + /* See VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_* above */ + + uint32_t alg_chain_order; + + /* length of the additional authenticated data in bytes */ + + uint32_t aad_len; + + struct + { + /* See VIRTIO_CRYPTO_CIPHER* above */ + + uint32_t algo; + + /* length of key */ + + uint32_t key_len; + + /* See VIRTIO_CRYPTO_OP_* above */ + + uint32_t op; + } cipher; + + struct + { + /* See VIRTIO_CRYPTO_HASH_* or VIRTIO_CRYPTO_MAC_* above */ + + uint32_t algo; + + /* length of authenticated key */ + + uint32_t auth_key_len; + + /* See VIRTIO_CRYPTO_SYM_HASH_MODE_* above */ + + uint32_t hash_mode; + } hash; + } sess_para; + + uint32_t iv_len; + + /* Length of source data */ + + uint32_t src_data_len; + + /* Length of destination data */ + + uint32_t dst_data_len; + + /* Starting point for cipher processing in source data */ + + uint32_t cipher_start_src_offset; + + /* Length of the source data that the cipher will be computed on */ + + uint32_t len_to_cipher; + + /* Starting point for hash processing in source data */ + + uint32_t hash_start_src_offset; + + /* Length of the source data that the hash will be computed on */ + + uint32_t len_to_hash; + + /* Length of the additional auth data */ + + uint32_t aad_len; + + /* Length of the hash result */ + + uint32_t hash_result_len; + uint32_t reserved; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_sym_data_flf_stateless_s +{ + /* Device read only portion */ + + union + { + struct virtio_crypto_cipher_data_flf_stateless_s cipher; + struct virtio_crypto_alg_chain_data_flf_stateless_s chain; + uint8_t padding[VIRTIO_CRYPTO_SYM_DATE_REQ_HDR_STATELESS_SIZE]; + } op_type_flf; + + /* Device write only portion */ + + /* See above VIRTIO_CRYPTO_SYM_OP_* */ + + uint32_t op_type; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_aead_data_flf_s +{ + /* Byte Length of valid IV data. + * + * For GCM mode, this is either 12 (for 96-bit IVs) or 16, in which + * case iv points to J0. + * For CCM mode, this is the length of the nonce, which can be in the + * range 7 to 13 inclusive. + */ + + uint32_t iv_len; + + /* length of additional auth data */ + + uint32_t aad_len; + + /* length of source data */ + + uint32_t src_data_len; + + /* length of dst data */ + + uint32_t dst_data_len; + + /* Authentication tag length */ + + uint32_t tag_len; + uint32_t reserved; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_aead_data_flf_stateless_s +{ + struct + { + /* See VIRTIO_CRYPTO_AEAD_* above */ + + uint32_t algo; + + /* length of key */ + + uint32_t key_len; + + /* encrypt or decrypt, See above VIRTIO_CRYPTO_OP_* */ + + uint32_t op; + } sess_para; + + /* Byte Length of valid IV data. */ + + uint32_t iv_len; + + /* Authentication tag length */ + + uint32_t tag_len; + + /* length of additional auth data */ + + uint32_t aad_len; + + /* length of source data */ + + uint32_t src_data_len; + + /* length of dst data, this should be at least src_data_len + tag_len */ + + uint32_t dst_data_len; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_akcipher_data_flf_s +{ + /* length of source data */ + + uint32_t src_data_len; + + /* length of dst data */ + + uint32_t dst_data_len; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_akcipher_data_flf_stateless_s +{ + struct + { + /* See VIRTIO_CYRPTO_AKCIPHER* above */ + + uint32_t algo; + + /* See VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_* above */ + + uint32_t key_type; + + /* length of key */ + + uint32_t key_len; + + /* algothrim specific parameters described above */ + + union + { + struct virtio_crypto_rsa_session_para_s rsa; + struct virtio_crypto_ecdsa_session_para_s ecdsa; + } u; + } sess_para; + + /* length of source data */ + + uint32_t src_data_len; + + /* length of destination data */ + + uint32_t dst_data_len; +} end_packed_struct; + +begin_packed_struct struct virtio_crypto_op_data_req_s +{ + /* Device read only portion */ + + struct virtio_crypto_op_header_s header; + + union + { + struct virtio_crypto_sym_data_flf_s sym; + struct virtio_crypto_hash_data_flf_s hash; + struct virtio_crypto_mac_data_flf_s mac; + struct virtio_crypto_aead_data_flf_s aead; + struct virtio_crypto_akcipher_data_flf_s akcipher; + uint8_t padding[VIRTIO_CRYPTO_DATAQ_OP_SPEC_HDR_LEGACY]; + } op_flf; +} end_packed_struct; + +#endif /* __INCLUDE_NUTTX_VIRTIO_VIRTIO_CRYPTO_H */
