Lines Matching +full:jh7110 +full:- +full:crypto

1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/crypto.h>
10 #include <crypto/akcipher.h>
11 #include <crypto/algapi.h>
12 #include <crypto/internal/akcipher.h>
13 #include <crypto/internal/rsa.h>
14 #include <crypto/scatterwalk.h>
16 #include "jh7110-cryp.h"
38 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_pka_wait_done()
41 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_PKA_CASR_OFFSET, status, in starfive_pka_wait_done()
47 if (!key->key_sz) in starfive_rsa_free_key()
50 kfree_sensitive(key->d); in starfive_rsa_free_key()
51 kfree_sensitive(key->e); in starfive_rsa_free_key()
52 kfree_sensitive(key->n); in starfive_rsa_free_key()
63 value = pa[key_sz - i - 1]; in starfive_rsa_get_nbit()
74 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_rsa_montgomery_form()
75 struct starfive_cryp_request_ctx *rctx = ctx->rctx; in starfive_rsa_montgomery_form()
76 int count = (ALIGN(rctx->total, 4) / 4) - 1; in starfive_rsa_montgomery_form()
81 opsize = (bit_len - 1) >> 5; in starfive_rsa_montgomery_form()
82 rctx->csr.pka.v = 0; in starfive_rsa_montgomery_form()
84 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET); in starfive_rsa_montgomery_form()
87 writel(mod[opsize - loop], cryp->base + STARFIVE_PKA_CANR_OFFSET + loop * 4); in starfive_rsa_montgomery_form()
90 rctx->csr.pka.v = 0; in starfive_rsa_montgomery_form()
91 rctx->csr.pka.cln_done = 1; in starfive_rsa_montgomery_form()
92 rctx->csr.pka.opsize = opsize; in starfive_rsa_montgomery_form()
93 rctx->csr.pka.exposize = opsize; in starfive_rsa_montgomery_form()
94 rctx->csr.pka.cmd = CRYPTO_CMD_PRE; in starfive_rsa_montgomery_form()
95 rctx->csr.pka.start = 1; in starfive_rsa_montgomery_form()
96 rctx->csr.pka.not_r2 = 1; in starfive_rsa_montgomery_form()
97 rctx->csr.pka.ie = 1; in starfive_rsa_montgomery_form()
99 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET); in starfive_rsa_montgomery_form()
102 return -ETIMEDOUT; in starfive_rsa_montgomery_form()
105 writel(in[opsize - loop], cryp->base + STARFIVE_PKA_CAAR_OFFSET + loop * 4); in starfive_rsa_montgomery_form()
107 writel(0x1000000, cryp->base + STARFIVE_PKA_CAER_OFFSET); in starfive_rsa_montgomery_form()
110 writel(0, cryp->base + STARFIVE_PKA_CAER_OFFSET + loop * 4); in starfive_rsa_montgomery_form()
112 rctx->csr.pka.v = 0; in starfive_rsa_montgomery_form()
113 rctx->csr.pka.cln_done = 1; in starfive_rsa_montgomery_form()
114 rctx->csr.pka.opsize = opsize; in starfive_rsa_montgomery_form()
115 rctx->csr.pka.exposize = opsize; in starfive_rsa_montgomery_form()
116 rctx->csr.pka.cmd = CRYPTO_CMD_AERN; in starfive_rsa_montgomery_form()
117 rctx->csr.pka.start = 1; in starfive_rsa_montgomery_form()
118 rctx->csr.pka.ie = 1; in starfive_rsa_montgomery_form()
120 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET); in starfive_rsa_montgomery_form()
123 return -ETIMEDOUT; in starfive_rsa_montgomery_form()
125 rctx->csr.pka.v = 0; in starfive_rsa_montgomery_form()
126 rctx->csr.pka.cln_done = 1; in starfive_rsa_montgomery_form()
127 rctx->csr.pka.opsize = opsize; in starfive_rsa_montgomery_form()
128 rctx->csr.pka.exposize = opsize; in starfive_rsa_montgomery_form()
129 rctx->csr.pka.cmd = CRYPTO_CMD_PRE; in starfive_rsa_montgomery_form()
130 rctx->csr.pka.start = 1; in starfive_rsa_montgomery_form()
131 rctx->csr.pka.pre_expf = 1; in starfive_rsa_montgomery_form()
132 rctx->csr.pka.ie = 1; in starfive_rsa_montgomery_form()
134 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET); in starfive_rsa_montgomery_form()
137 return -ETIMEDOUT; in starfive_rsa_montgomery_form()
140 writel(in[count - loop], cryp->base + STARFIVE_PKA_CAER_OFFSET + loop * 4); in starfive_rsa_montgomery_form()
144 writel(0, cryp->base + STARFIVE_PKA_CAER_OFFSET + loop * 4); in starfive_rsa_montgomery_form()
146 rctx->csr.pka.v = 0; in starfive_rsa_montgomery_form()
147 rctx->csr.pka.cln_done = 1; in starfive_rsa_montgomery_form()
148 rctx->csr.pka.opsize = opsize; in starfive_rsa_montgomery_form()
149 rctx->csr.pka.exposize = opsize; in starfive_rsa_montgomery_form()
150 rctx->csr.pka.cmd = CRYPTO_CMD_ARN; in starfive_rsa_montgomery_form()
151 rctx->csr.pka.start = 1; in starfive_rsa_montgomery_form()
152 rctx->csr.pka.ie = 1; in starfive_rsa_montgomery_form()
154 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET); in starfive_rsa_montgomery_form()
157 return -ETIMEDOUT; in starfive_rsa_montgomery_form()
161 temp = readl(cryp->base + STARFIVE_PKA_CAAR_OFFSET + 0x4 * loop); in starfive_rsa_montgomery_form()
162 out[opsize - loop] = temp; in starfive_rsa_montgomery_form()
171 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_rsa_cpu_start()
172 struct starfive_cryp_request_ctx *rctx = ctx->rctx; in starfive_rsa_cpu_start()
173 struct starfive_rsa_key *key = &ctx->rsa_key; in starfive_rsa_cpu_start()
179 opsize = (key_sz - 1) >> 2; in starfive_rsa_cpu_start()
183 return -ENOMEM; in starfive_rsa_cpu_start()
185 ret = starfive_rsa_montgomery_form(ctx, mta, (u32 *)rctx->rsa_data, in starfive_rsa_cpu_start()
188 dev_err_probe(cryp->dev, ret, "Conversion to Montgomery failed"); in starfive_rsa_cpu_start()
193 writel(mta[opsize - loop], in starfive_rsa_cpu_start()
194 cryp->base + STARFIVE_PKA_CAER_OFFSET + loop * 4); in starfive_rsa_cpu_start()
196 for (loop = key->bitlen - 1; loop > 0; loop--) { in starfive_rsa_cpu_start()
197 mlen = starfive_rsa_get_nbit(de, loop - 1, key_sz); in starfive_rsa_cpu_start()
199 rctx->csr.pka.v = 0; in starfive_rsa_cpu_start()
200 rctx->csr.pka.cln_done = 1; in starfive_rsa_cpu_start()
201 rctx->csr.pka.opsize = opsize; in starfive_rsa_cpu_start()
202 rctx->csr.pka.exposize = opsize; in starfive_rsa_cpu_start()
203 rctx->csr.pka.cmd = CRYPTO_CMD_AARN; in starfive_rsa_cpu_start()
204 rctx->csr.pka.start = 1; in starfive_rsa_cpu_start()
205 rctx->csr.pka.ie = 1; in starfive_rsa_cpu_start()
207 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET); in starfive_rsa_cpu_start()
209 ret = -ETIMEDOUT; in starfive_rsa_cpu_start()
214 rctx->csr.pka.v = 0; in starfive_rsa_cpu_start()
215 rctx->csr.pka.cln_done = 1; in starfive_rsa_cpu_start()
216 rctx->csr.pka.opsize = opsize; in starfive_rsa_cpu_start()
217 rctx->csr.pka.exposize = opsize; in starfive_rsa_cpu_start()
218 rctx->csr.pka.cmd = CRYPTO_CMD_AERN; in starfive_rsa_cpu_start()
219 rctx->csr.pka.start = 1; in starfive_rsa_cpu_start()
220 rctx->csr.pka.ie = 1; in starfive_rsa_cpu_start()
222 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET); in starfive_rsa_cpu_start()
230 temp = readl(cryp->base + STARFIVE_PKA_CAAR_OFFSET + 0x4 * loop); in starfive_rsa_cpu_start()
231 result[opsize - loop] = temp; in starfive_rsa_cpu_start()
236 dev_err_probe(cryp->dev, ret, "Conversion from Montgomery failed"); in starfive_rsa_cpu_start()
250 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_rsa_enc_core()
251 struct starfive_cryp_request_ctx *rctx = ctx->rctx; in starfive_rsa_enc_core()
252 struct starfive_rsa_key *key = &ctx->rsa_key; in starfive_rsa_enc_core()
255 writel(STARFIVE_RSA_RESET, cryp->base + STARFIVE_PKA_CACR_OFFSET); in starfive_rsa_enc_core()
257 if (!IS_ALIGNED(rctx->total, sizeof(u32))) { in starfive_rsa_enc_core()
258 shift = sizeof(u32) - (rctx->total & 0x3); in starfive_rsa_enc_core()
259 memset(rctx->rsa_data, 0, shift); in starfive_rsa_enc_core()
262 rctx->total = sg_copy_to_buffer(rctx->in_sg, sg_nents(rctx->in_sg), in starfive_rsa_enc_core()
263 rctx->rsa_data + shift, rctx->total); in starfive_rsa_enc_core()
266 key->bitlen = key->e_bitlen; in starfive_rsa_enc_core()
267 ret = starfive_rsa_start(ctx, rctx->rsa_data, key->e, in starfive_rsa_enc_core()
268 key->n, key->key_sz); in starfive_rsa_enc_core()
270 key->bitlen = key->d_bitlen; in starfive_rsa_enc_core()
271 ret = starfive_rsa_start(ctx, rctx->rsa_data, key->d, in starfive_rsa_enc_core()
272 key->n, key->key_sz); in starfive_rsa_enc_core()
278 sg_copy_buffer(rctx->out_sg, sg_nents(rctx->out_sg), in starfive_rsa_enc_core()
279 rctx->rsa_data, key->key_sz, 0, 0); in starfive_rsa_enc_core()
282 writel(STARFIVE_RSA_RESET, cryp->base + STARFIVE_PKA_CACR_OFFSET); in starfive_rsa_enc_core()
290 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_rsa_enc()
291 struct starfive_rsa_key *key = &ctx->rsa_key; in starfive_rsa_enc()
295 if (!key->key_sz) { in starfive_rsa_enc()
296 akcipher_request_set_tfm(req, ctx->akcipher_fbk); in starfive_rsa_enc()
302 if (unlikely(!key->n || !key->e)) in starfive_rsa_enc()
303 return -EINVAL; in starfive_rsa_enc()
305 if (req->dst_len < key->key_sz) in starfive_rsa_enc()
306 return dev_err_probe(cryp->dev, -EOVERFLOW, in starfive_rsa_enc()
309 rctx->in_sg = req->src; in starfive_rsa_enc()
310 rctx->out_sg = req->dst; in starfive_rsa_enc()
311 rctx->total = req->src_len; in starfive_rsa_enc()
312 ctx->rctx = rctx; in starfive_rsa_enc()
321 struct starfive_cryp_dev *cryp = ctx->cryp; in starfive_rsa_dec()
322 struct starfive_rsa_key *key = &ctx->rsa_key; in starfive_rsa_dec()
326 if (!key->key_sz) { in starfive_rsa_dec()
327 akcipher_request_set_tfm(req, ctx->akcipher_fbk); in starfive_rsa_dec()
333 if (unlikely(!key->n || !key->d)) in starfive_rsa_dec()
334 return -EINVAL; in starfive_rsa_dec()
336 if (req->dst_len < key->key_sz) in starfive_rsa_dec()
337 return dev_err_probe(cryp->dev, -EOVERFLOW, in starfive_rsa_dec()
340 rctx->in_sg = req->src; in starfive_rsa_dec()
341 rctx->out_sg = req->dst; in starfive_rsa_dec()
342 ctx->rctx = rctx; in starfive_rsa_dec()
343 rctx->total = req->src_len; in starfive_rsa_dec()
357 vlen--; in starfive_rsa_set_n()
359 rsa_key->key_sz = vlen; in starfive_rsa_set_n()
360 bitslen = rsa_key->key_sz << 3; in starfive_rsa_set_n()
364 return -EINVAL; in starfive_rsa_set_n()
366 ret = -ENOMEM; in starfive_rsa_set_n()
367 rsa_key->n = kmemdup(ptr, rsa_key->key_sz, GFP_KERNEL); in starfive_rsa_set_n()
368 if (!rsa_key->n) in starfive_rsa_set_n()
373 rsa_key->key_sz = 0; in starfive_rsa_set_n()
374 rsa_key->n = NULL; in starfive_rsa_set_n()
388 vlen--; in starfive_rsa_set_e()
392 if (!rsa_key->key_sz || !vlen || vlen > rsa_key->key_sz) { in starfive_rsa_set_e()
393 rsa_key->e = NULL; in starfive_rsa_set_e()
394 return -EINVAL; in starfive_rsa_set_e()
397 rsa_key->e = kzalloc(rsa_key->key_sz, GFP_KERNEL); in starfive_rsa_set_e()
398 if (!rsa_key->e) in starfive_rsa_set_e()
399 return -ENOMEM; in starfive_rsa_set_e()
401 for (loop = 8; loop > 0; loop--) { in starfive_rsa_set_e()
402 if (pt >> (loop - 1)) in starfive_rsa_set_e()
406 rsa_key->e_bitlen = (vlen - 1) * 8 + loop; in starfive_rsa_set_e()
408 memcpy(rsa_key->e + (rsa_key->key_sz - vlen), ptr, vlen); in starfive_rsa_set_e()
423 vlen--; in starfive_rsa_set_d()
427 ret = -EINVAL; in starfive_rsa_set_d()
428 if (!rsa_key->key_sz || !vlen || vlen > rsa_key->key_sz) in starfive_rsa_set_d()
431 ret = -ENOMEM; in starfive_rsa_set_d()
432 rsa_key->d = kzalloc(rsa_key->key_sz, GFP_KERNEL); in starfive_rsa_set_d()
433 if (!rsa_key->d) in starfive_rsa_set_d()
436 for (loop = 8; loop > 0; loop--) { in starfive_rsa_set_d()
437 if (pt >> (loop - 1)) in starfive_rsa_set_d()
441 rsa_key->d_bitlen = (vlen - 1) * 8 + loop; in starfive_rsa_set_d()
443 memcpy(rsa_key->d + (rsa_key->key_sz - vlen), ptr, vlen); in starfive_rsa_set_d()
447 rsa_key->d = NULL; in starfive_rsa_set_d()
456 struct starfive_rsa_key *rsa_key = &ctx->rsa_key; in starfive_rsa_setkey()
486 if (!rsa_key->n || !rsa_key->e) { in starfive_rsa_setkey()
487 ret = -EINVAL; in starfive_rsa_setkey()
491 if (private && !rsa_key->d) { in starfive_rsa_setkey()
492 ret = -EINVAL; in starfive_rsa_setkey()
508 ret = crypto_akcipher_set_pub_key(ctx->akcipher_fbk, key, keylen); in starfive_rsa_set_pub_key()
521 ret = crypto_akcipher_set_priv_key(ctx->akcipher_fbk, key, keylen); in starfive_rsa_set_priv_key()
532 if (ctx->rsa_key.key_sz) in starfive_rsa_max_size()
533 return ctx->rsa_key.key_sz; in starfive_rsa_max_size()
535 return crypto_akcipher_maxsize(ctx->akcipher_fbk); in starfive_rsa_max_size()
542 ctx->cryp = starfive_cryp_find_dev(ctx); in starfive_rsa_init_tfm()
543 if (!ctx->cryp) in starfive_rsa_init_tfm()
544 return -ENODEV; in starfive_rsa_init_tfm()
546 ctx->akcipher_fbk = crypto_alloc_akcipher("rsa-generic", 0, 0); in starfive_rsa_init_tfm()
547 if (IS_ERR(ctx->akcipher_fbk)) in starfive_rsa_init_tfm()
548 return PTR_ERR(ctx->akcipher_fbk); in starfive_rsa_init_tfm()
559 struct starfive_rsa_key *key = (struct starfive_rsa_key *)&ctx->rsa_key; in starfive_rsa_exit_tfm()
561 crypto_free_akcipher(ctx->akcipher_fbk); in starfive_rsa_exit_tfm()
575 .cra_driver_name = "starfive-rsa",