2024-02-12 14:00:00

by Alexey Romanov

[permalink] [raw]
Subject: [PATCH v4 08/20] drivers: crypto: meson: cleanup defines

It is bad to use hardcoded values directly in the code.

Signed-off-by: Alexey Romanov <[email protected]>
---
drivers/crypto/amlogic/amlogic-gxl-cipher.c | 24 ++++++++++-----------
drivers/crypto/amlogic/amlogic-gxl.h | 16 ++++++++------
2 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
index bc3092a8a2c2..c662c4b86e97 100644
--- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c
+++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
@@ -141,8 +141,8 @@ static int meson_cipher(struct skcipher_request *areq)
ivsize, 0);
}
}
- if (keyivlen == 24)
- keyivlen = 32;
+ if (keyivlen == AES_KEYSIZE_192)
+ keyivlen = AES_MAX_KEY_SIZE;

phykeyiv = dma_map_single(mc->dev, bkeyiv, keyivlen,
DMA_TO_DEVICE);
@@ -161,7 +161,7 @@ static int meson_cipher(struct skcipher_request *areq)
todo = min(keyivlen - eat, 16u);
desc->t_src = cpu_to_le32(phykeyiv + i * 16);
desc->t_dst = cpu_to_le32(i * 16);
- v = (MODE_KEY << 20) | DESC_OWN | 16;
+ v = DESC_MODE_KEY | DESC_OWN | 16;
desc->t_status = cpu_to_le32(v);

eat += todo;
@@ -205,7 +205,7 @@ static int meson_cipher(struct skcipher_request *areq)
desc->t_src = cpu_to_le32(sg_dma_address(src_sg));
desc->t_dst = cpu_to_le32(sg_dma_address(dst_sg));
todo = min(len, sg_dma_len(src_sg));
- v = (op->keymode << 20) | DESC_OWN | todo | (algt->blockmode << 26);
+ v = op->keymode | DESC_OWN | todo | algt->blockmode;
if (rctx->op_dir)
v |= DESC_ENCRYPTION;
len -= todo;
@@ -348,14 +348,14 @@ static int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
struct meson_dev *mc = op->mc;

switch (keylen) {
- case 128 / 8:
- op->keymode = MODE_AES_128;
+ case AES_KEYSIZE_128:
+ op->keymode = DESC_MODE_AES_128;
break;
- case 192 / 8:
- op->keymode = MODE_AES_192;
+ case AES_KEYSIZE_192:
+ op->keymode = DESC_MODE_AES_192;
break;
- case 256 / 8:
- op->keymode = MODE_AES_256;
+ case AES_KEYSIZE_256:
+ op->keymode = DESC_MODE_AES_256;
break;
default:
dev_dbg(mc->dev, "ERROR: Invalid keylen %u\n", keylen);
@@ -373,7 +373,7 @@ static int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
static struct meson_alg_template algs[] = {
{
.type = CRYPTO_ALG_TYPE_SKCIPHER,
- .blockmode = MESON_OPMODE_CBC,
+ .blockmode = DESC_OPMODE_CBC,
.alg.skcipher.base = {
.base = {
.cra_name = "cbc(aes)",
@@ -402,7 +402,7 @@ static struct meson_alg_template algs[] = {
},
{
.type = CRYPTO_ALG_TYPE_SKCIPHER,
- .blockmode = MESON_OPMODE_ECB,
+ .blockmode = DESC_OPMODE_ECB,
.alg.skcipher.base = {
.base = {
.cra_name = "ecb(aes)",
diff --git a/drivers/crypto/amlogic/amlogic-gxl.h b/drivers/crypto/amlogic/amlogic-gxl.h
index 0a03e8144977..a0d83c82906d 100644
--- a/drivers/crypto/amlogic/amlogic-gxl.h
+++ b/drivers/crypto/amlogic/amlogic-gxl.h
@@ -11,19 +11,21 @@
#include <linux/crypto.h>
#include <linux/scatterlist.h>

-#define MODE_KEY 1
-#define MODE_AES_128 0x8
-#define MODE_AES_192 0x9
-#define MODE_AES_256 0xa
-
#define MESON_DECRYPT 0
#define MESON_ENCRYPT 1

-#define MESON_OPMODE_ECB 0
-#define MESON_OPMODE_CBC 1
+#define DESC_MODE_KEY (0x1 << 20)
+#define DESC_MODE_AES_128 (0x8 << 20)
+#define DESC_MODE_AES_192 (0x9 << 20)
+#define DESC_MODE_AES_256 (0xa << 20)

#define MAXDESC 64

+#define DESC_OPMODE_ECB (0 << 26)
+#define DESC_OPMODE_CBC (1 << 26)
+
+#define DESC_MAXLEN ((1 << 17) - 1)
+
#define DESC_LAST BIT(18)
#define DESC_ENCRYPTION BIT(28)
#define DESC_OWN BIT(31)
--
2.34.1



2024-02-12 17:24:29

by Neil Armstrong

[permalink] [raw]
Subject: Re: [PATCH v4 08/20] drivers: crypto: meson: cleanup defines

On 12/02/2024 14:50, Alexey Romanov wrote:
> It is bad to use hardcoded values directly in the code.
>
> Signed-off-by: Alexey Romanov <[email protected]>
> ---
> drivers/crypto/amlogic/amlogic-gxl-cipher.c | 24 ++++++++++-----------
> drivers/crypto/amlogic/amlogic-gxl.h | 16 ++++++++------
> 2 files changed, 21 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> index bc3092a8a2c2..c662c4b86e97 100644
> --- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> +++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> @@ -141,8 +141,8 @@ static int meson_cipher(struct skcipher_request *areq)
> ivsize, 0);
> }
> }
> - if (keyivlen == 24)
> - keyivlen = 32;
> + if (keyivlen == AES_KEYSIZE_192)
> + keyivlen = AES_MAX_KEY_SIZE;
>
> phykeyiv = dma_map_single(mc->dev, bkeyiv, keyivlen,
> DMA_TO_DEVICE);
> @@ -161,7 +161,7 @@ static int meson_cipher(struct skcipher_request *areq)
> todo = min(keyivlen - eat, 16u);
> desc->t_src = cpu_to_le32(phykeyiv + i * 16);
> desc->t_dst = cpu_to_le32(i * 16);
> - v = (MODE_KEY << 20) | DESC_OWN | 16;
> + v = DESC_MODE_KEY | DESC_OWN | 16;
> desc->t_status = cpu_to_le32(v);
>
> eat += todo;
> @@ -205,7 +205,7 @@ static int meson_cipher(struct skcipher_request *areq)
> desc->t_src = cpu_to_le32(sg_dma_address(src_sg));
> desc->t_dst = cpu_to_le32(sg_dma_address(dst_sg));
> todo = min(len, sg_dma_len(src_sg));
> - v = (op->keymode << 20) | DESC_OWN | todo | (algt->blockmode << 26);
> + v = op->keymode | DESC_OWN | todo | algt->blockmode;
> if (rctx->op_dir)
> v |= DESC_ENCRYPTION;
> len -= todo;
> @@ -348,14 +348,14 @@ static int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
> struct meson_dev *mc = op->mc;
>
> switch (keylen) {
> - case 128 / 8:
> - op->keymode = MODE_AES_128;
> + case AES_KEYSIZE_128:
> + op->keymode = DESC_MODE_AES_128;
> break;
> - case 192 / 8:
> - op->keymode = MODE_AES_192;
> + case AES_KEYSIZE_192:
> + op->keymode = DESC_MODE_AES_192;
> break;
> - case 256 / 8:
> - op->keymode = MODE_AES_256;
> + case AES_KEYSIZE_256:
> + op->keymode = DESC_MODE_AES_256;
> break;
> default:
> dev_dbg(mc->dev, "ERROR: Invalid keylen %u\n", keylen);
> @@ -373,7 +373,7 @@ static int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
> static struct meson_alg_template algs[] = {
> {
> .type = CRYPTO_ALG_TYPE_SKCIPHER,
> - .blockmode = MESON_OPMODE_CBC,
> + .blockmode = DESC_OPMODE_CBC,
> .alg.skcipher.base = {
> .base = {
> .cra_name = "cbc(aes)",
> @@ -402,7 +402,7 @@ static struct meson_alg_template algs[] = {
> },
> {
> .type = CRYPTO_ALG_TYPE_SKCIPHER,
> - .blockmode = MESON_OPMODE_ECB,
> + .blockmode = DESC_OPMODE_ECB,
> .alg.skcipher.base = {
> .base = {
> .cra_name = "ecb(aes)",
> diff --git a/drivers/crypto/amlogic/amlogic-gxl.h b/drivers/crypto/amlogic/amlogic-gxl.h
> index 0a03e8144977..a0d83c82906d 100644
> --- a/drivers/crypto/amlogic/amlogic-gxl.h
> +++ b/drivers/crypto/amlogic/amlogic-gxl.h
> @@ -11,19 +11,21 @@
> #include <linux/crypto.h>
> #include <linux/scatterlist.h>
>
> -#define MODE_KEY 1
> -#define MODE_AES_128 0x8
> -#define MODE_AES_192 0x9
> -#define MODE_AES_256 0xa
> -
> #define MESON_DECRYPT 0
> #define MESON_ENCRYPT 1
>
> -#define MESON_OPMODE_ECB 0
> -#define MESON_OPMODE_CBC 1
> +#define DESC_MODE_KEY (0x1 << 20)
> +#define DESC_MODE_AES_128 (0x8 << 20)
> +#define DESC_MODE_AES_192 (0x9 << 20)
> +#define DESC_MODE_AES_256 (0xa << 20)


>
> #define MAXDESC 64
>
> +#define DESC_OPMODE_ECB (0 << 26)
> +#define DESC_OPMODE_CBC (1 << 26)
> +
> +#define DESC_MAXLEN ((1 << 17) - 1)

GENMASK(16, 0) ?

> +
> #define DESC_LAST BIT(18)
> #define DESC_ENCRYPTION BIT(28)
> #define DESC_OWN BIT(31)

Anyway:
Reviewed-by: Neil Armstrong <[email protected]>