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