Files
android_kernel_samsung_sm8750/drivers/input/sec_input/spu-sign-verify.c
2025-08-12 22:16:57 +02:00

265 lines
7.5 KiB
C
Executable File

/*
* spu-sign-verify.c - When SPU fireware update, image signature verify.
*
* Author: JJungs-lee <jhs2.lee@samsung.com>
*
*/
#define pr_fmt(fmt) "[sec_input] spu_verify: " fmt
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/export.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/ctype.h>
#include <linux/fcntl.h>
#include <linux/syscalls.h>
#include <linux/file.h>
#include <linux/uaccess.h>
#include <crypto/public_key.h>
#include <crypto/hash.h>
//#include <linux/spu-verify.h>
#include "spu-sign-verify.h"
#define FW_DIGEST_MISMATCH 999
const char *spu_tag_name[] = { TSP_TAG, WACOM_TAG };
const u8 PublicKey[] = {
0x30, 0x82, 0x02, 0x0a, 0x02, 0x82, 0x02, 0x01,
0x00, 0xc6, 0xe9, 0x59, 0xe8, 0xb0, 0xda, 0x2e,
0x8d, 0x4a, 0xa9, 0x09, 0x64, 0x51, 0xe5, 0x4e,
0xb3, 0x7c, 0x18, 0x59, 0xc5, 0x6a, 0x1d, 0xce,
0x11, 0x98, 0xb3, 0x44, 0x5b, 0x96, 0x88, 0xf2,
0x27, 0x3b, 0x33, 0x55, 0x94, 0xa9, 0xdd, 0x51,
0x76, 0x6f, 0x9f, 0xc3, 0x3c, 0x2e, 0x86, 0x7d,
0x24, 0xea, 0x0c, 0x5f, 0xcd, 0x6c, 0x83, 0xa1,
0xca, 0x34, 0x58, 0x03, 0x7f, 0x5a, 0x84, 0x91,
0xc6, 0xc7, 0x1f, 0xd2, 0x8b, 0x00, 0xef, 0xe4,
0xd4, 0xd7, 0x19, 0x15, 0x4b, 0x29, 0x15, 0x06,
0x6f, 0x61, 0x8f, 0xe5, 0x27, 0xc8, 0x9d, 0x80,
0xba, 0x87, 0x11, 0xdd, 0xd8, 0x69, 0xee, 0x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aa, 0xdc, 0xcc, 0xda, 0x57, 0x4f, 0x6e,
0xf5, 0x45, 0xc0, 0xb2, 0xa8, 0x01, 0x31, 0xc4,
0xab, 0x74, 0xaf, 0xb0, 0xa1, 0x27, 0x42, 0xe7,
0x2c, 0x3e, 0x36, 0xd8, 0xcc, 0xf8, 0x05, 0xfc,
0x66, 0x77, 0x1c, 0x15, 0xbd, 0x92, 0x12, 0x33,
0xd9, 0xc0, 0x0a, 0xf3, 0xca, 0x5c, 0x6f, 0xde,
0x69, 0x02, 0x03, 0x01, 0x00, 0x01
};
static int calc_digest_from_image(const u8 *data, const long size, u8 *digest)
{
int ret = 0;
struct crypto_shash *tfm = NULL;
struct shash_desc *desc = NULL;
const char *alg_name = "sha256";
if (!data || !digest || size < 0) {
pr_err("--> %s) invalid parameter\n", __func__);
return -EINVAL;
}
tfm = crypto_alloc_shash(alg_name, CRYPTO_ALG_TYPE_SHASH, 0);
if (IS_ERR(tfm)) {
pr_err("--> %s) can't alloc tfm\n", __func__);
return PTR_ERR(tfm);
}
desc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
if (!desc) {
pr_err("--> %s) can't alloc desc\n", __func__);
ret = PTR_ERR(desc);
goto fail_desc;
}
desc->tfm = tfm;
ret = crypto_shash_init(desc);
if (ret < 0) {
pr_err("--> %s) crypto_shash_init() failed\n", __func__);
goto out;
}
ret = crypto_shash_update(desc, data, size);
if (ret < 0) {
pr_err("--> %s) crypto_shash_update() failed\n", __func__);
goto out;
}
ret = crypto_shash_final(desc, digest);
if (ret < 0)
pr_err("--> %s) crypto_shash_final() failed\n", __func__);
out:
if (desc)
kfree(desc);
fail_desc:
crypto_free_shash(tfm);
return ret;
}
long spu_firmware_signature_verify(const char *fw_name, const u8 *fw_data, const long fw_size)
{
int i = 0;
int ret = 0;
long fw_offset = fw_size;
long TAG_LEN = 0;
struct public_key *rsa_pub_key = NULL;
struct public_key_signature *sig = NULL;
u8 *signature = NULL;
u8 read_digest[DIGEST_LEN] = { 0x00, };
u8 calc_digest[DIGEST_LEN] = { 0x00, };
if (!fw_name || !fw_data || fw_size < 0) {
pr_info("invalid parameter...\n");
return -EINVAL;
}
pr_info("%s requested for signature verify\n", fw_name);
for (i = 0; i < ARRAY_SIZE(spu_tag_name); ++i) {
TAG_LEN = strlen(spu_tag_name[i]);
if (strncmp(fw_name, spu_tag_name[i], TAG_LEN) == 0) {
ret = 1;
break;
}
}
if (!ret) {
pr_info("This firmware don't support...\n");
return -1;
}
signature = kzalloc(SIGN_LEN, GFP_KERNEL);
if (!signature) {
pr_err("signature kmalloc ERROR\n");
return -ENOMEM;
}
fw_offset -= SIGN_LEN;
memcpy(signature, fw_data + fw_offset, SIGN_LEN);
fw_offset -= DIGEST_LEN;
memcpy(read_digest, fw_data + fw_offset, DIGEST_LEN);
fw_offset -= TAG_LEN;
if (TAG_LEN <= 0 || strncmp(fw_name, fw_data + fw_offset, TAG_LEN) != 0) {
pr_info("Firmware mismatch...\n");
ret = -FW_DIGEST_MISMATCH;
goto fail_mismatch;
}
fw_offset += TAG_LEN;
ret = calc_digest_from_image(fw_data, fw_offset, calc_digest);
if (ret < 0) {
pr_err("calc_digest_from_image ERROR\n");
goto fail_mismatch;
}
if (memcmp(read_digest, calc_digest, DIGEST_LEN) != 0) {
pr_err("digset matching FAILED...\n");
ret = -FW_DIGEST_MISMATCH;
goto fail_mismatch;
}
rsa_pub_key = kzalloc(sizeof(struct public_key), GFP_KERNEL);
if (!rsa_pub_key) {
pr_err("rsa_pub_key kmalloc ERROR\n");
ret = -ENOMEM;
goto fail_mismatch;
}
sig = kzalloc(sizeof(struct public_key_signature), GFP_KERNEL);
if (!sig) {
pr_err("sig kmalloc ERROR\n");
ret = -ENOMEM;
goto fail_sig;
}
rsa_pub_key->key = (void *)PublicKey;
rsa_pub_key->keylen = (u32)(sizeof(PublicKey));
rsa_pub_key->id_type = "X509";
rsa_pub_key->pkey_algo = "rsa";
sig->s = signature;
sig->s_size = (u32)SIGN_LEN;
sig->digest = calc_digest;
sig->digest_size = (u8)DIGEST_LEN;
sig->pkey_algo = "rsa";
sig->hash_algo = "sha256";
sig->encoding = "pkcs1";
ret = public_key_verify_signature(rsa_pub_key, sig);
if (ret) {
pr_info("verifying ERROR(%d)\n", ret);
} else {
pr_info("verified successfully!!!\n");
ret = fw_offset - TAG_LEN;
}
if (sig)
kfree(sig);
fail_sig:
if (rsa_pub_key)
kfree(rsa_pub_key);
fail_mismatch:
if (signature)
kfree(signature);
return ret;
}
EXPORT_SYMBOL(spu_firmware_signature_verify);