1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
* spu-sign-verify.c - When SPU fireware update, image signature verify.
*
* Author: JJungs-lee <jhs2.lee@samsung.com>
*
*/
#define pr_fmt(fmt) "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, MFC_TAG, WACOM_TAG, PDIC_TAG, SENSORHUB_TAG };
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;
desc->flags = 0x0;
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";
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;
}
|