Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * RSA padding templates.
0004  *
0005  * Copyright (c) 2015  Intel Corporation
0006  */
0007 
0008 #include <crypto/algapi.h>
0009 #include <crypto/akcipher.h>
0010 #include <crypto/internal/akcipher.h>
0011 #include <crypto/internal/rsa.h>
0012 #include <linux/err.h>
0013 #include <linux/init.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/random.h>
0017 #include <linux/scatterlist.h>
0018 
0019 /*
0020  * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
0021  */
0022 static const u8 rsa_digest_info_md5[] = {
0023     0x30, 0x20, 0x30, 0x0c, 0x06, 0x08,
0024     0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */
0025     0x05, 0x00, 0x04, 0x10
0026 };
0027 
0028 static const u8 rsa_digest_info_sha1[] = {
0029     0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
0030     0x2b, 0x0e, 0x03, 0x02, 0x1a,
0031     0x05, 0x00, 0x04, 0x14
0032 };
0033 
0034 static const u8 rsa_digest_info_rmd160[] = {
0035     0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
0036     0x2b, 0x24, 0x03, 0x02, 0x01,
0037     0x05, 0x00, 0x04, 0x14
0038 };
0039 
0040 static const u8 rsa_digest_info_sha224[] = {
0041     0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
0042     0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
0043     0x05, 0x00, 0x04, 0x1c
0044 };
0045 
0046 static const u8 rsa_digest_info_sha256[] = {
0047     0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
0048     0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
0049     0x05, 0x00, 0x04, 0x20
0050 };
0051 
0052 static const u8 rsa_digest_info_sha384[] = {
0053     0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
0054     0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
0055     0x05, 0x00, 0x04, 0x30
0056 };
0057 
0058 static const u8 rsa_digest_info_sha512[] = {
0059     0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
0060     0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
0061     0x05, 0x00, 0x04, 0x40
0062 };
0063 
0064 static const struct rsa_asn1_template {
0065     const char  *name;
0066     const u8    *data;
0067     size_t      size;
0068 } rsa_asn1_templates[] = {
0069 #define _(X) { #X, rsa_digest_info_##X, sizeof(rsa_digest_info_##X) }
0070     _(md5),
0071     _(sha1),
0072     _(rmd160),
0073     _(sha256),
0074     _(sha384),
0075     _(sha512),
0076     _(sha224),
0077     { NULL }
0078 #undef _
0079 };
0080 
0081 static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)
0082 {
0083     const struct rsa_asn1_template *p;
0084 
0085     for (p = rsa_asn1_templates; p->name; p++)
0086         if (strcmp(name, p->name) == 0)
0087             return p;
0088     return NULL;
0089 }
0090 
0091 struct pkcs1pad_ctx {
0092     struct crypto_akcipher *child;
0093     unsigned int key_size;
0094 };
0095 
0096 struct pkcs1pad_inst_ctx {
0097     struct crypto_akcipher_spawn spawn;
0098     const struct rsa_asn1_template *digest_info;
0099 };
0100 
0101 struct pkcs1pad_request {
0102     struct scatterlist in_sg[2], out_sg[1];
0103     uint8_t *in_buf, *out_buf;
0104     struct akcipher_request child_req;
0105 };
0106 
0107 static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
0108         unsigned int keylen)
0109 {
0110     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0111     int err;
0112 
0113     ctx->key_size = 0;
0114 
0115     err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
0116     if (err)
0117         return err;
0118 
0119     /* Find out new modulus size from rsa implementation */
0120     err = crypto_akcipher_maxsize(ctx->child);
0121     if (err > PAGE_SIZE)
0122         return -ENOTSUPP;
0123 
0124     ctx->key_size = err;
0125     return 0;
0126 }
0127 
0128 static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
0129         unsigned int keylen)
0130 {
0131     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0132     int err;
0133 
0134     ctx->key_size = 0;
0135 
0136     err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
0137     if (err)
0138         return err;
0139 
0140     /* Find out new modulus size from rsa implementation */
0141     err = crypto_akcipher_maxsize(ctx->child);
0142     if (err > PAGE_SIZE)
0143         return -ENOTSUPP;
0144 
0145     ctx->key_size = err;
0146     return 0;
0147 }
0148 
0149 static unsigned int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
0150 {
0151     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0152 
0153     /*
0154      * The maximum destination buffer size for the encrypt/sign operations
0155      * will be the same as for RSA, even though it's smaller for
0156      * decrypt/verify.
0157      */
0158 
0159     return ctx->key_size;
0160 }
0161 
0162 static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
0163         struct scatterlist *next)
0164 {
0165     int nsegs = next ? 2 : 1;
0166 
0167     sg_init_table(sg, nsegs);
0168     sg_set_buf(sg, buf, len);
0169 
0170     if (next)
0171         sg_chain(sg, nsegs, next);
0172 }
0173 
0174 static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)
0175 {
0176     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0177     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0178     struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
0179     unsigned int pad_len;
0180     unsigned int len;
0181     u8 *out_buf;
0182 
0183     if (err)
0184         goto out;
0185 
0186     len = req_ctx->child_req.dst_len;
0187     pad_len = ctx->key_size - len;
0188 
0189     /* Four billion to one */
0190     if (likely(!pad_len))
0191         goto out;
0192 
0193     out_buf = kzalloc(ctx->key_size, GFP_KERNEL);
0194     err = -ENOMEM;
0195     if (!out_buf)
0196         goto out;
0197 
0198     sg_copy_to_buffer(req->dst, sg_nents_for_len(req->dst, len),
0199               out_buf + pad_len, len);
0200     sg_copy_from_buffer(req->dst,
0201                 sg_nents_for_len(req->dst, ctx->key_size),
0202                 out_buf, ctx->key_size);
0203     kfree_sensitive(out_buf);
0204 
0205 out:
0206     req->dst_len = ctx->key_size;
0207 
0208     kfree(req_ctx->in_buf);
0209 
0210     return err;
0211 }
0212 
0213 static void pkcs1pad_encrypt_sign_complete_cb(
0214         struct crypto_async_request *child_async_req, int err)
0215 {
0216     struct akcipher_request *req = child_async_req->data;
0217     struct crypto_async_request async_req;
0218 
0219     if (err == -EINPROGRESS)
0220         return;
0221 
0222     async_req.data = req->base.data;
0223     async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
0224     async_req.flags = child_async_req->flags;
0225     req->base.complete(&async_req,
0226             pkcs1pad_encrypt_sign_complete(req, err));
0227 }
0228 
0229 static int pkcs1pad_encrypt(struct akcipher_request *req)
0230 {
0231     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0232     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0233     struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
0234     int err;
0235     unsigned int i, ps_end;
0236 
0237     if (!ctx->key_size)
0238         return -EINVAL;
0239 
0240     if (req->src_len > ctx->key_size - 11)
0241         return -EOVERFLOW;
0242 
0243     if (req->dst_len < ctx->key_size) {
0244         req->dst_len = ctx->key_size;
0245         return -EOVERFLOW;
0246     }
0247 
0248     req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
0249                   GFP_KERNEL);
0250     if (!req_ctx->in_buf)
0251         return -ENOMEM;
0252 
0253     ps_end = ctx->key_size - req->src_len - 2;
0254     req_ctx->in_buf[0] = 0x02;
0255     for (i = 1; i < ps_end; i++)
0256         req_ctx->in_buf[i] = 1 + prandom_u32_max(255);
0257     req_ctx->in_buf[ps_end] = 0x00;
0258 
0259     pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
0260             ctx->key_size - 1 - req->src_len, req->src);
0261 
0262     akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
0263     akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
0264             pkcs1pad_encrypt_sign_complete_cb, req);
0265 
0266     /* Reuse output buffer */
0267     akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
0268                    req->dst, ctx->key_size - 1, req->dst_len);
0269 
0270     err = crypto_akcipher_encrypt(&req_ctx->child_req);
0271     if (err != -EINPROGRESS && err != -EBUSY)
0272         return pkcs1pad_encrypt_sign_complete(req, err);
0273 
0274     return err;
0275 }
0276 
0277 static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
0278 {
0279     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0280     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0281     struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
0282     unsigned int dst_len;
0283     unsigned int pos;
0284     u8 *out_buf;
0285 
0286     if (err)
0287         goto done;
0288 
0289     err = -EINVAL;
0290     dst_len = req_ctx->child_req.dst_len;
0291     if (dst_len < ctx->key_size - 1)
0292         goto done;
0293 
0294     out_buf = req_ctx->out_buf;
0295     if (dst_len == ctx->key_size) {
0296         if (out_buf[0] != 0x00)
0297             /* Decrypted value had no leading 0 byte */
0298             goto done;
0299 
0300         dst_len--;
0301         out_buf++;
0302     }
0303 
0304     if (out_buf[0] != 0x02)
0305         goto done;
0306 
0307     for (pos = 1; pos < dst_len; pos++)
0308         if (out_buf[pos] == 0x00)
0309             break;
0310     if (pos < 9 || pos == dst_len)
0311         goto done;
0312     pos++;
0313 
0314     err = 0;
0315 
0316     if (req->dst_len < dst_len - pos)
0317         err = -EOVERFLOW;
0318     req->dst_len = dst_len - pos;
0319 
0320     if (!err)
0321         sg_copy_from_buffer(req->dst,
0322                 sg_nents_for_len(req->dst, req->dst_len),
0323                 out_buf + pos, req->dst_len);
0324 
0325 done:
0326     kfree_sensitive(req_ctx->out_buf);
0327 
0328     return err;
0329 }
0330 
0331 static void pkcs1pad_decrypt_complete_cb(
0332         struct crypto_async_request *child_async_req, int err)
0333 {
0334     struct akcipher_request *req = child_async_req->data;
0335     struct crypto_async_request async_req;
0336 
0337     if (err == -EINPROGRESS)
0338         return;
0339 
0340     async_req.data = req->base.data;
0341     async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
0342     async_req.flags = child_async_req->flags;
0343     req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err));
0344 }
0345 
0346 static int pkcs1pad_decrypt(struct akcipher_request *req)
0347 {
0348     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0349     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0350     struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
0351     int err;
0352 
0353     if (!ctx->key_size || req->src_len != ctx->key_size)
0354         return -EINVAL;
0355 
0356     req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
0357     if (!req_ctx->out_buf)
0358         return -ENOMEM;
0359 
0360     pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
0361                 ctx->key_size, NULL);
0362 
0363     akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
0364     akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
0365             pkcs1pad_decrypt_complete_cb, req);
0366 
0367     /* Reuse input buffer, output to a new buffer */
0368     akcipher_request_set_crypt(&req_ctx->child_req, req->src,
0369                    req_ctx->out_sg, req->src_len,
0370                    ctx->key_size);
0371 
0372     err = crypto_akcipher_decrypt(&req_ctx->child_req);
0373     if (err != -EINPROGRESS && err != -EBUSY)
0374         return pkcs1pad_decrypt_complete(req, err);
0375 
0376     return err;
0377 }
0378 
0379 static int pkcs1pad_sign(struct akcipher_request *req)
0380 {
0381     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0382     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0383     struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
0384     struct akcipher_instance *inst = akcipher_alg_instance(tfm);
0385     struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
0386     const struct rsa_asn1_template *digest_info = ictx->digest_info;
0387     int err;
0388     unsigned int ps_end, digest_info_size = 0;
0389 
0390     if (!ctx->key_size)
0391         return -EINVAL;
0392 
0393     if (digest_info)
0394         digest_info_size = digest_info->size;
0395 
0396     if (req->src_len + digest_info_size > ctx->key_size - 11)
0397         return -EOVERFLOW;
0398 
0399     if (req->dst_len < ctx->key_size) {
0400         req->dst_len = ctx->key_size;
0401         return -EOVERFLOW;
0402     }
0403 
0404     req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
0405                   GFP_KERNEL);
0406     if (!req_ctx->in_buf)
0407         return -ENOMEM;
0408 
0409     ps_end = ctx->key_size - digest_info_size - req->src_len - 2;
0410     req_ctx->in_buf[0] = 0x01;
0411     memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
0412     req_ctx->in_buf[ps_end] = 0x00;
0413 
0414     if (digest_info)
0415         memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
0416                digest_info->size);
0417 
0418     pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
0419             ctx->key_size - 1 - req->src_len, req->src);
0420 
0421     akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
0422     akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
0423             pkcs1pad_encrypt_sign_complete_cb, req);
0424 
0425     /* Reuse output buffer */
0426     akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
0427                    req->dst, ctx->key_size - 1, req->dst_len);
0428 
0429     err = crypto_akcipher_decrypt(&req_ctx->child_req);
0430     if (err != -EINPROGRESS && err != -EBUSY)
0431         return pkcs1pad_encrypt_sign_complete(req, err);
0432 
0433     return err;
0434 }
0435 
0436 static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
0437 {
0438     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0439     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0440     struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
0441     struct akcipher_instance *inst = akcipher_alg_instance(tfm);
0442     struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
0443     const struct rsa_asn1_template *digest_info = ictx->digest_info;
0444     const unsigned int sig_size = req->src_len;
0445     const unsigned int digest_size = req->dst_len;
0446     unsigned int dst_len;
0447     unsigned int pos;
0448     u8 *out_buf;
0449 
0450     if (err)
0451         goto done;
0452 
0453     err = -EINVAL;
0454     dst_len = req_ctx->child_req.dst_len;
0455     if (dst_len < ctx->key_size - 1)
0456         goto done;
0457 
0458     out_buf = req_ctx->out_buf;
0459     if (dst_len == ctx->key_size) {
0460         if (out_buf[0] != 0x00)
0461             /* Decrypted value had no leading 0 byte */
0462             goto done;
0463 
0464         dst_len--;
0465         out_buf++;
0466     }
0467 
0468     err = -EBADMSG;
0469     if (out_buf[0] != 0x01)
0470         goto done;
0471 
0472     for (pos = 1; pos < dst_len; pos++)
0473         if (out_buf[pos] != 0xff)
0474             break;
0475 
0476     if (pos < 9 || pos == dst_len || out_buf[pos] != 0x00)
0477         goto done;
0478     pos++;
0479 
0480     if (digest_info) {
0481         if (digest_info->size > dst_len - pos)
0482             goto done;
0483         if (crypto_memneq(out_buf + pos, digest_info->data,
0484                   digest_info->size))
0485             goto done;
0486 
0487         pos += digest_info->size;
0488     }
0489 
0490     err = 0;
0491 
0492     if (digest_size != dst_len - pos) {
0493         err = -EKEYREJECTED;
0494         req->dst_len = dst_len - pos;
0495         goto done;
0496     }
0497     /* Extract appended digest. */
0498     sg_pcopy_to_buffer(req->src,
0499                sg_nents_for_len(req->src, sig_size + digest_size),
0500                req_ctx->out_buf + ctx->key_size,
0501                digest_size, sig_size);
0502     /* Do the actual verification step. */
0503     if (memcmp(req_ctx->out_buf + ctx->key_size, out_buf + pos,
0504            digest_size) != 0)
0505         err = -EKEYREJECTED;
0506 done:
0507     kfree_sensitive(req_ctx->out_buf);
0508 
0509     return err;
0510 }
0511 
0512 static void pkcs1pad_verify_complete_cb(
0513         struct crypto_async_request *child_async_req, int err)
0514 {
0515     struct akcipher_request *req = child_async_req->data;
0516     struct crypto_async_request async_req;
0517 
0518     if (err == -EINPROGRESS)
0519         return;
0520 
0521     async_req.data = req->base.data;
0522     async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
0523     async_req.flags = child_async_req->flags;
0524     req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
0525 }
0526 
0527 /*
0528  * The verify operation is here for completeness similar to the verification
0529  * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
0530  * as in RFC2437.  RFC2437 section 9.2 doesn't define any operation to
0531  * retrieve the DigestInfo from a signature, instead the user is expected
0532  * to call the sign operation to generate the expected signature and compare
0533  * signatures instead of the message-digests.
0534  */
0535 static int pkcs1pad_verify(struct akcipher_request *req)
0536 {
0537     struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
0538     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0539     struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
0540     const unsigned int sig_size = req->src_len;
0541     const unsigned int digest_size = req->dst_len;
0542     int err;
0543 
0544     if (WARN_ON(req->dst) || WARN_ON(!digest_size) ||
0545         !ctx->key_size || sig_size != ctx->key_size)
0546         return -EINVAL;
0547 
0548     req_ctx->out_buf = kmalloc(ctx->key_size + digest_size, GFP_KERNEL);
0549     if (!req_ctx->out_buf)
0550         return -ENOMEM;
0551 
0552     pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
0553                 ctx->key_size, NULL);
0554 
0555     akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
0556     akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
0557             pkcs1pad_verify_complete_cb, req);
0558 
0559     /* Reuse input buffer, output to a new buffer */
0560     akcipher_request_set_crypt(&req_ctx->child_req, req->src,
0561                    req_ctx->out_sg, sig_size, ctx->key_size);
0562 
0563     err = crypto_akcipher_encrypt(&req_ctx->child_req);
0564     if (err != -EINPROGRESS && err != -EBUSY)
0565         return pkcs1pad_verify_complete(req, err);
0566 
0567     return err;
0568 }
0569 
0570 static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
0571 {
0572     struct akcipher_instance *inst = akcipher_alg_instance(tfm);
0573     struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
0574     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0575     struct crypto_akcipher *child_tfm;
0576 
0577     child_tfm = crypto_spawn_akcipher(&ictx->spawn);
0578     if (IS_ERR(child_tfm))
0579         return PTR_ERR(child_tfm);
0580 
0581     ctx->child = child_tfm;
0582     return 0;
0583 }
0584 
0585 static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
0586 {
0587     struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
0588 
0589     crypto_free_akcipher(ctx->child);
0590 }
0591 
0592 static void pkcs1pad_free(struct akcipher_instance *inst)
0593 {
0594     struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
0595     struct crypto_akcipher_spawn *spawn = &ctx->spawn;
0596 
0597     crypto_drop_akcipher(spawn);
0598     kfree(inst);
0599 }
0600 
0601 static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
0602 {
0603     u32 mask;
0604     struct akcipher_instance *inst;
0605     struct pkcs1pad_inst_ctx *ctx;
0606     struct akcipher_alg *rsa_alg;
0607     const char *hash_name;
0608     int err;
0609 
0610     err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AKCIPHER, &mask);
0611     if (err)
0612         return err;
0613 
0614     inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
0615     if (!inst)
0616         return -ENOMEM;
0617 
0618     ctx = akcipher_instance_ctx(inst);
0619 
0620     err = crypto_grab_akcipher(&ctx->spawn, akcipher_crypto_instance(inst),
0621                    crypto_attr_alg_name(tb[1]), 0, mask);
0622     if (err)
0623         goto err_free_inst;
0624 
0625     rsa_alg = crypto_spawn_akcipher_alg(&ctx->spawn);
0626 
0627     if (strcmp(rsa_alg->base.cra_name, "rsa") != 0) {
0628         err = -EINVAL;
0629         goto err_free_inst;
0630     }
0631 
0632     err = -ENAMETOOLONG;
0633     hash_name = crypto_attr_alg_name(tb[2]);
0634     if (IS_ERR(hash_name)) {
0635         if (snprintf(inst->alg.base.cra_name,
0636                  CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
0637                  rsa_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
0638             goto err_free_inst;
0639 
0640         if (snprintf(inst->alg.base.cra_driver_name,
0641                  CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
0642                  rsa_alg->base.cra_driver_name) >=
0643                  CRYPTO_MAX_ALG_NAME)
0644             goto err_free_inst;
0645     } else {
0646         ctx->digest_info = rsa_lookup_asn1(hash_name);
0647         if (!ctx->digest_info) {
0648             err = -EINVAL;
0649             goto err_free_inst;
0650         }
0651 
0652         if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
0653                  "pkcs1pad(%s,%s)", rsa_alg->base.cra_name,
0654                  hash_name) >= CRYPTO_MAX_ALG_NAME)
0655             goto err_free_inst;
0656 
0657         if (snprintf(inst->alg.base.cra_driver_name,
0658                  CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s,%s)",
0659                  rsa_alg->base.cra_driver_name,
0660                  hash_name) >= CRYPTO_MAX_ALG_NAME)
0661             goto err_free_inst;
0662     }
0663 
0664     inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
0665     inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
0666 
0667     inst->alg.init = pkcs1pad_init_tfm;
0668     inst->alg.exit = pkcs1pad_exit_tfm;
0669 
0670     inst->alg.encrypt = pkcs1pad_encrypt;
0671     inst->alg.decrypt = pkcs1pad_decrypt;
0672     inst->alg.sign = pkcs1pad_sign;
0673     inst->alg.verify = pkcs1pad_verify;
0674     inst->alg.set_pub_key = pkcs1pad_set_pub_key;
0675     inst->alg.set_priv_key = pkcs1pad_set_priv_key;
0676     inst->alg.max_size = pkcs1pad_get_max_size;
0677     inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
0678 
0679     inst->free = pkcs1pad_free;
0680 
0681     err = akcipher_register_instance(tmpl, inst);
0682     if (err) {
0683 err_free_inst:
0684         pkcs1pad_free(inst);
0685     }
0686     return err;
0687 }
0688 
0689 struct crypto_template rsa_pkcs1pad_tmpl = {
0690     .name = "pkcs1pad",
0691     .create = pkcs1pad_create,
0692     .module = THIS_MODULE,
0693 };