Skip to content

Commit 445a4aa

Browse files
jiajiehoherbertx
authored andcommitted
crypto: starfive - Add RSA algo support
Adding RSA enc/dec and sign/verify feature for StarFive cryptographic module. The module only supports mod sizes up to 2048, therefore calculations more than that will use fallback algo. Co-developed-by: Huan Feng <[email protected]> Signed-off-by: Huan Feng <[email protected]> Signed-off-by: Jia Jie Ho <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent df12284 commit 445a4aa

File tree

5 files changed

+683
-2
lines changed

5 files changed

+683
-2
lines changed

drivers/crypto/starfive/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ config CRYPTO_DEV_JH7110
1111
select CRYPTO_SHA256
1212
select CRYPTO_SHA512
1313
select CRYPTO_SM3_GENERIC
14+
select CRYPTO_RSA
1415
help
1516
Support for StarFive JH7110 crypto hardware acceleration engine.
1617
This module provides acceleration for public key algo,

drivers/crypto/starfive/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
obj-$(CONFIG_CRYPTO_DEV_JH7110) += jh7110-crypto.o
4-
jh7110-crypto-objs := jh7110-cryp.o jh7110-hash.o
4+
jh7110-crypto-objs := jh7110-cryp.o jh7110-hash.o jh7110-rsa.o

drivers/crypto/starfive/jh7110-cryp.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,19 @@ static irqreturn_t starfive_cryp_irq(int irq, void *priv)
8686

8787
status = readl(cryp->base + STARFIVE_IE_FLAG_OFFSET);
8888
if (status & STARFIVE_IE_FLAG_HASH_DONE) {
89-
writel(STARFIVE_IE_MASK_HASH_DONE, cryp->base + STARFIVE_IE_MASK_OFFSET);
89+
status = readl(cryp->base + STARFIVE_IE_MASK_OFFSET);
90+
status |= STARFIVE_IE_MASK_HASH_DONE;
91+
writel(status, cryp->base + STARFIVE_IE_MASK_OFFSET);
9092
tasklet_schedule(&cryp->hash_done);
9193
}
9294

95+
if (status & STARFIVE_IE_FLAG_PKA_DONE) {
96+
status = readl(cryp->base + STARFIVE_IE_MASK_OFFSET);
97+
status |= STARFIVE_IE_MASK_PKA_DONE;
98+
writel(status, cryp->base + STARFIVE_IE_MASK_OFFSET);
99+
complete(&cryp->pka_done);
100+
}
101+
93102
return IRQ_HANDLED;
94103
}
95104

@@ -132,6 +141,8 @@ static int starfive_cryp_probe(struct platform_device *pdev)
132141
return dev_err_probe(&pdev->dev, PTR_ERR(cryp->rst),
133142
"Error getting hardware reset line\n");
134143

144+
init_completion(&cryp->pka_done);
145+
135146
irq = platform_get_irq(pdev, 0);
136147
if (irq < 0)
137148
return irq;
@@ -173,8 +184,14 @@ static int starfive_cryp_probe(struct platform_device *pdev)
173184
if (ret)
174185
goto err_algs_hash;
175186

187+
ret = starfive_rsa_register_algs();
188+
if (ret)
189+
goto err_algs_rsa;
190+
176191
return 0;
177192

193+
err_algs_rsa:
194+
starfive_hash_unregister_algs();
178195
err_algs_hash:
179196
crypto_engine_stop(cryp->engine);
180197
err_engine_start:
@@ -200,6 +217,7 @@ static int starfive_cryp_remove(struct platform_device *pdev)
200217
struct starfive_cryp_dev *cryp = platform_get_drvdata(pdev);
201218

202219
starfive_hash_unregister_algs();
220+
starfive_rsa_unregister_algs();
203221

204222
tasklet_kill(&cryp->hash_done);
205223

drivers/crypto/starfive/jh7110-cryp.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#define STARFIVE_DMA_OUT_LEN_OFFSET 0x14
1919

2020
#define STARFIVE_IE_MASK_HASH_DONE 0x4
21+
#define STARFIVE_IE_MASK_PKA_DONE 0x8
2122
#define STARFIVE_IE_FLAG_HASH_DONE 0x4
23+
#define STARFIVE_IE_FLAG_PKA_DONE 0x8
2224

2325
#define STARFIVE_MSG_BUFFER_SIZE SZ_16K
2426
#define MAX_KEY_SIZE SHA512_BLOCK_SIZE
@@ -54,6 +56,39 @@ union starfive_hash_csr {
5456
};
5557
};
5658

59+
union starfive_pka_cacr {
60+
u32 v;
61+
struct {
62+
u32 start :1;
63+
u32 reset :1;
64+
u32 ie :1;
65+
u32 rsvd_0 :1;
66+
u32 fifo_mode :1;
67+
u32 not_r2 :1;
68+
u32 ecc_sub :1;
69+
u32 pre_expf :1;
70+
u32 cmd :4;
71+
u32 rsvd_1 :1;
72+
u32 ctrl_dummy :1;
73+
u32 ctrl_false :1;
74+
u32 cln_done :1;
75+
u32 opsize :6;
76+
u32 rsvd_2 :2;
77+
u32 exposize :6;
78+
u32 rsvd_3 :1;
79+
u32 bigendian :1;
80+
};
81+
};
82+
83+
struct starfive_rsa_key {
84+
u8 *n;
85+
u8 *e;
86+
u8 *d;
87+
int e_bitlen;
88+
int d_bitlen;
89+
int bitlen;
90+
size_t key_sz;
91+
};
5792

5893
union starfive_alg_cr {
5994
u32 v;
@@ -78,6 +113,8 @@ struct starfive_cryp_ctx {
78113
u8 key[MAX_KEY_SIZE];
79114
int keylen;
80115
bool is_hmac;
116+
struct starfive_rsa_key rsa_key;
117+
struct crypto_akcipher *akcipher_fbk;
81118
struct crypto_ahash *ahash_fbk;
82119
};
83120

@@ -98,6 +135,7 @@ struct starfive_cryp_dev {
98135
struct dma_slave_config cfg_out;
99136
struct crypto_engine *engine;
100137
struct tasklet_struct hash_done;
138+
struct completion pka_done;
101139
int err;
102140
union starfive_alg_cr alg_cr;
103141
union {
@@ -108,20 +146,27 @@ struct starfive_cryp_dev {
108146
struct starfive_cryp_request_ctx {
109147
union {
110148
union starfive_hash_csr hash;
149+
union starfive_pka_cacr pka;
111150
} csr;
112151

113152
struct scatterlist *in_sg;
153+
struct scatterlist *out_sg;
114154
struct ahash_request ahash_fbk_req;
115155
size_t total;
156+
size_t nents;
116157
unsigned int blksize;
117158
unsigned int digsize;
118159
unsigned long in_sg_len;
160+
u8 rsa_data[] __aligned(sizeof(u32));
119161
};
120162

121163
struct starfive_cryp_dev *starfive_cryp_find_dev(struct starfive_cryp_ctx *ctx);
122164

123165
int starfive_hash_register_algs(void);
124166
void starfive_hash_unregister_algs(void);
125167

168+
int starfive_rsa_register_algs(void);
169+
void starfive_rsa_unregister_algs(void);
170+
126171
void starfive_hash_done_task(unsigned long param);
127172
#endif

0 commit comments

Comments
 (0)