* [PATCH v3 0/2] crypto: starfive: Fixes for RSA
@ 2024-06-26 1:40 Jia Jie Ho
2024-06-26 1:40 ` [PATCH v3 1/2] crypto: starfive - Align rsa input data to 32-bit Jia Jie Ho
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Jia Jie Ho @ 2024-06-26 1:40 UTC (permalink / raw)
To: Herbert Xu, David S . Miller, linux-crypto, linux-kernel
This series adds handling for 32-bit unaligned input text and a fix
for a missing pointer assignment in the driver.
v2->v3:
- Retain u32 aligned attribute for preallocated buffer.
- Replace sizeof(u32) with 4 to simplify calculation.
- Removed unrelated changes from patch 1.
- Move nent changes into patch 2.
v1->v2:
- Build the extra space required for shifting into reqsize.
Jia Jie Ho (2):
crypto: starfive - Align rsa input data to 32-bit
crypto: starfive - Fix nent assignment in rsa dec
drivers/crypto/starfive/jh7110-cryp.h | 4 ++--
drivers/crypto/starfive/jh7110-rsa.c | 15 +++++++++------
2 files changed, 11 insertions(+), 8 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3 1/2] crypto: starfive - Align rsa input data to 32-bit
2024-06-26 1:40 [PATCH v3 0/2] crypto: starfive: Fixes for RSA Jia Jie Ho
@ 2024-06-26 1:40 ` Jia Jie Ho
2024-06-26 1:40 ` [PATCH v3 2/2] crypto: starfive - Fix nent assignment in rsa dec Jia Jie Ho
2024-07-06 0:52 ` [PATCH v3 0/2] crypto: starfive: Fixes for RSA Herbert Xu
2 siblings, 0 replies; 4+ messages in thread
From: Jia Jie Ho @ 2024-06-26 1:40 UTC (permalink / raw)
To: Herbert Xu, David S . Miller, linux-crypto, linux-kernel
Hardware expects RSA input plain/ciphertext to be 32-bit aligned.
Set fixed length for preallocated buffer to the maximum supported
keysize of the hardware and shift input text accordingly.
Signed-off-by: Jia Jie Ho <jiajie.ho@starfivetech.com>
---
drivers/crypto/starfive/jh7110-cryp.h | 3 ++-
drivers/crypto/starfive/jh7110-rsa.c | 12 ++++++++----
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/drivers/crypto/starfive/jh7110-cryp.h b/drivers/crypto/starfive/jh7110-cryp.h
index 494a74f52706..85c65c6c0327 100644
--- a/drivers/crypto/starfive/jh7110-cryp.h
+++ b/drivers/crypto/starfive/jh7110-cryp.h
@@ -30,6 +30,7 @@
#define MAX_KEY_SIZE SHA512_BLOCK_SIZE
#define STARFIVE_AES_IV_LEN AES_BLOCK_SIZE
#define STARFIVE_AES_CTR_LEN AES_BLOCK_SIZE
+#define STARFIVE_RSA_MAX_KEYSZ 256
union starfive_aes_csr {
u32 v;
@@ -222,7 +223,7 @@ struct starfive_cryp_request_ctx {
unsigned int digsize;
unsigned long in_sg_len;
unsigned char *adata;
- u8 rsa_data[] __aligned(sizeof(u32));
+ u8 rsa_data[STARFIVE_RSA_MAX_KEYSZ] __aligned(sizeof(u32));
};
struct starfive_cryp_dev *starfive_cryp_find_dev(struct starfive_cryp_ctx *ctx);
diff --git a/drivers/crypto/starfive/jh7110-rsa.c b/drivers/crypto/starfive/jh7110-rsa.c
index 33093ba4b13a..59f5979e9360 100644
--- a/drivers/crypto/starfive/jh7110-rsa.c
+++ b/drivers/crypto/starfive/jh7110-rsa.c
@@ -31,7 +31,6 @@
/* A * A * R mod N ==> A */
#define CRYPTO_CMD_AARN 0x7
-#define STARFIVE_RSA_MAX_KEYSZ 256
#define STARFIVE_RSA_RESET 0x2
static inline int starfive_pka_wait_done(struct starfive_cryp_ctx *ctx)
@@ -74,7 +73,7 @@ static int starfive_rsa_montgomery_form(struct starfive_cryp_ctx *ctx,
{
struct starfive_cryp_dev *cryp = ctx->cryp;
struct starfive_cryp_request_ctx *rctx = ctx->rctx;
- int count = rctx->total / sizeof(u32) - 1;
+ int count = (ALIGN(rctx->total, 4) / 4) - 1;
int loop;
u32 temp;
u8 opsize;
@@ -251,12 +250,17 @@ static int starfive_rsa_enc_core(struct starfive_cryp_ctx *ctx, int enc)
struct starfive_cryp_dev *cryp = ctx->cryp;
struct starfive_cryp_request_ctx *rctx = ctx->rctx;
struct starfive_rsa_key *key = &ctx->rsa_key;
- int ret = 0;
+ int ret = 0, shift = 0;
writel(STARFIVE_RSA_RESET, cryp->base + STARFIVE_PKA_CACR_OFFSET);
+ if (!IS_ALIGNED(rctx->total, sizeof(u32))) {
+ shift = sizeof(u32) - (rctx->total & 0x3);
+ memset(rctx->rsa_data, 0, shift);
+ }
+
rctx->total = sg_copy_to_buffer(rctx->in_sg, rctx->nents,
- rctx->rsa_data, rctx->total);
+ rctx->rsa_data + shift, rctx->total);
if (enc) {
key->bitlen = key->e_bitlen;
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3 2/2] crypto: starfive - Fix nent assignment in rsa dec
2024-06-26 1:40 [PATCH v3 0/2] crypto: starfive: Fixes for RSA Jia Jie Ho
2024-06-26 1:40 ` [PATCH v3 1/2] crypto: starfive - Align rsa input data to 32-bit Jia Jie Ho
@ 2024-06-26 1:40 ` Jia Jie Ho
2024-07-06 0:52 ` [PATCH v3 0/2] crypto: starfive: Fixes for RSA Herbert Xu
2 siblings, 0 replies; 4+ messages in thread
From: Jia Jie Ho @ 2024-06-26 1:40 UTC (permalink / raw)
To: Herbert Xu, David S . Miller, linux-crypto, linux-kernel
Missing src scatterlist nent assignment in rsa decrypt function.
Removing all unneeded assignment and use nents value from req->src
instead.
Signed-off-by: Jia Jie Ho <jiajie.ho@starfivetech.com>
---
drivers/crypto/starfive/jh7110-cryp.h | 1 -
drivers/crypto/starfive/jh7110-rsa.c | 3 +--
2 files changed, 1 insertion(+), 3 deletions(-)
diff --git a/drivers/crypto/starfive/jh7110-cryp.h b/drivers/crypto/starfive/jh7110-cryp.h
index 85c65c6c0327..5ed4ba5da7f9 100644
--- a/drivers/crypto/starfive/jh7110-cryp.h
+++ b/drivers/crypto/starfive/jh7110-cryp.h
@@ -218,7 +218,6 @@ struct starfive_cryp_request_ctx {
struct scatterlist *out_sg;
struct ahash_request ahash_fbk_req;
size_t total;
- size_t nents;
unsigned int blksize;
unsigned int digsize;
unsigned long in_sg_len;
diff --git a/drivers/crypto/starfive/jh7110-rsa.c b/drivers/crypto/starfive/jh7110-rsa.c
index 59f5979e9360..a778c4846025 100644
--- a/drivers/crypto/starfive/jh7110-rsa.c
+++ b/drivers/crypto/starfive/jh7110-rsa.c
@@ -259,7 +259,7 @@ static int starfive_rsa_enc_core(struct starfive_cryp_ctx *ctx, int enc)
memset(rctx->rsa_data, 0, shift);
}
- rctx->total = sg_copy_to_buffer(rctx->in_sg, rctx->nents,
+ rctx->total = sg_copy_to_buffer(rctx->in_sg, sg_nents(rctx->in_sg),
rctx->rsa_data + shift, rctx->total);
if (enc) {
@@ -309,7 +309,6 @@ static int starfive_rsa_enc(struct akcipher_request *req)
rctx->in_sg = req->src;
rctx->out_sg = req->dst;
rctx->total = req->src_len;
- rctx->nents = sg_nents(rctx->in_sg);
ctx->rctx = rctx;
return starfive_rsa_enc_core(ctx, 1);
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3 0/2] crypto: starfive: Fixes for RSA
2024-06-26 1:40 [PATCH v3 0/2] crypto: starfive: Fixes for RSA Jia Jie Ho
2024-06-26 1:40 ` [PATCH v3 1/2] crypto: starfive - Align rsa input data to 32-bit Jia Jie Ho
2024-06-26 1:40 ` [PATCH v3 2/2] crypto: starfive - Fix nent assignment in rsa dec Jia Jie Ho
@ 2024-07-06 0:52 ` Herbert Xu
2 siblings, 0 replies; 4+ messages in thread
From: Herbert Xu @ 2024-07-06 0:52 UTC (permalink / raw)
To: Jia Jie Ho; +Cc: David S . Miller, linux-crypto, linux-kernel
On Wed, Jun 26, 2024 at 09:40:41AM +0800, Jia Jie Ho wrote:
> This series adds handling for 32-bit unaligned input text and a fix
> for a missing pointer assignment in the driver.
>
> v2->v3:
> - Retain u32 aligned attribute for preallocated buffer.
> - Replace sizeof(u32) with 4 to simplify calculation.
> - Removed unrelated changes from patch 1.
> - Move nent changes into patch 2.
>
> v1->v2:
> - Build the extra space required for shifting into reqsize.
>
> Jia Jie Ho (2):
> crypto: starfive - Align rsa input data to 32-bit
> crypto: starfive - Fix nent assignment in rsa dec
>
> drivers/crypto/starfive/jh7110-cryp.h | 4 ++--
> drivers/crypto/starfive/jh7110-rsa.c | 15 +++++++++------
> 2 files changed, 11 insertions(+), 8 deletions(-)
>
> --
> 2.43.0
All applied. Thanks.
--
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-07-06 0:52 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-26 1:40 [PATCH v3 0/2] crypto: starfive: Fixes for RSA Jia Jie Ho
2024-06-26 1:40 ` [PATCH v3 1/2] crypto: starfive - Align rsa input data to 32-bit Jia Jie Ho
2024-06-26 1:40 ` [PATCH v3 2/2] crypto: starfive - Fix nent assignment in rsa dec Jia Jie Ho
2024-07-06 0:52 ` [PATCH v3 0/2] crypto: starfive: Fixes for RSA Herbert Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®