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) /* In-software asymmetric public-key crypto subtype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * See Documentation/crypto/asymmetric-keys.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define pr_fmt(fmt) "PKEY: "fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <keys/asymmetric-subtype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <crypto/public_key.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <crypto/akcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <crypto/sm2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <crypto/sm3_base.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) MODULE_AUTHOR("Red Hat, Inc.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) MODULE_LICENSE("GPL");
^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)  * Provide a part of a description of the key for /proc/keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static void public_key_describe(const struct key *asymmetric_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 				struct seq_file *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct public_key *key = asymmetric_key->payload.data[asym_crypto];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^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)  * Destroy a public key algorithm key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) void public_key_free(struct public_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		kfree(key->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		kfree(key->params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) EXPORT_SYMBOL_GPL(public_key_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * Destroy a public key algorithm key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static void public_key_destroy(void *payload0, void *payload3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	public_key_free(payload0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	public_key_signature_free(payload3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * Determine the crypto algorithm name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) int software_key_determine_akcipher(const char *encoding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 				    const char *hash_algo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 				    const struct public_key *pkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 				    char alg_name[CRYPTO_MAX_ALG_NAME])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (strcmp(encoding, "pkcs1") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		/* The data wangled by the RSA algorithm is typically padded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		 * sec 8.2].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (!hash_algo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 				     "pkcs1pad(%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				     pkey->pkey_algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				     "pkcs1pad(%s,%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 				     pkey->pkey_algo, hash_algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (strcmp(encoding, "raw") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		strcpy(alg_name, pkey->pkey_algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return -ENOPKG;
^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) static u8 *pkey_pack_u32(u8 *dst, u32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	memcpy(dst, &val, sizeof(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return dst + sizeof(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * Query information about a key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int software_key_query(const struct kernel_pkey_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			      struct kernel_pkey_query *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct crypto_akcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct public_key *pkey = params->key->payload.data[asym_crypto];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	char alg_name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	u8 *key, *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int ret, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ret = software_key_determine_akcipher(params->encoding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 					      params->hash_algo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 					      pkey, alg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	tfm = crypto_alloc_akcipher(alg_name, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return PTR_ERR(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		goto error_free_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	memcpy(key, pkey->key, pkey->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	ptr = key + pkey->keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	ptr = pkey_pack_u32(ptr, pkey->algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	memcpy(ptr, pkey->params, pkey->paramlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (pkey->key_is_private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		goto error_free_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	len = crypto_akcipher_maxsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	info->key_size = len * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	info->max_data_size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	info->max_sig_size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	info->max_enc_size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	info->max_dec_size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			       KEYCTL_SUPPORTS_VERIFY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (pkey->key_is_private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 					KEYCTL_SUPPORTS_SIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) error_free_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) error_free_tfm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	crypto_free_akcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	pr_devel("<==%s() = %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * Do encryption, decryption and signing ops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int software_key_eds_op(struct kernel_pkey_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			       const void *in, void *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	const struct public_key *pkey = params->key->payload.data[asym_crypto];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	struct akcipher_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	struct crypto_akcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct crypto_wait cwait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct scatterlist in_sg, out_sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	char alg_name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	char *key, *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	pr_devel("==>%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ret = software_key_determine_akcipher(params->encoding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 					      params->hash_algo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 					      pkey, alg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	tfm = crypto_alloc_akcipher(alg_name, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return PTR_ERR(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	req = akcipher_request_alloc(tfm, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		goto error_free_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		goto error_free_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	memcpy(key, pkey->key, pkey->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	ptr = key + pkey->keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	ptr = pkey_pack_u32(ptr, pkey->algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	memcpy(ptr, pkey->params, pkey->paramlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (pkey->key_is_private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		goto error_free_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	sg_init_one(&in_sg, in, params->in_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	sg_init_one(&out_sg, out, params->out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 				   params->out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	crypto_init_wait(&cwait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				      CRYPTO_TFM_REQ_MAY_SLEEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 				      crypto_req_done, &cwait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	/* Perform the encryption calculation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	switch (params->op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	case kernel_pkey_encrypt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		ret = crypto_akcipher_encrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	case kernel_pkey_decrypt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		ret = crypto_akcipher_decrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	case kernel_pkey_sign:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		ret = crypto_akcipher_sign(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	ret = crypto_wait_req(ret, &cwait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		ret = req->dst_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) error_free_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) error_free_req:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	akcipher_request_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) error_free_tfm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	crypto_free_akcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	pr_devel("<==%s() = %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) #if IS_REACHABLE(CONFIG_CRYPTO_SM2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static int cert_sig_digest_update(const struct public_key_signature *sig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				  struct crypto_akcipher *tfm_pkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct crypto_shash *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct shash_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	size_t desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	unsigned char dgst[SM3_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	BUG_ON(!sig->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	ret = sm2_compute_z_digest(tfm_pkey, SM2_DEFAULT_USERID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 					SM2_DEFAULT_USERID_LEN, dgst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return PTR_ERR(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	desc = kzalloc(desc_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		goto error_free_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	desc->tfm = tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	ret = crypto_shash_init(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		goto error_free_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	ret = crypto_shash_update(desc, dgst, SM3_DIGEST_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		goto error_free_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	ret = crypto_shash_finup(desc, sig->data, sig->data_size, sig->digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) error_free_desc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	kfree(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) error_free_tfm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	crypto_free_shash(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static inline int cert_sig_digest_update(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	const struct public_key_signature *sig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	struct crypto_akcipher *tfm_pkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return -ENOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) #endif /* ! IS_REACHABLE(CONFIG_CRYPTO_SM2) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * Verify a signature using a public key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int public_key_verify_signature(const struct public_key *pkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 				const struct public_key_signature *sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct crypto_wait cwait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct crypto_akcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct akcipher_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct scatterlist src_sg[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	char alg_name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	char *key, *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	pr_devel("==>%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	BUG_ON(!pkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	BUG_ON(!sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	BUG_ON(!sig->s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	ret = software_key_determine_akcipher(sig->encoding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 					      sig->hash_algo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 					      pkey, alg_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	tfm = crypto_alloc_akcipher(alg_name, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		return PTR_ERR(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	req = akcipher_request_alloc(tfm, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		goto error_free_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		goto error_free_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	memcpy(key, pkey->key, pkey->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	ptr = key + pkey->keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	ptr = pkey_pack_u32(ptr, pkey->algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	memcpy(ptr, pkey->params, pkey->paramlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (pkey->key_is_private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		goto error_free_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (sig->pkey_algo && strcmp(sig->pkey_algo, "sm2") == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	    sig->data_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		ret = cert_sig_digest_update(sig, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			goto error_free_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	sg_init_table(src_sg, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	sg_set_buf(&src_sg[0], sig->s, sig->s_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 				   sig->digest_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	crypto_init_wait(&cwait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				      CRYPTO_TFM_REQ_MAY_SLEEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				      crypto_req_done, &cwait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) error_free_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) error_free_req:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	akcipher_request_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) error_free_tfm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	crypto_free_akcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	pr_devel("<==%s() = %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (WARN_ON_ONCE(ret > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) EXPORT_SYMBOL_GPL(public_key_verify_signature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static int public_key_verify_signature_2(const struct key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 					 const struct public_key_signature *sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	const struct public_key *pk = key->payload.data[asym_crypto];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	return public_key_verify_signature(pk, sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)  * Public key algorithm asymmetric key subtype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct asymmetric_key_subtype public_key_subtype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	.owner			= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	.name			= "public_key",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	.name_len		= sizeof("public_key") - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	.describe		= public_key_describe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	.destroy		= public_key_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	.query			= software_key_query,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	.eds_op			= software_key_eds_op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	.verify_signature	= public_key_verify_signature_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) EXPORT_SYMBOL_GPL(public_key_subtype);