Skip to content

Commit 4a651b1

Browse files
radua-nxpherbertx
authored andcommitted
crypto: caam - add support for RSA key form 3
CAAM RSA private key may have either of three representations. 1. The first representation consists of the pair (n, d), where the components have the following meanings: n the RSA modulus d the RSA private exponent 2. The second representation consists of the triplet (p, q, d), where the components have the following meanings: p the first prime factor of the RSA modulus n q the second prime factor of the RSA modulus n d the RSA private exponent 3. The third representation consists of the quintuple (p, q, dP, dQ, qInv), where the components have the following meanings: p the first prime factor of the RSA modulus n q the second prime factor of the RSA modulus n dP the first factors's CRT exponent dQ the second factors's CRT exponent qInv the (first) CRT coefficient The benefit of using the third or the second key form is lower computational cost for the decryption and signature operations. This patch adds support for the third RSA private key representations and extends caampkc to use the fastest key when all related components are present in the private key. Signed-off-by: Tudor Ambarus <tudor-dan.ambarus@nxp.com> Signed-off-by: Radu Alexe <radu.alexe@nxp.com> Signed-off-by: Horia Geantă <horia.geanta@nxp.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 52e26d7 commit 4a651b1

4 files changed

Lines changed: 291 additions & 2 deletions

File tree

‎drivers/crypto/caam/caampkc.c‎

Lines changed: 218 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
sizeof(struct rsa_priv_f1_pdb))
2121
#define DESC_RSA_PRIV_F2_LEN (2 * CAAM_CMD_SZ + \
2222
sizeof(struct rsa_priv_f2_pdb))
23+
#define DESC_RSA_PRIV_F3_LEN (2 * CAAM_CMD_SZ + \
24+
sizeof(struct rsa_priv_f3_pdb))
2325

2426
static void rsa_io_unmap(struct device *dev, struct rsa_edesc *edesc,
2527
struct akcipher_request *req)
@@ -73,6 +75,25 @@ static void rsa_priv_f2_unmap(struct device *dev, struct rsa_edesc *edesc,
7375
dma_unmap_single(dev, pdb->tmp2_dma, q_sz, DMA_TO_DEVICE);
7476
}
7577

78+
static void rsa_priv_f3_unmap(struct device *dev, struct rsa_edesc *edesc,
79+
struct akcipher_request *req)
80+
{
81+
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
82+
struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
83+
struct caam_rsa_key *key = &ctx->key;
84+
struct rsa_priv_f3_pdb *pdb = &edesc->pdb.priv_f3;
85+
size_t p_sz = key->p_sz;
86+
size_t q_sz = key->p_sz;
87+
88+
dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
89+
dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
90+
dma_unmap_single(dev, pdb->dp_dma, p_sz, DMA_TO_DEVICE);
91+
dma_unmap_single(dev, pdb->dq_dma, q_sz, DMA_TO_DEVICE);
92+
dma_unmap_single(dev, pdb->c_dma, p_sz, DMA_TO_DEVICE);
93+
dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_TO_DEVICE);
94+
dma_unmap_single(dev, pdb->tmp2_dma, q_sz, DMA_TO_DEVICE);
95+
}
96+
7697
/* RSA Job Completion handler */
7798
static void rsa_pub_done(struct device *dev, u32 *desc, u32 err, void *context)
7899
{
@@ -127,6 +148,24 @@ static void rsa_priv_f2_done(struct device *dev, u32 *desc, u32 err,
127148
akcipher_request_complete(req, err);
128149
}
129150

151+
static void rsa_priv_f3_done(struct device *dev, u32 *desc, u32 err,
152+
void *context)
153+
{
154+
struct akcipher_request *req = context;
155+
struct rsa_edesc *edesc;
156+
157+
if (err)
158+
caam_jr_strstatus(dev, err);
159+
160+
edesc = container_of(desc, struct rsa_edesc, hw_desc[0]);
161+
162+
rsa_priv_f3_unmap(dev, edesc, req);
163+
rsa_io_unmap(dev, edesc, req);
164+
kfree(edesc);
165+
166+
akcipher_request_complete(req, err);
167+
}
168+
130169
static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
131170
size_t desclen)
132171
{
@@ -370,6 +409,97 @@ static int set_rsa_priv_f2_pdb(struct akcipher_request *req,
370409
return -ENOMEM;
371410
}
372411

412+
static int set_rsa_priv_f3_pdb(struct akcipher_request *req,
413+
struct rsa_edesc *edesc)
414+
{
415+
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
416+
struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
417+
struct caam_rsa_key *key = &ctx->key;
418+
struct device *dev = ctx->dev;
419+
struct rsa_priv_f3_pdb *pdb = &edesc->pdb.priv_f3;
420+
int sec4_sg_index = 0;
421+
size_t p_sz = key->p_sz;
422+
size_t q_sz = key->p_sz;
423+
424+
pdb->p_dma = dma_map_single(dev, key->p, p_sz, DMA_TO_DEVICE);
425+
if (dma_mapping_error(dev, pdb->p_dma)) {
426+
dev_err(dev, "Unable to map RSA prime factor p memory\n");
427+
return -ENOMEM;
428+
}
429+
430+
pdb->q_dma = dma_map_single(dev, key->q, q_sz, DMA_TO_DEVICE);
431+
if (dma_mapping_error(dev, pdb->q_dma)) {
432+
dev_err(dev, "Unable to map RSA prime factor q memory\n");
433+
goto unmap_p;
434+
}
435+
436+
pdb->dp_dma = dma_map_single(dev, key->dp, p_sz, DMA_TO_DEVICE);
437+
if (dma_mapping_error(dev, pdb->dp_dma)) {
438+
dev_err(dev, "Unable to map RSA exponent dp memory\n");
439+
goto unmap_q;
440+
}
441+
442+
pdb->dq_dma = dma_map_single(dev, key->dq, q_sz, DMA_TO_DEVICE);
443+
if (dma_mapping_error(dev, pdb->dq_dma)) {
444+
dev_err(dev, "Unable to map RSA exponent dq memory\n");
445+
goto unmap_dp;
446+
}
447+
448+
pdb->c_dma = dma_map_single(dev, key->qinv, p_sz, DMA_TO_DEVICE);
449+
if (dma_mapping_error(dev, pdb->c_dma)) {
450+
dev_err(dev, "Unable to map RSA CRT coefficient qinv memory\n");
451+
goto unmap_dq;
452+
}
453+
454+
pdb->tmp1_dma = dma_map_single(dev, key->tmp1, p_sz, DMA_TO_DEVICE);
455+
if (dma_mapping_error(dev, pdb->tmp1_dma)) {
456+
dev_err(dev, "Unable to map RSA tmp1 memory\n");
457+
goto unmap_qinv;
458+
}
459+
460+
pdb->tmp2_dma = dma_map_single(dev, key->tmp2, q_sz, DMA_TO_DEVICE);
461+
if (dma_mapping_error(dev, pdb->tmp2_dma)) {
462+
dev_err(dev, "Unable to map RSA tmp2 memory\n");
463+
goto unmap_tmp1;
464+
}
465+
466+
if (edesc->src_nents > 1) {
467+
pdb->sgf |= RSA_PRIV_PDB_SGF_G;
468+
pdb->g_dma = edesc->sec4_sg_dma;
469+
sec4_sg_index += edesc->src_nents;
470+
} else {
471+
pdb->g_dma = sg_dma_address(req->src);
472+
}
473+
474+
if (edesc->dst_nents > 1) {
475+
pdb->sgf |= RSA_PRIV_PDB_SGF_F;
476+
pdb->f_dma = edesc->sec4_sg_dma +
477+
sec4_sg_index * sizeof(struct sec4_sg_entry);
478+
} else {
479+
pdb->f_dma = sg_dma_address(req->dst);
480+
}
481+
482+
pdb->sgf |= key->n_sz;
483+
pdb->p_q_len = (q_sz << RSA_PDB_Q_SHIFT) | p_sz;
484+
485+
return 0;
486+
487+
unmap_tmp1:
488+
dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_TO_DEVICE);
489+
unmap_qinv:
490+
dma_unmap_single(dev, pdb->c_dma, p_sz, DMA_TO_DEVICE);
491+
unmap_dq:
492+
dma_unmap_single(dev, pdb->dq_dma, q_sz, DMA_TO_DEVICE);
493+
unmap_dp:
494+
dma_unmap_single(dev, pdb->dp_dma, p_sz, DMA_TO_DEVICE);
495+
unmap_q:
496+
dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE);
497+
unmap_p:
498+
dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE);
499+
500+
return -ENOMEM;
501+
}
502+
373503
static int caam_rsa_enc(struct akcipher_request *req)
374504
{
375505
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
@@ -479,6 +609,39 @@ static int caam_rsa_dec_priv_f2(struct akcipher_request *req)
479609
return ret;
480610
}
481611

612+
static int caam_rsa_dec_priv_f3(struct akcipher_request *req)
613+
{
614+
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
615+
struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm);
616+
struct device *jrdev = ctx->dev;
617+
struct rsa_edesc *edesc;
618+
int ret;
619+
620+
/* Allocate extended descriptor */
621+
edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F3_LEN);
622+
if (IS_ERR(edesc))
623+
return PTR_ERR(edesc);
624+
625+
/* Set RSA Decrypt Protocol Data Block - Private Key Form #3 */
626+
ret = set_rsa_priv_f3_pdb(req, edesc);
627+
if (ret)
628+
goto init_fail;
629+
630+
/* Initialize Job Descriptor */
631+
init_rsa_priv_f3_desc(edesc->hw_desc, &edesc->pdb.priv_f3);
632+
633+
ret = caam_jr_enqueue(jrdev, edesc->hw_desc, rsa_priv_f3_done, req);
634+
if (!ret)
635+
return -EINPROGRESS;
636+
637+
rsa_priv_f3_unmap(jrdev, edesc, req);
638+
639+
init_fail:
640+
rsa_io_unmap(jrdev, edesc, req);
641+
kfree(edesc);
642+
return ret;
643+
}
644+
482645
static int caam_rsa_dec(struct akcipher_request *req)
483646
{
484647
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
@@ -495,7 +658,9 @@ static int caam_rsa_dec(struct akcipher_request *req)
495658
return -EOVERFLOW;
496659
}
497660

498-
if (key->priv_form == FORM2)
661+
if (key->priv_form == FORM3)
662+
ret = caam_rsa_dec_priv_f3(req);
663+
else if (key->priv_form == FORM2)
499664
ret = caam_rsa_dec_priv_f2(req);
500665
else
501666
ret = caam_rsa_dec_priv_f1(req);
@@ -508,6 +673,9 @@ static void caam_rsa_free_key(struct caam_rsa_key *key)
508673
kzfree(key->d);
509674
kzfree(key->p);
510675
kzfree(key->q);
676+
kzfree(key->dp);
677+
kzfree(key->dq);
678+
kzfree(key->qinv);
511679
kzfree(key->tmp1);
512680
kzfree(key->tmp2);
513681
kfree(key->e);
@@ -523,6 +691,34 @@ static void caam_rsa_drop_leading_zeros(const u8 **ptr, size_t *nbytes)
523691
}
524692
}
525693

694+
/**
695+
* caam_read_rsa_crt - Used for reading dP, dQ, qInv CRT members.
696+
* dP, dQ and qInv could decode to less than corresponding p, q length, as the
697+
* BER-encoding requires that the minimum number of bytes be used to encode the
698+
* integer. dP, dQ, qInv decoded values have to be zero-padded to appropriate
699+
* length.
700+
*
701+
* @ptr : pointer to {dP, dQ, qInv} CRT member
702+
* @nbytes: length in bytes of {dP, dQ, qInv} CRT member
703+
* @dstlen: length in bytes of corresponding p or q prime factor
704+
*/
705+
static u8 *caam_read_rsa_crt(const u8 *ptr, size_t nbytes, size_t dstlen)
706+
{
707+
u8 *dst;
708+
709+
caam_rsa_drop_leading_zeros(&ptr, &nbytes);
710+
if (!nbytes)
711+
return NULL;
712+
713+
dst = kzalloc(dstlen, GFP_DMA | GFP_KERNEL);
714+
if (!dst)
715+
return NULL;
716+
717+
memcpy(dst + (dstlen - nbytes), ptr, nbytes);
718+
719+
return dst;
720+
}
721+
526722
/**
527723
* caam_read_raw_data - Read a raw byte stream as a positive integer.
528724
* The function skips buffer's leading zeros, copies the remained data
@@ -629,8 +825,29 @@ static void caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx,
629825

630826
rsa_key->priv_form = FORM2;
631827

828+
rsa_key->dp = caam_read_rsa_crt(raw_key->dp, raw_key->dp_sz, p_sz);
829+
if (!rsa_key->dp)
830+
goto free_tmp2;
831+
832+
rsa_key->dq = caam_read_rsa_crt(raw_key->dq, raw_key->dq_sz, q_sz);
833+
if (!rsa_key->dq)
834+
goto free_dp;
835+
836+
rsa_key->qinv = caam_read_rsa_crt(raw_key->qinv, raw_key->qinv_sz,
837+
q_sz);
838+
if (!rsa_key->qinv)
839+
goto free_dq;
840+
841+
rsa_key->priv_form = FORM3;
842+
632843
return;
633844

845+
free_dq:
846+
kzfree(rsa_key->dq);
847+
free_dp:
848+
kzfree(rsa_key->dp);
849+
free_tmp2:
850+
kzfree(rsa_key->tmp2);
634851
free_tmp1:
635852
kzfree(rsa_key->tmp1);
636853
free_q:

‎drivers/crypto/caam/caampkc.h‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/**
1616
* caam_priv_key_form - CAAM RSA private key representation
17-
* CAAM RSA private key may have either of two forms.
17+
* CAAM RSA private key may have either of three forms.
1818
*
1919
* 1. The first representation consists of the pair (n, d), where the
2020
* components have the following meanings:
@@ -26,10 +26,22 @@
2626
* p the first prime factor of the RSA modulus n
2727
* q the second prime factor of the RSA modulus n
2828
* d the RSA private exponent
29+
*
30+
* 3. The third representation consists of the quintuple (p, q, dP, dQ, qInv),
31+
* where the components have the following meanings:
32+
* p the first prime factor of the RSA modulus n
33+
* q the second prime factor of the RSA modulus n
34+
* dP the first factors's CRT exponent
35+
* dQ the second factors's CRT exponent
36+
* qInv the (first) CRT coefficient
37+
*
38+
* The benefit of using the third or the second key form is lower computational
39+
* cost for the decryption and signature operations.
2940
*/
3041
enum caam_priv_key_form {
3142
FORM1,
3243
FORM2,
44+
FORM3
3345
};
3446

3547
/**
@@ -39,6 +51,9 @@ enum caam_priv_key_form {
3951
* @d : RSA private exponent raw byte stream
4052
* @p : RSA prime factor p of RSA modulus n
4153
* @q : RSA prime factor q of RSA modulus n
54+
* @dp : RSA CRT exponent of p
55+
* @dp : RSA CRT exponent of q
56+
* @qinv : RSA CRT coefficient
4257
* @tmp1 : CAAM uses this temporary buffer as internal state buffer.
4358
* It is assumed to be as long as p.
4459
* @tmp2 : CAAM uses this temporary buffer as internal state buffer.
@@ -56,6 +71,9 @@ struct caam_rsa_key {
5671
u8 *d;
5772
u8 *p;
5873
u8 *q;
74+
u8 *dp;
75+
u8 *dq;
76+
u8 *qinv;
5977
u8 *tmp1;
6078
u8 *tmp2;
6179
size_t n_sz;
@@ -96,6 +114,7 @@ struct rsa_edesc {
96114
struct rsa_pub_pdb pub;
97115
struct rsa_priv_f1_pdb priv_f1;
98116
struct rsa_priv_f2_pdb priv_f2;
117+
struct rsa_priv_f3_pdb priv_f3;
99118
} pdb;
100119
u32 hw_desc[];
101120
};
@@ -104,5 +123,6 @@ struct rsa_edesc {
104123
void init_rsa_pub_desc(u32 *desc, struct rsa_pub_pdb *pdb);
105124
void init_rsa_priv_f1_desc(u32 *desc, struct rsa_priv_f1_pdb *pdb);
106125
void init_rsa_priv_f2_desc(u32 *desc, struct rsa_priv_f2_pdb *pdb);
126+
void init_rsa_priv_f3_desc(u32 *desc, struct rsa_priv_f3_pdb *pdb);
107127

108128
#endif

‎drivers/crypto/caam/pdb.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ struct dsa_verify_pdb {
493493

494494
#define RSA_PRIV_KEY_FRM_1 0
495495
#define RSA_PRIV_KEY_FRM_2 1
496+
#define RSA_PRIV_KEY_FRM_3 2
496497

497498
/**
498499
* RSA Encrypt Protocol Data Block
@@ -554,4 +555,36 @@ struct rsa_priv_f2_pdb {
554555
u32 p_q_len;
555556
} __packed;
556557

558+
/**
559+
* RSA Decrypt PDB - Private Key Form #3
560+
* This is the RSA Chinese Reminder Theorem (CRT) form for two prime factors of
561+
* the RSA modulus.
562+
* @sgf : scatter-gather field
563+
* @g_dma : dma address of encrypted input data
564+
* @f_dma : dma address of output data
565+
* @c_dma : dma address of RSA CRT coefficient
566+
* @p_dma : dma address of RSA prime factor p of RSA modulus n
567+
* @q_dma : dma address of RSA prime factor q of RSA modulus n
568+
* @dp_dma : dma address of RSA CRT exponent of RSA prime factor p
569+
* @dp_dma : dma address of RSA CRT exponent of RSA prime factor q
570+
* @tmp1_dma: dma address of temporary buffer. CAAM uses this temporary buffer
571+
* as internal state buffer. It is assumed to be as long as p.
572+
* @tmp2_dma: dma address of temporary buffer. CAAM uses this temporary buffer
573+
* as internal state buffer. It is assumed to be as long as q.
574+
* @p_q_len : length in bytes of first two prime factors of the RSA modulus n
575+
*/
576+
struct rsa_priv_f3_pdb {
577+
u32 sgf;
578+
dma_addr_t g_dma;
579+
dma_addr_t f_dma;
580+
dma_addr_t c_dma;
581+
dma_addr_t p_dma;
582+
dma_addr_t q_dma;
583+
dma_addr_t dp_dma;
584+
dma_addr_t dq_dma;
585+
dma_addr_t tmp1_dma;
586+
dma_addr_t tmp2_dma;
587+
u32 p_q_len;
588+
} __packed;
589+
557590
#endif

0 commit comments

Comments
 (0)