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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2021 Philippe Reynes <philippe.reynes@softathome.com>
*/
#include <common.h>
#include <asm/global_data.h>
DECLARE_GLOBAL_DATA_PTR;
#include <image.h>
#include <mapmem.h>
#include <u-boot/sha256.h>
#define IMAGE_PRE_LOAD_SIG_MAGIC 0x55425348
#define IMAGE_PRE_LOAD_SIG_OFFSET_MAGIC 0
#define IMAGE_PRE_LOAD_SIG_OFFSET_IMG_LEN 4
#define IMAGE_PRE_LOAD_SIG_OFFSET_SIG 8
#define IMAGE_PRE_LOAD_PATH "/image/pre-load/sig"
#define IMAGE_PRE_LOAD_PROP_ALGO_NAME "algo-name"
#define IMAGE_PRE_LOAD_PROP_PADDING_NAME "padding-name"
#define IMAGE_PRE_LOAD_PROP_SIG_SIZE "signature-size"
#define IMAGE_PRE_LOAD_PROP_PUBLIC_KEY "public-key"
#define IMAGE_PRE_LOAD_PROP_MANDATORY "mandatory"
#ifndef CONFIG_SYS_BOOTM_LEN
/* use 8MByte as default max gunzip size */
#define CONFIG_SYS_BOOTM_LEN 0x800000
#endif
/*
* Information in the device-tree about the signature in the header
*/
struct image_sig_info {
char *algo_name; /* Name of the algo (eg: sha256,rsa2048) */
char *padding_name; /* Name of the padding */
u8 *key; /* Public signature key */
int key_len; /* Length of the public key */
u32 sig_size; /* size of the signature (in the header) */
int mandatory; /* Set if the signature is mandatory */
struct image_sign_info sig_info; /* Signature info */
};
/*
* Header of the signature header
*/
struct sig_header_s {
u32 magic;
u32 version;
u32 header_size;
u32 image_size;
u32 offset_img_sig;
u32 flags;
u32 reserved0;
u32 reserved1;
u8 sha256_img_sig[SHA256_SUM_LEN];
};
#define SIG_HEADER_LEN (sizeof(struct sig_header_s))
/*
* Offset of the image
*
* This value is used to skip the header before really launching the image
*/
ulong image_load_offset;
/*
* This function gathers information about the signature check
* that could be done before launching the image.
*
* return:
* < 0 => an error has occurred
* 0 => OK
* 1 => no setup
*/
static int image_pre_load_sig_setup(struct image_sig_info *info)
{
const void *algo_name, *padding_name, *key, *mandatory;
const u32 *sig_size;
int key_len;
int node, ret = 0;
if (!info) {
log_err("ERROR: info is NULL for image pre-load sig check\n");
ret = -EINVAL;
goto out;
}
memset(info, 0, sizeof(*info));
node = fdt_path_offset(gd_fdt_blob(), IMAGE_PRE_LOAD_PATH);
if (node < 0) {
log_info("INFO: no info for image pre-load sig check\n");
ret = 1;
goto out;
}
algo_name = fdt_getprop(gd_fdt_blob(), node,
IMAGE_PRE_LOAD_PROP_ALGO_NAME, NULL);
if (!algo_name) {
printf("ERROR: no algo_name for image pre-load sig check\n");
ret = -EINVAL;
goto out;
}
padding_name = fdt_getprop(gd_fdt_blob(), node,
IMAGE_PRE_LOAD_PROP_PADDING_NAME, NULL);
if (!padding_name) {
log_info("INFO: no padding_name provided, so using pkcs-1.5\n");
padding_name = "pkcs-1.5";
}
sig_size = fdt_getprop(gd_fdt_blob(), node,
IMAGE_PRE_LOAD_PROP_SIG_SIZE, NULL);
if (!sig_size) {
log_err("ERROR: no signature-size for image pre-load sig check\n");
ret = -EINVAL;
goto out;
}
key = fdt_getprop(gd_fdt_blob(), node,
IMAGE_PRE_LOAD_PROP_PUBLIC_KEY, &key_len);
if (!key) {
log_err("ERROR: no key for image pre-load sig check\n");
ret = -EINVAL;
goto out;
}
info->algo_name = (char *)algo_name;
info->padding_name = (char *)padding_name;
info->key = (uint8_t *)key;
info->key_len = key_len;
info->sig_size = fdt32_to_cpu(*sig_size);
mandatory = fdt_getprop(gd_fdt_blob(), node,
IMAGE_PRE_LOAD_PROP_MANDATORY, NULL);
if (mandatory && !strcmp((char *)mandatory, "yes"))
info->mandatory = 1;
/* Compute signature information */
info->sig_info.name = info->algo_name;
info->sig_info.padding = image_get_padding_algo(info->padding_name);
info->sig_info.checksum = image_get_checksum_algo(info->sig_info.name);
info->sig_info.crypto = image_get_crypto_algo(info->sig_info.name);
info->sig_info.key = info->key;
info->sig_info.keylen = info->key_len;
out:
return ret;
}
static int image_pre_load_sig_get_magic(ulong addr, u32 *magic)
{
struct sig_header_s *sig_header;
int ret = 0;
sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
if (!sig_header) {
log_err("ERROR: can't map first header\n");
ret = -EFAULT;
goto out;
}
*magic = fdt32_to_cpu(sig_header->magic);
unmap_sysmem(sig_header);
out:
return ret;
}
static int image_pre_load_sig_get_header_size(ulong addr, u32 *header_size)
{
struct sig_header_s *sig_header;
int ret = 0;
sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
if (!sig_header) {
log_err("ERROR: can't map first header\n");
ret = -EFAULT;
goto out;
}
*header_size = fdt32_to_cpu(sig_header->header_size);
unmap_sysmem(sig_header);
out:
return ret;
}
/*
* return:
* < 0 => no magic and magic mandatory (or error when reading magic)
* 0 => magic found
* 1 => magic NOT found
*/
static int image_pre_load_sig_check_magic(struct image_sig_info *info, ulong addr)
{
u32 magic;
int ret = 1;
ret = image_pre_load_sig_get_magic(addr, &magic);
if (ret < 0)
goto out;
if (magic != IMAGE_PRE_LOAD_SIG_MAGIC) {
if (info->mandatory) {
log_err("ERROR: signature is mandatory\n");
ret = -EINVAL;
goto out;
}
ret = 1;
goto out;
}
ret = 0; /* magic found */
out:
return ret;
}
static int image_pre_load_sig_check_header_sig(struct image_sig_info *info, ulong addr)
{
void *header;
struct image_region reg;
u32 sig_len;
u8 *sig;
int ret = 0;
/* Only map header of the header and its signature */
header = (void *)map_sysmem(addr, SIG_HEADER_LEN + info->sig_size);
if (!header) {
log_err("ERROR: can't map header\n");
ret = -EFAULT;
goto out;
}
reg.data = header;
reg.size = SIG_HEADER_LEN;
sig = (uint8_t *)header + SIG_HEADER_LEN;
sig_len = info->sig_size;
ret = info->sig_info.crypto->verify(&info->sig_info, ®, 1, sig, sig_len);
if (ret) {
log_err("ERROR: header signature check has failed (err=%d)\n", ret);
ret = -EINVAL;
goto out_unmap;
}
out_unmap:
unmap_sysmem(header);
out:
return ret;
}
static int image_pre_load_sig_check_img_sig_sha256(struct image_sig_info *info, ulong addr)
{
struct sig_header_s *sig_header;
u32 header_size, offset_img_sig;
void *header;
u8 sha256_img_sig[SHA256_SUM_LEN];
int ret = 0;
sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
if (!sig_header) {
log_err("ERROR: can't map first header\n");
ret = -EFAULT;
goto out;
}
header_size = fdt32_to_cpu(sig_header->header_size);
offset_img_sig = fdt32_to_cpu(sig_header->offset_img_sig);
header = (void *)map_sysmem(addr, header_size);
if (!header) {
log_err("ERROR: can't map header\n");
ret = -EFAULT;
goto out_sig_header;
}
sha256_csum_wd(header + offset_img_sig, info->sig_size,
sha256_img_sig, CHUNKSZ_SHA256);
ret = memcmp(sig_header->sha256_img_sig, sha256_img_sig, SHA256_SUM_LEN);
if (ret) {
log_err("ERROR: sha256 of image signature is invalid\n");
ret = -EFAULT;
goto out_header;
}
out_header:
unmap_sysmem(header);
out_sig_header:
unmap_sysmem(sig_header);
out:
return ret;
}
static int image_pre_load_sig_check_img_sig(struct image_sig_info *info, ulong addr)
{
struct sig_header_s *sig_header;
u32 header_size, image_size, offset_img_sig;
void *image;
struct image_region reg;
u32 sig_len;
u8 *sig;
int ret = 0;
sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
if (!sig_header) {
log_err("ERROR: can't map first header\n");
ret = -EFAULT;
goto out;
}
header_size = fdt32_to_cpu(sig_header->header_size);
image_size = fdt32_to_cpu(sig_header->image_size);
offset_img_sig = fdt32_to_cpu(sig_header->offset_img_sig);
unmap_sysmem(sig_header);
image = (void *)map_sysmem(addr, header_size + image_size);
if (!image) {
log_err("ERROR: can't map full image\n");
ret = -EFAULT;
goto out;
}
reg.data = image + header_size;
reg.size = image_size;
sig = (uint8_t *)image + offset_img_sig;
sig_len = info->sig_size;
ret = info->sig_info.crypto->verify(&info->sig_info, ®, 1, sig, sig_len);
if (ret) {
log_err("ERROR: signature check has failed (err=%d)\n", ret);
ret = -EINVAL;
goto out_unmap_image;
}
log_info("INFO: signature check has succeed\n");
out_unmap_image:
unmap_sysmem(image);
out:
return ret;
}
int image_pre_load_sig(ulong addr)
{
struct image_sig_info info;
int ret;
ret = image_pre_load_sig_setup(&info);
if (ret < 0)
goto out;
if (ret > 0) {
ret = 0;
goto out;
}
ret = image_pre_load_sig_check_magic(&info, addr);
if (ret < 0)
goto out;
if (ret > 0) {
ret = 0;
goto out;
}
/* Check the signature of the signature header */
ret = image_pre_load_sig_check_header_sig(&info, addr);
if (ret < 0)
goto out;
/* Check sha256 of the image signature */
ret = image_pre_load_sig_check_img_sig_sha256(&info, addr);
if (ret < 0)
goto out;
/* Check the image signature */
ret = image_pre_load_sig_check_img_sig(&info, addr);
if (!ret) {
u32 header_size;
ret = image_pre_load_sig_get_header_size(addr, &header_size);
if (ret) {
log_err("%s: can't get header size\n", __func__);
ret = -EINVAL;
goto out;
}
image_load_offset += header_size;
}
out:
return ret;
}
int image_pre_load(ulong addr)
{
int ret = 0;
image_load_offset = 0;
if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD_SIG))
ret = image_pre_load_sig(addr);
return ret;
}
|