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)  * echainiv: Encrypted Chain IV Generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This generator generates an IV based on a sequence number by multiplying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * it with a salt and then encrypting it with the same key as used to encrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * the plain text.  This algorithm requires that the block size be equal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * to the IV size.  It is mainly useful for CBC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * This generator can only be used by algorithms where authentication
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * is performed after encryption (i.e., authenc).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <crypto/internal/geniv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <crypto/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int echainiv_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct crypto_aead *geniv = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct aead_request *subreq = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	__be64 nseqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	u64 seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	u8 *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	unsigned int ivsize = crypto_aead_ivsize(geniv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (req->cryptlen < ivsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	aead_request_set_tfm(subreq, ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	info = req->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (req->src != req->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		skcipher_request_set_sync_tfm(nreq, ctx->sknull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		skcipher_request_set_callback(nreq, req->base.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 					      NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		skcipher_request_set_crypt(nreq, req->src, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 					   req->assoclen + req->cryptlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 					   NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		err = crypto_skcipher_encrypt(nreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	aead_request_set_callback(subreq, req->base.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 				  req->base.complete, req->base.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	aead_request_set_crypt(subreq, req->dst, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			       req->cryptlen, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	aead_request_set_ad(subreq, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	memcpy(&nseqno, info + ivsize - 8, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	seqno = be64_to_cpu(nseqno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	memset(info, 0, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		u64 a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		memcpy(&a, ctx->salt + ivsize - 8, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		a |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		a *= seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		memcpy(info + ivsize - 8, &a, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	} while ((ivsize -= 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return crypto_aead_encrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static int echainiv_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct crypto_aead *geniv = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct aead_request *subreq = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	crypto_completion_t compl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	unsigned int ivsize = crypto_aead_ivsize(geniv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (req->cryptlen < ivsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	aead_request_set_tfm(subreq, ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	compl = req->base.complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	data = req->base.data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	aead_request_set_callback(subreq, req->base.flags, compl, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	aead_request_set_crypt(subreq, req->src, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			       req->cryptlen - ivsize, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	aead_request_set_ad(subreq, req->assoclen + ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return crypto_aead_decrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int echainiv_aead_create(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 				struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct aead_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	inst = aead_geniv_alloc(tmpl, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (IS_ERR(inst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return PTR_ERR(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		goto free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	inst->alg.encrypt = echainiv_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	inst->alg.decrypt = echainiv_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	inst->alg.init = aead_init_geniv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	inst->alg.exit = aead_exit_geniv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	inst->alg.base.cra_ctxsize += inst->alg.ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	err = aead_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		inst->free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static struct crypto_template echainiv_tmpl = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	.name = "echainiv",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	.create = echainiv_aead_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int __init echainiv_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return crypto_register_template(&echainiv_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void __exit echainiv_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	crypto_unregister_template(&echainiv_tmpl);
^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) subsys_initcall(echainiv_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) module_exit(echainiv_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) MODULE_DESCRIPTION("Encrypted Chain IV Generator");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) MODULE_ALIAS_CRYPTO("echainiv");