Skip to content

Commit 6aad701

Browse files
jiajiehoherbertx
authored andcommitted
crypto: starfive - Align rsa input data to 32-bit
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 <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent f062289 commit 6aad701

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

drivers/crypto/starfive/jh7110-cryp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define MAX_KEY_SIZE SHA512_BLOCK_SIZE
3131
#define STARFIVE_AES_IV_LEN AES_BLOCK_SIZE
3232
#define STARFIVE_AES_CTR_LEN AES_BLOCK_SIZE
33+
#define STARFIVE_RSA_MAX_KEYSZ 256
3334

3435
union starfive_aes_csr {
3536
u32 v;
@@ -222,7 +223,7 @@ struct starfive_cryp_request_ctx {
222223
unsigned int digsize;
223224
unsigned long in_sg_len;
224225
unsigned char *adata;
225-
u8 rsa_data[] __aligned(sizeof(u32));
226+
u8 rsa_data[STARFIVE_RSA_MAX_KEYSZ] __aligned(sizeof(u32));
226227
};
227228

228229
struct starfive_cryp_dev *starfive_cryp_find_dev(struct starfive_cryp_ctx *ctx);

drivers/crypto/starfive/jh7110-rsa.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
/* A * A * R mod N ==> A */
3232
#define CRYPTO_CMD_AARN 0x7
3333

34-
#define STARFIVE_RSA_MAX_KEYSZ 256
3534
#define STARFIVE_RSA_RESET 0x2
3635

3736
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,
7473
{
7574
struct starfive_cryp_dev *cryp = ctx->cryp;
7675
struct starfive_cryp_request_ctx *rctx = ctx->rctx;
77-
int count = rctx->total / sizeof(u32) - 1;
76+
int count = (ALIGN(rctx->total, 4) / 4) - 1;
7877
int loop;
7978
u32 temp;
8079
u8 opsize;
@@ -251,12 +250,17 @@ static int starfive_rsa_enc_core(struct starfive_cryp_ctx *ctx, int enc)
251250
struct starfive_cryp_dev *cryp = ctx->cryp;
252251
struct starfive_cryp_request_ctx *rctx = ctx->rctx;
253252
struct starfive_rsa_key *key = &ctx->rsa_key;
254-
int ret = 0;
253+
int ret = 0, shift = 0;
255254

256255
writel(STARFIVE_RSA_RESET, cryp->base + STARFIVE_PKA_CACR_OFFSET);
257256

257+
if (!IS_ALIGNED(rctx->total, sizeof(u32))) {
258+
shift = sizeof(u32) - (rctx->total & 0x3);
259+
memset(rctx->rsa_data, 0, shift);
260+
}
261+
258262
rctx->total = sg_copy_to_buffer(rctx->in_sg, rctx->nents,
259-
rctx->rsa_data, rctx->total);
263+
rctx->rsa_data + shift, rctx->total);
260264

261265
if (enc) {
262266
key->bitlen = key->e_bitlen;

0 commit comments

Comments
 (0)