e224ee362d
[ Upstream commit 59ca6c93387d325e96577d8bd4c23c78c1491c11 ] Support rsa & pkcs1pad(rsa,sha1) with priority 150. Test with QEMU built-in backend, it works fine. 1, The self-test framework of crypto layer works fine in guest kernel 2, Test with Linux guest(with asym support), the following script test(note that pkey_XXX is supported only in a newer version of keyutils): - both public key & private key - create/close session - encrypt/decrypt/sign/verify basic driver operation - also test with kernel crypto layer(pkey add/query) All the cases work fine. rm -rf *.der *.pem *.pfx modprobe pkcs8_key_parser # if CONFIG_PKCS8_PRIVATE_KEY_PARSER=m rm -rf /tmp/data dd if=/dev/random of=/tmp/data count=1 bs=226 openssl req -nodes -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -subj "/C=CN/ST=BJ/L=HD/O=qemu/OU=dev/CN=qemu/emailAddress=qemu@qemu.org" openssl pkcs8 -in key.pem -topk8 -nocrypt -outform DER -out key.der openssl x509 -in cert.pem -inform PEM -outform DER -out cert.der PRIV_KEY_ID=`cat key.der | keyctl padd asymmetric test_priv_key @s` echo "priv key id = "$PRIV_KEY_ID PUB_KEY_ID=`cat cert.der | keyctl padd asymmetric test_pub_key @s` echo "pub key id = "$PUB_KEY_ID keyctl pkey_query $PRIV_KEY_ID 0 keyctl pkey_query $PUB_KEY_ID 0 echo "Enc with priv key..." keyctl pkey_encrypt $PRIV_KEY_ID 0 /tmp/data enc=pkcs1 >/tmp/enc.priv echo "Dec with pub key..." keyctl pkey_decrypt $PRIV_KEY_ID 0 /tmp/enc.priv enc=pkcs1 >/tmp/dec cmp /tmp/data /tmp/dec echo "Sign with priv key..." keyctl pkey_sign $PRIV_KEY_ID 0 /tmp/data enc=pkcs1 hash=sha1 > /tmp/sig echo "Verify with pub key..." keyctl pkey_verify $PRIV_KEY_ID 0 /tmp/data /tmp/sig enc=pkcs1 hash=sha1 echo "Enc with pub key..." keyctl pkey_encrypt $PUB_KEY_ID 0 /tmp/data enc=pkcs1 >/tmp/enc.pub echo "Dec with priv key..." keyctl pkey_decrypt $PRIV_KEY_ID 0 /tmp/enc.pub enc=pkcs1 >/tmp/dec cmp /tmp/data /tmp/dec echo "Verify with pub key..." keyctl pkey_verify $PUB_KEY_ID 0 /tmp/data /tmp/sig enc=pkcs1 hash=sha1 [1 compiling warning during development] Reported-by: kernel test robot <lkp@intel.com> Co-developed-by: lei he <helei.sig11@bytedance.com> Signed-off-by: lei he <helei.sig11@bytedance.com> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com> Link: https://lore.kernel.org/r/20220302033917.1295334-4-pizhenwei@bytedance.com Reviewed-by: Gonglei <arei.gonglei@huawei.com> Signed-off-by: Nathan Chancellor <nathan@kernel.org> #Kconfig tweaks Link: https://lore.kernel.org/r/20220308205309.2192502-1-nathan@kernel.org Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Stable-dep-of: fed93fb62e05 ("crypto: virtio - Handle dataq logic with tasklet") Signed-off-by: Sasha Levin <sashal@kernel.org>
138 lines
3.6 KiB
C
Executable file
138 lines
3.6 KiB
C
Executable file
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
/* Common header for Virtio crypto device.
|
|
*
|
|
* Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
|
|
*/
|
|
|
|
#ifndef _VIRTIO_CRYPTO_COMMON_H
|
|
#define _VIRTIO_CRYPTO_COMMON_H
|
|
|
|
#include <linux/virtio.h>
|
|
#include <linux/crypto.h>
|
|
#include <linux/spinlock.h>
|
|
#include <crypto/aead.h>
|
|
#include <crypto/aes.h>
|
|
#include <crypto/engine.h>
|
|
|
|
|
|
/* Internal representation of a data virtqueue */
|
|
struct data_queue {
|
|
/* Virtqueue associated with this send _queue */
|
|
struct virtqueue *vq;
|
|
|
|
/* To protect the vq operations for the dataq */
|
|
spinlock_t lock;
|
|
|
|
/* Name of the tx queue: dataq.$index */
|
|
char name[32];
|
|
|
|
struct crypto_engine *engine;
|
|
};
|
|
|
|
struct virtio_crypto {
|
|
struct virtio_device *vdev;
|
|
struct virtqueue *ctrl_vq;
|
|
struct data_queue *data_vq;
|
|
|
|
/* To protect the vq operations for the controlq */
|
|
spinlock_t ctrl_lock;
|
|
|
|
/* Maximum of data queues supported by the device */
|
|
u32 max_data_queues;
|
|
|
|
/* Number of queue currently used by the driver */
|
|
u32 curr_queue;
|
|
|
|
/*
|
|
* Specifies the services mask which the device support,
|
|
* see VIRTIO_CRYPTO_SERVICE_*
|
|
*/
|
|
u32 crypto_services;
|
|
|
|
/* Detailed algorithms mask */
|
|
u32 cipher_algo_l;
|
|
u32 cipher_algo_h;
|
|
u32 hash_algo;
|
|
u32 mac_algo_l;
|
|
u32 mac_algo_h;
|
|
u32 aead_algo;
|
|
u32 akcipher_algo;
|
|
|
|
/* Maximum length of cipher key */
|
|
u32 max_cipher_key_len;
|
|
/* Maximum length of authenticated key */
|
|
u32 max_auth_key_len;
|
|
/* Maximum size of per request */
|
|
u64 max_size;
|
|
|
|
/* Control VQ buffers: protected by the ctrl_lock */
|
|
struct virtio_crypto_op_ctrl_req ctrl;
|
|
struct virtio_crypto_session_input input;
|
|
struct virtio_crypto_inhdr ctrl_status;
|
|
|
|
unsigned long status;
|
|
atomic_t ref_count;
|
|
struct list_head list;
|
|
struct module *owner;
|
|
uint8_t dev_id;
|
|
|
|
/* Does the affinity hint is set for virtqueues? */
|
|
bool affinity_hint_set;
|
|
};
|
|
|
|
struct virtio_crypto_sym_session_info {
|
|
/* Backend session id, which come from the host side */
|
|
__u64 session_id;
|
|
};
|
|
|
|
struct virtio_crypto_request;
|
|
typedef void (*virtio_crypto_data_callback)
|
|
(struct virtio_crypto_request *vc_req, int len);
|
|
|
|
struct virtio_crypto_request {
|
|
uint8_t status;
|
|
struct virtio_crypto_op_data_req *req_data;
|
|
struct scatterlist **sgs;
|
|
struct data_queue *dataq;
|
|
virtio_crypto_data_callback alg_cb;
|
|
};
|
|
|
|
int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev);
|
|
struct list_head *virtcrypto_devmgr_get_head(void);
|
|
void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev);
|
|
struct virtio_crypto *virtcrypto_devmgr_get_first(void);
|
|
int virtcrypto_dev_in_use(struct virtio_crypto *vcrypto_dev);
|
|
int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev);
|
|
void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev);
|
|
int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev);
|
|
bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto_dev,
|
|
uint32_t service,
|
|
uint32_t algo);
|
|
struct virtio_crypto *virtcrypto_get_dev_node(int node,
|
|
uint32_t service,
|
|
uint32_t algo);
|
|
int virtcrypto_dev_start(struct virtio_crypto *vcrypto);
|
|
void virtcrypto_dev_stop(struct virtio_crypto *vcrypto);
|
|
int virtio_crypto_skcipher_crypt_req(
|
|
struct crypto_engine *engine, void *vreq);
|
|
|
|
void
|
|
virtcrypto_clear_request(struct virtio_crypto_request *vc_req);
|
|
|
|
static inline int virtio_crypto_get_current_node(void)
|
|
{
|
|
int cpu, node;
|
|
|
|
cpu = get_cpu();
|
|
node = topology_physical_package_id(cpu);
|
|
put_cpu();
|
|
|
|
return node;
|
|
}
|
|
|
|
int virtio_crypto_algs_register(struct virtio_crypto *vcrypto);
|
|
void virtio_crypto_algs_unregister(struct virtio_crypto *vcrypto);
|
|
int virtio_crypto_akcipher_algs_register(struct virtio_crypto *vcrypto);
|
|
void virtio_crypto_akcipher_algs_unregister(struct virtio_crypto *vcrypto);
|
|
|
|
#endif /* _VIRTIO_CRYPTO_COMMON_H */
|