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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright 2003-2004, Instant802 Networks, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright 2005-2006, Devicescape Software, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2014-2015, Qualcomm Atheros, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org>
^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) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/types.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/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <crypto/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "aead_api.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) int aead_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, size_t aad_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 		 u8 *data, size_t data_len, u8 *mic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	size_t mic_len = crypto_aead_authsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct scatterlist sg[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct aead_request *aead_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	u8 *__aad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	aead_req = kzalloc(reqsize + aad_len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	if (!aead_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	__aad = (u8 *)aead_req + reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	memcpy(__aad, aad, aad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	sg_init_table(sg, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	sg_set_buf(&sg[0], __aad, aad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	sg_set_buf(&sg[1], data, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	sg_set_buf(&sg[2], mic, mic_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	aead_request_set_tfm(aead_req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	aead_request_set_crypt(aead_req, sg, sg, data_len, b_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	aead_request_set_ad(aead_req, sg[0].length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	ret = crypto_aead_encrypt(aead_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	kfree_sensitive(aead_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	return ret;
^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) int aead_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, size_t aad_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		 u8 *data, size_t data_len, u8 *mic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	size_t mic_len = crypto_aead_authsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct scatterlist sg[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct aead_request *aead_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u8 *__aad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (data_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	aead_req = kzalloc(reqsize + aad_len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (!aead_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	__aad = (u8 *)aead_req + reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	memcpy(__aad, aad, aad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	sg_init_table(sg, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	sg_set_buf(&sg[0], __aad, aad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	sg_set_buf(&sg[1], data, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	sg_set_buf(&sg[2], mic, mic_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	aead_request_set_tfm(aead_req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	aead_request_set_crypt(aead_req, sg, sg, data_len + mic_len, b_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	aead_request_set_ad(aead_req, sg[0].length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	err = crypto_aead_decrypt(aead_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	kfree_sensitive(aead_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return err;
^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) struct crypto_aead *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) aead_key_setup_encrypt(const char *alg, const u8 key[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		       size_t key_len, size_t mic_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct crypto_aead *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	tfm = crypto_alloc_aead(alg, 0, CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		return tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	err = crypto_aead_setkey(tfm, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		goto free_aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	err = crypto_aead_setauthsize(tfm, mic_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		goto free_aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) free_aead:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	crypto_free_aead(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) void aead_key_free(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	crypto_free_aead(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }