// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2015-2017, 2019 The Linux Foundation. All rights reserved.
 */

#define pr_fmt(fmt) "OPLUS_CHG[CFG]: %s[%d]: " fmt, __func__, __LINE__

#include <linux/errno.h>
#include <crypto/hash.h>
#include <crypto/skcipher.h>
#include <crypto/public_key.h>
#include <linux/crypto.h>
#include "oplus_chg_comm.h"
#include "op_wlchg_v2/oplus_chg_wls.h"
#include "oplus_chg_cfg.h"

static char public_auth_code[] = {
	0x30, 0x82, 0x02, 0x0A, 0x02, 0x82, 0x02, 0x01, 0x00, 0xE3, 0xBF, 0x47, 0xB0, 0x0D, 0xEA, 0xC0,
	0x45, 0xC0, 0xBF, 0x24, 0x31, 0x9E, 0xEC, 0x64, 0xF8, 0x47, 0xF8, 0x58, 0x96, 0xCF, 0x5E, 0xDE,
	0xC6, 0xF1, 0x90, 0x26, 0xC9, 0x42, 0xF6, 0x21, 0x40, 0x70, 0x3D, 0x58, 0x26, 0x41, 0x6B, 0x7F,
	0x1E, 0x32, 0x46, 0x82, 0x1C, 0xCD, 0x77, 0x4B, 0xF1, 0xFB, 0xD7, 0x8F, 0x70, 0x5D, 0x99, 0x1E,
	0x0D, 0xD2, 0xB1, 0x9A, 0xEF, 0x9C, 0x2A, 0xC3, 0x08, 0x0B, 0x79, 0x38, 0x49, 0x45, 0xAA, 0x92,
	0xF1, 0x13, 0x08, 0x0F, 0x6E, 0xBF, 0xCE, 0x26, 0x7F, 0x56, 0x75, 0x4F, 0x34, 0x11, 0x80, 0x39,
	0xA7, 0x2A, 0xCC, 0xA5, 0xF0, 0x35, 0x3A, 0x9C, 0x0F, 0x0B, 0xB2, 0xB5, 0x2C, 0xC4, 0xE6, 0xC9,
	0x1C, 0x79, 0x85, 0xFB, 0x33, 0x49, 0x9B, 0x6B, 0x25, 0xC1, 0xD9, 0xE8, 0xA4, 0x21, 0xBF, 0xF0,
	0x2D, 0xC2, 0x17, 0xD1, 0xE1, 0xDC, 0x89, 0x2C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x76, 0x35, 0xF8, 0xB5, 0xF8, 0x19, 0x03, 0xA3, 0xD4, 0x40, 0xD0, 0xAC, 0xD9, 0x40, 0x2A, 0xE4,
	0xEF, 0x20, 0x3A, 0x66, 0x09, 0x2C, 0xF9, 0x09, 0x99, 0x02, 0x03, 0x01, 0x00, 0x01
};

static char public_auth_code_params[] = {
	0x05, 0x00
};

static struct public_key oplus_chg_pauth_code = {
	.key = public_auth_code,
	.keylen = ARRAY_SIZE(public_auth_code),
	.algo = OID_rsaEncryption,
	.params = public_auth_code_params,
	.paramlen = ARRAY_SIZE(public_auth_code_params),
	.key_is_private = false,
	.pkey_algo = "rsa",
};

static union { char c[4]; unsigned long l; } endian_test = { { 'l', '?', '?', 'b' } };
#define ENDIANNESS ((char)endian_test.l)

struct sdesc {
	struct shash_desc shash;
	char ctx[];
};

static struct sdesc *init_sdesc(struct crypto_shash *alg)
{
	struct sdesc *sdesc;
	int size;

	size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
	sdesc = kmalloc(size, GFP_KERNEL);
	if (!sdesc)
		return ERR_PTR(-ENOMEM);
	sdesc->shash.tfm = alg;
	return sdesc;
}

static int calc_hash(struct crypto_shash *alg, const unsigned char *data,
		     unsigned int datalen, unsigned char *digest)
{
	struct sdesc *sdesc;
	int ret;

	sdesc = init_sdesc(alg);
	if (IS_ERR(sdesc)) {
		pr_info("can't alloc sdesc\n");
		return PTR_ERR(sdesc);
	}

	ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
	kfree(sdesc);
	return ret;
}

static u32 oplus_chg_cfg_get_data_size(struct oplus_chg_param_head *param_head)
{
	struct oplus_chg_cfg_data_head *data_head;
	u32 index;

	for (index = 0; index < param_head->size; ) {
		data_head = (struct oplus_chg_cfg_data_head *)(param_head->data + index);
		if (data_head->magic != OPLUS_CHG_CFG_MAGIC) {
			pr_err("data magic error\n");
			return 0;
		}
		index += data_head->size + sizeof(struct oplus_chg_cfg_data_head);
	}

	return index;
}

int oplus_chg_verify_signature(u8 *s, u32 s_size, u8 *digest, u8 digest_size)
{
	struct public_key_signature sig = {0};

	sig.s = s;
	sig.s_size = s_size;
	sig.digest = digest;
	sig.digest_size = digest_size;
	sig.pkey_algo = "rsa";
	sig.hash_algo = "sha256";
	sig.encoding = "pkcs1";

	return public_key_verify_signature(&oplus_chg_pauth_code, &sig);
}

int oplus_chg_check_cfg_data(void *buf)
{
	struct oplus_chg_cfg_head *cfg_head;
	struct oplus_chg_param_head *param_head;
	struct crypto_shash *alg;
	char *hash_alg_name = "sha256";
	unsigned char digest[32];
	int rc;

	if (buf == NULL) {
		pr_err("data buf is null\n");
		return -EINVAL;
	}

	cfg_head = (struct oplus_chg_cfg_head *)buf;

	if (cfg_head->magic != OPLUS_CHG_CFG_MAGIC ||
	    cfg_head->head_size != sizeof(struct oplus_chg_cfg_head)) {
		pr_err("cfg head error, magic=0x%08x, size=%d\n", cfg_head->magic, cfg_head->head_size);
		return -EINVAL;
	}

	param_head = (struct oplus_chg_param_head *)((unsigned char *)buf + cfg_head->param_index[OPLUS_CHG_USB_PARAM]);
	if (param_head->size != oplus_chg_cfg_get_data_size(param_head)) {
		pr_err("usb charge parameter length error, len=%d\n", oplus_chg_cfg_get_data_size(param_head));
		return -EINVAL;
	}
	param_head = (struct oplus_chg_param_head *)((unsigned char *)buf + cfg_head->param_index[OPLUS_CHG_WLS_PARAM]);
	if (param_head->size != oplus_chg_cfg_get_data_size(param_head)) {
		pr_err("wireless charge parameter length error, len=%d\n", oplus_chg_cfg_get_data_size(param_head));
		return -EINVAL;
	}
	param_head = (struct oplus_chg_param_head *)((unsigned char *)buf + cfg_head->param_index[OPLUS_CHG_COMM_PARAM]);
	if (param_head->size != oplus_chg_cfg_get_data_size(param_head)) {
		pr_err("common parameter length error, len=%d\n", oplus_chg_cfg_get_data_size(param_head));
		return -EINVAL;
	}
	param_head = (struct oplus_chg_param_head *)((unsigned char *)buf + cfg_head->param_index[OPLUS_CHG_BATT_PARAM]);
	if (param_head->size != oplus_chg_cfg_get_data_size(param_head)) {
		pr_err("battery parameter length error, len=%d\n", oplus_chg_cfg_get_data_size(param_head));
		return -EINVAL;
	}

	alg = crypto_alloc_shash(hash_alg_name, 0, 0);
	if (IS_ERR(alg)) {
		pr_err("can't alloc alg %s\n", hash_alg_name);
		return PTR_ERR(alg);
	}
	rc = calc_hash(alg, (unsigned char *)buf + cfg_head->head_size, cfg_head->size, digest);
	if (rc < 0) {
		pr_err("Configuration file digest calculation failed, rc=%d\n", rc);
		return rc;
	}
	crypto_free_shash(alg);

	rc = oplus_chg_verify_signature(cfg_head->signature, 512, digest, 32);
	if (rc < 0) {
		pr_err("Configuration file signature verification failed, rc=%d\n", rc);
		return rc;
	}

	return 0;
}

void *oplus_chg_get_param(void *buf, enum oplus_chg_param_type type)
{
	struct oplus_chg_cfg_head *cfg_head;
	struct oplus_chg_param_head *param_head;

	cfg_head = (struct oplus_chg_cfg_head *)buf;
	param_head = (struct oplus_chg_param_head *)((unsigned char *)buf + cfg_head->param_index[type]);

	if (param_head->magic != OPLUS_CHG_CFG_MAGIC)
		return NULL;
	if (param_head->size == 0)
		return NULL;
	if (param_head->type != type)
		return NULL;
	if (param_head->size != oplus_chg_cfg_get_data_size(param_head))
		return NULL;

	return (void *)param_head->data;
}

int oplus_chg_cfg_load_param(void *src, enum oplus_chg_param_type type, u8 *out_buf)
{
	struct oplus_chg_cfg_head *cfg_head;
	struct oplus_chg_param_head *param_head;
	struct oplus_chg_cfg_data_head *data_head;
	uint32_t data_index;

	if (src == NULL) {
		pr_err("src is NULL\n");
		return -ENODATA;
	}
	if (out_buf == NULL) {
		pr_err("out_buf is NULL\n");
		return -EINVAL;
	}
	cfg_head = (struct oplus_chg_cfg_head *)src;
	param_head = (struct oplus_chg_param_head *)((unsigned char *)src + cfg_head->param_index[type]);

	if (param_head->magic != OPLUS_CHG_CFG_MAGIC) {
		pr_err("param(=%d) magic error\n", type);
		return -EINVAL;
	}
	if (param_head->size == 0)
		return 0;
	if (param_head->type != type) {
		pr_err("param(=%d) type(=%d) error\n", type, param_head->type);
		return -EINVAL;
	}
	if (ENDIANNESS == 'b') {
		pr_err("Big-endian mode is temporarily not supported\n");
		return -EINVAL;
	}

	for (data_index = 0; data_index < param_head->size; ) {
		data_head = (struct oplus_chg_cfg_data_head *)(param_head->data + data_index);
		if (data_head->magic != OPLUS_CHG_CFG_MAGIC) {
			pr_err("data magic error\n");
			return -EINVAL;
		}
		memcpy(out_buf + data_head->index, data_head->data, data_head->size);
		data_index += data_head->size + sizeof(struct oplus_chg_cfg_data_head);
	}

	if (data_index != param_head->size) {
		pr_err("param(=%d) size error\n", type);
		return -EINVAL;
	}

	return 0;
}
