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+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * s390 implementation of the DES Cipher Algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright IBM Corp. 2003, 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Author(s): Thomas Spatzier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	      Jan Glauber (jan.glauber@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/fips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <crypto/internal/des.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/cpacf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define DES3_KEY_SIZE	(3 * DES_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static u8 *ctrblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static DEFINE_MUTEX(ctrblk_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) struct s390_des_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	u8 iv[DES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	u8 key[DES3_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		      unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	err = crypto_des_verify_key(tfm, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	memcpy(ctx->key, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static int des_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			       unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	return des_setkey(crypto_skcipher_tfm(tfm), key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static void s390_des_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	cpacf_km(CPACF_KM_DEA, ctx->key, out, in, DES_BLOCK_SIZE);
^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) static void s390_des_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	cpacf_km(CPACF_KM_DEA | CPACF_DECRYPT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		 ctx->key, out, in, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static struct crypto_alg des_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	.cra_name		=	"des",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	.cra_driver_name	=	"des-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	.cra_priority		=	300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	.cra_blocksize		=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	.cra_ctxsize		=	sizeof(struct s390_des_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	.cra_module		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	.cra_u			=	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		.cipher = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			.cia_min_keysize	=	DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			.cia_max_keysize	=	DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			.cia_setkey		=	des_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			.cia_encrypt		=	s390_des_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			.cia_decrypt		=	s390_des_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		}
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static int ecb_desall_crypt(struct skcipher_request *req, unsigned long fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct s390_des_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	unsigned int nbytes, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	ret = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		/* only use complete blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		n = nbytes & ~(DES_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		cpacf_km(fc, ctx->key, walk.dst.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			 walk.src.virt.addr, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		ret = skcipher_walk_done(&walk, nbytes - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int cbc_desall_crypt(struct skcipher_request *req, unsigned long fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct s390_des_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	unsigned int nbytes, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		u8 iv[DES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		u8 key[DES3_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	} param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	ret = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	memcpy(param.iv, walk.iv, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	memcpy(param.key, ctx->key, DES3_KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		/* only use complete blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		n = nbytes & ~(DES_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		cpacf_kmc(fc, &param, walk.dst.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			  walk.src.virt.addr, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		memcpy(walk.iv, param.iv, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		ret = skcipher_walk_done(&walk, nbytes - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int ecb_des_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return ecb_desall_crypt(req, CPACF_KM_DEA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int ecb_des_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return ecb_desall_crypt(req, CPACF_KM_DEA | CPACF_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static struct skcipher_alg ecb_des_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	.base.cra_name		=	"ecb(des)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	.base.cra_driver_name	=	"ecb-des-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	.base.cra_priority	=	400,	/* combo: des + ecb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	.base.cra_blocksize	=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	.base.cra_ctxsize	=	sizeof(struct s390_des_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	.min_keysize		=	DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	.max_keysize		=	DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	.setkey			=	des_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	.encrypt		=	ecb_des_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	.decrypt		=	ecb_des_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int cbc_des_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return cbc_desall_crypt(req, CPACF_KMC_DEA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int cbc_des_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return cbc_desall_crypt(req, CPACF_KMC_DEA | CPACF_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static struct skcipher_alg cbc_des_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	.base.cra_name		=	"cbc(des)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	.base.cra_driver_name	=	"cbc-des-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	.base.cra_priority	=	400,	/* combo: des + cbc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	.base.cra_blocksize	=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	.base.cra_ctxsize	=	sizeof(struct s390_des_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	.min_keysize		=	DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	.max_keysize		=	DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	.ivsize			=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	.setkey			=	des_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	.encrypt		=	cbc_des_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	.decrypt		=	cbc_des_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * RFC2451:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  *   For DES-EDE3, there is no known need to reject weak or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  *   complementation keys.  Any weakness is obviated by the use of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  *   multiple keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  *   However, if the first two or last two independent 64-bit keys are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  *   equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  *   same as DES.  Implementers MUST reject keys that exhibit this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  *   property.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  *   In fips mode additinally check for all 3 keys are unique.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int des3_setkey(struct crypto_tfm *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		       unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	err = crypto_des3_ede_verify_key(tfm, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	memcpy(ctx->key, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int des3_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	return des3_setkey(crypto_skcipher_tfm(tfm), key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static void des3_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	cpacf_km(CPACF_KM_TDEA_192, ctx->key, dst, src, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static void des3_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct s390_des_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	cpacf_km(CPACF_KM_TDEA_192 | CPACF_DECRYPT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		 ctx->key, dst, src, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static struct crypto_alg des3_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	.cra_name		=	"des3_ede",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	.cra_driver_name	=	"des3_ede-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	.cra_priority		=	300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	.cra_blocksize		=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	.cra_ctxsize		=	sizeof(struct s390_des_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	.cra_module		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	.cra_u			=	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		.cipher = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			.cia_min_keysize	=	DES3_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			.cia_max_keysize	=	DES3_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			.cia_setkey		=	des3_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			.cia_encrypt		=	des3_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			.cia_decrypt		=	des3_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static int ecb_des3_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return ecb_desall_crypt(req, CPACF_KM_TDEA_192);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int ecb_des3_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return ecb_desall_crypt(req, CPACF_KM_TDEA_192 | CPACF_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static struct skcipher_alg ecb_des3_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	.base.cra_name		=	"ecb(des3_ede)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	.base.cra_driver_name	=	"ecb-des3_ede-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	.base.cra_priority	=	400,	/* combo: des3 + ecb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	.base.cra_blocksize	=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	.base.cra_ctxsize	=	sizeof(struct s390_des_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	.min_keysize		=	DES3_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	.max_keysize		=	DES3_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	.setkey			=	des3_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	.encrypt		=	ecb_des3_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	.decrypt		=	ecb_des3_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int cbc_des3_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return cbc_desall_crypt(req, CPACF_KMC_TDEA_192);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int cbc_des3_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return cbc_desall_crypt(req, CPACF_KMC_TDEA_192 | CPACF_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static struct skcipher_alg cbc_des3_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	.base.cra_name		=	"cbc(des3_ede)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	.base.cra_driver_name	=	"cbc-des3_ede-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	.base.cra_priority	=	400,	/* combo: des3 + cbc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	.base.cra_blocksize	=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	.base.cra_ctxsize	=	sizeof(struct s390_des_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	.min_keysize		=	DES3_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	.max_keysize		=	DES3_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	.ivsize			=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	.setkey			=	des3_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	.encrypt		=	cbc_des3_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	.decrypt		=	cbc_des3_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	unsigned int i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	/* align to block size, max. PAGE_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(DES_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	memcpy(ctrptr, iv, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	for (i = (n / DES_BLOCK_SIZE) - 1; i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		memcpy(ctrptr + DES_BLOCK_SIZE, ctrptr, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		crypto_inc(ctrptr + DES_BLOCK_SIZE, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		ctrptr += DES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static int ctr_desall_crypt(struct skcipher_request *req, unsigned long fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	struct s390_des_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	u8 buf[DES_BLOCK_SIZE], *ctrptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	unsigned int n, nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	int ret, locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	locked = mutex_trylock(&ctrblk_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	ret = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	while ((nbytes = walk.nbytes) >= DES_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		n = DES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		if (nbytes >= 2*DES_BLOCK_SIZE && locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			n = __ctrblk_init(ctrblk, walk.iv, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		ctrptr = (n > DES_BLOCK_SIZE) ? ctrblk : walk.iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		cpacf_kmctr(fc, ctx->key, walk.dst.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			    walk.src.virt.addr, n, ctrptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		if (ctrptr == ctrblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			memcpy(walk.iv, ctrptr + n - DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		crypto_inc(walk.iv, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		ret = skcipher_walk_done(&walk, nbytes - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		mutex_unlock(&ctrblk_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	/* final block may be < DES_BLOCK_SIZE, copy only nbytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		cpacf_kmctr(fc, ctx->key, buf, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			    DES_BLOCK_SIZE, walk.iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		memcpy(walk.dst.virt.addr, buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		crypto_inc(walk.iv, DES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		ret = skcipher_walk_done(&walk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static int ctr_des_crypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	return ctr_desall_crypt(req, CPACF_KMCTR_DEA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static struct skcipher_alg ctr_des_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	.base.cra_name		=	"ctr(des)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	.base.cra_driver_name	=	"ctr-des-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	.base.cra_priority	=	400,	/* combo: des + ctr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	.base.cra_blocksize	=	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	.base.cra_ctxsize	=	sizeof(struct s390_des_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	.min_keysize		=	DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	.max_keysize		=	DES_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	.ivsize			=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	.setkey			=	des_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	.encrypt		=	ctr_des_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	.decrypt		=	ctr_des_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	.chunksize		=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static int ctr_des3_crypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	return ctr_desall_crypt(req, CPACF_KMCTR_TDEA_192);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static struct skcipher_alg ctr_des3_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	.base.cra_name		=	"ctr(des3_ede)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	.base.cra_driver_name	=	"ctr-des3_ede-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	.base.cra_priority	=	400,	/* combo: des3 + ede */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	.base.cra_blocksize	=	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	.base.cra_ctxsize	=	sizeof(struct s390_des_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	.base.cra_module	=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	.min_keysize		=	DES3_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	.max_keysize		=	DES3_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	.ivsize			=	DES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	.setkey			=	des3_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	.encrypt		=	ctr_des3_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	.decrypt		=	ctr_des3_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	.chunksize		=	DES_BLOCK_SIZE,
^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) static struct crypto_alg *des_s390_algs_ptr[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) static int des_s390_algs_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static struct skcipher_alg *des_s390_skciphers_ptr[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static int des_s390_skciphers_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static int des_s390_register_alg(struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	ret = crypto_register_alg(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		des_s390_algs_ptr[des_s390_algs_num++] = alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static int des_s390_register_skcipher(struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	ret = crypto_register_skcipher(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		des_s390_skciphers_ptr[des_s390_skciphers_num++] = alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static void des_s390_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	while (des_s390_algs_num--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		crypto_unregister_alg(des_s390_algs_ptr[des_s390_algs_num]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	while (des_s390_skciphers_num--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		crypto_unregister_skcipher(des_s390_skciphers_ptr[des_s390_skciphers_num]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (ctrblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		free_page((unsigned long) ctrblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static int __init des_s390_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	/* Query available functions for KM, KMC and KMCTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	cpacf_query(CPACF_KM, &km_functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	cpacf_query(CPACF_KMC, &kmc_functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	cpacf_query(CPACF_KMCTR, &kmctr_functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	if (cpacf_test_func(&km_functions, CPACF_KM_DEA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		ret = des_s390_register_alg(&des_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		ret = des_s390_register_skcipher(&ecb_des_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (cpacf_test_func(&kmc_functions, CPACF_KMC_DEA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		ret = des_s390_register_skcipher(&cbc_des_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (cpacf_test_func(&km_functions, CPACF_KM_TDEA_192)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		ret = des_s390_register_alg(&des3_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		ret = des_s390_register_skcipher(&ecb_des3_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (cpacf_test_func(&kmc_functions, CPACF_KMC_TDEA_192)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		ret = des_s390_register_skcipher(&cbc_des3_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	    cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		if (!ctrblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_DEA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		ret = des_s390_register_skcipher(&ctr_des_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_TDEA_192)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		ret = des_s390_register_skcipher(&ctr_des3_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	des_s390_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) module_cpu_feature_match(MSA, des_s390_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) module_exit(des_s390_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) MODULE_ALIAS_CRYPTO("des");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) MODULE_ALIAS_CRYPTO("des3_ede");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");