Skip to content

Commit 8be0b84

Browse files
Salvatore Benedettoherbertx
Salvatore Benedetto
authored andcommitted
crypto: rsa - Store rest of the private key components
When parsing a private key, store all non-optional fields. These are required for enabling CRT mode for decrypt and verify Signed-off-by: Salvatore Benedetto <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent e24860f commit 8be0b84

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

Diff for: crypto/rsa_helper.c

+75
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,81 @@ int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
7878
return 0;
7979
}
8080

81+
int rsa_get_p(void *context, size_t hdrlen, unsigned char tag,
82+
const void *value, size_t vlen)
83+
{
84+
struct rsa_key *key = context;
85+
86+
/* invalid key provided */
87+
if (!value || !vlen || vlen > key->n_sz)
88+
return -EINVAL;
89+
90+
key->p = value;
91+
key->p_sz = vlen;
92+
93+
return 0;
94+
}
95+
96+
int rsa_get_q(void *context, size_t hdrlen, unsigned char tag,
97+
const void *value, size_t vlen)
98+
{
99+
struct rsa_key *key = context;
100+
101+
/* invalid key provided */
102+
if (!value || !vlen || vlen > key->n_sz)
103+
return -EINVAL;
104+
105+
key->q = value;
106+
key->q_sz = vlen;
107+
108+
return 0;
109+
}
110+
111+
int rsa_get_dp(void *context, size_t hdrlen, unsigned char tag,
112+
const void *value, size_t vlen)
113+
{
114+
struct rsa_key *key = context;
115+
116+
/* invalid key provided */
117+
if (!value || !vlen || vlen > key->n_sz)
118+
return -EINVAL;
119+
120+
key->dp = value;
121+
key->dp_sz = vlen;
122+
123+
return 0;
124+
}
125+
126+
int rsa_get_dq(void *context, size_t hdrlen, unsigned char tag,
127+
const void *value, size_t vlen)
128+
{
129+
struct rsa_key *key = context;
130+
131+
/* invalid key provided */
132+
if (!value || !vlen || vlen > key->n_sz)
133+
return -EINVAL;
134+
135+
key->dq = value;
136+
key->dq_sz = vlen;
137+
138+
return 0;
139+
}
140+
141+
int rsa_get_qinv(void *context, size_t hdrlen, unsigned char tag,
142+
const void *value, size_t vlen)
143+
{
144+
struct rsa_key *key = context;
145+
146+
/* invalid key provided */
147+
if (!value || !vlen || vlen > key->n_sz)
148+
return -EINVAL;
149+
150+
key->qinv = value;
151+
key->qinv_sz = vlen;
152+
153+
return 0;
154+
}
155+
81156
/**
82157
* rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
83158
* provided struct rsa_key, pointers to the raw key as is,

Diff for: crypto/rsaprivkey.asn1

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ RsaPrivKey ::= SEQUENCE {
33
n INTEGER ({ rsa_get_n }),
44
e INTEGER ({ rsa_get_e }),
55
d INTEGER ({ rsa_get_d }),
6-
prime1 INTEGER,
7-
prime2 INTEGER,
8-
exponent1 INTEGER,
9-
exponent2 INTEGER,
10-
coefficient INTEGER
6+
prime1 INTEGER ({ rsa_get_p }),
7+
prime2 INTEGER ({ rsa_get_q }),
8+
exponent1 INTEGER ({ rsa_get_dp }),
9+
exponent2 INTEGER ({ rsa_get_dq }),
10+
coefficient INTEGER ({ rsa_get_qinv })
1111
}

Diff for: include/crypto/internal/rsa.h

+20
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,37 @@
1919
* @n : RSA modulus raw byte stream
2020
* @e : RSA public exponent raw byte stream
2121
* @d : RSA private exponent raw byte stream
22+
* @p : RSA prime factor p of n raw byte stream
23+
* @q : RSA prime factor q of n raw byte stream
24+
* @dp : RSA exponent d mod (p - 1) raw byte stream
25+
* @dq : RSA exponent d mod (q - 1) raw byte stream
26+
* @qinv : RSA CRT coefficient q^(-1) mod p raw byte stream
2227
* @n_sz : length in bytes of RSA modulus n
2328
* @e_sz : length in bytes of RSA public exponent
2429
* @d_sz : length in bytes of RSA private exponent
30+
* @p_sz : length in bytes of p field
31+
* @q_sz : length in bytes of q field
32+
* @dp_sz : length in bytes of dp field
33+
* @dq_sz : length in bytes of dq field
34+
* @qinv_sz : length in bytes of qinv field
2535
*/
2636
struct rsa_key {
2737
const u8 *n;
2838
const u8 *e;
2939
const u8 *d;
40+
const u8 *p;
41+
const u8 *q;
42+
const u8 *dp;
43+
const u8 *dq;
44+
const u8 *qinv;
3045
size_t n_sz;
3146
size_t e_sz;
3247
size_t d_sz;
48+
size_t p_sz;
49+
size_t q_sz;
50+
size_t dp_sz;
51+
size_t dq_sz;
52+
size_t qinv_sz;
3353
};
3454

3555
int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,

0 commit comments

Comments
 (0)