1 // SPDX-License-Identifier: GPL-2.0
3 * StarFive Public Key Algo acceleration driver
5 * Copyright (c) 2022 StarFive Technology
8 #include <linux/crypto.h>
9 #include <linux/iopoll.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"
18 #define STARFIVE_PKA_REGS_OFFSET 0x400
19 #define STARFIVE_PKA_CACR_OFFSET (STARFIVE_PKA_REGS_OFFSET + 0x0)
20 #define STARFIVE_PKA_CASR_OFFSET (STARFIVE_PKA_REGS_OFFSET + 0x4)
21 #define STARFIVE_PKA_CAAR_OFFSET (STARFIVE_PKA_REGS_OFFSET + 0x8)
22 #define STARFIVE_PKA_CAER_OFFSET (STARFIVE_PKA_REGS_OFFSET + 0x108)
23 #define STARFIVE_PKA_CANR_OFFSET (STARFIVE_PKA_REGS_OFFSET + 0x208)
25 /* R ^ 2 mod N and N0' */
26 #define CRYPTO_CMD_PRE 0x0
27 /* A * R mod N ==> A */
28 #define CRYPTO_CMD_ARN 0x5
29 /* A * E * R mod N ==> A */
30 #define CRYPTO_CMD_AERN 0x6
31 /* A * A * R mod N ==> A */
32 #define CRYPTO_CMD_AARN 0x7
34 #define STARFIVE_RSA_MAX_KEYSZ 256
35 #define STARFIVE_RSA_RESET 0x2
37 static inline int starfive_pka_wait_done(struct starfive_cryp_ctx *ctx)
39 struct starfive_cryp_dev *cryp = ctx->cryp;
42 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_PKA_CASR_OFFSET, status,
43 status & STARFIVE_PKA_DONE, 10, 100000);
46 static void starfive_rsa_free_key(struct starfive_rsa_key *key)
51 kfree_sensitive(key->d);
52 kfree_sensitive(key->e);
53 kfree_sensitive(key->n);
54 memset(key, 0, sizeof(*key));
57 static unsigned int starfive_rsa_get_nbit(u8 *pa, u32 snum, int key_sz)
64 value = pa[key_sz - i - 1];
71 static int starfive_rsa_montgomery_form(struct starfive_cryp_ctx *ctx,
72 u32 *out, u32 *in, u8 mont,
73 u32 *mod, int bit_len)
75 struct starfive_cryp_dev *cryp = ctx->cryp;
76 struct starfive_cryp_request_ctx *rctx = ctx->rctx;
77 int count = rctx->total / sizeof(u32) - 1;
82 opsize = (bit_len - 1) >> 5;
85 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET);
87 for (loop = 0; loop <= opsize; loop++)
88 writel(mod[opsize - loop], cryp->base + STARFIVE_PKA_CANR_OFFSET + loop * 4);
92 rctx->csr.pka.cln_done = 1;
93 rctx->csr.pka.opsize = opsize;
94 rctx->csr.pka.exposize = opsize;
95 rctx->csr.pka.cmd = CRYPTO_CMD_PRE;
96 rctx->csr.pka.start = 1;
97 rctx->csr.pka.not_r2 = 1;
100 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET);
102 if (starfive_pka_wait_done(ctx))
105 for (loop = 0; loop <= opsize; loop++)
106 writel(in[opsize - loop], cryp->base + STARFIVE_PKA_CAAR_OFFSET + loop * 4);
108 writel(0x1000000, cryp->base + STARFIVE_PKA_CAER_OFFSET);
110 for (loop = 1; loop <= opsize; loop++)
111 writel(0, cryp->base + STARFIVE_PKA_CAER_OFFSET + loop * 4);
114 rctx->csr.pka.cln_done = 1;
115 rctx->csr.pka.opsize = opsize;
116 rctx->csr.pka.exposize = opsize;
117 rctx->csr.pka.cmd = CRYPTO_CMD_AERN;
118 rctx->csr.pka.start = 1;
119 rctx->csr.pka.ie = 1;
121 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET);
123 if (starfive_pka_wait_done(ctx))
127 rctx->csr.pka.cln_done = 1;
128 rctx->csr.pka.opsize = opsize;
129 rctx->csr.pka.exposize = opsize;
130 rctx->csr.pka.cmd = CRYPTO_CMD_PRE;
131 rctx->csr.pka.start = 1;
132 rctx->csr.pka.pre_expf = 1;
133 rctx->csr.pka.ie = 1;
135 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET);
137 if (starfive_pka_wait_done(ctx))
140 for (loop = 0; loop <= count; loop++)
141 writel(in[count - loop], cryp->base + STARFIVE_PKA_CAER_OFFSET + loop * 4);
143 /*pad with 0 up to opsize*/
144 for (loop = count + 1; loop <= opsize; loop++)
145 writel(0, cryp->base + STARFIVE_PKA_CAER_OFFSET + loop * 4);
148 rctx->csr.pka.cln_done = 1;
149 rctx->csr.pka.opsize = opsize;
150 rctx->csr.pka.exposize = opsize;
151 rctx->csr.pka.cmd = CRYPTO_CMD_ARN;
152 rctx->csr.pka.start = 1;
153 rctx->csr.pka.ie = 1;
155 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET);
157 if (starfive_pka_wait_done(ctx))
161 for (loop = 0; loop <= opsize; loop++) {
162 temp = readl(cryp->base + STARFIVE_PKA_CAAR_OFFSET + 0x4 * loop);
163 out[opsize - loop] = temp;
169 static int starfive_rsa_cpu_start(struct starfive_cryp_ctx *ctx, u32 *result,
170 u8 *de, u32 *n, int key_sz)
172 struct starfive_cryp_dev *cryp = ctx->cryp;
173 struct starfive_cryp_request_ctx *rctx = ctx->rctx;
174 struct starfive_rsa_key *key = &ctx->rsa_key;
177 int opsize, mlen, loop;
180 opsize = (key_sz - 1) >> 2;
182 mta = kmalloc(key_sz, GFP_KERNEL);
186 ret = starfive_rsa_montgomery_form(ctx, mta, (u32 *)rctx->rsa_data,
189 dev_err_probe(cryp->dev, ret, "Conversion to Montgomery failed");
193 for (loop = 0; loop <= opsize; loop++)
194 writel(mta[opsize - loop],
195 cryp->base + STARFIVE_PKA_CAER_OFFSET + loop * 4);
197 for (loop = key->bitlen - 1; loop > 0; loop--) {
198 mlen = starfive_rsa_get_nbit(de, loop - 1, key_sz);
201 rctx->csr.pka.cln_done = 1;
202 rctx->csr.pka.opsize = opsize;
203 rctx->csr.pka.exposize = opsize;
204 rctx->csr.pka.cmd = CRYPTO_CMD_AARN;
205 rctx->csr.pka.start = 1;
206 rctx->csr.pka.ie = 1;
208 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET);
211 if (starfive_pka_wait_done(ctx))
216 rctx->csr.pka.cln_done = 1;
217 rctx->csr.pka.opsize = opsize;
218 rctx->csr.pka.exposize = opsize;
219 rctx->csr.pka.cmd = CRYPTO_CMD_AERN;
220 rctx->csr.pka.start = 1;
221 rctx->csr.pka.ie = 1;
223 writel(rctx->csr.pka.v, cryp->base + STARFIVE_PKA_CACR_OFFSET);
225 if (starfive_pka_wait_done(ctx))
230 for (loop = 0; loop <= opsize; loop++) {
231 temp = readl(cryp->base + STARFIVE_PKA_CAAR_OFFSET + 0x4 * loop);
232 result[opsize - loop] = temp;
235 ret = starfive_rsa_montgomery_form(ctx, result, result, 1, n, key_sz << 3);
237 dev_err_probe(cryp->dev, ret, "Conversion from Montgomery failed");
243 static int starfive_rsa_start(struct starfive_cryp_ctx *ctx, u8 *result,
244 u8 *de, u8 *n, int key_sz)
246 return starfive_rsa_cpu_start(ctx, (u32 *)result, de, (u32 *)n, key_sz);
249 static int starfive_rsa_enc_core(struct starfive_cryp_ctx *ctx, int enc)
251 struct starfive_cryp_dev *cryp = ctx->cryp;
252 struct starfive_cryp_request_ctx *rctx = ctx->rctx;
253 struct starfive_rsa_key *key = &ctx->rsa_key;
256 writel(STARFIVE_RSA_RESET, cryp->base + STARFIVE_PKA_CACR_OFFSET);
258 rctx->total = sg_copy_to_buffer(rctx->in_sg, rctx->nents,
259 rctx->rsa_data, rctx->total);
262 key->bitlen = key->e_bitlen;
263 ret = starfive_rsa_start(ctx, rctx->rsa_data, key->e,
264 key->n, key->key_sz);
266 key->bitlen = key->d_bitlen;
267 ret = starfive_rsa_start(ctx, rctx->rsa_data, key->d,
268 key->n, key->key_sz);
274 sg_copy_buffer(rctx->out_sg, sg_nents(rctx->out_sg),
275 rctx->rsa_data, key->key_sz, 0, 0);
278 writel(STARFIVE_RSA_RESET, cryp->base + STARFIVE_PKA_CACR_OFFSET);
282 static int starfive_rsa_enc(struct akcipher_request *req)
284 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
285 struct starfive_cryp_ctx *ctx = akcipher_tfm_ctx(tfm);
286 struct starfive_cryp_dev *cryp = ctx->cryp;
287 struct starfive_rsa_key *key = &ctx->rsa_key;
288 struct starfive_cryp_request_ctx *rctx = akcipher_request_ctx(req);
292 akcipher_request_set_tfm(req, ctx->akcipher_fbk);
293 ret = crypto_akcipher_encrypt(req);
294 akcipher_request_set_tfm(req, tfm);
298 if (unlikely(!key->n || !key->e))
301 if (req->dst_len < key->key_sz)
302 return dev_err_probe(cryp->dev, -EOVERFLOW,
303 "Output buffer length less than parameter n\n");
305 rctx->in_sg = req->src;
306 rctx->out_sg = req->dst;
307 rctx->total = req->src_len;
308 rctx->nents = sg_nents(rctx->in_sg);
311 return starfive_rsa_enc_core(ctx, 1);
314 static int starfive_rsa_dec(struct akcipher_request *req)
316 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
317 struct starfive_cryp_ctx *ctx = akcipher_tfm_ctx(tfm);
318 struct starfive_cryp_dev *cryp = ctx->cryp;
319 struct starfive_rsa_key *key = &ctx->rsa_key;
320 struct starfive_cryp_request_ctx *rctx = akcipher_request_ctx(req);
324 akcipher_request_set_tfm(req, ctx->akcipher_fbk);
325 ret = crypto_akcipher_decrypt(req);
326 akcipher_request_set_tfm(req, tfm);
330 if (unlikely(!key->n || !key->d))
333 if (req->dst_len < key->key_sz)
334 return dev_err_probe(cryp->dev, -EOVERFLOW,
335 "Output buffer length less than parameter n\n");
337 rctx->in_sg = req->src;
338 rctx->out_sg = req->dst;
340 rctx->total = req->src_len;
342 return starfive_rsa_enc_core(ctx, 0);
345 static int starfive_rsa_set_n(struct starfive_rsa_key *rsa_key,
346 const char *value, size_t vlen)
348 const char *ptr = value;
349 unsigned int bitslen;
352 while (!*ptr && vlen) {
356 rsa_key->key_sz = vlen;
357 bitslen = rsa_key->key_sz << 3;
359 /* check valid key size */
364 rsa_key->n = kmemdup(ptr, rsa_key->key_sz, GFP_KERNEL);
372 starfive_rsa_free_key(rsa_key);
376 static int starfive_rsa_set_e(struct starfive_rsa_key *rsa_key,
377 const char *value, size_t vlen)
379 const char *ptr = value;
383 while (!*ptr && vlen) {
389 if (!rsa_key->key_sz || !vlen || vlen > rsa_key->key_sz) {
394 rsa_key->e = kzalloc(rsa_key->key_sz, GFP_KERNEL);
398 for (loop = 8; loop > 0; loop--) {
399 if (pt >> (loop - 1))
403 rsa_key->e_bitlen = (vlen - 1) * 8 + loop;
405 memcpy(rsa_key->e + (rsa_key->key_sz - vlen), ptr, vlen);
410 static int starfive_rsa_set_d(struct starfive_rsa_key *rsa_key,
411 const char *value, size_t vlen)
413 const char *ptr = value;
418 while (!*ptr && vlen) {
425 if (!rsa_key->key_sz || !vlen || vlen > rsa_key->key_sz)
429 rsa_key->d = kzalloc(rsa_key->key_sz, GFP_KERNEL);
433 for (loop = 8; loop > 0; loop--) {
434 if (pt >> (loop - 1))
438 rsa_key->d_bitlen = (vlen - 1) * 8 + loop;
440 memcpy(rsa_key->d + (rsa_key->key_sz - vlen), ptr, vlen);
448 static int starfive_rsa_setkey(struct crypto_akcipher *tfm, const void *key,
449 unsigned int keylen, bool private)
451 struct starfive_cryp_ctx *ctx = akcipher_tfm_ctx(tfm);
452 struct rsa_key raw_key = {NULL};
453 struct starfive_rsa_key *rsa_key = &ctx->rsa_key;
457 ret = rsa_parse_priv_key(&raw_key, key, keylen);
459 ret = rsa_parse_pub_key(&raw_key, key, keylen);
463 starfive_rsa_free_key(rsa_key);
465 /* Use fallback for mod > 256 + 1 byte prefix */
466 if (raw_key.n_sz > STARFIVE_RSA_MAX_KEYSZ + 1)
469 ret = starfive_rsa_set_n(rsa_key, raw_key.n, raw_key.n_sz);
473 ret = starfive_rsa_set_e(rsa_key, raw_key.e, raw_key.e_sz);
478 ret = starfive_rsa_set_d(rsa_key, raw_key.d, raw_key.d_sz);
483 if (!rsa_key->n || !rsa_key->e) {
488 if (private && !rsa_key->d) {
495 starfive_rsa_free_key(rsa_key);
499 static int starfive_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
502 struct starfive_cryp_ctx *ctx = akcipher_tfm_ctx(tfm);
505 ret = crypto_akcipher_set_pub_key(ctx->akcipher_fbk, key, keylen);
509 return starfive_rsa_setkey(tfm, key, keylen, false);
512 static int starfive_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
515 struct starfive_cryp_ctx *ctx = akcipher_tfm_ctx(tfm);
518 ret = crypto_akcipher_set_priv_key(ctx->akcipher_fbk, key, keylen);
522 return starfive_rsa_setkey(tfm, key, keylen, true);
525 static unsigned int starfive_rsa_max_size(struct crypto_akcipher *tfm)
527 struct starfive_cryp_ctx *ctx = akcipher_tfm_ctx(tfm);
529 if (ctx->rsa_key.key_sz)
530 return ctx->rsa_key.key_sz;
532 return crypto_akcipher_maxsize(ctx->akcipher_fbk);
535 static int starfive_rsa_init_tfm(struct crypto_akcipher *tfm)
537 struct starfive_cryp_ctx *ctx = akcipher_tfm_ctx(tfm);
539 ctx->cryp = starfive_cryp_find_dev(ctx);
543 ctx->akcipher_fbk = crypto_alloc_akcipher("rsa-generic", 0, 0);
544 if (IS_ERR(ctx->akcipher_fbk))
545 return PTR_ERR(ctx->akcipher_fbk);
547 akcipher_set_reqsize(tfm, sizeof(struct starfive_cryp_request_ctx) +
548 sizeof(struct crypto_akcipher) + 32);
553 static void starfive_rsa_exit_tfm(struct crypto_akcipher *tfm)
555 struct starfive_cryp_ctx *ctx = akcipher_tfm_ctx(tfm);
556 struct starfive_rsa_key *key = (struct starfive_rsa_key *)&ctx->rsa_key;
558 crypto_free_akcipher(ctx->akcipher_fbk);
559 starfive_rsa_free_key(key);
562 static struct akcipher_alg starfive_rsa = {
563 .encrypt = starfive_rsa_enc,
564 .decrypt = starfive_rsa_dec,
565 .sign = starfive_rsa_dec,
566 .verify = starfive_rsa_enc,
567 .set_pub_key = starfive_rsa_set_pub_key,
568 .set_priv_key = starfive_rsa_set_priv_key,
569 .max_size = starfive_rsa_max_size,
570 .init = starfive_rsa_init_tfm,
571 .exit = starfive_rsa_exit_tfm,
574 .cra_driver_name = "starfive-rsa",
575 .cra_flags = CRYPTO_ALG_TYPE_AKCIPHER |
576 CRYPTO_ALG_NEED_FALLBACK,
577 .cra_priority = 3000,
578 .cra_module = THIS_MODULE,
579 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
583 int starfive_rsa_register_algs(void)
585 return crypto_register_akcipher(&starfive_rsa);
588 void starfive_rsa_unregister_algs(void)
590 crypto_unregister_akcipher(&starfive_rsa);