crypto: zynqmp-aes-gcm: Bug fixes and aes-gcm support for Versal
Subsystems (3)
cover crypto: zynqmp-aes-gcm: Bug fixes and aes-gcm support for Versal
This series includes bug fixes and adds aes-gcm support for Versal device. It is based on
https://github.com/Xilinx/linux-xlnx/commits/for-next/
because of dependency on below patches
Harsh Jain (15):
firmware: zynqmp: Move crypto API's to separate file
crypto: zynqmp-aes-gcm: cleanup: Remove union from zynqmp_aead_drv_ctx
firmware: zynqmp: Add helper API to self discovery the device
dt-bindings: crypto: Mark zynqmp-aes as Deprecated
crypto: zynqmp-aes-gcm: Update probe to self discover the device
crypto: zynqmp-aes-gcm: Return linux error code instead of firmware
error code
crypto: zynqmp-aes-gcm: Avoid Encrypt request to fallback for authsize
< 16
crypto: zynqmp-aes-gcm: Avoid submitting fallback requests to engine
crypto: zynqmp-aes-gcm: Fix setkey operation to select HW keys
crypto: zynqmp-aes-gcm: Change coherent DMA to streaming DMA API
firmware: xilinx: Add firmware API's to support aes-gcm in Versal
device
crypto: xilinx: Replace zynqmp prefix with xilinx
crypto: xilinx: cleanup: Remove un-necessary typecast operation
crypto: zynqmp-aes-gcm: Save dma bit mask value in driver context
crypto: xilinx: Add gcm(aes) support for AMD/Xilinx Versal device
.../bindings/crypto/xlnx,zynqmp-aes.yaml | 2 + .../firmware/xilinx/xlnx,zynqmp-firmware.yaml | 1 + drivers/crypto/xilinx/zynqmp-aes-gcm.c | 820 ++++++++++++++---- drivers/firmware/xilinx/Makefile | 2 +- drivers/firmware/xilinx/zynqmp-crypto.c | 239 +++++ drivers/firmware/xilinx/zynqmp.c | 49 -- include/linux/firmware/xlnx-zynqmp-crypto.h | 119 +++ include/linux/firmware/xlnx-zynqmp.h | 14 +- 8 files changed, 1005 insertions(+), 241 deletions(-) create mode 100644 drivers/firmware/xilinx/zynqmp-crypto.c create mode 100644 include/linux/firmware/xlnx-zynqmp-crypto.h
Signature · 1 line
2.49.1
PATCH 2/15 crypto: zynqmp-aes-gcm: cleanup: Remove union from zynqmp_aead_drv_ctx
"alg" in zynqmp_aead_drv_ctx is single field union variable. Remove unnecessary alg union from structure.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +11 -13
@@ -43,9 +43,7 @@ enum zynqmp_aead_keysrc { };struct zynqmp_aead_drv_ctx { -union { -struct aead_engine_alg aead; - } alg; +struct aead_engine_alg aead;struct device *dev;struct crypto_engine *engine; };
@@ -289,7 +287,7 @@ static int zynqmp_aes_aead_encrypt(struct aead_request *req)struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); rq_ctx->op =ZYNQMP_AES_ENCRYPT ; - drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, alg.aead.base); + drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base);return crypto_transfer_aead_request_to_engine (drv_ctx->engine, req); }
@@ -302,7 +300,7 @@ static int zynqmp_aes_aead_decrypt(struct aead_request *req)struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); rq_ctx->op =ZYNQMP_AES_DECRYPT ; - drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, alg.aead.base); + drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base);return crypto_transfer_aead_request_to_engine (drv_ctx->engine, req); }
@@ -315,16 +313,16 @@ static int zynqmp_aes_aead_init(struct crypto_aead *aead)struct zynqmp_aead_drv_ctx *drv_ctx;struct aead_alg *alg =crypto_aead_alg (aead); - drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, alg.aead.base); + drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base); tfm_ctx->dev = drv_ctx->dev; - tfm_ctx->fbk_cipher =crypto_alloc_aead (drv_ctx->alg.aead.base.base.cra_name, + tfm_ctx->fbk_cipher =crypto_alloc_aead (drv_ctx->aead.base.base.cra_name,0 ,CRYPTO_ALG_NEED_FALLBACK );if (IS_ERR (tfm_ctx->fbk_cipher)) {pr_err ("%s() Error: failed to allocate fallback for %s\n" , - __func__, drv_ctx->alg.aead.base.base.cra_name); + __func__, drv_ctx->aead.base.base.cra_name);return PTR_ERR (tfm_ctx->fbk_cipher); }
@@ -349,7 +347,7 @@ static void zynqmp_aes_aead_exit(struct crypto_aead *aead) }static struct zynqmp_aead_drv_ctx aes_drv_ctx = { - .alg.aead.base = { + .aead.base = { .setkey = zynqmp_aes_aead_setkey, .setauthsize = zynqmp_aes_aead_setauthsize, .encrypt = zynqmp_aes_aead_encrypt,
@@ -372,7 +370,7 @@ static struct zynqmp_aead_drv_ctx aes_drv_ctx = { .cra_module = THIS_MODULE, } }, - .alg.aead.op = { + .aead.op = { .do_one_request = zynqmp_handle_aes_req, }, };
@@ -407,7 +405,7 @@ static int zynqmp_aes_aead_probe(struct platform_device *pdev) goto err_engine; } - err =crypto_engine_register_aead (&aes_drv_ctx.alg.aead); + err =crypto_engine_register_aead (&aes_drv_ctx.aead);if (err <0 ) {dev_err (dev,"Failed to register AEAD alg.\n" ); goto err_aead;
@@ -415,7 +413,7 @@ static int zynqmp_aes_aead_probe(struct platform_device *pdev)return 0 ; err_aead: -crypto_engine_unregister_aead (&aes_drv_ctx.alg.aead); +crypto_engine_unregister_aead (&aes_drv_ctx.aead); err_engine:if (aes_drv_ctx.engine)
@@ -427,7 +425,7 @@ static int zynqmp_aes_aead_probe(struct platform_device *pdev)static void zynqmp_aes_aead_remove (struct platform_device *pdev) {crypto_engine_exit (aes_drv_ctx.engine); -crypto_engine_unregister_aead (&aes_drv_ctx.alg.aead); +crypto_engine_unregister_aead (&aes_drv_ctx.aead); }static const struct of_device_id zynqmp_aes_dt_ids[] = {
Signature · 1 line
2.49.1
PATCH 4/15 dt-bindings: crypto: Mark zynqmp-aes as Deprecated
In reply to Harsh Jainzynqmp-aes-gcm updated to self discover, corresponding dt binding can be mark deprecated.
--- Documentation/devicetree/bindings/crypto/xlnx,zynqmp-aes.yaml | 2 ++ .../bindings/firmware/xilinx/xlnx,zynqmp-firmware.yaml | 1 + 2 files changed, 3 insertions(+)
Documentation/devicetree/bindings/crypto/xlnx,zynqmp-aes.yaml +2 -0
@@ -14,6 +14,8 @@ description: | The ZynqMP AES-GCM hardened cryptographic accelerator is used to encrypt or decrypt the data with provided key and initialization vector. +deprecated: true + properties: compatible: const: xlnx,zynqmp-aes
Documentation/devicetree/bindings/firmware/xilinx/xlnx,zynqmp-firmware.yaml +1 -0
@@ -104,6 +104,7 @@ properties: used to encrypt or decrypt the data with provided key and initialization vector. type: object + deprecated: true required: - compatible
Signature · 1 line
2.49.1
PATCH 5/15 crypto: zynqmp-aes-gcm: Update probe to self discover the device
In reply to Harsh JainUpdate driver to self discover the device.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 86 +++++++++++++++++++------- 1 file changed, 62 insertions(+), 24 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +62 -24
@@ -346,7 +346,7 @@ static void zynqmp_aes_aead_exit(struct crypto_aead *aead)memzero_explicit (tfm_ctx,sizeof (struct zynqmp_aead_tfm_ctx )); } -static struct zynqmp_aead_drv_ctx aes_drv_ctx = { +static struct zynqmp_aead_drv_ctx zynqmp_aes_drv_ctx = { .aead.base = { .setkey = zynqmp_aes_aead_setkey, .setauthsize = zynqmp_aes_aead_setauthsize,
@@ -375,74 +375,112 @@ static struct zynqmp_aead_drv_ctx aes_drv_ctx = { }, }; +static struct xlnx_feature aes_feature_map[] = { + { + .family =PM_ZYNQMP_FAMILY_CODE , + .feature_id =PM_SECURE_AES , + .data = &zynqmp_aes_drv_ctx, + }, + {/* sentinel */ } +}; +static int zynqmp_aes_aead_probe (struct platform_device *pdev) { +struct zynqmp_aead_drv_ctx *aes_drv_ctx;struct device *dev = &pdev->dev;int err; +/* Verify the hardware is present */ + aes_drv_ctx =xlnx_get_crypto_dev_data (aes_feature_map); +if (IS_ERR (aes_drv_ctx)) { +dev_err (dev,"AES is not supported on the platform\n" ); +return PTR_ERR (aes_drv_ctx); + } +/* ZynqMP AES driver supports only one instance */ -if (!aes_drv_ctx.dev) - aes_drv_ctx.dev = dev; +if (!aes_drv_ctx->dev) + aes_drv_ctx->dev = dev;else return -ENODEV ; +platform_set_drvdata (pdev, aes_drv_ctx); err =dma_set_mask_and_coherent (dev,DMA_BIT_MASK (ZYNQMP_DMA_BIT_MASK ));if (err <0 ) {dev_err (dev,"No usable DMA configuration\n" );return err; } - aes_drv_ctx.engine =crypto_engine_alloc_init (dev,1 ); -if (!aes_drv_ctx.engine) { + aes_drv_ctx->engine =crypto_engine_alloc_init (dev,1 ); +if (!aes_drv_ctx->engine) {dev_err (dev,"Cannot alloc AES engine\n" ); err = -ENOMEM ; goto err_engine; } - err =crypto_engine_start (aes_drv_ctx.engine); + err =crypto_engine_start (aes_drv_ctx->engine);if (err) {dev_err (dev,"Cannot start AES engine\n" ); goto err_engine; } - err =crypto_engine_register_aead (&aes_drv_ctx.aead); + err =crypto_engine_register_aead (&aes_drv_ctx->aead);if (err <0 ) {dev_err (dev,"Failed to register AEAD alg.\n" ); - goto err_aead; + goto err_engine; }return 0 ; -err_aead: -crypto_engine_unregister_aead (&aes_drv_ctx.aead); - err_engine: -if (aes_drv_ctx.engine) -crypto_engine_exit (aes_drv_ctx.engine); +if (aes_drv_ctx->engine) +crypto_engine_exit (aes_drv_ctx->engine);return err; }static void zynqmp_aes_aead_remove (struct platform_device *pdev) { -crypto_engine_exit (aes_drv_ctx.engine); -crypto_engine_unregister_aead (&aes_drv_ctx.aead); -} +struct zynqmp_aead_drv_ctx *aes_drv_ctx; -static const struct of_device_id zynqmp_aes_dt_ids[] = { - { .compatible ="xlnx,zynqmp-aes" }, - {/* sentinel */ } -}; -MODULE_DEVICE_TABLE (of, zynqmp_aes_dt_ids); + aes_drv_ctx =platform_get_drvdata (pdev); +crypto_engine_exit (aes_drv_ctx->engine); +crypto_engine_unregister_aead (&aes_drv_ctx->aead); +}static struct platform_driver zynqmp_aes_driver = { .probe = zynqmp_aes_aead_probe, .remove = zynqmp_aes_aead_remove, .driver = { .name ="zynqmp-aes" , - .of_match_table = zynqmp_aes_dt_ids, }, }; -module_platform_driver (zynqmp_aes_driver); -MODULE_DESCRIPTION ("Xilinx ZynqMP AES Driver" ); +static struct platform_device *platform_dev; + +static int __init aes_driver_init (void ) +{ +int ret; + + ret =platform_driver_register (&zynqmp_aes_driver); +if (ret) +return ret; + + platform_dev =platform_device_register_simple (zynqmp_aes_driver.driver.name, +0 ,NULL ,0 ); +if (IS_ERR (platform_dev)) { + ret =PTR_ERR (platform_dev); +platform_driver_unregister (&zynqmp_aes_driver); + } + +return ret; +} + +static void __exit aes_driver_exit(void) +{ +platform_device_unregister (platform_dev); +platform_driver_unregister (&zynqmp_aes_driver); +} + +module_init (aes_driver_init); +module_exit (aes_driver_exit); +MODULE_DESCRIPTION ("zynqmp aes-gcm hardware acceleration support." );MODULE_LICENSE ("GPL" );
Signature · 1 line
2.49.1
PATCH 7/15 crypto: zynqmp-aes-gcm: Avoid Encrypt request to fallback for authsize < 16
In reply to Harsh JainEncrypt requests can be handled by driver when authsize is less than 16.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +3 -3
@@ -150,7 +150,7 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req) } }else {if (hwreq->op ==ZYNQMP_AES_ENCRYPT ) - data_size = data_size +ZYNQMP_AES_AUTH_SIZE ; + data_size = data_size +crypto_aead_authsize (aead);else data_size = data_size -ZYNQMP_AES_AUTH_SIZE ;
@@ -178,8 +178,8 @@ static int zynqmp_fallback_check(struct zynqmp_aead_tfm_ctx *tfm_ctx,int need_fallback =0 ;struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); -if (tfm_ctx->authsize !=ZYNQMP_AES_AUTH_SIZE ) - need_fallback =1 ; +if (tfm_ctx->authsize !=ZYNQMP_AES_AUTH_SIZE && rq_ctx->op ==ZYNQMP_AES_DECRYPT ) +return 1 ;if (tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY && tfm_ctx->keylen !=ZYNQMP_AES_KEY_SIZE ) {
Signature · 1 line
2.49.1
PATCH 9/15 crypto: zynqmp-aes-gcm: Fix setkey operation to select HW keys
In reply to Harsh JainCurrently keylen 1 is used to select hw key. There are -ve self test which can fail for setkey length 1. Update driver to use 4 bytes with magic number to select H/W key type.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 94 ++++++++++++++++---------- 1 file changed, 60 insertions(+), 34 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +60 -34
@@ -22,14 +22,14 @@#define ZYNQMP_AES_KEY_SIZE AES_KEYSIZE_256#define ZYNQMP_AES_AUTH_SIZE 16U -#define ZYNQMP_KEY_SRC_SEL_KEY_LEN 1U#define ZYNQMP_AES_BLK_SIZE 1U#define ZYNQMP_AES_MIN_INPUT_BLK_SIZE 4U#define ZYNQMP_AES_WORD_LEN 4U -#define ZYNQMP_AES_GCM_TAG_MISMATCH_ERR 0x01 -#define ZYNQMP_AES_WRONG_KEY_SRC_ERR 0x13 -#define ZYNQMP_AES_PUF_NOT_PROGRAMMED 0xE300 +#define ZYNQMP_AES_GCM_TAG_MISMATCH_ERR 0x01 +#define ZYNQMP_AES_WRONG_KEY_SRC_ERR 0x13 +#define ZYNQMP_AES_PUF_NOT_PROGRAMMED 0xE300 +#define XILINX_KEY_MAGIC 0x3EA0enum zynqmp_aead_op {ZYNQMP_AES_DECRYPT =0 ,
@@ -48,6 +48,11 @@ struct zynqmp_aead_drv_ctx {struct crypto_engine *engine; }; +struct xilinx_hwkey_info { +u16 magic; +u16 type; +} __packed; +struct zynqmp_aead_hw_req {u64 src;u64 iv;
@@ -64,7 +69,7 @@ struct zynqmp_aead_tfm_ctx {u8 *iv;u32 keylen;u32 authsize; -enum zynqmp_aead_keysrc keysrc; +u8 keysrc;struct crypto_aead *fbk_cipher; };
@@ -175,32 +180,29 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)static int zynqmp_fallback_check (struct zynqmp_aead_tfm_ctx *tfm_ctx,struct aead_request *req) { -int need_fallback =0 ;struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req);if (tfm_ctx->authsize !=ZYNQMP_AES_AUTH_SIZE && rq_ctx->op ==ZYNQMP_AES_DECRYPT )return 1 ; -if (tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY && - tfm_ctx->keylen !=ZYNQMP_AES_KEY_SIZE ) { - need_fallback =1 ; - }if (req->assoclen !=0 || - req->cryptlen <ZYNQMP_AES_MIN_INPUT_BLK_SIZE ) { - need_fallback =1 ; - } + req->cryptlen <ZYNQMP_AES_MIN_INPUT_BLK_SIZE ) +return 1 ; +if (tfm_ctx->keylen ==AES_KEYSIZE_128 || + tfm_ctx->keylen ==AES_KEYSIZE_192 ) +return 1 ; +if ((req->cryptlen %ZYNQMP_AES_WORD_LEN ) !=0 ) - need_fallback =1 ; +return 1 ;if (rq_ctx->op ==ZYNQMP_AES_DECRYPT && - req->cryptlen <=ZYNQMP_AES_AUTH_SIZE ) { - need_fallback =1 ; - } -return need_fallback; + req->cryptlen <=ZYNQMP_AES_AUTH_SIZE ) +return 1 ; + +return 0 ; } -static int zynqmp_handle_aes_req (struct crypto_engine *engine, -void *req) +static int zynqmp_handle_aes_req (struct crypto_engine *engine,void *req) {struct aead_request *areq = container_of(req, struct aead_request, base);
@@ -218,32 +220,42 @@ static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key, unsigned int keylen) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct zynqmp_aead_tfm_ctx *tfm_ctx = - (struct zynqmp_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_tfm_ctx (tfm); +struct xilinx_hwkey_info hwkey;unsigned char keysrc; +int err; -if (keylen ==ZYNQMP_KEY_SRC_SEL_KEY_LEN ) { - keysrc = *key; +if (keylen ==sizeof (struct xilinx_hwkey_info )) { +memcpy (&hwkey, key,sizeof (struct xilinx_hwkey_info )); +if (hwkey.magic !=XILINX_KEY_MAGIC ) +return -EINVAL ; + keysrc = hwkey.type;if (keysrc ==ZYNQMP_AES_KUP_KEY || keysrc ==ZYNQMP_AES_DEV_KEY || keysrc ==ZYNQMP_AES_PUF_KEY ) { - tfm_ctx->keysrc = (enum zynqmp_aead_keysrc )keysrc; - }else { - tfm_ctx->keylen = keylen; + tfm_ctx->keysrc = keysrc; + tfm_ctx->keylen =sizeof (struct xilinx_hwkey_info ); +return 0 ; } - }else { +return -EINVAL ; + } + +if (keylen ==ZYNQMP_AES_KEY_SIZE && tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY ) { tfm_ctx->keylen = keylen; -if (keylen ==ZYNQMP_AES_KEY_SIZE ) { - tfm_ctx->keysrc =ZYNQMP_AES_KUP_KEY ; -memcpy (tfm_ctx->key, key, keylen); - } +memcpy (tfm_ctx->key, key, keylen); + }else if (tfm_ctx->keysrc !=ZYNQMP_AES_KUP_KEY ) { +return -EINVAL ; } tfm_ctx->fbk_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK ; tfm_ctx->fbk_cipher->base.crt_flags |= (aead->base.crt_flags &CRYPTO_TFM_REQ_MASK ); -return crypto_aead_setkey (tfm_ctx->fbk_cipher, key, keylen); + err =crypto_aead_setkey (tfm_ctx->fbk_cipher, key, keylen); +if (!err) + tfm_ctx->keylen = keylen; + +return err; } static int zynqmp_aes_aead_setauthsize(struct crypto_aead *aead,
@@ -267,9 +279,16 @@ static int zynqmp_aes_aead_encrypt(struct aead_request *req)struct zynqmp_aead_drv_ctx *drv_ctx;int err; - rq_ctx->op =ZYNQMP_AES_ENCRYPT ; drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base); +if (tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY && + tfm_ctx->keylen ==sizeof (struct xilinx_hwkey_info )) +return -EINVAL ; + + rq_ctx->op =ZYNQMP_AES_ENCRYPT ; err =zynqmp_fallback_check (tfm_ctx, req); +if (err && tfm_ctx->keysrc !=ZYNQMP_AES_KUP_KEY ) +return -EOPNOTSUPP ; +if (err) {aead_request_set_tfm (subreq, tfm_ctx->fbk_cipher); aead_request_set_callback(subreq, req->base.flags,
@@ -297,7 +316,12 @@ static int zynqmp_aes_aead_decrypt(struct aead_request *req) rq_ctx->op =ZYNQMP_AES_DECRYPT ; drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base); +if (tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY && + tfm_ctx->keylen ==sizeof (struct xilinx_hwkey_info )) +return -EINVAL ; err =zynqmp_fallback_check (tfm_ctx, req); +if (err && tfm_ctx->keysrc !=ZYNQMP_AES_KUP_KEY ) +return -EOPNOTSUPP ;if (err) {aead_request_set_tfm (subreq, tfm_ctx->fbk_cipher); aead_request_set_callback(subreq, req->base.flags,
@@ -323,6 +347,8 @@ static int zynqmp_aes_aead_init(struct crypto_aead *aead) drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base); tfm_ctx->dev = drv_ctx->dev; + tfm_ctx->keylen =0 ; + tfm_ctx->keysrc =ZYNQMP_AES_KUP_KEY ; tfm_ctx->fbk_cipher = crypto_alloc_aead(drv_ctx->aead.base.base.cra_name, 0,
Signature · 1 line
2.49.1
PATCH 1/15 firmware: zynqmp: Move crypto API's to separate file
In reply to Harsh JainFor better maintainability move crypto related API's to new zynqmp-crypto.c file.
--- drivers/firmware/xilinx/Makefile | 2 +- drivers/firmware/xilinx/zynqmp-crypto.c | 59 +++++++++++++++++++++ drivers/firmware/xilinx/zynqmp.c | 49 ----------------- include/linux/firmware/xlnx-zynqmp-crypto.h | 28 ++++++++++ include/linux/firmware/xlnx-zynqmp.h | 14 +---- 5 files changed, 89 insertions(+), 63 deletions(-) create mode 100644 drivers/firmware/xilinx/zynqmp-crypto.c create mode 100644 include/linux/firmware/xlnx-zynqmp-crypto.h
drivers/firmware/xilinx/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 # Makefile for Xilinx firmwares -obj-$(CONFIG_ZYNQMP_FIRMWARE) += zynqmp.o +obj-$(CONFIG_ZYNQMP_FIRMWARE) += zynqmp.o zynqmp-crypto.o obj-$(CONFIG_ZYNQMP_FIRMWARE_DEBUG) += zynqmp-debug.o
drivers/firmware/xilinx/zynqmp-crypto.c new file +59 -0
59 lines not on this page · raw message
drivers/firmware/xilinx/zynqmp.c +0 -49
61 lines not on this page · raw message
include/linux/firmware/xlnx-zynqmp-crypto.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Firmware layer for XilSECURE APIs. +* +* Copyright (C) 2014-2022 Xilinx, Inc. +* Copyright (C) 2022-2025 Advanced Micro Devices, Inc. +*/ + +#ifndef __FIRMWARE_XLNX_ZYNQMP_CRYPTO_H__ +#define __FIRMWARE_XLNX_ZYNQMP_CRYPTO_H__ + +#if IS_REACHABLE (CONFIG_ZYNQMP_FIRMWARE ) +int zynqmp_pm_aes_engine (const u64 address,u32 *out); +int zynqmp_pm_sha_hash (const u64 address,const u32 size,const u32 flags); +#else +static inline int zynqmp_pm_aes_engine (const u64 address,u32 *out) +{ +return -ENODEV ; +} + +static inline int zynqmp_pm_sha_hash (const u64 address,const u32 size, +const u32 flags) +{ +return -ENODEV ; +} +#endif + +#endif /* __FIRMWARE_XLNX_ZYNQMP_CRYPTO_H__ */
include/linux/firmware/xlnx-zynqmp.h +1 -13
@@ -16,6 +16,7 @@#include <linux/types.h> #include <linux/err.h> +#include <linux/firmware/xlnx-zynqmp-crypto.h> #define ZYNQMP_PM_VERSION_MAJOR 1#define ZYNQMP_PM_VERSION_MINOR 0
@@ -587,9 +588,7 @@ int zynqmp_pm_release_node(const u32 node);int zynqmp_pm_set_requirement (const u32 node,const u32 capabilities,const u32 qos,const enum zynqmp_pm_request_ack ack); -int zynqmp_pm_aes_engine (const u64 address,u32 *out);int zynqmp_pm_efuse_access (const u64 address,u32 *out); -int zynqmp_pm_sha_hash (const u64 address,const u32 size,const u32 flags);int zynqmp_pm_fpga_load (const u64 address,const u32 size,const u32 flags);int zynqmp_pm_fpga_get_status (u32 *value);int zynqmp_pm_fpga_get_config_status (u32 *value);
@@ -767,22 +766,11 @@ static inline int zynqmp_pm_set_requirement(const u32 node,return -ENODEV ; } -static inline int zynqmp_pm_aes_engine (const u64 address,u32 *out) -{ -return -ENODEV ; -} -static inline int zynqmp_pm_efuse_access (const u64 address,u32 *out) {return -ENODEV ; } -static inline int zynqmp_pm_sha_hash (const u64 address,const u32 size, -const u32 flags) -{ -return -ENODEV ; -} -static inline int zynqmp_pm_fpga_load (const u64 address,const u32 size,const u32 flags) {
Signature · 1 line
2.49.1
PATCH 3/15 firmware: zynqmp: Add helper API to self discovery the device
In reply to Harsh JainAdd API to get SoC version and family info.
--- drivers/firmware/xilinx/zynqmp-crypto.c | 31 +++++++++++++++++++++ include/linux/firmware/xlnx-zynqmp-crypto.h | 19 +++++++++++++ 2 files changed, 50 insertions(+)
drivers/firmware/xilinx/zynqmp-crypto.c +31 -0
@@ -57,3 +57,34 @@ int zynqmp_pm_sha_hash(const u64 address, const u32 size, const u32 flags)return zynqmp_pm_invoke_fn (PM_SECURE_SHA ,NULL ,4 , upper_addr, lower_addr, size, flags); }EXPORT_SYMBOL_GPL (zynqmp_pm_sha_hash); + +/** +* xlnx_get_crypto_dev_data() - Get crypto dev data of platform +* @feature_map: List of available feature map of all platform +* +* Return: Returns crypto dev data, either address crypto dev or ERR PTR +*/ +void *xlnx_get_crypto_dev_data (struct xlnx_feature *feature_map) +{ +struct xlnx_feature *feature; +u32 pm_family_code; +int ret; + +/* Get the Family code and sub family code of platform */ + ret =zynqmp_pm_get_family_info (&pm_family_code); +if (ret <0 ) +return ERR_PTR (ret); + + feature = feature_map; +for (; feature->family; feature++) { +if (feature->family == pm_family_code) { + ret =zynqmp_pm_feature (feature->feature_id); +if (ret <0 ) +return ERR_PTR (ret); + +return feature->data; + } + } +return ERR_PTR (-ENODEV ); +} +EXPORT_SYMBOL_GPL (xlnx_get_crypto_dev_data);
include/linux/firmware/xlnx-zynqmp-crypto.h +19 -0
@@ -9,9 +9,23 @@ #ifndef __FIRMWARE_XLNX_ZYNQMP_CRYPTO_H__ #define __FIRMWARE_XLNX_ZYNQMP_CRYPTO_H__ +/** + * struct xlnx_feature - Feature data + * @family: Family code of platform + * @subfamily: Subfamily code of platform + * @feature_id: Feature id of module + * @data: Collection of all supported platform data + */ +struct xlnx_feature { + u32 family; + u32 feature_id; + void *data; +}; + #if IS_REACHABLE(CONFIG_ZYNQMP_FIRMWARE) int zynqmp_pm_aes_engine(const u64 address, u32 *out); int zynqmp_pm_sha_hash(const u64 address, const u32 size, const u32 flags); +void *xlnx_get_crypto_dev_data(struct xlnx_feature *feature_map); #else static inline int zynqmp_pm_aes_engine(const u64 address, u32 *out) {
@@ -23,6 +37,11 @@ static inline int zynqmp_pm_sha_hash(const u64 address, const u32 size, {return -ENODEV ; } + +static inline void *xlnx_get_crypto_dev_data (struct xlnx_feature *feature_map) +{ +return ERR_PTR (-ENODEV ); +}#endif #endif /* __FIRMWARE_XLNX_ZYNQMP_CRYPTO_H__ */
Signature · 1 line
2.49.1
PATCH 11/15 firmware: xilinx: Add firmware API's to support aes-gcm in Versal device
In reply to Harsh JainAdd aes-gcm crypto API's for AMD/Xilinx Versal device.
--- drivers/firmware/xilinx/zynqmp-crypto.c | 151 +++++++++++++++++++- include/linux/firmware/xlnx-zynqmp-crypto.h | 76 +++++++++- 2 files changed, 224 insertions(+), 3 deletions(-)
drivers/firmware/xilinx/zynqmp-crypto.c +150 -1
@@ -60,7 +60,7 @@ EXPORT_SYMBOL_GPL(zynqmp_pm_sha_hash);/** * xlnx_get_crypto_dev_data() - Get crypto dev data of platform -* @feature_map: List of available feature map of all platform +* @feature_map: List of available feature map of all platform * * Return: Returns crypto dev data, either address crypto dev or ERR PTR */
@@ -88,3 +88,152 @@ void *xlnx_get_crypto_dev_data(struct xlnx_feature *feature_map)return ERR_PTR (-ENODEV ); }EXPORT_SYMBOL_GPL (xlnx_get_crypto_dev_data); + +/** +* versal_pm_aes_key_write - Write AES key registers +* @keylen: Size of the input key to be written +* @keysrc: Key Source to be selected to which provided +* key should be updated +* @keyaddr: Address of a buffer which should contain the key +* to be written +* +* This function provides support to write AES volatile user keys. +* +* Return: Returns status, either success or error+reason +*/ +int versal_pm_aes_key_write (const u32 keylen, +const u32 keysrc,const u64 keyaddr) +{ +return zynqmp_pm_invoke_fn (XSECURE_API_AES_WRITE_KEY ,NULL ,4 , + keylen, keysrc, +lower_32_bits (keyaddr), +upper_32_bits (keyaddr)); +} +EXPORT_SYMBOL_GPL (versal_pm_aes_key_write); + +/** +* versal_pm_aes_key_zero - Zeroise AES User key registers +* @keysrc: Key Source to be selected to which provided +* key should be updated +* +* This function provides support to zeroise AES volatile user keys. +* +* Return: Returns status, either success or error+reason +*/ +int versal_pm_aes_key_zero (const u32 keysrc) +{ +return zynqmp_pm_invoke_fn (XSECURE_API_AES_KEY_ZERO ,NULL ,1 , keysrc); +} +EXPORT_SYMBOL_GPL (versal_pm_aes_key_zero); + +/** +* versal_pm_aes_op_init - Init AES operation +* @hw_req: AES op init structure address +* +* This function provides support to init AES operation. +* +* Return: Returns status, either success or error+reason +*/ +int versal_pm_aes_op_init (const u64 hw_req) +{ +return zynqmp_pm_invoke_fn (XSECURE_API_AES_OP_INIT ,NULL ,2 , +lower_32_bits (hw_req), +upper_32_bits (hw_req)); +} +EXPORT_SYMBOL_GPL (versal_pm_aes_op_init); + +/** +* versal_pm_aes_update_aad - AES update aad +* @aad_addr: AES aad address +* @aad_len: AES aad data length +* +* This function provides support to update AAD data. +* +* Return: Returns status, either success or error+reason +*/ +int versal_pm_aes_update_aad (const u64 aad_addr,const u32 aad_len) +{ +return zynqmp_pm_invoke_fn (XSECURE_API_AES_UPDATE_AAD ,NULL ,3 , +lower_32_bits (aad_addr), +upper_32_bits (aad_addr), + aad_len); +} +EXPORT_SYMBOL_GPL (versal_pm_aes_update_aad); + +/** +* versal_pm_aes_enc_update - Access AES hardware to encrypt the data using +* AES-GCM core. +* @in_params: Address of the AesParams structure +* @in_addr: Address of input buffer +* +* Return: Returns status, either success or error code. +*/ +int versal_pm_aes_enc_update (const u64 in_params,const u64 in_addr) +{ +return zynqmp_pm_invoke_fn (XSECURE_API_AES_ENCRYPT_UPDATE ,NULL ,4 , +lower_32_bits (in_params), +upper_32_bits (in_params), +lower_32_bits (in_addr), +upper_32_bits (in_addr)); +} +EXPORT_SYMBOL_GPL (versal_pm_aes_enc_update); + +/** +* versal_pm_aes_enc_final - Access AES hardware to store the GCM tag +* @gcm_addr: Address of the gcm tag +* +* Return: Returns status, either success or error code. +*/ +int versal_pm_aes_enc_final (const u64 gcm_addr) +{ +return zynqmp_pm_invoke_fn (XSECURE_API_AES_ENCRYPT_FINAL ,NULL ,2 , +lower_32_bits (gcm_addr), +upper_32_bits (gcm_addr)); +} +EXPORT_SYMBOL_GPL (versal_pm_aes_enc_final); + +/** +* versal_pm_aes_dec_update - Access AES hardware to decrypt the data using +* AES-GCM core. +* @in_params: Address of the AesParams structure +* @in_addr: Address of input buffer +* +* Return: Returns status, either success or error code. +*/ +int versal_pm_aes_dec_update (const u64 in_params,const u64 in_addr) +{ +return zynqmp_pm_invoke_fn (XSECURE_API_AES_DECRYPT_UPDATE ,NULL ,4 , +lower_32_bits (in_params), +upper_32_bits (in_params), +lower_32_bits (in_addr), +upper_32_bits (in_addr)); +} +EXPORT_SYMBOL_GPL (versal_pm_aes_dec_update); + +/** +* versal_pm_aes_dec_final - Access AES hardware to get the GCM tag +* @gcm_addr: Address of the gcm tag +* +* Return: Returns status, either success or error code. +*/ +int versal_pm_aes_dec_final (const u64 gcm_addr) +{ +return zynqmp_pm_invoke_fn (XSECURE_API_AES_DECRYPT_FINAL ,NULL ,2 , +lower_32_bits (gcm_addr), +upper_32_bits (gcm_addr)); +} +EXPORT_SYMBOL_GPL (versal_pm_aes_dec_final); + +/** +* versal_pm_aes_init - Init AES block +* +* This function initialise AES block. +* +* Return: Returns status, either success or error+reason +*/ +int versal_pm_aes_init (void ) +{ +return zynqmp_pm_invoke_fn (XSECURE_API_AES_INIT ,NULL ,0 ); +} +EXPORT_SYMBOL_GPL (versal_pm_aes_init); +
include/linux/firmware/xlnx-zynqmp-crypto.h +74 -2
98 lines not on this page · raw message
Signature · 1 line
2.49.1
PATCH 6/15 crypto: zynqmp-aes-gcm: Return linux error code instead of firmware error code
In reply to Harsh JainConvert FW error code to linux error code and remove dmesg error print for authentication tag mismatch failure.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +7 -7
@@ -85,7 +85,6 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)int ret;size_t dma_size;char *kbuf; -int err;if (tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY ) dma_size = req->cryptlen +ZYNQMP_AES_KEY_SIZE
@@ -132,23 +131,23 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)if (ret) {dev_err (dev,"ERROR: AES PM API failed\n" ); - err = ret; }else if (status) {switch (status) {case ZYNQMP_AES_GCM_TAG_MISMATCH_ERR : -dev_err (dev,"ERROR: Gcm Tag mismatch\n" ); + ret = -EBADMSG ;break ;case ZYNQMP_AES_WRONG_KEY_SRC_ERR : + ret = -EINVAL ;dev_err (dev,"ERROR: Wrong KeySrc, enable secure mode\n" );break ;case ZYNQMP_AES_PUF_NOT_PROGRAMMED : + ret = -EINVAL ;dev_err (dev,"ERROR: PUF is not registered\n" );break ;default : -dev_err (dev,"ERROR: Unknown error\n" ); + ret = -EINVAL ;break ; } - err = -status; }else {if (hwreq->op ==ZYNQMP_AES_ENCRYPT ) data_size = data_size +ZYNQMP_AES_AUTH_SIZE ;
@@ -157,7 +156,7 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)sg_copy_from_buffer (req->dst,sg_nents (req->dst), kbuf, data_size); - err =0 ; + ret =0 ; }if (kbuf) {
@@ -169,7 +168,8 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)dma_free_coherent (dev,sizeof (struct zynqmp_aead_hw_req ), hwreq, dma_addr_hw_req); } -return err; + +return ret; } static int zynqmp_fallback_check(struct zynqmp_aead_tfm_ctx *tfm_ctx,
Signature · 1 line
2.49.1
PATCH 8/15 crypto: zynqmp-aes-gcm: Avoid submitting fallback requests to engine
In reply to Harsh JainDon't enqueue requests which are supposed to fallback to s/w crypto.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 62 +++++++++++++++----------- 1 file changed, 35 insertions(+), 27 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +35 -27
@@ -204,31 +204,9 @@ static int zynqmp_handle_aes_req(struct crypto_engine *engine, {struct aead_request *areq = container_of(req, struct aead_request, base); -struct crypto_aead *aead =crypto_aead_reqtfm (req); -struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead); -struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (areq); -struct aead_request *subreq =aead_request_ctx (req); -int need_fallback;int err; - need_fallback =zynqmp_fallback_check (tfm_ctx, areq); - -if (need_fallback) { -aead_request_set_tfm (subreq, tfm_ctx->fbk_cipher); - -aead_request_set_callback (subreq, areq->base.flags, -NULL ,NULL ); -aead_request_set_crypt (subreq, areq->src, areq->dst, - areq->cryptlen, areq->iv); -aead_request_set_ad (subreq, areq->assoclen); -if (rq_ctx->op ==ZYNQMP_AES_ENCRYPT ) - err =crypto_aead_encrypt (subreq); -else - err =crypto_aead_decrypt (subreq); - }else { - err =zynqmp_aes_aead_cipher (areq); - } - + err =zynqmp_aes_aead_cipher (areq);local_bh_disable ();crypto_finalize_aead_request (engine, areq, err);local_bh_enable ();
@@ -281,26 +259,56 @@ static int zynqmp_aes_aead_setauthsize(struct crypto_aead *aead,static int zynqmp_aes_aead_encrypt (struct aead_request *req) { -struct zynqmp_aead_drv_ctx *drv_ctx; +struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); +struct aead_request *subreq =aead_request_ctx (req);struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead);struct aead_alg *alg =crypto_aead_alg (aead); -struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); +struct zynqmp_aead_drv_ctx *drv_ctx; +int err; rq_ctx->op =ZYNQMP_AES_ENCRYPT ; drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base); + err =zynqmp_fallback_check (tfm_ctx, req); +if (err) { +aead_request_set_tfm (subreq, tfm_ctx->fbk_cipher); +aead_request_set_callback (subreq, req->base.flags, +NULL ,NULL ); +aead_request_set_crypt (subreq, req->src, req->dst, + req->cryptlen, req->iv); +aead_request_set_ad (subreq, req->assoclen); + err =crypto_aead_encrypt (subreq); + +return err; + }return crypto_transfer_aead_request_to_engine (drv_ctx->engine, req); }static int zynqmp_aes_aead_decrypt (struct aead_request *req) { -struct zynqmp_aead_drv_ctx *drv_ctx; +struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); +struct aead_request *subreq =aead_request_ctx (req);struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead);struct aead_alg *alg =crypto_aead_alg (aead); -struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); +struct zynqmp_aead_drv_ctx *drv_ctx; +int err; rq_ctx->op =ZYNQMP_AES_DECRYPT ; drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base); + err =zynqmp_fallback_check (tfm_ctx, req); +if (err) { +aead_request_set_tfm (subreq, tfm_ctx->fbk_cipher); +aead_request_set_callback (subreq, req->base.flags, +NULL ,NULL ); +aead_request_set_crypt (subreq, req->src, req->dst, + req->cryptlen, req->iv); +aead_request_set_ad (subreq, req->assoclen); + err =crypto_aead_decrypt (subreq); + +return err; + }return crypto_transfer_aead_request_to_engine (drv_ctx->engine, req); }
Signature · 1 line
2.49.1
PATCH 10/15 crypto: zynqmp-aes-gcm: Change coherent DMA to streaming DMA API
In reply to Harsh JainUpdate the driver to use streaming DMA API.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 92 ++++++++++++++++---------- 1 file changed, 57 insertions(+), 35 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +57 -35
@@ -65,8 +65,8 @@ struct zynqmp_aead_hw_req {struct zynqmp_aead_tfm_ctx {struct device *dev; -u8 key[ZYNQMP_AES_KEY_SIZE ]; -u8 *iv; +dma_addr_t key_dma_addr; +u8 *key;u32 keylen;u32 authsize;u8 keysrc;
@@ -82,39 +82,38 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)struct crypto_aead *aead =crypto_aead_reqtfm (req);struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead);struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); +dma_addr_t dma_addr_data, dma_addr_hw_req;struct device *dev = tfm_ctx->dev;struct zynqmp_aead_hw_req *hwreq; -dma_addr_t dma_addr_data, dma_addr_hw_req;unsigned int data_size;unsigned int status;int ret;size_t dma_size; +void *dmabuf;char *kbuf; -if (tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY ) - dma_size = req->cryptlen +ZYNQMP_AES_KEY_SIZE - +GCM_AES_IV_SIZE ; -else - dma_size = req->cryptlen +GCM_AES_IV_SIZE ; - - kbuf =dma_alloc_coherent (dev, dma_size, &dma_addr_data,GFP_KERNEL ); + dma_size = req->cryptlen +ZYNQMP_AES_AUTH_SIZE ; + kbuf =kmalloc (dma_size,GFP_KERNEL );if (!kbuf)return -ENOMEM ; - hwreq =dma_alloc_coherent (dev,sizeof (struct zynqmp_aead_hw_req ), - &dma_addr_hw_req,GFP_KERNEL ); -if (!hwreq) { -dma_free_coherent (dev, dma_size, kbuf, dma_addr_data); + dmabuf =kmalloc (sizeof (*hwreq) +GCM_AES_IV_SIZE ,GFP_KERNEL ); +if (!dmabuf) { +kfree (kbuf);return -ENOMEM ; } - + hwreq = dmabuf; data_size = req->cryptlen;scatterwalk_map_and_copy (kbuf, req->src,0 , req->cryptlen,0 ); -memcpy (kbuf + data_size, req->iv,GCM_AES_IV_SIZE ); +memcpy (dmabuf +sizeof (struct zynqmp_aead_hw_req ), req->iv,GCM_AES_IV_SIZE ); + dma_addr_data =dma_map_single (dev, kbuf, dma_size,DMA_BIDIRECTIONAL ); +if (unlikely (dma_mapping_error (dev, dma_addr_data))) { + ret = -ENOMEM ; + goto freemem; + } hwreq->src = dma_addr_data; hwreq->dst = dma_addr_data; - hwreq->iv = hwreq->src + data_size; hwreq->keysrc = tfm_ctx->keysrc; hwreq->op = rq_ctx->op;
@@ -123,17 +122,26 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)else hwreq->size = data_size -ZYNQMP_AES_AUTH_SIZE ; -if (hwreq->keysrc ==ZYNQMP_AES_KUP_KEY ) { -memcpy (kbuf + data_size +GCM_AES_IV_SIZE , - tfm_ctx->key,ZYNQMP_AES_KEY_SIZE ); - - hwreq->key = hwreq->src + data_size +GCM_AES_IV_SIZE ; - }else { +if (hwreq->keysrc ==ZYNQMP_AES_KUP_KEY ) + hwreq->key = tfm_ctx->key_dma_addr; +else hwreq->key =0 ; - } + dma_addr_hw_req =dma_map_single (dev, dmabuf,sizeof (struct zynqmp_aead_hw_req ) + +GCM_AES_IV_SIZE , +DMA_TO_DEVICE ); +if (unlikely (dma_mapping_error (dev, dma_addr_hw_req))) { + ret = -ENOMEM ; +dma_unmap_single (dev, dma_addr_data, dma_size,DMA_BIDIRECTIONAL ); + goto freemem; + } + hwreq->iv = dma_addr_hw_req +sizeof (struct zynqmp_aead_hw_req ); +dma_sync_single_for_device (dev, dma_addr_hw_req,sizeof (struct zynqmp_aead_hw_req ) + +GCM_AES_IV_SIZE ,DMA_TO_DEVICE ); ret =zynqmp_pm_aes_engine (dma_addr_hw_req, &status); - +dma_unmap_single (dev, dma_addr_hw_req,sizeof (struct zynqmp_aead_hw_req ) +GCM_AES_IV_SIZE , +DMA_TO_DEVICE ); +dma_unmap_single (dev, dma_addr_data, dma_size,DMA_BIDIRECTIONAL );if (ret) {dev_err (dev,"ERROR: AES PM API failed\n" ); }else if (status) {
@@ -164,15 +172,11 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req) ret =0 ; } -if (kbuf) { -memzero_explicit (kbuf, dma_size); -dma_free_coherent (dev, dma_size, kbuf, dma_addr_data); - } -if (hwreq) { -memzero_explicit (hwreq,sizeof (struct zynqmp_aead_hw_req )); -dma_free_coherent (dev,sizeof (struct zynqmp_aead_hw_req ), - hwreq, dma_addr_hw_req); - } +freemem: +memzero_explicit (kbuf, dma_size); +kfree (kbuf); +memzero_explicit (dmabuf,sizeof (struct zynqmp_aead_hw_req ) +GCM_AES_IV_SIZE ); +kfree (dmabuf);return ret; }
@@ -243,6 +247,9 @@ static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key,if (keylen ==ZYNQMP_AES_KEY_SIZE && tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY ) { tfm_ctx->keylen = keylen;memcpy (tfm_ctx->key, key, keylen); +dma_sync_single_for_device (tfm_ctx->dev, tfm_ctx->key_dma_addr, +ZYNQMP_AES_KEY_SIZE , +DMA_TO_DEVICE ); }else if (tfm_ctx->keysrc !=ZYNQMP_AES_KUP_KEY ) {return -EINVAL ; }
@@ -359,7 +366,20 @@ static int zynqmp_aes_aead_init(struct crypto_aead *aead) __func__, drv_ctx->aead.base.base.cra_name);return PTR_ERR (tfm_ctx->fbk_cipher); } - + tfm_ctx->key =kmalloc (ZYNQMP_AES_KEY_SIZE ,GFP_KERNEL ); +if (!tfm_ctx->key) { +crypto_free_aead (tfm_ctx->fbk_cipher); +return -ENOMEM ; + } + tfm_ctx->key_dma_addr =dma_map_single (tfm_ctx->dev, tfm_ctx->key, +ZYNQMP_AES_KEY_SIZE , +DMA_TO_DEVICE ); +if (unlikely (dma_mapping_error (tfm_ctx->dev, tfm_ctx->key_dma_addr))) { +kfree (tfm_ctx->key); +crypto_free_aead (tfm_ctx->fbk_cipher); + tfm_ctx->fbk_cipher =NULL ; +return -ENOMEM ; + } crypto_aead_set_reqsize(aead, max(sizeof(struct zynqmp_aead_req_ctx), sizeof(struct aead_request) +
@@ -373,6 +393,8 @@ static void zynqmp_aes_aead_exit(struct crypto_aead *aead)struct zynqmp_aead_tfm_ctx *tfm_ctx = (struct zynqmp_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +dma_unmap_single (tfm_ctx->dev, tfm_ctx->key_dma_addr,ZYNQMP_AES_KEY_SIZE ,DMA_TO_DEVICE ); +kfree (tfm_ctx->key);if (tfm_ctx->fbk_cipher) {crypto_free_aead (tfm_ctx->fbk_cipher); tfm_ctx->fbk_cipher =NULL ;
Signature · 1 line
2.49.1
PATCH 13/15 crypto: xilinx: cleanup: Remove un-necessary typecast operation
In reply to Harsh JainReturn type of crypto_tfm_ctx() is void *. Remove explicit type cast.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +3 -6
@@ -267,8 +267,7 @@ static int xilinx_aes_aead_setauthsize(struct crypto_aead *aead, unsigned int authsize) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct xilinx_aead_tfm_ctx *tfm_ctx = - (struct xilinx_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_tfm_ctx (tfm); tfm_ctx->authsize = authsize;return crypto_aead_setauthsize (tfm_ctx->fbk_cipher, authsize);
@@ -345,8 +344,7 @@ static int zynqmp_aes_aead_decrypt(struct aead_request *req)static int xilinx_aes_aead_init (struct crypto_aead *aead) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct xilinx_aead_tfm_ctx *tfm_ctx = - (struct xilinx_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_tfm_ctx (tfm);struct xilinx_aead_drv_ctx *drv_ctx;struct aead_alg *alg =crypto_aead_alg (aead);
@@ -388,8 +386,7 @@ static int xilinx_aes_aead_init(struct crypto_aead *aead)static void xilinx_aes_aead_exit (struct crypto_aead *aead) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct xilinx_aead_tfm_ctx *tfm_ctx = - (struct xilinx_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_tfm_ctx (tfm);dma_unmap_single (tfm_ctx->dev, tfm_ctx->key_dma_addr,AES_KEYSIZE_256 ,DMA_TO_DEVICE );kfree (tfm_ctx->key);
Signature · 1 line
2.49.1
PATCH 12/15 crypto: xilinx: Replace zynqmp prefix with xilinx
In reply to Harsh JainReplace zynqmp with xilinx to have more generic name.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 130 ++++++++++++------------- 1 file changed, 64 insertions(+), 66 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +64 -66
@@ -19,10 +19,8 @@#include <linux/string.h> #define ZYNQMP_DMA_BIT_MASK 32U - -#define ZYNQMP_AES_KEY_SIZE AES_KEYSIZE_256 -#define ZYNQMP_AES_AUTH_SIZE 16U -#define ZYNQMP_AES_BLK_SIZE 1U +#define XILINX_AES_AUTH_SIZE 16U +#define XILINX_AES_BLK_SIZE 1U#define ZYNQMP_AES_MIN_INPUT_BLK_SIZE 4U#define ZYNQMP_AES_WORD_LEN 4U
@@ -31,9 +29,9 @@#define ZYNQMP_AES_PUF_NOT_PROGRAMMED 0xE300#define XILINX_KEY_MAGIC 0x3EA0 -enum zynqmp_aead_op { -ZYNQMP_AES_DECRYPT =0 , -ZYNQMP_AES_ENCRYPT +enum xilinx_aead_op { +XILINX_AES_DECRYPT =0 , +XILINX_AES_ENCRYPT };enum zynqmp_aead_keysrc {
@@ -42,7 +40,7 @@ enum zynqmp_aead_keysrc {ZYNQMP_AES_PUF_KEY }; -struct zynqmp_aead_drv_ctx { +struct xilinx_aead_drv_ctx {struct aead_engine_alg aead;struct device *dev;struct crypto_engine *engine;
@@ -63,7 +61,7 @@ struct zynqmp_aead_hw_req {u64 keysrc; }; -struct zynqmp_aead_tfm_ctx { +struct xilinx_aead_tfm_ctx {struct device *dev;dma_addr_t key_dma_addr;u8 *key;
@@ -74,13 +72,13 @@ struct zynqmp_aead_tfm_ctx { };struct zynqmp_aead_req_ctx { -enum zynqmp_aead_op op; +enum xilinx_aead_op op; };static int zynqmp_aes_aead_cipher (struct aead_request *req) {struct crypto_aead *aead =crypto_aead_reqtfm (req); -struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead);struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req);dma_addr_t dma_addr_data, dma_addr_hw_req;struct device *dev = tfm_ctx->dev;
@@ -92,7 +90,7 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)void *dmabuf;char *kbuf; - dma_size = req->cryptlen +ZYNQMP_AES_AUTH_SIZE ; + dma_size = req->cryptlen +XILINX_AES_AUTH_SIZE ; kbuf =kmalloc (dma_size,GFP_KERNEL );if (!kbuf)return -ENOMEM ;
@@ -117,10 +115,10 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req) hwreq->keysrc = tfm_ctx->keysrc; hwreq->op = rq_ctx->op; -if (hwreq->op ==ZYNQMP_AES_ENCRYPT ) +if (hwreq->op ==XILINX_AES_ENCRYPT ) hwreq->size = data_size;else - hwreq->size = data_size -ZYNQMP_AES_AUTH_SIZE ; + hwreq->size = data_size -XILINX_AES_AUTH_SIZE ;if (hwreq->keysrc ==ZYNQMP_AES_KUP_KEY ) hwreq->key = tfm_ctx->key_dma_addr;
@@ -162,10 +160,10 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)break ; } }else { -if (hwreq->op ==ZYNQMP_AES_ENCRYPT ) +if (hwreq->op ==XILINX_AES_ENCRYPT ) data_size = data_size +crypto_aead_authsize (aead);else - data_size = data_size -ZYNQMP_AES_AUTH_SIZE ; + data_size = data_size -XILINX_AES_AUTH_SIZE ;sg_copy_from_buffer (req->dst,sg_nents (req->dst), kbuf, data_size);
@@ -181,12 +179,12 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)return ret; } -static int zynqmp_fallback_check (struct zynqmp_aead_tfm_ctx *tfm_ctx, +static int zynqmp_fallback_check (struct xilinx_aead_tfm_ctx *tfm_ctx,struct aead_request *req) {struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); -if (tfm_ctx->authsize !=ZYNQMP_AES_AUTH_SIZE && rq_ctx->op ==ZYNQMP_AES_DECRYPT ) +if (tfm_ctx->authsize !=XILINX_AES_AUTH_SIZE && rq_ctx->op ==XILINX_AES_DECRYPT )return 1 ; if (req->assoclen != 0 ||
@@ -199,14 +197,14 @@ static int zynqmp_fallback_check(struct zynqmp_aead_tfm_ctx *tfm_ctx,if ((req->cryptlen %ZYNQMP_AES_WORD_LEN ) !=0 )return 1 ; -if (rq_ctx->op ==ZYNQMP_AES_DECRYPT && - req->cryptlen <=ZYNQMP_AES_AUTH_SIZE ) +if (rq_ctx->op ==XILINX_AES_DECRYPT && + req->cryptlen <=XILINX_AES_AUTH_SIZE )return 1 ;return 0 ; } -static int zynqmp_handle_aes_req (struct crypto_engine *engine,void *req) +static int xilinx_handle_aes_req (struct crypto_engine *engine,void *req) {struct aead_request *areq = container_of(req, struct aead_request, base);
@@ -224,7 +222,7 @@ static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key, unsigned int keylen) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_tfm_ctx (tfm); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_tfm_ctx (tfm);struct xilinx_hwkey_info hwkey;unsigned char keysrc;int err;
@@ -244,11 +242,11 @@ static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key,return -EINVAL ; } -if (keylen ==ZYNQMP_AES_KEY_SIZE && tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY ) { +if (keylen ==AES_KEYSIZE_256 && tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY ) { tfm_ctx->keylen = keylen;memcpy (tfm_ctx->key, key, keylen);dma_sync_single_for_device (tfm_ctx->dev, tfm_ctx->key_dma_addr, -ZYNQMP_AES_KEY_SIZE , +AES_KEYSIZE_256 ,DMA_TO_DEVICE ); }else if (tfm_ctx->keysrc !=ZYNQMP_AES_KUP_KEY ) {return -EINVAL ;
@@ -265,12 +263,12 @@ static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key,return err; } -static int zynqmp_aes_aead_setauthsize (struct crypto_aead *aead, +static int xilinx_aes_aead_setauthsize (struct crypto_aead *aead,unsigned int authsize) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct zynqmp_aead_tfm_ctx *tfm_ctx = - (struct zynqmp_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +struct xilinx_aead_tfm_ctx *tfm_ctx = + (struct xilinx_aead_tfm_ctx *)crypto_tfm_ctx (tfm); tfm_ctx->authsize = authsize;return crypto_aead_setauthsize (tfm_ctx->fbk_cipher, authsize);
@@ -281,17 +279,17 @@ static int zynqmp_aes_aead_encrypt(struct aead_request *req)struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req);struct aead_request *subreq =aead_request_ctx (req);struct crypto_aead *aead =crypto_aead_reqtfm (req); -struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead);struct aead_alg *alg =crypto_aead_alg (aead); -struct zynqmp_aead_drv_ctx *drv_ctx; +struct xilinx_aead_drv_ctx *drv_ctx;int err; - drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base); + drv_ctx = container_of(alg, struct xilinx_aead_drv_ctx, aead.base);if (tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY && tfm_ctx->keylen ==sizeof (struct xilinx_hwkey_info ))return -EINVAL ; - rq_ctx->op =ZYNQMP_AES_ENCRYPT ; + rq_ctx->op =XILINX_AES_ENCRYPT ; err =zynqmp_fallback_check (tfm_ctx, req);if (err && tfm_ctx->keysrc !=ZYNQMP_AES_KUP_KEY )return -EOPNOTSUPP ;
@@ -316,13 +314,13 @@ static int zynqmp_aes_aead_decrypt(struct aead_request *req)struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req);struct aead_request *subreq =aead_request_ctx (req);struct crypto_aead *aead =crypto_aead_reqtfm (req); -struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead);struct aead_alg *alg =crypto_aead_alg (aead); -struct zynqmp_aead_drv_ctx *drv_ctx; +struct xilinx_aead_drv_ctx *drv_ctx;int err; - rq_ctx->op =ZYNQMP_AES_DECRYPT ; - drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base); + rq_ctx->op =XILINX_AES_DECRYPT ; + drv_ctx = container_of(alg, struct xilinx_aead_drv_ctx, aead.base);if (tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY && tfm_ctx->keylen ==sizeof (struct xilinx_hwkey_info ))return -EINVAL ;
@@ -344,15 +342,15 @@ static int zynqmp_aes_aead_decrypt(struct aead_request *req)return crypto_transfer_aead_request_to_engine (drv_ctx->engine, req); } -static int zynqmp_aes_aead_init (struct crypto_aead *aead) +static int xilinx_aes_aead_init (struct crypto_aead *aead) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct zynqmp_aead_tfm_ctx *tfm_ctx = - (struct zynqmp_aead_tfm_ctx *)crypto_tfm_ctx (tfm); -struct zynqmp_aead_drv_ctx *drv_ctx; +struct xilinx_aead_tfm_ctx *tfm_ctx = + (struct xilinx_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +struct xilinx_aead_drv_ctx *drv_ctx;struct aead_alg *alg =crypto_aead_alg (aead); - drv_ctx = container_of(alg, struct zynqmp_aead_drv_ctx, aead.base); + drv_ctx = container_of(alg, struct xilinx_aead_drv_ctx, aead.base); tfm_ctx->dev = drv_ctx->dev; tfm_ctx->keylen =0 ; tfm_ctx->keysrc =ZYNQMP_AES_KUP_KEY ;
@@ -366,13 +364,13 @@ static int zynqmp_aes_aead_init(struct crypto_aead *aead) __func__, drv_ctx->aead.base.base.cra_name);return PTR_ERR (tfm_ctx->fbk_cipher); } - tfm_ctx->key =kmalloc (ZYNQMP_AES_KEY_SIZE ,GFP_KERNEL ); + tfm_ctx->key =kmalloc (AES_KEYSIZE_256 ,GFP_KERNEL );if (!tfm_ctx->key) {crypto_free_aead (tfm_ctx->fbk_cipher);return -ENOMEM ; } tfm_ctx->key_dma_addr =dma_map_single (tfm_ctx->dev, tfm_ctx->key, -ZYNQMP_AES_KEY_SIZE , +AES_KEYSIZE_256 ,DMA_TO_DEVICE );if (unlikely (dma_mapping_error (tfm_ctx->dev, tfm_ctx->key_dma_addr))) {kfree (tfm_ctx->key);
@@ -387,31 +385,31 @@ static int zynqmp_aes_aead_init(struct crypto_aead *aead)return 0 ; } -static void zynqmp_aes_aead_exit (struct crypto_aead *aead) +static void xilinx_aes_aead_exit (struct crypto_aead *aead) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct zynqmp_aead_tfm_ctx *tfm_ctx = - (struct zynqmp_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +struct xilinx_aead_tfm_ctx *tfm_ctx = + (struct xilinx_aead_tfm_ctx *)crypto_tfm_ctx (tfm); -dma_unmap_single (tfm_ctx->dev, tfm_ctx->key_dma_addr,ZYNQMP_AES_KEY_SIZE ,DMA_TO_DEVICE ); +dma_unmap_single (tfm_ctx->dev, tfm_ctx->key_dma_addr,AES_KEYSIZE_256 ,DMA_TO_DEVICE );kfree (tfm_ctx->key);if (tfm_ctx->fbk_cipher) {crypto_free_aead (tfm_ctx->fbk_cipher); tfm_ctx->fbk_cipher =NULL ; } -memzero_explicit (tfm_ctx,sizeof (struct zynqmp_aead_tfm_ctx )); +memzero_explicit (tfm_ctx,sizeof (struct xilinx_aead_tfm_ctx )); } -static struct zynqmp_aead_drv_ctx zynqmp_aes_drv_ctx = { +static struct xilinx_aead_drv_ctx zynqmp_aes_drv_ctx = { .aead.base = { .setkey = zynqmp_aes_aead_setkey, - .setauthsize = zynqmp_aes_aead_setauthsize, + .setauthsize = xilinx_aes_aead_setauthsize, .encrypt = zynqmp_aes_aead_encrypt, .decrypt = zynqmp_aes_aead_decrypt, - .init = zynqmp_aes_aead_init, - .exit = zynqmp_aes_aead_exit, + .init = xilinx_aes_aead_init, + .exit = xilinx_aes_aead_exit, .ivsize =GCM_AES_IV_SIZE , - .maxauthsize =ZYNQMP_AES_AUTH_SIZE , + .maxauthsize =XILINX_AES_AUTH_SIZE , .base = { .cra_name ="gcm(aes)" , .cra_driver_name ="xilinx-zynqmp-aes-gcm" ,
@@ -421,13 +419,13 @@ static struct zynqmp_aead_drv_ctx zynqmp_aes_drv_ctx = {CRYPTO_ALG_ALLOCATES_MEMORY |CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_NEED_FALLBACK, - .cra_blocksize = ZYNQMP_AES_BLK_SIZE, - .cra_ctxsize = sizeof(struct zynqmp_aead_tfm_ctx), + .cra_blocksize = XILINX_AES_BLK_SIZE, + .cra_ctxsize = sizeof(struct xilinx_aead_tfm_ctx), .cra_module = THIS_MODULE, } }, .aead.op = { - .do_one_request = zynqmp_handle_aes_req, + .do_one_request = xilinx_handle_aes_req, }, };
@@ -440,9 +438,9 @@ static struct xlnx_feature aes_feature_map[] = { {/* sentinel */ } }; -static int zynqmp_aes_aead_probe (struct platform_device *pdev) +static int xilinx_aes_aead_probe (struct platform_device *pdev) { -struct zynqmp_aead_drv_ctx *aes_drv_ctx; +struct xilinx_aead_drv_ctx *aes_drv_ctx;struct device *dev = &pdev->dev;int err;
@@ -493,18 +491,18 @@ static int zynqmp_aes_aead_probe(struct platform_device *pdev)return err; } -static void zynqmp_aes_aead_remove (struct platform_device *pdev) +static void xilinx_aes_aead_remove (struct platform_device *pdev) { -struct zynqmp_aead_drv_ctx *aes_drv_ctx; +struct xilinx_aead_drv_ctx *aes_drv_ctx; aes_drv_ctx =platform_get_drvdata (pdev);crypto_engine_exit (aes_drv_ctx->engine);crypto_engine_unregister_aead (&aes_drv_ctx->aead); } -static struct platform_driver zynqmp_aes_driver = { - .probe = zynqmp_aes_aead_probe, - .remove = zynqmp_aes_aead_remove, +static struct platform_driver xilinx_aes_driver = { + .probe = xilinx_aes_aead_probe, + .remove = xilinx_aes_aead_remove, .driver = { .name ="zynqmp-aes" , },
@@ -516,15 +514,15 @@ static int __init aes_driver_init(void) {int ret; - ret =platform_driver_register (&zynqmp_aes_driver); + ret =platform_driver_register (&xilinx_aes_driver);if (ret)return ret; - platform_dev =platform_device_register_simple (zynqmp_aes_driver.driver.name, + platform_dev =platform_device_register_simple (xilinx_aes_driver.driver.name,0 ,NULL ,0 );if (IS_ERR (platform_dev)) { ret =PTR_ERR (platform_dev); -platform_driver_unregister (&zynqmp_aes_driver); +platform_driver_unregister (&xilinx_aes_driver); }return ret;
@@ -533,7 +531,7 @@ static int __init aes_driver_init(void) static void __exit aes_driver_exit(void) {platform_device_unregister (platform_dev); -platform_driver_unregister (&zynqmp_aes_driver); +platform_driver_unregister (&xilinx_aes_driver); }module_init (aes_driver_init);
Signature · 1 line
2.49.1
PATCH 14/15 crypto: zynqmp-aes-gcm: Save dma bit mask value in driver context
In reply to Harsh JainSave dma mask in driver context. It will allow upcoming Versal device to use different value.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +4 -1
@@ -44,6 +44,7 @@ struct xilinx_aead_drv_ctx {struct aead_engine_alg aead;struct device *dev;struct crypto_engine *engine; +u8 dma_bit_mask; };struct xilinx_hwkey_info {
@@ -424,6 +425,7 @@ static struct xilinx_aead_drv_ctx zynqmp_aes_drv_ctx = { .aead.op = { .do_one_request = xilinx_handle_aes_req, }, + .dma_bit_mask = ZYNQMP_DMA_BIT_MASK, };static struct xlnx_feature aes_feature_map[] = {
@@ -455,7 +457,8 @@ static int xilinx_aes_aead_probe(struct platform_device *pdev)return -ENODEV ;platform_set_drvdata (pdev, aes_drv_ctx); - err =dma_set_mask_and_coherent (dev,DMA_BIT_MASK (ZYNQMP_DMA_BIT_MASK )); + + err =dma_set_mask_and_coherent (dev,DMA_BIT_MASK (aes_drv_ctx->dma_bit_mask));if (err <0 ) {dev_err (dev,"No usable DMA configuration\n" );return err;
Signature · 1 line
2.49.1
PATCH 15/15 crypto: xilinx: Add gcm(aes) support for AMD/Xilinx Versal device
In reply to Harsh JainAdd gcm(aes) algorithm support for AMD/Xilinx Versal devices.
--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 380 ++++++++++++++++++++++++- 1 file changed, 377 insertions(+), 3 deletions(-)
drivers/crypto/xilinx/zynqmp-aes-gcm.c +377 -3
@@ -1,7 +1,8 @@// SPDX-License-Identifier: GPL-2.0 /* * Xilinx ZynqMP AES Driver. -* Copyright (c) 2020 Xilinx Inc. +* Copyright (C) 2020-2022 Xilinx Inc. +* Copyright (C) 2022-2025 Advanced Micro Devices, Inc. */ #include <crypto/aes.h>
@@ -19,11 +20,13 @@#include <linux/string.h> #define ZYNQMP_DMA_BIT_MASK 32U +#define VERSAL_DMA_BIT_MASK 64U#define XILINX_AES_AUTH_SIZE 16U#define XILINX_AES_BLK_SIZE 1U#define ZYNQMP_AES_MIN_INPUT_BLK_SIZE 4U#define ZYNQMP_AES_WORD_LEN 4U +#define VERSAL_AES_QWORD_LEN 16U#define ZYNQMP_AES_GCM_TAG_MISMATCH_ERR 0x01#define ZYNQMP_AES_WRONG_KEY_SRC_ERR 0x13#define ZYNQMP_AES_PUF_NOT_PROGRAMMED 0xE300
@@ -44,7 +47,9 @@ struct xilinx_aead_drv_ctx {struct aead_engine_alg aead;struct device *dev;struct crypto_engine *engine; +u8 keysrc;u8 dma_bit_mask; +int (*aes_aead_cipher)(struct aead_request *areq); };struct xilinx_hwkey_info {
@@ -76,6 +81,54 @@ struct zynqmp_aead_req_ctx {enum xilinx_aead_op op; }; +enum versal_aead_keysrc { +VERSAL_AES_BBRAM_KEY =0 , +VERSAL_AES_BBRAM_RED_KEY , +VERSAL_AES_BH_KEY , +VERSAL_AES_BH_RED_KEY , +VERSAL_AES_EFUSE_KEY , +VERSAL_AES_EFUSE_RED_KEY , +VERSAL_AES_EFUSE_USER_KEY_0 , +VERSAL_AES_EFUSE_USER_KEY_1 , +VERSAL_AES_EFUSE_USER_RED_KEY_0 , +VERSAL_AES_EFUSE_USER_RED_KEY_1 , +VERSAL_AES_KUP_KEY , +VERSAL_AES_PUF_KEY , +VERSAL_AES_USER_KEY_0 , +VERSAL_AES_USER_KEY_1 , +VERSAL_AES_USER_KEY_2 , +VERSAL_AES_USER_KEY_3 , +VERSAL_AES_USER_KEY_4 , +VERSAL_AES_USER_KEY_5 , +VERSAL_AES_USER_KEY_6 , +VERSAL_AES_USER_KEY_7 , +VERSAL_AES_EXPANDED_KEYS , +VERSAL_AES_ALL_KEYS , +}; + +enum versal_aead_op { +VERSAL_AES_ENCRYPT =0 , +VERSAL_AES_DECRYPT +}; + +enum versal_aes_keysize { +HW_AES_KEY_SIZE_128 =0 , +HW_AES_KEY_SIZE_256 =2 , +}; + +struct versal_init_ops { +u64 iv; +u32 op; +u32 keysrc; +u32 size; +}; + +struct versal_in_params { +u64 in_data_addr; +u32 size; +u32 is_last; +}; +static int zynqmp_aes_aead_cipher (struct aead_request *req) {struct crypto_aead *aead =crypto_aead_reqtfm (req);
@@ -180,6 +233,141 @@ static int zynqmp_aes_aead_cipher(struct aead_request *req)return ret; } +static int versal_aes_aead_cipher (struct aead_request *req) +{ +struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead); +struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); +dma_addr_t dma_addr_data, dma_addr_hw_req, dma_addr_in; +u32 total_len = req->assoclen + req->cryptlen; +struct device *dev = tfm_ctx->dev; +struct versal_init_ops *hwreq; +struct versal_in_params *in; +u32 gcm_offset, out_len; +size_t dmabuf_size; +size_t kbuf_size; +void *dmabuf; +char *kbuf; +int ret; + + kbuf_size = total_len +XILINX_AES_AUTH_SIZE ; + kbuf =kmalloc (kbuf_size,GFP_KERNEL ); +if (unlikely (!kbuf)) { + ret = -ENOMEM ; + goto err; + } + dmabuf_size =sizeof (struct versal_init_ops ) + +sizeof (struct versal_in_params ) + +GCM_AES_IV_SIZE ; + dmabuf =kmalloc (dmabuf_size,GFP_KERNEL ); +if (unlikely (!dmabuf)) { + ret = -ENOMEM ; + goto buf1_free; + } + + dma_addr_hw_req =dma_map_single (dev, dmabuf, dmabuf_size,DMA_BIDIRECTIONAL ); +if (unlikely (dma_mapping_error (dev, dma_addr_hw_req))) { + ret = -ENOMEM ; + goto buf2_free; + } +scatterwalk_map_and_copy (kbuf, req->src,0 , total_len,0 ); + dma_addr_data =dma_map_single (dev, kbuf, kbuf_size,DMA_BIDIRECTIONAL ); +if (unlikely (dma_mapping_error (dev, dma_addr_data))) { +dma_unmap_single (dev, dma_addr_hw_req, dmabuf_size,DMA_BIDIRECTIONAL ); + ret = -ENOMEM ; + goto buf2_free; + } + hwreq = dmabuf; + in = dmabuf +sizeof (struct versal_init_ops ); +memcpy (dmabuf +sizeof (struct versal_init_ops ) + +sizeof (struct versal_in_params ), req->iv,GCM_AES_IV_SIZE ); + hwreq->iv = dma_addr_hw_req +sizeof (struct versal_init_ops ) + +sizeof (struct versal_in_params ); + hwreq->keysrc = tfm_ctx->keysrc; + dma_addr_in = dma_addr_hw_req +sizeof (struct versal_init_ops ); +if (rq_ctx->op ==XILINX_AES_ENCRYPT ) { + hwreq->op =VERSAL_AES_ENCRYPT ; + out_len = total_len +crypto_aead_authsize (aead); + in->size = req->cryptlen; + }else { + hwreq->op =VERSAL_AES_DECRYPT ; + out_len = total_len -XILINX_AES_AUTH_SIZE ; + in->size = req->cryptlen -XILINX_AES_AUTH_SIZE ; + } + +if (tfm_ctx->keylen ==AES_KEYSIZE_128 ) + hwreq->size =HW_AES_KEY_SIZE_128 ; +else + hwreq->size =HW_AES_KEY_SIZE_256 ; + +/* Request aes key write for volatile user keys */ +if (hwreq->keysrc >=VERSAL_AES_USER_KEY_0 && hwreq->keysrc <=VERSAL_AES_USER_KEY_7 ) { + ret =versal_pm_aes_key_write (hwreq->size, hwreq->keysrc, + tfm_ctx->key_dma_addr); +if (ret) + goto unmap; + } + + in->in_data_addr = dma_addr_data + req->assoclen; + in->is_last =1 ; + gcm_offset = req->assoclen + in->size; +dma_sync_single_for_device (dev, dma_addr_hw_req, dmabuf_size,DMA_BIDIRECTIONAL ); + ret =versal_pm_aes_op_init (dma_addr_hw_req); +if (ret) + goto clearkey; + +if (req->assoclen >0 ) { +/* Currently GMAC is OFF by default */ + ret =versal_pm_aes_update_aad (dma_addr_data, req->assoclen); +if (ret) + goto clearkey; + } +if (rq_ctx->op ==XILINX_AES_ENCRYPT ) { + ret =versal_pm_aes_enc_update (dma_addr_in, + dma_addr_data + req->assoclen); +if (ret) + goto clearkey; + + ret =versal_pm_aes_enc_final (dma_addr_data + gcm_offset); +if (ret) + goto clearkey; + }else { + ret =versal_pm_aes_dec_update (dma_addr_in, + dma_addr_data + req->assoclen); +if (ret) + goto clearkey; + + ret =versal_pm_aes_dec_final (dma_addr_data + gcm_offset); +if (ret) { + ret = -EBADMSG ; + goto clearkey; + } + } +dma_unmap_single (dev, dma_addr_data, kbuf_size,DMA_BIDIRECTIONAL ); +dma_unmap_single (dev, dma_addr_hw_req, dmabuf_size,DMA_BIDIRECTIONAL ); +sg_copy_from_buffer (req->dst,sg_nents (req->dst), + kbuf, out_len); + dma_addr_data =0 ; + dma_addr_hw_req =0 ; + +clearkey: +if (hwreq->keysrc >=VERSAL_AES_USER_KEY_0 && hwreq->keysrc <=VERSAL_AES_USER_KEY_7 ) +versal_pm_aes_key_zero (hwreq->keysrc); +unmap: +if (unlikely (dma_addr_data)) +dma_unmap_single (dev, dma_addr_data, kbuf_size,DMA_BIDIRECTIONAL ); +if (unlikely (dma_addr_hw_req)) +dma_unmap_single (dev, dma_addr_hw_req, dmabuf_size,DMA_BIDIRECTIONAL ); +buf2_free: +memzero_explicit (dmabuf, dmabuf_size); +kfree (dmabuf); +buf1_free: +memzero_explicit (kbuf, kbuf_size); +kfree (kbuf); +err: +return ret; +} +static int zynqmp_fallback_check (struct xilinx_aead_tfm_ctx *tfm_ctx,struct aead_request *req) {
@@ -205,13 +393,40 @@ static int zynqmp_fallback_check(struct xilinx_aead_tfm_ctx *tfm_ctx,return 0 ; } +static int versal_fallback_check (struct xilinx_aead_tfm_ctx *tfm_ctx, +struct aead_request *req) +{ +struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); + +if (tfm_ctx->authsize !=XILINX_AES_AUTH_SIZE && rq_ctx->op ==XILINX_AES_DECRYPT ) +return 1 ; + +if (tfm_ctx->keylen ==AES_KEYSIZE_192 ) +return 1 ; + +if (req->cryptlen <ZYNQMP_AES_MIN_INPUT_BLK_SIZE || + req->cryptlen %ZYNQMP_AES_WORD_LEN || + req->assoclen %VERSAL_AES_QWORD_LEN ) +return 1 ; + +if (rq_ctx->op ==XILINX_AES_DECRYPT && + req->cryptlen <=XILINX_AES_AUTH_SIZE ) +return 1 ; + +return 0 ; +} +static int xilinx_handle_aes_req (struct crypto_engine *engine,void *req) {struct aead_request *areq = container_of(req, struct aead_request, base); +struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct aead_alg *alg =crypto_aead_alg (aead); +struct xilinx_aead_drv_ctx *drv_ctx;int err; - err =zynqmp_aes_aead_cipher (areq); + drv_ctx = container_of(alg, struct xilinx_aead_drv_ctx, aead.base); + err = drv_ctx->aes_aead_cipher (areq);local_bh_disable ();crypto_finalize_aead_request (engine, areq, err);local_bh_enable ();
@@ -264,6 +479,50 @@ static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key,return err; } +static int versal_aes_aead_setkey (struct crypto_aead *aead,const u8 *key, +unsigned int keylen) +{ +struct crypto_tfm *tfm =crypto_aead_tfm (aead); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_tfm_ctx (tfm); +struct xilinx_hwkey_info hwkey; +unsigned char keysrc; +int err; + +if (keylen ==sizeof (struct xilinx_hwkey_info )) { +memcpy (&hwkey, key,sizeof (struct xilinx_hwkey_info )); +if (hwkey.magic !=XILINX_KEY_MAGIC ) +return -EINVAL ; + + keysrc = hwkey.type; +if ((keysrc >=VERSAL_AES_EFUSE_USER_KEY_0 && + keysrc <=VERSAL_AES_USER_KEY_7 ) && + keysrc !=VERSAL_AES_KUP_KEY ) { + tfm_ctx->keysrc = keysrc; + tfm_ctx->keylen =sizeof (struct xilinx_hwkey_info ); +return 0 ; + } +return -EINVAL ; + } +if (tfm_ctx->keysrc <VERSAL_AES_USER_KEY_0 || tfm_ctx->keysrc >VERSAL_AES_USER_KEY_7 ) +return -EINVAL ; +if (keylen ==AES_KEYSIZE_256 || keylen ==AES_KEYSIZE_128 ) { + tfm_ctx->keylen = keylen; +memcpy (tfm_ctx->key, key, keylen); +dma_sync_single_for_device (tfm_ctx->dev, tfm_ctx->key_dma_addr, +AES_KEYSIZE_256 , +DMA_TO_DEVICE ); + } + + tfm_ctx->fbk_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK ; + tfm_ctx->fbk_cipher->base.crt_flags |= (aead->base.crt_flags & +CRYPTO_TFM_REQ_MASK ); + err =crypto_aead_setkey (tfm_ctx->fbk_cipher, key, keylen); +if (!err) + tfm_ctx->keylen = keylen; + +return err; +} +static int xilinx_aes_aead_setauthsize (struct crypto_aead *aead,unsigned int authsize) {
@@ -309,6 +568,43 @@ static int zynqmp_aes_aead_encrypt(struct aead_request *req)return crypto_transfer_aead_request_to_engine (drv_ctx->engine, req); } +static int versal_aes_aead_encrypt (struct aead_request *req) +{ +struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); +struct aead_request *subreq =aead_request_ctx (req); +struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead); +struct aead_alg *alg =crypto_aead_alg (aead); +struct xilinx_aead_drv_ctx *drv_ctx; +int err; + + drv_ctx = container_of(alg, struct xilinx_aead_drv_ctx, aead.base); + rq_ctx->op =XILINX_AES_ENCRYPT ; +if (tfm_ctx->keysrc >=VERSAL_AES_USER_KEY_0 && + tfm_ctx->keysrc <=VERSAL_AES_USER_KEY_7 && + tfm_ctx->keylen ==sizeof (struct xilinx_hwkey_info )) +return -EINVAL ; + err =versal_fallback_check (tfm_ctx, req); +if (err && (tfm_ctx->keysrc <VERSAL_AES_USER_KEY_0 || + tfm_ctx->keysrc >VERSAL_AES_USER_KEY_7 )) +return -EOPNOTSUPP ; +if (err) { +aead_request_set_tfm (subreq, tfm_ctx->fbk_cipher); +aead_request_set_callback (subreq, req->base.flags, +NULL ,NULL ); +aead_request_set_crypt (subreq, req->src, req->dst, + req->cryptlen, req->iv); +aead_request_set_ad (subreq, req->assoclen); +if (rq_ctx->op ==XILINX_AES_ENCRYPT ) + err =crypto_aead_encrypt (subreq); +else + err =crypto_aead_decrypt (subreq); +return err; + } + +return crypto_transfer_aead_request_to_engine (drv_ctx->engine, req); +} +static int zynqmp_aes_aead_decrypt (struct aead_request *req) {struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req);
@@ -342,6 +638,45 @@ static int zynqmp_aes_aead_decrypt(struct aead_request *req)return crypto_transfer_aead_request_to_engine (drv_ctx->engine, req); } +static int versal_aes_aead_decrypt (struct aead_request *req) +{ +struct zynqmp_aead_req_ctx *rq_ctx =aead_request_ctx (req); +struct aead_request *subreq =aead_request_ctx (req); +struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct xilinx_aead_tfm_ctx *tfm_ctx =crypto_aead_ctx (aead); +struct aead_alg *alg =crypto_aead_alg (aead); +struct xilinx_aead_drv_ctx *drv_ctx; +int err; + + drv_ctx = container_of(alg, struct xilinx_aead_drv_ctx, aead.base); + rq_ctx->op =XILINX_AES_DECRYPT ; +if (tfm_ctx->keysrc >=VERSAL_AES_USER_KEY_0 && + tfm_ctx->keysrc <=VERSAL_AES_USER_KEY_7 && + tfm_ctx->keylen ==sizeof (struct xilinx_hwkey_info )) +return -EINVAL ; + + err =versal_fallback_check (tfm_ctx, req); +if (err && + (tfm_ctx->keysrc <VERSAL_AES_USER_KEY_0 || + tfm_ctx->keysrc >VERSAL_AES_USER_KEY_7 )) +return -EOPNOTSUPP ; +if (err) { +aead_request_set_tfm (subreq, tfm_ctx->fbk_cipher); +aead_request_set_callback (subreq, req->base.flags, +NULL ,NULL ); +aead_request_set_crypt (subreq, req->src, req->dst, + req->cryptlen, req->iv); +aead_request_set_ad (subreq, req->assoclen); +if (rq_ctx->op ==XILINX_AES_ENCRYPT ) + err =crypto_aead_encrypt (subreq); +else + err =crypto_aead_decrypt (subreq); +return err; + } + +return crypto_transfer_aead_request_to_engine (drv_ctx->engine, req); +} +static int xilinx_aes_aead_init (struct crypto_aead *aead) {struct crypto_tfm *tfm =crypto_aead_tfm (aead);
@@ -352,7 +687,7 @@ static int xilinx_aes_aead_init(struct crypto_aead *aead) drv_ctx = container_of(alg, struct xilinx_aead_drv_ctx, aead.base); tfm_ctx->dev = drv_ctx->dev; tfm_ctx->keylen =0 ; - tfm_ctx->keysrc =ZYNQMP_AES_KUP_KEY ; + tfm_ctx->keysrc = drv_ctx->keysrc; tfm_ctx->fbk_cipher = crypto_alloc_aead(drv_ctx->aead.base.base.cra_name, 0,
@@ -399,6 +734,8 @@ static void xilinx_aes_aead_exit(struct crypto_aead *aead) }static struct xilinx_aead_drv_ctx zynqmp_aes_drv_ctx = { + .aes_aead_cipher = zynqmp_aes_aead_cipher, + .keysrc =ZYNQMP_AES_KUP_KEY , .aead.base = { .setkey = zynqmp_aes_aead_setkey, .setauthsize = xilinx_aes_aead_setauthsize,
@@ -428,12 +765,49 @@ static struct xilinx_aead_drv_ctx zynqmp_aes_drv_ctx = { .dma_bit_mask = ZYNQMP_DMA_BIT_MASK, }; +static struct xilinx_aead_drv_ctx versal_aes_drv_ctx = { + .aes_aead_cipher = versal_aes_aead_cipher, + .keysrc =VERSAL_AES_USER_KEY_0 , + .aead.base = { + .setkey = versal_aes_aead_setkey, + .setauthsize = xilinx_aes_aead_setauthsize, + .encrypt = versal_aes_aead_encrypt, + .decrypt = versal_aes_aead_decrypt, + .init = xilinx_aes_aead_init, + .exit = xilinx_aes_aead_exit, + .ivsize =GCM_AES_IV_SIZE , + .maxauthsize =XILINX_AES_AUTH_SIZE , + .base = { + .cra_name ="gcm(aes)" , + .cra_driver_name ="versal-aes-gcm" , + .cra_priority =300 , + .cra_flags =CRYPTO_ALG_TYPE_AEAD | +CRYPTO_ALG_ASYNC | +CRYPTO_ALG_ALLOCATES_MEMORY | +CRYPTO_ALG_KERN_DRIVER_ONLY | +CRYPTO_ALG_NEED_FALLBACK , + .cra_blocksize =XILINX_AES_BLK_SIZE , + .cra_ctxsize =sizeof (struct xilinx_aead_tfm_ctx ), + .cra_module =THIS_MODULE , + } + }, + .aead.op = { + .do_one_request = xilinx_handle_aes_req, + }, + .dma_bit_mask =VERSAL_DMA_BIT_MASK , +}; +static struct xlnx_feature aes_feature_map[] = { { .family =PM_ZYNQMP_FAMILY_CODE , .feature_id =PM_SECURE_AES , .data = &zynqmp_aes_drv_ctx, }, + { + .family =PM_VERSAL_FAMILY_CODE , + .feature_id =XSECURE_API_AES_OP_INIT , + .data = &versal_aes_drv_ctx, + }, {/* sentinel */ } };
Signature · 1 line
2.49.1
Re: [PATCH 04/15] dt-bindings: crypto: Mark zynqmp-aes as Deprecated
In reply to Harsh JainQuoted 7 lines · Harsh Jain
zynqmp-aes-gcm updated to self discover, corresponding dt binding can be mark deprecated.
Signed-off-by Harsh Jain <[email protected]>--- Documentation/devicetree/bindings/crypto/xlnx,zynqmp-aes.yaml | 2 ++ .../bindings/firmware/xilinx/xlnx,zynqmp-firmware.yaml | 1 +
Best regards,
Krzysztof
Herbert Xu
maintainerRe: [PATCH 09/15] crypto: zynqmp-aes-gcm: Fix setkey operation to select HW keys
In reply to Harsh JainQuoted 8 lines · Harsh Jain
Currently keylen 1 is used to select hw key. There are -ve self test which can fail for setkey length 1. Update driver to use 4 bytes with magic number to select H/W key type.
Signed-off-by Harsh Jain <[email protected]>--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 94 ++++++++++++++++---------- 1 file changed, 60 insertions(+), 34 deletions(-)
The hardware key support should be registered under the name paes instead of aes. Grep for paes in drivers/crypto for examples.
Quoted 41 lines
drivers/crypto/xilinx/zynqmp-aes-gcm.c +18
-12@@ -218,32 +220,42 @@ static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key, unsigned int keylen) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct zynqmp_aead_tfm_ctx *tfm_ctx = - (struct zynqmp_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_tfm_ctx (tfm); +struct xilinx_hwkey_info hwkey;unsigned char keysrc; +int err; -if (keylen ==ZYNQMP_KEY_SRC_SEL_KEY_LEN ) { - keysrc = *key; +if (keylen ==sizeof (struct xilinx_hwkey_info )) { +memcpy (&hwkey, key,sizeof (struct xilinx_hwkey_info )); +if (hwkey.magic !=XILINX_KEY_MAGIC ) +return -EINVAL ; + keysrc = hwkey.type;if (keysrc ==ZYNQMP_AES_KUP_KEY || keysrc ==ZYNQMP_AES_DEV_KEY || keysrc ==ZYNQMP_AES_PUF_KEY ) { - tfm_ctx->keysrc = (enum zynqmp_aead_keysrc )keysrc; - }else { - tfm_ctx->keylen = keylen; + tfm_ctx->keysrc = keysrc; + tfm_ctx->keylen =sizeof (struct xilinx_hwkey_info ); +return 0 ; } - }else { +return -EINVAL ; + } + +if (keylen ==ZYNQMP_AES_KEY_SIZE && tfm_ctx->keysrc ==ZYNQMP_AES_KUP_KEY ) { tfm_ctx->keylen = keylen; -if (keylen ==ZYNQMP_AES_KEY_SIZE ) { - tfm_ctx->keysrc =ZYNQMP_AES_KUP_KEY ; -memcpy (tfm_ctx->key, key, keylen); - } +memcpy (tfm_ctx->key, key, keylen); + }else if (tfm_ctx->keysrc !=ZYNQMP_AES_KUP_KEY ) { +return -EINVAL ;
drivers/crypto/xilinx/zynqmp-aes-gcm.c static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key, · line 256
Quoted 10 lines
drivers/crypto/xilinx/zynqmp-aes-gcm.c +3
-1} tfm_ctx->fbk_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK ; tfm_ctx->fbk_cipher->base.crt_flags |= (aead->base.crt_flags &CRYPTO_TFM_REQ_MASK ); -return crypto_aead_setkey (tfm_ctx->fbk_cipher, key, keylen); + err =crypto_aead_setkey (tfm_ctx->fbk_cipher, key, keylen); +if (!err) + tfm_ctx->keylen = keylen;
You can't have a fallback when there is a hardware key. How did the fallback not return an error here?
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
RE: [PATCH 09/15] crypto: zynqmp-aes-gcm: Fix setkey operation to select HW keys
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 20 lines
-----Original Message-----
From: Herbert Xu <[email protected]>
Sent: Thursday, November 6, 2025 10:03 AM
To: Jain, Harsh (AECG-SSW) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Botcha, Mounika <[email protected]>; Savitala, Sarat Chand <[email protected]>; Simek, Michal <[email protected]>; [email protected]; Buddhabhatti, Jay <[email protected]>
Subject: Re: [PATCH 09/15] crypto: zynqmp-aes-gcm: Fix setkey operation to select HW keysQuoted 4 lines · Harsh Jain
Currently keylen 1 is used to select hw key. There are -ve self test which can fail for setkey length 1. Update driver to use 4 bytes with magic number to select H/W key type.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Harsh Jain <[email protected]>--- drivers/crypto/xilinx/zynqmp-aes-gcm.c | 94 ++++++++++++++++---------- 1 file changed, 60 insertions(+), 34 deletions(-)The hardware key support should be registered under the name paes instead of aes. Grep for paes in
drivers/cryptofor examples.
Sure, Will check and update the driver accordingly.
Quoted 50 lines
Review static int zynqmp_aes_aead_setkey(struct
Quoted 2 lines
(quoted diff) +0
-0@@ -218,32 +220,42 @@ static int zynqmp_aes_aead_setkey(struct
crypto_aead*aead, const u8 *key,Quoted 22 lines
drivers/crypto/xilinx/zynqmp-aes-gcm.c +8
-6unsigned int keylen) {struct crypto_tfm *tfm =crypto_aead_tfm (aead); -struct zynqmp_aead_tfm_ctx *tfm_ctx = - (struct zynqmp_aead_tfm_ctx *)crypto_tfm_ctx (tfm); +struct zynqmp_aead_tfm_ctx *tfm_ctx =crypto_tfm_ctx (tfm); +struct xilinx_hwkey_info hwkey;unsigned char keysrc; +int err; -if (keylen ==ZYNQMP_KEY_SRC_SEL_KEY_LEN ) { - keysrc = *key; +if (keylen ==sizeof (struct xilinx_hwkey_info )) { +memcpy (&hwkey, key,sizeof (struct xilinx_hwkey_info )); +if (hwkey.magic !=XILINX_KEY_MAGIC ) +return -EINVAL ; + keysrc = hwkey.type;if (keysrc ==ZYNQMP_AES_KUP_KEY || keysrc ==ZYNQMP_AES_DEV_KEY || keysrc ==ZYNQMP_AES_PUF_KEY ) { - tfm_ctx->keysrc = (enum zynqmp_aead_keysrc )keysrc; - }else {drivers/crypto/xilinx/zynqmp-aes-gcm.c static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key,
Quoted 10 lines
drivers/crypto/xilinx/zynqmp-aes-gcm.c +7
-2- tfm_ctx->keylen = keylen; + tfm_ctx->keysrc = keysrc; + tfm_ctx->keylen =sizeof (struct xilinx_hwkey_info ); +return 0 ; } - }else { +return -EINVAL ; + } + + if (keylen == ZYNQMP_AES_KEY_SIZE && tfm_ctx->keysrc ==
ZYNQMP_AES_KUP_KEY) {Quoted 14 lines
drivers/crypto/xilinx/zynqmp-aes-gcm.c +3
-4tfm_ctx->keylen = keylen; -if (keylen ==ZYNQMP_AES_KEY_SIZE ) { - tfm_ctx->keysrc =ZYNQMP_AES_KUP_KEY ; -memcpy (tfm_ctx->key, key, keylen); - } +memcpy (tfm_ctx->key, key, keylen); + }else if (tfm_ctx->keysrc !=ZYNQMP_AES_KUP_KEY ) { +return -EINVAL ; } tfm_ctx->fbk_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK ; tfm_ctx->fbk_cipher->base.crt_flags |= (aead->base.crt_flags &CRYPTO_TFM_REQ_MASK );
Quoted 7 lines
drivers/crypto/xilinx/zynqmp-aes-gcm.c static int zynqmp_aes_aead_setkey(struct crypto_aead *aead, const u8 *key, · line 256
Quoted 4 lines
drivers/crypto/xilinx/zynqmp-aes-gcm.c +3
-1-return crypto_aead_setkey (tfm_ctx->fbk_cipher, key, keylen); + err =crypto_aead_setkey (tfm_ctx->fbk_cipher, key, keylen); +if (!err) + tfm_ctx->keylen = keylen;You can't have a fallback when there is a hardware key. How did the fallback not return an error here?
We have two types of key registers
1) Registers to save user supplied keys like ZYNQMP_AES_KUP_KEY
2) Register Where H/W internally generates the keys like ZYNQMP_AES_DEV_KEY and ZYNQMP_AES_PUF_KEY
Fallback is for 1), because driver has saved key in private ctx and can be fallback to S/W.
Quoted 6 lines
Cheers,
--
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Herbert Xu
maintainerRe: [PATCH 09/15] crypto: zynqmp-aes-gcm: Fix setkey operation to select HW keys
Quoted 6 lines · Harsh (AECG-SSW)
We have two types of key registers
1) Registers to save user supplied keys likeZYNQMP_AES_KUP_KEY
2) Register Where H/W internally generates the keys likeZYNQMP_AES_DEV_KEYandZYNQMP_AES_PUF_KEYFallback is for 1), because driver has saved key in private ctx and can be fallback to S/W.
OK so in our parlance this isn't a hardware key and it should stay with the plain "aes" driver. "paes" should be used for hardware keys with no corresponding software key.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt