Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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