1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: Secure Verify for Loaderboot and Flashboot
15 *
16 * Create: 2023-03-09
17 */
18
19 #include "boot_serial.h"
20 #include "securec.h"
21 #include "efuse_wrap.h"
22 #include "drv_rom_cipher.h"
23 #include "boot_errcode.h"
24 #include "secure_verify_boot.h"
25
26 #define MAINTENANCE_MODE_EN 0x2A13C812
27
28 #define OEM_ROOT_PUBLIC_KEY_IMAGE_ID 0x4BA5C31E
29 #define PROVISION_KEY_AREA_IMAGE_ID 0x4BD2F01E
30 #define PROVISION_CODE_INFO_IMAGE_ID 0x4BD2F02D
31 #define PARAMS_KET_AREA_IMAGE_ID 0x4B87A51E
32 #define PARAMS_AREA_INFO_IMAGE_ID 0x4B87A52D
33 #define PARAMS_AREA_IMAGE_ID 0x4B87A54B
34 #define FLASHBOOT_KEY_AREA_IMAGE_ID 0x4B1E3C1E
35 #define FLASHBOOT_CODE_INFO_IMAGE_ID 0x4B1E3C2D
36 #define FLASHBOOT_BACKUP_KEY_AREA_IMAGE_ID 0x4B69871E
37 #define FLASHBOOT_BACKUP_CODE_INFO_IMAGE_ID 0x4B69872D
38 #define APPBOOT_KEY_AREA_IMAGE_ID 0x4B0F2D1E
39 #define APPBOOT_CODE_INFO_IMAGE_ID 0x4B0F2D2D
40
41 #define boot_array_size(arr) (sizeof(arr) / sizeof((arr)[0]))
42
43 typedef struct {
44 image_type_t image_type;
45 area_type_t erea_type;
46 uint32_t image_id;
47 efuse_idx efuse_type;
48 } verify_table_item_t;
49
50 uint32_t g_rootkey_status = 0;
51
52 static verify_table_item_t g_verify_table[] = {
53 {
54 .image_type = LOADER_BOOT_TYPE, .erea_type = KEY_EREA_TYPE,
55 .image_id = PROVISION_KEY_AREA_IMAGE_ID
56 },
57
58 {
59 .image_type = LOADER_BOOT_TYPE, .erea_type = CODE_INFO_TYPE,
60 .image_id = PROVISION_CODE_INFO_IMAGE_ID
61 },
62
63 {
64 .image_type = FLASH_BOOT_TYPE, .erea_type = KEY_EREA_TYPE,
65 .image_id = FLASHBOOT_KEY_AREA_IMAGE_ID,
66 .efuse_type = EFUSE_FLASHBOOT_VER_ID
67 },
68
69 {
70 .image_type = FLASH_BOOT_TYPE, .erea_type = CODE_INFO_TYPE,
71 .image_id = FLASHBOOT_CODE_INFO_IMAGE_ID,
72 .efuse_type = EFUSE_FLASHBOOT_VER_ID
73 },
74
75 {
76 .image_type = FLASH_BOOT_BACK_UP_TYPE, .erea_type = KEY_EREA_TYPE,
77 .image_id = FLASHBOOT_BACKUP_KEY_AREA_IMAGE_ID,
78 .efuse_type = EFUSE_FLASHBOOT_VER_ID
79 },
80
81 {
82 .image_type = FLASH_BOOT_BACK_UP_TYPE, .erea_type = CODE_INFO_TYPE,
83 .image_id = FLASHBOOT_BACKUP_CODE_INFO_IMAGE_ID,
84 .efuse_type = EFUSE_FLASHBOOT_VER_ID
85 },
86
87 {
88 .image_type = SECOND_FLASH_BOOT_TYPE, .erea_type = KEY_EREA_TYPE,
89 .image_id = FLASHBOOT_KEY_AREA_IMAGE_ID,
90 .efuse_type = EFUSE_FLASHBOOT_VER_ID
91 },
92
93 {
94 .image_type = SECOND_FLASH_BOOT_TYPE, .erea_type = CODE_INFO_TYPE,
95 .image_id = FLASHBOOT_CODE_INFO_IMAGE_ID,
96 .efuse_type = EFUSE_FLASHBOOT_VER_ID
97 },
98
99 {
100 .image_type = SECOND_FLASH_BOOT_BACK_UP_TYPE, .erea_type = KEY_EREA_TYPE,
101 .image_id = FLASHBOOT_BACKUP_KEY_AREA_IMAGE_ID,
102 .efuse_type = EFUSE_FLASHBOOT_VER_ID
103 },
104
105 {
106 .image_type = SECOND_FLASH_BOOT_BACK_UP_TYPE, .erea_type = CODE_INFO_TYPE,
107 .image_id = FLASHBOOT_BACKUP_CODE_INFO_IMAGE_ID,
108 .efuse_type = EFUSE_FLASHBOOT_VER_ID
109 },
110
111 {
112 .image_type = PARAMS_BOOT_TYPE, .erea_type = PARAMS_KEY_AREA_TYPE,
113 .image_id = PARAMS_KET_AREA_IMAGE_ID,
114 .efuse_type = EFUSE_PARAMS_VER_ID
115 },
116
117 {
118 .image_type = PARAMS_BOOT_TYPE, .erea_type = PARAMS_AREA_IOFO_TYPE,
119 .image_id = PARAMS_AREA_INFO_IMAGE_ID,
120 .efuse_type = EFUSE_PARAMS_VER_ID
121 },
122
123 {
124 .image_type = APP_BOOT_TYPE, .erea_type = KEY_EREA_TYPE,
125 .image_id = APPBOOT_KEY_AREA_IMAGE_ID,
126 .efuse_type = EFUSE_MCU_VER_ID
127 },
128
129 {
130 .image_type = APP_BOOT_TYPE, .erea_type = CODE_INFO_TYPE,
131 .image_id = APPBOOT_CODE_INFO_IMAGE_ID,
132 .efuse_type = EFUSE_MCU_VER_ID
133 },
134 };
135
check_image_id(image_type_t image_type,area_type_t erea_type,uint32_t image_id)136 static errcode_t check_image_id(image_type_t image_type, area_type_t erea_type, uint32_t image_id)
137 {
138 uint32_t image_id_check = 0;
139
140 for (uint32_t i = 0; i < boot_array_size(g_verify_table); i++) {
141 if (image_type == g_verify_table[i].image_type && erea_type == g_verify_table[i].erea_type) {
142 image_id_check = g_verify_table[i].image_id;
143 break;
144 }
145 if (i == boot_array_size(g_verify_table)) {
146 boot_msg0("get key erea iamge id failed!");
147 return ERRCODE_FAIL;
148 }
149 }
150
151 if (image_id != image_id_check) {
152 boot_msg0("image id error!");
153 return ERRCODE_FAIL;
154 }
155
156 return ERRCODE_SUCC;
157 }
158
count_bit_num(uint32_t input)159 static uint32_t count_bit_num(uint32_t input)
160 {
161 uint32_t count = 0; /* count accumulates the total bits set in input */
162 uint32_t input_copy = input;
163
164 for (count = 0; input_copy >= 1; count++) {
165 input_copy &= input_copy - 1; /* clear the least significant bit set */
166 }
167
168 return count;
169 }
170
check_version(image_type_t image_type,uint32_t version,uint32_t version_mask)171 static errcode_t check_version(image_type_t image_type, uint32_t version, uint32_t version_mask)
172 {
173 uint32_t efuse_version = 0;
174 uint32_t efuse_idx = 0;
175 uint32_t ret = 0;
176
177 for (uint32_t i = 0; i < boot_array_size(g_verify_table); i++) {
178 if (image_type == g_verify_table[i].image_type) {
179 efuse_idx = g_verify_table[i].efuse_type;
180 break;
181 }
182 if (i == boot_array_size(g_verify_table)) {
183 boot_msg0("get key erea iamge id failed!");
184 return ERRCODE_FAIL;
185 }
186 }
187
188 /* read otp version */
189 /* sizeof(efuse_version) = 4 */
190 ret = efuse_read_item(efuse_idx, (uint8_t *)&efuse_version, (uint16_t)sizeof(efuse_version));
191 if (ret != ERRCODE_SUCC) {
192 boot_msg0("version efuse_read_item fail!");
193 return ERRCODE_FAIL;
194 }
195
196 if (count_bit_num(version & version_mask) < count_bit_num(efuse_version & version_mask)) {
197 boot_msg0("version compare fail!");
198 return ERRCODE_FAIL;
199 }
200
201 return ERRCODE_SUCC;
202 }
203
check_msid(uint32_t msid,uint32_t msid_mask)204 static errcode_t check_msid(uint32_t msid, uint32_t msid_mask)
205 {
206 uint32_t efuse_msid = 0;
207 uint32_t ret = 0;
208
209 /* sizeof(efuse_msid) = 4 */
210 ret = efuse_read_item(EFUSE_MSID_ID, (uint8_t *)&efuse_msid, (uint16_t)sizeof(efuse_msid));
211 if (ret != ERRCODE_SUCC) {
212 boot_msg0("version efuse_read_item fail!");
213 return ERRCODE_FAIL;
214 }
215
216 if ((msid & msid_mask) != (efuse_msid & msid_mask)) {
217 boot_msg0("msid compare fail!");
218 return ERRCODE_FAIL;
219 }
220
221 return ERRCODE_SUCC;
222 }
223
check_die_id(uint8_t * die_id,uint32_t die_id_length)224 static errcode_t check_die_id(uint8_t *die_id, uint32_t die_id_length)
225 {
226 uint8_t efuse_die_id[DIE_ID_LEN] = { 0 };
227 uint32_t ret = 0;
228
229 if (die_id_length != DIE_ID_LEN) {
230 return ERRCODE_FAIL;
231 }
232
233 /* sizeof(efuse_die_id) = 16 */
234 ret = efuse_read_item(EFUSE_DIE_ID, (uint8_t *)efuse_die_id, (uint16_t)sizeof(efuse_die_id));
235 if (ret != ERRCODE_SUCC) {
236 boot_msg0("die_id efuse_read_item fail!");
237 return ERRCODE_FAIL;
238 }
239
240 ret = memcmp(efuse_die_id, die_id, die_id_length);
241 if (ret != 0) {
242 boot_msg0("die id memcmp fail!");
243 return ERRCODE_FAIL;
244 }
245
246 return ret;
247 }
248
check_verify_enable(void)249 static errcode_t check_verify_enable(void)
250 {
251 uint32_t ret = 0;
252 uint8_t verify_enable = 0xff;
253
254 /* sizeof(verify_enable) = 1 */
255 ret = efuse_read_item(EFUSE_SEC_VERIFY_ENABLE, &verify_enable, (uint16_t)sizeof(verify_enable));
256 if (ret != ERRCODE_SUCC) {
257 boot_msg0("efuse_read_item verify enable fail!");
258 return ERRCODE_FAIL;
259 }
260 if (verify_enable == 0) {
261 return ERRCODE_NOT_SUPPORT;
262 }
263
264 return ERRCODE_SUCC;
265 }
266
267 #if defined(CONFIG_BOOT_SUPPORT_ECC_VERIFY)
secure_authenticate(const uint8_t * key,const uint8_t * data,uint32_t data_length,const uint8_t * sign_buff,uint32_t sign_length)268 static errcode_t secure_authenticate(const uint8_t *key, const uint8_t *data, uint32_t data_length,
269 const uint8_t *sign_buff, uint32_t sign_length)
270 {
271 uint8_t hash_result[HASH_LEN];
272 drv_rom_cipher_ecc_point input_pub_key = { 0 };
273 drv_rom_cipher_data input_hash = { 0 };
274 drv_rom_cipher_ecc_sig input_sig = { 0 };
275 uint32_t ret = 0;
276
277 ret = drv_rom_cipher_sha256(data, data_length, hash_result, (uint32_t)HASH_LEN);
278 if (ret != ERRCODE_SUCC) {
279 boot_msg0("secure_authenticate drv_rom_cipher_sha256 fail!");
280 return ERRCODE_FAIL;
281 }
282
283 input_pub_key.x = (uint8_t *)key;
284 input_pub_key.y = (uint8_t *)(key + 32); /* 32 is ecc bp256 key length */
285 input_pub_key.length = 32; /* 32 is ecc bp256 key length */
286 input_hash.data = hash_result;
287 input_hash.length = HASH_LEN;
288 input_sig.r = (uint8_t *)sign_buff;
289 input_sig.s = (uint8_t *)(sign_buff + sign_length / 2); /* 2: sign = r + s */
290 input_sig.length = sign_length / 2; /* 2: sign = r + s */
291
292 ret = (uint32_t)drv_rom_cipher_pke_bp256r_verify(&input_pub_key, &input_hash, &input_sig);
293 if (ret != ERRCODE_SUCC) {
294 boot_msg0("secure_authenticate drv_rom_cipher_pke_bp256r_verify fail!");
295 return ERRCODE_FAIL;
296 }
297
298 return ret;
299 }
300 #endif
301
302 #if defined(CONFIG_BOOT_SUPPORT_SM2_VERIFY)
secure_authenticate(const uint8_t * key,const uint8_t * data,uint32_t data_length,const uint8_t * sign_buff,uint32_t sign_length)303 static errcode_t secure_authenticate(const uint8_t *key, const uint8_t *data, uint32_t data_length,
304 const uint8_t *sign_buff, uint32_t sign_length)
305 {
306 uint8_t hash_result[HASH_LEN] = { 0 };
307 drv_rom_cipher_ecc_point input_pub_key = { 0 };
308 drv_rom_cipher_pke_msg input_msg = { 0 };
309 drv_rom_cipher_data input_hash = { 0 };
310 drv_rom_cipher_ecc_sig input_sig = { 0 };
311 uint8_t sm2_id[18] = "\x42\x4c\x49\x43\x45\x31\x32\x33\x40\x59\x41\x48\x4f\x4f\x2\x43\x4f\x11";
312 uint32_t ret = 0;
313
314 input_pub_key.x = (uint8_t *)key;
315 input_pub_key.y = (uint8_t *)(key + 32); /* 32 is sm2 key length */
316 input_pub_key.length = 32; /* 32 is sm2 key length */
317 input_msg.data = (uint8_t *)data;
318 input_msg.length = data_length;
319 input_hash.data = hash_result;
320 input_hash.length = sizeof(hash_result);
321 ret = drv_rom_cipher_pke_sm2_dsa_hash(sm2_id, &input_pub_key, &input_msg, &input_hash);
322 if (ret != ERRCODE_SUCC) {
323 boot_msg0("secure_authenticate drv_rom_cipher_pke_sm2_dsa_hash fail!");
324 return ERRCODE_FAIL;
325 }
326
327 input_sig.r = (uint8_t *)sign_buff;
328 input_sig.s = (uint8_t *)(sign_buff + sign_length / 2); /* 2: sign = r + s */
329 input_sig.length = sign_length / 2; /* 2: sign = r + s */
330 ret = drv_rom_cipher_pke_sm2_verify(input_pub_key, input_hash, input_sig);
331 if (ret != ERRCODE_SUCC) {
332 boot_msg0("secure_authenticate drv_rom_cipher_pke_sm2_verify fail!");
333 return ERRCODE_FAIL;
334 }
335
336 return ret;
337 }
338 #endif
339
verify_params_key_area(const params_key_area_t * key_area,const uint8_t * public_key)340 static errcode_t verify_params_key_area(const params_key_area_t *key_area, const uint8_t *public_key)
341 {
342 uint32_t ret = 0;
343
344 /* check verify enable */
345 ret = check_verify_enable();
346 if (ret == ERRCODE_NOT_SUPPORT) {
347 return ERRCODE_SUCC;
348 } else if (ret == ERRCODE_FAIL) {
349 boot_msg0("verify_params_key_area secure verify error!");
350 return ERRCODE_BOOT_VERIFY_CHECK_ENABLE;
351 }
352
353 if (g_rootkey_status != 0x5) {
354 boot_msg0("verify_params_key_area rootkey_status invalid!");
355 return ERRCODE_BOOT_VERIFY_INVALID_ROOT_KEY;
356 }
357
358 /* check image id */
359 ret = check_image_id(PARAMS_BOOT_TYPE, PARAMS_KEY_AREA_TYPE, key_area->image_id);
360 if (ret != ERRCODE_SUCC) {
361 boot_msg0("verify_params_key_area image id error!");
362 return ERRCODE_BOOT_VERIFY_INVALID_IMAGE_ID;
363 }
364
365 /* verify key area with rootkey */
366 ret = secure_authenticate(public_key, (uint8_t *)key_area, KEY_AREA_STRUCTURE_LENGTH - BOOT_SIG_LEN,
367 key_area->sig_params_key_area, BOOT_PUBLIC_KEY_LEN);
368 if (ret != ERRCODE_SUCC) {
369 boot_msg0("verify_params_key_area secure_authenticate fail!");
370 return ERRCODE_BOOT_VERIFY_PKE_VERIFY;
371 }
372
373 /* check key version ext */
374 ret = check_version(PARAMS_BOOT_TYPE,
375 key_area->params_key_version_ext, key_area->mask_params_key_version_ext);
376 if (ret != ERRCODE_SUCC) {
377 boot_msg0("verify_params_key_area key version error!");
378 return ERRCODE_BOOT_VERIFY_INVALID_VERSION;
379 }
380
381 /* check key msid */
382 ret = check_msid(key_area->msid_ext, key_area->mask_msid_ext);
383 if (ret != ERRCODE_SUCC) {
384 boot_msg0("verify_params_key_area msid error!");
385 return ERRCODE_BOOT_VERIFY_INVALID_MSID;
386 }
387
388 /* check maintention mode */
389 if (key_area->maintenance_mode == MAINTENANCE_MODE_EN) {
390 ret = check_die_id((uint8_t *)key_area->die_id, DIE_ID_LEN);
391 if (ret != ERRCODE_SUCC) {
392 boot_msg0("verify_params_key_area die id error!");
393 return ERRCODE_BOOT_VERIFY_INVALID_DIE_ID;
394 }
395 }
396
397 return ret;
398 }
399
verify_params_area_info(const params_area_info_t * area_info,const uint8_t * public_key)400 static errcode_t verify_params_area_info(const params_area_info_t *area_info, const uint8_t *public_key)
401 {
402 uint32_t ret = 0;
403
404 /* check verify enable */
405 ret = check_verify_enable();
406 if (ret == ERRCODE_NOT_SUPPORT) {
407 return ERRCODE_SUCC;
408 } else if (ret == ERRCODE_FAIL) {
409 boot_msg0("verify_params_area_info secure verify error!");
410 return ERRCODE_BOOT_VERIFY_CHECK_ENABLE;
411 }
412
413 /* check image id */
414 ret = check_image_id(PARAMS_BOOT_TYPE, PARAMS_AREA_IOFO_TYPE, area_info->image_id);
415 if (ret != ERRCODE_SUCC) {
416 boot_msg0("verify_params_area_info image id error!");
417 return ERRCODE_BOOT_VERIFY_INVALID_IMAGE_ID;
418 }
419
420 /* verify code info with external public key */
421 ret = secure_authenticate(public_key, (uint8_t *)area_info,
422 CODE_INFO_STRUCTURE_LENGTH - BOOT_SIG_LEN - BOOT_EXT_SIG_LEN, area_info->sig_params_info, BOOT_PUBLIC_KEY_LEN);
423 if (ret != ERRCODE_SUCC) {
424 boot_msg0("verify_params_area_info secure_authenticate fail!");
425 return ERRCODE_BOOT_VERIFY_PKE_VERIFY;
426 }
427
428 /* check version ext */
429 ret = check_version(PARAMS_BOOT_TYPE,
430 area_info->params_version_ext, area_info->mask_params_version_ext);
431 if (ret != ERRCODE_SUCC) {
432 boot_msg0("verify_params_area_info key version error!");
433 return ERRCODE_BOOT_VERIFY_INVALID_VERSION;
434 }
435
436 /* check msid */
437 ret = check_msid(area_info->msid_ext, area_info->mask_msid_ext);
438 if (ret != ERRCODE_SUCC) {
439 boot_msg0("verify_params_area_info msid error!");
440 return ERRCODE_BOOT_VERIFY_INVALID_MSID;
441 }
442
443 return ret;
444 }
445
verify_params_area(const params_area_info_t * area_info,const uint8_t * area_addr)446 static errcode_t verify_params_area(const params_area_info_t *area_info, const uint8_t *area_addr)
447 {
448 uint32_t area_length = 0;
449 uint8_t hash_result[HASH_LEN] = { 0 };
450 uint32_t ret = 0;
451
452 /* caculate code area hash */
453 area_length = area_info->params_area_len;
454 ret = drv_rom_cipher_sha256((unsigned char *)(uintptr_t)area_addr, area_length, hash_result, (uint32_t)HASH_LEN);
455 if (ret != ERRCODE_SUCC) {
456 boot_msg0("verify_params_area drv_rom_cipher_sha256 fail!");
457 return ERRCODE_BOOT_VERIFY_HASH_CALCULATION;
458 }
459
460 ret = memcmp(hash_result, area_info->params_area_hash, (uint32_t)HASH_LEN);
461 if (ret != 0) {
462 boot_msg0("verify_params_area hash memcmp fail!");
463 return ERRCODE_BOOT_VERIFY_INVALID_HASH_RESULT;
464 }
465
466 return ret;
467 }
468
verify_image_key_area(image_type_t image_type,area_type_t erea_type,const image_key_area_t * key_area,const uint8_t * public_key)469 static errcode_t verify_image_key_area(image_type_t image_type, area_type_t erea_type,
470 const image_key_area_t *key_area, const uint8_t *public_key)
471 {
472 uint32_t ret = 0;
473
474 /* check verify enable */
475 ret = check_verify_enable();
476 if (ret == ERRCODE_NOT_SUPPORT) {
477 boot_msg0("verify_image_key_area secure verify disable!");
478 return ERRCODE_SUCC;
479 } else if (ret == ERRCODE_FAIL) {
480 boot_msg0("verify_image_key_area secure verify error!");
481 return ERRCODE_BOOT_VERIFY_CHECK_ENABLE;
482 }
483
484 if ((image_type == LOADER_BOOT_TYPE) || (image_type == FLASH_BOOT_TYPE) ||
485 (image_type == FLASH_BOOT_BACK_UP_TYPE)) {
486 if (g_rootkey_status != 0x5) {
487 boot_msg0("verify_image_key_area rootkey_status invalid!");
488 return ERRCODE_BOOT_VERIFY_INVALID_ROOT_KEY;
489 }
490 }
491
492 /* check image id */
493 ret = check_image_id(image_type, erea_type, key_area->image_id);
494 if (ret != ERRCODE_SUCC) {
495 boot_msg0("verify_image_key_area image id error!");
496 return ERRCODE_BOOT_VERIFY_INVALID_IMAGE_ID;
497 }
498
499 /* verify key area with rootkey */
500 ret = secure_authenticate(public_key, (uint8_t *)key_area, KEY_AREA_STRUCTURE_LENGTH - BOOT_SIG_LEN,
501 key_area->sig_key_area, BOOT_PUBLIC_KEY_LEN);
502 if (ret != ERRCODE_SUCC) {
503 boot_msg0("verify_image_key_area secure_authenticate!");
504 return ERRCODE_BOOT_VERIFY_PKE_VERIFY;
505 }
506
507 /* check key version ext */
508 if (image_type != LOADER_BOOT_TYPE) {
509 ret = check_version(image_type, key_area->key_version_ext, key_area->mask_key_version_ext);
510 if (ret != ERRCODE_SUCC) {
511 boot_msg0("verify_image_key_area key version error!");
512 return ERRCODE_BOOT_VERIFY_INVALID_VERSION;
513 }
514 }
515
516 /* check key msid */
517 ret = check_msid(key_area->msid_ext, key_area->mask_msid_ext);
518 if (ret != ERRCODE_SUCC) {
519 boot_msg0("verify_image_key_area msid error!");
520 return ERRCODE_BOOT_VERIFY_INVALID_MSID;
521 }
522
523 /* check maintention mode */
524 if (key_area->maintenance_mode == MAINTENANCE_MODE_EN) {
525 ret = check_die_id((uint8_t *)key_area->die_id, DIE_ID_LEN);
526 if (ret != ERRCODE_SUCC) {
527 boot_msg0("verify_image_key_area die id error!");
528 return ERRCODE_BOOT_VERIFY_INVALID_DIE_ID;
529 }
530 }
531
532 return ret;
533 }
534
verify_image_code_info(image_type_t image_type,area_type_t erea_type,const image_code_info_t * code_info,const uint8_t * public_key)535 static errcode_t verify_image_code_info(image_type_t image_type, area_type_t erea_type,
536 const image_code_info_t *code_info, const uint8_t *public_key)
537 {
538 uint32_t ret = 0;
539
540 /* check verify enable */
541 ret = check_verify_enable();
542 if (ret == ERRCODE_NOT_SUPPORT) {
543 boot_msg0("verify_image_code_info secure verify disable!");
544 return ERRCODE_SUCC;
545 } else if (ret == ERRCODE_FAIL) {
546 boot_msg0("verify_image_code_info secure verify error!");
547 return ERRCODE_BOOT_VERIFY_CHECK_ENABLE;
548 }
549
550 /* check image id */
551 ret = check_image_id(image_type, erea_type, code_info->image_id);
552 if (ret != ERRCODE_SUCC) {
553 boot_msg0("verify_image_code_info image id error!");
554 return ERRCODE_BOOT_VERIFY_INVALID_IMAGE_ID;
555 }
556
557 /* verify code info with external public key */
558 ret = secure_authenticate(public_key, (uint8_t *)code_info,
559 CODE_INFO_STRUCTURE_LENGTH - BOOT_SIG_LEN - BOOT_EXT_SIG_LEN, code_info->sig_code_info, BOOT_PUBLIC_KEY_LEN);
560 if (ret != ERRCODE_SUCC) {
561 boot_msg0("verify_image_code_info secure_authenticate!");
562 return ERRCODE_BOOT_VERIFY_PKE_VERIFY;
563 }
564
565 /* check version ext */
566 if (image_type != LOADER_BOOT_TYPE) {
567 ret = check_version(image_type, code_info->version_ext, code_info->mask_version_ext);
568 if (ret != ERRCODE_SUCC) {
569 boot_msg0("verify_image_code_info key version error!");
570 return ERRCODE_BOOT_VERIFY_INVALID_VERSION;
571 }
572 }
573
574 /* check msid */
575 ret = check_msid(code_info->msid_ext, code_info->mask_msid_ext);
576 if (ret != ERRCODE_SUCC) {
577 boot_msg0("verify_image_code_info msid error!");
578 return ERRCODE_BOOT_VERIFY_INVALID_MSID;
579 }
580
581 return ret;
582 }
583
verify_image_code_area(const image_code_info_t * code_info,const uint8_t * code_addr)584 static errcode_t verify_image_code_area(const image_code_info_t *code_info, const uint8_t *code_addr)
585 {
586 uint32_t code_length = 0;
587 uint8_t hash_result[HASH_LEN] = { 0 };
588 uint32_t ret = 0;
589
590 /* caculate code area hash */
591 code_length = code_info->code_area_len;
592 ret = drv_rom_cipher_sha256((unsigned char *)(uintptr_t)code_addr, code_length, hash_result, (uint32_t)HASH_LEN);
593 if (ret != ERRCODE_SUCC) {
594 boot_msg0("verify_image_code_area drv_rom_cipher_sha256!");
595 return ERRCODE_BOOT_VERIFY_HASH_CALCULATION;
596 }
597 ret = memcmp(hash_result, code_info->code_area_hash, (uint32_t)HASH_LEN);
598 if (ret != 0) {
599 boot_msg0("verify_image_code_area hash memcmp fail!");
600 return ERRCODE_BOOT_VERIFY_INVALID_HASH_RESULT;
601 }
602
603 return ret;
604 }
605
verify_public_rootkey(uint32_t rootkey_buff_addr)606 errcode_t verify_public_rootkey(uint32_t rootkey_buff_addr)
607 {
608 uint8_t hash_result[HASH_LEN] = { 0 };
609 uint8_t hash_from_otp[HASH_LEN] = { 0 };
610 uint32_t ret = ERRCODE_SUCC;
611 const root_public_key_area_t *rootkey_buff = (root_public_key_area_t *)(uintptr_t)rootkey_buff_addr;
612
613 if (rootkey_buff == NULL) {
614 return ERRCODE_BOOT_VERIFY_PARAM_NULL;
615 }
616
617 g_rootkey_status = 0xA;
618
619 /* check verify enable */
620 ret = check_verify_enable();
621 if (ret == ERRCODE_NOT_SUPPORT) {
622 return ERRCODE_SUCC;
623 } else if (ret == ERRCODE_FAIL) {
624 boot_msg0("verify_public_rootkey secure verify error!");
625 return ERRCODE_BOOT_VERIFY_CHECK_ENABLE;
626 }
627
628 boot_msg0("secure boot.");
629
630 /* check rootkey_buff->image_id */
631 if (rootkey_buff->image_id != OEM_ROOT_PUBLIC_KEY_IMAGE_ID) {
632 boot_msg0("verify_public_rootkey image id error!");
633 return ERRCODE_BOOT_VERIFY_INVALID_IMAGE_ID;
634 }
635
636 /* caculate rootkey hash */
637 ret = drv_rom_cipher_sha256((unsigned char *)rootkey_buff, ROOT_PUBLIC_KEY_STRUCTURE_LENGTH,
638 hash_result, (uint32_t)HASH_LEN);
639 if (ret != ERRCODE_SUCC) {
640 boot_msg0("verify_public_rootkey drv_rom_cipher_sha256 fail!");
641 return ERRCODE_BOOT_VERIFY_HASH_CALCULATION;
642 }
643
644 /* read efuse rootkey hash */
645 /* sizeof(hash_from_otp) = 32 */
646 ret = efuse_read_item(EFUSE_HASH_ROOT_PUBLIC_KEY_ID, hash_from_otp, (uint16_t)sizeof(hash_from_otp));
647 if (ret != ERRCODE_SUCC) {
648 boot_msg0("verify_public_rootkey efuse_read_item fail!");
649 return ERRCODE_BOOT_VERIFY_EFUSE_READ;
650 }
651
652 /* check hash */
653 ret = memcmp(hash_from_otp, hash_result, (uint32_t)HASH_LEN);
654 if (ret != 0) {
655 boot_msg0("verify_public_rootkey hash memcmp fail!");
656 return ERRCODE_BOOT_VERIFY_INVALID_HASH_RESULT;
657 }
658
659 g_rootkey_status = 0x5;
660
661 return ret;
662 }
663
verify_image_head(image_type_t image_type,uint32_t public_key_addr,uint32_t boot_head_addr)664 errcode_t verify_image_head(image_type_t image_type, uint32_t public_key_addr, uint32_t boot_head_addr)
665 {
666 uint32_t ret = 0;
667 image_key_area_t *key_area = (image_key_area_t *)(uintptr_t)boot_head_addr;
668 image_code_info_t *code_info = (image_code_info_t *)(uintptr_t)((uintptr_t)key_area + KEY_AREA_STRUCTURE_LENGTH);
669
670 if (key_area == NULL || code_info == NULL) {
671 return ERRCODE_BOOT_VERIFY_PARAM_NULL;
672 }
673
674 ret = verify_image_key_area(image_type, KEY_EREA_TYPE, key_area, (uint8_t *)(uintptr_t)public_key_addr);
675 if (ret != ERRCODE_SUCC) {
676 boot_msg0("verify_image_key_area fail!");
677 return ret;
678 }
679
680 ret = verify_image_code_info(image_type, CODE_INFO_TYPE, code_info, key_area->ext_pulic_key_area);
681 if (ret != ERRCODE_SUCC) {
682 boot_msg0("verify_image_code_info fail!");
683 return ret;
684 }
685
686 return ret;
687 }
688
verify_image_body(uint32_t boot_head_addr,uint32_t boot_body_addr)689 errcode_t verify_image_body(uint32_t boot_head_addr, uint32_t boot_body_addr)
690 {
691 uint32_t ret = 0;
692 const image_code_info_t *code_info = (image_code_info_t *)(uintptr_t)(boot_head_addr + KEY_AREA_STRUCTURE_LENGTH);
693 const uint8_t *code_addr = (uint8_t *)(uintptr_t)boot_body_addr;
694
695 if (code_info == NULL || code_addr == NULL) {
696 return ERRCODE_BOOT_VERIFY_PARAM_NULL;
697 }
698
699 ret = verify_image_code_area(code_info, code_addr);
700 if (ret != ERRCODE_SUCC) {
701 boot_msg0("verify_image_code_area fail!");
702 return ret;
703 }
704
705 return ret;
706 }
707
verify_params_head(uint32_t root_public_key_addr,uint32_t params_head_addr)708 errcode_t verify_params_head(uint32_t root_public_key_addr, uint32_t params_head_addr)
709 {
710 uint32_t ret = 0;
711 root_public_key_area_t *public_rootkey = (root_public_key_area_t *)(uintptr_t)root_public_key_addr;
712 params_key_area_t *key_area = (params_key_area_t *)(uintptr_t)params_head_addr;
713 params_area_info_t *area_info = (params_area_info_t *)(uintptr_t)((uintptr_t)key_area + KEY_AREA_STRUCTURE_LENGTH);
714
715 if (public_rootkey == NULL || key_area == NULL || area_info == NULL) {
716 return ERRCODE_BOOT_VERIFY_PARAM_NULL;
717 }
718
719 ret = verify_params_key_area(key_area, public_rootkey->root_key_area);
720 if (ret != ERRCODE_SUCC) {
721 boot_msg0("verify_params_key_area fail!");
722 return ret;
723 }
724
725 ret = verify_params_area_info(area_info, key_area->params_ext_key_area);
726 if (ret != ERRCODE_SUCC) {
727 boot_msg0("verify_params_area_info fail!");
728 return ret;
729 }
730
731 return ret;
732 }
733
verify_params_body(uint32_t params_head_addr,uint32_t params_body_addr)734 errcode_t verify_params_body(uint32_t params_head_addr, uint32_t params_body_addr)
735 {
736 uint32_t ret = 0;
737 const params_area_info_t *area_info =
738 (params_area_info_t *)(uintptr_t)(params_head_addr + KEY_AREA_STRUCTURE_LENGTH);
739 const uint8_t *area_addr = (uint8_t *)(uintptr_t)params_body_addr;
740
741 if (area_info == NULL || area_addr == NULL) {
742 return ERRCODE_BOOT_VERIFY_PARAM_NULL;
743 }
744
745 ret = verify_params_area(area_info, area_addr);
746 if (ret != ERRCODE_SUCC) {
747 boot_msg0("verify_params_area fail!");
748 return ret;
749 }
750
751 return ret;
752 }