Detect & register virtio-crypto algos only if it can be supported by backend
Subsystems (4)
cover Detect & register virtio-crypto algos only if it can be supported by backend
Hi,
Currently the Linux virtio-crypto driver registers the crypto algorithm without verifying if the backend actually supports the algorithm.
This kernel patch series adds support for registering algorithm with Linux crypto layer, only if the algorithm is supported by the backend device. This also makes the driver more compliant with the virtio-crypto spec [1].
I would appreciate any feedback or comments on this.
Thank you
Farhan
Reference
---------
[1] Virtio crypto spec proposal https://lists.gnu.org/archive/html/qemu-devel/2017-12/msg00816.html
Farhan Ali (2):
crypto/virtio-crypto: Read crypto services and algorithm masks crypto/virtio-crypto: Register an algo only if it's supported
drivers/crypto/virtio/virtio_crypto_algs.c | 110 ++++++++++++++++++--------- drivers/crypto/virtio/virtio_crypto_common.h | 25 +++++- drivers/crypto/virtio/virtio_crypto_core.c | 29 +++++++ drivers/crypto/virtio/virtio_crypto_mgr.c | 81 ++++++++++++++++++-- 4 files changed, 201 insertions(+), 44 deletions(-)
Signature · 1 line
2.7.4
RFC 1/2 crypto/virtio-crypto: Read crypto services and algorithm masks
Read the crypto services and algorithm masks which provides information about the services and algorithms supported by virtio-crypto backend.
--- drivers/crypto/virtio/virtio_crypto_common.h | 14 ++++++++++++++ drivers/crypto/virtio/virtio_crypto_core.c | 29 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)
drivers/crypto/virtio/virtio_crypto_common.h +14 -0
@@ -55,6 +55,20 @@ struct virtio_crypto {/* Number of queue currently used by the driver */ u32 curr_queue; +/* +* Specifies the services mask which the device support, +* see VIRTIO_CRYPTO_SERVICE_* above +*/ +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; +/* Maximum length of cipher key */ u32 max_cipher_key_len;/* Maximum length of authenticated key */
drivers/crypto/virtio/virtio_crypto_core.c +29 -0
@@ -303,6 +303,13 @@ static int virtcrypto_probe(struct virtio_device *vdev)u32 max_data_queues =0 , max_cipher_key_len =0 ;u32 max_auth_key_len =0 ;u64 max_size =0 ; +u32 cipher_algo_l =0 ; +u32 cipher_algo_h =0 ; +u32 hash_algo =0 ; +u32 mac_algo_l =0 ; +u32 mac_algo_h =0 ; +u32 aead_algo =0 ; +u32 crypto_services =0 ;if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 ))return -ENODEV ;
@@ -339,6 +346,20 @@ static int virtcrypto_probe(struct virtio_device *vdev) max_auth_key_len, &max_auth_key_len); virtio_cread(vdev, struct virtio_crypto_config, max_size, &max_size); + virtio_cread(vdev, struct virtio_crypto_config, + crypto_services, &crypto_services); + virtio_cread(vdev, struct virtio_crypto_config, + cipher_algo_l, &cipher_algo_l); + virtio_cread(vdev, struct virtio_crypto_config, + cipher_algo_h, &cipher_algo_h); + virtio_cread(vdev, struct virtio_crypto_config, + hash_algo, &hash_algo); + virtio_cread(vdev, struct virtio_crypto_config, + mac_algo_l, &mac_algo_l); + virtio_cread(vdev, struct virtio_crypto_config, + mac_algo_h, &mac_algo_h); + virtio_cread(vdev, struct virtio_crypto_config, + aead_algo, &aead_algo);/* Add virtio crypto device to global table */ err =virtcrypto_devmgr_add_dev (vcrypto);
@@ -358,6 +379,14 @@ static int virtcrypto_probe(struct virtio_device *vdev) vcrypto->max_cipher_key_len = max_cipher_key_len; vcrypto->max_auth_key_len = max_auth_key_len; vcrypto->max_size = max_size; + vcrypto->crypto_services = crypto_services; + vcrypto->cipher_algo_l = cipher_algo_l; + vcrypto->cipher_algo_h = cipher_algo_h; + vcrypto->mac_algo_l = mac_algo_l; + vcrypto->mac_algo_h = mac_algo_h; + vcrypto->hash_algo = hash_algo; + vcrypto->aead_algo = aead_algo; + dev_info(&vdev->dev, "max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u, max_size 0x%llx\n",
Signature · 1 line
2.7.4
RE: [RFC v1 1/2] crypto/virtio-crypto: Read crypto services and algorithm masks
Quoted 29 lines
-----Original Message-----
From: Farhan Ali [mailto:[email protected]]
Sent: Saturday, June 09, 2018 3:09 AM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; Gonglei (Arei) <[email protected]>; longpeng <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [RFC v1 1/2]crypto/virtio-crypto: Read crypto services and algorithm masksRead the crypto services and algorithm masks which provides information about the services and algorithms supported by virtio-crypto backend.
Signed-off-by Farhan Ali <[email protected]>--- drivers/crypto/virtio/virtio_crypto_common.h | 14 ++++++++++++++ drivers/crypto/virtio/virtio_crypto_core.c | 29 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)drivers/crypto/virtio/virtio_crypto_common.h +0
-0
drivers/crypto/virtio/virtio_crypto_common.h struct virtio_crypto { · line 61
Quoted 7 lines
drivers/crypto/virtio/virtio_crypto_common.h +4
-0@@ -55,6 +55,20 @@ struct virtio_crypto {/* Number of queue currently used by the driver */ u32 curr_queue; +/* +* Specifies the services mask which the device support, +* see VIRTIO_CRYPTO_SERVICE_* above +*/
Pls update the above comments. Except that:
Quoted 71 lines
drivers/crypto/virtio/virtio_crypto_common.h +10
-0+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; +/* Maximum length of cipher key */ u32 max_cipher_key_len;/* Maximum length of authenticated key */ drivers/crypto/virtio/virtio_crypto_core.c +29
-0@@ -303,6 +303,13 @@ static int virtcrypto_probe(struct virtio_device *vdev)u32 max_data_queues =0 , max_cipher_key_len =0 ;u32 max_auth_key_len =0 ;u64 max_size =0 ; +u32 cipher_algo_l =0 ; +u32 cipher_algo_h =0 ; +u32 hash_algo =0 ; +u32 mac_algo_l =0 ; +u32 mac_algo_h =0 ; +u32 aead_algo =0 ; +u32 crypto_services =0 ;if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 ))return -ENODEV ;@@ -339,6 +346,20 @@ static int virtcrypto_probe(struct virtio_device *vdev) max_auth_key_len, &max_auth_key_len); virtio_cread(vdev, struct virtio_crypto_config, max_size, &max_size); + virtio_cread(vdev, struct virtio_crypto_config, + crypto_services, &crypto_services); + virtio_cread(vdev, struct virtio_crypto_config, + cipher_algo_l, &cipher_algo_l); + virtio_cread(vdev, struct virtio_crypto_config, + cipher_algo_h, &cipher_algo_h); + virtio_cread(vdev, struct virtio_crypto_config, + hash_algo, &hash_algo); + virtio_cread(vdev, struct virtio_crypto_config, + mac_algo_l, &mac_algo_l); + virtio_cread(vdev, struct virtio_crypto_config, + mac_algo_h, &mac_algo_h); + virtio_cread(vdev, struct virtio_crypto_config, + aead_algo, &aead_algo);/* Add virtio crypto device to global table */ err =virtcrypto_devmgr_add_dev (vcrypto);@@ -358,6 +379,14 @@ static int virtcrypto_probe(struct virtio_device *vdev) vcrypto->max_cipher_key_len = max_cipher_key_len; vcrypto->max_auth_key_len = max_auth_key_len; vcrypto->max_size = max_size; + vcrypto->crypto_services = crypto_services; + vcrypto->cipher_algo_l = cipher_algo_l; + vcrypto->cipher_algo_h = cipher_algo_h; + vcrypto->mac_algo_l = mac_algo_l; + vcrypto->mac_algo_h = mac_algo_h; + vcrypto->hash_algo = hash_algo; + vcrypto->aead_algo = aead_algo; + dev_info(&vdev->dev, "max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u,
max_size0x%llx\n",
--
2.7.4
Re: [RFC v1 1/2] crypto/virtio-crypto: Read crypto services and algorithm masks
Hi Arei
Quoted 32 lines · Gonglei (Arei)
Quoted 32 lines
-----Original Message-----
From: Farhan Ali [mailto:[email protected]]
Sent: Saturday, June 09, 2018 3:09 AM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; Gonglei (Arei) <[email protected]>; longpeng <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [RFC v1 1/2]crypto/virtio-crypto: Read crypto services and algorithm masksRead the crypto services and algorithm masks which provides information about the services and algorithms supported by virtio-crypto backend.
Signed-off-by Farhan Ali <[email protected]>--- drivers/crypto/virtio/virtio_crypto_common.h | 14 ++++++++++++++ drivers/crypto/virtio/virtio_crypto_core.c | 29 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)drivers/crypto/virtio/virtio_crypto_common.h +0
-0@@ -55,6 +55,20 @@ struct virtio_crypto {/* Number of queue currently used by the driver */ u32 curr_queue;
Quoted 9 lines
drivers/crypto/virtio/virtio_crypto_common.h struct virtio_crypto { · line 61
Quoted 4 lines
drivers/crypto/virtio/virtio_crypto_common.h +4
-0+/* +* Specifies the services mask which the device support, +* see VIRTIO_CRYPTO_SERVICE_* above +*/ Pls update the above comments. Except that:
Acked-by Gonglei <[email protected]>
Sure will update the comment. How about " Specifies the services mask which the device support, * see VIRTIO_CRYPTO_SERVICE_*" ?
or should I specify the file where the VIRTIO_CRYPTO_SERVICE_* are defined?
Thanks
Farhan
Quoted 73 lines
Quoted 71 lines
drivers/crypto/virtio/virtio_crypto_common.h +10
-0+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; +/* Maximum length of cipher key */ u32 max_cipher_key_len;/* Maximum length of authenticated key */ drivers/crypto/virtio/virtio_crypto_core.c +29
-0@@ -303,6 +303,13 @@ static int virtcrypto_probe(struct virtio_device *vdev)u32 max_data_queues =0 , max_cipher_key_len =0 ;u32 max_auth_key_len =0 ;u64 max_size =0 ; +u32 cipher_algo_l =0 ; +u32 cipher_algo_h =0 ; +u32 hash_algo =0 ; +u32 mac_algo_l =0 ; +u32 mac_algo_h =0 ; +u32 aead_algo =0 ; +u32 crypto_services =0 ;if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 ))return -ENODEV ;@@ -339,6 +346,20 @@ static int virtcrypto_probe(struct virtio_device *vdev) max_auth_key_len, &max_auth_key_len); virtio_cread(vdev, struct virtio_crypto_config, max_size, &max_size); + virtio_cread(vdev, struct virtio_crypto_config, + crypto_services, &crypto_services); + virtio_cread(vdev, struct virtio_crypto_config, + cipher_algo_l, &cipher_algo_l); + virtio_cread(vdev, struct virtio_crypto_config, + cipher_algo_h, &cipher_algo_h); + virtio_cread(vdev, struct virtio_crypto_config, + hash_algo, &hash_algo); + virtio_cread(vdev, struct virtio_crypto_config, + mac_algo_l, &mac_algo_l); + virtio_cread(vdev, struct virtio_crypto_config, + mac_algo_h, &mac_algo_h); + virtio_cread(vdev, struct virtio_crypto_config, + aead_algo, &aead_algo);/* Add virtio crypto device to global table */ err =virtcrypto_devmgr_add_dev (vcrypto);@@ -358,6 +379,14 @@ static int virtcrypto_probe(struct virtio_device *vdev) vcrypto->max_cipher_key_len = max_cipher_key_len; vcrypto->max_auth_key_len = max_auth_key_len; vcrypto->max_size = max_size; + vcrypto->crypto_services = crypto_services; + vcrypto->cipher_algo_l = cipher_algo_l; + vcrypto->cipher_algo_h = cipher_algo_h; + vcrypto->mac_algo_l = mac_algo_l; + vcrypto->mac_algo_h = mac_algo_h; + vcrypto->hash_algo = hash_algo; + vcrypto->aead_algo = aead_algo; + dev_info(&vdev->dev, "max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u,
max_size0x%llx\n",
--
2.7.4
Gonglei (Arei)
maintainerRE: [RFC v1 1/2] crypto/virtio-crypto: Read crypto services and algorithm masks
Quoted 55 lines
-----Original Message-----
From: Farhan Ali [mailto:[email protected]]
Sent: Wednesday, June 13, 2018 1:07 AM
To: Gonglei (Arei) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; longpeng <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC v1 1/2]crypto/virtio-crypto: Read crypto services and algorithm masksHi Arei
Quoted 10 lines · Gonglei (Arei)
Quoted 10 lines
-----Original Message-----
From: Farhan Ali [mailto:[email protected]]
Sent: Saturday, June 09, 2018 3:09 AM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; Gonglei (Arei) <[email protected]>; longpeng <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [RFC v1 1/2]crypto/virtio-crypto: Read crypto services andalgorithm
Quoted 29 lines
Quoted 19 lines
masks
Read the crypto services and algorithm masks which provides information about the services and algorithms supported by virtio-crypto backend.
Signed-off-by Farhan Ali <[email protected]>--- drivers/crypto/virtio/virtio_crypto_common.h | 14 ++++++++++++++ drivers/crypto/virtio/virtio_crypto_core.c | 29 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)drivers/crypto/virtio/virtio_crypto_common.h +0
-0drivers/crypto/virtio/virtio_crypto_common.h struct virtio_crypto { · line 61
Quoted 7 lines
drivers/crypto/virtio/virtio_crypto_common.h +4
-0@@ -55,6 +55,20 @@ struct virtio_crypto {/* Number of queue currently used by the driver */ u32 curr_queue; +/* +* Specifies the services mask which the device support, +* see VIRTIO_CRYPTO_SERVICE_* above +*/ Pls update the above comments. Except that:
Quoted 6 lines
Quoted 2 lines
Acked-by Gonglei <[email protected]>Sure will update the comment. How about " Specifies the services mask which the device support, * see VIRTIO_CRYPTO_SERVICE_*" ?
It makes sense IMHO :)
Regards,
-Gonglei
Quoted 82 lines
or should I specify the file where the VIRTIO_CRYPTO_SERVICE_* are defined?
Thanks
FarhanQuoted 11 lines
Quoted 11 lines
drivers/crypto/virtio/virtio_crypto_common.h +10
-0+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; +/* Maximum length of cipher key */ Quoted 8 lines
drivers/crypto/virtio/virtio_crypto_core.c static int virtcrypto_probe(struct virtio_device
Quoted 8 lines
drivers/crypto/virtio/virtio_crypto_common.h +0
-0u32 max_cipher_key_len;/* Maximum length of authenticated key */ drivers/crypto/virtio/virtio_crypto_core.c +0
-0@@ -303,6 +303,13 @@ static int virtcrypto_probe(struct virtio_device*vdev)
Quoted 11 lines
Quoted 11 lines
drivers/crypto/virtio/virtio_crypto_core.c +7
-0u32 max_data_queues =0 , max_cipher_key_len =0 ;u32 max_auth_key_len =0 ;u64 max_size =0 ; +u32 cipher_algo_l =0 ; +u32 cipher_algo_h =0 ; +u32 hash_algo =0 ; +u32 mac_algo_l =0 ; +u32 mac_algo_h =0 ; +u32 aead_algo =0 ; +u32 crypto_services =0 ;Quoted 3 lines
drivers/crypto/virtio/virtio_crypto_core.c static int virtcrypto_probe(struct virtio_device
Quoted 3 lines
drivers/crypto/virtio/virtio_crypto_core.c +0
-0if (!virtio_has_feature (vdev,VIRTIO_F_VERSION_1 ))return -ENODEV ;@@ -339,6 +346,20 @@ static int virtcrypto_probe(struct virtio_device*vdev)
Quoted 18 lines
Quoted 18 lines
drivers/crypto/virtio/virtio_crypto_core.c +14
-0max_auth_key_len, &max_auth_key_len); virtio_cread(vdev, struct virtio_crypto_config, max_size, &max_size); + virtio_cread(vdev, struct virtio_crypto_config, + crypto_services, &crypto_services); + virtio_cread(vdev, struct virtio_crypto_config, + cipher_algo_l, &cipher_algo_l); + virtio_cread(vdev, struct virtio_crypto_config, + cipher_algo_h, &cipher_algo_h); + virtio_cread(vdev, struct virtio_crypto_config, + hash_algo, &hash_algo); + virtio_cread(vdev, struct virtio_crypto_config, + mac_algo_l, &mac_algo_l); + virtio_cread(vdev, struct virtio_crypto_config, + mac_algo_h, &mac_algo_h); + virtio_cread(vdev, struct virtio_crypto_config, + aead_algo, &aead_algo);Quoted 3 lines
drivers/crypto/virtio/virtio_crypto_core.c static int virtcrypto_probe(struct virtio_device
Quoted 3 lines
drivers/crypto/virtio/virtio_crypto_core.c +0
-0/* Add virtio crypto device to global table */ err =virtcrypto_devmgr_add_dev (vcrypto);@@ -358,6 +379,14 @@ static int virtcrypto_probe(struct virtio_device*vdev)
Quoted 12 lines
Quoted 12 lines
drivers/crypto/virtio/virtio_crypto_core.c +8
-0vcrypto->max_cipher_key_len = max_cipher_key_len; vcrypto->max_auth_key_len = max_auth_key_len; vcrypto->max_size = max_size; + vcrypto->crypto_services = crypto_services; + vcrypto->cipher_algo_l = cipher_algo_l; + vcrypto->cipher_algo_h = cipher_algo_h; + vcrypto->mac_algo_l = mac_algo_l; + vcrypto->mac_algo_h = mac_algo_h; + vcrypto->hash_algo = hash_algo; + vcrypto->aead_algo = aead_algo; +Quoted 2 lines
drivers/crypto/virtio/virtio_crypto_core.c static int virtcrypto_probe(struct virtio_device · line 392
Quoted 2 lines
drivers/crypto/virtio/virtio_crypto_core.c +0
-0dev_info(&vdev->dev, "max_queues: %u, max_cipher_key_len: %u,
max_auth_key_len: %u,Quoted 5 lines
Quoted 3 lines
max_size0x%llx\n",
--
2.7.4
RFC 2/2 crypto/virtio-crypto: Register an algo only if it's supported
In reply to Farhan AliFrom: Farhan Ali <[email protected]>
Register a crypto algo with the Linux crypto layer only if the algorithm is supported by the backend virtio-crypto device.
Also route crypto requests to a virtio-crypto device, only if it can support the requested service and algorithm.
--- drivers/crypto/virtio/virtio_crypto_algs.c | 110 ++++++++++++++++++--------- drivers/crypto/virtio/virtio_crypto_common.h | 11 ++- drivers/crypto/virtio/virtio_crypto_mgr.c | 81 ++++++++++++++++++-- 3 files changed, 158 insertions(+), 44 deletions(-)
drivers/crypto/virtio/virtio_crypto_algs.c +75 -35
@@ -49,12 +49,18 @@ struct virtio_crypto_sym_request {bool encrypt; }; +struct virtio_crypto_algo { +uint32_t algonum; +uint32_t service; +unsigned int active_devs; +struct crypto_alg algo; +}; +/* * The algs_lock protects the below global virtio_crypto_active_devs * and crypto algorithms registion. */ static DEFINE_MUTEX (algs_lock); -static unsigned int virtio_crypto_active_devs; static void virtio_crypto_ablkcipher_finalize_req( struct virtio_crypto_sym_request *vc_sym_req, struct ablkcipher_request *req,
@@ -312,13 +318,19 @@ static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, unsigned int keylen) {struct virtio_crypto_ablkcipher_ctx *ctx =crypto_ablkcipher_ctx (tfm); +uint32_t alg;int ret; + ret =virtio_crypto_alg_validate_key (keylen, &alg); +if (ret) +return ret; +if (!ctx->vcrypto) {/* New key */ int node =virtio_crypto_get_current_node ();struct virtio_crypto *vcrypto = -virtcrypto_get_dev_node (node); +virtcrypto_get_dev_node (node, +VIRTIO_CRYPTO_SERVICE_CIPHER , alg);if (!vcrypto) {pr_err ("virtio_crypto: Could not find a virtio device in the system\n" );return -ENODEV ;
@@ -571,57 +583,85 @@ static void virtio_crypto_ablkcipher_finalize_req(virtcrypto_clear_request (&vc_sym_req->base); } -static struct crypto_alg virtio_crypto_algs[] = { { - .cra_name ="cbc(aes)" , - .cra_driver_name ="virtio_crypto_aes_cbc" , - .cra_priority =150 , - .cra_flags =CRYPTO_ALG_TYPE_ABLKCIPHER |CRYPTO_ALG_ASYNC , - .cra_blocksize =AES_BLOCK_SIZE , - .cra_ctxsize =sizeof (struct virtio_crypto_ablkcipher_ctx ), - .cra_alignmask =0 , - .cra_module =THIS_MODULE , - .cra_type = &crypto_ablkcipher_type, - .cra_init = virtio_crypto_ablkcipher_init, - .cra_exit = virtio_crypto_ablkcipher_exit, - .cra_u = { - .ablkcipher = { - .setkey = virtio_crypto_ablkcipher_setkey, - .decrypt = virtio_crypto_ablkcipher_decrypt, - .encrypt = virtio_crypto_ablkcipher_encrypt, - .min_keysize =AES_MIN_KEY_SIZE , - .max_keysize =AES_MAX_KEY_SIZE , - .ivsize =AES_BLOCK_SIZE , +static struct virtio_crypto_algo virtio_crypto_algs[] = { { + .algonum =VIRTIO_CRYPTO_CIPHER_AES_CBC , + .service =VIRTIO_CRYPTO_SERVICE_CIPHER , + .algo = { + .cra_name ="cbc(aes)" , + .cra_driver_name ="virtio_crypto_aes_cbc" , + .cra_priority =150 , + .cra_flags =CRYPTO_ALG_TYPE_ABLKCIPHER |CRYPTO_ALG_ASYNC , + .cra_blocksize =AES_BLOCK_SIZE , + .cra_ctxsize =sizeof (struct virtio_crypto_ablkcipher_ctx ), + .cra_alignmask =0 , + .cra_module =THIS_MODULE , + .cra_type = &crypto_ablkcipher_type, + .cra_init = virtio_crypto_ablkcipher_init, + .cra_exit = virtio_crypto_ablkcipher_exit, + .cra_u = { + .ablkcipher = { + .setkey = virtio_crypto_ablkcipher_setkey, + .decrypt = virtio_crypto_ablkcipher_decrypt, + .encrypt = virtio_crypto_ablkcipher_encrypt, + .min_keysize =AES_MIN_KEY_SIZE , + .max_keysize =AES_MAX_KEY_SIZE , + .ivsize =AES_BLOCK_SIZE , + }, }, }, } }; -int virtio_crypto_algs_register (void ) +int virtio_crypto_algs_register (struct virtio_crypto *vcrypto) {int ret =0 ; +int i =0 ;mutex_lock (&algs_lock); -if (++virtio_crypto_active_devs !=1 ) - goto unlock; - ret =crypto_register_algs (virtio_crypto_algs, -ARRAY_SIZE (virtio_crypto_algs)); -if (ret) - virtio_crypto_active_devs--; +for (i =0 ; i <ARRAY_SIZE (virtio_crypto_algs); i++) { + +uint32_t service = virtio_crypto_algs[i].service; +uint32_t algonum = virtio_crypto_algs[i].algonum; + +if (!virtcrypto_algo_is_supported (vcrypto, service, algonum)) +continue ; + +if (virtio_crypto_algs[i].active_devs ==0 ) { + ret =crypto_register_alg (&virtio_crypto_algs[i].algo); +if (ret) + goto unlock; + } + + virtio_crypto_algs[i].active_devs++; +dev_info (&vcrypto->vdev->dev,"Registered algo %s\n" , + virtio_crypto_algs[i].algo.cra_name); + } unlock:mutex_unlock (&algs_lock);return ret; } -void virtio_crypto_algs_unregister (void ) +void virtio_crypto_algs_unregister (struct virtio_crypto *vcrypto) { +int i =0 ; +mutex_lock (&algs_lock); -if (--virtio_crypto_active_devs !=0 ) - goto unlock; -crypto_unregister_algs (virtio_crypto_algs, -ARRAY_SIZE (virtio_crypto_algs)); +for (i =0 ; i <ARRAY_SIZE (virtio_crypto_algs); i++) { + +uint32_t service = virtio_crypto_algs[i].service; +uint32_t algonum = virtio_crypto_algs[i].algonum; + +if (virtio_crypto_algs[i].active_devs ==0 || + !virtcrypto_algo_is_supported (vcrypto, service, algonum)) +continue ; + +if (virtio_crypto_algs[i].active_devs ==1 ) +crypto_unregister_alg (&virtio_crypto_algs[i].algo); + + virtio_crypto_algs[i].active_devs--; + } -unlock:mutex_unlock (&algs_lock); }
drivers/crypto/virtio/virtio_crypto_common.h +8 -3
@@ -116,7 +116,12 @@ 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); -struct virtio_crypto *virtcrypto_get_dev_node (int node); +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_ablkcipher_crypt_req(
@@ -136,7 +141,7 @@ static inline int virtio_crypto_get_current_node(void) return node; } -int virtio_crypto_algs_register (void ); -void virtio_crypto_algs_unregister (void ); +int virtio_crypto_algs_register (struct virtio_crypto *vcrypto); +void virtio_crypto_algs_unregister (struct virtio_crypto *vcrypto);#endif /* _VIRTIO_CRYPTO_COMMON_H */
drivers/crypto/virtio/virtio_crypto_mgr.c +75 -6
115 lines not on this page · raw message
Signature · 1 line
2.7.4
Gonglei (Arei)
maintainerRE: [RFC v1 2/2] crypto/virtio-crypto: Register an algo only if it's supported
Quoted 76 lines
-----Original Message-----
From: Farhan Ali [mailto:[email protected]]
Sent: Saturday, June 09, 2018 3:09 AM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; Gonglei (Arei) <[email protected]>; longpeng <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [RFC v1 2/2]crypto/virtio-crypto: Register an algo only if it's supportedFrom: Farhan Ali <[email protected]>
Register a crypto algo with the Linux crypto layer only if the algorithm is supported by the backend virtio-crypto device.
Also route crypto requests to a virtio-crypto device, only if it can support the requested service and algorithm.
Signed-off-by Farhan Ali <[email protected]>--- drivers/crypto/virtio/virtio_crypto_algs.c | 110 ++++++++++++++++++--------- drivers/crypto/virtio/virtio_crypto_common.h | 11 ++- drivers/crypto/virtio/virtio_crypto_mgr.c | 81 ++++++++++++++++++-- 3 files changed, 158 insertions(+), 44 deletions(-)drivers/crypto/virtio/virtio_crypto_algs.c +14
-2@@ -49,12 +49,18 @@ struct virtio_crypto_sym_request {bool encrypt; }; +struct virtio_crypto_algo { +uint32_t algonum; +uint32_t service; +unsigned int active_devs; +struct crypto_alg algo; +}; +/* * The algs_lock protects the below global virtio_crypto_active_devs * and crypto algorithms registion. */ static DEFINE_MUTEX (algs_lock); -static unsigned int virtio_crypto_active_devs; static void virtio_crypto_ablkcipher_finalize_req( struct virtio_crypto_sym_request *vc_sym_req, struct ablkcipher_request *req,@@ -312,13 +318,19 @@ static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, unsigned int keylen) {struct virtio_crypto_ablkcipher_ctx *ctx =crypto_ablkcipher_ctx (tfm); +uint32_t alg;int ret; + ret =virtio_crypto_alg_validate_key (keylen, &alg); +if (ret) +return ret; +if (!ctx->vcrypto) {/* New key */ int node =virtio_crypto_get_current_node ();struct virtio_crypto *vcrypto = -virtcrypto_get_dev_node (node); +virtcrypto_get_dev_node (node, +VIRTIO_CRYPTO_SERVICE_CIPHER , alg);if (!vcrypto) { pr_err("virtio_crypto: Could not find a virtio device in the
Quoted 1 line
system\n");
We'd better change the above error message now. What about:
" virtio_crypto: Could not find a virtio device in the system or unsupported algo" ?
Regards,
-Gonglei
Re: [RFC v1 2/2] crypto/virtio-crypto: Register an algo only if it's supported
Quoted 81 lines · Gonglei (Arei)
Quoted 76 lines
-----Original Message-----
From: Farhan Ali [mailto:[email protected]]
Sent: Saturday, June 09, 2018 3:09 AM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; Gonglei (Arei) <[email protected]>; longpeng <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [RFC v1 2/2]crypto/virtio-crypto: Register an algo only if it's supportedFrom: Farhan Ali <[email protected]>
Register a crypto algo with the Linux crypto layer only if the algorithm is supported by the backend virtio-crypto device.
Also route crypto requests to a virtio-crypto device, only if it can support the requested service and algorithm.
Signed-off-by Farhan Ali <[email protected]>--- drivers/crypto/virtio/virtio_crypto_algs.c | 110 ++++++++++++++++++--------- drivers/crypto/virtio/virtio_crypto_common.h | 11 ++- drivers/crypto/virtio/virtio_crypto_mgr.c | 81 ++++++++++++++++++-- 3 files changed, 158 insertions(+), 44 deletions(-)drivers/crypto/virtio/virtio_crypto_algs.c +14
-2@@ -49,12 +49,18 @@ struct virtio_crypto_sym_request {bool encrypt; }; +struct virtio_crypto_algo { +uint32_t algonum; +uint32_t service; +unsigned int active_devs; +struct crypto_alg algo; +}; +/* * The algs_lock protects the below global virtio_crypto_active_devs * and crypto algorithms registion. */ static DEFINE_MUTEX (algs_lock); -static unsigned int virtio_crypto_active_devs; static void virtio_crypto_ablkcipher_finalize_req( struct virtio_crypto_sym_request *vc_sym_req, struct ablkcipher_request *req,@@ -312,13 +318,19 @@ static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, unsigned int keylen) {struct virtio_crypto_ablkcipher_ctx *ctx =crypto_ablkcipher_ctx (tfm); +uint32_t alg;int ret; + ret =virtio_crypto_alg_validate_key (keylen, &alg); +if (ret) +return ret; +if (!ctx->vcrypto) {/* New key */ int node =virtio_crypto_get_current_node ();struct virtio_crypto *vcrypto = -virtcrypto_get_dev_node (node); +virtcrypto_get_dev_node (node, +VIRTIO_CRYPTO_SERVICE_CIPHER , alg);if (!vcrypto) { pr_err("virtio_crypto: Could not find a virtio device in theQuoted 1 line
system\n");
We'd better change the above error message now. What about:
"virtio_crypto: Could not find a virtio device in the system or unsupported algo" ?
Quoted 2 lines
Regards,
-Gonglei
Sure, I will update the error message. But other than that does the rest of the code looks good to you?
Thanks
Farhan
Quoted 4 lines
RE: [RFC v1 2/2] crypto/virtio-crypto: Register an algo only if it's supported
Quoted 99 lines
-----Original Message-----
From: Farhan Ali [mailto:[email protected]]
Sent: Wednesday, June 13, 2018 1:08 AM
To: Gonglei (Arei) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; longpeng <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC v1 2/2]crypto/virtio-crypto: Register an algo only if it's supportedQuoted 11 lines · Gonglei (Arei)
Quoted 11 lines
-----Original Message-----
From: Farhan Ali [mailto:[email protected]]
Sent: Saturday, June 09, 2018 3:09 AM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; Gonglei (Arei) <[email protected]>; longpeng <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [RFC v1 2/2]crypto/virtio-crypto: Register an algo only if it'ssupported
Quoted 11 lines
Quoted 11 lines
From: Farhan Ali <[email protected]>
Register a crypto algo with the Linux crypto layer only if the algorithm is supported by the backend virtio-crypto device.
Also route crypto requests to a virtio-crypto device, only if it can support the requested service and algorithm.
Quoted 6 lines
Quoted 6 lines
Signed-off-by Farhan Ali <[email protected]>--- drivers/crypto/virtio/virtio_crypto_algs.c | 110 ++++++++++++++++++--------- drivers/crypto/virtio/virtio_crypto_common.h | 11 ++- drivers/crypto/virtio/virtio_crypto_mgr.c | 81++++++++++++++++++--
Quoted 24 lines
Quoted 24 lines
3 files changed, 158 insertions(+), 44 deletions(-)drivers/crypto/virtio/virtio_crypto_algs.c +7
-1@@ -49,12 +49,18 @@ struct virtio_crypto_sym_request {bool encrypt; }; +struct virtio_crypto_algo { +uint32_t algonum; +uint32_t service; +unsigned int active_devs; +struct crypto_alg algo; +}; +/* * The algs_lock protects the below global virtio_crypto_active_devs * and crypto algorithms registion. */ static DEFINE_MUTEX (algs_lock); -static unsigned int virtio_crypto_active_devs;Quoted 8 lines
drivers/crypto/virtio/virtio_crypto_algs.c static int virtio_crypto_ablkcipher_setkey(struct · line 321
Quoted 8 lines
drivers/crypto/virtio/virtio_crypto_algs.c +0
-0static void virtio_crypto_ablkcipher_finalize_req( struct virtio_crypto_sym_request *vc_sym_req, struct ablkcipher_request *req,@@ -312,13 +318,19 @@ static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, unsigned int keylen) { struct virtio_crypto_ablkcipher_ctx *ctx =
crypto_ablkcipher_ctx(tfm);Quoted 12 lines
Quoted 12 lines
drivers/crypto/virtio/virtio_crypto_algs.c +5
-1+uint32_t alg;int ret; + ret =virtio_crypto_alg_validate_key (keylen, &alg); +if (ret) +return ret; +if (!ctx->vcrypto) {/* New key */ int node =virtio_crypto_get_current_node (); struct virtio_crypto *vcrypto = -virtcrypto_get_dev_node (node);Quoted 8 lines
Quoted 5 lines
drivers/crypto/virtio/virtio_crypto_algs.c +2
-0+virtcrypto_get_dev_node (node, +VIRTIO_CRYPTO_SERVICE_CIPHER , alg);if (!vcrypto) { pr_err("virtio_crypto: Could not find a virtio device in thesystem\n");
We'd better change the above error message now. What about:
"virtio_crypto: Could not find a virtio device in the system or unsupported
Quoted 9 lines
algo" ?
Quoted 3 lines
Regards,
-GongleiSure, I will update the error message. But other than that does the rest of the code looks good to you?
Yes, good work. You can add my ack in v2:
Regards,
-Gonglei