xref: /OK3568_Linux_fs/kernel/crypto/rsa-pkcs1pad.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * RSA padding templates.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2015  Intel Corporation
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <crypto/algapi.h>
9*4882a593Smuzhiyun #include <crypto/akcipher.h>
10*4882a593Smuzhiyun #include <crypto/internal/akcipher.h>
11*4882a593Smuzhiyun #include <crypto/internal/rsa.h>
12*4882a593Smuzhiyun #include <linux/err.h>
13*4882a593Smuzhiyun #include <linux/init.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/random.h>
17*4882a593Smuzhiyun #include <linux/scatterlist.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun static const u8 rsa_digest_info_md5[] = {
23*4882a593Smuzhiyun 	0x30, 0x20, 0x30, 0x0c, 0x06, 0x08,
24*4882a593Smuzhiyun 	0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */
25*4882a593Smuzhiyun 	0x05, 0x00, 0x04, 0x10
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun static const u8 rsa_digest_info_sha1[] = {
29*4882a593Smuzhiyun 	0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
30*4882a593Smuzhiyun 	0x2b, 0x0e, 0x03, 0x02, 0x1a,
31*4882a593Smuzhiyun 	0x05, 0x00, 0x04, 0x14
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static const u8 rsa_digest_info_rmd160[] = {
35*4882a593Smuzhiyun 	0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
36*4882a593Smuzhiyun 	0x2b, 0x24, 0x03, 0x02, 0x01,
37*4882a593Smuzhiyun 	0x05, 0x00, 0x04, 0x14
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static const u8 rsa_digest_info_sha224[] = {
41*4882a593Smuzhiyun 	0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
42*4882a593Smuzhiyun 	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
43*4882a593Smuzhiyun 	0x05, 0x00, 0x04, 0x1c
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun static const u8 rsa_digest_info_sha256[] = {
47*4882a593Smuzhiyun 	0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
48*4882a593Smuzhiyun 	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
49*4882a593Smuzhiyun 	0x05, 0x00, 0x04, 0x20
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun static const u8 rsa_digest_info_sha384[] = {
53*4882a593Smuzhiyun 	0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
54*4882a593Smuzhiyun 	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
55*4882a593Smuzhiyun 	0x05, 0x00, 0x04, 0x30
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun static const u8 rsa_digest_info_sha512[] = {
59*4882a593Smuzhiyun 	0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
60*4882a593Smuzhiyun 	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
61*4882a593Smuzhiyun 	0x05, 0x00, 0x04, 0x40
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static const struct rsa_asn1_template {
65*4882a593Smuzhiyun 	const char	*name;
66*4882a593Smuzhiyun 	const u8	*data;
67*4882a593Smuzhiyun 	size_t		size;
68*4882a593Smuzhiyun } rsa_asn1_templates[] = {
69*4882a593Smuzhiyun #define _(X) { #X, rsa_digest_info_##X, sizeof(rsa_digest_info_##X) }
70*4882a593Smuzhiyun 	_(md5),
71*4882a593Smuzhiyun 	_(sha1),
72*4882a593Smuzhiyun 	_(rmd160),
73*4882a593Smuzhiyun 	_(sha256),
74*4882a593Smuzhiyun 	_(sha384),
75*4882a593Smuzhiyun 	_(sha512),
76*4882a593Smuzhiyun 	_(sha224),
77*4882a593Smuzhiyun 	{ NULL }
78*4882a593Smuzhiyun #undef _
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun 
rsa_lookup_asn1(const char * name)81*4882a593Smuzhiyun static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	const struct rsa_asn1_template *p;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	for (p = rsa_asn1_templates; p->name; p++)
86*4882a593Smuzhiyun 		if (strcmp(name, p->name) == 0)
87*4882a593Smuzhiyun 			return p;
88*4882a593Smuzhiyun 	return NULL;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun struct pkcs1pad_ctx {
92*4882a593Smuzhiyun 	struct crypto_akcipher *child;
93*4882a593Smuzhiyun 	unsigned int key_size;
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun struct pkcs1pad_inst_ctx {
97*4882a593Smuzhiyun 	struct crypto_akcipher_spawn spawn;
98*4882a593Smuzhiyun 	const struct rsa_asn1_template *digest_info;
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun struct pkcs1pad_request {
102*4882a593Smuzhiyun 	struct scatterlist in_sg[2], out_sg[1];
103*4882a593Smuzhiyun 	uint8_t *in_buf, *out_buf;
104*4882a593Smuzhiyun 	struct akcipher_request child_req;
105*4882a593Smuzhiyun };
106*4882a593Smuzhiyun 
pkcs1pad_set_pub_key(struct crypto_akcipher * tfm,const void * key,unsigned int keylen)107*4882a593Smuzhiyun static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
108*4882a593Smuzhiyun 		unsigned int keylen)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
111*4882a593Smuzhiyun 	int err;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	ctx->key_size = 0;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
116*4882a593Smuzhiyun 	if (err)
117*4882a593Smuzhiyun 		return err;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	/* Find out new modulus size from rsa implementation */
120*4882a593Smuzhiyun 	err = crypto_akcipher_maxsize(ctx->child);
121*4882a593Smuzhiyun 	if (err > PAGE_SIZE)
122*4882a593Smuzhiyun 		return -ENOTSUPP;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	ctx->key_size = err;
125*4882a593Smuzhiyun 	return 0;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
pkcs1pad_set_priv_key(struct crypto_akcipher * tfm,const void * key,unsigned int keylen)128*4882a593Smuzhiyun static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
129*4882a593Smuzhiyun 		unsigned int keylen)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
132*4882a593Smuzhiyun 	int err;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	ctx->key_size = 0;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
137*4882a593Smuzhiyun 	if (err)
138*4882a593Smuzhiyun 		return err;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	/* Find out new modulus size from rsa implementation */
141*4882a593Smuzhiyun 	err = crypto_akcipher_maxsize(ctx->child);
142*4882a593Smuzhiyun 	if (err > PAGE_SIZE)
143*4882a593Smuzhiyun 		return -ENOTSUPP;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	ctx->key_size = err;
146*4882a593Smuzhiyun 	return 0;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
pkcs1pad_get_max_size(struct crypto_akcipher * tfm)149*4882a593Smuzhiyun static unsigned int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/*
154*4882a593Smuzhiyun 	 * The maximum destination buffer size for the encrypt/sign operations
155*4882a593Smuzhiyun 	 * will be the same as for RSA, even though it's smaller for
156*4882a593Smuzhiyun 	 * decrypt/verify.
157*4882a593Smuzhiyun 	 */
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	return ctx->key_size;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
pkcs1pad_sg_set_buf(struct scatterlist * sg,void * buf,size_t len,struct scatterlist * next)162*4882a593Smuzhiyun static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
163*4882a593Smuzhiyun 		struct scatterlist *next)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	int nsegs = next ? 2 : 1;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	sg_init_table(sg, nsegs);
168*4882a593Smuzhiyun 	sg_set_buf(sg, buf, len);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	if (next)
171*4882a593Smuzhiyun 		sg_chain(sg, nsegs, next);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
pkcs1pad_encrypt_sign_complete(struct akcipher_request * req,int err)174*4882a593Smuzhiyun static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
177*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
178*4882a593Smuzhiyun 	struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
179*4882a593Smuzhiyun 	unsigned int pad_len;
180*4882a593Smuzhiyun 	unsigned int len;
181*4882a593Smuzhiyun 	u8 *out_buf;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	if (err)
184*4882a593Smuzhiyun 		goto out;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	len = req_ctx->child_req.dst_len;
187*4882a593Smuzhiyun 	pad_len = ctx->key_size - len;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/* Four billion to one */
190*4882a593Smuzhiyun 	if (likely(!pad_len))
191*4882a593Smuzhiyun 		goto out;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	out_buf = kzalloc(ctx->key_size, GFP_KERNEL);
194*4882a593Smuzhiyun 	err = -ENOMEM;
195*4882a593Smuzhiyun 	if (!out_buf)
196*4882a593Smuzhiyun 		goto out;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	sg_copy_to_buffer(req->dst, sg_nents_for_len(req->dst, len),
199*4882a593Smuzhiyun 			  out_buf + pad_len, len);
200*4882a593Smuzhiyun 	sg_copy_from_buffer(req->dst,
201*4882a593Smuzhiyun 			    sg_nents_for_len(req->dst, ctx->key_size),
202*4882a593Smuzhiyun 			    out_buf, ctx->key_size);
203*4882a593Smuzhiyun 	kfree_sensitive(out_buf);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun out:
206*4882a593Smuzhiyun 	req->dst_len = ctx->key_size;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	kfree(req_ctx->in_buf);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	return err;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
pkcs1pad_encrypt_sign_complete_cb(struct crypto_async_request * child_async_req,int err)213*4882a593Smuzhiyun static void pkcs1pad_encrypt_sign_complete_cb(
214*4882a593Smuzhiyun 		struct crypto_async_request *child_async_req, int err)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	struct akcipher_request *req = child_async_req->data;
217*4882a593Smuzhiyun 	struct crypto_async_request async_req;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	if (err == -EINPROGRESS)
220*4882a593Smuzhiyun 		return;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	async_req.data = req->base.data;
223*4882a593Smuzhiyun 	async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
224*4882a593Smuzhiyun 	async_req.flags = child_async_req->flags;
225*4882a593Smuzhiyun 	req->base.complete(&async_req,
226*4882a593Smuzhiyun 			pkcs1pad_encrypt_sign_complete(req, err));
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
pkcs1pad_encrypt(struct akcipher_request * req)229*4882a593Smuzhiyun static int pkcs1pad_encrypt(struct akcipher_request *req)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
232*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
233*4882a593Smuzhiyun 	struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
234*4882a593Smuzhiyun 	int err;
235*4882a593Smuzhiyun 	unsigned int i, ps_end;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	if (!ctx->key_size)
238*4882a593Smuzhiyun 		return -EINVAL;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	if (req->src_len > ctx->key_size - 11)
241*4882a593Smuzhiyun 		return -EOVERFLOW;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	if (req->dst_len < ctx->key_size) {
244*4882a593Smuzhiyun 		req->dst_len = ctx->key_size;
245*4882a593Smuzhiyun 		return -EOVERFLOW;
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
249*4882a593Smuzhiyun 				  GFP_KERNEL);
250*4882a593Smuzhiyun 	if (!req_ctx->in_buf)
251*4882a593Smuzhiyun 		return -ENOMEM;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	ps_end = ctx->key_size - req->src_len - 2;
254*4882a593Smuzhiyun 	req_ctx->in_buf[0] = 0x02;
255*4882a593Smuzhiyun 	for (i = 1; i < ps_end; i++)
256*4882a593Smuzhiyun 		req_ctx->in_buf[i] = 1 + prandom_u32_max(255);
257*4882a593Smuzhiyun 	req_ctx->in_buf[ps_end] = 0x00;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
260*4882a593Smuzhiyun 			ctx->key_size - 1 - req->src_len, req->src);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
263*4882a593Smuzhiyun 	akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
264*4882a593Smuzhiyun 			pkcs1pad_encrypt_sign_complete_cb, req);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	/* Reuse output buffer */
267*4882a593Smuzhiyun 	akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
268*4882a593Smuzhiyun 				   req->dst, ctx->key_size - 1, req->dst_len);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	err = crypto_akcipher_encrypt(&req_ctx->child_req);
271*4882a593Smuzhiyun 	if (err != -EINPROGRESS && err != -EBUSY)
272*4882a593Smuzhiyun 		return pkcs1pad_encrypt_sign_complete(req, err);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	return err;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun 
pkcs1pad_decrypt_complete(struct akcipher_request * req,int err)277*4882a593Smuzhiyun static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
280*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
281*4882a593Smuzhiyun 	struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
282*4882a593Smuzhiyun 	unsigned int dst_len;
283*4882a593Smuzhiyun 	unsigned int pos;
284*4882a593Smuzhiyun 	u8 *out_buf;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	if (err)
287*4882a593Smuzhiyun 		goto done;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	err = -EINVAL;
290*4882a593Smuzhiyun 	dst_len = req_ctx->child_req.dst_len;
291*4882a593Smuzhiyun 	if (dst_len < ctx->key_size - 1)
292*4882a593Smuzhiyun 		goto done;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	out_buf = req_ctx->out_buf;
295*4882a593Smuzhiyun 	if (dst_len == ctx->key_size) {
296*4882a593Smuzhiyun 		if (out_buf[0] != 0x00)
297*4882a593Smuzhiyun 			/* Decrypted value had no leading 0 byte */
298*4882a593Smuzhiyun 			goto done;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 		dst_len--;
301*4882a593Smuzhiyun 		out_buf++;
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	if (out_buf[0] != 0x02)
305*4882a593Smuzhiyun 		goto done;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	for (pos = 1; pos < dst_len; pos++)
308*4882a593Smuzhiyun 		if (out_buf[pos] == 0x00)
309*4882a593Smuzhiyun 			break;
310*4882a593Smuzhiyun 	if (pos < 9 || pos == dst_len)
311*4882a593Smuzhiyun 		goto done;
312*4882a593Smuzhiyun 	pos++;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	err = 0;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	if (req->dst_len < dst_len - pos)
317*4882a593Smuzhiyun 		err = -EOVERFLOW;
318*4882a593Smuzhiyun 	req->dst_len = dst_len - pos;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	if (!err)
321*4882a593Smuzhiyun 		sg_copy_from_buffer(req->dst,
322*4882a593Smuzhiyun 				sg_nents_for_len(req->dst, req->dst_len),
323*4882a593Smuzhiyun 				out_buf + pos, req->dst_len);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun done:
326*4882a593Smuzhiyun 	kfree_sensitive(req_ctx->out_buf);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	return err;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
pkcs1pad_decrypt_complete_cb(struct crypto_async_request * child_async_req,int err)331*4882a593Smuzhiyun static void pkcs1pad_decrypt_complete_cb(
332*4882a593Smuzhiyun 		struct crypto_async_request *child_async_req, int err)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	struct akcipher_request *req = child_async_req->data;
335*4882a593Smuzhiyun 	struct crypto_async_request async_req;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	if (err == -EINPROGRESS)
338*4882a593Smuzhiyun 		return;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	async_req.data = req->base.data;
341*4882a593Smuzhiyun 	async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
342*4882a593Smuzhiyun 	async_req.flags = child_async_req->flags;
343*4882a593Smuzhiyun 	req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err));
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
pkcs1pad_decrypt(struct akcipher_request * req)346*4882a593Smuzhiyun static int pkcs1pad_decrypt(struct akcipher_request *req)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
349*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
350*4882a593Smuzhiyun 	struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
351*4882a593Smuzhiyun 	int err;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	if (!ctx->key_size || req->src_len != ctx->key_size)
354*4882a593Smuzhiyun 		return -EINVAL;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
357*4882a593Smuzhiyun 	if (!req_ctx->out_buf)
358*4882a593Smuzhiyun 		return -ENOMEM;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
361*4882a593Smuzhiyun 			    ctx->key_size, NULL);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
364*4882a593Smuzhiyun 	akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
365*4882a593Smuzhiyun 			pkcs1pad_decrypt_complete_cb, req);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/* Reuse input buffer, output to a new buffer */
368*4882a593Smuzhiyun 	akcipher_request_set_crypt(&req_ctx->child_req, req->src,
369*4882a593Smuzhiyun 				   req_ctx->out_sg, req->src_len,
370*4882a593Smuzhiyun 				   ctx->key_size);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	err = crypto_akcipher_decrypt(&req_ctx->child_req);
373*4882a593Smuzhiyun 	if (err != -EINPROGRESS && err != -EBUSY)
374*4882a593Smuzhiyun 		return pkcs1pad_decrypt_complete(req, err);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	return err;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
pkcs1pad_sign(struct akcipher_request * req)379*4882a593Smuzhiyun static int pkcs1pad_sign(struct akcipher_request *req)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
382*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
383*4882a593Smuzhiyun 	struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
384*4882a593Smuzhiyun 	struct akcipher_instance *inst = akcipher_alg_instance(tfm);
385*4882a593Smuzhiyun 	struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
386*4882a593Smuzhiyun 	const struct rsa_asn1_template *digest_info = ictx->digest_info;
387*4882a593Smuzhiyun 	int err;
388*4882a593Smuzhiyun 	unsigned int ps_end, digest_size = 0;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	if (!ctx->key_size)
391*4882a593Smuzhiyun 		return -EINVAL;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	if (digest_info)
394*4882a593Smuzhiyun 		digest_size = digest_info->size;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (req->src_len + digest_size > ctx->key_size - 11)
397*4882a593Smuzhiyun 		return -EOVERFLOW;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	if (req->dst_len < ctx->key_size) {
400*4882a593Smuzhiyun 		req->dst_len = ctx->key_size;
401*4882a593Smuzhiyun 		return -EOVERFLOW;
402*4882a593Smuzhiyun 	}
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
405*4882a593Smuzhiyun 				  GFP_KERNEL);
406*4882a593Smuzhiyun 	if (!req_ctx->in_buf)
407*4882a593Smuzhiyun 		return -ENOMEM;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	ps_end = ctx->key_size - digest_size - req->src_len - 2;
410*4882a593Smuzhiyun 	req_ctx->in_buf[0] = 0x01;
411*4882a593Smuzhiyun 	memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
412*4882a593Smuzhiyun 	req_ctx->in_buf[ps_end] = 0x00;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	if (digest_info)
415*4882a593Smuzhiyun 		memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
416*4882a593Smuzhiyun 		       digest_info->size);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
419*4882a593Smuzhiyun 			ctx->key_size - 1 - req->src_len, req->src);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
422*4882a593Smuzhiyun 	akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
423*4882a593Smuzhiyun 			pkcs1pad_encrypt_sign_complete_cb, req);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	/* Reuse output buffer */
426*4882a593Smuzhiyun 	akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
427*4882a593Smuzhiyun 				   req->dst, ctx->key_size - 1, req->dst_len);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	err = crypto_akcipher_decrypt(&req_ctx->child_req);
430*4882a593Smuzhiyun 	if (err != -EINPROGRESS && err != -EBUSY)
431*4882a593Smuzhiyun 		return pkcs1pad_encrypt_sign_complete(req, err);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	return err;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
pkcs1pad_verify_complete(struct akcipher_request * req,int err)436*4882a593Smuzhiyun static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
439*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
440*4882a593Smuzhiyun 	struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
441*4882a593Smuzhiyun 	struct akcipher_instance *inst = akcipher_alg_instance(tfm);
442*4882a593Smuzhiyun 	struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
443*4882a593Smuzhiyun 	const struct rsa_asn1_template *digest_info = ictx->digest_info;
444*4882a593Smuzhiyun 	unsigned int dst_len;
445*4882a593Smuzhiyun 	unsigned int pos;
446*4882a593Smuzhiyun 	u8 *out_buf;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	if (err)
449*4882a593Smuzhiyun 		goto done;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	err = -EINVAL;
452*4882a593Smuzhiyun 	dst_len = req_ctx->child_req.dst_len;
453*4882a593Smuzhiyun 	if (dst_len < ctx->key_size - 1)
454*4882a593Smuzhiyun 		goto done;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	out_buf = req_ctx->out_buf;
457*4882a593Smuzhiyun 	if (dst_len == ctx->key_size) {
458*4882a593Smuzhiyun 		if (out_buf[0] != 0x00)
459*4882a593Smuzhiyun 			/* Decrypted value had no leading 0 byte */
460*4882a593Smuzhiyun 			goto done;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 		dst_len--;
463*4882a593Smuzhiyun 		out_buf++;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	err = -EBADMSG;
467*4882a593Smuzhiyun 	if (out_buf[0] != 0x01)
468*4882a593Smuzhiyun 		goto done;
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	for (pos = 1; pos < dst_len; pos++)
471*4882a593Smuzhiyun 		if (out_buf[pos] != 0xff)
472*4882a593Smuzhiyun 			break;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	if (pos < 9 || pos == dst_len || out_buf[pos] != 0x00)
475*4882a593Smuzhiyun 		goto done;
476*4882a593Smuzhiyun 	pos++;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	if (digest_info) {
479*4882a593Smuzhiyun 		if (digest_info->size > dst_len - pos)
480*4882a593Smuzhiyun 			goto done;
481*4882a593Smuzhiyun 		if (crypto_memneq(out_buf + pos, digest_info->data,
482*4882a593Smuzhiyun 				  digest_info->size))
483*4882a593Smuzhiyun 			goto done;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 		pos += digest_info->size;
486*4882a593Smuzhiyun 	}
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	err = 0;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	if (req->dst_len != dst_len - pos) {
491*4882a593Smuzhiyun 		err = -EKEYREJECTED;
492*4882a593Smuzhiyun 		req->dst_len = dst_len - pos;
493*4882a593Smuzhiyun 		goto done;
494*4882a593Smuzhiyun 	}
495*4882a593Smuzhiyun 	/* Extract appended digest. */
496*4882a593Smuzhiyun 	sg_pcopy_to_buffer(req->src,
497*4882a593Smuzhiyun 			   sg_nents_for_len(req->src,
498*4882a593Smuzhiyun 					    req->src_len + req->dst_len),
499*4882a593Smuzhiyun 			   req_ctx->out_buf + ctx->key_size,
500*4882a593Smuzhiyun 			   req->dst_len, req->src_len);
501*4882a593Smuzhiyun 	/* Do the actual verification step. */
502*4882a593Smuzhiyun 	if (memcmp(req_ctx->out_buf + ctx->key_size, out_buf + pos,
503*4882a593Smuzhiyun 		   req->dst_len) != 0)
504*4882a593Smuzhiyun 		err = -EKEYREJECTED;
505*4882a593Smuzhiyun done:
506*4882a593Smuzhiyun 	kfree_sensitive(req_ctx->out_buf);
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	return err;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun 
pkcs1pad_verify_complete_cb(struct crypto_async_request * child_async_req,int err)511*4882a593Smuzhiyun static void pkcs1pad_verify_complete_cb(
512*4882a593Smuzhiyun 		struct crypto_async_request *child_async_req, int err)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun 	struct akcipher_request *req = child_async_req->data;
515*4882a593Smuzhiyun 	struct crypto_async_request async_req;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	if (err == -EINPROGRESS)
518*4882a593Smuzhiyun 		return;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	async_req.data = req->base.data;
521*4882a593Smuzhiyun 	async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
522*4882a593Smuzhiyun 	async_req.flags = child_async_req->flags;
523*4882a593Smuzhiyun 	req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun  * The verify operation is here for completeness similar to the verification
528*4882a593Smuzhiyun  * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
529*4882a593Smuzhiyun  * as in RFC2437.  RFC2437 section 9.2 doesn't define any operation to
530*4882a593Smuzhiyun  * retrieve the DigestInfo from a signature, instead the user is expected
531*4882a593Smuzhiyun  * to call the sign operation to generate the expected signature and compare
532*4882a593Smuzhiyun  * signatures instead of the message-digests.
533*4882a593Smuzhiyun  */
pkcs1pad_verify(struct akcipher_request * req)534*4882a593Smuzhiyun static int pkcs1pad_verify(struct akcipher_request *req)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
537*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
538*4882a593Smuzhiyun 	struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
539*4882a593Smuzhiyun 	int err;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	if (WARN_ON(req->dst) ||
542*4882a593Smuzhiyun 	    WARN_ON(!req->dst_len) ||
543*4882a593Smuzhiyun 	    !ctx->key_size || req->src_len != ctx->key_size)
544*4882a593Smuzhiyun 		return -EINVAL;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	req_ctx->out_buf = kmalloc(ctx->key_size + req->dst_len, GFP_KERNEL);
547*4882a593Smuzhiyun 	if (!req_ctx->out_buf)
548*4882a593Smuzhiyun 		return -ENOMEM;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
551*4882a593Smuzhiyun 			    ctx->key_size, NULL);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
554*4882a593Smuzhiyun 	akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
555*4882a593Smuzhiyun 			pkcs1pad_verify_complete_cb, req);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	/* Reuse input buffer, output to a new buffer */
558*4882a593Smuzhiyun 	akcipher_request_set_crypt(&req_ctx->child_req, req->src,
559*4882a593Smuzhiyun 				   req_ctx->out_sg, req->src_len,
560*4882a593Smuzhiyun 				   ctx->key_size);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	err = crypto_akcipher_encrypt(&req_ctx->child_req);
563*4882a593Smuzhiyun 	if (err != -EINPROGRESS && err != -EBUSY)
564*4882a593Smuzhiyun 		return pkcs1pad_verify_complete(req, err);
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	return err;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
pkcs1pad_init_tfm(struct crypto_akcipher * tfm)569*4882a593Smuzhiyun static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	struct akcipher_instance *inst = akcipher_alg_instance(tfm);
572*4882a593Smuzhiyun 	struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
573*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
574*4882a593Smuzhiyun 	struct crypto_akcipher *child_tfm;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	child_tfm = crypto_spawn_akcipher(&ictx->spawn);
577*4882a593Smuzhiyun 	if (IS_ERR(child_tfm))
578*4882a593Smuzhiyun 		return PTR_ERR(child_tfm);
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	ctx->child = child_tfm;
581*4882a593Smuzhiyun 	return 0;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun 
pkcs1pad_exit_tfm(struct crypto_akcipher * tfm)584*4882a593Smuzhiyun static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
585*4882a593Smuzhiyun {
586*4882a593Smuzhiyun 	struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	crypto_free_akcipher(ctx->child);
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun 
pkcs1pad_free(struct akcipher_instance * inst)591*4882a593Smuzhiyun static void pkcs1pad_free(struct akcipher_instance *inst)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun 	struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
594*4882a593Smuzhiyun 	struct crypto_akcipher_spawn *spawn = &ctx->spawn;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	crypto_drop_akcipher(spawn);
597*4882a593Smuzhiyun 	kfree(inst);
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun 
pkcs1pad_create(struct crypto_template * tmpl,struct rtattr ** tb)600*4882a593Smuzhiyun static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun 	u32 mask;
603*4882a593Smuzhiyun 	struct akcipher_instance *inst;
604*4882a593Smuzhiyun 	struct pkcs1pad_inst_ctx *ctx;
605*4882a593Smuzhiyun 	struct akcipher_alg *rsa_alg;
606*4882a593Smuzhiyun 	const char *hash_name;
607*4882a593Smuzhiyun 	int err;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AKCIPHER, &mask);
610*4882a593Smuzhiyun 	if (err)
611*4882a593Smuzhiyun 		return err;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
614*4882a593Smuzhiyun 	if (!inst)
615*4882a593Smuzhiyun 		return -ENOMEM;
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	ctx = akcipher_instance_ctx(inst);
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	err = crypto_grab_akcipher(&ctx->spawn, akcipher_crypto_instance(inst),
620*4882a593Smuzhiyun 				   crypto_attr_alg_name(tb[1]), 0, mask);
621*4882a593Smuzhiyun 	if (err)
622*4882a593Smuzhiyun 		goto err_free_inst;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	rsa_alg = crypto_spawn_akcipher_alg(&ctx->spawn);
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	if (strcmp(rsa_alg->base.cra_name, "rsa") != 0) {
627*4882a593Smuzhiyun 		err = -EINVAL;
628*4882a593Smuzhiyun 		goto err_free_inst;
629*4882a593Smuzhiyun 	}
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	err = -ENAMETOOLONG;
632*4882a593Smuzhiyun 	hash_name = crypto_attr_alg_name(tb[2]);
633*4882a593Smuzhiyun 	if (IS_ERR(hash_name)) {
634*4882a593Smuzhiyun 		if (snprintf(inst->alg.base.cra_name,
635*4882a593Smuzhiyun 			     CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
636*4882a593Smuzhiyun 			     rsa_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
637*4882a593Smuzhiyun 			goto err_free_inst;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 		if (snprintf(inst->alg.base.cra_driver_name,
640*4882a593Smuzhiyun 			     CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
641*4882a593Smuzhiyun 			     rsa_alg->base.cra_driver_name) >=
642*4882a593Smuzhiyun 			     CRYPTO_MAX_ALG_NAME)
643*4882a593Smuzhiyun 			goto err_free_inst;
644*4882a593Smuzhiyun 	} else {
645*4882a593Smuzhiyun 		ctx->digest_info = rsa_lookup_asn1(hash_name);
646*4882a593Smuzhiyun 		if (!ctx->digest_info) {
647*4882a593Smuzhiyun 			err = -EINVAL;
648*4882a593Smuzhiyun 			goto err_free_inst;
649*4882a593Smuzhiyun 		}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 		if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
652*4882a593Smuzhiyun 			     "pkcs1pad(%s,%s)", rsa_alg->base.cra_name,
653*4882a593Smuzhiyun 			     hash_name) >= CRYPTO_MAX_ALG_NAME)
654*4882a593Smuzhiyun 			goto err_free_inst;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 		if (snprintf(inst->alg.base.cra_driver_name,
657*4882a593Smuzhiyun 			     CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s,%s)",
658*4882a593Smuzhiyun 			     rsa_alg->base.cra_driver_name,
659*4882a593Smuzhiyun 			     hash_name) >= CRYPTO_MAX_ALG_NAME)
660*4882a593Smuzhiyun 			goto err_free_inst;
661*4882a593Smuzhiyun 	}
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
664*4882a593Smuzhiyun 	inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	inst->alg.init = pkcs1pad_init_tfm;
667*4882a593Smuzhiyun 	inst->alg.exit = pkcs1pad_exit_tfm;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	inst->alg.encrypt = pkcs1pad_encrypt;
670*4882a593Smuzhiyun 	inst->alg.decrypt = pkcs1pad_decrypt;
671*4882a593Smuzhiyun 	inst->alg.sign = pkcs1pad_sign;
672*4882a593Smuzhiyun 	inst->alg.verify = pkcs1pad_verify;
673*4882a593Smuzhiyun 	inst->alg.set_pub_key = pkcs1pad_set_pub_key;
674*4882a593Smuzhiyun 	inst->alg.set_priv_key = pkcs1pad_set_priv_key;
675*4882a593Smuzhiyun 	inst->alg.max_size = pkcs1pad_get_max_size;
676*4882a593Smuzhiyun 	inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	inst->free = pkcs1pad_free;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	err = akcipher_register_instance(tmpl, inst);
681*4882a593Smuzhiyun 	if (err) {
682*4882a593Smuzhiyun err_free_inst:
683*4882a593Smuzhiyun 		pkcs1pad_free(inst);
684*4882a593Smuzhiyun 	}
685*4882a593Smuzhiyun 	return err;
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun struct crypto_template rsa_pkcs1pad_tmpl = {
689*4882a593Smuzhiyun 	.name = "pkcs1pad",
690*4882a593Smuzhiyun 	.create = pkcs1pad_create,
691*4882a593Smuzhiyun 	.module = THIS_MODULE,
692*4882a593Smuzhiyun };
693